@celljs/ta-bots 0.0.0-stage → 4.9.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/web/index.js ADDED
@@ -0,0 +1,1241 @@
1
+ // web-src/bots-protocol.ts
2
+ var Keys;
3
+ ((Keys) => {
4
+ Keys.BOTS = "bots";
5
+ Keys.STATUS = "status";
6
+ })(Keys ||= {});
7
+ var INBOUND_TYPE = "ta-bots";
8
+ var SENT_TYPE = "ta-bots-sent";
9
+ var BotTargetUtil;
10
+ ((BotTargetUtil) => {
11
+ function same(a, b) {
12
+ if (!a || !b || a.kind !== b.kind) {
13
+ return false;
14
+ }
15
+ return a.kind === "double" || b.kind === "session" && a.sessionFile === b.sessionFile;
16
+ }
17
+ BotTargetUtil.same = same;
18
+ function parse(value) {
19
+ if (!value || typeof value !== "object") {
20
+ return;
21
+ }
22
+ const target = value;
23
+ if (target.kind === "double") {
24
+ return { kind: "double" };
25
+ }
26
+ if (target.kind === "session" && typeof target.sessionFile === "string" && target.sessionFile) {
27
+ const text = (key) => typeof target[key] === "string" && target[key] ? { [key]: target[key] } : {};
28
+ return { kind: "session", sessionFile: target.sessionFile, ...text("cwd"), ...text("title"), ...text("group") };
29
+ }
30
+ return;
31
+ }
32
+ BotTargetUtil.parse = parse;
33
+ })(BotTargetUtil ||= {});
34
+
35
+ // web-src/i18n.ts
36
+ var EN = {
37
+ title: "Bots",
38
+ menu: "Bots",
39
+ menuHere: "{count} here",
40
+ groupBots: "Your bots",
41
+ groupAdd: "Connect a bot",
42
+ addWeixin: "WeChat…",
43
+ addWecom: "WeCom…",
44
+ here: "Here",
45
+ elsewhere: "In {place}",
46
+ nowhere: "Not connected",
47
+ double: "the double",
48
+ conversation: "a conversation",
49
+ moved: "{name} moved here from {place}",
50
+ connectedHere: "{name} is connected here",
51
+ disconnected: "{name} disconnected",
52
+ disconnect: "Disconnect",
53
+ kindWeixin: "WeChat",
54
+ kindWecom: "WeCom",
55
+ statusOnline: "Online",
56
+ statusConnecting: "Connecting",
57
+ statusOffline: "Offline",
58
+ statusExpired: "Scan again",
59
+ statusError: "Failed",
60
+ weixinTitle: "Connect WeChat",
61
+ scanHint: "Scan with WeChat on your phone",
62
+ scanned: "Scanned: confirm on your phone",
63
+ scanHintPhone: "This phone shows the code: let WeChat read it from your photos",
64
+ phoneStep1: "Save the code (or hold it and save the image)",
65
+ phoneStep2: "In WeChat, Scan, then Album at the top right: pick it",
66
+ saveQr: "Save the code",
67
+ openWeixin: "Open WeChat",
68
+ qrSaved: "Saved: now scan it from the album in WeChat",
69
+ scanHintWecom: "Scan with WeCom on your phone, then create the robot",
70
+ scanHintPhoneWecom: "This phone shows the code: let WeCom read it from your photos",
71
+ phoneStep2Wecom: "In WeCom, Scan, then pick it from the album",
72
+ openWecom: "Open WeCom",
73
+ qrSavedWecom: "Saved: now scan it from the album in WeCom",
74
+ scannedWecom: "Scanned: create the robot on your phone",
75
+ wecomDone: "Connected: talk to the robot in WeCom",
76
+ wecomScanNote: "Scanning makes an intelligent robot of your company in one tap and connects it here; it needs the right to create robots.",
77
+ wecomManual: "Enter a Bot ID and Secret instead",
78
+ wecomScan: "Scan a code instead",
79
+ codeHint: "Type the number your phone shows",
80
+ wrongCode: "That number does not match; try again",
81
+ codeSend: "Go on",
82
+ weixinDone: "Connected: talk to it in WeChat",
83
+ already: "This WeChat is connected already",
84
+ expired: "The code expired",
85
+ tryAgain: "New code",
86
+ loading: "Getting a code…",
87
+ weixinNote: "One bot per WeChat account: scanning with it again renews that bot.",
88
+ wecomTitle: "Connect a robot of WeCom",
89
+ wecomHint: "In the admin console of WeCom, make an intelligent robot in API mode with a long connection, and copy its Bot ID and Secret.",
90
+ botId: "Bot ID",
91
+ secret: "Secret",
92
+ name: "Name",
93
+ namePlaceholder: "Optional",
94
+ connect: "Connect",
95
+ connecting: "Connecting…",
96
+ close: "Close",
97
+ pageHint: "Each bot is connected to one place at most; one place can have several.",
98
+ emptyTitle: "No bots yet",
99
+ emptyHint: "Connect one and tell Ta what to do from WeChat",
100
+ rename: "Rename",
101
+ remove: "Remove",
102
+ removeConfirm: "Remove {name}?",
103
+ removeMessage: "It disconnects and its sign-in is deleted. The conversations stay.",
104
+ connectTo: "Connect to the double",
105
+ lastMessage: "Last message {time}",
106
+ sentTo: "Sent to {name}",
107
+ sentFile: "Sent {file} to {name}",
108
+ sendFailed: "Not sent to {name}: {error}",
109
+ image: "Picture",
110
+ voice: "Voice",
111
+ file: "File",
112
+ video: "Video",
113
+ group: "group",
114
+ failedAction: "Failed: {message}"
115
+ };
116
+ var ZH = {
117
+ title: "机器人",
118
+ menu: "机器人",
119
+ menuHere: "这里 {count} 个",
120
+ groupBots: "我的机器人",
121
+ groupAdd: "连接机器人",
122
+ addWeixin: "微信…",
123
+ addWecom: "企业微信…",
124
+ here: "在这里",
125
+ elsewhere: "在「{place}」",
126
+ nowhere: "未连接",
127
+ double: "分身",
128
+ conversation: "一个对话",
129
+ moved: "已把 {name} 从「{place}」移到这里",
130
+ connectedHere: "{name} 已连到这里",
131
+ disconnected: "已断开 {name}",
132
+ disconnect: "断开",
133
+ kindWeixin: "微信",
134
+ kindWecom: "企业微信",
135
+ statusOnline: "在线",
136
+ statusConnecting: "连接中",
137
+ statusOffline: "离线",
138
+ statusExpired: "需重新扫码",
139
+ statusError: "出错",
140
+ weixinTitle: "连接微信",
141
+ scanHint: "用手机微信扫一扫",
142
+ scanned: "已扫码,请在手机上确认",
143
+ scanHintPhone: "二维码在这台手机上,用微信从相册识别",
144
+ phoneStep1: "保存二维码(或长按二维码存储图像)",
145
+ phoneStep2: "打开微信「扫一扫」,点右上角「相册」选中它",
146
+ saveQr: "保存二维码",
147
+ openWeixin: "打开微信",
148
+ qrSaved: "已保存,去微信扫一扫里从相册选它",
149
+ scanHintWecom: "用手机企业微信扫一扫,按提示创建机器人",
150
+ scanHintPhoneWecom: "二维码在这台手机上,用企业微信从相册识别",
151
+ phoneStep2Wecom: "打开企业微信「扫一扫」,从相册里选中它",
152
+ openWecom: "打开企业微信",
153
+ qrSavedWecom: "已保存,去企业微信扫一扫里从相册选它",
154
+ scannedWecom: "已扫码,请在手机上创建机器人",
155
+ wecomDone: "已连接,去企业微信里和机器人说话吧",
156
+ wecomScanNote: "扫码后一键创建企业的智能机器人并连到这里;需要有创建机器人的权限。",
157
+ wecomManual: "改为填写 Bot ID 和 Secret",
158
+ wecomScan: "改为扫码",
159
+ codeHint: "输入手机上显示的数字",
160
+ wrongCode: "数字不对,请重新输入",
161
+ codeSend: "继续",
162
+ weixinDone: "已连接,去微信里和它说话吧",
163
+ already: "这个微信已经连接过了",
164
+ expired: "二维码已失效",
165
+ tryAgain: "重新获取",
166
+ loading: "正在获取二维码…",
167
+ weixinNote: "一个微信号对应一个机器人,用它重复扫码会更新这个机器人。",
168
+ wecomTitle: "连接企业微信智能机器人",
169
+ wecomHint: "在企业微信管理后台创建智能机器人,选择 API 模式里的长连接,复制它的 Bot ID 和 Secret。",
170
+ botId: "Bot ID",
171
+ secret: "Secret",
172
+ name: "名称",
173
+ namePlaceholder: "可不填",
174
+ connect: "连接",
175
+ connecting: "正在连接…",
176
+ close: "关闭",
177
+ pageHint: "一个机器人只连在一个地方;一个地方可以连多个机器人。",
178
+ emptyTitle: "还没有机器人",
179
+ emptyHint: "连接后,在微信里就能让 Ta 干活",
180
+ rename: "重命名",
181
+ remove: "移除",
182
+ removeConfirm: "移除 {name}?",
183
+ removeMessage: "它会断开,登录信息会删除,对话保留。",
184
+ connectTo: "连到分身",
185
+ lastMessage: "最近消息 {time}",
186
+ sentTo: "已发回 {name}",
187
+ sentFile: "已把 {file} 发到 {name}",
188
+ sendFailed: "没能发回 {name}:{error}",
189
+ image: "图片",
190
+ voice: "语音",
191
+ file: "文件",
192
+ video: "视频",
193
+ group: "群聊",
194
+ failedAction: "操作失败:{message}"
195
+ };
196
+ var BotWords;
197
+ ((BotWords) => {
198
+ function of(language) {
199
+ return language === "zh-CN" ? ZH : EN;
200
+ }
201
+ BotWords.of = of;
202
+ function format(text, values) {
203
+ return text.replace(/\{(\w+)\}/g, (whole, key) => (key in values) ? String(values[key]) : whole);
204
+ }
205
+ BotWords.format = format;
206
+ })(BotWords ||= {});
207
+
208
+ // web-src/react.ts
209
+ var React;
210
+ function bind(react) {
211
+ React = react;
212
+ }
213
+
214
+ // web-src/page.ts
215
+ var pi;
216
+ function bindPage(api) {
217
+ pi = api;
218
+ }
219
+ function t(key, values = {}) {
220
+ return BotWords.format(BotWords.of(pi.ui.language)[key], values);
221
+ }
222
+ function icon(name) {
223
+ return pi.ui.icons[name] ?? pi.ui.icons.Bot;
224
+ }
225
+ function message(error) {
226
+ return error instanceof Error ? error.message : String(error);
227
+ }
228
+ var KIND_ICON = { weixin: "MessageCircle", wecom: "Building2" };
229
+ function kindLabel(kind) {
230
+ return t(kind === "weixin" ? "kindWeixin" : "kindWecom");
231
+ }
232
+ function statusLabel(state) {
233
+ const keys = { online: "statusOnline", connecting: "statusConnecting", offline: "statusOffline", expired: "statusExpired", error: "statusError" };
234
+ return t(keys[state]);
235
+ }
236
+ function targetOf(conversation) {
237
+ if (conversation.kind === "double") {
238
+ return { kind: "double" };
239
+ }
240
+ return conversation.sessionFile ? {
241
+ kind: "session",
242
+ sessionFile: conversation.sessionFile,
243
+ ...conversation.cwd ? { cwd: conversation.cwd } : {},
244
+ ...conversation.title ? { title: conversation.title } : {},
245
+ ...conversation.group ? { group: conversation.group } : {}
246
+ } : undefined;
247
+ }
248
+ function placeName(target) {
249
+ if (target.kind === "double") {
250
+ return t("double");
251
+ }
252
+ const base = target.cwd?.replace(/[\\/]+$/, "").split(/[\\/]/).pop();
253
+ return target.title || base || t("conversation");
254
+ }
255
+
256
+ class BotsClient {
257
+ state = { bots: [], loaded: false };
258
+ listeners = new Set;
259
+ stop;
260
+ start() {
261
+ this.stop = pi.storage.onChange((keys) => {
262
+ if (keys.includes(Keys.BOTS) || keys.includes(Keys.STATUS)) {
263
+ this.load();
264
+ }
265
+ });
266
+ this.load();
267
+ }
268
+ dispose() {
269
+ this.stop?.();
270
+ this.listeners.clear();
271
+ }
272
+ subscribe(listener) {
273
+ this.listeners.add(listener);
274
+ return () => this.listeners.delete(listener);
275
+ }
276
+ call(method, ...params) {
277
+ return pi.callHost(method, ...params);
278
+ }
279
+ async bindHere(bot, target) {
280
+ const { before } = await this.call("bind", bot.id, target);
281
+ await this.load();
282
+ pi.ui.notify(before && !BotTargetUtil.same(before, target) ? t("moved", { name: bot.name, place: placeName(before) }) : t("connectedHere", { name: bot.name }));
283
+ }
284
+ async unbind(bot) {
285
+ await this.call("bind", bot.id, null);
286
+ await this.load();
287
+ pi.ui.notify(t("disconnected", { name: bot.name }));
288
+ }
289
+ async load() {
290
+ const [bots, status] = await Promise.all([pi.storage.get(Keys.BOTS), pi.storage.get(Keys.STATUS)]);
291
+ const list = Array.isArray(bots) ? bots : [];
292
+ this.state = { loaded: true, bots: list.map((bot) => ({ ...bot, ...status?.[bot.id] ?? { status: "offline" } })) };
293
+ for (const listener of this.listeners) {
294
+ listener();
295
+ }
296
+ }
297
+ }
298
+ function useBots(client) {
299
+ return React.useSyncExternalStore((listener) => client.subscribe(listener), () => client.state);
300
+ }
301
+
302
+ class DialogState {
303
+ current;
304
+ listeners = new Set;
305
+ open(dialog) {
306
+ this.current = dialog;
307
+ for (const listener of this.listeners) {
308
+ listener();
309
+ }
310
+ }
311
+ subscribe(listener) {
312
+ this.listeners.add(listener);
313
+ return () => this.listeners.delete(listener);
314
+ }
315
+ use() {
316
+ return React.useSyncExternalStore((listener) => this.subscribe(listener), () => this.current);
317
+ }
318
+ }
319
+
320
+ // web-src/qr-image.ts
321
+ var QrImage;
322
+ ((QrImage) => {
323
+ const SIZE = 720;
324
+ const NAME = "ta-bot-qr.png";
325
+ async function png(element) {
326
+ const svg = element.querySelector("svg");
327
+ if (!svg) {
328
+ throw new Error("No QR code is drawn yet.");
329
+ }
330
+ const copy = svg.cloneNode(true);
331
+ copy.setAttribute("xmlns", "http://www.w3.org/2000/svg");
332
+ copy.setAttribute("width", String(SIZE));
333
+ copy.setAttribute("height", String(SIZE));
334
+ const image = new Image;
335
+ image.src = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(new XMLSerializer().serializeToString(copy))}`;
336
+ await image.decode();
337
+ const canvas = document.createElement("canvas");
338
+ canvas.width = SIZE;
339
+ canvas.height = SIZE;
340
+ const context = canvas.getContext("2d");
341
+ context.fillStyle = "#fff";
342
+ context.fillRect(0, 0, SIZE, SIZE);
343
+ context.drawImage(image, 0, 0, SIZE, SIZE);
344
+ return new Promise((resolve, reject) => canvas.toBlob((blob) => blob ? resolve(blob) : reject(new Error("The picture was not made.")), "image/png"));
345
+ }
346
+ QrImage.png = png;
347
+ async function save(blob) {
348
+ const file = new File([blob], NAME, { type: "image/png" });
349
+ if (navigator.canShare?.({ files: [file] })) {
350
+ try {
351
+ await navigator.share({ files: [file] });
352
+ return true;
353
+ } catch (error) {
354
+ if (error.name === "AbortError") {
355
+ return false;
356
+ }
357
+ }
358
+ }
359
+ const url = URL.createObjectURL(blob);
360
+ const link = document.createElement("a");
361
+ link.href = url;
362
+ link.download = NAME;
363
+ document.body.appendChild(link);
364
+ link.click();
365
+ link.remove();
366
+ setTimeout(() => URL.revokeObjectURL(url), 1e4);
367
+ return true;
368
+ }
369
+ QrImage.save = save;
370
+ function openApp(kind) {
371
+ window.location.href = kind === "weixin" ? "weixin://scanqrcode" : "wxwork://";
372
+ }
373
+ QrImage.openApp = openApp;
374
+ })(QrImage ||= {});
375
+
376
+ // web-src/connect-dialog.tsx
377
+ var DONE = ["done", "already", "expired", "failed"];
378
+ var POLL_MS = 1200;
379
+ function useLogin(client, start, onClose) {
380
+ const [login, setLogin] = React.useState();
381
+ const [error, setError] = React.useState();
382
+ const [round, setRound] = React.useState(0);
383
+ const id = login?.id;
384
+ const status = login?.status;
385
+ const begin = React.useRef(start);
386
+ begin.current = start;
387
+ React.useEffect(() => {
388
+ let alive = true;
389
+ setLogin(undefined);
390
+ setError(undefined);
391
+ begin.current().then((state) => alive && setLogin(state), (cause) => alive && setError(message(cause)));
392
+ return () => {
393
+ alive = false;
394
+ };
395
+ }, [round]);
396
+ React.useEffect(() => {
397
+ if (!id || status && DONE.includes(status)) {
398
+ return;
399
+ }
400
+ const timer = setInterval(() => {
401
+ client.call("login", id).then((state) => setLogin((before) => before?.id === id ? state : before), () => {
402
+ return;
403
+ });
404
+ }, POLL_MS);
405
+ return () => clearInterval(timer);
406
+ }, [client, id, status]);
407
+ const open = React.useRef(undefined);
408
+ open.current = status && !DONE.includes(status) ? id : undefined;
409
+ React.useEffect(() => () => void (open.current && client.call("cancelLogin", open.current).catch(() => {
410
+ return;
411
+ })), [client]);
412
+ React.useEffect(() => {
413
+ if (status === "done" || status === "already") {
414
+ client.load();
415
+ const timer = setTimeout(onClose, 1600);
416
+ return () => clearTimeout(timer);
417
+ }
418
+ return;
419
+ }, [status, client, onClose]);
420
+ return { login, setLogin, error, setError, again: () => setRound((value) => value + 1) };
421
+ }
422
+ function QrPanel({
423
+ kind,
424
+ login,
425
+ error,
426
+ dim,
427
+ status: said,
428
+ onAgain,
429
+ onError
430
+ }) {
431
+ const Check = icon("Check");
432
+ const status = login?.status;
433
+ const handheld = pi.ui.useDevice().handheld;
434
+ const qrBox = React.useRef(null);
435
+ const [picture, setPicture] = React.useState();
436
+ const qr = login?.qr;
437
+ React.useEffect(() => {
438
+ if (!handheld || !qr || !qrBox.current) {
439
+ return;
440
+ }
441
+ let url;
442
+ let alive = true;
443
+ QrImage.png(qrBox.current).then((blob) => {
444
+ url = URL.createObjectURL(blob);
445
+ if (alive) {
446
+ setPicture({ qr, url, blob });
447
+ }
448
+ }, () => {
449
+ return;
450
+ });
451
+ return () => {
452
+ alive = false;
453
+ if (url) {
454
+ URL.revokeObjectURL(url);
455
+ }
456
+ };
457
+ }, [handheld, qr]);
458
+ const shown = picture?.qr === qr ? picture : undefined;
459
+ const save = () => {
460
+ if (shown) {
461
+ QrImage.save(shown.blob).then((saved) => saved && pi.ui.notify(t(kind === "weixin" ? "qrSaved" : "qrSavedWecom")), (cause) => onError(message(cause)));
462
+ }
463
+ };
464
+ const connected = status === "done" || status === "already";
465
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", {
466
+ ref: qrBox,
467
+ className: `tbo-qr ${dim || status === "scanned" ? "tbo-qr-dim" : ""}`
468
+ }, qr && !connected ? shown ? /* @__PURE__ */ React.createElement("img", {
469
+ className: "tbo-qr-image",
470
+ src: shown.url,
471
+ alt: t("scanHint")
472
+ }) : /* @__PURE__ */ React.createElement(pi.ui.QrCode, {
473
+ text: qr,
474
+ label: t("scanHint")
475
+ }) : connected ? /* @__PURE__ */ React.createElement("span", {
476
+ className: "tbo-qr-done"
477
+ }, /* @__PURE__ */ React.createElement(Check, {
478
+ size: 40
479
+ })) : /* @__PURE__ */ React.createElement("span", {
480
+ className: "tbo-qr-wait"
481
+ }, error ? "" : t("loading")), status === "expired" && /* @__PURE__ */ React.createElement("button", {
482
+ type: "button",
483
+ className: "tbo-qr-again",
484
+ onClick: onAgain
485
+ }, t("tryAgain"))), /* @__PURE__ */ React.createElement("div", {
486
+ className: `tbo-connect-status ${error || status === "failed" ? "tbo-bad" : ""}`,
487
+ "aria-live": "polite"
488
+ }, error ?? said ?? (status === "scanned" ? t(kind === "weixin" ? "scanned" : "scannedWecom") : status === "done" ? t(kind === "weixin" ? "weixinDone" : "wecomDone") : status === "already" ? t("already") : status === "expired" ? t("expired") : status === "failed" ? login?.error : kind === "weixin" ? t(handheld ? "scanHintPhone" : "scanHint") : t(handheld ? "scanHintPhoneWecom" : "scanHintWecom"))), handheld && shown && (status === "waiting" || status === "scanned") && /* @__PURE__ */ React.createElement("div", {
489
+ className: "tbo-phone-steps"
490
+ }, /* @__PURE__ */ React.createElement("ol", {
491
+ className: "tbo-steps"
492
+ }, /* @__PURE__ */ React.createElement("li", null, t("phoneStep1")), /* @__PURE__ */ React.createElement("li", null, t(kind === "weixin" ? "phoneStep2" : "phoneStep2Wecom"))), /* @__PURE__ */ React.createElement("div", {
493
+ className: "tbo-actions tbo-actions-wide"
494
+ }, /* @__PURE__ */ React.createElement("button", {
495
+ type: "button",
496
+ className: "tbo-button",
497
+ onClick: save
498
+ }, t("saveQr")), /* @__PURE__ */ React.createElement("button", {
499
+ type: "button",
500
+ className: "tbo-button tbo-primary",
501
+ onClick: () => QrImage.openApp(kind)
502
+ }, t(kind === "weixin" ? "openWeixin" : "openWecom")))));
503
+ }
504
+ function WeixinDialog({ client, target, onClose }) {
505
+ const { login, setLogin, error, setError, again } = useLogin(client, () => client.call("startWeixinLogin", target), onClose);
506
+ const [code, setCode] = React.useState("");
507
+ const id = login?.id;
508
+ const status = login?.status;
509
+ const asking = status === "code" || status === "wrongCode";
510
+ const send = () => {
511
+ if (id && code.trim()) {
512
+ client.call("verifyLogin", id, code.trim()).then(setLogin, (cause) => setError(message(cause)));
513
+ setCode("");
514
+ }
515
+ };
516
+ return /* @__PURE__ */ React.createElement(pi.ui.Dialog, {
517
+ title: t("weixinTitle"),
518
+ onClose
519
+ }, /* @__PURE__ */ React.createElement("div", {
520
+ className: "tbo-connect"
521
+ }, /* @__PURE__ */ React.createElement(QrPanel, {
522
+ kind: "weixin",
523
+ login,
524
+ error,
525
+ dim: asking,
526
+ status: asking ? t(status === "wrongCode" ? "wrongCode" : "codeHint") : undefined,
527
+ onAgain: again,
528
+ onError: setError
529
+ }), asking && /* @__PURE__ */ React.createElement("form", {
530
+ className: "tbo-code",
531
+ onSubmit: (event) => {
532
+ event.preventDefault();
533
+ send();
534
+ }
535
+ }, /* @__PURE__ */ React.createElement("input", {
536
+ className: "tbo-input",
537
+ inputMode: "numeric",
538
+ autoComplete: "one-time-code",
539
+ autoFocus: true,
540
+ value: code,
541
+ onChange: (event) => setCode(event.target.value),
542
+ "aria-label": t("codeHint")
543
+ }), /* @__PURE__ */ React.createElement("button", {
544
+ type: "submit",
545
+ className: "tbo-button tbo-primary",
546
+ disabled: !code.trim()
547
+ }, t("codeSend"))), /* @__PURE__ */ React.createElement("p", {
548
+ className: "tbo-note"
549
+ }, t("weixinNote"))));
550
+ }
551
+ function WecomDialog({ client, target, onClose }) {
552
+ const [manual, setManual] = React.useState(false);
553
+ return /* @__PURE__ */ React.createElement(pi.ui.Dialog, {
554
+ title: t("wecomTitle"),
555
+ onClose
556
+ }, manual ? /* @__PURE__ */ React.createElement(WecomForm, {
557
+ client,
558
+ target,
559
+ onClose,
560
+ onScan: () => setManual(false)
561
+ }) : /* @__PURE__ */ React.createElement(WecomScan, {
562
+ client,
563
+ target,
564
+ onClose,
565
+ onManual: () => setManual(true)
566
+ }));
567
+ }
568
+ function WecomScan({ client, target, onClose, onManual }) {
569
+ const { login, error, setError, again } = useLogin(client, () => client.call("startWecomLogin", target), onClose);
570
+ return /* @__PURE__ */ React.createElement("div", {
571
+ className: "tbo-connect"
572
+ }, /* @__PURE__ */ React.createElement(QrPanel, {
573
+ kind: "wecom",
574
+ login,
575
+ error,
576
+ status: undefined,
577
+ onAgain: again,
578
+ onError: setError
579
+ }), /* @__PURE__ */ React.createElement("p", {
580
+ className: "tbo-note"
581
+ }, t("wecomScanNote")), /* @__PURE__ */ React.createElement("button", {
582
+ type: "button",
583
+ className: "tbo-link",
584
+ onClick: onManual
585
+ }, t("wecomManual")));
586
+ }
587
+ function WecomForm({ client, target, onClose, onScan }) {
588
+ const [botId, setBotId] = React.useState("");
589
+ const [secret, setSecret] = React.useState("");
590
+ const [name, setName] = React.useState("");
591
+ const [busy, setBusy] = React.useState(false);
592
+ const [error, setError] = React.useState();
593
+ const submit = async () => {
594
+ setBusy(true);
595
+ setError(undefined);
596
+ try {
597
+ const bot = await client.call("addWecom", { botId, secret, name: name || undefined, target });
598
+ await client.load();
599
+ pi.ui.notify(t("connectedHere", { name: bot.name }));
600
+ onClose();
601
+ } catch (cause) {
602
+ setError(message(cause));
603
+ } finally {
604
+ setBusy(false);
605
+ }
606
+ };
607
+ return /* @__PURE__ */ React.createElement("form", {
608
+ className: "tbo-form",
609
+ onSubmit: (event) => {
610
+ event.preventDefault();
611
+ submit();
612
+ }
613
+ }, /* @__PURE__ */ React.createElement("p", {
614
+ className: "tbo-note"
615
+ }, t("wecomHint")), /* @__PURE__ */ React.createElement("label", {
616
+ className: "tbo-field"
617
+ }, /* @__PURE__ */ React.createElement("span", null, t("botId")), /* @__PURE__ */ React.createElement("input", {
618
+ className: "tbo-input",
619
+ value: botId,
620
+ spellCheck: false,
621
+ autoComplete: "off",
622
+ onChange: (event) => setBotId(event.target.value)
623
+ })), /* @__PURE__ */ React.createElement("label", {
624
+ className: "tbo-field"
625
+ }, /* @__PURE__ */ React.createElement("span", null, t("secret")), /* @__PURE__ */ React.createElement("input", {
626
+ className: "tbo-input",
627
+ type: "password",
628
+ value: secret,
629
+ autoComplete: "off",
630
+ onChange: (event) => setSecret(event.target.value)
631
+ })), /* @__PURE__ */ React.createElement("label", {
632
+ className: "tbo-field"
633
+ }, /* @__PURE__ */ React.createElement("span", null, t("name")), /* @__PURE__ */ React.createElement("input", {
634
+ className: "tbo-input",
635
+ value: name,
636
+ placeholder: t("namePlaceholder"),
637
+ onChange: (event) => setName(event.target.value)
638
+ })), error && /* @__PURE__ */ React.createElement("div", {
639
+ className: "tbo-connect-status tbo-bad"
640
+ }, error), /* @__PURE__ */ React.createElement("div", {
641
+ className: "tbo-actions"
642
+ }, /* @__PURE__ */ React.createElement("button", {
643
+ type: "button",
644
+ className: "tbo-link tbo-actions-start",
645
+ onClick: onScan
646
+ }, t("wecomScan")), /* @__PURE__ */ React.createElement("button", {
647
+ type: "button",
648
+ className: "tbo-button",
649
+ onClick: onClose
650
+ }, t("close")), /* @__PURE__ */ React.createElement("button", {
651
+ type: "submit",
652
+ className: "tbo-button tbo-primary",
653
+ disabled: busy || !botId.trim() || !secret.trim()
654
+ }, busy ? t("connecting") : t("connect"))));
655
+ }
656
+
657
+ // web-src/composer.tsx
658
+ function KindMark({ kind, size = 16 }) {
659
+ const Icon = icon(KIND_ICON[kind]);
660
+ return /* @__PURE__ */ React.createElement("span", {
661
+ className: `tbo-mark tbo-mark-${kind}`,
662
+ style: { width: size, height: size },
663
+ "aria-hidden": "true"
664
+ }, /* @__PURE__ */ React.createElement(Icon, {
665
+ size: Math.round(size * 0.66),
666
+ strokeWidth: 2.4
667
+ }));
668
+ }
669
+ function run(action) {
670
+ action.catch((error) => pi.ui.notify(t("failedAction", { message: message(error) }), "error"));
671
+ }
672
+ function composerAddon(client, dialogs) {
673
+ return {
674
+ subscribe: (listener) => client.subscribe(listener),
675
+ actions: (conversation) => {
676
+ const target = targetOf(conversation);
677
+ if (!target) {
678
+ return [];
679
+ }
680
+ const { bots } = client.state;
681
+ const here = bots.filter((bot) => BotTargetUtil.same(bot.target, target));
682
+ const Bot = icon("Bot");
683
+ const Plus = icon("Plus");
684
+ return [
685
+ {
686
+ key: "ta-bots",
687
+ label: t("menu"),
688
+ icon: /* @__PURE__ */ React.createElement(Bot, {
689
+ size: 16
690
+ }),
691
+ detail: here.length > 0 ? t("menuHere", { count: here.length }) : undefined,
692
+ items: [
693
+ ...bots.map((bot) => {
694
+ const isHere = here.includes(bot);
695
+ return {
696
+ key: bot.id,
697
+ label: bot.name,
698
+ icon: /* @__PURE__ */ React.createElement(KindMark, {
699
+ kind: bot.kind
700
+ }),
701
+ group: t("groupBots"),
702
+ detail: isHere ? t("here") : bot.target ? t("elsewhere", { place: placeName(bot.target) }) : t("nowhere"),
703
+ checked: isHere,
704
+ run: () => run(isHere ? client.unbind(bot) : client.bindHere(bot, target))
705
+ };
706
+ }),
707
+ {
708
+ key: "add-weixin",
709
+ label: t("addWeixin"),
710
+ icon: /* @__PURE__ */ React.createElement(Plus, {
711
+ size: 16
712
+ }),
713
+ group: t("groupAdd"),
714
+ run: () => dialogs.open({ kind: "weixin", target, from: conversation.sessionId })
715
+ },
716
+ {
717
+ key: "add-wecom",
718
+ label: t("addWecom"),
719
+ icon: /* @__PURE__ */ React.createElement(Plus, {
720
+ size: 16
721
+ }),
722
+ group: t("groupAdd"),
723
+ run: () => dialogs.open({ kind: "wecom", target, from: conversation.sessionId })
724
+ }
725
+ ]
726
+ }
727
+ ];
728
+ },
729
+ above: ({ conversation }) => /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement(BotStrip, {
730
+ client,
731
+ conversation
732
+ }), /* @__PURE__ */ React.createElement(Dialogs, {
733
+ client,
734
+ dialogs,
735
+ from: conversation.sessionId
736
+ }))
737
+ };
738
+ }
739
+ function BotStrip({ client, conversation }) {
740
+ const { bots } = useBots(client);
741
+ const target = targetOf(conversation);
742
+ const here = target ? bots.filter((bot) => BotTargetUtil.same(bot.target, target)) : [];
743
+ if (here.length === 0) {
744
+ return null;
745
+ }
746
+ const X = icon("X");
747
+ return /* @__PURE__ */ React.createElement("ul", {
748
+ className: "tbo-strip"
749
+ }, here.map((bot) => /* @__PURE__ */ React.createElement("li", {
750
+ key: bot.id,
751
+ className: "tbo-chip",
752
+ title: `${kindLabel(bot.kind)} · ${bot.name}${bot.error ? `
753
+ ${bot.error}` : ""}`
754
+ }, /* @__PURE__ */ React.createElement(KindMark, {
755
+ kind: bot.kind,
756
+ size: 16
757
+ }), /* @__PURE__ */ React.createElement("span", {
758
+ className: "tbo-chip-name"
759
+ }, bot.name), /* @__PURE__ */ React.createElement("span", {
760
+ className: `tbo-dot tbo-${bot.status}`
761
+ }), bot.status !== "online" && /* @__PURE__ */ React.createElement("span", {
762
+ className: "tbo-chip-state"
763
+ }, statusLabel(bot.status)), /* @__PURE__ */ React.createElement("button", {
764
+ type: "button",
765
+ className: "tbo-chip-x",
766
+ "aria-label": `${t("disconnect")} ${bot.name}`,
767
+ title: t("disconnect"),
768
+ onClick: () => run(client.unbind(bot))
769
+ }, /* @__PURE__ */ React.createElement(X, {
770
+ size: 12
771
+ })))));
772
+ }
773
+ function Dialogs({ client, dialogs, from }) {
774
+ const dialog = dialogs.use();
775
+ const close = React.useCallback(() => dialogs.open(undefined), [dialogs]);
776
+ if (!dialog || dialog.from !== from) {
777
+ return null;
778
+ }
779
+ return dialog.kind === "weixin" ? /* @__PURE__ */ React.createElement(WeixinDialog, {
780
+ client,
781
+ target: dialog.target,
782
+ onClose: close
783
+ }) : /* @__PURE__ */ React.createElement(WecomDialog, {
784
+ client,
785
+ target: dialog.target,
786
+ onClose: close
787
+ });
788
+ }
789
+
790
+ // web-src/pictures.tsx
791
+ function BotsPicture() {
792
+ return /* @__PURE__ */ React.createElement(pi.ui.Picture, {
793
+ width: 200,
794
+ height: 96
795
+ }, /* @__PURE__ */ React.createElement("rect", {
796
+ className: "ta-pic-pane",
797
+ x: "22",
798
+ y: "12",
799
+ width: "44",
800
+ height: "72",
801
+ rx: "8"
802
+ }), /* @__PURE__ */ React.createElement("rect", {
803
+ className: "ta-pic-accent",
804
+ x: "28",
805
+ y: "26",
806
+ width: "26",
807
+ height: "9",
808
+ rx: "4.5"
809
+ }), /* @__PURE__ */ React.createElement("rect", {
810
+ className: "ta-pic-line",
811
+ x: "36",
812
+ y: "41",
813
+ width: "24",
814
+ height: "9",
815
+ rx: "4.5"
816
+ }), /* @__PURE__ */ React.createElement("rect", {
817
+ className: "ta-pic-soft",
818
+ x: "28",
819
+ y: "56",
820
+ width: "20",
821
+ height: "9",
822
+ rx: "4.5"
823
+ }), /* @__PURE__ */ React.createElement("path", {
824
+ className: "ta-pic-path",
825
+ d: "M74 48h44"
826
+ }), /* @__PURE__ */ React.createElement("circle", {
827
+ className: "ta-pic-mark",
828
+ cx: "96",
829
+ cy: "48",
830
+ r: "3"
831
+ }), /* @__PURE__ */ React.createElement("rect", {
832
+ className: "ta-pic-pane",
833
+ x: "126",
834
+ y: "20",
835
+ width: "56",
836
+ height: "56",
837
+ rx: "8"
838
+ }), /* @__PURE__ */ React.createElement("rect", {
839
+ className: "ta-pic-line",
840
+ x: "134",
841
+ y: "32",
842
+ width: "40",
843
+ height: "5",
844
+ rx: "2.5"
845
+ }), /* @__PURE__ */ React.createElement("rect", {
846
+ className: "ta-pic-line",
847
+ x: "134",
848
+ y: "43",
849
+ width: "30",
850
+ height: "5",
851
+ rx: "2.5"
852
+ }), /* @__PURE__ */ React.createElement("rect", {
853
+ className: "ta-pic-box",
854
+ x: "134",
855
+ y: "56",
856
+ width: "40",
857
+ height: "12",
858
+ rx: "6"
859
+ }));
860
+ }
861
+
862
+ // web-src/bots-view.tsx
863
+ var PAGE = "page";
864
+ function run2(action) {
865
+ action.catch((error) => pi.ui.notify(t("failedAction", { message: message(error) }), "error"));
866
+ }
867
+ function when(time) {
868
+ return new Date(time).toLocaleString(pi.ui.language, { month: "numeric", day: "numeric", hour: "2-digit", minute: "2-digit" });
869
+ }
870
+ function ConnectButtons({ dialogs }) {
871
+ const Plus = icon("Plus");
872
+ return /* @__PURE__ */ React.createElement("div", {
873
+ className: "tbo-connect-buttons"
874
+ }, /* @__PURE__ */ React.createElement("button", {
875
+ type: "button",
876
+ className: "tbo-button",
877
+ onClick: () => dialogs.open({ kind: "weixin", from: PAGE })
878
+ }, /* @__PURE__ */ React.createElement(KindMark, {
879
+ kind: "weixin",
880
+ size: 16
881
+ }), t("kindWeixin"), /* @__PURE__ */ React.createElement(Plus, {
882
+ size: 14
883
+ })), /* @__PURE__ */ React.createElement("button", {
884
+ type: "button",
885
+ className: "tbo-button",
886
+ onClick: () => dialogs.open({ kind: "wecom", from: PAGE })
887
+ }, /* @__PURE__ */ React.createElement(KindMark, {
888
+ kind: "wecom",
889
+ size: 16
890
+ }), t("kindWecom"), /* @__PURE__ */ React.createElement(Plus, {
891
+ size: 14
892
+ })));
893
+ }
894
+ function BotRow({ bot, client, dialogs }) {
895
+ const Pencil = icon("Pencil");
896
+ const Trash = icon("Trash2");
897
+ const Unplug = icon("Unplug");
898
+ const Refresh = icon("RefreshCw");
899
+ const rename = async () => {
900
+ const name = await pi.ui.input(t("rename"), bot.name, { value: bot.name });
901
+ if (name?.trim()) {
902
+ run2(client.call("rename", bot.id, name.trim()).then(() => client.load()));
903
+ }
904
+ };
905
+ const remove = async () => {
906
+ 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()));
908
+ }
909
+ };
910
+ return /* @__PURE__ */ React.createElement("div", {
911
+ className: "tbo-row"
912
+ }, /* @__PURE__ */ React.createElement(KindMark, {
913
+ kind: bot.kind,
914
+ size: 32
915
+ }), /* @__PURE__ */ React.createElement("div", {
916
+ className: "tbo-row-main"
917
+ }, /* @__PURE__ */ React.createElement("div", {
918
+ className: "tbo-row-title"
919
+ }, /* @__PURE__ */ React.createElement("span", {
920
+ className: "tbo-row-name"
921
+ }, bot.name), /* @__PURE__ */ React.createElement("span", {
922
+ className: "tbo-row-kind"
923
+ }, kindLabel(bot.kind), bot.account ? ` · ${bot.account}` : "")), /* @__PURE__ */ React.createElement("div", {
924
+ className: "tbo-row-sub"
925
+ }, /* @__PURE__ */ React.createElement("span", {
926
+ className: `tbo-dot tbo-${bot.status}`
927
+ }), /* @__PURE__ */ React.createElement("span", {
928
+ title: bot.error
929
+ }, statusLabel(bot.status)), /* @__PURE__ */ React.createElement("span", {
930
+ className: "tbo-from-sep"
931
+ }, "·"), /* @__PURE__ */ React.createElement("span", {
932
+ className: bot.target ? "" : "tbo-muted"
933
+ }, bot.target ? t("elsewhere", { place: placeName(bot.target) }) : t("nowhere")), bot.lastMessageAt && /* @__PURE__ */ React.createElement("span", {
934
+ className: "tbo-muted tbo-row-time"
935
+ }, t("lastMessage", { time: when(bot.lastMessageAt) })))), /* @__PURE__ */ React.createElement("div", {
936
+ className: "tbo-row-actions"
937
+ }, bot.kind === "weixin" && (bot.status === "expired" || bot.status === "error") && /* @__PURE__ */ React.createElement("button", {
938
+ type: "button",
939
+ className: "tbo-icon",
940
+ title: t("tryAgain"),
941
+ "aria-label": t("tryAgain"),
942
+ onClick: () => dialogs.open({ kind: "weixin", target: bot.target, from: PAGE })
943
+ }, /* @__PURE__ */ React.createElement(Refresh, {
944
+ size: 15
945
+ })), !BotTargetUtil.same(bot.target, { kind: "double" }) && /* @__PURE__ */ React.createElement("button", {
946
+ 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", {
950
+ type: "button",
951
+ className: "tbo-icon",
952
+ title: t("disconnect"),
953
+ "aria-label": t("disconnect"),
954
+ onClick: () => run2(client.unbind(bot))
955
+ }, /* @__PURE__ */ React.createElement(Unplug, {
956
+ size: 15
957
+ })), /* @__PURE__ */ React.createElement("button", {
958
+ type: "button",
959
+ className: "tbo-icon",
960
+ title: t("rename"),
961
+ "aria-label": t("rename"),
962
+ onClick: () => void rename()
963
+ }, /* @__PURE__ */ React.createElement(Pencil, {
964
+ size: 15
965
+ })), /* @__PURE__ */ React.createElement("button", {
966
+ type: "button",
967
+ className: "tbo-icon tbo-danger",
968
+ title: t("remove"),
969
+ "aria-label": t("remove"),
970
+ onClick: () => void remove()
971
+ }, /* @__PURE__ */ React.createElement(Trash, {
972
+ size: 15
973
+ }))));
974
+ }
975
+ function BotsPage({ client, dialogs }) {
976
+ const { bots, loaded } = useBots(client);
977
+ return /* @__PURE__ */ React.createElement("div", {
978
+ className: "tbo-page"
979
+ }, !loaded ? /* @__PURE__ */ React.createElement(pi.ui.Skeleton, {
980
+ rows: 3
981
+ }) : bots.length === 0 ? /* @__PURE__ */ React.createElement(pi.ui.Empty, {
982
+ title: t("emptyTitle"),
983
+ hint: t("emptyHint"),
984
+ picture: /* @__PURE__ */ React.createElement(BotsPicture, null)
985
+ }, /* @__PURE__ */ React.createElement(ConnectButtons, {
986
+ dialogs
987
+ })) : /* @__PURE__ */ React.createElement(React.Fragment, null, /* @__PURE__ */ React.createElement("div", {
988
+ className: "tbo-page-head"
989
+ }, /* @__PURE__ */ React.createElement("span", {
990
+ className: "tbo-muted"
991
+ }, t("pageHint")), /* @__PURE__ */ React.createElement(ConnectButtons, {
992
+ dialogs
993
+ })), /* @__PURE__ */ React.createElement("div", {
994
+ className: "tbo-list"
995
+ }, bots.map((bot) => /* @__PURE__ */ React.createElement(BotRow, {
996
+ key: bot.id,
997
+ bot,
998
+ client,
999
+ dialogs
1000
+ })))), /* @__PURE__ */ React.createElement(Dialogs, {
1001
+ client,
1002
+ dialogs,
1003
+ from: PAGE
1004
+ }));
1005
+ }
1006
+
1007
+ // web-src/message.tsx
1008
+ var ATTACHMENT_ICON = { image: "Image", voice: "Mic", file: "Paperclip", video: "Film" };
1009
+ function Thumb({ file, cwd }) {
1010
+ const [url, setUrl] = React.useState();
1011
+ const [failed, setFailed] = React.useState(false);
1012
+ React.useEffect(() => {
1013
+ let alive = true;
1014
+ if (file.path) {
1015
+ pi.workspace.fileUrl(file.path, cwd).then((value) => alive && setUrl(value), () => alive && setFailed(true));
1016
+ }
1017
+ return () => {
1018
+ alive = false;
1019
+ };
1020
+ }, [file.path, cwd]);
1021
+ if (!file.path || failed || !url) {
1022
+ return /* @__PURE__ */ React.createElement(FileChip, {
1023
+ file
1024
+ });
1025
+ }
1026
+ return /* @__PURE__ */ React.createElement("img", {
1027
+ className: "tbo-thumb",
1028
+ src: url,
1029
+ alt: file.name,
1030
+ loading: "lazy",
1031
+ onError: () => setFailed(true)
1032
+ });
1033
+ }
1034
+ function FileChip({ file }) {
1035
+ const Icon = icon(ATTACHMENT_ICON[file.kind]);
1036
+ return /* @__PURE__ */ React.createElement("span", {
1037
+ className: "tbo-file",
1038
+ title: file.path ?? file.name
1039
+ }, /* @__PURE__ */ React.createElement(Icon, {
1040
+ size: 14
1041
+ }), /* @__PURE__ */ React.createElement("span", {
1042
+ className: "tbo-file-name"
1043
+ }, file.kind === "voice" ? t("voice") : file.name), file.size !== undefined && /* @__PURE__ */ React.createElement("span", {
1044
+ className: "tbo-file-size"
1045
+ }, size(file.size)));
1046
+ }
1047
+ function size(bytes) {
1048
+ return bytes < 1024 ? `${bytes} B` : bytes < 1024 * 1024 ? `${Math.round(bytes / 1024)} KB` : `${(bytes / 1024 / 1024).toFixed(1)} MB`;
1049
+ }
1050
+ function InboundMessage({ message: message2 }) {
1051
+ const details = message2.details;
1052
+ const cwd = pi.ui.useCwd();
1053
+ if (!details) {
1054
+ return null;
1055
+ }
1056
+ const pictures = details.attachments.filter((file) => file.kind === "image");
1057
+ const others = details.attachments.filter((file) => file.kind !== "image");
1058
+ return /* @__PURE__ */ React.createElement("div", {
1059
+ className: `ta-message ta-user tbo-in tbo-in-${details.kind}`
1060
+ }, /* @__PURE__ */ React.createElement("div", {
1061
+ className: "tbo-from",
1062
+ title: `${kindLabel(details.kind)} · ${details.botName}`
1063
+ }, /* @__PURE__ */ React.createElement(KindMark, {
1064
+ kind: details.kind,
1065
+ size: 14
1066
+ }), /* @__PURE__ */ React.createElement("span", {
1067
+ className: "tbo-from-bot"
1068
+ }, details.botName), details.group && /* @__PURE__ */ React.createElement("span", {
1069
+ className: "tbo-from-tag"
1070
+ }, t("group")), details.group && /* @__PURE__ */ React.createElement("span", {
1071
+ className: "tbo-from-who"
1072
+ }, details.from.name || details.from.id)), pictures.length > 0 && /* @__PURE__ */ React.createElement("div", {
1073
+ className: "tbo-thumbs"
1074
+ }, pictures.map((file, index) => /* @__PURE__ */ React.createElement(Thumb, {
1075
+ key: index,
1076
+ file,
1077
+ cwd
1078
+ }))), details.text && /* @__PURE__ */ React.createElement("div", {
1079
+ className: "ta-bubble tbo-bubble"
1080
+ }, details.text), others.length > 0 && /* @__PURE__ */ React.createElement("div", {
1081
+ className: "tbo-files"
1082
+ }, others.map((file, index) => /* @__PURE__ */ React.createElement(FileChip, {
1083
+ key: index,
1084
+ file
1085
+ }))));
1086
+ }
1087
+ function SentMessageView({ message: message2 }) {
1088
+ const details = message2.details;
1089
+ const Back = icon("CornerUpLeft");
1090
+ return /* @__PURE__ */ React.createElement("div", {
1091
+ className: "tbo-sent"
1092
+ }, (details?.sent ?? []).map((item, index) => /* @__PURE__ */ React.createElement("div", {
1093
+ key: index,
1094
+ className: `tbo-sent-line ${item.error ? "tbo-bad" : ""}`
1095
+ }, /* @__PURE__ */ React.createElement(Back, {
1096
+ size: 13
1097
+ }), /* @__PURE__ */ React.createElement(KindMark, {
1098
+ kind: item.kind,
1099
+ size: 14
1100
+ }), /* @__PURE__ */ React.createElement("span", null, item.error ? t("sendFailed", { name: item.botName, error: item.error }) : item.files?.length ? t("sentFile", { file: item.files.join(", "), name: item.botName }) : t("sentTo", { name: item.botName })))));
1101
+ }
1102
+
1103
+ // web-src/style.ts
1104
+ var STYLE = `
1105
+ .tbo-mark { flex: none; display: inline-flex; align-items: center; justify-content: center; border-radius: 30%; color: #fff; }
1106
+ .tbo-mark-weixin { background: var(--ta-green); }
1107
+ .tbo-mark-wecom { background: var(--ta-blue); }
1108
+ .tbo-dot { flex: none; width: 7px; height: 7px; border-radius: 50%; background: var(--ta-border-field); transition: background 200ms; }
1109
+ .tbo-online { background: var(--ta-green); }
1110
+ .tbo-connecting { background: var(--ta-yellow); animation: tbo-fade 1.4s ease-in-out infinite; }
1111
+ .tbo-expired, .tbo-error { background: var(--ta-red); }
1112
+ @keyframes tbo-fade { 50% { opacity: .35; } }
1113
+ @keyframes tbo-in { from { opacity: 0; transform: translateY(4px); } }
1114
+ .tbo-muted { color: var(--ta-muted); }
1115
+ .tbo-bad { color: var(--ta-red); }
1116
+
1117
+ /* Above the message box: the bots connected here. */
1118
+ .tbo-strip { display: flex; margin: 0; list-style: none; gap: 6px; padding: 0 2px 8px; overflow-x: auto; scrollbar-width: none; }
1119
+ .tbo-strip::-webkit-scrollbar { display: none; }
1120
+ .tbo-chip { flex: none; display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 4px 0 6px; border: 1px solid var(--ta-border-muted); border-radius: 14px; background: var(--ta-card); color: var(--ta-secondary); font-size: calc(12.5px * var(--ta-font-scale)); animation: tbo-in 200ms var(--ta-ease) both; }
1121
+ .tbo-chip-name { max-width: 140px; overflow: hidden; color: var(--ta-text); font-weight: 500; text-overflow: ellipsis; white-space: nowrap; }
1122
+ .tbo-chip-state { color: var(--ta-muted); font-size: calc(11.5px * var(--ta-font-scale)); }
1123
+ .tbo-chip-x { display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px; margin: -4px; padding: 4px; box-sizing: content-box; border: 0; border-radius: 50%; background: none; color: var(--ta-muted); cursor: pointer; background-clip: content-box; }
1124
+ .tbo-chip-x:hover { background-color: var(--ta-hover); color: var(--ta-text); }
1125
+
1126
+ /* A message of a bot, on the side of the user. */
1127
+ .tbo-in { animation: tbo-in 220ms var(--ta-ease) both; }
1128
+ .tbo-from { display: flex; align-items: center; gap: 5px; color: var(--ta-muted); font-size: calc(12px * var(--ta-font-scale)); line-height: calc(16px * var(--ta-font-scale)); }
1129
+ .tbo-from-bot { color: var(--ta-secondary); font-weight: 500; }
1130
+ .tbo-from-sep { color: var(--ta-border-field); }
1131
+ .tbo-from-tag { padding: 0 5px; border-radius: 4px; background: var(--ta-chip); font-size: calc(11px * var(--ta-font-scale)); }
1132
+ .tbo-from-who::before { content: '·'; margin-right: 5px; color: var(--ta-border-field); }
1133
+ .tbo-in-weixin .tbo-bubble { box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--ta-green) 28%, transparent); }
1134
+ .tbo-in-wecom .tbo-bubble { box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--ta-blue) 28%, transparent); }
1135
+ .tbo-thumbs, .tbo-files { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; max-width: 78%; }
1136
+ .tbo-thumb { display: block; width: 120px; height: 120px; object-fit: cover; border-radius: 12px; border: 1px solid var(--ta-border); background: var(--ta-raised); }
1137
+ .tbo-file { display: inline-flex; align-items: center; gap: 6px; max-width: 260px; height: 30px; padding: 0 10px; border: 1px solid var(--ta-border-muted); border-radius: 10px; background: var(--ta-card); color: var(--ta-secondary); font-size: calc(12.5px * var(--ta-font-scale)); }
1138
+ .tbo-file-name { overflow: hidden; color: var(--ta-text); text-overflow: ellipsis; white-space: nowrap; }
1139
+ .tbo-file-size { flex: none; color: var(--ta-muted); font-size: calc(11.5px * var(--ta-font-scale)); }
1140
+ .tbo-sent { display: flex; flex-direction: column; gap: 2px; margin: -4px 0 4px; }
1141
+ .tbo-sent-line { display: flex; align-items: center; gap: 6px; color: var(--ta-muted); font-size: calc(12px * var(--ta-font-scale)); line-height: calc(18px * var(--ta-font-scale)); animation: tbo-in 200ms var(--ta-ease) both; }
1142
+ .tbo-sent-line.tbo-bad { color: var(--ta-red); }
1143
+
1144
+ /* Dialogs. */
1145
+ .tbo-connect { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 4px 0 2px; }
1146
+ .tbo-qr { position: relative; display: flex; align-items: center; justify-content: center; width: 220px; height: 220px; border-radius: 14px; background: var(--ta-raised); }
1147
+ .tbo-qr .ta-qr { transition: opacity 200ms, filter 200ms; }
1148
+ .tbo-qr-dim .ta-qr { opacity: .25; filter: blur(2px); }
1149
+ .tbo-qr-wait { color: var(--ta-muted); font-size: calc(13px * var(--ta-font-scale)); }
1150
+ .tbo-qr-done { display: inline-flex; align-items: center; justify-content: center; width: 72px; height: 72px; border-radius: 50%; background: var(--ta-green); color: #fff; animation: tbo-pop 320ms var(--ta-ease) both; }
1151
+ @keyframes tbo-pop { from { opacity: 0; transform: scale(.7); } }
1152
+ .tbo-qr-again { position: absolute; inset: auto auto 50% 50%; transform: translate(-50%, 50%); height: 32px; padding: 0 14px; border: 0; border-radius: 16px; background: var(--ta-brand); color: var(--ta-brand-text); font: inherit; font-size: calc(13px * var(--ta-font-scale)); cursor: pointer; }
1153
+ .tbo-qr-image { width: 204px; height: 204px; border-radius: 6px; -webkit-touch-callout: default; -webkit-user-select: auto; user-select: auto; }
1154
+ .tbo-phone-steps { display: flex; flex-direction: column; gap: 12px; width: 100%; }
1155
+ .tbo-steps { margin: 0; padding-left: 20px; color: var(--ta-secondary); font-size: calc(13px * var(--ta-font-scale)); line-height: 1.7; }
1156
+ .tbo-actions-wide { justify-content: stretch; margin-top: 0; }
1157
+ .tbo-actions-wide .tbo-button { flex: 1; justify-content: center; height: 40px; }
1158
+ .tbo-connect-status { min-height: 20px; color: var(--ta-text); font-size: calc(14px * var(--ta-font-scale)); text-align: center; }
1159
+ .tbo-note { margin: 0; color: var(--ta-muted); font-size: calc(12.5px * var(--ta-font-scale)); line-height: 1.55; text-align: center; }
1160
+ .tbo-form .tbo-note { text-align: left; }
1161
+ .tbo-code { display: flex; gap: 8px; }
1162
+ .tbo-code .tbo-input { width: 120px; text-align: center; letter-spacing: 4px; font-size: calc(16px * var(--ta-font-scale)); }
1163
+ .tbo-form { display: flex; flex-direction: column; gap: 12px; min-width: min(380px, 80vw); }
1164
+ .tbo-field { display: flex; flex-direction: column; gap: 5px; color: var(--ta-secondary); font-size: calc(12.5px * var(--ta-font-scale)); }
1165
+ .tbo-input { box-sizing: border-box; height: 34px; padding: 0 10px; border: 1px solid var(--ta-border-field); border-radius: 8px; background: var(--ta-input); color: var(--ta-text); font: inherit; font-size: calc(14px * var(--ta-font-scale)); outline: none; }
1166
+ .tbo-input:focus { border-color: var(--ta-border-focus); }
1167
+ .tbo-link { padding: 4px 2px; border: 0; background: none; color: var(--ta-secondary); font: inherit; font-size: calc(12.5px * var(--ta-font-scale)); text-decoration: underline; text-underline-offset: 3px; cursor: pointer; }
1168
+ .tbo-link:hover { color: var(--ta-text); }
1169
+ .tbo-actions-start { margin-right: auto; }
1170
+ .tbo-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; margin-top: 4px; }
1171
+ .tbo-button { display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px; border: 1px solid var(--ta-border); border-radius: 8px; background: transparent; color: var(--ta-text); font: inherit; font-size: calc(13px * var(--ta-font-scale)); font-weight: 500; cursor: pointer; transition: background 120ms, opacity 120ms, transform 120ms var(--ta-ease); }
1172
+ .tbo-button:hover:not(:disabled) { background: var(--ta-hover); }
1173
+ .tbo-button:active:not(:disabled) { transform: scale(.98); }
1174
+ .tbo-button:disabled { opacity: .45; cursor: default; }
1175
+ .tbo-primary { background: var(--ta-brand); border-color: var(--ta-brand); color: var(--ta-brand-text); }
1176
+ .tbo-primary:hover:not(:disabled) { background: var(--ta-brand); opacity: .88; }
1177
+ .tbo-small { height: 28px; padding: 0 10px; font-size: calc(12.5px * var(--ta-font-scale)); }
1178
+ .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
+ .tbo-icon:hover { background: var(--ta-hover); color: var(--ta-text); }
1180
+ .tbo-danger:hover { color: var(--ta-red); }
1181
+
1182
+ /* The page of the bots. */
1183
+ .tbo-page { box-sizing: border-box; display: flex; flex-direction: column; width: 100%; max-width: 860px; min-height: 100%; margin: 0 auto; padding: 20px 24px 48px; }
1184
+ .tbo-page-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; font-size: calc(13px * var(--ta-font-scale)); }
1185
+ .tbo-connect-buttons { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
1186
+ .tbo-list { display: flex; flex-direction: column; gap: 8px; }
1187
+ .tbo-row { display: flex; align-items: center; gap: 12px; padding: 12px 10px 12px 14px; border: 1px solid var(--ta-border-muted); border-radius: 12px; background: var(--ta-card); animation: tbo-in 200ms var(--ta-ease) both; }
1188
+ .tbo-row:hover { border-color: var(--ta-border); }
1189
+ .tbo-row-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
1190
+ .tbo-row-title { display: flex; align-items: baseline; gap: 8px; min-width: 0; }
1191
+ .tbo-row-name { overflow: hidden; font-size: calc(14px * var(--ta-font-scale)); font-weight: 600; text-overflow: ellipsis; white-space: nowrap; }
1192
+ .tbo-row-kind { flex: none; color: var(--ta-muted); font-size: calc(12px * var(--ta-font-scale)); }
1193
+ .tbo-row-sub { display: flex; align-items: center; gap: 6px; min-width: 0; color: var(--ta-secondary); font-size: calc(12.5px * var(--ta-font-scale)); white-space: nowrap; }
1194
+ .tbo-row-sub > span { overflow: hidden; text-overflow: ellipsis; }
1195
+ .tbo-row-time { margin-left: 4px; }
1196
+ .tbo-row-actions { flex: none; display: flex; align-items: center; gap: 2px; }
1197
+ [data-layout='compact'] .tbo-page { padding: 14px 12px 40px; }
1198
+ [data-layout='compact'] .tbo-page-head { flex-direction: column; align-items: stretch; }
1199
+ [data-layout='compact'] .tbo-row { flex-wrap: wrap; }
1200
+ [data-layout='compact'] .tbo-row-actions { width: 100%; justify-content: flex-end; }
1201
+ [data-layout='compact'] .tbo-row-time { display: none; }
1202
+ [data-touch] .tbo-chip-x { width: 24px; height: 24px; }
1203
+ @media (prefers-reduced-motion: reduce) { .tbo-connecting, .tbo-chip, .tbo-in, .tbo-row, .tbo-sent-line, .tbo-qr-done { animation: none; } }
1204
+ `;
1205
+ function injectStyle() {
1206
+ const element = document.createElement("style");
1207
+ element.dataset.taPackage = "ta-bots";
1208
+ element.textContent = STYLE;
1209
+ document.head.appendChild(element);
1210
+ return () => element.remove();
1211
+ }
1212
+
1213
+ // web-src/index.tsx
1214
+ function activate(pi2) {
1215
+ bind(pi2.React);
1216
+ bindPage(pi2);
1217
+ const removeStyle = injectStyle();
1218
+ const client = new BotsClient;
1219
+ const dialogs = new DialogState;
1220
+ client.start();
1221
+ pi2.registerComposerAddon(composerAddon(client, dialogs));
1222
+ pi2.registerMessageRenderer(INBOUND_TYPE, InboundMessage);
1223
+ pi2.registerMessageRenderer(SENT_TYPE, SentMessageView);
1224
+ pi2.registerView("bots", {
1225
+ title: () => t("title"),
1226
+ icon: icon("Bot"),
1227
+ render: () => /* @__PURE__ */ React.createElement(BotsPage, {
1228
+ client,
1229
+ dialogs
1230
+ }),
1231
+ order: 75,
1232
+ discover: true
1233
+ });
1234
+ return () => {
1235
+ client.dispose();
1236
+ removeStyle();
1237
+ };
1238
+ }
1239
+ export {
1240
+ activate as default
1241
+ };