体验:tool_call 参数流式早反馈——id+name 就绪即建卡,参数碎片增量下发实时填充
- llm.js callModel:tool_call 碎片凑齐 id+name 时触发 onToolCallStart,其后参数按增量触发 onToolCallArgs(announced/announcedLen 按 slot 去重) - engine.js streamAssistant:接回调广播 tool_call_start / tool_call_args 两个 SSE 事件 - 前端:tool_call_start 即建展开态「生成参数中…」早卡(顺带解决原 tool_call 事件因 assistant_message 先建卡而永远收起的问题);tool_call_args 150ms 节流流式填参数区+字符计数 - 收尾:assistant_message / 执行时 tool_call 带完整(解包后)参数回填 finalizeToolCardArgs;done 异常兜底回填,防占位态永久呼吸;toolCallStreaming 随会话/回合各处重置 - CSS:.tool-generating 的 ◌ 呼吸动画 - 修复写坏的复制按钮 SVG path(上一轮 fuzzy 编辑吞字),与 HEAD 比对还原
This commit is contained in:
@@ -261,6 +261,8 @@ function deepMergeInto(dst, src) {
|
||||
|
||||
// 调模型一次。onText(textDelta) 回调流式文本。
|
||||
// 返回 { content, tool_calls: [{id,name,arguments(string)}], finish_reason, usage, aborted }
|
||||
// 可选回调 opts.onToolCallStart({id,name}):某 tool_call 首次凑齐 id+name 时触发一次;
|
||||
// opts.onToolCallArgs({id,args}):此后该调用每段参数增量各触发一次(供前端流式渲染)。
|
||||
// options.deepThink: true 时将 modelCfg.thinking_params 合并进 body。
|
||||
async function callModel(cfg, modelId, messages, tools, options) {
|
||||
const opts = options || {};
|
||||
@@ -319,6 +321,8 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
let aborted = false;
|
||||
let sseFrames = 0; // 收到的上游 data 帧数(仅计数成功下传到 onEvent 的)
|
||||
const toolCalls = new Map(); // index -> {id, name, arguments}
|
||||
const announced = new Set(); // 已早广播的 slot idx(id+name 就绪即通知前端建卡,不等整轮生成完)
|
||||
const announcedLen = new Map(); // idx -> 已转发的参数字符数(参数碎片只发增量,不重复全量)
|
||||
|
||||
try {
|
||||
await forEachSSEData(res, evt => {
|
||||
@@ -355,6 +359,20 @@ async function callModel(cfg, modelId, messages, tools, options) {
|
||||
if (tc.function.name) acc.name += tc.function.name;
|
||||
if (tc.function.arguments) acc.arguments += tc.function.arguments;
|
||||
}
|
||||
// 早反馈:id+name 就绪即通知前端建卡(写大文件等长参数生成期间不再静默如卡死);
|
||||
// 其后参数碎片按增量转发(流式填「生成参数中」卡片)。
|
||||
if (acc.id && acc.name && !announced.has(idx)) {
|
||||
announced.add(idx);
|
||||
announcedLen.set(idx, acc.arguments.length);
|
||||
if (opts.onToolCallStart) opts.onToolCallStart({ id: acc.id, name: acc.name });
|
||||
}
|
||||
if (announced.has(idx) && opts.onToolCallArgs) {
|
||||
const from = announcedLen.get(idx) || 0;
|
||||
if (acc.arguments.length > from) {
|
||||
announcedLen.set(idx, acc.arguments.length);
|
||||
opts.onToolCallArgs({ id: acc.id, args: acc.arguments.slice(from) });
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
opts.onMeta,
|
||||
|
||||
Reference in New Issue
Block a user