@celljs/ta-host 4.16.0 → 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/extension-providers.d.ts +16 -1
- package/esm/node/extension-providers.js +23 -3
- package/esm/node/index.d.ts +6 -0
- package/esm/node/index.js +6 -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/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 +22 -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/extension-providers.d.ts +16 -1
- package/lib/node/extension-providers.js +23 -3
- package/lib/node/index.d.ts +6 -0
- package/lib/node/index.js +6 -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/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 +22 -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
|
@@ -16,6 +16,7 @@ import { RpcContext } from '@celljs/rpc/lib/node';
|
|
|
16
16
|
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
17
17
|
import { TaServeRestart } from '@celljs/ta-harness/lib/node/serve-restart';
|
|
18
18
|
import { TA_OWNER, TaMessageAuthor, TaServer, } from '@celljs/ta-protocol';
|
|
19
|
+
import { TaDirectoryRelocator } from './directory-relocator.js';
|
|
19
20
|
import { TaSessionCatalog, TaSessionFactory, TaSessionRegistry } from './host-protocol.js';
|
|
20
21
|
import { TaServeUpdates } from './serve-updates.js';
|
|
21
22
|
import { SessionInfoUtil } from './session-info-util.js';
|
|
@@ -315,6 +316,23 @@ let TaServerImpl = class TaServerImpl {
|
|
|
315
316
|
return { sessionFile, text };
|
|
316
317
|
});
|
|
317
318
|
}
|
|
319
|
+
forkHere(sessionId) {
|
|
320
|
+
return this.on(sessionId, async ({ pi }) => {
|
|
321
|
+
var _a;
|
|
322
|
+
var _b;
|
|
323
|
+
const file = pi.sessionManager.getSessionFile();
|
|
324
|
+
if (!file || !existsSync(file)) {
|
|
325
|
+
return {};
|
|
326
|
+
}
|
|
327
|
+
const sdk = await PiSdkUtil.load();
|
|
328
|
+
const sessionFile = sdk.SessionManager.forkFrom(file, pi.sessionManager.getCwd()).getSessionFile();
|
|
329
|
+
this.runs.created(sessionFile, (_b = (_a = this.sessions.locate(sessionId)) === null || _a === void 0 ? void 0 : _a.cwd) !== null && _b !== void 0 ? _b : '');
|
|
330
|
+
return { sessionFile };
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
relocateDirectory(from, to) {
|
|
334
|
+
return this.guard(() => this.relocator.relocate(from, to));
|
|
335
|
+
}
|
|
318
336
|
clone(sessionId) {
|
|
319
337
|
return this.authored(sessionId, async ({ pi, runtime }) => {
|
|
320
338
|
const leafId = pi.sessionManager.getLeafId();
|
|
@@ -435,6 +453,10 @@ __decorate([
|
|
|
435
453
|
Autowired(TaServeUpdates),
|
|
436
454
|
__metadata("design:type", Object)
|
|
437
455
|
], TaServerImpl.prototype, "updates", void 0);
|
|
456
|
+
__decorate([
|
|
457
|
+
Autowired(TaDirectoryRelocator),
|
|
458
|
+
__metadata("design:type", Object)
|
|
459
|
+
], TaServerImpl.prototype, "relocator", void 0);
|
|
438
460
|
TaServerImpl = __decorate([
|
|
439
461
|
Rpc(TaServer)
|
|
440
462
|
], TaServerImpl);
|
|
@@ -7,6 +7,7 @@ import { type TaHostOptions, TaSessionFactory } from './host-protocol.js';
|
|
|
7
7
|
import { PackageHosts } from './package-hosts.js';
|
|
8
8
|
import { PiDirectories } from './remote/remote-protocol.js';
|
|
9
9
|
import { TaSession } from './ta-session.js';
|
|
10
|
+
import { TaWorkingDirectory } from './working-directory.js';
|
|
10
11
|
type AgentsFilesOverride = (base: {
|
|
11
12
|
agentsFiles: ContextFile[];
|
|
12
13
|
}) => {
|
|
@@ -20,6 +21,8 @@ export declare namespace SessionFileUtil {
|
|
|
20
21
|
* not there, and pi would take the directory ta serve runs in, the home directory under the desktop app.
|
|
21
22
|
*/
|
|
22
23
|
function cwdOverride(sessionFile: string, cwd: string): string | undefined;
|
|
24
|
+
/** `path` when it is a directory there now. */
|
|
25
|
+
function existing(path: string | undefined): string | undefined;
|
|
23
26
|
}
|
|
24
27
|
type ModelRuntime = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' } }).ModelRuntime;
|
|
25
28
|
type AgentSessionServices = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' } }).AgentSessionServices;
|
|
@@ -43,6 +46,7 @@ export declare class DefaultTaSessionFactory implements TaSessionFactory {
|
|
|
43
46
|
protected readonly tools: AgentTool[];
|
|
44
47
|
protected readonly packageHosts: PackageHosts;
|
|
45
48
|
protected readonly directories: PiDirectories;
|
|
49
|
+
protected readonly workingDirectory?: TaWorkingDirectory;
|
|
46
50
|
protected readonly extensionProviders: ExtensionProviders;
|
|
47
51
|
create(requested: TaOpenSessionOptions, requestClose: (session: TaSession, idle?: boolean) => void, id?: string): Promise<TaSession>;
|
|
48
52
|
commands(cwd: string): Promise<TaSlashCommand[]>;
|
|
@@ -51,7 +55,15 @@ export declare class DefaultTaSessionFactory implements TaSessionFactory {
|
|
|
51
55
|
* those of Ta. `commandsOnly`: for what a session would have, the context files of another machine not read.
|
|
52
56
|
*/
|
|
53
57
|
protected services(sdk: PiSdk, options: ServicesOptions): Promise<AgentSessionServices>;
|
|
54
|
-
|
|
58
|
+
/**
|
|
59
|
+
* pi's manager of the session file asked, or of a new one. A file whose directory is no longer there goes on
|
|
60
|
+
* in the directory of the server (TaWorkingDirectory), else the home directory, as pi's Continue: `movedFrom`, the directory as
|
|
61
|
+
* the file says it.
|
|
62
|
+
*/
|
|
63
|
+
protected openSessionManager(sdk: PiSdk, options: TaOpenSessionOptions): {
|
|
64
|
+
sessionManager: SessionManager;
|
|
65
|
+
movedFrom?: string;
|
|
66
|
+
};
|
|
55
67
|
/**
|
|
56
68
|
* For another machine, its context files (AGENTS.md...) instead of those pi finds above the directory
|
|
57
69
|
* of this machine it works in; the user's own, in the agent directory, stay.
|
|
@@ -7,13 +7,15 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
|
-
import { statSync } from 'node:fs';
|
|
10
|
+
import { existsSync, statSync } from 'node:fs';
|
|
11
|
+
import { homedir } from 'node:os';
|
|
11
12
|
import { join, resolve, sep } from 'node:path';
|
|
12
13
|
import { AgentExtension, AgentTool, AgentToolUtil, CODING_AGENT_OPTIONS, ModelRuntimeProvider, PiSdkUtil, } from '@celljs/ai-coding-agent';
|
|
13
14
|
import { Autowired, Component, Optional, Value } from '@celljs/core';
|
|
14
15
|
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
15
16
|
import { BrandExtension, ExecExtension, ExecMcpTransport, RemoteContextFiles, SessionLock, SessionLockExtension, TaExtensionUtil, TaPackages, } from '@celljs/ta-harness';
|
|
16
17
|
import { ClientToolBroker } from './client-tool-broker.js';
|
|
18
|
+
import { DelegateExtension } from './delegate-extension.js';
|
|
17
19
|
import { ExtensionProviders } from './extension-providers.js';
|
|
18
20
|
import { TA_HOST_OPTIONS, TaSessionFactory } from './host-protocol.js';
|
|
19
21
|
import { PackageHostExtension } from './package-host-extension.js';
|
|
@@ -24,6 +26,7 @@ import { ExecSessionFiles } from './session-files.js';
|
|
|
24
26
|
import { SlashCommandUtil } from './slash-command-util.js';
|
|
25
27
|
import { TaErrors } from './ta-errors.js';
|
|
26
28
|
import { TaSession } from './ta-session.js';
|
|
29
|
+
import { TaWorkingDirectory } from './working-directory.js';
|
|
27
30
|
export var SessionFileUtil;
|
|
28
31
|
(function (SessionFileUtil) {
|
|
29
32
|
/**
|
|
@@ -41,6 +44,16 @@ export var SessionFileUtil;
|
|
|
41
44
|
}
|
|
42
45
|
}
|
|
43
46
|
SessionFileUtil.cwdOverride = cwdOverride;
|
|
47
|
+
/** `path` when it is a directory there now. */
|
|
48
|
+
function existing(path) {
|
|
49
|
+
try {
|
|
50
|
+
return path && statSync(path).isDirectory() ? path : undefined;
|
|
51
|
+
}
|
|
52
|
+
catch (_a) {
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
SessionFileUtil.existing = existing;
|
|
44
57
|
})(SessionFileUtil || (SessionFileUtil = {}));
|
|
45
58
|
/** Sets up pi for each session the way its command does, with one model runtime for all of them. */
|
|
46
59
|
let DefaultTaSessionFactory = class DefaultTaSessionFactory {
|
|
@@ -57,7 +70,7 @@ let DefaultTaSessionFactory = class DefaultTaSessionFactory {
|
|
|
57
70
|
const modelRuntime = await this.modelRuntimeProvider.provide();
|
|
58
71
|
// `options.cwd` is a location; pi works in a directory of this machine (PiDirectories). A session
|
|
59
72
|
// file works where its header says, as pi's `--session` does, whatever `cwd` the caller gave.
|
|
60
|
-
const sessionManager = this.openSessionManager(sdk, { ...requested, cwd: this.directories.toPi(requested.cwd) });
|
|
73
|
+
const { sessionManager, movedFrom } = this.openSessionManager(sdk, { ...requested, cwd: this.directories.toPi(requested.cwd) });
|
|
61
74
|
// Opened again from its file without its role or group (a package relaying a message to it, the list of
|
|
62
75
|
// conversations): those it was opened with before.
|
|
63
76
|
const entries = requested.sessionFile ? sessionManager.getEntries() : [];
|
|
@@ -101,6 +114,7 @@ let DefaultTaSessionFactory = class DefaultTaSessionFactory {
|
|
|
101
114
|
group: options.group,
|
|
102
115
|
role: options.appendSystemPrompt,
|
|
103
116
|
locate: cwd => this.directories.fromPi(cwd),
|
|
117
|
+
movedFrom,
|
|
104
118
|
});
|
|
105
119
|
await session.start();
|
|
106
120
|
return session;
|
|
@@ -153,12 +167,27 @@ let DefaultTaSessionFactory = class DefaultTaSessionFactory {
|
|
|
153
167
|
},
|
|
154
168
|
});
|
|
155
169
|
}
|
|
170
|
+
/**
|
|
171
|
+
* pi's manager of the session file asked, or of a new one. A file whose directory is no longer there goes on
|
|
172
|
+
* in the directory of the server (TaWorkingDirectory), else the home directory, as pi's Continue: `movedFrom`, the directory as
|
|
173
|
+
* the file says it.
|
|
174
|
+
*/
|
|
156
175
|
openSessionManager(sdk, options) {
|
|
176
|
+
var _a, _b;
|
|
177
|
+
var _c;
|
|
157
178
|
if (!options.sessionFile) {
|
|
158
|
-
return sdk.SessionManager.create(options.cwd);
|
|
179
|
+
return { sessionManager: sdk.SessionManager.create(options.cwd) };
|
|
159
180
|
}
|
|
160
181
|
try {
|
|
161
|
-
|
|
182
|
+
const sessionManager = sdk.SessionManager.open(options.sessionFile, undefined, SessionFileUtil.cwdOverride(options.sessionFile, options.cwd));
|
|
183
|
+
const cwd = sessionManager.getCwd();
|
|
184
|
+
if (existsSync(cwd)) {
|
|
185
|
+
return { sessionManager };
|
|
186
|
+
}
|
|
187
|
+
const here = (_c = SessionFileUtil.existing((_a = this.workingDirectory) === null || _a === void 0 ? void 0 : _a.get())) !== null && _c !== void 0 ? _c : homedir();
|
|
188
|
+
// As its file says it: pi resolves a path of another system (C:\...) against the directory of ta serve.
|
|
189
|
+
const movedFrom = ((_b = sessionManager.getHeader()) === null || _b === void 0 ? void 0 : _b.cwd) || cwd;
|
|
190
|
+
return { sessionManager: sdk.SessionManager.open(options.sessionFile, undefined, here), movedFrom };
|
|
162
191
|
}
|
|
163
192
|
catch (error) {
|
|
164
193
|
throw TaErrors.invalidArgument(`Cannot open the session ${options.sessionFile}: ${error.message}`, 'Pass a session file that exists; listSessions shows the open ones.');
|
|
@@ -182,6 +211,7 @@ let DefaultTaSessionFactory = class DefaultTaSessionFactory {
|
|
|
182
211
|
new SessionLockExtension(lock),
|
|
183
212
|
new PackageHostExtension(this.packageHosts, new ExecSessionFiles(environment, piCwd)),
|
|
184
213
|
new BrandExtension(sdk),
|
|
214
|
+
new DelegateExtension(),
|
|
185
215
|
...(environment.local ? [] : [new ExecExtension(environment, sdk, piCwd)]),
|
|
186
216
|
];
|
|
187
217
|
return [
|
|
@@ -258,6 +288,11 @@ __decorate([
|
|
|
258
288
|
Autowired(PiDirectories),
|
|
259
289
|
__metadata("design:type", Object)
|
|
260
290
|
], DefaultTaSessionFactory.prototype, "directories", void 0);
|
|
291
|
+
__decorate([
|
|
292
|
+
Autowired(TaWorkingDirectory),
|
|
293
|
+
Optional(),
|
|
294
|
+
__metadata("design:type", Object)
|
|
295
|
+
], DefaultTaSessionFactory.prototype, "workingDirectory", void 0);
|
|
261
296
|
__decorate([
|
|
262
297
|
Autowired(ExtensionProviders),
|
|
263
298
|
__metadata("design:type", Object)
|
package/esm/node/ta-session.d.ts
CHANGED
|
@@ -26,6 +26,8 @@ export interface TaSessionContext {
|
|
|
26
26
|
role?: string;
|
|
27
27
|
/** The location of the directory pi works in (PiDirectories.fromPi); default: that directory. */
|
|
28
28
|
locate?(cwd: string): string;
|
|
29
|
+
/** The directory its file says, no longer there: it works in another (TaSessionInfo.movedFrom). */
|
|
30
|
+
movedFrom?: string;
|
|
29
31
|
}
|
|
30
32
|
/**
|
|
31
33
|
* One open session of `ta serve`: pi's runtime, its event log, and the clients attached to it. Its id
|
package/esm/node/ta-session.js
CHANGED
|
@@ -66,6 +66,7 @@ export class TaSession {
|
|
|
66
66
|
sessionName: pi.sessionName,
|
|
67
67
|
firstMessage: SessionInfoUtil.firstMessage(pi.messages),
|
|
68
68
|
lastMessage: SessionInfoUtil.lastMessage(pi.messages),
|
|
69
|
+
movedFrom: this.context.movedFrom,
|
|
69
70
|
model: model ? `${model.provider}/${model.id}` : undefined,
|
|
70
71
|
isStreaming: pi.isStreaming,
|
|
71
72
|
waiting: this.ui.pendingRequests.length > 0,
|
|
@@ -104,6 +104,8 @@ const RULES = {
|
|
|
104
104
|
switchSession: session('chat.manage'),
|
|
105
105
|
fork: session('chat.create'),
|
|
106
106
|
forkToFile: session('chat.create'),
|
|
107
|
+
forkHere: session('chat.create'),
|
|
108
|
+
relocateDirectory: owner,
|
|
107
109
|
clone: session('chat.create'),
|
|
108
110
|
getForkMessages: session('chat.view'),
|
|
109
111
|
getEntries: session('chat.view'),
|
|
@@ -181,6 +183,8 @@ const RULES = {
|
|
|
181
183
|
addStore: fixed('settings.manage'),
|
|
182
184
|
removeRelay: fixed('settings.manage'),
|
|
183
185
|
chooseRelay: fixed('settings.manage'),
|
|
186
|
+
joinDeployment: fixed('settings.manage'),
|
|
187
|
+
leaveDeployment: fixed('settings.manage'),
|
|
184
188
|
probeRelay: fixed('settings.view'),
|
|
185
189
|
sharePackage: fixed('extensions.install'),
|
|
186
190
|
unsharePackage: fixed('extensions.install'),
|
|
@@ -196,6 +200,7 @@ const RULES = {
|
|
|
196
200
|
'listRooms',
|
|
197
201
|
'createRoom',
|
|
198
202
|
'invite',
|
|
203
|
+
'configureRoom',
|
|
199
204
|
'join',
|
|
200
205
|
'listRequests',
|
|
201
206
|
'answer',
|
|
@@ -215,10 +220,27 @@ const RULES = {
|
|
|
215
220
|
'watch',
|
|
216
221
|
'unwatch',
|
|
217
222
|
].map(method => [method, fixed('settings.manage')])),
|
|
223
|
+
// Calls by a voice service: talking to the models; its services and their keys are settings.
|
|
224
|
+
TaVoiceServer: {
|
|
225
|
+
listVoices: fixed('settings.view'),
|
|
226
|
+
addVoice: fixed('settings.manage'),
|
|
227
|
+
updateVoice: fixed('settings.manage'),
|
|
228
|
+
removeVoice: fixed('settings.manage'),
|
|
229
|
+
chooseVoice: fixed('settings.manage'),
|
|
230
|
+
openVoice: fixed('models.use'),
|
|
231
|
+
sendVoice: fixed('models.use'),
|
|
232
|
+
sayVoice: fixed('models.use'),
|
|
233
|
+
hushVoice: fixed('models.use'),
|
|
234
|
+
closeVoice: fixed('models.use'),
|
|
235
|
+
},
|
|
218
236
|
// What a page is at: each page's own, and only of conversations it may see (TaPresenceServerImpl).
|
|
219
237
|
TaPresenceServer: {
|
|
220
238
|
report: anyone,
|
|
221
239
|
},
|
|
240
|
+
// Which requests a page answers: each page's own; ta serve asks only the pages of who asked a run (TaPageBook).
|
|
241
|
+
TaPageServer: {
|
|
242
|
+
attach: anyone,
|
|
243
|
+
},
|
|
222
244
|
TaSettingsServer: {
|
|
223
245
|
getSettings: fixed('settings.view'),
|
|
224
246
|
updateSettings: fixed('settings.manage'),
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type CodingAgentOptions } from '@celljs/ai-coding-agent';
|
|
2
|
+
import { type RpcConnection } from '@celljs/rpc/lib/node';
|
|
3
|
+
import { type TaExtensionVoices, type TaVoiceEngine, TaVoiceServer, type TaVoiceSession, type TaVoices, type TaVoiceType } from '@celljs/ta-protocol';
|
|
4
|
+
import { VoicesFile } from './voices-file.js';
|
|
5
|
+
export declare const VOICE_BRIDGE: unique symbol;
|
|
6
|
+
interface OpenVoice {
|
|
7
|
+
id: string;
|
|
8
|
+
connection: RpcConnection;
|
|
9
|
+
engine: TaVoiceEngine;
|
|
10
|
+
speaks: boolean;
|
|
11
|
+
nextSay: number;
|
|
12
|
+
closed: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The voice services of calls (docs/design/ta-voice.md): the types packages register, voices.json, and the voices
|
|
16
|
+
* pages open, each of its connection and closed with it. A page never gets a key: its audio comes here.
|
|
17
|
+
*/
|
|
18
|
+
export declare class VoiceBridge {
|
|
19
|
+
protected readonly codingAgentOptions?: CodingAgentOptions;
|
|
20
|
+
protected readonly types: Map<string, TaVoiceType>;
|
|
21
|
+
protected readonly open: Map<string, OpenVoice>;
|
|
22
|
+
protected nextId: number;
|
|
23
|
+
protected filed?: VoicesFile;
|
|
24
|
+
protected get file(): VoicesFile;
|
|
25
|
+
/** For the server side of packages (TaExtensionHost.voices). */
|
|
26
|
+
readonly extensionVoices: TaExtensionVoices;
|
|
27
|
+
list(): TaVoices;
|
|
28
|
+
/** Opens a service of the type once before it is kept, so that a wrong key is said now. */
|
|
29
|
+
add(type: string, options: Record<string, unknown>, name?: string): Promise<string>;
|
|
30
|
+
/** Its fields as the form gave them, a secret left empty kept as it was; tried before it is written. */
|
|
31
|
+
update(id: string, options: Record<string, unknown>, name?: string): Promise<void>;
|
|
32
|
+
/** The fields of a form of the type, needed ones checked, then opened once and closed: what is kept. */
|
|
33
|
+
protected tried(type: string, options: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
34
|
+
remove(id: string): void;
|
|
35
|
+
choose(id: string | null): void;
|
|
36
|
+
openFor(connection: RpcConnection, language: string): Promise<TaVoiceSession>;
|
|
37
|
+
send(connection: RpcConnection, id: string, audio: string): void;
|
|
38
|
+
say(connection: RpcConnection, id: string, text: string): number;
|
|
39
|
+
hush(connection: RpcConnection, id: string): void;
|
|
40
|
+
close(connection: RpcConnection, id: string): void;
|
|
41
|
+
protected require(connection: RpcConnection, id: string): OpenVoice;
|
|
42
|
+
protected static where(definition: TaVoiceType, options: Record<string, unknown>): string;
|
|
43
|
+
/** What trying a service hears: nothing is told. */
|
|
44
|
+
protected static readonly SILENT: {
|
|
45
|
+
speech(): void;
|
|
46
|
+
text(): void;
|
|
47
|
+
audio(): void;
|
|
48
|
+
said(): void;
|
|
49
|
+
closed(): void;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** The voices for pages (TaVoiceServer), each of the connection that opened it. */
|
|
53
|
+
export declare class TaVoiceServerImpl implements TaVoiceServer {
|
|
54
|
+
protected readonly bridge: VoiceBridge;
|
|
55
|
+
listVoices(): Promise<TaVoices>;
|
|
56
|
+
addVoice(type: string, options: Record<string, unknown>, name?: string): Promise<string>;
|
|
57
|
+
updateVoice(id: string, options: Record<string, unknown>, name?: string): Promise<void>;
|
|
58
|
+
removeVoice(id: string): Promise<void>;
|
|
59
|
+
chooseVoice(id: string | null): Promise<void>;
|
|
60
|
+
openVoice(options: {
|
|
61
|
+
language: string;
|
|
62
|
+
}): Promise<TaVoiceSession>;
|
|
63
|
+
sendVoice(voiceId: string, audio: string): Promise<void>;
|
|
64
|
+
sayVoice(voiceId: string, text: string): Promise<number>;
|
|
65
|
+
hushVoice(voiceId: string): Promise<void>;
|
|
66
|
+
closeVoice(voiceId: string): Promise<void>;
|
|
67
|
+
}
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { createRequire as __cellCreateRequire } from "node:module";
|
|
2
|
+
const require = __cellCreateRequire(import.meta.url);
|
|
3
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
4
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
5
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
6
|
+
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;
|
|
7
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
8
|
+
};
|
|
9
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
10
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
11
|
+
};
|
|
12
|
+
var VoiceBridge_1;
|
|
13
|
+
import { CODING_AGENT_OPTIONS } from '@celljs/ai-coding-agent';
|
|
14
|
+
import { Autowired, Component, Value } from '@celljs/core';
|
|
15
|
+
import { Rpc } from '@celljs/rpc';
|
|
16
|
+
import { RpcContext } from '@celljs/rpc/lib/node';
|
|
17
|
+
import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
|
|
18
|
+
import { TaClient, TaVoiceServer, } from '@celljs/ta-protocol';
|
|
19
|
+
import { TaErrors } from './ta-errors.js';
|
|
20
|
+
import { VoicesFile } from './voices-file.js';
|
|
21
|
+
export const VOICE_BRIDGE = Symbol('VoiceBridge');
|
|
22
|
+
/** The voices a connection may have open at once: a call each, of a page or two. */
|
|
23
|
+
const MAX_PER_CONNECTION = 2;
|
|
24
|
+
/** The longest a reading may be, in characters: a reply read out is cut there. */
|
|
25
|
+
const MAX_SAY = 4000;
|
|
26
|
+
/** The most audio a page may send at once (base64 of about 2 s at 24 kHz). */
|
|
27
|
+
const MAX_AUDIO = 160 * 1024;
|
|
28
|
+
/**
|
|
29
|
+
* The voice services of calls (docs/design/ta-voice.md): the types packages register, voices.json, and the voices
|
|
30
|
+
* pages open, each of its connection and closed with it. A page never gets a key: its audio comes here.
|
|
31
|
+
*/
|
|
32
|
+
let VoiceBridge = VoiceBridge_1 = class VoiceBridge {
|
|
33
|
+
constructor() {
|
|
34
|
+
this.types = new Map();
|
|
35
|
+
this.open = new Map();
|
|
36
|
+
this.nextId = 0;
|
|
37
|
+
/** For the server side of packages (TaExtensionHost.voices). */
|
|
38
|
+
this.extensionVoices = {
|
|
39
|
+
registerType: (type, definition) => {
|
|
40
|
+
this.types.set(type, definition);
|
|
41
|
+
return () => {
|
|
42
|
+
if (this.types.get(type) === definition) {
|
|
43
|
+
this.types.delete(type);
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
get file() {
|
|
50
|
+
var _a;
|
|
51
|
+
var _b, _c;
|
|
52
|
+
(_b = this.filed) !== null && _b !== void 0 ? _b : (this.filed = new VoicesFile((_c = (_a = this.codingAgentOptions) === null || _a === void 0 ? void 0 : _a.agentDir) !== null && _c !== void 0 ? _c : TaBrand.agentDir()));
|
|
53
|
+
return this.filed;
|
|
54
|
+
}
|
|
55
|
+
list() {
|
|
56
|
+
var _a;
|
|
57
|
+
const { config } = this.file.safeRead();
|
|
58
|
+
const services = ((_a = config.services) !== null && _a !== void 0 ? _a : []).map(service => {
|
|
59
|
+
const name = service.name ? { name: service.name } : {};
|
|
60
|
+
const definition = this.types.get(service.type);
|
|
61
|
+
if (!definition) {
|
|
62
|
+
return { id: service.id, type: service.type, unavailable: true, ...name };
|
|
63
|
+
}
|
|
64
|
+
// What the page sees of it: never a password or a key.
|
|
65
|
+
const secret = new Set(definition.options.filter(option => option.secret).map(option => option.key));
|
|
66
|
+
const options = Object.fromEntries(Object.entries(service.options).filter(([key]) => !secret.has(key)));
|
|
67
|
+
return { id: service.id, type: service.type, where: VoiceBridge_1.where(definition, service.options), options, ...name };
|
|
68
|
+
});
|
|
69
|
+
const types = [...this.types].map(([type, { title, hint, icon, options, speaks }]) => ({
|
|
70
|
+
type,
|
|
71
|
+
title,
|
|
72
|
+
...(hint ? { hint } : {}),
|
|
73
|
+
...(icon ? { icon } : {}),
|
|
74
|
+
options,
|
|
75
|
+
speaks,
|
|
76
|
+
}));
|
|
77
|
+
const use = config.use && services.some(service => service.id === config.use) ? { use: config.use } : {};
|
|
78
|
+
return { services, types, file: this.file.file, ...use };
|
|
79
|
+
}
|
|
80
|
+
/** Opens a service of the type once before it is kept, so that a wrong key is said now. */
|
|
81
|
+
async add(type, options, name) {
|
|
82
|
+
const filled = await this.tried(type, options);
|
|
83
|
+
const id = this.file.add(type, filled, (name === null || name === void 0 ? void 0 : name.trim()) || undefined);
|
|
84
|
+
if (!this.file.safeRead().config.use) {
|
|
85
|
+
this.file.use(id);
|
|
86
|
+
}
|
|
87
|
+
return id;
|
|
88
|
+
}
|
|
89
|
+
/** Its fields as the form gave them, a secret left empty kept as it was; tried before it is written. */
|
|
90
|
+
async update(id, options, name) {
|
|
91
|
+
var _a;
|
|
92
|
+
var _b, _c;
|
|
93
|
+
const service = this.file.services().find(each => each.id === id);
|
|
94
|
+
if (!service) {
|
|
95
|
+
throw TaErrors.invalidArgument(`There is no voice service ${id}.`, 'Read the services again with listVoices.');
|
|
96
|
+
}
|
|
97
|
+
const secrets = new Set(((_b = (_a = this.types.get(service.type)) === null || _a === void 0 ? void 0 : _a.options) !== null && _b !== void 0 ? _b : []).filter(option => option.secret).map(option => option.key));
|
|
98
|
+
const given = { ...(options !== null && options !== void 0 ? options : {}) };
|
|
99
|
+
for (const key of secrets) {
|
|
100
|
+
if (!String((_c = given[key]) !== null && _c !== void 0 ? _c : '').trim() && service.options[key] !== undefined) {
|
|
101
|
+
given[key] = service.options[key];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const filled = await this.tried(service.type, given);
|
|
105
|
+
this.file.update(id, filled, (name === null || name === void 0 ? void 0 : name.trim()) || undefined);
|
|
106
|
+
}
|
|
107
|
+
/** The fields of a form of the type, needed ones checked, then opened once and closed: what is kept. */
|
|
108
|
+
async tried(type, options) {
|
|
109
|
+
const definition = this.types.get(type);
|
|
110
|
+
if (!definition) {
|
|
111
|
+
throw TaErrors.invalidArgument(`There is no voice service of type ${type}.`, 'Turn on the package that adds it.');
|
|
112
|
+
}
|
|
113
|
+
const filled = {};
|
|
114
|
+
for (const option of definition.options) {
|
|
115
|
+
const value = typeof (options === null || options === void 0 ? void 0 : options[option.key]) === 'string' ? String(options[option.key]).trim() : '';
|
|
116
|
+
if (!value && !option.optional) {
|
|
117
|
+
throw TaErrors.invalidArgument(`${option.key} is needed.`, 'Fill in every field that is not optional.');
|
|
118
|
+
}
|
|
119
|
+
if (value) {
|
|
120
|
+
filled[option.key] = value;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const engine = await definition.open(filled, { language: 'zh-CN' }, VoiceBridge_1.SILENT);
|
|
124
|
+
engine.close();
|
|
125
|
+
return filled;
|
|
126
|
+
}
|
|
127
|
+
remove(id) {
|
|
128
|
+
this.file.remove(id);
|
|
129
|
+
}
|
|
130
|
+
choose(id) {
|
|
131
|
+
this.file.use(id !== null && id !== void 0 ? id : undefined);
|
|
132
|
+
}
|
|
133
|
+
async openFor(connection, language) {
|
|
134
|
+
var _a;
|
|
135
|
+
const service = this.file.inUse();
|
|
136
|
+
if (!service) {
|
|
137
|
+
throw TaErrors.invalidArgument('No voice service is chosen for calls.', 'Choose one in Settings → Voice, or call with the voice of the device.');
|
|
138
|
+
}
|
|
139
|
+
const definition = this.types.get(service.type);
|
|
140
|
+
if (!definition) {
|
|
141
|
+
throw TaErrors.invalidArgument(`The voice service ${(_a = service.name) !== null && _a !== void 0 ? _a : service.id} is of type ${service.type}.`, 'Turn on the package that adds it.');
|
|
142
|
+
}
|
|
143
|
+
if ([...this.open.values()].filter(voice => voice.connection === connection).length >= MAX_PER_CONNECTION) {
|
|
144
|
+
throw TaErrors.invalidArgument('This page has calls open already.', 'Hang up a call first.');
|
|
145
|
+
}
|
|
146
|
+
const id = `v${++this.nextId}`;
|
|
147
|
+
const tell = (event) => {
|
|
148
|
+
if (!connection.closed) {
|
|
149
|
+
connection
|
|
150
|
+
.client(TaClient)
|
|
151
|
+
.onVoiceEvent(event)
|
|
152
|
+
.catch(() => undefined);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const engine = await definition.open(service.options, { language: String(language || 'zh-CN') }, {
|
|
156
|
+
speech: () => tell({ voiceId: id, type: 'speech' }),
|
|
157
|
+
text: (text, final) => tell({ voiceId: id, type: 'text', text, final }),
|
|
158
|
+
audio: (pcm, say) => tell({ voiceId: id, type: 'audio', say, data: Buffer.from(pcm).toString('base64') }),
|
|
159
|
+
said: say => tell({ voiceId: id, type: 'said', say }),
|
|
160
|
+
closed: error => {
|
|
161
|
+
const voice = this.open.get(id);
|
|
162
|
+
if (voice && !voice.closed) {
|
|
163
|
+
voice.closed = true;
|
|
164
|
+
this.open.delete(id);
|
|
165
|
+
tell({ voiceId: id, type: 'closed', ...(error ? { error } : {}) });
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
const voice = { id, connection, engine, speaks: definition.speaks && !!engine.say, nextSay: 0, closed: false };
|
|
170
|
+
this.open.set(id, voice);
|
|
171
|
+
connection.onClose(() => this.close(connection, id));
|
|
172
|
+
return { voiceId: id, inputRate: engine.inputRate, outputRate: engine.outputRate, speaks: voice.speaks };
|
|
173
|
+
}
|
|
174
|
+
send(connection, id, audio) {
|
|
175
|
+
if (typeof audio !== 'string' || audio.length > MAX_AUDIO) {
|
|
176
|
+
throw TaErrors.invalidArgument('The audio is too long.', 'Send it in pieces of about 100 ms.');
|
|
177
|
+
}
|
|
178
|
+
this.require(connection, id).engine.send(Buffer.from(audio, 'base64'));
|
|
179
|
+
}
|
|
180
|
+
say(connection, id, text) {
|
|
181
|
+
const voice = this.require(connection, id);
|
|
182
|
+
if (!voice.speaks || !voice.engine.say) {
|
|
183
|
+
throw TaErrors.invalidArgument('This voice service does not read text.', 'Read with the voice of the device.');
|
|
184
|
+
}
|
|
185
|
+
const say = ++voice.nextSay;
|
|
186
|
+
voice.engine.say(String(text !== null && text !== void 0 ? text : '').slice(0, MAX_SAY), say);
|
|
187
|
+
return say;
|
|
188
|
+
}
|
|
189
|
+
hush(connection, id) {
|
|
190
|
+
this.require(connection, id).engine.hush();
|
|
191
|
+
}
|
|
192
|
+
close(connection, id) {
|
|
193
|
+
const voice = this.open.get(id);
|
|
194
|
+
if (!voice || voice.connection !== connection || voice.closed) {
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
voice.closed = true;
|
|
198
|
+
this.open.delete(id);
|
|
199
|
+
try {
|
|
200
|
+
voice.engine.close();
|
|
201
|
+
}
|
|
202
|
+
catch (_a) {
|
|
203
|
+
// Closed already.
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
require(connection, id) {
|
|
207
|
+
const voice = this.open.get(id);
|
|
208
|
+
if (!voice || voice.connection !== connection) {
|
|
209
|
+
throw TaErrors.invalidArgument(`There is no voice ${id} open.`, 'Open it again with openVoice.');
|
|
210
|
+
}
|
|
211
|
+
return voice;
|
|
212
|
+
}
|
|
213
|
+
static where(definition, options) {
|
|
214
|
+
try {
|
|
215
|
+
return definition.where(options);
|
|
216
|
+
}
|
|
217
|
+
catch (_a) {
|
|
218
|
+
return '';
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
/** What trying a service hears: nothing is told. */
|
|
223
|
+
VoiceBridge.SILENT = { speech() { }, text() { }, audio() { }, said() { }, closed() { } };
|
|
224
|
+
__decorate([
|
|
225
|
+
Value(CODING_AGENT_OPTIONS),
|
|
226
|
+
__metadata("design:type", Object)
|
|
227
|
+
], VoiceBridge.prototype, "codingAgentOptions", void 0);
|
|
228
|
+
VoiceBridge = VoiceBridge_1 = __decorate([
|
|
229
|
+
Component(VOICE_BRIDGE)
|
|
230
|
+
], VoiceBridge);
|
|
231
|
+
export { VoiceBridge };
|
|
232
|
+
/** The voices for pages (TaVoiceServer), each of the connection that opened it. */
|
|
233
|
+
let TaVoiceServerImpl = class TaVoiceServerImpl {
|
|
234
|
+
listVoices() {
|
|
235
|
+
return TaErrors.guard(() => this.bridge.list());
|
|
236
|
+
}
|
|
237
|
+
addVoice(type, options, name) {
|
|
238
|
+
return TaErrors.guard(() => this.bridge.add(type, options !== null && options !== void 0 ? options : {}, name !== null && name !== void 0 ? name : undefined));
|
|
239
|
+
}
|
|
240
|
+
updateVoice(id, options, name) {
|
|
241
|
+
return TaErrors.guard(() => this.bridge.update(id, options !== null && options !== void 0 ? options : {}, name !== null && name !== void 0 ? name : undefined));
|
|
242
|
+
}
|
|
243
|
+
removeVoice(id) {
|
|
244
|
+
return TaErrors.guard(() => this.bridge.remove(id));
|
|
245
|
+
}
|
|
246
|
+
chooseVoice(id) {
|
|
247
|
+
return TaErrors.guard(() => this.bridge.choose(id !== null && id !== void 0 ? id : null));
|
|
248
|
+
}
|
|
249
|
+
openVoice(options) {
|
|
250
|
+
return TaErrors.guard(() => this.bridge.openFor(RpcContext.requireConnection(), options === null || options === void 0 ? void 0 : options.language));
|
|
251
|
+
}
|
|
252
|
+
sendVoice(voiceId, audio) {
|
|
253
|
+
return TaErrors.guard(() => this.bridge.send(RpcContext.requireConnection(), voiceId, audio));
|
|
254
|
+
}
|
|
255
|
+
sayVoice(voiceId, text) {
|
|
256
|
+
return TaErrors.guard(() => this.bridge.say(RpcContext.requireConnection(), voiceId, text));
|
|
257
|
+
}
|
|
258
|
+
hushVoice(voiceId) {
|
|
259
|
+
return TaErrors.guard(() => this.bridge.hush(RpcContext.requireConnection(), voiceId));
|
|
260
|
+
}
|
|
261
|
+
closeVoice(voiceId) {
|
|
262
|
+
return TaErrors.guard(() => this.bridge.close(RpcContext.requireConnection(), voiceId));
|
|
263
|
+
}
|
|
264
|
+
};
|
|
265
|
+
__decorate([
|
|
266
|
+
Autowired(VOICE_BRIDGE),
|
|
267
|
+
__metadata("design:type", VoiceBridge)
|
|
268
|
+
], TaVoiceServerImpl.prototype, "bridge", void 0);
|
|
269
|
+
TaVoiceServerImpl = __decorate([
|
|
270
|
+
Rpc(TaVoiceServer)
|
|
271
|
+
], TaVoiceServerImpl);
|
|
272
|
+
export { TaVoiceServerImpl };
|
|
273
|
+
//# sourceMappingURL=voice-bridge.js.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/** A service of voices.json: a type a package registered, and the options of its form. */
|
|
2
|
+
export interface VoiceServiceEntry {
|
|
3
|
+
id: string;
|
|
4
|
+
type: string;
|
|
5
|
+
name?: string;
|
|
6
|
+
options: Record<string, unknown>;
|
|
7
|
+
}
|
|
8
|
+
export interface VoicesConfig {
|
|
9
|
+
services?: VoiceServiceEntry[];
|
|
10
|
+
/** The service calls use; none: the voice of the device. */
|
|
11
|
+
use?: string;
|
|
12
|
+
}
|
|
13
|
+
/** voices.json of the agent directory (docs/design/ta-voice.md 3): its keys stay there, written 0600. */
|
|
14
|
+
export declare class VoicesFile {
|
|
15
|
+
readonly file: string;
|
|
16
|
+
constructor(agentDir: string);
|
|
17
|
+
read(): VoicesConfig;
|
|
18
|
+
/** What reads, or nothing with why it does not parse. */
|
|
19
|
+
safeRead(): {
|
|
20
|
+
config: VoicesConfig;
|
|
21
|
+
error?: string;
|
|
22
|
+
};
|
|
23
|
+
services(): VoiceServiceEntry[];
|
|
24
|
+
/** The service in use, if it is still there. */
|
|
25
|
+
inUse(): VoiceServiceEntry | undefined;
|
|
26
|
+
/** Its id: the type with a number when there is one of it already. */
|
|
27
|
+
add(type: string, options: Record<string, unknown>, name?: string): string;
|
|
28
|
+
/** Its options and name, the rest as it was; throws for one that is not there. */
|
|
29
|
+
update(id: string, options: Record<string, unknown>, name?: string): void;
|
|
30
|
+
remove(id: string): void;
|
|
31
|
+
use(id: string | undefined): void;
|
|
32
|
+
protected write(config: VoicesConfig): void;
|
|
33
|
+
}
|