Skip to content

Commit 68ba1ef

Browse files
authored
refactor(xai): share responses tool request transport (#128571)
* refactor(xai): share responses tool request transport * refactor(xai): remove unused responses body test seam * test(xai): preserve responses request contracts at fetch boundaries
1 parent 6530948 commit 68ba1ef

8 files changed

Lines changed: 71 additions & 114 deletions

‎config/assertion-safety-baseline.txt‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,12 +1457,11 @@ extensions/xai/realtime-voice-config.ts 2
14571457
extensions/xai/runtime-model-compat.ts 1
14581458
extensions/xai/speech-provider-metadata.ts 3
14591459
extensions/xai/src/code-execution-config.ts 10
1460-
extensions/xai/src/code-execution-shared.ts 1
14611460
extensions/xai/src/tool-auth-shared.ts 2
14621461
extensions/xai/src/web-search-provider.runtime.ts 2
1463-
extensions/xai/src/web-search-shared.ts 2
1462+
extensions/xai/src/web-search-shared.ts 1
14641463
extensions/xai/src/x-search-config.ts 4
1465-
extensions/xai/src/x-search-shared.ts 2
1464+
extensions/xai/src/x-search-shared.ts 1
14661465
extensions/xai/stream.ts 5
14671466
extensions/xai/tts.ts 1
14681467
extensions/xai/x-search.ts 7

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ describe("xai code_execution tool", () => {
219219
});
220220
});
221221

222-
it("reuses the xAI plugin web search key for code_execution requests", async () => {
222+
it("reuses the xAI plugin web search key without overriding custom model reasoning", async () => {
223223
const mockFetch = installCodeExecutionFetch();
224224
const tool = createCodeExecutionTool({
225225
config: {
@@ -230,6 +230,7 @@ describe("xai code_execution tool", () => {
230230
webSearch: {
231231
apiKey: "xai-plugin-key", // pragma: allowlist secret
232232
},
233+
codeExecution: { model: "grok-build-0.1" },
233234
},
234235
},
235236
},
@@ -242,6 +243,14 @@ describe("xai code_execution tool", () => {
242243
});
243244

244245
expect(firstAuthorizationHeader(mockFetch)).toBe("Bearer xai-plugin-key");
246+
const body = parseFirstRequestBody(mockFetch);
247+
expect(body.model).toBe("grok-build-0.1");
248+
expect(body.input).toEqual([
249+
{ role: "user", content: "Compute the standard deviation of [1, 2, 3]" },
250+
]);
251+
expect(body.store).toBe(false);
252+
expect(body).not.toHaveProperty("reasoning");
253+
expect(body).not.toHaveProperty("max_turns");
245254
});
246255

247256
it("reports malformed code_execution JSON as a provider error", async () => {

‎extensions/xai/src/code-execution-shared.ts‎

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
// Xai plugin module implements code execution shared behavior.
2-
import { readProviderJsonObjectResponse } from "openclaw/plugin-sdk/provider-http";
3-
import { postTrustedWebToolsJson } from "openclaw/plugin-sdk/provider-web-search";
42
import { XAI_DEFAULT_MODEL_ID } from "../model-definitions.js";
53
import {
6-
buildXaiResponsesToolBody,
4+
requestXaiResponsesTool,
75
requireXaiResponseTextAndCitations,
86
XAI_RESPONSES_ENDPOINT,
97
} from "./responses-tool-shared.js";
108
import {
119
resolveNormalizedXaiToolModel,
1210
resolvePositiveIntegerToolConfig,
1311
} from "./tool-config-shared.js";
14-
import type { XaiWebSearchResponse } from "./web-search-shared.js";
1512

1613
const XAI_CODE_EXECUTION_ENDPOINT = XAI_RESPONSES_ENDPOINT;
1714
const XAI_DEFAULT_CODE_EXECUTION_MODEL = XAI_DEFAULT_MODEL_ID;
1815

19-
type XaiCodeExecutionResponse = XaiWebSearchResponse & {
20-
output?: Array<{
21-
type?: string;
22-
}>;
23-
};
24-
2516
type XaiCodeExecutionResult = {
2617
content: string;
2718
citations: string[];
@@ -70,25 +61,16 @@ export async function requestXaiCodeExecution(params: {
7061
maxTurns?: number;
7162
task: string;
7263
}): Promise<XaiCodeExecutionResult> {
73-
return await postTrustedWebToolsJson(
64+
return await requestXaiResponsesTool(
7465
{
75-
url: XAI_CODE_EXECUTION_ENDPOINT,
76-
timeoutSeconds: params.timeoutSeconds,
77-
apiKey: params.apiKey,
78-
body: buildXaiResponsesToolBody({
79-
model: params.model,
80-
inputText: params.task,
81-
tools: [{ type: "code_interpreter" }],
82-
maxTurns: params.maxTurns,
83-
reasoningEffort: params.model === XAI_DEFAULT_CODE_EXECUTION_MODEL ? "low" : undefined,
84-
}),
85-
errorLabel: "xAI",
66+
...params,
67+
endpoint: XAI_CODE_EXECUTION_ENDPOINT,
68+
inputText: params.task,
69+
tools: [{ type: "code_interpreter" }],
70+
reasoningEffort: params.model === XAI_DEFAULT_CODE_EXECUTION_MODEL ? "low" : undefined,
71+
errorLabel: "xAI code execution failed",
8672
},
87-
async (response) => {
88-
const data = (await readProviderJsonObjectResponse(
89-
response,
90-
"xAI code execution failed",
91-
)) as XaiCodeExecutionResponse;
73+
(data) => {
9274
const { content, citations } = requireXaiResponseTextAndCitations(
9375
data,
9476
"xAI code execution failed",

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

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,12 @@
11
// Xai tests cover responses tool shared plugin behavior.
22
import { describe, expect, it } from "vitest";
33
import {
4-
buildXaiResponsesToolBody,
54
extractXaiWebSearchContent,
65
requireXaiResponseTextAndCitations,
76
requireXaiResponseTextCitationsAndInline,
87
} from "./responses-tool-shared.js";
98

109
describe("xai responses tool helpers", () => {
11-
it("builds the shared xAI Responses tool body", () => {
12-
expect(
13-
buildXaiResponsesToolBody({
14-
model: "grok-4.3",
15-
inputText: "search for openclaw",
16-
tools: [{ type: "x_search" }],
17-
maxTurns: 2,
18-
reasoningEffort: "none",
19-
}),
20-
).toEqual({
21-
model: "grok-4.3",
22-
input: [{ role: "user", content: "search for openclaw" }],
23-
tools: [{ type: "x_search" }],
24-
store: false,
25-
reasoning: { effort: "none" },
26-
max_turns: 2,
27-
});
28-
});
29-
30-
it("keeps custom model reasoning untouched while disabling response storage", () => {
31-
expect(
32-
buildXaiResponsesToolBody({
33-
model: "grok-build-0.1",
34-
inputText: "run code",
35-
tools: [{ type: "code_interpreter" }],
36-
}),
37-
).toEqual({
38-
model: "grok-build-0.1",
39-
input: [{ role: "user", content: "run code" }],
40-
tools: [{ type: "code_interpreter" }],
41-
store: false,
42-
});
43-
});
44-
4510
it("falls back to annotation citations when the API omits top-level citations", () => {
4611
expect(
4712
requireXaiResponseTextAndCitations(

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// Xai plugin module implements responses tool shared behavior.
2+
import { readProviderJsonObjectResponse } from "openclaw/plugin-sdk/provider-http";
3+
import { postTrustedWebToolsJson } from "openclaw/plugin-sdk/provider-web-search";
24
import { truncateSanitizedExternalContent } from "openclaw/plugin-sdk/security-runtime";
35
import {
46
isRecord,
@@ -55,7 +57,7 @@ export function resolveXaiResponsesEndpoint(baseUrl?: unknown): string {
5557
return `${(trimString(baseUrl) ?? XAI_RESPONSES_BASE_URL).replace(/\/+$/, "")}/responses`;
5658
}
5759

58-
export function buildXaiResponsesToolBody(params: {
60+
function buildXaiResponsesToolBody(params: {
5961
model: string;
6062
inputText: string;
6163
tools: Array<Record<string, unknown>>;
@@ -72,6 +74,30 @@ export function buildXaiResponsesToolBody(params: {
7274
};
7375
}
7476

77+
export async function requestXaiResponsesTool<T>(
78+
params: Parameters<typeof buildXaiResponsesToolBody>[0] & {
79+
apiKey: string;
80+
endpoint: string;
81+
timeoutSeconds: number;
82+
errorLabel: string;
83+
signal?: AbortSignal;
84+
},
85+
parseResponse: (data: XaiWebSearchResponse) => T,
86+
): Promise<T> {
87+
return await postTrustedWebToolsJson(
88+
{
89+
url: params.endpoint,
90+
timeoutSeconds: params.timeoutSeconds,
91+
apiKey: params.apiKey,
92+
...(params.signal ? { signal: params.signal } : {}),
93+
body: buildXaiResponsesToolBody(params),
94+
errorLabel: "xAI",
95+
},
96+
async (response) =>
97+
parseResponse(await readProviderJsonObjectResponse(response, params.errorLabel)),
98+
);
99+
}
100+
75101
export function extractXaiWebSearchContent(
76102
data: XaiWebSearchResponse,
77103
maxContentChars?: number,

‎extensions/xai/src/web-search-shared.ts‎

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
// Xai plugin module implements web search shared behavior.
2-
import { readProviderJsonObjectResponse } from "openclaw/plugin-sdk/provider-http";
3-
import { postTrustedWebToolsJson, wrapWebContent } from "openclaw/plugin-sdk/provider-web-search";
2+
import { wrapWebContent } from "openclaw/plugin-sdk/provider-web-search";
43
import { isRecord } from "openclaw/plugin-sdk/string-coerce-runtime";
54
import { XAI_DEFAULT_MODEL_ID } from "../model-definitions.js";
65
import { normalizeXaiModelId } from "../model-id.js";
76
import {
8-
buildXaiResponsesToolBody,
7+
requestXaiResponsesTool,
98
requireXaiResponseTextCitationsAndInline,
109
resolveXaiResponsesEndpoint,
1110
} from "./responses-tool-shared.js";
@@ -108,32 +107,21 @@ export async function requestXaiWebSearch(params: {
108107
signal?: AbortSignal;
109108
}): Promise<XaiWebSearchResult> {
110109
params.signal?.throwIfAborted();
111-
return await postTrustedWebToolsJson(
110+
return await requestXaiResponsesTool(
112111
{
113-
url: params.endpoint,
114-
timeoutSeconds: params.timeoutSeconds,
115-
apiKey: params.apiKey,
116-
...(params.signal ? { signal: params.signal } : {}),
117-
body: buildXaiResponsesToolBody({
118-
model: params.model,
119-
inputText: params.query,
120-
tools: [{ type: "web_search" }],
121-
reasoningEffort: params.model === XAI_DEFAULT_WEB_SEARCH_MODEL ? "low" : undefined,
122-
}),
123-
errorLabel: "xAI",
112+
...params,
113+
inputText: params.query,
114+
tools: [{ type: "web_search" }],
115+
reasoningEffort: params.model === XAI_DEFAULT_WEB_SEARCH_MODEL ? "low" : undefined,
116+
errorLabel: "xAI web search failed",
124117
},
125-
async (response) => {
126-
const data = (await readProviderJsonObjectResponse(
127-
response,
128-
"xAI web search failed",
129-
)) as XaiWebSearchResponse;
130-
return requireXaiResponseTextCitationsAndInline(
118+
(data) =>
119+
requireXaiResponseTextCitationsAndInline(
131120
data,
132121
"xAI web search failed",
133122
params.inlineCitations,
134123
XAI_WEB_SEARCH_MAX_CONTENT_CHARS,
135-
);
136-
},
124+
),
137125
).catch((error: unknown) => {
138126
if (params.signal?.aborted && error === params.signal.reason) {
139127
throw error;

‎extensions/xai/src/x-search-shared.ts‎

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
// Xai plugin module implements x search shared behavior.
2-
import { readProviderJsonObjectResponse } from "openclaw/plugin-sdk/provider-http";
3-
import { postTrustedWebToolsJson, wrapWebContent } from "openclaw/plugin-sdk/provider-web-search";
2+
import { wrapWebContent } from "openclaw/plugin-sdk/provider-web-search";
43
import { XAI_DEFAULT_MODEL_ID } from "../model-definitions.js";
54
import {
6-
buildXaiResponsesToolBody,
5+
requestXaiResponsesTool,
76
requireXaiResponseTextCitationsAndInline,
87
resolveXaiResponsesEndpoint,
98
} from "./responses-tool-shared.js";
@@ -126,32 +125,20 @@ export async function requestXaiXSearch(params: {
126125
signal?: AbortSignal;
127126
}): Promise<XaiXSearchResult> {
128127
params.signal?.throwIfAborted();
129-
return await postTrustedWebToolsJson(
128+
return await requestXaiResponsesTool(
130129
{
131-
url: params.endpoint,
132-
timeoutSeconds: params.timeoutSeconds,
133-
apiKey: params.apiKey,
134-
...(params.signal ? { signal: params.signal } : {}),
135-
body: buildXaiResponsesToolBody({
136-
model: params.model,
137-
inputText: params.options.query,
138-
tools: [buildXSearchTool(params.options)],
139-
maxTurns: params.maxTurns,
140-
reasoningEffort: params.model === XAI_DEFAULT_X_SEARCH_MODEL ? "none" : undefined,
141-
}),
142-
errorLabel: "xAI",
130+
...params,
131+
inputText: params.options.query,
132+
tools: [buildXSearchTool(params.options)],
133+
reasoningEffort: params.model === XAI_DEFAULT_X_SEARCH_MODEL ? "none" : undefined,
134+
errorLabel: "xAI X search failed",
143135
},
144-
async (response) => {
145-
const data = (await readProviderJsonObjectResponse(
146-
response,
147-
"xAI X search failed",
148-
)) as XaiWebSearchResponse;
149-
return requireXaiResponseTextCitationsAndInline(
136+
(data) =>
137+
requireXaiResponseTextCitationsAndInline(
150138
data,
151139
"xAI X search failed",
152140
params.inlineCitations,
153141
XAI_X_SEARCH_MAX_CONTENT_CHARS,
154-
);
155-
},
142+
),
156143
);
157144
}

‎extensions/xai/x-search.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ describe("xai x_search tool", () => {
315315
expect(firstFetchUrl(mockFetch)).toContain("api.x.ai/v1/responses");
316316
const body = parseFirstRequestBody(mockFetch);
317317
expect(body.model).toBe("grok-4.3");
318+
expect(body.input).toEqual([{ role: "user", content: "dinner recipes" }]);
318319
expect(body.store).toBe(false);
319320
expect(body.reasoning).toEqual({ effort: "none" });
320321
expect(body.max_turns).toBe(2);

0 commit comments

Comments
 (0)