Repository navigation
Description
Project: billion-context
Version: v0.1.85 (npm, bin bili)
Environment: Windows 10/11, Node via npm global install, used as OpenAI-compatible reverse proxy in front of a custom SSE gateway
Summary
In dist/index.js, extractEventType() only recognizes SSE events that carry an explicit event: line. Streams that emit valid SSE with only data: lines (which is legal: per the WHATWG server-sent events spec, data: is the only required field and event: defaults to message) get every event skipped. parseStream never sees a terminal event, so when the upstream stream ends normally the proxy synthesizes:
event: response.failed
error: {"code": "server_error", "message": "upstream returned no response"}
…and the client (codex) shows stream disconnected before completion: upstream returned no response, even though the upstream completed successfully and delivered a full response.completed event.
Root cause
extractEventType (≈ dist/index.js:52400):
function extractEventType(rawEvent) {
for (const l of rawEvent.split("\n")) {
if (l.startsWith("event:")) return l.slice(6).trim();
}
return null;
}parseStream (≈ dist/index.js:52564) then does:
const type = extractEventType(eventStr);
if (!type || !dataLine) continue; // ← all data-only events skippediterSseEvents (≈ dist/index.js:52372) splits frames on \n\n, which is fine. The problem is only the event: dependency.
Many real-world OpenAI-compatible servers (including official clients' own minimal gateways, and custom proxies) emit events as pure data: {"type":"response.created",...} lines with no event: line — this is what the codex CLI itself tolerates (it reads the type field from the JSON payload). The proxy should do the same as a fallback.
Minimal reproduction
- Run any SSE server that responds to
POST /v1/responseswithstream: trueand emits only:(nodata: {"type":"response.created"} data: {"type":"response.completed"}event:lines) — e.g. a 20-line PythonBaseHTTPRequestHandler. - Start the proxy:
bili start --port 8786(orbili codex). - Point a client at
http://127.0.0.1:8786/bili/http://127.0.0.1:<port>/v1and make a streaming request (send anx-session-idheader — required by this proxy). - The upstream returns 200 with a complete stream, but the client receives
response.failed+upstream returned no response.
Expected behavior
Events whose event: line is absent should fall back to the type field inside the data: JSON payload, e.g.:
function extractEventType(rawEvent) {
for (const l of rawEvent.split("\n")) {
if (l.startsWith("event:")) return l.slice(6).trim();
}
const dataLine = rawEvent.split("\n").find((l) => l.startsWith("data:"));
if (dataLine) {
try {
const parsed = JSON.parse(dataLine.slice(5).trim());
if (parsed && typeof parsed.type === "string") return parsed.type;
} catch {}
}
return null;
}That is backward compatible (explicit event: still wins) and makes the parser accept valid spec-compliant streams that omit the optional event: field.
Notes / environment details
- Triggered through the full path:
bili codexlauncher (random port) andbili start --port 8786, client = codex TUI 0.147.0 withbase_url = <bili-origin>/bili/http://127.0.0.1:8088/v1. - The proxy's own synthetic-error path (
emitCompletion, ≈dist/index.js:52743) is what turns the mis-parsed stream into the misleadingupstream returned no response— worth reviewing too, since it masked the real cause. - In our case the upstream was a minimal OpenAI-compatible SSE gateway; the fix on the server side (adding
event:lines) resolves it, but the parser should not require an optional field.
Activity
ranxianglei commentedon Sep 5, 2026
[bot] 🏷 Verified and fixed — PR: #550
Triage (verified before implementing):
src/loop/adapter-responses.ts—extractEventType()returnsnullfor frames without anevent:line, andparseStream()then doesif (!type || !dataLine) continue;, so every data-only frame is skipped. With no terminal event ever seen, a cleanly-finished upstream stream falls into the "no completion" path andemitCompletion()synthesizes exactly theresponse.failed/"upstream returned no response"you observed. Your root-cause analysis is correct, including the layering: the parser requiring the optionalevent:field is the cause; the synthetic error was the downstream mask (you were right that it deserved review — it now only fires for genuinely unterminated streams).event:is optional, and Responses-API payloads always carry atypefield equal to the event name — the same thing codex itself relies on. One adjustment in implementation: instead of re-parsing the JSON insideextractEventType, the fix parses the payload once inparseStreamand falls back to the already-parsed object'stype. Same semantics, no double parse. An explicitevent:line still wins; non-JSON data or payloads without a stringtypeare skipped exactly as before (strictly additive).adapter-openai.ts) already ignoresevent:lines entirely (chat-completions SSE is data-only by design) — the responses adapter was the outlier. The anthropic adapter has the same strictness (parseAnthropicSserequires anevent:line); I left it unchanged since Anthropic streams always carry event lines and no compatible gateway omitting them has been reported — flagging it here for awareness rather than bundling an unreported change.Regression tests (3 added,
tests/loop-adapters.test.ts): data-only stream parses fully todone/completed; explicitevent:precedence pinned; end-to-end drain of a data-only stream emitsresponse.completedand never the synthetic failure. Tests 1 and 3 verified to fail against the unfixed code.Pre-flight: typecheck pass · 949/950 tests pass (the one failure is pre-existing on clean master in this environment — missing client binaries, unrelated) · build pass.
ranxianglei commentedon Sep 5, 2026
[bot] 🏷 Update: independent review of PR #550 is complete and it passed — full pre-flight re-run (typecheck / 949-950 tests with the one failure confirmed environmental / build) plus a negative check proving the new regression tests fail against the unfixed code. Verdict: LGTM, ready for human merge. Details in #550.
ranxianglei commentedon Sep 6, 2026
Review 完成,PR #550 可合并。
验证:对最新 master(含 #587/#528/#563/#534/#544)零冲突合并;typecheck ✓;全量 1139/1141(2 个已知 plugin-agent 环境失败);build ✓;
tests/loop-adapters.test.ts28/28(含本 PR 新增 3 个 F8 用例)。正确性:
event:可选(缺省message),纯data:帧合法;codex CLI 本身就靠读 JSON payload 的type字段容错,proxy 对齐这个行为正确explicitType ?? obj.type顺序对:显式event:行优先,payloadtype兜底,两者皆无才 continue(与非 JSON 帧行为一致)src/loop/adapter-responses.ts是当前唯一的 Responses SSE 解析器(stream-responses.ts的流式变体已移除,server.ts 把流全部路由进 compress-loop adapter);openai adapter 本来就只看data:行;stream.ts/stream-openai.ts 无event:依赖 —— 修复完整upstream returned no response合成失败)、显式 event: 优先级非阻塞备注(建议后续 follow-up,不属本 PR):
src/loop/adapter-anthropic.ts:48parseAnthropicSse有同类潜在问题(if (!type) return null,data-only Anthropic 帧会被丢弃)。官方 Anthropic API 总是发event:行所以无实际复现,但同样的 payloadtype兜底可平移。ranxianglei commentedon Sep 6, 2026
[bot] 🏷 收到,感谢完整 review(零冲突合并验证 + 覆盖面确认 + 1139/1141 全量回归)。
两点说明:
event:行),按仓库约定只报告不单开 issue;如果你想让它可跟踪,说一声我再补一个带来源标记(来源: #549 / PR #550)的 follow-up issue。Merge pull request #550 from ranxianglei/2026-09-05_responses-data-on…
ranxianglei commentedon Sep 7, 2026
修复已随 PR#550 合并 (b5bc655): extractEventType 不再要求必须有 event: 行, data-only SSE 帧按 WHATWG 规范正常处理。PR 标题里的 (#549) 不是 closing 关键字所以没有自动关, 手动关闭。
fix: accept data-only SSE streams in responses adapter (ranxianglei#549)