@pmndre/pine 0.0.0-stage → 0.22.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 (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
package/src/util.d.ts ADDED
@@ -0,0 +1,65 @@
1
+ export declare const sleep: (ms: number) => Promise<void>;
2
+ export declare function expandHome(p: string): string;
3
+ /** Resolve a config path: `~` expands, relative paths are relative to `base`. */
4
+ export declare function resolvePath(p: string, base: string): string;
5
+ /** MQTT-style topic match: `+` is one level, a trailing `#` is the rest (including nothing). */
6
+ export declare function topicMatches(pattern: string, topic: string): boolean;
7
+ export declare const matchesAny: (patterns: readonly string[], topic: string) => boolean;
8
+ export declare function getPath(obj: unknown, dotted: string): unknown;
9
+ /** Replace `{{a.b}}` with values from `ctx`. Objects render as JSON, missing values as "". */
10
+ export declare function render(template: string, ctx: unknown): string;
11
+ /** Render every string inside a JSON-like value. A string that is exactly one `{{path}}` keeps the value's type. */
12
+ export declare function renderDeep(value: unknown, ctx: unknown): unknown;
13
+ /**
14
+ * Declarative filter. Keys are dotted paths into `ctx`; a plain value means equality, an object
15
+ * may use eq, ne, in, gt, gte, lt, lte, exists, contains, regex. All keys must match.
16
+ */
17
+ export declare function matchWhen(when: Record<string, unknown> | undefined, ctx: unknown): boolean;
18
+ /** "90s", "15m", "2h", "1d" or a number of milliseconds. */
19
+ export declare function parseDuration(v: string | number): number;
20
+ /** A string that is safe as one path component: no dots, no separators, never empty. */
21
+ export declare const safeName: (s: string) => string;
22
+ export declare const truncate: (s: string, n: number) => string;
23
+ export type LogLevel = "debug" | "info" | "warn" | "error" | "silent";
24
+ export interface Logger {
25
+ debug(msg: string, extra?: unknown): void;
26
+ info(msg: string, extra?: unknown): void;
27
+ warn(msg: string, extra?: unknown): void;
28
+ error(msg: string, extra?: unknown): void;
29
+ child(scope: string): Logger;
30
+ }
31
+ export declare function createLogger(level?: LogLevel, scope?: string, sink?: (line: string) => void): Logger;
32
+ export declare const errText: (e: unknown) => string;
33
+ /** Hand a file or a web address to the system: the program the owner set for it opens it. Only ever on the owner's click. */
34
+ export declare function openInSystem(target: string): void;
35
+ /** "vite --port 5173" from "node /Users/x/project/node_modules/.bin/vite --port 5173": what a person would call it. */
36
+ export declare function programName(command: string, max?: number): string;
37
+ /** "12 s", "5 min", "2 h 10 min": how long something has been running. */
38
+ export declare function since(seconds: number): string;
39
+ /**
40
+ * How fast a model went, in tokens a second. `read` is how fast it took in the part of the
41
+ * prompt it had not seen before (what a local server calls prompt processing), `write` how fast
42
+ * it produced its answer, thinking included.
43
+ */
44
+ export interface Speed {
45
+ read?: number;
46
+ write?: number;
47
+ }
48
+ /** What one model call took: tokens and milliseconds for each half, as far as they can be told apart. */
49
+ export interface CallTiming {
50
+ readTokens: number;
51
+ readMs: number;
52
+ writeTokens: number;
53
+ writeMs: number;
54
+ }
55
+ /**
56
+ * Split a model call at its first token. Up to there the server read the prompt (`input` new
57
+ * tokens: what it took from its cache costs next to nothing and is not counted); from there
58
+ * to the end it wrote `output` tokens. A half that is too small to say anything is left out:
59
+ * a handful of new tokens after a cache hit measures the round trip, not the model, and an
60
+ * answer that arrived in one piece has no duration.
61
+ */
62
+ export declare function callTiming(input: number, output: number, startedAt: number, firstTokenAt: number, endedAt: number): CallTiming;
63
+ export declare function speedOf(t: CallTiming | undefined): Speed;
64
+ /** "↑ 1.4k t/s ↓ 42 t/s": up is what the model read, down is what it wrote, as with the token counts. Empty when neither is known. */
65
+ export declare function speedText(s: Speed | undefined): string;
package/src/util.js ADDED
@@ -0,0 +1,171 @@
1
+ import { spawn } from "node:child_process";
2
+ import os from "node:os";
3
+ import path from "node:path";
4
+ export const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
5
+ export function expandHome(p) {
6
+ return p === "~" || p.startsWith("~/") ? path.join(os.homedir(), p.slice(1)) : p;
7
+ }
8
+ /** Resolve a config path: `~` expands, relative paths are relative to `base`. */
9
+ export function resolvePath(p, base) {
10
+ const e = expandHome(p);
11
+ return path.isAbsolute(e) ? e : path.resolve(base, e);
12
+ }
13
+ /** MQTT-style topic match: `+` is one level, a trailing `#` is the rest (including nothing). */
14
+ export function topicMatches(pattern, topic) {
15
+ if (pattern === "#" || pattern === topic)
16
+ return true;
17
+ const p = pattern.split("/");
18
+ const t = topic.split("/");
19
+ for (let i = 0; i < p.length; i++) {
20
+ if (p[i] === "#")
21
+ return i === p.length - 1;
22
+ if (i >= t.length)
23
+ return false;
24
+ if (p[i] !== "+" && p[i] !== t[i])
25
+ return false;
26
+ }
27
+ return p.length === t.length;
28
+ }
29
+ export const matchesAny = (patterns, topic) => patterns.some((p) => topicMatches(p, topic));
30
+ export function getPath(obj, dotted) {
31
+ let cur = obj;
32
+ for (const key of dotted.split(".")) {
33
+ if (cur == null)
34
+ return undefined;
35
+ cur = cur[key];
36
+ }
37
+ return cur;
38
+ }
39
+ const asText = (v) => (v == null ? "" : typeof v === "string" ? v : JSON.stringify(v));
40
+ /** Replace `{{a.b}}` with values from `ctx`. Objects render as JSON, missing values as "". */
41
+ export function render(template, ctx) {
42
+ return template.replace(/\{\{\s*([\w.$/-]+)\s*\}\}/g, (_m, p) => asText(getPath(ctx, p)));
43
+ }
44
+ /** Render every string inside a JSON-like value. A string that is exactly one `{{path}}` keeps the value's type. */
45
+ export function renderDeep(value, ctx) {
46
+ if (typeof value === "string") {
47
+ const whole = value.match(/^\{\{\s*([\w.$/-]+)\s*\}\}$/);
48
+ return whole ? getPath(ctx, whole[1]) : render(value, ctx);
49
+ }
50
+ if (Array.isArray(value))
51
+ return value.map((v) => renderDeep(v, ctx));
52
+ if (value && typeof value === "object")
53
+ return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, renderDeep(v, ctx)]));
54
+ return value;
55
+ }
56
+ /**
57
+ * Declarative filter. Keys are dotted paths into `ctx`; a plain value means equality, an object
58
+ * may use eq, ne, in, gt, gte, lt, lte, exists, contains, regex. All keys must match.
59
+ */
60
+ export function matchWhen(when, ctx) {
61
+ if (!when)
62
+ return true;
63
+ for (const [key, cond] of Object.entries(when)) {
64
+ const v = getPath(ctx, key);
65
+ if (cond === null || typeof cond !== "object" || Array.isArray(cond)) {
66
+ if (JSON.stringify(v) !== JSON.stringify(cond))
67
+ return false;
68
+ continue;
69
+ }
70
+ const c = cond;
71
+ if ("eq" in c && JSON.stringify(v) !== JSON.stringify(c.eq))
72
+ return false;
73
+ if ("ne" in c && JSON.stringify(v) === JSON.stringify(c.ne))
74
+ return false;
75
+ if ("in" in c && !(Array.isArray(c.in) && c.in.some((x) => JSON.stringify(x) === JSON.stringify(v))))
76
+ return false;
77
+ if ("gt" in c && !(v > c.gt))
78
+ return false;
79
+ if ("gte" in c && !(v >= c.gte))
80
+ return false;
81
+ if ("lt" in c && !(v < c.lt))
82
+ return false;
83
+ if ("lte" in c && !(v <= c.lte))
84
+ return false;
85
+ if ("exists" in c && (v !== undefined && v !== null) !== Boolean(c.exists))
86
+ return false;
87
+ if ("contains" in c && !(typeof v === "string" || Array.isArray(v) ? v.includes(c.contains) : false))
88
+ return false;
89
+ if ("regex" in c && !(typeof v === "string" && new RegExp(c.regex, c.flags ?? "").test(v)))
90
+ return false;
91
+ }
92
+ return true;
93
+ }
94
+ /** "90s", "15m", "2h", "1d" or a number of milliseconds. */
95
+ export function parseDuration(v) {
96
+ if (typeof v === "number")
97
+ return v;
98
+ const m = v.trim().match(/^(\d+(?:\.\d+)?)\s*(ms|s|m|h|d)?$/);
99
+ if (!m)
100
+ throw new Error(`invalid duration: ${v}`);
101
+ return Number(m[1]) * { ms: 1, s: 1000, m: 60_000, h: 3_600_000, d: 86_400_000 }[m[2] ?? "ms"];
102
+ }
103
+ /** A string that is safe as one path component: no dots, no separators, never empty. */
104
+ export const safeName = (s) => s.replace(/[^A-Za-z0-9_-]+/g, "_").slice(0, 80) || "_";
105
+ export const truncate = (s, n) => (s.length > n ? `${s.slice(0, n)}… [${s.length - n} more chars]` : s);
106
+ const LEVELS = ["debug", "info", "warn", "error", "silent"];
107
+ export function createLogger(level = "info", scope = "pine", sink = (l) => process.stderr.write(`${l}\n`)) {
108
+ const min = LEVELS.indexOf(level);
109
+ const emit = (lvl) => (msg, extra) => {
110
+ if (LEVELS.indexOf(lvl) < min)
111
+ return;
112
+ const tail = extra === undefined ? "" : ` ${extra instanceof Error ? (extra.stack ?? extra.message) : typeof extra === "string" ? extra : JSON.stringify(extra)}`;
113
+ sink(`${new Date().toISOString()} ${lvl.toUpperCase().padEnd(5)} [${scope}] ${msg}${tail}`);
114
+ };
115
+ return { debug: emit("debug"), info: emit("info"), warn: emit("warn"), error: emit("error"), child: (s) => createLogger(level, `${scope}:${s}`, sink) };
116
+ }
117
+ export const errText = (e) => (e instanceof Error ? e.message : String(e));
118
+ /** Hand a file or a web address to the system: the program the owner set for it opens it. Only ever on the owner's click. */
119
+ export function openInSystem(target) {
120
+ spawn(process.platform === "darwin" ? "open" : "xdg-open", [target], { stdio: "ignore", detached: true })
121
+ .on("error", () => { })
122
+ .unref();
123
+ }
124
+ /** "vite --port 5173" from "node /Users/x/project/node_modules/.bin/vite --port 5173": what a person would call it. */
125
+ export function programName(command, max = 60) {
126
+ const words = command.trim().split(/\s+/);
127
+ const base = (w) => w.split("/").pop() || w;
128
+ // An interpreter in front of a script says less than the script.
129
+ if (words.length > 1 && /^(node|bun|deno|python[\d.]*|ruby|sh|bash|zsh)$/.test(base(words[0])) && !words[1].startsWith("-"))
130
+ words.shift();
131
+ words[0] = base(words[0]);
132
+ const text = words.join(" ");
133
+ return text.length > max ? `${text.slice(0, max - 1)}…` : text;
134
+ }
135
+ /** "12 s", "5 min", "2 h 10 min": how long something has been running. */
136
+ export function since(seconds) {
137
+ if (seconds < 60)
138
+ return `${seconds} s`;
139
+ if (seconds < 3600)
140
+ return `${Math.floor(seconds / 60)} min`;
141
+ const h = Math.floor(seconds / 3600);
142
+ const m = Math.floor((seconds % 3600) / 60);
143
+ return m ? `${h} h ${m} min` : `${h} h`;
144
+ }
145
+ /**
146
+ * Split a model call at its first token. Up to there the server read the prompt (`input` new
147
+ * tokens: what it took from its cache costs next to nothing and is not counted); from there
148
+ * to the end it wrote `output` tokens. A half that is too small to say anything is left out:
149
+ * a handful of new tokens after a cache hit measures the round trip, not the model, and an
150
+ * answer that arrived in one piece has no duration.
151
+ */
152
+ export function callTiming(input, output, startedAt, firstTokenAt, endedAt) {
153
+ const first = firstTokenAt || endedAt;
154
+ const readMs = startedAt ? Math.max(0, first - startedAt) : 0;
155
+ const writeMs = Math.max(0, endedAt - first);
156
+ const reads = input >= 200 && readMs >= 50;
157
+ const writes = output >= 8 && writeMs >= 200;
158
+ return { readTokens: reads ? input : 0, readMs: reads ? readMs : 0, writeTokens: writes ? output : 0, writeMs: writes ? writeMs : 0 };
159
+ }
160
+ export function speedOf(t) {
161
+ if (!t)
162
+ return {};
163
+ return { ...(t.readMs ? { read: (t.readTokens * 1000) / t.readMs } : {}), ...(t.writeMs ? { write: (t.writeTokens * 1000) / t.writeMs } : {}) };
164
+ }
165
+ const rate = (n) => (n >= 10_000 ? `${Math.round(n / 1000)}k` : n >= 1000 ? `${(n / 1000).toFixed(1)}k` : n >= 20 ? String(Math.round(n)) : n.toFixed(1));
166
+ /** "↑ 1.4k t/s ↓ 42 t/s": up is what the model read, down is what it wrote, as with the token counts. Empty when neither is known. */
167
+ export function speedText(s) {
168
+ if (!s)
169
+ return "";
170
+ return [s.read ? `↑ ${rate(s.read)} t/s` : "", s.write ? `↓ ${rate(s.write)} t/s` : ""].filter(Boolean).join(" ");
171
+ }
@@ -0,0 +1,20 @@
1
+ export declare const clip: (s: string, n?: number) => string;
2
+ /** A value as an event carries it: text and large structures cut to a sensible length. */
3
+ export declare function shown(value: unknown): unknown;
4
+ /**
5
+ * A value as a status line may say it. A status line is read by agents without the mark that
6
+ * outside content gets, so it only repeats what cannot carry a sentence: a number, an amount, one word.
7
+ */
8
+ export declare function harmless(value: unknown): string;
9
+ /** A regular expression an agent or owner wrote, or an error that says what is wrong with it. */
10
+ export declare function pattern(source: string): RegExp;
11
+ /** What a pattern finds in a text: its first group if it has one, else the whole match. Nothing found is null. */
12
+ export declare function found(re: RegExp, text: string): string | null;
13
+ /** The lines that are new and the lines that are gone between two texts, a handful of each. */
14
+ export declare function lineDiff(before: string, now: string): {
15
+ added: string[];
16
+ removed: string[];
17
+ more?: number;
18
+ };
19
+ /** What a reader of a web page sees, line by line: no scripts, no styles, no markup. */
20
+ export declare function pageText(html: string): string;
@@ -0,0 +1,68 @@
1
+ // What the source types that look at something again and again (http, command) have in common:
2
+ // picking the part that counts out of what they got, and saying what changed in it.
3
+ const LINES = 30;
4
+ const LINE = 300;
5
+ const VALUE = 4000;
6
+ export const clip = (s, n = VALUE) => (s.length > n ? `${s.slice(0, n)}…` : s);
7
+ /** A value as an event carries it: text and large structures cut to a sensible length. */
8
+ export function shown(value) {
9
+ if (typeof value === "string")
10
+ return clip(value);
11
+ const json = JSON.stringify(value) ?? "null";
12
+ return json.length > VALUE ? clip(json) : value;
13
+ }
14
+ /**
15
+ * A value as a status line may say it. A status line is read by agents without the mark that
16
+ * outside content gets, so it only repeats what cannot carry a sentence: a number, an amount, one word.
17
+ */
18
+ export function harmless(value) {
19
+ if (value === null || value === undefined)
20
+ return "nothing";
21
+ const s = typeof value === "string" ? value : JSON.stringify(value);
22
+ return /^[\d.,:%€$£+\- ]{1,24}$/.test(s) || /^[\w.-]{1,32}$/.test(s) ? s : `a value of ${s.length} characters`;
23
+ }
24
+ /** A regular expression an agent or owner wrote, or an error that says what is wrong with it. */
25
+ export function pattern(source) {
26
+ try {
27
+ return new RegExp(source, "m");
28
+ }
29
+ catch (e) {
30
+ throw new Error(`"match" is not a regular expression: ${e instanceof Error ? e.message : e}`);
31
+ }
32
+ }
33
+ /** What a pattern finds in a text: its first group if it has one, else the whole match. Nothing found is null. */
34
+ export function found(re, text) {
35
+ const m = re.exec(text);
36
+ return m ? clip((m[1] ?? m[0]).trim()) : null;
37
+ }
38
+ /** The lines that are new and the lines that are gone between two texts, a handful of each. */
39
+ export function lineDiff(before, now) {
40
+ const lines = (s) => s.split("\n").map((l) => l.trim()).filter(Boolean);
41
+ const a = lines(before);
42
+ const b = lines(now);
43
+ const had = new Set(a);
44
+ const has = new Set(b);
45
+ const added = b.filter((l) => !had.has(l));
46
+ const removed = a.filter((l) => !has.has(l));
47
+ const more = Math.max(0, added.length - LINES) + Math.max(0, removed.length - LINES);
48
+ return { added: added.slice(0, LINES).map((l) => clip(l, LINE)), removed: removed.slice(0, LINES).map((l) => clip(l, LINE)), ...(more ? { more } : {}) };
49
+ }
50
+ const ENTITIES = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'", nbsp: " ", euro: "€", pound: "£", copy: "©", ndash: "–", mdash: "—", hellip: "…" };
51
+ /** What a reader of a web page sees, line by line: no scripts, no styles, no markup. */
52
+ export function pageText(html) {
53
+ return html
54
+ .replace(/<!--[\s\S]*?-->/g, "")
55
+ .replace(/<(script|style|noscript|template|svg|head)\b[\s\S]*?<\/\1\s*>/gi, "")
56
+ .replace(/<(br|hr)\b[^>]*>|<\/(p|div|li|tr|h[1-6]|section|article|table|ul|ol|dd|dt|header|footer|main|nav|blockquote|pre|title)\s*>/gi, "\n")
57
+ .replace(/<[^>]+>/g, " ")
58
+ .replace(/&(#x?[0-9a-f]+|[a-z]+);/gi, (all, name) => {
59
+ if (name[0] !== "#")
60
+ return ENTITIES[name.toLowerCase()] ?? all;
61
+ const code = name[1].toLowerCase() === "x" ? Number.parseInt(name.slice(2), 16) : Number.parseInt(name.slice(1), 10);
62
+ return Number.isFinite(code) && code > 0 && code < 0x110000 ? String.fromCodePoint(code) : all;
63
+ })
64
+ .split("\n")
65
+ .map((l) => l.replace(/\s+/g, " ").trim())
66
+ .filter(Boolean)
67
+ .join("\n");
68
+ }
@@ -0,0 +1,72 @@
1
+ export interface Change {
2
+ /** Path from the top of the repository. */
3
+ path: string;
4
+ /** "M" changed, "A" added, "D" deleted, "R" renamed, "?" not tracked yet. */
5
+ status: string;
6
+ added?: number;
7
+ removed?: number;
8
+ }
9
+ /**
10
+ * Run git so that it can only look. A repository's own configuration can name programs for git
11
+ * to run (filters, diff drivers, hooks, a file system monitor); a folder somebody cloned must not
12
+ * get to run anything with the daemon's rights just because it was looked at. So git runs in a
13
+ * sandbox that can neither write nor use the network, and with the programs it would start for
14
+ * a diff turned off. Every git call of the daemon goes through here.
15
+ */
16
+ export declare function git(cwd: string, args: string[], opts?: GitOptions): Promise<{
17
+ code: number;
18
+ out: string;
19
+ raw: Buffer;
20
+ err: string;
21
+ }>;
22
+ /**
23
+ * What a git call may do beyond looking. The snapshots behind undo (`src/snapshots.ts`) are the
24
+ * one user: they write into a git directory of pine's own and, for a restore the owner asked
25
+ * for, into the folder itself. `write` is enforced by the sandbox, not by git.
26
+ */
27
+ export interface GitOptions {
28
+ /** Folders git may write in. Without any it can only look. */
29
+ write?: string[];
30
+ stdin?: Buffer | string;
31
+ /** More environment: a git directory, an index file of its own. */
32
+ env?: Record<string, string>;
33
+ maxBytes?: number;
34
+ timeoutMs?: number;
35
+ }
36
+ /** What differs from the last commit in a folder: changed, added, deleted and new files, with line counts. */
37
+ export declare function changes(root: string): Promise<{
38
+ repo: boolean;
39
+ branch?: string;
40
+ top?: string;
41
+ files: Change[];
42
+ more: boolean;
43
+ }>;
44
+ /** The diff of one file against the last commit. A file git does not know yet is shown as all new. */
45
+ export declare function diffOf(root: string, file: string): Promise<{
46
+ diff: string;
47
+ truncated: boolean;
48
+ } | undefined>;
49
+ /**
50
+ * Files of a folder whose path fits what somebody typed, the best first: for pointing an agent at
51
+ * a file by a few letters of its name. In a repository these are the files git knows or would add
52
+ * (ignored ones are left out); elsewhere a walk of bounded size that skips hidden folders and
53
+ * `node_modules`. Nothing outside the folder is named.
54
+ */
55
+ export declare function findFiles(root: string, query: string, limit?: number): Promise<string[]>;
56
+ export interface Entry {
57
+ name: string;
58
+ dir: boolean;
59
+ size: number;
60
+ }
61
+ /** What is in one folder: folders first, then files, by name. Git's own folder is left out. */
62
+ export declare function listDir(root: string, rel: string): {
63
+ entries: Entry[];
64
+ more: boolean;
65
+ } | undefined;
66
+ /** A file's text, for reading. Binary files and very large ones are only described. */
67
+ export declare function readText(root: string, rel: string): {
68
+ text?: string;
69
+ size: number;
70
+ binary: boolean;
71
+ truncated: boolean;
72
+ } | undefined;
@@ -0,0 +1,221 @@
1
+ import { spawn } from "node:child_process";
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+ import { bin, helperArgv, isInside, realish } from "./security.js";
5
+ const MAX_OUT = 2_000_000;
6
+ /**
7
+ * Run git so that it can only look. A repository's own configuration can name programs for git
8
+ * to run (filters, diff drivers, hooks, a file system monitor); a folder somebody cloned must not
9
+ * get to run anything with the daemon's rights just because it was looked at. So git runs in a
10
+ * sandbox that can neither write nor use the network, and with the programs it would start for
11
+ * a diff turned off. Every git call of the daemon goes through here.
12
+ */
13
+ export function git(cwd, args, opts = {}) {
14
+ const none = { out: "", raw: Buffer.alloc(0) };
15
+ const jail = helperArgv(opts.write);
16
+ if (!jail)
17
+ return Promise.resolve({ code: 127, ...none, err: "there is no sandbox on this system to run git in" });
18
+ const safe = ["--no-optional-locks", "-c", "core.fsmonitor=false", "-c", "core.hooksPath=/dev/null", "-c", "core.pager=cat", "-c", "diff.external=", "-c", "safe.directory=*", "-c", "color.ui=false", "-c", "core.quotePath=false"];
19
+ const max = opts.maxBytes ?? MAX_OUT;
20
+ return new Promise((resolve) => {
21
+ const child = spawn(jail[0], [...jail.slice(1), bin("git"), "-C", cwd, ...safe, ...args], { stdio: [opts.stdin === undefined ? "ignore" : "pipe", "pipe", "pipe"], env: { PATH: "/usr/bin:/bin", HOME: process.env.HOME ?? "", GIT_TERMINAL_PROMPT: "0", LC_ALL: "C", ...opts.env } });
22
+ const chunks = [];
23
+ let size = 0;
24
+ let err = "";
25
+ child.stdout.on("data", (d) => {
26
+ size += d.length;
27
+ if (size <= max)
28
+ chunks.push(d);
29
+ else
30
+ child.kill("SIGKILL");
31
+ });
32
+ child.stderr.on("data", (d) => (err.length < 2000 ? (err += d.toString("utf8")) : undefined));
33
+ if (opts.stdin !== undefined) {
34
+ child.stdin.on("error", () => { });
35
+ child.stdin.end(opts.stdin);
36
+ }
37
+ const timer = setTimeout(() => child.kill("SIGKILL"), opts.timeoutMs ?? 15_000);
38
+ child.on("error", (e) => (clearTimeout(timer), resolve({ code: 127, ...none, err: e.message })));
39
+ child.on("close", (code) => {
40
+ clearTimeout(timer);
41
+ const raw = Buffer.concat(chunks);
42
+ resolve({ code: code ?? 1, out: raw.toString("utf8"), raw, err });
43
+ });
44
+ });
45
+ }
46
+ /** The repository a folder lies in, if any: its top folder and the branch that is checked out. */
47
+ async function repository(root) {
48
+ const top = await git(root, ["rev-parse", "--show-toplevel"]);
49
+ if (top.code !== 0 || !top.out.trim())
50
+ return undefined;
51
+ const branch = await git(root, ["rev-parse", "--abbrev-ref", "HEAD"]);
52
+ return { top: realish(top.out.trim()), branch: branch.code === 0 ? branch.out.trim() : "" };
53
+ }
54
+ /** What differs from the last commit in a folder: changed, added, deleted and new files, with line counts. */
55
+ export async function changes(root) {
56
+ const repo = await repository(root);
57
+ if (!repo)
58
+ return { repo: false, files: [], more: false };
59
+ const status = await git(repo.top, ["status", "--porcelain=v1", "-z", "--", root]);
60
+ const files = [];
61
+ const parts = status.out.split("\0");
62
+ for (let i = 0; i < parts.length; i++) {
63
+ const entry = parts[i];
64
+ if (entry.length < 4)
65
+ continue;
66
+ const xy = entry.slice(0, 2);
67
+ const file = entry.slice(3);
68
+ // A rename is followed by the name it had before.
69
+ if (xy.includes("R") || xy.includes("C"))
70
+ i++;
71
+ const code = xy === "??" ? "?" : xy.includes("D") ? "D" : xy.includes("A") ? "A" : xy.includes("R") ? "R" : "M";
72
+ files.push({ path: file, status: code });
73
+ }
74
+ const hasHead = (await git(repo.top, ["rev-parse", "--verify", "--quiet", "HEAD"])).code === 0;
75
+ const counts = await git(repo.top, ["diff", "--numstat", "-z", "--no-ext-diff", "--no-textconv", ...(hasHead ? ["HEAD"] : []), "--", root]);
76
+ for (const line of counts.out.split("\0")) {
77
+ const m = /^(\d+|-)\t(\d+|-)\t(.+)$/.exec(line);
78
+ const file = m && files.find((f) => f.path === m[3]);
79
+ if (m && file && m[1] !== "-")
80
+ Object.assign(file, { added: Number(m[1]), removed: Number(m[2]) });
81
+ }
82
+ // A new file has no counts from git; its lines are all new.
83
+ for (const f of files) {
84
+ if (f.status !== "?" || f.path.endsWith("/"))
85
+ continue;
86
+ try {
87
+ const full = path.join(repo.top, f.path);
88
+ if (fs.statSync(full).size < 1_000_000)
89
+ f.added = fs.readFileSync(full, "utf8").split("\n").length - 1;
90
+ }
91
+ catch {
92
+ // gone since
93
+ }
94
+ }
95
+ return { repo: true, branch: repo.branch, top: repo.top, files: files.slice(0, 300), more: files.length > 300 };
96
+ }
97
+ /** The diff of one file against the last commit. A file git does not know yet is shown as all new. */
98
+ export async function diffOf(root, file) {
99
+ const repo = await repository(root);
100
+ if (!repo || file.includes("\0"))
101
+ return undefined;
102
+ const target = realish(path.join(repo.top, file));
103
+ if (!isInside(realish(root), target) && target !== realish(root))
104
+ return undefined;
105
+ const hasHead = (await git(repo.top, ["rev-parse", "--verify", "--quiet", "HEAD"])).code === 0;
106
+ let r = await git(repo.top, ["diff", "--no-ext-diff", "--no-textconv", ...(hasHead ? ["HEAD"] : []), "--", file]);
107
+ if (!r.out.trim())
108
+ r = await git(repo.top, ["diff", "--no-ext-diff", "--no-textconv", "--no-index", "--", "/dev/null", file]);
109
+ const max = 200_000;
110
+ return { diff: r.out.slice(0, max), truncated: r.out.length > max };
111
+ }
112
+ /** A path inside a folder, with links resolved, or undefined when it leads out of it. */
113
+ function inside(root, rel) {
114
+ if (rel.includes("\0"))
115
+ return undefined;
116
+ const base = realish(root);
117
+ const target = realish(path.join(base, rel));
118
+ return target === base || isInside(base, target) ? target : undefined;
119
+ }
120
+ /**
121
+ * Files of a folder whose path fits what somebody typed, the best first: for pointing an agent at
122
+ * a file by a few letters of its name. In a repository these are the files git knows or would add
123
+ * (ignored ones are left out); elsewhere a walk of bounded size that skips hidden folders and
124
+ * `node_modules`. Nothing outside the folder is named.
125
+ */
126
+ export async function findFiles(root, query, limit = 20) {
127
+ const base = realish(root);
128
+ let files = [];
129
+ const listed = (await repository(base)) ? await git(base, ["ls-files", "-co", "--exclude-standard", "-z"]) : undefined;
130
+ if (listed?.code === 0)
131
+ files = listed.out.split("\0").filter(Boolean);
132
+ else {
133
+ const walk = (rel, depth) => {
134
+ if (files.length >= 5000 || depth > 6)
135
+ return;
136
+ for (const e of listDir(base, rel)?.entries ?? []) {
137
+ const next = rel ? `${rel}/${e.name}` : e.name;
138
+ if (!e.dir)
139
+ files.push(next);
140
+ else if (!e.name.startsWith(".") && e.name !== "node_modules")
141
+ walk(next, depth + 1);
142
+ }
143
+ };
144
+ walk("", 0);
145
+ }
146
+ const q = query.toLowerCase();
147
+ if (!q)
148
+ return files.sort((a, b) => a.split("/").length - b.split("/").length || a.localeCompare(b)).slice(0, limit);
149
+ // Letters in order, with anything between them: "agts" finds "src/agents.ts".
150
+ const inOrder = (text) => {
151
+ let i = 0;
152
+ for (const ch of text)
153
+ if (ch === q[i] && ++i === q.length)
154
+ return true;
155
+ return false;
156
+ };
157
+ const rank = (file) => {
158
+ const lower = file.toLowerCase();
159
+ const name = lower.slice(lower.lastIndexOf("/") + 1);
160
+ return name === q ? 0 : name.startsWith(q) ? 1 : name.includes(q) ? 2 : lower.includes(q) ? 3 : inOrder(name) ? 4 : inOrder(lower) ? 5 : -1;
161
+ };
162
+ return files
163
+ .map((file) => ({ file, rank: rank(file) }))
164
+ .filter((x) => x.rank >= 0)
165
+ .sort((a, b) => a.rank - b.rank || a.file.length - b.file.length || a.file.localeCompare(b.file))
166
+ .slice(0, limit)
167
+ .map((x) => x.file);
168
+ }
169
+ /** What is in one folder: folders first, then files, by name. Git's own folder is left out. */
170
+ export function listDir(root, rel) {
171
+ const dir = inside(root, rel);
172
+ if (!dir)
173
+ return undefined;
174
+ let names;
175
+ try {
176
+ names = fs.readdirSync(dir, { withFileTypes: true });
177
+ }
178
+ catch {
179
+ return undefined;
180
+ }
181
+ const entries = [];
182
+ for (const d of names) {
183
+ if (d.name === ".git" || d.name === ".DS_Store")
184
+ continue;
185
+ const full = path.join(dir, d.name);
186
+ try {
187
+ const stat = fs.statSync(full);
188
+ // A link that leads out of the folder is not shown as part of it.
189
+ if (!inside(root, path.relative(realish(root), full)))
190
+ continue;
191
+ entries.push({ name: d.name, dir: stat.isDirectory(), size: stat.isDirectory() ? 0 : stat.size });
192
+ }
193
+ catch {
194
+ // a broken link
195
+ }
196
+ }
197
+ entries.sort((a, b) => Number(b.dir) - Number(a.dir) || a.name.localeCompare(b.name));
198
+ return { entries: entries.slice(0, 500), more: entries.length > 500 };
199
+ }
200
+ /** A file's text, for reading. Binary files and very large ones are only described. */
201
+ export function readText(root, rel) {
202
+ const file = inside(root, rel);
203
+ if (!file)
204
+ return undefined;
205
+ try {
206
+ const stat = fs.statSync(file);
207
+ if (!stat.isFile())
208
+ return undefined;
209
+ const max = 256 * 1024;
210
+ const fd = fs.openSync(file, "r");
211
+ const buf = Buffer.alloc(Math.min(stat.size, max));
212
+ fs.readSync(fd, buf, 0, buf.length, 0);
213
+ fs.closeSync(fd);
214
+ if (buf.subarray(0, 8192).includes(0))
215
+ return { size: stat.size, binary: true, truncated: false };
216
+ return { text: buf.toString("utf8"), size: stat.size, binary: false, truncated: stat.size > max };
217
+ }
218
+ catch {
219
+ return undefined;
220
+ }
221
+ }