Skip to content
Open
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
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,7 @@ Security boundary: the file controls ONLY *when* to upgrade and *which published
- **Default:** `{ retries: 0, bufCapBytes: 16777216 }`
- **Status:** ACTIVE
- **Description:** Fake-completion safety net for hosts that abort mid-stream (#371) — **opt-in**, `retries: 0` (default) is the pre-#371 passthrough. `bufCapBytes` is the OOM guard on the buffered response. Twins `BILI_FAKE_COMPLETION_RETRIES`, `BILI_FAKE_BUF_CAP`.
- A response exceeding `bufCapBytes` continues through the normal streaming path, including its already-read prefix. The incomplete inspection neither retries nor changes the session's fake-completion streak; the cap does not truncate the answer. This applies to the first response and hinted retries.

### `codexCompact` / `ccrRetrievalTtlMs` / `decompressTmpCap`

Expand Down
1 change: 1 addition & 0 deletions docs/configuration.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -1107,6 +1107,7 @@
- **默认:** `{ retries: 0, bufCapBytes: 16777216 }`
- **状态:** ACTIVE
- **说明:** 针对流中途被宿主掐断场景的 fake-completion 安全网(#371)—— **显式开启**,`retries: 0`(默认)即 #371 之前的透传行为。`bufCapBytes` 是缓冲响应的 OOM 护栏。对应 `BILI_FAKE_COMPLETION_RETRIES`、`BILI_FAKE_BUF_CAP`。
- 响应超过 `bufCapBytes` 时,已读前缀和后续内容会继续进入正常流式处理。未完成的检查不会触发重试或改变会话的 fake-completion 连续计数;该上限不会截断回答。首次响应与附带纠正提示的重试响应都遵循此规则。

### `codexCompact` / `ccrRetrievalTtlMs` / `decompressTmpCap`

Expand Down
31 changes: 29 additions & 2 deletions src/loop/adapter-openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,24 @@ function openaiCachedTokens(u: Record<string, unknown>): number | null {

export function createOpenaiAdapter(requestBody: Record<string, unknown>, clientSystem?: string, absorbName?: string, notes?: string[], errorShape: "protocol" | "completion" = "protocol"): CompressLoopAdapter {
const model = (requestBody.model as string) ?? "unknown";
// OpenAI clients opt into the trailing usage frame with
// stream_options.include_usage. bili forces the upstream option on for its
// own ledger, but the frame must also reach the CLIENT when it asked for
// it: cost/context-aware clients read provider fields from that frame, and
// swallowing it on plain-text rounds silently loses them. Tool-call rounds
// keep the existing verbatim forward.
const clientWantsUsage = (() => {
const so = (requestBody as { stream_options?: unknown }).stream_options;
return typeof so === "object" && so !== null
&& (so as Record<string, unknown>).include_usage === true;
})();
let responseId = `chatcmpl-proxy-${Date.now()}`;
let toolIndex = 0;
// Last upstream usage object seen on the wire. The synthesized completion
// frame re-states tokens; provider-specific fields (cost/credits, cache
// details, validity flags) must survive that re-statement — dropping them
// breaks any client that prices or accounts the turn from them.
let lastUsageRaw: Record<string, unknown> | null = null;

const makeBase = () => ({
id: responseId,
Expand Down Expand Up @@ -280,6 +296,9 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
},

async *parseStream(upstream, round) {
// Per-round scope: a later round that emits no usage frame must not
// inherit the previous round's provider fields (e.g. `credits`).
lastUsageRaw = null;
const pending = new Map<number, ToolCallBuffer>();
// #206: strip model-imitated render tags from PROSE deltas; the
// filter may hold back a short tail, flushed at finish/[DONE].
Expand Down Expand Up @@ -535,6 +554,7 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
if (!choice) {
if (parsed.usage) {
const u = parsed.usage as Record<string, unknown>;
lastUsageRaw = u;
yield {
kind: "usage",
inputTokens: typeof u.prompt_tokens === "number" ? u.prompt_tokens : undefined,
Expand All @@ -545,7 +565,7 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
// from this trailing empty-choices frame; raw tool-call rounds
// must forward it verbatim, not swallow it into the internal
// ledger.
if (sawRealToolCall) {
if (sawRealToolCall || clientWantsUsage) {
yield { kind: "meta", chunk: rawBuf } as ParsedStreamEvent;
}
}
Expand Down Expand Up @@ -578,6 +598,7 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
// only the ledger gains the sample.
if (parsed.usage && !finishReason && delta && Object.keys(delta).length === 0) {
const u = parsed.usage as Record<string, unknown>;
lastUsageRaw = u;
yield {
kind: "usage",
inputTokens: typeof u.prompt_tokens === "number" ? u.prompt_tokens : undefined,
Expand All @@ -591,6 +612,7 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
const hadToolCalls = [...pending.values()].some((tc) => tc.name.length > 0 || tc.id.length > 0);
yield* settleToolCalls();
const u = parsed.usage as Record<string, unknown> | undefined;
if (u) lastUsageRaw = u;
yield {
kind: "usage",
inputTokens: typeof u?.prompt_tokens === "number" ? u.prompt_tokens : undefined,
Expand Down Expand Up @@ -727,13 +749,18 @@ export function createOpenaiAdapter(requestBody: Record<string, unknown>, client
// Numeric fields must never serialize to absent/undefined: strict
// clients (dsh's mapUsage) compute over prompt_tokens/completion_tokens
// and a usage object missing them yields NaN (non-JSON-serializable).
const raw = lastUsageRaw ?? {};
const rawDetails = raw.prompt_tokens_details && typeof raw.prompt_tokens_details === "object"
? raw.prompt_tokens_details as Record<string, unknown>
: undefined;
const usage = opts?.usage
? {
...raw,
prompt_tokens: opts.usage.inputTokens ?? 0,
completion_tokens: opts.usage.outputTokens ?? 0,
total_tokens: (opts.usage.inputTokens ?? 0) + (opts.usage.outputTokens ?? 0),
...(typeof opts.usage.cachedTokens === "number"
? { prompt_tokens_details: { cached_tokens: opts.usage.cachedTokens } }
? { prompt_tokens_details: { ...rawDetails, cached_tokens: opts.usage.cachedTokens } }
: {}),
}
: null;
Expand Down
14 changes: 7 additions & 7 deletions src/loop/adapter-responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ function buildMessageItemSequence(itemId: string, outputIndex: number, text: str
return Buffer.from(
[
`event: response.output_item.added\ndata: ${JSON.stringify({ type: "response.output_item.added", output_index: outputIndex, item })}\n\n`,
`event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: outputIndex, part })}\n\n`,
`event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: outputIndex, delta: text })}\n\n`,
`event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: outputIndex, text })}\n\n`,
`event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: outputIndex, part: { type: "output_text", text } })}\n\n`,
`event: response.content_part.added\ndata: ${JSON.stringify({ type: "response.content_part.added", item_id: itemId, output_index: outputIndex, content_index: 0, part })}\n\n`,
`event: response.output_text.delta\ndata: ${JSON.stringify({ type: "response.output_text.delta", item_id: itemId, output_index: outputIndex, content_index: 0, delta: text })}\n\n`,
`event: response.output_text.done\ndata: ${JSON.stringify({ type: "response.output_text.done", item_id: itemId, output_index: outputIndex, content_index: 0, text })}\n\n`,
`event: response.content_part.done\ndata: ${JSON.stringify({ type: "response.content_part.done", item_id: itemId, output_index: outputIndex, content_index: 0, part: { type: "output_text", text } })}\n\n`,
`event: response.output_item.done\ndata: ${JSON.stringify({ type: "response.output_item.done", output_index: outputIndex, item: doneItem })}\n\n`,
].join(""),
"utf8",
Expand Down Expand Up @@ -413,12 +413,12 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
// identity filters, byte-verbatim (never rewritten; matches the
// openai adapter's reasoning_filter and #1960/KDD#10).
const reasoningFilters = new Map<string, { filter: TagEchoFilter; ref: { itemId: string; outputIndex: number; summaryIndex: number } }>();
let lastTextRef: { itemId: string; outputIndex: number } | null = null;
let lastTextRef: { itemId: string; outputIndex: number; contentIndex: unknown } | null = null;
const flushFilter = function* (): Generator<ParsedStreamEvent> {
const tail = tagFilter.flush();
if (tail.length > 0) {
const raw = lastTextRef
? rebuildResponsesEvent("response.output_text.delta", { type: "response.output_text.delta", item_id: lastTextRef.itemId, output_index: lastTextRef.outputIndex, delta: tail })
? rebuildResponsesEvent("response.output_text.delta", { type: "response.output_text.delta", item_id: lastTextRef.itemId, output_index: lastTextRef.outputIndex, content_index: lastTextRef.contentIndex, delta: tail })
: undefined;
yield { kind: "text", delta: tail, ...(raw ? { raw } : {}) } as ParsedStreamEvent;
}
Expand Down Expand Up @@ -545,7 +545,7 @@ export function createResponsesAdapter(textProtocol?: boolean, projection?: Resp
const itemId = typeof obj.item_id === "string" ? obj.item_id : "";
const outputIndex = typeof obj.output_index === "number" ? obj.output_index : 0;
const mapped = remapped.get(itemId);
lastTextRef = { itemId: mapped ? mapped.id : itemId, outputIndex: mapped ? mapped.index : outputIndex };
lastTextRef = { itemId: mapped ? mapped.id : itemId, outputIndex: mapped ? mapped.index : outputIndex, contentIndex: obj.content_index };
const clean = tagFilter.push(delta);
if (clean.length > 0) {
if (mapped) {
Expand Down
6 changes: 3 additions & 3 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4612,13 +4612,13 @@ export async function pipePluginResponsesWithStrip(
if (proseAcc.length === 0 || !session) return;
scanModelOutputForPhantomAttachments(session.id, proseAcc);
};
let lastDeltaMeta: { item_id?: unknown; output_index?: unknown } | null = null;
let lastDeltaMeta: { item_id?: unknown; output_index?: unknown; content_index?: unknown } | null = null;
const flushTail = (after: string) => {
const tail = tagFilter.flush();
if (tail.length > 0) {
visibleTextChars += tail.length;
answerProse += tail;
const meta = inRetry && heldItemId !== undefined ? { item_id: heldItemId, output_index: heldOutputIndex } : (lastDeltaMeta ?? {});
const meta = inRetry && heldItemId !== undefined ? { ...lastDeltaMeta, item_id: heldItemId, output_index: heldOutputIndex } : (lastDeltaMeta ?? {});
appendReplyDelta(meta, tail);
return `data: ${JSON.stringify({ type: "response.output_text.delta", ...meta, delta: tail })}\n\n` + after;
}
Expand Down Expand Up @@ -5070,7 +5070,7 @@ export async function pipePluginResponsesWithStrip(
continue;
}
const clean = tagFilter.push(delta);
lastDeltaMeta = { item_id: ev["item_id"], output_index: ev["output_index"] };
lastDeltaMeta = { item_id: ev["item_id"], output_index: ev["output_index"], content_index: ev["content_index"] };
if (!inRetry && heldItemId === undefined && ev["item_id"] !== undefined) {
heldItemId = ev["item_id"];
heldOutputIndex = ev["output_index"];
Expand Down
Loading
Loading