Skip to content
Merged
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
3 changes: 2 additions & 1 deletion src/app/v1/_lib/proxy/stream-gate/frame-classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ const STREAM_SIGNALS: Record<ProtocolFamily, StreamSignal> = {
"openai-chat": {
contentRules: [
{
// chunk 无事件名;delta 携带 content/tool_calls/refusal/audio 即内容
// chunk 无事件名;delta 携带 content/reasoning/tool_calls/refusal/audio 即内容
anyPaths: [
"choices.#.delta.content",
"choices.#.delta.reasoning_content",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[HIGH] [LOGIC-BUG] reasoning_content is only supported on the live-stream gate, so fake-streaming requests still reject or drop DeepSeek reasoning-only output.

Why this is a problem: This line makes the normal SSE gate treat choices[].delta.reasoning_content as deliverable content, but the OpenAI-chat fake-streaming path still ignores the same field in both validateOpenAIChatCompletion() and emitOpenAIChatStream(). For a fake-streaming-eligible OpenAI request, tryFakeStreamingPath() rewrites the upstream call to non-stream, so a DeepSeek response whose payload is only message.reasoning_content will still fail validation with no_deliverable_content, and the stream emitter would omit the reasoning text even if validation were relaxed. That means the PR fixes the direct streaming path but leaves the fake-streaming path broken for the same provider behavior.

Suggested fix:

const typed = message as {
  content?: unknown;
  reasoning_content?: unknown;
  tool_calls?: unknown;
  function_call?: unknown;
};

if (isNonEmptyString(typed.reasoning_content)) return true;

if (typeof message.reasoning_content === "string") {
  delta.reasoning_content = message.reasoning_content;
}

Also add a fake-streaming regression test that covers a reasoning-only OpenAI-chat completion.

"choices.#.delta.tool_calls.#.function.arguments",
"choices.#.delta.function_call.arguments",
"choices.#.delta.refusal",
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/proxy/stream-gate-content-gate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ describe("runStreamContentGate", () => {
}
});

it("openai-chat: DeepSeek reasoning_content commits before the default event cap", async () => {
const reasoningFrames = Array.from(
{ length: 65 },
(_, index) =>
`data: {"choices":[{"delta":{"reasoning_content":"reasoning step ${index}"}}]}\n\n`
);
const reader = readerFromChunks(reasoningFrames);
const result = await runStreamContentGate(reader, {
...GATE_OPTIONS,
family: "openai-chat",
});

expect(result.committed).toBe(true);
if (!result.committed) return;
expect(await drainPrefix(result.prefixChunks)).toBe(reasoningFrames[0]);
expect(result.readerDone).toBe(false);
});

it("gemini: usage-only chunks buffer until content commits", async () => {
const reader = readerFromChunks([
'data: {"usageMetadata":{"totalTokenCount":1}}\n\n',
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/proxy/stream-gate-forwarder-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,31 @@ describe("F1 stream content gate x ProxyForwarder sequential path", () => {
}
);

test("Replay owner 将 OpenAI-compatible DeepSeek reasoning_content 视为首个有效内容", async () => {
const provider = createProvider({
id: 1,
name: "deepseek-reasoning",
providerType: "openai-compatible",
});
const session = createSession();
session.setProvider(provider);
attachReplayOwner(session, REPLAY_GATE_CASES[1]);

const reasoningFrames = Array.from({ length: 65 }, (_, index) =>
sseFrame(null, { choices: [{ delta: { reasoning_content: `reasoning step ${index}` } }] })
);
const doForward = spyOnDoForward();
doForward.mockImplementationOnce(async () => createSseResponse(reasoningFrames));

const response = await ProxyForwarder.send(session);
const text = await response.text();

expect(doForward).toHaveBeenCalledTimes(1);
expect(text).toBe(reasoningFrames.join(""));
expect(mocks.pickRandomProviderWithExclusion).not.toHaveBeenCalled();
expect(mocks.recordFailure).not.toHaveBeenCalled();
});

test("Replay owner 在所有 precommit attempt 失败后立即释放所有权", async () => {
const provider = createProvider({ id: 1, name: "replay-only", providerType: "codex" });
const session = createSession();
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/proxy/stream-gate-frame-classifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,17 @@ describe("classifyFrame: anthropic", () => {
});

describe("classifyFrame: openai-chat", () => {
it("content: delta content / tool arguments / refusal / audio", () => {
it("content: delta content / reasoning / tool arguments / refusal / audio", () => {
expect(classifyFrame("openai-chat", null, '{"choices":[{"delta":{"content":"hi"}}]}')).toBe(
"content"
);
expect(
classifyFrame(
"openai-chat",
null,
'{"choices":[{"delta":{"reasoning_content":"reasoning step"}}]}'
)
).toBe("content");
expect(
classifyFrame(
"openai-chat",
Expand Down Expand Up @@ -197,10 +204,13 @@ describe("classifyFrame: openai-chat", () => {
).toBe("neutral");
});

it("neutral: empty string content delta", () => {
it("neutral: empty string content or reasoning delta", () => {
expect(classifyFrame("openai-chat", null, '{"choices":[{"delta":{"content":""}}]}')).toBe(
"neutral"
);
expect(
classifyFrame("openai-chat", null, '{"choices":[{"delta":{"reasoning_content":""}}]}')
).toBe("neutral");
});

it("error: in-stream error payload", () => {
Expand Down
Loading