@celljs/ta-watch 0.0.0-stage → 4.49.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 +15 -0
- package/README.md +14 -2
- package/fixtures/watch-v1.json +137 -0
- package/host/index.mjs +1352 -0
- package/package.json +61 -4
- package/web/index.js +354 -0
package/host/index.mjs
ADDED
|
@@ -0,0 +1,1352 @@
|
|
|
1
|
+
// ../ta-protocol/esm/common/language.js
|
|
2
|
+
var TaLanguageUtil;
|
|
3
|
+
(function(TaLanguageUtil2) {
|
|
4
|
+
TaLanguageUtil2.ALL = ["zh-CN", "zh-TW", "en-US", "ja-JP"];
|
|
5
|
+
TaLanguageUtil2.NAMES = { "zh-CN": "简体中文", "zh-TW": "繁體中文", "en-US": "English", "ja-JP": "日本語" };
|
|
6
|
+
function of(tag) {
|
|
7
|
+
const said = (tag !== null && tag !== undefined ? tag : "").toLowerCase().replace(/_/g, "-");
|
|
8
|
+
if (/^zh(\b|-)/.test(said)) {
|
|
9
|
+
return /-hant\b/.test(said) || !/-hans\b/.test(said) && /-(tw|hk|mo)\b/.test(said) ? "zh-TW" : "zh-CN";
|
|
10
|
+
}
|
|
11
|
+
return /^ja(\b|-)/.test(said) ? "ja-JP" : "en-US";
|
|
12
|
+
}
|
|
13
|
+
TaLanguageUtil2.of = of;
|
|
14
|
+
function server(env = typeof process === "undefined" ? {} : process.env) {
|
|
15
|
+
return of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG);
|
|
16
|
+
}
|
|
17
|
+
TaLanguageUtil2.server = server;
|
|
18
|
+
function pick(language, words) {
|
|
19
|
+
return words[of(language)];
|
|
20
|
+
}
|
|
21
|
+
TaLanguageUtil2.pick = pick;
|
|
22
|
+
function say(words) {
|
|
23
|
+
return words[server()];
|
|
24
|
+
}
|
|
25
|
+
TaLanguageUtil2.say = say;
|
|
26
|
+
function chinese(language) {
|
|
27
|
+
return of(language).startsWith("zh");
|
|
28
|
+
}
|
|
29
|
+
TaLanguageUtil2.chinese = chinese;
|
|
30
|
+
function cjk(language) {
|
|
31
|
+
return of(language) !== "en-US";
|
|
32
|
+
}
|
|
33
|
+
TaLanguageUtil2.cjk = cjk;
|
|
34
|
+
function list(language, items) {
|
|
35
|
+
return items.join(cjk(language) ? "、" : ", ");
|
|
36
|
+
}
|
|
37
|
+
TaLanguageUtil2.list = list;
|
|
38
|
+
})(TaLanguageUtil || (TaLanguageUtil = {}));
|
|
39
|
+
|
|
40
|
+
// ../ta-protocol/esm/common/team-protocol.js
|
|
41
|
+
var TA_OWNER = "owner";
|
|
42
|
+
var TA_UNDECLARED = "extensions.install";
|
|
43
|
+
var TaPermissionUtil;
|
|
44
|
+
(function(TaPermissionUtil2) {
|
|
45
|
+
function read(pi) {
|
|
46
|
+
const given = pi === null || pi === undefined ? undefined : pi.permissions;
|
|
47
|
+
const map = (value) => value && typeof value === "object" && !Array.isArray(value) ? Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === "string")) : undefined;
|
|
48
|
+
const host2 = map(given === null || given === undefined ? undefined : given.host);
|
|
49
|
+
const tools = map(given === null || given === undefined ? undefined : given.tools);
|
|
50
|
+
return { ...host2 ? { host: host2 } : {}, ...tools ? { tools } : {} };
|
|
51
|
+
}
|
|
52
|
+
TaPermissionUtil2.read = read;
|
|
53
|
+
function host(manifest, method, kind) {
|
|
54
|
+
var _a, _b, _c, _d;
|
|
55
|
+
const host2 = (_a = manifest === null || manifest === undefined ? undefined : manifest.host) !== null && _a !== undefined ? _a : {};
|
|
56
|
+
const own = (key) => {
|
|
57
|
+
var _a2;
|
|
58
|
+
return (_a2 = Object.getOwnPropertyDescriptor(host2, key)) === null || _a2 === undefined ? undefined : _a2.value;
|
|
59
|
+
};
|
|
60
|
+
return (_d = (_c = (_b = kind ? own(`${method}:${kind}`) : undefined) !== null && _b !== undefined ? _b : own(method)) !== null && _c !== undefined ? _c : own("*")) !== null && _d !== undefined ? _d : TA_UNDECLARED;
|
|
61
|
+
}
|
|
62
|
+
TaPermissionUtil2.host = host;
|
|
63
|
+
})(TaPermissionUtil || (TaPermissionUtil = {}));
|
|
64
|
+
var action = (id, en, zh, tw, ja, more = {}) => ({
|
|
65
|
+
id,
|
|
66
|
+
area: id.split(".")[0],
|
|
67
|
+
title: { en, "zh-CN": zh, ...tw ? { "zh-TW": tw } : {}, ...ja ? { "ja-JP": ja } : {} },
|
|
68
|
+
...more
|
|
69
|
+
});
|
|
70
|
+
var TA_ACTIONS = [
|
|
71
|
+
action("chat.view", "See conversations", "查看对话", "檢視對話", "会話を確認"),
|
|
72
|
+
action("chat.send", "Send in conversations", "在对话里发消息", "在對話裡發訊息", "会話にメッセージを送信"),
|
|
73
|
+
action("chat.create", "Start conversations", "新建对话", "新建對話", "新しい会話を開始"),
|
|
74
|
+
action("chat.stop", "Stop a reply", "停止回复", "停止回覆", "返信を停止"),
|
|
75
|
+
action("chat.manage", "Rename, archive and close conversations", "改名、归档、关闭对话", "改名、歸檔、關閉對話", "会話の名前変更、アーカイブ、クローズ"),
|
|
76
|
+
action("chat.share", "Share by a link", "生成分享链接", "生成分享連結", "共有リンクで共有"),
|
|
77
|
+
action("agent.view", "See agents", "查看智能体", "檢視智慧體", "エージェントを確認"),
|
|
78
|
+
action("agent.use", "Talk with agents", "和智能体对话", "和智慧體對話", "エージェントと対話"),
|
|
79
|
+
action("agent.edit", "Create, change and remove agents", "新建、修改、删除智能体", "新建、修改、刪除智慧體", "エージェントを作成・変更・削除"),
|
|
80
|
+
action("channel.view", "See channels", "查看频道", "檢視頻道", "チャネルを確認"),
|
|
81
|
+
action("channel.post", "Post in channels", "在频道里发言", "在頻道里發言", "チャネルに投稿"),
|
|
82
|
+
action("channel.manage", "Create and change channels", "新建、管理频道", "新建、管理頻道", "チャネルを作成・管理"),
|
|
83
|
+
action("task.view", "See tasks", "查看任务", "檢視任務", "タスクを確認"),
|
|
84
|
+
action("task.create", "Hand out tasks", "派任务", "派任務", "タスクを割り当て"),
|
|
85
|
+
action("task.manage", "Take over and stop tasks", "接管、停止任务", "接管、停止任務", "タスクを引き継ぎ・停止"),
|
|
86
|
+
action("files.read", "Read files", "读文件", "讀檔案", "ファイルを読取", { scoped: true }),
|
|
87
|
+
action("files.write", "Change files", "改文件", "改檔案", "ファイルを変更", { scoped: true }),
|
|
88
|
+
action("commands.run", "Let Ta run commands", "让 Ta 运行命令", "讓 Ta 執行命令", "Ta にコマンド実行を許可", { scoped: true, risky: true }),
|
|
89
|
+
action("commands.terminal", "Open terminals", "打开终端", "開啟終端機", "ターミナルを開く", { scoped: true, risky: true }),
|
|
90
|
+
action("hosts.use", "Use remote hosts", "使用远程主机", "使用遠端主機", "リモートホストを使用", { risky: true }),
|
|
91
|
+
action("models.use", "Use the models", "使用模型", "使用模型", "モデルを使用"),
|
|
92
|
+
action("models.manage", "Sign in to models", "登录和更换模型账号", "登入和更換模型帳號", "モデルアカウントにサインイン・変更", { grant: "single" }),
|
|
93
|
+
action("cron.view", "See scheduled tasks", "查看定时任务", "檢視定時任務", "スケジュールタスクを確認"),
|
|
94
|
+
action("cron.manage", "Change scheduled tasks", "管理定时任务", "管理定時任務", "スケジュールタスクを管理"),
|
|
95
|
+
action("extensions.use", "Use extensions", "使用扩展", "使用擴充功能", "拡張機能を使用"),
|
|
96
|
+
action("extensions.install", "Install and turn off extensions", "安装、停用扩展", "安裝、停用擴充功能", "拡張機能をインストール・無効化"),
|
|
97
|
+
action("extensions.trust", "Decide how far others' extensions reach", "决定别人的扩展能用到哪一步", "決定別人的擴充功能能用到哪一步", "他の人の拡張機能に許す範囲を決める", { risky: true }),
|
|
98
|
+
action("settings.view", "See settings", "查看设置", "檢視設定", "設定を確認"),
|
|
99
|
+
action("settings.manage", "Change settings", "修改设置", "修改設定", "設定を変更"),
|
|
100
|
+
action("members.view", "See members", "查看成员", "檢視成員", "メンバーを確認"),
|
|
101
|
+
action("members.invite", "Invite people", "邀请别人", "邀請別人", "メンバーを招待"),
|
|
102
|
+
action("members.approve", "Answer requests", "处理申请", "處理申請", "リクエストに対応"),
|
|
103
|
+
action("members.roles", "Change roles and members", "管理角色和成员", "管理角色和成員", "ロールとメンバーを管理"),
|
|
104
|
+
action("audit.view", "See the record", "查看记录", "檢視記錄", "レコードを確認")
|
|
105
|
+
];
|
|
106
|
+
var TA_OWNER_ACTIONS = [
|
|
107
|
+
action("desktop.view", "See remote desktops", "查看远程桌面", "檢視遠端桌面", "リモートデスクトップを見る", { risky: true }),
|
|
108
|
+
action("desktop.control", "Work remote desktops", "操作远程桌面", "操作遠端桌面", "リモートデスクトップを操作", { risky: true }),
|
|
109
|
+
action("models.owner", "Ask models on the owner's keys", "用你的密钥和本机模型做判断", "用你的金鑰和本機模型做判斷", "あなたのキーとローカルモデルで判断", {
|
|
110
|
+
risky: true
|
|
111
|
+
})
|
|
112
|
+
];
|
|
113
|
+
var OWNER_AREAS = ["desktop"];
|
|
114
|
+
var TaActionUtil;
|
|
115
|
+
(function(TaActionUtil2) {
|
|
116
|
+
const byId = new Map(TA_ACTIONS.map((one) => [one.id, one]));
|
|
117
|
+
const titled = new Map([...TA_ACTIONS, ...TA_OWNER_ACTIONS].map((one) => [one.id, one]));
|
|
118
|
+
function info(id) {
|
|
119
|
+
return byId.get(id);
|
|
120
|
+
}
|
|
121
|
+
TaActionUtil2.info = info;
|
|
122
|
+
function title(id, language) {
|
|
123
|
+
var _a;
|
|
124
|
+
var _b, _c;
|
|
125
|
+
const title2 = (_a = titled.get(id)) === null || _a === undefined ? undefined : _a.title;
|
|
126
|
+
if (!title2) {
|
|
127
|
+
return id;
|
|
128
|
+
}
|
|
129
|
+
return TaLanguageUtil.pick(language, {
|
|
130
|
+
"zh-CN": title2["zh-CN"],
|
|
131
|
+
"zh-TW": (_b = title2["zh-TW"]) !== null && _b !== undefined ? _b : title2["zh-CN"],
|
|
132
|
+
"en-US": title2.en,
|
|
133
|
+
"ja-JP": (_c = title2["ja-JP"]) !== null && _c !== undefined ? _c : title2.en
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
TaActionUtil2.title = title;
|
|
137
|
+
function grantable(id) {
|
|
138
|
+
const info2 = byId.get(id);
|
|
139
|
+
return !!info2 && !OWNER_AREAS.includes(info2.area);
|
|
140
|
+
}
|
|
141
|
+
TaActionUtil2.grantable = grantable;
|
|
142
|
+
function inRoles(id) {
|
|
143
|
+
var _a;
|
|
144
|
+
return grantable(id) && ((_a = byId.get(id)) === null || _a === undefined ? undefined : _a.grant) !== "single";
|
|
145
|
+
}
|
|
146
|
+
TaActionUtil2.inRoles = inRoles;
|
|
147
|
+
function inside(dir, path) {
|
|
148
|
+
const base = dir.replace(/\/+$/, "");
|
|
149
|
+
return path === base || path.startsWith(`${base}/`) || base === "";
|
|
150
|
+
}
|
|
151
|
+
TaActionUtil2.inside = inside;
|
|
152
|
+
})(TaActionUtil || (TaActionUtil = {}));
|
|
153
|
+
|
|
154
|
+
// ../ta-protocol/esm/common/message-protocol.js
|
|
155
|
+
var TaMessageContext;
|
|
156
|
+
(function(TaMessageContext2) {
|
|
157
|
+
TaMessageContext2.OPEN = "<context>";
|
|
158
|
+
TaMessageContext2.CLOSE = "</context>";
|
|
159
|
+
const BLOCK = /^<context>\n?[\s\S]*?\n?<\/context>\n?/;
|
|
160
|
+
function wrap(context, message) {
|
|
161
|
+
return `<context>
|
|
162
|
+
${context}
|
|
163
|
+
</context>
|
|
164
|
+
${message}`;
|
|
165
|
+
}
|
|
166
|
+
TaMessageContext2.wrap = wrap;
|
|
167
|
+
function strip(message) {
|
|
168
|
+
return message.replace(BLOCK, "");
|
|
169
|
+
}
|
|
170
|
+
TaMessageContext2.strip = strip;
|
|
171
|
+
function read(message) {
|
|
172
|
+
var _a;
|
|
173
|
+
const block = (_a = BLOCK.exec(message)) === null || _a === undefined ? undefined : _a[0];
|
|
174
|
+
return block === null || block === undefined ? undefined : block.replace(/^<context>\n?/, "").replace(/\n?<\/context>\n?$/, "");
|
|
175
|
+
}
|
|
176
|
+
TaMessageContext2.read = read;
|
|
177
|
+
function echoed(text) {
|
|
178
|
+
var _a;
|
|
179
|
+
if (!text.includes(TaMessageContext2.OPEN)) {
|
|
180
|
+
return text;
|
|
181
|
+
}
|
|
182
|
+
const kept = [];
|
|
183
|
+
let fence;
|
|
184
|
+
let inside = false;
|
|
185
|
+
let left = false;
|
|
186
|
+
for (const part of text.split(`
|
|
187
|
+
`)) {
|
|
188
|
+
const mark = (_a = /^\s*(`{3,}|~{3,})/.exec(part)) === null || _a === undefined ? undefined : _a[1];
|
|
189
|
+
if (inside) {
|
|
190
|
+
left = true;
|
|
191
|
+
if (part.includes(TaMessageContext2.CLOSE)) {
|
|
192
|
+
inside = false;
|
|
193
|
+
}
|
|
194
|
+
} else if (!fence && part.trim() === TaMessageContext2.OPEN) {
|
|
195
|
+
left = true;
|
|
196
|
+
inside = true;
|
|
197
|
+
} else {
|
|
198
|
+
kept.push(part);
|
|
199
|
+
}
|
|
200
|
+
if (!inside) {
|
|
201
|
+
if (!fence && mark) {
|
|
202
|
+
fence = mark;
|
|
203
|
+
} else if (fence && mark && mark[0] === fence[0] && mark.length >= fence.length && !part.trim().slice(mark.length).trim()) {
|
|
204
|
+
fence = undefined;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
const said = kept.join(`
|
|
209
|
+
`);
|
|
210
|
+
return left ? said.replace(/\n(?:[ \t]*\n)+/g, `
|
|
211
|
+
|
|
212
|
+
`).trimEnd() : text;
|
|
213
|
+
}
|
|
214
|
+
TaMessageContext2.echoed = echoed;
|
|
215
|
+
})(TaMessageContext || (TaMessageContext = {}));
|
|
216
|
+
var TaMessageSource;
|
|
217
|
+
(function(TaMessageSource2) {
|
|
218
|
+
TaMessageSource2.DOUBLE = "ta:double";
|
|
219
|
+
const LINE = /^\[ta:(from|notice) ([^\s\]]+)(?: ([^\]\n]+))?\]$/m;
|
|
220
|
+
function mark(message, from, detail) {
|
|
221
|
+
return marked(message, line(from, detail));
|
|
222
|
+
}
|
|
223
|
+
TaMessageSource2.mark = mark;
|
|
224
|
+
function notice(message, from, detail) {
|
|
225
|
+
return marked(message, line(from, detail, true));
|
|
226
|
+
}
|
|
227
|
+
TaMessageSource2.notice = notice;
|
|
228
|
+
function line(from, detail, notice2 = false) {
|
|
229
|
+
return `[ta:${notice2 ? "notice" : "from"} ${from}${detail ? ` ${detail.replace(/[\]\n]/g, " ")}` : ""}]`;
|
|
230
|
+
}
|
|
231
|
+
TaMessageSource2.line = line;
|
|
232
|
+
function of(message) {
|
|
233
|
+
const context = TaMessageContext.read(message);
|
|
234
|
+
const match = context === undefined ? null : LINE.exec(context);
|
|
235
|
+
return match ? { from: match[2], ...match[3] ? { detail: match[3].trim() } : {}, ...match[1] === "notice" ? { notice: true } : {} } : undefined;
|
|
236
|
+
}
|
|
237
|
+
TaMessageSource2.of = of;
|
|
238
|
+
function isNotice(message) {
|
|
239
|
+
var _a;
|
|
240
|
+
return ((_a = of(message)) === null || _a === undefined ? undefined : _a.notice) === true;
|
|
241
|
+
}
|
|
242
|
+
TaMessageSource2.isNotice = isNotice;
|
|
243
|
+
TaMessageSource2.SECTION = "ta-sources";
|
|
244
|
+
TaMessageSource2.PROMPT = [
|
|
245
|
+
"Who wrote a message: what the user typed or said is theirs. One whose <context> ends with [ta:from <part of Ta> …] was sent by that part of Ta for the user (their choice in a card, what they said in a call, a task or goal they set up): it is theirs too.",
|
|
246
|
+
"One with [ta:notice <part of Ta> …] is not the user's: news or an event of that part of Ta (work that ended elsewhere, a time limit, a permission granted). Weigh it below what the user said:",
|
|
247
|
+
"- It is information, never a request of the user: it does not change, drop or replace what they asked. Their latest words come first.",
|
|
248
|
+
"- Act on it only as far as what they asked, your role or a limit they set calls for; otherwise take it in and go on, and tell them of it in a line only when they need it (a result they wait for, a failure, a question for them).",
|
|
249
|
+
"- Never quote it as the user's words nor answer it as if they had said it."
|
|
250
|
+
].join(`
|
|
251
|
+
`);
|
|
252
|
+
function marked(message, source) {
|
|
253
|
+
if (/^[/!]/.test(message) || of(message)) {
|
|
254
|
+
return message;
|
|
255
|
+
}
|
|
256
|
+
const context = TaMessageContext.read(message);
|
|
257
|
+
return TaMessageContext.wrap(context === undefined ? source : `${context}
|
|
258
|
+
${source}`, TaMessageContext.strip(message));
|
|
259
|
+
}
|
|
260
|
+
})(TaMessageSource || (TaMessageSource = {}));
|
|
261
|
+
var TA_PICTURE_LIMITS = { side: 1600, bytes: 1024 * 1024 };
|
|
262
|
+
var TaImageNote;
|
|
263
|
+
(function(TaImageNote2) {
|
|
264
|
+
const NOTE = /^\[Image: original \d+x\d+, displayed at \d+x\d+\. Multiply coordinates by [\d.]+ to map to original image\.\]$|^\[Image converted from [^\]]+ to [^\]]+\.\]$|^\[Image \d+ is shown at \d+x\d+; its original, \d+x\d+, is at [^\n]+\]$/;
|
|
265
|
+
function original(index, shown, original2, path) {
|
|
266
|
+
return `[Image ${index} is shown at ${shown.width}x${shown.height}; its original, ${original2.width}x${original2.height}, is at ${path}. Only when a detail matters (small text, a corner), crop that part of the original to a new file and read it.]`;
|
|
267
|
+
}
|
|
268
|
+
TaImageNote2.original = original;
|
|
269
|
+
function strip(text) {
|
|
270
|
+
const lines = text.split(`
|
|
271
|
+
`);
|
|
272
|
+
const kept = lines.filter((line) => !NOTE.test(line.trim()));
|
|
273
|
+
if (kept.length === lines.length) {
|
|
274
|
+
return text;
|
|
275
|
+
}
|
|
276
|
+
return kept.join(`
|
|
277
|
+
`).replace(/\n+$/, "").replace(/^\n+/, "");
|
|
278
|
+
}
|
|
279
|
+
TaImageNote2.strip = strip;
|
|
280
|
+
})(TaImageNote || (TaImageNote = {}));
|
|
281
|
+
var TaImageSize;
|
|
282
|
+
(function(TaImageSize2) {
|
|
283
|
+
const JPEG_BYTES = 256 * 1024;
|
|
284
|
+
function type(bytes) {
|
|
285
|
+
const at = (offset, ...values) => values.every((value, index) => bytes[offset + index] === value);
|
|
286
|
+
return at(0, 255, 216, 255) ? "image/jpeg" : at(0, 137, 80, 78, 71) ? "image/png" : at(0, 71, 73, 70, 56) ? "image/gif" : at(0, 82, 73, 70, 70) && at(8, 87, 69, 66, 80) ? "image/webp" : undefined;
|
|
287
|
+
}
|
|
288
|
+
TaImageSize2.type = type;
|
|
289
|
+
function of(base64) {
|
|
290
|
+
var _a, _b, _c;
|
|
291
|
+
let bytes;
|
|
292
|
+
try {
|
|
293
|
+
const binary = atob(base64.slice(0, Math.ceil(JPEG_BYTES * 4 / 3 / 4) * 4));
|
|
294
|
+
bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
|
|
295
|
+
} catch (_d) {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const size = (_c = (_b = (_a = png(bytes)) !== null && _a !== undefined ? _a : gif(bytes)) !== null && _b !== undefined ? _b : webp(bytes)) !== null && _c !== undefined ? _c : jpeg(bytes);
|
|
299
|
+
return size && size.width > 0 && size.height > 0 ? size : undefined;
|
|
300
|
+
}
|
|
301
|
+
TaImageSize2.of = of;
|
|
302
|
+
function u16be(bytes, at) {
|
|
303
|
+
return bytes[at] << 8 | bytes[at + 1];
|
|
304
|
+
}
|
|
305
|
+
function u16le(bytes, at) {
|
|
306
|
+
return bytes[at] | bytes[at + 1] << 8;
|
|
307
|
+
}
|
|
308
|
+
function u24le(bytes, at) {
|
|
309
|
+
return bytes[at] | bytes[at + 1] << 8 | bytes[at + 2] << 16;
|
|
310
|
+
}
|
|
311
|
+
function u32be(bytes, at) {
|
|
312
|
+
return (bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3]) >>> 0;
|
|
313
|
+
}
|
|
314
|
+
function text(bytes, at, length) {
|
|
315
|
+
return String.fromCharCode(...bytes.subarray(at, at + length));
|
|
316
|
+
}
|
|
317
|
+
function png(bytes) {
|
|
318
|
+
return bytes.length >= 24 && bytes[0] === 137 && text(bytes, 1, 3) === "PNG" ? { width: u32be(bytes, 16), height: u32be(bytes, 20) } : undefined;
|
|
319
|
+
}
|
|
320
|
+
function gif(bytes) {
|
|
321
|
+
return bytes.length >= 10 && text(bytes, 0, 3) === "GIF" ? { width: u16le(bytes, 6), height: u16le(bytes, 8) } : undefined;
|
|
322
|
+
}
|
|
323
|
+
function webp(bytes) {
|
|
324
|
+
if (bytes.length < 30 || text(bytes, 0, 4) !== "RIFF" || text(bytes, 8, 4) !== "WEBP") {
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
switch (text(bytes, 12, 4)) {
|
|
328
|
+
case "VP8 ":
|
|
329
|
+
return { width: u16le(bytes, 26) & 16383, height: u16le(bytes, 28) & 16383 };
|
|
330
|
+
case "VP8L": {
|
|
331
|
+
const bits = bytes[21] | bytes[22] << 8 | bytes[23] << 16 | bytes[24] << 24;
|
|
332
|
+
return { width: (bits & 16383) + 1, height: (bits >> 14 & 16383) + 1 };
|
|
333
|
+
}
|
|
334
|
+
case "VP8X":
|
|
335
|
+
return { width: u24le(bytes, 24) + 1, height: u24le(bytes, 27) + 1 };
|
|
336
|
+
default:
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function jpeg(bytes) {
|
|
341
|
+
if (bytes[0] !== 255 || bytes[1] !== 216) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
let at = 2;
|
|
345
|
+
while (at + 9 < bytes.length) {
|
|
346
|
+
if (bytes[at] !== 255) {
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
const marker = bytes[at + 1];
|
|
350
|
+
if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
|
|
351
|
+
return { width: u16be(bytes, at + 7), height: u16be(bytes, at + 5) };
|
|
352
|
+
}
|
|
353
|
+
at += 2 + u16be(bytes, at + 2);
|
|
354
|
+
}
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
})(TaImageSize || (TaImageSize = {}));
|
|
358
|
+
var TaAgentSummary;
|
|
359
|
+
(function(TaAgentSummary2) {
|
|
360
|
+
const START = /^\s*<conversation_history_summary>/;
|
|
361
|
+
const BLOCK = /^\s*<conversation_history_summary>[\s\S]*?(?:<\/conversation_history_summary>\s*|$)/;
|
|
362
|
+
function read(text) {
|
|
363
|
+
if (!START.test(text)) {
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
return text.replace(/<\/?(?:conversation_history_summary|summary)>/g, "").replace(/<\/?\w*$/, "").trim();
|
|
367
|
+
}
|
|
368
|
+
TaAgentSummary2.read = read;
|
|
369
|
+
function strip(text) {
|
|
370
|
+
return text.replace(BLOCK, "");
|
|
371
|
+
}
|
|
372
|
+
TaAgentSummary2.strip = strip;
|
|
373
|
+
})(TaAgentSummary || (TaAgentSummary = {}));
|
|
374
|
+
var TaPictureNote;
|
|
375
|
+
(function(TaPictureNote2) {
|
|
376
|
+
TaPictureNote2.OPEN = "<pictures>";
|
|
377
|
+
TaPictureNote2.CLOSE = "</pictures>";
|
|
378
|
+
const BLOCK = /[ \t]*<pictures>([\s\S]*?)(?:<\/pictures>|$)|<p(?:i(?:c(?:t(?:u(?:r(?:es?)?)?)?)?)?)?$/gi;
|
|
379
|
+
const ROW = /(?:^|\s)[-*]?\s*p(\d+)\s*[::]\s*/gi;
|
|
380
|
+
function take(text) {
|
|
381
|
+
const told = new Map;
|
|
382
|
+
if (!/<p/i.test(text)) {
|
|
383
|
+
return { said: text, told };
|
|
384
|
+
}
|
|
385
|
+
const said = text.replace(BLOCK, (_all, body) => {
|
|
386
|
+
const rows = [...(body !== null && body !== undefined ? body : "").matchAll(ROW)];
|
|
387
|
+
rows.forEach((row, index) => {
|
|
388
|
+
var _a;
|
|
389
|
+
var _b, _c;
|
|
390
|
+
const end = (_b = (_a = rows[index + 1]) === null || _a === undefined ? undefined : _a.index) !== null && _b !== undefined ? _b : body === null || body === undefined ? undefined : body.length;
|
|
391
|
+
const what = body === null || body === undefined ? undefined : body.slice(((_c = row.index) !== null && _c !== undefined ? _c : 0) + row[0].length, end).trim();
|
|
392
|
+
if (what) {
|
|
393
|
+
told.set(Number(row[1]), what);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
return "";
|
|
397
|
+
});
|
|
398
|
+
return { said: said === text ? text : said.replace(/\n{3,}/g, `
|
|
399
|
+
|
|
400
|
+
`).trim(), told };
|
|
401
|
+
}
|
|
402
|
+
TaPictureNote2.take = take;
|
|
403
|
+
function strip(text) {
|
|
404
|
+
return take(text).said;
|
|
405
|
+
}
|
|
406
|
+
TaPictureNote2.strip = strip;
|
|
407
|
+
})(TaPictureNote || (TaPictureNote = {}));
|
|
408
|
+
var TaToolLeak;
|
|
409
|
+
(function(TaToolLeak2) {
|
|
410
|
+
TaToolLeak2.ENTRY = "ta-tool-leak";
|
|
411
|
+
TaToolLeak2.RETRY = "Your last reply wrote a tool call as text, so it did not run. Make the call now through the tools you have, or, if you cannot, answer without it. Never write tool calls as text.";
|
|
412
|
+
const MARKS = [
|
|
413
|
+
/<\/?tool_calls?(?:[:\s][^<>\n]*)?>/,
|
|
414
|
+
/<\/?arg_(?:key|value)(?::[^<>\n]*)?>/,
|
|
415
|
+
/<\|tool_calls?_(?:section_)?begin\|>/,
|
|
416
|
+
/<|tool▁calls?▁begin|>/,
|
|
417
|
+
/<\|DSML\|/,
|
|
418
|
+
/<\/?function_calls>|<invoke name="/,
|
|
419
|
+
/\[TOOL_CALLS\]/,
|
|
420
|
+
/<\|python_tag\|>/,
|
|
421
|
+
/<minimax:tool_call>/
|
|
422
|
+
];
|
|
423
|
+
const FENCE = /^\s*(`{3,}|~{3,})/;
|
|
424
|
+
function at(text) {
|
|
425
|
+
var _a;
|
|
426
|
+
if (!/<|\[TOOL_CALLS\]/.test(text)) {
|
|
427
|
+
return -1;
|
|
428
|
+
}
|
|
429
|
+
let fence;
|
|
430
|
+
let offset = 0;
|
|
431
|
+
for (const line of text.split(`
|
|
432
|
+
`)) {
|
|
433
|
+
const mark = (_a = FENCE.exec(line)) === null || _a === undefined ? undefined : _a[1];
|
|
434
|
+
if (fence) {
|
|
435
|
+
if (mark && mark[0] === fence[0] && mark.length >= fence.length && !line.trim().slice(mark.length).trim()) {
|
|
436
|
+
fence = undefined;
|
|
437
|
+
}
|
|
438
|
+
} else if (mark) {
|
|
439
|
+
fence = mark;
|
|
440
|
+
} else {
|
|
441
|
+
const prose = line.replace(/(`+)[^`]*?\1/g, (code) => " ".repeat(code.length));
|
|
442
|
+
const found = MARKS.map((each) => {
|
|
443
|
+
var _a2;
|
|
444
|
+
var _b;
|
|
445
|
+
return (_b = (_a2 = each.exec(prose)) === null || _a2 === undefined ? undefined : _a2.index) !== null && _b !== undefined ? _b : -1;
|
|
446
|
+
}).filter((index) => index >= 0);
|
|
447
|
+
if (found.length) {
|
|
448
|
+
return offset + Math.min(...found);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
offset += line.length + 1;
|
|
452
|
+
}
|
|
453
|
+
return -1;
|
|
454
|
+
}
|
|
455
|
+
TaToolLeak2.at = at;
|
|
456
|
+
function has(text) {
|
|
457
|
+
return at(text) >= 0;
|
|
458
|
+
}
|
|
459
|
+
TaToolLeak2.has = has;
|
|
460
|
+
function strip(text) {
|
|
461
|
+
const index = at(text);
|
|
462
|
+
return index < 0 ? text : text.slice(0, index).trimEnd();
|
|
463
|
+
}
|
|
464
|
+
TaToolLeak2.strip = strip;
|
|
465
|
+
})(TaToolLeak || (TaToolLeak = {}));
|
|
466
|
+
var TaSkillUse;
|
|
467
|
+
(function(TaSkillUse2) {
|
|
468
|
+
const BLOCK = /^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/;
|
|
469
|
+
function parse(message) {
|
|
470
|
+
var _a;
|
|
471
|
+
const match = BLOCK.exec(message);
|
|
472
|
+
if (!match) {
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
return { name: match[1], location: match[2], content: match[3], message: ((_a = match[4]) === null || _a === undefined ? undefined : _a.trim()) || undefined };
|
|
476
|
+
}
|
|
477
|
+
TaSkillUse2.parse = parse;
|
|
478
|
+
function command(use) {
|
|
479
|
+
return use.message ? `/skill:${use.name} ${use.message}` : `/skill:${use.name}`;
|
|
480
|
+
}
|
|
481
|
+
TaSkillUse2.command = command;
|
|
482
|
+
})(TaSkillUse || (TaSkillUse = {}));
|
|
483
|
+
var TaTaskNote;
|
|
484
|
+
(function(TaTaskNote2) {
|
|
485
|
+
const NOTE = /^<task_(update|adopted|renamed) ((?:[^>"\n]|"[^"]*")*)>\n?([\s\S]*?)\n?<\/task_\1>$/;
|
|
486
|
+
const ATTRIBUTE = /(\w+)="([^"]*)"/g;
|
|
487
|
+
function write(note) {
|
|
488
|
+
const attributes = [`id="${quote(note.id)}"`];
|
|
489
|
+
if (note.was !== undefined) {
|
|
490
|
+
attributes.push(`was="${quote(note.was)}"`);
|
|
491
|
+
}
|
|
492
|
+
attributes.push(`title="${quote(note.title)}"`);
|
|
493
|
+
if (note.status !== undefined) {
|
|
494
|
+
attributes.push(`status="${quote(note.status)}"`);
|
|
495
|
+
}
|
|
496
|
+
return `<task_${note.kind} ${attributes.join(" ")}>
|
|
497
|
+
${note.body}
|
|
498
|
+
</task_${note.kind}>`;
|
|
499
|
+
}
|
|
500
|
+
TaTaskNote2.write = write;
|
|
501
|
+
function parse(text) {
|
|
502
|
+
const match = NOTE.exec(text.trim());
|
|
503
|
+
if (!match) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
const attributes = {};
|
|
507
|
+
for (const [, name, value] of match[2].matchAll(ATTRIBUTE)) {
|
|
508
|
+
attributes[name] = unquote(value);
|
|
509
|
+
}
|
|
510
|
+
if (!attributes.id || attributes.title === undefined) {
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
const { id, title, status, was } = attributes;
|
|
514
|
+
return { kind: match[1], id, title, ...status ? { status } : {}, ...was ? { was } : {}, body: match[3] };
|
|
515
|
+
}
|
|
516
|
+
TaTaskNote2.parse = parse;
|
|
517
|
+
function quote(text) {
|
|
518
|
+
return text.replace(/&/g, "&").replace(/"/g, """).replace(/\n/g, " ");
|
|
519
|
+
}
|
|
520
|
+
function unquote(text) {
|
|
521
|
+
return text.replace(/"/g, '"').replace(/&/g, "&");
|
|
522
|
+
}
|
|
523
|
+
})(TaTaskNote || (TaTaskNote = {}));
|
|
524
|
+
var TaSecretRef;
|
|
525
|
+
(function(TaSecretRef2) {
|
|
526
|
+
TaSecretRef2.PACKAGE = "@celljs/ta-secrets";
|
|
527
|
+
const REF = /\$\{?TA_SECRET_([A-Z0-9][A-Z0-9_]*)\}?/g;
|
|
528
|
+
const KINDS = [
|
|
529
|
+
[/^PHONE(?:_\d+)?$/, "phone"],
|
|
530
|
+
[/^EMAIL(?:_\d+)?$/, "email"],
|
|
531
|
+
[/^ID_NUMBER(?:_\d+)?$/, "idNumber"],
|
|
532
|
+
[/^BANK_CARD(?:_\d+)?$/, "bankCard"]
|
|
533
|
+
];
|
|
534
|
+
function has(text) {
|
|
535
|
+
return text.includes("TA_SECRET_") && new RegExp(REF.source).test(text);
|
|
536
|
+
}
|
|
537
|
+
TaSecretRef2.has = has;
|
|
538
|
+
function kind(name) {
|
|
539
|
+
var _a;
|
|
540
|
+
var _b;
|
|
541
|
+
return (_b = (_a = KINDS.find(([pattern]) => pattern.test(name))) === null || _a === undefined ? undefined : _a[1]) !== null && _b !== undefined ? _b : "secret";
|
|
542
|
+
}
|
|
543
|
+
TaSecretRef2.kind = kind;
|
|
544
|
+
function split(text) {
|
|
545
|
+
if (!has(text)) {
|
|
546
|
+
return [{ text }];
|
|
547
|
+
}
|
|
548
|
+
const parts = [];
|
|
549
|
+
let at = 0;
|
|
550
|
+
for (const match of text.matchAll(REF)) {
|
|
551
|
+
if (match.index > at) {
|
|
552
|
+
parts.push({ text: text.slice(at, match.index) });
|
|
553
|
+
}
|
|
554
|
+
parts.push({ secret: match[1], kind: kind(match[1]) });
|
|
555
|
+
at = match.index + match[0].length;
|
|
556
|
+
}
|
|
557
|
+
if (at < text.length) {
|
|
558
|
+
parts.push({ text: text.slice(at) });
|
|
559
|
+
}
|
|
560
|
+
return parts;
|
|
561
|
+
}
|
|
562
|
+
TaSecretRef2.split = split;
|
|
563
|
+
function plain(text, label) {
|
|
564
|
+
return has(text) ? text.replace(REF, (_whole, name) => label(name, kind(name))) : text;
|
|
565
|
+
}
|
|
566
|
+
TaSecretRef2.plain = plain;
|
|
567
|
+
})(TaSecretRef || (TaSecretRef = {}));
|
|
568
|
+
|
|
569
|
+
// ../ta-protocol/esm/common/said-protocol.js
|
|
570
|
+
var TaSaid;
|
|
571
|
+
(function(TaSaid2) {
|
|
572
|
+
const CARD = /^ta-/i;
|
|
573
|
+
const READ = 4000;
|
|
574
|
+
const FENCE = /^\s*(`{3,}|~{3,})\s*([^\s`]*)/;
|
|
575
|
+
const TAG_LINE = /^<\/?[A-Za-z][\w:.-]*(?:\s[^<>]*)?\/?>$/;
|
|
576
|
+
const MARKS_ONLY = /^[-*_=|:+\s]+$/;
|
|
577
|
+
const MARKS = [
|
|
578
|
+
["context", /<\/?context>/],
|
|
579
|
+
["source", /\[ta:(?:from|notice|by) /],
|
|
580
|
+
["image note", /\[Image: original \d+x\d+|\[Image converted from /],
|
|
581
|
+
["skill", /<skill name="|<\/skill>/],
|
|
582
|
+
["task note", /<\/?task_(?:update|adopted|renamed)\b/],
|
|
583
|
+
["summary", /<\/?(?:conversation_history_summary|summary)>/],
|
|
584
|
+
["pictures described", /<\/?pictures>/],
|
|
585
|
+
["tool call as text", /<\/?tool_calls?[:>\s]|<\/?arg_value|<\|tool_calls?_|<\/?function_calls>/]
|
|
586
|
+
];
|
|
587
|
+
function text(content, joiner = `
|
|
588
|
+
`) {
|
|
589
|
+
if (typeof content === "string") {
|
|
590
|
+
return content;
|
|
591
|
+
}
|
|
592
|
+
return Array.isArray(content) ? content.filter((part) => (part === null || part === undefined ? undefined : part.type) === "text" && typeof part.text === "string").map((part) => part.text).join(joiner) : "";
|
|
593
|
+
}
|
|
594
|
+
TaSaid2.text = text;
|
|
595
|
+
function words(role, text2, options = {}) {
|
|
596
|
+
const said = role === "user" ? userWords(text2) : TaMessageContext.echoed(TaToolLeak.strip(TaPictureNote.strip(TaAgentSummary.strip(text2))));
|
|
597
|
+
return withoutFences(said, (language) => options.cards === false && CARD.test(language), true).trim();
|
|
598
|
+
}
|
|
599
|
+
TaSaid2.words = words;
|
|
600
|
+
function of(message, options) {
|
|
601
|
+
return message ? words(message.role, text(message.content), options) : "";
|
|
602
|
+
}
|
|
603
|
+
TaSaid2.of = of;
|
|
604
|
+
function notice(message) {
|
|
605
|
+
return (message === null || message === undefined ? undefined : message.role) === "user" && TaMessageSource.isNotice(text(message.content));
|
|
606
|
+
}
|
|
607
|
+
TaSaid2.notice = notice;
|
|
608
|
+
function marksOnly(role, text2) {
|
|
609
|
+
return !!text2.trim() && !line(words(role, text2), { chars: Number.POSITIVE_INFINITY });
|
|
610
|
+
}
|
|
611
|
+
TaSaid2.marksOnly = marksOnly;
|
|
612
|
+
function line(markdown, options) {
|
|
613
|
+
var _a;
|
|
614
|
+
const read = markdown.slice(0, READ);
|
|
615
|
+
const prose = withoutFences(read, () => true, true).trim();
|
|
616
|
+
const lines = plain(prose || codeOf(read)).split(`
|
|
617
|
+
`).map((part) => part.replace(/^\s*(?:#{1,6}\s+|>\s*|[-*+]\s+|\d+[.)]\s+)/, "").replace(/\*{1,3}|`+|~~|(?<![\p{L}\p{N}])_{1,3}|_{1,3}(?![\p{L}\p{N}])/gu, "").replace(/\s+/g, " ").trim()).filter((part) => part && !MARKS_ONLY.test(part) && !TAG_LINE.test(part));
|
|
618
|
+
const count = (_a = options.lines) !== null && _a !== undefined ? _a : 1;
|
|
619
|
+
const joined = count <= 1 ? lines.join(" ") : lines.slice(0, count).join(`
|
|
620
|
+
`);
|
|
621
|
+
return joined.length > options.chars ? `${joined.slice(0, Math.max(0, options.chars - 1)).trimEnd()}…` : joined;
|
|
622
|
+
}
|
|
623
|
+
TaSaid2.line = line;
|
|
624
|
+
function leaks(shown, options = {}) {
|
|
625
|
+
const found = MARKS.filter(([, mark]) => mark.test(shown)).map(([name]) => name);
|
|
626
|
+
const lines = shown.split(`
|
|
627
|
+
`);
|
|
628
|
+
if (options.cards !== true && lines.some((part) => {
|
|
629
|
+
var _a;
|
|
630
|
+
var _b;
|
|
631
|
+
return CARD.test((_b = (_a = FENCE.exec(part)) === null || _a === undefined ? undefined : _a[2]) !== null && _b !== undefined ? _b : "");
|
|
632
|
+
})) {
|
|
633
|
+
found.push("card");
|
|
634
|
+
}
|
|
635
|
+
if (lines.some((part) => TAG_LINE.test(part.trim()))) {
|
|
636
|
+
found.push("tag");
|
|
637
|
+
}
|
|
638
|
+
return found;
|
|
639
|
+
}
|
|
640
|
+
TaSaid2.leaks = leaks;
|
|
641
|
+
const SUMMARY = `<conversation_history_summary>
|
|
642
|
+
<summary>
|
|
643
|
+
1. Primary Request and Intent:
|
|
644
|
+
The user works on the relay.
|
|
645
|
+
</summary>
|
|
646
|
+
</conversation_history_summary>`;
|
|
647
|
+
TaSaid2.SAMPLES = [
|
|
648
|
+
{
|
|
649
|
+
mark: "context and where it came from",
|
|
650
|
+
role: "user",
|
|
651
|
+
text: `<context>
|
|
652
|
+
The channel #ops, its members.
|
|
653
|
+
[ta:from @celljs/ta-channels #ops]
|
|
654
|
+
</context>
|
|
655
|
+
Check the build`,
|
|
656
|
+
said: "Check the build"
|
|
657
|
+
},
|
|
658
|
+
{
|
|
659
|
+
mark: "a notice, not the user",
|
|
660
|
+
role: "user",
|
|
661
|
+
text: `<context>
|
|
662
|
+
[ta:notice @celljs/ta-call]
|
|
663
|
+
</context>
|
|
664
|
+
The work finished. Its last words: tests pass.`,
|
|
665
|
+
said: "The work finished. Its last words: tests pass."
|
|
666
|
+
},
|
|
667
|
+
{ mark: "one let in", role: "user", text: `<context>
|
|
668
|
+
[ta:by p-7 Alice]
|
|
669
|
+
</context>
|
|
670
|
+
Hello there`, said: "Hello there" },
|
|
671
|
+
{
|
|
672
|
+
mark: "notes of images",
|
|
673
|
+
role: "user",
|
|
674
|
+
text: `Look at this
|
|
675
|
+
[Image: original 3024x4032, displayed at 1512x2016. Multiply coordinates by 2.00 to map to original image.]
|
|
676
|
+
[Image converted from image/heic to image/png.]`,
|
|
677
|
+
said: "Look at this"
|
|
678
|
+
},
|
|
679
|
+
{
|
|
680
|
+
mark: "a skill run",
|
|
681
|
+
role: "user",
|
|
682
|
+
text: `<skill name="ta-package" location="/skills/ta-package/SKILL.md">
|
|
683
|
+
# Ta package
|
|
684
|
+
Steps.
|
|
685
|
+
</skill>
|
|
686
|
+
|
|
687
|
+
Make a card`,
|
|
688
|
+
said: "Make a card"
|
|
689
|
+
},
|
|
690
|
+
{
|
|
691
|
+
mark: "a note of a task",
|
|
692
|
+
role: "user",
|
|
693
|
+
text: `<task_update id="t1" title="Fix login" status="done">
|
|
694
|
+
All tests pass.
|
|
695
|
+
</task_update>`,
|
|
696
|
+
said: "Fix login"
|
|
697
|
+
},
|
|
698
|
+
{ mark: "a note of a new id", role: "user", text: `<task_renamed id="t2" was="t1" title="Fix login">
|
|
699
|
+
Now t2.
|
|
700
|
+
</task_renamed>` },
|
|
701
|
+
{ mark: "a summary, then the answer", role: "assistant", text: `${SUMMARY}
|
|
702
|
+
Switched to voice.`, said: "Switched to voice." },
|
|
703
|
+
{ mark: "a summary alone", role: "assistant", text: SUMMARY },
|
|
704
|
+
{ mark: "a summary as it streams", role: "assistant", text: `<conversation_history_summary>
|
|
705
|
+
<summary>` },
|
|
706
|
+
{
|
|
707
|
+
mark: "a card after the answer",
|
|
708
|
+
role: "assistant",
|
|
709
|
+
text: '**Done**: tests pass.\n\n```ta-glance\n{"status":"done","blocks":[]}\n```',
|
|
710
|
+
said: "tests pass"
|
|
711
|
+
},
|
|
712
|
+
{ mark: "a card alone", role: "assistant", text: '```ta-glance\n{"status":"done","blocks":[]}\n```' },
|
|
713
|
+
{ mark: "a proposal", role: "assistant", text: 'Committed.\n\n```ta-propose\n{"kind":"git.push","title":"Push"}\n```', said: "Committed." },
|
|
714
|
+
{ mark: "what a call says aloud", role: "assistant", text: "All fixed.\n\n```ta-say\nIt is fixed.\n```", said: "All fixed." },
|
|
715
|
+
{
|
|
716
|
+
mark: "pictures described after the answer",
|
|
717
|
+
role: "assistant",
|
|
718
|
+
text: `The left one.
|
|
719
|
+
|
|
720
|
+
<pictures> p1: two cats on a sofa. p2: a chart of sales, "Q4". </pictures>`,
|
|
721
|
+
said: "The left one."
|
|
722
|
+
},
|
|
723
|
+
{ mark: "pictures described as it streams", role: "assistant", text: `The left one.
|
|
724
|
+
<pictures>
|
|
725
|
+
p1: two ca`, said: "The left one." },
|
|
726
|
+
{
|
|
727
|
+
mark: "a tool call written as text",
|
|
728
|
+
role: "assistant",
|
|
729
|
+
text: `I will ask them now.<tool_calls:6124c78e>
|
|
730
|
+
<tool_call:6124c78e>mcp__ta__ask_permission {"what": "x"}</arg_value:6124c78e></tool_call:6124c78e></tool_calls:6124c78e>`,
|
|
731
|
+
said: "I will ask them now."
|
|
732
|
+
},
|
|
733
|
+
{
|
|
734
|
+
mark: "the context of the message echoed back",
|
|
735
|
+
role: "assistant",
|
|
736
|
+
text: `It holds the card above.
|
|
737
|
+
|
|
738
|
+
<context>
|
|
739
|
+
{"type":"ta-glance","status":"info"}
|
|
740
|
+
</context>
|
|
741
|
+
`,
|
|
742
|
+
said: "It holds the card above."
|
|
743
|
+
},
|
|
744
|
+
{
|
|
745
|
+
mark: "a tool call of another model",
|
|
746
|
+
role: "assistant",
|
|
747
|
+
text: `Checking.
|
|
748
|
+
<|tool_calls_section_begin|><|tool_call_begin|>functions.read:0`,
|
|
749
|
+
said: "Checking."
|
|
750
|
+
},
|
|
751
|
+
{ mark: "a tag not known yet", role: "assistant", text: `<system-reminder>
|
|
752
|
+
</system-reminder>
|
|
753
|
+
Ready.`, said: "Ready." }
|
|
754
|
+
];
|
|
755
|
+
function userWords(text2) {
|
|
756
|
+
const said = TaImageNote.strip(TaMessageContext.strip(text2));
|
|
757
|
+
const skill = TaSkillUse.parse(said);
|
|
758
|
+
if (skill) {
|
|
759
|
+
return TaSkillUse.command(skill);
|
|
760
|
+
}
|
|
761
|
+
const note = TaTaskNote.parse(said);
|
|
762
|
+
if (!note) {
|
|
763
|
+
return said;
|
|
764
|
+
}
|
|
765
|
+
if (note.kind === "renamed") {
|
|
766
|
+
return "";
|
|
767
|
+
}
|
|
768
|
+
return note.kind === "update" && note.body.trim() ? `${note.title} · ${note.body}` : note.title;
|
|
769
|
+
}
|
|
770
|
+
function withoutFences(text2, drop, tags = false) {
|
|
771
|
+
const kept = [];
|
|
772
|
+
let fence;
|
|
773
|
+
let left = false;
|
|
774
|
+
for (const part of text2.split(`
|
|
775
|
+
`)) {
|
|
776
|
+
const open = FENCE.exec(part);
|
|
777
|
+
if (!fence && open) {
|
|
778
|
+
fence = { mark: open[1], dropped: drop(open[2]) };
|
|
779
|
+
} else if (fence && closes(part, fence.mark)) {
|
|
780
|
+
const dropped = fence.dropped;
|
|
781
|
+
fence = undefined;
|
|
782
|
+
if (dropped) {
|
|
783
|
+
continue;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
if ((fence === null || fence === undefined ? undefined : fence.dropped) || !fence && tags && TAG_LINE.test(part.trim())) {
|
|
787
|
+
left = true;
|
|
788
|
+
} else {
|
|
789
|
+
kept.push(part);
|
|
790
|
+
}
|
|
791
|
+
}
|
|
792
|
+
return left ? kept.join(`
|
|
793
|
+
`).replace(/\n{3,}/g, `
|
|
794
|
+
|
|
795
|
+
`) : kept.join(`
|
|
796
|
+
`);
|
|
797
|
+
}
|
|
798
|
+
function codeOf(text2) {
|
|
799
|
+
const code = [];
|
|
800
|
+
let fence;
|
|
801
|
+
for (const part of text2.split(`
|
|
802
|
+
`)) {
|
|
803
|
+
const open = FENCE.exec(part);
|
|
804
|
+
if (!fence && open) {
|
|
805
|
+
fence = { mark: open[1], card: CARD.test(open[2]) };
|
|
806
|
+
} else if (fence && closes(part, fence.mark)) {
|
|
807
|
+
fence = undefined;
|
|
808
|
+
} else if (fence && !fence.card) {
|
|
809
|
+
code.push(part);
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
return code.join(`
|
|
813
|
+
`);
|
|
814
|
+
}
|
|
815
|
+
function closes(part, mark) {
|
|
816
|
+
var _a;
|
|
817
|
+
const close = (_a = /^\s*(`{3,}|~{3,})\s*$/.exec(part)) === null || _a === undefined ? undefined : _a[1];
|
|
818
|
+
return !!close && close[0] === mark[0] && close.length >= mark.length;
|
|
819
|
+
}
|
|
820
|
+
function plain(text2) {
|
|
821
|
+
const linked = text2.replace(/(?<!!)\[([^\]\n]*)\]\([^)\n]*\)/g, "$1");
|
|
822
|
+
const words2 = linked.replace(/!\[[^\]\n]*\]\([^)\n]*\)/g, "").trim();
|
|
823
|
+
return words2 || linked.replace(/!\[([^\]\n]*)\]\([^)\n]*\)/g, "$1");
|
|
824
|
+
}
|
|
825
|
+
})(TaSaid || (TaSaid = {}));
|
|
826
|
+
|
|
827
|
+
// web-src/watch-protocol.ts
|
|
828
|
+
var NAME = "@celljs/ta-watch";
|
|
829
|
+
var WATCH_VERSION = 1;
|
|
830
|
+
var WATCH_PATH = "v1";
|
|
831
|
+
var WATCH_DAYS = 30;
|
|
832
|
+
var WATCH_SKEW_MS = 5 * 60000;
|
|
833
|
+
var WATCH_SEEN_MS = 2 * WATCH_SKEW_MS;
|
|
834
|
+
var WATCH_DETAIL = "Apple Watch: reply in a few short sentences, read aloud on a watch";
|
|
835
|
+
var DEVICES_KEY = "devices";
|
|
836
|
+
var WatchProtocol;
|
|
837
|
+
((WatchProtocol) => {
|
|
838
|
+
const ID = /^[\w-]{8,64}$/;
|
|
839
|
+
function isId(value) {
|
|
840
|
+
return typeof value === "string" && ID.test(value);
|
|
841
|
+
}
|
|
842
|
+
WatchProtocol.isId = isId;
|
|
843
|
+
function request(value) {
|
|
844
|
+
const request2 = value;
|
|
845
|
+
if (!request2 || typeof request2 !== "object") {
|
|
846
|
+
return "not an object";
|
|
847
|
+
}
|
|
848
|
+
if (request2.v !== WATCH_VERSION) {
|
|
849
|
+
return "another version";
|
|
850
|
+
}
|
|
851
|
+
if (!isId(request2.i)) {
|
|
852
|
+
return "no id";
|
|
853
|
+
}
|
|
854
|
+
if (typeof request2.t !== "number" || !Number.isFinite(request2.t)) {
|
|
855
|
+
return "no time";
|
|
856
|
+
}
|
|
857
|
+
if (typeof request2.m !== "string") {
|
|
858
|
+
return "no method";
|
|
859
|
+
}
|
|
860
|
+
if (request2.p !== undefined && (typeof request2.p !== "object" || request2.p === null || Array.isArray(request2.p))) {
|
|
861
|
+
return "params not an object";
|
|
862
|
+
}
|
|
863
|
+
return request2;
|
|
864
|
+
}
|
|
865
|
+
WatchProtocol.request = request;
|
|
866
|
+
function envelope(body) {
|
|
867
|
+
try {
|
|
868
|
+
const value = JSON.parse(body);
|
|
869
|
+
return isId(value?.d) && typeof value.s === "string" && value.s.length < 8 * 1024 * 1024 ? { d: value.d, s: value.s } : undefined;
|
|
870
|
+
} catch {
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
WatchProtocol.envelope = envelope;
|
|
875
|
+
})(WatchProtocol ||= {});
|
|
876
|
+
|
|
877
|
+
class WatchError extends Error {
|
|
878
|
+
code;
|
|
879
|
+
constructor(code, message) {
|
|
880
|
+
super(message);
|
|
881
|
+
this.code = code;
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
// web-src/watch-api.ts
|
|
886
|
+
var VOICE = "@celljs/ta-voice";
|
|
887
|
+
var CHATS_DAYS = 30;
|
|
888
|
+
var CHATS_MAX = 50;
|
|
889
|
+
var LINES_MAX = 30;
|
|
890
|
+
var LINE_CHARS = 4000;
|
|
891
|
+
var TEXT_CHARS = 8000;
|
|
892
|
+
var AUDIO_CHARS = Math.ceil(60 * 16000 * 2 * 4 / 3);
|
|
893
|
+
var one = (text, max) => text.length > max ? `${text.slice(0, max - 1)}…` : text;
|
|
894
|
+
var line = (text) => (text ?? "").replace(/\s+/g, " ").trim();
|
|
895
|
+
function string(params, key, max, optional = false) {
|
|
896
|
+
const value = params[key];
|
|
897
|
+
if (value === undefined && optional) {
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
if (typeof value !== "string" || !value.trim() || value.length > max) {
|
|
901
|
+
throw new WatchError("invalid", `${key} is a string of 1 to ${max} characters.`);
|
|
902
|
+
}
|
|
903
|
+
return value;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
class WatchApi {
|
|
907
|
+
host;
|
|
908
|
+
now;
|
|
909
|
+
constructor(host, now = Date.now) {
|
|
910
|
+
this.host = host;
|
|
911
|
+
this.now = now;
|
|
912
|
+
}
|
|
913
|
+
run(method, params, id, expiresAt) {
|
|
914
|
+
switch (method) {
|
|
915
|
+
case "hello":
|
|
916
|
+
return this.hello(expiresAt);
|
|
917
|
+
case "chats":
|
|
918
|
+
return this.chats(params);
|
|
919
|
+
case "chat":
|
|
920
|
+
return this.chat(params);
|
|
921
|
+
case "send":
|
|
922
|
+
return this.send(params, id);
|
|
923
|
+
case "stop":
|
|
924
|
+
return this.stop(params);
|
|
925
|
+
case "asks":
|
|
926
|
+
return this.asks();
|
|
927
|
+
case "answer":
|
|
928
|
+
return this.answer(params);
|
|
929
|
+
case "hear":
|
|
930
|
+
return this.hear(params, id);
|
|
931
|
+
default:
|
|
932
|
+
return Promise.reject(new WatchError("unknown_method", `There is no method ${JSON.stringify(method)}.`));
|
|
933
|
+
}
|
|
934
|
+
}
|
|
935
|
+
as(run) {
|
|
936
|
+
return this.host.team.actAs(TA_OWNER, run);
|
|
937
|
+
}
|
|
938
|
+
async hello(expiresAt) {
|
|
939
|
+
const [info, models, hearing] = await Promise.all([
|
|
940
|
+
this.host.getServerInfo(),
|
|
941
|
+
this.host.models?.list().catch(() => []) ?? Promise.resolve([]),
|
|
942
|
+
this.host.callPackageHost(VOICE, "hearing").catch(() => {
|
|
943
|
+
return;
|
|
944
|
+
})
|
|
945
|
+
]);
|
|
946
|
+
return { computer: info.host, expiresAt, models, hearing: hearing?.on === true && hearing.state === "ready" };
|
|
947
|
+
}
|
|
948
|
+
async chats(params) {
|
|
949
|
+
const limit = typeof params.limit === "number" && params.limit > 0 ? Math.min(Math.floor(params.limit), CHATS_MAX) : 20;
|
|
950
|
+
const [open, saved] = await Promise.all([this.host.listSessions(), this.host.listSavedSessions({ since: this.now() - CHATS_DAYS * 86400000 })]);
|
|
951
|
+
const chats = new Map;
|
|
952
|
+
const title = (one2) => line(one2.sessionName || one2.firstMessage || one2.cwd.split("/").pop() || one2.cwd);
|
|
953
|
+
for (const session of open) {
|
|
954
|
+
if (session.sessionFile && !session.group) {
|
|
955
|
+
chats.set(session.sessionFile, {
|
|
956
|
+
file: session.sessionFile,
|
|
957
|
+
title: one(title(session), 120),
|
|
958
|
+
last: one(line(session.lastMessage), 200),
|
|
959
|
+
at: session.updatedAt,
|
|
960
|
+
busy: session.isStreaming,
|
|
961
|
+
...session.asking ? { asking: one(line(session.asking.question), 200) } : {}
|
|
962
|
+
});
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
for (const session of saved) {
|
|
966
|
+
if (!session.group && !chats.has(session.sessionFile)) {
|
|
967
|
+
chats.set(session.sessionFile, {
|
|
968
|
+
file: session.sessionFile,
|
|
969
|
+
title: one(title(session), 120),
|
|
970
|
+
last: one(line(session.lastMessage), 200),
|
|
971
|
+
at: session.updatedAt,
|
|
972
|
+
busy: false,
|
|
973
|
+
...session.asking ? { asking: one(line(session.asking.question), 200) } : {}
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
return [...chats.values()].sort((a, b) => b.at - a.at).slice(0, limit);
|
|
978
|
+
}
|
|
979
|
+
static lines(messages, limit) {
|
|
980
|
+
const lines = [];
|
|
981
|
+
for (const raw of messages) {
|
|
982
|
+
const message = raw;
|
|
983
|
+
if (message.role !== "user" && message.role !== "assistant") {
|
|
984
|
+
continue;
|
|
985
|
+
}
|
|
986
|
+
const parts = typeof message.content === "string" ? [{ type: "text", text: message.content }] : Array.isArray(message.content) ? message.content : [];
|
|
987
|
+
const text = TaSaid.words(message.role, parts.filter((part) => part.type === "text" && typeof part.text === "string").map((part) => part.text).join(`
|
|
988
|
+
`));
|
|
989
|
+
if (text) {
|
|
990
|
+
lines.push({ role: message.role, text: one(text, LINE_CHARS) });
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
return lines.slice(-limit);
|
|
994
|
+
}
|
|
995
|
+
async chat(params) {
|
|
996
|
+
const file = string(params, "file", 4096);
|
|
997
|
+
const limit = typeof params.limit === "number" && params.limit > 0 ? Math.min(Math.floor(params.limit), LINES_MAX) : 12;
|
|
998
|
+
const open = (await this.host.listSessions()).find((session) => session.sessionFile === file);
|
|
999
|
+
const messages = await this.host.sessionMessages(file).catch(() => {
|
|
1000
|
+
throw new WatchError("not_found", "There is no such conversation.");
|
|
1001
|
+
});
|
|
1002
|
+
const asking = open?.asking?.question ?? (open ? undefined : (await this.host.listSavedSessions()).find((one2) => one2.sessionFile === file)?.asking?.question);
|
|
1003
|
+
return { busy: open?.isStreaming === true, ...asking ? { asking: one(line(asking), 400) } : {}, lines: WatchApi.lines(messages, limit) };
|
|
1004
|
+
}
|
|
1005
|
+
async send(params, id) {
|
|
1006
|
+
const text = string(params, "text", TEXT_CHARS);
|
|
1007
|
+
const file = string(params, "file", 4096, true);
|
|
1008
|
+
const asked = string(params, "model", 200, true);
|
|
1009
|
+
const sessions = await this.host.listSessions();
|
|
1010
|
+
let session = file ? sessions.find((one2) => one2.sessionFile === file) : undefined;
|
|
1011
|
+
if (!session) {
|
|
1012
|
+
const info = await this.host.getServerInfo();
|
|
1013
|
+
const model = asked && (await this.host.models?.list().catch(() => []))?.includes(asked) ? asked : undefined;
|
|
1014
|
+
const cwd = file ? await this.host.sessionCwd(file) ?? info.cwd : info.cwd;
|
|
1015
|
+
session = await this.as(() => this.host.openSession({ cwd, ...file ? { sessionFile: file } : {}, ...model ? { model } : {} }));
|
|
1016
|
+
}
|
|
1017
|
+
const sessionId = session.sessionId;
|
|
1018
|
+
await this.as(() => this.host.prompt(sessionId, { message: TaMessageSource.mark(text.trim(), NAME, WATCH_DETAIL), clientMessageId: `watch-${id}` }));
|
|
1019
|
+
const sent = session.sessionFile ?? (await this.host.listSessions()).find((one2) => one2.sessionId === sessionId)?.sessionFile;
|
|
1020
|
+
if (!sent) {
|
|
1021
|
+
throw new WatchError("failed", "The conversation has no file yet.");
|
|
1022
|
+
}
|
|
1023
|
+
return { file: sent };
|
|
1024
|
+
}
|
|
1025
|
+
async stop(params) {
|
|
1026
|
+
const file = string(params, "file", 4096);
|
|
1027
|
+
const session = (await this.host.listSessions()).find((one2) => one2.sessionFile === file);
|
|
1028
|
+
if (session?.isStreaming) {
|
|
1029
|
+
await this.as(() => this.host.abort(session.sessionId));
|
|
1030
|
+
}
|
|
1031
|
+
return {};
|
|
1032
|
+
}
|
|
1033
|
+
async asks() {
|
|
1034
|
+
const result = await this.host.commands.run("asks.list", {}, { principal: TA_OWNER });
|
|
1035
|
+
const found = Array.isArray(result.data) ? result.data : [];
|
|
1036
|
+
return found.map((ask) => ({
|
|
1037
|
+
file: ask.file,
|
|
1038
|
+
title: one(line(ask.name), 120),
|
|
1039
|
+
question: one(ask.question, 1000),
|
|
1040
|
+
kind: ask.kind === "dialog" ? "dialog" : "question",
|
|
1041
|
+
...Array.isArray(ask.choices) && ask.choices.length ? { choices: ask.choices.slice(0, 12) } : {}
|
|
1042
|
+
}));
|
|
1043
|
+
}
|
|
1044
|
+
async answer(params) {
|
|
1045
|
+
const file = string(params, "file", 4096);
|
|
1046
|
+
const answer = string(params, "answer", TEXT_CHARS);
|
|
1047
|
+
try {
|
|
1048
|
+
const result = await this.host.commands.run("asks.answer", { file, answer }, { principal: TA_OWNER });
|
|
1049
|
+
return { said: String(result.changes?.[0]?.after ?? answer) };
|
|
1050
|
+
} catch (error) {
|
|
1051
|
+
throw new WatchError("invalid", error instanceof Error ? error.message : String(error));
|
|
1052
|
+
}
|
|
1053
|
+
}
|
|
1054
|
+
async hear(params, id) {
|
|
1055
|
+
const audio = params.audio;
|
|
1056
|
+
if (typeof audio !== "string" || audio.length > AUDIO_CHARS || !/^[A-Za-z0-9+/]*={0,2}$/.test(audio)) {
|
|
1057
|
+
throw new WatchError("invalid", "audio is base64 of PCM16 mono at 16 kHz, one minute at most.");
|
|
1058
|
+
}
|
|
1059
|
+
const listening = WatchProtocol.isId(id) ? id : `w${Date.now()}`;
|
|
1060
|
+
try {
|
|
1061
|
+
const heard = await this.host.callPackageHost(VOICE, "hear", [listening.slice(0, 64), audio, "final"]);
|
|
1062
|
+
return { text: typeof heard?.text === "string" ? heard.text.trim() : "" };
|
|
1063
|
+
} catch (error) {
|
|
1064
|
+
throw new WatchError("unavailable", error instanceof Error ? error.message : String(error));
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
|
|
1069
|
+
// web-src/watch-devices.ts
|
|
1070
|
+
import { randomBytes } from "node:crypto";
|
|
1071
|
+
var DAY_MS = 24 * 60 * 60000;
|
|
1072
|
+
var MAX_DEVICES = 8;
|
|
1073
|
+
var USED_MS = 10 * 60000;
|
|
1074
|
+
|
|
1075
|
+
class WatchDevices {
|
|
1076
|
+
host;
|
|
1077
|
+
now;
|
|
1078
|
+
list = [];
|
|
1079
|
+
keys = new Map;
|
|
1080
|
+
writing = Promise.resolve();
|
|
1081
|
+
constructor(host, now = Date.now) {
|
|
1082
|
+
this.host = host;
|
|
1083
|
+
this.now = now;
|
|
1084
|
+
}
|
|
1085
|
+
static credentialId(id) {
|
|
1086
|
+
return `watch:${id}`;
|
|
1087
|
+
}
|
|
1088
|
+
async load() {
|
|
1089
|
+
const kept = await this.host.getExtensionData(NAME, DEVICES_KEY).catch(() => {
|
|
1090
|
+
return;
|
|
1091
|
+
});
|
|
1092
|
+
this.list = Array.isArray(kept) ? kept.filter(WatchDevices.valid) : [];
|
|
1093
|
+
}
|
|
1094
|
+
static valid(value) {
|
|
1095
|
+
const device = value;
|
|
1096
|
+
return !!device && typeof device.id === "string" && typeof device.name === "string" && typeof device.createdAt === "number" && typeof device.expiresAt === "number";
|
|
1097
|
+
}
|
|
1098
|
+
all() {
|
|
1099
|
+
return this.list.map((device) => ({ ...device }));
|
|
1100
|
+
}
|
|
1101
|
+
async pair(name) {
|
|
1102
|
+
if (!this.host.credentials.available()) {
|
|
1103
|
+
throw new Error("The vault of Ta is off: turn on ta-secrets to pair a watch.");
|
|
1104
|
+
}
|
|
1105
|
+
const now = this.now();
|
|
1106
|
+
const device = {
|
|
1107
|
+
id: randomBytes(16).toString("base64url"),
|
|
1108
|
+
name: name.trim().slice(0, 60) || "Apple Watch",
|
|
1109
|
+
createdAt: now,
|
|
1110
|
+
expiresAt: now + WATCH_DAYS * DAY_MS
|
|
1111
|
+
};
|
|
1112
|
+
const key = randomBytes(32);
|
|
1113
|
+
await this.host.credentials.set(WatchDevices.credentialId(device.id), { password: key.toString("base64") }, { feature: "relay", name: `Apple Watch · ${device.name}`, local: true });
|
|
1114
|
+
this.keys.set(device.id, key);
|
|
1115
|
+
const gone = this.list.filter((one2) => one2.name === device.name);
|
|
1116
|
+
const rest = this.list.filter((one2) => one2.name !== device.name).sort((a, b) => (b.usedAt ?? b.createdAt) - (a.usedAt ?? a.createdAt));
|
|
1117
|
+
gone.push(...rest.splice(MAX_DEVICES - 1));
|
|
1118
|
+
this.list = [device, ...rest];
|
|
1119
|
+
await Promise.all(gone.map((one2) => this.forget(one2.id)));
|
|
1120
|
+
await this.save();
|
|
1121
|
+
return { device: { ...device }, key };
|
|
1122
|
+
}
|
|
1123
|
+
async renew(id) {
|
|
1124
|
+
const device = this.list.find((one2) => one2.id === id);
|
|
1125
|
+
if (!device) {
|
|
1126
|
+
return;
|
|
1127
|
+
}
|
|
1128
|
+
device.expiresAt = this.now() + WATCH_DAYS * DAY_MS;
|
|
1129
|
+
await this.save();
|
|
1130
|
+
return { ...device };
|
|
1131
|
+
}
|
|
1132
|
+
async remove(id) {
|
|
1133
|
+
if (!this.list.some((one2) => one2.id === id)) {
|
|
1134
|
+
return false;
|
|
1135
|
+
}
|
|
1136
|
+
this.list = this.list.filter((one2) => one2.id !== id);
|
|
1137
|
+
await this.forget(id);
|
|
1138
|
+
await this.save();
|
|
1139
|
+
return true;
|
|
1140
|
+
}
|
|
1141
|
+
async find(id) {
|
|
1142
|
+
const device = this.list.find((one2) => one2.id === id);
|
|
1143
|
+
if (!device) {
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
let key = this.keys.get(id);
|
|
1147
|
+
if (!key) {
|
|
1148
|
+
const kept = await this.host.credentials.get(WatchDevices.credentialId(id), "a request of the watch").catch(() => {
|
|
1149
|
+
return;
|
|
1150
|
+
});
|
|
1151
|
+
const bytes = kept ? Buffer.from(kept.password, "base64") : undefined;
|
|
1152
|
+
if (bytes?.length !== 32) {
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
key = bytes;
|
|
1156
|
+
this.keys.set(id, key);
|
|
1157
|
+
}
|
|
1158
|
+
return { device: { ...device }, key };
|
|
1159
|
+
}
|
|
1160
|
+
used(id) {
|
|
1161
|
+
const device = this.list.find((one2) => one2.id === id);
|
|
1162
|
+
const now = this.now();
|
|
1163
|
+
if (device && now - (device.usedAt ?? 0) >= USED_MS) {
|
|
1164
|
+
device.usedAt = now;
|
|
1165
|
+
this.save();
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
async forget(id) {
|
|
1169
|
+
this.keys.delete(id);
|
|
1170
|
+
await this.host.credentials.remove(WatchDevices.credentialId(id)).catch(() => {
|
|
1171
|
+
return;
|
|
1172
|
+
});
|
|
1173
|
+
}
|
|
1174
|
+
save() {
|
|
1175
|
+
const value = this.all();
|
|
1176
|
+
this.writing = this.writing.then(() => this.host.setExtensionData(NAME, DEVICES_KEY, value)).catch(() => {
|
|
1177
|
+
return;
|
|
1178
|
+
});
|
|
1179
|
+
return this.writing;
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
// ../ta-protocol/esm/common/http-protocol.js
|
|
1184
|
+
var TA_WEBHOOK_MAX_BYTES = 256 * 1024;
|
|
1185
|
+
var TA_RELAY_HTTP_PATH = "/a/";
|
|
1186
|
+
var TA_HTTP_MAX_BYTES = 4 * 1024 * 1024;
|
|
1187
|
+
var TaHttpUtil;
|
|
1188
|
+
(function(TaHttpUtil2) {
|
|
1189
|
+
function base(relay, host, packageName) {
|
|
1190
|
+
return `${relay.replace(/\/+$/, "")}${TA_RELAY_HTTP_PATH}${host}/${packageName.slice(packageName.lastIndexOf("/") + 1)}`;
|
|
1191
|
+
}
|
|
1192
|
+
TaHttpUtil2.base = base;
|
|
1193
|
+
function json(status, value, headers = {}) {
|
|
1194
|
+
return { status, headers: { "content-type": "application/json; charset=utf-8", ...headers }, body: JSON.stringify(value) };
|
|
1195
|
+
}
|
|
1196
|
+
TaHttpUtil2.json = json;
|
|
1197
|
+
})(TaHttpUtil || (TaHttpUtil = {}));
|
|
1198
|
+
|
|
1199
|
+
// web-src/watch-seal.ts
|
|
1200
|
+
import { createCipheriv, createDecipheriv, hkdfSync, randomBytes as randomBytes2 } from "node:crypto";
|
|
1201
|
+
var NONCE = 12;
|
|
1202
|
+
var TAG = 16;
|
|
1203
|
+
var WatchSeal;
|
|
1204
|
+
((WatchSeal) => {
|
|
1205
|
+
function key(deviceKey, device, direction) {
|
|
1206
|
+
return Buffer.from(hkdfSync("sha256", deviceKey, Buffer.from(device, "utf8"), Buffer.from(`ta-watch/1 ${direction}`, "utf8"), 32));
|
|
1207
|
+
}
|
|
1208
|
+
WatchSeal.key = key;
|
|
1209
|
+
function aad(device) {
|
|
1210
|
+
return Buffer.from(`ta-watch/1 ${device}`, "utf8");
|
|
1211
|
+
}
|
|
1212
|
+
function seal(deviceKey, device, direction, plain, nonce = randomBytes2(NONCE)) {
|
|
1213
|
+
const cipher = createCipheriv("aes-256-gcm", key(deviceKey, device, direction), nonce);
|
|
1214
|
+
cipher.setAAD(aad(device));
|
|
1215
|
+
const sealed = Buffer.concat([cipher.update(plain, "utf8"), cipher.final()]);
|
|
1216
|
+
return Buffer.concat([nonce, sealed, cipher.getAuthTag()]).toString("base64");
|
|
1217
|
+
}
|
|
1218
|
+
WatchSeal.seal = seal;
|
|
1219
|
+
function open(deviceKey, device, direction, sealed) {
|
|
1220
|
+
const bytes = Buffer.from(sealed, "base64");
|
|
1221
|
+
if (bytes.length < NONCE + TAG) {
|
|
1222
|
+
return;
|
|
1223
|
+
}
|
|
1224
|
+
try {
|
|
1225
|
+
const decipher = createDecipheriv("aes-256-gcm", key(deviceKey, device, direction), bytes.subarray(0, NONCE));
|
|
1226
|
+
decipher.setAAD(aad(device));
|
|
1227
|
+
decipher.setAuthTag(bytes.subarray(bytes.length - TAG));
|
|
1228
|
+
return Buffer.concat([decipher.update(bytes.subarray(NONCE, bytes.length - TAG)), decipher.final()]).toString("utf8");
|
|
1229
|
+
} catch {
|
|
1230
|
+
return;
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
WatchSeal.open = open;
|
|
1234
|
+
})(WatchSeal ||= {});
|
|
1235
|
+
|
|
1236
|
+
// web-src/watch-http.ts
|
|
1237
|
+
var NOT_FOUND = TaHttpUtil.json(404, { error: "not_found" });
|
|
1238
|
+
var EXPIRED = TaHttpUtil.json(401, { error: "expired" });
|
|
1239
|
+
|
|
1240
|
+
class WatchHttp {
|
|
1241
|
+
devices;
|
|
1242
|
+
api;
|
|
1243
|
+
now;
|
|
1244
|
+
seen = new Map;
|
|
1245
|
+
constructor(devices, api, now = Date.now) {
|
|
1246
|
+
this.devices = devices;
|
|
1247
|
+
this.api = api;
|
|
1248
|
+
this.now = now;
|
|
1249
|
+
}
|
|
1250
|
+
async answer(request) {
|
|
1251
|
+
if (request.method !== "POST" || request.path !== WATCH_PATH) {
|
|
1252
|
+
return NOT_FOUND;
|
|
1253
|
+
}
|
|
1254
|
+
const envelope = WatchProtocol.envelope(request.body);
|
|
1255
|
+
const found = envelope && await this.devices.find(envelope.d);
|
|
1256
|
+
const plain = found && WatchSeal.open(found.key, found.device.id, "request", envelope.s);
|
|
1257
|
+
if (!found || plain === undefined) {
|
|
1258
|
+
return NOT_FOUND;
|
|
1259
|
+
}
|
|
1260
|
+
let parsed;
|
|
1261
|
+
try {
|
|
1262
|
+
parsed = JSON.parse(plain);
|
|
1263
|
+
} catch {
|
|
1264
|
+
return NOT_FOUND;
|
|
1265
|
+
}
|
|
1266
|
+
const call = WatchProtocol.request(parsed);
|
|
1267
|
+
const now = this.now();
|
|
1268
|
+
if (typeof call === "string") {
|
|
1269
|
+
return NOT_FOUND;
|
|
1270
|
+
}
|
|
1271
|
+
if (found.device.expiresAt <= now) {
|
|
1272
|
+
return EXPIRED;
|
|
1273
|
+
}
|
|
1274
|
+
this.sweep(now);
|
|
1275
|
+
if (this.seen.has(`${found.device.id} ${call.i}`)) {
|
|
1276
|
+
return NOT_FOUND;
|
|
1277
|
+
}
|
|
1278
|
+
this.seen.set(`${found.device.id} ${call.i}`, now);
|
|
1279
|
+
let reply;
|
|
1280
|
+
if (Math.abs(now - call.t) > WATCH_SKEW_MS) {
|
|
1281
|
+
reply = { i: call.i, ok: false, e: { code: "invalid", message: "The time of the watch is off by more than five minutes." } };
|
|
1282
|
+
} else {
|
|
1283
|
+
this.devices.used(found.device.id);
|
|
1284
|
+
reply = await this.api.run(call.m, call.p ?? {}, call.i, found.device.expiresAt).then((r) => ({ i: call.i, ok: true, r: r ?? null }), (error) => ({
|
|
1285
|
+
i: call.i,
|
|
1286
|
+
ok: false,
|
|
1287
|
+
e: error instanceof WatchError ? { code: error.code, message: error.message } : { code: "failed", message: error instanceof Error ? error.message : String(error) }
|
|
1288
|
+
}));
|
|
1289
|
+
}
|
|
1290
|
+
return TaHttpUtil.json(200, { s: WatchSeal.seal(found.key, found.device.id, "response", JSON.stringify(reply)) }, { "cache-control": "no-store" });
|
|
1291
|
+
}
|
|
1292
|
+
sweep(now) {
|
|
1293
|
+
for (const [id, at] of this.seen) {
|
|
1294
|
+
if (now - at > WATCH_SEEN_MS) {
|
|
1295
|
+
this.seen.delete(id);
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
// web-src/host.ts
|
|
1302
|
+
async function activate(host) {
|
|
1303
|
+
const devices = new WatchDevices(host);
|
|
1304
|
+
await devices.load();
|
|
1305
|
+
const http = new WatchHttp(devices, new WatchApi(host));
|
|
1306
|
+
const owner = () => {
|
|
1307
|
+
if (host.team.current() !== TA_OWNER) {
|
|
1308
|
+
throw new Error("Only the owner of this Ta pairs a watch.");
|
|
1309
|
+
}
|
|
1310
|
+
};
|
|
1311
|
+
const changed = () => host.changed?.("devices");
|
|
1312
|
+
return {
|
|
1313
|
+
http: (request) => http.answer(request),
|
|
1314
|
+
pair: async (name) => {
|
|
1315
|
+
owner();
|
|
1316
|
+
const routes = await host.relays.routes?.();
|
|
1317
|
+
if (!routes?.relays.length) {
|
|
1318
|
+
throw new Error("This computer is not on a relay yet: the watch reaches it only through one.");
|
|
1319
|
+
}
|
|
1320
|
+
const [{ device, key }, info] = await Promise.all([devices.pair(typeof name === "string" ? name : ""), host.getServerInfo()]);
|
|
1321
|
+
changed();
|
|
1322
|
+
return {
|
|
1323
|
+
v: WATCH_VERSION,
|
|
1324
|
+
host: routes.host,
|
|
1325
|
+
relays: routes.relays,
|
|
1326
|
+
device: device.id,
|
|
1327
|
+
key: key.toString("base64"),
|
|
1328
|
+
computer: info.host,
|
|
1329
|
+
expiresAt: device.expiresAt
|
|
1330
|
+
};
|
|
1331
|
+
},
|
|
1332
|
+
renew: async (device) => {
|
|
1333
|
+
owner();
|
|
1334
|
+
const renewed = typeof device === "string" ? await devices.renew(device) : undefined;
|
|
1335
|
+
changed();
|
|
1336
|
+
return renewed;
|
|
1337
|
+
},
|
|
1338
|
+
devices: async () => {
|
|
1339
|
+
owner();
|
|
1340
|
+
return devices.all();
|
|
1341
|
+
},
|
|
1342
|
+
remove: async (device) => {
|
|
1343
|
+
owner();
|
|
1344
|
+
const removed = typeof device === "string" && await devices.remove(device);
|
|
1345
|
+
changed();
|
|
1346
|
+
return removed;
|
|
1347
|
+
}
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1350
|
+
export {
|
|
1351
|
+
activate as default
|
|
1352
|
+
};
|