deictic 0.4.0 → 0.6.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.
Files changed (69) hide show
  1. package/API.md +19 -8
  2. package/dist/cli.js +34 -28
  3. package/dist/cli.js.map +1 -1
  4. package/dist/shared/board.js +8 -19
  5. package/dist/shared/board.js.map +1 -1
  6. package/dist/shared/clean.js +105 -0
  7. package/dist/shared/clean.js.map +1 -0
  8. package/dist/shared/endpoints.js +35 -9
  9. package/dist/shared/endpoints.js.map +1 -1
  10. package/dist/shared/find.js +3 -2
  11. package/dist/shared/find.js.map +1 -1
  12. package/dist/shared/frame.js +4 -18
  13. package/dist/shared/frame.js.map +1 -1
  14. package/dist/shared/index.d.ts +8 -4
  15. package/dist/shared/index.d.ts.map +1 -1
  16. package/dist/shared/index7.d.ts +2 -0
  17. package/dist/shared/index7.d.ts.map +1 -1
  18. package/dist/shared/init.js +17 -24
  19. package/dist/shared/init.js.map +1 -1
  20. package/dist/shared/local.js +2 -1
  21. package/dist/shared/local.js.map +1 -1
  22. package/dist/shared/packages.js +9 -2
  23. package/dist/shared/packages.js.map +1 -1
  24. package/dist/shared/recording.js +4 -27
  25. package/dist/shared/recording.js.map +1 -1
  26. package/dist/shared/src.js.map +1 -1
  27. package/dist/shared/task.js +43 -10
  28. package/dist/shared/task.js.map +1 -1
  29. package/dist/shared/tasks.js +109 -115
  30. package/dist/shared/tasks.js.map +1 -1
  31. package/dist/shared/transcribe.js +1 -128
  32. package/dist/shared/transcribe2.js +119 -0
  33. package/dist/shared/transcribe2.js.map +1 -0
  34. package/dist/shared/vb.js +2 -3
  35. package/dist/shared/vb.js.map +1 -1
  36. package/dist/shared/voice2.js +4 -17
  37. package/dist/shared/voice2.js.map +1 -1
  38. package/dist/shared/watch.js +56 -39
  39. package/dist/shared/watch.js.map +1 -1
  40. package/dist/tailwind/index.js +6 -2
  41. package/dist/tailwind/index.js.map +1 -1
  42. package/dist/tasks/client.d.ts.map +1 -1
  43. package/dist/tasks/client.js +4 -5
  44. package/dist/tasks/client.js.map +1 -1
  45. package/dist/tasks/index.js +1 -1
  46. package/package.json +2 -1
  47. package/skills/deictic/SKILL.md +5 -3
  48. package/skills/deictic/references/tasks.md +6 -5
  49. package/src/cli/board.ts +7 -13
  50. package/src/cli/clean.ts +104 -0
  51. package/src/cli/find.ts +3 -1
  52. package/src/cli/frame.ts +3 -13
  53. package/src/cli/init.ts +16 -20
  54. package/src/cli/recording.ts +15 -21
  55. package/src/cli/task.ts +49 -10
  56. package/src/cli/transcribe.ts +13 -16
  57. package/src/cli/vb.ts +0 -2
  58. package/src/cli/voice.ts +3 -12
  59. package/src/cli/watch.ts +58 -29
  60. package/src/cli.ts +130 -31
  61. package/src/index.ts +8 -4
  62. package/src/plugins/tailwind/index.ts +6 -2
  63. package/src/plugins/tasks/client.ts +4 -5
  64. package/src/plugins/tasks/index.ts +113 -124
  65. package/src/plugins/tasks/local.ts +2 -1
  66. package/src/tools/endpoints.ts +18 -7
  67. package/src/tools/packages.ts +3 -0
  68. package/src/tools/source.ts +25 -5
  69. package/dist/shared/transcribe.js.map +0 -1
@@ -15,7 +15,7 @@
15
15
  */
16
16
  import type { IncomingMessage, ServerResponse } from 'node:http';
17
17
 
18
- import { type Loc, Refusal, type ServerPlugin, type Target } from '#src/index.ts';
18
+ import { type Loc, Refusal, type ServerPlugin, type Source, type Target } from '#src/index.ts';
19
19
 
20
20
  import { History, type Scope } from './history.ts';
21
21
  import { missingMessage } from './packages.ts';
@@ -63,22 +63,33 @@ export function endpoints({ root, plugins, base = '/', open, history = new Histo
63
63
  apply: async ({ plugin: name, target, op }) => {
64
64
  const plugin = pluginOf(name);
65
65
  if (plugin.apply === undefined) throw new Refusal(`${name} changes nothing from its panel: it has no \`apply\``);
66
- const source = sourceAt(root, target);
67
- const before = await readOf(plugin, target);
66
+ const source = sourceOf(target);
67
+ const before = await readOf(plugin, target, source);
68
68
  const { file, line } = target.loc;
69
69
  const label = `${name} ${file}:${line}: ${plugin.describe?.(op, before) ?? 'edit'}`;
70
70
  const result = await plugin.apply(source, target, op);
71
71
  const scope = scopeOf(name, target.loc);
72
- const change = Array.isArray(result)
73
- ? history.commit(label, applyEdits(source, result), scope)
74
- : history.record(label, name, result.done, scope);
72
+ if (!Array.isArray(result)) return { change: history.record(label, name, result.done, scope), model: await readOf(plugin, target) };
73
+ // Edits to a file whose element isn't found would go who knows where: refused, as only they need it.
74
+ if (source instanceof Refusal) throw source;
75
+ const change = history.commit(label, applyEdits(source, result), scope);
75
76
  return { model: await readOf(plugin, target), change };
76
77
  },
77
78
  undo: async (body) => ({ change: await history.undo(scopeIn(body)) }),
78
79
  redo: async (body) => ({ change: await history.redo(scopeIn(body)) }),
79
80
  };
81
+ // The source or why it can't be had, for the plugin to decide: only an edit to the file needs it, not a task.
82
+ const sourceOf = (target: Target): Source | Refusal => {
83
+ try {
84
+ return sourceAt(root, target);
85
+ } catch (e) {
86
+ if (e instanceof Refusal) return e;
87
+ throw e;
88
+ }
89
+ };
80
90
  // A plugin without `read` has no model, and no need for its element to be where the page says.
81
- const readOf = (plugin: ServerPlugin, target: Target) => (plugin.read === undefined ? null : plugin.read(sourceAt(root, target), target));
91
+ const readOf = (plugin: ServerPlugin, target: Target, source?: Source | Refusal) =>
92
+ plugin.read === undefined ? null : plugin.read(source ?? sourceOf(target), target);
82
93
  // One write at a time: a plugin's change may wait on the network, and an undo sent meanwhile must
83
94
  // take back that change, not the one before it.
84
95
  let queue: Promise<unknown> = Promise.resolve();
@@ -77,6 +77,9 @@ export function installCommand(manager: PackageManager, names: readonly string[]
77
77
  /** What runs a package's CLI without installing it, for the manager: `npx`, `bunx`. */
78
78
  export const runnerOf = (manager: PackageManager) => ({ npm: 'npx', pnpm: 'pnpm dlx', yarn: 'npx', bun: 'bunx' })[manager];
79
79
 
80
+ /** What runs a CLI the project installed, for the manager: `npx`, `bunx`, each the project's own version. */
81
+ export const execOf = (manager: PackageManager) => ({ npm: 'npx', pnpm: 'pnpm exec', yarn: 'yarn', bun: 'bunx' })[manager];
82
+
80
83
  /** Whether the project can import `name`: it's found from the root, as the bundler and deictic's own imports find it. */
81
84
  export function installed(root: string, name: string): boolean {
82
85
  try {
@@ -29,18 +29,38 @@ export function readSource(root: string, file: string): string {
29
29
  }
30
30
  }
31
31
 
32
- /** The target's file and element, as they are on disk now. */
32
+ /**
33
+ * The target's file and element, as they are on disk now. The element is the one the page's mark names, or else the
34
+ * only one of its tag on that line: the page may count columns in code a plugin before deictic's changed.
35
+ */
33
36
  export function sourceAt(root: string, target: Target): Source {
34
37
  const { file, line, column } = target.loc;
35
38
  const code = readSource(root, file);
36
- const offset = offsetOf(lineStarts(code), line, column);
39
+ const lines = lineStarts(code);
40
+ const offset = offsetOf(lines, line, column);
37
41
  const { program, elements } = ASTRO.test(file) ? astro(root, file, code) : jsx(file, code);
38
- const element = elements.find((el) => el.start === offset && isHost(el));
39
- if (element === undefined || (element.name.type === 'JSXIdentifier' && element.name.name !== target.tag))
40
- throw new Refusal(`no <${target.tag}> at ${file}:${line}:${column} any more: the file changed since the page loaded, so reload it`);
42
+ const tagged = elements.filter((el) => isHost(el) && el.name.type === 'JSXIdentifier' && el.name.name === target.tag);
43
+ const start = lines[line - 1];
44
+ const onLine = start === undefined ? [] : tagged.filter((el) => el.start >= start && el.start < (lines[line] ?? Infinity));
45
+ const element = tagged.find((el) => el.start === offset) ?? (onLine.length === 1 ? onLine[0] : undefined);
46
+ if (element === undefined) throw new Refusal(notThere(target, code, lines, onLine.length));
41
47
  return { file, code, program, element, read: (other) => readSource(root, other) };
42
48
  }
43
49
 
50
+ /** Why the page's element isn't in the file, from what the file has there: it may not have changed at all. */
51
+ function notThere({ loc: { file, line, column }, tag }: Target, code: string, lines: number[], onLine: number): string {
52
+ const start = lines[line - 1];
53
+ const found =
54
+ start === undefined
55
+ ? `${file} has no line ${line}`
56
+ : onLine === 0
57
+ ? `line ${line} of ${file} has no <${tag}>: it reads \`${clip(code.slice(start, (lines[line] ?? code.length + 1) - 1).trim())}\``
58
+ : `line ${line} of ${file} has ${onLine} <${tag}>s, none at column ${column}`;
59
+ return `${found}. The page isn't running the file as it is: reload it, and if it says this again, a plugin before deictic's changes the file, so put deictic first`;
60
+ }
61
+
62
+ const clip = (text: string) => (text.length > 80 ? `${text.slice(0, 79)}…` : text);
63
+
44
64
  function jsx(file: string, code: string): Parsed {
45
65
  const { program, errors } = parse(file, code);
46
66
  if (errors.length > 0) throw new Refusal(`${file} doesn't parse: fix it first (${errors[0]!.message})`);
@@ -1 +0,0 @@
1
- {"version":3,"file":"transcribe.js","names":[],"sources":["../../src/cli/transcribe.ts"],"sourcesContent":["/**\n * `deictic transcribe`: what was said in voice clips, with when: Whisper on this machine, through transformers.js.\n * For a recording, the times are the recording's, to line up with its timeline. Nothing leaves the machine once the\n * model is downloaded.\n *\n * `--setup` puts @huggingface/transformers and the model in ~/.cache/deictic/transcribe (DEICTIC_CACHE moves it),\n * outside the project; an agent asks the person first. Models: tiny (~40 MB), base (~77 MB, the default), small\n * (~250 MB), large-v3-turbo (~800 MB), or any ONNX Whisper on Hugging Face by its ID. Needs ffmpeg on the PATH.\n */\nimport { spawnSync } from 'node:child_process';\nimport { existsSync, mkdirSync, writeFileSync } from 'node:fs';\nimport { createRequire } from 'node:module';\nimport { homedir } from 'node:os';\nimport { join } from 'node:path';\nimport { pathToFileURL } from 'node:url';\n\nimport { clock, fail, ME, parseArgs } from './board.ts';\nimport { ffmpeg, voiceClips } from './find.ts';\n\nexport const usage = `transcribe <task | replay URL | recording ID | file | clip ID> [--model base] [--language en] [--json] what was said\n transcribe --setup [--model base] installs it, outside the project: ask the person first`;\n\n// Pinned, so a setup installs what this was written against; move it on deliberately.\nconst TRANSFORMERS = '@huggingface/transformers@4.3.2';\nconst MODELS: Record<string, string> = {\n tiny: 'whisper-tiny',\n base: 'whisper-base',\n small: 'whisper-small',\n 'large-v3-turbo': 'whisper-large-v3-turbo',\n};\n\n/** What of transformers.js this uses: its type isn't the project's, as it's installed outside it. */\ninterface Transformers {\n env: { cacheDir: string; allowRemoteModels: boolean };\n pipeline(\n task: 'automatic-speech-recognition',\n model: string,\n options: { dtype: string },\n ): Promise<\n (\n audio: Float32Array,\n options: Record<string, unknown>,\n ) => Promise<{ text: string; chunks?: { timestamp: [number, number | null]; text: string }[] }>\n >;\n}\n\nexport async function run(args: string[]): Promise<void> {\n const { values, positionals } = parseArgs(args, {\n setup: { type: 'boolean' },\n json: { type: 'boolean' },\n model: { type: 'string', default: 'base' },\n language: { type: 'string' },\n });\n const size = values.model;\n const named = MODELS[size];\n if (!size.includes('/') && named === undefined) fail(`--model is one of ${Object.keys(MODELS).join(', ')}, or a model ID`);\n const model = named === undefined ? size : `onnx-community/${named}`;\n const dir = join(process.env.DEICTIC_CACHE ?? join(homedir(), '.cache', 'deictic'), 'transcribe');\n const models = join(dir, 'models');\n const installed = existsSync(join(dir, 'node_modules', '@huggingface', 'transformers', 'package.json'));\n const downloaded = existsSync(join(models, ...model.split('/')));\n const again = `${ME} transcribe`;\n const sized = size === 'base' ? '' : ` --model ${size}`;\n\n if (values.setup) {\n mkdirSync(dir, { recursive: true });\n if (!existsSync(join(dir, 'package.json'))) writeFileSync(join(dir, 'package.json'), '{ \"private\": true }\\n');\n if (!installed) {\n console.log(`Installing ${TRANSFORMERS} in ${dir}…`);\n const windows = process.platform === 'win32';\n const npm = ['install', '--prefix', dir, '--no-audit', '--no-fund', '--save-exact', TRANSFORMERS];\n const run = spawnSync(windows ? `npm ${npm.map((a) => `\"${a}\"`).join(' ')}` : 'npm', windows ? [] : npm, {\n stdio: 'inherit',\n shell: windows,\n });\n if (run.status !== 0) fail(`npm install failed (${run.error?.message ?? `exit ${run.status}`}).`);\n }\n } else if (!installed || !downloaded) {\n fail(\n `Local transcription isn't set up${installed ? ` with the ${size} model` : ''}. Ask the person first: it downloads ` +\n `${installed ? '' : `${TRANSFORMERS} (with ONNX Runtime, ~470 MB) and `}Whisper ${size} into ${dir}, outside ` +\n `the project; after that, audio is transcribed on this machine. On yes: ${again} --setup${sized} (--model small ` +\n `hears better, ~250 MB more). On no, don't ask again this session: work from ${ME} voice's times.`,\n );\n }\n\n const entry = createRequire(join(dir, 'package.json')).resolve('@huggingface/transformers');\n const transformers = (await import(pathToFileURL(entry).href)) as Transformers;\n transformers.env.cacheDir = models;\n // Only setup downloads; a transcription uses what's here.\n transformers.env.allowRemoteModels = values.setup === true;\n if (values.setup) console.log(`Downloading ${model} (quantized) into ${models}…`);\n const asr = await transformers.pipeline('automatic-speech-recognition', model, { dtype: 'q8' });\n if (values.setup) {\n console.log(`Ready: ${again} <task>${sized}`);\n if (positionals[0] === undefined) return;\n }\n\n const results = [];\n for (const c of await voiceClips(positionals[0])) {\n // Whisper takes 16 kHz mono samples; ffmpeg decodes the clip (webm, ogg or mp4) to them.\n const raw = ffmpeg(['-loglevel', 'error', '-i', c.file, '-f', 'f32le', '-ac', '1', '-ar', '16000', '-']).stdout;\n const audio = new Float32Array(raw.buffer.slice(raw.byteOffset, raw.byteOffset + raw.byteLength - (raw.byteLength % 4)));\n const seconds = audio.length / 16000;\n const out = await asr(audio, {\n return_timestamps: true,\n chunk_length_s: 30,\n stride_length_s: 5,\n ...(values.language === undefined ? {} : { language: values.language }),\n });\n const said = (out.chunks ?? [{ timestamp: [0, seconds] as [number, number], text: out.text }]).map(\n ({ timestamp: [from, to], text }) => ({\n from: c.at + from * 1000,\n to: c.at + (to ?? seconds) * 1000,\n text: text.trim(),\n }),\n );\n results.push({ ...c, text: out.text.trim(), said });\n }\n\n if (values.json) {\n console.log(JSON.stringify(results, null, 2));\n return;\n }\n for (const r of results) {\n console.log(`clip ${r.id} (${r.recording === undefined ? 'a voice note' : `in recording ${r.recording}, from ${clock(r.at)}`}):`);\n for (const s of r.said) console.log(` ${clock(s.from).padStart(6)} – ${clock(s.to).padStart(6)} ${s.text}`);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,MAAa,QAAQ;;AAIrB,MAAM,eAAe;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