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
6 changes: 5 additions & 1 deletion src/agent/opencode-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,11 @@ export function createOpencodeV2Setup(options: OpencodeV2SetupOptions = {}): (ct

try {
const wsReg = await ctx.session?.hook?.("experimental.ws.handshake", async (e) => {
if (pluginDisabled() || e.model?.providerID !== "openai" || typeof e.url !== "string" || !/^wss?:\/\//.test(e.url)) return;
// #2359: azure speaks the same OpenAI Responses WebSocket vocabulary;
// the egress filter in responses-ws keeps its handshake under Azure's
// custom-header cap. Any other provider stays unintercepted.
const providerID = e.model?.providerID;
if (pluginDisabled() || (providerID !== "openai" && providerID !== "azure") || typeof e.url !== "string" || !/^wss?:\/\//.test(e.url)) return;
const httpUrl = e.url.replace(/^ws/, "http");
if (!new URL(httpUrl).pathname.endsWith("/responses")) return;
const requestEvent: V2HttpRequestEvent = { sessionID: e.sessionID, agent: e.agent, model: e.model, request: new Request(httpUrl, { headers: e.headers }) };
Expand Down
29 changes: 27 additions & 2 deletions src/responses-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,32 @@ function describeEventType(frame: unknown): string {
return typeof frame.type === "string" ? frame.type : "<missing type>";
}

// #2359: Azure OpenAI caps custom request headers on the WS upgrade (documented
// 10). Bili's forwarded x-bili-* metadata (already consumed at ingress) plus
// OpenCode's own x-* headers push a typical handshake past it, so the gateway
// closes with 1011 before any event. Strip bili-internal headers at final egress
// to *.openai.azure.com ONLY — auth + OpenCode headers stay intact, and every
// other host keeps its existing forwarding/chaining behavior byte-for-byte.
function isAzureOpenAiHost(url: string): boolean {
try {
return new URL(url).hostname.toLowerCase().endsWith(".openai.azure.com");
} catch {
return false;
}
}

/** Final wire header set for an upstream Responses WebSocket upgrade: drop
* hop-by-hop + sec-websocket- markers, then (azure only) the consumed x-bili-*
* metadata. Exported so the exact egress decision — including the #2359 azure
* custom-header-cap strip — is asserted directly rather than via a permissive
* round-trip mock. */
export function finalizeResponsesWsUpstreamHeaders(url: string, rawHeaders: FetchOptions["headers"]): Record<string, string> {
const headers = Object.fromEntries(new Headers(rawHeaders).entries());
for (const key of Object.keys(headers)) if (UPSTREAM_HOP_HEADERS.has(key) || key.startsWith("sec-websocket-")) delete headers[key];
if (isAzureOpenAiHost(url)) for (const key of Object.keys(headers)) if (key.startsWith("x-bili-")) delete headers[key];
return headers;
}

// Transport options the WebSocket lane cannot honor. The lane forces stream:true
// and owns streaming itself, so HTTP-side transport controls are refused — but
// only the ones genuinely incompatible with a single WS exchange. stream_options
Expand Down Expand Up @@ -168,8 +194,7 @@ export class ResponsesWsUpstream {
}

private async connect(url: string, options: FetchOptions): Promise<InstanceType<typeof UpstreamWebSocket>> {
const headers = Object.fromEntries(new Headers(options.headers).entries());
for (const key of Object.keys(headers)) if (UPSTREAM_HOP_HEADERS.has(key) || key.startsWith("sec-websocket-")) delete headers[key];
const headers = finalizeResponsesWsUpstreamHeaders(url, options.headers);
const wsUrl = url.replace(/^http/, "ws");
const key = `${wsUrl}:${canonical(headers)}`;
if (this.key === key && this.socket?.readyState === UpstreamWebSocket.OPEN) return this.socket;
Expand Down
54 changes: 53 additions & 1 deletion tests/responses-websocket.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import WebSocket, { WebSocketServer } from "ws";
import { Agent } from "undici";
import { defaultConfig } from "acp-kernel";
import { startServer } from "../src/server.ts";
import { ResponsesWsHistory, ResponsesWsUpstream } from "../src/responses-ws.ts";
import { ResponsesWsHistory, ResponsesWsUpstream, finalizeResponsesWsUpstreamHeaders } from "../src/responses-ws.ts";
import { _liveUpstreamTimersForTest } from "../src/fetch-util.ts";
import { proxyDispatcher } from "../src/upstream-proxy.ts";
import { ensureRootCA, mintHostCert, rootCaPath } from "../src/ca.ts";
Expand Down Expand Up @@ -67,6 +67,56 @@ test("V2 handshake: routes OAuth/API Responses sockets and stamps native identit
} finally { cleanup(); }
});

test("Responses WS egress headers: azure strips consumed x-bili-* under the custom-header cap; other hosts unchanged", () => {
const raw = {
host: "westus2.openai.azure.com",
connection: "Upgrade",
"content-length": "128",
"content-type": "application/json",
"sec-websocket-key": "dGhlIHNhbXBsZSBub25jZQ==",
"sec-websocket-version": "13",
"api-key": "sk-azure-test-credential",
"x-openai-organization": "org-test",
"x-stainless-runtime": "node",
"x-bili-plugin": "opencode",
"x-bili-plugin-conversation": "ses_az",
"x-bili-plugin-model": "gpt-5.2",
"x-bili-plugin-context-window": "200000",
};
const azure = finalizeResponsesWsUpstreamHeaders("https://westus2.openai.azure.com/openai/v1/responses?api-version=2024-08-01-preview", raw);
assert.equal(azure["api-key"], "sk-azure-test-credential");
assert.equal(azure["x-openai-organization"], "org-test");
assert.equal(azure["content-type"], "application/json");
for (const dropped of ["host", "connection", "content-length", "sec-websocket-key", "sec-websocket-version", "x-bili-plugin", "x-bili-plugin-conversation", "x-bili-plugin-model", "x-bili-plugin-context-window"]) assert.ok(!(dropped in azure), `azure egress must not carry ${dropped}`);
const openai = finalizeResponsesWsUpstreamHeaders("https://api.openai.com/v1/responses", raw);
assert.equal(openai["x-bili-plugin"], "opencode");
assert.equal(openai["x-bili-plugin-conversation"], "ses_az");
assert.equal(openai["x-bili-plugin-model"], "gpt-5.2");
assert.equal(openai["api-key"], "sk-azure-test-credential");
for (const dropped of ["host", "connection", "content-length", "sec-websocket-key"]) assert.ok(!(dropped in openai), `non-azure egress must still drop hop marker ${dropped}`);
});

test("V2 handshake: azure Responses sockets are intercepted; uncovered providers stay unintercepted", async () => {
const hooks = new Map<string, (event: V2HttpRequestEvent) => void | Promise<void>>();
const origin = "http://127.0.0.1:8787";
const cleanup = await createOpencodeV2Setup({ route: createNativeRoute({ origin, ready: Promise.resolve(origin) }, { probe: async () => true }) })({
session: { hook: async (name, cb) => { hooks.set(name, cb); return {}; } },
});
try {
const azureUrl = "wss://myres.openai.azure.com/openai/v1/responses?api-version=2024-08-01-preview";
const azureEvent: V2HttpRequestEvent = { url: azureUrl, headers: { "api-key": "sk-azure-test" }, sessionID: "ses_az", agent: "build", model: { providerID: "azure", id: "gpt-5.2" } };
await hooks.get("experimental.ws.handshake")!(azureEvent);
assert.equal(azureEvent.url, `ws://127.0.0.1:8787/bili/responses/${azureUrl.replace(/^ws/, "http")}`);
assert.equal(azureEvent.headers?.["x-bili-plugin"], "opencode");
assert.equal(azureEvent.headers?.["x-bili-plugin-conversation"], "ses_az");
const otherUrl = "wss://api.anthropic.com/v1/messages";
const otherEvent: V2HttpRequestEvent = { url: otherUrl, headers: {}, sessionID: "ses_other", agent: "build", model: { providerID: "anthropic", id: "claude-x" } };
await hooks.get("experimental.ws.handshake")!(otherEvent);
assert.equal(otherEvent.url, otherUrl);
assert.ok(otherEvent.headers?.["x-bili-plugin"] === undefined);
} finally { cleanup(); }
});

async function fixture(terminalOutput: "full" | "empty" | "omitted" | "partial" | "suffix" = "full") {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "bili-responses-ws-"));
_setStoreForTest(new SessionStore({ enabled: false }));
Expand Down Expand Up @@ -216,6 +266,8 @@ test("Responses WS: both ends use sockets; incremental tool continuation reaches
assert.equal((f.rows[1].request.input as Item[]).length, 2);
assert.ok(JSON.stringify(f.rows[1].full).includes("first-round-sentinel"));
assert.equal(f.rows[0].headers.authorization, "Bearer fake-credential");
// #2359 control: a non-azure upstream still receives the consumed x-bili-* metadata verbatim — only *.openai.azure.com egress strips it.
assert.equal(f.rows[0].headers["x-bili-plugin"], "opencode");
assert.equal(second.status, "completed");
const session = peekSession(f.sid);
assert.ok(session);
Expand Down
Loading