@illuminis/comprism 0.1.8 → 2.4.1

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 (225) hide show
  1. package/out/{graph → build/machine/codemap}/read-python.js +2 -2
  2. package/out/{executor → build/machine/executor}/diagnostics.js +1 -1
  3. package/out/{executor → build/machine/executor}/hooks.js +1 -1
  4. package/out/{executor → build/machine/executor}/sandbox.js +1 -1
  5. package/out/build/machine/line/index.js +339 -0
  6. package/out/{commands/privacy.js → build/machine/runtime/home.js} +10 -19
  7. package/out/build/machine/runtime/needs.js +89 -0
  8. package/out/{lib → build/machine/runtime}/self.js +25 -3
  9. package/out/build/machine/terminal/jobs.js +67 -0
  10. package/out/build/machine/terminal/sign-in.js +212 -0
  11. package/out/build/machine/terminal/steps.js +460 -0
  12. package/out/build/machine/terminal/voice.js +136 -0
  13. package/out/build/machine/voice/record.js +286 -0
  14. package/out/cli-start.js +130 -0
  15. package/out/machine/line/index.js +339 -0
  16. package/out/machine/runtime/self.js +22 -0
  17. package/out/machine/terminal/clipboard.js +182 -0
  18. package/out/machine/terminal/jobs.js +67 -0
  19. package/out/machine/terminal/sign-in.js +212 -0
  20. package/out/machine/terminal/steps.js +460 -0
  21. package/out/machine/terminal/voice.js +136 -0
  22. package/out/machine/voice/record.js +79 -1
  23. package/out/native/dictate +0 -0
  24. package/out/server.generated.js +6 -0
  25. package/package.json +8 -40
  26. package/README.md +0 -343
  27. package/out/agent/command.d.ts +0 -157
  28. package/out/agent/command.js +0 -454
  29. package/out/agent/render.d.ts +0 -169
  30. package/out/agent/render.js +0 -689
  31. package/out/agent/session.d.ts +0 -334
  32. package/out/agent/session.js +0 -1232
  33. package/out/commands/agents.d.ts +0 -2
  34. package/out/commands/agents.js +0 -79
  35. package/out/commands/ask.d.ts +0 -1
  36. package/out/commands/ask.js +0 -222
  37. package/out/commands/codemap.d.ts +0 -2
  38. package/out/commands/codemap.js +0 -151
  39. package/out/commands/commands-thin.d.ts +0 -39
  40. package/out/commands/commands-thin.js +0 -244
  41. package/out/commands/config.d.ts +0 -1
  42. package/out/commands/config.js +0 -138
  43. package/out/commands/cost.d.ts +0 -1
  44. package/out/commands/cost.js +0 -167
  45. package/out/commands/decisionEngine.d.ts +0 -1
  46. package/out/commands/decisionEngine.js +0 -66
  47. package/out/commands/hooks.d.ts +0 -1
  48. package/out/commands/hooks.js +0 -83
  49. package/out/commands/install.d.ts +0 -206
  50. package/out/commands/install.js +0 -738
  51. package/out/commands/instructions.d.ts +0 -1
  52. package/out/commands/instructions.js +0 -113
  53. package/out/commands/integrations.d.ts +0 -3
  54. package/out/commands/integrations.js +0 -215
  55. package/out/commands/jobs.d.ts +0 -5
  56. package/out/commands/jobs.js +0 -157
  57. package/out/commands/keys.d.ts +0 -55
  58. package/out/commands/keys.js +0 -344
  59. package/out/commands/login.d.ts +0 -9
  60. package/out/commands/login.js +0 -606
  61. package/out/commands/memory.d.ts +0 -3
  62. package/out/commands/memory.js +0 -113
  63. package/out/commands/permissions.d.ts +0 -1
  64. package/out/commands/permissions.js +0 -94
  65. package/out/commands/plugins.d.ts +0 -4
  66. package/out/commands/plugins.js +0 -192
  67. package/out/commands/privacy.d.ts +0 -1
  68. package/out/commands/providerKey.d.ts +0 -32
  69. package/out/commands/providerKey.js +0 -108
  70. package/out/commands/repl.d.ts +0 -12
  71. package/out/commands/repl.js +0 -2303
  72. package/out/commands/report.d.ts +0 -39
  73. package/out/commands/report.js +0 -115
  74. package/out/commands/review.d.ts +0 -5
  75. package/out/commands/review.js +0 -223
  76. package/out/commands/sessions.d.ts +0 -23
  77. package/out/commands/sessions.js +0 -115
  78. package/out/commands/settings.d.ts +0 -23
  79. package/out/commands/settings.js +0 -153
  80. package/out/commands/skills.d.ts +0 -21
  81. package/out/commands/skills.js +0 -207
  82. package/out/commands/unattended.d.ts +0 -7
  83. package/out/commands/unattended.js +0 -351
  84. package/out/commands/update.d.ts +0 -1
  85. package/out/commands/update.js +0 -123
  86. package/out/commands/welcome.d.ts +0 -25
  87. package/out/commands/welcome.js +0 -223
  88. package/out/commands/worktrees.d.ts +0 -5
  89. package/out/commands/worktrees.js +0 -186
  90. package/out/executor/browser.d.ts +0 -14
  91. package/out/executor/diagnostics.d.ts +0 -2
  92. package/out/executor/documents.d.ts +0 -40
  93. package/out/executor/files.d.ts +0 -2
  94. package/out/executor/git.d.ts +0 -48
  95. package/out/executor/hooks.d.ts +0 -51
  96. package/out/executor/index.d.ts +0 -45
  97. package/out/executor/notebook.d.ts +0 -2
  98. package/out/executor/paths.d.ts +0 -20
  99. package/out/executor/sandbox.d.ts +0 -40
  100. package/out/executor/shell.d.ts +0 -86
  101. package/out/executor/toolservers.d.ts +0 -20
  102. package/out/executor/worktree.d.ts +0 -9
  103. package/out/graph/build.d.ts +0 -45
  104. package/out/graph/facts.d.ts +0 -47
  105. package/out/graph/files.d.ts +0 -45
  106. package/out/graph/read-locales.d.ts +0 -29
  107. package/out/graph/read-python.d.ts +0 -11
  108. package/out/graph/read-typescript.d.ts +0 -16
  109. package/out/graph/sync.d.ts +0 -66
  110. package/out/graph/sync.js +0 -242
  111. package/out/lib/attach.d.ts +0 -106
  112. package/out/lib/attach.js +0 -396
  113. package/out/lib/clipboard.d.ts +0 -23
  114. package/out/lib/commandlist.d.ts +0 -20
  115. package/out/lib/commandlist.js +0 -58
  116. package/out/lib/config.d.ts +0 -94
  117. package/out/lib/config.js +0 -197
  118. package/out/lib/connection.d.ts +0 -73
  119. package/out/lib/connection.js +0 -188
  120. package/out/lib/decision.d.ts +0 -22
  121. package/out/lib/decision.js +0 -50
  122. package/out/lib/fingerprint.d.ts +0 -25
  123. package/out/lib/fingerprint.js +0 -58
  124. package/out/lib/gateway.d.ts +0 -427
  125. package/out/lib/gateway.js +0 -226
  126. package/out/lib/history.d.ts +0 -24
  127. package/out/lib/history.js +0 -137
  128. package/out/lib/ide.d.ts +0 -19
  129. package/out/lib/ide.js +0 -131
  130. package/out/lib/keyboard.d.ts +0 -98
  131. package/out/lib/keyboard.js +0 -390
  132. package/out/lib/lastlogin.d.ts +0 -14
  133. package/out/lib/lastlogin.js +0 -105
  134. package/out/lib/machine.d.ts +0 -21
  135. package/out/lib/machine.js +0 -91
  136. package/out/lib/notify.d.ts +0 -4
  137. package/out/lib/notify.js +0 -52
  138. package/out/lib/output.d.ts +0 -48
  139. package/out/lib/output.js +0 -108
  140. package/out/lib/picker.d.ts +0 -1
  141. package/out/lib/picker.js +0 -158
  142. package/out/lib/project-ops.d.ts +0 -19
  143. package/out/lib/project-ops.js +0 -146
  144. package/out/lib/project.d.ts +0 -28
  145. package/out/lib/prompt.d.ts +0 -37
  146. package/out/lib/prompt.js +0 -142
  147. package/out/lib/queue.d.ts +0 -13
  148. package/out/lib/queue.js +0 -116
  149. package/out/lib/readiness.d.ts +0 -22
  150. package/out/lib/readiness.js +0 -207
  151. package/out/lib/self.d.ts +0 -23
  152. package/out/lib/servicecommand.d.ts +0 -14
  153. package/out/lib/servicecommand.js +0 -74
  154. package/out/lib/sessions.d.ts +0 -19
  155. package/out/lib/sessions.js +0 -221
  156. package/out/lib/startcache.d.ts +0 -13
  157. package/out/lib/startcache.js +0 -78
  158. package/out/lib/stdin.d.ts +0 -32
  159. package/out/lib/stdin.js +0 -117
  160. package/out/lib/store.d.ts +0 -40
  161. package/out/lib/store.js +0 -138
  162. package/out/lib/sync.d.ts +0 -18
  163. package/out/lib/sync.js +0 -81
  164. package/out/lib/types.d.ts +0 -417
  165. package/out/lib/types.js +0 -21
  166. package/out/lib/ui.d.ts +0 -123
  167. package/out/lib/ui.js +0 -299
  168. package/out/lib/version.d.ts +0 -23
  169. package/out/lib/version.js +0 -46
  170. package/out/lib/voice.d.ts +0 -49
  171. package/out/lib/voice.js +0 -236
  172. package/out/lib/words.d.ts +0 -20
  173. package/out/lib/words.js +0 -27
  174. package/out/machine/codemap/build.d.ts +0 -45
  175. package/out/machine/codemap/facts.d.ts +0 -47
  176. package/out/machine/codemap/files.d.ts +0 -45
  177. package/out/machine/codemap/read-locales.d.ts +0 -29
  178. package/out/machine/codemap/read-python.d.ts +0 -11
  179. package/out/machine/codemap/read-typescript.d.ts +0 -16
  180. package/out/machine/executor/browser.d.ts +0 -14
  181. package/out/machine/executor/diagnostics.d.ts +0 -2
  182. package/out/machine/executor/documents.d.ts +0 -40
  183. package/out/machine/executor/files.d.ts +0 -2
  184. package/out/machine/executor/git.d.ts +0 -48
  185. package/out/machine/executor/hooks.d.ts +0 -51
  186. package/out/machine/executor/index.d.ts +0 -45
  187. package/out/machine/executor/notebook.d.ts +0 -2
  188. package/out/machine/executor/paths.d.ts +0 -20
  189. package/out/machine/executor/sandbox.d.ts +0 -40
  190. package/out/machine/executor/shell.d.ts +0 -86
  191. package/out/machine/executor/toolservers.d.ts +0 -20
  192. package/out/machine/executor/worktree.d.ts +0 -9
  193. package/out/machine/folder/project.d.ts +0 -28
  194. package/out/machine/runtime/home.d.ts +0 -2
  195. package/out/machine/runtime/needs.d.ts +0 -30
  196. package/out/machine/runtime/self.d.ts +0 -23
  197. package/out/machine/voice/record.d.ts +0 -38
  198. package/out/postinstall.d.ts +0 -2
  199. package/out/postinstall.js +0 -120
  200. package/out/providers/anthropic.d.ts +0 -22
  201. package/out/providers/anthropic.js +0 -80
  202. package/out/providers/index.d.ts +0 -8
  203. package/out/providers/index.js +0 -84
  204. package/out/providers/openai.d.ts +0 -11
  205. package/out/providers/openai.js +0 -57
  206. package/out/thin.d.ts +0 -2
  207. package/out/thin.js +0 -855
  208. package/scripts/read_python.py +0 -270
  209. /package/out/{graph → build/machine/codemap}/build.js +0 -0
  210. /package/out/{graph → build/machine/codemap}/facts.js +0 -0
  211. /package/out/{graph → build/machine/codemap}/files.js +0 -0
  212. /package/out/{graph → build/machine/codemap}/read-locales.js +0 -0
  213. /package/out/{graph → build/machine/codemap}/read-typescript.js +0 -0
  214. /package/out/{executor → build/machine/executor}/browser.js +0 -0
  215. /package/out/{executor → build/machine/executor}/documents.js +0 -0
  216. /package/out/{executor → build/machine/executor}/files.js +0 -0
  217. /package/out/{executor → build/machine/executor}/git.js +0 -0
  218. /package/out/{executor → build/machine/executor}/index.js +0 -0
  219. /package/out/{executor → build/machine/executor}/notebook.js +0 -0
  220. /package/out/{executor → build/machine/executor}/paths.js +0 -0
  221. /package/out/{executor → build/machine/executor}/shell.js +0 -0
  222. /package/out/{executor → build/machine/executor}/toolservers.js +0 -0
  223. /package/out/{executor → build/machine/executor}/worktree.js +0 -0
  224. /package/out/{lib → build/machine/folder}/project.js +0 -0
  225. /package/out/{lib → build/machine/terminal}/clipboard.js +0 -0
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.liveStart = liveStart;
4
+ exports.liveState = liveState;
5
+ exports.liveStop = liveStop;
6
+ exports.recordMissing = recordMissing;
7
+ exports.recordStart = recordStart;
8
+ exports.recordStop = recordStop;
9
+ exports.recordDiscard = recordDiscard;
10
+ /**
11
+ * Listening on this computer's microphone, on the server's order.
12
+ *
13
+ * Live words: the microphone is streamed to the service's live words address
14
+ * as it is heard, and the words come back as the service recognizes them; the
15
+ * terminal asks for the level and the words so far and draws them. Recording:
16
+ * a plain recording the server writes down when the person presses Enter.
17
+ * Every recording is deleted straight after; nothing is kept.
18
+ */
19
+ const node_crypto_1 = require("node:crypto");
20
+ const record_1 = require("../voice/record");
21
+ const lives = new Map();
22
+ const recordings = new Map();
23
+ /** Start live words: `{server, key}`; null id when it cannot stream here. */
24
+ function liveStart(server, key, listen = '') {
25
+ if ((0, record_1.missing)() || typeof WebSocket === 'undefined')
26
+ return { id: null };
27
+ const address = new URL(server);
28
+ address.protocol = address.protocol === 'https:' ? 'wss:' : 'ws:';
29
+ address.pathname = '/api/v1/front-door/prism_attachments/live';
30
+ const socket = new WebSocket(address.toString());
31
+ socket.binaryType = 'arraybuffer';
32
+ const held = [];
33
+ let ready = false;
34
+ let settle = null;
35
+ const live = { level: 0, words: '', why: '', socket, stop: async () => undefined,
36
+ ended: new Promise((resolve) => { settle = resolve; }) };
37
+ socket.onopen = () => socket.send(JSON.stringify({ type: 'start', token: key, ...(listen ? { listen } : {}) }));
38
+ socket.onerror = () => { live.why = live.why || 'The service could not be reached for live words.'; settle?.(); };
39
+ socket.onclose = () => settle?.();
40
+ socket.onmessage = (event) => {
41
+ let frame;
42
+ try {
43
+ frame = JSON.parse(String(event.data));
44
+ }
45
+ catch {
46
+ return;
47
+ }
48
+ if (frame.type === 'ready') {
49
+ ready = true;
50
+ for (const chunk of held.splice(0))
51
+ socket.send(new Uint8Array(chunk));
52
+ }
53
+ else if (frame.type === 'words' || frame.type === 'done') {
54
+ live.words = frame.text ?? live.words;
55
+ if (frame.type === 'done')
56
+ settle?.();
57
+ }
58
+ else if (frame.type === 'error') {
59
+ live.why = frame.message ?? 'The words could not be written.';
60
+ settle?.();
61
+ }
62
+ };
63
+ try {
64
+ const recording = (0, record_1.startLive)((chunk) => {
65
+ let peak = 0;
66
+ for (let i = 0; i + 1 < chunk.length; i += 2)
67
+ peak = Math.max(peak, Math.abs(chunk.readInt16LE(i)));
68
+ live.level = Math.min(1, peak / 16000);
69
+ if (socket.readyState !== WebSocket.OPEN)
70
+ return;
71
+ if (ready)
72
+ socket.send(new Uint8Array(chunk));
73
+ else
74
+ held.push(chunk);
75
+ });
76
+ live.stop = () => recording.stop();
77
+ }
78
+ catch (err) {
79
+ try {
80
+ socket.close();
81
+ }
82
+ catch { /* gone */ }
83
+ live.why = err.message;
84
+ }
85
+ const id = (0, node_crypto_1.randomUUID)();
86
+ lives.set(id, live);
87
+ return { id };
88
+ }
89
+ /** The level, the words so far and any problem: `{id}`. */
90
+ function liveState(id) {
91
+ const live = lives.get(id);
92
+ return live ? { level: live.level, words: live.words, problem: live.why }
93
+ : { level: 0, words: '', problem: 'not listening' };
94
+ }
95
+ /** Stop live words and get the final words, or null when it could not listen: `{id}`. */
96
+ async function liveStop(id) {
97
+ const live = lives.get(id);
98
+ if (!live)
99
+ return { text: null, problem: 'not listening' };
100
+ lives.delete(id);
101
+ await live.stop();
102
+ try {
103
+ live.socket.send(JSON.stringify({ type: 'stop' }));
104
+ }
105
+ catch { /* closed */ }
106
+ await Promise.race([live.ended, new Promise((r) => setTimeout(r, 6000).unref())]);
107
+ try {
108
+ live.socket.close();
109
+ }
110
+ catch { /* gone */ }
111
+ return { text: live.why && !live.words ? null : live.words.trim(), problem: live.why };
112
+ }
113
+ /** Why recording cannot work here, or null. */
114
+ function recordMissing() {
115
+ return { why: (0, record_1.missing)() };
116
+ }
117
+ /** Start a plain recording: `{}`; its id and file. */
118
+ function recordStart() {
119
+ const rec = (0, record_1.start)();
120
+ const id = (0, node_crypto_1.randomUUID)();
121
+ recordings.set(id, rec);
122
+ return { id, file: rec.file, by: rec.by };
123
+ }
124
+ /** Stop a recording and hand back its file: `{id}`. */
125
+ async function recordStop(id) {
126
+ const rec = recordings.get(id);
127
+ recordings.delete(id);
128
+ if (!rec)
129
+ return { file: '', bytes: 0 };
130
+ return rec.stop();
131
+ }
132
+ /** Delete a recording and its folder: `{file}`. */
133
+ function recordDiscard(file) {
134
+ (0, record_1.discard)(file);
135
+ return { removed: true };
136
+ }
@@ -0,0 +1,286 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.LIVE_RATE = void 0;
37
+ exports.builtInReader = builtInReader;
38
+ exports.recorder = recorder;
39
+ exports.missing = missing;
40
+ exports.start = start;
41
+ exports.discard = discard;
42
+ exports.startLive = startLive;
43
+ /**
44
+ * Recording what a person says, on their own computer, for every client that
45
+ * offers a microphone: the command line and the editor extension.
46
+ *
47
+ * Physical work on the person's machine, so it lives here once rather than in
48
+ * each client (rule 17). Nothing here transcribes: the recording goes to our
49
+ * server, which asks a vendor, and the words come back. See the terminal's
50
+ * `lib/voice.ts` and the service's `transcription.py`.
51
+ *
52
+ * ── Recording without adding a dependency ─────────────────────────────────
53
+ *
54
+ * This drives a recording tool the machine already has rather than a compiled
55
+ * microphone library on every customer's laptop. macOS ships no command line
56
+ * recorder, so on a Mac with neither ffmpeg nor sox this does not work, and
57
+ * `missing` says so in one sentence naming the one command that fixes it.
58
+ *
59
+ * ── Stopping ──────────────────────────────────────────────────────────────
60
+ *
61
+ * Every one of these tools writes a WAV header that is only correct once the
62
+ * recording ends properly, so stopping means an interrupt and then WAITING for
63
+ * the tool to finish writing. Killing it outright leaves a file whose header
64
+ * claims a length it does not have, which every transcriber reads as silence.
65
+ */
66
+ const node_child_process_1 = require("node:child_process");
67
+ const fs = __importStar(require("node:fs"));
68
+ const os = __importStar(require("node:os"));
69
+ const path = __importStar(require("node:path"));
70
+ /** Is this on the PATH at all. */
71
+ function present(command) {
72
+ try {
73
+ return (0, node_child_process_1.spawnSync)('which', [command], { stdio: 'ignore' }).status === 0;
74
+ }
75
+ catch {
76
+ return false;
77
+ }
78
+ }
79
+ /**
80
+ * Sixteen kilohertz, one channel, everywhere.
81
+ *
82
+ * What every speech model wants and nothing more. Recording in studio quality
83
+ * would make a file several times the size, take longer to upload, and be
84
+ * downsampled by the vendor on arrival anyway.
85
+ */
86
+ const RATE = '16000';
87
+ const RECORDERS = [
88
+ // sox first. It is the one built for this, it needs no device name, and it
89
+ // stops cleanly.
90
+ { name: 'sox', command: 'rec', args: (out) => ['-q', '-c', '1', '-r', RATE, out] },
91
+ {
92
+ name: 'ffmpeg',
93
+ command: 'ffmpeg',
94
+ args: (out) => [
95
+ '-loglevel', 'quiet', '-nostdin',
96
+ ...(process.platform === 'darwin'
97
+ ? ['-f', 'avfoundation', '-i', ':default']
98
+ : ['-f', 'alsa', '-i', 'default']),
99
+ // Written as it is heard, not held until the end, so the meter and the
100
+ // words so far have something to read while the person is speaking.
101
+ '-ac', '1', '-ar', RATE, '-flush_packets', '1', '-y', out,
102
+ ],
103
+ },
104
+ // Linux's own, present on most desktop installs.
105
+ { name: 'arecord', command: 'arecord', args: (out) => ['-q', '-f', 'S16_LE', '-c', '1', '-r', RATE, out] },
106
+ ];
107
+ /**
108
+ * The microphone reader that ships inside the package (a Mac program built
109
+ * from native/dictate.swift), so a person installs nothing to speak. It only
110
+ * passes the microphone's sound on; the server does the transcribing.
111
+ */
112
+ function builtInReader() {
113
+ if (process.platform !== 'darwin')
114
+ return null;
115
+ for (const at of [path.join(__dirname, '..', '..', 'native', 'dictate'), path.join(__dirname, '..', 'native', 'dictate')]) {
116
+ if (fs.existsSync(at))
117
+ return at;
118
+ }
119
+ return null;
120
+ }
121
+ /** The first recording tool this machine has, or null. */
122
+ function recorder() {
123
+ return RECORDERS.find((r) => present(r.command)) ?? null;
124
+ }
125
+ /**
126
+ * What to tell somebody who has no recording tool, or null when they do.
127
+ *
128
+ * One sentence and one command. Not a list of three options with trade-offs:
129
+ * somebody who just wanted to say a sentence out loud is not shopping for audio
130
+ * software.
131
+ */
132
+ function missing() {
133
+ if (builtInReader() || recorder())
134
+ return null;
135
+ return process.platform === 'darwin'
136
+ ? 'Speaking needs a recording tool, and macOS does not come with one. '
137
+ + 'Install it once with: brew install sox'
138
+ : 'Speaking needs a recording tool. Install it once with: '
139
+ + 'sudo apt install sox (or your system\'s package manager)';
140
+ }
141
+ /**
142
+ * Start listening.
143
+ *
144
+ * Throws when there is nothing to listen with, which the caller should have
145
+ * headed off with `missing` so the person gets a sentence rather than an error.
146
+ */
147
+ function start() {
148
+ const tool = recorder();
149
+ if (!tool)
150
+ throw new Error(missing() ?? 'No recording tool.');
151
+ const file = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'comprism-voice-')), 'said.wav');
152
+ const child = (0, node_child_process_1.spawn)(tool.command, tool.args(file), {
153
+ stdio: ['ignore', 'ignore', 'ignore'],
154
+ });
155
+ let stopped = null;
156
+ // The recorder never outlives the session. Closing the window or ending the
157
+ // process while it listens would otherwise leave a microphone recording to a
158
+ // file nobody knows about, the worst thing this tool could do.
159
+ const abandon = () => {
160
+ try {
161
+ child.kill('SIGKILL');
162
+ }
163
+ catch { /* already gone */ }
164
+ discard(file);
165
+ };
166
+ const onSignal = (signal) => {
167
+ abandon();
168
+ release();
169
+ process.kill(process.pid, signal);
170
+ };
171
+ const release = () => {
172
+ process.off('exit', abandon);
173
+ process.off('SIGHUP', onSignal);
174
+ process.off('SIGTERM', onSignal);
175
+ };
176
+ process.once('exit', abandon);
177
+ process.once('SIGHUP', onSignal);
178
+ process.once('SIGTERM', onSignal);
179
+ child.once('close', release);
180
+ const stop = () => {
181
+ if (stopped)
182
+ return stopped;
183
+ stopped = new Promise((resolve) => {
184
+ const done = () => {
185
+ let bytes = 0;
186
+ try {
187
+ bytes = fs.statSync(file).size;
188
+ }
189
+ catch {
190
+ bytes = 0;
191
+ }
192
+ resolve({ file, bytes });
193
+ };
194
+ // Interrupt, not kill. Every one of these tools finishes writing the WAV
195
+ // header on an interrupt and none of them does on a kill, and a header
196
+ // that was never finished reads as silence to whoever transcribes it.
197
+ child.once('close', done);
198
+ try {
199
+ child.kill('SIGINT');
200
+ }
201
+ catch {
202
+ done();
203
+ }
204
+ // A tool that has not closed in two seconds is not going to. Taking the
205
+ // file as it stands beats leaving somebody at a prompt that has stopped
206
+ // answering them.
207
+ setTimeout(() => {
208
+ try {
209
+ child.kill('SIGKILL');
210
+ }
211
+ catch { /* already gone */ }
212
+ done();
213
+ }, 2000).unref();
214
+ });
215
+ return stopped;
216
+ };
217
+ return { file, by: tool.name, stop };
218
+ }
219
+ /** Delete the recording and the folder holding it. Always, and immediately. */
220
+ function discard(file) {
221
+ try {
222
+ fs.rmSync(path.dirname(file), { recursive: true, force: true });
223
+ }
224
+ catch { /* a temp file we could not remove is not worth a word to anybody */ }
225
+ }
226
+ // ── Live: the microphone as it is heard, for the service's live words ───────
227
+ /** What the live words service takes: 16 bit samples, one channel, this rate. */
228
+ exports.LIVE_RATE = '24000';
229
+ /** The same tools, writing raw samples to their output instead of a file. */
230
+ function liveArgs(tool) {
231
+ // A test's fake microphone: a recording played in at its real speed, as the
232
+ // browser tests do with their fake audio device. Only ffmpeg reads one.
233
+ const fake = process.env.COMPRISM_FAKE_MIC;
234
+ if (fake && tool.name === 'ffmpeg') {
235
+ return ['-loglevel', 'quiet', '-nostdin', '-re', '-stream_loop', '-1', '-i', fake,
236
+ '-af', 'speechnorm=e=25:r=0.0001:l=1', '-ac', '1', '-ar', exports.LIVE_RATE, '-f', 's16le', '-flush_packets', '1', '-'];
237
+ }
238
+ if (tool.name === 'sox')
239
+ return ['-q', '-t', 'raw', '-b', '16', '-e', 'signed', '-c', '1', '-r', exports.LIVE_RATE, '-'];
240
+ if (tool.name === 'arecord')
241
+ return ['-q', '-t', 'raw', '-f', 'S16_LE', '-c', '1', '-r', exports.LIVE_RATE, '-'];
242
+ return [
243
+ '-loglevel', 'quiet', '-nostdin',
244
+ ...(process.platform === 'darwin' ? ['-f', 'avfoundation', '-i', ':default'] : ['-f', 'alsa', '-i', 'default']),
245
+ // A laptop microphone can arrive at a hundredth of speaking volume, too
246
+ // quiet for the transcriber to notice speech. This lifts quiet speech to a
247
+ // normal level as it is heard, without the second of delay other levelers add.
248
+ '-af', 'speechnorm=e=25:r=0.0001:l=1',
249
+ '-ac', '1', '-ar', exports.LIVE_RATE, '-f', 's16le', '-flush_packets', '1', '-',
250
+ ];
251
+ }
252
+ /** Start listening live. Throws when there is nothing to listen with. */
253
+ function startLive(onSound) {
254
+ const reader = builtInReader();
255
+ const tool = reader ? { name: 'built in microphone reader', command: reader, args: () => [] } : recorder();
256
+ if (!tool)
257
+ throw new Error(missing() ?? 'No recording tool.');
258
+ // The built in reader stops when its input closes, so it is given one.
259
+ const child = reader
260
+ ? (0, node_child_process_1.spawn)(reader, ['--raw', exports.LIVE_RATE], { stdio: ['pipe', 'pipe', 'ignore'] })
261
+ : (0, node_child_process_1.spawn)(tool.command, liveArgs(tool), { stdio: ['ignore', 'pipe', 'ignore'] });
262
+ child.stdout?.on('data', (chunk) => onSound(chunk));
263
+ // As with a file: the microphone never outlives the process that opened it.
264
+ const abandon = () => { try {
265
+ child.kill('SIGKILL');
266
+ }
267
+ catch { /* already gone */ } };
268
+ process.once('exit', abandon);
269
+ let stopped = null;
270
+ const stop = () => {
271
+ if (stopped)
272
+ return stopped;
273
+ stopped = new Promise((resolve) => {
274
+ child.once('close', () => resolve());
275
+ try {
276
+ child.kill('SIGINT');
277
+ }
278
+ catch {
279
+ resolve();
280
+ }
281
+ setTimeout(() => { abandon(); resolve(); }, 500).unref();
282
+ }).then(() => { process.off('exit', abandon); });
283
+ return stopped;
284
+ };
285
+ return { by: tool.name, stop };
286
+ }
@@ -0,0 +1,130 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ /**
38
+ * The terminal tool: a thin shell around CompletionPrism's front door.
39
+ *
40
+ * Everything happens on the server: what each command means, every check,
41
+ * every word printed. This file only sends what the person typed to the front
42
+ * door and shows what comes back, in one live session.
43
+ *
44
+ * The two device helpers it uses, and why each must be on this computer:
45
+ * signIn the sign in, kept in this computer's key store; it is needed to
46
+ * get through the front door at all
47
+ * openLine the line the server orders steps on this computer through
48
+ * (save a file it edited, run a test); it does a step only when
49
+ * the server orders it
50
+ */
51
+ const os = __importStar(require("node:os"));
52
+ const index_1 = require("./build/machine/line/index");
53
+ const signIn = __importStar(require("./build/machine/terminal/sign-in"));
54
+ const server_generated_1 = require("./server.generated");
55
+ // This tool's own version, sent so the server can say when a newer one is out.
56
+ const VERSION = require('../package.json').version;
57
+ function main() {
58
+ const card = signIn.readSignIn();
59
+ const server = (card?.url || server_generated_1.HOME_SERVER).replace(/\/+$/, '');
60
+ // The line the server sends its orders for this computer down.
61
+ const line = card?.workspaceKey
62
+ ? (0, index_1.openLine)({ baseUrl: server, token: () => card.workspaceKey, client: 'terminal', ...(card.tenant ? { tenant: card.tenant } : {}),
63
+ ...(process.env.COMPRISM_LINE_LOG ? { log: (l) => process.stderr.write(`[line] ${l}\n`) } : {}) })
64
+ : null;
65
+ // Keys and window changes go to whichever connection is open.
66
+ let door;
67
+ let ended = false;
68
+ if (process.stdin.isTTY)
69
+ process.stdin.setRawMode(true);
70
+ process.stdin.on('data', (keys) => { if (door?.readyState === WebSocket.OPEN)
71
+ door.send(keys); });
72
+ process.stdout.on('resize', () => {
73
+ if (door?.readyState === WebSocket.OPEN)
74
+ door.send(JSON.stringify({ type: 'resize', cols: process.stdout.columns, rows: process.stdout.rows }));
75
+ });
76
+ connect();
77
+ // The front door: everything typed goes here, everything shown comes back.
78
+ // If the server restarts mid session it is asked again once it answers.
79
+ function connect() {
80
+ door = new WebSocket(`${server.replace(/^http/, 'ws')}/api/v1/front-door/terminal/live?client=terminal&client_version=${VERSION}`);
81
+ door.binaryType = 'arraybuffer';
82
+ door.onopen = () => {
83
+ door.send(JSON.stringify({
84
+ token: card?.workspaceKey ?? '', tenant: card?.tenant, words: process.argv.slice(2), version: VERSION,
85
+ folder: process.cwd(), machine: os.hostname(), term: process.env.TERM,
86
+ termProgram: process.env.TERM_PROGRAM,
87
+ cols: process.stdout.columns, rows: process.stdout.rows,
88
+ }));
89
+ };
90
+ door.onmessage = (event) => {
91
+ if (typeof event.data !== 'string') {
92
+ process.stdout.write(Buffer.from(event.data));
93
+ return;
94
+ }
95
+ const said = JSON.parse(event.data);
96
+ if (said.type === 'save_sign_in')
97
+ signIn.writeSignIn(said.value);
98
+ else if (said.type === 'clear_sign_in')
99
+ signIn.clearSignIn();
100
+ else if (said.type === 'exit')
101
+ finish(said.code ?? 0, said.message);
102
+ };
103
+ door.onerror = () => undefined;
104
+ door.onclose = () => { if (!ended)
105
+ void again(); };
106
+ }
107
+ // The server went away before the command finished: wait for it, then reconnect.
108
+ async function again() {
109
+ process.stderr.write('\r\n CompletionPrism restarted. Reconnecting...\r\n');
110
+ for (let i = 0; i < 120; i++) {
111
+ try {
112
+ if ((await fetch(`${server}/health`)).ok) {
113
+ connect();
114
+ return;
115
+ }
116
+ }
117
+ catch { /* not up yet */ }
118
+ await new Promise((r) => setTimeout(r, 1000));
119
+ }
120
+ finish(1, `CompletionPrism could not be reached at ${server}.`);
121
+ }
122
+ function finish(code, message) {
123
+ ended = true;
124
+ if (message)
125
+ process.stderr.write(`${message}\n`);
126
+ line?.stop();
127
+ process.exit(code);
128
+ }
129
+ }
130
+ main();