@celljs/ta-host 4.15.2 → 4.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/esm/node/app-commands.d.ts +111 -0
  2. package/esm/node/app-commands.js +629 -0
  3. package/esm/node/delegate-extension.d.ts +11 -0
  4. package/esm/node/delegate-extension.js +33 -0
  5. package/esm/node/directory-relocator.d.ts +49 -0
  6. package/esm/node/directory-relocator.js +267 -0
  7. package/esm/node/exec-endpoint.d.ts +14 -0
  8. package/esm/node/exec-endpoint.js +48 -0
  9. package/esm/node/extension-providers.d.ts +16 -1
  10. package/esm/node/extension-providers.js +23 -3
  11. package/esm/node/gateway-models.d.ts +38 -0
  12. package/esm/node/gateway-models.js +124 -0
  13. package/esm/node/index.d.ts +8 -0
  14. package/esm/node/index.js +8 -0
  15. package/esm/node/keep-awake.d.ts +5 -1
  16. package/esm/node/keep-awake.js +18 -2
  17. package/esm/node/message-page.d.ts +6 -1
  18. package/esm/node/message-page.js +50 -2
  19. package/esm/node/package-hosts.d.ts +6 -0
  20. package/esm/node/package-hosts.js +35 -0
  21. package/esm/node/package-webhooks.d.ts +26 -0
  22. package/esm/node/package-webhooks.js +101 -0
  23. package/esm/node/page-book.d.ts +48 -0
  24. package/esm/node/page-book.js +117 -0
  25. package/esm/node/relay-bridge.d.ts +2 -0
  26. package/esm/node/relay-bridge.js +35 -1
  27. package/esm/node/remote/cloud-hosts.d.ts +40 -0
  28. package/esm/node/remote/cloud-hosts.js +99 -0
  29. package/esm/node/remote/index.d.ts +1 -0
  30. package/esm/node/remote/index.js +1 -0
  31. package/esm/node/remote/pi-directories.d.ts +2 -1
  32. package/esm/node/remote/pi-directories.js +7 -2
  33. package/esm/node/remote/remote-hosts.d.ts +5 -0
  34. package/esm/node/remote/remote-hosts.js +71 -2
  35. package/esm/node/remote/remote-protocol.d.ts +2 -0
  36. package/esm/node/remote/remote-server-impl.d.ts +1 -0
  37. package/esm/node/remote/remote-server-impl.js +3 -0
  38. package/esm/node/rooms-bridge.d.ts +3 -0
  39. package/esm/node/rooms-bridge.js +4 -0
  40. package/esm/node/session-info-util.d.ts +3 -1
  41. package/esm/node/session-info-util.js +32 -6
  42. package/esm/node/ta-server-impl.d.ts +5 -1
  43. package/esm/node/ta-server-impl.js +22 -0
  44. package/esm/node/ta-session-factory.d.ts +13 -1
  45. package/esm/node/ta-session-factory.js +39 -4
  46. package/esm/node/ta-session.d.ts +2 -0
  47. package/esm/node/ta-session.js +1 -0
  48. package/esm/node/team/team-calls.js +23 -0
  49. package/esm/node/voice-bridge.d.ts +68 -0
  50. package/esm/node/voice-bridge.js +273 -0
  51. package/esm/node/voices-file.d.ts +33 -0
  52. package/esm/node/voices-file.js +89 -0
  53. package/lib/node/app-commands.d.ts +112 -0
  54. package/lib/node/app-commands.js +633 -0
  55. package/lib/node/delegate-extension.d.ts +12 -0
  56. package/lib/node/delegate-extension.js +37 -0
  57. package/lib/node/directory-relocator.d.ts +50 -0
  58. package/lib/node/directory-relocator.js +270 -0
  59. package/lib/node/exec-endpoint.d.ts +15 -0
  60. package/lib/node/exec-endpoint.js +51 -0
  61. package/lib/node/extension-providers.d.ts +16 -1
  62. package/lib/node/extension-providers.js +23 -3
  63. package/lib/node/gateway-models.d.ts +39 -0
  64. package/lib/node/gateway-models.js +127 -0
  65. package/lib/node/index.d.ts +8 -0
  66. package/lib/node/index.js +8 -0
  67. package/lib/node/keep-awake.d.ts +5 -1
  68. package/lib/node/keep-awake.js +18 -2
  69. package/lib/node/message-page.d.ts +6 -1
  70. package/lib/node/message-page.js +50 -2
  71. package/lib/node/package-hosts.d.ts +6 -0
  72. package/lib/node/package-hosts.js +35 -0
  73. package/lib/node/package-webhooks.d.ts +27 -0
  74. package/lib/node/package-webhooks.js +104 -0
  75. package/lib/node/page-book.d.ts +49 -0
  76. package/lib/node/page-book.js +120 -0
  77. package/lib/node/relay-bridge.d.ts +2 -0
  78. package/lib/node/relay-bridge.js +34 -0
  79. package/lib/node/remote/cloud-hosts.d.ts +41 -0
  80. package/lib/node/remote/cloud-hosts.js +103 -0
  81. package/lib/node/remote/index.d.ts +1 -0
  82. package/lib/node/remote/index.js +1 -0
  83. package/lib/node/remote/pi-directories.d.ts +2 -1
  84. package/lib/node/remote/pi-directories.js +7 -2
  85. package/lib/node/remote/remote-hosts.d.ts +5 -0
  86. package/lib/node/remote/remote-hosts.js +71 -2
  87. package/lib/node/remote/remote-protocol.d.ts +2 -0
  88. package/lib/node/remote/remote-server-impl.d.ts +1 -0
  89. package/lib/node/remote/remote-server-impl.js +3 -0
  90. package/lib/node/rooms-bridge.d.ts +3 -0
  91. package/lib/node/rooms-bridge.js +4 -0
  92. package/lib/node/session-info-util.d.ts +3 -1
  93. package/lib/node/session-info-util.js +31 -5
  94. package/lib/node/ta-server-impl.d.ts +5 -1
  95. package/lib/node/ta-server-impl.js +22 -0
  96. package/lib/node/ta-session-factory.d.ts +13 -1
  97. package/lib/node/ta-session-factory.js +38 -3
  98. package/lib/node/ta-session.d.ts +2 -0
  99. package/lib/node/ta-session.js +1 -0
  100. package/lib/node/team/team-calls.js +23 -0
  101. package/lib/node/voice-bridge.d.ts +69 -0
  102. package/lib/node/voice-bridge.js +274 -0
  103. package/lib/node/voices-file.d.ts +34 -0
  104. package/lib/node/voices-file.js +93 -0
  105. package/package.json +10 -10
@@ -0,0 +1,633 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ 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;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TaAppCommandsImpl = exports.CoreAppCommands = exports.AppCommandRegistry = exports.TaAppCommands = void 0;
13
+ const node_crypto_1 = require("node:crypto");
14
+ const core_1 = require("@celljs/core");
15
+ const ta_exec_1 = require("@celljs/ta-exec");
16
+ const ta_protocol_1 = require("@celljs/ta-protocol");
17
+ const host_protocol_1 = require("./host-protocol");
18
+ const page_book_1 = require("./page-book");
19
+ const settings_store_1 = require("./settings-store");
20
+ const ta_errors_1 = require("./ta-errors");
21
+ const team_protocol_1 = require("./team/team-protocol");
22
+ exports.TaAppCommands = Symbol('TaAppCommands');
23
+ /** How long the user has to agree to a danger action on their screen. */
24
+ const CONFIRM_MS = 120000;
25
+ /** The actions by id, and who may run them: pure, for TaAppCommandsImpl and its tests. */
26
+ class AppCommandRegistry {
27
+ constructor(access, language = () => ta_protocol_1.TaLanguageUtil.server()) {
28
+ this.access = access;
29
+ this.language = language;
30
+ this.commands = new Map();
31
+ }
32
+ list() {
33
+ return [...this.commands.values()].map(({ command, owner }) => ({
34
+ id: command.id,
35
+ title: command.title,
36
+ description: command.description,
37
+ ...(command.input ? { input: command.input } : {}),
38
+ risk: command.risk,
39
+ action: command.action,
40
+ ...(owner ? { owner } : {}),
41
+ }));
42
+ }
43
+ register(command, owner) {
44
+ var _a;
45
+ if (!/^[a-z][\w-]*\.[a-z][\w-]*$/.test((_a = command.id) !== null && _a !== void 0 ? _a : '')) {
46
+ throw ta_errors_1.TaErrors.invalidArgument(`${JSON.stringify(command.id)} is not the id of an action.`, 'Name it <area>.<verb>: settings.set.');
47
+ }
48
+ if (this.commands.has(command.id)) {
49
+ throw ta_errors_1.TaErrors.invalidArgument(`The action ${command.id} is there already.`, 'Name yours after your package: cron.add.');
50
+ }
51
+ const entry = { command, owner };
52
+ this.commands.set(command.id, entry);
53
+ return () => {
54
+ if (this.commands.get(command.id) === entry) {
55
+ this.commands.delete(command.id);
56
+ }
57
+ };
58
+ }
59
+ dropOwner(owner) {
60
+ for (const [id, entry] of [...this.commands]) {
61
+ if (entry.owner === owner) {
62
+ this.commands.delete(id);
63
+ }
64
+ }
65
+ }
66
+ async run(id, input = {}, options = {}) {
67
+ var _a, _b, _c;
68
+ var _d, _e;
69
+ const command = (_a = this.commands.get(id)) === null || _a === void 0 ? void 0 : _a.command;
70
+ if (!command) {
71
+ throw ta_errors_1.TaErrors.invalidArgument(`There is no action ${JSON.stringify(id)}.`, 'See app_commands for the actions there are.');
72
+ }
73
+ if (!input || typeof input !== 'object' || Array.isArray(input)) {
74
+ throw ta_errors_1.TaErrors.invalidArgument(`The input of ${id} is an object.`, 'Pass { ... } as its schema says.');
75
+ }
76
+ const principals = this.principals(options);
77
+ const refused = principals.find(principal => !this.access.can(principal, command.action));
78
+ if (refused) {
79
+ throw ta_errors_1.TaErrors.invalidArgument(`Who asked this may not do ${id} (${command.action}).`, 'Ask the owner of this Ta to let them, or to do it.');
80
+ }
81
+ const principal = (_d = principals.find(one => one !== ta_protocol_1.TA_OWNER)) !== null && _d !== void 0 ? _d : ta_protocol_1.TA_OWNER;
82
+ // What cannot be undone, or acts for the user, waits for them to agree on their screen; a page's tap is theirs already.
83
+ if (command.risk === 'danger' && !options.principal) {
84
+ const agreed = await ((_c = (_b = this.access).confirm) === null || _c === void 0 ? void 0 : _c.call(_b, principal, (_e = command.title[this.language()]) !== null && _e !== void 0 ? _e : command.title['en-US'], options.sessionFile));
85
+ if (agreed === undefined) {
86
+ throw ta_errors_1.TaErrors.invalidArgument(`${id} needs the user to agree on their screen, and no page of Ta is open.`, 'Tell the user to open Ta, or to do it themselves.');
87
+ }
88
+ if (!agreed) {
89
+ throw ta_errors_1.TaErrors.invalidArgument(`The user did not agree to ${id}.`, 'Do not do it another way; ask them what they want instead.');
90
+ }
91
+ }
92
+ return command.run(input, {
93
+ principal,
94
+ language: this.language(),
95
+ ...(options.sessionFile ? { sessionFile: options.sessionFile } : {}),
96
+ });
97
+ }
98
+ /** Each who asked: the principal given, or those of the run of the conversation; the owner for a run nobody asked. */
99
+ principals(options) {
100
+ if (options.principal) {
101
+ return [options.principal];
102
+ }
103
+ const actors = options.sessionFile ? this.access.actorsOf(options.sessionFile) : [this.access.current()];
104
+ return actors.length ? actors : [ta_protocol_1.TA_OWNER];
105
+ }
106
+ }
107
+ exports.AppCommandRegistry = AppCommandRegistry;
108
+ const words = (en, zh, tw, ja) => ({ 'en-US': en, 'zh-CN': zh, 'zh-TW': tw, 'ja-JP': ja });
109
+ /** pi's settings an action may change: not the shell, which runs any command (danger). */
110
+ const AGENT_SETTINGS = ta_protocol_1.TA_SETTINGS.filter(spec => spec.key !== 'shellPath' && spec.key !== 'shellCommandPrefix');
111
+ /** Built-in packages an action does not turn off: who may do what (ta-team). */
112
+ const KEPT_ON = new Set(['@celljs/ta-team']);
113
+ const CHATS_MAX = 50;
114
+ /** The actions of Ta itself (docs/design/ta-app-use.md 2.5). */
115
+ var CoreAppCommands;
116
+ (function (CoreAppCommands) {
117
+ function all(deps) {
118
+ return [
119
+ settingsGet(deps),
120
+ settingsSet(deps),
121
+ chatsList(deps),
122
+ chatsPin(deps),
123
+ chatsStop(deps),
124
+ asksList(deps),
125
+ asksAnswer(deps),
126
+ extensionsList(deps),
127
+ extensionsSet(deps),
128
+ ];
129
+ }
130
+ CoreAppCommands.all = all;
131
+ async function webSettings(deps) {
132
+ const kept = await deps.getData(ta_protocol_1.TA_WEB_SETTINGS_DATA.name, ta_protocol_1.TA_WEB_SETTINGS_DATA.key);
133
+ return kept && typeof kept === 'object' && !Array.isArray(kept) ? kept : {};
134
+ }
135
+ function settingsGet(deps) {
136
+ return {
137
+ id: 'settings.get',
138
+ title: words('See settings', '查看设置', '檢視設定', '設定を確認'),
139
+ description: 'The settings of Ta: those of its pages (ta: look, voice, the double, the list of conversations) and of the agent (agent: the default model, the thinking level, compaction), each with its value, the values it may take and what it is; models: those that can be chosen. Change them with settings.set.',
140
+ risk: 'read',
141
+ action: 'settings.view',
142
+ run: async () => {
143
+ const [web, agent] = await Promise.all([webSettings(deps), deps.agentSettings()]);
144
+ const ta = ta_protocol_1.TA_WEB_SETTINGS.map(spec => { var _a; return ({
145
+ key: spec.key,
146
+ value: (_a = web[spec.key]) !== null && _a !== void 0 ? _a : spec.default,
147
+ ...(spec.values ? { values: spec.values } : { type: spec.type }),
148
+ what: spec.what,
149
+ }); });
150
+ const pi = AGENT_SETTINGS.map(spec => { var _a, _b, _c; return ({
151
+ key: spec.key,
152
+ value: (_c = (_b = (_a = agent.project[spec.key]) !== null && _a !== void 0 ? _a : agent.user[spec.key]) !== null && _b !== void 0 ? _b : spec.default) !== null && _c !== void 0 ? _c : null,
153
+ ...(spec.values ? { values: spec.values } : { type: spec.type }),
154
+ ...(agent.project[spec.key] !== undefined ? { project: true } : {}),
155
+ }); });
156
+ return { text: 'The settings, in data.', data: { ta, agent: pi, models: agent.models } };
157
+ },
158
+ };
159
+ }
160
+ function settingsSet(deps) {
161
+ return {
162
+ id: 'settings.set',
163
+ title: words('Change settings', '修改设置', '修改設定', '設定を変更'),
164
+ description: 'Changes settings of Ta, those settings.get lists (of ta and of agent, in one object): changes is { key: value }, null for the default of an agent setting. It shows at once on every page of the user.',
165
+ input: {
166
+ type: 'object',
167
+ properties: { changes: { type: 'object', description: '{ "callCaptions": true, "theme": "dark", "model": "anthropic/claude-sonnet-4-5" }' } },
168
+ required: ['changes'],
169
+ },
170
+ risk: 'act',
171
+ action: 'settings.manage',
172
+ run: async (input, context) => {
173
+ var _a;
174
+ var _b, _c;
175
+ const changes = input.changes;
176
+ if (!changes || typeof changes !== 'object' || Array.isArray(changes) || !Object.keys(changes).length) {
177
+ throw ta_errors_1.TaErrors.invalidArgument('changes is an object of the settings to change.', 'Pass { "changes": { "theme": "dark" } }.');
178
+ }
179
+ const web = {};
180
+ const agent = {};
181
+ for (const [key, value] of Object.entries(changes)) {
182
+ const spec = ta_protocol_1.TA_WEB_SETTINGS.find(one => one.key === key);
183
+ if (spec) {
184
+ const wrong = ta_protocol_1.TaAppUtil.check(spec, value);
185
+ if (wrong) {
186
+ throw ta_errors_1.TaErrors.invalidArgument(`${key} is ${wrong}, not ${JSON.stringify(value)}.`, 'See settings.get for the values.');
187
+ }
188
+ web[key] = value;
189
+ }
190
+ else if (AGENT_SETTINGS.some(one => one.key === key)) {
191
+ agent[key] = value;
192
+ }
193
+ else {
194
+ throw ta_errors_1.TaErrors.invalidArgument(`There is no setting ${key} an action changes.`, 'See settings.get for the keys; the others are changed on the settings page.');
195
+ }
196
+ }
197
+ const [kept, snapshot] = await Promise.all([webSettings(deps), deps.agentSettings()]);
198
+ for (const key of ['assistantModel', 'taskModel', 'model']) {
199
+ const model = key in web ? web[key] : agent[key];
200
+ if (typeof model === 'string' && model && snapshot.models.length && !snapshot.models.includes(model)) {
201
+ throw ta_errors_1.TaErrors.invalidArgument(`${model} is no model that can be chosen.`, `Pick one of ${snapshot.models.join(', ')}.`);
202
+ }
203
+ }
204
+ const before = {};
205
+ for (const key of Object.keys(web)) {
206
+ before[key] = (_b = kept[key]) !== null && _b !== void 0 ? _b : (_a = ta_protocol_1.TA_WEB_SETTINGS.find(one => one.key === key)) === null || _a === void 0 ? void 0 : _a.default;
207
+ }
208
+ for (const key of Object.keys(agent)) {
209
+ before[key] = (_c = snapshot.user[key]) !== null && _c !== void 0 ? _c : null;
210
+ }
211
+ if (Object.keys(agent).length) {
212
+ await deps.updateAgentSettings(agent);
213
+ }
214
+ if (Object.keys(web).length) {
215
+ await deps.setData(ta_protocol_1.TA_WEB_SETTINGS_DATA.name, ta_protocol_1.TA_WEB_SETTINGS_DATA.key, { ...kept, ...web });
216
+ }
217
+ const shown = Object.entries(changes).map(([key, value]) => {
218
+ const spec = ta_protocol_1.TA_WEB_SETTINGS.find(one => one.key === key);
219
+ return {
220
+ label: spec ? ta_protocol_1.TaLanguageUtil.pick(context.language, spec.title) : key,
221
+ before: ta_protocol_1.TaAppUtil.shown(before[key], context.language, spec),
222
+ after: ta_protocol_1.TaAppUtil.shown(value, context.language, spec),
223
+ };
224
+ });
225
+ return {
226
+ text: Object.entries(changes)
227
+ .map(([key, value]) => `${key}: ${JSON.stringify(before[key])} → ${JSON.stringify(value)}`)
228
+ .join('; '),
229
+ notice: ta_protocol_1.TaLanguageUtil.pick(context.language, words('Changed settings', '修改了设置', '修改了設定', '設定を変更しました')),
230
+ changes: shown,
231
+ undo: { command: 'settings.set', input: { changes: before } },
232
+ };
233
+ },
234
+ };
235
+ }
236
+ async function chats(deps) {
237
+ const [open, saved, pinnedKept] = await Promise.all([deps.sessions(), deps.saved(), deps.getData(ta_protocol_1.TA_CHAT_PINNED.name, ta_protocol_1.TA_CHAT_PINNED.key)]);
238
+ const pinned = new Set(Array.isArray(pinnedKept) ? pinnedKept.filter((one) => typeof one === 'string') : []);
239
+ const byFile = new Map();
240
+ const cut = (text) => (text && text.length > 80 ? `${text.slice(0, 79)}…` : text);
241
+ for (const one of saved) {
242
+ byFile.set(one.sessionFile, {
243
+ file: one.sessionFile,
244
+ name: one.sessionName || cut(one.firstMessage) || '',
245
+ cwd: one.cwd,
246
+ state: one.asking ? 'asking' : 'idle',
247
+ pinned: pinned.has(one.sessionFile),
248
+ updatedAt: new Date(one.updatedAt).toISOString(),
249
+ ...(one.group ? { group: one.group } : {}),
250
+ ...(one.lastMessage ? { last: cut(one.lastMessage) } : {}),
251
+ });
252
+ }
253
+ for (const one of open) {
254
+ if (!one.sessionFile) {
255
+ continue;
256
+ }
257
+ byFile.set(one.sessionFile, {
258
+ file: one.sessionFile,
259
+ name: one.sessionName || cut(one.firstMessage) || '',
260
+ cwd: one.cwd,
261
+ state: one.isStreaming ? 'working' : one.waiting ? 'waiting' : one.asking ? 'asking' : 'idle',
262
+ pinned: pinned.has(one.sessionFile),
263
+ updatedAt: new Date(one.updatedAt).toISOString(),
264
+ ...(one.group ? { group: one.group } : {}),
265
+ ...(one.lastMessage ? { last: cut(one.lastMessage) } : {}),
266
+ });
267
+ }
268
+ return [...byFile.values()].sort((a, b) => Number(b.pinned) - Number(a.pinned) || b.updatedAt.localeCompare(a.updatedAt));
269
+ }
270
+ async function chatOf(deps, file) {
271
+ if (typeof file !== 'string' || !file) {
272
+ throw ta_errors_1.TaErrors.invalidArgument('file is the session file of a conversation.', 'Find it with chats.list.');
273
+ }
274
+ const found = (await chats(deps)).find(one => one.file === file);
275
+ if (!found) {
276
+ throw ta_errors_1.TaErrors.invalidArgument(`There is no conversation ${file}.`, 'Find it with chats.list.');
277
+ }
278
+ return found;
279
+ }
280
+ function chatsList(deps) {
281
+ return {
282
+ id: 'chats.list',
283
+ title: words('See conversations', '查看对话', '檢視對話', '会話を確認'),
284
+ description: 'The conversations of the list, pinned first then the latest: file (what the other chats actions take), name, cwd, state (working, waiting for a dialog, asking the user, idle), pinned, the last thing said. query: words of the name, the directory or what was said.',
285
+ input: {
286
+ type: 'object',
287
+ properties: { query: { type: 'string' }, limit: { type: 'number', description: `At most ${CHATS_MAX}; 20 at first.` } },
288
+ },
289
+ risk: 'read',
290
+ action: 'chat.view',
291
+ run: async (input) => {
292
+ const query = typeof input.query === 'string' ? input.query.trim().toLowerCase() : '';
293
+ const limit = Math.min(CHATS_MAX, Math.max(1, typeof input.limit === 'number' ? Math.floor(input.limit) : 20));
294
+ const all = await chats(deps);
295
+ const found = query ? all.filter(one => [one.name, one.cwd, one.last].some(text => text === null || text === void 0 ? void 0 : text.toLowerCase().includes(query))) : all;
296
+ return { text: `${found.length} conversations${found.length > limit ? `, the first ${limit}` : ''}.`, data: found.slice(0, limit) };
297
+ },
298
+ };
299
+ }
300
+ function chatsPin(deps) {
301
+ return {
302
+ id: 'chats.pin',
303
+ title: words('Pin conversations', '置顶对话', '置頂對話', '会話をピン留め'),
304
+ description: 'Pins a conversation to the top of the lists, as the user does; pinned: false unpins it. file: of chats.list.',
305
+ input: { type: 'object', properties: { file: { type: 'string' }, pinned: { type: 'boolean' } }, required: ['file'] },
306
+ risk: 'act',
307
+ action: 'chat.manage',
308
+ run: async (input, context) => {
309
+ const chat = await chatOf(deps, input.file);
310
+ const pin = input.pinned !== false;
311
+ const kept = await deps.getData(ta_protocol_1.TA_CHAT_PINNED.name, ta_protocol_1.TA_CHAT_PINNED.key);
312
+ const pinned = Array.isArray(kept) ? kept.filter((one) => typeof one === 'string') : [];
313
+ const name = chat.name || chat.file;
314
+ const undo = { command: 'chats.pin', input: { file: chat.file, pinned: !pin } };
315
+ if (pinned.includes(chat.file) === pin) {
316
+ return { text: `${name} was ${pin ? 'pinned' : 'not pinned'} already.` };
317
+ }
318
+ await deps.setData(ta_protocol_1.TA_CHAT_PINNED.name, ta_protocol_1.TA_CHAT_PINNED.key, pin ? [chat.file, ...pinned] : pinned.filter(one => one !== chat.file));
319
+ return {
320
+ text: `${pin ? 'Pinned' : 'Unpinned'} ${name}.`,
321
+ notice: ta_protocol_1.TaLanguageUtil.pick(context.language, pin
322
+ ? words('Pinned a conversation', '置顶了对话', '置頂了對話', '会話をピン留めしました')
323
+ : words('Unpinned a conversation', '取消置顶对话', '取消置頂對話', '会話のピン留めを外しました')),
324
+ changes: [{ after: name }],
325
+ undo,
326
+ };
327
+ },
328
+ };
329
+ }
330
+ function chatsStop(deps) {
331
+ return {
332
+ id: 'chats.stop',
333
+ title: words('Stop a reply', '停止回复', '停止回覆', '返信を停止'),
334
+ description: 'Stops what a conversation does now, as Stop does. file: of chats.list.',
335
+ input: { type: 'object', properties: { file: { type: 'string' } }, required: ['file'] },
336
+ risk: 'act',
337
+ action: 'chat.stop',
338
+ run: async (input, context) => {
339
+ const chat = await chatOf(deps, input.file);
340
+ const session = (await deps.sessions()).find(one => one.sessionFile === chat.file && one.isStreaming);
341
+ const name = chat.name || chat.file;
342
+ if (!session) {
343
+ return { text: `${name} is doing nothing.` };
344
+ }
345
+ await deps.abort(session.sessionId);
346
+ return {
347
+ text: `Stopped ${name}.`,
348
+ notice: ta_protocol_1.TaLanguageUtil.pick(context.language, words('Stopped a conversation', '停下了对话', '停下了對話', '会話を停止しました')),
349
+ changes: [{ after: name }],
350
+ };
351
+ },
352
+ };
353
+ }
354
+ const isDialog = (request) => ['select', 'confirm', 'input', 'editor'].includes(request.method);
355
+ async function asks(deps) {
356
+ var _a;
357
+ var _b;
358
+ const [open, saved] = await Promise.all([deps.sessions(), deps.saved()]);
359
+ const found = [];
360
+ const name = (one) => one.sessionName || one.firstMessage || one.sessionFile || '';
361
+ for (const one of open) {
362
+ const dialog = deps.pendingUi(one.sessionId).find(isDialog);
363
+ if (!one.sessionFile || (!dialog && !one.asking)) {
364
+ continue;
365
+ }
366
+ found.push(dialog
367
+ ? {
368
+ file: one.sessionFile,
369
+ name: name(one),
370
+ kind: 'dialog',
371
+ question: dialog.method === 'confirm' ? `${dialog.title}: ${dialog.message}` : dialog.title,
372
+ method: dialog.method,
373
+ ...(dialog.method === 'select' ? { choices: dialog.options } : dialog.method === 'confirm' ? { choices: ['yes', 'no'] } : {}),
374
+ sessionId: one.sessionId,
375
+ cwd: one.cwd,
376
+ dialog,
377
+ }
378
+ : {
379
+ file: one.sessionFile,
380
+ name: name(one),
381
+ kind: 'question',
382
+ question: (_b = (_a = one.asking) === null || _a === void 0 ? void 0 : _a.question) !== null && _b !== void 0 ? _b : '',
383
+ sessionId: one.sessionId,
384
+ cwd: one.cwd,
385
+ });
386
+ }
387
+ for (const one of saved) {
388
+ if (one.asking && !found.some(ask => ask.file === one.sessionFile) && !open.some(session => session.sessionFile === one.sessionFile)) {
389
+ found.push({ file: one.sessionFile, name: name(one), kind: 'question', question: one.asking.question, cwd: one.cwd });
390
+ }
391
+ }
392
+ return found;
393
+ }
394
+ function asksList(deps) {
395
+ return {
396
+ id: 'asks.list',
397
+ title: words('See what waits for you', '查看待你决定', '檢視待你決定', 'あなたの判断待ちを確認'),
398
+ description: 'What waits for the user to decide (the list of 待你决定): a dialog of an extension (kind dialog, its choices, or any text for input) or a question an agent asked (kind question), with the file and name of its conversation. Read them out to the user and answer with asks.answer what they say.',
399
+ risk: 'read',
400
+ action: 'chat.view',
401
+ run: async () => {
402
+ const found = (await asks(deps)).map(({ sessionId: _id, cwd: _cwd, dialog: _dialog, ...ask }) => ask);
403
+ return { text: found.length ? `${found.length} wait for the user.` : 'Nothing waits for the user.', data: found };
404
+ },
405
+ };
406
+ }
407
+ /** What the user said, as the answer of a dialog: one of its choices, by its words or its number; yes or no. */
408
+ function answerOf(dialog, answer) {
409
+ var _a, _b, _c;
410
+ const said = typeof answer === 'boolean'
411
+ ? answer
412
+ ? 'yes'
413
+ : 'no'
414
+ : typeof answer === 'number'
415
+ ? String(answer)
416
+ : typeof answer === 'string'
417
+ ? answer.trim()
418
+ : '';
419
+ if (dialog.method === 'confirm') {
420
+ if (/^(y|yes|true|ok|confirm|allow|是|好|对|可以|确认|確認|允许|允許|はい)$/i.test(said)) {
421
+ return { confirmed: true };
422
+ }
423
+ return /^(n|no|false|cancel|deny|否|不|不要|不行|取消|拒绝|拒絕|いいえ)$/i.test(said) ? { confirmed: false } : undefined;
424
+ }
425
+ if (dialog.method === 'select') {
426
+ const options = (_a = dialog.options) !== null && _a !== void 0 ? _a : [];
427
+ const number = /^\d+$/.test(said) ? options[Number(said) - 1] : undefined;
428
+ const value = (_c = (_b = options.find(option => option === said)) !== null && _b !== void 0 ? _b : options.find(option => option.toLowerCase() === said.toLowerCase())) !== null && _c !== void 0 ? _c : number;
429
+ return value === undefined ? undefined : { value };
430
+ }
431
+ return said ? { value: said } : undefined;
432
+ }
433
+ CoreAppCommands.answerOf = answerOf;
434
+ function asksAnswer(deps) {
435
+ return {
436
+ id: 'asks.answer',
437
+ title: words('Answer for you', '替你回答', '替你回答', 'あなたの代わりに回答'),
438
+ description: "Answers what waits for the user (asks.list) with what the user said: file of the conversation; answer: one of a dialog's choices (or its number), yes or no for a confirmation, any text for an input or a question. Only what the user said: never decide for them.",
439
+ input: { type: 'object', properties: { file: { type: 'string' }, answer: { type: 'string' } }, required: ['file', 'answer'] },
440
+ risk: 'act',
441
+ action: 'chat.send',
442
+ run: async (input, context) => {
443
+ const ask = (await asks(deps)).find(one => one.file === input.file);
444
+ if (!ask) {
445
+ throw ta_errors_1.TaErrors.invalidArgument('That conversation waits for nothing.', 'See asks.list for what waits.');
446
+ }
447
+ const notice = ta_protocol_1.TaLanguageUtil.pick(context.language, words('Answered for you', '替你回答了', '替你回答了', 'あなたの代わりに回答しました'));
448
+ if (ask.dialog && ask.sessionId) {
449
+ const response = answerOf(ask.dialog, input.answer);
450
+ if (!response) {
451
+ throw ta_errors_1.TaErrors.invalidArgument(`"${String(input.answer)}" does not answer it${ask.choices ? `: it takes one of ${ask.choices.join(', ')}` : ''}.`, 'Ask the user again.');
452
+ }
453
+ await deps.respondUi(ask.sessionId, { type: 'extension_ui_response', id: ask.dialog.id, ...response });
454
+ const said = 'value' in response ? response.value : ta_protocol_1.TaAppUtil.shown(response.confirmed, context.language);
455
+ return { text: `Answered ${ask.name}: ${said}.`, notice, changes: [{ label: ask.question, after: said }] };
456
+ }
457
+ const text = typeof input.answer === 'string' ? input.answer.trim() : '';
458
+ if (!text) {
459
+ throw ta_errors_1.TaErrors.invalidArgument('answer is what the user said.', 'Pass their words.');
460
+ }
461
+ await deps.send(ask.file, ask.cwd, text);
462
+ return { text: `Told ${ask.name}: ${text}`, notice, changes: [{ label: ask.question, after: text }] };
463
+ },
464
+ };
465
+ }
466
+ function onOff(on, language) {
467
+ return ta_protocol_1.TaAppUtil.shown(on, language);
468
+ }
469
+ function titleOf(pkg, language) {
470
+ var _a, _b;
471
+ const title = pkg.title;
472
+ if (typeof title === 'string') {
473
+ return title;
474
+ }
475
+ return (title && ((_b = (_a = title[language]) !== null && _a !== void 0 ? _a : title[language.slice(0, 2)]) !== null && _b !== void 0 ? _b : title.en)) || pkg.name;
476
+ }
477
+ function extensionsList(deps) {
478
+ return {
479
+ id: 'extensions.list',
480
+ title: words('See extensions', '查看扩展', '檢視擴充功能', '拡張機能を確認'),
481
+ description: 'The extensions (pi packages) of Ta: name, title, on or off, built in or installed. Turn a built-in one on or off with extensions.set.',
482
+ risk: 'read',
483
+ action: 'extensions.use',
484
+ run: async (_input, context) => {
485
+ const packages = await deps.packages();
486
+ return {
487
+ text: `${packages.length} extensions.`,
488
+ data: packages.map(pkg => ({
489
+ name: pkg.name,
490
+ title: titleOf(pkg, context.language),
491
+ enabled: pkg.enabled,
492
+ builtin: pkg.origin === 'builtin',
493
+ })),
494
+ };
495
+ },
496
+ };
497
+ }
498
+ function extensionsSet(deps) {
499
+ return {
500
+ id: 'extensions.set',
501
+ title: words('Turn extensions on or off', '打开或关掉扩展', '開啟或關閉擴充功能', '拡張機能をオン・オフ'),
502
+ description: 'Turns a built-in extension on or off, as the switch of the Extensions page: name (or title) of extensions.list. Its pages and tools come or go at once.',
503
+ input: { type: 'object', properties: { name: { type: 'string' }, enabled: { type: 'boolean' } }, required: ['name', 'enabled'] },
504
+ risk: 'act',
505
+ action: 'extensions.install',
506
+ run: async (input, context) => {
507
+ var _a;
508
+ if (typeof input.name !== 'string' || typeof input.enabled !== 'boolean') {
509
+ throw ta_errors_1.TaErrors.invalidArgument('name is the name of an extension, enabled true or false.', 'Pass { "name": "@celljs/ta-town", "enabled": false }.');
510
+ }
511
+ const wanted = input.name.trim().toLowerCase();
512
+ const packages = await deps.packages();
513
+ const pkg = (_a = packages.find(one => one.name.toLowerCase() === wanted)) !== null && _a !== void 0 ? _a : packages.find(one => titleOf(one, context.language).toLowerCase() === wanted);
514
+ if (!pkg) {
515
+ throw ta_errors_1.TaErrors.invalidArgument(`There is no extension ${input.name}.`, 'See extensions.list.');
516
+ }
517
+ if (pkg.origin !== 'builtin') {
518
+ throw ta_errors_1.TaErrors.invalidArgument(`${pkg.name} is installed, not built in.`, 'Only built-in ones are turned on or off here; the user does the others on the Extensions page.');
519
+ }
520
+ if (KEPT_ON.has(pkg.name) && !input.enabled) {
521
+ throw ta_errors_1.TaErrors.invalidArgument(`${pkg.name} decides who may do what.`, 'Only the user turns it off, on the Extensions page.');
522
+ }
523
+ const title = titleOf(pkg, context.language);
524
+ if (pkg.enabled === input.enabled) {
525
+ return { text: `${pkg.name} was ${input.enabled ? 'on' : 'off'} already.` };
526
+ }
527
+ await deps.setPackageEnabled(pkg.name, input.enabled);
528
+ return {
529
+ text: `Turned ${pkg.name} ${input.enabled ? 'on' : 'off'}.`,
530
+ notice: ta_protocol_1.TaLanguageUtil.pick(context.language, input.enabled
531
+ ? words('Turned on an extension', '打开了扩展', '開啟了擴充功能', '拡張機能をオンにしました')
532
+ : words('Turned off an extension', '关掉了扩展', '關閉了擴充功能', '拡張機能をオフにしました')),
533
+ changes: [{ label: title, before: onOff(!input.enabled, context.language), after: onOff(input.enabled, context.language) }],
534
+ undo: { command: 'extensions.set', input: { name: pkg.name, enabled: !input.enabled } },
535
+ };
536
+ },
537
+ };
538
+ }
539
+ })(CoreAppCommands || (exports.CoreAppCommands = CoreAppCommands = {}));
540
+ /** The catalog of ta serve, with the actions of Ta itself; packages add theirs through their host (host.commands). */
541
+ let TaAppCommandsImpl = class TaAppCommandsImpl {
542
+ list() {
543
+ return this.catalog().list();
544
+ }
545
+ run(id, input, options) {
546
+ return this.catalog().run(id, input, options);
547
+ }
548
+ register(command) {
549
+ return this.catalog().register(command);
550
+ }
551
+ registerOf(owner, command) {
552
+ return this.catalog().register(command, owner);
553
+ }
554
+ dropOwner(owner) {
555
+ this.catalog().dropOwner(owner);
556
+ }
557
+ catalog() {
558
+ if (!this.registry) {
559
+ const team = () => this.teamProvider.team;
560
+ this.registry = new AppCommandRegistry({
561
+ can: (principal, action) => team().can(principal, action),
562
+ actorsOf: file => team().actorsOf(file),
563
+ current: () => team().current(),
564
+ confirm: async (principal, what, sessionFile) => {
565
+ if (!this.pages.has(principal, ta_protocol_1.TA_PAGE_CONFIRM)) {
566
+ return undefined;
567
+ }
568
+ const session = sessionFile ? (await this.server.listSessions()).find(one => one.sessionFile === sessionFile) : undefined;
569
+ const ask = { who: (session === null || session === void 0 ? void 0 : session.sessionName) || (session === null || session === void 0 ? void 0 : session.firstMessage) || 'Ta', what };
570
+ return (await this.pages.ask(principal, ta_protocol_1.TA_PAGE_CONFIRM, ask, CONFIRM_MS)) === true;
571
+ },
572
+ });
573
+ const cwd = () => this.environments.defaultCwd();
574
+ for (const command of CoreAppCommands.all({
575
+ getData: (name, key) => this.packageServer.getExtensionData(name, key),
576
+ setData: (name, key, value) => this.packageServer.setExtensionData(name, key, value),
577
+ agentSettings: async () => this.settings.get(await cwd()),
578
+ updateAgentSettings: async (changes) => {
579
+ await this.settings.update('user', changes, await cwd());
580
+ },
581
+ sessions: () => this.server.listSessions(),
582
+ saved: () => this.server.listSavedSessions(),
583
+ abort: sessionId => this.server.abort(sessionId),
584
+ packages: () => this.packageServer.listPackages(),
585
+ setPackageEnabled: (name, enabled) => this.packageServer.setPackageEnabled(name, enabled),
586
+ pendingUi: sessionId => { var _a; var _b; return (_b = (_a = this.sessions.list().find(one => one.id === sessionId)) === null || _a === void 0 ? void 0 : _a.ui.pendingRequests) !== null && _b !== void 0 ? _b : []; },
587
+ respondUi: (sessionId, response) => this.server.respondUi(sessionId, response),
588
+ send: async (file, cwd, text) => {
589
+ var _a;
590
+ const open = (await this.server.listSessions()).find(one => one.sessionFile === file);
591
+ const sessionId = (_a = open === null || open === void 0 ? void 0 : open.sessionId) !== null && _a !== void 0 ? _a : (await this.server.openSession({ cwd, sessionFile: file })).sessionId;
592
+ await this.server.prompt(sessionId, { clientMessageId: (0, node_crypto_1.randomUUID)(), message: text });
593
+ },
594
+ })) {
595
+ this.registry.register(command);
596
+ }
597
+ }
598
+ return this.registry;
599
+ }
600
+ };
601
+ exports.TaAppCommandsImpl = TaAppCommandsImpl;
602
+ __decorate([
603
+ (0, core_1.Autowired)({ id: ta_protocol_1.TaServer, detached: true }),
604
+ __metadata("design:type", Object)
605
+ ], TaAppCommandsImpl.prototype, "server", void 0);
606
+ __decorate([
607
+ (0, core_1.Autowired)({ id: ta_protocol_1.TaPackageServer, detached: true }),
608
+ __metadata("design:type", Object)
609
+ ], TaAppCommandsImpl.prototype, "packageServer", void 0);
610
+ __decorate([
611
+ (0, core_1.Autowired)({ id: settings_store_1.TaSettingsStore, detached: true }),
612
+ __metadata("design:type", Object)
613
+ ], TaAppCommandsImpl.prototype, "settings", void 0);
614
+ __decorate([
615
+ (0, core_1.Autowired)({ id: ta_exec_1.ExecEnvironmentFactory, detached: true }),
616
+ __metadata("design:type", Object)
617
+ ], TaAppCommandsImpl.prototype, "environments", void 0);
618
+ __decorate([
619
+ (0, core_1.Autowired)({ id: team_protocol_1.TaHostTeamProvider, detached: true }),
620
+ __metadata("design:type", Object)
621
+ ], TaAppCommandsImpl.prototype, "teamProvider", void 0);
622
+ __decorate([
623
+ (0, core_1.Autowired)({ id: page_book_1.TaPageBook, detached: true }),
624
+ __metadata("design:type", Object)
625
+ ], TaAppCommandsImpl.prototype, "pages", void 0);
626
+ __decorate([
627
+ (0, core_1.Autowired)({ id: host_protocol_1.TaSessionRegistry, detached: true }),
628
+ __metadata("design:type", Object)
629
+ ], TaAppCommandsImpl.prototype, "sessions", void 0);
630
+ exports.TaAppCommandsImpl = TaAppCommandsImpl = __decorate([
631
+ (0, core_1.Component)(exports.TaAppCommands)
632
+ ], TaAppCommandsImpl);
633
+ //# sourceMappingURL=app-commands.js.map
@@ -0,0 +1,12 @@
1
+ import type { ExtensionAPI, TaExtension } from '@celljs/ta-harness';
2
+ /**
3
+ * Holds the double to how much the user wants it to hand out (Settings.delegate): in a session whose role has the
4
+ * mark of a level (TaDelegateUtil.mark), its calls of the tools that change files are turned down, and those of its
5
+ * own tools past the steps of a run, each with what to do instead. Other sessions (tasks, chats, agents) have no
6
+ * mark and are left alone.
7
+ */
8
+ export declare class DelegateExtension implements TaExtension {
9
+ readonly name = "ta-delegate";
10
+ register(pi: ExtensionAPI): void;
11
+ }
12
+ //# sourceMappingURL=delegate-extension.d.ts.map