@celljs/ta-host 4.15.2 → 4.17.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.
- package/esm/node/app-commands.d.ts +111 -0
- package/esm/node/app-commands.js +629 -0
- package/esm/node/delegate-extension.d.ts +11 -0
- package/esm/node/delegate-extension.js +33 -0
- package/esm/node/directory-relocator.d.ts +49 -0
- package/esm/node/directory-relocator.js +267 -0
- package/esm/node/exec-endpoint.d.ts +14 -0
- package/esm/node/exec-endpoint.js +48 -0
- package/esm/node/extension-providers.d.ts +16 -1
- package/esm/node/extension-providers.js +23 -3
- package/esm/node/gateway-models.d.ts +38 -0
- package/esm/node/gateway-models.js +124 -0
- package/esm/node/index.d.ts +8 -0
- package/esm/node/index.js +8 -0
- package/esm/node/keep-awake.d.ts +5 -1
- package/esm/node/keep-awake.js +18 -2
- package/esm/node/message-page.d.ts +6 -1
- package/esm/node/message-page.js +50 -2
- package/esm/node/package-hosts.d.ts +6 -0
- package/esm/node/package-hosts.js +35 -0
- package/esm/node/package-webhooks.d.ts +26 -0
- package/esm/node/package-webhooks.js +101 -0
- package/esm/node/page-book.d.ts +48 -0
- package/esm/node/page-book.js +117 -0
- package/esm/node/relay-bridge.d.ts +2 -0
- package/esm/node/relay-bridge.js +35 -1
- package/esm/node/remote/cloud-hosts.d.ts +40 -0
- package/esm/node/remote/cloud-hosts.js +99 -0
- package/esm/node/remote/index.d.ts +1 -0
- package/esm/node/remote/index.js +1 -0
- package/esm/node/remote/pi-directories.d.ts +2 -1
- package/esm/node/remote/pi-directories.js +7 -2
- package/esm/node/remote/remote-hosts.d.ts +5 -0
- package/esm/node/remote/remote-hosts.js +71 -2
- package/esm/node/remote/remote-protocol.d.ts +2 -0
- package/esm/node/remote/remote-server-impl.d.ts +1 -0
- package/esm/node/remote/remote-server-impl.js +3 -0
- package/esm/node/rooms-bridge.d.ts +3 -0
- package/esm/node/rooms-bridge.js +4 -0
- package/esm/node/session-info-util.d.ts +3 -1
- package/esm/node/session-info-util.js +32 -6
- package/esm/node/ta-server-impl.d.ts +5 -1
- package/esm/node/ta-server-impl.js +22 -0
- package/esm/node/ta-session-factory.d.ts +13 -1
- package/esm/node/ta-session-factory.js +39 -4
- package/esm/node/ta-session.d.ts +2 -0
- package/esm/node/ta-session.js +1 -0
- package/esm/node/team/team-calls.js +23 -0
- package/esm/node/voice-bridge.d.ts +68 -0
- package/esm/node/voice-bridge.js +273 -0
- package/esm/node/voices-file.d.ts +33 -0
- package/esm/node/voices-file.js +89 -0
- package/lib/node/app-commands.d.ts +112 -0
- package/lib/node/app-commands.js +633 -0
- package/lib/node/delegate-extension.d.ts +12 -0
- package/lib/node/delegate-extension.js +37 -0
- package/lib/node/directory-relocator.d.ts +50 -0
- package/lib/node/directory-relocator.js +270 -0
- package/lib/node/exec-endpoint.d.ts +15 -0
- package/lib/node/exec-endpoint.js +51 -0
- package/lib/node/extension-providers.d.ts +16 -1
- package/lib/node/extension-providers.js +23 -3
- package/lib/node/gateway-models.d.ts +39 -0
- package/lib/node/gateway-models.js +127 -0
- package/lib/node/index.d.ts +8 -0
- package/lib/node/index.js +8 -0
- package/lib/node/keep-awake.d.ts +5 -1
- package/lib/node/keep-awake.js +18 -2
- package/lib/node/message-page.d.ts +6 -1
- package/lib/node/message-page.js +50 -2
- package/lib/node/package-hosts.d.ts +6 -0
- package/lib/node/package-hosts.js +35 -0
- package/lib/node/package-webhooks.d.ts +27 -0
- package/lib/node/package-webhooks.js +104 -0
- package/lib/node/page-book.d.ts +49 -0
- package/lib/node/page-book.js +120 -0
- package/lib/node/relay-bridge.d.ts +2 -0
- package/lib/node/relay-bridge.js +34 -0
- package/lib/node/remote/cloud-hosts.d.ts +41 -0
- package/lib/node/remote/cloud-hosts.js +103 -0
- package/lib/node/remote/index.d.ts +1 -0
- package/lib/node/remote/index.js +1 -0
- package/lib/node/remote/pi-directories.d.ts +2 -1
- package/lib/node/remote/pi-directories.js +7 -2
- package/lib/node/remote/remote-hosts.d.ts +5 -0
- package/lib/node/remote/remote-hosts.js +71 -2
- package/lib/node/remote/remote-protocol.d.ts +2 -0
- package/lib/node/remote/remote-server-impl.d.ts +1 -0
- package/lib/node/remote/remote-server-impl.js +3 -0
- package/lib/node/rooms-bridge.d.ts +3 -0
- package/lib/node/rooms-bridge.js +4 -0
- package/lib/node/session-info-util.d.ts +3 -1
- package/lib/node/session-info-util.js +31 -5
- package/lib/node/ta-server-impl.d.ts +5 -1
- package/lib/node/ta-server-impl.js +22 -0
- package/lib/node/ta-session-factory.d.ts +13 -1
- package/lib/node/ta-session-factory.js +38 -3
- package/lib/node/ta-session.d.ts +2 -0
- package/lib/node/ta-session.js +1 -0
- package/lib/node/team/team-calls.js +23 -0
- package/lib/node/voice-bridge.d.ts +69 -0
- package/lib/node/voice-bridge.js +274 -0
- package/lib/node/voices-file.d.ts +34 -0
- package/lib/node/voices-file.js +93 -0
- package/package.json +10 -10
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { TaDelegateUtil } from '@celljs/ta-protocol';
|
|
2
|
+
/**
|
|
3
|
+
* Holds the double to how much the user wants it to hand out (Settings.delegate): in a session whose role has the
|
|
4
|
+
* mark of a level (TaDelegateUtil.mark), its calls of the tools that change files are turned down, and those of its
|
|
5
|
+
* own tools past the steps of a run, each with what to do instead. Other sessions (tasks, chats, agents) have no
|
|
6
|
+
* mark and are left alone.
|
|
7
|
+
*/
|
|
8
|
+
export class DelegateExtension {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.name = 'ta-delegate';
|
|
11
|
+
}
|
|
12
|
+
register(pi) {
|
|
13
|
+
let steps = 0;
|
|
14
|
+
pi.on('agent_start', () => {
|
|
15
|
+
steps = 0;
|
|
16
|
+
});
|
|
17
|
+
pi.on('tool_call', (event, ctx) => {
|
|
18
|
+
const level = TaDelegateUtil.levelOf(ctx.getSystemPrompt());
|
|
19
|
+
if (!level) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
const reason = TaDelegateUtil.refusal(level, event.toolName, steps);
|
|
23
|
+
if (reason) {
|
|
24
|
+
return { block: true, reason };
|
|
25
|
+
}
|
|
26
|
+
if (TaDelegateUtil.WORK_TOOLS.includes(event.toolName)) {
|
|
27
|
+
steps++;
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=delegate-extension.js.map
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { type CodingAgentOptions } from '@celljs/ai-coding-agent';
|
|
2
|
+
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
3
|
+
import { TaPathMap, type TaRelocateResult } from '@celljs/ta-protocol';
|
|
4
|
+
import { ExtensionDataStore } from './extension-data.js';
|
|
5
|
+
import { type TaExecOptions, TaSessionRegistry } from './host-protocol.js';
|
|
6
|
+
import { PiDirectories } from './remote/remote-protocol.js';
|
|
7
|
+
import { TaSessionHandles } from './session-handles.js';
|
|
8
|
+
import { TaWorkingDirectory } from './working-directory.js';
|
|
9
|
+
export declare const TaDirectoryRelocator: unique symbol;
|
|
10
|
+
/** TaServer.relocateDirectory: what of Ta names a directory, made another. */
|
|
11
|
+
export interface TaDirectoryRelocator {
|
|
12
|
+
relocate(from: string, to: string): Promise<TaRelocateResult>;
|
|
13
|
+
}
|
|
14
|
+
export declare namespace DirectoryRelocatorUtil {
|
|
15
|
+
/** pi's folder of the sessions of a directory (SessionManager's getDefaultSessionDirPath), its name. */
|
|
16
|
+
function sessionDir(cwd: string): string;
|
|
17
|
+
/** The directory of a session file, from its header; undefined for what is not a session of pi. */
|
|
18
|
+
function headerCwd(file: string): Promise<string | undefined>;
|
|
19
|
+
/** The file with its header in \`cwd\`; the rest as it was. */
|
|
20
|
+
function withCwd(text: string, cwd: string): string;
|
|
21
|
+
}
|
|
22
|
+
export declare class TaDirectoryRelocatorImpl implements TaDirectoryRelocator {
|
|
23
|
+
protected readonly codingAgentOptions?: CodingAgentOptions;
|
|
24
|
+
protected readonly execOptions?: TaExecOptions;
|
|
25
|
+
protected readonly sessions: TaSessionRegistry;
|
|
26
|
+
protected readonly handles: TaSessionHandles;
|
|
27
|
+
protected readonly data: ExtensionDataStore;
|
|
28
|
+
protected readonly directories: PiDirectories;
|
|
29
|
+
protected readonly workingDirectory: TaWorkingDirectory;
|
|
30
|
+
protected readonly environments: ExecEnvironmentFactory;
|
|
31
|
+
/** One at a time: two would move the same files. */
|
|
32
|
+
protected running: Promise<void>;
|
|
33
|
+
relocate(from: string, to: string): Promise<TaRelocateResult>;
|
|
34
|
+
protected move(from: string, to: string): Promise<TaRelocateResult>;
|
|
35
|
+
/** A session file into the folder of its new directory, its header saying so; one open idle closes first and opens again there under its id. */
|
|
36
|
+
protected moveSession(file: string, next: string, cwd: string, result: TaRelocateResult): Promise<boolean>;
|
|
37
|
+
/**
|
|
38
|
+
* The data of packages with the paths mapped, key by key through the store (pages hear of each): the files of
|
|
39
|
+
* every package, and those of the directories moved, into the data of their new directory.
|
|
40
|
+
*/
|
|
41
|
+
protected moveData(pairs: TaPathMap.Pair[], cwds: Map<string, string>): Promise<number>;
|
|
42
|
+
/** \`all\`: every key goes, into the data of the directory \`cwd\`; otherwise only what changed, in place. */
|
|
43
|
+
protected rewrite(name: string, values: Record<string, unknown>, pairs: TaPathMap.Pair[], cwd?: string, all?: boolean): Promise<boolean>;
|
|
44
|
+
/** The packages with a file of data in \`root\`, by name (\`@scope/name\` in a folder of its scope); not the data of directories. */
|
|
45
|
+
protected packageFiles(root: string, directory: boolean): Promise<string[]>;
|
|
46
|
+
protected sessionFiles(root: string): Promise<string[]>;
|
|
47
|
+
protected write(file: string, text: string): Promise<void>;
|
|
48
|
+
protected agentDir(): string;
|
|
49
|
+
}
|
|
@@ -0,0 +1,267 @@
|
|
|
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 { mkdir, open, readdir, readFile, rename, rm, rmdir, stat, writeFile } from 'node:fs/promises';
|
|
11
|
+
import { basename, dirname, join } from 'node:path';
|
|
12
|
+
import { CODING_AGENT_OPTIONS } from '@celljs/ai-coding-agent';
|
|
13
|
+
import { Autowired, Component, Value } from '@celljs/core';
|
|
14
|
+
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
15
|
+
import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
|
|
16
|
+
import { TaLocation, TaPathMap } from '@celljs/ta-protocol';
|
|
17
|
+
import { ExtensionDataStore, ExtensionDataUtil } from './extension-data.js';
|
|
18
|
+
import { TA_EXEC_OPTIONS, TaSessionRegistry } from './host-protocol.js';
|
|
19
|
+
import { PiDirectories } from './remote/remote-protocol.js';
|
|
20
|
+
import { TaSessionHandles } from './session-handles.js';
|
|
21
|
+
import { TaErrors } from './ta-errors.js';
|
|
22
|
+
import { TaWorkingDirectory } from './working-directory.js';
|
|
23
|
+
export const TaDirectoryRelocator = Symbol('TaDirectoryRelocator');
|
|
24
|
+
/** The first line of a session file of pi is its header; it is read without the rest. */
|
|
25
|
+
const HEADER_BYTES = 64 * 1024;
|
|
26
|
+
export var DirectoryRelocatorUtil;
|
|
27
|
+
(function (DirectoryRelocatorUtil) {
|
|
28
|
+
/** pi's folder of the sessions of a directory (SessionManager's getDefaultSessionDirPath), its name. */
|
|
29
|
+
function sessionDir(cwd) {
|
|
30
|
+
return `--${cwd.replace(/^[/\\]/, '').replace(/[/\\:]/g, '-')}--`;
|
|
31
|
+
}
|
|
32
|
+
DirectoryRelocatorUtil.sessionDir = sessionDir;
|
|
33
|
+
/** The directory of a session file, from its header; undefined for what is not a session of pi. */
|
|
34
|
+
async function headerCwd(file) {
|
|
35
|
+
const handle = await open(file, 'r').catch(() => undefined);
|
|
36
|
+
if (!handle) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const buffer = Buffer.alloc(HEADER_BYTES);
|
|
41
|
+
const { bytesRead } = await handle.read(buffer, 0, HEADER_BYTES, 0);
|
|
42
|
+
const text = buffer.subarray(0, bytesRead).toString('utf8');
|
|
43
|
+
const end = text.indexOf('\n');
|
|
44
|
+
const header = JSON.parse(end < 0 ? text : text.slice(0, end));
|
|
45
|
+
return header.type === 'session' && typeof header.cwd === 'string' ? header.cwd : undefined;
|
|
46
|
+
}
|
|
47
|
+
catch (_a) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
await handle.close();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
DirectoryRelocatorUtil.headerCwd = headerCwd;
|
|
55
|
+
/** The file with its header in \`cwd\`; the rest as it was. */
|
|
56
|
+
function withCwd(text, cwd) {
|
|
57
|
+
const end = text.indexOf('\n');
|
|
58
|
+
const header = JSON.parse(end < 0 ? text : text.slice(0, end));
|
|
59
|
+
return JSON.stringify({ ...header, cwd }) + (end < 0 ? '' : text.slice(end));
|
|
60
|
+
}
|
|
61
|
+
DirectoryRelocatorUtil.withCwd = withCwd;
|
|
62
|
+
})(DirectoryRelocatorUtil || (DirectoryRelocatorUtil = {}));
|
|
63
|
+
let TaDirectoryRelocatorImpl = class TaDirectoryRelocatorImpl {
|
|
64
|
+
constructor() {
|
|
65
|
+
/** One at a time: two would move the same files. */
|
|
66
|
+
this.running = Promise.resolve();
|
|
67
|
+
}
|
|
68
|
+
relocate(from, to) {
|
|
69
|
+
const next = this.running.catch(() => undefined).then(() => this.move(from, to));
|
|
70
|
+
this.running = next.then(() => undefined, () => undefined);
|
|
71
|
+
return next;
|
|
72
|
+
}
|
|
73
|
+
async move(from, to) {
|
|
74
|
+
var _a;
|
|
75
|
+
const old = typeof from === 'string' ? from.trim() : '';
|
|
76
|
+
const target = typeof to === 'string' && to.trim() ? await this.environments.resolve(to.trim()) : '';
|
|
77
|
+
const env = target ? this.environments.create(target) : undefined;
|
|
78
|
+
if (!old || !env || !((_a = (await env.stat(env.cwd))) === null || _a === void 0 ? void 0 : _a.isDirectory)) {
|
|
79
|
+
throw TaErrors.invalidArgument(`${to} is not a directory.`, 'Pass the directory as it was, and one that exists now: a path, or an ssh:// location.');
|
|
80
|
+
}
|
|
81
|
+
const fromPi = TaLocation.isRemote(old) ? this.directories.toPi(old) : old;
|
|
82
|
+
const toPi = this.directories.toPi(target);
|
|
83
|
+
if (fromPi === toPi || TaPathMap.under(toPi, fromPi)) {
|
|
84
|
+
throw TaErrors.invalidArgument(`${target} is ${old} or in it.`, 'Pass another directory, not one inside the old one.');
|
|
85
|
+
}
|
|
86
|
+
const pairs = [[fromPi, toPi]];
|
|
87
|
+
if (old !== fromPi) {
|
|
88
|
+
pairs.push([old, target]);
|
|
89
|
+
}
|
|
90
|
+
const result = { conversations: 0, busy: 0, data: 0, files: {} };
|
|
91
|
+
const cwds = new Map([[fromPi, toPi]]);
|
|
92
|
+
const root = join(this.agentDir(), 'sessions');
|
|
93
|
+
for (const file of await this.sessionFiles(root)) {
|
|
94
|
+
const cwd = await DirectoryRelocatorUtil.headerCwd(file);
|
|
95
|
+
if (cwd === undefined || !TaPathMap.under(cwd, fromPi)) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const cwdNow = TaPathMap.path(cwd, pairs);
|
|
99
|
+
if (await this.moveSession(file, join(root, DirectoryRelocatorUtil.sessionDir(cwdNow), basename(file)), cwdNow, result)) {
|
|
100
|
+
cwds.set(cwd, cwdNow);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const filePairs = Object.entries(result.files);
|
|
104
|
+
result.data = await this.moveData([...filePairs, ...pairs], cwds);
|
|
105
|
+
const working = this.workingDirectory.get();
|
|
106
|
+
if (working) {
|
|
107
|
+
const moved = TaPathMap.path(working, pairs);
|
|
108
|
+
if (moved !== working) {
|
|
109
|
+
this.workingDirectory.set(moved);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
return result;
|
|
113
|
+
}
|
|
114
|
+
/** A session file into the folder of its new directory, its header saying so; one open idle closes first and opens again there under its id. */
|
|
115
|
+
async moveSession(file, next, cwd, result) {
|
|
116
|
+
var _a;
|
|
117
|
+
const open = this.sessions.list().find(session => session.pi.sessionFile === file);
|
|
118
|
+
if (open === null || open === void 0 ? void 0 : open.pi.isStreaming) {
|
|
119
|
+
result.busy++;
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
if (file === next || (await stat(next).catch(() => undefined))) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
const id = (_a = open === null || open === void 0 ? void 0 : open.id) !== null && _a !== void 0 ? _a : this.handles.idOf(file);
|
|
126
|
+
if (open) {
|
|
127
|
+
await this.sessions.close(open.id, true);
|
|
128
|
+
}
|
|
129
|
+
const text = await readFile(file, 'utf8');
|
|
130
|
+
await this.write(next, DirectoryRelocatorUtil.withCwd(text, cwd));
|
|
131
|
+
await rm(file, { force: true });
|
|
132
|
+
await rmdir(dirname(file)).catch(() => undefined);
|
|
133
|
+
if (id) {
|
|
134
|
+
const handle = this.handles.get(id);
|
|
135
|
+
this.handles.set(id, { ...handle, sessionFile: next, cwd: this.directories.fromPi(cwd) });
|
|
136
|
+
}
|
|
137
|
+
result.files[file] = next;
|
|
138
|
+
result.conversations++;
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* The data of packages with the paths mapped, key by key through the store (pages hear of each): the files of
|
|
143
|
+
* every package, and those of the directories moved, into the data of their new directory.
|
|
144
|
+
*/
|
|
145
|
+
async moveData(pairs, cwds) {
|
|
146
|
+
var _a;
|
|
147
|
+
var _b;
|
|
148
|
+
const changed = new Set();
|
|
149
|
+
const root = join(this.agentDir(), 'extension-data');
|
|
150
|
+
for (const name of await this.packageFiles(root, false)) {
|
|
151
|
+
const values = ExtensionDataUtil.parse(await readFile(join(root, `${name}.json`), 'utf8').catch(() => undefined));
|
|
152
|
+
if (await this.rewrite(name, values, pairs)) {
|
|
153
|
+
changed.add(name);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const environment = (_b = (_a = this.execOptions) === null || _a === void 0 ? void 0 : _a.url) !== null && _b !== void 0 ? _b : '';
|
|
157
|
+
for (const [cwd, cwdNow] of cwds) {
|
|
158
|
+
const before = this.directories.fromPi(cwd);
|
|
159
|
+
const dir = join(root, 'directories', ExtensionDataUtil.directoryId(before, environment));
|
|
160
|
+
const names = await this.packageFiles(dir, true);
|
|
161
|
+
for (const name of names) {
|
|
162
|
+
const values = ExtensionDataUtil.parse(await readFile(join(dir, `${name}.json`), 'utf8').catch(() => undefined));
|
|
163
|
+
await this.rewrite(name, values, pairs, this.directories.fromPi(cwdNow), true);
|
|
164
|
+
changed.add(name);
|
|
165
|
+
}
|
|
166
|
+
if (names.length) {
|
|
167
|
+
await rm(dir, { recursive: true, force: true });
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return changed.size;
|
|
171
|
+
}
|
|
172
|
+
/** \`all\`: every key goes, into the data of the directory \`cwd\`; otherwise only what changed, in place. */
|
|
173
|
+
async rewrite(name, values, pairs, cwd, all = false) {
|
|
174
|
+
let changed = false;
|
|
175
|
+
for (const [key, value] of Object.entries(values)) {
|
|
176
|
+
const keyNow = TaPathMap.json(key, pairs);
|
|
177
|
+
const valueNow = TaPathMap.json(value, pairs);
|
|
178
|
+
if (!all && keyNow === key && JSON.stringify(valueNow) === JSON.stringify(value)) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (!all && keyNow !== key) {
|
|
182
|
+
await this.data.set(name, key, null, cwd);
|
|
183
|
+
}
|
|
184
|
+
await this.data.set(name, keyNow, valueNow, cwd);
|
|
185
|
+
changed = true;
|
|
186
|
+
}
|
|
187
|
+
return changed;
|
|
188
|
+
}
|
|
189
|
+
/** The packages with a file of data in \`root\`, by name (\`@scope/name\` in a folder of its scope); not the data of directories. */
|
|
190
|
+
async packageFiles(root, directory) {
|
|
191
|
+
const names = [];
|
|
192
|
+
for (const entry of await readdir(root, { withFileTypes: true }).catch(() => [])) {
|
|
193
|
+
if (entry.isFile() && entry.name.endsWith('.json')) {
|
|
194
|
+
names.push(entry.name.slice(0, -'.json'.length));
|
|
195
|
+
}
|
|
196
|
+
else if (entry.isDirectory() && entry.name.startsWith('@') && (directory || entry.name !== 'directories')) {
|
|
197
|
+
for (const inner of await readdir(join(root, entry.name)).catch(() => [])) {
|
|
198
|
+
if (inner.endsWith('.json')) {
|
|
199
|
+
names.push(`${entry.name}/${inner.slice(0, -'.json'.length)}`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return names;
|
|
205
|
+
}
|
|
206
|
+
async sessionFiles(root) {
|
|
207
|
+
const files = [];
|
|
208
|
+
for (const dir of await readdir(root, { withFileTypes: true }).catch(() => [])) {
|
|
209
|
+
if (dir.isDirectory()) {
|
|
210
|
+
for (const name of await readdir(join(root, dir.name)).catch(() => [])) {
|
|
211
|
+
if (name.endsWith('.jsonl')) {
|
|
212
|
+
files.push(join(root, dir.name, name));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return files;
|
|
218
|
+
}
|
|
219
|
+
async write(file, text) {
|
|
220
|
+
await mkdir(dirname(file), { recursive: true });
|
|
221
|
+
const temporary = `${file}.ta-relocate-${process.pid}`;
|
|
222
|
+
await writeFile(temporary, text);
|
|
223
|
+
await rename(temporary, file);
|
|
224
|
+
}
|
|
225
|
+
agentDir() {
|
|
226
|
+
var _a;
|
|
227
|
+
var _b;
|
|
228
|
+
return (_b = (_a = this.codingAgentOptions) === null || _a === void 0 ? void 0 : _a.agentDir) !== null && _b !== void 0 ? _b : TaBrand.agentDir();
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
__decorate([
|
|
232
|
+
Value(CODING_AGENT_OPTIONS),
|
|
233
|
+
__metadata("design:type", Object)
|
|
234
|
+
], TaDirectoryRelocatorImpl.prototype, "codingAgentOptions", void 0);
|
|
235
|
+
__decorate([
|
|
236
|
+
Value(TA_EXEC_OPTIONS),
|
|
237
|
+
__metadata("design:type", Object)
|
|
238
|
+
], TaDirectoryRelocatorImpl.prototype, "execOptions", void 0);
|
|
239
|
+
__decorate([
|
|
240
|
+
Autowired(TaSessionRegistry),
|
|
241
|
+
__metadata("design:type", Object)
|
|
242
|
+
], TaDirectoryRelocatorImpl.prototype, "sessions", void 0);
|
|
243
|
+
__decorate([
|
|
244
|
+
Autowired(TaSessionHandles),
|
|
245
|
+
__metadata("design:type", Object)
|
|
246
|
+
], TaDirectoryRelocatorImpl.prototype, "handles", void 0);
|
|
247
|
+
__decorate([
|
|
248
|
+
Autowired(ExtensionDataStore),
|
|
249
|
+
__metadata("design:type", Object)
|
|
250
|
+
], TaDirectoryRelocatorImpl.prototype, "data", void 0);
|
|
251
|
+
__decorate([
|
|
252
|
+
Autowired(PiDirectories),
|
|
253
|
+
__metadata("design:type", Object)
|
|
254
|
+
], TaDirectoryRelocatorImpl.prototype, "directories", void 0);
|
|
255
|
+
__decorate([
|
|
256
|
+
Autowired(TaWorkingDirectory),
|
|
257
|
+
__metadata("design:type", Object)
|
|
258
|
+
], TaDirectoryRelocatorImpl.prototype, "workingDirectory", void 0);
|
|
259
|
+
__decorate([
|
|
260
|
+
Autowired(ExecEnvironmentFactory),
|
|
261
|
+
__metadata("design:type", Object)
|
|
262
|
+
], TaDirectoryRelocatorImpl.prototype, "environments", void 0);
|
|
263
|
+
TaDirectoryRelocatorImpl = __decorate([
|
|
264
|
+
Component(TaDirectoryRelocator)
|
|
265
|
+
], TaDirectoryRelocatorImpl);
|
|
266
|
+
export { TaDirectoryRelocatorImpl };
|
|
267
|
+
//# sourceMappingURL=directory-relocator.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
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.js';
|
|
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
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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 { Autowired, Component } from '@celljs/core';
|
|
11
|
+
import { ExecServer } from '@celljs/ta-exec';
|
|
12
|
+
import { TA_WEBSOCKET_PATH } from '@celljs/ta-protocol';
|
|
13
|
+
import { ServerAware } from '@celljs/web/lib/node';
|
|
14
|
+
import { TA_EXEC_PATH } from './remote/cloud-hosts.js';
|
|
15
|
+
import { TA_ACCESS } from './ta-access.js';
|
|
16
|
+
/**
|
|
17
|
+
* `/api/exec` of ta serve: its machine as the exec server of another Ta (a cloud workspace, `ta://<name>`), whose
|
|
18
|
+
* agent stays there while commands and files are here. Its owner's token only: what it opens is a shell. A gateway
|
|
19
|
+
* forwards it to the sandbox of the account that asks (GatewayExecProxy).
|
|
20
|
+
*/
|
|
21
|
+
let TaExecEndpoint = class TaExecEndpoint {
|
|
22
|
+
async setServer(server) {
|
|
23
|
+
if (typeof (server === null || server === void 0 ? void 0 : server.on) !== 'function') {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
server.on('upgrade', (request, socket, head) => {
|
|
27
|
+
var _a, _b;
|
|
28
|
+
const path = ((_a = request.url) !== null && _a !== void 0 ? _a : '/').split('?')[0];
|
|
29
|
+
if (path === TA_EXEC_PATH) {
|
|
30
|
+
(_b = this.exec) !== null && _b !== void 0 ? _b : (this.exec = new ExecServer({ token: this.access.token, cwd: process.cwd() }));
|
|
31
|
+
this.exec.accept(request, socket, head);
|
|
32
|
+
}
|
|
33
|
+
else if (path !== TA_WEBSOCKET_PATH && server.listenerCount('upgrade') <= 2) {
|
|
34
|
+
// Nobody else answers it: the RPC socket answers only its own path while another listens too.
|
|
35
|
+
socket.destroy();
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
__decorate([
|
|
41
|
+
Autowired(TA_ACCESS),
|
|
42
|
+
__metadata("design:type", Function)
|
|
43
|
+
], TaExecEndpoint.prototype, "access", void 0);
|
|
44
|
+
TaExecEndpoint = __decorate([
|
|
45
|
+
Component(ServerAware)
|
|
46
|
+
], TaExecEndpoint);
|
|
47
|
+
export { TaExecEndpoint };
|
|
48
|
+
//# sourceMappingURL=exec-endpoint.js.map
|
|
@@ -1,22 +1,37 @@
|
|
|
1
1
|
import { type CodingAgentOptions, ModelRuntimeProvider } from '@celljs/ai-coding-agent';
|
|
2
2
|
export declare const ExtensionProviders: unique symbol;
|
|
3
|
+
/** The part of pi's extension runtime that holds what extensions register before a session binds them. */
|
|
4
|
+
interface PendingProviders {
|
|
5
|
+
pendingProviderRegistrations: {
|
|
6
|
+
name: string;
|
|
7
|
+
config: unknown;
|
|
8
|
+
}[];
|
|
9
|
+
}
|
|
3
10
|
/**
|
|
4
11
|
* The model providers that pi packages register (`pi.registerProvider`), before any session: pi registers
|
|
5
12
|
* them in the model runtime that the sessions of ta serve share when it loads a session's extensions, so
|
|
6
13
|
* until a session opened, a client would see none of them (no sign-in, no model to pick).
|
|
7
14
|
*/
|
|
8
15
|
export interface ExtensionProviders {
|
|
9
|
-
/**
|
|
16
|
+
/**
|
|
17
|
+
* Loads the extensions of the user once, for the providers they register; later calls wait for that, and take
|
|
18
|
+
* those they registered since (ta-agents, once it installed an agent).
|
|
19
|
+
*/
|
|
10
20
|
ensure(): Promise<void>;
|
|
11
21
|
}
|
|
12
22
|
export declare class ExtensionProvidersImpl implements ExtensionProviders {
|
|
13
23
|
protected readonly codingAgentOptions?: CodingAgentOptions;
|
|
14
24
|
protected readonly modelRuntimeProvider: ModelRuntimeProvider;
|
|
15
25
|
protected loading?: Promise<void>;
|
|
26
|
+
/** Where the extensions loaded without a session keep what they register later: pi flushes it only when a session binds them. */
|
|
27
|
+
protected pending?: PendingProviders;
|
|
16
28
|
ensure(): Promise<void>;
|
|
29
|
+
/** What the extensions registered since they loaded, into the model runtime, as pi does when they load. */
|
|
30
|
+
protected flush(): Promise<void>;
|
|
17
31
|
/**
|
|
18
32
|
* pi's services of a directory without a project (the home directory, its project resources not
|
|
19
33
|
* trusted): the built-in packages and those of the user's settings, as a session loads them.
|
|
20
34
|
*/
|
|
21
35
|
protected load(): Promise<void>;
|
|
22
36
|
}
|
|
37
|
+
export {};
|
|
@@ -13,10 +13,29 @@ import { Autowired, Component, Value } from '@celljs/core';
|
|
|
13
13
|
import { TaPackages } from '@celljs/ta-harness';
|
|
14
14
|
export const ExtensionProviders = Symbol('ExtensionProviders');
|
|
15
15
|
let ExtensionProvidersImpl = class ExtensionProvidersImpl {
|
|
16
|
-
ensure() {
|
|
16
|
+
async ensure() {
|
|
17
17
|
var _a;
|
|
18
18
|
(_a = this.loading) !== null && _a !== void 0 ? _a : (this.loading = this.load().catch(() => undefined));
|
|
19
|
-
|
|
19
|
+
await this.loading;
|
|
20
|
+
await this.flush();
|
|
21
|
+
}
|
|
22
|
+
/** What the extensions registered since they loaded, into the model runtime, as pi does when they load. */
|
|
23
|
+
async flush() {
|
|
24
|
+
const pending = this.pending;
|
|
25
|
+
if (!(pending === null || pending === void 0 ? void 0 : pending.pendingProviderRegistrations.length)) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
const registrations = pending.pendingProviderRegistrations;
|
|
29
|
+
pending.pendingProviderRegistrations = [];
|
|
30
|
+
const runtime = await this.modelRuntimeProvider.provide();
|
|
31
|
+
for (const { name, config } of registrations) {
|
|
32
|
+
try {
|
|
33
|
+
runtime.registerProvider(name, config);
|
|
34
|
+
}
|
|
35
|
+
catch (_a) {
|
|
36
|
+
// As pi does: a provider that does not register is left out.
|
|
37
|
+
}
|
|
38
|
+
}
|
|
20
39
|
}
|
|
21
40
|
/**
|
|
22
41
|
* pi's services of a directory without a project (the home directory, its project resources not
|
|
@@ -28,13 +47,14 @@ let ExtensionProvidersImpl = class ExtensionProvidersImpl {
|
|
|
28
47
|
const sdk = await PiSdkUtil.load();
|
|
29
48
|
const agentDir = (_b = (_a = this.codingAgentOptions) === null || _a === void 0 ? void 0 : _a.agentDir) !== null && _b !== void 0 ? _b : sdk.getAgentDir();
|
|
30
49
|
const cwd = homedir();
|
|
31
|
-
await sdk.createAgentSessionServices({
|
|
50
|
+
const services = await sdk.createAgentSessionServices({
|
|
32
51
|
cwd,
|
|
33
52
|
agentDir,
|
|
34
53
|
settingsManager: sdk.SettingsManager.create(cwd, agentDir, { projectTrusted: false }),
|
|
35
54
|
modelRuntime: await this.modelRuntimeProvider.provide(),
|
|
36
55
|
resourceLoaderOptions: TaPackages.loaderOptions(agentDir, { loadSkills: sdk.loadSkillsFromDir }),
|
|
37
56
|
});
|
|
57
|
+
this.pending = services.resourceLoader.getExtensions().runtime;
|
|
38
58
|
}
|
|
39
59
|
};
|
|
40
60
|
__decorate([
|
|
@@ -0,0 +1,38 @@
|
|
|
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 {};
|
|
@@ -0,0 +1,124 @@
|
|
|
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 { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
11
|
+
import { dirname, join } from 'node:path';
|
|
12
|
+
import { CODING_AGENT_OPTIONS, PiSdkUtil } from '@celljs/ai-coding-agent';
|
|
13
|
+
import { ApplicationLifecycle, Component, Value } from '@celljs/core';
|
|
14
|
+
/** How long ta serve waits for the catalog of the gateway as it starts. */
|
|
15
|
+
const CATALOG_TIMEOUT_MS = 5000;
|
|
16
|
+
export var GatewayModelsUtil;
|
|
17
|
+
(function (GatewayModelsUtil) {
|
|
18
|
+
GatewayModelsUtil.URL_ENV = 'TA_MODEL_PROXY_URL';
|
|
19
|
+
GatewayModelsUtil.KEY_ENV = 'TA_MODEL_PROXY_KEY';
|
|
20
|
+
/**
|
|
21
|
+
* models.json with the providers of the gateway, each at the gateway (`<url>/api/models/<id>`) with the model key of
|
|
22
|
+
* this sandbox: those it gave before and no longer does go; those of the user stay.
|
|
23
|
+
*/
|
|
24
|
+
function apply(models, catalog, url, key, before) {
|
|
25
|
+
var _a, _b;
|
|
26
|
+
const providers = { ...((_a = models.providers) !== null && _a !== void 0 ? _a : {}) };
|
|
27
|
+
for (const id of before) {
|
|
28
|
+
delete providers[id];
|
|
29
|
+
}
|
|
30
|
+
const managed = [];
|
|
31
|
+
for (const provider of (_b = catalog.providers) !== null && _b !== void 0 ? _b : []) {
|
|
32
|
+
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)) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
providers[provider.id] = {
|
|
36
|
+
...(provider.name ? { name: provider.name } : {}),
|
|
37
|
+
baseUrl: `${url}/api/models/${provider.id}`,
|
|
38
|
+
api: provider.api,
|
|
39
|
+
apiKey: key,
|
|
40
|
+
models: provider.models,
|
|
41
|
+
};
|
|
42
|
+
managed.push(provider.id);
|
|
43
|
+
}
|
|
44
|
+
return { models: { ...models, providers }, managed };
|
|
45
|
+
}
|
|
46
|
+
GatewayModelsUtil.apply = apply;
|
|
47
|
+
/** settings.json with the first model of the gateway as the default, when it has none. */
|
|
48
|
+
function defaults(settings, catalog) {
|
|
49
|
+
var _a, _b;
|
|
50
|
+
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; });
|
|
51
|
+
const model = (_b = first === null || first === void 0 ? void 0 : first.models[0]) === null || _b === void 0 ? void 0 : _b.id;
|
|
52
|
+
if (settings.defaultProvider || !first || typeof model !== 'string') {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
return { ...settings, defaultProvider: first.id, defaultModel: model };
|
|
56
|
+
}
|
|
57
|
+
GatewayModelsUtil.defaults = defaults;
|
|
58
|
+
})(GatewayModelsUtil || (GatewayModelsUtil = {}));
|
|
59
|
+
/**
|
|
60
|
+
* The models of a gateway, for a Ta in its sandbox (docs/design/ta-cloud.md 3.4): with TA_MODEL_PROXY_URL and
|
|
61
|
+
* TA_MODEL_PROXY_KEY, ta serve reads the catalog of the gateway as it starts and puts its providers in pi's
|
|
62
|
+
* models.json, at the gateway with the model key of this sandbox. The keys of the providers stay with the gateway,
|
|
63
|
+
* which counts what each call takes. The two variables leave the environment, so that no command sees them.
|
|
64
|
+
*/
|
|
65
|
+
let GatewayModels = class GatewayModels {
|
|
66
|
+
async onStart() {
|
|
67
|
+
var _a, _b;
|
|
68
|
+
var _c, _d;
|
|
69
|
+
const url = (_a = process.env[GatewayModelsUtil.URL_ENV]) === null || _a === void 0 ? void 0 : _a.replace(/\/+$/, '');
|
|
70
|
+
const key = process.env[GatewayModelsUtil.KEY_ENV];
|
|
71
|
+
delete process.env[GatewayModelsUtil.URL_ENV];
|
|
72
|
+
delete process.env[GatewayModelsUtil.KEY_ENV];
|
|
73
|
+
if (!url || !key) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const answer = await fetch(`${url}/api/models`, { headers: { authorization: `Bearer ${key}` }, signal: AbortSignal.timeout(CATALOG_TIMEOUT_MS) });
|
|
78
|
+
if (!answer.ok) {
|
|
79
|
+
throw new Error(`it answered ${answer.status}`);
|
|
80
|
+
}
|
|
81
|
+
const catalog = (await answer.json());
|
|
82
|
+
const agentDir = (_c = (_b = this.codingAgentOptions) === null || _b === void 0 ? void 0 : _b.agentDir) !== null && _c !== void 0 ? _c : (await PiSdkUtil.load()).getAgentDir();
|
|
83
|
+
const managedFile = join(agentDir, 'gateway-models.json');
|
|
84
|
+
const before = (_d = this.read(managedFile).providers) !== null && _d !== void 0 ? _d : [];
|
|
85
|
+
const modelsFile = join(agentDir, 'models.json');
|
|
86
|
+
const { models, managed } = GatewayModelsUtil.apply(this.read(modelsFile), catalog, url, key, before);
|
|
87
|
+
this.write(modelsFile, models);
|
|
88
|
+
this.write(managedFile, { providers: managed });
|
|
89
|
+
const settingsFile = join(agentDir, 'settings.json');
|
|
90
|
+
const settings = GatewayModelsUtil.defaults(this.read(settingsFile), catalog);
|
|
91
|
+
if (settings) {
|
|
92
|
+
this.write(settingsFile, settings);
|
|
93
|
+
}
|
|
94
|
+
console.log(`ta serve: models of the gateway: ${managed.join(', ') || 'none'}`);
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
console.warn(`ta serve: the models of the gateway did not load: ${error.message}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
read(file) {
|
|
101
|
+
try {
|
|
102
|
+
const value = JSON.parse(readFileSync(file, 'utf8'));
|
|
103
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
104
|
+
}
|
|
105
|
+
catch (_a) {
|
|
106
|
+
return {};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
write(file, data) {
|
|
110
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
111
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
112
|
+
writeFileSync(temporary, `${JSON.stringify(data, undefined, 2)}\n`, { mode: 0o600 });
|
|
113
|
+
renameSync(temporary, file);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
__decorate([
|
|
117
|
+
Value(CODING_AGENT_OPTIONS),
|
|
118
|
+
__metadata("design:type", Object)
|
|
119
|
+
], GatewayModels.prototype, "codingAgentOptions", void 0);
|
|
120
|
+
GatewayModels = __decorate([
|
|
121
|
+
Component(ApplicationLifecycle)
|
|
122
|
+
], GatewayModels);
|
|
123
|
+
export { GatewayModels };
|
|
124
|
+
//# sourceMappingURL=gateway-models.js.map
|