@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.
- package/README.en.md +29 -0
- package/README.md +28 -2
- package/extension/index.mjs +763 -0
- package/host/index.mjs +156 -0
- package/package.json +60 -3
- package/web/index.js +2153 -0
package/web/index.js
ADDED
|
@@ -0,0 +1,2153 @@
|
|
|
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_ASK_ENTRY = "ta-ask";
|
|
54
|
+
var TaAskUtil;
|
|
55
|
+
(function(TaAskUtil2) {
|
|
56
|
+
const LENGTH = 120;
|
|
57
|
+
function entry(question) {
|
|
58
|
+
const line = question.replace(/\s+/g, " ").trim();
|
|
59
|
+
return { question: line.length > LENGTH ? `${line.slice(0, LENGTH - 1)}…` : line };
|
|
60
|
+
}
|
|
61
|
+
TaAskUtil2.entry = entry;
|
|
62
|
+
function asking(entries) {
|
|
63
|
+
var _a;
|
|
64
|
+
for (let i = entries.length - 1;i >= 0; i--) {
|
|
65
|
+
const entry2 = entries[i];
|
|
66
|
+
if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY) {
|
|
70
|
+
return read(entry2);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
TaAskUtil2.asking = asking;
|
|
76
|
+
function next(asked, entry2) {
|
|
77
|
+
var _a;
|
|
78
|
+
if (entry2.type === "message" && ((_a = entry2.message) === null || _a === undefined ? undefined : _a.role) === "user") {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
return entry2.type === "custom" && entry2.customType === TA_ASK_ENTRY ? read(entry2) : asked;
|
|
82
|
+
}
|
|
83
|
+
TaAskUtil2.next = next;
|
|
84
|
+
function read(found) {
|
|
85
|
+
var _a;
|
|
86
|
+
const question = (_a = found.data) === null || _a === undefined ? undefined : _a.question;
|
|
87
|
+
if (typeof question !== "string" || !question.trim()) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
const at = found.timestamp ? Date.parse(found.timestamp) : Number.NaN;
|
|
91
|
+
return { question: entry(question).question, at: Number.isNaN(at) ? 0 : at };
|
|
92
|
+
}
|
|
93
|
+
})(TaAskUtil || (TaAskUtil = {}));
|
|
94
|
+
|
|
95
|
+
// ../ta-protocol/esm/common/delegate-protocol.js
|
|
96
|
+
var TaDelegateUtil;
|
|
97
|
+
(function(TaDelegateUtil2) {
|
|
98
|
+
TaDelegateUtil2.LEVELS = ["less", "balanced", "more"];
|
|
99
|
+
TaDelegateUtil2.DEFAULT = "balanced";
|
|
100
|
+
TaDelegateUtil2.WORK_TOOLS = ["read", "bash", "powershell", "edit", "write", "grep", "find", "ls"];
|
|
101
|
+
TaDelegateUtil2.CHANGE_TOOLS = ["edit", "write"];
|
|
102
|
+
const LIMITS = {
|
|
103
|
+
less: { noChanges: false },
|
|
104
|
+
balanced: { noChanges: true, steps: 6 },
|
|
105
|
+
more: { noChanges: true, steps: 2 }
|
|
106
|
+
};
|
|
107
|
+
function parse(value) {
|
|
108
|
+
return TaDelegateUtil2.LEVELS.includes(value) ? value : TaDelegateUtil2.DEFAULT;
|
|
109
|
+
}
|
|
110
|
+
TaDelegateUtil2.parse = parse;
|
|
111
|
+
function mark(level) {
|
|
112
|
+
return `(delegate=${level})`;
|
|
113
|
+
}
|
|
114
|
+
TaDelegateUtil2.mark = mark;
|
|
115
|
+
function levelOf(systemPrompt) {
|
|
116
|
+
const role = TaHostUtil.role(systemPrompt);
|
|
117
|
+
return TaDelegateUtil2.LEVELS.find((level) => role.includes(mark(level)));
|
|
118
|
+
}
|
|
119
|
+
TaDelegateUtil2.levelOf = levelOf;
|
|
120
|
+
function limits(level) {
|
|
121
|
+
return LIMITS[level];
|
|
122
|
+
}
|
|
123
|
+
TaDelegateUtil2.limits = limits;
|
|
124
|
+
function refusal(level, tool, steps) {
|
|
125
|
+
if (!TaDelegateUtil2.WORK_TOOLS.includes(tool)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const limit = LIMITS[level];
|
|
129
|
+
if (limit.noChanges && TaDelegateUtil2.CHANGE_TOOLS.includes(tool)) {
|
|
130
|
+
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.";
|
|
131
|
+
}
|
|
132
|
+
if (limit.steps !== undefined && steps >= limit.steps) {
|
|
133
|
+
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.";
|
|
134
|
+
}
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
TaDelegateUtil2.refusal = refusal;
|
|
138
|
+
})(TaDelegateUtil || (TaDelegateUtil = {}));
|
|
139
|
+
|
|
140
|
+
// ../ta-protocol/esm/common/language.js
|
|
141
|
+
var TaLanguageUtil;
|
|
142
|
+
(function(TaLanguageUtil2) {
|
|
143
|
+
TaLanguageUtil2.ALL = ["zh-CN", "zh-TW", "en-US", "ja-JP"];
|
|
144
|
+
TaLanguageUtil2.NAMES = { "zh-CN": "简体中文", "zh-TW": "繁體中文", "en-US": "English", "ja-JP": "日本語" };
|
|
145
|
+
function of(tag) {
|
|
146
|
+
const said = (tag !== null && tag !== undefined ? tag : "").toLowerCase().replace(/_/g, "-");
|
|
147
|
+
if (/^zh(\b|-)/.test(said)) {
|
|
148
|
+
return /-hant\b/.test(said) || !/-hans\b/.test(said) && /-(tw|hk|mo)\b/.test(said) ? "zh-TW" : "zh-CN";
|
|
149
|
+
}
|
|
150
|
+
return /^ja(\b|-)/.test(said) ? "ja-JP" : "en-US";
|
|
151
|
+
}
|
|
152
|
+
TaLanguageUtil2.of = of;
|
|
153
|
+
function server(env = typeof process === "undefined" ? {} : process.env) {
|
|
154
|
+
return of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG);
|
|
155
|
+
}
|
|
156
|
+
TaLanguageUtil2.server = server;
|
|
157
|
+
function pick(language, words) {
|
|
158
|
+
return words[of(language)];
|
|
159
|
+
}
|
|
160
|
+
TaLanguageUtil2.pick = pick;
|
|
161
|
+
function say(words) {
|
|
162
|
+
return words[server()];
|
|
163
|
+
}
|
|
164
|
+
TaLanguageUtil2.say = say;
|
|
165
|
+
function chinese(language) {
|
|
166
|
+
return of(language).startsWith("zh");
|
|
167
|
+
}
|
|
168
|
+
TaLanguageUtil2.chinese = chinese;
|
|
169
|
+
function cjk(language) {
|
|
170
|
+
return of(language) !== "en-US";
|
|
171
|
+
}
|
|
172
|
+
TaLanguageUtil2.cjk = cjk;
|
|
173
|
+
function list(language, items) {
|
|
174
|
+
return items.join(cjk(language) ? "、" : ", ");
|
|
175
|
+
}
|
|
176
|
+
TaLanguageUtil2.list = list;
|
|
177
|
+
})(TaLanguageUtil || (TaLanguageUtil = {}));
|
|
178
|
+
|
|
179
|
+
// ../ta-protocol/esm/common/app-protocol.js
|
|
180
|
+
var TA_APP_TOOLS = ["app_commands", "app_run", "app_look", "app_go", "app_do"];
|
|
181
|
+
var FONT_SIZES = ["small", "standard", "large", "xlarge"];
|
|
182
|
+
var words = (en, zh, tw, ja) => ({ "en-US": en, "zh-CN": zh, "zh-TW": tw, "ja-JP": ja });
|
|
183
|
+
var SYSTEM = words("as the system", "跟随系统", "跟隨系統", "システムに合わせる");
|
|
184
|
+
var FONT_SHOWN = {
|
|
185
|
+
small: words("small", "小", "小", "小"),
|
|
186
|
+
standard: words("standard", "标准", "標準", "標準"),
|
|
187
|
+
large: words("large", "大", "大", "大"),
|
|
188
|
+
xlarge: words("extra large", "特大", "特大", "特大")
|
|
189
|
+
};
|
|
190
|
+
var TA_WEB_SETTINGS = [
|
|
191
|
+
{
|
|
192
|
+
key: "theme",
|
|
193
|
+
type: "enum",
|
|
194
|
+
values: ["system", "light", "dark"],
|
|
195
|
+
default: "system",
|
|
196
|
+
what: "Light or dark look; system follows the device.",
|
|
197
|
+
title: words("Theme", "外观", "外觀", "外観"),
|
|
198
|
+
shown: { system: SYSTEM, light: words("light", "浅色", "淺色", "ライト"), dark: words("dark", "深色", "深色", "ダーク") }
|
|
199
|
+
},
|
|
200
|
+
{
|
|
201
|
+
key: "language",
|
|
202
|
+
type: "enum",
|
|
203
|
+
values: ["system", ...TaLanguageUtil.ALL],
|
|
204
|
+
default: "system",
|
|
205
|
+
what: "The language of Ta's pages and of what Ta says; system follows the device.",
|
|
206
|
+
title: words("Language", "语言", "語言", "言語"),
|
|
207
|
+
shown: {
|
|
208
|
+
system: SYSTEM,
|
|
209
|
+
...Object.fromEntries(TaLanguageUtil.ALL.map((language) => [
|
|
210
|
+
language,
|
|
211
|
+
words(TaLanguageUtil.NAMES[language], TaLanguageUtil.NAMES[language], TaLanguageUtil.NAMES[language], TaLanguageUtil.NAMES[language])
|
|
212
|
+
]))
|
|
213
|
+
}
|
|
214
|
+
},
|
|
215
|
+
{
|
|
216
|
+
key: "fontSize",
|
|
217
|
+
type: "enum",
|
|
218
|
+
values: FONT_SIZES,
|
|
219
|
+
default: "standard",
|
|
220
|
+
what: "The size of the text of the whole page.",
|
|
221
|
+
title: words("Text size", "字体大小", "字型大小", "文字サイズ"),
|
|
222
|
+
shown: FONT_SHOWN
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
key: "chatFontSize",
|
|
226
|
+
type: "enum",
|
|
227
|
+
values: FONT_SIZES,
|
|
228
|
+
default: "standard",
|
|
229
|
+
what: "The size of the text of conversations, on top of fontSize.",
|
|
230
|
+
title: words("Text size of conversations", "对话字体大小", "對話字型大小", "会話の文字サイズ"),
|
|
231
|
+
shown: FONT_SHOWN
|
|
232
|
+
},
|
|
233
|
+
{
|
|
234
|
+
key: "speak",
|
|
235
|
+
type: "boolean",
|
|
236
|
+
default: false,
|
|
237
|
+
what: "Read every reply aloud.",
|
|
238
|
+
title: words("Read replies aloud", "朗读回复", "朗讀回覆", "返信を読み上げ")
|
|
239
|
+
},
|
|
240
|
+
{
|
|
241
|
+
key: "speakTalk",
|
|
242
|
+
type: "boolean",
|
|
243
|
+
default: false,
|
|
244
|
+
what: "Read the reply aloud when the user spoke to the double by voice.",
|
|
245
|
+
title: words("Read aloud what was asked by voice", "语音提问时朗读回复", "語音提問時朗讀回覆", "音声で聞いたら読み上げ")
|
|
246
|
+
},
|
|
247
|
+
{
|
|
248
|
+
key: "callCaptions",
|
|
249
|
+
type: "boolean",
|
|
250
|
+
default: false,
|
|
251
|
+
what: "Show captions in a call: what the user says and what Ta reads.",
|
|
252
|
+
title: words("Call captions", "通话字幕", "通話字幕", "通話の字幕")
|
|
253
|
+
},
|
|
254
|
+
{
|
|
255
|
+
key: "voice",
|
|
256
|
+
type: "string",
|
|
257
|
+
default: "",
|
|
258
|
+
what: "The voice replies are read with (a voiceURI of the device's voices); empty for the most natural one.",
|
|
259
|
+
title: words("Voice", "朗读声音", "朗讀聲音", "読み上げの声")
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
key: "confirmTasks",
|
|
263
|
+
type: "boolean",
|
|
264
|
+
default: false,
|
|
265
|
+
what: "The double asks before it hands out work.",
|
|
266
|
+
title: words("Ask before handing out work", "派任务前先问我", "派任務前先問我", "作業を割り当てる前に確認")
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
key: "maxTasks",
|
|
270
|
+
type: "number",
|
|
271
|
+
values: [0, 2, 4, 8],
|
|
272
|
+
default: 0,
|
|
273
|
+
what: "How many of the double's tasks run at once; 0 for as many as it starts.",
|
|
274
|
+
title: words("Tasks at once", "同时运行的任务", "同時執行的任務", "同時に実行するタスク")
|
|
275
|
+
},
|
|
276
|
+
{
|
|
277
|
+
key: "delegate",
|
|
278
|
+
type: "enum",
|
|
279
|
+
values: TaDelegateUtil.LEVELS,
|
|
280
|
+
default: TaDelegateUtil.DEFAULT,
|
|
281
|
+
what: "How much of the work the double hands out to tasks rather than doing it itself.",
|
|
282
|
+
title: words("Hand out work", "派活多少", "派活多少", "作業の割り当て"),
|
|
283
|
+
shown: { less: words("less", "少", "少", "少なめ"), balanced: words("balanced", "适中", "適中", "ふつう"), more: words("more", "多", "多", "多め") }
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
key: "assistantModel",
|
|
287
|
+
type: "model",
|
|
288
|
+
default: "",
|
|
289
|
+
what: "The model of the double, <provider>/<model id>; empty for the default model.",
|
|
290
|
+
title: words("Model of the double", "分身的模型", "分身的模型", "分身のモデル")
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
key: "taskModel",
|
|
294
|
+
type: "model",
|
|
295
|
+
default: "",
|
|
296
|
+
what: "The model of the tasks the double starts; empty for the default model.",
|
|
297
|
+
title: words("Model of tasks", "任务的模型", "任務的模型", "タスクのモデル")
|
|
298
|
+
},
|
|
299
|
+
{ key: "reduceMotion", type: "boolean", default: false, what: "Fewer animations.", title: words("Reduce motion", "减少动画", "減少動畫", "動きを減らす") },
|
|
300
|
+
{
|
|
301
|
+
key: "diagramStyle",
|
|
302
|
+
type: "enum",
|
|
303
|
+
values: ["clean", "sketch", "colour", "motion"],
|
|
304
|
+
default: "clean",
|
|
305
|
+
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.",
|
|
306
|
+
title: words("Diagram style", "图表风格", "圖表風格", "図のスタイル"),
|
|
307
|
+
shown: {
|
|
308
|
+
clean: words("clean", "简洁", "簡潔", "シンプル"),
|
|
309
|
+
sketch: words("sketch", "草图", "草圖", "スケッチ"),
|
|
310
|
+
colour: words("colour", "彩色", "彩色", "カラー"),
|
|
311
|
+
motion: words("motion", "动效", "動效", "モーション")
|
|
312
|
+
}
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
key: "keepAliveMinutes",
|
|
316
|
+
type: "number",
|
|
317
|
+
values: [1, 5, 15, 30],
|
|
318
|
+
default: 5,
|
|
319
|
+
what: "How long an extension started on open keeps running once left, in minutes.",
|
|
320
|
+
title: words("Keep extensions running", "扩展离开后保留", "擴充功能離開後保留", "拡張機能を保持")
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
key: "chatFoldDays",
|
|
324
|
+
type: "number",
|
|
325
|
+
values: [3, 7, 30, 0],
|
|
326
|
+
default: 7,
|
|
327
|
+
what: "Conversations not updated for these days fold at the end of the list; 0 for none.",
|
|
328
|
+
title: words("Fold old conversations", "折叠旧对话", "摺疊舊對話", "古い会話を折りたたむ")
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
key: "chatArchiveDays",
|
|
332
|
+
type: "number",
|
|
333
|
+
values: [30, 90, 365, 0],
|
|
334
|
+
default: 30,
|
|
335
|
+
what: "Conversations not updated for these days are archived (out of the list, found by search); 0 for none.",
|
|
336
|
+
title: words("Archive old conversations", "归档旧对话", "歸檔舊對話", "古い会話をアーカイブ")
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
key: "recentShots",
|
|
340
|
+
type: "boolean",
|
|
341
|
+
default: true,
|
|
342
|
+
what: "The screenshot just taken is offered above the message box, once.",
|
|
343
|
+
title: words("Offer recent screenshots", "提示刚截的图", "提示剛截的圖", "撮ったばかりのスクリーンショットを提案")
|
|
344
|
+
}
|
|
345
|
+
];
|
|
346
|
+
var TaAppUtil;
|
|
347
|
+
(function(TaAppUtil2) {
|
|
348
|
+
function check(spec, value) {
|
|
349
|
+
var _a, _b;
|
|
350
|
+
switch (spec.type) {
|
|
351
|
+
case "boolean":
|
|
352
|
+
return typeof value === "boolean" ? undefined : "true or false";
|
|
353
|
+
case "string":
|
|
354
|
+
return typeof value === "string" ? undefined : "a string";
|
|
355
|
+
case "model":
|
|
356
|
+
return value === "" || typeof value === "string" && /^[^/\s]+\/\S+$/.test(value) ? undefined : "<provider>/<model id>, or empty";
|
|
357
|
+
default:
|
|
358
|
+
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(", ")}`;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
TaAppUtil2.check = check;
|
|
362
|
+
function shown(value, language, spec) {
|
|
363
|
+
var _a;
|
|
364
|
+
const named = (_a = spec === null || spec === undefined ? undefined : spec.shown) === null || _a === undefined ? undefined : _a[String(value)];
|
|
365
|
+
if (named) {
|
|
366
|
+
return TaLanguageUtil.pick(language, named);
|
|
367
|
+
}
|
|
368
|
+
if (typeof value === "boolean") {
|
|
369
|
+
return TaLanguageUtil.pick(language, value ? words("on", "开", "開", "オン") : words("off", "关", "關", "オフ"));
|
|
370
|
+
}
|
|
371
|
+
if (value === "" || value === undefined || value === null) {
|
|
372
|
+
return TaLanguageUtil.pick(language, words("default", "默认", "預設", "デフォルト"));
|
|
373
|
+
}
|
|
374
|
+
return String(value);
|
|
375
|
+
}
|
|
376
|
+
TaAppUtil2.shown = shown;
|
|
377
|
+
})(TaAppUtil || (TaAppUtil = {}));
|
|
378
|
+
|
|
379
|
+
// web-src/app-protocol.ts
|
|
380
|
+
var DONE_KEY = "done";
|
|
381
|
+
var DONE_MAX = 20;
|
|
382
|
+
|
|
383
|
+
// web-src/app-util.ts
|
|
384
|
+
var SCROLL_STEPS = ["down", "up", "top", "bottom"];
|
|
385
|
+
var DATA_CHARS = 8000;
|
|
386
|
+
var AppUseUtil;
|
|
387
|
+
((AppUseUtil) => {
|
|
388
|
+
function find(commands, query) {
|
|
389
|
+
const words2 = (query ?? "").toLowerCase().split(/\s+/).filter(Boolean);
|
|
390
|
+
if (!words2.length) {
|
|
391
|
+
return [...commands];
|
|
392
|
+
}
|
|
393
|
+
return commands.filter((command) => {
|
|
394
|
+
const text = `${command.id} ${command.description}`.toLowerCase();
|
|
395
|
+
return words2.some((word) => text.includes(word));
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
AppUseUtil.find = find;
|
|
399
|
+
function listText(commands) {
|
|
400
|
+
if (!commands.length) {
|
|
401
|
+
return "No action matches; app_commands without a query lists them all.";
|
|
402
|
+
}
|
|
403
|
+
return commands.map((command) => {
|
|
404
|
+
const properties = command.input?.properties;
|
|
405
|
+
const required = command.input?.required;
|
|
406
|
+
const input = properties ? ` Input: ${JSON.stringify(properties)}${Array.isArray(required) && required.length ? `, required ${required.join(", ")}` : ""}.` : " No input.";
|
|
407
|
+
return `- ${command.id} (${command.risk}${command.owner ? `, of ${command.owner}` : ""}): ${command.description}${input}`;
|
|
408
|
+
}).join(`
|
|
409
|
+
`);
|
|
410
|
+
}
|
|
411
|
+
AppUseUtil.listText = listText;
|
|
412
|
+
function resultText(result) {
|
|
413
|
+
const lines = [result.text];
|
|
414
|
+
if (result.data !== undefined) {
|
|
415
|
+
const data = JSON.stringify(result.data);
|
|
416
|
+
lines.push(data.length > DATA_CHARS ? `${data.slice(0, DATA_CHARS)}… (cut; narrow it down)` : data);
|
|
417
|
+
}
|
|
418
|
+
if (result.notice) {
|
|
419
|
+
lines.push(`The user's pages show it already, and a notice with Undo.`);
|
|
420
|
+
}
|
|
421
|
+
return lines.join(`
|
|
422
|
+
`);
|
|
423
|
+
}
|
|
424
|
+
AppUseUtil.resultText = resultText;
|
|
425
|
+
function scroll(value) {
|
|
426
|
+
if (value === undefined || value === null || value === "") {
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (typeof value === "string" && SCROLL_STEPS.includes(value.trim().toLowerCase())) {
|
|
430
|
+
return value.trim().toLowerCase();
|
|
431
|
+
}
|
|
432
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
433
|
+
const { item, find: find2 } = value;
|
|
434
|
+
if (typeof item === "string" && item.trim() && find2 === undefined) {
|
|
435
|
+
return { item: item.trim() };
|
|
436
|
+
}
|
|
437
|
+
if (typeof find2 === "string" && find2.trim() && item === undefined) {
|
|
438
|
+
return { find: find2.trim() };
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
throw new Error(`scroll takes ${SCROLL_STEPS.join(", ")}, { "item": "p1.7" } or { "find": "words of a row" }, not ${JSON.stringify(value)}.`);
|
|
442
|
+
}
|
|
443
|
+
AppUseUtil.scroll = scroll;
|
|
444
|
+
function add(done, entry) {
|
|
445
|
+
return [...entries(done), entry].slice(-DONE_MAX);
|
|
446
|
+
}
|
|
447
|
+
AppUseUtil.add = add;
|
|
448
|
+
function entries(done) {
|
|
449
|
+
return Array.isArray(done) ? done.filter((one) => !!one && typeof one === "object" && typeof one.id === "string" && typeof one.notice === "string" && typeof one.at === "number") : [];
|
|
450
|
+
}
|
|
451
|
+
AppUseUtil.entries = entries;
|
|
452
|
+
function fresh(done, since) {
|
|
453
|
+
return entries(done).filter((entry) => entry.at > since && !entry.undone);
|
|
454
|
+
}
|
|
455
|
+
AppUseUtil.fresh = fresh;
|
|
456
|
+
})(AppUseUtil ||= {});
|
|
457
|
+
|
|
458
|
+
// web-src/i18n.ts
|
|
459
|
+
var EN = {
|
|
460
|
+
undo: "Undo",
|
|
461
|
+
commands: "Look up what Ta can do",
|
|
462
|
+
run: "Do in Ta",
|
|
463
|
+
"settings.get": "Read settings",
|
|
464
|
+
"settings.set": "Change settings",
|
|
465
|
+
"chats.list": "Read conversations",
|
|
466
|
+
"chats.pin": "Pin conversation",
|
|
467
|
+
unpin: "Unpin conversation",
|
|
468
|
+
"chats.stop": "Stop conversation",
|
|
469
|
+
"extensions.list": "Read extensions",
|
|
470
|
+
"extensions.set": "Turn extension on or off",
|
|
471
|
+
"asks.list": "Read what waits for you",
|
|
472
|
+
"asks.answer": "Answer for you",
|
|
473
|
+
look: "Look at the screen",
|
|
474
|
+
go: "Go to",
|
|
475
|
+
do: "Do on the screen",
|
|
476
|
+
scroll: "Scroll",
|
|
477
|
+
"scroll.down": "down",
|
|
478
|
+
"scroll.up": "up",
|
|
479
|
+
"scroll.top": "to the top",
|
|
480
|
+
"scroll.bottom": "to the bottom",
|
|
481
|
+
"game.point": "Tap in the game",
|
|
482
|
+
"game.move": "Move in the game",
|
|
483
|
+
"game.key": "Press",
|
|
484
|
+
"game.hold": "Hold",
|
|
485
|
+
screen: "Ta's screen",
|
|
486
|
+
operating: "Ta is at work",
|
|
487
|
+
watch: "Watch",
|
|
488
|
+
stop: "Stop",
|
|
489
|
+
bigger: "Larger",
|
|
490
|
+
smaller: "Smaller",
|
|
491
|
+
hide: "Hide",
|
|
492
|
+
settings: "Settings",
|
|
493
|
+
discover: "Discover",
|
|
494
|
+
on: "Turn on",
|
|
495
|
+
off: "Turn off"
|
|
496
|
+
};
|
|
497
|
+
var ZH = {
|
|
498
|
+
undo: "撤销",
|
|
499
|
+
commands: "查看 Ta 能做什么",
|
|
500
|
+
run: "在 Ta 里操作",
|
|
501
|
+
"settings.get": "查看设置",
|
|
502
|
+
"settings.set": "修改设置",
|
|
503
|
+
"chats.list": "查看对话",
|
|
504
|
+
"chats.pin": "置顶对话",
|
|
505
|
+
unpin: "取消置顶",
|
|
506
|
+
"chats.stop": "停止对话",
|
|
507
|
+
"extensions.list": "查看扩展",
|
|
508
|
+
"extensions.set": "开关扩展",
|
|
509
|
+
"asks.list": "查看待你决定",
|
|
510
|
+
"asks.answer": "替你回答",
|
|
511
|
+
look: "看看屏幕",
|
|
512
|
+
go: "打开",
|
|
513
|
+
do: "在屏幕上操作",
|
|
514
|
+
scroll: "滚动",
|
|
515
|
+
"scroll.down": "向下",
|
|
516
|
+
"scroll.up": "向上",
|
|
517
|
+
"scroll.top": "到顶部",
|
|
518
|
+
"scroll.bottom": "到底部",
|
|
519
|
+
"game.point": "在游戏里点一下",
|
|
520
|
+
"game.move": "在游戏里移动",
|
|
521
|
+
"game.key": "按下",
|
|
522
|
+
"game.hold": "按住",
|
|
523
|
+
screen: "Ta 的屏幕",
|
|
524
|
+
operating: "Ta 正在操作",
|
|
525
|
+
watch: "看看",
|
|
526
|
+
stop: "停止",
|
|
527
|
+
bigger: "放大",
|
|
528
|
+
smaller: "缩小",
|
|
529
|
+
hide: "收起",
|
|
530
|
+
settings: "设置",
|
|
531
|
+
discover: "发现",
|
|
532
|
+
on: "打开",
|
|
533
|
+
off: "关掉"
|
|
534
|
+
};
|
|
535
|
+
var ZH_TW = {
|
|
536
|
+
undo: "復原",
|
|
537
|
+
commands: "查看 Ta 能做什麼",
|
|
538
|
+
run: "在 Ta 裡操作",
|
|
539
|
+
"settings.get": "查看設定",
|
|
540
|
+
"settings.set": "修改設定",
|
|
541
|
+
"chats.list": "查看對話",
|
|
542
|
+
"chats.pin": "置頂對話",
|
|
543
|
+
unpin: "取消置頂",
|
|
544
|
+
"chats.stop": "停止對話",
|
|
545
|
+
"extensions.list": "查看擴充功能",
|
|
546
|
+
"extensions.set": "開關擴充功能",
|
|
547
|
+
"asks.list": "查看待你決定",
|
|
548
|
+
"asks.answer": "替你回答",
|
|
549
|
+
look: "看看螢幕",
|
|
550
|
+
go: "打開",
|
|
551
|
+
do: "在螢幕上操作",
|
|
552
|
+
scroll: "捲動",
|
|
553
|
+
"scroll.down": "向下",
|
|
554
|
+
"scroll.up": "向上",
|
|
555
|
+
"scroll.top": "到頂部",
|
|
556
|
+
"scroll.bottom": "到底部",
|
|
557
|
+
"game.point": "在遊戲裡點一下",
|
|
558
|
+
"game.move": "在遊戲裡移動",
|
|
559
|
+
"game.key": "按下",
|
|
560
|
+
"game.hold": "按住",
|
|
561
|
+
screen: "Ta 的螢幕",
|
|
562
|
+
operating: "Ta 正在操作",
|
|
563
|
+
watch: "看看",
|
|
564
|
+
stop: "停止",
|
|
565
|
+
bigger: "放大",
|
|
566
|
+
smaller: "縮小",
|
|
567
|
+
hide: "收起",
|
|
568
|
+
settings: "設定",
|
|
569
|
+
discover: "發現",
|
|
570
|
+
on: "開啟",
|
|
571
|
+
off: "關閉"
|
|
572
|
+
};
|
|
573
|
+
var JA = {
|
|
574
|
+
undo: "元に戻す",
|
|
575
|
+
commands: "Ta にできることを確認",
|
|
576
|
+
run: "Ta で操作",
|
|
577
|
+
"settings.get": "設定を確認",
|
|
578
|
+
"settings.set": "設定を変更",
|
|
579
|
+
"chats.list": "会話を確認",
|
|
580
|
+
"chats.pin": "会話をピン留め",
|
|
581
|
+
unpin: "ピン留めを外す",
|
|
582
|
+
"chats.stop": "会話を停止",
|
|
583
|
+
"extensions.list": "拡張機能を確認",
|
|
584
|
+
"extensions.set": "拡張機能をオン・オフ",
|
|
585
|
+
"asks.list": "あなたの判断待ちを確認",
|
|
586
|
+
"asks.answer": "代わりに回答",
|
|
587
|
+
look: "画面を見る",
|
|
588
|
+
go: "開く",
|
|
589
|
+
do: "画面で操作",
|
|
590
|
+
scroll: "スクロール",
|
|
591
|
+
"scroll.down": "下へ",
|
|
592
|
+
"scroll.up": "上へ",
|
|
593
|
+
"scroll.top": "先頭へ",
|
|
594
|
+
"scroll.bottom": "末尾へ",
|
|
595
|
+
"game.point": "ゲームをタップ",
|
|
596
|
+
"game.move": "ゲームで移動",
|
|
597
|
+
"game.key": "押す",
|
|
598
|
+
"game.hold": "長押し",
|
|
599
|
+
screen: "Ta の画面",
|
|
600
|
+
operating: "Ta が操作中",
|
|
601
|
+
watch: "見る",
|
|
602
|
+
stop: "停止",
|
|
603
|
+
bigger: "拡大",
|
|
604
|
+
smaller: "縮小",
|
|
605
|
+
hide: "しまう",
|
|
606
|
+
settings: "設定",
|
|
607
|
+
discover: "見つける",
|
|
608
|
+
on: "オンにする",
|
|
609
|
+
off: "オフにする"
|
|
610
|
+
};
|
|
611
|
+
var WORDS = { "zh-CN": ZH, "zh-TW": ZH_TW, "en-US": EN, "ja-JP": JA };
|
|
612
|
+
var AppUseWords;
|
|
613
|
+
((AppUseWords) => {
|
|
614
|
+
function of(language) {
|
|
615
|
+
return TaLanguageUtil.pick(language, WORDS);
|
|
616
|
+
}
|
|
617
|
+
AppUseWords.of = of;
|
|
618
|
+
})(AppUseWords ||= {});
|
|
619
|
+
|
|
620
|
+
// web-src/screen-cursor.ts
|
|
621
|
+
var STYLE = `
|
|
622
|
+
.ta-agent-layer { position: fixed; inset: 0; pointer-events: none; z-index: 2147483646; }
|
|
623
|
+
.ta-agent-cursor { position: absolute; left: 0; top: 0; transform: translate3d(-40px, -40px, 0); opacity: 0; transition: opacity 180ms; will-change: transform; }
|
|
624
|
+
.ta-agent-cursor[data-shown] { opacity: 1; }
|
|
625
|
+
.ta-agent-cursor svg { display: block; filter: drop-shadow(0 2px 4px rgba(0,0,0,.25)); transition: transform 110ms ease-out; transform-origin: 3px 3px; }
|
|
626
|
+
.ta-agent-cursor[data-press] svg { transform: scale(.84); }
|
|
627
|
+
.ta-agent-touch { display: none; position: absolute; left: -15px; top: -15px; width: 30px; height: 30px; border-radius: 50%; background: color-mix(in srgb, var(--ta-accent, #07c160) 30%, transparent); border: 2px solid #fff; box-shadow: 0 0 0 2px var(--ta-accent, #07c160), 0 2px 8px rgba(0,0,0,.25); transition: transform 120ms ease-out, opacity 200ms; }
|
|
628
|
+
.ta-agent-layer[data-touch] .ta-agent-cursor svg { display: none; }
|
|
629
|
+
.ta-agent-layer[data-touch] .ta-agent-touch { display: block; }
|
|
630
|
+
.ta-agent-cursor[data-press] .ta-agent-touch { transform: scale(.8); }
|
|
631
|
+
.ta-agent-cursor[data-lifted] .ta-agent-touch { transform: scale(.6); opacity: 0; }
|
|
632
|
+
.ta-agent-caption { position: absolute; left: 22px; top: 20px; max-width: min(260px, calc(100vw - 16px)); padding: 4px 9px; border-radius: 9px; background: var(--ta-accent, #07c160); color: #fff; font: 500 12px/1.4 var(--ta-sans, system-ui, sans-serif); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; box-shadow: 0 4px 12px rgba(0,0,0,.18); transition: opacity 200ms; }
|
|
633
|
+
.ta-agent-caption:empty { display: none; }
|
|
634
|
+
.ta-agent-cursor[data-quiet] .ta-agent-caption { opacity: 0; }
|
|
635
|
+
.ta-agent-ring { position: absolute; left: 0; top: 0; border-radius: 10px; box-shadow: 0 0 0 2px var(--ta-accent, #07c160), 0 0 0 6px color-mix(in srgb, var(--ta-accent, #07c160) 22%, transparent); opacity: 0; transition: opacity 200ms; will-change: transform; }
|
|
636
|
+
.ta-agent-ring[data-shown] { opacity: 1; animation: ta-agent-ring-in 240ms cubic-bezier(.2,.8,.2,1); }
|
|
637
|
+
@keyframes ta-agent-ring-in { from { scale: 1.08; } }
|
|
638
|
+
.ta-agent-ripple { position: absolute; width: 36px; height: 36px; margin: -18px 0 0 -18px; border-radius: 50%; background: color-mix(in srgb, var(--ta-accent, #07c160) 35%, transparent); animation: ta-agent-ripple 420ms ease-out forwards; }
|
|
639
|
+
@keyframes ta-agent-ripple { from { transform: scale(.2); opacity: 1; } to { transform: scale(1.6); opacity: 0; } }
|
|
640
|
+
@media (prefers-reduced-motion: reduce) { .ta-agent-ring[data-shown] { animation: none; } .ta-agent-ripple { animation-duration: 1ms; } }
|
|
641
|
+
`;
|
|
642
|
+
var ARROW = '<svg width="22" height="22" viewBox="0 0 22 22"><path d="M3 2.5 L18 11 L11 12.6 L7.6 19.5 Z" fill="var(--ta-accent, #07c160)" stroke="#fff" stroke-width="1.6" stroke-linejoin="round"/></svg>';
|
|
643
|
+
var wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
644
|
+
var EASE = "cubic-bezier(.35,.05,.2,1)";
|
|
645
|
+
var CursorPath;
|
|
646
|
+
((CursorPath) => {
|
|
647
|
+
function duration(distance) {
|
|
648
|
+
return Math.round(Math.min(640, Math.max(200, 160 + Math.sqrt(distance) * 16)));
|
|
649
|
+
}
|
|
650
|
+
CursorPath.duration = duration;
|
|
651
|
+
function arc(from, to, count = 12) {
|
|
652
|
+
const dx = to.x - from.x;
|
|
653
|
+
const dy = to.y - from.y;
|
|
654
|
+
const distance = Math.hypot(dx, dy);
|
|
655
|
+
const bow = Math.min(60, distance * 0.12) * (dx >= 0 ? 1 : -1);
|
|
656
|
+
const control = { x: (from.x + to.x) / 2 + (distance ? -dy / distance * bow : 0), y: (from.y + to.y) / 2 + (distance ? dx / distance * bow : 0) };
|
|
657
|
+
const points = [];
|
|
658
|
+
for (let index = 0;index <= count; index++) {
|
|
659
|
+
const t = index / count;
|
|
660
|
+
const a = (1 - t) * (1 - t);
|
|
661
|
+
const b = 2 * (1 - t) * t;
|
|
662
|
+
const c = t * t;
|
|
663
|
+
points.push({ x: a * from.x + b * control.x + c * to.x, y: a * from.y + b * control.y + c * to.y });
|
|
664
|
+
}
|
|
665
|
+
return points;
|
|
666
|
+
}
|
|
667
|
+
CursorPath.arc = arc;
|
|
668
|
+
function caption(point, size, view, touch) {
|
|
669
|
+
let left = touch ? -Math.round(size.width / 2) : 22;
|
|
670
|
+
let top = touch ? 24 : 20;
|
|
671
|
+
if (touch) {
|
|
672
|
+
left = Math.min(Math.max(point.x + left, 8), Math.max(8, view.width - size.width - 8)) - point.x;
|
|
673
|
+
} else if (point.x + left + size.width > view.width - 8) {
|
|
674
|
+
left = Math.max(-point.x + 8, -size.width - 8);
|
|
675
|
+
}
|
|
676
|
+
if (point.y + top + size.height > view.height - 8) {
|
|
677
|
+
top = touch ? -size.height - 24 : -size.height - 6;
|
|
678
|
+
}
|
|
679
|
+
return { left, top };
|
|
680
|
+
}
|
|
681
|
+
CursorPath.caption = caption;
|
|
682
|
+
function gone(from, to, view) {
|
|
683
|
+
const x = to.left + to.width / 2;
|
|
684
|
+
const y = to.top + to.height / 2;
|
|
685
|
+
return Math.abs(to.left - from.left) > 4 || Math.abs(to.width - from.width) > 4 || x < 0 || x > view.width || y < 0 || y > view.height;
|
|
686
|
+
}
|
|
687
|
+
CursorPath.gone = gone;
|
|
688
|
+
})(CursorPath ||= {});
|
|
689
|
+
|
|
690
|
+
class ScreenCursor {
|
|
691
|
+
document;
|
|
692
|
+
touch;
|
|
693
|
+
linger;
|
|
694
|
+
layer;
|
|
695
|
+
pointer;
|
|
696
|
+
caption;
|
|
697
|
+
ring;
|
|
698
|
+
hideTimer;
|
|
699
|
+
ringTimer;
|
|
700
|
+
quietTimer;
|
|
701
|
+
at = { x: -40, y: -40 };
|
|
702
|
+
ringed;
|
|
703
|
+
following = false;
|
|
704
|
+
placed = "";
|
|
705
|
+
anchor;
|
|
706
|
+
constructor(document2, touch = () => false, linger = 1600) {
|
|
707
|
+
this.document = document2;
|
|
708
|
+
this.touch = touch;
|
|
709
|
+
this.linger = linger;
|
|
710
|
+
}
|
|
711
|
+
async point(element, says, spot) {
|
|
712
|
+
this.mount();
|
|
713
|
+
for (const timer of [this.hideTimer, this.ringTimer, this.quietTimer]) {
|
|
714
|
+
clearTimeout(timer);
|
|
715
|
+
}
|
|
716
|
+
const touch = this.touch();
|
|
717
|
+
this.layer?.toggleAttribute("data-touch", touch);
|
|
718
|
+
const rect = await this.inSight(element);
|
|
719
|
+
const x = rect.left + (spot ? spot.x * rect.width : Math.min(rect.width / 2, touch ? 64 : 40));
|
|
720
|
+
const y = rect.top + (spot ? spot.y : 0.5) * rect.height;
|
|
721
|
+
const { pointer, caption, ring } = this.parts();
|
|
722
|
+
delete pointer.dataset.quiet;
|
|
723
|
+
delete pointer.dataset.lifted;
|
|
724
|
+
caption.textContent = says;
|
|
725
|
+
this.placeCaption({ x, y }, touch);
|
|
726
|
+
if (touch) {
|
|
727
|
+
await this.land(x, y);
|
|
728
|
+
} else {
|
|
729
|
+
await this.glide(x, y);
|
|
730
|
+
}
|
|
731
|
+
this.at = { x, y };
|
|
732
|
+
if (spot) {
|
|
733
|
+
this.unring();
|
|
734
|
+
} else {
|
|
735
|
+
this.ringed = element;
|
|
736
|
+
this.anchor = element.getBoundingClientRect();
|
|
737
|
+
this.placeRing(this.anchor, true);
|
|
738
|
+
ring.dataset.shown = "";
|
|
739
|
+
this.follow();
|
|
740
|
+
}
|
|
741
|
+
await wait(110);
|
|
742
|
+
}
|
|
743
|
+
async press() {
|
|
744
|
+
const { pointer } = this.parts();
|
|
745
|
+
pointer.dataset.press = "";
|
|
746
|
+
const ripple = this.document.createElement("div");
|
|
747
|
+
ripple.className = "ta-agent-ripple";
|
|
748
|
+
Object.assign(ripple.style, { left: `${this.at.x}px`, top: `${this.at.y}px` });
|
|
749
|
+
this.layer?.appendChild(ripple);
|
|
750
|
+
await wait(130);
|
|
751
|
+
delete pointer.dataset.press;
|
|
752
|
+
if (this.layer?.hasAttribute("data-touch")) {
|
|
753
|
+
pointer.dataset.lifted = "";
|
|
754
|
+
}
|
|
755
|
+
setTimeout(() => ripple.remove(), 500);
|
|
756
|
+
}
|
|
757
|
+
async type(text, set) {
|
|
758
|
+
const letters = [...text];
|
|
759
|
+
const slow = Math.min(letters.length, 32);
|
|
760
|
+
for (let index = 1;index <= slow; index++) {
|
|
761
|
+
set(letters.slice(0, index).join(""));
|
|
762
|
+
await wait(16 + index * 7 % 13);
|
|
763
|
+
}
|
|
764
|
+
if (letters.length > slow) {
|
|
765
|
+
set(text);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
rest() {
|
|
769
|
+
for (const timer of [this.hideTimer, this.ringTimer, this.quietTimer]) {
|
|
770
|
+
clearTimeout(timer);
|
|
771
|
+
}
|
|
772
|
+
this.ringTimer = setTimeout(() => this.unring(), 900);
|
|
773
|
+
this.quietTimer = setTimeout(() => {
|
|
774
|
+
if (this.pointer) {
|
|
775
|
+
this.pointer.dataset.quiet = "";
|
|
776
|
+
}
|
|
777
|
+
}, 1600);
|
|
778
|
+
this.hideTimer = setTimeout(() => this.hide(), Math.max(this.linger, 1600));
|
|
779
|
+
}
|
|
780
|
+
hide() {
|
|
781
|
+
for (const timer of [this.hideTimer, this.ringTimer, this.quietTimer]) {
|
|
782
|
+
clearTimeout(timer);
|
|
783
|
+
}
|
|
784
|
+
if (this.pointer) {
|
|
785
|
+
delete this.pointer.dataset.shown;
|
|
786
|
+
}
|
|
787
|
+
this.unring();
|
|
788
|
+
this.at = { x: -40, y: -40 };
|
|
789
|
+
}
|
|
790
|
+
dispose() {
|
|
791
|
+
this.hide();
|
|
792
|
+
this.layer?.remove();
|
|
793
|
+
this.layer = undefined;
|
|
794
|
+
}
|
|
795
|
+
reduced() {
|
|
796
|
+
return this.document.defaultView?.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
|
|
797
|
+
}
|
|
798
|
+
async inSight(element) {
|
|
799
|
+
const view = this.document.defaultView;
|
|
800
|
+
let rect = element.getBoundingClientRect();
|
|
801
|
+
if (!view || rect.top >= 0 && rect.bottom <= view.innerHeight && rect.left >= 0 && rect.right <= view.innerWidth) {
|
|
802
|
+
return rect;
|
|
803
|
+
}
|
|
804
|
+
element.scrollIntoView({ block: "center", inline: "nearest", behavior: this.reduced() ? "auto" : "smooth" });
|
|
805
|
+
const until = Date.now() + 900;
|
|
806
|
+
let still = 0;
|
|
807
|
+
while (Date.now() < until && still < 3) {
|
|
808
|
+
await wait(32);
|
|
809
|
+
const next = element.getBoundingClientRect();
|
|
810
|
+
still = Math.abs(next.top - rect.top) < 0.5 && Math.abs(next.left - rect.left) < 0.5 ? still + 1 : 0;
|
|
811
|
+
rect = next;
|
|
812
|
+
}
|
|
813
|
+
return rect;
|
|
814
|
+
}
|
|
815
|
+
async glide(x, y) {
|
|
816
|
+
const pointer = this.pointer;
|
|
817
|
+
const to = `translate3d(${x}px, ${y}px, 0)`;
|
|
818
|
+
const from = this.at.x < 0 ? { x: x + 70, y: y + 56 } : this.at;
|
|
819
|
+
const distance = Math.hypot(x - from.x, y - from.y);
|
|
820
|
+
pointer.dataset.shown = "";
|
|
821
|
+
if (this.reduced() || distance < 2 || typeof pointer.animate !== "function") {
|
|
822
|
+
pointer.style.transform = to;
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
for (const running of pointer.getAnimations?.() ?? []) {
|
|
826
|
+
running.cancel();
|
|
827
|
+
}
|
|
828
|
+
const duration = CursorPath.duration(distance);
|
|
829
|
+
const frames = CursorPath.arc(from, { x, y }).map((point, index, all) => ({
|
|
830
|
+
transform: `translate3d(${point.x}px, ${point.y}px, 0)`,
|
|
831
|
+
offset: index / (all.length - 1)
|
|
832
|
+
}));
|
|
833
|
+
pointer.style.transform = to;
|
|
834
|
+
const motion = pointer.animate(frames, { duration, easing: EASE });
|
|
835
|
+
await Promise.race([motion.finished.catch(() => {
|
|
836
|
+
return;
|
|
837
|
+
}), wait(duration + 80)]);
|
|
838
|
+
}
|
|
839
|
+
async land(x, y) {
|
|
840
|
+
const pointer = this.pointer;
|
|
841
|
+
for (const running of pointer.getAnimations?.({ subtree: true }) ?? []) {
|
|
842
|
+
running.cancel();
|
|
843
|
+
}
|
|
844
|
+
pointer.style.transform = `translate3d(${x}px, ${y}px, 0)`;
|
|
845
|
+
pointer.dataset.shown = "";
|
|
846
|
+
const dot = pointer.querySelector(".ta-agent-touch");
|
|
847
|
+
if (dot && !this.reduced() && typeof dot.animate === "function") {
|
|
848
|
+
dot.animate([
|
|
849
|
+
{ transform: "scale(1.6)", opacity: 0 },
|
|
850
|
+
{ transform: "scale(1)", opacity: 1 }
|
|
851
|
+
], { duration: 170, easing: "cubic-bezier(.2,.8,.2,1)" });
|
|
852
|
+
await wait(170);
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
placeCaption(point, touch) {
|
|
856
|
+
const caption = this.caption;
|
|
857
|
+
const view = this.document.defaultView;
|
|
858
|
+
Object.assign(caption.style, { left: "", top: "" });
|
|
859
|
+
if (!view || !caption.textContent) {
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
const { left, top } = CursorPath.caption(point, { width: caption.offsetWidth, height: caption.offsetHeight }, { width: view.innerWidth, height: view.innerHeight }, touch);
|
|
863
|
+
Object.assign(caption.style, { left: `${left}px`, top: `${top}px` });
|
|
864
|
+
}
|
|
865
|
+
placeRing(rect, now = false) {
|
|
866
|
+
const ring = this.ring;
|
|
867
|
+
const key = `${Math.round(rect.left)},${Math.round(rect.top)},${Math.round(rect.width)},${Math.round(rect.height)}`;
|
|
868
|
+
if (!now && key === this.placed) {
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
this.placed = key;
|
|
872
|
+
Object.assign(ring.style, {
|
|
873
|
+
transform: `translate3d(${rect.left - 3}px, ${rect.top - 3}px, 0)`,
|
|
874
|
+
width: `${rect.width + 6}px`,
|
|
875
|
+
height: `${rect.height + 6}px`
|
|
876
|
+
});
|
|
877
|
+
}
|
|
878
|
+
follow() {
|
|
879
|
+
const view = this.document.defaultView;
|
|
880
|
+
if (this.following || !view?.requestAnimationFrame) {
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
this.following = true;
|
|
884
|
+
const step = () => {
|
|
885
|
+
const element = this.ringed;
|
|
886
|
+
if (!element || !this.ring?.hasAttribute("data-shown")) {
|
|
887
|
+
this.following = false;
|
|
888
|
+
return;
|
|
889
|
+
}
|
|
890
|
+
const rect = element.getBoundingClientRect();
|
|
891
|
+
if (!element.isConnected || this.anchor && CursorPath.gone(this.anchor, rect, { width: view.innerWidth, height: view.innerHeight })) {
|
|
892
|
+
this.following = false;
|
|
893
|
+
this.unring();
|
|
894
|
+
if (this.pointer) {
|
|
895
|
+
this.pointer.dataset.quiet = "";
|
|
896
|
+
}
|
|
897
|
+
return;
|
|
898
|
+
}
|
|
899
|
+
this.placeRing(rect);
|
|
900
|
+
view.requestAnimationFrame(step);
|
|
901
|
+
};
|
|
902
|
+
view.requestAnimationFrame(step);
|
|
903
|
+
}
|
|
904
|
+
unring() {
|
|
905
|
+
this.ringed = undefined;
|
|
906
|
+
this.anchor = undefined;
|
|
907
|
+
if (this.ring) {
|
|
908
|
+
delete this.ring.dataset.shown;
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
parts() {
|
|
912
|
+
this.mount();
|
|
913
|
+
return { pointer: this.pointer, caption: this.caption, ring: this.ring };
|
|
914
|
+
}
|
|
915
|
+
mount() {
|
|
916
|
+
if (this.layer?.isConnected) {
|
|
917
|
+
return;
|
|
918
|
+
}
|
|
919
|
+
const document2 = this.document;
|
|
920
|
+
if (!document2.getElementById("ta-agent-style")) {
|
|
921
|
+
const style = document2.createElement("style");
|
|
922
|
+
style.id = "ta-agent-style";
|
|
923
|
+
style.textContent = STYLE;
|
|
924
|
+
document2.head.appendChild(style);
|
|
925
|
+
}
|
|
926
|
+
const layer = document2.createElement("div");
|
|
927
|
+
layer.className = "ta-agent-layer";
|
|
928
|
+
layer.setAttribute("aria-hidden", "true");
|
|
929
|
+
const ring = document2.createElement("div");
|
|
930
|
+
ring.className = "ta-agent-ring";
|
|
931
|
+
const pointer = document2.createElement("div");
|
|
932
|
+
pointer.className = "ta-agent-cursor";
|
|
933
|
+
pointer.innerHTML = ARROW;
|
|
934
|
+
const touch = document2.createElement("div");
|
|
935
|
+
touch.className = "ta-agent-touch";
|
|
936
|
+
const caption = document2.createElement("div");
|
|
937
|
+
caption.className = "ta-agent-caption";
|
|
938
|
+
pointer.append(touch, caption);
|
|
939
|
+
layer.append(ring, pointer);
|
|
940
|
+
document2.body.appendChild(layer);
|
|
941
|
+
Object.assign(this, { layer, pointer, caption, ring });
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
// web-src/screen-dom.ts
|
|
946
|
+
var CONTROLS = [
|
|
947
|
+
"button",
|
|
948
|
+
"a[href]",
|
|
949
|
+
"input:not([type=hidden])",
|
|
950
|
+
"textarea",
|
|
951
|
+
"select",
|
|
952
|
+
'[contenteditable="true"]',
|
|
953
|
+
"[role=button]",
|
|
954
|
+
"[role=link]",
|
|
955
|
+
"[role=switch]",
|
|
956
|
+
"[role=checkbox]",
|
|
957
|
+
"[role=tab]",
|
|
958
|
+
"[role=menuitem]",
|
|
959
|
+
"[role=option]",
|
|
960
|
+
"[role=textbox]"
|
|
961
|
+
].join(",");
|
|
962
|
+
var DIALOGS = "[role=dialog], [role=alertdialog], dialog[open]";
|
|
963
|
+
var ScreenDom;
|
|
964
|
+
((ScreenDom) => {
|
|
965
|
+
function visible(element) {
|
|
966
|
+
const html = element;
|
|
967
|
+
if (typeof html.checkVisibility === "function" && !html.checkVisibility({ opacityProperty: true, visibilityProperty: true })) {
|
|
968
|
+
return false;
|
|
969
|
+
}
|
|
970
|
+
const rect = html.getBoundingClientRect();
|
|
971
|
+
const view = html.ownerDocument.defaultView;
|
|
972
|
+
return rect.width > 0 && rect.height > 0 && rect.bottom > 0 && rect.right > 0 && rect.top < (view?.innerHeight ?? 0) && rect.left < (view?.innerWidth ?? 0);
|
|
973
|
+
}
|
|
974
|
+
ScreenDom.visible = visible;
|
|
975
|
+
function controls(document2) {
|
|
976
|
+
const dialogs = [...document2.querySelectorAll(DIALOGS)].filter(visible);
|
|
977
|
+
const dialog = dialogs[dialogs.length - 1];
|
|
978
|
+
const root = dialog ?? document2;
|
|
979
|
+
const title = dialog ? labelOf(dialog) || headingOf(dialog) || "" : undefined;
|
|
980
|
+
const controls2 = [];
|
|
981
|
+
const elements = [];
|
|
982
|
+
for (const element of root.querySelectorAll(CONTROLS)) {
|
|
983
|
+
if (element.closest("[data-ta-item], [data-ta-action], [data-ta-skip], .ta-agent-layer, .ta-screen-box, .ta-screen-pill, .ta-rail-item") || element.closest('[aria-hidden="true"], [inert]') || !visible(element)) {
|
|
984
|
+
continue;
|
|
985
|
+
}
|
|
986
|
+
const control = read(element);
|
|
987
|
+
if (!control) {
|
|
988
|
+
continue;
|
|
989
|
+
}
|
|
990
|
+
if (title !== undefined) {
|
|
991
|
+
control.dialog = title;
|
|
992
|
+
}
|
|
993
|
+
controls2.push(control);
|
|
994
|
+
elements.push(element);
|
|
995
|
+
}
|
|
996
|
+
return { controls: controls2, elements };
|
|
997
|
+
}
|
|
998
|
+
ScreenDom.controls = controls;
|
|
999
|
+
function headings(document2) {
|
|
1000
|
+
const found = [];
|
|
1001
|
+
for (const heading of document2.querySelectorAll("h1, h2, [role=heading], .ta-main-head .ta-title, .ta-page-title")) {
|
|
1002
|
+
const text = heading.textContent?.replace(/\s+/g, " ").trim();
|
|
1003
|
+
if (text && visible(heading) && !found.includes(text)) {
|
|
1004
|
+
found.push(text);
|
|
1005
|
+
}
|
|
1006
|
+
if (found.length >= 8) {
|
|
1007
|
+
break;
|
|
1008
|
+
}
|
|
1009
|
+
}
|
|
1010
|
+
return found;
|
|
1011
|
+
}
|
|
1012
|
+
ScreenDom.headings = headings;
|
|
1013
|
+
function read(element) {
|
|
1014
|
+
const role = element.getAttribute("role");
|
|
1015
|
+
const tag = element.tagName.toLowerCase();
|
|
1016
|
+
const type = (element.getAttribute("type") ?? "").toLowerCase();
|
|
1017
|
+
const kind = role === "switch" || tag === "input" && type === "checkbox" && element.closest(".ta-switch") ? "switch" : role === "checkbox" || tag === "input" && (type === "checkbox" || type === "radio") ? "checkbox" : tag === "select" ? "select" : tag === "textarea" || tag === "input" && !["button", "submit", "reset", "image", "range", "color", "file"].includes(type) || role === "textbox" || element.isContentEditable ? "textbox" : role === "tab" ? "tab" : role === "menuitem" ? "menuitem" : role === "option" ? "option" : tag === "a" || role === "link" ? "link" : tag === "button" || role === "button" || tag === "input" && ["button", "submit", "reset"].includes(type) ? "button" : undefined;
|
|
1018
|
+
if (!kind) {
|
|
1019
|
+
return;
|
|
1020
|
+
}
|
|
1021
|
+
const label = labelOf(element) || (kind === "textbox" ? element.getAttribute("placeholder") || "" : "");
|
|
1022
|
+
if (!label) {
|
|
1023
|
+
return;
|
|
1024
|
+
}
|
|
1025
|
+
const control = { kind, label };
|
|
1026
|
+
if (kind === "switch" || kind === "checkbox") {
|
|
1027
|
+
const aria = element.getAttribute("aria-checked");
|
|
1028
|
+
control.checked = aria !== null ? aria === "true" : element.checked === true;
|
|
1029
|
+
} else if (kind === "textbox") {
|
|
1030
|
+
control.value = element.isContentEditable ? element.textContent ?? "" : element.value ?? "";
|
|
1031
|
+
} else if (kind === "select") {
|
|
1032
|
+
const select = element;
|
|
1033
|
+
control.value = select.selectedOptions[0]?.textContent?.trim() ?? select.value;
|
|
1034
|
+
}
|
|
1035
|
+
if (element.matches(".ta-danger, .ta-danger *, [data-danger]") || /\bdanger\b/.test(element.className)) {
|
|
1036
|
+
control.danger = true;
|
|
1037
|
+
}
|
|
1038
|
+
return control;
|
|
1039
|
+
}
|
|
1040
|
+
function labelOf(element) {
|
|
1041
|
+
const aria = element.getAttribute("aria-label");
|
|
1042
|
+
if (aria?.trim()) {
|
|
1043
|
+
return clean(aria);
|
|
1044
|
+
}
|
|
1045
|
+
const by = element.getAttribute("aria-labelledby");
|
|
1046
|
+
if (by) {
|
|
1047
|
+
const text = by.split(/\s+/).map((id2) => element.ownerDocument.getElementById(id2)?.textContent ?? "").join(" ");
|
|
1048
|
+
if (text.trim()) {
|
|
1049
|
+
return clean(text);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
const id = element.getAttribute("id");
|
|
1053
|
+
const forLabel = id ? element.ownerDocument.querySelector(`label[for="${CSS.escape(id)}"]`) : null;
|
|
1054
|
+
const label = forLabel ?? element.closest("label");
|
|
1055
|
+
if (label && label !== element && label.textContent?.trim()) {
|
|
1056
|
+
return clean(label.textContent);
|
|
1057
|
+
}
|
|
1058
|
+
const tag = element.tagName.toLowerCase();
|
|
1059
|
+
if (tag !== "input" && tag !== "textarea" && tag !== "select") {
|
|
1060
|
+
const words2 = clean(element.textContent ?? "");
|
|
1061
|
+
if (words2) {
|
|
1062
|
+
return words2;
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
const row = element.closest(".ta-setting-row, .ta-list-row, [data-setting]");
|
|
1066
|
+
const rowLabel = row?.querySelector(".ta-setting-label, .ta-list-label, label");
|
|
1067
|
+
if (rowLabel?.textContent?.trim()) {
|
|
1068
|
+
return clean(rowLabel.textContent);
|
|
1069
|
+
}
|
|
1070
|
+
return clean(element.getAttribute("title") ?? element.querySelector("img")?.getAttribute("alt") ?? "");
|
|
1071
|
+
}
|
|
1072
|
+
ScreenDom.labelOf = labelOf;
|
|
1073
|
+
function headingOf(element) {
|
|
1074
|
+
return clean(element.querySelector("h1, h2, h3, [role=heading], .ta-dialog-title")?.textContent ?? "");
|
|
1075
|
+
}
|
|
1076
|
+
function clean(text) {
|
|
1077
|
+
const one = text.replace(/\s+/g, " ").trim();
|
|
1078
|
+
return one.length > 80 ? `${one.slice(0, 79)}…` : one;
|
|
1079
|
+
}
|
|
1080
|
+
})(ScreenDom ||= {});
|
|
1081
|
+
|
|
1082
|
+
// web-src/screen-protocol.ts
|
|
1083
|
+
var LOOK_MAX = 3000;
|
|
1084
|
+
var OLD_MAX = 200;
|
|
1085
|
+
var CONTROLS_MAX = 50;
|
|
1086
|
+
|
|
1087
|
+
// web-src/screen-text.ts
|
|
1088
|
+
var ScreenText;
|
|
1089
|
+
((ScreenText) => {
|
|
1090
|
+
const cut = (text, length) => text.length > length ? `${text.slice(0, length - 1)}…` : text;
|
|
1091
|
+
const HEAD = /^(The user's screen|Ta's own screen) \(/m;
|
|
1092
|
+
function placeNames(places) {
|
|
1093
|
+
return places.map((place) => place.title && places.filter((one) => one.title === place.title).length === 1 ? place.title : `${place.id}${place.title ? ` “${place.title}”` : ""}`);
|
|
1094
|
+
}
|
|
1095
|
+
function scrolled(scrollable) {
|
|
1096
|
+
const { at, offset, total, shown } = scrollable.position;
|
|
1097
|
+
const span = total !== undefined ? `rows ${offset}–${offset + Math.max(1, shown ?? 1) - 1} of ${total}` : `${offset}% down`;
|
|
1098
|
+
const where = at === "top" ? "at the top" : at === "bottom" ? "at the bottom" : "in the middle";
|
|
1099
|
+
const side = scrollable.more === "top" ? "above" : "below";
|
|
1100
|
+
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"}` : "";
|
|
1101
|
+
return [span, where, more].filter(Boolean).join(", ");
|
|
1102
|
+
}
|
|
1103
|
+
ScreenText.scrolled = scrolled;
|
|
1104
|
+
function rowId(partIndex, part, itemIndex) {
|
|
1105
|
+
const item = part.items[itemIndex];
|
|
1106
|
+
if (item?.at !== undefined) {
|
|
1107
|
+
return `p${partIndex + 1}.${item.at}`;
|
|
1108
|
+
}
|
|
1109
|
+
if (part.items.some((one) => one.at !== undefined)) {
|
|
1110
|
+
return `p${partIndex + 1}.f${part.items.slice(0, itemIndex + 1).filter((one) => one.at === undefined).length}`;
|
|
1111
|
+
}
|
|
1112
|
+
return `p${partIndex + 1}.${itemIndex + 1}`;
|
|
1113
|
+
}
|
|
1114
|
+
ScreenText.rowId = rowId;
|
|
1115
|
+
function whole(text) {
|
|
1116
|
+
return HEAD.test(text);
|
|
1117
|
+
}
|
|
1118
|
+
ScreenText.whole = whole;
|
|
1119
|
+
const heads = (line) => /^\[p\d+\]/.test(line) || line === "Also on the screen:" || /^ {2}Dialog “/.test(line);
|
|
1120
|
+
function changes(before, after) {
|
|
1121
|
+
if (!before || !whole(before) || !whole(after) || before.split(`
|
|
1122
|
+
`)[0] !== after.split(`
|
|
1123
|
+
`)[0]) {
|
|
1124
|
+
return;
|
|
1125
|
+
}
|
|
1126
|
+
const old = new Set(before.split(`
|
|
1127
|
+
`));
|
|
1128
|
+
const lines = after.split(`
|
|
1129
|
+
`);
|
|
1130
|
+
const now = new Set(lines);
|
|
1131
|
+
const body = lines.filter((line) => line.trim());
|
|
1132
|
+
const changed = body.filter((line) => !old.has(line));
|
|
1133
|
+
if (changed.length > Math.max(3, body.length * 0.4)) {
|
|
1134
|
+
return;
|
|
1135
|
+
}
|
|
1136
|
+
const gone = before.split(`
|
|
1137
|
+
`).filter((line) => line.trim() && !now.has(line)).length - changed.length;
|
|
1138
|
+
const out = [];
|
|
1139
|
+
let head;
|
|
1140
|
+
let said;
|
|
1141
|
+
for (const line of lines) {
|
|
1142
|
+
if (heads(line)) {
|
|
1143
|
+
head = line;
|
|
1144
|
+
}
|
|
1145
|
+
if (!line.trim() || old.has(line)) {
|
|
1146
|
+
continue;
|
|
1147
|
+
}
|
|
1148
|
+
if (head && head !== line && said !== head) {
|
|
1149
|
+
out.push(head);
|
|
1150
|
+
}
|
|
1151
|
+
out.push(line);
|
|
1152
|
+
said = head;
|
|
1153
|
+
}
|
|
1154
|
+
if (!out.length) {
|
|
1155
|
+
return gone > 0 ? `Nothing new on the screen; ${gone} line(s) went away.` : "Nothing else on the screen changed.";
|
|
1156
|
+
}
|
|
1157
|
+
return `What changed (the rest is as before, with the same ids):
|
|
1158
|
+
${out.join(`
|
|
1159
|
+
`)}${gone > 0 ? `
|
|
1160
|
+
(${gone} more line(s) went away)` : ""}`;
|
|
1161
|
+
}
|
|
1162
|
+
ScreenText.changes = changes;
|
|
1163
|
+
function forget(messages, tools) {
|
|
1164
|
+
const textOf = (message) => Array.isArray(message.content) ? message.content.map((part) => part.type === "text" ? part.text ?? "" : "").join("") : typeof message.content === "string" ? message.content : "";
|
|
1165
|
+
const sight = (message) => message.role === "toolResult" && tools.includes(message.toolName ?? "");
|
|
1166
|
+
let last = -1;
|
|
1167
|
+
messages.forEach((message, index) => {
|
|
1168
|
+
if (sight(message) && whole(textOf(message))) {
|
|
1169
|
+
last = index;
|
|
1170
|
+
}
|
|
1171
|
+
});
|
|
1172
|
+
let cut2 = false;
|
|
1173
|
+
const out = messages.map((message, index) => {
|
|
1174
|
+
const text = index < last && sight(message) ? textOf(message) : "";
|
|
1175
|
+
if (text.length <= OLD_MAX) {
|
|
1176
|
+
return message;
|
|
1177
|
+
}
|
|
1178
|
+
cut2 = true;
|
|
1179
|
+
const first = text.split(`
|
|
1180
|
+
`).find((line) => line.trim()) ?? "";
|
|
1181
|
+
return { ...message, content: [{ type: "text", text: `${cut1(first)} … (an older sight of the screen, left out: a later one is below)` }] };
|
|
1182
|
+
});
|
|
1183
|
+
return cut2 ? out : undefined;
|
|
1184
|
+
}
|
|
1185
|
+
ScreenText.forget = forget;
|
|
1186
|
+
const cut1 = (line) => cut(line, 160);
|
|
1187
|
+
function look(sight, only) {
|
|
1188
|
+
const targets = new Map;
|
|
1189
|
+
const lines = [];
|
|
1190
|
+
if (!only) {
|
|
1191
|
+
lines.push(`${sight.whose === "ta" ? "Ta's own screen" : "The user's screen"} (${sight.device}): ${sight.where || "starting"}`);
|
|
1192
|
+
if (sight.places.length) {
|
|
1193
|
+
lines.push(`Places (app_go to): ${placeNames(sight.places).join(" · ")} · chat:<name> a conversation`);
|
|
1194
|
+
}
|
|
1195
|
+
if (sight.headings.length) {
|
|
1196
|
+
lines.push(`Headings: ${sight.headings.map((heading) => cut(heading, 60)).join(" · ")}`);
|
|
1197
|
+
}
|
|
1198
|
+
const page = sight.scroll;
|
|
1199
|
+
const index = page?.part ? sight.parts.findIndex((part) => part.id === page.part) : -1;
|
|
1200
|
+
if (page) {
|
|
1201
|
+
lines.push(`Scrolls (app_do scroll, no id): ${index >= 0 ? `[p${index + 1}]` : `the page, ${scrolled(page.scrollable)}`}`);
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
sight.parts.forEach((part, index) => {
|
|
1205
|
+
const id = `p${index + 1}`;
|
|
1206
|
+
targets.set(id, { part: part.id });
|
|
1207
|
+
if (only && only !== part.id && only !== id) {
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
lines.push("", `[${id}] ${part.title}${part.state ? ` — ${part.state}` : ""}${part.floats ? " (over every page)" : ""}`);
|
|
1211
|
+
if (part.scrollable) {
|
|
1212
|
+
lines.push(` scrolls: ${scrolled(part.scrollable)}`);
|
|
1213
|
+
}
|
|
1214
|
+
part.items.forEach((item, itemIndex) => {
|
|
1215
|
+
const itemId = rowId(index, part, itemIndex);
|
|
1216
|
+
targets.set(itemId, { part: part.id, item: item.id });
|
|
1217
|
+
lines.push(` [${itemId}] ${cut(item.text, 200)}${item.marks?.length ? ` · ${item.marks.map((mark) => cut(mark, 80)).join(" · ")}` : ""}`);
|
|
1218
|
+
});
|
|
1219
|
+
if (part.actions.length) {
|
|
1220
|
+
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" : ""}`);
|
|
1221
|
+
lines.push(` does: ${actions.join(" · ")}`);
|
|
1222
|
+
}
|
|
1223
|
+
});
|
|
1224
|
+
const controls = sight.controls.slice(0, CONTROLS_MAX);
|
|
1225
|
+
controls.forEach((_control, index) => {
|
|
1226
|
+
targets.set(`e${index + 1}`, { control: index });
|
|
1227
|
+
});
|
|
1228
|
+
if (!only && controls.length) {
|
|
1229
|
+
lines.push("", "Also on the screen:");
|
|
1230
|
+
let dialog;
|
|
1231
|
+
controls.forEach((control, index) => {
|
|
1232
|
+
if (control.dialog !== dialog) {
|
|
1233
|
+
dialog = control.dialog;
|
|
1234
|
+
if (dialog !== undefined) {
|
|
1235
|
+
lines.push(` Dialog “${cut(dialog, 80)}”:`);
|
|
1236
|
+
}
|
|
1237
|
+
}
|
|
1238
|
+
const state = control.checked !== undefined ? control.checked ? " on" : " off" : control.value !== undefined ? ` = “${cut(control.value, 80)}”` : "";
|
|
1239
|
+
lines.push(` ${control.dialog !== undefined ? " " : ""}[e${index + 1}] ${control.kind} “${cut(control.label, 80)}”${state}${control.danger ? " !danger" : ""}`);
|
|
1240
|
+
});
|
|
1241
|
+
if (sight.controls.length > controls.length) {
|
|
1242
|
+
lines.push(` … ${sight.controls.length - controls.length} more: look at a part, or go elsewhere.`);
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
const all = lines.join(`
|
|
1246
|
+
`).trim();
|
|
1247
|
+
return { text: all.length > LOOK_MAX ? `${all.slice(0, LOOK_MAX - 40)}
|
|
1248
|
+
… (more: app_look a part)` : all, targets };
|
|
1249
|
+
}
|
|
1250
|
+
ScreenText.look = look;
|
|
1251
|
+
})(ScreenText ||= {});
|
|
1252
|
+
|
|
1253
|
+
// web-src/screen-driver.ts
|
|
1254
|
+
var wait2 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
1255
|
+
var ARROWS = { down: "↓", up: "↑", top: "⤒", bottom: "⤓" };
|
|
1256
|
+
var score0 = (id) => id.startsWith("e") || id.includes(".");
|
|
1257
|
+
|
|
1258
|
+
class ScreenDriver {
|
|
1259
|
+
window;
|
|
1260
|
+
host;
|
|
1261
|
+
targets = new Map;
|
|
1262
|
+
names = new Map;
|
|
1263
|
+
elements = [];
|
|
1264
|
+
shown = new Map;
|
|
1265
|
+
cursor;
|
|
1266
|
+
constructor(window2, host) {
|
|
1267
|
+
this.window = window2;
|
|
1268
|
+
this.host = host;
|
|
1269
|
+
this.cursor = new ScreenCursor(window2.document, () => this.host.touch?.() ?? false, host.linger);
|
|
1270
|
+
}
|
|
1271
|
+
async ready(timeoutMs = 20000) {
|
|
1272
|
+
const until = Date.now() + timeoutMs;
|
|
1273
|
+
while (!this.host.screen.where() && Date.now() < until) {
|
|
1274
|
+
await wait2(150);
|
|
1275
|
+
}
|
|
1276
|
+
await wait2(200);
|
|
1277
|
+
}
|
|
1278
|
+
look(part, who) {
|
|
1279
|
+
const text = this.read(part);
|
|
1280
|
+
if (who !== undefined && !part) {
|
|
1281
|
+
this.shown.set(who, text);
|
|
1282
|
+
}
|
|
1283
|
+
return text;
|
|
1284
|
+
}
|
|
1285
|
+
hideCursor() {
|
|
1286
|
+
this.cursor.hide();
|
|
1287
|
+
}
|
|
1288
|
+
read(part) {
|
|
1289
|
+
const { controls, elements } = ScreenDom.controls(this.window.document);
|
|
1290
|
+
const sight = {
|
|
1291
|
+
whose: this.host.apart ? "ta" : "user",
|
|
1292
|
+
device: this.host.device(),
|
|
1293
|
+
where: this.host.screen.where(),
|
|
1294
|
+
places: this.host.screen.places(),
|
|
1295
|
+
parts: this.host.screen.parts(),
|
|
1296
|
+
controls,
|
|
1297
|
+
headings: ScreenDom.headings(this.window.document)
|
|
1298
|
+
};
|
|
1299
|
+
const scroll = this.host.screen.pageScroll?.();
|
|
1300
|
+
if (scroll) {
|
|
1301
|
+
sight.scroll = scroll;
|
|
1302
|
+
}
|
|
1303
|
+
const { text, targets } = ScreenText.look(sight, part);
|
|
1304
|
+
this.targets = targets;
|
|
1305
|
+
this.elements = elements;
|
|
1306
|
+
this.names = new Map;
|
|
1307
|
+
for (const [index, one] of sight.parts.entries()) {
|
|
1308
|
+
this.names.set(`p${index + 1}`, one.title);
|
|
1309
|
+
for (const [itemIndex, item] of one.items.entries()) {
|
|
1310
|
+
this.names.set(ScreenText.rowId(index, one, itemIndex), item.text);
|
|
1311
|
+
}
|
|
1312
|
+
}
|
|
1313
|
+
for (const [index, control] of controls.entries()) {
|
|
1314
|
+
if (targets.has(`e${index + 1}`)) {
|
|
1315
|
+
this.names.set(`e${index + 1}`, control.label);
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
return text;
|
|
1319
|
+
}
|
|
1320
|
+
async go(to, who) {
|
|
1321
|
+
await this.host.quiet?.();
|
|
1322
|
+
const word = to.trim().toLowerCase();
|
|
1323
|
+
const place = this.host.screen.places().find((one) => one.id.toLowerCase() === word || one.title.toLowerCase() === word);
|
|
1324
|
+
const button = place && this.window.document.querySelector(`[data-ta-place="${CSS.escape(place.id)}"]`);
|
|
1325
|
+
if (button && ScreenDom.visible(button) && this.host.watched()) {
|
|
1326
|
+
await this.cursor.point(button, place.title);
|
|
1327
|
+
await this.cursor.press();
|
|
1328
|
+
}
|
|
1329
|
+
const went = await this.host.screen.go(to);
|
|
1330
|
+
if (!went) {
|
|
1331
|
+
throw new Error(`There is no place ${JSON.stringify(to)}: see the places of app_look.`);
|
|
1332
|
+
}
|
|
1333
|
+
await this.settle(150, 800);
|
|
1334
|
+
this.cursor.rest();
|
|
1335
|
+
return this.look(undefined, who);
|
|
1336
|
+
}
|
|
1337
|
+
async do(id, action, input = {}, options = {}) {
|
|
1338
|
+
const words2 = options.find?.trim();
|
|
1339
|
+
if (!this.targets.size || words2 && !id) {
|
|
1340
|
+
this.read();
|
|
1341
|
+
}
|
|
1342
|
+
let key = id.replace(/^\[|\]$/g, "");
|
|
1343
|
+
if (!key && words2) {
|
|
1344
|
+
const found = this.find(words2);
|
|
1345
|
+
if (typeof found !== "string") {
|
|
1346
|
+
return { text: `${found.why}
|
|
1347
|
+
|
|
1348
|
+
${this.look(undefined, options.who)}` };
|
|
1349
|
+
}
|
|
1350
|
+
key = found;
|
|
1351
|
+
}
|
|
1352
|
+
const target = this.targets.get(key);
|
|
1353
|
+
if (!target) {
|
|
1354
|
+
throw new Error(key ? `Nothing on the screen is [${key}] now: app_look again.` : "Say what to do it to: an id of app_look, or find.");
|
|
1355
|
+
}
|
|
1356
|
+
await this.host.quiet?.();
|
|
1357
|
+
const answer = "part" in target ? await this.part(target, action, input, options) : await this.control(target.control, action, input, options);
|
|
1358
|
+
this.cursor.rest();
|
|
1359
|
+
return answer;
|
|
1360
|
+
}
|
|
1361
|
+
async scroll(id, to, options = {}) {
|
|
1362
|
+
const screen = this.host.screen;
|
|
1363
|
+
if (typeof screen.scroll !== "function") {
|
|
1364
|
+
throw new Error("This Ta cannot scroll its screen yet: it needs an update.");
|
|
1365
|
+
}
|
|
1366
|
+
if (!this.targets.size) {
|
|
1367
|
+
this.read();
|
|
1368
|
+
}
|
|
1369
|
+
let part;
|
|
1370
|
+
let goal = to;
|
|
1371
|
+
const key = id.replace(/^\[|\]$/g, "");
|
|
1372
|
+
if (key) {
|
|
1373
|
+
const target = this.targets.get(key);
|
|
1374
|
+
if (!target || !("part" in target)) {
|
|
1375
|
+
throw new Error(target ? "A control does not scroll: name a part ([p1]), or none for the page." : `Nothing on the screen is [${key}] now: app_look again.`);
|
|
1376
|
+
}
|
|
1377
|
+
part = target.part;
|
|
1378
|
+
}
|
|
1379
|
+
if (typeof to === "object" && "item" in to) {
|
|
1380
|
+
const name = to.item.replace(/^\[|\]$/g, "");
|
|
1381
|
+
const row = this.targets.get(name);
|
|
1382
|
+
if (row && "part" in row && row.item) {
|
|
1383
|
+
part = row.part;
|
|
1384
|
+
goal = { item: row.item };
|
|
1385
|
+
} else if (/^p\d+\.\d+$/.test(name)) {
|
|
1386
|
+
const of = this.targets.get(name.split(".")[0]);
|
|
1387
|
+
const scrollable = of && "part" in of ? screen.parts().find((one) => one.id === of.part)?.scrollable : undefined;
|
|
1388
|
+
throw new Error(`[${name}] is not in sight${scrollable ? ` (${ScreenText.scrolled(scrollable)})` : ""}: scroll toward it a screen at a time, or to it by its words ({ "find": … }), then look again.`);
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
const before = options.who !== undefined ? this.shown.get(options.who) : undefined;
|
|
1392
|
+
await this.host.quiet?.();
|
|
1393
|
+
const shown = part ?? screen.pageScroll?.()?.part;
|
|
1394
|
+
const element = shown ? screen.element(shown) : undefined;
|
|
1395
|
+
if (element && this.host.watched()) {
|
|
1396
|
+
await this.cursor.point(element, typeof to === "string" ? ARROWS[to] : "↕");
|
|
1397
|
+
}
|
|
1398
|
+
const done = await screen.scroll(part, goal);
|
|
1399
|
+
await this.settle(60, 450);
|
|
1400
|
+
const now = (done.part ? screen.parts().find((one) => one.id === done.part)?.scrollable : screen.pageScroll?.()?.scrollable) ?? done.scrollable;
|
|
1401
|
+
const after = this.look(undefined, options.who);
|
|
1402
|
+
const head = done.moved ? `Scrolled ${typeof to === "string" ? to : "to the row"}: ${ScreenText.scrolled(now)}.` : `Nothing moved, ${typeof to === "string" ? "it goes no further" : "the row is in sight"}: ${ScreenText.scrolled(now)}.`;
|
|
1403
|
+
return { text: `${head}
|
|
1404
|
+
|
|
1405
|
+
${ScreenText.changes(before, after) ?? after}` };
|
|
1406
|
+
}
|
|
1407
|
+
static spot(input) {
|
|
1408
|
+
const unit = (value) => typeof value === "number" && value >= 0 && value <= 1;
|
|
1409
|
+
if (!unit(input.x)) {
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
return { x: input.x, y: unit(input.y) ? input.y : 0.5 };
|
|
1413
|
+
}
|
|
1414
|
+
dispose() {
|
|
1415
|
+
this.cursor.dispose();
|
|
1416
|
+
}
|
|
1417
|
+
async part(target, name, input, options) {
|
|
1418
|
+
const part = this.host.screen.parts().find((one) => one.id === target.part);
|
|
1419
|
+
const action = part?.actions.find((one) => one.name === name) ?? (target.item ? part?.actions.find((one) => one.name === "open") : undefined);
|
|
1420
|
+
if (!part || !action) {
|
|
1421
|
+
throw new Error(part ? `Say what to do to it: ${part.actions.map((one) => one.name).join(", ")}.` : "That part is gone: app_look again.");
|
|
1422
|
+
}
|
|
1423
|
+
const item = target.item ? part.items.find((one) => one.id === target.item) : undefined;
|
|
1424
|
+
const says = `${action.label}${item ? ` “${item.text.slice(0, 30)}”` : ""}`;
|
|
1425
|
+
if (action.danger && !await this.agree(says, options)) {
|
|
1426
|
+
return { text: "The user did not agree: it was not done." };
|
|
1427
|
+
}
|
|
1428
|
+
const show = options.show === true || !action.command;
|
|
1429
|
+
if (show && this.host.watched()) {
|
|
1430
|
+
const element = this.host.screen.element(target.part, target.item, action.name);
|
|
1431
|
+
const spot = ScreenDriver.spot(input);
|
|
1432
|
+
if (element) {
|
|
1433
|
+
await this.cursor.point(spot ? element.querySelector("canvas") ?? element : element, says, spot);
|
|
1434
|
+
await this.cursor.press();
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
const done = await this.host.screen.act(target.part, action.name, target.item, input, show);
|
|
1438
|
+
if ("command" in done) {
|
|
1439
|
+
return { text: says, command: done.command, input: done.input };
|
|
1440
|
+
}
|
|
1441
|
+
await this.settle(60, 450);
|
|
1442
|
+
return { text: `${done.text ?? "Done."}
|
|
1443
|
+
|
|
1444
|
+
${this.look(target.part)}` };
|
|
1445
|
+
}
|
|
1446
|
+
async control(index, action, input, options) {
|
|
1447
|
+
const element = this.elements[index];
|
|
1448
|
+
const before = options.who !== undefined ? this.shown.get(options.who) : undefined;
|
|
1449
|
+
if (!element?.isConnected || !ScreenDom.visible(element)) {
|
|
1450
|
+
throw new Error("It is not on the screen any more: app_look again.");
|
|
1451
|
+
}
|
|
1452
|
+
const label = ScreenDom.labelOf(element);
|
|
1453
|
+
const text = typeof input.text === "string" ? input.text : typeof input.value === "string" ? input.value : undefined;
|
|
1454
|
+
const typing = element.matches('input:not([type=checkbox]):not([type=radio]):not([type=button]):not([type=submit]), textarea, [contenteditable="true"], [role=textbox]');
|
|
1455
|
+
const choosing = element.tagName.toLowerCase() === "select";
|
|
1456
|
+
if ((typing || choosing) && text === undefined) {
|
|
1457
|
+
throw new Error(`Say what to ${choosing ? "choose" : "type"}: input.text.`);
|
|
1458
|
+
}
|
|
1459
|
+
const danger = element.matches(".ta-danger, .ta-danger *, [data-danger]") || /\bdanger\b/.test(element.className);
|
|
1460
|
+
if (danger && !await this.agree(label, options)) {
|
|
1461
|
+
return { text: "The user did not agree: it was not done." };
|
|
1462
|
+
}
|
|
1463
|
+
const watched = this.host.watched();
|
|
1464
|
+
if (watched) {
|
|
1465
|
+
await this.cursor.point(element, typing ? label : choosing ? `${label}: ${text}` : label);
|
|
1466
|
+
await this.cursor.press();
|
|
1467
|
+
}
|
|
1468
|
+
if (typing) {
|
|
1469
|
+
const set = (value) => this.setValue(element, value);
|
|
1470
|
+
if (watched) {
|
|
1471
|
+
await this.cursor.type(text ?? "", set);
|
|
1472
|
+
} else {
|
|
1473
|
+
set(text ?? "");
|
|
1474
|
+
}
|
|
1475
|
+
if (input.submit === true || action === "submit") {
|
|
1476
|
+
for (const type of ["keydown", "keyup"]) {
|
|
1477
|
+
element.dispatchEvent(new KeyboardEvent(type, { key: "Enter", code: "Enter", bubbles: true, cancelable: true }));
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
} else if (choosing) {
|
|
1481
|
+
const select = element;
|
|
1482
|
+
const option = [...select.options].find((one) => one.value === text || one.textContent?.trim() === text);
|
|
1483
|
+
if (!option) {
|
|
1484
|
+
throw new Error(`It has no choice ${JSON.stringify(text)}: ${[...select.options].map((one) => one.textContent?.trim()).join(", ")}.`);
|
|
1485
|
+
}
|
|
1486
|
+
this.setValue(element, option.value);
|
|
1487
|
+
} else {
|
|
1488
|
+
element.click();
|
|
1489
|
+
}
|
|
1490
|
+
await this.settle(60, 500);
|
|
1491
|
+
const after = this.look(undefined, options.who);
|
|
1492
|
+
return { text: `Done: ${label}.
|
|
1493
|
+
|
|
1494
|
+
${ScreenText.changes(before, after) ?? after}` };
|
|
1495
|
+
}
|
|
1496
|
+
find(words2) {
|
|
1497
|
+
const norm = (text) => text.replace(/\s+/g, " ").trim().toLowerCase();
|
|
1498
|
+
const want = norm(words2);
|
|
1499
|
+
let best = 0;
|
|
1500
|
+
let found = [];
|
|
1501
|
+
for (const [id, name] of this.names) {
|
|
1502
|
+
const have = norm(name);
|
|
1503
|
+
const score = (have === want ? 6 : have.startsWith(want) ? 4 : have.includes(want) ? 2 : 0) + (score0(id) && have.includes(want) ? 1 : 0);
|
|
1504
|
+
if (score > best) {
|
|
1505
|
+
best = score;
|
|
1506
|
+
found = [id];
|
|
1507
|
+
} else if (score === best && score > 0) {
|
|
1508
|
+
found.push(id);
|
|
1509
|
+
}
|
|
1510
|
+
}
|
|
1511
|
+
if (found.length === 1) {
|
|
1512
|
+
return found[0];
|
|
1513
|
+
}
|
|
1514
|
+
const quoted = JSON.stringify(words2);
|
|
1515
|
+
return {
|
|
1516
|
+
why: found.length ? `More than one is ${quoted}: ${found.slice(0, 6).map((one) => `[${one}] “${this.names.get(one)}”`).join(" · ")}; app_do one by its id.` : `Nothing on the screen is ${quoted}: app_do by an id of what it shows.`
|
|
1517
|
+
};
|
|
1518
|
+
}
|
|
1519
|
+
async settle(min, max) {
|
|
1520
|
+
const document2 = this.window.document;
|
|
1521
|
+
const Observer = this.window.MutationObserver;
|
|
1522
|
+
if (!Observer || !document2.body) {
|
|
1523
|
+
await wait2(max);
|
|
1524
|
+
return;
|
|
1525
|
+
}
|
|
1526
|
+
const start = Date.now();
|
|
1527
|
+
let last = start;
|
|
1528
|
+
const observer = new Observer((records) => {
|
|
1529
|
+
for (const record of records) {
|
|
1530
|
+
const node = record.target;
|
|
1531
|
+
const element = node.nodeType === 1 ? node : node.parentElement;
|
|
1532
|
+
if (record.attributeName !== "style" && !element?.closest("svg, .ta-agent-layer, .ta-screen-pill, .ta-screen-box")) {
|
|
1533
|
+
last = Date.now();
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
});
|
|
1538
|
+
observer.observe(document2.body, { subtree: true, childList: true, attributes: true, characterData: true });
|
|
1539
|
+
try {
|
|
1540
|
+
for (;; ) {
|
|
1541
|
+
await wait2(40);
|
|
1542
|
+
const now = Date.now();
|
|
1543
|
+
if (now - start >= max || now - start >= min && now - last >= 120) {
|
|
1544
|
+
return;
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
} finally {
|
|
1548
|
+
observer.disconnect();
|
|
1549
|
+
}
|
|
1550
|
+
}
|
|
1551
|
+
async agree(what, options) {
|
|
1552
|
+
return options.confirm ? options.confirm(what) : false;
|
|
1553
|
+
}
|
|
1554
|
+
setValue(element, value) {
|
|
1555
|
+
const window2 = this.window;
|
|
1556
|
+
if (element.isContentEditable) {
|
|
1557
|
+
element.textContent = value;
|
|
1558
|
+
element.dispatchEvent(new window2.InputEvent("input", { bubbles: true }));
|
|
1559
|
+
return;
|
|
1560
|
+
}
|
|
1561
|
+
const proto = element instanceof window2.HTMLTextAreaElement ? window2.HTMLTextAreaElement.prototype : element instanceof window2.HTMLSelectElement ? window2.HTMLSelectElement.prototype : window2.HTMLInputElement.prototype;
|
|
1562
|
+
Object.getOwnPropertyDescriptor(proto, "value")?.set?.call(element, value);
|
|
1563
|
+
element.dispatchEvent(new window2.Event(element instanceof window2.HTMLSelectElement ? "change" : "input", { bubbles: true }));
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
// web-src/screen-window.ts
|
|
1568
|
+
var SCREEN_IDLE_MS = 2 * 60000;
|
|
1569
|
+
var INPUTS = ["pointerdown", "keydown", "wheel", "touchmove"];
|
|
1570
|
+
var STYLE2 = `
|
|
1571
|
+
.ta-screen-box { position: fixed; z-index: 2147483000; display: flex; flex-direction: column; overflow: hidden; border-radius: 14px; border: 1px solid var(--ta-border, rgba(0,0,0,.12)); background: var(--ta-side, #fff); color: var(--ta-text, #111); box-shadow: var(--ta-shadow-pop, 0 12px 32px rgba(0,0,0,.18)); font: 13px/1.4 var(--ta-sans, system-ui, sans-serif); transition: transform 260ms cubic-bezier(.2,.8,.2,1), opacity 200ms; }
|
|
1572
|
+
.ta-screen-box[data-hidden] { transform: translate3d(0, 24px, 0); opacity: 0; pointer-events: none; }
|
|
1573
|
+
.ta-screen-box[data-away] { left: -300vw !important; top: 0 !important; transition: none; }
|
|
1574
|
+
.ta-screen-head { flex: none; display: flex; align-items: center; gap: 8px; height: 36px; padding: 0 6px 0 12px; border-bottom: 1px solid var(--ta-border-muted, rgba(0,0,0,.08)); cursor: grab; user-select: none; touch-action: none; }
|
|
1575
|
+
.ta-screen-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ta-muted, #aaa); flex: none; }
|
|
1576
|
+
.ta-screen-box[data-busy] .ta-screen-dot, .ta-screen-pill .ta-screen-dot { background: var(--ta-accent, #07c160); animation: ta-screen-pulse 1.2s ease-in-out infinite; }
|
|
1577
|
+
@keyframes ta-screen-pulse { 50% { opacity: .35; } }
|
|
1578
|
+
.ta-screen-title { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
|
|
1579
|
+
.ta-screen-title small { margin-left: 6px; font-weight: 400; color: var(--ta-secondary, #666); }
|
|
1580
|
+
.ta-screen-button { flex: none; white-space: nowrap; height: 26px; padding: 0 9px; border: none; border-radius: 7px; background: transparent; color: var(--ta-secondary, #555); font: inherit; font-size: 12px; cursor: pointer; }
|
|
1581
|
+
.ta-screen-button:hover { background: var(--ta-hover, rgba(0,0,0,.06)); color: var(--ta-text, #111); }
|
|
1582
|
+
.ta-screen-button.ta-screen-stop { color: var(--ta-red, #e5484d); }
|
|
1583
|
+
.ta-screen-view { position: relative; flex: 1; overflow: hidden; background: var(--ta-body, #f2f2f2); }
|
|
1584
|
+
.ta-screen-view iframe { position: absolute; left: 0; top: 0; border: 0; transform-origin: 0 0; pointer-events: none; background: var(--ta-main, #fff); }
|
|
1585
|
+
.ta-screen-pill { position: fixed; z-index: 2147483001; left: 50%; top: calc(env(safe-area-inset-top, 0px) + 52px); transform: translate3d(-50%, 0, 0); display: flex; align-items: center; gap: 4px; height: 34px; padding: 0 4px 0 12px; border-radius: 17px; background: var(--ta-side, #fff); color: var(--ta-text, #111); border: 1px solid var(--ta-border, rgba(0,0,0,.12)); box-shadow: var(--ta-shadow-pop, 0 8px 24px rgba(0,0,0,.16)); font: 13px/1 var(--ta-sans, system-ui, sans-serif); transition: opacity 200ms, transform 260ms cubic-bezier(.2,.8,.2,1); }
|
|
1586
|
+
.ta-screen-pill[data-hidden] { opacity: 0; transform: translate3d(-50%, -12px, 0); pointer-events: none; }
|
|
1587
|
+
.ta-screen-pill span { margin: 0 4px 0 2px; font-weight: 600; white-space: nowrap; }
|
|
1588
|
+
.ta-screen-pill .ta-screen-button[hidden] { display: none; }
|
|
1589
|
+
@media (prefers-reduced-motion: reduce) { .ta-screen-box, .ta-screen-pill { transition: none; } .ta-screen-dot { animation: none !important; } }
|
|
1590
|
+
`;
|
|
1591
|
+
var wait3 = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
1592
|
+
|
|
1593
|
+
class ScreenWindow {
|
|
1594
|
+
pi;
|
|
1595
|
+
words;
|
|
1596
|
+
box;
|
|
1597
|
+
frame;
|
|
1598
|
+
pill;
|
|
1599
|
+
title;
|
|
1600
|
+
sizeButton;
|
|
1601
|
+
size = { width: 1280, height: 800 };
|
|
1602
|
+
big = false;
|
|
1603
|
+
awayFor;
|
|
1604
|
+
shownOnPhone = false;
|
|
1605
|
+
busy = 0;
|
|
1606
|
+
from;
|
|
1607
|
+
idleTimer;
|
|
1608
|
+
restTimer;
|
|
1609
|
+
own;
|
|
1610
|
+
position;
|
|
1611
|
+
hereBusy = 0;
|
|
1612
|
+
hereTimer;
|
|
1613
|
+
lastInput = 0;
|
|
1614
|
+
input = (event) => {
|
|
1615
|
+
if (!event.target?.closest?.(".ta-screen-pill, .ta-screen-box, .ta-agent-layer")) {
|
|
1616
|
+
this.lastInput = Date.now();
|
|
1617
|
+
}
|
|
1618
|
+
};
|
|
1619
|
+
constructor(pi, words2) {
|
|
1620
|
+
this.pi = pi;
|
|
1621
|
+
this.words = words2;
|
|
1622
|
+
for (const type of INPUTS) {
|
|
1623
|
+
window.addEventListener(type, this.input, { capture: true, passive: true });
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
async run(ask) {
|
|
1627
|
+
if (!ask.apart) {
|
|
1628
|
+
return this.runHere(ask);
|
|
1629
|
+
}
|
|
1630
|
+
clearTimeout(this.idleTimer);
|
|
1631
|
+
this.begin(ask.from);
|
|
1632
|
+
const outer = document.activeElement;
|
|
1633
|
+
try {
|
|
1634
|
+
return await this.step(await this.driver(), ask, ask.show === true);
|
|
1635
|
+
} finally {
|
|
1636
|
+
if (document.activeElement === this.frame && outer instanceof HTMLElement) {
|
|
1637
|
+
outer.focus({ preventScroll: true });
|
|
1638
|
+
}
|
|
1639
|
+
this.end();
|
|
1640
|
+
}
|
|
1641
|
+
}
|
|
1642
|
+
dispose() {
|
|
1643
|
+
for (const type of INPUTS) {
|
|
1644
|
+
window.removeEventListener(type, this.input, { capture: true });
|
|
1645
|
+
}
|
|
1646
|
+
clearTimeout(this.hereTimer);
|
|
1647
|
+
this.close();
|
|
1648
|
+
this.pill?.remove();
|
|
1649
|
+
this.pill = undefined;
|
|
1650
|
+
this.own?.dispose();
|
|
1651
|
+
}
|
|
1652
|
+
async runHere(ask) {
|
|
1653
|
+
this.own ??= new ScreenDriver(window, {
|
|
1654
|
+
screen: this.pi.ui.screen,
|
|
1655
|
+
watched: () => document.visibilityState === "visible",
|
|
1656
|
+
device: () => this.device(),
|
|
1657
|
+
touch: () => typeof matchMedia === "function" && matchMedia("(pointer: coarse)").matches,
|
|
1658
|
+
quiet: () => this.quiet(),
|
|
1659
|
+
linger: 60000
|
|
1660
|
+
});
|
|
1661
|
+
const own = this.own;
|
|
1662
|
+
this.from = ask.from;
|
|
1663
|
+
const looking = ask.kind === "look";
|
|
1664
|
+
if (!looking) {
|
|
1665
|
+
this.hereBusy++;
|
|
1666
|
+
clearTimeout(this.hereTimer);
|
|
1667
|
+
this.showPill(true, false);
|
|
1668
|
+
}
|
|
1669
|
+
try {
|
|
1670
|
+
return await this.step(own, ask, true);
|
|
1671
|
+
} finally {
|
|
1672
|
+
if (!looking) {
|
|
1673
|
+
this.hereBusy = Math.max(0, this.hereBusy - 1);
|
|
1674
|
+
this.lingerHere();
|
|
1675
|
+
}
|
|
1676
|
+
}
|
|
1677
|
+
}
|
|
1678
|
+
async step(driver, ask, show) {
|
|
1679
|
+
const who = ask.from?.sessionId ?? "";
|
|
1680
|
+
if (ask.kind === "look") {
|
|
1681
|
+
return { text: driver.look(ask.part, who) };
|
|
1682
|
+
}
|
|
1683
|
+
if (ask.kind === "go") {
|
|
1684
|
+
return { text: await driver.go(String(ask.to ?? ""), who) };
|
|
1685
|
+
}
|
|
1686
|
+
if (ask.to) {
|
|
1687
|
+
await driver.go(ask.to);
|
|
1688
|
+
}
|
|
1689
|
+
if (ask.scroll) {
|
|
1690
|
+
return driver.scroll(String(ask.id ?? ""), ask.scroll, { who });
|
|
1691
|
+
}
|
|
1692
|
+
return driver.do(String(ask.id ?? ""), ask.action, ask.input, {
|
|
1693
|
+
show,
|
|
1694
|
+
who,
|
|
1695
|
+
...ask.find ? { find: ask.find } : {},
|
|
1696
|
+
confirm: (what) => this.confirm(what, ask)
|
|
1697
|
+
});
|
|
1698
|
+
}
|
|
1699
|
+
lingerHere() {
|
|
1700
|
+
clearTimeout(this.hereTimer);
|
|
1701
|
+
const since = Date.now();
|
|
1702
|
+
const check = async () => {
|
|
1703
|
+
if (this.hereBusy > 0) {
|
|
1704
|
+
return;
|
|
1705
|
+
}
|
|
1706
|
+
const sessionId = this.from?.sessionId;
|
|
1707
|
+
const idle = Date.now() - since;
|
|
1708
|
+
const going = !!sessionId && idle < 30000 && await this.pi.server.listSessions().then((list) => list.some((one) => one.sessionId === sessionId && one.isStreaming), () => false);
|
|
1709
|
+
if (this.hereBusy > 0) {
|
|
1710
|
+
return;
|
|
1711
|
+
}
|
|
1712
|
+
if (going || idle < (sessionId ? 1200 : 2500)) {
|
|
1713
|
+
this.hereTimer = setTimeout(() => void check(), 1000);
|
|
1714
|
+
return;
|
|
1715
|
+
}
|
|
1716
|
+
if (this.busy === 0) {
|
|
1717
|
+
this.showPill(false);
|
|
1718
|
+
}
|
|
1719
|
+
this.own?.hideCursor();
|
|
1720
|
+
};
|
|
1721
|
+
this.hereTimer = setTimeout(() => void check(), 1200);
|
|
1722
|
+
}
|
|
1723
|
+
async quiet() {
|
|
1724
|
+
const until = Date.now() + 8000;
|
|
1725
|
+
while (Date.now() - this.lastInput < 1200 && Date.now() < until) {
|
|
1726
|
+
await wait3(150);
|
|
1727
|
+
}
|
|
1728
|
+
}
|
|
1729
|
+
confirm(what, ask) {
|
|
1730
|
+
return this.pi.ui.confirm(`${ask.from?.name ?? "Ta"}: ${what}`, undefined, { danger: true });
|
|
1731
|
+
}
|
|
1732
|
+
device() {
|
|
1733
|
+
return window.innerWidth < 640 ? "phone" : "computer";
|
|
1734
|
+
}
|
|
1735
|
+
async driver() {
|
|
1736
|
+
if (!this.frame?.isConnected) {
|
|
1737
|
+
this.open();
|
|
1738
|
+
}
|
|
1739
|
+
const frame = this.frame;
|
|
1740
|
+
const until = Date.now() + 25000;
|
|
1741
|
+
const found = () => frame.contentWindow?.taScreen;
|
|
1742
|
+
while (!found() && Date.now() < until) {
|
|
1743
|
+
await wait3(150);
|
|
1744
|
+
}
|
|
1745
|
+
const driver = found();
|
|
1746
|
+
if (!driver) {
|
|
1747
|
+
this.close();
|
|
1748
|
+
throw new Error("Ta's screen did not open in time: try again, or do it with app_run.");
|
|
1749
|
+
}
|
|
1750
|
+
frame.contentWindow.taScreenWatched = () => this.watched();
|
|
1751
|
+
await driver.ready();
|
|
1752
|
+
return driver;
|
|
1753
|
+
}
|
|
1754
|
+
watched() {
|
|
1755
|
+
return !!this.box && !this.box.hasAttribute("data-hidden") && !this.box.hasAttribute("data-away") && document.visibilityState === "visible";
|
|
1756
|
+
}
|
|
1757
|
+
open() {
|
|
1758
|
+
this.style();
|
|
1759
|
+
const phone = this.device() === "phone";
|
|
1760
|
+
this.size = phone ? { width: window.innerWidth, height: window.innerHeight } : { width: Math.min(1440, Math.max(1024, window.innerWidth)), height: Math.min(900, Math.max(680, window.innerHeight)) };
|
|
1761
|
+
const box = document.createElement("div");
|
|
1762
|
+
box.className = "ta-screen-box";
|
|
1763
|
+
box.setAttribute("role", "region");
|
|
1764
|
+
box.dataset.hidden = "";
|
|
1765
|
+
const head = document.createElement("div");
|
|
1766
|
+
head.className = "ta-screen-head";
|
|
1767
|
+
const dot = document.createElement("span");
|
|
1768
|
+
dot.className = "ta-screen-dot";
|
|
1769
|
+
const title = document.createElement("div");
|
|
1770
|
+
title.className = "ta-screen-title";
|
|
1771
|
+
const sizeButton = this.button(this.words().bigger, () => this.resize(!this.big));
|
|
1772
|
+
const stop = this.button(this.words().stop, () => this.stop());
|
|
1773
|
+
stop.classList.add("ta-screen-stop");
|
|
1774
|
+
const hide = this.button(this.words().hide, () => this.putAway());
|
|
1775
|
+
head.append(dot, title, ...phone ? [] : [sizeButton], stop, hide);
|
|
1776
|
+
const view = document.createElement("div");
|
|
1777
|
+
view.className = "ta-screen-view";
|
|
1778
|
+
const frame = document.createElement("iframe");
|
|
1779
|
+
frame.title = this.words().screen;
|
|
1780
|
+
frame.tabIndex = -1;
|
|
1781
|
+
frame.setAttribute("aria-hidden", "true");
|
|
1782
|
+
frame.src = `${location.pathname}?ta-screen`;
|
|
1783
|
+
Object.assign(frame.style, { width: `${this.size.width}px`, height: `${this.size.height}px` });
|
|
1784
|
+
view.appendChild(frame);
|
|
1785
|
+
box.append(head, view);
|
|
1786
|
+
box.setAttribute("aria-label", this.words().screen);
|
|
1787
|
+
this.drag(head, box);
|
|
1788
|
+
document.body.appendChild(box);
|
|
1789
|
+
Object.assign(this, { box, frame, title, sizeButton });
|
|
1790
|
+
this.layout();
|
|
1791
|
+
this.away(true);
|
|
1792
|
+
}
|
|
1793
|
+
button(label, run) {
|
|
1794
|
+
const button = document.createElement("button");
|
|
1795
|
+
button.type = "button";
|
|
1796
|
+
button.className = "ta-screen-button";
|
|
1797
|
+
button.textContent = label;
|
|
1798
|
+
button.addEventListener("click", (event) => {
|
|
1799
|
+
event.stopPropagation();
|
|
1800
|
+
run();
|
|
1801
|
+
});
|
|
1802
|
+
button.addEventListener("pointerdown", (event) => event.stopPropagation());
|
|
1803
|
+
return button;
|
|
1804
|
+
}
|
|
1805
|
+
layout() {
|
|
1806
|
+
const box = this.box;
|
|
1807
|
+
const frame = this.frame;
|
|
1808
|
+
if (!box || !frame) {
|
|
1809
|
+
return;
|
|
1810
|
+
}
|
|
1811
|
+
if (this.device() === "phone") {
|
|
1812
|
+
const height = Math.round(window.innerHeight * 0.74);
|
|
1813
|
+
const scale2 = Math.min(1, (height - 36) / this.size.height, window.innerWidth / this.size.width);
|
|
1814
|
+
Object.assign(box.style, {
|
|
1815
|
+
left: "0px",
|
|
1816
|
+
right: "0px",
|
|
1817
|
+
bottom: "0px",
|
|
1818
|
+
top: "auto",
|
|
1819
|
+
width: "auto",
|
|
1820
|
+
height: `${height}px`,
|
|
1821
|
+
borderRadius: "16px 16px 0 0"
|
|
1822
|
+
});
|
|
1823
|
+
Object.assign(frame.style, { transform: `scale(${scale2})`, left: `${Math.max(0, (window.innerWidth - this.size.width * scale2) / 2)}px` });
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1826
|
+
const width = this.big ? Math.min(720, window.innerWidth - 48) : 340;
|
|
1827
|
+
const scale = width / this.size.width;
|
|
1828
|
+
const position = this.position ?? { right: 20, bottom: 20 };
|
|
1829
|
+
Object.assign(box.style, {
|
|
1830
|
+
width: `${width}px`,
|
|
1831
|
+
height: `${Math.round(this.size.height * scale) + 37}px`,
|
|
1832
|
+
right: `${position.right}px`,
|
|
1833
|
+
bottom: `${position.bottom}px`,
|
|
1834
|
+
left: "auto",
|
|
1835
|
+
top: "auto"
|
|
1836
|
+
});
|
|
1837
|
+
frame.style.transform = `scale(${scale})`;
|
|
1838
|
+
}
|
|
1839
|
+
resize(big) {
|
|
1840
|
+
this.big = big;
|
|
1841
|
+
if (this.sizeButton) {
|
|
1842
|
+
this.sizeButton.textContent = big ? this.words().smaller : this.words().bigger;
|
|
1843
|
+
}
|
|
1844
|
+
this.layout();
|
|
1845
|
+
}
|
|
1846
|
+
drag(head, box) {
|
|
1847
|
+
head.addEventListener("pointerdown", (event) => {
|
|
1848
|
+
if (this.device() === "phone") {
|
|
1849
|
+
return;
|
|
1850
|
+
}
|
|
1851
|
+
const start = { x: event.clientX, y: event.clientY, ...this.position ?? { right: 20, bottom: 20 } };
|
|
1852
|
+
head.setPointerCapture(event.pointerId);
|
|
1853
|
+
const move = (moved) => {
|
|
1854
|
+
const right = Math.min(window.innerWidth - 80, Math.max(0, start.right - (moved.clientX - start.x)));
|
|
1855
|
+
const bottom = Math.min(window.innerHeight - 40, Math.max(0, start.bottom - (moved.clientY - start.y)));
|
|
1856
|
+
this.position = { right, bottom };
|
|
1857
|
+
Object.assign(box.style, { right: `${right}px`, bottom: `${bottom}px` });
|
|
1858
|
+
};
|
|
1859
|
+
const up = () => {
|
|
1860
|
+
head.removeEventListener("pointermove", move);
|
|
1861
|
+
head.removeEventListener("pointerup", up);
|
|
1862
|
+
};
|
|
1863
|
+
head.addEventListener("pointermove", move);
|
|
1864
|
+
head.addEventListener("pointerup", up);
|
|
1865
|
+
});
|
|
1866
|
+
}
|
|
1867
|
+
away(away) {
|
|
1868
|
+
if (!this.box) {
|
|
1869
|
+
return;
|
|
1870
|
+
}
|
|
1871
|
+
if (away) {
|
|
1872
|
+
this.box.dataset.hidden = "";
|
|
1873
|
+
setTimeout(() => this.box?.hasAttribute("data-hidden") && this.box.setAttribute("data-away", ""), 240);
|
|
1874
|
+
} else {
|
|
1875
|
+
this.box.removeAttribute("data-away");
|
|
1876
|
+
this.layout();
|
|
1877
|
+
this.box.getBoundingClientRect();
|
|
1878
|
+
this.box.removeAttribute("data-hidden");
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
begin(from) {
|
|
1882
|
+
clearTimeout(this.restTimer);
|
|
1883
|
+
this.busy++;
|
|
1884
|
+
if (from?.sessionId !== this.from?.sessionId) {
|
|
1885
|
+
this.awayFor = undefined;
|
|
1886
|
+
}
|
|
1887
|
+
this.from = from;
|
|
1888
|
+
if (!this.frame?.isConnected) {
|
|
1889
|
+
this.open();
|
|
1890
|
+
}
|
|
1891
|
+
this.box?.setAttribute("data-busy", "");
|
|
1892
|
+
this.heading();
|
|
1893
|
+
if (this.device() === "phone") {
|
|
1894
|
+
this.showPill(true, true);
|
|
1895
|
+
} else if (this.awayFor !== (from?.sessionId ?? "")) {
|
|
1896
|
+
this.away(false);
|
|
1897
|
+
}
|
|
1898
|
+
}
|
|
1899
|
+
end() {
|
|
1900
|
+
this.busy = Math.max(0, this.busy - 1);
|
|
1901
|
+
clearTimeout(this.restTimer);
|
|
1902
|
+
this.restTimer = setTimeout(() => {
|
|
1903
|
+
if (this.busy === 0) {
|
|
1904
|
+
this.box?.removeAttribute("data-busy");
|
|
1905
|
+
this.heading();
|
|
1906
|
+
if (this.hereBusy === 0) {
|
|
1907
|
+
this.showPill(false);
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
}, 4000);
|
|
1911
|
+
clearTimeout(this.idleTimer);
|
|
1912
|
+
this.idleTimer = setTimeout(() => this.busy === 0 && this.close(), SCREEN_IDLE_MS);
|
|
1913
|
+
}
|
|
1914
|
+
heading() {
|
|
1915
|
+
if (!this.title) {
|
|
1916
|
+
return;
|
|
1917
|
+
}
|
|
1918
|
+
const busy = this.busy > 0 || this.box?.hasAttribute("data-busy");
|
|
1919
|
+
this.title.textContent = busy ? this.words().operating : this.words().screen;
|
|
1920
|
+
if (busy && this.from?.name) {
|
|
1921
|
+
const small = document.createElement("small");
|
|
1922
|
+
small.textContent = this.from.name;
|
|
1923
|
+
this.title.appendChild(small);
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
putAway() {
|
|
1927
|
+
this.awayFor = this.from?.sessionId ?? "";
|
|
1928
|
+
this.shownOnPhone = false;
|
|
1929
|
+
this.away(true);
|
|
1930
|
+
if (this.device() === "phone" && this.busy > 0) {
|
|
1931
|
+
this.showPill(true, true);
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
showPill(show, watchable = false) {
|
|
1935
|
+
if (show && !this.pill) {
|
|
1936
|
+
this.style();
|
|
1937
|
+
const pill = document.createElement("div");
|
|
1938
|
+
pill.className = "ta-screen-pill";
|
|
1939
|
+
pill.dataset.hidden = "";
|
|
1940
|
+
const dot = document.createElement("span");
|
|
1941
|
+
dot.className = "ta-screen-dot";
|
|
1942
|
+
const words2 = document.createElement("span");
|
|
1943
|
+
words2.textContent = this.words().operating;
|
|
1944
|
+
const watch2 = this.button(this.words().watch, () => {
|
|
1945
|
+
this.shownOnPhone = true;
|
|
1946
|
+
this.showPill(false);
|
|
1947
|
+
this.away(false);
|
|
1948
|
+
});
|
|
1949
|
+
const stop = this.button(this.words().stop, () => this.stop());
|
|
1950
|
+
stop.classList.add("ta-screen-stop");
|
|
1951
|
+
watch2.classList.add("ta-screen-watch");
|
|
1952
|
+
pill.append(dot, words2, watch2, stop);
|
|
1953
|
+
document.body.appendChild(pill);
|
|
1954
|
+
this.pill = pill;
|
|
1955
|
+
}
|
|
1956
|
+
if (!this.pill) {
|
|
1957
|
+
return;
|
|
1958
|
+
}
|
|
1959
|
+
const watch = this.pill.querySelector(".ta-screen-watch");
|
|
1960
|
+
if (watch && show) {
|
|
1961
|
+
watch.hidden = !watchable;
|
|
1962
|
+
}
|
|
1963
|
+
if (show && !this.shownOnPhone) {
|
|
1964
|
+
this.pill.getBoundingClientRect();
|
|
1965
|
+
this.pill.removeAttribute("data-hidden");
|
|
1966
|
+
} else {
|
|
1967
|
+
this.pill.dataset.hidden = "";
|
|
1968
|
+
}
|
|
1969
|
+
}
|
|
1970
|
+
stop() {
|
|
1971
|
+
const sessionId = this.from?.sessionId;
|
|
1972
|
+
if (sessionId) {
|
|
1973
|
+
this.pi.server.abort(sessionId).catch(() => {
|
|
1974
|
+
return;
|
|
1975
|
+
});
|
|
1976
|
+
}
|
|
1977
|
+
this.busy = 0;
|
|
1978
|
+
this.hereBusy = 0;
|
|
1979
|
+
clearTimeout(this.hereTimer);
|
|
1980
|
+
this.own?.hideCursor();
|
|
1981
|
+
this.box?.removeAttribute("data-busy");
|
|
1982
|
+
this.heading();
|
|
1983
|
+
if (this.box) {
|
|
1984
|
+
this.putAway();
|
|
1985
|
+
}
|
|
1986
|
+
this.showPill(false);
|
|
1987
|
+
}
|
|
1988
|
+
close() {
|
|
1989
|
+
clearTimeout(this.idleTimer);
|
|
1990
|
+
clearTimeout(this.restTimer);
|
|
1991
|
+
this.box?.remove();
|
|
1992
|
+
this.box = undefined;
|
|
1993
|
+
this.frame = undefined;
|
|
1994
|
+
this.busy = 0;
|
|
1995
|
+
if (this.hereBusy === 0) {
|
|
1996
|
+
this.showPill(false);
|
|
1997
|
+
}
|
|
1998
|
+
this.shownOnPhone = false;
|
|
1999
|
+
}
|
|
2000
|
+
style() {
|
|
2001
|
+
if (!document.getElementById("ta-screen-style")) {
|
|
2002
|
+
const style = document.createElement("style");
|
|
2003
|
+
style.id = "ta-screen-style";
|
|
2004
|
+
style.textContent = STYLE2;
|
|
2005
|
+
document.head.appendChild(style);
|
|
2006
|
+
}
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
|
|
2010
|
+
// web-src/tool-line.ts
|
|
2011
|
+
function text(value) {
|
|
2012
|
+
return typeof value === "string" ? value.trim() : "";
|
|
2013
|
+
}
|
|
2014
|
+
var AppToolLine;
|
|
2015
|
+
((AppToolLine) => {
|
|
2016
|
+
function of(tool, args, words2, language) {
|
|
2017
|
+
if (tool === "app_commands") {
|
|
2018
|
+
return { verb: words2.commands, target: text(args.query) };
|
|
2019
|
+
}
|
|
2020
|
+
if (tool === "app_look") {
|
|
2021
|
+
return { verb: words2.look, target: "" };
|
|
2022
|
+
}
|
|
2023
|
+
if (tool === "app_go") {
|
|
2024
|
+
const to = text(args.to);
|
|
2025
|
+
const [kind, rest = ""] = to.split(/:(.*)/s);
|
|
2026
|
+
const target = kind === "settings" ? rest ? `${words2.settings} · ${rest}` : words2.settings : kind === "discover" ? words2.discover : /^(chat|page|list)$/.test(kind) ? rest.replace(/^.*\//, "") : to;
|
|
2027
|
+
return { verb: words2.go, target };
|
|
2028
|
+
}
|
|
2029
|
+
if (tool === "app_do" && args.scroll !== undefined) {
|
|
2030
|
+
const scroll = args.scroll;
|
|
2031
|
+
const step = typeof scroll === "string" ? words2[`scroll.${scroll}`] : undefined;
|
|
2032
|
+
const find = scroll && typeof scroll === "object" ? text(scroll.find) : "";
|
|
2033
|
+
return { verb: words2.scroll, target: (step ?? find).slice(0, 40) };
|
|
2034
|
+
}
|
|
2035
|
+
if (tool === "app_do") {
|
|
2036
|
+
const input2 = args.input && typeof args.input === "object" ? args.input : {};
|
|
2037
|
+
const game = words2[`game.${text(args.action)}`];
|
|
2038
|
+
if (game) {
|
|
2039
|
+
return { verb: game, target: text(input2.key) };
|
|
2040
|
+
}
|
|
2041
|
+
return { verb: words2.do, target: (text(args.find) || text(input2.text)).slice(0, 40) };
|
|
2042
|
+
}
|
|
2043
|
+
if (tool !== "app_run") {
|
|
2044
|
+
return;
|
|
2045
|
+
}
|
|
2046
|
+
const command = text(args.command);
|
|
2047
|
+
const input = args.input && typeof args.input === "object" ? args.input : {};
|
|
2048
|
+
switch (command) {
|
|
2049
|
+
case "settings.set": {
|
|
2050
|
+
const changes = input.changes && typeof input.changes === "object" ? Object.keys(input.changes) : [];
|
|
2051
|
+
const names = changes.map((key) => {
|
|
2052
|
+
const spec = TA_WEB_SETTINGS.find((one) => one.key === key);
|
|
2053
|
+
return spec ? TaLanguageUtil.pick(language, spec.title) : key;
|
|
2054
|
+
});
|
|
2055
|
+
return { verb: words2["settings.set"], target: names.join(TaLanguageUtil.cjk(language) ? "、" : ", ") };
|
|
2056
|
+
}
|
|
2057
|
+
case "chats.pin":
|
|
2058
|
+
return { verb: input.pinned === false ? words2.unpin : words2["chats.pin"], target: "" };
|
|
2059
|
+
case "extensions.set":
|
|
2060
|
+
return { verb: input.enabled === false ? words2.off : input.enabled === true ? words2.on : words2["extensions.set"], target: text(input.name) };
|
|
2061
|
+
case "asks.list":
|
|
2062
|
+
case "asks.answer":
|
|
2063
|
+
return { verb: words2[command], target: "" };
|
|
2064
|
+
case "settings.get":
|
|
2065
|
+
case "chats.list":
|
|
2066
|
+
case "chats.stop":
|
|
2067
|
+
case "extensions.list":
|
|
2068
|
+
return { verb: words2[command], target: text(input.query) };
|
|
2069
|
+
default:
|
|
2070
|
+
return { verb: words2.run, target: command };
|
|
2071
|
+
}
|
|
2072
|
+
}
|
|
2073
|
+
AppToolLine.of = of;
|
|
2074
|
+
})(AppToolLine ||= {});
|
|
2075
|
+
|
|
2076
|
+
// web-src/index.ts
|
|
2077
|
+
function activate(pi) {
|
|
2078
|
+
let since = Number.POSITIVE_INFINITY;
|
|
2079
|
+
let stopped = false;
|
|
2080
|
+
const words2 = () => AppUseWords.of(pi.ui.language);
|
|
2081
|
+
pi.registerToolRenderer?.((name, next) => {
|
|
2082
|
+
if (!TA_APP_TOOLS.includes(name)) {
|
|
2083
|
+
return next();
|
|
2084
|
+
}
|
|
2085
|
+
return { renderCall: (args) => AppToolLine.of(name, args, words2(), pi.ui.language) ?? { verb: name, target: "" } };
|
|
2086
|
+
});
|
|
2087
|
+
if (pi.ui.screen?.agent) {
|
|
2088
|
+
const frame = window;
|
|
2089
|
+
const driver = new ScreenDriver(window, {
|
|
2090
|
+
screen: pi.ui.screen,
|
|
2091
|
+
watched: () => frame.taScreenWatched?.() ?? false,
|
|
2092
|
+
device: () => window.innerWidth < 640 ? "phone" : "computer",
|
|
2093
|
+
apart: true,
|
|
2094
|
+
linger: 6000
|
|
2095
|
+
});
|
|
2096
|
+
frame.taScreen = driver;
|
|
2097
|
+
return () => {
|
|
2098
|
+
if (frame.taScreen === driver) {
|
|
2099
|
+
frame.taScreen = undefined;
|
|
2100
|
+
}
|
|
2101
|
+
driver.dispose();
|
|
2102
|
+
};
|
|
2103
|
+
}
|
|
2104
|
+
const screens = new ScreenWindow(pi, words2);
|
|
2105
|
+
const stopAnswering = pi.ui.answer?.("screen", (payload) => screens.run(payload));
|
|
2106
|
+
const show = (entry) => {
|
|
2107
|
+
const undo = entry.undo ? {
|
|
2108
|
+
label: words2().undo,
|
|
2109
|
+
run: () => {
|
|
2110
|
+
pi.callHost("undo", entry.id).then(() => entry.command.startsWith("extensions.") ? pi.reloadExtensions() : undefined).catch((error) => pi.ui.notify(error instanceof Error ? error.message : String(error), "error"));
|
|
2111
|
+
}
|
|
2112
|
+
} : undefined;
|
|
2113
|
+
pi.ui.notify(entry.notice, "info", {
|
|
2114
|
+
key: `ta-app-use:${entry.id}`,
|
|
2115
|
+
ta: true,
|
|
2116
|
+
...entry.changes ? { changes: entry.changes } : {},
|
|
2117
|
+
...undo ? { action: undo } : {}
|
|
2118
|
+
});
|
|
2119
|
+
};
|
|
2120
|
+
const read = async (tell) => {
|
|
2121
|
+
const done = AppUseUtil.entries(await pi.storage.get(DONE_KEY).catch(() => {
|
|
2122
|
+
return;
|
|
2123
|
+
}));
|
|
2124
|
+
const latest = done.reduce((most, entry) => Math.max(most, entry.at), 0);
|
|
2125
|
+
if (tell && !stopped) {
|
|
2126
|
+
const fresh = AppUseUtil.fresh(done, since);
|
|
2127
|
+
for (const entry of fresh) {
|
|
2128
|
+
show(entry);
|
|
2129
|
+
}
|
|
2130
|
+
if (fresh.some((entry) => entry.command.startsWith("extensions."))) {
|
|
2131
|
+
pi.reloadExtensions().catch(() => {
|
|
2132
|
+
return;
|
|
2133
|
+
});
|
|
2134
|
+
}
|
|
2135
|
+
}
|
|
2136
|
+
since = Math.max(Number.isFinite(since) ? since : 0, latest);
|
|
2137
|
+
};
|
|
2138
|
+
const first = read(false);
|
|
2139
|
+
const off = pi.storage.onChange((keys) => {
|
|
2140
|
+
if (keys.includes(DONE_KEY)) {
|
|
2141
|
+
first.then(() => read(true));
|
|
2142
|
+
}
|
|
2143
|
+
});
|
|
2144
|
+
return () => {
|
|
2145
|
+
stopped = true;
|
|
2146
|
+
off();
|
|
2147
|
+
stopAnswering?.();
|
|
2148
|
+
screens.dispose();
|
|
2149
|
+
};
|
|
2150
|
+
}
|
|
2151
|
+
export {
|
|
2152
|
+
activate as default
|
|
2153
|
+
};
|