@celljs/ta-host 4.16.0 → 4.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/esm/node/app-commands.d.ts +111 -0
  2. package/esm/node/app-commands.js +646 -0
  3. package/esm/node/credentials.d.ts +12 -0
  4. package/esm/node/credentials.js +35 -0
  5. package/esm/node/delegate-extension.d.ts +11 -0
  6. package/esm/node/delegate-extension.js +33 -0
  7. package/esm/node/directory-relocator.d.ts +49 -0
  8. package/esm/node/directory-relocator.js +267 -0
  9. package/esm/node/extension-providers.d.ts +16 -1
  10. package/esm/node/extension-providers.js +23 -3
  11. package/esm/node/index.d.ts +8 -0
  12. package/esm/node/index.js +8 -0
  13. package/esm/node/keep-awake.d.ts +8 -1
  14. package/esm/node/keep-awake.js +24 -3
  15. package/esm/node/message-page.d.ts +6 -1
  16. package/esm/node/message-page.js +50 -2
  17. package/esm/node/package-hosts.d.ts +8 -0
  18. package/esm/node/package-hosts.js +41 -0
  19. package/esm/node/package-webhooks.d.ts +26 -0
  20. package/esm/node/package-webhooks.js +101 -0
  21. package/esm/node/page-book.d.ts +48 -0
  22. package/esm/node/page-book.js +117 -0
  23. package/esm/node/relay-bridge.d.ts +2 -0
  24. package/esm/node/relay-bridge.js +35 -1
  25. package/esm/node/rooms-bridge.d.ts +13 -1
  26. package/esm/node/rooms-bridge.js +29 -0
  27. package/esm/node/screen-share/desktop-picture.d.ts +51 -0
  28. package/esm/node/screen-share/desktop-picture.js +133 -0
  29. package/esm/node/screen-share/desktop-session.d.ts +128 -0
  30. package/esm/node/screen-share/desktop-session.js +437 -0
  31. package/esm/node/screen-share/desktop-targets.d.ts +24 -0
  32. package/esm/node/screen-share/desktop-targets.js +80 -0
  33. package/esm/node/screen-share/desktop-viewer.d.ts +146 -0
  34. package/esm/node/screen-share/desktop-viewer.js +339 -0
  35. package/esm/node/screen-share/index.d.ts +8 -0
  36. package/esm/node/screen-share/index.js +9 -0
  37. package/esm/node/screen-share/rfb-client.d.ts +54 -0
  38. package/esm/node/screen-share/rfb-client.js +179 -0
  39. package/esm/node/screen-share/rfb-des.d.ts +11 -0
  40. package/esm/node/screen-share/rfb-des.js +143 -0
  41. package/esm/node/screen-share/rfb-encodings.d.ts +71 -0
  42. package/esm/node/screen-share/rfb-encodings.js +288 -0
  43. package/esm/node/screen-share/rfb-framebuffer.d.ts +16 -0
  44. package/esm/node/screen-share/rfb-framebuffer.js +53 -0
  45. package/esm/node/screen-share/rfb-protocol.d.ts +90 -0
  46. package/esm/node/screen-share/rfb-protocol.js +36 -0
  47. package/esm/node/screen-share/rfb-reader.d.ts +34 -0
  48. package/esm/node/screen-share/rfb-reader.js +110 -0
  49. package/esm/node/screen-share/rfb-security.d.ts +30 -0
  50. package/esm/node/screen-share/rfb-security.js +136 -0
  51. package/esm/node/screen-share/rfb-test-server.d.ts +37 -0
  52. package/esm/node/screen-share/rfb-test-server.js +116 -0
  53. package/esm/node/screen-share/screen-share-server-impl.d.ts +37 -0
  54. package/esm/node/screen-share/screen-share-server-impl.js +76 -0
  55. package/esm/node/screen-share/screen-shares.d.ts +123 -0
  56. package/esm/node/screen-share/screen-shares.js +463 -0
  57. package/esm/node/session-info-util.d.ts +10 -9
  58. package/esm/node/session-info-util.js +16 -68
  59. package/esm/node/settings-store.d.ts +1 -1
  60. package/esm/node/settings-store.js +7 -4
  61. package/esm/node/sign-in/access-store.d.ts +1 -1
  62. package/esm/node/sign-in/access-store.js +4 -1
  63. package/esm/node/sign-in/index.d.ts +2 -0
  64. package/esm/node/sign-in/index.js +2 -0
  65. package/esm/node/sign-in/polled-sign-in.d.ts +50 -0
  66. package/esm/node/sign-in/polled-sign-in.js +83 -0
  67. package/esm/node/sign-in/sign-in-devices.d.ts +55 -0
  68. package/esm/node/sign-in/sign-in-devices.js +113 -0
  69. package/esm/node/sign-in/sign-in-flow.d.ts +38 -4
  70. package/esm/node/sign-in/sign-in-flow.js +79 -4
  71. package/esm/node/sign-in/sign-in-pages.d.ts +8 -0
  72. package/esm/node/sign-in/sign-in-pages.js +137 -8
  73. package/esm/node/sign-in/sign-in-provider.d.ts +6 -0
  74. package/esm/node/sign-in/sign-in-provider.js +15 -0
  75. package/esm/node/sources-extension.d.ts +10 -0
  76. package/esm/node/sources-extension.js +18 -0
  77. package/esm/node/ta-server-impl.d.ts +5 -1
  78. package/esm/node/ta-server-impl.js +25 -3
  79. package/esm/node/ta-session-factory.d.ts +13 -1
  80. package/esm/node/ta-session-factory.js +41 -4
  81. package/esm/node/ta-session.d.ts +2 -0
  82. package/esm/node/ta-session.js +1 -0
  83. package/esm/node/team/team-calls.js +49 -0
  84. package/esm/node/voice-bridge.d.ts +70 -0
  85. package/esm/node/voice-bridge.js +280 -0
  86. package/esm/node/voices-file.d.ts +38 -0
  87. package/esm/node/voices-file.js +100 -0
  88. package/esm/node/working-directory.d.ts +8 -2
  89. package/esm/node/working-directory.js +7 -0
  90. package/lib/node/app-commands.d.ts +112 -0
  91. package/lib/node/app-commands.js +650 -0
  92. package/lib/node/credentials.d.ts +13 -0
  93. package/lib/node/credentials.js +38 -0
  94. package/lib/node/delegate-extension.d.ts +12 -0
  95. package/lib/node/delegate-extension.js +37 -0
  96. package/lib/node/directory-relocator.d.ts +50 -0
  97. package/lib/node/directory-relocator.js +270 -0
  98. package/lib/node/extension-providers.d.ts +16 -1
  99. package/lib/node/extension-providers.js +23 -3
  100. package/lib/node/index.d.ts +8 -0
  101. package/lib/node/index.js +8 -0
  102. package/lib/node/keep-awake.d.ts +8 -1
  103. package/lib/node/keep-awake.js +24 -3
  104. package/lib/node/message-page.d.ts +6 -1
  105. package/lib/node/message-page.js +50 -2
  106. package/lib/node/package-hosts.d.ts +8 -0
  107. package/lib/node/package-hosts.js +41 -0
  108. package/lib/node/package-webhooks.d.ts +27 -0
  109. package/lib/node/package-webhooks.js +104 -0
  110. package/lib/node/page-book.d.ts +49 -0
  111. package/lib/node/page-book.js +120 -0
  112. package/lib/node/relay-bridge.d.ts +2 -0
  113. package/lib/node/relay-bridge.js +34 -0
  114. package/lib/node/rooms-bridge.d.ts +13 -1
  115. package/lib/node/rooms-bridge.js +29 -0
  116. package/lib/node/screen-share/desktop-picture.d.ts +52 -0
  117. package/lib/node/screen-share/desktop-picture.js +136 -0
  118. package/lib/node/screen-share/desktop-session.d.ts +129 -0
  119. package/lib/node/screen-share/desktop-session.js +442 -0
  120. package/lib/node/screen-share/desktop-targets.d.ts +25 -0
  121. package/lib/node/screen-share/desktop-targets.js +84 -0
  122. package/lib/node/screen-share/desktop-viewer.d.ts +147 -0
  123. package/lib/node/screen-share/desktop-viewer.js +343 -0
  124. package/lib/node/screen-share/index.d.ts +9 -0
  125. package/lib/node/screen-share/index.js +25 -0
  126. package/lib/node/screen-share/rfb-client.d.ts +55 -0
  127. package/lib/node/screen-share/rfb-client.js +183 -0
  128. package/lib/node/screen-share/rfb-des.d.ts +12 -0
  129. package/lib/node/screen-share/rfb-des.js +146 -0
  130. package/lib/node/screen-share/rfb-encodings.d.ts +72 -0
  131. package/lib/node/screen-share/rfb-encodings.js +295 -0
  132. package/lib/node/screen-share/rfb-framebuffer.d.ts +17 -0
  133. package/lib/node/screen-share/rfb-framebuffer.js +57 -0
  134. package/lib/node/screen-share/rfb-protocol.d.ts +91 -0
  135. package/lib/node/screen-share/rfb-protocol.js +39 -0
  136. package/lib/node/screen-share/rfb-reader.d.ts +35 -0
  137. package/lib/node/screen-share/rfb-reader.js +115 -0
  138. package/lib/node/screen-share/rfb-security.d.ts +31 -0
  139. package/lib/node/screen-share/rfb-security.js +141 -0
  140. package/lib/node/screen-share/rfb-test-server.d.ts +38 -0
  141. package/lib/node/screen-share/rfb-test-server.js +120 -0
  142. package/lib/node/screen-share/screen-share-server-impl.d.ts +38 -0
  143. package/lib/node/screen-share/screen-share-server-impl.js +79 -0
  144. package/lib/node/screen-share/screen-shares.d.ts +124 -0
  145. package/lib/node/screen-share/screen-shares.js +464 -0
  146. package/lib/node/session-info-util.d.ts +10 -9
  147. package/lib/node/session-info-util.js +15 -67
  148. package/lib/node/settings-store.d.ts +1 -1
  149. package/lib/node/settings-store.js +6 -3
  150. package/lib/node/sign-in/access-store.d.ts +1 -1
  151. package/lib/node/sign-in/access-store.js +4 -1
  152. package/lib/node/sign-in/index.d.ts +2 -0
  153. package/lib/node/sign-in/index.js +2 -0
  154. package/lib/node/sign-in/polled-sign-in.d.ts +51 -0
  155. package/lib/node/sign-in/polled-sign-in.js +87 -0
  156. package/lib/node/sign-in/sign-in-devices.d.ts +56 -0
  157. package/lib/node/sign-in/sign-in-devices.js +117 -0
  158. package/lib/node/sign-in/sign-in-flow.d.ts +38 -4
  159. package/lib/node/sign-in/sign-in-flow.js +80 -5
  160. package/lib/node/sign-in/sign-in-pages.d.ts +8 -0
  161. package/lib/node/sign-in/sign-in-pages.js +136 -7
  162. package/lib/node/sign-in/sign-in-provider.d.ts +6 -0
  163. package/lib/node/sign-in/sign-in-provider.js +15 -0
  164. package/lib/node/sources-extension.d.ts +11 -0
  165. package/lib/node/sources-extension.js +22 -0
  166. package/lib/node/ta-server-impl.d.ts +5 -1
  167. package/lib/node/ta-server-impl.js +24 -2
  168. package/lib/node/ta-session-factory.d.ts +13 -1
  169. package/lib/node/ta-session-factory.js +40 -3
  170. package/lib/node/ta-session.d.ts +2 -0
  171. package/lib/node/ta-session.js +1 -0
  172. package/lib/node/team/team-calls.js +49 -0
  173. package/lib/node/voice-bridge.d.ts +71 -0
  174. package/lib/node/voice-bridge.js +281 -0
  175. package/lib/node/voices-file.d.ts +39 -0
  176. package/lib/node/voices-file.js +104 -0
  177. package/lib/node/working-directory.d.ts +8 -2
  178. package/lib/node/working-directory.js +7 -0
  179. package/package.json +14 -10
@@ -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';
@@ -22,8 +24,10 @@ import { PiDirectories } from './remote/remote-protocol.js';
22
24
  import { SessionCatalogUtil } from './session-catalog.js';
23
25
  import { ExecSessionFiles } from './session-files.js';
24
26
  import { SlashCommandUtil } from './slash-command-util.js';
27
+ import { SourcesExtension } from './sources-extension.js';
25
28
  import { TaErrors } from './ta-errors.js';
26
29
  import { TaSession } from './ta-session.js';
30
+ import { TaWorkingDirectory } from './working-directory.js';
27
31
  export var SessionFileUtil;
28
32
  (function (SessionFileUtil) {
29
33
  /**
@@ -41,6 +45,16 @@ export var SessionFileUtil;
41
45
  }
42
46
  }
43
47
  SessionFileUtil.cwdOverride = cwdOverride;
48
+ /** `path` when it is a directory there now. */
49
+ function existing(path) {
50
+ try {
51
+ return path && statSync(path).isDirectory() ? path : undefined;
52
+ }
53
+ catch (_a) {
54
+ return undefined;
55
+ }
56
+ }
57
+ SessionFileUtil.existing = existing;
44
58
  })(SessionFileUtil || (SessionFileUtil = {}));
45
59
  /** Sets up pi for each session the way its command does, with one model runtime for all of them. */
46
60
  let DefaultTaSessionFactory = class DefaultTaSessionFactory {
@@ -57,7 +71,7 @@ let DefaultTaSessionFactory = class DefaultTaSessionFactory {
57
71
  const modelRuntime = await this.modelRuntimeProvider.provide();
58
72
  // `options.cwd` is a location; pi works in a directory of this machine (PiDirectories). A session
59
73
  // 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) });
74
+ const { sessionManager, movedFrom } = this.openSessionManager(sdk, { ...requested, cwd: this.directories.toPi(requested.cwd) });
61
75
  // Opened again from its file without its role or group (a package relaying a message to it, the list of
62
76
  // conversations): those it was opened with before.
63
77
  const entries = requested.sessionFile ? sessionManager.getEntries() : [];
@@ -101,6 +115,7 @@ let DefaultTaSessionFactory = class DefaultTaSessionFactory {
101
115
  group: options.group,
102
116
  role: options.appendSystemPrompt,
103
117
  locate: cwd => this.directories.fromPi(cwd),
118
+ movedFrom,
104
119
  });
105
120
  await session.start();
106
121
  return session;
@@ -153,12 +168,27 @@ let DefaultTaSessionFactory = class DefaultTaSessionFactory {
153
168
  },
154
169
  });
155
170
  }
171
+ /**
172
+ * pi's manager of the session file asked, or of a new one. A file whose directory is no longer there goes on
173
+ * in the directory of the server (TaWorkingDirectory), else the home directory, as pi's Continue: `movedFrom`, the directory as
174
+ * the file says it.
175
+ */
156
176
  openSessionManager(sdk, options) {
177
+ var _a, _b;
178
+ var _c;
157
179
  if (!options.sessionFile) {
158
- return sdk.SessionManager.create(options.cwd);
180
+ return { sessionManager: sdk.SessionManager.create(options.cwd) };
159
181
  }
160
182
  try {
161
- return sdk.SessionManager.open(options.sessionFile, undefined, SessionFileUtil.cwdOverride(options.sessionFile, options.cwd));
183
+ const sessionManager = sdk.SessionManager.open(options.sessionFile, undefined, SessionFileUtil.cwdOverride(options.sessionFile, options.cwd));
184
+ const cwd = sessionManager.getCwd();
185
+ if (existsSync(cwd)) {
186
+ return { sessionManager };
187
+ }
188
+ const here = (_c = SessionFileUtil.existing((_a = this.workingDirectory) === null || _a === void 0 ? void 0 : _a.get())) !== null && _c !== void 0 ? _c : homedir();
189
+ // As its file says it: pi resolves a path of another system (C:\...) against the directory of ta serve.
190
+ const movedFrom = ((_b = sessionManager.getHeader()) === null || _b === void 0 ? void 0 : _b.cwd) || cwd;
191
+ return { sessionManager: sdk.SessionManager.open(options.sessionFile, undefined, here), movedFrom };
162
192
  }
163
193
  catch (error) {
164
194
  throw TaErrors.invalidArgument(`Cannot open the session ${options.sessionFile}: ${error.message}`, 'Pass a session file that exists; listSessions shows the open ones.');
@@ -182,6 +212,8 @@ let DefaultTaSessionFactory = class DefaultTaSessionFactory {
182
212
  new SessionLockExtension(lock),
183
213
  new PackageHostExtension(this.packageHosts, new ExecSessionFiles(environment, piCwd)),
184
214
  new BrandExtension(sdk),
215
+ new DelegateExtension(),
216
+ new SourcesExtension(),
185
217
  ...(environment.local ? [] : [new ExecExtension(environment, sdk, piCwd)]),
186
218
  ];
187
219
  return [
@@ -258,6 +290,11 @@ __decorate([
258
290
  Autowired(PiDirectories),
259
291
  __metadata("design:type", Object)
260
292
  ], DefaultTaSessionFactory.prototype, "directories", void 0);
293
+ __decorate([
294
+ Autowired(TaWorkingDirectory),
295
+ Optional(),
296
+ __metadata("design:type", Object)
297
+ ], DefaultTaSessionFactory.prototype, "workingDirectory", void 0);
261
298
  __decorate([
262
299
  Autowired(ExtensionProviders),
263
300
  __metadata("design:type", Object)
@@ -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
@@ -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',
@@ -208,6 +213,12 @@ const RULES = {
208
213
  'friendLink',
209
214
  'addFriend',
210
215
  'offerFriend',
216
+ 'inviteFriends',
217
+ 'listGroupInvites',
218
+ 'answerGroupInvite',
219
+ 'sendToFriends',
220
+ 'listFriendShares',
221
+ 'answerFriendShare',
211
222
  'listFriendRequests',
212
223
  'answerFriend',
213
224
  'removeFriend',
@@ -215,10 +226,28 @@ const RULES = {
215
226
  'watch',
216
227
  'unwatch',
217
228
  ].map(method => [method, fixed('settings.manage')])),
229
+ // Calls by a voice service: talking to the models; its services and their keys are settings.
230
+ TaVoiceServer: {
231
+ listVoices: fixed('settings.view'),
232
+ addVoice: fixed('settings.manage'),
233
+ updateVoice: fixed('settings.manage'),
234
+ removeVoice: fixed('settings.manage'),
235
+ chooseVoice: fixed('settings.manage'),
236
+ setBargeIn: fixed('settings.manage'),
237
+ openVoice: fixed('models.use'),
238
+ sendVoice: fixed('models.use'),
239
+ sayVoice: fixed('models.use'),
240
+ hushVoice: fixed('models.use'),
241
+ closeVoice: fixed('models.use'),
242
+ },
218
243
  // What a page is at: each page's own, and only of conversations it may see (TaPresenceServerImpl).
219
244
  TaPresenceServer: {
220
245
  report: anyone,
221
246
  },
247
+ // Which requests a page answers: each page's own; ta serve asks only the pages of who asked a run (TaPageBook).
248
+ TaPageServer: {
249
+ attach: anyone,
250
+ },
222
251
  TaSettingsServer: {
223
252
  getSettings: fixed('settings.view'),
224
253
  updateSettings: fixed('settings.manage'),
@@ -263,6 +292,26 @@ const RULES = {
263
292
  answer: fixed('hosts.use'),
264
293
  disconnect: fixed('hosts.use'),
265
294
  },
295
+ // Remote desktops: seeing and working a screen is all the owner can do, and in the first stage only the owner does it
296
+ // (docs/design/ta-desktop-remote.md 6.2); no role or grant gives it.
297
+ TaScreenShareServer: {
298
+ listDesktops: owner,
299
+ listDesktopScreens: owner,
300
+ addDesktop: owner,
301
+ removeDesktop: owner,
302
+ openDesktop: owner,
303
+ closeDesktop: owner,
304
+ readDesktopFrames: owner,
305
+ setDesktopView: owner,
306
+ sendDesktopInput: owner,
307
+ pasteToDesktop: owner,
308
+ readDesktopClipboard: owner,
309
+ takeDesktopControl: owner,
310
+ releaseDesktopControl: owner,
311
+ endAllDesktops: owner,
312
+ watchDesktops: owner,
313
+ answerDesktop: owner,
314
+ },
266
315
  };
267
316
  /** By name, own keys only: a method such as `toString` is no rule. */
268
317
  const BY_SERVICE = new Map(Object.entries(RULES).map(([service, rules]) => [service, new Map(Object.entries(rules))]));
@@ -0,0 +1,70 @@
1
+ import { type CodingAgentOptions } from '@celljs/ai-coding-agent';
2
+ import { type RpcConnection } from '@celljs/rpc/lib/node';
3
+ import { TaBargeIn, 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
+ setBargeIn(bargeIn: Partial<TaBargeIn>): void;
37
+ openFor(connection: RpcConnection, language: string): Promise<TaVoiceSession>;
38
+ send(connection: RpcConnection, id: string, audio: string): void;
39
+ say(connection: RpcConnection, id: string, text: string): number;
40
+ hush(connection: RpcConnection, id: string): void;
41
+ close(connection: RpcConnection, id: string): void;
42
+ protected require(connection: RpcConnection, id: string): OpenVoice;
43
+ protected static where(definition: TaVoiceType, options: Record<string, unknown>): string;
44
+ /** What trying a service hears: nothing is told. */
45
+ protected static readonly SILENT: {
46
+ speech(): void;
47
+ text(): void;
48
+ audio(): void;
49
+ said(): void;
50
+ closed(): void;
51
+ };
52
+ }
53
+ /** The voices for pages (TaVoiceServer), each of the connection that opened it. */
54
+ export declare class TaVoiceServerImpl implements TaVoiceServer {
55
+ protected readonly bridge: VoiceBridge;
56
+ listVoices(): Promise<TaVoices>;
57
+ addVoice(type: string, options: Record<string, unknown>, name?: string): Promise<string>;
58
+ updateVoice(id: string, options: Record<string, unknown>, name?: string): Promise<void>;
59
+ removeVoice(id: string): Promise<void>;
60
+ chooseVoice(id: string | null): Promise<void>;
61
+ setBargeIn(bargeIn: Partial<TaBargeIn>): Promise<void>;
62
+ openVoice(options: {
63
+ language: string;
64
+ }): Promise<TaVoiceSession>;
65
+ sendVoice(voiceId: string, audio: string): Promise<void>;
66
+ sayVoice(voiceId: string, text: string): Promise<number>;
67
+ hushVoice(voiceId: string): Promise<void>;
68
+ closeVoice(voiceId: string): Promise<void>;
69
+ }
70
+ export {};
@@ -0,0 +1,280 @@
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 { TaBargeIn, 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, bargeIn: TaBargeIn.resolve(config.bargeIn), ...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
+ setBargeIn(bargeIn) {
134
+ this.file.setBargeIn(TaBargeIn.pick(bargeIn));
135
+ }
136
+ async openFor(connection, language) {
137
+ var _a;
138
+ const service = this.file.inUse();
139
+ if (!service) {
140
+ throw TaErrors.invalidArgument('No voice service is chosen for calls.', 'Choose one in Settings → Voice, or call with the voice of the device.');
141
+ }
142
+ const definition = this.types.get(service.type);
143
+ if (!definition) {
144
+ 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.');
145
+ }
146
+ if ([...this.open.values()].filter(voice => voice.connection === connection).length >= MAX_PER_CONNECTION) {
147
+ throw TaErrors.invalidArgument('This page has calls open already.', 'Hang up a call first.');
148
+ }
149
+ const id = `v${++this.nextId}`;
150
+ const tell = (event) => {
151
+ if (!connection.closed) {
152
+ connection
153
+ .client(TaClient)
154
+ .onVoiceEvent(event)
155
+ .catch(() => undefined);
156
+ }
157
+ };
158
+ const engine = await definition.open(service.options, { language: String(language || 'zh-CN') }, {
159
+ speech: () => tell({ voiceId: id, type: 'speech' }),
160
+ text: (text, final) => tell({ voiceId: id, type: 'text', text, final }),
161
+ audio: (pcm, say) => tell({ voiceId: id, type: 'audio', say, data: Buffer.from(pcm).toString('base64') }),
162
+ said: say => tell({ voiceId: id, type: 'said', say }),
163
+ closed: error => {
164
+ const voice = this.open.get(id);
165
+ if (voice && !voice.closed) {
166
+ voice.closed = true;
167
+ this.open.delete(id);
168
+ tell({ voiceId: id, type: 'closed', ...(error ? { error } : {}) });
169
+ }
170
+ },
171
+ });
172
+ const voice = { id, connection, engine, speaks: definition.speaks && !!engine.say, nextSay: 0, closed: false };
173
+ this.open.set(id, voice);
174
+ connection.onClose(() => this.close(connection, id));
175
+ const bargeIn = TaBargeIn.resolve(this.file.safeRead().config.bargeIn);
176
+ return { voiceId: id, inputRate: engine.inputRate, outputRate: engine.outputRate, speaks: voice.speaks, bargeIn };
177
+ }
178
+ send(connection, id, audio) {
179
+ if (typeof audio !== 'string' || audio.length > MAX_AUDIO) {
180
+ throw TaErrors.invalidArgument('The audio is too long.', 'Send it in pieces of about 100 ms.');
181
+ }
182
+ this.require(connection, id).engine.send(Buffer.from(audio, 'base64'));
183
+ }
184
+ say(connection, id, text) {
185
+ const voice = this.require(connection, id);
186
+ if (!voice.speaks || !voice.engine.say) {
187
+ throw TaErrors.invalidArgument('This voice service does not read text.', 'Read with the voice of the device.');
188
+ }
189
+ const say = ++voice.nextSay;
190
+ voice.engine.say(String(text !== null && text !== void 0 ? text : '').slice(0, MAX_SAY), say);
191
+ return say;
192
+ }
193
+ hush(connection, id) {
194
+ this.require(connection, id).engine.hush();
195
+ }
196
+ close(connection, id) {
197
+ const voice = this.open.get(id);
198
+ if (!voice || voice.connection !== connection || voice.closed) {
199
+ return;
200
+ }
201
+ voice.closed = true;
202
+ this.open.delete(id);
203
+ try {
204
+ voice.engine.close();
205
+ }
206
+ catch (_a) {
207
+ // Closed already.
208
+ }
209
+ }
210
+ require(connection, id) {
211
+ const voice = this.open.get(id);
212
+ if (!voice || voice.connection !== connection) {
213
+ throw TaErrors.invalidArgument(`There is no voice ${id} open.`, 'Open it again with openVoice.');
214
+ }
215
+ return voice;
216
+ }
217
+ static where(definition, options) {
218
+ try {
219
+ return definition.where(options);
220
+ }
221
+ catch (_a) {
222
+ return '';
223
+ }
224
+ }
225
+ };
226
+ /** What trying a service hears: nothing is told. */
227
+ VoiceBridge.SILENT = { speech() { }, text() { }, audio() { }, said() { }, closed() { } };
228
+ __decorate([
229
+ Value(CODING_AGENT_OPTIONS),
230
+ __metadata("design:type", Object)
231
+ ], VoiceBridge.prototype, "codingAgentOptions", void 0);
232
+ VoiceBridge = VoiceBridge_1 = __decorate([
233
+ Component(VOICE_BRIDGE)
234
+ ], VoiceBridge);
235
+ export { VoiceBridge };
236
+ /** The voices for pages (TaVoiceServer), each of the connection that opened it. */
237
+ let TaVoiceServerImpl = class TaVoiceServerImpl {
238
+ listVoices() {
239
+ return TaErrors.guard(() => this.bridge.list());
240
+ }
241
+ addVoice(type, options, name) {
242
+ return TaErrors.guard(() => this.bridge.add(type, options !== null && options !== void 0 ? options : {}, name !== null && name !== void 0 ? name : undefined));
243
+ }
244
+ updateVoice(id, options, name) {
245
+ return TaErrors.guard(() => this.bridge.update(id, options !== null && options !== void 0 ? options : {}, name !== null && name !== void 0 ? name : undefined));
246
+ }
247
+ removeVoice(id) {
248
+ return TaErrors.guard(() => this.bridge.remove(id));
249
+ }
250
+ chooseVoice(id) {
251
+ return TaErrors.guard(() => this.bridge.choose(id !== null && id !== void 0 ? id : null));
252
+ }
253
+ setBargeIn(bargeIn) {
254
+ return TaErrors.guard(() => this.bridge.setBargeIn(bargeIn !== null && bargeIn !== void 0 ? bargeIn : {}));
255
+ }
256
+ openVoice(options) {
257
+ return TaErrors.guard(() => this.bridge.openFor(RpcContext.requireConnection(), options === null || options === void 0 ? void 0 : options.language));
258
+ }
259
+ sendVoice(voiceId, audio) {
260
+ return TaErrors.guard(() => this.bridge.send(RpcContext.requireConnection(), voiceId, audio));
261
+ }
262
+ sayVoice(voiceId, text) {
263
+ return TaErrors.guard(() => this.bridge.say(RpcContext.requireConnection(), voiceId, text));
264
+ }
265
+ hushVoice(voiceId) {
266
+ return TaErrors.guard(() => this.bridge.hush(RpcContext.requireConnection(), voiceId));
267
+ }
268
+ closeVoice(voiceId) {
269
+ return TaErrors.guard(() => this.bridge.close(RpcContext.requireConnection(), voiceId));
270
+ }
271
+ };
272
+ __decorate([
273
+ Autowired(VOICE_BRIDGE),
274
+ __metadata("design:type", VoiceBridge)
275
+ ], TaVoiceServerImpl.prototype, "bridge", void 0);
276
+ TaVoiceServerImpl = __decorate([
277
+ Rpc(TaVoiceServer)
278
+ ], TaVoiceServerImpl);
279
+ export { TaVoiceServerImpl };
280
+ //# sourceMappingURL=voice-bridge.js.map
@@ -0,0 +1,38 @@
1
+ import type { TaBargeIn } from '@celljs/ta-protocol';
2
+ /** A service of voices.json: a type a package registered, and the options of its form. */
3
+ export interface VoiceServiceEntry {
4
+ id: string;
5
+ type: string;
6
+ name?: string;
7
+ options: Record<string, unknown>;
8
+ }
9
+ export interface VoicesConfig {
10
+ services?: VoiceServiceEntry[];
11
+ /** The service calls use; none: the voice of the device. */
12
+ use?: string;
13
+ /** When a sound stops what the service reads: any of it, over the defaults (TaBargeIn.resolve). */
14
+ bargeIn?: Partial<TaBargeIn>;
15
+ }
16
+ /** voices.json of the agent directory (docs/design/ta-voice.md 3): its keys stay there, written 0600. */
17
+ export declare class VoicesFile {
18
+ readonly file: string;
19
+ constructor(agentDir: string);
20
+ read(): VoicesConfig;
21
+ /** What reads, or nothing with why it does not parse. */
22
+ safeRead(): {
23
+ config: VoicesConfig;
24
+ error?: string;
25
+ };
26
+ services(): VoiceServiceEntry[];
27
+ /** The service in use, if it is still there. */
28
+ inUse(): VoiceServiceEntry | undefined;
29
+ /** Its id: the type with a number when there is one of it already. */
30
+ add(type: string, options: Record<string, unknown>, name?: string): string;
31
+ /** Its options and name, the rest as it was; throws for one that is not there. */
32
+ update(id: string, options: Record<string, unknown>, name?: string): void;
33
+ /** Sets keys of bargeIn, the others kept. */
34
+ setBargeIn(bargeIn: Partial<TaBargeIn>): void;
35
+ remove(id: string): void;
36
+ use(id: string | undefined): void;
37
+ protected write(config: VoicesConfig): void;
38
+ }