@celljs/ta-host 4.9.1 → 4.11.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/README.en.md +5 -5
- package/README.md +5 -5
- package/esm/node/client-tool-broker.d.ts +6 -2
- package/esm/node/client-tool-broker.js +11 -4
- package/esm/node/event-log.d.ts +2 -1
- package/esm/node/event-log.js +3 -2
- package/esm/node/exec-environment-factory.d.ts +7 -0
- package/esm/node/exec-environment-factory.js +30 -7
- package/esm/node/host-protocol.d.ts +24 -10
- package/esm/node/index.d.ts +2 -0
- package/esm/node/index.js +2 -0
- package/esm/node/package-hosts.d.ts +6 -0
- package/esm/node/package-hosts.js +36 -1
- package/esm/node/package-server-impl.d.ts +0 -1
- package/esm/node/package-server-impl.js +0 -3
- package/esm/node/relay-bridge.d.ts +12 -2
- package/esm/node/relay-bridge.js +93 -3
- package/esm/node/remote/remote-hosts.d.ts +1 -3
- package/esm/node/remote/remote-hosts.js +0 -15
- package/esm/node/remote/remote-protocol.d.ts +0 -2
- package/esm/node/remote/remote-server-impl.js +1 -1
- package/esm/node/remote-ui-context.d.ts +1 -1
- package/esm/node/remote-ui-context.js +3 -1
- package/esm/node/rooms-bridge.d.ts +60 -0
- package/esm/node/rooms-bridge.js +155 -0
- package/esm/node/session-catalog.d.ts +7 -3
- package/esm/node/session-catalog.js +15 -4
- package/esm/node/session-handles.d.ts +32 -0
- package/esm/node/session-handles.js +86 -0
- package/esm/node/session-info-util.d.ts +7 -0
- package/esm/node/session-info-util.js +63 -4
- package/esm/node/session-list-feed.d.ts +28 -0
- package/esm/node/session-list-feed.js +62 -0
- package/esm/node/sign-in/sign-in-flow.d.ts +2 -0
- package/esm/node/sign-in/sign-in-flow.js +9 -0
- package/esm/node/sign-in/sign-in-provider.js +3 -2
- package/esm/node/slash-command-util.d.ts +43 -0
- package/esm/node/slash-command-util.js +105 -0
- package/esm/node/ta-access.d.ts +11 -0
- package/esm/node/ta-access.js +29 -3
- package/esm/node/ta-errors.js +17 -2
- package/esm/node/ta-event-util.d.ts +19 -2
- package/esm/node/ta-event-util.js +97 -3
- package/esm/node/ta-server-impl.d.ts +13 -7
- package/esm/node/ta-server-impl.js +43 -24
- package/esm/node/ta-session-factory.d.ts +26 -2
- package/esm/node/ta-session-factory.js +64 -16
- package/esm/node/ta-session-registry.d.ts +16 -2
- package/esm/node/ta-session-registry.js +79 -20
- package/esm/node/ta-session.d.ts +16 -6
- package/esm/node/ta-session.js +59 -14
- package/esm/node/web-extension-catalog.d.ts +5 -1
- package/esm/node/web-extension-catalog.js +15 -19
- package/esm/node/web-extension-util.js +7 -3
- package/esm/node/workspace-files.d.ts +19 -5
- package/esm/node/workspace-files.js +26 -15
- package/esm/node/workspace-protocol.d.ts +1 -1
- package/esm/node/workspace-util.d.ts +2 -0
- package/esm/node/workspace-util.js +8 -3
- package/lib/node/client-tool-broker.d.ts +6 -2
- package/lib/node/client-tool-broker.js +11 -4
- package/lib/node/event-log.d.ts +2 -1
- package/lib/node/event-log.js +3 -2
- package/lib/node/exec-environment-factory.d.ts +7 -0
- package/lib/node/exec-environment-factory.js +30 -7
- package/lib/node/host-protocol.d.ts +24 -10
- package/lib/node/index.d.ts +2 -0
- package/lib/node/index.js +2 -0
- package/lib/node/package-hosts.d.ts +6 -0
- package/lib/node/package-hosts.js +35 -0
- package/lib/node/package-server-impl.d.ts +0 -1
- package/lib/node/package-server-impl.js +0 -3
- package/lib/node/relay-bridge.d.ts +12 -2
- package/lib/node/relay-bridge.js +91 -1
- package/lib/node/remote/remote-hosts.d.ts +1 -3
- package/lib/node/remote/remote-hosts.js +0 -15
- package/lib/node/remote/remote-protocol.d.ts +0 -2
- package/lib/node/remote/remote-server-impl.js +1 -1
- package/lib/node/remote-ui-context.d.ts +1 -1
- package/lib/node/remote-ui-context.js +3 -1
- package/lib/node/rooms-bridge.d.ts +61 -0
- package/lib/node/rooms-bridge.js +158 -0
- package/lib/node/session-catalog.d.ts +7 -3
- package/lib/node/session-catalog.js +15 -4
- package/lib/node/session-handles.d.ts +33 -0
- package/lib/node/session-handles.js +89 -0
- package/lib/node/session-info-util.d.ts +7 -0
- package/lib/node/session-info-util.js +62 -3
- package/lib/node/session-list-feed.d.ts +29 -0
- package/lib/node/session-list-feed.js +66 -0
- package/lib/node/sign-in/sign-in-flow.d.ts +2 -0
- package/lib/node/sign-in/sign-in-flow.js +10 -1
- package/lib/node/sign-in/sign-in-provider.js +3 -2
- package/lib/node/slash-command-util.d.ts +44 -0
- package/lib/node/slash-command-util.js +108 -0
- package/lib/node/ta-access.d.ts +11 -0
- package/lib/node/ta-access.js +29 -3
- package/lib/node/ta-errors.js +17 -2
- package/lib/node/ta-event-util.d.ts +19 -2
- package/lib/node/ta-event-util.js +97 -3
- package/lib/node/ta-server-impl.d.ts +13 -7
- package/lib/node/ta-server-impl.js +41 -22
- package/lib/node/ta-session-factory.d.ts +26 -2
- package/lib/node/ta-session-factory.js +65 -17
- package/lib/node/ta-session-registry.d.ts +16 -2
- package/lib/node/ta-session-registry.js +79 -20
- package/lib/node/ta-session.d.ts +16 -6
- package/lib/node/ta-session.js +59 -14
- package/lib/node/web-extension-catalog.d.ts +5 -1
- package/lib/node/web-extension-catalog.js +15 -19
- package/lib/node/web-extension-util.js +7 -3
- package/lib/node/workspace-files.d.ts +19 -5
- package/lib/node/workspace-files.js +26 -15
- package/lib/node/workspace-protocol.d.ts +1 -1
- package/lib/node/workspace-util.d.ts +2 -0
- package/lib/node/workspace-util.js +8 -3
- package/package.json +12 -12
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { TaClient } from '@celljs/ta-protocol';
|
|
2
|
+
/** At most one list per connection in this time, the latest; the first change leaves at once. */
|
|
3
|
+
export const SESSION_LIST_MS = 250;
|
|
4
|
+
/**
|
|
5
|
+
* The open sessions told to the connections that watch them (TaServer.watchSessions): the whole list each time
|
|
6
|
+
* what a list shows of it changes, so that every page shows the same at once, rather than what it read a few
|
|
7
|
+
* seconds before. A session streams hundreds of events a second, of which few change the list: those come
|
|
8
|
+
* here (`changed`), and a moment of them leaves as one list, only when it differs from the one sent.
|
|
9
|
+
*/
|
|
10
|
+
export class SessionListFeed {
|
|
11
|
+
constructor(list, intervalMs = SESSION_LIST_MS) {
|
|
12
|
+
this.list = list;
|
|
13
|
+
this.intervalMs = intervalMs;
|
|
14
|
+
this.watchers = new Map();
|
|
15
|
+
}
|
|
16
|
+
/** The list now; the connection is told of each change from now on, until it closes. */
|
|
17
|
+
watch(connection) {
|
|
18
|
+
const list = this.list();
|
|
19
|
+
let watcher = this.watchers.get(connection);
|
|
20
|
+
if (!watcher) {
|
|
21
|
+
const created = { lastAt: Number.NEGATIVE_INFINITY };
|
|
22
|
+
this.watchers.set(connection, created);
|
|
23
|
+
connection.onClose(() => {
|
|
24
|
+
clearTimeout(created.timer);
|
|
25
|
+
this.watchers.delete(connection);
|
|
26
|
+
});
|
|
27
|
+
watcher = created;
|
|
28
|
+
}
|
|
29
|
+
watcher.sent = JSON.stringify(list);
|
|
30
|
+
return list;
|
|
31
|
+
}
|
|
32
|
+
/** What a list shows changed: a session opened or closed, or one of them changed. */
|
|
33
|
+
changed() {
|
|
34
|
+
for (const [connection, watcher] of this.watchers) {
|
|
35
|
+
this.schedule(connection, watcher);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
schedule(connection, watcher) {
|
|
39
|
+
if (watcher.timer !== undefined) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const wait = Math.max(0, watcher.lastAt + this.intervalMs - performance.now());
|
|
43
|
+
watcher.timer = setTimeout(() => {
|
|
44
|
+
watcher.timer = undefined;
|
|
45
|
+
watcher.lastAt = performance.now();
|
|
46
|
+
if (connection.closed) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const list = this.list();
|
|
50
|
+
const json = JSON.stringify(list);
|
|
51
|
+
if (json === watcher.sent) {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
watcher.sent = json;
|
|
55
|
+
connection
|
|
56
|
+
.client(TaClient)
|
|
57
|
+
.onSessions(list)
|
|
58
|
+
.catch(() => undefined);
|
|
59
|
+
}, wait);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=session-list-feed.js.map
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { IncomingMessage } from 'node:http';
|
|
2
2
|
import type { AccessStore } from './access-store.js';
|
|
3
3
|
import { type SignInAccount, type SignInProvider } from './sign-in-provider.js';
|
|
4
|
+
/** Sign-ins under way at most: anyone may start one without a token, and each is kept until it ends or expires. */
|
|
5
|
+
export declare const MAX_PENDING_SIGN_INS = 1000;
|
|
4
6
|
interface Pending {
|
|
5
7
|
provider: string;
|
|
6
8
|
verifier: string;
|
|
@@ -4,6 +4,8 @@ import { SignInError } from './sign-in-provider.js';
|
|
|
4
4
|
/** How long the browser has with the provider, and a link of {@link SignInFlow.bindUrl}. */
|
|
5
5
|
const PENDING_TTL_MS = 10 * 60 * 1000;
|
|
6
6
|
const BIND_TTL_MS = 5 * 60 * 1000;
|
|
7
|
+
/** Sign-ins under way at most: anyone may start one without a token, and each is kept until it ends or expires. */
|
|
8
|
+
export const MAX_PENDING_SIGN_INS = 1000;
|
|
7
9
|
export var SignInUtil;
|
|
8
10
|
(function (SignInUtil) {
|
|
9
11
|
/**
|
|
@@ -64,6 +66,13 @@ export class SignInFlow {
|
|
|
64
66
|
if (bind) {
|
|
65
67
|
this.binds.delete(bind);
|
|
66
68
|
}
|
|
69
|
+
// The oldest goes first: a flood of starts takes no more memory, and costs only sign-ins as old.
|
|
70
|
+
for (const oldest of this.pending.keys()) {
|
|
71
|
+
if (this.pending.size < MAX_PENDING_SIGN_INS) {
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
this.pending.delete(oldest);
|
|
75
|
+
}
|
|
67
76
|
const state = randomBytes(18).toString('base64url');
|
|
68
77
|
const { verifier, challenge } = SignInUtil.pkce();
|
|
69
78
|
const redirectUri = SignInUtil.redirectUri(origin, provider.id);
|
|
@@ -130,8 +130,9 @@ export class OidcSignIn {
|
|
|
130
130
|
throw new SignInError(`${this.name}: ${(_b = (_a = token.error_description) !== null && _a !== void 0 ? _a : token.error) !== null && _b !== void 0 ? _b : 'no access token'}`);
|
|
131
131
|
}
|
|
132
132
|
const user = await SignInHttp.json(userinfo_endpoint, { headers: { Authorization: `Bearer ${token.access_token}`, Accept: 'application/json' } });
|
|
133
|
-
//
|
|
134
|
-
|
|
133
|
+
// Only an address the provider says it verified (some say "true" as a string): one it does not vouch for
|
|
134
|
+
// (no email_verified, as Microsoft Entra) may be any address the user typed, and would match TA_ALLOWED_USERS.
|
|
135
|
+
const email = user.email && (user.email_verified === true || user.email_verified === 'true') ? user.email : undefined;
|
|
135
136
|
if (!email && (this.options.requireEmail || !user.sub)) {
|
|
136
137
|
throw new SignInError(`${this.name}: the account has no verified email address`);
|
|
137
138
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { TaSlashCommand } from '@celljs/ta-protocol';
|
|
2
|
+
type Descriptions = Readonly<Record<string, string>>;
|
|
3
|
+
type SourceInfo = TaSlashCommand['sourceInfo'];
|
|
4
|
+
/** What the `/` menu lists, as pi has them: commands of extensions by the names they are run by, prompt templates, skills. */
|
|
5
|
+
export interface SlashCommandSources {
|
|
6
|
+
commands: readonly {
|
|
7
|
+
invocationName: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
sourceInfo: SourceInfo;
|
|
10
|
+
}[];
|
|
11
|
+
prompts: readonly {
|
|
12
|
+
name: string;
|
|
13
|
+
description?: string;
|
|
14
|
+
sourceInfo: SourceInfo;
|
|
15
|
+
}[];
|
|
16
|
+
skills: readonly {
|
|
17
|
+
name: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
sourceInfo: SourceInfo;
|
|
20
|
+
}[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The descriptions in other languages of the commands of the `/` menu: `pi.descriptions` of the package.json
|
|
24
|
+
* of the package each command or skill comes from, by its name there (`loop`, `skill:ta-game`), each a
|
|
25
|
+
* string by language (`{ "zh-CN": "…" }`). The package is the nearest directory up from its file with a package.json.
|
|
26
|
+
*/
|
|
27
|
+
export declare namespace SlashCommandUtil {
|
|
28
|
+
/** The commands of the `/` menu, in pi's order (its `get_commands`), with their descriptions in other languages. */
|
|
29
|
+
function list({ commands, prompts, skills }: SlashCommandSources): TaSlashCommand[];
|
|
30
|
+
/**
|
|
31
|
+
* The names the commands of extensions are run by, as pi's extension runner gives them: a name two
|
|
32
|
+
* extensions register is `name:1`, `name:2`, in the order the extensions loaded.
|
|
33
|
+
*/
|
|
34
|
+
function invocationNames<C extends {
|
|
35
|
+
name: string;
|
|
36
|
+
}>(commands: readonly C[]): (C & {
|
|
37
|
+
invocationName: string;
|
|
38
|
+
})[];
|
|
39
|
+
function withDescriptions(commands: readonly TaSlashCommand[]): TaSlashCommand[];
|
|
40
|
+
/** `pi.descriptions` of the package `directory` is in, with only what is text. */
|
|
41
|
+
function descriptionsOf(directory: string): Record<string, Descriptions> | undefined;
|
|
42
|
+
}
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { dirname, isAbsolute, join } from 'node:path';
|
|
3
|
+
/**
|
|
4
|
+
* The descriptions in other languages of the commands of the `/` menu: `pi.descriptions` of the package.json
|
|
5
|
+
* of the package each command or skill comes from, by its name there (`loop`, `skill:ta-game`), each a
|
|
6
|
+
* string by language (`{ "zh-CN": "…" }`). The package is the nearest directory up from its file with a package.json.
|
|
7
|
+
*/
|
|
8
|
+
export var SlashCommandUtil;
|
|
9
|
+
(function (SlashCommandUtil) {
|
|
10
|
+
/** The commands of the `/` menu, in pi's order (its `get_commands`), with their descriptions in other languages. */
|
|
11
|
+
function list({ commands, prompts, skills }) {
|
|
12
|
+
return withDescriptions([
|
|
13
|
+
...commands.map(c => ({ name: c.invocationName, description: c.description, source: 'extension', sourceInfo: c.sourceInfo })),
|
|
14
|
+
...prompts.map(p => ({ name: p.name, description: p.description, source: 'prompt', sourceInfo: p.sourceInfo })),
|
|
15
|
+
...skills.map(s => ({ name: `skill:${s.name}`, description: s.description, source: 'skill', sourceInfo: s.sourceInfo })),
|
|
16
|
+
]);
|
|
17
|
+
}
|
|
18
|
+
SlashCommandUtil.list = list;
|
|
19
|
+
/**
|
|
20
|
+
* The names the commands of extensions are run by, as pi's extension runner gives them: a name two
|
|
21
|
+
* extensions register is `name:1`, `name:2`, in the order the extensions loaded.
|
|
22
|
+
*/
|
|
23
|
+
function invocationNames(commands) {
|
|
24
|
+
var _a;
|
|
25
|
+
const counts = new Map();
|
|
26
|
+
for (const command of commands) {
|
|
27
|
+
counts.set(command.name, ((_a = counts.get(command.name)) !== null && _a !== void 0 ? _a : 0) + 1);
|
|
28
|
+
}
|
|
29
|
+
const seen = new Map();
|
|
30
|
+
const taken = new Set();
|
|
31
|
+
return commands.map(command => {
|
|
32
|
+
var _a, _b;
|
|
33
|
+
const occurrence = ((_a = seen.get(command.name)) !== null && _a !== void 0 ? _a : 0) + 1;
|
|
34
|
+
seen.set(command.name, occurrence);
|
|
35
|
+
let invocationName = ((_b = counts.get(command.name)) !== null && _b !== void 0 ? _b : 0) > 1 ? `${command.name}:${occurrence}` : command.name;
|
|
36
|
+
for (let suffix = occurrence; taken.has(invocationName);) {
|
|
37
|
+
suffix++;
|
|
38
|
+
invocationName = `${command.name}:${suffix}`;
|
|
39
|
+
}
|
|
40
|
+
taken.add(invocationName);
|
|
41
|
+
return { ...command, invocationName };
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
SlashCommandUtil.invocationNames = invocationNames;
|
|
45
|
+
function withDescriptions(commands) {
|
|
46
|
+
const packages = new Map();
|
|
47
|
+
return commands.map(command => {
|
|
48
|
+
var _a;
|
|
49
|
+
const { path } = command.sourceInfo;
|
|
50
|
+
if (!isAbsolute(path)) {
|
|
51
|
+
return command;
|
|
52
|
+
}
|
|
53
|
+
const directory = dirname(path);
|
|
54
|
+
if (!packages.has(directory)) {
|
|
55
|
+
packages.set(directory, descriptionsOf(directory));
|
|
56
|
+
}
|
|
57
|
+
const descriptions = (_a = packages.get(directory)) === null || _a === void 0 ? void 0 : _a[command.name];
|
|
58
|
+
return descriptions ? { ...command, descriptions } : command;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
SlashCommandUtil.withDescriptions = withDescriptions;
|
|
62
|
+
/** `pi.descriptions` of the package `directory` is in, with only what is text. */
|
|
63
|
+
function descriptionsOf(directory) {
|
|
64
|
+
var _a, _b;
|
|
65
|
+
const root = packageRoot(directory);
|
|
66
|
+
if (!root) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
let declared;
|
|
70
|
+
try {
|
|
71
|
+
declared = (_b = (_a = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'))) === null || _a === void 0 ? void 0 : _a.pi) === null || _b === void 0 ? void 0 : _b.descriptions;
|
|
72
|
+
}
|
|
73
|
+
catch (_c) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
if (!isRecord(declared)) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
const byName = {};
|
|
80
|
+
for (const [name, texts] of Object.entries(declared)) {
|
|
81
|
+
const languages = isRecord(texts)
|
|
82
|
+
? Object.entries(texts).filter((entry) => typeof entry[1] === 'string' && entry[1].trim() !== '')
|
|
83
|
+
: [];
|
|
84
|
+
if (languages.length > 0) {
|
|
85
|
+
byName[name] = Object.fromEntries(languages);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return byName;
|
|
89
|
+
}
|
|
90
|
+
SlashCommandUtil.descriptionsOf = descriptionsOf;
|
|
91
|
+
function packageRoot(directory) {
|
|
92
|
+
for (let current = directory;; current = dirname(current)) {
|
|
93
|
+
if (existsSync(join(current, 'package.json'))) {
|
|
94
|
+
return current;
|
|
95
|
+
}
|
|
96
|
+
if (dirname(current) === current) {
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function isRecord(value) {
|
|
102
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
103
|
+
}
|
|
104
|
+
})(SlashCommandUtil || (SlashCommandUtil = {}));
|
|
105
|
+
//# sourceMappingURL=slash-command-util.js.map
|
package/esm/node/ta-access.d.ts
CHANGED
|
@@ -21,6 +21,14 @@ export declare namespace TaAccessUtil {
|
|
|
21
21
|
* calls on its WebSocket only, where a call knows its connection (attach, terminals) and gets pushes.
|
|
22
22
|
*/
|
|
23
23
|
function isHttpCall(request: IncomingMessage): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* What starts this ta serve (`ta serve --host --port --exec`, the desktop app, the gateway) and is read
|
|
26
|
+
* once it listens. Left in its environment, every program of its sessions and terminals would take it: a
|
|
27
|
+
* `ta serve` or a Cell app run there would listen on this one's port, a Ta reached from there would take its token.
|
|
28
|
+
*/
|
|
29
|
+
const SERVE_ENV: string[];
|
|
30
|
+
/** Removes {@link SERVE_ENV} from `env`, for what this process starts. */
|
|
31
|
+
function releaseServeEnv(env: NodeJS.ProcessEnv): void;
|
|
24
32
|
/** The token goes in the fragment, which the browser does not send to the server or in `Referer`. */
|
|
25
33
|
function webUrl(httpUrl: string, token: string): string;
|
|
26
34
|
/**
|
|
@@ -40,6 +48,9 @@ export declare namespace TaAccessUtil {
|
|
|
40
48
|
export declare class TaAccess implements RpcWebSocketGuard, Middleware, ServerAware, ApplicationLifecycle<Application> {
|
|
41
49
|
readonly priority = 10000;
|
|
42
50
|
readonly token: string;
|
|
51
|
+
/** Read before the environment lets them go (TaAccessUtil.releaseServeEnv). */
|
|
52
|
+
protected readonly chosenToken: boolean;
|
|
53
|
+
protected readonly openWeb: boolean;
|
|
43
54
|
protected readonly host?: string;
|
|
44
55
|
protected readonly sessions: TaSessionRegistry;
|
|
45
56
|
protected readonly signIn: TaSignIn;
|
package/esm/node/ta-access.js
CHANGED
|
@@ -65,6 +65,27 @@ export var TaAccessUtil;
|
|
|
65
65
|
return request.method === 'POST' && path.startsWith(`${API_PATH}/`);
|
|
66
66
|
}
|
|
67
67
|
TaAccessUtil.isHttpCall = isHttpCall;
|
|
68
|
+
/**
|
|
69
|
+
* What starts this ta serve (`ta serve --host --port --exec`, the desktop app, the gateway) and is read
|
|
70
|
+
* once it listens. Left in its environment, every program of its sessions and terminals would take it: a
|
|
71
|
+
* `ta serve` or a Cell app run there would listen on this one's port, a Ta reached from there would take its token.
|
|
72
|
+
*/
|
|
73
|
+
TaAccessUtil.SERVE_ENV = [
|
|
74
|
+
'CELL_SERVER__HOST',
|
|
75
|
+
'CELL_SERVER__PORT',
|
|
76
|
+
'CELL_TA__EXEC__URL',
|
|
77
|
+
'CELL_TA__EXEC__TOKEN',
|
|
78
|
+
'TA_EXEC_TOKEN',
|
|
79
|
+
'TA_SERVE_TOKEN',
|
|
80
|
+
'TA_SERVE_OPEN_WEB',
|
|
81
|
+
];
|
|
82
|
+
/** Removes {@link SERVE_ENV} from `env`, for what this process starts. */
|
|
83
|
+
function releaseServeEnv(env) {
|
|
84
|
+
for (const key of TaAccessUtil.SERVE_ENV) {
|
|
85
|
+
delete env[key];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
TaAccessUtil.releaseServeEnv = releaseServeEnv;
|
|
68
89
|
/** The token goes in the fragment, which the browser does not send to the server or in `Referer`. */
|
|
69
90
|
function webUrl(httpUrl, token) {
|
|
70
91
|
return `${httpUrl}/#token=${token}`;
|
|
@@ -106,6 +127,9 @@ let TaAccess = class TaAccess {
|
|
|
106
127
|
constructor() {
|
|
107
128
|
this.priority = ACCESS_MIDDLEWARE_PRIORITY;
|
|
108
129
|
this.token = process.env.TA_SERVE_TOKEN || TaServeStateUtil.read().token || randomBytes(32).toString('base64url');
|
|
130
|
+
/** Read before the environment lets them go (TaAccessUtil.releaseServeEnv). */
|
|
131
|
+
this.chosenToken = !!process.env.TA_SERVE_TOKEN;
|
|
132
|
+
this.openWeb = !!process.env.TA_SERVE_OPEN_WEB;
|
|
109
133
|
this.infoFile = join(TaBrand.agentDir(), TA_SERVE_FILE);
|
|
110
134
|
}
|
|
111
135
|
allows(request) {
|
|
@@ -159,16 +183,18 @@ let TaAccess = class TaAccess {
|
|
|
159
183
|
chmodSync(this.infoFile, 0o600);
|
|
160
184
|
// A token that a parent process chose is its to keep.
|
|
161
185
|
const kept = TaServeStateUtil.read().token;
|
|
162
|
-
TaServeStateUtil.write({ port, token:
|
|
186
|
+
TaServeStateUtil.write({ port, token: this.chosenToken ? kept : this.token });
|
|
187
|
+
// Read: what the sessions and terminals start does not take it.
|
|
188
|
+
TaAccessUtil.releaseServeEnv(process.env);
|
|
163
189
|
console.log(`ta serve: ${info.url} (token in ${this.infoFile})`);
|
|
164
|
-
if (
|
|
190
|
+
if (this.openWeb) {
|
|
165
191
|
const url = TaAccessUtil.webUrl(`http://${origin}`, this.token);
|
|
166
192
|
console.log(`ta serve: web client at ${url}`);
|
|
167
193
|
TaAccessUtil.openBrowser(url);
|
|
168
194
|
}
|
|
169
195
|
else if (!TaAccessUtil.isLoopbackHost(this.host)) {
|
|
170
196
|
// Reached from elsewhere (a container, a server): whoever started it opens the link to sign in.
|
|
171
|
-
for (const line of TaAccessUtil.signInLines(this.signIn, port,
|
|
197
|
+
for (const line of TaAccessUtil.signInLines(this.signIn, port, this.chosenToken ? undefined : this.token)) {
|
|
172
198
|
console.log(`ta serve: ${line}`);
|
|
173
199
|
}
|
|
174
200
|
}
|
package/esm/node/ta-errors.js
CHANGED
|
@@ -5,7 +5,18 @@ const TaError = ApplicationError.declare(TA_RPC_ERROR_CODE, (code, message, hint
|
|
|
5
5
|
data: { code, hint },
|
|
6
6
|
}));
|
|
7
7
|
/** What pi says when no provider is signed in for the model, or no model can be used. */
|
|
8
|
-
const NOT_SIGNED_IN = /^No (API key found for|model selected|models available)\b/;
|
|
8
|
+
const NOT_SIGNED_IN = /^(No (API key found for|model selected|models available)|Authentication failed for)\b/;
|
|
9
|
+
/** What else pi says that a client words for the user by its code, as pi's messages are English. */
|
|
10
|
+
const KNOWN = [
|
|
11
|
+
{ pattern: /^Nothing to compact\b/, code: TaErrorCode.NOTHING_TO_COMPACT, hint: 'Compact once the conversation is longer.' },
|
|
12
|
+
{ pattern: /^Already compacted$/, code: TaErrorCode.ALREADY_COMPACTED, hint: 'Compact again once more has been said.' },
|
|
13
|
+
{
|
|
14
|
+
pattern: /^(Cannot submit a prompt while compaction is in progress|Wait for the current (response|compaction))\b/,
|
|
15
|
+
code: TaErrorCode.SESSION_BUSY,
|
|
16
|
+
hint: 'Try again once the session is idle.',
|
|
17
|
+
},
|
|
18
|
+
{ pattern: /^Compaction cancelled$/, code: TaErrorCode.CANCELLED, hint: 'It was stopped; nothing to do.' },
|
|
19
|
+
];
|
|
9
20
|
/** The failures of calls of the services of ta serve ({@link TaServer} and the others), which reach the client with their message and code. */
|
|
10
21
|
export var TaErrors;
|
|
11
22
|
(function (TaErrors) {
|
|
@@ -51,7 +62,11 @@ export var TaErrors;
|
|
|
51
62
|
const message = cause instanceof Error ? cause.message : String(cause);
|
|
52
63
|
if (NOT_SIGNED_IN.test(message)) {
|
|
53
64
|
// pi's own words go on about its /login and the paths of its documents, which a client does not have.
|
|
54
|
-
return TaError(TaErrorCode.NOT_SIGNED_IN, message.split(/\n| Use \/login/)[0].trim(), 'Sign in to a provider of models (TaAuthServer.login), then send it again.');
|
|
65
|
+
return TaError(TaErrorCode.NOT_SIGNED_IN, message.split(/\n| (Use |Run ')\/login/)[0].trim(), 'Sign in to a provider of models (TaAuthServer.login), then send it again.');
|
|
66
|
+
}
|
|
67
|
+
const known = KNOWN.find(({ pattern }) => pattern.test(message));
|
|
68
|
+
if (known) {
|
|
69
|
+
return TaError(known.code, message, known.hint);
|
|
55
70
|
}
|
|
56
71
|
return TaError(TaErrorCode.COMMAND_FAILED, message, 'The message comes from the agent; fix what it says and send the command again.');
|
|
57
72
|
}
|
|
@@ -1,11 +1,28 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type TaConversation, type TaEvent, type TaMessage } from '@celljs/ta-protocol';
|
|
2
2
|
type AgentSessionEvent = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' } }).AgentSessionEvent;
|
|
3
3
|
export declare namespace TaEventUtil {
|
|
4
4
|
/**
|
|
5
5
|
* An event of pi as it goes on the wire, the JSON form of pi's RPC and JSON modes (pi does not export
|
|
6
6
|
* its function). pi's `message_update` holds the whole message so far: sent as is, a reply would cost
|
|
7
|
-
* the square of its length.
|
|
7
|
+
* the square of its length. The images of tool results go without their bytes (`leanMessage`).
|
|
8
8
|
*/
|
|
9
9
|
function toWire(event: AgentSessionEvent): TaEvent;
|
|
10
|
+
/**
|
|
11
|
+
* A message as clients get it: a tool's result keeps its images' kind but not their bytes (`data` is
|
|
12
|
+
* empty). Clients do not show them (the model reads them), and screenshots are most of the bytes of a
|
|
13
|
+
* conversation that has them: on a slow network a page of it would be one picture nobody sees.
|
|
14
|
+
*/
|
|
15
|
+
function leanMessage(message: TaMessage): TaMessage;
|
|
16
|
+
/** The result of a tool, or a message of one, without the bytes of its images. */
|
|
17
|
+
function leanResult<T>(result: T): T;
|
|
18
|
+
/**
|
|
19
|
+
* A conversation as clients get it: its messages and the results of its calls lean, and the images of the
|
|
20
|
+
* messages of the user without their bytes but with their size (`TaShownImage`): they come with
|
|
21
|
+
* `getMessageImage` once shown, and a conversation with pictures opens as fast as one without. Events keep
|
|
22
|
+
* them: a message just sent shows on every device at once.
|
|
23
|
+
*/
|
|
24
|
+
function leanConversation(conversation: TaConversation): TaConversation;
|
|
25
|
+
/** A message as a page of the conversation gives it: `leanMessage`, and the images of the user's without their bytes. */
|
|
26
|
+
function leanShown(message: TaMessage): TaMessage;
|
|
10
27
|
}
|
|
11
28
|
export {};
|
|
@@ -1,14 +1,34 @@
|
|
|
1
|
+
import { TaImageSize } from '@celljs/ta-protocol';
|
|
2
|
+
/** The lean form of each message and result met, made once: the pages of a conversation measure it by its object. */
|
|
3
|
+
const leaned = new WeakMap();
|
|
1
4
|
export var TaEventUtil;
|
|
2
5
|
(function (TaEventUtil) {
|
|
3
6
|
/**
|
|
4
7
|
* An event of pi as it goes on the wire, the JSON form of pi's RPC and JSON modes (pi does not export
|
|
5
8
|
* its function). pi's `message_update` holds the whole message so far: sent as is, a reply would cost
|
|
6
|
-
* the square of its length.
|
|
9
|
+
* the square of its length. The images of tool results go without their bytes (`leanMessage`).
|
|
7
10
|
*/
|
|
8
11
|
function toWire(event) {
|
|
9
12
|
var _a;
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
switch (event.type) {
|
|
14
|
+
case 'message_update':
|
|
15
|
+
break;
|
|
16
|
+
case 'message_start':
|
|
17
|
+
case 'message_end':
|
|
18
|
+
return withLean(event, 'message', leanMessage(event.message));
|
|
19
|
+
case 'tool_execution_update':
|
|
20
|
+
return withLean(event, 'partialResult', leanResult(event.partialResult));
|
|
21
|
+
case 'tool_execution_end':
|
|
22
|
+
return withLean(event, 'result', leanResult(event.result));
|
|
23
|
+
case 'turn_end': {
|
|
24
|
+
const message = leanMessage(event.message);
|
|
25
|
+
const toolResults = leanAll(event.toolResults);
|
|
26
|
+
return (message === event.message && toolResults === event.toolResults ? event : { ...event, message, toolResults });
|
|
27
|
+
}
|
|
28
|
+
case 'agent_end':
|
|
29
|
+
return withLean(event, 'messages', leanAll(event.messages));
|
|
30
|
+
default:
|
|
31
|
+
return event;
|
|
12
32
|
}
|
|
13
33
|
const { partial, ...delta } = event.assistantMessageEvent;
|
|
14
34
|
const block = delta.type === 'toolcall_start' ? partial === null || partial === void 0 ? void 0 : partial.content[(_a = delta.contentIndex) !== null && _a !== void 0 ? _a : -1] : undefined;
|
|
@@ -16,5 +36,79 @@ export var TaEventUtil;
|
|
|
16
36
|
return { type: 'message_update', usage: event.message.usage, assistantMessageEvent };
|
|
17
37
|
}
|
|
18
38
|
TaEventUtil.toWire = toWire;
|
|
39
|
+
/**
|
|
40
|
+
* A message as clients get it: a tool's result keeps its images' kind but not their bytes (`data` is
|
|
41
|
+
* empty). Clients do not show them (the model reads them), and screenshots are most of the bytes of a
|
|
42
|
+
* conversation that has them: on a slow network a page of it would be one picture nobody sees.
|
|
43
|
+
*/
|
|
44
|
+
function leanMessage(message) {
|
|
45
|
+
return (message === null || message === void 0 ? void 0 : message.role) === 'toolResult' ? leanResult(message) : message;
|
|
46
|
+
}
|
|
47
|
+
TaEventUtil.leanMessage = leanMessage;
|
|
48
|
+
/** The result of a tool, or a message of one, without the bytes of its images. */
|
|
49
|
+
function leanResult(result) {
|
|
50
|
+
const content = result === null || result === void 0 ? void 0 : result.content;
|
|
51
|
+
if (!result || typeof result !== 'object' || !Array.isArray(content)) {
|
|
52
|
+
return result;
|
|
53
|
+
}
|
|
54
|
+
const known = leaned.get(result);
|
|
55
|
+
if (known) {
|
|
56
|
+
return known;
|
|
57
|
+
}
|
|
58
|
+
const heavy = content.some(block => (block === null || block === void 0 ? void 0 : block.type) === 'image' && block.data);
|
|
59
|
+
const lean = heavy
|
|
60
|
+
? {
|
|
61
|
+
...result,
|
|
62
|
+
content: content.map(block => (block === null || block === void 0 ? void 0 : block.type) === 'image' ? { type: 'image', mimeType: block.mimeType, data: '' } : block),
|
|
63
|
+
}
|
|
64
|
+
: result;
|
|
65
|
+
leaned.set(result, lean);
|
|
66
|
+
return lean;
|
|
67
|
+
}
|
|
68
|
+
TaEventUtil.leanResult = leanResult;
|
|
69
|
+
/**
|
|
70
|
+
* A conversation as clients get it: its messages and the results of its calls lean, and the images of the
|
|
71
|
+
* messages of the user without their bytes but with their size (`TaShownImage`): they come with
|
|
72
|
+
* `getMessageImage` once shown, and a conversation with pictures opens as fast as one without. Events keep
|
|
73
|
+
* them: a message just sent shows on every device at once.
|
|
74
|
+
*/
|
|
75
|
+
function leanConversation(conversation) {
|
|
76
|
+
const tools = {};
|
|
77
|
+
for (const [id, run] of Object.entries(conversation.tools)) {
|
|
78
|
+
tools[id] = run.result === undefined ? run : { ...run, result: leanResult(run.result) };
|
|
79
|
+
}
|
|
80
|
+
return { ...conversation, messages: leanAll(conversation.messages, leanShown), tools };
|
|
81
|
+
}
|
|
82
|
+
TaEventUtil.leanConversation = leanConversation;
|
|
83
|
+
/** A message as a page of the conversation gives it: `leanMessage`, and the images of the user's without their bytes. */
|
|
84
|
+
function leanShown(message) {
|
|
85
|
+
const { role, content } = message;
|
|
86
|
+
if (role !== 'user' || !Array.isArray(content)) {
|
|
87
|
+
return leanMessage(message);
|
|
88
|
+
}
|
|
89
|
+
const known = leaned.get(message);
|
|
90
|
+
if (known) {
|
|
91
|
+
return known;
|
|
92
|
+
}
|
|
93
|
+
const heavy = content.some(block => (block === null || block === void 0 ? void 0 : block.type) === 'image' && block.data);
|
|
94
|
+
const lean = heavy ? { ...message, content: content.map(block => ((block === null || block === void 0 ? void 0 : block.type) === 'image' ? shown(block) : block)) } : message;
|
|
95
|
+
leaned.set(message, lean);
|
|
96
|
+
return lean;
|
|
97
|
+
}
|
|
98
|
+
TaEventUtil.leanShown = leanShown;
|
|
99
|
+
function shown(block) {
|
|
100
|
+
var _a;
|
|
101
|
+
return { type: 'image', mimeType: (_a = block.mimeType) !== null && _a !== void 0 ? _a : '', data: '', ...(block.data ? TaImageSize.of(block.data) : undefined) };
|
|
102
|
+
}
|
|
103
|
+
function leanAll(messages, lean = leanMessage) {
|
|
104
|
+
if (!Array.isArray(messages)) {
|
|
105
|
+
return messages;
|
|
106
|
+
}
|
|
107
|
+
const leant = messages.map(lean);
|
|
108
|
+
return leant.every((message, index) => message === messages[index]) ? messages : leant;
|
|
109
|
+
}
|
|
110
|
+
function withLean(event, key, value) {
|
|
111
|
+
return (value === event[key] ? event : { ...event, [key]: value });
|
|
112
|
+
}
|
|
19
113
|
})(TaEventUtil || (TaEventUtil = {}));
|
|
20
114
|
//# sourceMappingURL=ta-event-util.js.map
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
2
|
-
import { type RpcExtensionUIResponse, type TaAttachOptions, type TaAttachResult, type TaClientToolResult, type TaCommandResult, type TaForkFileResult, type TaMessagePage, type TaNavigateResult, type TaOpenSessionOptions, type TaPromptInput, type TaPromptReceipt, type TaSavedSessionInfo, TaServer, type TaServerInfo, type TaSessionInfo, type ThinkingLevel } from '@celljs/ta-protocol';
|
|
3
|
-
import { TaSessionCatalog, TaSessionRegistry } from './host-protocol.js';
|
|
4
|
-
import { RemoteHosts } from './remote/remote-protocol.js';
|
|
2
|
+
import { type ImageContent, type RpcExtensionUIResponse, type TaAttachOptions, type TaAttachResult, type TaClientToolResult, type TaCommandResult, type TaForkFileResult, type TaMessagePage, type TaNavigateResult, type TaOpenedSession, type TaOpenSessionOptions, type TaPromptInput, type TaPromptReceipt, type TaSavedSessionInfo, type TaSavedSessionQuery, TaServer, type TaServerInfo, type TaSessionInfo, type TaSlashCommand, type ThinkingLevel } from '@celljs/ta-protocol';
|
|
3
|
+
import { TaSessionCatalog, TaSessionFactory, TaSessionRegistry } from './host-protocol.js';
|
|
5
4
|
import type { TaSession } from './ta-session.js';
|
|
6
5
|
import { TaWorkingDirectory } from './working-directory.js';
|
|
7
6
|
/**
|
|
@@ -13,16 +12,18 @@ export declare class TaServerImpl implements TaServer {
|
|
|
13
12
|
protected readonly sessions: TaSessionRegistry;
|
|
14
13
|
protected readonly environments: ExecEnvironmentFactory;
|
|
15
14
|
protected readonly workingDirectory: TaWorkingDirectory;
|
|
16
|
-
protected readonly remoteHosts: RemoteHosts;
|
|
17
15
|
protected readonly catalog: TaSessionCatalog;
|
|
16
|
+
protected readonly factory: TaSessionFactory;
|
|
18
17
|
getServerInfo(): Promise<TaServerInfo>;
|
|
19
18
|
setWorkingDirectory(cwd: string): Promise<TaServerInfo>;
|
|
20
|
-
openSession(options: TaOpenSessionOptions): Promise<
|
|
19
|
+
openSession({ attach, ...options }: TaOpenSessionOptions): Promise<TaOpenedSession>;
|
|
21
20
|
closeSession(sessionId: string): Promise<void>;
|
|
22
21
|
listSessions(): Promise<TaSessionInfo[]>;
|
|
23
|
-
|
|
22
|
+
watchSessions(): Promise<TaSessionInfo[]>;
|
|
23
|
+
listSavedSessions(query?: TaSavedSessionQuery): Promise<TaSavedSessionInfo[]>;
|
|
24
24
|
attach(sessionId: string, afterSeq?: number, options?: TaAttachOptions): Promise<TaAttachResult>;
|
|
25
25
|
getEarlierMessages(sessionId: string, before: number | undefined, timestamp: number | undefined, budget: number): Promise<TaMessagePage>;
|
|
26
|
+
getMessageImage(sessionId: string, timestamp: number, index: number): Promise<ImageContent | undefined>;
|
|
26
27
|
detach(sessionId: string): Promise<void>;
|
|
27
28
|
respondUi(sessionId: string, response: RpcExtensionUIResponse): Promise<boolean>;
|
|
28
29
|
claimClientTool(sessionId: string, id: string): Promise<boolean>;
|
|
@@ -62,7 +63,12 @@ export declare class TaServerImpl implements TaServer {
|
|
|
62
63
|
getLastAssistantText(sessionId: string): Promise<TaCommandResult<'get_last_assistant_text'>>;
|
|
63
64
|
setSessionName(sessionId: string, name: string): Promise<void>;
|
|
64
65
|
getMessages(sessionId: string): Promise<TaCommandResult<'get_messages'>>;
|
|
65
|
-
getCommands(sessionId: string): Promise<
|
|
66
|
+
getCommands(sessionId: string): Promise<{
|
|
67
|
+
commands: TaSlashCommand[];
|
|
68
|
+
}>;
|
|
69
|
+
getDirectoryCommands(cwd: string): Promise<{
|
|
70
|
+
commands: TaSlashCommand[];
|
|
71
|
+
}>;
|
|
66
72
|
reload(sessionId: string): Promise<void>;
|
|
67
73
|
protected on<T>(sessionId: string, command: (session: TaSession) => T | Promise<T>): Promise<T>;
|
|
68
74
|
protected guard<T>(command: () => Promise<T>): Promise<T>;
|