@celljs/ta-host 4.15.0 → 4.15.2

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 (45) hide show
  1. package/esm/node/extension-data.js +7 -3
  2. package/esm/node/index.d.ts +2 -0
  3. package/esm/node/index.js +2 -0
  4. package/esm/node/jpeg-encoder.d.ts +7 -0
  5. package/esm/node/jpeg-encoder.js +268 -0
  6. package/esm/node/png-thumbnail.d.ts +40 -0
  7. package/esm/node/png-thumbnail.js +384 -0
  8. package/esm/node/serve-updates.d.ts +38 -0
  9. package/esm/node/serve-updates.js +89 -0
  10. package/esm/node/ta-server-impl.d.ts +3 -0
  11. package/esm/node/ta-server-impl.js +33 -9
  12. package/esm/node/team/team-calls.js +6 -0
  13. package/esm/node/usage/agent-activity.d.ts +13 -0
  14. package/esm/node/usage/agent-activity.js +84 -0
  15. package/esm/node/usage/index.d.ts +3 -0
  16. package/esm/node/usage/index.js +4 -0
  17. package/esm/node/usage/usage-reader.d.ts +28 -0
  18. package/esm/node/usage/usage-reader.js +159 -0
  19. package/esm/node/usage/usage-server-impl.d.ts +12 -0
  20. package/esm/node/usage/usage-server-impl.js +46 -0
  21. package/esm/node/workspace-files.d.ts +23 -0
  22. package/esm/node/workspace-files.js +86 -0
  23. package/lib/node/extension-data.js +6 -2
  24. package/lib/node/index.d.ts +2 -0
  25. package/lib/node/index.js +2 -0
  26. package/lib/node/jpeg-encoder.d.ts +8 -0
  27. package/lib/node/jpeg-encoder.js +271 -0
  28. package/lib/node/png-thumbnail.d.ts +41 -0
  29. package/lib/node/png-thumbnail.js +387 -0
  30. package/lib/node/serve-updates.d.ts +39 -0
  31. package/lib/node/serve-updates.js +92 -0
  32. package/lib/node/ta-server-impl.d.ts +3 -0
  33. package/lib/node/ta-server-impl.js +33 -9
  34. package/lib/node/team/team-calls.js +6 -0
  35. package/lib/node/usage/agent-activity.d.ts +14 -0
  36. package/lib/node/usage/agent-activity.js +87 -0
  37. package/lib/node/usage/index.d.ts +4 -0
  38. package/lib/node/usage/index.js +20 -0
  39. package/lib/node/usage/usage-reader.d.ts +29 -0
  40. package/lib/node/usage/usage-reader.js +163 -0
  41. package/lib/node/usage/usage-server-impl.d.ts +13 -0
  42. package/lib/node/usage/usage-server-impl.js +49 -0
  43. package/lib/node/workspace-files.d.ts +23 -0
  44. package/lib/node/workspace-files.js +86 -0
  45. package/package.json +14 -10
@@ -0,0 +1,387 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PngThumbnail = void 0;
4
+ const node_zlib_1 = require("node:zlib");
5
+ const jpeg_encoder_1 = require("./jpeg-encoder");
6
+ const SIGNATURE = [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a];
7
+ /** Smaller than this much of its size, a picture is sent as it is: what it would save is not worth the time. */
8
+ const WORTH = 0.8;
9
+ /** Filled, a picture is never more than this many times its box on its long side (a very long one). */
10
+ const COVER_LONG = 4;
11
+ /** Rows read between two turns of the event loop, so that a large picture never holds up the other requests. */
12
+ const ROWS_A_TURN = 96;
13
+ /** A smaller PNG of more bytes a pixel than this is of a photo, a 3D scene or a gradient: a JPEG is tried too. */
14
+ const PHOTO_BYTES_A_PIXEL = 0.25;
15
+ const JPEG_QUALITY = 82;
16
+ /** Pictures of more pixels than this are sent as they are. */
17
+ const MAX_PIXELS = 64 * 1024 * 1024;
18
+ const CRC_TABLE = (() => {
19
+ const table = new Uint32Array(256);
20
+ for (let n = 0; n < 256; n++) {
21
+ let c = n;
22
+ for (let k = 0; k < 8; k++) {
23
+ c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
24
+ }
25
+ table[n] = c >>> 0;
26
+ }
27
+ return table;
28
+ })();
29
+ /**
30
+ * A smaller picture of a PNG, made without a library: the image read (any color type, 8 or 16 bits, a palette of 1 to 8,
31
+ * not interlaced), averaged down to the box (alpha weighted, so edges keep their colour) and written again, RGB when
32
+ * it is opaque. It yields to the event loop as it reads. Undefined for what it does not read, and for a picture already
33
+ * near the size of the box: then the picture itself is sent.
34
+ */
35
+ var PngThumbnail;
36
+ (function (PngThumbnail) {
37
+ function isPng(bytes) {
38
+ return bytes.length > 8 && SIGNATURE.every((byte, index) => bytes[index] === byte);
39
+ }
40
+ PngThumbnail.isPng = isPng;
41
+ /** The size a picture of `width` × `height` takes for `box`, never larger than it is. */
42
+ function size(width, height, box) {
43
+ let scale = box.fit === 'cover' ? Math.max(box.width / width, box.height / height) : Math.min(box.width / width, box.height / height);
44
+ if (box.fit === 'cover') {
45
+ scale = Math.min(scale, (box.width * COVER_LONG) / width, (box.height * COVER_LONG) / height);
46
+ }
47
+ scale = Math.min(1, scale);
48
+ return { width: Math.max(1, Math.round(width * scale)), height: Math.max(1, Math.round(height * scale)) };
49
+ }
50
+ PngThumbnail.size = size;
51
+ /**
52
+ * The smaller picture: a PNG, or a JPEG when the picture is opaque and of a photo (its PNG large for its pixels)
53
+ * and the JPEG is smaller.
54
+ */
55
+ async function make(bytes, box) {
56
+ const small = await smaller(bytes, box);
57
+ if (!small) {
58
+ return undefined;
59
+ }
60
+ const solid = opaque(small.rgba);
61
+ // Of its own size it is only written again as a JPEG: its PNG would be much what it is.
62
+ const png = small.resized ? encode(small.rgba, small.width, small.height) : undefined;
63
+ let made = png && { bytes: png, type: 'image/png' };
64
+ if (solid && (!png || png.length > small.width * small.height * PHOTO_BYTES_A_PIXEL)) {
65
+ await new Promise(resolve => setImmediate(resolve));
66
+ const jpeg = jpeg_encoder_1.JpegEncoder.encode(small.rgba, small.width, small.height, JPEG_QUALITY);
67
+ if (!made || jpeg.length < made.bytes.length) {
68
+ made = { bytes: jpeg, type: 'image/jpeg' };
69
+ }
70
+ }
71
+ // Not much smaller than the picture itself: the picture is sent.
72
+ return made && made.bytes.length < bytes.length * WORTH ? made : undefined;
73
+ }
74
+ PngThumbnail.make = make;
75
+ function opaque(rgba) {
76
+ for (let i = 3; i < rgba.length; i += 4) {
77
+ if (rgba[i] !== 255) {
78
+ return false;
79
+ }
80
+ }
81
+ return true;
82
+ }
83
+ async function smaller(bytes, box) {
84
+ if (!isPng(bytes)) {
85
+ return undefined;
86
+ }
87
+ const parsed = chunks(bytes);
88
+ if (!parsed) {
89
+ return undefined;
90
+ }
91
+ const { header, palette, transparency, data } = parsed;
92
+ const channels = CHANNELS[header.color];
93
+ if (!channels || header.interlace !== 0 || !depthFits(header) || header.width * header.height > MAX_PIXELS) {
94
+ return undefined;
95
+ }
96
+ // Near the size of the box, only a photo is written again: as a JPEG, smaller (make).
97
+ const pixels = header.width * header.height;
98
+ const near = size(header.width, header.height, box);
99
+ const out = near.width * near.height > pixels * WORTH * WORTH ? { width: header.width, height: header.height } : near;
100
+ if (out.width === header.width && bytes.length < pixels * PHOTO_BYTES_A_PIXEL * 2) {
101
+ return undefined;
102
+ }
103
+ let raw;
104
+ try {
105
+ raw = (0, node_zlib_1.inflateSync)(data);
106
+ }
107
+ catch (_a) {
108
+ return undefined;
109
+ }
110
+ const rgba = await average(raw, header, channels, palette, transparency, out.width, out.height);
111
+ return rgba && { width: out.width, height: out.height, rgba, resized: out.width !== header.width || out.height !== header.height };
112
+ }
113
+ /** The pixels of a PNG as RGBA, as `make` reads them; undefined for one it does not read. */
114
+ async function read(bytes) {
115
+ const parsed = isPng(bytes) ? chunks(bytes) : undefined;
116
+ const channels = parsed && CHANNELS[parsed.header.color];
117
+ if (!parsed || !channels || parsed.header.interlace !== 0 || !depthFits(parsed.header)) {
118
+ return undefined;
119
+ }
120
+ const { header, palette, transparency, data } = parsed;
121
+ const rgba = await average((0, node_zlib_1.inflateSync)(data), header, channels, palette, transparency, header.width, header.height);
122
+ return rgba && { width: header.width, height: header.height, rgba };
123
+ }
124
+ PngThumbnail.read = read;
125
+ const CHANNELS = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 };
126
+ function depthFits({ color, depth }) {
127
+ return color === 3 ? [1, 2, 4, 8].includes(depth) : color === 0 ? [1, 2, 4, 8, 16].includes(depth) : depth === 8 || depth === 16;
128
+ }
129
+ function chunks(bytes) {
130
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
131
+ let header;
132
+ let palette;
133
+ let transparency;
134
+ const data = [];
135
+ for (let at = 8; at + 8 <= bytes.length;) {
136
+ const length = view.getUint32(at);
137
+ const type = String.fromCharCode(bytes[at + 4], bytes[at + 5], bytes[at + 6], bytes[at + 7]);
138
+ const body = bytes.subarray(at + 8, at + 8 + length);
139
+ if (body.length !== length) {
140
+ return undefined;
141
+ }
142
+ if (type === 'IHDR') {
143
+ header = {
144
+ width: view.getUint32(at + 8),
145
+ height: view.getUint32(at + 12),
146
+ depth: bytes[at + 16],
147
+ color: bytes[at + 17],
148
+ interlace: bytes[at + 20],
149
+ };
150
+ }
151
+ else if (type === 'PLTE') {
152
+ palette = body;
153
+ }
154
+ else if (type === 'tRNS') {
155
+ transparency = body;
156
+ }
157
+ else if (type === 'IDAT') {
158
+ data.push(body);
159
+ }
160
+ else if (type === 'IEND') {
161
+ break;
162
+ }
163
+ at += 12 + length;
164
+ }
165
+ return header && header.width > 0 && header.height > 0 && data.length > 0 ? { header, palette, transparency, data: Buffer.concat(data) } : undefined;
166
+ }
167
+ /** The image averaged down to `width` × `height`, as RGBA. */
168
+ async function average(raw, header, channels, palette, transparency, width, height) {
169
+ const { width: sourceWidth, height: sourceHeight, depth, color } = header;
170
+ const bits = channels * depth;
171
+ const stride = Math.ceil((sourceWidth * bits) / 8);
172
+ const step = Math.max(1, bits >> 3);
173
+ if (raw.length < (stride + 1) * sourceHeight || (color === 3 && !palette)) {
174
+ return undefined;
175
+ }
176
+ // Premultiplied sums of each pixel of the smaller picture, and its alpha.
177
+ const sums = new Float64Array(width * height * 4);
178
+ const counts = new Uint32Array(width * height);
179
+ const columns = new Uint32Array(sourceWidth);
180
+ for (let x = 0; x < sourceWidth; x++) {
181
+ columns[x] = Math.min(width - 1, Math.floor((x * width) / sourceWidth));
182
+ }
183
+ const max = (1 << Math.min(depth, 8)) - 1;
184
+ const key = transparency && (color === 0 || color === 2) ? transparency : undefined;
185
+ let previous = new Uint8Array(stride);
186
+ let line = new Uint8Array(stride);
187
+ const rgba = [0, 0, 0, 0];
188
+ for (let y = 0; y < sourceHeight; y++) {
189
+ if (y % ROWS_A_TURN === ROWS_A_TURN - 1) {
190
+ await new Promise(resolve => setImmediate(resolve));
191
+ }
192
+ const start = y * (stride + 1);
193
+ const filter = raw[start];
194
+ line.set(raw.subarray(start + 1, start + 1 + stride));
195
+ if (!unfilter(filter, line, previous, step)) {
196
+ return undefined;
197
+ }
198
+ const row = Math.min(height - 1, Math.floor((y * height) / sourceHeight)) * width;
199
+ for (let x = 0; x < sourceWidth; x++) {
200
+ pixel(line, x, depth, color, max, palette, transparency, key, rgba);
201
+ const at = row + columns[x];
202
+ const alpha = rgba[3];
203
+ sums[at * 4] += rgba[0] * alpha;
204
+ sums[at * 4 + 1] += rgba[1] * alpha;
205
+ sums[at * 4 + 2] += rgba[2] * alpha;
206
+ sums[at * 4 + 3] += alpha;
207
+ counts[at]++;
208
+ }
209
+ [previous, line] = [line, previous];
210
+ }
211
+ const out = new Uint8Array(width * height * 4);
212
+ for (let i = 0; i < width * height; i++) {
213
+ const alpha = sums[i * 4 + 3];
214
+ if (alpha > 0) {
215
+ out[i * 4] = Math.round(sums[i * 4] / alpha);
216
+ out[i * 4 + 1] = Math.round(sums[i * 4 + 1] / alpha);
217
+ out[i * 4 + 2] = Math.round(sums[i * 4 + 2] / alpha);
218
+ }
219
+ out[i * 4 + 3] = counts[i] ? Math.round(alpha / counts[i]) : 0;
220
+ }
221
+ return out;
222
+ }
223
+ function unfilter(filter, line, previous, step) {
224
+ const length = line.length;
225
+ switch (filter) {
226
+ case 0:
227
+ return true;
228
+ case 1:
229
+ for (let i = step; i < length; i++) {
230
+ line[i] = (line[i] + line[i - step]) & 255;
231
+ }
232
+ return true;
233
+ case 2:
234
+ for (let i = 0; i < length; i++) {
235
+ line[i] = (line[i] + previous[i]) & 255;
236
+ }
237
+ return true;
238
+ case 3:
239
+ for (let i = 0; i < length; i++) {
240
+ line[i] = (line[i] + (((i >= step ? line[i - step] : 0) + previous[i]) >> 1)) & 255;
241
+ }
242
+ return true;
243
+ case 4:
244
+ for (let i = 0; i < length; i++) {
245
+ line[i] = (line[i] + paeth(i >= step ? line[i - step] : 0, previous[i], i >= step ? previous[i - step] : 0)) & 255;
246
+ }
247
+ return true;
248
+ default:
249
+ return false;
250
+ }
251
+ }
252
+ function paeth(a, b, c) {
253
+ const p = a + b - c;
254
+ const pa = Math.abs(p - a);
255
+ const pb = Math.abs(p - b);
256
+ const pc = Math.abs(p - c);
257
+ return pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
258
+ }
259
+ /** The RGBA (0–255) of pixel `x` of a row, into `out`. */
260
+ function pixel(line, x, depth, color, max, palette, transparency, key, out) {
261
+ if (depth < 8) {
262
+ const bit = x * depth;
263
+ const value = (line[bit >> 3] >> (8 - depth - (bit & 7))) & max;
264
+ if (color === 3 && palette) {
265
+ fromPalette(value, palette, transparency, out);
266
+ }
267
+ else {
268
+ const gray = Math.round((value * 255) / max);
269
+ out[0] = out[1] = out[2] = gray;
270
+ out[3] = key && ((key[0] << 8) | key[1]) === value ? 0 : 255;
271
+ }
272
+ return;
273
+ }
274
+ const wide = depth === 16;
275
+ const size = wide ? 2 : 1;
276
+ const sample = (channel, channels) => line[(x * channels + channel) * size];
277
+ switch (color) {
278
+ case 0: {
279
+ const gray = sample(0, 1);
280
+ out[0] = out[1] = out[2] = gray;
281
+ out[3] = key && !wide && key[1] === gray ? 0 : 255;
282
+ return;
283
+ }
284
+ case 2:
285
+ out[0] = sample(0, 3);
286
+ out[1] = sample(1, 3);
287
+ out[2] = sample(2, 3);
288
+ out[3] = key && !wide && key[1] === out[0] && key[3] === out[1] && key[5] === out[2] ? 0 : 255;
289
+ return;
290
+ case 3:
291
+ fromPalette(line[x], palette, transparency, out);
292
+ return;
293
+ case 4:
294
+ out[0] = out[1] = out[2] = sample(0, 2);
295
+ out[3] = sample(1, 2);
296
+ return;
297
+ default:
298
+ out[0] = sample(0, 4);
299
+ out[1] = sample(1, 4);
300
+ out[2] = sample(2, 4);
301
+ out[3] = sample(3, 4);
302
+ }
303
+ }
304
+ function fromPalette(index, palette, transparency, out) {
305
+ var _a, _b, _c;
306
+ out[0] = (_a = palette[index * 3]) !== null && _a !== void 0 ? _a : 0;
307
+ out[1] = (_b = palette[index * 3 + 1]) !== null && _b !== void 0 ? _b : 0;
308
+ out[2] = (_c = palette[index * 3 + 2]) !== null && _c !== void 0 ? _c : 0;
309
+ out[3] = transparency && index < transparency.length ? transparency[index] : 255;
310
+ }
311
+ /** RGBA written as a PNG: RGB when every pixel is opaque, each row with the filter that leaves the smallest sums. */
312
+ function encode(rgba, width, height) {
313
+ const solid = opaque(rgba);
314
+ const step = solid ? 3 : 4;
315
+ const stride = width * step;
316
+ const raw = new Uint8Array((stride + 1) * height);
317
+ let previous = new Uint8Array(stride);
318
+ let line = new Uint8Array(stride);
319
+ const trial = new Uint8Array(stride);
320
+ const best = new Uint8Array(stride);
321
+ for (let y = 0; y < height; y++) {
322
+ for (let x = 0; x < width; x++) {
323
+ for (let c = 0; c < step; c++) {
324
+ line[x * step + c] = rgba[(y * width + x) * 4 + c];
325
+ }
326
+ }
327
+ let bestFilter = 0;
328
+ let bestScore = Number.POSITIVE_INFINITY;
329
+ for (let filter = 0; filter < 5; filter++) {
330
+ let score = 0;
331
+ for (let i = 0; i < stride; i++) {
332
+ const left = i >= step ? line[i - step] : 0;
333
+ const up = previous[i];
334
+ const corner = i >= step ? previous[i - step] : 0;
335
+ const predicted = filter === 0 ? 0 : filter === 1 ? left : filter === 2 ? up : filter === 3 ? (left + up) >> 1 : paeth(left, up, corner);
336
+ const value = (line[i] - predicted) & 255;
337
+ trial[i] = value;
338
+ score += value < 128 ? value : 256 - value;
339
+ }
340
+ if (score < bestScore) {
341
+ bestScore = score;
342
+ bestFilter = filter;
343
+ best.set(trial);
344
+ }
345
+ }
346
+ raw[y * (stride + 1)] = bestFilter;
347
+ raw.set(best, y * (stride + 1) + 1);
348
+ [previous, line] = [line, previous];
349
+ }
350
+ const header = new Uint8Array(13);
351
+ const view = new DataView(header.buffer);
352
+ view.setUint32(0, width);
353
+ view.setUint32(4, height);
354
+ header[8] = 8;
355
+ header[9] = solid ? 2 : 6;
356
+ return chunked([
357
+ ['IHDR', header],
358
+ ['IDAT', (0, node_zlib_1.deflateSync)(raw, { level: 6 })],
359
+ ['IEND', new Uint8Array(0)],
360
+ ]);
361
+ }
362
+ PngThumbnail.encode = encode;
363
+ /** A PNG of these chunks, each with its length and CRC. */
364
+ function chunked(parts) {
365
+ return Buffer.concat([Uint8Array.from(SIGNATURE), ...parts.map(([type, body]) => chunk(type, body))]);
366
+ }
367
+ PngThumbnail.chunked = chunked;
368
+ function chunk(type, body) {
369
+ const out = new Uint8Array(12 + body.length);
370
+ const view = new DataView(out.buffer);
371
+ view.setUint32(0, body.length);
372
+ for (let i = 0; i < 4; i++) {
373
+ out[4 + i] = type.charCodeAt(i);
374
+ }
375
+ out.set(body, 8);
376
+ view.setUint32(8 + body.length, crc(out.subarray(4, 8 + body.length)));
377
+ return out;
378
+ }
379
+ function crc(bytes) {
380
+ let c = 0xffffffff;
381
+ for (let i = 0; i < bytes.length; i++) {
382
+ c = CRC_TABLE[(c ^ bytes[i]) & 255] ^ (c >>> 8);
383
+ }
384
+ return (c ^ 0xffffffff) >>> 0;
385
+ }
386
+ })(PngThumbnail || (exports.PngThumbnail = PngThumbnail = {}));
387
+ //# sourceMappingURL=png-thumbnail.js.map
@@ -0,0 +1,39 @@
1
+ import { type Application, ApplicationLifecycle } from '@celljs/core';
2
+ import type { TaServerUpdate } from '@celljs/ta-protocol';
3
+ export declare const TaServeUpdates: unique symbol;
4
+ /**
5
+ * The build of Ta that runs, and one installed over it since (an update of Ta Desktop, `npm i -g`, a build of the
6
+ * repository), which a restart of ta serve would run: TaServerInfo.version, build and update.
7
+ */
8
+ export interface TaServeUpdates {
9
+ /** The build read as the server started. */
10
+ running(): TaInstalledBuild;
11
+ /** Another build installed since, if any. */
12
+ update(): TaServerUpdate | undefined;
13
+ }
14
+ /** A build of Ta in its directory. */
15
+ export interface TaInstalledBuild extends TaServerUpdate {
16
+ /** What changes with each build: the file of its package.json, replaced or written again. */
17
+ stamp?: string;
18
+ }
19
+ export declare namespace TaServeUpdateUtil {
20
+ /** Where pi reads ta's package.json (TaBrand.activate): dist/pi of @celljs/ta, which each build writes again. */
21
+ function directory(): string | undefined;
22
+ function stamp(dir: string): string | undefined;
23
+ /**
24
+ * The build in `dir`: its version, and for a build of the development channel of Ta Desktop its commit, from
25
+ * ta-build.json in Contents/Resources of the app (`dir` is Resources/ta/dist/pi). Undefined while it cannot be read.
26
+ */
27
+ function read(dir: string): TaInstalledBuild | undefined;
28
+ }
29
+ export declare class TaServeUpdatesImpl implements TaServeUpdates, ApplicationLifecycle<Application> {
30
+ protected readonly dir: string | undefined;
31
+ /** As the server started: the files of the build that runs, read before anything replaced them. */
32
+ protected readonly started: TaInstalledBuild;
33
+ /** The build found installed over it, read once for each stamp. */
34
+ protected found?: TaInstalledBuild;
35
+ onStart(): void;
36
+ running(): TaInstalledBuild;
37
+ update(): TaServerUpdate | undefined;
38
+ }
39
+ //# sourceMappingURL=serve-updates.d.ts.map
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.TaServeUpdatesImpl = exports.TaServeUpdateUtil = exports.TaServeUpdates = void 0;
10
+ const node_fs_1 = require("node:fs");
11
+ const node_path_1 = require("node:path");
12
+ const core_1 = require("@celljs/core");
13
+ exports.TaServeUpdates = Symbol('TaServeUpdates');
14
+ var TaServeUpdateUtil;
15
+ (function (TaServeUpdateUtil) {
16
+ /** Where pi reads ta's package.json (TaBrand.activate): dist/pi of @celljs/ta, which each build writes again. */
17
+ function directory() {
18
+ return process.env.PI_PACKAGE_DIR || undefined;
19
+ }
20
+ TaServeUpdateUtil.directory = directory;
21
+ function stamp(dir) {
22
+ try {
23
+ const { ino, mtimeMs, size } = (0, node_fs_1.statSync)((0, node_path_1.join)(dir, 'package.json'));
24
+ return `${ino}:${mtimeMs}:${size}`;
25
+ }
26
+ catch (_a) {
27
+ return undefined;
28
+ }
29
+ }
30
+ TaServeUpdateUtil.stamp = stamp;
31
+ /**
32
+ * The build in `dir`: its version, and for a build of the development channel of Ta Desktop its commit, from
33
+ * ta-build.json in Contents/Resources of the app (`dir` is Resources/ta/dist/pi). Undefined while it cannot be read.
34
+ */
35
+ function read(dir) {
36
+ const stamp = TaServeUpdateUtil.stamp(dir);
37
+ let version;
38
+ try {
39
+ version = JSON.parse((0, node_fs_1.readFileSync)((0, node_path_1.join)(dir, 'package.json'), 'utf8')).version;
40
+ }
41
+ catch (_a) {
42
+ return undefined;
43
+ }
44
+ if (typeof version !== 'string') {
45
+ return undefined;
46
+ }
47
+ return { version, ...buildOf((0, node_path_1.join)(dir, '..', '..', '..', 'ta-build.json')), ...(stamp ? { stamp } : {}) };
48
+ }
49
+ TaServeUpdateUtil.read = read;
50
+ function buildOf(file) {
51
+ try {
52
+ const { channel, commit } = JSON.parse((0, node_fs_1.readFileSync)(file, 'utf8'));
53
+ return channel === 'dev' && typeof commit === 'string' && commit ? { build: commit.slice(0, 7) } : {};
54
+ }
55
+ catch (_a) {
56
+ return {};
57
+ }
58
+ }
59
+ })(TaServeUpdateUtil || (exports.TaServeUpdateUtil = TaServeUpdateUtil = {}));
60
+ let TaServeUpdatesImpl = class TaServeUpdatesImpl {
61
+ constructor() {
62
+ this.dir = TaServeUpdateUtil.directory();
63
+ /** As the server started: the files of the build that runs, read before anything replaced them. */
64
+ this.started = (this.dir && TaServeUpdateUtil.read(this.dir)) || { version: 'unknown' };
65
+ }
66
+ onStart() {
67
+ // Constructed as the server starts, so that `started` is the build that runs.
68
+ }
69
+ running() {
70
+ return this.started;
71
+ }
72
+ update() {
73
+ var _a;
74
+ const stamp = this.dir && TaServeUpdateUtil.stamp(this.dir);
75
+ if (!this.dir || !stamp || !this.started.stamp || stamp === this.started.stamp) {
76
+ return undefined;
77
+ }
78
+ if (((_a = this.found) === null || _a === void 0 ? void 0 : _a.stamp) !== stamp) {
79
+ this.found = TaServeUpdateUtil.read(this.dir);
80
+ }
81
+ if (!this.found) {
82
+ return undefined;
83
+ }
84
+ const { version, build } = this.found;
85
+ return { version, ...(build ? { build } : {}) };
86
+ }
87
+ };
88
+ exports.TaServeUpdatesImpl = TaServeUpdatesImpl;
89
+ exports.TaServeUpdatesImpl = TaServeUpdatesImpl = __decorate([
90
+ (0, core_1.Component)([exports.TaServeUpdates, core_1.ApplicationLifecycle])
91
+ ], TaServeUpdatesImpl);
92
+ //# sourceMappingURL=serve-updates.js.map
@@ -1,6 +1,7 @@
1
1
  import { ExecEnvironmentFactory } from '@celljs/ta-exec';
2
2
  import { type ImageContent, type RpcExtensionUIResponse, type TaAttachOptions, type TaAttachResult, type TaClientToolResult, type TaCommandResult, type TaForkFileResult, type TaMessagePage, type TaNavigateResult, type TaOpenedSession, type TaOpenSessionOptions, type TaPromptInput, type TaPromptReceipt, type TaSavedSessionInfo, type TaSavedSessionQuery, TaServer, type TaServerInfo, type TaSessionInfo, type TaSlashCommand, type ThinkingLevel } from '@celljs/ta-protocol';
3
3
  import { TaSessionCatalog, TaSessionFactory, TaSessionRegistry } from './host-protocol';
4
+ import { TaServeUpdates } from './serve-updates';
4
5
  import type { TaSession } from './ta-session';
5
6
  import { TaAuthority, TaPrincipals, TaRunActors, TaVisibility } from './team/team-protocol';
6
7
  import { TaWorkingDirectory } from './working-directory';
@@ -19,7 +20,9 @@ export declare class TaServerImpl implements TaServer {
19
20
  protected readonly principals: TaPrincipals;
20
21
  protected readonly authority: TaAuthority;
21
22
  protected readonly visibility: TaVisibility;
23
+ protected readonly updates: TaServeUpdates;
22
24
  getServerInfo(): Promise<TaServerInfo>;
25
+ restartServer(): Promise<void>;
23
26
  setWorkingDirectory(cwd: string): Promise<TaServerInfo>;
24
27
  openSession({ attach, ...options }: TaOpenSessionOptions): Promise<TaOpenedSession>;
25
28
  closeSession(sessionId: string): Promise<void>;
@@ -17,14 +17,17 @@ const core_1 = require("@celljs/core");
17
17
  const rpc_1 = require("@celljs/rpc");
18
18
  const node_1 = require("@celljs/rpc/lib/node");
19
19
  const ta_exec_1 = require("@celljs/ta-exec");
20
- const ta_brand_1 = require("@celljs/ta-harness/lib/node/ta-brand");
20
+ const serve_restart_1 = require("@celljs/ta-harness/lib/node/serve-restart");
21
21
  const ta_protocol_1 = require("@celljs/ta-protocol");
22
22
  const host_protocol_1 = require("./host-protocol");
23
+ const serve_updates_1 = require("./serve-updates");
23
24
  const session_info_util_1 = require("./session-info-util");
24
25
  const slash_command_util_1 = require("./slash-command-util");
25
26
  const ta_errors_1 = require("./ta-errors");
26
27
  const team_protocol_1 = require("./team/team-protocol");
27
28
  const working_directory_1 = require("./working-directory");
29
+ /** How long restartServer leaves its answer to go out before ta serve stops. */
30
+ const RESTART_DELAY_MS = 200;
28
31
  /**
29
32
  * The sessions of ta serve. Each command of pi's RPC mode does here what it does in pi's `rpc-mode`, which
30
33
  * pi exports only bound to stdio: when pi changes one, change it here too (the spec of TaServer in
@@ -32,14 +35,31 @@ const working_directory_1 = require("./working-directory");
32
35
  */
33
36
  let TaServerImpl = class TaServerImpl {
34
37
  async getServerInfo() {
35
- return this.guard(async () => ({
36
- version: ta_brand_1.TaBrand.version(),
37
- cwd: await this.environments.defaultCwd(),
38
- user: (0, node_os_1.userInfo)().username,
39
- host: (0, node_os_1.hostname)().replace(/\.local$/, ''),
40
- home: (0, node_os_1.homedir)(),
41
- ...(this.member() ? { member: this.member() } : {}),
42
- }));
38
+ return this.guard(async () => {
39
+ const { version, build } = this.updates.running();
40
+ const member = this.member();
41
+ // Restarting is the owner's: one let in is not told of it.
42
+ const update = member ? undefined : this.updates.update();
43
+ return {
44
+ version,
45
+ ...(build ? { build } : {}),
46
+ cwd: await this.environments.defaultCwd(),
47
+ user: (0, node_os_1.userInfo)().username,
48
+ host: (0, node_os_1.hostname)().replace(/\.local$/, ''),
49
+ home: (0, node_os_1.homedir)(),
50
+ ...(member ? { member } : { restartable: serve_restart_1.TaServeRestart.supported() }),
51
+ ...(update ? { update } : {}),
52
+ };
53
+ });
54
+ }
55
+ async restartServer() {
56
+ return this.guard(async () => {
57
+ if (!serve_restart_1.TaServeRestart.supported()) {
58
+ throw ta_errors_1.TaErrors.invalidArgument('This Ta service was not started as ta serve, and cannot restart itself.', 'Stop it and start it again where it was started.');
59
+ }
60
+ // Once the answer went out: then the connections close, and connect again to the new one.
61
+ setTimeout(() => serve_restart_1.TaServeRestart.request(), RESTART_DELAY_MS);
62
+ });
43
63
  }
44
64
  async setWorkingDirectory(cwd) {
45
65
  return this.guard(async () => {
@@ -415,6 +435,10 @@ __decorate([
415
435
  (0, core_1.Autowired)(team_protocol_1.TaVisibility),
416
436
  __metadata("design:type", Object)
417
437
  ], TaServerImpl.prototype, "visibility", void 0);
438
+ __decorate([
439
+ (0, core_1.Autowired)(serve_updates_1.TaServeUpdates),
440
+ __metadata("design:type", Object)
441
+ ], TaServerImpl.prototype, "updates", void 0);
418
442
  exports.TaServerImpl = TaServerImpl = __decorate([
419
443
  (0, rpc_1.Rpc)(ta_protocol_1.TaServer)
420
444
  ], TaServerImpl);
@@ -60,6 +60,7 @@ const RULES = {
60
60
  TaServer: {
61
61
  getServerInfo: anyone,
62
62
  setWorkingDirectory: fixed('settings.manage'),
63
+ restartServer: owner,
63
64
  openSession: (params, lookup) => {
64
65
  var _a;
65
66
  const options = ((_a = params[0]) !== null && _a !== void 0 ? _a : {});
@@ -248,6 +249,11 @@ const RULES = {
248
249
  disallow: fixed('members.roles'),
249
250
  signOut: fixed('members.roles'),
250
251
  },
252
+ // What the agents took and did: for the owner (and the gateway, which calls as the owner).
253
+ TaUsageServer: {
254
+ getUsage: owner,
255
+ getActivity: owner,
256
+ },
251
257
  TaRemoteServer: {
252
258
  listHosts: fixed('hosts.use'),
253
259
  addHost: fixed('settings.manage'),
@@ -0,0 +1,14 @@
1
+ import type { TaAgentActivity } from '@celljs/ta-protocol';
2
+ export declare namespace AgentActivityUtil {
3
+ const DEFAULT_LIMIT = 50;
4
+ const MAX_LIMIT = 200;
5
+ function limit(value: unknown): number;
6
+ function summary(text: string | undefined): string | undefined;
7
+ /**
8
+ * The tool calls and failed calls of the model in the entries of a session, newest first: a call without a
9
+ * result yet is `running`. Arguments and results never: a tool's name, whether it worked, the first line of
10
+ * its error, its secrets hidden.
11
+ */
12
+ function from(entries: readonly unknown[], max: number): TaAgentActivity[];
13
+ }
14
+ //# sourceMappingURL=agent-activity.d.ts.map