fix(openai): fix stop_reason null, zero usage fields and max_tokens forwarding

- Fix stop_reason always null in assembled AssistantMessage by applying
  the value captured from message_delta event
- Reset partialMessage to null after message_stop to prevent duplicate
  AssistantMessage emission causing doubled content in next API request
- Forward computed maxTokens into buildOpenAIRequestBody as max_tokens
  so OpenAI-compatible endpoints receive the intended output cap
- Extract assembleFinalAssistantOutputs helper to deduplicate message
  assembly logic between message_stop handler and post-loop fallback
- Fix test helper to use events parameter instead of hidden global
- Add regression test for max_tokens request forwarding

Signed-off-by: guunergooner <tongchao0923@gmail.com>
This commit is contained in:
guunergooner
2026-04-09 18:53:17 +08:00
parent e6affc7053
commit c82f59943c
6 changed files with 865 additions and 61 deletions

View File

@@ -1,6 +1,16 @@
import { describe, expect, test } from "bun:test";
import { describe, expect, test, beforeAll, afterAll } from "bun:test";
import { formatBriefTimestamp } from "../formatBriefTimestamp";
let savedLcAll: string | undefined;
beforeAll(() => {
savedLcAll = process.env.LC_ALL;
process.env.LC_ALL = "en_US.UTF-8";
});
afterAll(() => {
if (savedLcAll === undefined) delete process.env.LC_ALL;
else process.env.LC_ALL = savedLcAll;
});
describe("formatBriefTimestamp", () => {
// Fixed "now" for deterministic tests: 2026-04-02T14:00:00Z (Thursday)
const now = new Date("2026-04-02T14:00:00Z");