Elffuss BraidHub

utopia-ia/elffuss-claw public

main · 4.5 KiBRawPermalink
1// Frase legible por humano para una tool-call («leyendo app.js…»). Compartido
2// por el cerebro CEO (ceo.js) y el streaming en vivo del chat — así nunca se
3// le enseña JSON crudo al usuario, siempre la misma redacción. Cubre los
4// nombres de herramienta de Elffuss Code (code.*, terminal.*) y de Elffuss
5// Claw (fs.*, app.*, web.*, skill.*, memory.*, tasks.*) — un tool desconocido
6// cae al genérico «nombre argumento» sin romper nada.
7//
8// Localizado: ES por defecto (usuarios españoles intactos), EN de fallback.
9// Autocontenido a propósito — este fichero es IDÉNTICO en Code y en Claw, y
10// Code no tiene i18n.js, así que la tabla de idioma vive aquí.
11const HL = {
12 es: {
13 read: p => `leyendo ${p}…`, write: p => `escribiendo ${p}…`,
14 tree: p => `explorando ${p || 'el proyecto'}…`, list: p => `explorando ${p || 'la carpeta'}…`,
15 search: q => `buscando «${q}»…`, pick: () => 'pidiendo acceso a una carpeta…',
16 copy: x => `copiando ${x || 'archivos'}…`, watch: x => `vigilando ${x || 'una carpeta'}…`,
17 run: c => `ejecutando: ${c}`,
18 appCreate: n => `creando la app «${n}»…`, appOpen: n => `abriendo la app «${n}»…`,
19 skillCreate: n => `creando la skill «${n}»…`, memSave: f => `recordando: ${f}…`,
20 taskAdd: () => 'programando una tarea…',
21 webSearch: q => `buscando en internet «${q}»…`, webImages: q => `buscando imágenes de «${q}»…`,
22 webFetch: u => `leyendo ${u}…`, prep: 'preparando una acción…',
23 },
24 en: {
25 read: p => `reading ${p}…`, write: p => `writing ${p}…`,
26 tree: p => `exploring ${p || 'the project'}…`, list: p => `exploring ${p || 'the folder'}…`,
27 search: q => `searching “${q}”…`, pick: () => 'requesting folder access…',
28 copy: x => `copying ${x || 'files'}…`, watch: x => `watching ${x || 'a folder'}…`,
29 run: c => `running: ${c}`,
30 appCreate: n => `creating the app “${n}”…`, appOpen: n => `opening the app “${n}”…`,
31 skillCreate: n => `creating the skill “${n}”…`, memSave: f => `remembering: ${f}…`,
32 taskAdd: () => 'scheduling a task…',
33 webSearch: q => `searching the web for “${q}”…`, webImages: q => `searching images of “${q}”…`,
34 webFetch: u => `reading ${u}…`, prep: 'preparing an action…',
35 },
36};
37function hl() { return HL[(navigator.language || 'es').slice(0, 2).toLowerCase()] || HL.en; }
38
39export function humanizeTool(name, args) {
40 const p = args?.path, q = args?.query, c = args?.command, n = args?.name;
41 const t = hl();
42 switch (name) {
43 case 'code.read': case 'fs.read': return t.read(p);
44 case 'code.write': case 'fs.write': return t.write(p);
45 case 'code.tree': return t.tree(p);
46 case 'fs.list': return t.list(p);
47 case 'code.search': return t.search(q);
48 case 'fs.pick_folder': return t.pick();
49 case 'fs.copy': return t.copy(args?.pattern);
50 case 'fs.watch': return t.watch(args?.from);
51 case 'terminal.run': return t.run(c);
52 case 'app.create': return t.appCreate(n);
53 case 'app.open': return t.appOpen(n);
54 case 'skill.create': return t.skillCreate(n);
55 case 'memory.save': return t.memSave(args?.fact);
56 case 'tasks.add': return t.taskAdd();
57 case 'web.search': return t.webSearch(q);
58 case 'web.images': return t.webImages(q);
59 case 'web.fetch': return t.webFetch(p || args?.url);
60 default: return name + (p || q || c || n ? ' ' + (p || q || c || n) : '');
61 }
62}
63
64// Detecta si el buffer que va llegando EN STREAMING ha entrado en un bloque de
65// tool-call (```tool { … }) y, si es así, devuelve una frase humana en vez del
66// JSON crudo — «preparando una acción…» hasta que el nombre de la tool sea
67// legible, luego «leyendo app.js…» tan pronto como el campo aparezca, aunque
68// el JSON todavía no haya cerrado.
69export function humanizeStreamPreview(buf) {
70 if (buf.search(/```/) === -1 && !/^\s*\{\s*"tool"/.test(buf)) return null;
71 const toolM = buf.match(/"tool"\s*:\s*"([\w.]+)"/);
72 if (!toolM) return hl().prep;
73 const pathM = buf.match(/"path"\s*:\s*"((?:[^"\\]|\\.)*)"/);
74 const queryM = buf.match(/"query"\s*:\s*"((?:[^"\\]|\\.)*)"/);
75 const cmdM = buf.match(/"command"\s*:\s*"((?:[^"\\]|\\.)*)"/);
76 const nameM = buf.match(/"name"\s*:\s*"((?:[^"\\]|\\.)*)"/);
77 const out = humanizeTool(toolM[1], { path: pathM?.[1], query: queryM?.[1], command: cmdM?.[1], name: nameM?.[1] });
78 // el nombre de la tool ya se ve pero su campo (path/query/…) aún no llegó:
79 // mejor el genérico que un «leyendo undefined…» a medio streamear
80 return /undefined/.test(out) ? hl().prep : out;
81}