mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-23 10:57:44 +08:00
Merge pull request #111 from CJackHwang/dev
Merge pull request #110 from CJackHwang/codex/align-js-runtime-with-go-runtime-logic Align Vercel JS stream tool-call delta handling with Go runtime
This commit is contained in:
@@ -1,33 +1,22 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const {
|
const {
|
||||||
extractToolNames,
|
|
||||||
createToolSieveState,
|
createToolSieveState,
|
||||||
processToolSieveChunk,
|
processToolSieveChunk,
|
||||||
flushToolSieve,
|
flushToolSieve,
|
||||||
parseStandaloneToolCalls,
|
parseStandaloneToolCalls,
|
||||||
formatOpenAIStreamToolCalls,
|
formatOpenAIStreamToolCalls,
|
||||||
} = require('../helpers/stream-tool-sieve');
|
} = require('../helpers/stream-tool-sieve');
|
||||||
const {
|
const { BASE_HEADERS } = require('../shared/deepseek-constants');
|
||||||
BASE_HEADERS,
|
const { writeOpenAIError } = require('./error_shape');
|
||||||
} = require('../shared/deepseek-constants');
|
const { parseChunkForContent, isCitation } = require('./sse_parse');
|
||||||
|
const { buildUsage } = require('./token_usage');
|
||||||
const {
|
|
||||||
writeOpenAIError,
|
|
||||||
} = require('./error_shape');
|
|
||||||
const {
|
|
||||||
parseChunkForContent,
|
|
||||||
isCitation,
|
|
||||||
} = require('./sse_parse');
|
|
||||||
const {
|
|
||||||
buildUsage,
|
|
||||||
} = require('./token_usage');
|
|
||||||
const {
|
const {
|
||||||
resolveToolcallPolicy,
|
resolveToolcallPolicy,
|
||||||
|
formatIncrementalToolCallDeltas,
|
||||||
|
filterIncrementalToolCallDeltasByAllowed,
|
||||||
} = require('./toolcall_policy');
|
} = require('./toolcall_policy');
|
||||||
const {
|
const { createChatCompletionEmitter } = require('./stream_emitter');
|
||||||
createChatCompletionEmitter,
|
|
||||||
} = require('./stream_emitter');
|
|
||||||
const {
|
const {
|
||||||
asString,
|
asString,
|
||||||
isAbortError,
|
isAbortError,
|
||||||
@@ -57,6 +46,7 @@ async function handleVercelStream(req, res, rawBody, payload) {
|
|||||||
const searchEnabled = toBool(prep.body.search_enabled);
|
const searchEnabled = toBool(prep.body.search_enabled);
|
||||||
const toolPolicy = resolveToolcallPolicy(prep.body, payload.tools);
|
const toolPolicy = resolveToolcallPolicy(prep.body, payload.tools);
|
||||||
const toolNames = toolPolicy.toolNames;
|
const toolNames = toolPolicy.toolNames;
|
||||||
|
const emitEarlyToolDeltas = toolPolicy.emitEarlyToolDeltas;
|
||||||
|
|
||||||
if (!model || !leaseID || !deepseekToken || !powHeader || !completionPayload) {
|
if (!model || !leaseID || !deepseekToken || !powHeader || !completionPayload) {
|
||||||
writeOpenAIError(res, 500, 'invalid vercel prepare response');
|
writeOpenAIError(res, 500, 'invalid vercel prepare response');
|
||||||
@@ -132,6 +122,7 @@ async function handleVercelStream(req, res, rawBody, payload) {
|
|||||||
const toolSieveState = createToolSieveState();
|
const toolSieveState = createToolSieveState();
|
||||||
let toolCallsEmitted = false;
|
let toolCallsEmitted = false;
|
||||||
const streamToolCallIDs = new Map();
|
const streamToolCallIDs = new Map();
|
||||||
|
const streamToolNames = new Map();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
reader = completionRes.body.getReader();
|
reader = completionRes.body.getReader();
|
||||||
let buffered = '';
|
let buffered = '';
|
||||||
@@ -255,6 +246,18 @@ async function handleVercelStream(req, res, rawBody, payload) {
|
|||||||
}
|
}
|
||||||
const events = processToolSieveChunk(toolSieveState, p.text, toolNames);
|
const events = processToolSieveChunk(toolSieveState, p.text, toolNames);
|
||||||
for (const evt of events) {
|
for (const evt of events) {
|
||||||
|
if (evt.type === 'tool_call_deltas') {
|
||||||
|
if (!emitEarlyToolDeltas) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const filtered = filterIncrementalToolCallDeltasByAllowed(evt.deltas, toolNames, streamToolNames);
|
||||||
|
const formatted = formatIncrementalToolCallDeltas(filtered, streamToolCallIDs);
|
||||||
|
if (formatted.length > 0) {
|
||||||
|
toolCallsEmitted = true;
|
||||||
|
sendDeltaFrame({ tool_calls: formatted });
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (evt.type === 'tool_calls') {
|
if (evt.type === 'tool_calls') {
|
||||||
toolCallsEmitted = true;
|
toolCallsEmitted = true;
|
||||||
sendDeltaFrame({ tool_calls: formatOpenAIStreamToolCalls(evt.calls, streamToolCallIDs) });
|
sendDeltaFrame({ tool_calls: formatOpenAIStreamToolCalls(evt.calls, streamToolCallIDs) });
|
||||||
|
|||||||
Reference in New Issue
Block a user