@pmndre/pine 0.0.0-stage → 0.22.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 (151) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +226 -2
  3. package/bin/pine.js +27 -0
  4. package/docs/SKILL.md +234 -0
  5. package/docs/references/agents.md +454 -0
  6. package/docs/references/coding.md +311 -0
  7. package/docs/references/computer.md +212 -0
  8. package/docs/references/configuration.md +310 -0
  9. package/docs/references/events.md +267 -0
  10. package/docs/references/mcp.md +420 -0
  11. package/docs/references/operations.md +394 -0
  12. package/docs/references/overview.md +195 -0
  13. package/docs/references/security.md +896 -0
  14. package/docs/references/setup.md +385 -0
  15. package/docs/references/sources.md +772 -0
  16. package/docs/references/stack.md +121 -0
  17. package/docs/references/tui.md +575 -0
  18. package/examples/home/.env.example +7 -0
  19. package/examples/home/USER.md +4 -0
  20. package/examples/home/agents/main/agent.json +4 -0
  21. package/examples/home/agents/main/workspace/AGENTS.md +14 -0
  22. package/examples/home/agents/main/workspace/MEMORY.md +4 -0
  23. package/examples/home/agents/main/workspace/SOUL.md +4 -0
  24. package/examples/home/models.json +62 -0
  25. package/examples/home/pine.json +34 -0
  26. package/examples/home/skills/README.txt +7 -0
  27. package/examples/launchd/com.pine.daemon.plist +32 -0
  28. package/examples/systemd/pine.service +22 -0
  29. package/mcp/browser.d.ts +99 -0
  30. package/mcp/browser.js +496 -0
  31. package/mcp/commands.d.ts +20 -0
  32. package/mcp/commands.js +88 -0
  33. package/mcp/computer.d.ts +139 -0
  34. package/mcp/computer.js +698 -0
  35. package/mcp/computer.swift +989 -0
  36. package/mcp/lib.d.ts +23 -0
  37. package/mcp/lib.js +52 -0
  38. package/mcp/web.d.ts +6 -0
  39. package/mcp/web.js +140 -0
  40. package/package.json +32 -4
  41. package/src/access.d.ts +112 -0
  42. package/src/access.js +438 -0
  43. package/src/actions.d.ts +75 -0
  44. package/src/actions.js +167 -0
  45. package/src/admin.d.ts +167 -0
  46. package/src/admin.js +625 -0
  47. package/src/agent-admin.d.ts +24 -0
  48. package/src/agent-admin.js +55 -0
  49. package/src/agents.d.ts +540 -0
  50. package/src/agents.js +2776 -0
  51. package/src/background.d.ts +29 -0
  52. package/src/background.js +100 -0
  53. package/src/cli.d.ts +2 -0
  54. package/src/cli.js +777 -0
  55. package/src/computer.d.ts +30 -0
  56. package/src/computer.js +32 -0
  57. package/src/config.d.ts +396 -0
  58. package/src/config.js +549 -0
  59. package/src/dev/claude-model.d.ts +37 -0
  60. package/src/dev/claude-model.js +204 -0
  61. package/src/env-file.d.ts +4 -0
  62. package/src/env-file.js +40 -0
  63. package/src/goals.d.ts +133 -0
  64. package/src/goals.js +316 -0
  65. package/src/http.d.ts +46 -0
  66. package/src/http.js +509 -0
  67. package/src/limiter.d.ts +32 -0
  68. package/src/limiter.js +84 -0
  69. package/src/login.d.ts +23 -0
  70. package/src/login.js +130 -0
  71. package/src/manual.d.ts +61 -0
  72. package/src/manual.js +443 -0
  73. package/src/mcp-config.d.ts +49 -0
  74. package/src/mcp-config.js +182 -0
  75. package/src/models.d.ts +90 -0
  76. package/src/models.js +274 -0
  77. package/src/net.d.ts +50 -0
  78. package/src/net.js +150 -0
  79. package/src/pine.d.ts +32 -0
  80. package/src/pine.js +134 -0
  81. package/src/programs.d.ts +123 -0
  82. package/src/programs.js +412 -0
  83. package/src/router.d.ts +56 -0
  84. package/src/router.js +164 -0
  85. package/src/sandbox-fs.d.ts +34 -0
  86. package/src/sandbox-fs.js +186 -0
  87. package/src/sandbox-linux.d.ts +96 -0
  88. package/src/sandbox-linux.js +297 -0
  89. package/src/sandbox-setup.d.ts +70 -0
  90. package/src/sandbox-setup.js +216 -0
  91. package/src/security.d.ts +238 -0
  92. package/src/security.js +660 -0
  93. package/src/skills.d.ts +67 -0
  94. package/src/skills.js +163 -0
  95. package/src/snapshots.d.ts +68 -0
  96. package/src/snapshots.js +306 -0
  97. package/src/source-types.d.ts +61 -0
  98. package/src/source-types.js +1 -0
  99. package/src/sources/command.d.ts +4 -0
  100. package/src/sources/command.js +77 -0
  101. package/src/sources/folder.d.ts +6 -0
  102. package/src/sources/folder.js +116 -0
  103. package/src/sources/ftp.d.ts +4 -0
  104. package/src/sources/ftp.js +307 -0
  105. package/src/sources/http.d.ts +4 -0
  106. package/src/sources/http.js +201 -0
  107. package/src/sources/mqtt.d.ts +18 -0
  108. package/src/sources/mqtt.js +97 -0
  109. package/src/sources/telegram.d.ts +22 -0
  110. package/src/sources/telegram.js +283 -0
  111. package/src/sources/webhook.d.ts +4 -0
  112. package/src/sources/webhook.js +52 -0
  113. package/src/sources.d.ts +344 -0
  114. package/src/sources.js +966 -0
  115. package/src/store.d.ts +99 -0
  116. package/src/store.js +277 -0
  117. package/src/timers.d.ts +83 -0
  118. package/src/timers.js +318 -0
  119. package/src/tools.d.ts +11 -0
  120. package/src/tools.js +1297 -0
  121. package/src/tui/app.d.ts +305 -0
  122. package/src/tui/app.js +3195 -0
  123. package/src/tui/client.d.ts +236 -0
  124. package/src/tui/client.js +131 -0
  125. package/src/tui/code-pane.d.ts +68 -0
  126. package/src/tui/code-pane.js +535 -0
  127. package/src/tui/highlight.d.ts +8 -0
  128. package/src/tui/highlight.js +86 -0
  129. package/src/tui/picture.d.ts +206 -0
  130. package/src/tui/picture.js +801 -0
  131. package/src/tui/run.d.ts +8 -0
  132. package/src/tui/run.js +97 -0
  133. package/src/tui/theme.d.ts +32 -0
  134. package/src/tui/theme.js +128 -0
  135. package/src/tui/undo.d.ts +5 -0
  136. package/src/tui/undo.js +50 -0
  137. package/src/tui/voice.d.ts +49 -0
  138. package/src/tui/voice.js +155 -0
  139. package/src/tui/voice.swift +223 -0
  140. package/src/types.d.ts +139 -0
  141. package/src/types.js +1 -0
  142. package/src/undo.d.ts +224 -0
  143. package/src/undo.js +465 -0
  144. package/src/update.d.ts +35 -0
  145. package/src/update.js +130 -0
  146. package/src/util.d.ts +65 -0
  147. package/src/util.js +171 -0
  148. package/src/watching.d.ts +20 -0
  149. package/src/watching.js +68 -0
  150. package/src/workspace-view.d.ts +72 -0
  151. package/src/workspace-view.js +221 -0
@@ -0,0 +1,8 @@
1
+ import { type PineConfig } from "../config.ts";
2
+ /**
3
+ * Open the terminal UI. It attaches to a running daemon; if none answers, it starts one inside
4
+ * this process (logging to data/pine.log) and stops it again on exit.
5
+ */
6
+ export declare function runTui(cfg: PineConfig, opts?: {
7
+ folder?: string;
8
+ }): Promise<void>;
package/src/tui/run.js ADDED
@@ -0,0 +1,97 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import { ProcessTerminal } from "@earendil-works/pi-tui";
4
+ import { loadConfig } from "../config.js";
5
+ import { findServer, listServers, piMcp, serverStates } from "../mcp-config.js";
6
+ import { PineTui } from "./app.js";
7
+ import { Client } from "./client.js";
8
+ const reachable = async (url) => {
9
+ try {
10
+ return (await fetch(url, { signal: AbortSignal.timeout(800) })).ok;
11
+ }
12
+ catch {
13
+ return false;
14
+ }
15
+ };
16
+ /**
17
+ * Open the terminal UI. It attaches to a running daemon; if none answers, it starts one inside
18
+ * this process (logging to data/pine.log) and stops it again on exit.
19
+ */
20
+ export async function runTui(cfg, opts = {}) {
21
+ if (!cfg.http)
22
+ throw new Error("the terminal UI needs the control API; remove \"http\": false from pine.json");
23
+ if (!process.stdout.isTTY || !process.stdin.isTTY)
24
+ throw new Error("the terminal UI needs an interactive terminal");
25
+ const base = `http://${cfg.http.host === "0.0.0.0" ? "127.0.0.1" : cfg.http.host}:${cfg.http.port}`;
26
+ const cleanup = [];
27
+ let token;
28
+ let mode = "attached";
29
+ if (await reachable(`${base}/health`)) {
30
+ const tokenFile = path.join(cfg.dataDir, "http-token");
31
+ token = cfg.http.token ?? fs.readFileSync(tokenFile, "utf8").trim();
32
+ }
33
+ else {
34
+ // A provider called "dev" is the claude -p stand-in; bring it up too if nothing serves it.
35
+ try {
36
+ const models = JSON.parse(fs.readFileSync(path.join(cfg.home, "models.json"), "utf8"));
37
+ const dev = models.providers?.dev?.baseUrl;
38
+ const uses = [cfg.defaults.model, ...Object.values(cfg.agents).map((a) => a.model)].some((m) => m?.startsWith("dev/"));
39
+ if (dev && uses && !(await reachable(`${dev}/models`))) {
40
+ const { startClaudeModel } = await import("../dev/claude-model.js");
41
+ const server = await startClaudeModel({ port: Number(new URL(dev).port) });
42
+ cleanup.push(() => server.close());
43
+ mode = "embedded daemon + claude -p";
44
+ }
45
+ }
46
+ catch {
47
+ // No models.json or no dev provider: nothing to start.
48
+ }
49
+ const { startPine } = await import("../pine.js");
50
+ const pine = await startPine({ config: cfg, logFile: path.join(cfg.dataDir, "pine.log") });
51
+ cleanup.push(() => pine.stop());
52
+ token = pine.token;
53
+ if (mode === "attached")
54
+ mode = "embedded daemon";
55
+ }
56
+ const done = Promise.withResolvers();
57
+ const app = new PineTui({
58
+ client: new Client(base, token),
59
+ terminal: new ProcessTerminal(),
60
+ mode,
61
+ mcp: {
62
+ // Read the files again each time: the daemon may have added a server since this UI started.
63
+ list: async (states = true) => {
64
+ const now = loadConfig(cfg.home);
65
+ if (!states)
66
+ return listServers(now).map((s) => ({ ...s, state: s.enabled ? undefined : "disabled" }));
67
+ const found = await serverStates(now);
68
+ return listServers(now).map((s) => ({ ...s, state: s.enabled ? (found[s.name] ?? (s.remote ? "unknown" : "local")) : "disabled" }));
69
+ },
70
+ login: async (name, onOutput) => {
71
+ const now = loadConfig(cfg.home);
72
+ const server = findServer(now, name);
73
+ if (!server?.url)
74
+ return onOutput(server ? `"${name}" is a local server and has no sign-in` : `no server "${name}"`), 1;
75
+ return (await piMcp(now, { [name]: server }, ["login", name], { capture: true, onOutput })).code;
76
+ },
77
+ },
78
+ onQuit: async () => {
79
+ for (const fn of cleanup.reverse())
80
+ await Promise.resolve(fn()).catch(() => { });
81
+ done.resolve();
82
+ },
83
+ });
84
+ process.on("SIGTERM", () => void app.stop());
85
+ try {
86
+ await app.start();
87
+ // Started as "pine <folder>": the same tabs as always, and one on that folder.
88
+ if (opts.folder)
89
+ await app.openFolder(opts.folder);
90
+ }
91
+ catch (e) {
92
+ await app.stop();
93
+ throw e;
94
+ }
95
+ await done.promise;
96
+ process.exit(0);
97
+ }
@@ -0,0 +1,32 @@
1
+ import { type EditorTheme, Markdown, type MarkdownTheme, type SelectListTheme } from "@earendil-works/pi-tui";
2
+ /** Plain 16/256-colour styling, so the UI follows the terminal's own palette. */
3
+ export declare const c: {
4
+ bold: (s: string) => string;
5
+ dim: (s: string) => string;
6
+ italic: (s: string) => string;
7
+ underline: (s: string) => string;
8
+ inverse: (s: string) => string;
9
+ strike: (s: string) => string;
10
+ red: (s: string) => string;
11
+ green: (s: string) => string;
12
+ yellow: (s: string) => string;
13
+ blue: (s: string) => string;
14
+ magenta: (s: string) => string;
15
+ cyan: (s: string) => string;
16
+ gray: (s: string) => string;
17
+ accent: (s: string) => string;
18
+ bgPanel: (s: string) => string;
19
+ bgUser: (s: string) => string;
20
+ bgAccent: (s: string) => string;
21
+ };
22
+ export declare const selectTheme: SelectListTheme;
23
+ export declare const editorTheme: EditorTheme;
24
+ export declare const markdownTheme: MarkdownTheme;
25
+ export declare const SPINNER: string[];
26
+ /** Markdown from an agent or a tool, drawn: headings, lists, tables, quotes, and code in colour. */
27
+ export declare class Answer extends Markdown {
28
+ private from?;
29
+ private drawn;
30
+ constructor(text: string);
31
+ render(width: number): string[];
32
+ }
@@ -0,0 +1,128 @@
1
+ import { Markdown, visibleWidth } from "@earendil-works/pi-tui";
2
+ import { highlightCode } from "./highlight.js";
3
+ const on = !process.env.NO_COLOR;
4
+ const sgr = (open, close) => (s) => (on ? `\x1b[${open}m${s}\x1b[${close}m` : s);
5
+ /** Plain 16/256-colour styling, so the UI follows the terminal's own palette. */
6
+ export const c = {
7
+ bold: sgr("1", "22"),
8
+ dim: sgr("2", "22"),
9
+ italic: sgr("3", "23"),
10
+ underline: sgr("4", "24"),
11
+ inverse: sgr("7", "27"),
12
+ strike: sgr("9", "29"),
13
+ red: sgr("31", "39"),
14
+ green: sgr("32", "39"),
15
+ yellow: sgr("33", "39"),
16
+ blue: sgr("34", "39"),
17
+ magenta: sgr("35", "39"),
18
+ cyan: sgr("36", "39"),
19
+ gray: sgr("90", "39"),
20
+ accent: sgr("38;5;114", "39"),
21
+ bgPanel: sgr("48;5;236", "49"),
22
+ bgUser: sgr("48;5;237", "49"),
23
+ bgAccent: sgr("48;5;114;30", "49;39"),
24
+ };
25
+ export const selectTheme = {
26
+ selectedPrefix: c.accent,
27
+ selectedText: (s) => c.bold(c.accent(s)),
28
+ description: c.gray,
29
+ scrollInfo: c.gray,
30
+ noMatch: c.gray,
31
+ };
32
+ export const editorTheme = { borderColor: c.gray, selectList: selectTheme };
33
+ /** Marks a line that only held a code block's backticks. A private-use character: no text contains it. */
34
+ const FENCE = "\uE000";
35
+ export const markdownTheme = {
36
+ // From the third level on, the library puts the "###" in front as a piece of its own. It is markup, so it is left out.
37
+ heading: (s) => (/^(?:\x1b\[[0-9;]*m)*#{3,6} (?:\x1b\[[0-9;]*m)*$/.test(s) ? "" : c.bold(c.accent(s))),
38
+ link: c.cyan,
39
+ linkUrl: c.gray,
40
+ code: c.yellow,
41
+ codeBlock: (s) => s,
42
+ // A code block is a bar down its left side with its language above it. The lines of backticks
43
+ // around it are markup, not content: the opening one becomes the language, the others are
44
+ // marked here and left out by Answer below.
45
+ codeBlockBorder: (s) => {
46
+ const lang = s.replace(/^[`~]+/, "").trim();
47
+ return lang ? c.gray(lang) : FENCE;
48
+ },
49
+ codeBlockIndent: c.gray("│ "),
50
+ highlightCode,
51
+ quote: c.italic,
52
+ quoteBorder: c.gray,
53
+ hr: c.gray,
54
+ listBullet: c.accent,
55
+ bold: c.bold,
56
+ italic: c.italic,
57
+ strikethrough: c.strike,
58
+ underline: c.underline,
59
+ };
60
+ export const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
61
+ /** Markdown from an agent or a tool, drawn: headings, lists, tables, quotes, and code in colour. */
62
+ export class Answer extends Markdown {
63
+ from;
64
+ drawn = [];
65
+ constructor(text) {
66
+ super(text, 1, 0, markdownTheme);
67
+ }
68
+ render(width) {
69
+ const lines = super.render(width);
70
+ if (lines !== this.from) {
71
+ this.from = lines;
72
+ this.drawn = lines.some((l) => l.includes(FENCE)) ? lines.filter((l) => !l.includes(FENCE)) : lines;
73
+ }
74
+ return this.drawn;
75
+ }
76
+ }
77
+ // Two things the library's tables leave out, added where it wraps a cell's text to its column:
78
+ // a <br>, which models write for a line break inside a cell, becomes that break; and a column
79
+ // that the table's second line aligns to the right ("---:") or centres (":---:") is set that way.
80
+ const proto = Markdown.prototype;
81
+ const wrapCell = proto.wrapCellText;
82
+ const drawTable = proto.renderTable;
83
+ const inline = proto.renderInlineTokens;
84
+ const BR = /\s*<br\s*\/?>\s*/i;
85
+ if (wrapCell && drawTable && inline) {
86
+ const own = Answer.prototype;
87
+ own.renderTable = function (token, ...rest) {
88
+ // The library first reads every cell once to size the columns, then once more, cell after
89
+ // cell and row by row, to draw them. Counting says which of the two it is doing, and which
90
+ // column a cell is in.
91
+ const cells = (token?.header?.length ?? 0) + (token?.rows ?? []).reduce((n, row) => n + row.length, 0);
92
+ this.table = { align: Array.isArray(token?.align) ? token.align : [], columns: token?.header?.length || 1, cell: 0, sizing: cells, depth: 0 };
93
+ try {
94
+ return drawTable.call(this, token, ...rest);
95
+ }
96
+ finally {
97
+ this.table = undefined;
98
+ }
99
+ };
100
+ own.renderInlineTokens = function (...args) {
101
+ const table = this.table;
102
+ if (!table)
103
+ return inline.apply(this, args);
104
+ table.depth++;
105
+ let text;
106
+ try {
107
+ text = inline.apply(this, args);
108
+ }
109
+ finally {
110
+ table.depth--;
111
+ }
112
+ if (table.depth > 0 || table.sizing <= 0)
113
+ return text;
114
+ table.sizing--;
115
+ // A cell with line breaks is as wide as its longest line, not as all of them in a row.
116
+ return BR.test(text) ? text.split(BR).reduce((a, b) => (visibleWidth(b) > visibleWidth(a) ? b : a)) : text;
117
+ };
118
+ own.wrapCellText = function (text, width, stylePrefix) {
119
+ const lines = text.split(BR).flatMap((part) => wrapCell.call(this, part, width, stylePrefix));
120
+ const side = this.table ? this.table.align[this.table.cell++ % this.table.columns] : undefined;
121
+ if (side !== "right" && side !== "center")
122
+ return lines;
123
+ return lines.map((line) => {
124
+ const free = Math.max(0, width - visibleWidth(line));
125
+ return " ".repeat(side === "right" ? free : Math.floor(free / 2)) + line;
126
+ });
127
+ };
128
+ }
@@ -0,0 +1,5 @@
1
+ import type { UndoResult } from "../undo.ts";
2
+ /** After /undo. */
3
+ export declare function undoText(r: UndoResult): string;
4
+ /** After /redo. */
5
+ export declare function redoText(r: UndoResult): string;
@@ -0,0 +1,50 @@
1
+ import os from "node:os";
2
+ import { c } from "./theme.js";
3
+ // What the conversation says after /undo and /redo: which message, which files, and what the
4
+ // command could not take back. Said every time, because an undo that is believed to have undone
5
+ // more than it did is worse than none.
6
+ const tilde = (p) => (p === os.homedir() ? "~" : p.startsWith(`${os.homedir()}/`) ? `~${p.slice(os.homedir().length)}` : p);
7
+ const plain = (s) => s.replace(/[\x00-\x1f\x7f]/g, " ");
8
+ const quoted = (s) => {
9
+ const line = plain(s).replace(/\s+/g, " ").trim();
10
+ return `“${line.length > 80 ? `${line.slice(0, 79)}…` : line}”`;
11
+ };
12
+ const files = (n) => `${n} file${n === 1 ? "" : "s"}`;
13
+ /** The lines about files. `redo`: they are again as the message left them. */
14
+ function fileLines(r, redo) {
15
+ const out = [];
16
+ const f = r.restored;
17
+ if (f) {
18
+ const n = f.changed + f.removed + f.back;
19
+ const where = tilde(f.root);
20
+ if (!n)
21
+ out.push(c.gray(redo ? `no file had to be changed in ${where}` : `no file had to be restored in ${where}: the message changed none that pine records`));
22
+ else {
23
+ const parts = [f.changed ? `${f.changed} changed back` : "", f.removed ? `${f.removed} removed` : "", f.back ? `${f.back} brought back` : ""].filter(Boolean).join(", ");
24
+ const names = `${f.files.map(plain).join(", ")}${n > f.files.length ? `, and ${n - f.files.length} more` : ""}`;
25
+ out.push(`${c.green(redo ? `${files(n)} in ${where} ${n === 1 ? "is" : "are"} again as the message left ${n === 1 ? "it" : "them"}` : `${files(n)} restored in ${where}`)}${c.gray(` (${parts}): ${names}`)}`);
26
+ }
27
+ if (f.left.length)
28
+ out.push(c.yellow(`left as ${f.left.length === 1 ? "it is" : "they are"}, too large to be recorded: ${f.left.slice(0, 8).map(plain).join(", ")}`));
29
+ }
30
+ if (r.note)
31
+ out.push(c.yellow(r.note));
32
+ return out;
33
+ }
34
+ /** After /undo. */
35
+ export function undoText(r) {
36
+ if (r.waiting)
37
+ return `${c.accent("↶ withdrawn before it was worked on:")} ${quoted(r.prompt)}\n ${c.gray("the message is in the input again, to change and send")}`;
38
+ return [
39
+ `${c.accent("↶ taken back:")} ${quoted(r.prompt)}`,
40
+ ...(r.stopped ? [c.gray(`what was still running was stopped first (${r.stopped} task${r.stopped === 1 ? "" : "s"})`)] : []),
41
+ ...fileLines(r, false),
42
+ c.gray("not taken back: what commands did outside this folder, files git ignores or that are over 2 MB, and anything that was sent out"),
43
+ c.gray("the message is in the input again, to change and send"),
44
+ c.gray(`/redo brings it back, until you send a new message${r.more ? " · /undo again takes back the message before it" : ""}`),
45
+ ].map((line, i) => (i ? ` ${line}` : line)).join("\n");
46
+ }
47
+ /** After /redo. */
48
+ export function redoText(r) {
49
+ return [`${c.accent("↷ brought back:")} ${quoted(r.prompt)}`, ...fileLines(r, true), c.gray(`/undo takes it back again${r.redo ? ` · /redo brings back ${r.redo === 1 ? "one more message" : `${r.redo} more messages`}` : ""}`)].map((line, i) => (i ? ` ${line}` : line)).join("\n");
50
+ }
@@ -0,0 +1,49 @@
1
+ export declare const VOICE_SOURCE: string;
2
+ export declare const NOT_HERE = "Voice input needs a Mac: it uses the speech recognition that macOS has on the device. On this system pine works as before, by typing.";
3
+ export declare const NO_COMPILER = "Voice input needs Apple's command line developer tools, once, to build its helper, and they are not installed on this Mac. Install them by typing this into a terminal: xcode-select --install (macOS then shows a window to confirm). Then try again.";
4
+ type Run = (file: string, args: string[], ms: number) => Promise<{
5
+ code: number;
6
+ stdout: string;
7
+ stderr: string;
8
+ }>;
9
+ /**
10
+ * The helper as a program: built from its source the first time, then found again by the
11
+ * source's hash, so an update of pine builds a new one. Rejects with a sentence for the owner.
12
+ */
13
+ export declare function buildVoice(dir: string, source?: string, run?: Run): Promise<string>;
14
+ /** How the UI gets something that listens. A test puts a stand-in here. */
15
+ export interface VoiceTools {
16
+ /** Why there is no voice input here, in a sentence for the owner. Undefined: there is. */
17
+ unavailable?: string;
18
+ /** The command that listens: it writes what it hears as lines of JSON and ends when its input is closed. May take a while the first time (the helper is built). */
19
+ program(): Promise<string[]>;
20
+ }
21
+ /** Voice input as this system has it: the helper on a Mac, a sentence everywhere else. */
22
+ export declare function voiceTools(platform?: string, dir?: string): VoiceTools;
23
+ export interface Heard {
24
+ /** Listening has begun. */
25
+ ready?(): void;
26
+ /** Everything heard so far. The end of it may still change. */
27
+ partial(text: string): void;
28
+ /** The system is fetching its speech model for the language, which it does once. */
29
+ model?(state: string): void;
30
+ /** Over: the whole text, or why it could not be done. Called once. */
31
+ done(text: string | undefined, error?: string): void;
32
+ }
33
+ /**
34
+ * One time of listening. `stop()` says "that was all": the helper finishes what it heard and
35
+ * `done` gets the text. `cancel()` ends it and gives nothing.
36
+ */
37
+ export declare class Listening {
38
+ private child?;
39
+ private over;
40
+ private last;
41
+ private heard;
42
+ constructor(command: string[], language: string | undefined, heard: Heard);
43
+ get active(): boolean;
44
+ stop(): void;
45
+ cancel(keep?: boolean): void;
46
+ private end;
47
+ private line;
48
+ }
49
+ export {};
@@ -0,0 +1,155 @@
1
+ // Dictating a message: the terminal UI's side of voice input.
2
+ //
3
+ // The words are recognised on the owner's own Mac by the system's speech recognition, through a
4
+ // small helper (voice.swift) that this file builds with `swiftc` on first use and keeps. Nothing
5
+ // is sent anywhere, and nothing of this runs in the daemon: the microphone is where the owner
6
+ // sits, which is where the UI runs.
7
+ //
8
+ // On a system that is not macOS there is no such recognition to use. Then nothing is built and
9
+ // nothing is started: `voiceTools()` says why in a sentence, and the UI shows that sentence.
10
+ import { execFile, spawn } from "node:child_process";
11
+ import { createHash } from "node:crypto";
12
+ import fs from "node:fs";
13
+ import os from "node:os";
14
+ import path from "node:path";
15
+ export const VOICE_SOURCE = path.join(import.meta.dirname, "voice.swift");
16
+ /**
17
+ * What the helper says when the system asks the owner whether it may listen. A program without
18
+ * these words is ended by the system the moment it asks for the microphone.
19
+ */
20
+ const INFO = `<?xml version="1.0" encoding="UTF-8"?>
21
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
22
+ <plist version="1.0"><dict>
23
+ <key>CFBundleIdentifier</key><string>dev.pine.voice</string>
24
+ <key>CFBundleName</key><string>pine voice</string>
25
+ <key>NSMicrophoneUsageDescription</key><string>pine listens while you dictate a message.</string>
26
+ <key>NSSpeechRecognitionUsageDescription</key><string>pine turns what you say into text for the message you are writing. The words are recognised on this Mac.</string>
27
+ </dict></plist>
28
+ `;
29
+ export const NOT_HERE = "Voice input needs a Mac: it uses the speech recognition that macOS has on the device. On this system pine works as before, by typing.";
30
+ export const NO_COMPILER = "Voice input needs Apple's command line developer tools, once, to build its helper, and they are not installed on this Mac. Install them by typing this into a terminal: xcode-select --install (macOS then shows a window to confirm). Then try again.";
31
+ const runProgram = (file, args, ms) => new Promise((resolve) => {
32
+ execFile(file, args, { timeout: ms, maxBuffer: 4 * 1024 * 1024 }, (err, stdout, stderr) => resolve({ code: err ? (typeof err.code === "number" ? err.code : 1) : 0, stdout: String(stdout), stderr: String(stderr) }));
33
+ });
34
+ /**
35
+ * The helper as a program: built from its source the first time, then found again by the
36
+ * source's hash, so an update of pine builds a new one. Rejects with a sentence for the owner.
37
+ */
38
+ export async function buildVoice(dir, source = VOICE_SOURCE, run = runProgram) {
39
+ const file = path.join(dir, `pine-voice-${createHash("sha256").update(fs.readFileSync(source)).update(INFO).digest("hex").slice(0, 12)}`);
40
+ if (fs.existsSync(file))
41
+ return file;
42
+ // /usr/bin/swiftc exists on every Mac, but without the tools it only opens a window that offers to install them.
43
+ const tools = await run("/usr/bin/xcode-select", ["-p"], 10_000);
44
+ if (tools.code !== 0 || !fs.existsSync(tools.stdout.trim()))
45
+ throw new Error(NO_COMPILER);
46
+ fs.mkdirSync(dir, { recursive: true });
47
+ const tmp = `${file}.${process.pid}.tmp`;
48
+ const info = `${tmp}.plist`;
49
+ fs.writeFileSync(info, INFO);
50
+ // Its reasons for listening are put into the program itself, where the system looks for them.
51
+ const built = await run("/usr/bin/swiftc", ["-swift-version", "5", "-O", "-o", tmp, source, "-Xlinker", "-sectcreate", "-Xlinker", "__TEXT", "-Xlinker", "__info_plist", "-Xlinker", info], 300_000);
52
+ fs.rmSync(info, { force: true });
53
+ if (built.code !== 0 || !fs.existsSync(tmp)) {
54
+ fs.rmSync(tmp, { force: true });
55
+ const why = built.stderr.split("\n").filter((l) => /error/i.test(l)).slice(0, 3).join(" | ") || built.stderr.trim().split("\n").slice(-2).join(" | ");
56
+ throw new Error(`pine could not build its helper for voice input (${why.slice(0, 400) || "the compiler gave no reason"}). Apple's command line developer tools may be missing a part or need an update: xcode-select --install, or Software Update.`);
57
+ }
58
+ // Whole or not at all: another window of pine may be building the same program at this moment.
59
+ fs.renameSync(tmp, file);
60
+ for (const old of fs.readdirSync(dir))
61
+ if (old.startsWith("pine-voice-") && path.join(dir, old) !== file && !old.includes(".tmp"))
62
+ fs.rmSync(path.join(dir, old), { force: true });
63
+ return file;
64
+ }
65
+ /** Voice input as this system has it: the helper on a Mac, a sentence everywhere else. */
66
+ export function voiceTools(platform = process.platform, dir = path.join(os.homedir(), "Library", "Caches", "pine-voice")) {
67
+ if (platform !== "darwin")
68
+ return { unavailable: NOT_HERE, program: () => Promise.reject(new Error(NOT_HERE)) };
69
+ let built;
70
+ return { program: async () => [await (built ??= buildVoice(dir).catch((e) => ((built = undefined), Promise.reject(e))))] };
71
+ }
72
+ /**
73
+ * One time of listening. `stop()` says "that was all": the helper finishes what it heard and
74
+ * `done` gets the text. `cancel()` ends it and gives nothing.
75
+ */
76
+ export class Listening {
77
+ child;
78
+ over = false;
79
+ last = "";
80
+ heard;
81
+ constructor(command, language, heard) {
82
+ this.heard = heard;
83
+ let child;
84
+ try {
85
+ child = spawn(command[0], [...command.slice(1), ...(language ? ["--locale", language] : [])], { stdio: ["pipe", "pipe", "ignore"] });
86
+ }
87
+ catch (e) {
88
+ this.end(undefined, `voice input could not be started (${e instanceof Error ? e.message : String(e)})`);
89
+ return;
90
+ }
91
+ this.child = child;
92
+ let buf = "";
93
+ child.stdin?.on("error", () => { });
94
+ child.stdout?.on("data", (d) => {
95
+ buf += d.toString("utf8");
96
+ for (let i = buf.indexOf("\n"); i >= 0; i = buf.indexOf("\n")) {
97
+ this.line(buf.slice(0, i));
98
+ buf = buf.slice(i + 1);
99
+ }
100
+ });
101
+ child.on("error", (e) => this.end(undefined, `voice input could not be started (${e.message})`));
102
+ // Ended without a last word: what was heard up to then is what there is.
103
+ child.on("close", () => this.end(this.last || undefined, this.last ? undefined : "voice input ended without having heard anything"));
104
+ }
105
+ get active() {
106
+ return !this.over;
107
+ }
108
+ stop() {
109
+ this.child?.stdin?.end();
110
+ // A helper that does not come to an end is ended, and what it had heard is kept.
111
+ setTimeout(() => this.cancel(true), 8000).unref?.();
112
+ }
113
+ cancel(keep = false) {
114
+ if (this.over)
115
+ return;
116
+ const text = keep ? this.last : "";
117
+ this.child?.kill();
118
+ this.end(text || undefined, text || !keep ? undefined : "voice input did not finish");
119
+ }
120
+ end(text, error) {
121
+ if (this.over)
122
+ return;
123
+ this.over = true;
124
+ this.heard.done(text, error);
125
+ }
126
+ line(line) {
127
+ if (this.over || !line.startsWith("{"))
128
+ return;
129
+ let m;
130
+ try {
131
+ m = JSON.parse(line);
132
+ }
133
+ catch {
134
+ return;
135
+ }
136
+ // Words from a microphone are text for a message, and are shown as text: nothing in them may move the cursor.
137
+ const text = typeof m.text === "string" ? m.text.replace(/[\x00-\x1f\x7f]/g, " ").replace(/ {2,}/g, " ").trim() : "";
138
+ if (m.type === "ready")
139
+ this.heard.ready?.();
140
+ else if (m.type === "model")
141
+ this.heard.model?.(String(m.state ?? ""));
142
+ else if (m.type === "partial") {
143
+ this.last = text;
144
+ this.heard.partial(text);
145
+ }
146
+ else if (m.type === "final") {
147
+ this.child?.kill();
148
+ this.end(text || undefined, text ? undefined : "nothing was heard");
149
+ }
150
+ else if (m.type === "error") {
151
+ this.child?.kill();
152
+ this.end(undefined, String(m.message ?? "voice input failed"));
153
+ }
154
+ }
155
+ }