Skip to content

Commit 8a5cfa4

Browse files
authored
fix(xai): preserve all response text blocks and citations (#118477)
1 parent 59cd041 commit 8a5cfa4

3 files changed

Lines changed: 117 additions & 21 deletions

File tree

‎extensions/xai/code-execution.test.ts‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,60 @@ describe("xai code_execution tool", () => {
165165
).toBe(true);
166166
});
167167

168+
it("returns every response answer and citation from the code execution HTTP boundary", async () => {
169+
const mockFetch = installCodeExecutionFetch({
170+
output: [
171+
{ type: "code_interpreter_call" },
172+
{
173+
type: "message",
174+
content: [
175+
{
176+
type: "output_text",
177+
text: "Mean: ",
178+
annotations: [{ type: "url_citation", url: "https://example.com/input.csv" }],
179+
},
180+
{
181+
type: "output_text",
182+
text: "42",
183+
annotations: [
184+
{ type: "url_citation", url: "https://example.com/result.csv" },
185+
{ type: "url_citation", url: "https://example.com/input.csv" },
186+
],
187+
},
188+
],
189+
},
190+
{
191+
type: "message",
192+
content: [{ type: "output_text", text: ". Verified." }],
193+
},
194+
],
195+
});
196+
const tool = createCodeExecutionTool({
197+
config: {
198+
plugins: {
199+
entries: {
200+
xai: {
201+
config: {
202+
webSearch: { apiKey: "xai-plugin-key" }, // pragma: allowlist secret
203+
},
204+
},
205+
},
206+
},
207+
},
208+
});
209+
210+
const result = await tool?.execute?.("code-execution:multi-block", {
211+
task: "Calculate and verify the mean.",
212+
});
213+
214+
expect(firstFetchUrl(mockFetch)).toContain("api.x.ai/v1/responses");
215+
expect(result?.details).toMatchObject({
216+
content: "Mean: 42. Verified.",
217+
citations: ["https://example.com/input.csv", "https://example.com/result.csv"],
218+
usedCodeExecution: true,
219+
});
220+
});
221+
168222
it("reuses the xAI plugin web search key for code_execution requests", async () => {
169223
const mockFetch = installCodeExecutionFetch();
170224
const tool = createCodeExecutionTool({

‎extensions/xai/src/responses-tool-shared.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,49 @@ describe("xai responses tool helpers", () => {
6767
});
6868
});
6969

70+
it("collects every response text block and deduplicates citations across output items", () => {
71+
expect(
72+
requireXaiResponseTextAndCitations(
73+
{
74+
output: [
75+
{ type: "web_search_call" },
76+
{
77+
type: "message",
78+
content: [
79+
{
80+
type: "output_text",
81+
text: "First ",
82+
annotations: [{ type: "url_citation", url: "https://example.com/a" }],
83+
},
84+
{
85+
type: "output_text",
86+
text: "second",
87+
annotations: [
88+
{ type: "url_citation", url: "https://example.com/b" },
89+
{ type: "url_citation", url: "https://example.com/a" },
90+
],
91+
},
92+
],
93+
},
94+
{
95+
type: "output_text",
96+
text: " and ",
97+
annotations: [{ type: "url_citation", url: "https://example.com/c" }],
98+
},
99+
{
100+
type: "message",
101+
content: [{ type: "output_text", text: "third" }],
102+
},
103+
],
104+
},
105+
"xAI tool failed",
106+
),
107+
).toEqual({
108+
content: "First second and third",
109+
citations: ["https://example.com/a", "https://example.com/b", "https://example.com/c"],
110+
});
111+
});
112+
70113
it("ignores malformed output, content, and annotation entries", () => {
71114
expect(
72115
extractXaiWebSearchContent({

‎extensions/xai/src/responses-tool-shared.ts‎

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
// Xai plugin module implements responses tool shared behavior.
22
import {
3+
isRecord,
34
normalizeOptionalString as trimString,
45
uniqueStrings,
56
} from "openclaw/plugin-sdk/string-coerce-runtime";
67
import type { XaiWebSearchResponse } from "./web-search-response.types.js";
78

8-
function isRecord(value: unknown): value is Record<string, unknown> {
9-
return value !== null && typeof value === "object";
10-
}
11-
129
function extractUrlCitations(annotations: unknown): string[] {
1310
if (!Array.isArray(annotations)) {
1411
return [];
@@ -51,32 +48,34 @@ export function extractXaiWebSearchContent(data: XaiWebSearchResponse): {
5148
text: string | undefined;
5249
annotationCitations: string[];
5350
} {
51+
const textParts: string[] = [];
52+
const annotationCitations: string[] = [];
5453
for (const output of data.output ?? []) {
5554
if (!isRecord(output)) {
5655
continue;
5756
}
58-
if (output.type === "message") {
59-
const content = Array.isArray(output.content) ? output.content : [];
60-
for (const block of content) {
61-
if (!isRecord(block)) {
62-
continue;
63-
}
64-
if (block.type === "output_text" && typeof block.text === "string" && block.text) {
65-
const urls = extractUrlCitations(block.annotations);
66-
return { text: block.text, annotationCitations: uniqueStrings(urls) };
67-
}
57+
const blocks =
58+
output.type === "message" && Array.isArray(output.content)
59+
? output.content
60+
: output.type === "output_text"
61+
? [output]
62+
: [];
63+
for (const block of blocks) {
64+
if (!isRecord(block) || block.type !== "output_text" || typeof block.text !== "string") {
65+
continue;
66+
}
67+
if (block.text) {
68+
textParts.push(block.text);
69+
annotationCitations.push(...extractUrlCitations(block.annotations));
6870
}
69-
}
70-
71-
if (output.type === "output_text" && typeof output.text === "string" && output.text) {
72-
const urls = extractUrlCitations(output.annotations);
73-
return { text: output.text, annotationCitations: uniqueStrings(urls) };
7471
}
7572
}
7673

74+
// Match the Responses SDK: adjacent output text blocks have no separator.
75+
const text = textParts.join("");
7776
return {
78-
text: typeof data.output_text === "string" ? data.output_text : undefined,
79-
annotationCitations: [],
77+
text: text || (typeof data.output_text === "string" ? data.output_text : undefined),
78+
annotationCitations: uniqueStrings(annotationCitations),
8079
};
8180
}
8281

0 commit comments

Comments
 (0)