| 1 | // Human-readable phrase for a tool call ("leyendo app.js…"). Shared by the CEO |
| 2 | // brain (ceo.js) and by the live chat streaming — so the user is never shown |
| 3 | // raw JSON, and always the same wording. Covers the tool names of Elffuss Code |
| 4 | // (code.*, terminal.*) and Elffuss Claw (fs.*, app.*, web.*, skill.*, memory.*, |
| 5 | // tasks.*) — an unknown tool falls back to a generic "name argument" without |
| 6 | // breaking anything. |
| 7 | // NOTE: the returned strings are user-facing product copy and stay in Spanish. |
| 8 | export function humanizeTool(name, args) { |
| 9 | const p = args?.path, q = args?.query, c = args?.command, n = args?.name; |
| 10 | switch (name) { |
| 11 | case 'code.read': case 'fs.read': return `leyendo ${p}…`; |
| 12 | case 'code.write': case 'fs.write': return `escribiendo ${p}…`; |
| 13 | case 'code.tree': return `explorando ${p || 'el proyecto'}…`; |
| 14 | case 'fs.list': return `explorando ${p || 'la carpeta'}…`; |
| 15 | case 'code.search': return `buscando «${q}»…`; |
| 16 | case 'fs.pick_folder': return 'pidiendo acceso a una carpeta…'; |
| 17 | case 'fs.copy': return `copiando ${args?.pattern || 'archivos'}…`; |
| 18 | case 'fs.watch': return `vigilando ${args?.from || 'una carpeta'}…`; |
| 19 | case 'terminal.run': return `ejecutando: ${c}`; |
| 20 | case 'app.create': return `creando la app «${n}»…`; |
| 21 | case 'app.open': return `abriendo la app «${n}»…`; |
| 22 | case 'skill.create': return `creando la skill «${n}»…`; |
| 23 | case 'memory.save': return `recordando: ${args?.fact}…`; |
| 24 | case 'tasks.add': return 'programando una tarea…'; |
| 25 | case 'web.search': return `buscando en internet «${q}»…`; |
| 26 | case 'web.images': return `buscando imágenes de «${q}»…`; |
| 27 | case 'web.fetch': return `leyendo ${p || args?.url}…`; |
| 28 | default: return name + (p || q || c || n ? ' ' + (p || q || c || n) : ''); |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | // Detects whether the buffer arriving IN STREAM has entered a tool-call block |
| 33 | // (```tool { … }) and, if so, returns a human phrase instead of the raw JSON — |
| 34 | // "preparando una acción…" until the tool name is legible, then "leyendo |
| 35 | // app.js…" as soon as the field shows up, even if the JSON has not closed yet. |
| 36 | export function humanizeStreamPreview(buf) { |
| 37 | if (buf.search(/```/) === -1 && !/^\s*\{\s*"tool"/.test(buf)) return null; |
| 38 | const toolM = buf.match(/"tool"\s*:\s*"([\w.]+)"/); |
| 39 | if (!toolM) return 'preparando una acción…'; |
| 40 | const pathM = buf.match(/"path"\s*:\s*"((?:[^"\\]|\\.)*)"/); |
| 41 | const queryM = buf.match(/"query"\s*:\s*"((?:[^"\\]|\\.)*)"/); |
| 42 | const cmdM = buf.match(/"command"\s*:\s*"((?:[^"\\]|\\.)*)"/); |
| 43 | const nameM = buf.match(/"name"\s*:\s*"((?:[^"\\]|\\.)*)"/); |
| 44 | const out = humanizeTool(toolM[1], { path: pathM?.[1], query: queryM?.[1], command: cmdM?.[1], name: nameM?.[1] }); |
| 45 | // the tool name is already visible but its field (path/query/…) has not |
| 46 | // arrived yet: better the generic one than a half-streamed "leyendo undefined…" |
| 47 | return /undefined/.test(out) ? 'preparando una acción…' : out; |
| 48 | } |