@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
@@ -245,10 +245,16 @@ 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'),
251
256
  removeHost: fixed('settings.manage'),
257
+ addCloudHost: fixed('settings.manage'),
252
258
  resolveLocation: fixed('hosts.use'),
253
259
  forwardPort: fixed('hosts.use'),
254
260
  listForwardedPorts: fixed('hosts.use'),
@@ -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
@@ -0,0 +1,15 @@
1
+ import type { Server } from 'node:http';
2
+ import { ExecServer } from '@celljs/ta-exec';
3
+ import { ServerAware } from '@celljs/web/lib/node';
4
+ import { type TaAccess } from './ta-access';
5
+ /**
6
+ * `/api/exec` of ta serve: its machine as the exec server of another Ta (a cloud workspace, `ta://<name>`), whose
7
+ * agent stays there while commands and files are here. Its owner's token only: what it opens is a shell. A gateway
8
+ * forwards it to the sandbox of the account that asks (GatewayExecProxy).
9
+ */
10
+ export declare class TaExecEndpoint implements ServerAware {
11
+ protected readonly access: TaAccess;
12
+ protected exec?: ExecServer;
13
+ setServer(server: Server): Promise<void>;
14
+ }
15
+ //# sourceMappingURL=exec-endpoint.d.ts.map
@@ -0,0 +1,51 @@
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.TaExecEndpoint = void 0;
13
+ const core_1 = require("@celljs/core");
14
+ const ta_exec_1 = require("@celljs/ta-exec");
15
+ const ta_protocol_1 = require("@celljs/ta-protocol");
16
+ const node_1 = require("@celljs/web/lib/node");
17
+ const cloud_hosts_1 = require("./remote/cloud-hosts");
18
+ const ta_access_1 = require("./ta-access");
19
+ /**
20
+ * `/api/exec` of ta serve: its machine as the exec server of another Ta (a cloud workspace, `ta://<name>`), whose
21
+ * agent stays there while commands and files are here. Its owner's token only: what it opens is a shell. A gateway
22
+ * forwards it to the sandbox of the account that asks (GatewayExecProxy).
23
+ */
24
+ let TaExecEndpoint = class TaExecEndpoint {
25
+ async setServer(server) {
26
+ if (typeof (server === null || server === void 0 ? void 0 : server.on) !== 'function') {
27
+ return;
28
+ }
29
+ server.on('upgrade', (request, socket, head) => {
30
+ var _a, _b;
31
+ const path = ((_a = request.url) !== null && _a !== void 0 ? _a : '/').split('?')[0];
32
+ if (path === cloud_hosts_1.TA_EXEC_PATH) {
33
+ (_b = this.exec) !== null && _b !== void 0 ? _b : (this.exec = new ta_exec_1.ExecServer({ token: this.access.token, cwd: process.cwd() }));
34
+ this.exec.accept(request, socket, head);
35
+ }
36
+ else if (path !== ta_protocol_1.TA_WEBSOCKET_PATH && server.listenerCount('upgrade') <= 2) {
37
+ // Nobody else answers it: the RPC socket answers only its own path while another listens too.
38
+ socket.destroy();
39
+ }
40
+ });
41
+ }
42
+ };
43
+ exports.TaExecEndpoint = TaExecEndpoint;
44
+ __decorate([
45
+ (0, core_1.Autowired)(ta_access_1.TA_ACCESS),
46
+ __metadata("design:type", Function)
47
+ ], TaExecEndpoint.prototype, "access", void 0);
48
+ exports.TaExecEndpoint = TaExecEndpoint = __decorate([
49
+ (0, core_1.Component)(node_1.ServerAware)
50
+ ], TaExecEndpoint);
51
+ //# sourceMappingURL=exec-endpoint.js.map
@@ -0,0 +1,39 @@
1
+ import { type CodingAgentOptions } from '@celljs/ai-coding-agent';
2
+ import { type Application, ApplicationLifecycle } from '@celljs/core';
3
+ interface Catalog {
4
+ providers?: {
5
+ id: string;
6
+ name?: string;
7
+ api: string;
8
+ models: Record<string, unknown>[];
9
+ }[];
10
+ }
11
+ type Json = Record<string, unknown>;
12
+ export declare namespace GatewayModelsUtil {
13
+ const URL_ENV = "TA_MODEL_PROXY_URL";
14
+ const KEY_ENV = "TA_MODEL_PROXY_KEY";
15
+ /**
16
+ * models.json with the providers of the gateway, each at the gateway (`<url>/api/models/<id>`) with the model key of
17
+ * this sandbox: those it gave before and no longer does go; those of the user stay.
18
+ */
19
+ function apply(models: Json, catalog: Catalog, url: string, key: string, before: string[]): {
20
+ models: Json;
21
+ managed: string[];
22
+ };
23
+ /** settings.json with the first model of the gateway as the default, when it has none. */
24
+ function defaults(settings: Json, catalog: Catalog): Json | undefined;
25
+ }
26
+ /**
27
+ * The models of a gateway, for a Ta in its sandbox (docs/design/ta-cloud.md 3.4): with TA_MODEL_PROXY_URL and
28
+ * TA_MODEL_PROXY_KEY, ta serve reads the catalog of the gateway as it starts and puts its providers in pi's
29
+ * models.json, at the gateway with the model key of this sandbox. The keys of the providers stay with the gateway,
30
+ * which counts what each call takes. The two variables leave the environment, so that no command sees them.
31
+ */
32
+ export declare class GatewayModels implements ApplicationLifecycle<Application> {
33
+ protected readonly codingAgentOptions?: CodingAgentOptions;
34
+ onStart(): Promise<void>;
35
+ protected read(file: string): Json;
36
+ protected write(file: string, data: Json): void;
37
+ }
38
+ export {};
39
+ //# sourceMappingURL=gateway-models.d.ts.map
@@ -0,0 +1,127 @@
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.GatewayModels = exports.GatewayModelsUtil = void 0;
13
+ const node_fs_1 = require("node:fs");
14
+ const node_path_1 = require("node:path");
15
+ const ai_coding_agent_1 = require("@celljs/ai-coding-agent");
16
+ const core_1 = require("@celljs/core");
17
+ /** How long ta serve waits for the catalog of the gateway as it starts. */
18
+ const CATALOG_TIMEOUT_MS = 5000;
19
+ var GatewayModelsUtil;
20
+ (function (GatewayModelsUtil) {
21
+ GatewayModelsUtil.URL_ENV = 'TA_MODEL_PROXY_URL';
22
+ GatewayModelsUtil.KEY_ENV = 'TA_MODEL_PROXY_KEY';
23
+ /**
24
+ * models.json with the providers of the gateway, each at the gateway (`<url>/api/models/<id>`) with the model key of
25
+ * this sandbox: those it gave before and no longer does go; those of the user stay.
26
+ */
27
+ function apply(models, catalog, url, key, before) {
28
+ var _a, _b;
29
+ const providers = { ...((_a = models.providers) !== null && _a !== void 0 ? _a : {}) };
30
+ for (const id of before) {
31
+ delete providers[id];
32
+ }
33
+ const managed = [];
34
+ for (const provider of (_b = catalog.providers) !== null && _b !== void 0 ? _b : []) {
35
+ if (typeof (provider === null || provider === void 0 ? void 0 : provider.id) !== 'string' || !/^[a-z0-9][a-z0-9._-]{0,63}$/i.test(provider.id) || !Array.isArray(provider.models)) {
36
+ continue;
37
+ }
38
+ providers[provider.id] = {
39
+ ...(provider.name ? { name: provider.name } : {}),
40
+ baseUrl: `${url}/api/models/${provider.id}`,
41
+ api: provider.api,
42
+ apiKey: key,
43
+ models: provider.models,
44
+ };
45
+ managed.push(provider.id);
46
+ }
47
+ return { models: { ...models, providers }, managed };
48
+ }
49
+ GatewayModelsUtil.apply = apply;
50
+ /** settings.json with the first model of the gateway as the default, when it has none. */
51
+ function defaults(settings, catalog) {
52
+ var _a, _b;
53
+ const first = (_a = catalog.providers) === null || _a === void 0 ? void 0 : _a.find(provider => { var _a; return (_a = provider.models) === null || _a === void 0 ? void 0 : _a.length; });
54
+ const model = (_b = first === null || first === void 0 ? void 0 : first.models[0]) === null || _b === void 0 ? void 0 : _b.id;
55
+ if (settings.defaultProvider || !first || typeof model !== 'string') {
56
+ return undefined;
57
+ }
58
+ return { ...settings, defaultProvider: first.id, defaultModel: model };
59
+ }
60
+ GatewayModelsUtil.defaults = defaults;
61
+ })(GatewayModelsUtil || (exports.GatewayModelsUtil = GatewayModelsUtil = {}));
62
+ /**
63
+ * The models of a gateway, for a Ta in its sandbox (docs/design/ta-cloud.md 3.4): with TA_MODEL_PROXY_URL and
64
+ * TA_MODEL_PROXY_KEY, ta serve reads the catalog of the gateway as it starts and puts its providers in pi's
65
+ * models.json, at the gateway with the model key of this sandbox. The keys of the providers stay with the gateway,
66
+ * which counts what each call takes. The two variables leave the environment, so that no command sees them.
67
+ */
68
+ let GatewayModels = class GatewayModels {
69
+ async onStart() {
70
+ var _a, _b;
71
+ var _c, _d;
72
+ const url = (_a = process.env[GatewayModelsUtil.URL_ENV]) === null || _a === void 0 ? void 0 : _a.replace(/\/+$/, '');
73
+ const key = process.env[GatewayModelsUtil.KEY_ENV];
74
+ delete process.env[GatewayModelsUtil.URL_ENV];
75
+ delete process.env[GatewayModelsUtil.KEY_ENV];
76
+ if (!url || !key) {
77
+ return;
78
+ }
79
+ try {
80
+ const answer = await fetch(`${url}/api/models`, { headers: { authorization: `Bearer ${key}` }, signal: AbortSignal.timeout(CATALOG_TIMEOUT_MS) });
81
+ if (!answer.ok) {
82
+ throw new Error(`it answered ${answer.status}`);
83
+ }
84
+ const catalog = (await answer.json());
85
+ const agentDir = (_c = (_b = this.codingAgentOptions) === null || _b === void 0 ? void 0 : _b.agentDir) !== null && _c !== void 0 ? _c : (await ai_coding_agent_1.PiSdkUtil.load()).getAgentDir();
86
+ const managedFile = (0, node_path_1.join)(agentDir, 'gateway-models.json');
87
+ const before = (_d = this.read(managedFile).providers) !== null && _d !== void 0 ? _d : [];
88
+ const modelsFile = (0, node_path_1.join)(agentDir, 'models.json');
89
+ const { models, managed } = GatewayModelsUtil.apply(this.read(modelsFile), catalog, url, key, before);
90
+ this.write(modelsFile, models);
91
+ this.write(managedFile, { providers: managed });
92
+ const settingsFile = (0, node_path_1.join)(agentDir, 'settings.json');
93
+ const settings = GatewayModelsUtil.defaults(this.read(settingsFile), catalog);
94
+ if (settings) {
95
+ this.write(settingsFile, settings);
96
+ }
97
+ console.log(`ta serve: models of the gateway: ${managed.join(', ') || 'none'}`);
98
+ }
99
+ catch (error) {
100
+ console.warn(`ta serve: the models of the gateway did not load: ${error.message}`);
101
+ }
102
+ }
103
+ read(file) {
104
+ try {
105
+ const value = JSON.parse((0, node_fs_1.readFileSync)(file, 'utf8'));
106
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
107
+ }
108
+ catch (_a) {
109
+ return {};
110
+ }
111
+ }
112
+ write(file, data) {
113
+ (0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(file), { recursive: true });
114
+ const temporary = `${file}.${process.pid}.tmp`;
115
+ (0, node_fs_1.writeFileSync)(temporary, `${JSON.stringify(data, undefined, 2)}\n`, { mode: 0o600 });
116
+ (0, node_fs_1.renameSync)(temporary, file);
117
+ }
118
+ };
119
+ exports.GatewayModels = GatewayModels;
120
+ __decorate([
121
+ (0, core_1.Value)(ai_coding_agent_1.CODING_AGENT_OPTIONS),
122
+ __metadata("design:type", Object)
123
+ ], GatewayModels.prototype, "codingAgentOptions", void 0);
124
+ exports.GatewayModels = GatewayModels = __decorate([
125
+ (0, core_1.Component)(core_1.ApplicationLifecycle)
126
+ ], GatewayModels);
127
+ //# sourceMappingURL=gateway-models.js.map
@@ -2,10 +2,12 @@ export * from './auth-server-impl';
2
2
  export * from './client-channel';
3
3
  export * from './client-tool-broker';
4
4
  export * from './event-log';
5
+ export * from './exec-endpoint';
5
6
  export * from './exec-environment-factory';
6
7
  export * from './extension-data';
7
8
  export * from './extension-data-notifier';
8
9
  export * from './extension-providers';
10
+ export * from './gateway-models';
9
11
  export * from './host-protocol';
10
12
  export * from './keep-awake';
11
13
  export * from './package-host-extension';
@@ -41,6 +43,7 @@ export * from './temporary-purge';
41
43
  export * from './terminal-protocol';
42
44
  export * from './terminal-service';
43
45
  export * from './terminal-util';
46
+ export * from './usage';
44
47
  export * from './web-extension-catalog';
45
48
  export * from './web-extension-util';
46
49
  export * from './working-directory';
package/lib/node/index.js CHANGED
@@ -18,10 +18,12 @@ __exportStar(require("./auth-server-impl"), exports);
18
18
  __exportStar(require("./client-channel"), exports);
19
19
  __exportStar(require("./client-tool-broker"), exports);
20
20
  __exportStar(require("./event-log"), exports);
21
+ __exportStar(require("./exec-endpoint"), exports);
21
22
  __exportStar(require("./exec-environment-factory"), exports);
22
23
  __exportStar(require("./extension-data"), exports);
23
24
  __exportStar(require("./extension-data-notifier"), exports);
24
25
  __exportStar(require("./extension-providers"), exports);
26
+ __exportStar(require("./gateway-models"), exports);
25
27
  __exportStar(require("./host-protocol"), exports);
26
28
  __exportStar(require("./keep-awake"), exports);
27
29
  __exportStar(require("./package-host-extension"), exports);
@@ -57,6 +59,7 @@ __exportStar(require("./temporary-purge"), exports);
57
59
  __exportStar(require("./terminal-protocol"), exports);
58
60
  __exportStar(require("./terminal-service"), exports);
59
61
  __exportStar(require("./terminal-util"), exports);
62
+ __exportStar(require("./usage"), exports);
60
63
  __exportStar(require("./web-extension-catalog"), exports);
61
64
  __exportStar(require("./web-extension-util"), exports);
62
65
  __exportStar(require("./working-directory"), exports);