new_pull
This commit is contained in:
@@ -590,6 +590,57 @@ describe("openai-codex streaming", () => {
|
||||
await streamResult.result();
|
||||
});
|
||||
|
||||
it("omits SSE cache affinity when cacheRetention is none", async () => {
|
||||
const token = mockToken();
|
||||
const encoder = new TextEncoder();
|
||||
let capturedHeaders: Headers | undefined;
|
||||
let capturedBody: Record<string, unknown> | null = null;
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async (_input: string | URL, init?: RequestInit) => {
|
||||
capturedHeaders = init?.headers instanceof Headers ? init.headers : undefined;
|
||||
capturedBody = decodeCodexRequestBody(init?.body);
|
||||
return new Response(
|
||||
new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(buildSSEPayload({ status: "completed" })));
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
{ status: 200, headers: { "content-type": "text/event-stream" } },
|
||||
);
|
||||
}),
|
||||
);
|
||||
|
||||
const model: Model<"openai-codex-responses"> = {
|
||||
id: "gpt-5.1-codex",
|
||||
name: "GPT-5.1 Codex",
|
||||
api: "openai-codex-responses",
|
||||
provider: "openai-codex",
|
||||
baseUrl: "https://chatgpt.com/backend-api",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 400000,
|
||||
maxTokens: 128000,
|
||||
};
|
||||
const context: Context = {
|
||||
systemPrompt: "You are a helpful assistant.",
|
||||
messages: [{ role: "user", content: "Say hello", timestamp: Date.now() }],
|
||||
};
|
||||
|
||||
await streamOpenAICodexResponses(model, context, {
|
||||
apiKey: token,
|
||||
cacheRetention: "none",
|
||||
sessionId: "one-off-summary",
|
||||
transport: "sse",
|
||||
}).result();
|
||||
|
||||
expect(capturedHeaders?.has("session-id")).toBe(false);
|
||||
expect(capturedHeaders?.has("x-client-request-id")).toBe(false);
|
||||
expect(capturedBody).not.toHaveProperty("prompt_cache_key");
|
||||
});
|
||||
|
||||
it("clamps prompt_cache_key to OpenAI's 64-character limit", async () => {
|
||||
const token = mockToken();
|
||||
const sessionId = "x".repeat(67);
|
||||
@@ -804,6 +855,75 @@ describe("openai-codex streaming", () => {
|
||||
expect(requestedToolChoice).toBe("required");
|
||||
});
|
||||
|
||||
it("sets Codex strict mode explicitly and honors constrained sampling", async () => {
|
||||
const token = mockToken();
|
||||
const encoder = new TextEncoder();
|
||||
const sse = buildSSEPayload({ status: "completed" });
|
||||
let requestedTools: Array<{ type?: string; name?: string; strict?: boolean | null }> | undefined;
|
||||
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(
|
||||
async () =>
|
||||
new Response(
|
||||
new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode(sse));
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
{ status: 200, headers: { "content-type": "text/event-stream" } },
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const model: Model<"openai-codex-responses"> = {
|
||||
id: "gpt-5.5",
|
||||
name: "GPT-5.5",
|
||||
api: "openai-codex-responses",
|
||||
provider: "openai-codex",
|
||||
baseUrl: "https://chatgpt.com/backend-api",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 400000,
|
||||
maxTokens: 128000,
|
||||
};
|
||||
|
||||
await streamOpenAICodexResponses(
|
||||
model,
|
||||
{
|
||||
messages: [{ role: "user", content: "Use a tool", timestamp: Date.now() }],
|
||||
tools: [
|
||||
{
|
||||
name: "optional",
|
||||
description: "Optional constrained sampling",
|
||||
parameters: Type.Object({ value: Type.String() }),
|
||||
constrainedSampling: false,
|
||||
},
|
||||
{
|
||||
name: "strict",
|
||||
description: "Strict constrained sampling",
|
||||
parameters: Type.Object({ value: Type.String() }, { additionalProperties: false }),
|
||||
constrainedSampling: { type: "json_schema", strict: "prefer" },
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
apiKey: token,
|
||||
transport: "sse",
|
||||
onPayload: (payload) => {
|
||||
requestedTools = (payload as { tools?: typeof requestedTools }).tools;
|
||||
},
|
||||
},
|
||||
).result();
|
||||
|
||||
expect(requestedTools).toMatchObject([
|
||||
{ type: "function", name: "optional", strict: null },
|
||||
{ type: "function", name: "strict", strict: true },
|
||||
]);
|
||||
});
|
||||
|
||||
it.each(["gpt-5.3-codex", "gpt-5.4", "gpt-5.5"])("clamps %s minimal reasoning effort to low", async (modelId) => {
|
||||
const tempDir = mkdtempSync(join(tmpdir(), "pi-codex-stream-"));
|
||||
process.env.PI_CODING_AGENT_DIR = tempDir;
|
||||
@@ -1214,6 +1334,100 @@ describe("openai-codex streaming", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("closes one-shot websockets when cacheRetention is none", async () => {
|
||||
const token = mockToken();
|
||||
const sentBodies: Array<{ prompt_cache_key?: string }> = [];
|
||||
let connections = 0;
|
||||
let closedConnections = 0;
|
||||
|
||||
class MockWebSocket {
|
||||
private listeners = new Map<string, Set<(event: unknown) => void>>();
|
||||
|
||||
constructor() {
|
||||
connections++;
|
||||
queueMicrotask(() => this.dispatch("open", {}));
|
||||
}
|
||||
|
||||
addEventListener(type: string, listener: (event: unknown) => void): void {
|
||||
let listeners = this.listeners.get(type);
|
||||
if (!listeners) {
|
||||
listeners = new Set();
|
||||
this.listeners.set(type, listeners);
|
||||
}
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
removeEventListener(type: string, listener: (event: unknown) => void): void {
|
||||
this.listeners.get(type)?.delete(listener);
|
||||
}
|
||||
|
||||
send(data: string): void {
|
||||
sentBodies.push(JSON.parse(data) as { prompt_cache_key?: string });
|
||||
queueMicrotask(() => {
|
||||
this.dispatch("message", {
|
||||
data: JSON.stringify({
|
||||
type: "response.completed",
|
||||
response: {
|
||||
id: `resp_${connections}`,
|
||||
status: "completed",
|
||||
usage: { input_tokens: 5, output_tokens: 3, total_tokens: 8 },
|
||||
},
|
||||
}),
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
close(): void {
|
||||
closedConnections++;
|
||||
}
|
||||
|
||||
private dispatch(type: string, event: unknown): void {
|
||||
for (const listener of this.listeners.get(type) ?? []) {
|
||||
listener(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vi.stubGlobal("WebSocket", MockWebSocket);
|
||||
vi.stubGlobal(
|
||||
"fetch",
|
||||
vi.fn(async () => new Response("unexpected fetch", { status: 500 })),
|
||||
);
|
||||
|
||||
const model: Model<"openai-codex-responses"> = {
|
||||
id: "gpt-5.1-codex",
|
||||
name: "GPT-5.1 Codex",
|
||||
api: "openai-codex-responses",
|
||||
provider: "openai-codex",
|
||||
baseUrl: "https://chatgpt.com/backend-api",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 400000,
|
||||
maxTokens: 128000,
|
||||
};
|
||||
const context: Context = {
|
||||
systemPrompt: "You are a helpful assistant.",
|
||||
messages: [{ role: "user", content: "Say hello", timestamp: 1 }],
|
||||
};
|
||||
const options = {
|
||||
apiKey: token,
|
||||
cacheRetention: "none" as const,
|
||||
sessionId: "one-off-summary",
|
||||
transport: "auto" as const,
|
||||
};
|
||||
|
||||
await streamOpenAICodexResponses(model, context, options).result();
|
||||
await streamOpenAICodexResponses(model, context, options).result();
|
||||
|
||||
expect(connections).toBe(2);
|
||||
expect(closedConnections).toBe(2);
|
||||
expect(sentBodies).toHaveLength(2);
|
||||
expect(sentBodies.every((body) => body.prompt_cache_key === undefined)).toBe(true);
|
||||
expect(getOpenAICodexWebSocketDebugStats("one-off-summary")).toBeUndefined();
|
||||
expect(global.fetch).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("falls back to SSE when websocket connect does not open before the connect timeout", async () => {
|
||||
vi.useFakeTimers();
|
||||
const token = mockToken();
|
||||
@@ -1658,10 +1872,6 @@ describe("openai-codex streaming", () => {
|
||||
it("sends only response input deltas in websocket-cached mode", async () => {
|
||||
const token = mockToken();
|
||||
const sentBodies: unknown[] = [];
|
||||
const responses = [
|
||||
{ responseId: "resp_1", messageId: "msg_1", text: "Hello" },
|
||||
{ responseId: "resp_2", messageId: "msg_2", text: "Done" },
|
||||
];
|
||||
|
||||
class MockWebSocket {
|
||||
static OPEN = 1;
|
||||
@@ -1687,36 +1897,41 @@ describe("openai-codex streaming", () => {
|
||||
|
||||
send(data: string): void {
|
||||
sentBodies.push(JSON.parse(data));
|
||||
const response = responses.shift();
|
||||
if (!response) throw new Error("unexpected websocket request");
|
||||
const responseId = `resp_${sentBodies.length}`;
|
||||
const outputEvents =
|
||||
sentBodies.length === 1
|
||||
? [
|
||||
{
|
||||
type: "response.output_item.added",
|
||||
item: {
|
||||
type: "custom_tool_call",
|
||||
id: "ctc_1",
|
||||
call_id: "call_1",
|
||||
name: "sample_tool",
|
||||
input: "",
|
||||
},
|
||||
},
|
||||
{ type: "response.custom_tool_call_input.delta", item_id: "ctc_1", delta: "abc" },
|
||||
{ type: "response.custom_tool_call_input.done", item_id: "ctc_1", input: "abc" },
|
||||
{
|
||||
type: "response.output_item.done",
|
||||
item: {
|
||||
type: "custom_tool_call",
|
||||
id: "ctc_1",
|
||||
call_id: "call_1",
|
||||
name: "sample_tool",
|
||||
input: "abc",
|
||||
},
|
||||
},
|
||||
]
|
||||
: [];
|
||||
const events = [
|
||||
{ type: "response.created", response: { id: response.responseId } },
|
||||
{
|
||||
type: "response.output_item.added",
|
||||
item: {
|
||||
type: "message",
|
||||
id: response.messageId,
|
||||
role: "assistant",
|
||||
status: "in_progress",
|
||||
content: [],
|
||||
},
|
||||
},
|
||||
{ type: "response.content_part.added", part: { type: "output_text", text: "" } },
|
||||
{ type: "response.output_text.delta", delta: response.text },
|
||||
{
|
||||
type: "response.output_item.done",
|
||||
item: {
|
||||
type: "message",
|
||||
id: response.messageId,
|
||||
role: "assistant",
|
||||
status: "completed",
|
||||
content: [{ type: "output_text", text: response.text }],
|
||||
},
|
||||
},
|
||||
{ type: "response.created", response: { id: responseId } },
|
||||
...outputEvents,
|
||||
{
|
||||
type: "response.completed",
|
||||
response: {
|
||||
id: response.responseId,
|
||||
id: responseId,
|
||||
status: "completed",
|
||||
usage: {
|
||||
input_tokens: 5,
|
||||
@@ -1758,10 +1973,19 @@ describe("openai-codex streaming", () => {
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 400000,
|
||||
maxTokens: 128000,
|
||||
compat: { supportsOpenAIGrammarTools: true },
|
||||
};
|
||||
const firstContext: Context = {
|
||||
systemPrompt: "You are a helpful assistant.",
|
||||
messages: [{ role: "user", content: "Say hello", timestamp: 1 }],
|
||||
messages: [{ role: "user", content: "Use the tool", timestamp: 1 }],
|
||||
tools: [
|
||||
{
|
||||
name: "sample_tool",
|
||||
description: "Sample tool",
|
||||
parameters: Type.Object({ payload: Type.String() }),
|
||||
constrainedSampling: { type: "grammar", variants: { openai_lark: "start: /[a-z]+/" } },
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const first = await streamOpenAICodexResponses(model, firstContext, {
|
||||
@@ -1771,8 +1995,20 @@ describe("openai-codex streaming", () => {
|
||||
}).result();
|
||||
|
||||
const secondContext: Context = {
|
||||
systemPrompt: "You are a helpful assistant.",
|
||||
messages: [...firstContext.messages, first, { role: "user", content: "Now finish", timestamp: 2 }],
|
||||
...firstContext,
|
||||
messages: [
|
||||
...firstContext.messages,
|
||||
first,
|
||||
{
|
||||
role: "toolResult",
|
||||
toolCallId: "call_1|ctc_1",
|
||||
toolName: "sample_tool",
|
||||
content: [{ type: "text", text: "real result" }],
|
||||
isError: false,
|
||||
timestamp: 2,
|
||||
},
|
||||
{ role: "user", content: "Now finish", timestamp: 3 },
|
||||
],
|
||||
};
|
||||
await streamOpenAICodexResponses(model, secondContext, {
|
||||
apiKey: token,
|
||||
@@ -1785,10 +2021,13 @@ describe("openai-codex streaming", () => {
|
||||
const secondBody = sentBodies[1] as { input: unknown[]; previous_response_id?: string; store?: boolean };
|
||||
expect(firstBody.store).toBe(false);
|
||||
expect(firstBody.previous_response_id).toBeUndefined();
|
||||
expect(firstBody.input).toEqual([{ role: "user", content: [{ type: "input_text", text: "Say hello" }] }]);
|
||||
expect(firstBody.input).toEqual([{ role: "user", content: [{ type: "input_text", text: "Use the tool" }] }]);
|
||||
expect(secondBody.store).toBe(false);
|
||||
expect(secondBody.previous_response_id).toBe("resp_1");
|
||||
expect(secondBody.input).toEqual([{ role: "user", content: [{ type: "input_text", text: "Now finish" }] }]);
|
||||
expect(secondBody.input).toEqual([
|
||||
{ type: "custom_tool_call_output", call_id: "call_1", output: "real result" },
|
||||
{ role: "user", content: [{ type: "input_text", text: "Now finish" }] },
|
||||
]);
|
||||
expect(getOpenAICodexWebSocketDebugStats("session-1")).toMatchObject({
|
||||
requests: 2,
|
||||
connectionsCreated: 1,
|
||||
@@ -1797,7 +2036,7 @@ describe("openai-codex streaming", () => {
|
||||
storeTrueRequests: 0,
|
||||
fullContextRequests: 1,
|
||||
deltaRequests: 1,
|
||||
lastDeltaInputItems: 1,
|
||||
lastDeltaInputItems: 2,
|
||||
lastPreviousResponseId: "resp_1",
|
||||
});
|
||||
});
|
||||
@@ -2094,6 +2333,46 @@ describe("openai-codex streaming", () => {
|
||||
expect(codexRequests).toBe(2);
|
||||
});
|
||||
|
||||
it.each([429, 503])("fails immediately when a %i retry delay exceeds the limit", async (status) => {
|
||||
const token = mockToken();
|
||||
const fetchMock = vi.fn(
|
||||
async () =>
|
||||
new Response(JSON.stringify({ error: { code: "temporarily_unavailable", message: "retry later" } }), {
|
||||
status,
|
||||
headers: { "content-type": "application/json", "retry-after": "2" },
|
||||
}),
|
||||
);
|
||||
vi.stubGlobal("fetch", fetchMock);
|
||||
|
||||
const model: Model<"openai-codex-responses"> = {
|
||||
id: "gpt-5.1-codex",
|
||||
name: "GPT-5.1 Codex",
|
||||
api: "openai-codex-responses",
|
||||
provider: "openai-codex",
|
||||
baseUrl: "https://chatgpt.com/backend-api",
|
||||
reasoning: true,
|
||||
input: ["text"],
|
||||
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
||||
contextWindow: 400000,
|
||||
maxTokens: 128000,
|
||||
};
|
||||
const context: Context = {
|
||||
systemPrompt: "You are a helpful assistant.",
|
||||
messages: [{ role: "user", content: "Say hello", timestamp: Date.now() }],
|
||||
};
|
||||
|
||||
const result = await streamOpenAICodexResponses(model, context, {
|
||||
apiKey: token,
|
||||
transport: "sse",
|
||||
maxRetries: 3,
|
||||
maxRetryDelayMs: 1000,
|
||||
}).result();
|
||||
|
||||
expect(result.stopReason).toBe("error");
|
||||
expect(result.errorMessage).toBe("Server requested 2s retry delay (max: 1s)");
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("zstd-compresses SSE request bodies", async () => {
|
||||
const token = mockToken();
|
||||
const encoder = new TextEncoder();
|
||||
|
||||
Reference in New Issue
Block a user