@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,84 @@
1
+ import { Redactor } from '@celljs/ta-protocol/lib/common/redactor';
2
+ /** The first line of an error, its secrets hidden, cut short: what a console shows of it. */
3
+ const ERROR_LENGTH = 160;
4
+ export var AgentActivityUtil;
5
+ (function (AgentActivityUtil) {
6
+ AgentActivityUtil.DEFAULT_LIMIT = 50;
7
+ AgentActivityUtil.MAX_LIMIT = 200;
8
+ function limit(value) {
9
+ const count = Math.floor(Number(value !== null && value !== void 0 ? value : AgentActivityUtil.DEFAULT_LIMIT));
10
+ return Number.isFinite(count) && count > 0 ? Math.min(count, AgentActivityUtil.MAX_LIMIT) : AgentActivityUtil.DEFAULT_LIMIT;
11
+ }
12
+ AgentActivityUtil.limit = limit;
13
+ function summary(text) {
14
+ var _a;
15
+ const line = (_a = text === null || text === void 0 ? void 0 : text.split('\n').find(part => part.trim())) === null || _a === void 0 ? void 0 : _a.trim();
16
+ return line ? Redactor.cut(new Redactor().text(line), ERROR_LENGTH) : undefined;
17
+ }
18
+ AgentActivityUtil.summary = summary;
19
+ function text(content) {
20
+ if (typeof content === 'string') {
21
+ return content;
22
+ }
23
+ if (Array.isArray(content)) {
24
+ return content
25
+ .map(part => { var _a; return part && typeof part === 'object' && part.type === 'text' ? String((_a = part.text) !== null && _a !== void 0 ? _a : '') : ''; })
26
+ .join('\n');
27
+ }
28
+ return undefined;
29
+ }
30
+ /**
31
+ * The tool calls and failed calls of the model in the entries of a session, newest first: a call without a
32
+ * result yet is `running`. Arguments and results never: a tool's name, whether it worked, the first line of
33
+ * its error, its secrets hidden.
34
+ */
35
+ function from(entries, max) {
36
+ var _a, _b, _c;
37
+ const calls = new Map();
38
+ const list = [];
39
+ for (const raw of entries) {
40
+ const entry = raw;
41
+ const message = entry.message;
42
+ if (entry.type !== 'message' || !message) {
43
+ continue;
44
+ }
45
+ const at = (_a = message.timestamp) !== null && _a !== void 0 ? _a : (entry.timestamp ? Date.parse(entry.timestamp) : 0);
46
+ if (message.role === 'assistant') {
47
+ if (message.stopReason === 'error') {
48
+ list.push({ at, kind: 'model', name: (_b = message.model) !== null && _b !== void 0 ? _b : 'model', ok: false, error: summary(message.errorMessage) });
49
+ }
50
+ if (Array.isArray(message.content)) {
51
+ for (const part of message.content) {
52
+ if ((part === null || part === void 0 ? void 0 : part.type) === 'toolCall' && part.id) {
53
+ const call = { at, kind: 'tool', name: (_c = part.name) !== null && _c !== void 0 ? _c : 'tool', ok: true, running: true };
54
+ calls.set(part.id, call);
55
+ list.push(call);
56
+ }
57
+ }
58
+ }
59
+ }
60
+ else if (message.role === 'toolResult') {
61
+ const call = message.toolCallId ? calls.get(message.toolCallId) : undefined;
62
+ if (call) {
63
+ delete call.running;
64
+ call.ok = !message.isError;
65
+ if (at > call.at) {
66
+ call.ms = at - call.at;
67
+ }
68
+ const error = message.isError ? summary(text(message.content)) : undefined;
69
+ if (error) {
70
+ call.error = error;
71
+ }
72
+ }
73
+ }
74
+ }
75
+ for (const each of list) {
76
+ if (each.error === undefined) {
77
+ delete each.error;
78
+ }
79
+ }
80
+ return list.reverse().slice(0, max);
81
+ }
82
+ AgentActivityUtil.from = from;
83
+ })(AgentActivityUtil || (AgentActivityUtil = {}));
84
+ //# sourceMappingURL=agent-activity.js.map
@@ -0,0 +1,3 @@
1
+ export * from './agent-activity.js';
2
+ export * from './usage-reader.js';
3
+ export * from './usage-server-impl.js';
@@ -0,0 +1,4 @@
1
+ export * from './agent-activity.js';
2
+ export * from './usage-reader.js';
3
+ export * from './usage-server-impl.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,28 @@
1
+ import type { TaUsageBucket } from '@celljs/ta-protocol';
2
+ interface FileUsage {
3
+ /** Bytes read so far: whole lines only. */
4
+ offset: number;
5
+ /** By `<hour>|<model>`. */
6
+ buckets: Map<string, TaUsageBucket>;
7
+ }
8
+ export declare namespace UsageReaderUtil {
9
+ const HOUR_MS = 3600000;
10
+ /** What one line adds to the buckets, if it is an answer of a model with its usage. */
11
+ function add(buckets: Map<string, TaUsageBucket>, text: string): void;
12
+ /** The buckets of many files from `since` on, an hour and a model once. */
13
+ function merge(lists: Iterable<Iterable<TaUsageBucket>>, since: number): TaUsageBucket[];
14
+ }
15
+ /**
16
+ * What the models took, by hour, from the session files under a directory (pi's `sessions/<dir>/*.jsonl`). A file
17
+ * is read once, then only what was appended to it; one unchanged since before the window is not read at all.
18
+ */
19
+ export declare class UsageReader {
20
+ protected readonly root: () => Promise<string>;
21
+ protected readonly files: Map<string, FileUsage>;
22
+ constructor(root: () => Promise<string>);
23
+ read(since: number): Promise<TaUsageBucket[]>;
24
+ protected file(path: string, since: number): Promise<FileUsage | undefined>;
25
+ /** The lines written since the last time; a line still being written waits for the next. */
26
+ protected append(path: string, usage: FileUsage, size: number): Promise<void>;
27
+ }
28
+ export {};
@@ -0,0 +1,159 @@
1
+ import { open, readdir, stat } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ const HOUR = 3600000;
4
+ /** Read at once, at most: a file grows by its tail, which is what is read after the first time. */
5
+ const CHUNK = 1 << 20;
6
+ export var UsageReaderUtil;
7
+ (function (UsageReaderUtil) {
8
+ UsageReaderUtil.HOUR_MS = HOUR;
9
+ /** What one line adds to the buckets, if it is an answer of a model with its usage. */
10
+ function add(buckets, text) {
11
+ var _a;
12
+ var _b, _c, _d, _e, _f, _g, _h, _j;
13
+ // Most lines are not answers, and some are large: those are not parsed.
14
+ if (!text.includes('"usage"') || !text.includes('"assistant"')) {
15
+ return;
16
+ }
17
+ let line;
18
+ try {
19
+ line = JSON.parse(text);
20
+ }
21
+ catch (_k) {
22
+ return;
23
+ }
24
+ const message = line.message;
25
+ if (line.type !== 'message' || (message === null || message === void 0 ? void 0 : message.role) !== 'assistant' || !message.usage) {
26
+ return;
27
+ }
28
+ const time = (_b = message.timestamp) !== null && _b !== void 0 ? _b : (line.timestamp ? Date.parse(line.timestamp) : Number.NaN);
29
+ if (!Number.isFinite(time)) {
30
+ return;
31
+ }
32
+ const at = Math.floor(time / HOUR) * HOUR;
33
+ const model = `${(_c = message.provider) !== null && _c !== void 0 ? _c : 'unknown'}/${(_d = message.model) !== null && _d !== void 0 ? _d : 'unknown'}`;
34
+ const key = `${at}|${model}`;
35
+ let bucket = buckets.get(key);
36
+ if (!bucket) {
37
+ bucket = { at, model, input: 0, output: 0, cacheRead: 0, cacheWrite: 0, cost: 0, turns: 0 };
38
+ buckets.set(key, bucket);
39
+ }
40
+ const usage = message.usage;
41
+ bucket.input += (_e = usage.input) !== null && _e !== void 0 ? _e : 0;
42
+ bucket.output += (_f = usage.output) !== null && _f !== void 0 ? _f : 0;
43
+ bucket.cacheRead += (_g = usage.cacheRead) !== null && _g !== void 0 ? _g : 0;
44
+ bucket.cacheWrite += (_h = usage.cacheWrite) !== null && _h !== void 0 ? _h : 0;
45
+ bucket.cost += (_j = (_a = usage.cost) === null || _a === void 0 ? void 0 : _a.total) !== null && _j !== void 0 ? _j : 0;
46
+ bucket.turns++;
47
+ }
48
+ UsageReaderUtil.add = add;
49
+ /** The buckets of many files from `since` on, an hour and a model once. */
50
+ function merge(lists, since) {
51
+ const merged = new Map();
52
+ for (const list of lists) {
53
+ for (const bucket of list) {
54
+ if (bucket.at + HOUR <= since) {
55
+ continue;
56
+ }
57
+ const key = `${bucket.at}|${bucket.model}`;
58
+ const known = merged.get(key);
59
+ if (known) {
60
+ known.input += bucket.input;
61
+ known.output += bucket.output;
62
+ known.cacheRead += bucket.cacheRead;
63
+ known.cacheWrite += bucket.cacheWrite;
64
+ known.cost += bucket.cost;
65
+ known.turns += bucket.turns;
66
+ }
67
+ else {
68
+ merged.set(key, { ...bucket });
69
+ }
70
+ }
71
+ }
72
+ return [...merged.values()].sort((a, b) => a.at - b.at || a.model.localeCompare(b.model));
73
+ }
74
+ UsageReaderUtil.merge = merge;
75
+ })(UsageReaderUtil || (UsageReaderUtil = {}));
76
+ /**
77
+ * What the models took, by hour, from the session files under a directory (pi's `sessions/<dir>/*.jsonl`). A file
78
+ * is read once, then only what was appended to it; one unchanged since before the window is not read at all.
79
+ */
80
+ export class UsageReader {
81
+ constructor(root) {
82
+ this.root = root;
83
+ this.files = new Map();
84
+ }
85
+ async read(since) {
86
+ const root = await this.root();
87
+ const dirs = await readdir(root, { withFileTypes: true }).catch(() => []);
88
+ const paths = (await Promise.all(dirs
89
+ .filter(dir => dir.isDirectory())
90
+ .map(async (dir) => (await readdir(join(root, dir.name)).catch(() => []))
91
+ .filter(name => name.endsWith('.jsonl'))
92
+ .map(name => join(root, dir.name, name))))).flat();
93
+ const listed = new Set(paths);
94
+ for (const path of this.files.keys()) {
95
+ if (!listed.has(path)) {
96
+ this.files.delete(path);
97
+ }
98
+ }
99
+ const used = [];
100
+ for (const path of paths) {
101
+ const usage = await this.file(path, since);
102
+ if (usage) {
103
+ used.push(usage.buckets);
104
+ }
105
+ }
106
+ return UsageReaderUtil.merge(used.map(buckets => buckets.values()), since);
107
+ }
108
+ async file(path, since) {
109
+ const stats = await stat(path).catch(() => undefined);
110
+ if (!(stats === null || stats === void 0 ? void 0 : stats.isFile())) {
111
+ return undefined;
112
+ }
113
+ let usage = this.files.get(path);
114
+ if (!usage && stats.mtimeMs < since) {
115
+ // Last written before the window: nothing in it is of the window.
116
+ return undefined;
117
+ }
118
+ if (!usage || stats.size < usage.offset) {
119
+ usage = { offset: 0, buckets: new Map() };
120
+ this.files.set(path, usage);
121
+ }
122
+ if (stats.size > usage.offset) {
123
+ await this.append(path, usage, stats.size);
124
+ }
125
+ return usage;
126
+ }
127
+ /** The lines written since the last time; a line still being written waits for the next. */
128
+ async append(path, usage, size) {
129
+ const handle = await open(path, 'r').catch(() => undefined);
130
+ if (!handle) {
131
+ return;
132
+ }
133
+ try {
134
+ let partial = Buffer.alloc(0);
135
+ let position = usage.offset;
136
+ const buffer = Buffer.alloc(CHUNK);
137
+ while (position < size) {
138
+ const { bytesRead } = await handle.read(buffer, 0, Math.min(CHUNK, size - position), position);
139
+ if (bytesRead === 0) {
140
+ break;
141
+ }
142
+ position += bytesRead;
143
+ // Split as bytes: a character of several bytes may be cut at the end of a chunk.
144
+ const data = Buffer.concat([partial, buffer.subarray(0, bytesRead)]);
145
+ let start = 0;
146
+ for (let end = data.indexOf(10); end >= 0; end = data.indexOf(10, start)) {
147
+ UsageReaderUtil.add(usage.buckets, data.toString('utf8', start, end));
148
+ start = end + 1;
149
+ }
150
+ partial = Buffer.from(data.subarray(start));
151
+ }
152
+ usage.offset = position - partial.length;
153
+ }
154
+ finally {
155
+ await handle.close();
156
+ }
157
+ }
158
+ }
159
+ //# sourceMappingURL=usage-reader.js.map
@@ -0,0 +1,12 @@
1
+ import { type CodingAgentOptions } from '@celljs/ai-coding-agent';
2
+ import { type TaAgentActivity, type TaUsageBucket, TaUsageServer } from '@celljs/ta-protocol';
3
+ import { TaSessionRegistry } from '../host-protocol.js';
4
+ import { UsageReader } from './usage-reader.js';
5
+ /** What the agents of this Ta took, from its session files, and what one of them did lately, from its entries. */
6
+ export declare class TaUsageServerImpl implements TaUsageServer {
7
+ protected readonly codingAgentOptions?: CodingAgentOptions;
8
+ protected readonly sessions: TaSessionRegistry;
9
+ protected readonly reader: UsageReader;
10
+ getUsage(since: number): Promise<TaUsageBucket[]>;
11
+ getActivity(sessionId: string, limit?: number): Promise<TaAgentActivity[]>;
12
+ }
@@ -0,0 +1,46 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ 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;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { join } from 'node:path';
11
+ import { CODING_AGENT_OPTIONS, PiSdkUtil } from '@celljs/ai-coding-agent';
12
+ import { Autowired, Value } from '@celljs/core';
13
+ import { Rpc } from '@celljs/rpc';
14
+ import { TaUsageServer } from '@celljs/ta-protocol';
15
+ import { TaSessionRegistry } from '../host-protocol.js';
16
+ import { TaErrors } from '../ta-errors.js';
17
+ import { AgentActivityUtil } from './agent-activity.js';
18
+ import { UsageReader } from './usage-reader.js';
19
+ /** What the agents of this Ta took, from its session files, and what one of them did lately, from its entries. */
20
+ let TaUsageServerImpl = class TaUsageServerImpl {
21
+ constructor() {
22
+ this.reader = new UsageReader(async () => { var _a; var _b; return join((_b = (_a = this.codingAgentOptions) === null || _a === void 0 ? void 0 : _a.agentDir) !== null && _b !== void 0 ? _b : (await PiSdkUtil.load()).getAgentDir(), 'sessions'); });
23
+ }
24
+ getUsage(since) {
25
+ return TaErrors.guard(() => this.reader.read(Number.isFinite(Number(since)) ? Number(since) : 0));
26
+ }
27
+ getActivity(sessionId, limit) {
28
+ return TaErrors.guard(async () => {
29
+ const session = await this.sessions.resolve(String(sessionId));
30
+ return AgentActivityUtil.from(session.pi.sessionManager.getEntries(), AgentActivityUtil.limit(limit));
31
+ });
32
+ }
33
+ };
34
+ __decorate([
35
+ Value(CODING_AGENT_OPTIONS),
36
+ __metadata("design:type", Object)
37
+ ], TaUsageServerImpl.prototype, "codingAgentOptions", void 0);
38
+ __decorate([
39
+ Autowired(TaSessionRegistry),
40
+ __metadata("design:type", Object)
41
+ ], TaUsageServerImpl.prototype, "sessions", void 0);
42
+ TaUsageServerImpl = __decorate([
43
+ Rpc(TaUsageServer)
44
+ ], TaUsageServerImpl);
45
+ export { TaUsageServerImpl };
46
+ //# sourceMappingURL=usage-server-impl.js.map
@@ -1,6 +1,7 @@
1
1
  import type { IncomingMessage, ServerResponse } from 'node:http';
2
2
  import { type ExecEnvironment, ExecEnvironmentFactory, type FileStat } from '@celljs/ta-exec';
3
3
  import { type Context, Middleware } from '@celljs/web/lib/node';
4
+ import { type ThumbnailBox } from './png-thumbnail.js';
4
5
  import { WorkspaceFiles } from './workspace-protocol.js';
5
6
  interface ServedDirectory {
6
7
  root: string;
@@ -24,6 +25,11 @@ export declare namespace WorkspaceFilesUtil {
24
25
  start: number;
25
26
  end: number;
26
27
  } | false | undefined;
28
+ /**
29
+ * The box of `?w=<px>&h=<px>&fit=cover`, which asks for a smaller picture, as the page shows one it has not
30
+ * enlarged (contain without `fit`); undefined for a URL without both sides, or sides out of bounds.
31
+ */
32
+ function thumbnail(url: string | undefined): ThumbnailBox | undefined;
27
33
  function etag(stat: FileStat): string;
28
34
  }
29
35
  /**
@@ -45,9 +51,26 @@ export declare class DefaultWorkspaceFiles implements WorkspaceFiles, Middleware
45
51
  */
46
52
  protected readonly directories: Map<string, ServedDirectory>;
47
53
  protected readonly ids: Map<string, string>;
54
+ /** Smaller pictures made, by file, version and box, the last used last; `null` for one sent as it is. */
55
+ protected readonly thumbnails: Map<string, {
56
+ bytes: Uint8Array;
57
+ type: string;
58
+ } | null>;
59
+ protected thumbnailBytes: number;
60
+ /** Made one at a time: each takes the event loop in turns, and many at once would hold many large pictures. */
61
+ protected making: Promise<unknown>;
48
62
  url(cwd: string, path: string): string;
49
63
  handle(ctx: Context, next: () => Promise<void>): Promise<void>;
50
64
  protected send(request: IncomingMessage, response: ServerResponse, directory: ServedDirectory, path: string): Promise<void>;
65
+ /**
66
+ * A smaller picture of a PNG for `box` (PngThumbnail), with an ETag of its own; false when the picture itself is
67
+ * to be sent (not much larger than the box, or a PNG it does not read).
68
+ */
69
+ protected sendThumbnail(request: IncomingMessage, response: ServerResponse, env: ExecEnvironment, absolute: string, etag: string, box: ThumbnailBox): Promise<boolean>;
70
+ protected keepThumbnail(key: string, made: {
71
+ bytes: Uint8Array;
72
+ type: string;
73
+ } | null): void;
51
74
  /** The file at `path` of `root`, or the `index.html` of the directory there. */
52
75
  protected find(env: ExecEnvironment, root: string, path: string): Promise<{
53
76
  absolute: string;
@@ -14,6 +14,7 @@ import { Autowired, Component, Value } from '@celljs/core';
14
14
  import { ExecEnvironmentFactory } from '@celljs/ta-exec';
15
15
  import { TA_FILES_PATH } from '@celljs/ta-protocol';
16
16
  import { Middleware } from '@celljs/web/lib/node';
17
+ import { PngThumbnail } from './png-thumbnail.js';
17
18
  import { ACCESS_MIDDLEWARE_PRIORITY, TaAccessUtil } from './ta-access.js';
18
19
  import { WorkspaceFiles } from './workspace-protocol.js';
19
20
  import { WorkspaceUtil } from './workspace-util.js';
@@ -65,6 +66,13 @@ const TYPES = {
65
66
  */
66
67
  const SANDBOX = 'sandbox allow-scripts allow-forms allow-popups allow-modals allow-downloads';
67
68
  const ID_BYTES = 16;
69
+ /** The smallest and largest sides of the box a smaller picture is asked for. */
70
+ const THUMB_MIN = 16;
71
+ const THUMB_MAX = 4096;
72
+ /** Smaller pictures kept, made once for every page that shows them, up to this many bytes. */
73
+ const THUMB_CACHE_BYTES = 32 * 1024 * 1024;
74
+ /** Pictures larger than this are sent as they are, never read whole to make a smaller one. */
75
+ const THUMB_SOURCE_BYTES = 48 * 1024 * 1024;
68
76
  /** Read whole from another machine: a part of a larger file is read with `tail` and `head`. */
69
77
  const REMOTE_WHOLE_BYTES = 8 * 1024 * 1024;
70
78
  export var WorkspaceFilesUtil;
@@ -110,6 +118,25 @@ export var WorkspaceFilesUtil;
110
118
  return start <= end && start < size ? { start, end } : false;
111
119
  }
112
120
  WorkspaceFilesUtil.range = range;
121
+ /**
122
+ * The box of `?w=<px>&h=<px>&fit=cover`, which asks for a smaller picture, as the page shows one it has not
123
+ * enlarged (contain without `fit`); undefined for a URL without both sides, or sides out of bounds.
124
+ */
125
+ function thumbnail(url) {
126
+ const query = (url !== null && url !== void 0 ? url : '').split('#')[0].split('?')[1];
127
+ if (!query) {
128
+ return undefined;
129
+ }
130
+ const params = new URLSearchParams(query);
131
+ const side = (name) => {
132
+ const value = Number(params.get(name));
133
+ return Number.isInteger(value) && value >= THUMB_MIN && value <= THUMB_MAX ? value : undefined;
134
+ };
135
+ const width = side('w');
136
+ const height = side('h');
137
+ return width && height ? { width, height, fit: params.get('fit') === 'cover' ? 'cover' : 'contain' } : undefined;
138
+ }
139
+ WorkspaceFilesUtil.thumbnail = thumbnail;
113
140
  function etag(stat) {
114
141
  return `"${stat.size.toString(36)}-${Math.floor(stat.mtimeMs).toString(36)}"`;
115
142
  }
@@ -133,6 +160,11 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
133
160
  */
134
161
  this.directories = new Map();
135
162
  this.ids = new Map();
163
+ /** Smaller pictures made, by file, version and box, the last used last; `null` for one sent as it is. */
164
+ this.thumbnails = new Map();
165
+ this.thumbnailBytes = 0;
166
+ /** Made one at a time: each takes the event loop in turns, and many at once would hold many large pictures. */
167
+ this.making = Promise.resolve();
136
168
  }
137
169
  url(cwd, path) {
138
170
  const absolute = resolve(cwd, path || '.');
@@ -193,6 +225,10 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
193
225
  response.end();
194
226
  return;
195
227
  }
228
+ const box = type === 'image/png' && !request.headers.range && stat.size <= THUMB_SOURCE_BYTES ? WorkspaceFilesUtil.thumbnail(request.url) : undefined;
229
+ if (box && (await this.sendThumbnail(request, response, env, absolute, etag, box))) {
230
+ return;
231
+ }
196
232
  const range = WorkspaceFilesUtil.range(request.headers.range, stat.size);
197
233
  if (range === false) {
198
234
  response.setHeader('Content-Range', `bytes */${stat.size}`);
@@ -216,6 +252,56 @@ let DefaultWorkspaceFiles = class DefaultWorkspaceFiles {
216
252
  response.end(await this.read(env, absolute, stat.size, start, end));
217
253
  }
218
254
  }
255
+ /**
256
+ * A smaller picture of a PNG for `box` (PngThumbnail), with an ETag of its own; false when the picture itself is
257
+ * to be sent (not much larger than the box, or a PNG it does not read).
258
+ */
259
+ async sendThumbnail(request, response, env, absolute, etag, box) {
260
+ var _a;
261
+ const own = `${etag.slice(0, -1)}-${box.width}x${box.height}${box.fit === 'cover' ? 'c' : ''}"`;
262
+ // Made from this version of the file before: what the page has is still right.
263
+ if (request.headers['if-none-match'] === own) {
264
+ response.setHeader('ETag', own);
265
+ response.removeHeader('Accept-Ranges');
266
+ response.statusCode = 304;
267
+ response.end();
268
+ return true;
269
+ }
270
+ const key = `${absolute}\0${own}`;
271
+ let made = this.thumbnails.get(key);
272
+ if (made === undefined) {
273
+ const making = this.making.then(async () => PngThumbnail.make(await env.readFile(absolute), box).catch(() => undefined));
274
+ this.making = making.catch(() => undefined);
275
+ made = (_a = (await making.catch(() => undefined))) !== null && _a !== void 0 ? _a : null;
276
+ this.keepThumbnail(key, made);
277
+ }
278
+ else {
279
+ this.thumbnails.delete(key);
280
+ this.thumbnails.set(key, made);
281
+ }
282
+ if (!made) {
283
+ return false;
284
+ }
285
+ response.setHeader('ETag', own);
286
+ response.setHeader('Content-Type', made.type);
287
+ response.removeHeader('Accept-Ranges');
288
+ response.statusCode = 200;
289
+ response.setHeader('Content-Length', made.bytes.length);
290
+ response.end(request.method === 'HEAD' ? undefined : made.bytes);
291
+ return true;
292
+ }
293
+ keepThumbnail(key, made) {
294
+ var _a, _b;
295
+ this.thumbnails.set(key, made);
296
+ this.thumbnailBytes += (_a = made === null || made === void 0 ? void 0 : made.bytes.length) !== null && _a !== void 0 ? _a : 0;
297
+ for (const [old, bytes] of this.thumbnails) {
298
+ if (this.thumbnailBytes <= THUMB_CACHE_BYTES && this.thumbnails.size <= 4096) {
299
+ break;
300
+ }
301
+ this.thumbnails.delete(old);
302
+ this.thumbnailBytes -= (_b = bytes === null || bytes === void 0 ? void 0 : bytes.bytes.length) !== null && _b !== void 0 ? _b : 0;
303
+ }
304
+ }
219
305
  /** The file at `path` of `root`, or the `index.html` of the directory there. */
220
306
  async find(env, root, path) {
221
307
  let absolute;
@@ -129,8 +129,12 @@ let ExtensionDataStoreImpl = class ExtensionDataStoreImpl {
129
129
  .then(async () => {
130
130
  const text = ExtensionDataUtil.update(await this.read(file), key, value);
131
131
  await (0, promises_1.mkdir)((0, node_path_1.dirname)(file), { recursive: true });
132
- await (0, promises_1.writeFile)(`${file}.tmp`, text, { mode: 0o600 });
133
- await (0, promises_1.rename)(`${file}.tmp`, file);
132
+ // A file of its own: another Ta of this agent directory (one starting as one stops) writing
133
+ // the same name at once would have one rename a file the other is halfway through, and the
134
+ // next read of it, unparsed, would leave every other key out.
135
+ const temporary = `${file}.${(0, node_crypto_1.randomUUID)()}.tmp`;
136
+ await (0, promises_1.writeFile)(temporary, text, { mode: 0o600 });
137
+ await (0, promises_1.rename)(temporary, file);
134
138
  for (const listener of this.listeners) {
135
139
  try {
136
140
  listener(name, key, directory);
@@ -18,6 +18,7 @@ export * from './relay-bridge';
18
18
  export * from './remote';
19
19
  export * from './remote-ui-context';
20
20
  export * from './rooms-bridge';
21
+ export * from './serve-updates';
21
22
  export * from './session-catalog';
22
23
  export * from './session-files';
23
24
  export * from './session-handles';
@@ -40,6 +41,7 @@ export * from './temporary-purge';
40
41
  export * from './terminal-protocol';
41
42
  export * from './terminal-service';
42
43
  export * from './terminal-util';
44
+ export * from './usage';
43
45
  export * from './web-extension-catalog';
44
46
  export * from './web-extension-util';
45
47
  export * from './working-directory';
package/lib/node/index.js CHANGED
@@ -34,6 +34,7 @@ __exportStar(require("./relay-bridge"), exports);
34
34
  __exportStar(require("./remote"), exports);
35
35
  __exportStar(require("./remote-ui-context"), exports);
36
36
  __exportStar(require("./rooms-bridge"), exports);
37
+ __exportStar(require("./serve-updates"), exports);
37
38
  __exportStar(require("./session-catalog"), exports);
38
39
  __exportStar(require("./session-files"), exports);
39
40
  __exportStar(require("./session-handles"), exports);
@@ -56,6 +57,7 @@ __exportStar(require("./temporary-purge"), exports);
56
57
  __exportStar(require("./terminal-protocol"), exports);
57
58
  __exportStar(require("./terminal-service"), exports);
58
59
  __exportStar(require("./terminal-util"), exports);
60
+ __exportStar(require("./usage"), exports);
59
61
  __exportStar(require("./web-extension-catalog"), exports);
60
62
  __exportStar(require("./web-extension-util"), exports);
61
63
  __exportStar(require("./working-directory"), exports);
@@ -0,0 +1,8 @@
1
+ /**
2
+ * A baseline JPEG of opaque RGBA, without a library: YCbCr without subsampling (the letters of a screenshot keep
3
+ * their colour), the tables of the standard scaled to `quality` (1–100, as libjpeg reads it).
4
+ */
5
+ export declare namespace JpegEncoder {
6
+ function encode(rgba: Uint8Array, width: number, height: number, quality?: number): Uint8Array;
7
+ }
8
+ //# sourceMappingURL=jpeg-encoder.d.ts.map