Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/adapters/openai-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,11 @@ const MUSE_SPARK_WEB_SEARCH_STRICT_MODELS = new Set([
"muse-spark-1.2-contributor",
]);

const MUSE_SPARK_WEB_SEARCH_STRICT_RESPONSE_URLS = new Set([
"https://opencode.ai/zen/v1/responses",
"https://opencode.ai/zen/go/v1/responses",
]);

const MUSE_SPARK_UNSUPPORTED_WEB_SEARCH_FIELDS = [
"search_content_types",
"indexed_web_access",
Expand All @@ -2124,13 +2129,28 @@ const MUSE_SPARK_UNSUPPORTED_WEB_SEARCH_FIELDS = [
/**
* OpenCode Zen / Go Muse Spark Responses gateway refuses a short list of Codex
* `web_search` fields. `web_search_preview` keeps its accepted shape, and Luna
* remains untouched. Keep the rejected names together so a newly identified field
* is a one-line compatibility update rather than another bespoke rewrite.
* remains untouched. Match the exact effective request URL; malformed, credentialed,
* or parameterized destinations keep their original body instead of assuming this
* gateway contract. Keep the rejected names together so a newly identified field is
* a one-line compatibility update rather than another bespoke rewrite.
*/
function stripMuseSparkUnsupportedWebSearchFields(body: unknown, modelId: unknown): unknown {
function stripMuseSparkUnsupportedWebSearchFields(
body: unknown,
modelId: unknown,
responseUrl: string,
): unknown {
if (!isPlainObject(body)) return body;
if (typeof modelId !== "string") return body;
if (!MUSE_SPARK_WEB_SEARCH_STRICT_MODELS.has(modelId.trim().toLowerCase())) return body;
let destination: string;
try {
const url = new URL(responseUrl);
if (url.username || url.password || url.search || url.hash) return body;
destination = `${url.origin.toLowerCase()}${url.pathname.replace(/\/+$/, "")}`;
} catch {
return body;
}
if (!MUSE_SPARK_WEB_SEARCH_STRICT_RESPONSE_URLS.has(destination)) return body;

const rewriteTools = (tools: unknown[]): { tools: unknown[]; changed: boolean } => {
let changed = false;
Expand Down Expand Up @@ -2409,7 +2429,7 @@ export function createResponsesPassthroughAdapter(provider: OcxProviderConfig):
if (provider.supportsOpenAiWebSearchToolFields === false) {
outBody = stripOpenAiOnlyWebSearchFields(outBody);
}
outBody = stripMuseSparkUnsupportedWebSearchFields(outBody, parsed.modelId);
outBody = stripMuseSparkUnsupportedWebSearchFields(outBody, parsed.modelId, url);
// Last, so promoted namespace children are also cleared of Codex-private fields.
outBody = stripCanonicalOnlyToolFields(outBody, provider.supportsOpenAiWebSearchToolFields === false);
}
Expand Down
72 changes: 69 additions & 3 deletions tests/muse-spark-web-search-compat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,34 @@ import { withTestTranslatorBudget } from "./helpers/translator-budget";
const createResponsesPassthroughAdapter = (...args: Parameters<typeof createResponsesPassthroughAdapterProduction>) =>
withTestTranslatorBudget(createResponsesPassthroughAdapterProduction(...args));

const PROVIDER = {
const ZEN_PROVIDER = {
adapter: "openai-responses",
baseUrl: "https://opencode.ai/zen/v1",
apiKey: "test-key",
} as unknown as OcxProviderConfig;

const ZEN_GO_PROVIDER = {
...ZEN_PROVIDER,
baseUrl: "https://opencode.ai/zen/go/v1",
};

const ZEN_PATH_PROVIDER = {
...ZEN_PROVIDER,
baseUrl: "https://opencode.ai",
responsesPath: "/zen/v1/responses",
};

const ZEN_GO_PATH_PROVIDER = {
...ZEN_PROVIDER,
baseUrl: "https://opencode.ai",
responsesPath: "/zen/go/v1/responses",
};

const META_PROVIDER = {
...ZEN_PROVIDER,
baseUrl: "https://api.meta.ai/v1",
};

/** A Codex web_search declaration exactly as `hosted_spec.rs` emits it for TextAndImage. */
function webSearchTool(): Record<string, unknown> {
return {
Expand All @@ -23,8 +45,13 @@ function webSearchTool(): Record<string, unknown> {
};
}

function build(modelId: string, rawBody: Record<string, unknown>): Record<string, unknown> {
const request = createResponsesPassthroughAdapter(PROVIDER).buildRequest({
/** Build one passthrough request for an explicit Responses provider fixture. */
function buildForProvider(
provider: OcxProviderConfig,
modelId: string,
rawBody: Record<string, unknown>,
): Record<string, unknown> {
const request = createResponsesPassthroughAdapter(provider).buildRequest({
modelId,
context: { messages: [] },
stream: true,
Expand All @@ -34,6 +61,11 @@ function build(modelId: string, rawBody: Record<string, unknown>): Record<string
return JSON.parse(request.body) as Record<string, unknown>;
}

/** Build with the default OpenCode Zen fixture used by the original regressions. */
function build(modelId: string, rawBody: Record<string, unknown>): Record<string, unknown> {
return buildForProvider(ZEN_PROVIDER, modelId, rawBody);
}

const toolsOf = (body: Record<string, unknown>) => body.tools as Array<Record<string, unknown>>;

/**
Expand Down Expand Up @@ -125,4 +157,38 @@ describe("#2617/#3378 Muse Spark web_search compatibility", () => {
expect(Object.hasOwn(nested, "search_content_types")).toBe(false);
expect(Object.hasOwn(nested, "indexed_web_access")).toBe(false);
});

test("OpenCode Go applies the same Muse compatibility guard", () => {
const body = buildForProvider(ZEN_GO_PROVIDER, "muse-spark-1.3-contributor", {
tools: [webSearchTool()],
});
const tool = toolsOf(body)[0]!;
expect(Object.hasOwn(tool, "search_content_types")).toBe(false);
expect(Object.hasOwn(tool, "indexed_web_access")).toBe(false);
});

test("split baseUrl and responsesPath configurations derive both strict destinations", () => {
for (const provider of [ZEN_PATH_PROVIDER, ZEN_GO_PATH_PROVIDER]) {
const body = buildForProvider(provider, "muse-spark-1.3-contributor", {
tools: [webSearchTool()],
});
const tool = toolsOf(body)[0]!;
expect(Object.hasOwn(tool, "search_content_types")).toBe(false);
expect(Object.hasOwn(tool, "indexed_web_access")).toBe(false);
}
});

test("direct Meta preserves its web_search fields at both tool positions", () => {
const body = buildForProvider(META_PROVIDER, "muse-spark-1.3-contributor", {
tools: [webSearchTool()],
input: [{ type: "additional_tools", tools: [webSearchTool()] }],
});
const tool = toolsOf(body)[0]!;
const item = (body.input as Array<Record<string, unknown>>)[0]!;
const nested = (item.tools as Array<Record<string, unknown>>)[0]!;
for (const declaration of [tool, nested]) {
expect(declaration.search_content_types).toEqual(["text", "image"]);
expect(declaration.indexed_web_access).toBe(true);
}
});
});
Loading