@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.
- package/esm/node/exec-endpoint.d.ts +14 -0
- package/esm/node/exec-endpoint.js +48 -0
- package/esm/node/gateway-models.d.ts +38 -0
- package/esm/node/gateway-models.js +124 -0
- package/esm/node/index.d.ts +3 -0
- package/esm/node/index.js +3 -0
- 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/team/team-calls.js +6 -0
- package/esm/node/usage/agent-activity.d.ts +13 -0
- package/esm/node/usage/agent-activity.js +84 -0
- package/esm/node/usage/index.d.ts +3 -0
- package/esm/node/usage/index.js +4 -0
- package/esm/node/usage/usage-reader.d.ts +28 -0
- package/esm/node/usage/usage-reader.js +159 -0
- package/esm/node/usage/usage-server-impl.d.ts +12 -0
- package/esm/node/usage/usage-server-impl.js +46 -0
- package/lib/node/exec-endpoint.d.ts +15 -0
- package/lib/node/exec-endpoint.js +51 -0
- package/lib/node/gateway-models.d.ts +39 -0
- package/lib/node/gateway-models.js +127 -0
- package/lib/node/index.d.ts +3 -0
- package/lib/node/index.js +3 -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/team/team-calls.js +6 -0
- package/lib/node/usage/agent-activity.d.ts +14 -0
- package/lib/node/usage/agent-activity.js +87 -0
- package/lib/node/usage/index.d.ts +4 -0
- package/lib/node/usage/index.js +20 -0
- package/lib/node/usage/usage-reader.d.ts +29 -0
- package/lib/node/usage/usage-reader.js +163 -0
- package/lib/node/usage/usage-server-impl.d.ts +13 -0
- package/lib/node/usage/usage-server-impl.js +49 -0
- package/package.json +14 -10
|
@@ -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
|
|
@@ -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
|
package/esm/node/index.d.ts
CHANGED
|
@@ -2,10 +2,12 @@ export * from './auth-server-impl.js';
|
|
|
2
2
|
export * from './client-channel.js';
|
|
3
3
|
export * from './client-tool-broker.js';
|
|
4
4
|
export * from './event-log.js';
|
|
5
|
+
export * from './exec-endpoint.js';
|
|
5
6
|
export * from './exec-environment-factory.js';
|
|
6
7
|
export * from './extension-data.js';
|
|
7
8
|
export * from './extension-data-notifier.js';
|
|
8
9
|
export * from './extension-providers.js';
|
|
10
|
+
export * from './gateway-models.js';
|
|
9
11
|
export * from './host-protocol.js';
|
|
10
12
|
export * from './keep-awake.js';
|
|
11
13
|
export * from './package-host-extension.js';
|
|
@@ -41,6 +43,7 @@ export * from './temporary-purge.js';
|
|
|
41
43
|
export * from './terminal-protocol.js';
|
|
42
44
|
export * from './terminal-service.js';
|
|
43
45
|
export * from './terminal-util.js';
|
|
46
|
+
export * from './usage/index.js';
|
|
44
47
|
export * from './web-extension-catalog.js';
|
|
45
48
|
export * from './web-extension-util.js';
|
|
46
49
|
export * from './working-directory.js';
|
package/esm/node/index.js
CHANGED
|
@@ -2,10 +2,12 @@ export * from './auth-server-impl.js';
|
|
|
2
2
|
export * from './client-channel.js';
|
|
3
3
|
export * from './client-tool-broker.js';
|
|
4
4
|
export * from './event-log.js';
|
|
5
|
+
export * from './exec-endpoint.js';
|
|
5
6
|
export * from './exec-environment-factory.js';
|
|
6
7
|
export * from './extension-data.js';
|
|
7
8
|
export * from './extension-data-notifier.js';
|
|
8
9
|
export * from './extension-providers.js';
|
|
10
|
+
export * from './gateway-models.js';
|
|
9
11
|
export * from './host-protocol.js';
|
|
10
12
|
export * from './keep-awake.js';
|
|
11
13
|
export * from './package-host-extension.js';
|
|
@@ -41,6 +43,7 @@ export * from './temporary-purge.js';
|
|
|
41
43
|
export * from './terminal-protocol.js';
|
|
42
44
|
export * from './terminal-service.js';
|
|
43
45
|
export * from './terminal-util.js';
|
|
46
|
+
export * from './usage/index.js';
|
|
44
47
|
export * from './web-extension-catalog.js';
|
|
45
48
|
export * from './web-extension-util.js';
|
|
46
49
|
export * from './working-directory.js';
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ExecClient } from '@celljs/ta-exec';
|
|
2
|
+
/** The path of the exec server of ta serve (and, forwarded, of ta gateway): ExecEndpoint. */
|
|
3
|
+
export declare const TA_EXEC_PATH = "/api/exec";
|
|
4
|
+
export interface CloudHost {
|
|
5
|
+
name: string;
|
|
6
|
+
/** The http(s) address of the gateway or Ta service. */
|
|
7
|
+
url: string;
|
|
8
|
+
/** That of the account there: a sign-in of the gateway, or the access token of a Ta service. */
|
|
9
|
+
token: string;
|
|
10
|
+
addedAt: number;
|
|
11
|
+
}
|
|
12
|
+
export declare namespace CloudHostsUtil {
|
|
13
|
+
/** An address as one writes it: http(s), without a path, a query or credentials. */
|
|
14
|
+
function url(value: string): string;
|
|
15
|
+
/** The WebSocket of its exec server. */
|
|
16
|
+
function execUrl(url: string): string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The cloud workspaces the user added (`<agent directory>/cloud-hosts.json`, readable by this user only, as their
|
|
20
|
+
* tokens are there), and a connection to each, made on its first call. Each connection fences the ones of
|
|
21
|
+
* before (its epoch is when it was made), as with `ta.exec.url`.
|
|
22
|
+
*/
|
|
23
|
+
export declare class CloudHosts {
|
|
24
|
+
protected readonly dir: () => string;
|
|
25
|
+
protected readonly clients: Map<string, ExecClient>;
|
|
26
|
+
constructor(dir: () => string);
|
|
27
|
+
list(): CloudHost[];
|
|
28
|
+
get(name: string): CloudHost | undefined;
|
|
29
|
+
save(host: CloudHost): void;
|
|
30
|
+
remove(name: string): void;
|
|
31
|
+
/** A connection to its exec server, made once; undefined for a name not added. */
|
|
32
|
+
client(name: string): ExecClient | undefined;
|
|
33
|
+
/** What a connection to it would be, for a host not kept yet (checked before it is). */
|
|
34
|
+
static connect(host: Pick<CloudHost, 'url' | 'token'>): ExecClient;
|
|
35
|
+
connected(name: string): boolean;
|
|
36
|
+
/** Its connection ends; the next call connects again. */
|
|
37
|
+
drop(name: string): ExecClient | undefined;
|
|
38
|
+
protected file(): string;
|
|
39
|
+
protected write(hosts: CloudHost[]): void;
|
|
40
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { chmodSync, readFileSync, renameSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { ExecClient } from '@celljs/ta-exec';
|
|
4
|
+
import { TaLocation } from '@celljs/ta-protocol';
|
|
5
|
+
/** The path of the exec server of ta serve (and, forwarded, of ta gateway): ExecEndpoint. */
|
|
6
|
+
export const TA_EXEC_PATH = '/api/exec';
|
|
7
|
+
export var CloudHostsUtil;
|
|
8
|
+
(function (CloudHostsUtil) {
|
|
9
|
+
/** An address as one writes it: http(s), without a path, a query or credentials. */
|
|
10
|
+
function url(value) {
|
|
11
|
+
let parsed;
|
|
12
|
+
try {
|
|
13
|
+
parsed = new URL(value.trim());
|
|
14
|
+
}
|
|
15
|
+
catch (_a) {
|
|
16
|
+
throw new Error(`${value} is not an address: write https://ta.example.com.`);
|
|
17
|
+
}
|
|
18
|
+
if ((parsed.protocol !== 'https:' && parsed.protocol !== 'http:') || parsed.username || parsed.password) {
|
|
19
|
+
throw new Error(`${value} is not an address of a Ta gateway: write https://ta.example.com.`);
|
|
20
|
+
}
|
|
21
|
+
return `${parsed.origin}${parsed.pathname.replace(/\/+$/, '')}`;
|
|
22
|
+
}
|
|
23
|
+
CloudHostsUtil.url = url;
|
|
24
|
+
/** The WebSocket of its exec server. */
|
|
25
|
+
function execUrl(url) {
|
|
26
|
+
return `${url.replace(/^http/, 'ws')}${TA_EXEC_PATH}`;
|
|
27
|
+
}
|
|
28
|
+
CloudHostsUtil.execUrl = execUrl;
|
|
29
|
+
})(CloudHostsUtil || (CloudHostsUtil = {}));
|
|
30
|
+
/**
|
|
31
|
+
* The cloud workspaces the user added (`<agent directory>/cloud-hosts.json`, readable by this user only, as their
|
|
32
|
+
* tokens are there), and a connection to each, made on its first call. Each connection fences the ones of
|
|
33
|
+
* before (its epoch is when it was made), as with `ta.exec.url`.
|
|
34
|
+
*/
|
|
35
|
+
export class CloudHosts {
|
|
36
|
+
constructor(dir) {
|
|
37
|
+
this.dir = dir;
|
|
38
|
+
this.clients = new Map();
|
|
39
|
+
}
|
|
40
|
+
list() {
|
|
41
|
+
try {
|
|
42
|
+
const value = JSON.parse(readFileSync(this.file(), 'utf8'));
|
|
43
|
+
return Array.isArray(value.hosts)
|
|
44
|
+
? value.hosts.filter(host => TaLocation.isCloudName(host === null || host === void 0 ? void 0 : host.name) && typeof host.url === 'string' && typeof host.token === 'string')
|
|
45
|
+
: [];
|
|
46
|
+
}
|
|
47
|
+
catch (_a) {
|
|
48
|
+
return [];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
get(name) {
|
|
52
|
+
return this.list().find(host => host.name === name);
|
|
53
|
+
}
|
|
54
|
+
save(host) {
|
|
55
|
+
this.write([host, ...this.list().filter(other => other.name !== host.name)]);
|
|
56
|
+
}
|
|
57
|
+
remove(name) {
|
|
58
|
+
this.write(this.list().filter(other => other.name !== name));
|
|
59
|
+
}
|
|
60
|
+
/** A connection to its exec server, made once; undefined for a name not added. */
|
|
61
|
+
client(name) {
|
|
62
|
+
let client = this.clients.get(name);
|
|
63
|
+
if (!client) {
|
|
64
|
+
const host = this.get(name);
|
|
65
|
+
if (!host) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
client = CloudHosts.connect(host);
|
|
69
|
+
this.clients.set(name, client);
|
|
70
|
+
}
|
|
71
|
+
return client;
|
|
72
|
+
}
|
|
73
|
+
/** What a connection to it would be, for a host not kept yet (checked before it is). */
|
|
74
|
+
static connect(host) {
|
|
75
|
+
return new ExecClient({ connect: ExecClient.webSocket(CloudHostsUtil.execUrl(host.url), host.token), epoch: Date.now() });
|
|
76
|
+
}
|
|
77
|
+
connected(name) {
|
|
78
|
+
var _a;
|
|
79
|
+
var _b;
|
|
80
|
+
return (_b = (_a = this.clients.get(name)) === null || _a === void 0 ? void 0 : _a.connected) !== null && _b !== void 0 ? _b : false;
|
|
81
|
+
}
|
|
82
|
+
/** Its connection ends; the next call connects again. */
|
|
83
|
+
drop(name) {
|
|
84
|
+
const client = this.clients.get(name);
|
|
85
|
+
this.clients.delete(name);
|
|
86
|
+
return client;
|
|
87
|
+
}
|
|
88
|
+
file() {
|
|
89
|
+
return join(this.dir(), 'cloud-hosts.json');
|
|
90
|
+
}
|
|
91
|
+
write(hosts) {
|
|
92
|
+
const file = this.file();
|
|
93
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
94
|
+
writeFileSync(temporary, JSON.stringify({ hosts }, undefined, 2), { mode: 0o600 });
|
|
95
|
+
chmodSync(temporary, 0o600);
|
|
96
|
+
renameSync(temporary, file);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
//# sourceMappingURL=cloud-hosts.js.map
|
package/esm/node/remote/index.js
CHANGED
|
@@ -2,7 +2,8 @@ import { type CodingAgentOptions } from '@celljs/ai-coding-agent';
|
|
|
2
2
|
import { type TaExecOptions } from '../host-protocol.js';
|
|
3
3
|
import { PiDirectories } from './remote-protocol.js';
|
|
4
4
|
/**
|
|
5
|
-
* `<agent directory>/remote/ssh/<host>/<path>` for `ssh://<host>/<path
|
|
5
|
+
* `<agent directory>/remote/ssh/<host>/<path>` for `ssh://<host>/<path>`, `remote/ta/<name>/<path>` for a cloud
|
|
6
|
+
* workspace, `ta://<name>/<path>`. With `ta.exec.url` every
|
|
6
7
|
* directory is one of the machine of the exec server: `<agent directory>/remote/exec/<path>`.
|
|
7
8
|
*/
|
|
8
9
|
export declare class DefaultPiDirectories implements PiDirectories {
|
|
@@ -16,14 +16,16 @@ import { TaLocation } from '@celljs/ta-protocol';
|
|
|
16
16
|
import { TA_EXEC_OPTIONS } from '../host-protocol.js';
|
|
17
17
|
import { PiDirectories } from './remote-protocol.js';
|
|
18
18
|
/**
|
|
19
|
-
* `<agent directory>/remote/ssh/<host>/<path>` for `ssh://<host>/<path
|
|
19
|
+
* `<agent directory>/remote/ssh/<host>/<path>` for `ssh://<host>/<path>`, `remote/ta/<name>/<path>` for a cloud
|
|
20
|
+
* workspace, `ta://<name>/<path>`. With `ta.exec.url` every
|
|
20
21
|
* directory is one of the machine of the exec server: `<agent directory>/remote/exec/<path>`.
|
|
21
22
|
*/
|
|
22
23
|
let DefaultPiDirectories = class DefaultPiDirectories {
|
|
23
24
|
toPi(location) {
|
|
24
25
|
var _a;
|
|
25
26
|
const remote = TaLocation.parse(location);
|
|
26
|
-
const
|
|
27
|
+
const cloud = remote && TaLocation.cloudName(remote.host);
|
|
28
|
+
const prefix = cloud ? join('ta', cloud) : remote ? join('ssh', remote.host) : ((_a = this.execOptions) === null || _a === void 0 ? void 0 : _a.url) ? 'exec' : undefined;
|
|
27
29
|
if (!prefix) {
|
|
28
30
|
return location;
|
|
29
31
|
}
|
|
@@ -41,6 +43,9 @@ let DefaultPiDirectories = class DefaultPiDirectories {
|
|
|
41
43
|
if (kind === 'ssh' && rest.length > 0 && TaLocation.isHost(rest[0])) {
|
|
42
44
|
return TaLocation.ssh(rest[0], `/${rest.slice(1).join('/')}`);
|
|
43
45
|
}
|
|
46
|
+
if (kind === 'ta' && rest.length > 0 && TaLocation.isCloudName(rest[0])) {
|
|
47
|
+
return TaLocation.cloud(rest[0], `/${rest.slice(1).join('/')}`);
|
|
48
|
+
}
|
|
44
49
|
return kind === 'exec' ? `/${rest.join('/')}` : cwd;
|
|
45
50
|
}
|
|
46
51
|
root() {
|
|
@@ -4,6 +4,7 @@ import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
|
4
4
|
import { type ExecClient, SshHost, type SshQuestion } from '@celljs/ta-exec';
|
|
5
5
|
import { type TaRemoteEvent, type TaRemoteHost, type TaRemotePrompt } from '@celljs/ta-protocol';
|
|
6
6
|
import { TaWorkingDirectory } from '../working-directory.js';
|
|
7
|
+
import { CloudHosts } from './cloud-hosts.js';
|
|
7
8
|
import { RemoteHosts, type TaRemoteOptions } from './remote-protocol.js';
|
|
8
9
|
interface Waiting {
|
|
9
10
|
prompt: TaRemotePrompt;
|
|
@@ -21,9 +22,13 @@ export declare class DefaultRemoteHosts implements RemoteHosts, ApplicationLifec
|
|
|
21
22
|
protected readonly waiting: Map<string, Waiting>;
|
|
22
23
|
protected readonly watchers: Set<RpcConnection>;
|
|
23
24
|
protected readonly disconnectListeners: Set<(host: string) => void>;
|
|
25
|
+
protected readonly cloud: CloudHosts;
|
|
26
|
+
protected readonly watchedCloud: Set<string>;
|
|
24
27
|
client(host: string): ExecClient;
|
|
25
28
|
list(): TaRemoteHost[];
|
|
26
29
|
add(host: string): void;
|
|
30
|
+
/** Checked by connecting and asking where its home is; kept only then. */
|
|
31
|
+
addCloud(name: string, url: string, token: string): Promise<TaRemoteHost>;
|
|
27
32
|
remove(host: string): Promise<void>;
|
|
28
33
|
disconnect(host: string): Promise<void>;
|
|
29
34
|
onDisconnect(listener: (host: string) => void): () => void;
|
|
@@ -15,6 +15,7 @@ import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
|
|
|
15
15
|
import { TaClient, TaLocation } from '@celljs/ta-protocol';
|
|
16
16
|
import { TaErrors } from '../ta-errors.js';
|
|
17
17
|
import { TaWorkingDirectory } from '../working-directory.js';
|
|
18
|
+
import { CloudHosts, CloudHostsUtil } from './cloud-hosts.js';
|
|
18
19
|
import { RemoteHosts, TA_REMOTE_OPTIONS } from './remote-protocol.js';
|
|
19
20
|
/** A prompt nobody answers is cancelled after this: ssh gives up. */
|
|
20
21
|
const PROMPT_TIMEOUT_MS = 5 * 60 * 1000;
|
|
@@ -28,11 +29,25 @@ let DefaultRemoteHosts = class DefaultRemoteHosts {
|
|
|
28
29
|
this.waiting = new Map();
|
|
29
30
|
this.watchers = new Set();
|
|
30
31
|
this.disconnectListeners = new Set();
|
|
32
|
+
this.cloud = new CloudHosts(() => { var _a; var _b; return (_b = (_a = this.codingAgentOptions) === null || _a === void 0 ? void 0 : _a.agentDir) !== null && _b !== void 0 ? _b : TaBrand.agentDir(); });
|
|
33
|
+
this.watchedCloud = new Set();
|
|
31
34
|
}
|
|
32
35
|
client(host) {
|
|
33
36
|
var _a, _b, _c, _d;
|
|
34
37
|
var _e;
|
|
35
38
|
this.check(host);
|
|
39
|
+
const name = TaLocation.cloudName(host);
|
|
40
|
+
if (name) {
|
|
41
|
+
const client = this.cloud.client(name);
|
|
42
|
+
if (!client) {
|
|
43
|
+
throw TaErrors.invalidArgument(`There is no cloud workspace ${name}.`, 'Add it in Settings, Remote hosts, with the address of its gateway and a token.');
|
|
44
|
+
}
|
|
45
|
+
if (!this.watchedCloud.has(name)) {
|
|
46
|
+
this.watchedCloud.add(name);
|
|
47
|
+
client.onState((connected, error) => this.broadcast(error ? { type: 'connection', host, connected, error } : { type: 'connection', host, connected }));
|
|
48
|
+
}
|
|
49
|
+
return client;
|
|
50
|
+
}
|
|
36
51
|
let ssh = this.hosts.get(host);
|
|
37
52
|
if (!ssh) {
|
|
38
53
|
ssh = new SshHost({
|
|
@@ -58,18 +73,68 @@ let DefaultRemoteHosts = class DefaultRemoteHosts {
|
|
|
58
73
|
const recent = this.workingDirectory.recentHosts();
|
|
59
74
|
const config = SshConfig.hosts().filter(host => TaLocation.isHost(host) && !recent.includes(host));
|
|
60
75
|
return [
|
|
76
|
+
...this.cloud
|
|
77
|
+
.list()
|
|
78
|
+
.map(host => ({ host: TaLocation.cloud(host.name), source: 'cloud', url: host.url, connected: this.cloud.connected(host.name) })),
|
|
61
79
|
...recent.map(host => ({ host, source: 'recent', connected: this.connected(host) })),
|
|
62
80
|
...config.map(host => ({ host, source: 'config', connected: this.connected(host) })),
|
|
63
81
|
];
|
|
64
82
|
}
|
|
65
83
|
add(host) {
|
|
84
|
+
if (TaLocation.isCloud(host)) {
|
|
85
|
+
throw TaErrors.invalidArgument('A cloud workspace is added with its address and token.', 'Call addCloudHost.');
|
|
86
|
+
}
|
|
66
87
|
this.workingDirectory.addRecentHost(this.check(host));
|
|
67
88
|
}
|
|
89
|
+
/** Checked by connecting and asking where its home is; kept only then. */
|
|
90
|
+
async addCloud(name, url, token) {
|
|
91
|
+
if (typeof name !== 'string' || !TaLocation.isCloudName(name)) {
|
|
92
|
+
throw TaErrors.invalidArgument(`${JSON.stringify(name)} cannot name a cloud workspace.`, 'Use lower-case letters, digits and -, 40 at most.');
|
|
93
|
+
}
|
|
94
|
+
let address;
|
|
95
|
+
try {
|
|
96
|
+
address = CloudHostsUtil.url(String(url !== null && url !== void 0 ? url : ''));
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
throw TaErrors.invalidArgument(error.message, 'Write the address of the Ta gateway, as its page is opened.');
|
|
100
|
+
}
|
|
101
|
+
if (typeof token !== 'string' || !token.trim()) {
|
|
102
|
+
throw TaErrors.invalidArgument('A cloud workspace is reached with a token.', 'Make one on the gateway: Settings, Environment, Use it from another Ta.');
|
|
103
|
+
}
|
|
104
|
+
const probe = CloudHosts.connect({ url: address, token: token.trim() });
|
|
105
|
+
try {
|
|
106
|
+
await probe.info();
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
throw TaErrors.invalidArgument(`${address} did not let Ta in: ${error.message}`, 'Check the address and the token; a token signed out on the gateway no longer works.');
|
|
110
|
+
}
|
|
111
|
+
finally {
|
|
112
|
+
probe.close();
|
|
113
|
+
}
|
|
114
|
+
await this.disconnect(TaLocation.cloud(name));
|
|
115
|
+
this.cloud.save({ name, url: address, token: token.trim(), addedAt: Date.now() });
|
|
116
|
+
return { host: TaLocation.cloud(name), source: 'cloud', url: address, connected: false };
|
|
117
|
+
}
|
|
68
118
|
async remove(host) {
|
|
69
119
|
await this.disconnect(host);
|
|
120
|
+
const name = TaLocation.cloudName(host);
|
|
121
|
+
if (name) {
|
|
122
|
+
this.cloud.remove(name);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
70
125
|
this.workingDirectory.removeRecentHost(host);
|
|
71
126
|
}
|
|
72
127
|
async disconnect(host) {
|
|
128
|
+
const name = TaLocation.cloudName(host);
|
|
129
|
+
if (name) {
|
|
130
|
+
const client = this.cloud.drop(name);
|
|
131
|
+
this.watchedCloud.delete(name);
|
|
132
|
+
for (const listener of this.disconnectListeners) {
|
|
133
|
+
listener(host);
|
|
134
|
+
}
|
|
135
|
+
client === null || client === void 0 ? void 0 : client.close();
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
73
138
|
const ssh = this.hosts.get(host);
|
|
74
139
|
this.hosts.delete(host);
|
|
75
140
|
for (const listener of this.disconnectListeners) {
|
|
@@ -97,7 +162,7 @@ let DefaultRemoteHosts = class DefaultRemoteHosts {
|
|
|
97
162
|
for (const waiting of [...this.waiting.values()]) {
|
|
98
163
|
waiting.settle(undefined);
|
|
99
164
|
}
|
|
100
|
-
await Promise.all([...this.hosts.keys()].map(host => this.disconnect(host)));
|
|
165
|
+
await Promise.all([...this.hosts.keys(), ...[...this.watchedCloud].map(name => TaLocation.cloud(name))].map(host => this.disconnect(host)));
|
|
101
166
|
}
|
|
102
167
|
ask(host, question) {
|
|
103
168
|
const prompt = { promptId: randomUUID(), host, ...question };
|
|
@@ -126,13 +191,17 @@ let DefaultRemoteHosts = class DefaultRemoteHosts {
|
|
|
126
191
|
}
|
|
127
192
|
check(host) {
|
|
128
193
|
if (typeof host !== 'string' || !TaLocation.isHost(host)) {
|
|
129
|
-
throw TaErrors.invalidArgument(`${JSON.stringify(host)} is not a host.`, 'Name a Host of ~/.ssh/config,
|
|
194
|
+
throw TaErrors.invalidArgument(`${JSON.stringify(host)} is not a host.`, 'Name a Host of ~/.ssh/config, [user@]host[:port], or ta://<name> of a cloud workspace.');
|
|
130
195
|
}
|
|
131
196
|
return host;
|
|
132
197
|
}
|
|
133
198
|
connected(host) {
|
|
134
199
|
var _a;
|
|
135
200
|
var _b;
|
|
201
|
+
const name = TaLocation.cloudName(host);
|
|
202
|
+
if (name) {
|
|
203
|
+
return this.cloud.connected(name);
|
|
204
|
+
}
|
|
136
205
|
return (_b = (_a = this.hosts.get(host)) === null || _a === void 0 ? void 0 : _a.client.connected) !== null && _b !== void 0 ? _b : false;
|
|
137
206
|
}
|
|
138
207
|
};
|
|
@@ -21,6 +21,8 @@ export interface RemoteHosts {
|
|
|
21
21
|
list(): TaRemoteHost[];
|
|
22
22
|
/** Keeps a host in the list without connecting; throws for what is not a host. */
|
|
23
23
|
add(host: string): void;
|
|
24
|
+
/** A cloud workspace, `ta://<name>`, kept once a connection to it worked. */
|
|
25
|
+
addCloud(name: string, url: string, token: string): Promise<TaRemoteHost>;
|
|
24
26
|
/** Disconnects and drops it from the list of those opened before. */
|
|
25
27
|
remove(host: string): Promise<void>;
|
|
26
28
|
disconnect(host: string): Promise<void>;
|
|
@@ -8,6 +8,7 @@ export declare class TaRemoteServerImpl implements TaRemoteServer {
|
|
|
8
8
|
protected readonly environments: ExecEnvironmentFactory;
|
|
9
9
|
listHosts(): Promise<TaRemoteHost[]>;
|
|
10
10
|
addHost(host: string): Promise<void>;
|
|
11
|
+
addCloudHost(name: string, url: string, token: string): Promise<TaRemoteHost>;
|
|
11
12
|
removeHost(host: string): Promise<void>;
|
|
12
13
|
resolveLocation(location: string): Promise<string>;
|
|
13
14
|
forwardPort(port: number, cwd?: string): Promise<TaForwardedPort>;
|
|
@@ -22,6 +22,9 @@ let TaRemoteServerImpl = class TaRemoteServerImpl {
|
|
|
22
22
|
addHost(host) {
|
|
23
23
|
return TaErrors.guard(() => this.hosts.add(host));
|
|
24
24
|
}
|
|
25
|
+
addCloudHost(name, url, token) {
|
|
26
|
+
return TaErrors.guard(() => this.hosts.addCloud(name, url, token));
|
|
27
|
+
}
|
|
25
28
|
removeHost(host) {
|
|
26
29
|
return TaErrors.guard(() => this.hosts.remove(host));
|
|
27
30
|
}
|