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