@vincentbanana/bananacli 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,7 +54,6 @@ export async function runTurn(deps, messages, input, images) {
54
54
  allowOutsideWorkspace: deps.allowOutsideWorkspace,
55
55
  permissions: deps.permissions,
56
56
  todos,
57
- onOutput: deps.onToolOutput,
58
57
  recordChange: deps.recordChange,
59
58
  };
60
59
  const tryCompact = async (aggressive) => {
@@ -141,13 +140,17 @@ export async function runTurn(deps, messages, input, images) {
141
140
  ok: false,
142
141
  content: `未知工具 "${call.name}"。可用工具: ${[...tools.keys()].join(', ')}。请改用已有工具。`,
143
142
  };
144
- deps.onToolEnd?.(call.name, result);
143
+ deps.onToolEnd?.(call.name, result, call.id);
145
144
  convo.push({ role: 'tool', toolCallId: call.id, name: call.name, content: result.content, isError: true });
146
145
  continue;
147
146
  }
148
- deps.onToolStart?.(call.name, input);
149
- const toolResult = await runTool(tool, input, toolContext, permissionFor(deps.permissions, tool));
150
- deps.onToolEnd?.(call.name, toolResult);
147
+ deps.onToolStart?.(call.name, input, call.id);
148
+ const callContext = {
149
+ ...toolContext,
150
+ onOutput: (chunk, stream) => deps.onToolOutput?.(chunk, stream, call.id),
151
+ };
152
+ const toolResult = await runTool(tool, input, callContext, permissionFor(deps.permissions, tool));
153
+ deps.onToolEnd?.(call.name, toolResult, call.id);
151
154
  convo.push({
152
155
  role: 'tool',
153
156
  toolCallId: call.id,
@@ -876,6 +876,7 @@ export async function startUiServer(options) {
876
876
  todos: session.todos,
877
877
  };
878
878
  const started = Date.now();
879
+ const toolStartedAt = new Map();
879
880
  let result;
880
881
  try {
881
882
  result = await runTurn({
@@ -892,15 +893,40 @@ export async function startUiServer(options) {
892
893
  recordChange: (path, beforeContent) => recorder.record(path, beforeContent),
893
894
  onText: (chunk) => send('text', { chunk }),
894
895
  onReasoning: (chunk) => send('reasoning', { chunk }),
895
- onToolStart: (name, input) => send('tool_start', { name, detail: summarizeInput(input) }),
896
- onToolEnd: (name, toolResult) => send('tool_end', {
897
- name,
898
- ok: toolResult.ok,
899
- display: toolResult.display ?? '',
900
- error: toolResult.ok ? '' : toolResult.content.slice(0, 400),
901
- // 详情面板用:命令输出 / 搜索结果等,最多 8KB
902
- detail: toolResult.content.slice(0, 8000),
903
- }),
896
+ onStep: (step) => send('step', { step }),
897
+ onToolStart: (name, input, id) => {
898
+ if (id)
899
+ toolStartedAt.set(id, Date.now());
900
+ send('tool_start', {
901
+ id: id ?? name,
902
+ name,
903
+ detail: summarizeInput(input),
904
+ input,
905
+ });
906
+ },
907
+ onToolEnd: (name, toolResult, id) => {
908
+ const key = id ?? name;
909
+ const began = toolStartedAt.get(key);
910
+ toolStartedAt.delete(key);
911
+ send('tool_end', {
912
+ id: key,
913
+ name,
914
+ ok: toolResult.ok,
915
+ ms: began ? Date.now() - began : undefined,
916
+ display: toolResult.display ?? '',
917
+ error: toolResult.ok ? '' : toolResult.content.slice(0, 400),
918
+ // 详情面板用:命令输出 / 搜索结果等,最多 8KB
919
+ detail: toolResult.content.slice(0, 8000),
920
+ });
921
+ },
922
+ onToolOutput: (chunk, stream, id) => {
923
+ if (!id)
924
+ return;
925
+ // 实时输出直接进 DOM,先去掉 ANSI 颜色/光标控制符
926
+ const clean = chunk.replace(/\u001b\[[0-9;?]*[ -/]*[@-~]/g, '').replace(/\r/g, '');
927
+ if (clean)
928
+ send('tool_output', { id, chunk: clean, stream });
929
+ },
904
930
  onNotice: (message) => send('status', { message }),
905
931
  onCompact: () => send('status', { message: '已压缩历史对话' }),
906
932
  onRetry: (info) => send('status', { message: `网络重试 ${info.attempt}:${info.reason}` }),