@celljs/ta-host 4.15.1 → 4.16.0

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 (53) hide show
  1. package/esm/node/exec-endpoint.d.ts +14 -0
  2. package/esm/node/exec-endpoint.js +48 -0
  3. package/esm/node/gateway-models.d.ts +38 -0
  4. package/esm/node/gateway-models.js +124 -0
  5. package/esm/node/index.d.ts +3 -0
  6. package/esm/node/index.js +3 -0
  7. package/esm/node/remote/cloud-hosts.d.ts +40 -0
  8. package/esm/node/remote/cloud-hosts.js +99 -0
  9. package/esm/node/remote/index.d.ts +1 -0
  10. package/esm/node/remote/index.js +1 -0
  11. package/esm/node/remote/pi-directories.d.ts +2 -1
  12. package/esm/node/remote/pi-directories.js +7 -2
  13. package/esm/node/remote/remote-hosts.d.ts +5 -0
  14. package/esm/node/remote/remote-hosts.js +71 -2
  15. package/esm/node/remote/remote-protocol.d.ts +2 -0
  16. package/esm/node/remote/remote-server-impl.d.ts +1 -0
  17. package/esm/node/remote/remote-server-impl.js +3 -0
  18. package/esm/node/team/team-calls.js +6 -0
  19. package/esm/node/usage/agent-activity.d.ts +13 -0
  20. package/esm/node/usage/agent-activity.js +84 -0
  21. package/esm/node/usage/index.d.ts +3 -0
  22. package/esm/node/usage/index.js +4 -0
  23. package/esm/node/usage/usage-reader.d.ts +28 -0
  24. package/esm/node/usage/usage-reader.js +159 -0
  25. package/esm/node/usage/usage-server-impl.d.ts +12 -0
  26. package/esm/node/usage/usage-server-impl.js +46 -0
  27. package/lib/node/exec-endpoint.d.ts +15 -0
  28. package/lib/node/exec-endpoint.js +51 -0
  29. package/lib/node/gateway-models.d.ts +39 -0
  30. package/lib/node/gateway-models.js +127 -0
  31. package/lib/node/index.d.ts +3 -0
  32. package/lib/node/index.js +3 -0
  33. package/lib/node/remote/cloud-hosts.d.ts +41 -0
  34. package/lib/node/remote/cloud-hosts.js +103 -0
  35. package/lib/node/remote/index.d.ts +1 -0
  36. package/lib/node/remote/index.js +1 -0
  37. package/lib/node/remote/pi-directories.d.ts +2 -1
  38. package/lib/node/remote/pi-directories.js +7 -2
  39. package/lib/node/remote/remote-hosts.d.ts +5 -0
  40. package/lib/node/remote/remote-hosts.js +71 -2
  41. package/lib/node/remote/remote-protocol.d.ts +2 -0
  42. package/lib/node/remote/remote-server-impl.d.ts +1 -0
  43. package/lib/node/remote/remote-server-impl.js +3 -0
  44. package/lib/node/team/team-calls.js +6 -0
  45. package/lib/node/usage/agent-activity.d.ts +14 -0
  46. package/lib/node/usage/agent-activity.js +87 -0
  47. package/lib/node/usage/index.d.ts +4 -0
  48. package/lib/node/usage/index.js +20 -0
  49. package/lib/node/usage/usage-reader.d.ts +29 -0
  50. package/lib/node/usage/usage-reader.js +163 -0
  51. package/lib/node/usage/usage-server-impl.d.ts +13 -0
  52. package/lib/node/usage/usage-server-impl.js +49 -0
  53. package/package.json +14 -10
@@ -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.16.0",
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.16.0",
38
+ "@celljs/ai-coding-agent": "4.16.0",
39
+ "@celljs/core": "4.16.0",
40
+ "@celljs/rpc": "4.16.0",
41
+ "@celljs/ta-exec": "4.16.0",
42
+ "@celljs/ta-harness": "4.16.0",
43
+ "@celljs/ta-protocol": "4.16.0",
44
+ "@celljs/ta-relay": "4.16.0",
45
+ "@celljs/web": "4.16.0",
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"