Files
openclaw/extensions/xai/src/responses-tool-shared.ts
Vincent Koc 65842aabad refactor(providers): share google and xai provider helpers (#60722)
* refactor(google): share oauth token helpers

* refactor(xai): share tool auth fallback helpers

* refactor(xai): share tool auth resolution

* refactor(xai): share tool config helpers

* refactor(xai): share fallback auth helpers

* refactor(xai): share responses tool helpers

* refactor(google): share http request config helper

* fix(xai): re-export shared web search extractor

* fix(xai): import plugin config type

* fix(providers): preserve default google network guard
2026-04-04 16:14:15 +09:00

74 lines
2.3 KiB
TypeScript

import type { XaiWebSearchResponse } from "./web-search-shared.js";
export const XAI_RESPONSES_ENDPOINT = "https://api.x.ai/v1/responses";
export function buildXaiResponsesToolBody(params: {
model: string;
inputText: string;
tools: Array<Record<string, unknown>>;
maxTurns?: number;
}): Record<string, unknown> {
return {
model: params.model,
input: [{ role: "user", content: params.inputText }],
tools: params.tools,
...(params.maxTurns ? { max_turns: params.maxTurns } : {}),
};
}
export function extractXaiWebSearchContent(data: XaiWebSearchResponse): {
text: string | undefined;
annotationCitations: string[];
} {
for (const output of data.output ?? []) {
if (output.type === "message") {
for (const block of output.content ?? []) {
if (block.type === "output_text" && typeof block.text === "string" && block.text) {
const urls = (block.annotations ?? [])
.filter(
(annotation) =>
annotation.type === "url_citation" && typeof annotation.url === "string",
)
.map((annotation) => annotation.url as string);
return { text: block.text, annotationCitations: [...new Set(urls)] };
}
}
}
if (output.type === "output_text" && typeof output.text === "string" && output.text) {
const urls = (output.annotations ?? [])
.filter(
(annotation) => annotation.type === "url_citation" && typeof annotation.url === "string",
)
.map((annotation) => annotation.url as string);
return { text: output.text, annotationCitations: [...new Set(urls)] };
}
}
return {
text: typeof data.output_text === "string" ? data.output_text : undefined,
annotationCitations: [],
};
}
export function resolveXaiResponseTextAndCitations(data: XaiWebSearchResponse): {
content: string;
citations: string[];
} {
const { text, annotationCitations } = extractXaiWebSearchContent(data);
return {
content: text ?? "No response",
citations:
Array.isArray(data.citations) && data.citations.length > 0
? data.citations
: annotationCitations,
};
}
export const __testing = {
buildXaiResponsesToolBody,
extractXaiWebSearchContent,
resolveXaiResponseTextAndCitations,
XAI_RESPONSES_ENDPOINT,
} as const;