feat: add SSE passthrough stream action

This commit is contained in:
2026-07-20 00:40:14 +08:00
parent ca8935d73e
commit 31e673f777
4 changed files with 282 additions and 4 deletions
+63
View File
@@ -822,6 +822,7 @@ async function run() {
retryable_error_messages: ["capacity test"],
upstream_fetch_retry_attempts: 3,
upstream_fetch_retry_backoff_ms: 50,
stream_action: "passthrough",
endpoints: ["/responses", "/v1/responses"],
}),
},
@@ -829,6 +830,15 @@ async function run() {
assert(exportableTextProfileResponse.status === 200, `可导出文本 profile 保存失败: ${exportableTextProfileResponse.status}`);
const exportableTextProfilePayload = await exportableTextProfileResponse.json();
assert(!JSON.stringify(exportableTextProfilePayload).includes("text-export-secret"), "普通 profile API 不应返回导出 key");
const exportableTextProfile = (exportableTextProfilePayload.profiles || []).find(
(profile) => profile?.name === "export-source",
);
assert(exportableTextProfile?.form?.stream_action === "passthrough", "文本 profile 未保留 SSE 直通模式");
assert(
(await readFile(path.join(profilesDir, "export-source.env"), "utf8"))
.includes("CODEX_RETRY_GATEWAY_STREAM_ACTION=passthrough"),
"文本 profile env 未写入 SSE 直通模式",
);
const passthroughExportResponse = await fetch(
`http://127.0.0.1:${gatewayPort}/__codex_retry_gateway/api/profiles/export`,
@@ -1916,6 +1926,59 @@ async function run() {
}
}
const enablePassthroughResponse = await fetch(`http://127.0.0.1:${gatewayPort}/__codex_retry_gateway/api/config`, {
method: "POST",
headers: { ...adminHeaders, "content-type": "application/json" },
body: JSON.stringify({ stream_action: "passthrough" }),
});
const enablePassthroughPayload = await enablePassthroughResponse.json();
assert(enablePassthroughResponse.status === 200, `启用 SSE 直通模式失败: ${enablePassthroughResponse.status}`);
assert(enablePassthroughPayload?.config?.stream_action === "passthrough", "当前配置未切换到 SSE 直通模式");
const passthroughThreadId = "thread_sse_passthrough";
const passthroughRetryKey = reasoningRetryKeyForRequest("/responses", {
stream: true,
thread_id: passthroughThreadId,
test_reasoning_retry_key: "sse-passthrough",
});
const passthroughStream = await readSseUntilClose(
`http://127.0.0.1:${gatewayPort}/responses`,
{
stream: true,
thread_id: passthroughThreadId,
test_reasoning_before_success_times: 1,
test_reasoning_retry_key: "sse-passthrough",
test_stream_include_lifecycle: true,
test_stream_lifecycle_marker: "passthrough-raw-marker",
test_stream_delta_chunks: 2,
test_stream_chunk_delay_ms: 2,
},
);
assert(passthroughStream.status === 200, `SSE 直通状态异常: ${passthroughStream.status}`);
assert(passthroughStream.headers.get("x-upstream-reasoning-attempt") === "1", "SSE 直通不应发起 reasoning 重试");
assert(passthroughStream.text.includes("passthrough-raw-marker"), "SSE 直通不应改写上游生命周期 payload");
assert(passthroughStream.text.includes('"reasoning_tokens":516'), "SSE 直通不应拦截匹配的 reasoning 响应");
const passthroughRetryStats = upstream.getReasoningRetryStat(passthroughRetryKey);
assert(passthroughRetryStats.totalRequests === 1, "SSE 直通不应启动多轮 reasoning 请求");
const passthroughRequestsResponse = await fetch(
`http://127.0.0.1:${gatewayPort}/__codex_retry_gateway/api/requests?query=${encodeURIComponent(passthroughThreadId)}`,
{ headers: adminHeaders },
);
const passthroughRequestsPayload = await passthroughRequestsResponse.json();
const passthroughEntry = (passthroughRequestsPayload?.entries || []).find(
(entry) => entry.thread_id === passthroughThreadId,
);
assert(passthroughEntry?.inspected === false, "SSE 直通请求不应标记为已检查");
assert(passthroughEntry?.reasoning_retry_stop_reason === "stream_passthrough", "SSE 直通请求未记录跳过重试原因");
assert((passthroughEntry?.stream_chunk_count || 0) >= 1, "SSE 直通请求未累计流式 chunk");
const restoreStrictStreamResponse = await fetch(`http://127.0.0.1:${gatewayPort}/__codex_retry_gateway/api/config`, {
method: "POST",
headers: { ...adminHeaders, "content-type": "application/json" },
body: JSON.stringify({ stream_action: "strict_502" }),
});
assert(restoreStrictStreamResponse.status === 200, "测试后恢复严格 SSE 模式失败");
const capturedLifecycleStream = await readSseUntilClose(
`http://127.0.0.1:${gatewayPort}/responses`,
{