@pmndre/pine 0.22.1 → 0.33.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 (99) hide show
  1. package/README.md +2 -1
  2. package/changes.json +264 -0
  3. package/docs/SKILL.md +31 -7
  4. package/docs/references/agents.md +245 -21
  5. package/docs/references/coding.md +1 -1
  6. package/docs/references/computer.md +1 -1
  7. package/docs/references/configuration.md +14 -5
  8. package/docs/references/mcp.md +9 -0
  9. package/docs/references/operations.md +21 -10
  10. package/docs/references/overview.md +3 -1
  11. package/docs/references/security.md +83 -24
  12. package/docs/references/setup.md +55 -9
  13. package/docs/references/source-types.md +1418 -0
  14. package/docs/references/sources.md +333 -19
  15. package/docs/references/stack.md +1 -0
  16. package/docs/references/tui.md +86 -12
  17. package/examples/home/AGENTS.md +4 -0
  18. package/examples/home/TOOLS.md +5 -0
  19. package/examples/home/agents/main/workspace/AGENTS.md +1 -1
  20. package/examples/home/agents/main/workspace/TOOLS.md +5 -0
  21. package/mcp/browser.d.ts +6 -0
  22. package/mcp/browser.js +19 -0
  23. package/mcp/render.d.ts +1 -0
  24. package/mcp/render.js +237 -0
  25. package/package.json +6 -4
  26. package/src/actions.d.ts +6 -1
  27. package/src/actions.js +17 -4
  28. package/src/admin.d.ts +4 -0
  29. package/src/admin.js +25 -6
  30. package/src/agents.d.ts +167 -2
  31. package/src/agents.js +912 -45
  32. package/src/catalogue.d.ts +60 -0
  33. package/src/catalogue.js +791 -0
  34. package/src/cli.js +10 -7
  35. package/src/config.d.ts +53 -4
  36. package/src/config.js +30 -5
  37. package/src/goals.d.ts +1 -1
  38. package/src/goals.js +14 -1
  39. package/src/manual.js +24 -2
  40. package/src/mime.d.ts +78 -0
  41. package/src/mime.js +342 -0
  42. package/src/net.d.ts +11 -0
  43. package/src/net.js +29 -2
  44. package/src/pine.js +10 -1
  45. package/src/router.js +3 -3
  46. package/src/sandbox-linux.d.ts +3 -1
  47. package/src/sandbox-linux.js +5 -0
  48. package/src/security.d.ts +22 -6
  49. package/src/security.js +44 -13
  50. package/src/slots.d.ts +106 -0
  51. package/src/slots.js +250 -0
  52. package/src/source-types.d.ts +20 -0
  53. package/src/sources/calendar.d.ts +24 -0
  54. package/src/sources/calendar.js +265 -0
  55. package/src/sources/command.js +109 -4
  56. package/src/sources/discord.d.ts +4 -0
  57. package/src/sources/discord.js +298 -0
  58. package/src/sources/feed.d.ts +19 -0
  59. package/src/sources/feed.js +257 -0
  60. package/src/sources/http.js +154 -103
  61. package/src/sources/mail.d.ts +51 -0
  62. package/src/sources/mail.js +1013 -0
  63. package/src/sources/matrix.d.ts +4 -0
  64. package/src/sources/matrix.js +232 -0
  65. package/src/sources/modbus.d.ts +15 -0
  66. package/src/sources/modbus.js +240 -0
  67. package/src/sources/onvif.d.ts +35 -0
  68. package/src/sources/onvif.js +326 -0
  69. package/src/sources/presence.d.ts +80 -0
  70. package/src/sources/presence.js +480 -0
  71. package/src/sources/serial.d.ts +21 -0
  72. package/src/sources/serial.js +220 -0
  73. package/src/sources/slack.d.ts +16 -0
  74. package/src/sources/slack.js +1062 -0
  75. package/src/sources/smtp.d.ts +4 -0
  76. package/src/sources/smtp.js +360 -0
  77. package/src/sources/stream.d.ts +68 -0
  78. package/src/sources/stream.js +534 -0
  79. package/src/sources/syslog.d.ts +32 -0
  80. package/src/sources/syslog.js +243 -0
  81. package/src/sources/system.d.ts +14 -0
  82. package/src/sources/system.js +236 -0
  83. package/src/sources/system.swift +102 -0
  84. package/src/sources/telegram.js +34 -1
  85. package/src/sources.d.ts +41 -1
  86. package/src/sources.js +147 -24
  87. package/src/tools.d.ts +1 -1
  88. package/src/tools.js +304 -23
  89. package/src/tui/app.d.ts +35 -2
  90. package/src/tui/app.js +306 -43
  91. package/src/tui/levels.d.ts +68 -0
  92. package/src/tui/levels.js +273 -0
  93. package/src/tui/picture.d.ts +17 -1
  94. package/src/tui/picture.js +72 -5
  95. package/src/types.d.ts +3 -0
  96. package/src/update.d.ts +9 -1
  97. package/src/update.js +75 -3
  98. package/src/watching.d.ts +156 -0
  99. package/src/watching.js +609 -4
@@ -0,0 +1,68 @@
1
+ /**
2
+ * The levels of a sound, worked out before it plays: for each tenth of a second, how much there is
3
+ * in each of a row of frequency bands. The sound line draws them as bars (see Sound in picture.ts).
4
+ * pine does not hear what the system's player plays, so this is read from the file and shown by
5
+ * the line's own clock.
6
+ */
7
+ /** How many bars. */
8
+ export declare const BANDS = 24;
9
+ /** Seconds one row of levels stands for: the pace the line is redrawn at. */
10
+ export declare const STEP = 0.1;
11
+ export interface Levels {
12
+ bands: number;
13
+ /** Seconds per row. */
14
+ step: number;
15
+ rows: number;
16
+ /** rows * bands values, 0 (nothing) to 255 (the loudest the sound gets), a row after the other. */
17
+ data: Uint8Array;
18
+ }
19
+ /** The frequencies at which one band ends and the next begins: evenly spaced for the ear, from 80 Hz to 8 kHz or as far as the file goes. */
20
+ export declare function bandEdges(rate: number): number[];
21
+ /** Takes samples as they come and keeps a row of levels for every tenth of a second. */
22
+ export declare class Meter {
23
+ private rate;
24
+ /** Samples looked at per row: the largest power of two that fits into a tenth of a second. */
25
+ private size;
26
+ private window;
27
+ private cos;
28
+ private sin;
29
+ private swap;
30
+ private re;
31
+ private im;
32
+ /** Where each band begins, counted in the transform's steps of frequency. */
33
+ private edges;
34
+ private held;
35
+ private at;
36
+ private row;
37
+ private until;
38
+ /** Decibels, a row after the other. */
39
+ private found;
40
+ constructor(rate: number);
41
+ /** Samples in row `i`. A rate that a tenth of a second does not divide gets rows of two lengths, and stays in time. */
42
+ private length;
43
+ /** One sample, between -1 and 1. */
44
+ add(sample: number): void;
45
+ /** The row that is full: its beginning through the transform, the result summed up by band. */
46
+ private measure;
47
+ /** The levels of everything that was added. Undefined: there was nothing. */
48
+ done(): Levels | undefined;
49
+ }
50
+ /** Uncompressed samples as they lie in a file or come out of a decoder. */
51
+ export interface Pcm {
52
+ rate: number;
53
+ channels: number;
54
+ bits: number;
55
+ float: boolean;
56
+ }
57
+ /**
58
+ * The levels of a sound that arrives in pieces. With `pcm` the pieces are bare samples (what a
59
+ * decoder was asked to write); without it they are a wav file, which says what it holds.
60
+ * Undefined: nothing here that can be read as samples.
61
+ */
62
+ export declare function levelsOf(pieces: AsyncIterable<Buffer>, pcm?: Pcm): Promise<Levels | undefined>;
63
+ /**
64
+ * The bars at a moment of the sound, lowest band first. Before the start and after the end they
65
+ * lie flat. With room for fewer bars than there are bands, neighbouring bands share a bar, which
66
+ * stands as high as the highest of them.
67
+ */
68
+ export declare function bars(levels: Levels, seconds: number, count?: number): string;
@@ -0,0 +1,273 @@
1
+ /**
2
+ * The levels of a sound, worked out before it plays: for each tenth of a second, how much there is
3
+ * in each of a row of frequency bands. The sound line draws them as bars (see Sound in picture.ts).
4
+ * pine does not hear what the system's player plays, so this is read from the file and shown by
5
+ * the line's own clock.
6
+ */
7
+ /** How many bars. */
8
+ export const BANDS = 24;
9
+ /** Seconds one row of levels stands for: the pace the line is redrawn at. */
10
+ export const STEP = 0.1;
11
+ /** What separates the loudest bar from an empty one, in decibels. */
12
+ const RANGE = 42;
13
+ /** Below this (against a full tone) a sound is taken for silence, whatever is loudest in it. */
14
+ const SILENCE = -72;
15
+ /** A bar falls by at most this much from one row to the next, so that it does not flicker. */
16
+ const FALL = 56;
17
+ /** The frequencies at which one band ends and the next begins: evenly spaced for the ear, from 80 Hz to 8 kHz or as far as the file goes. */
18
+ export function bandEdges(rate) {
19
+ const low = 80;
20
+ const high = Math.min(8000, rate / 2);
21
+ return Array.from({ length: BANDS + 1 }, (_, i) => low * (high / low) ** (i / BANDS));
22
+ }
23
+ /** Takes samples as they come and keeps a row of levels for every tenth of a second. */
24
+ export class Meter {
25
+ rate;
26
+ /** Samples looked at per row: the largest power of two that fits into a tenth of a second. */
27
+ size;
28
+ window;
29
+ cos;
30
+ sin;
31
+ swap;
32
+ re;
33
+ im;
34
+ /** Where each band begins, counted in the transform's steps of frequency. */
35
+ edges;
36
+ held;
37
+ at = 0;
38
+ row = 0;
39
+ until;
40
+ /** Decibels, a row after the other. */
41
+ found = new Float32Array(BANDS * 256);
42
+ constructor(rate) {
43
+ this.rate = rate;
44
+ let size = 64;
45
+ while (size * 2 <= rate * STEP)
46
+ size *= 2;
47
+ this.size = size;
48
+ this.window = Float32Array.from({ length: size }, (_, i) => 0.5 - 0.5 * Math.cos((2 * Math.PI * i) / size));
49
+ this.cos = Float32Array.from({ length: size / 2 }, (_, i) => Math.cos((2 * Math.PI * i) / size));
50
+ this.sin = Float32Array.from({ length: size / 2 }, (_, i) => Math.sin((2 * Math.PI * i) / size));
51
+ const bits = Math.log2(size);
52
+ this.swap = Uint32Array.from({ length: size }, (_, i) => {
53
+ let r = 0;
54
+ for (let b = 0; b < bits; b++)
55
+ r |= ((i >> b) & 1) << (bits - 1 - b);
56
+ return r;
57
+ });
58
+ this.re = new Float32Array(size);
59
+ this.im = new Float32Array(size);
60
+ // Step k of the transform stands for the frequencies around k * rate / size: from k to k + 1 on this scale.
61
+ this.edges = bandEdges(rate).map((hz) => (hz * size) / rate + 0.5);
62
+ this.held = new Float32Array(size);
63
+ this.until = this.length(0);
64
+ }
65
+ /** Samples in row `i`. A rate that a tenth of a second does not divide gets rows of two lengths, and stays in time. */
66
+ length(i) {
67
+ return Math.round((i + 1) * this.rate * STEP) - Math.round(i * this.rate * STEP);
68
+ }
69
+ /** One sample, between -1 and 1. */
70
+ add(sample) {
71
+ if (this.at < this.size)
72
+ this.held[this.at] = sample;
73
+ if (++this.at >= this.until)
74
+ this.measure();
75
+ }
76
+ /** The row that is full: its beginning through the transform, the result summed up by band. */
77
+ measure() {
78
+ const { size, re, im, cos, sin } = this;
79
+ this.held.fill(0, Math.min(this.at, size));
80
+ for (let i = 0; i < size; i++) {
81
+ re[this.swap[i]] = this.held[i] * this.window[i];
82
+ im[i] = 0;
83
+ }
84
+ for (let half = 1; half < size; half *= 2) {
85
+ const stride = size / (half * 2);
86
+ for (let start = 0; start < size; start += half * 2) {
87
+ for (let k = 0; k < half; k++) {
88
+ const a = start + k;
89
+ const b = a + half;
90
+ const wr = cos[k * stride];
91
+ const wi = -sin[k * stride];
92
+ const tr = re[b] * wr - im[b] * wi;
93
+ const ti = re[b] * wi + im[b] * wr;
94
+ re[b] = re[a] - tr;
95
+ im[b] = im[a] - ti;
96
+ re[a] += tr;
97
+ im[a] += ti;
98
+ }
99
+ }
100
+ }
101
+ if ((this.row + 1) * BANDS > this.found.length) {
102
+ const more = new Float32Array(this.found.length * 2);
103
+ more.set(this.found);
104
+ this.found = more;
105
+ }
106
+ // A tone as loud as the file can hold comes out as 1, which is 0 decibels.
107
+ const full = 1.5 * (size / 4) ** 2;
108
+ for (let band = 0; band < BANDS; band++) {
109
+ const from = this.edges[band];
110
+ const to = Math.min(this.edges[band + 1], size / 2);
111
+ let power = 0;
112
+ // A step that lies across the border of two bands counts for each by the part that is in it.
113
+ for (let k = Math.floor(from); k < to; k++)
114
+ power += (re[k] * re[k] + im[k] * im[k]) * (Math.min(k + 1, to) - Math.max(k, from));
115
+ this.found[this.row * BANDS + band] = 10 * Math.log10(power / full);
116
+ }
117
+ this.row++;
118
+ this.at = 0;
119
+ this.until = this.length(this.row);
120
+ }
121
+ /** The levels of everything that was added. Undefined: there was nothing. */
122
+ done() {
123
+ if (this.at > 0)
124
+ this.measure();
125
+ const rows = this.row;
126
+ if (!rows)
127
+ return undefined;
128
+ const found = this.found.subarray(0, rows * BANDS);
129
+ let loudest = -Infinity;
130
+ for (const db of found)
131
+ if (db > loudest)
132
+ loudest = db;
133
+ const data = new Uint8Array(rows * BANDS);
134
+ // The bars are measured against the loudest moment of this sound: a quiet recording moves as much as a loud one.
135
+ if (loudest >= SILENCE) {
136
+ for (let i = 0; i < data.length; i++) {
137
+ const level = found[i] < SILENCE ? 0 : Math.round(Math.max(0, Math.min(1, (found[i] - loudest) / RANGE + 1)) * 255);
138
+ data[i] = Math.max(level, i >= BANDS ? data[i - BANDS] - FALL : 0);
139
+ }
140
+ }
141
+ return { bands: BANDS, step: STEP, rows, data };
142
+ }
143
+ }
144
+ /** Reads one sample at a place in a buffer, as a number between -1 and 1. Undefined: a kind that is not read here. */
145
+ function reader(pcm) {
146
+ if (pcm.float)
147
+ return pcm.bits === 32 ? (b, at) => b.readFloatLE(at) : pcm.bits === 64 ? (b, at) => b.readDoubleLE(at) : undefined;
148
+ if (pcm.bits === 8)
149
+ return (b, at) => (b[at] - 128) / 128;
150
+ if (pcm.bits === 16)
151
+ return (b, at) => b.readInt16LE(at) / 32768;
152
+ if (pcm.bits === 24)
153
+ return (b, at) => b.readIntLE(at, 3) / 8388608;
154
+ if (pcm.bits === 32)
155
+ return (b, at) => b.readInt32LE(at) / 2147483648;
156
+ return undefined;
157
+ }
158
+ /**
159
+ * What a wav file says about itself before its samples. `need`: more bytes have to be read first
160
+ * to tell. Undefined: not a wav file, or one whose samples are compressed.
161
+ */
162
+ function wavHead(b) {
163
+ if (b.length < 12)
164
+ return { need: true };
165
+ if (b.toString("latin1", 0, 4) !== "RIFF" || b.toString("latin1", 8, 12) !== "WAVE")
166
+ return undefined;
167
+ let pcm;
168
+ for (let at = 12;;) {
169
+ if (at + 8 > b.length)
170
+ return { need: true };
171
+ const id = b.toString("latin1", at, at + 4);
172
+ const size = b.readUInt32LE(at + 4);
173
+ if (id === "data") {
174
+ // A file that was written as a stream says 0 or the largest number here: then it runs to the end.
175
+ return pcm ? { pcm, from: at + 8, bytes: size && size < 0xffff_fff0 ? size : Infinity } : undefined;
176
+ }
177
+ if (id === "fmt ") {
178
+ if (at + 8 + size > b.length)
179
+ return { need: true };
180
+ if (size < 16)
181
+ return undefined;
182
+ let tag = b.readUInt16LE(at + 8);
183
+ // "Extensible": the kind of sample is said further on, in the first two bytes of a longer name.
184
+ if (tag === 0xfffe && size >= 26)
185
+ tag = b.readUInt16LE(at + 32);
186
+ if (tag !== 1 && tag !== 3)
187
+ return undefined;
188
+ pcm = { rate: b.readUInt32LE(at + 12), channels: b.readUInt16LE(at + 10), bits: b.readUInt16LE(at + 22), float: tag === 3 };
189
+ }
190
+ at += 8 + size + (size % 2);
191
+ }
192
+ }
193
+ /**
194
+ * The levels of a sound that arrives in pieces. With `pcm` the pieces are bare samples (what a
195
+ * decoder was asked to write); without it they are a wav file, which says what it holds.
196
+ * Undefined: nothing here that can be read as samples.
197
+ */
198
+ export async function levelsOf(pieces, pcm) {
199
+ let head = pcm ? undefined : Buffer.alloc(0);
200
+ let left = Infinity;
201
+ let meter;
202
+ let read;
203
+ let frame = 0;
204
+ let rest = Buffer.alloc(0);
205
+ const begin = (p) => {
206
+ read = reader(p);
207
+ if (!read || p.channels < 1 || p.channels > 64 || p.rate < 2000 || p.rate > 400_000)
208
+ return false;
209
+ pcm = p;
210
+ frame = (p.bits / 8) * p.channels;
211
+ meter = new Meter(p.rate);
212
+ return true;
213
+ };
214
+ if (pcm && !begin(pcm))
215
+ return undefined;
216
+ for await (let piece of pieces) {
217
+ if (head) {
218
+ head = Buffer.concat([head, piece]);
219
+ const found = wavHead(head);
220
+ if (!found)
221
+ return undefined;
222
+ if ("need" in found) {
223
+ // Whatever comes before the samples (a title, a cover) is skipped; a megabyte of it is no sound file.
224
+ if (head.length > 1024 * 1024)
225
+ return undefined;
226
+ continue;
227
+ }
228
+ if (!begin(found.pcm))
229
+ return undefined;
230
+ piece = head.subarray(found.from);
231
+ left = found.bytes;
232
+ head = undefined;
233
+ }
234
+ if (piece.length > left)
235
+ piece = piece.subarray(0, left);
236
+ left -= piece.length;
237
+ // A piece can end in the middle of a sample: that part waits for the next piece.
238
+ const all = rest.length ? Buffer.concat([rest, piece]) : piece;
239
+ const whole = all.length - (all.length % frame);
240
+ const width = pcm.bits / 8;
241
+ for (let at = 0; at < whole; at += frame) {
242
+ let sum = 0;
243
+ for (let ch = 0; ch < pcm.channels; ch++)
244
+ sum += read(all, at + ch * width);
245
+ meter.add(sum / pcm.channels);
246
+ }
247
+ rest = all.subarray(whole);
248
+ if (left <= 0)
249
+ break;
250
+ }
251
+ return meter?.done();
252
+ }
253
+ const BLOCKS = "▁▂▃▄▅▆▇█";
254
+ /**
255
+ * The bars at a moment of the sound, lowest band first. Before the start and after the end they
256
+ * lie flat. With room for fewer bars than there are bands, neighbouring bands share a bar, which
257
+ * stands as high as the highest of them.
258
+ */
259
+ export function bars(levels, seconds, count = levels.bands) {
260
+ // A little more than the division gives: 0.3 seconds is the fourth row, though 0.3 / 0.1 comes out just under 3.
261
+ const row = Math.floor(seconds / levels.step + 1e-6);
262
+ const there = row >= 0 && row < levels.rows;
263
+ let out = "";
264
+ for (let bar = 0; bar < count; bar++) {
265
+ const from = Math.floor((bar * levels.bands) / count);
266
+ const to = Math.max(from + 1, Math.floor(((bar + 1) * levels.bands) / count));
267
+ let level = 0;
268
+ for (let band = from; there && band < to; band++)
269
+ level = Math.max(level, levels.data[row * levels.bands + band]);
270
+ out += BLOCKS[Math.round((level / 255) * 7)];
271
+ }
272
+ return out;
273
+ }
@@ -1,4 +1,5 @@
1
1
  import { type Component } from "@earendil-works/pi-tui";
2
+ import { type Levels } from "./levels.ts";
2
3
  /**
3
4
  * Pictures in the conversation. Display only: nothing here is sent to a model.
4
5
  *
@@ -151,14 +152,29 @@ export interface PreparedSound {
151
152
  file: string;
152
153
  name: string;
153
154
  seconds?: number;
155
+ /** What the bars are drawn from, once it is worked out. A sound that nothing here can decode never has it. */
156
+ levels?: Levels;
157
+ /** Settles when the levels are there, or when it is clear that there are none. */
158
+ measured?: Promise<void>;
154
159
  }
155
- /** Get a sound file ready: how long it runs, where that can be told. */
160
+ export type SoundDecoder = "ffmpeg" | "afconvert";
161
+ /**
162
+ * The levels of a sound file, for its bars. A wav file is read as it is. Anything else is decoded
163
+ * first, by ffmpeg where it is installed and by macOS's own afconvert; both run confined like the
164
+ * other converters. Undefined: nothing here can decode it, and the line has no bars.
165
+ */
166
+ export declare function soundLevels(file: string, using?: SoundDecoder[]): Promise<Levels | undefined>;
167
+ /** Get a sound file ready: how long it runs, where that can be told, and its levels for the bars. */
156
168
  export declare function prepareSound(file: string, name: string): Promise<PreparedSound>;
157
169
  /**
158
170
  * One sound in the conversation: a line with play and pause, like a video's. A pause ends the
159
171
  * player and play starts one from where it was, as a video does with ffmpeg; a player that
160
172
  * cannot start in the middle is held instead (see SoundPlayer). At the end the line is back at
161
173
  * the start.
174
+ *
175
+ * Between the control and the name stands a row of bars, one for each band of frequencies from
176
+ * low to high: the sound's levels at the place the clock says is being heard. They move while it
177
+ * plays, stand still at a pause and lie flat when nothing plays.
162
178
  */
163
179
  export declare class Sound implements Component {
164
180
  private s;
@@ -5,6 +5,7 @@ import path from "node:path";
5
5
  import zlib from "node:zlib";
6
6
  import { allocateImageId, getCapabilities, getCellDimensions, getImageDimensions, Image, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
7
7
  import { helperArgv } from "../security.js";
8
+ import { bars, levelsOf } from "./levels.js";
8
9
  import { c } from "./theme.js";
9
10
  const SIPS = "/usr/bin/sips";
10
11
  const hasSips = process.platform === "darwin" && fs.existsSync(SIPS);
@@ -604,8 +605,50 @@ function wavSeconds(file) {
604
605
  fs.closeSync(fd);
605
606
  }
606
607
  }
607
- /** Get a sound file ready: how long it runs, where that can be told. */
608
+ const AFCONVERT = "/usr/bin/afconvert";
609
+ /** What a decoder is asked for: one channel at 16000 samples a second shows everything up to 8 kHz, which is all the bars cover. */
610
+ const PLAIN = { rate: 16_000, channels: 1, bits: 16, float: false };
611
+ const decoders = () => [...(FFMPEG ? ["ffmpeg"] : []), ...(process.platform === "darwin" && d_exists(AFCONVERT) ? ["afconvert"] : [])];
612
+ let measuring = Promise.resolve();
613
+ /**
614
+ * The levels of a sound file, for its bars. A wav file is read as it is. Anything else is decoded
615
+ * first, by ffmpeg where it is installed and by macOS's own afconvert; both run confined like the
616
+ * other converters. Undefined: nothing here can decode it, and the line has no bars.
617
+ */
618
+ export function soundLevels(file, using = decoders()) {
619
+ // One at a time: a conversation full of sounds should not start a dozen decoders at once.
620
+ const job = measuring.then(() => measure(file, using));
621
+ measuring = job.catch(() => { });
622
+ return job.catch(() => undefined);
623
+ }
624
+ async function measure(file, using) {
625
+ const plain = await levelsOf(fs.createReadStream(file)).catch(() => undefined);
626
+ if (plain)
627
+ return plain;
628
+ if (using.includes("ffmpeg") && FFMPEG) {
629
+ const child = confined(FFMPEG, ["-loglevel", "error", "-i", file, "-vn", "-ac", String(PLAIN.channels), "-ar", String(PLAIN.rate), "-f", "s16le", "-"], "pipe");
630
+ const ended = finished(child, 30_000);
631
+ const levels = child.stdout ? await levelsOf(child.stdout, PLAIN).catch(() => undefined) : undefined;
632
+ if ((await ended) && levels)
633
+ return levels;
634
+ }
635
+ if (using.includes("afconvert") && d_exists(AFCONVERT)) {
636
+ const dir = fs.mkdtempSync(path.join(TMP, "pine-snd-"));
637
+ try {
638
+ const out = path.join(dir, "out.wav");
639
+ if (await finished(confined(AFCONVERT, ["-f", "WAVE", "-d", `LEI16@${PLAIN.rate}`, "-c", String(PLAIN.channels), "--mix", file, out], "ignore", dir), 30_000))
640
+ return await levelsOf(fs.createReadStream(out));
641
+ }
642
+ finally {
643
+ fs.rmSync(dir, { recursive: true, force: true });
644
+ }
645
+ }
646
+ return undefined;
647
+ }
648
+ /** Get a sound file ready: how long it runs, where that can be told, and its levels for the bars. */
608
649
  export async function prepareSound(file, name) {
650
+ const sound = { file, name };
651
+ sound.measured = soundLevels(file).then((levels) => void (sound.levels = levels));
609
652
  let seconds;
610
653
  try {
611
654
  seconds = wavSeconds(file);
@@ -620,14 +663,24 @@ export async function prepareSound(file, name) {
620
663
  catch {
621
664
  // played without a length
622
665
  }
623
- return { file, name, seconds: seconds && Number.isFinite(seconds) ? seconds : undefined };
666
+ sound.seconds = seconds && Number.isFinite(seconds) ? seconds : undefined;
667
+ // A short sound is measured in a moment, and its line then has its bars from the start. A long one
668
+ // is not waited for: it plays, and the bars come when they are ready.
669
+ await Promise.race([sound.measured, new Promise((r) => setTimeout(r, 250))]);
670
+ return sound;
624
671
  }
625
672
  let sounding;
673
+ /** Fewer bars than this say nothing about a sound. */
674
+ const FEWEST_BARS = 8;
626
675
  /**
627
676
  * One sound in the conversation: a line with play and pause, like a video's. A pause ends the
628
677
  * player and play starts one from where it was, as a video does with ffmpeg; a player that
629
678
  * cannot start in the middle is held instead (see SoundPlayer). At the end the line is back at
630
679
  * the start.
680
+ *
681
+ * Between the control and the name stands a row of bars, one for each band of frequencies from
682
+ * low to high: the sound's levels at the place the clock says is being heard. They move while it
683
+ * plays, stand still at a pause and lie flat when nothing plays.
631
684
  */
632
685
  export class Sound {
633
686
  s;
@@ -648,6 +701,9 @@ export class Sound {
648
701
  this.player = player;
649
702
  this.redraw = redraw;
650
703
  this.open = open;
704
+ // Levels that come after the line was drawn (a long sound) show as soon as they are there.
705
+ if (!sound.levels)
706
+ void sound.measured?.then(() => sound.levels && this.redraw());
651
707
  }
652
708
  invalidate() { }
653
709
  get state() {
@@ -705,7 +761,9 @@ export class Sound {
705
761
  child.on("error", () => over(false));
706
762
  child.on("close", (code) => over(code === 0));
707
763
  clearInterval(this.ticker);
708
- const ticker = setInterval(() => this.paused || this.redraw(), 500);
764
+ // The bars move ten times a second, like a video's frames; the time alone needs a look twice a second.
765
+ let tick = 0;
766
+ const ticker = setInterval(() => this.paused || ((this.s.levels || tick++ % 5 === 0) && this.redraw()), 1000 / FPS);
709
767
  ticker.unref?.();
710
768
  this.ticker = ticker;
711
769
  this.redraw();
@@ -755,10 +813,19 @@ export class Sound {
755
813
  const at = this.s.seconds ? Math.min(this.played() / 1000, this.s.seconds) : this.played() / 1000;
756
814
  const time = this.state !== "idle" ? `${clock(at)}${total ? ` / ${total}` : ""}` : total;
757
815
  const note = this.failed ? " · could not be played here" : !this.player ? " · no program here that plays sound" : "";
758
- const left = ` ${control ? `${c.accent(control)} ` : ""}${c.gray(`${control ? "· " : ""}♪ ${this.s.name}${time ? ` · ${time}` : ""}${note}`)}`;
816
+ const rest = `♪ ${this.s.name}${time ? ` · ${time}` : ""}${note}`;
817
+ const open = ` ${c.accent("▸ open in player")}`;
818
+ let left = ` ${control ? `${c.accent(control)} ` : ""}${c.gray(`${control ? "· " : ""}${rest}`)}`;
819
+ // The bars stand where the dot was, as many as the line has room for beside everything else: a
820
+ // narrow terminal gets fewer, and one without room for a few gets the line as it is without them.
821
+ const room = this.s.levels && control ? Math.min(this.s.levels.bands, width - visibleWidth(left) - visibleWidth(open) - 1) : 0;
822
+ if (this.s.levels && room >= FEWEST_BARS) {
823
+ const drawn = bars(this.s.levels, this.state === "idle" ? -1 : at, room);
824
+ left = ` ${c.accent(control)} ${this.state === "idle" ? c.gray(drawn) : c.accent(drawn)} ${c.gray(rest)}`;
825
+ }
759
826
  this.playTo = control ? 1 + visibleWidth(control) : -1;
760
827
  this.openFrom = visibleWidth(left) + 3;
761
- return [truncateToWidth(`${left} ${c.accent("▸ open in player")}`, width)];
828
+ return [truncateToWidth(`${left}${open}`, width)];
762
829
  }
763
830
  }
764
831
  /** Stop whatever sound is playing, for example when the UI closes. */
package/src/types.d.ts CHANGED
@@ -58,6 +58,9 @@ export interface TaskMeta {
58
58
  /** Child sessions of a sub-agent get no delegation tools and an optional narrower tool list. */
59
59
  leaf?: boolean;
60
60
  tools?: string[];
61
+ /** A sub-agent that does not run on its agent's model or at its reasoning level: what the conversation that started it chose. */
62
+ model?: string;
63
+ thinking?: string;
61
64
  label?: string;
62
65
  /** This message was given to a task that was already working (that task's id) instead of becoming work of its own. */
63
66
  steered?: number;
package/src/update.d.ts CHANGED
@@ -14,13 +14,21 @@ export declare function installed(root?: string): {
14
14
  kind: InstallKind;
15
15
  root: string;
16
16
  };
17
+ /**
18
+ * What the versions after `from` changed, up to `to`: a list for every version, the newest
19
+ * first, a line for every commit. `list` is the package's changes.json (scripts/changes.mjs).
20
+ * Empty when it says nothing about these versions.
21
+ */
22
+ export declare function whatChanged(list: unknown, from: string, to: string): string;
23
+ /** The same for a checkout, which has commits and no versions: the subjects that the pull brought. */
24
+ export declare function whatCame(subjects: string[]): string;
17
25
  /**
18
26
  * What to say after an update, and whether it worked: the version on disk has to be the one
19
27
  * that was asked for. npm can say that a version exists and still install an older one for a
20
28
  * few minutes after it was published (its two answers come from different places), and
21
29
  * "pine is now <the old version>" must not read like success.
22
30
  */
23
- export declare function outcome(name: string, wanted: string | undefined, now: string): {
31
+ export declare function outcome(name: string, wanted: string | undefined, now: string, news?: string): {
24
32
  ok: boolean;
25
33
  text: string;
26
34
  };
package/src/update.js CHANGED
@@ -37,17 +37,83 @@ function capture(cmd, args, cwd) {
37
37
  }
38
38
  const show = (cmd, args, cwd) => spawnSync(cmd, args, { cwd, stdio: "inherit" }).status ?? 1;
39
39
  const RESTART = "A pine that is running keeps the old version until it is restarted: quit the terminal UI and open it again, or restart the daemon.";
40
+ /** Text from a commit's subject, safe to print: one line, without anything a terminal would act on. */
41
+ const plain = (s) => s.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ").trim();
42
+ /** A commit's subject as an owner reads it: "feat: x" is something new, "fix: x" something fixed. */
43
+ function worded(subject) {
44
+ const m = /^([a-z]+)(?:\([^)]*\))?!?:\s*(.+)$/.exec(plain(subject));
45
+ if (!m)
46
+ return plain(subject);
47
+ return `${m[1] === "feat" ? "new" : m[1] === "fix" ? "fixed" : m[1]}: ${m[2]}`;
48
+ }
49
+ /** The most lines of changes that are printed; the rest is counted. */
50
+ const MOST = 25;
51
+ /** The list as it is printed: a heading by itself, then the lines, cut after MOST changes with a count of the rest. */
52
+ function listed(heading, groups) {
53
+ const total = groups.reduce((n, g) => n + g.changes.length, 0);
54
+ if (!total)
55
+ return "";
56
+ const lines = [];
57
+ let left = MOST;
58
+ for (const g of groups) {
59
+ if (!g.changes.length || left <= 0)
60
+ continue;
61
+ if (g.title)
62
+ lines.push(` ${g.title}`);
63
+ for (const c of g.changes.slice(0, left))
64
+ lines.push(`${g.title ? " " : " "}- ${c}`);
65
+ left -= g.changes.length;
66
+ }
67
+ return `${heading}\n\n${lines.join("\n")}${total > MOST ? `\n ... and ${total - MOST} more` : ""}`;
68
+ }
69
+ /**
70
+ * What the versions after `from` changed, up to `to`: a list for every version, the newest
71
+ * first, a line for every commit. `list` is the package's changes.json (scripts/changes.mjs).
72
+ * Empty when it says nothing about these versions.
73
+ */
74
+ export function whatChanged(list, from, to) {
75
+ if (!Array.isArray(list))
76
+ return "";
77
+ const groups = [];
78
+ for (const entry of list) {
79
+ const version = typeof entry?.version === "string" ? plain(entry.version) : "";
80
+ if (!version || !newer(version, from) || newer(version, to) || !Array.isArray(entry.changes))
81
+ continue;
82
+ const changes = entry.changes.filter((c) => typeof c === "string" && Boolean(plain(c))).map(worded);
83
+ // A version can stand twice in a list that was built from an odd history: its changes are one list.
84
+ const at = groups.find((g) => g.title === version);
85
+ if (at)
86
+ at.changes.push(...changes);
87
+ else
88
+ groups.push({ title: version, changes });
89
+ }
90
+ return listed(`What changed since ${from}:`, groups);
91
+ }
92
+ /** The same for a checkout, which has commits and no versions: the subjects that the pull brought. */
93
+ export function whatCame(subjects) {
94
+ return listed("What changed:", [{ changes: subjects.map(plain).filter(Boolean).map(worded) }]);
95
+ }
96
+ /** What the installed package says its releases changed. */
97
+ function changesIn(root) {
98
+ try {
99
+ return JSON.parse(fs.readFileSync(path.join(root, "changes.json"), "utf8"));
100
+ }
101
+ catch {
102
+ return undefined;
103
+ }
104
+ }
40
105
  /**
41
106
  * What to say after an update, and whether it worked: the version on disk has to be the one
42
107
  * that was asked for. npm can say that a version exists and still install an older one for a
43
108
  * few minutes after it was published (its two answers come from different places), and
44
109
  * "pine is now <the old version>" must not read like success.
45
110
  */
46
- export function outcome(name, wanted, now) {
111
+ export function outcome(name, wanted, now, news = "") {
47
112
  if (wanted && newer(wanted, now)) {
48
113
  return { ok: false, text: `\nNot updated: pine is still ${now}. npm knows ${wanted} but did not install it; that happens for a few minutes after a version is published. Run "pine update" again in a moment, or install it by its number:\n\n npm install -g ${name}@${wanted}` };
49
114
  }
50
- return { ok: true, text: `\npine is now ${now}. ${RESTART}` };
115
+ // The list stands by itself, between the version and what to do about it, so that it is not read over.
116
+ return { ok: true, text: news ? `\npine is now ${now}.\n\n${news}\n\n${RESTART}` : `\npine is now ${now}. ${RESTART}` };
51
117
  }
52
118
  /**
53
119
  * Update pine, or with `check` only say whether there is something newer. Returns the exit code.
@@ -57,6 +123,8 @@ export async function update(opts) {
57
123
  const { out } = opts;
58
124
  const me = installed();
59
125
  let wanted;
126
+ /** Where a checkout stood before it was moved forward. */
127
+ let was;
60
128
  if (me.kind === "checkout") {
61
129
  const branch = capture("git", ["rev-parse", "--abbrev-ref", "HEAD"], me.root) ?? "?";
62
130
  if (!capture("git", ["rev-parse", "--abbrev-ref", "@{u}"], me.root)) {
@@ -77,6 +145,7 @@ export async function update(opts) {
77
145
  return 0;
78
146
  }
79
147
  out(`Getting ${behind} new commit${behind === 1 ? "" : "s"} ...`);
148
+ was = capture("git", ["rev-parse", "HEAD"], me.root);
80
149
  // Only ever a fast-forward: your own commits and changes are never rewritten or thrown away.
81
150
  if (show("git", ["pull", "--ff-only"], me.root) !== 0) {
82
151
  out("git could not simply move this checkout forward (you have commits or changes of your own in the way). Nothing was changed; sort it out with git and run pine update again.");
@@ -113,7 +182,10 @@ export async function update(opts) {
113
182
  }
114
183
  }
115
184
  // The version on disk decides whether it worked, not what npm said it would do.
116
- const done = outcome(me.name, wanted, installed(me.root).version);
185
+ const now = installed(me.root).version;
186
+ // The package that is on disk now says what its releases changed; a checkout has its commits.
187
+ const news = me.kind === "checkout" ? (was ? whatCame((capture("git", ["log", "--first-parent", "--format=%s", `${was}..HEAD`], me.root) ?? "").split("\n")) : "") : whatChanged(changesIn(me.root), me.version, now);
188
+ const done = outcome(me.name, wanted, now, news);
117
189
  if (!done.ok) {
118
190
  out(done.text);
119
191
  return 1;