fix(web-search): handle xAI Responses API format in Grok provider

The xAI /v1/responses API returns content in a structured format with
typed output blocks (type: 'message') containing typed content blocks
(type: 'output_text') and url_citation annotations. The previous code
only checked output[0].content[0].text without filtering by type,
which could miss content in responses with multiple output entries.

Changes:
- Update GrokSearchResponse type to include annotations on content blocks
- Filter output blocks by type='message' and content by type='output_text'
- Extract url_citation annotations as fallback citations when top-level
  citations array is empty
- Deduplicate annotation-derived citation URLs
- Update tests for the new structured return type

Closes #13520
This commit is contained in:
Rain
2026-02-11 01:57:22 +08:00
committed by Peter Steinberger
parent a36b9be245
commit 27453f5a31
2 changed files with 89 additions and 21 deletions

View File

@@ -109,6 +109,12 @@ type GrokSearchResponse = {
content?: Array<{
type?: string;
text?: string;
annotations?: Array<{
type?: string;
url?: string;
start_index?: number;
end_index?: number;
}>;
}>;
}>;
output_text?: string; // deprecated field - kept for backwards compatibility
@@ -131,13 +137,28 @@ type PerplexitySearchResponse = {
type PerplexityBaseUrlHint = "direct" | "openrouter";
function extractGrokContent(data: GrokSearchResponse): string | undefined {
// xAI Responses API format: output[0].content[0].text
const fromResponses = data.output?.[0]?.content?.[0]?.text;
if (typeof fromResponses === "string" && fromResponses) {
return fromResponses;
function extractGrokContent(data: GrokSearchResponse): {
text: string | undefined;
annotationCitations: string[];
} {
// xAI Responses API format: find the message output with text content
for (const output of data.output ?? []) {
if (output.type !== "message") {
continue;
}
for (const block of output.content ?? []) {
if (block.type === "output_text" && typeof block.text === "string" && block.text) {
// Extract url_citation annotations from this content block
const urls = (block.annotations ?? [])
.filter((a) => a.type === "url_citation" && typeof a.url === "string")
.map((a) => a.url as string);
return { text: block.text, annotationCitations: [...new Set(urls)] };
}
}
}
return typeof data.output_text === "string" ? data.output_text : undefined;
// Fallback: deprecated output_text field
const text = typeof data.output_text === "string" ? data.output_text : undefined;
return { text, annotationCitations: [] };
}
function resolveSearchConfig(cfg?: OpenClawConfig): WebSearchConfig {
@@ -494,8 +515,10 @@ async function runGrokSearch(params: {
}
const data = (await res.json()) as GrokSearchResponse;
const content = extractGrokContent(data) ?? "No response";
const citations = data.citations ?? [];
const { text: extractedText, annotationCitations } = extractGrokContent(data);
const content = extractedText ?? "No response";
// Prefer top-level citations; fall back to annotation-derived ones
const citations = (data.citations ?? []).length > 0 ? data.citations! : annotationCitations;
const inlineCitations = data.inline_citations;
return { content, citations, inlineCitations };