import { friendlyToolDescription } from '../loop/toolPresentation.js'; import type { Message, ToolCall } from '../types.js'; import { conciseToolPath } from '../runtime/toolDisplay.js'; import { activityText } from './activityPresentation.js'; export { conciseToolPath } from '../runtime/toolDisplay.js'; /** Display only: never rewrite the arguments used by tools or /show. */ export function conciseToolDescription(name: string, args: string): string { return liveToolDescription(name, friendlyToolDescription(name, args, conciseToolPath)); } const coordinationLabels = new Map(Object.entries({ AgentWait:'等待子任务回报', AgentList:'查看子任务状态', AgentMessage:'向子任务发送补充信息', AgentFollowup:'续接子任务', AgentReport:'提交子任务报告', AgentResolve:'处理子任务交接', TaskOutput:'读取任务回执', })); /** Old event producers may supply a shell command disguised as a friendly label. */ export function liveToolDescription(name: string, friendly?: string): string { const coordination = coordinationLabels.get(name); if (coordination) return coordination; if (name === 'Bash' || name === 'PowerShell') { const generic = name === 'PowerShell' ? '运行 PowerShell 命令' : '运行终端命令'; return friendly?.startsWith('终端 · ') ? activityText(friendly, generic) : generic; } const label = humanizeToolProgress(friendly || name) .replace(/^子任务\[explore\] /, '调研子任务 · ') .replace(/^子任务\[general\] /, '执行子任务 · '); return activityText(label, name, 96); } /** 把内核的人话工具描述统一成 TUI 的中文工作进展文案。 */ export function humanizeToolProgress(text: string): string { const replacements: Array<[RegExp, string]> = [ [/^Reading\s+/i, '读取 '], [/^Editing\s+/i, '编辑 '], [/^Writing\s+/i, '写入 '], [/^Running\s+/i, '运行 '], [/^Searching\s+/i, '搜索 '], [/^Globbing\s+/i, '查找 '], [/^Fetching\s+/i, '访问 '], [/^Moving to Trash\s+/i, '移入回收站 '], ]; for (const [pattern, replacement] of replacements) { if (pattern.test(text)) return text.replace(pattern, replacement); } if (/^PresentResult\b/.test(text)) return '提交最终结果'; return text; } /** * 一批工具调用在 scrollback 中只占一条阶段摘要;保留短 id,用户仍可 `/show id`。 */ export function toolBatchSummary(calls: ToolCall[], maxItems = 2): string { const shown = calls.slice(0, maxItems).map((call) => { const friendly = conciseToolDescription(call.function.name, call.function.arguments); return `${friendly} #${call.id.slice(-8)}`; }); const rest = Math.max(0, calls.length - shown.length); return `${shown.join(';')}${rest > 0 ? `;另 ${rest} 项` : ''}`; } /** /show is an explicit detail view. Never pick an arbitrary suffix collision. */ export function showToolDetails(history: Message[], suffix: string): string { const results = history.filter((m): m is Extract => m.role === 'tool'); const calls = new Map(history.flatMap(m => m.role === 'assistant' ? (m.tool_calls ?? []).map(c => [c.id,c] as const) : [])); const query = suffix.trim().replace(/^#/, ''); if (!query) { const recent = results.slice(-20); return recent.length ? ['最近的工具回执 · 输入 /show <编号> 查看参数与原文', ...recent.map(m => { const call = calls.get(m.tool_call_id); return `#${m.tool_call_id.slice(-8)} ${call ? conciseToolDescription(call.function.name, call.function.arguments) : '工具回执'}`; })].join('\n') : '当前会话还没有工具回执。'; } const matches = results.filter(m => m.tool_call_id.endsWith(query)); if (!matches.length) return '没有找到匹配的工具回执。输入 /show 查看最近的编号。'; if (matches.length !== 1) return '编号不唯一,请使用更长的编号:\n' + matches.map(m => m.tool_call_id).join('\n'); const hit = matches[0]!, call = calls.get(hit.tool_call_id); return [`工具详情 · ${call?.function.name ?? '工具'} · #${hit.tool_call_id.slice(-8)}`, ...(call ? ['调用参数',call.function.arguments] : []),'返回原文',hit.content].join('\n'); } export type ToolHistoryVisibility = 'hidden' | 'compact' | 'full'; /** 成功的普通工具结果不写 scrollback;失败和高价值结果仍保留。 */ export function toolHistoryVisibility(toolName: string | undefined, content: string, ok?: boolean): ToolHistoryVisibility { if (toolName === 'Advisor' || toolName === 'PresentResult' || toolName === 'RememberMemory' || toolName === 'UpdateMemory' || toolName === 'ForgetMemory' || toolName === 'ConfirmMemory' || toolName === 'SetPersona' || toolName === 'ClearPersona') return 'full'; if (toolName === 'Agent') return 'compact'; if (ok === false || (ok === undefined && /^\s*(?:Error:|错误[::]|失败[::])/i.test(content))) return 'full'; return 'hidden'; }