feat: implement trimContinuationOverlap utility to remove redundant stream prefixes and add associated tests.

This commit is contained in:
CJACK
2026-04-06 02:23:28 +08:00
parent 4d36afea4c
commit 49012a227c
10 changed files with 66878 additions and 23 deletions

View File

@@ -22,6 +22,7 @@ const {
shouldSkipPath,
isNodeStreamSupportedPath,
extractPathname,
trimContinuationOverlap,
} = handler.__test;
test('chat-stream exposes parser test hooks', () => {
@@ -368,3 +369,10 @@ test('extractPathname strips query only', () => {
assert.equal(extractPathname('/v1/chat/completions?stream=true'), '/v1/chat/completions');
assert.equal(extractPathname('/v1beta/models/gemini-2.5-flash:streamGenerateContent?key=1'), '/v1beta/models/gemini-2.5-flash:streamGenerateContent');
});
test('trimContinuationOverlap preserves short normal tokens and trims long snapshots', () => {
assert.equal(trimContinuationOverlap('我们被问到', '我们'), '我们');
const existing = '我们被问到:这是一个很长的续答快照前缀,用来验证去重逻辑不会误伤正常 token。';
const incoming = `${existing}继续分析`;
assert.equal(trimContinuationOverlap(existing, incoming), '继续分析');
});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -7,6 +7,7 @@ import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const chatStream = require('../../api/chat-stream.js');
const { parseChunkForContent } = chatStream.__test;
const { trimContinuationOverlap } = chatStream.__test;
function parseArgs(argv) {
const out = {
@@ -179,6 +180,8 @@ function parseDeepSeekReplay(raw) {
let currentType = 'thinking';
let sawFinish = false;
let outputText = '';
let thinkingText = '';
let textOutput = '';
let parsedChunks = 0;
for (const evt of events) {
@@ -201,7 +204,15 @@ function parseDeepSeekReplay(raw) {
sawFinish = true;
}
for (const part of parsed.parts) {
outputText += part.text;
if (part.type === 'thinking') {
const trimmed = trimContinuationOverlap(thinkingText, part.text);
thinkingText += trimmed;
outputText += trimmed;
} else {
const trimmed = trimContinuationOverlap(textOutput, part.text);
textOutput += trimmed;
outputText += trimmed;
}
}
}