@celljs/ta-host 4.15.1 → 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.
@@ -41,6 +41,7 @@ export * from './temporary-purge.js';
41
41
  export * from './terminal-protocol.js';
42
42
  export * from './terminal-service.js';
43
43
  export * from './terminal-util.js';
44
+ export * from './usage/index.js';
44
45
  export * from './web-extension-catalog.js';
45
46
  export * from './web-extension-util.js';
46
47
  export * from './working-directory.js';
package/esm/node/index.js CHANGED
@@ -41,6 +41,7 @@ export * from './temporary-purge.js';
41
41
  export * from './terminal-protocol.js';
42
42
  export * from './terminal-service.js';
43
43
  export * from './terminal-util.js';
44
+ export * from './usage/index.js';
44
45
  export * from './web-extension-catalog.js';
45
46
  export * from './web-extension-util.js';
46
47
  export * from './working-directory.js';
@@ -245,6 +245,11 @@ const RULES = {
245
245
  disallow: fixed('members.roles'),
246
246
  signOut: fixed('members.roles'),
247
247
  },
248
+ // What the agents took and did: for the owner (and the gateway, which calls as the owner).
249
+ TaUsageServer: {
250
+ getUsage: owner,
251
+ getActivity: owner,
252
+ },
248
253
  TaRemoteServer: {
249
254
  listHosts: fixed('hosts.use'),
250
255
  addHost: fixed('settings.manage'),
@@ -0,0 +1,13 @@
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
+ }
@@ -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
@@ -41,6 +41,7 @@ export * from './temporary-purge';
41
41
  export * from './terminal-protocol';
42
42
  export * from './terminal-service';
43
43
  export * from './terminal-util';
44
+ export * from './usage';
44
45
  export * from './web-extension-catalog';
45
46
  export * from './web-extension-util';
46
47
  export * from './working-directory';
package/lib/node/index.js CHANGED
@@ -57,6 +57,7 @@ __exportStar(require("./temporary-purge"), exports);
57
57
  __exportStar(require("./terminal-protocol"), exports);
58
58
  __exportStar(require("./terminal-service"), exports);
59
59
  __exportStar(require("./terminal-util"), exports);
60
+ __exportStar(require("./usage"), exports);
60
61
  __exportStar(require("./web-extension-catalog"), exports);
61
62
  __exportStar(require("./web-extension-util"), exports);
62
63
  __exportStar(require("./working-directory"), exports);
@@ -249,6 +249,11 @@ const RULES = {
249
249
  disallow: fixed('members.roles'),
250
250
  signOut: fixed('members.roles'),
251
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
+ },
252
257
  TaRemoteServer: {
253
258
  listHosts: fixed('hosts.use'),
254
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
@@ -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,4 @@
1
+ export * from './agent-activity';
2
+ export * from './usage-reader';
3
+ export * from './usage-server-impl';
4
+ //# sourceMappingURL=index.d.ts.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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-host",
3
- "version": "4.15.1",
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.1",
38
- "@celljs/ai-coding-agent": "4.15.1",
39
- "@celljs/core": "4.15.1",
40
- "@celljs/rpc": "4.15.1",
41
- "@celljs/ta-exec": "4.15.1",
42
- "@celljs/ta-harness": "4.15.1",
43
- "@celljs/ta-protocol": "4.15.1",
44
- "@celljs/ta-relay": "4.15.1",
45
- "@celljs/web": "4.15.1",
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"