Skip to content

Commit 7755142

Browse files
committed
merge origin/dev into the lane I3 bottom link [skip ci]
2 parents 0b89a77 + eb81eaa commit 7755142

4 files changed

Lines changed: 62 additions & 4 deletions

File tree

‎src/chat/inbound.ts‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,35 @@ function contentToText(content: unknown): string {
4646
}
4747

4848
function imageUrlFromPart(part: Rec): string | null {
49-
if (part.type !== "image_url") return null;
50-
const imageUrl = part.image_url;
51-
if (typeof imageUrl === "string" && imageUrl.length > 0) return imageUrl;
52-
if (isRec(imageUrl) && typeof imageUrl.url === "string" && imageUrl.url.length > 0) return imageUrl.url;
49+
if (part.type === "image_url") {
50+
const imageUrl = part.image_url;
51+
if (typeof imageUrl === "string" && imageUrl.length > 0) return imageUrl;
52+
if (isRec(imageUrl) && typeof imageUrl.url === "string" && imageUrl.url.length > 0) return imageUrl.url;
53+
return null;
54+
}
55+
// Agent clients whose native wire shape is not OpenAI's still send images over
56+
// Chat Completions: Pi/MCP-style parts carry {type:"image", data, mimeType}
57+
// (Aside read_file tool results), Anthropic-shaped clients carry a source
58+
// object. Dropping either silently blinds a vision model, so normalize both
59+
// to the URL/data-URI form the Responses pipeline already understands.
60+
if (part.type === "image") {
61+
const data = part.data;
62+
if (typeof data === "string" && data.length > 0) {
63+
if (data.startsWith("data:")) return data;
64+
const media = typeof part.mimeType === "string" && part.mimeType.length > 0 ? part.mimeType
65+
: typeof part.mediaType === "string" && part.mediaType.length > 0 ? part.mediaType
66+
: "image/png";
67+
return "data:" + media + ";base64," + data;
68+
}
69+
const source = part.source;
70+
if (isRec(source)) {
71+
if (source.type === "base64" && typeof source.data === "string" && source.data.length > 0) {
72+
const media = typeof source.media_type === "string" && source.media_type.length > 0 ? source.media_type : "image/png";
73+
return "data:" + media + ";base64," + source.data;
74+
}
75+
if (source.type === "url" && typeof source.url === "string" && source.url.length > 0) return source.url;
76+
}
77+
}
5378
return null;
5479
}
5580

‎src/server/index.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,13 @@ export function attachLiveSidebandUpstream(
790790
return;
791791
}
792792
ws.data.liveOpened = true;
793+
// The upstream opened before this socket existed, so the "open" listener
794+
// below can never fire for it. Disarm the connect watchdog exactly as that
795+
// listener would, or every session with a max lifetime is force-closed ten
796+
// seconds after attach. The session timer stays armed: it bounds the whole
797+
// session, not the connect phase.
798+
if (ws.data.liveConnectTimer !== undefined) clearTimeout(ws.data.liveConnectTimer);
799+
ws.data.liveConnectTimer = undefined;
793800
for (const frame of takeover.frames) {
794801
try {
795802
// Mirror the live message listener exactly: same ceiling, same diagnostic

‎tests/responses/chat-completions-endpoint.test.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,11 @@ describe("chatCompletionsToResponsesBody image parts", () => {
242242
{ part: { type: "image_url", image_url: "https://example.com/image.png" }, expected: { type: "input_image", image_url: "https://example.com/image.png" } },
243243
{ part: { type: "image_url", image_url: "https://example.com/image.png", detail: "low" }, expected: { type: "input_image", image_url: "https://example.com/image.png", detail: "low" } },
244244
{ part: { type: "image_url", image_url: { url: "https://example.com/image.png" } }, expected: { type: "input_image", image_url: "https://example.com/image.png" } },
245+
{ part: { type: "image", data: "aGVsbG8=", mimeType: "image/png" }, expected: { type: "input_image", image_url: "data:image/png;base64,aGVsbG8=" } },
246+
{ part: { type: "image", data: "aGVsbG8=", mediaType: "image/jpeg" }, expected: { type: "input_image", image_url: "data:image/jpeg;base64,aGVsbG8=" } },
247+
{ part: { type: "image", data: "data:image/webp;base64,aGVsbG8=", mimeType: "image/png" }, expected: { type: "input_image", image_url: "data:image/webp;base64,aGVsbG8=" } },
248+
{ part: { type: "image", source: { type: "base64", media_type: "image/jpeg", data: "aGVsbG8=" } }, expected: { type: "input_image", image_url: "data:image/jpeg;base64,aGVsbG8=" } },
249+
{ part: { type: "image", source: { type: "url", url: "https://example.com/claude.png" } }, expected: { type: "input_image", image_url: "https://example.com/claude.png" } },
245250
])("preserves user image shorthand and omitted detail: %j", ({ part, expected }) => {
246251
const body = chatCompletionsToResponsesBody({ model: "mock/test-model", messages: [{ role: "user", content: [part] }] });
247252
expect(body.input).toEqual([{ type: "message", role: "user", content: [expected] }]);

‎tests/server/server-live.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,27 @@ describe("attachLiveSidebandUpstream ownership", () => {
18761876
expect(upstream.readyState).toBe(WebSocket.CLOSED);
18771877
expect(client.releases()).toBe(1);
18781878
});
1879+
1880+
test("disarms the connect watchdog when the upstream was pre-opened", () => {
1881+
const upstream = new FakeUpstreamSocket();
1882+
upstream.readyState = WebSocket.OPEN;
1883+
const client = fakeSidebandClient(upstream, {
1884+
failure: () => undefined,
1885+
take: () => ({ ok: true, frames: ["session.created"] }),
1886+
});
1887+
client.ws.data.liveMaxSessionMs = 60_000;
1888+
1889+
attachLiveSidebandUpstream(client.ws as never);
1890+
1891+
// The pre-opened upstream's "open" event fired before attach, so the
1892+
// listener that would clear the connect timer can never run. The timer
1893+
// must be disarmed on the takeover path instead; the session timer stays
1894+
// armed because it bounds the whole session.
1895+
expect(client.ws.data.liveConnectTimer).toBeUndefined();
1896+
expect(client.ws.data.liveSessionTimer).toBeDefined();
1897+
upstream.emit("close", { code: 1000 });
1898+
expect(client.releases()).toBe(1);
1899+
});
18791900
});
18801901

18811902
describe("openLiveSidebandUpstream", () => {

0 commit comments

Comments
 (0)