@celljs/ta-bots 4.10.0 → 4.11.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.
@@ -1,287 +1,24 @@
1
- // ../ta-protocol/esm/common/access-protocol.js
2
- var TaAccessServer = Symbol("TaAccessServer");
3
- // ../ta-protocol/esm/common/auth-protocol.js
4
- var TaAuthServer = Symbol("TaAuthServer");
5
- // ../ta-protocol/esm/common/gateway-protocol.js
6
- var TaGatewayServer = Symbol("TaGatewayServer");
7
- // ../ta-protocol/esm/common/location.js
8
- var TaLocation;
9
- (function(TaLocation2) {
10
- TaLocation2.SSH = "ssh://";
11
- const HOST = /^(?!-)[\w.@:%+[\]-]+$/;
12
- function isRemote(location) {
13
- return location.startsWith(TaLocation2.SSH);
14
- }
15
- TaLocation2.isRemote = isRemote;
16
- function parse(location) {
17
- if (!isRemote(location)) {
18
- return;
19
- }
20
- const rest = location.slice(TaLocation2.SSH.length);
21
- const slash = rest.indexOf("/");
22
- const host2 = slash < 0 ? rest : rest.slice(0, slash);
23
- const path2 = slash < 0 ? "" : normalize(rest.slice(slash));
24
- if (!isHost(host2)) {
25
- throw new Error(`${JSON.stringify(location)} names no host: write ssh://<host>/<path>, the host as ssh takes it.`);
26
- }
27
- return { host: host2, path: path2 };
28
- }
29
- TaLocation2.parse = parse;
30
- function isHost(host2) {
31
- return HOST.test(host2);
32
- }
33
- TaLocation2.isHost = isHost;
34
- const FLAGS = /^-[46AaCfGgKkMNnqsTtVvXxYy]+$/;
35
- function readHost(input) {
36
- const words = input.trim().split(/\s+/).filter(Boolean);
37
- if (words[0] === "ssh") {
38
- words.shift();
39
- }
40
- let destination;
41
- let port;
42
- let user;
43
- const options = [];
44
- for (let i = 0;i < words.length; i++) {
45
- const word = words[i];
46
- const glued = /^-([pl])(.+)$/.exec(word);
47
- if (word === "-p" || word === "-l" || glued) {
48
- const value = glued ? glued[2] : words[++i];
49
- if (value === undefined) {
50
- return { error: "invalid" };
51
- }
52
- if ((glued ? glued[1] : word[1]) === "p") {
53
- port = value;
54
- } else {
55
- user = value;
56
- }
57
- } else if (word === "-o" || word.startsWith("-o")) {
58
- const value = word === "-o" ? words[++i] : word.slice(2);
59
- const option = /^(port|user)\s*=?\s*(.+)$/i.exec(value !== null && value !== undefined ? value : "");
60
- if (!option) {
61
- options.push(`-o ${value !== null && value !== undefined ? value : ""}`.trim());
62
- } else if (option[1].toLowerCase() === "port") {
63
- port = option[2];
64
- } else {
65
- user = option[2];
66
- }
67
- } else if (FLAGS.test(word)) {} else if (word.startsWith("-")) {
68
- options.push(/^-[bBcDeEFiIJLmOQRSwW]$/.test(word) && words[i + 1] !== undefined ? `${word} ${words[++i]}` : word);
69
- } else if (destination === undefined) {
70
- destination = word;
71
- } else {
72
- return { error: "invalid" };
73
- }
74
- }
75
- if (options.length > 0) {
76
- return { error: "options", options };
77
- }
78
- if (!destination) {
79
- return { error: "empty" };
80
- }
81
- let host2 = destination.replace(/^ssh:\/\//, "").replace(/\/.*$/, "");
82
- if (user && !host2.includes("@")) {
83
- host2 = `${user}@${host2}`;
84
- }
85
- if (port) {
86
- if (!/^\d{1,5}$/.test(port) || /:\d+$/.test(host2)) {
87
- return { error: "invalid" };
88
- }
89
- host2 = port === "22" ? host2 : `${host2}:${port}`;
90
- }
91
- return isHost(host2) ? { host: host2 } : { error: "invalid" };
92
- }
93
- TaLocation2.readHost = readHost;
94
- function ssh(host2, path2 = "") {
95
- return `${TaLocation2.SSH}${host2}${path2 ? normalize(path2) : ""}`;
96
- }
97
- TaLocation2.ssh = ssh;
98
- function hostLabel(host2) {
99
- const name = host2.replace(/^[^@]*@/, "").replace(/^\[(.*)\](?::\d+)?$/, "$1").replace(/^([^:]*):\d+$/, "$1");
100
- return /^[\d.]+$/.test(name) || name.includes(":") ? name : name.split(".")[0] || name;
101
- }
102
- TaLocation2.hostLabel = hostLabel;
103
- function host(location) {
104
- var _a;
105
- return isRemote(location) ? (_a = parse(location)) === null || _a === undefined ? undefined : _a.host : undefined;
106
- }
107
- TaLocation2.host = host;
108
- function path(location) {
109
- var _a;
110
- var _b;
111
- return (_b = (_a = parse(location)) === null || _a === undefined ? undefined : _a.path) !== null && _b !== undefined ? _b : location;
112
- }
113
- TaLocation2.path = path;
114
- function parent(location) {
115
- const remote = parse(location);
116
- if (remote) {
117
- const above = remote.path ? parentPath(remote.path) : undefined;
118
- return above === undefined ? undefined : ssh(remote.host, above);
119
- }
120
- return parentPath(location);
121
- }
122
- TaLocation2.parent = parent;
123
- function child(location, name) {
124
- const remote = parse(location);
125
- if (remote) {
126
- return ssh(remote.host, childPath(remote.path || "/", name));
127
- }
128
- return childPath(location, name);
129
- }
130
- TaLocation2.child = child;
131
- function basename(location) {
132
- const remote = parse(location);
133
- const path2 = (remote ? remote.path : location).replace(/[\\/]+$/, "");
134
- const name = path2.slice(Math.max(path2.lastIndexOf("/"), path2.lastIndexOf("\\")) + 1);
135
- return name || (remote === null || remote === undefined ? undefined : remote.host) || location;
136
- }
137
- TaLocation2.basename = basename;
138
- function homeRelative(location) {
139
- var _a;
140
- const remote = parse(location);
141
- const path2 = remote ? remote.path ? remote.path.slice(1) : "~" : location;
142
- const match = /^~(?:[\\/]+(.*))?$/.exec(path2);
143
- return match ? ((_a = match[1]) !== null && _a !== undefined ? _a : "").replace(/[\\/]+$/, "") : undefined;
144
- }
145
- TaLocation2.homeRelative = homeRelative;
146
- function isAbsolute(location) {
147
- if (homeRelative(location) !== undefined) {
148
- return false;
149
- }
150
- return isRemote(location) || /^([\\/]|[A-Za-z]:)/.test(location);
151
- }
152
- TaLocation2.isAbsolute = isAbsolute;
153
- function parentPath(path2) {
154
- const trimmed = path2.replace(/[\\/]+$/, "");
155
- const at = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
156
- if (at < 0) {
157
- return;
158
- }
159
- const head = trimmed.slice(0, at);
160
- return head === "" || /^[A-Za-z]:$/.test(head) ? head + trimmed[at] : head;
161
- }
162
- function childPath(path2, name) {
163
- const separator = path2.includes("/") || !path2.includes("\\") ? "/" : "\\";
164
- return path2.endsWith(separator) ? path2 + name : path2 + separator + name;
165
- }
166
- function normalize(path2) {
167
- const squeezed = `/${path2}`.replace(/\/+/g, "/");
168
- return squeezed.length > 1 ? squeezed.replace(/\/$/, "") : squeezed;
169
- }
170
- })(TaLocation || (TaLocation = {}));
171
- // ../ta-protocol/esm/common/package-protocol.js
172
- var TaPackageServer = Symbol("TaPackageServer");
173
- var TaPackageTextUtil;
174
- (function(TaPackageTextUtil2) {
175
- function pick(text, language) {
176
- var _a, _b, _c, _d;
177
- if (text === undefined || typeof text === "string") {
178
- return text;
179
- }
180
- return (_d = (_c = (_b = (_a = text[language]) !== null && _a !== undefined ? _a : text[language.split("-")[0]]) !== null && _b !== undefined ? _b : text.en) !== null && _c !== undefined ? _c : text["en-US"]) !== null && _d !== undefined ? _d : Object.values(text)[0];
181
- }
182
- TaPackageTextUtil2.pick = pick;
183
- })(TaPackageTextUtil || (TaPackageTextUtil = {}));
184
- // ../ta-protocol/esm/common/relay-protocol.js
185
- var TaRelayServer = Symbol("TaRelayServer");
186
- // ../ta-protocol/esm/common/remote-protocol.js
187
- var TaRemoteServer = Symbol("TaRemoteServer");
188
- // ../ta-protocol/esm/common/settings-protocol.js
189
- var TaSettingsServer = Symbol("TaSettingsServer");
190
1
  // ../ta-protocol/esm/common/ta-extension-host.js
191
2
  var TA_HOST_EVENT = "ta:host";
192
- // ../ta-protocol/esm/common/ta-protocol.js
193
- var TaServer = Symbol("TaServer");
194
- var TaClient = Symbol("TaClient");
195
- var TaErrorUtil;
196
- (function(TaErrorUtil2) {
197
- function code(error) {
198
- var _a;
199
- var _b;
200
- const failure = error;
201
- const code2 = (_b = failure === null || failure === undefined ? undefined : failure.code) !== null && _b !== undefined ? _b : (_a = failure === null || failure === undefined ? undefined : failure.data) === null || _a === undefined ? undefined : _a.code;
202
- return typeof code2 === "string" ? code2 : undefined;
203
- }
204
- TaErrorUtil2.code = code;
205
- })(TaErrorUtil || (TaErrorUtil = {}));
206
- var TaMessageContext;
207
- (function(TaMessageContext2) {
208
- const BLOCK = /^<context>\n?[\s\S]*?\n?<\/context>\n?/;
209
- function wrap(context, message) {
210
- return `<context>
211
- ${context}
212
- </context>
213
- ${message}`;
214
- }
215
- TaMessageContext2.wrap = wrap;
216
- function strip(message) {
217
- return message.replace(BLOCK, "");
218
- }
219
- TaMessageContext2.strip = strip;
220
- })(TaMessageContext || (TaMessageContext = {}));
221
- var TaImageNote;
222
- (function(TaImageNote2) {
223
- 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 [^\]]+\.\]$/;
224
- function strip(text) {
225
- const lines = text.split(`
226
- `);
227
- const kept = lines.filter((line) => !NOTE.test(line.trim()));
228
- if (kept.length === lines.length) {
229
- return text;
230
- }
231
- return kept.join(`
232
- `).replace(/\n+$/, "").replace(/^\n+/, "");
233
- }
234
- TaImageNote2.strip = strip;
235
- })(TaImageNote || (TaImageNote = {}));
236
- var TaAgentSummary;
237
- (function(TaAgentSummary2) {
238
- const START = /^\s*<conversation_history_summary>/;
239
- const BLOCK = /^\s*<conversation_history_summary>[\s\S]*?(?:<\/conversation_history_summary>\s*|$)/;
240
- function read(text) {
241
- if (!START.test(text)) {
242
- return;
243
- }
244
- return text.replace(/<\/?(?:conversation_history_summary|summary)>/g, "").replace(/<\/?\w*$/, "").trim();
245
- }
246
- TaAgentSummary2.read = read;
247
- function strip(text) {
248
- return text.replace(BLOCK, "");
249
- }
250
- TaAgentSummary2.strip = strip;
251
- })(TaAgentSummary || (TaAgentSummary = {}));
252
- var TaSkillUse;
253
- (function(TaSkillUse2) {
254
- const BLOCK = /^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/;
255
- function parse(message) {
256
- var _a;
257
- const match = BLOCK.exec(message);
258
- if (!match) {
259
- return;
260
- }
261
- return { name: match[1], location: match[2], content: match[3], message: ((_a = match[4]) === null || _a === undefined ? undefined : _a.trim()) || undefined };
262
- }
263
- TaSkillUse2.parse = parse;
264
- function command(use) {
265
- return use.message ? `/skill:${use.name} ${use.message}` : `/skill:${use.name}`;
266
- }
267
- TaSkillUse2.command = command;
268
- })(TaSkillUse || (TaSkillUse = {}));
269
- // ../ta-protocol/esm/common/workspace-protocol.js
270
- var TaWorkspaceServer = Symbol("TaWorkspaceServer");
271
- var TaTerminalServer = Symbol("TaTerminalServer");
272
- var TaExcludedFiles;
273
- (function(TaExcludedFiles2) {
274
- TaExcludedFiles2.NAMES = [".git", ".svn", ".hg", ".DS_Store", "Thumbs.db"];
275
- function has(name) {
276
- return TaExcludedFiles2.NAMES.includes(name);
277
- }
278
- TaExcludedFiles2.has = has;
279
- function filter(entries) {
280
- return entries.filter((entry) => !has(entry.name));
3
+ var TaHostUtil;
4
+ (function(TaHostUtil2) {
5
+ TaHostUtil2.WAIT_MS = 1000;
6
+ function reach(events, name, waitMs = TaHostUtil2.WAIT_MS) {
7
+ return new Promise((resolve) => {
8
+ const timer = setTimeout(() => resolve(undefined), waitMs);
9
+ const request = {
10
+ name,
11
+ resolve: (answer) => {
12
+ clearTimeout(timer);
13
+ resolve(answer);
14
+ }
15
+ };
16
+ events.emit(TA_HOST_EVENT, request);
17
+ });
281
18
  }
282
- TaExcludedFiles2.filter = filter;
283
- })(TaExcludedFiles || (TaExcludedFiles = {}));
284
- var TA_UPLOAD_PART_BYTES = 4 * 1024 * 1024;
19
+ TaHostUtil2.reach = reach;
20
+ })(TaHostUtil || (TaHostUtil = {}));
21
+
285
22
  // web-src/bots-protocol.ts
286
23
  var NAME = "@celljs/ta-bots";
287
24
  var Keys;
@@ -291,6 +28,7 @@ var Keys;
291
28
  })(Keys ||= {});
292
29
  var INBOUND_TYPE = "ta-bots";
293
30
  var SENT_TYPE = "ta-bots-sent";
31
+ var SEND_FILE_TOOL = "bot_send_file";
294
32
  var BotTargetUtil;
295
33
  ((BotTargetUtil) => {
296
34
  function same(a, b) {
@@ -420,13 +158,19 @@ var BotText;
420
158
  function plain(markdown) {
421
159
  const lines = [];
422
160
  let fenced = false;
161
+ let card = false;
423
162
  for (const line of markdown.replace(/\r\n/g, `
424
163
  `).split(`
425
164
  `)) {
426
- if (/^\s*(```|~~~)/.test(line)) {
165
+ const fence = /^\s*(?:```|~~~)\s*([\w-]*)/.exec(line);
166
+ if (fence) {
167
+ card = !fenced && fence[1].toLowerCase().startsWith("ta-");
427
168
  fenced = !fenced;
428
169
  continue;
429
170
  }
171
+ if (card) {
172
+ continue;
173
+ }
430
174
  if (fenced) {
431
175
  lines.push(line);
432
176
  continue;
@@ -483,12 +227,12 @@ var BotText;
483
227
  })(BotText ||= {});
484
228
 
485
229
  // web-src/extension.ts
486
- var HOST_WAIT_MS = 1000;
487
230
  var INBOX = ".ta/inbox";
488
231
  var IMAGE_BYTES = 5 * 1024 * 1024;
489
232
  var MODEL_IMAGES = new Set(["image/png", "image/jpeg", "image/gif", "image/webp"]);
490
- var TOOL = "bot_send_file";
233
+ var TOOL = SEND_FILE_TOOL;
491
234
  var KIND = { weixin: "WeChat", wecom: "WeCom" };
235
+ var SECTION = "ta-bots";
492
236
  function inboundText(details) {
493
237
  const who = details.from.name || details.from.id;
494
238
  const head = `[${KIND[details.kind]} · ${details.botName}${details.group ? " · group" : ""}] ${who}:`;
@@ -497,9 +241,7 @@ function inboundText(details) {
497
241
  `);
498
242
  }
499
243
  function botNote(details) {
500
- return `
501
-
502
- This message came from ${KIND[details.kind]} through the bot "${details.botName}" of Ta. The person reads your final answer there, as ${details.kind === "weixin" ? "plain text without Markdown" : "Markdown"}, on a phone: answer in a few short paragraphs, and say what you did rather than showing long output. Files they sent are saved under ${INBOX}/. To send them a file, a picture or a video, call ${TOOL} with its path.`;
244
+ return `This message came from ${KIND[details.kind]} through the bot "${details.botName}" of Ta. The person reads your final answer there, as ${details.kind === "weixin" ? "plain text without Markdown" : "Markdown"}, on a phone: answer in a few short paragraphs, and say what you did rather than showing long output; write no fenced block of a ta- language (a card of Ta's page, such as ta-glance), which the chat cannot draw. Files they sent are saved under ${INBOX}/. To send them a file, a picture or a video, call ${TOOL} with its path.`;
503
245
  }
504
246
  function activate(pi) {
505
247
  let reached;
@@ -511,16 +253,7 @@ function activate(pi) {
511
253
  let sessionFile = () => {
512
254
  return;
513
255
  };
514
- const reach = () => reached ??= new Promise((resolve) => {
515
- const timer = setTimeout(() => resolve(undefined), HOST_WAIT_MS);
516
- pi.events.emit(TA_HOST_EVENT, {
517
- name: NAME,
518
- resolve: (found) => {
519
- clearTimeout(timer);
520
- resolve(found.service && found.files ? { service: found.service, files: found.files } : undefined);
521
- }
522
- });
523
- });
256
+ const reach = () => reached ??= TaHostUtil.reach(pi.events, NAME).then((found) => found?.service ? { service: found.service, files: found.files } : undefined);
524
257
  pi.on("session_start", async (_event, ctx) => {
525
258
  const found = await reach();
526
259
  if (!found) {
@@ -544,9 +277,8 @@ function activate(pi) {
544
277
  }
545
278
  }
546
279
  if (event.messageType === "custom" && event.message?.customType === INBOUND_TYPE) {
547
- return { systemPrompt: event.systemPrompt + botNote(event.message.details) };
280
+ event.systemPromptOptions.sections = { ...event.systemPromptOptions.sections, [SECTION]: botNote(event.message.details) };
548
281
  }
549
- return;
550
282
  });
551
283
  pi.on("message_end", (event) => {
552
284
  const message = event.message;
package/host/index.mjs CHANGED
@@ -385,9 +385,64 @@ var TaPackageTextUtil;
385
385
  var TaRelayServer = Symbol("TaRelayServer");
386
386
  // ../ta-protocol/esm/common/remote-protocol.js
387
387
  var TaRemoteServer = Symbol("TaRemoteServer");
388
+ // ../ta-protocol/esm/common/room-protocol.js
389
+ var TaRoomServer = Symbol("TaRoomServer");
390
+ var TaRoomWatchUtil;
391
+ (function(TaRoomWatchUtil2) {
392
+ function of(messages, state) {
393
+ const prefixes = Array.isArray(state) ? state.filter((prefix) => typeof prefix === "string" && prefix.length > 0) : [];
394
+ return { messages: messages !== false, state: [...new Set(prefixes)].sort() };
395
+ }
396
+ TaRoomWatchUtil2.of = of;
397
+ function union(watches) {
398
+ let messages = false;
399
+ const state = [];
400
+ for (const watch of watches) {
401
+ messages || (messages = watch.messages);
402
+ state.push(...watch.state);
403
+ }
404
+ return of(messages, messages ? [] : state);
405
+ }
406
+ TaRoomWatchUtil2.union = union;
407
+ function same(a, b) {
408
+ return a === b || !!a && !!b && a.messages === b.messages && a.state.join(`
409
+ `) === b.state.join(`
410
+ `);
411
+ }
412
+ TaRoomWatchUtil2.same = same;
413
+ function hears(watch, event) {
414
+ if (event.type === "message") {
415
+ return watch.messages;
416
+ }
417
+ if (event.type === "state") {
418
+ return watch.messages || watch.state.some((prefix) => event.key.startsWith(prefix));
419
+ }
420
+ return true;
421
+ }
422
+ TaRoomWatchUtil2.hears = hears;
423
+ })(TaRoomWatchUtil || (TaRoomWatchUtil = {}));
388
424
  // ../ta-protocol/esm/common/settings-protocol.js
389
425
  var TaSettingsServer = Symbol("TaSettingsServer");
390
426
  // ../ta-protocol/esm/common/ta-extension-host.js
427
+ var TA_HOST_EVENT = "ta:host";
428
+ var TaHostUtil;
429
+ (function(TaHostUtil2) {
430
+ TaHostUtil2.WAIT_MS = 1000;
431
+ function reach(events, name, waitMs = TaHostUtil2.WAIT_MS) {
432
+ return new Promise((resolve) => {
433
+ const timer = setTimeout(() => resolve(undefined), waitMs);
434
+ const request = {
435
+ name,
436
+ resolve: (answer) => {
437
+ clearTimeout(timer);
438
+ resolve(answer);
439
+ }
440
+ };
441
+ events.emit(TA_HOST_EVENT, request);
442
+ });
443
+ }
444
+ TaHostUtil2.reach = reach;
445
+ })(TaHostUtil || (TaHostUtil = {}));
391
446
  var TA_DOUBLE_FILE = { name: "@celljs/ta-web", key: "assistant.file" };
392
447
  // ../ta-protocol/esm/common/ta-protocol.js
393
448
  var TaServer = Symbol("TaServer");
@@ -403,6 +458,15 @@ var TaErrorUtil;
403
458
  }
404
459
  TaErrorUtil2.code = code;
405
460
  })(TaErrorUtil || (TaErrorUtil = {}));
461
+ var TaSlashCommandUtil;
462
+ (function(TaSlashCommandUtil2) {
463
+ function description(command, language) {
464
+ var _a, _b;
465
+ const { descriptions } = command;
466
+ return (_b = (_a = descriptions === null || descriptions === undefined ? undefined : descriptions[language]) !== null && _a !== undefined ? _a : descriptions === null || descriptions === undefined ? undefined : descriptions[language.split("-")[0]]) !== null && _b !== undefined ? _b : command.description;
467
+ }
468
+ TaSlashCommandUtil2.description = description;
469
+ })(TaSlashCommandUtil || (TaSlashCommandUtil = {}));
406
470
  var TaMessageContext;
407
471
  (function(TaMessageContext2) {
408
472
  const BLOCK = /^<context>\n?[\s\S]*?\n?<\/context>\n?/;
@@ -433,6 +497,78 @@ var TaImageNote;
433
497
  }
434
498
  TaImageNote2.strip = strip;
435
499
  })(TaImageNote || (TaImageNote = {}));
500
+ var TaImageSize;
501
+ (function(TaImageSize2) {
502
+ const JPEG_BYTES = 256 * 1024;
503
+ function of(base64) {
504
+ var _a, _b, _c;
505
+ let bytes;
506
+ try {
507
+ const binary = atob(base64.slice(0, Math.ceil(JPEG_BYTES * 4 / 3 / 4) * 4));
508
+ bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
509
+ } catch (_d) {
510
+ return;
511
+ }
512
+ 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);
513
+ return size && size.width > 0 && size.height > 0 ? size : undefined;
514
+ }
515
+ TaImageSize2.of = of;
516
+ function u16be(bytes, at) {
517
+ return bytes[at] << 8 | bytes[at + 1];
518
+ }
519
+ function u16le(bytes, at) {
520
+ return bytes[at] | bytes[at + 1] << 8;
521
+ }
522
+ function u24le(bytes, at) {
523
+ return bytes[at] | bytes[at + 1] << 8 | bytes[at + 2] << 16;
524
+ }
525
+ function u32be(bytes, at) {
526
+ return (bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3]) >>> 0;
527
+ }
528
+ function text(bytes, at, length) {
529
+ return String.fromCharCode(...bytes.subarray(at, at + length));
530
+ }
531
+ function png(bytes) {
532
+ return bytes.length >= 24 && bytes[0] === 137 && text(bytes, 1, 3) === "PNG" ? { width: u32be(bytes, 16), height: u32be(bytes, 20) } : undefined;
533
+ }
534
+ function gif(bytes) {
535
+ return bytes.length >= 10 && text(bytes, 0, 3) === "GIF" ? { width: u16le(bytes, 6), height: u16le(bytes, 8) } : undefined;
536
+ }
537
+ function webp(bytes) {
538
+ if (bytes.length < 30 || text(bytes, 0, 4) !== "RIFF" || text(bytes, 8, 4) !== "WEBP") {
539
+ return;
540
+ }
541
+ switch (text(bytes, 12, 4)) {
542
+ case "VP8 ":
543
+ return { width: u16le(bytes, 26) & 16383, height: u16le(bytes, 28) & 16383 };
544
+ case "VP8L": {
545
+ const bits = bytes[21] | bytes[22] << 8 | bytes[23] << 16 | bytes[24] << 24;
546
+ return { width: (bits & 16383) + 1, height: (bits >> 14 & 16383) + 1 };
547
+ }
548
+ case "VP8X":
549
+ return { width: u24le(bytes, 24) + 1, height: u24le(bytes, 27) + 1 };
550
+ default:
551
+ return;
552
+ }
553
+ }
554
+ function jpeg(bytes) {
555
+ if (bytes[0] !== 255 || bytes[1] !== 216) {
556
+ return;
557
+ }
558
+ let at = 2;
559
+ while (at + 9 < bytes.length) {
560
+ if (bytes[at] !== 255) {
561
+ return;
562
+ }
563
+ const marker = bytes[at + 1];
564
+ if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
565
+ return { width: u16be(bytes, at + 7), height: u16be(bytes, at + 5) };
566
+ }
567
+ at += 2 + u16be(bytes, at + 2);
568
+ }
569
+ return;
570
+ }
571
+ })(TaImageSize || (TaImageSize = {}));
436
572
  var TaAgentSummary;
437
573
  (function(TaAgentSummary2) {
438
574
  const START = /^\s*<conversation_history_summary>/;
@@ -711,13 +847,19 @@ var BotText;
711
847
  function plain(markdown) {
712
848
  const lines = [];
713
849
  let fenced = false;
850
+ let card = false;
714
851
  for (const line of markdown.replace(/\r\n/g, `
715
852
  `).split(`
716
853
  `)) {
717
- if (/^\s*(```|~~~)/.test(line)) {
854
+ const fence = /^\s*(?:```|~~~)\s*([\w-]*)/.exec(line);
855
+ if (fence) {
856
+ card = !fenced && fence[1].toLowerCase().startsWith("ta-");
718
857
  fenced = !fenced;
719
858
  continue;
720
859
  }
860
+ if (card) {
861
+ continue;
862
+ }
721
863
  if (fenced) {
722
864
  lines.push(line);
723
865
  continue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@celljs/ta-bots",
3
- "version": "4.10.0",
3
+ "version": "4.11.0",
4
4
  "description": "A pi package for Ta: bots of WeChat (ClawBot) and WeCom connected to the double, an agent or a conversation, messages and files both ways",
5
5
  "keywords": [
6
6
  "pi-package"
@@ -27,8 +27,8 @@
27
27
  "watch": "tsc -b -w --preserveWatchOutput"
28
28
  },
29
29
  "devDependencies": {
30
- "@celljs/ta-protocol": "4.10.0",
31
- "@celljs/ta-web": "4.10.0",
30
+ "@celljs/ta-protocol": "4.11.0",
31
+ "@celljs/ta-web": "4.11.0",
32
32
  "@types/react": "^19.3.0"
33
33
  },
34
34
  "publishConfig": {
package/web/index.js CHANGED
@@ -6,6 +6,7 @@ var Keys;
6
6
  })(Keys ||= {});
7
7
  var INBOUND_TYPE = "ta-bots";
8
8
  var SENT_TYPE = "ta-bots-sent";
9
+ var SEND_FILE_TOOL = "bot_send_file";
9
10
  var BotTargetUtil;
10
11
  ((BotTargetUtil) => {
11
12
  function same(a, b) {
@@ -111,7 +112,8 @@ var EN = {
111
112
  file: "File",
112
113
  video: "Video",
113
114
  group: "group",
114
- failedAction: "Failed: {message}"
115
+ failedAction: "Failed: {message}",
116
+ toolSendFile: "Send to the chat"
115
117
  };
116
118
  var ZH = {
117
119
  title: "机器人",
@@ -191,7 +193,8 @@ var ZH = {
191
193
  file: "文件",
192
194
  video: "视频",
193
195
  group: "群聊",
194
- failedAction: "操作失败:{message}"
196
+ failedAction: "操作失败:{message}",
197
+ toolSendFile: "发到聊天"
195
198
  };
196
199
  var BotWords;
197
200
  ((BotWords) => {
@@ -688,6 +691,7 @@ function composerAddon(client, dialogs) {
688
691
  icon: /* @__PURE__ */ React.createElement(Bot, {
689
692
  size: 16
690
693
  }),
694
+ setting: true,
691
695
  detail: here.length > 0 ? t("menuHere", { count: here.length }) : undefined,
692
696
  items: [
693
697
  ...bots.map((bot) => {
@@ -862,7 +866,9 @@ function BotsPicture() {
862
866
  // web-src/bots-view.tsx
863
867
  var PAGE = "page";
864
868
  function run2(action) {
865
- action.catch((error) => pi.ui.notify(t("failedAction", { message: message(error) }), "error"));
869
+ return action.then(() => {
870
+ return;
871
+ }, (error) => pi.ui.notify(t("failedAction", { message: message(error) }), "error"));
866
872
  }
867
873
  function when(time) {
868
874
  return new Date(time).toLocaleString(pi.ui.language, { month: "numeric", day: "numeric", hour: "2-digit", minute: "2-digit" });
@@ -896,19 +902,29 @@ function BotRow({ bot, client, dialogs }) {
896
902
  const Trash = icon("Trash2");
897
903
  const Unplug = icon("Unplug");
898
904
  const Refresh = icon("RefreshCw");
905
+ const [doing, setDoing] = React.useState();
906
+ const act = (key, action) => {
907
+ setDoing(key);
908
+ run2(action).finally(() => setDoing(undefined));
909
+ };
910
+ const busy = (key, base) => doing === key ? `${base} ta-busy` : base;
911
+ const mark = (key) => doing === key && /* @__PURE__ */ React.createElement("span", {
912
+ className: "ta-spinner ta-busy-mark"
913
+ });
899
914
  const rename = async () => {
900
915
  const name = await pi.ui.input(t("rename"), bot.name, { value: bot.name });
901
916
  if (name?.trim()) {
902
- run2(client.call("rename", bot.id, name.trim()).then(() => client.load()));
917
+ act("rename", client.call("rename", bot.id, name.trim()).then(() => client.load()));
903
918
  }
904
919
  };
905
920
  const remove = async () => {
906
921
  if (await pi.ui.confirm(t("removeConfirm", { name: bot.name }), t("removeMessage"), { confirmLabel: t("remove"), danger: true })) {
907
- run2(client.call("remove", bot.id).then(() => client.load()));
922
+ act("remove", client.call("remove", bot.id).then(() => client.load()));
908
923
  }
909
924
  };
910
925
  return /* @__PURE__ */ React.createElement("div", {
911
- className: "tbo-row"
926
+ className: doing === "remove" ? "tbo-row ta-pending" : "tbo-row",
927
+ "aria-busy": doing !== undefined || undefined
912
928
  }, /* @__PURE__ */ React.createElement(KindMark, {
913
929
  kind: bot.kind,
914
930
  size: 32
@@ -944,33 +960,33 @@ function BotRow({ bot, client, dialogs }) {
944
960
  size: 15
945
961
  })), !BotTargetUtil.same(bot.target, { kind: "double" }) && /* @__PURE__ */ React.createElement("button", {
946
962
  type: "button",
947
- className: "tbo-button tbo-small",
948
- onClick: () => run2(client.bindHere(bot, { kind: "double" }))
949
- }, t("connectTo")), bot.target && /* @__PURE__ */ React.createElement("button", {
963
+ className: busy("bind", "tbo-button tbo-small"),
964
+ onClick: () => act("bind", client.bindHere(bot, { kind: "double" }))
965
+ }, t("connectTo"), mark("bind")), bot.target && /* @__PURE__ */ React.createElement("button", {
950
966
  type: "button",
951
- className: "tbo-icon",
967
+ className: busy("unbind", "tbo-icon"),
952
968
  title: t("disconnect"),
953
969
  "aria-label": t("disconnect"),
954
- onClick: () => run2(client.unbind(bot))
970
+ onClick: () => act("unbind", client.unbind(bot))
955
971
  }, /* @__PURE__ */ React.createElement(Unplug, {
956
972
  size: 15
957
- })), /* @__PURE__ */ React.createElement("button", {
973
+ }), mark("unbind")), /* @__PURE__ */ React.createElement("button", {
958
974
  type: "button",
959
- className: "tbo-icon",
975
+ className: busy("rename", "tbo-icon"),
960
976
  title: t("rename"),
961
977
  "aria-label": t("rename"),
962
978
  onClick: () => void rename()
963
979
  }, /* @__PURE__ */ React.createElement(Pencil, {
964
980
  size: 15
965
- })), /* @__PURE__ */ React.createElement("button", {
981
+ }), mark("rename")), /* @__PURE__ */ React.createElement("button", {
966
982
  type: "button",
967
- className: "tbo-icon tbo-danger",
983
+ className: busy("remove", "tbo-icon tbo-danger"),
968
984
  title: t("remove"),
969
985
  "aria-label": t("remove"),
970
986
  onClick: () => void remove()
971
987
  }, /* @__PURE__ */ React.createElement(Trash, {
972
988
  size: 15
973
- }))));
989
+ }), mark("remove"))));
974
990
  }
975
991
  function BotsPage({ client, dialogs }) {
976
992
  const { bots, loaded } = useBots(client);
@@ -1175,6 +1191,7 @@ var STYLE = `
1175
1191
  .tbo-primary { background: var(--ta-brand); border-color: var(--ta-brand); color: var(--ta-brand-text); }
1176
1192
  .tbo-primary:hover:not(:disabled) { background: var(--ta-brand); opacity: .88; }
1177
1193
  .tbo-small { height: 28px; padding: 0 10px; font-size: calc(12.5px * var(--ta-font-scale)); }
1194
+ .tbo-icon.ta-busy, .tbo-button.ta-busy { position: relative; }
1178
1195
  .tbo-icon { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; border: 0; border-radius: 7px; background: none; color: var(--ta-muted); cursor: pointer; }
1179
1196
  .tbo-icon:hover { background: var(--ta-hover); color: var(--ta-text); }
1180
1197
  .tbo-danger:hover { color: var(--ta-red); }
@@ -1210,6 +1227,19 @@ function injectStyle() {
1210
1227
  return () => element.remove();
1211
1228
  }
1212
1229
 
1230
+ // web-src/tool-line.ts
1231
+ var BotToolLine;
1232
+ ((BotToolLine) => {
1233
+ function renderCall(args, verb) {
1234
+ return { verb, target: typeof args.path === "string" ? args.path.trim() : "" };
1235
+ }
1236
+ BotToolLine.renderCall = renderCall;
1237
+ function register(pi2, verb) {
1238
+ pi2.registerToolRenderer?.((name, next) => name === SEND_FILE_TOOL ? { renderCall: (args) => renderCall(args, verb()) } : next());
1239
+ }
1240
+ BotToolLine.register = register;
1241
+ })(BotToolLine ||= {});
1242
+
1213
1243
  // web-src/index.tsx
1214
1244
  function activate(pi2) {
1215
1245
  bind(pi2.React);
@@ -1221,6 +1251,7 @@ function activate(pi2) {
1221
1251
  pi2.registerComposerAddon(composerAddon(client, dialogs));
1222
1252
  pi2.registerMessageRenderer(INBOUND_TYPE, InboundMessage);
1223
1253
  pi2.registerMessageRenderer(SENT_TYPE, SentMessageView);
1254
+ BotToolLine.register(pi2, () => t("toolSendFile"));
1224
1255
  pi2.registerView("bots", {
1225
1256
  title: () => t("title"),
1226
1257
  icon: icon("Bot"),