@celljs/ta-app-use 0.0.0-stage → 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.
@@ -0,0 +1,763 @@
1
+ // ../ta-protocol/esm/common/ta-extension-host.js
2
+ var TA_WEBHOOK_MAX_BYTES = 256 * 1024;
3
+ var TA_HOST_EVENT = "ta:host";
4
+ var TaHostUtil;
5
+ (function(TaHostUtil2) {
6
+ TaHostUtil2.WAIT_MS = 1000;
7
+ function reach(events, name, waitMs = TaHostUtil2.WAIT_MS) {
8
+ return new Promise((resolve) => {
9
+ let heard = false;
10
+ let answered = false;
11
+ const timer = setTimeout(() => resolve(undefined), waitMs);
12
+ const request = {
13
+ name,
14
+ heard: () => {
15
+ heard = true;
16
+ },
17
+ resolve: (answer) => {
18
+ answered = true;
19
+ clearTimeout(timer);
20
+ resolve(answer);
21
+ }
22
+ };
23
+ events.emit(TA_HOST_EVENT, request);
24
+ if (!heard && !answered) {
25
+ clearTimeout(timer);
26
+ resolve(undefined);
27
+ }
28
+ });
29
+ }
30
+ TaHostUtil2.reach = reach;
31
+ function marked(systemPrompt, mark) {
32
+ return role(systemPrompt).includes(mark);
33
+ }
34
+ TaHostUtil2.marked = marked;
35
+ function role(systemPrompt) {
36
+ if (!systemPrompt) {
37
+ return "";
38
+ }
39
+ const open = `<addendum>
40
+ `;
41
+ const start = systemPrompt.indexOf(open);
42
+ const context = systemPrompt.indexOf(`<project_context>
43
+ `);
44
+ if (start < 0 || context >= 0 && context < start) {
45
+ return "";
46
+ }
47
+ const end = systemPrompt.indexOf(`
48
+ </addendum>`, start);
49
+ return systemPrompt.slice(start + open.length, end < 0 ? undefined : end);
50
+ }
51
+ TaHostUtil2.role = role;
52
+ })(TaHostUtil || (TaHostUtil = {}));
53
+ var TA_WITH_OTHERS_MARK = "[ta:speaks-with-others]";
54
+ var TA_ASK_ENTRY = "ta-ask";
55
+ var TaAskUtil;
56
+ (function(TaAskUtil2) {
57
+ const LENGTH = 120;
58
+ function entry(question) {
59
+ const line = question.replace(/\s+/g, " ").trim();
60
+ return { question: line.length > LENGTH ? `${line.slice(0, LENGTH - 1)}…` : line };
61
+ }
62
+ TaAskUtil2.entry = entry;
63
+ function asking(entries) {
64
+ var _a;
65
+ for (let i = entries.length - 1;i >= 0; i--) {
66
+ const entry2 = entries[i];
67
+ if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
68
+ return;
69
+ }
70
+ if (entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY) {
71
+ return read(entry2);
72
+ }
73
+ }
74
+ return;
75
+ }
76
+ TaAskUtil2.asking = asking;
77
+ function next(asked, entry2) {
78
+ var _a;
79
+ if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
80
+ return;
81
+ }
82
+ return entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY ? read(entry2) : asked;
83
+ }
84
+ TaAskUtil2.next = next;
85
+ function read(found) {
86
+ var _a;
87
+ const question = (_a = found.data) === null || _a === undefined ? undefined : _a.question;
88
+ if (typeof question !== "string" || !question.trim()) {
89
+ return;
90
+ }
91
+ const at = found.timestamp ? Date.parse(found.timestamp) : Number.NaN;
92
+ return { question: entry(question).question, at: Number.isNaN(at) ? 0 : at };
93
+ }
94
+ })(TaAskUtil || (TaAskUtil = {}));
95
+
96
+ // ../ta-protocol/esm/common/delegate-protocol.js
97
+ var TaDelegateUtil;
98
+ (function(TaDelegateUtil2) {
99
+ TaDelegateUtil2.LEVELS = ["less", "balanced", "more"];
100
+ TaDelegateUtil2.DEFAULT = "balanced";
101
+ TaDelegateUtil2.WORK_TOOLS = ["read", "bash", "powershell", "edit", "write", "grep", "find", "ls"];
102
+ TaDelegateUtil2.CHANGE_TOOLS = ["edit", "write"];
103
+ const LIMITS = {
104
+ less: { noChanges: false },
105
+ balanced: { noChanges: true, steps: 6 },
106
+ more: { noChanges: true, steps: 2 }
107
+ };
108
+ function parse(value) {
109
+ return TaDelegateUtil2.LEVELS.includes(value) ? value : TaDelegateUtil2.DEFAULT;
110
+ }
111
+ TaDelegateUtil2.parse = parse;
112
+ function mark(level) {
113
+ return `(delegate=${level})`;
114
+ }
115
+ TaDelegateUtil2.mark = mark;
116
+ function levelOf(systemPrompt) {
117
+ const role = TaHostUtil.role(systemPrompt);
118
+ return TaDelegateUtil2.LEVELS.find((level) => role.includes(mark(level)));
119
+ }
120
+ TaDelegateUtil2.levelOf = levelOf;
121
+ function limits(level) {
122
+ return LIMITS[level];
123
+ }
124
+ TaDelegateUtil2.limits = limits;
125
+ function refusal(level, tool, steps) {
126
+ if (!TaDelegateUtil2.WORK_TOOLS.includes(tool)) {
127
+ return;
128
+ }
129
+ const limit = LIMITS[level];
130
+ if (limit.noChanges && TaDelegateUtil2.CHANGE_TOOLS.includes(tool)) {
131
+ return "You do not change files yourself: hand this to a task with start_task, with instructions complete enough to do it. The user set how much you hand out in the settings of the double.";
132
+ }
133
+ if (limit.steps !== undefined && steps >= limit.steps) {
134
+ return "You have looked enough for this: hand the work to a task with start_task now, with what you found in its instructions, and tell the user what you started.";
135
+ }
136
+ return;
137
+ }
138
+ TaDelegateUtil2.refusal = refusal;
139
+ })(TaDelegateUtil || (TaDelegateUtil = {}));
140
+
141
+ // ../ta-protocol/esm/common/language.js
142
+ var TaLanguageUtil;
143
+ (function(TaLanguageUtil2) {
144
+ TaLanguageUtil2.ALL = ["zh-CN", "zh-TW", "en-US", "ja-JP"];
145
+ TaLanguageUtil2.NAMES = { "zh-CN": "简体中文", "zh-TW": "繁體中文", "en-US": "English", "ja-JP": "日本語" };
146
+ function of(tag) {
147
+ const said = (tag !== null && tag !== undefined ? tag : "").toLowerCase().replace(/_/g, "-");
148
+ if (/^zh(\b|-)/.test(said)) {
149
+ return /-hant\b/.test(said) || !/-hans\b/.test(said) && /-(tw|hk|mo)\b/.test(said) ? "zh-TW" : "zh-CN";
150
+ }
151
+ return /^ja(\b|-)/.test(said) ? "ja-JP" : "en-US";
152
+ }
153
+ TaLanguageUtil2.of = of;
154
+ function server(env = typeof process === "undefined" ? {} : process.env) {
155
+ return of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG);
156
+ }
157
+ TaLanguageUtil2.server = server;
158
+ function pick(language, words) {
159
+ return words[of(language)];
160
+ }
161
+ TaLanguageUtil2.pick = pick;
162
+ function say(words) {
163
+ return words[server()];
164
+ }
165
+ TaLanguageUtil2.say = say;
166
+ function chinese(language) {
167
+ return of(language).startsWith("zh");
168
+ }
169
+ TaLanguageUtil2.chinese = chinese;
170
+ function cjk(language) {
171
+ return of(language) !== "en-US";
172
+ }
173
+ TaLanguageUtil2.cjk = cjk;
174
+ function list(language, items) {
175
+ return items.join(cjk(language) ? "、" : ", ");
176
+ }
177
+ TaLanguageUtil2.list = list;
178
+ })(TaLanguageUtil || (TaLanguageUtil = {}));
179
+
180
+ // ../ta-protocol/esm/common/app-protocol.js
181
+ var TA_APP_TOOLS = ["app_commands", "app_run", "app_look", "app_go", "app_do"];
182
+ var FONT_SIZES = ["small", "standard", "large", "xlarge"];
183
+ var words = (en, zh, tw, ja) => ({ "en-US": en, "zh-CN": zh, "zh-TW": tw, "ja-JP": ja });
184
+ var SYSTEM = words("as the system", "跟随系统", "跟隨系統", "システムに合わせる");
185
+ var FONT_SHOWN = {
186
+ small: words("small", "小", "小", "小"),
187
+ standard: words("standard", "标准", "標準", "標準"),
188
+ large: words("large", "大", "大", "大"),
189
+ xlarge: words("extra large", "特大", "特大", "特大")
190
+ };
191
+ var TA_WEB_SETTINGS = [
192
+ {
193
+ key: "theme",
194
+ type: "enum",
195
+ values: ["system", "light", "dark"],
196
+ default: "system",
197
+ what: "Light or dark look; system follows the device.",
198
+ title: words("Theme", "外观", "外觀", "外観"),
199
+ shown: { system: SYSTEM, light: words("light", "浅色", "淺色", "ライト"), dark: words("dark", "深色", "深色", "ダーク") }
200
+ },
201
+ {
202
+ key: "language",
203
+ type: "enum",
204
+ values: ["system", ...TaLanguageUtil.ALL],
205
+ default: "system",
206
+ what: "The language of Ta's pages and of what Ta says; system follows the device.",
207
+ title: words("Language", "语言", "語言", "言語"),
208
+ shown: {
209
+ system: SYSTEM,
210
+ ...Object.fromEntries(TaLanguageUtil.ALL.map((language) => [
211
+ language,
212
+ words(TaLanguageUtil.NAMES[language], TaLanguageUtil.NAMES[language], TaLanguageUtil.NAMES[language], TaLanguageUtil.NAMES[language])
213
+ ]))
214
+ }
215
+ },
216
+ {
217
+ key: "fontSize",
218
+ type: "enum",
219
+ values: FONT_SIZES,
220
+ default: "standard",
221
+ what: "The size of the text of the whole page.",
222
+ title: words("Text size", "字体大小", "字型大小", "文字サイズ"),
223
+ shown: FONT_SHOWN
224
+ },
225
+ {
226
+ key: "chatFontSize",
227
+ type: "enum",
228
+ values: FONT_SIZES,
229
+ default: "standard",
230
+ what: "The size of the text of conversations, on top of fontSize.",
231
+ title: words("Text size of conversations", "对话字体大小", "對話字型大小", "会話の文字サイズ"),
232
+ shown: FONT_SHOWN
233
+ },
234
+ {
235
+ key: "speak",
236
+ type: "boolean",
237
+ default: false,
238
+ what: "Read every reply aloud.",
239
+ title: words("Read replies aloud", "朗读回复", "朗讀回覆", "返信を読み上げ")
240
+ },
241
+ {
242
+ key: "speakTalk",
243
+ type: "boolean",
244
+ default: false,
245
+ what: "Read the reply aloud when the user spoke to the double by voice.",
246
+ title: words("Read aloud what was asked by voice", "语音提问时朗读回复", "語音提問時朗讀回覆", "音声で聞いたら読み上げ")
247
+ },
248
+ {
249
+ key: "callCaptions",
250
+ type: "boolean",
251
+ default: false,
252
+ what: "Show captions in a call: what the user says and what Ta reads.",
253
+ title: words("Call captions", "通话字幕", "通話字幕", "通話の字幕")
254
+ },
255
+ {
256
+ key: "voice",
257
+ type: "string",
258
+ default: "",
259
+ what: "The voice replies are read with (a voiceURI of the device's voices); empty for the most natural one.",
260
+ title: words("Voice", "朗读声音", "朗讀聲音", "読み上げの声")
261
+ },
262
+ {
263
+ key: "confirmTasks",
264
+ type: "boolean",
265
+ default: false,
266
+ what: "The double asks before it hands out work.",
267
+ title: words("Ask before handing out work", "派任务前先问我", "派任務前先問我", "作業を割り当てる前に確認")
268
+ },
269
+ {
270
+ key: "maxTasks",
271
+ type: "number",
272
+ values: [0, 2, 4, 8],
273
+ default: 0,
274
+ what: "How many of the double's tasks run at once; 0 for as many as it starts.",
275
+ title: words("Tasks at once", "同时运行的任务", "同時執行的任務", "同時に実行するタスク")
276
+ },
277
+ {
278
+ key: "delegate",
279
+ type: "enum",
280
+ values: TaDelegateUtil.LEVELS,
281
+ default: TaDelegateUtil.DEFAULT,
282
+ what: "How much of the work the double hands out to tasks rather than doing it itself.",
283
+ title: words("Hand out work", "派活多少", "派活多少", "作業の割り当て"),
284
+ shown: { less: words("less", "少", "少", "少なめ"), balanced: words("balanced", "适中", "適中", "ふつう"), more: words("more", "多", "多", "多め") }
285
+ },
286
+ {
287
+ key: "assistantModel",
288
+ type: "model",
289
+ default: "",
290
+ what: "The model of the double, <provider>/<model id>; empty for the default model.",
291
+ title: words("Model of the double", "分身的模型", "分身的模型", "分身のモデル")
292
+ },
293
+ {
294
+ key: "taskModel",
295
+ type: "model",
296
+ default: "",
297
+ what: "The model of the tasks the double starts; empty for the default model.",
298
+ title: words("Model of tasks", "任务的模型", "任務的模型", "タスクのモデル")
299
+ },
300
+ { key: "reduceMotion", type: "boolean", default: false, what: "Fewer animations.", title: words("Reduce motion", "减少动画", "減少動畫", "動きを減らす") },
301
+ {
302
+ key: "diagramStyle",
303
+ type: "enum",
304
+ values: ["clean", "sketch", "colour", "motion"],
305
+ default: "clean",
306
+ what: "How diagrams in replies draw: in the colours of the page, as by hand, a colour to a node, or coming in with flowing lines.",
307
+ title: words("Diagram style", "图表风格", "圖表風格", "図のスタイル"),
308
+ shown: {
309
+ clean: words("clean", "简洁", "簡潔", "シンプル"),
310
+ sketch: words("sketch", "草图", "草圖", "スケッチ"),
311
+ colour: words("colour", "彩色", "彩色", "カラー"),
312
+ motion: words("motion", "动效", "動效", "モーション")
313
+ }
314
+ },
315
+ {
316
+ key: "keepAliveMinutes",
317
+ type: "number",
318
+ values: [1, 5, 15, 30],
319
+ default: 5,
320
+ what: "How long an extension started on open keeps running once left, in minutes.",
321
+ title: words("Keep extensions running", "扩展离开后保留", "擴充功能離開後保留", "拡張機能を保持")
322
+ },
323
+ {
324
+ key: "chatFoldDays",
325
+ type: "number",
326
+ values: [3, 7, 30, 0],
327
+ default: 7,
328
+ what: "Conversations not updated for these days fold at the end of the list; 0 for none.",
329
+ title: words("Fold old conversations", "折叠旧对话", "摺疊舊對話", "古い会話を折りたたむ")
330
+ },
331
+ {
332
+ key: "chatArchiveDays",
333
+ type: "number",
334
+ values: [30, 90, 365, 0],
335
+ default: 30,
336
+ what: "Conversations not updated for these days are archived (out of the list, found by search); 0 for none.",
337
+ title: words("Archive old conversations", "归档旧对话", "歸檔舊對話", "古い会話をアーカイブ")
338
+ },
339
+ {
340
+ key: "recentShots",
341
+ type: "boolean",
342
+ default: true,
343
+ what: "The screenshot just taken is offered above the message box, once.",
344
+ title: words("Offer recent screenshots", "提示刚截的图", "提示剛截的圖", "撮ったばかりのスクリーンショットを提案")
345
+ }
346
+ ];
347
+ var TaAppUtil;
348
+ (function(TaAppUtil2) {
349
+ function check(spec, value) {
350
+ var _a, _b;
351
+ switch (spec.type) {
352
+ case "boolean":
353
+ return typeof value === "boolean" ? undefined : "true or false";
354
+ case "string":
355
+ return typeof value === "string" ? undefined : "a string";
356
+ case "model":
357
+ return value === "" || typeof value === "string" && /^[^/\s]+\/\S+$/.test(value) ? undefined : "<provider>/<model id>, or empty";
358
+ default:
359
+ return ((_a = spec.values) === null || _a === undefined ? undefined : _a.includes(value)) ? undefined : `one of ${(_b = spec.values) === null || _b === undefined ? undefined : _b.map((one) => JSON.stringify(one)).join(", ")}`;
360
+ }
361
+ }
362
+ TaAppUtil2.check = check;
363
+ function shown(value, language, spec) {
364
+ var _a;
365
+ const named = (_a = spec === null || spec === undefined ? undefined : spec.shown) === null || _a === undefined ? undefined : _a[String(value)];
366
+ if (named) {
367
+ return TaLanguageUtil.pick(language, named);
368
+ }
369
+ if (typeof value === "boolean") {
370
+ return TaLanguageUtil.pick(language, value ? words("on", "开", "開", "オン") : words("off", "关", "關", "オフ"));
371
+ }
372
+ if (value === "" || value === undefined || value === null) {
373
+ return TaLanguageUtil.pick(language, words("default", "默认", "預設", "デフォルト"));
374
+ }
375
+ return String(value);
376
+ }
377
+ TaAppUtil2.shown = shown;
378
+ })(TaAppUtil || (TaAppUtil = {}));
379
+
380
+ // web-src/app-protocol.ts
381
+ var NAME = "@celljs/ta-app-use";
382
+ var DONE_MAX = 20;
383
+
384
+ // web-src/app-util.ts
385
+ var SCROLL_STEPS = ["down", "up", "top", "bottom"];
386
+ var DATA_CHARS = 8000;
387
+ var AppUseUtil;
388
+ ((AppUseUtil) => {
389
+ function find(commands, query) {
390
+ const words2 = (query ?? "").toLowerCase().split(/\s+/).filter(Boolean);
391
+ if (!words2.length) {
392
+ return [...commands];
393
+ }
394
+ return commands.filter((command) => {
395
+ const text = `${command.id} ${command.description}`.toLowerCase();
396
+ return words2.some((word) => text.includes(word));
397
+ });
398
+ }
399
+ AppUseUtil.find = find;
400
+ function listText(commands) {
401
+ if (!commands.length) {
402
+ return "No action matches; app_commands without a query lists them all.";
403
+ }
404
+ return commands.map((command) => {
405
+ const properties = command.input?.properties;
406
+ const required = command.input?.required;
407
+ const input = properties ? ` Input: ${JSON.stringify(properties)}${Array.isArray(required) && required.length ? `, required ${required.join(", ")}` : ""}.` : " No input.";
408
+ return `- ${command.id} (${command.risk}${command.owner ? `, of ${command.owner}` : ""}): ${command.description}${input}`;
409
+ }).join(`
410
+ `);
411
+ }
412
+ AppUseUtil.listText = listText;
413
+ function resultText(result) {
414
+ const lines = [result.text];
415
+ if (result.data !== undefined) {
416
+ const data = JSON.stringify(result.data);
417
+ lines.push(data.length > DATA_CHARS ? `${data.slice(0, DATA_CHARS)}… (cut; narrow it down)` : data);
418
+ }
419
+ if (result.notice) {
420
+ lines.push(`The user's pages show it already, and a notice with Undo.`);
421
+ }
422
+ return lines.join(`
423
+ `);
424
+ }
425
+ AppUseUtil.resultText = resultText;
426
+ function scroll(value) {
427
+ if (value === undefined || value === null || value === "") {
428
+ return;
429
+ }
430
+ if (typeof value === "string" && SCROLL_STEPS.includes(value.trim().toLowerCase())) {
431
+ return value.trim().toLowerCase();
432
+ }
433
+ if (value && typeof value === "object" && !Array.isArray(value)) {
434
+ const { item, find: find2 } = value;
435
+ if (typeof item === "string" && item.trim() && find2 === undefined) {
436
+ return { item: item.trim() };
437
+ }
438
+ if (typeof find2 === "string" && find2.trim() && item === undefined) {
439
+ return { find: find2.trim() };
440
+ }
441
+ }
442
+ throw new Error(`scroll takes ${SCROLL_STEPS.join(", ")}, { "item": "p1.7" } or { "find": "words of a row" }, not ${JSON.stringify(value)}.`);
443
+ }
444
+ AppUseUtil.scroll = scroll;
445
+ function add(done, entry) {
446
+ return [...entries(done), entry].slice(-DONE_MAX);
447
+ }
448
+ AppUseUtil.add = add;
449
+ function entries(done) {
450
+ return Array.isArray(done) ? done.filter((one) => !!one && typeof one === "object" && typeof one.id === "string" && typeof one.notice === "string" && typeof one.at === "number") : [];
451
+ }
452
+ AppUseUtil.entries = entries;
453
+ function fresh(done, since) {
454
+ return entries(done).filter((entry) => entry.at > since && !entry.undone);
455
+ }
456
+ AppUseUtil.fresh = fresh;
457
+ })(AppUseUtil ||= {});
458
+
459
+ // web-src/screen-protocol.ts
460
+ var LOOK_MAX = 3000;
461
+ var OLD_MAX = 200;
462
+ var CONTROLS_MAX = 50;
463
+
464
+ // web-src/screen-text.ts
465
+ var ScreenText;
466
+ ((ScreenText) => {
467
+ const cut = (text, length) => text.length > length ? `${text.slice(0, length - 1)}…` : text;
468
+ const HEAD = /^(The user's screen|Ta's own screen) \(/m;
469
+ function placeNames(places) {
470
+ return places.map((place) => place.title && places.filter((one) => one.title === place.title).length === 1 ? place.title : `${place.id}${place.title ? ` “${place.title}”` : ""}`);
471
+ }
472
+ function scrolled(scrollable) {
473
+ const { at, offset, total, shown } = scrollable.position;
474
+ const span = total !== undefined ? `rows ${offset}–${offset + Math.max(1, shown ?? 1) - 1} of ${total}` : `${offset}% down`;
475
+ const where = at === "top" ? "at the top" : at === "bottom" ? "at the bottom" : "in the middle";
476
+ const side = scrollable.more === "top" ? "above" : "below";
477
+ const more = scrollable.more === "none" ? "all loaded" : scrollable.loading ? `more loading ${side}: look again` : scrollable.more ? `more ${side} as it scrolls ${scrollable.more === "top" ? "up" : "down"}` : "";
478
+ return [span, where, more].filter(Boolean).join(", ");
479
+ }
480
+ ScreenText.scrolled = scrolled;
481
+ function rowId(partIndex, part, itemIndex) {
482
+ const item = part.items[itemIndex];
483
+ if (item?.at !== undefined) {
484
+ return `p${partIndex + 1}.${item.at}`;
485
+ }
486
+ if (part.items.some((one) => one.at !== undefined)) {
487
+ return `p${partIndex + 1}.f${part.items.slice(0, itemIndex + 1).filter((one) => one.at === undefined).length}`;
488
+ }
489
+ return `p${partIndex + 1}.${itemIndex + 1}`;
490
+ }
491
+ ScreenText.rowId = rowId;
492
+ function whole(text) {
493
+ return HEAD.test(text);
494
+ }
495
+ ScreenText.whole = whole;
496
+ const heads = (line) => /^\[p\d+\]/.test(line) || line === "Also on the screen:" || /^ {2}Dialog “/.test(line);
497
+ function changes(before, after) {
498
+ if (!before || !whole(before) || !whole(after) || before.split(`
499
+ `)[0] !== after.split(`
500
+ `)[0]) {
501
+ return;
502
+ }
503
+ const old = new Set(before.split(`
504
+ `));
505
+ const lines = after.split(`
506
+ `);
507
+ const now = new Set(lines);
508
+ const body = lines.filter((line) => line.trim());
509
+ const changed = body.filter((line) => !old.has(line));
510
+ if (changed.length > Math.max(3, body.length * 0.4)) {
511
+ return;
512
+ }
513
+ const gone = before.split(`
514
+ `).filter((line) => line.trim() && !now.has(line)).length - changed.length;
515
+ const out = [];
516
+ let head;
517
+ let said;
518
+ for (const line of lines) {
519
+ if (heads(line)) {
520
+ head = line;
521
+ }
522
+ if (!line.trim() || old.has(line)) {
523
+ continue;
524
+ }
525
+ if (head && head !== line && said !== head) {
526
+ out.push(head);
527
+ }
528
+ out.push(line);
529
+ said = head;
530
+ }
531
+ if (!out.length) {
532
+ return gone > 0 ? `Nothing new on the screen; ${gone} line(s) went away.` : "Nothing else on the screen changed.";
533
+ }
534
+ return `What changed (the rest is as before, with the same ids):
535
+ ${out.join(`
536
+ `)}${gone > 0 ? `
537
+ (${gone} more line(s) went away)` : ""}`;
538
+ }
539
+ ScreenText.changes = changes;
540
+ function forget(messages, tools) {
541
+ const textOf = (message) => Array.isArray(message.content) ? message.content.map((part) => part.type === "text" ? part.text ?? "" : "").join("") : typeof message.content === "string" ? message.content : "";
542
+ const sight = (message) => message.role === "toolResult" && tools.includes(message.toolName ?? "");
543
+ let last = -1;
544
+ messages.forEach((message, index) => {
545
+ if (sight(message) && whole(textOf(message))) {
546
+ last = index;
547
+ }
548
+ });
549
+ let cut2 = false;
550
+ const out = messages.map((message, index) => {
551
+ const text = index < last && sight(message) ? textOf(message) : "";
552
+ if (text.length <= OLD_MAX) {
553
+ return message;
554
+ }
555
+ cut2 = true;
556
+ const first = text.split(`
557
+ `).find((line) => line.trim()) ?? "";
558
+ return { ...message, content: [{ type: "text", text: `${cut1(first)} … (an older sight of the screen, left out: a later one is below)` }] };
559
+ });
560
+ return cut2 ? out : undefined;
561
+ }
562
+ ScreenText.forget = forget;
563
+ const cut1 = (line) => cut(line, 160);
564
+ function look(sight, only) {
565
+ const targets = new Map;
566
+ const lines = [];
567
+ if (!only) {
568
+ lines.push(`${sight.whose === "ta" ? "Ta's own screen" : "The user's screen"} (${sight.device}): ${sight.where || "starting"}`);
569
+ if (sight.places.length) {
570
+ lines.push(`Places (app_go to): ${placeNames(sight.places).join(" · ")} · chat:<name> a conversation`);
571
+ }
572
+ if (sight.headings.length) {
573
+ lines.push(`Headings: ${sight.headings.map((heading) => cut(heading, 60)).join(" · ")}`);
574
+ }
575
+ const page = sight.scroll;
576
+ const index = page?.part ? sight.parts.findIndex((part) => part.id === page.part) : -1;
577
+ if (page) {
578
+ lines.push(`Scrolls (app_do scroll, no id): ${index >= 0 ? `[p${index + 1}]` : `the page, ${scrolled(page.scrollable)}`}`);
579
+ }
580
+ }
581
+ sight.parts.forEach((part, index) => {
582
+ const id = `p${index + 1}`;
583
+ targets.set(id, { part: part.id });
584
+ if (only && only !== part.id && only !== id) {
585
+ return;
586
+ }
587
+ lines.push("", `[${id}] ${part.title}${part.state ? ` — ${part.state}` : ""}${part.floats ? " (over every page)" : ""}`);
588
+ if (part.scrollable) {
589
+ lines.push(` scrolls: ${scrolled(part.scrollable)}`);
590
+ }
591
+ part.items.forEach((item, itemIndex) => {
592
+ const itemId = rowId(index, part, itemIndex);
593
+ targets.set(itemId, { part: part.id, item: item.id });
594
+ lines.push(` [${itemId}] ${cut(item.text, 200)}${item.marks?.length ? ` · ${item.marks.map((mark) => cut(mark, 80)).join(" · ")}` : ""}`);
595
+ });
596
+ if (part.actions.length) {
597
+ const actions = part.actions.map((action) => `${action.name}${action.label.toLowerCase() !== action.name ? ` “${action.label}”` : ""}${action.each ? " [a row]" : ""}${action.takes ? ` (${action.takes})` : ""}${action.command ? ` = app_run ${action.command}` : ""}${action.danger ? " !danger" : ""}`);
598
+ lines.push(` does: ${actions.join(" · ")}`);
599
+ }
600
+ });
601
+ const controls = sight.controls.slice(0, CONTROLS_MAX);
602
+ controls.forEach((_control, index) => {
603
+ targets.set(`e${index + 1}`, { control: index });
604
+ });
605
+ if (!only && controls.length) {
606
+ lines.push("", "Also on the screen:");
607
+ let dialog;
608
+ controls.forEach((control, index) => {
609
+ if (control.dialog !== dialog) {
610
+ dialog = control.dialog;
611
+ if (dialog !== undefined) {
612
+ lines.push(` Dialog “${cut(dialog, 80)}”:`);
613
+ }
614
+ }
615
+ const state = control.checked !== undefined ? control.checked ? " on" : " off" : control.value !== undefined ? ` = “${cut(control.value, 80)}”` : "";
616
+ lines.push(` ${control.dialog !== undefined ? " " : ""}[e${index + 1}] ${control.kind} “${cut(control.label, 80)}”${state}${control.danger ? " !danger" : ""}`);
617
+ });
618
+ if (sight.controls.length > controls.length) {
619
+ lines.push(` … ${sight.controls.length - controls.length} more: look at a part, or go elsewhere.`);
620
+ }
621
+ }
622
+ const all = lines.join(`
623
+ `).trim();
624
+ return { text: all.length > LOOK_MAX ? `${all.slice(0, LOOK_MAX - 40)}
625
+ … (more: app_look a part)` : all, targets };
626
+ }
627
+ ScreenText.look = look;
628
+ })(ScreenText ||= {});
629
+
630
+ // web-src/extension.ts
631
+ var NO_SERVER = "Doing things to Ta needs the Ta service: start it with `ta serve`.";
632
+ var APP_RUN_DESCRIPTION = "Does something to Ta itself at once, as the user would on its pages but without them: change its settings (look, language, voice, captions of calls, the double, models), list, pin or stop conversations, read and answer what waits for the user to decide (with what they said), turn a built-in extension on or off. Prefer it to operating the screen: it is quick, needs no page open, and the user's pages show the change at once, with a notice and Undo. command: settings.get, settings.set, chats.list, chats.pin, chats.stop, asks.list, asks.answer, extensions.list, extensions.set, or another of app_commands (packages add theirs); input: as app_commands says. Do what the user asked; ask before anything they did not.";
633
+ var APART = {
634
+ type: "boolean",
635
+ description: "On Ta's own screen beside the user's, for long work they need not watch or that should not move their page; else on theirs."
636
+ };
637
+ var SCREEN_TOOLS = [
638
+ {
639
+ name: "app_look",
640
+ label: "Look at the screen",
641
+ description: "What is on the user's screen of Ta now, the very page they see, on the device they use (a phone's on a phone, a computer's on a computer): where it is, the places to go, the parts (the list of conversations, the conversation, the settings...) with their rows and what each does, and what else can be clicked or typed into, each with an id for app_do. A part that scrolls lists only its rows in sight and says where it is (`scrolls: rows 3–12 of 40, in the middle, more below as it scrolls down`; `all loaded` at the real end), and `Scrolls` names what scrolls without an id: scroll with app_do to read more. A call going on is a part over every page, listed first (Call: what it is at, with which conversation, captions on or off, on which device when on another of the user's): hang it up, interrupt Ta, turn captions on or off with app_do from any page. You work it as the user's shadow, as they talk with you (in a call too): they see your cursor do each step. part: one part only. apart: true reads Ta's own screen instead. To change data without the screen, app_run first.",
642
+ parameters: { type: "object", properties: { part: { type: "string" }, apart: APART } }
643
+ },
644
+ {
645
+ name: "app_go",
646
+ label: "Go on the screen",
647
+ description: "Goes to a place on the user's screen, as they would with the navigation, and says what is there then, as app_look: a place of app_look (list:..., page:<id>, discover, settings, settings:<section>) by its id or title, or a conversation (chat:<its name>). A call goes on while their screen shows something else. apart: true goes on Ta's own screen.",
648
+ parameters: { type: "object", properties: { to: { type: "string" }, apart: APART }, required: ["to"] }
649
+ },
650
+ {
651
+ name: "app_do",
652
+ label: "Do on the screen",
653
+ description: `Does something on the user's screen, as they would, their button pressed under your cursor, and says what was done and what changed since you looked (the whole screen when it is another). id: of the last app_look ([p1.2] a row, [p1] a part, [e3] a control); or, without a look first, find: its words as the screen shows them ("Captions"), with to: a place to go to first, as app_go (a step less, quicker in a call; when not found, you get the screen to choose by id). action: what of a part (open, pin, send, answer...; a row opens without), none to click a control; a text box takes input.text (input.submit: true presses Enter), a choice input.text. What cannot be undone waits for the user to agree on their screen. scroll, in place of action, scrolls the part id names ([p1]), or what app_look says scrolls without an id: "down" or "up" most of a screen (one screen a call at most: call again for more), "top" or "bottom" to an end, {"item": "p1.7"} a row of the last look, {"find": "words"} a row by its words (one drawn but out of sight too; one not drawn yet comes as you scroll toward it); it says where it is then and the rows that came into sight. A game (a part \`game-<n>\`: "Breakout — running · canvas 390×678 · score 12") is played by its actions, x and y from 0 to 1 across and down its canvas: point {x, y} taps there (to start, a button, a cell of a board); move {x, y?} puts the pointer there (a paddle follows it; y kept without), with drag: true drags it there pressed; key {key} presses ArrowLeft, ArrowRight, ArrowUp, ArrowDown, Space, Enter, Escape, a letter or a digit; hold {ms, x, y} or {ms, key} presses that long (30 to 5000 ms: to charge a jump). Each call is one step and answers with the game's state (its score, when it tells it): play in steps, not frame by frame. apart: true does it on Ta's own screen, where an action marked = app_run runs that action without the screen unless show: true.`,
654
+ parameters: {
655
+ type: "object",
656
+ properties: {
657
+ id: { type: "string" },
658
+ find: { type: "string" },
659
+ to: { type: "string" },
660
+ action: { type: "string" },
661
+ scroll: {
662
+ anyOf: [
663
+ { type: "string", enum: SCROLL_STEPS },
664
+ { type: "object", properties: { item: { type: "string" }, find: { type: "string" } } }
665
+ ]
666
+ },
667
+ input: { type: "object" },
668
+ show: { type: "boolean" },
669
+ apart: APART
670
+ }
671
+ }
672
+ }
673
+ ];
674
+ function activate(pi) {
675
+ let reached;
676
+ const reach = () => reached ??= TaHostUtil.reach(pi.events, NAME).then((answer) => ({
677
+ answered: !!answer,
678
+ app: answer?.service?.service
679
+ }));
680
+ const others = (ctx) => TaHostUtil.marked(ctx.getSystemPrompt(), TA_WITH_OTHERS_MARK);
681
+ const ours = (name) => TA_APP_TOOLS.includes(name);
682
+ pi.on("before_agent_start", (_event, ctx) => {
683
+ if (others(ctx)) {
684
+ const active = pi.getActiveTools();
685
+ if (active.some(ours)) {
686
+ pi.setActiveTools(active.filter((name) => !ours(name)));
687
+ }
688
+ }
689
+ });
690
+ const screenTools = SCREEN_TOOLS.map((tool) => tool.name);
691
+ pi.on("context", (event) => {
692
+ const messages = ScreenText.forget(event.messages, screenTools);
693
+ return messages ? { messages } : undefined;
694
+ });
695
+ pi.on("tool_call", (event, ctx) => others(ctx) && ours(event.toolName) ? { block: true, reason: "Here you speak with other people: what is the user's is not yours to change." } : undefined);
696
+ const tools = [
697
+ {
698
+ name: "app_commands",
699
+ label: "Find what Ta can do",
700
+ description: "The actions of Ta that app_run takes: id, what each does, its input and its risk; query: words to find some (settings, pin, extension), none for all.",
701
+ parameters: { type: "object", properties: { query: { type: "string" } } },
702
+ run: async (app, params) => AppUseUtil.listText(app.commands(typeof params.query === "string" ? params.query : undefined))
703
+ },
704
+ {
705
+ name: "app_run",
706
+ label: "Do it in Ta",
707
+ description: APP_RUN_DESCRIPTION,
708
+ parameters: {
709
+ type: "object",
710
+ properties: { command: { type: "string" }, input: { type: "object" } },
711
+ required: ["command"]
712
+ },
713
+ run: async (app, params, ctx) => {
714
+ const input = params.input && typeof params.input === "object" && !Array.isArray(params.input) ? params.input : {};
715
+ return AppUseUtil.resultText(await app.run(String(params.command ?? ""), input, ctx.sessionManager?.getSessionFile()));
716
+ }
717
+ }
718
+ ];
719
+ const object = (value) => value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
720
+ for (const tool of SCREEN_TOOLS) {
721
+ tools.push({
722
+ ...tool,
723
+ run: async (app, params, ctx) => {
724
+ const kind = tool.name === "app_look" ? "look" : tool.name === "app_go" ? "go" : "do";
725
+ const input = object(params.input);
726
+ const scroll = kind === "do" ? AppUseUtil.scroll(params.scroll) : undefined;
727
+ return app.screen({
728
+ kind,
729
+ ...typeof params.part === "string" && params.part ? { part: params.part } : {},
730
+ ...typeof params.to === "string" ? { to: params.to } : {},
731
+ ...typeof params.id === "string" && params.id ? { id: params.id } : {},
732
+ ...typeof params.find === "string" && params.find.trim() ? { find: params.find.trim() } : {},
733
+ ...typeof params.action === "string" && params.action && !scroll ? { action: params.action } : {},
734
+ ...scroll ? { scroll } : {},
735
+ ...input ? { input } : {},
736
+ ...params.apart === true ? { apart: true } : {},
737
+ ...params.show === true ? { show: true } : {}
738
+ }, ctx.sessionManager?.getSessionFile());
739
+ }
740
+ });
741
+ }
742
+ for (const tool of tools) {
743
+ pi.registerTool({
744
+ name: tool.name,
745
+ label: tool.label,
746
+ description: tool.description,
747
+ parameters: tool.parameters,
748
+ execute: async (_id, params, _signal, _update, ctx) => {
749
+ const { answered, app } = await reach();
750
+ if (!app) {
751
+ throw new Error(answered ? "ta-app-use did not start in the Ta service; its log says why." : NO_SERVER);
752
+ }
753
+ const text = await tool.run(app, params ?? {}, ctx);
754
+ return { content: [{ type: "text", text }], details: undefined };
755
+ }
756
+ });
757
+ }
758
+ }
759
+ export {
760
+ activate as default,
761
+ SCREEN_TOOLS,
762
+ APP_RUN_DESCRIPTION
763
+ };