okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221merge f-split-ui2 baeb4533b (approved UI designs, context fix, workspace default with rename, autotune progress) into f-split-intc5755ed83

main
8.8 KiB125 linesraw
1/**
2 * An approval question in plain words: what the agent is about to do, as a sentence a person who never opens a terminal can answer.
3 *
4 * `askFor(name, args)` returns `{ id, params, why, detail }`:
5 *   - `id` + `params` name one fixed sentence the page translates (src/lib/ask.ts holds the English templates and `{0}` slots),
6 *   - `why` is an optional second fixed sentence id, `detail` a short plain line (a path) shown beneath it,
7 *   - the exact command or arguments stay on the card under "Details" for experts.
8 * Anything not recognised falls back to a generic question, never to the raw command as the headline.
9 */
10
11const KNOWN_FOLDERS = { documents: 'Documents', downloads: 'Downloads', desktop: 'Desktop', pictures: 'Pictures', music: 'Music', videos: 'Videos' };
12
13const clip = (value, max = 120) => { const t = String(value ?? '').replace(/\s+/g, ' ').trim(); return t.length > max ? `${t.slice(0, max - 1)}…` : t; };
14const unquote = text => String(text ?? '').trim().replace(/^(['"])(.*)\1$/, '$2');
15const baseName = path => { const t = unquote(path).replace(/[\\/]+$/, ''); return t.split(/[\\/]/).pop() || t; };
16const hasExtension = path => /\.[A-Za-z0-9]{1,8}$/.test(baseName(path));
17const isExecutable = path => /\.(exe|msi|bat|cmd|ps1|com|scr|msix|appx|vbs|jar|sh)$/i.test(unquote(path));
18
19/** Words of a command line, quotes kept together. */
20function words(text) {
21  const out = []; const re = /"([^"]*)"|'([^']*)'|(\S+)/g; let m;
22  while ((m = re.exec(text))) out.push(m[1] ?? m[2] ?? m[3]);
23  return out;
24}
25
26/** The first argument that is not a switch (-Name value pairs are skipped by the callers that need them). */
27const firstPath = list => list.find(w => !/^-/.test(w)) ?? '';
28
29function folderAsk(target) {
30  const clean = unquote(target);
31  const known = KNOWN_FOLDERS[baseName(clean).toLowerCase()];
32  if (known) return { id: 'open-known-folder', params: [known], why: 'opens-window', detail: clip(clean, 160) };
33  if (hasExtension(clean)) return { id: 'open-file', params: [baseName(clean)], why: 'opens-window', detail: clip(clean, 160) };
34  return { id: 'open-folder', params: [baseName(clean) || clean], why: 'opens-window', detail: clip(clean, 160) };
35}
36
37function shellAsk(command) {
38  const text = String(command ?? '').trim();
39  // Several steps in one command: say so rather than guessing at one of them.
40  const steps = text.split(/\s*(?:;|&&|\|\|)\s*|\r?\n/).filter(Boolean);
41  const first = steps[0] ?? '';
42  const w = words(first);
43  const verb = (w[0] ?? '').toLowerCase().replace(/\.exe$/, '');
44  const rest = w.slice(1);
45  if (steps.length > 1) {
46    const kinds = new Set(steps.map(s => (words(s)[0] ?? '').toLowerCase()));
47    if (kinds.size > 1) return { id: 'run-steps', params: [steps.length], why: 'run-steps-why', detail: clip(first, 100) };
48  }
49  if (verb === 'start-process' || verb === 'start' || verb === 'saps') {
50    const pathAt = rest.findIndex((x, i) => !/^-/.test(x) && !(i > 0 && /^-(argumentlist|args|workingdirectory|verb|wait)$/i.test(rest[i - 1])));
51    const nameFlag = rest.findIndex(x => /^-(filepath|path)$/i.test(x));
52    const target = nameFlag >= 0 ? rest[nameFlag + 1] : rest[pathAt] ?? '';
53    const lower = baseName(target).toLowerCase();
54    if (/^explorer(\.exe)?$/.test(lower)) {
55      const argAt = rest.findIndex(x => /^-(argumentlist|args)$/i.test(x));
56      const tail = argAt >= 0 ? rest.slice(argAt + 1) : rest.filter((x, i) => i !== (nameFlag >= 0 ? nameFlag + 1 : pathAt));
57      const folder = tail.find(x => !/^-/.test(x) && !/^\/(select|e|root)/i.test(x));
58      return folder ? folderAsk(folder) : { id: 'open-explorer', params: [], why: 'opens-window' };
59    }
60    if (isExecutable(target)) return { id: 'run-program', params: [baseName(target)], why: 'run-program-why', detail: clip(unquote(target), 160) };
61    if (target) return folderAsk(target);
62  }
63  if (verb === 'explorer') return rest.length ? folderAsk(firstPath(rest.filter(x => !/^\/(select|e|root)/i.test(x))) || rest[0]) : { id: 'open-explorer', params: [], why: 'opens-window' };
64  if (verb === 'invoke-item' || verb === 'ii' || verb === 'xdg-open' || verb === 'open') return folderAsk(firstPath(rest));
65  if (verb === 'remove-item' || verb === 'rm' || verb === 'del' || verb === 'erase' || verb === 'rmdir' || verb === 'rd') {
66    const target = firstPath(rest.filter(x => !/^-(recurse|force|r|rf|fr|f|path|literalpath)$/i.test(x)));
67    return { id: 'delete', params: [baseName(target) || 'these files'], why: 'delete-why', detail: clip(unquote(target), 160) };
68  }
69  if (verb === 'new-item' || verb === 'mkdir' || verb === 'md' || verb === 'ni') {
70    const pathAt = rest.findIndex(x => /^-path$/i.test(x));
71    const target = pathAt >= 0 ? rest[pathAt + 1] : firstPath(rest);
72    const asFile = rest.some((x, i) => /^-(itemtype|type)$/i.test(x) && /file/i.test(rest[i + 1] ?? '')) || (verb === 'new-item' && hasExtension(target));
73    return asFile ? { id: 'create-file', params: [baseName(target)], why: 'replaces-if-exists', detail: clip(unquote(target), 160) } : { id: 'create-folder', params: [baseName(target) || target], detail: clip(unquote(target), 160) };
74  }
75  if (['copy-item', 'cp', 'copy'].includes(verb)) return { id: 'copy', params: [baseName(firstPath(rest))], detail: clip(rest.filter(x => !/^-/.test(x)).join('  →  '), 160) };
76  if (['move-item', 'mv', 'move'].includes(verb)) return { id: 'move', params: [baseName(firstPath(rest))], detail: clip(rest.filter(x => !/^-/.test(x)).join('  →  '), 160) };
77  if (['rename-item', 'ren', 'rni'].includes(verb)) return { id: 'rename', params: [baseName(firstPath(rest))], detail: clip(rest.filter(x => !/^-/.test(x)).join('  →  '), 160) };
78  if (/(set-content|add-content|out-file|tee-object)/i.test(first) || />{1,2}\s*\S+/.test(first)) {
79    const redirect = />{1,2}\s*("[^"]+"|'[^']+'|\S+)/.exec(first)?.[1];
80    const all = words(first);
81    const cmdAt = all.findIndex(x => /^(set-content|add-content|out-file|tee-object)$/i.test(x));
82    const after = cmdAt >= 0 ? all.slice(cmdAt + 1) : rest;
83    const pathAt = after.findIndex(x => /^-(path|filepath|literalpath)$/i.test(x));
84    const target = unquote(redirect ?? (pathAt >= 0 ? after[pathAt + 1] : firstPath(after)));
85    return { id: 'write-file', params: [baseName(target) || 'a file'], why: 'replaces-if-exists', detail: clip(target, 160) };
86  }
87  if (verb === 'git') {
88    const sub = (rest[0] ?? '').toLowerCase();
89    if (['commit', 'push', 'pull', 'merge', 'rebase', 'reset', 'checkout', 'clone', 'fetch', 'stash', 'switch', 'tag', 'branch', 'add', 'rm', 'mv', 'cherry-pick', 'revert'].includes(sub)) return { id: 'git', params: [sub], why: 'changes-repo' };
90  }
91  if (['npm', 'pnpm', 'yarn', 'pip', 'pip3', 'winget', 'choco', 'scoop', 'apt', 'apt-get', 'brew', 'cargo', 'dotnet', 'npx'].includes(verb)) {
92    const sub = (rest[0] ?? '').toLowerCase();
93    if (/^(install|i|add|update|upgrade|remove|uninstall|restore|publish)$/.test(sub)) return { id: 'install', params: [verb], why: 'install-why', detail: clip(rest.slice(1).filter(x => !/^-/.test(x)).join(' '), 120) };
94  }
95  if (isExecutable(w[0] ?? '') || /^[.&]/.test(first) && isExecutable(rest[0] ?? '')) return { id: 'run-program', params: [baseName(isExecutable(w[0]) ? w[0] : rest[0])], why: 'run-program-why' };
96  if (['python', 'python3', 'py', 'node', 'pwsh', 'powershell', 'bash', 'sh', 'cmd'].includes(verb)) {
97    const script = rest.find(x => /\.(py|js|mjs|ps1|sh|bat|cmd)$/i.test(x));
98    return script ? { id: 'run-script', params: [baseName(script)], why: 'run-program-why' } : { id: 'run-command', params: [], why: 'run-command-why' };
99  }
100  return { id: 'run-command', params: [], why: 'run-command-why' };
101}
102
103/** @param {string} name tool name @param {object} args @param {string|null} [described] the model's own one-line description */
104export function askFor(name, args = {}, described = null) {
105  const path = args.file_path ?? args.path ?? args.filePath ?? '';
106  switch (name) {
107    case 'write': return { id: 'create-file', params: [baseName(path) || 'a file'], why: 'replaces-if-exists', detail: clip(path, 160) };
108    case 'edit': return { id: 'change-file', params: [baseName(path) || 'a file'], detail: clip(path, 160) };
109    case 'pwsh': case 'bash': {
110      const ask = shellAsk(args.command);
111      // A command nothing above could read: the model's own one-line description is better than "Run a command" alone.
112      if (ask.id === 'run-command' && described) return { ...ask, detail: clip(described, 140) };
113      return ask;
114    }
115    case 'job_kill': return { id: 'stop-task', params: [] };
116    case 'web_fetch': {
117      let host = String(args.url ?? '');
118      try { host = new URL(host).hostname; } catch { /* keep the text */ }
119      return { id: 'open-page', params: [clip(host, 80)], detail: clip(args.url, 160) };
120    }
121    default:
122      if (String(name).startsWith('mcp__')) return { id: 'use-tool', params: [String(name).split('__').slice(1).join(' / ')], why: 'use-tool-why' };
123      return { id: 'use-other', params: [String(name)] };
124  }
125}