@celljs/ta-channels 4.22.0 → 4.23.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/host/index.mjs CHANGED
@@ -37,113 +37,6 @@ var TaLanguageUtil;
37
37
  TaLanguageUtil2.list = list;
38
38
  })(TaLanguageUtil || (TaLanguageUtil = {}));
39
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) => Object.prototype.hasOwnProperty.call(host2, key) ? host2[key] : undefined;
57
- 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;
58
- }
59
- TaPermissionUtil2.host = host;
60
- })(TaPermissionUtil || (TaPermissionUtil = {}));
61
- var action = (id, en, zh, tw, ja, more = {}) => ({
62
- id,
63
- area: id.split(".")[0],
64
- title: { en, "zh-CN": zh, ...tw ? { "zh-TW": tw } : {}, ...ja ? { "ja-JP": ja } : {} },
65
- ...more
66
- });
67
- var TA_ACTIONS = [
68
- action("chat.view", "See conversations", "查看对话", "檢視對話", "会話を確認"),
69
- action("chat.send", "Send in conversations", "在对话里发消息", "在對話裡發訊息", "会話にメッセージを送信"),
70
- action("chat.create", "Start conversations", "新建对话", "新建對話", "新しい会話を開始"),
71
- action("chat.stop", "Stop a reply", "停止回复", "停止回覆", "返信を停止"),
72
- action("chat.manage", "Rename, archive and close conversations", "改名、归档、关闭对话", "改名、歸檔、關閉對話", "会話の名前変更、アーカイブ、クローズ"),
73
- action("chat.share", "Share by a link", "生成分享链接", "生成分享連結", "共有リンクで共有"),
74
- action("agent.view", "See agents", "查看智能体", "檢視智慧體", "エージェントを確認"),
75
- action("agent.use", "Talk with agents", "和智能体对话", "和智慧體對話", "エージェントと対話"),
76
- action("agent.edit", "Create, change and remove agents", "新建、修改、删除智能体", "新建、修改、刪除智慧體", "エージェントを作成・変更・削除"),
77
- action("channel.view", "See channels", "查看频道", "檢視頻道", "チャネルを確認"),
78
- action("channel.post", "Post in channels", "在频道里发言", "在頻道里發言", "チャネルに投稿"),
79
- action("channel.manage", "Create and change channels", "新建、管理频道", "新建、管理頻道", "チャネルを作成・管理"),
80
- action("task.view", "See tasks", "查看任务", "檢視任務", "タスクを確認"),
81
- action("task.create", "Hand out tasks", "派任务", "派任務", "タスクを割り当て"),
82
- action("task.manage", "Take over and stop tasks", "接管、停止任务", "接管、停止任務", "タスクを引き継ぎ・停止"),
83
- action("files.read", "Read files", "读文件", "讀檔案", "ファイルを読取", { scoped: true }),
84
- action("files.write", "Change files", "改文件", "改檔案", "ファイルを変更", { scoped: true }),
85
- action("commands.run", "Let Ta run commands", "让 Ta 运行命令", "讓 Ta 執行命令", "Ta にコマンド実行を許可", { scoped: true, risky: true }),
86
- action("commands.terminal", "Open terminals", "打开终端", "開啟終端機", "ターミナルを開く", { scoped: true, risky: true }),
87
- action("hosts.use", "Use remote hosts", "使用远程主机", "使用遠端主機", "リモートホストを使用", { risky: true }),
88
- action("models.use", "Use the models", "使用模型", "使用模型", "モデルを使用"),
89
- action("models.manage", "Sign in to models", "登录和更换模型账号", "登入和更換模型帳號", "モデルアカウントにサインイン・変更", { grant: "single" }),
90
- action("cron.view", "See scheduled tasks", "查看定时任务", "檢視定時任務", "スケジュールタスクを確認"),
91
- action("cron.manage", "Change scheduled tasks", "管理定时任务", "管理定時任務", "スケジュールタスクを管理"),
92
- action("extensions.use", "Use extensions", "使用扩展", "使用擴充功能", "拡張機能を使用"),
93
- action("extensions.install", "Install and turn off extensions", "安装、停用扩展", "安裝、停用擴充功能", "拡張機能をインストール・無効化"),
94
- action("settings.view", "See settings", "查看设置", "檢視設定", "設定を確認"),
95
- action("settings.manage", "Change settings", "修改设置", "修改設定", "設定を変更"),
96
- action("members.view", "See members", "查看成员", "檢視成員", "メンバーを確認"),
97
- action("members.invite", "Invite people", "邀请别人", "邀請別人", "メンバーを招待"),
98
- action("members.approve", "Answer requests", "处理申请", "處理申請", "リクエストに対応"),
99
- action("members.roles", "Change roles and members", "管理角色和成员", "管理角色和成員", "ロールとメンバーを管理"),
100
- action("audit.view", "See the record", "查看记录", "檢視記錄", "レコードを確認")
101
- ];
102
- var TA_OWNER_ACTIONS = [
103
- action("desktop.view", "See remote desktops", "查看远程桌面", "檢視遠端桌面", "リモートデスクトップを見る", { risky: true }),
104
- action("desktop.control", "Work remote desktops", "操作远程桌面", "操作遠端桌面", "リモートデスクトップを操作", { risky: true })
105
- ];
106
- var OWNER_AREAS = ["desktop"];
107
- var TaActionUtil;
108
- (function(TaActionUtil2) {
109
- const byId = new Map(TA_ACTIONS.map((one) => [one.id, one]));
110
- const titled = new Map([...TA_ACTIONS, ...TA_OWNER_ACTIONS].map((one) => [one.id, one]));
111
- function info(id) {
112
- return byId.get(id);
113
- }
114
- TaActionUtil2.info = info;
115
- function title(id, language) {
116
- var _a;
117
- var _b, _c;
118
- const title2 = (_a = titled.get(id)) === null || _a === undefined ? undefined : _a.title;
119
- if (!title2) {
120
- return id;
121
- }
122
- return TaLanguageUtil.pick(language, {
123
- "zh-CN": title2["zh-CN"],
124
- "zh-TW": (_b = title2["zh-TW"]) !== null && _b !== undefined ? _b : title2["zh-CN"],
125
- "en-US": title2.en,
126
- "ja-JP": (_c = title2["ja-JP"]) !== null && _c !== undefined ? _c : title2.en
127
- });
128
- }
129
- TaActionUtil2.title = title;
130
- function grantable(id) {
131
- const info2 = byId.get(id);
132
- return !!info2 && !OWNER_AREAS.includes(info2.area);
133
- }
134
- TaActionUtil2.grantable = grantable;
135
- function inRoles(id) {
136
- var _a;
137
- return grantable(id) && ((_a = byId.get(id)) === null || _a === undefined ? undefined : _a.grant) !== "single";
138
- }
139
- TaActionUtil2.inRoles = inRoles;
140
- function inside(dir, path) {
141
- const base = dir.replace(/\/+$/, "");
142
- return path === base || path.startsWith(`${base}/`) || base === "";
143
- }
144
- TaActionUtil2.inside = inside;
145
- })(TaActionUtil || (TaActionUtil = {}));
146
-
147
40
  // ../ta-protocol/esm/common/message-protocol.js
148
41
  var TaMessageContext;
149
42
  (function(TaMessageContext2) {
@@ -229,6 +122,11 @@ var TaImageNote;
229
122
  var TaImageSize;
230
123
  (function(TaImageSize2) {
231
124
  const JPEG_BYTES = 256 * 1024;
125
+ function type(bytes) {
126
+ const at = (offset, ...values) => values.every((value, index) => bytes[offset + index] === value);
127
+ 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;
128
+ }
129
+ TaImageSize2.type = type;
232
130
  function of(base64) {
233
131
  var _a, _b, _c;
234
132
  let bytes;
@@ -816,6 +714,9 @@ var BUDGETS = {
816
714
  long: { tokens: 3000000, replies: 100 }
817
715
  };
818
716
  var DEFAULT_BUDGET = "normal";
717
+ var REPLIES = ["mentioned", "lead", "all"];
718
+ var DEFAULT_REPLY = "lead";
719
+ var PASS = "PASS";
819
720
  var WRAP_UP_AT = 0.7;
820
721
  var EXTEND_BY = 0.5;
821
722
  var WATCH_MS = 30000;
@@ -833,6 +734,16 @@ var CARD_DAYS = 30;
833
734
  // web-src/channels-util.ts
834
735
  var ChannelsUtil;
835
736
  ((ChannelsUtil) => {
737
+ function may(team) {
738
+ const given = team.may;
739
+ return {
740
+ create: !given || given.create,
741
+ agent: (id) => !given || given.agents.includes(id),
742
+ manage: (id) => !given || given.manage.includes(id),
743
+ edit: (id) => !given || given.edit.includes(id)
744
+ };
745
+ }
746
+ ChannelsUtil.may = may;
836
747
  function newRound() {
837
748
  return { total: 0, tokens: 0, stopped: false };
838
749
  }
@@ -1102,16 +1013,22 @@ var ChannelsUtil;
1102
1013
  delete channel.pinned;
1103
1014
  }
1104
1015
  }
1105
- if (input.budget !== undefined) {
1106
- if (typeof input.budget !== "string" || !(input.budget in BUDGETS)) {
1107
- throw new Error(`The budget of a channel is one of ${Object.keys(BUDGETS).join(", ")}.`);
1016
+ const choose = (key, choices, fallback, what) => {
1017
+ const value = input[key];
1018
+ if (value === undefined) {
1019
+ return;
1108
1020
  }
1109
- if (input.budget === DEFAULT_BUDGET) {
1110
- delete channel.budget;
1021
+ if (typeof value !== "string" || !choices.includes(value)) {
1022
+ throw new Error(`${what} is one of ${choices.join(", ")}.`);
1023
+ }
1024
+ if (value === fallback) {
1025
+ delete channel[key];
1111
1026
  } else {
1112
- channel.budget = input.budget;
1027
+ channel[key] = value;
1113
1028
  }
1114
- }
1029
+ };
1030
+ choose("budget", Object.keys(BUDGETS), DEFAULT_BUDGET, "The budget of a channel");
1031
+ choose("reply", REPLIES, DEFAULT_REPLY, "Who answers what names nobody in a channel");
1115
1032
  if (input.dailyLimit === null) {
1116
1033
  delete channel.dailyLimit;
1117
1034
  } else if (input.dailyLimit !== undefined) {
@@ -1611,18 +1528,38 @@ var ChannelsText;
1611
1528
  }
1612
1529
  ChannelsText.mentions = mentions;
1613
1530
  function targets(team, channel, message) {
1614
- const muted = channel.muted ?? [];
1615
- const members = channel.members.filter((id) => !muted.includes(id)).map((id) => team.agents.find((agent) => agent.id === id)).filter((agent) => !!agent);
1531
+ const members = answering(team, channel);
1616
1532
  const named = mentions(message.text, members).filter((id) => id !== message.author);
1617
1533
  if (message.author === SYSTEM) {
1618
1534
  return [];
1619
1535
  }
1620
- if (named.length > 0 || message.author !== TA) {
1536
+ if (named.length > 0 || message.author !== TA && message.author !== USER || mentions(message.text, team.agents).length > 0) {
1621
1537
  return named;
1622
1538
  }
1623
- return members.length > 0 ? [members[0].id] : [];
1539
+ const reply = message.author === TA ? "lead" : replyOf(channel);
1540
+ if (reply === "all") {
1541
+ return members.map((agent) => agent.id);
1542
+ }
1543
+ return reply === "lead" && members.length > 0 ? [members[0].id] : [];
1624
1544
  }
1625
1545
  ChannelsText.targets = targets;
1546
+ function replyOf(channel) {
1547
+ return channel?.reply && REPLIES.includes(channel.reply) ? channel.reply : DEFAULT_REPLY;
1548
+ }
1549
+ ChannelsText.replyOf = replyOf;
1550
+ function passes(text) {
1551
+ return text.replace(/[\s.。!!*_`"'“”]/g, "").toUpperCase() === PASS;
1552
+ }
1553
+ ChannelsText.passes = passes;
1554
+ function answering(team, channel) {
1555
+ const muted = channel.muted ?? [];
1556
+ return channel.members.filter((id) => !muted.includes(id)).map((id) => team.agents.find((agent) => agent.id === id)).filter((agent) => !!agent);
1557
+ }
1558
+ function mayPass(team, channel, message, agent) {
1559
+ const members = answering(team, channel);
1560
+ return message.author === USER && replyOf(channel) === "all" && members[0]?.id !== agent.id && mentions(message.text, team.agents).length === 0;
1561
+ }
1562
+ ChannelsText.mayPass = mayPass;
1626
1563
  function authorName(team, author, labels) {
1627
1564
  if (author === SYSTEM) {
1628
1565
  return "channel";
@@ -1657,18 +1594,29 @@ var ChannelsText;
1657
1594
  lines.push("", "Notice of the channel (follow it; keep it with update_channel_notice):", channel.notice.text);
1658
1595
  }
1659
1596
  if (before.length > 0) {
1660
- lines.push("", "Recent messages:", ...before.map((item) => `${authorName(team, item.author, labels)}: ${item.text}`));
1597
+ lines.push("", "Recent messages:", ...before.map((item) => `${authorName(team, item.author, labels)}: ${said(item)}`));
1661
1598
  }
1662
1599
  if (options.closing) {
1663
1600
  lines.push("", CLOSING_NOTE);
1664
1601
  }
1602
+ if (mayPass(team, channel, message, agent)) {
1603
+ lines.push("", PASS_NOTE);
1604
+ }
1665
1605
  lines.push("", `${authorName(team, message.author, labels)} to you:`);
1666
1606
  const speaker = message.author === USER ? "" : ` · ${authorName(team, message.author, labels)}`;
1667
1607
  lines.push(TaMessageSource.line(NAME, `#${channel.name}${speaker}`));
1608
+ if (message.images?.length) {
1609
+ lines.push(`(${message.images.length > 1 ? `${message.images.length} pictures` : "A picture"} sent with it, attached.)`);
1610
+ }
1668
1611
  return TaMessageContext.wrap(lines.join(`
1669
1612
  `), message.text);
1670
1613
  }
1671
1614
  ChannelsText.prompt = prompt;
1615
+ function said(message) {
1616
+ const pictures = message.images?.length ? `[${message.images.length > 1 ? `${message.images.length} pictures` : "picture"}]` : "";
1617
+ return [pictures, message.text].filter(Boolean).join(" ");
1618
+ }
1619
+ ChannelsText.said = said;
1672
1620
  function summary(team) {
1673
1621
  if (team.agents.length === 0) {
1674
1622
  return "No agents and no channels yet.";
@@ -1683,7 +1631,7 @@ var ChannelsText;
1683
1631
  ChannelsText.summary = summary;
1684
1632
  function transcript(team, messages) {
1685
1633
  const labels = { user: "user", ta: "Ta" };
1686
- return messages.map((item) => `${authorName(team, item.author, labels)}${item.failed ? " (failed)" : ""}: ${item.text}`).join(`
1634
+ return messages.map((item) => `${authorName(team, item.author, labels)}${item.failed ? " (failed)" : ""}: ${said(item)}`).join(`
1687
1635
 
1688
1636
  `);
1689
1637
  }
@@ -1727,6 +1675,7 @@ var ChannelsText;
1727
1675
  }
1728
1676
  })(ChannelsText ||= {});
1729
1677
  var CLOSING_NOTE = "This discussion has spent most of what the user allows it before they look in: conclude now. Hand on only if the work cannot go on without that member; the lead sums up what was concluded and what the user is to decide.";
1678
+ var PASS_NOTE = `The user named nobody, and every member got this message: answer only if your role has something of its own to add; the lead answers for the channel. Else answer exactly ${PASS}, which is not posted.`;
1730
1679
  var NOTICE_RULE = [
1731
1680
  "A channel may have a notice, in the <context> block: what its members decided, agreed and are to do. Follow it.",
1732
1681
  `When the talk settles a decision or a convention, or adds, assigns or finishes a to-do, update it with update_channel_notice: the whole notice, short, under the headings ### ${NOTICE_HEADINGS.zh.join(" / ")} (### ${NOTICE_HEADINGS.en.join(" / ")} in English), in the language of the channel, to-dos as - [ ] or - [x] with who does them. The lead keeps it most; leave it for small talk.`
@@ -2931,6 +2880,7 @@ var TaRemoteServer = Symbol("TaRemoteServer");
2931
2880
  // ../ta-protocol/esm/common/room-protocol.js
2932
2881
  var TaRoomServer = Symbol("TaRoomServer");
2933
2882
  var TA_PICTURE = { key: "picture:", maxChars: 16 * 1024 };
2883
+ var TA_CHAT_KEY = "chat:";
2934
2884
  var TaRoomWatchUtil;
2935
2885
  (function(TaRoomWatchUtil2) {
2936
2886
  function of(messages, state) {
@@ -2958,6 +2908,9 @@ var TaRoomWatchUtil;
2958
2908
  if (event.type === "message") {
2959
2909
  return watch.messages;
2960
2910
  }
2911
+ if (event.type === "chat") {
2912
+ return watch.messages || watch.state.some((prefix) => TA_CHAT_KEY.startsWith(prefix) || prefix.startsWith(TA_CHAT_KEY));
2913
+ }
2961
2914
  if (event.type === "state") {
2962
2915
  return watch.messages || watch.state.some((prefix) => event.key.startsWith(prefix));
2963
2916
  }
@@ -3021,6 +2974,112 @@ var TaSlashCommandUtil;
3021
2974
  }
3022
2975
  TaSlashCommandUtil2.description = description;
3023
2976
  })(TaSlashCommandUtil || (TaSlashCommandUtil = {}));
2977
+ // ../ta-protocol/esm/common/team-protocol.js
2978
+ var TA_OWNER = "owner";
2979
+ var TA_UNDECLARED = "extensions.install";
2980
+ var TaPermissionUtil;
2981
+ (function(TaPermissionUtil2) {
2982
+ function read(pi) {
2983
+ const given = pi === null || pi === undefined ? undefined : pi.permissions;
2984
+ const map = (value) => value && typeof value === "object" && !Array.isArray(value) ? Object.fromEntries(Object.entries(value).filter((entry) => typeof entry[1] === "string")) : undefined;
2985
+ const host2 = map(given === null || given === undefined ? undefined : given.host);
2986
+ const tools = map(given === null || given === undefined ? undefined : given.tools);
2987
+ return { ...host2 ? { host: host2 } : {}, ...tools ? { tools } : {} };
2988
+ }
2989
+ TaPermissionUtil2.read = read;
2990
+ function host(manifest, method, kind) {
2991
+ var _a, _b, _c, _d;
2992
+ const host2 = (_a = manifest === null || manifest === undefined ? undefined : manifest.host) !== null && _a !== undefined ? _a : {};
2993
+ const own = (key) => Object.prototype.hasOwnProperty.call(host2, key) ? host2[key] : undefined;
2994
+ 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;
2995
+ }
2996
+ TaPermissionUtil2.host = host;
2997
+ })(TaPermissionUtil || (TaPermissionUtil = {}));
2998
+ var action = (id, en, zh, tw, ja, more = {}) => ({
2999
+ id,
3000
+ area: id.split(".")[0],
3001
+ title: { en, "zh-CN": zh, ...tw ? { "zh-TW": tw } : {}, ...ja ? { "ja-JP": ja } : {} },
3002
+ ...more
3003
+ });
3004
+ var TA_ACTIONS = [
3005
+ action("chat.view", "See conversations", "查看对话", "檢視對話", "会話を確認"),
3006
+ action("chat.send", "Send in conversations", "在对话里发消息", "在對話裡發訊息", "会話にメッセージを送信"),
3007
+ action("chat.create", "Start conversations", "新建对话", "新建對話", "新しい会話を開始"),
3008
+ action("chat.stop", "Stop a reply", "停止回复", "停止回覆", "返信を停止"),
3009
+ action("chat.manage", "Rename, archive and close conversations", "改名、归档、关闭对话", "改名、歸檔、關閉對話", "会話の名前変更、アーカイブ、クローズ"),
3010
+ action("chat.share", "Share by a link", "生成分享链接", "生成分享連結", "共有リンクで共有"),
3011
+ action("agent.view", "See agents", "查看智能体", "檢視智慧體", "エージェントを確認"),
3012
+ action("agent.use", "Talk with agents", "和智能体对话", "和智慧體對話", "エージェントと対話"),
3013
+ action("agent.edit", "Create, change and remove agents", "新建、修改、删除智能体", "新建、修改、刪除智慧體", "エージェントを作成・変更・削除"),
3014
+ action("channel.view", "See channels", "查看频道", "檢視頻道", "チャネルを確認"),
3015
+ action("channel.post", "Post in channels", "在频道里发言", "在頻道里發言", "チャネルに投稿"),
3016
+ action("channel.manage", "Create and change channels", "新建、管理频道", "新建、管理頻道", "チャネルを作成・管理"),
3017
+ action("task.view", "See tasks", "查看任务", "檢視任務", "タスクを確認"),
3018
+ action("task.create", "Hand out tasks", "派任务", "派任務", "タスクを割り当て"),
3019
+ action("task.manage", "Take over and stop tasks", "接管、停止任务", "接管、停止任務", "タスクを引き継ぎ・停止"),
3020
+ action("files.read", "Read files", "读文件", "讀檔案", "ファイルを読取", { scoped: true }),
3021
+ action("files.write", "Change files", "改文件", "改檔案", "ファイルを変更", { scoped: true }),
3022
+ action("commands.run", "Let Ta run commands", "让 Ta 运行命令", "讓 Ta 執行命令", "Ta にコマンド実行を許可", { scoped: true, risky: true }),
3023
+ action("commands.terminal", "Open terminals", "打开终端", "開啟終端機", "ターミナルを開く", { scoped: true, risky: true }),
3024
+ action("hosts.use", "Use remote hosts", "使用远程主机", "使用遠端主機", "リモートホストを使用", { risky: true }),
3025
+ action("models.use", "Use the models", "使用模型", "使用模型", "モデルを使用"),
3026
+ action("models.manage", "Sign in to models", "登录和更换模型账号", "登入和更換模型帳號", "モデルアカウントにサインイン・変更", { grant: "single" }),
3027
+ action("cron.view", "See scheduled tasks", "查看定时任务", "檢視定時任務", "スケジュールタスクを確認"),
3028
+ action("cron.manage", "Change scheduled tasks", "管理定时任务", "管理定時任務", "スケジュールタスクを管理"),
3029
+ action("extensions.use", "Use extensions", "使用扩展", "使用擴充功能", "拡張機能を使用"),
3030
+ action("extensions.install", "Install and turn off extensions", "安装、停用扩展", "安裝、停用擴充功能", "拡張機能をインストール・無効化"),
3031
+ action("settings.view", "See settings", "查看设置", "檢視設定", "設定を確認"),
3032
+ action("settings.manage", "Change settings", "修改设置", "修改設定", "設定を変更"),
3033
+ action("members.view", "See members", "查看成员", "檢視成員", "メンバーを確認"),
3034
+ action("members.invite", "Invite people", "邀请别人", "邀請別人", "メンバーを招待"),
3035
+ action("members.approve", "Answer requests", "处理申请", "處理申請", "リクエストに対応"),
3036
+ action("members.roles", "Change roles and members", "管理角色和成员", "管理角色和成員", "ロールとメンバーを管理"),
3037
+ action("audit.view", "See the record", "查看记录", "檢視記錄", "レコードを確認")
3038
+ ];
3039
+ var TA_OWNER_ACTIONS = [
3040
+ action("desktop.view", "See remote desktops", "查看远程桌面", "檢視遠端桌面", "リモートデスクトップを見る", { risky: true }),
3041
+ action("desktop.control", "Work remote desktops", "操作远程桌面", "操作遠端桌面", "リモートデスクトップを操作", { risky: true })
3042
+ ];
3043
+ var OWNER_AREAS = ["desktop"];
3044
+ var TaActionUtil;
3045
+ (function(TaActionUtil2) {
3046
+ const byId = new Map(TA_ACTIONS.map((one) => [one.id, one]));
3047
+ const titled = new Map([...TA_ACTIONS, ...TA_OWNER_ACTIONS].map((one) => [one.id, one]));
3048
+ function info(id) {
3049
+ return byId.get(id);
3050
+ }
3051
+ TaActionUtil2.info = info;
3052
+ function title(id, language) {
3053
+ var _a;
3054
+ var _b, _c;
3055
+ const title2 = (_a = titled.get(id)) === null || _a === undefined ? undefined : _a.title;
3056
+ if (!title2) {
3057
+ return id;
3058
+ }
3059
+ return TaLanguageUtil.pick(language, {
3060
+ "zh-CN": title2["zh-CN"],
3061
+ "zh-TW": (_b = title2["zh-TW"]) !== null && _b !== undefined ? _b : title2["zh-CN"],
3062
+ "en-US": title2.en,
3063
+ "ja-JP": (_c = title2["ja-JP"]) !== null && _c !== undefined ? _c : title2.en
3064
+ });
3065
+ }
3066
+ TaActionUtil2.title = title;
3067
+ function grantable(id) {
3068
+ const info2 = byId.get(id);
3069
+ return !!info2 && !OWNER_AREAS.includes(info2.area);
3070
+ }
3071
+ TaActionUtil2.grantable = grantable;
3072
+ function inRoles(id) {
3073
+ var _a;
3074
+ return grantable(id) && ((_a = byId.get(id)) === null || _a === undefined ? undefined : _a.grant) !== "single";
3075
+ }
3076
+ TaActionUtil2.inRoles = inRoles;
3077
+ function inside(dir, path) {
3078
+ const base = dir.replace(/\/+$/, "");
3079
+ return path === base || path.startsWith(`${base}/`) || base === "";
3080
+ }
3081
+ TaActionUtil2.inside = inside;
3082
+ })(TaActionUtil || (TaActionUtil = {}));
3024
3083
  // ../ta-protocol/esm/common/usage-protocol.js
3025
3084
  var TaUsageServer = Symbol("TaUsageServer");
3026
3085
  // ../ta-protocol/esm/common/voice-protocol.js
@@ -3088,12 +3147,100 @@ var TaExcludedFiles;
3088
3147
  TaExcludedFiles2.filter = filter;
3089
3148
  })(TaExcludedFiles || (TaExcludedFiles = {}));
3090
3149
  var TA_UPLOAD_PART_BYTES = 4 * 1024 * 1024;
3150
+ // web-src/channel-pictures.ts
3151
+ import { randomBytes } from "node:crypto";
3152
+ import { mkdir, readdir, readFile, rm, writeFile } from "node:fs/promises";
3153
+ import { join } from "node:path";
3154
+ var CHANNEL_IMAGES = 9;
3155
+ var CHANNEL_IMAGE_BYTES = 2 * 1024 * 1024;
3156
+ var EXTENSIONS = { "image/jpeg": "jpg", "image/png": "png", "image/gif": "gif", "image/webp": "webp" };
3157
+ var SAFE_ID = /^[A-Za-z0-9_-]{1,80}$/;
3158
+ var PICTURE_ID = /^[a-f0-9]{24}$/;
3159
+
3160
+ class ChannelPictures {
3161
+ agentDir;
3162
+ constructor(agentDir) {
3163
+ this.agentDir = agentDir;
3164
+ }
3165
+ root() {
3166
+ const dir = this.agentDir();
3167
+ return dir ? join(dir, "ta-channels", "pictures") : undefined;
3168
+ }
3169
+ file(channel, image) {
3170
+ const root = this.root();
3171
+ const extension = EXTENSIONS[image.type];
3172
+ return root && SAFE_ID.test(channel) && PICTURE_ID.test(image.id) && extension ? join(root, channel, `${image.id}.${extension}`) : undefined;
3173
+ }
3174
+ async put(channel, inputs) {
3175
+ if (!Array.isArray(inputs) || !inputs.length) {
3176
+ return [];
3177
+ }
3178
+ if (inputs.length > CHANNEL_IMAGES) {
3179
+ throw new Error(`At most ${CHANNEL_IMAGES} pictures at once.`);
3180
+ }
3181
+ const images = [];
3182
+ for (const input of inputs) {
3183
+ const bytes = new Uint8Array(Buffer.from(String(input?.data ?? ""), "base64"));
3184
+ const type = TaImageSize.type(bytes);
3185
+ if (!type || bytes.length > CHANNEL_IMAGE_BYTES) {
3186
+ throw new Error("Not a picture, or too large.");
3187
+ }
3188
+ const size = TaImageSize.of(String(input?.data)) ?? { width: Number(input?.width) || 1, height: Number(input?.height) || 1 };
3189
+ const image = { id: randomBytes(12).toString("hex"), type, w: size.width, h: size.height };
3190
+ const file = this.file(channel, image);
3191
+ if (!file) {
3192
+ throw new Error("The pictures cannot be kept here.");
3193
+ }
3194
+ await mkdir(join(file, ".."), { recursive: true });
3195
+ await writeFile(file, bytes);
3196
+ images.push(image);
3197
+ }
3198
+ return images;
3199
+ }
3200
+ async read(channel, image) {
3201
+ const file = this.file(channel, image);
3202
+ const bytes = file ? await readFile(file).catch(() => {
3203
+ return;
3204
+ }) : undefined;
3205
+ return bytes?.toString("base64");
3206
+ }
3207
+ async images(channel, images) {
3208
+ const read = [];
3209
+ for (const image of images ?? []) {
3210
+ const data = await this.read(channel, image);
3211
+ if (data) {
3212
+ read.push({ type: "image", data, mimeType: image.type });
3213
+ }
3214
+ }
3215
+ return read;
3216
+ }
3217
+ async keepOnly(channel, named) {
3218
+ const root = this.root();
3219
+ if (!root || !SAFE_ID.test(channel)) {
3220
+ return;
3221
+ }
3222
+ const ids = new Set(named.map((image) => image.id));
3223
+ for (const name of await readdir(join(root, channel)).catch(() => [])) {
3224
+ if (!ids.has(name.replace(/\.[a-z]+$/, ""))) {
3225
+ await rm(join(root, channel, name), { force: true });
3226
+ }
3227
+ }
3228
+ }
3229
+ async forget(channel) {
3230
+ const root = this.root();
3231
+ if (root && SAFE_ID.test(channel)) {
3232
+ await rm(join(root, channel), { recursive: true, force: true });
3233
+ }
3234
+ }
3235
+ }
3236
+
3091
3237
  // web-src/team-view.ts
3092
3238
  var TeamViewUtil;
3093
3239
  ((TeamViewUtil) => {
3094
3240
  function visible(team2, can) {
3095
3241
  const channels = new Set(team2.channels.filter((channel) => can("channel", channel.id)).map((channel) => channel.id));
3096
- const agents = new Set(team2.agents.filter((agent) => can("agent", agent.id)).map((agent) => agent.id));
3242
+ const own = new Set(team2.agents.filter((agent) => can("agent", agent.id)).map((agent) => agent.id));
3243
+ const agents = new Set(own);
3097
3244
  for (const channel of team2.channels) {
3098
3245
  if (channels.has(channel.id)) {
3099
3246
  for (const member of channel.members) {
@@ -3101,20 +3248,50 @@ var TeamViewUtil;
3101
3248
  }
3102
3249
  }
3103
3250
  }
3104
- return { agents, channels };
3251
+ return { agents, channels, own };
3105
3252
  }
3106
3253
  TeamViewUtil.visible = visible;
3107
- function team(value, visible2) {
3254
+ function team(value, visible2, can) {
3108
3255
  if (!value) {
3109
3256
  return value;
3110
3257
  }
3258
+ const may = { create: can("agent.edit"), agents: [], manage: [], edit: [] };
3259
+ const agents = value.agents.filter((agent) => visible2.agents.has(agent.id)).map((agent) => {
3260
+ const edits = can("agent.edit", { kind: "agent", id: agent.id });
3261
+ if (edits) {
3262
+ may.agents.push(agent.id);
3263
+ }
3264
+ return agentAs(agent, edits, visible2.own.has(agent.id));
3265
+ });
3266
+ const channels = value.channels.filter((channel) => visible2.channels.has(channel.id)).map((channel) => {
3267
+ const resource = { kind: "channel", id: channel.id };
3268
+ const manages = can("channel.manage", resource);
3269
+ if (manages) {
3270
+ may.manage.push(channel.id);
3271
+ }
3272
+ if (can("agent.edit", resource)) {
3273
+ may.edit.push(channel.id);
3274
+ }
3275
+ return manages ? channel : channelAs(channel);
3276
+ });
3277
+ return { ...value, agents, channels, may };
3278
+ }
3279
+ TeamViewUtil.team = team;
3280
+ function agentAs(agent, edits, own) {
3281
+ const { model, cwd, sessionFile, ...rest } = agent;
3111
3282
  return {
3112
- ...value,
3113
- agents: value.agents.filter((agent) => visible2.agents.has(agent.id)),
3114
- channels: value.channels.filter((channel) => visible2.channels.has(channel.id))
3283
+ ...rest,
3284
+ ...edits && model !== undefined ? { model } : {},
3285
+ ...own && cwd !== undefined ? { cwd } : {},
3286
+ ...own && sessionFile !== undefined ? { sessionFile } : {}
3115
3287
  };
3116
3288
  }
3117
- TeamViewUtil.team = team;
3289
+ function channelAs(channel) {
3290
+ const rest = { ...channel };
3291
+ delete rest.budget;
3292
+ delete rest.dailyLimit;
3293
+ return rest;
3294
+ }
3118
3295
  function byId(value, seen) {
3119
3296
  if (!value || typeof value !== "object" || Array.isArray(value)) {
3120
3297
  return value;
@@ -3141,6 +3318,7 @@ class TeamStore {
3141
3318
  watched = new Map;
3142
3319
  options;
3143
3320
  disposed = false;
3321
+ pictures = new ChannelPictures(() => this.host.agentDir);
3144
3322
  constructor(host, options = {}) {
3145
3323
  this.host = host;
3146
3324
  this.options = { pollMs: 300, timeoutMs: 15 * 60 * 1000, ...options };
@@ -3300,9 +3478,13 @@ class TeamStore {
3300
3478
  return message;
3301
3479
  }
3302
3480
  await this.messagesOf(channelId);
3303
- const messages = ChannelsUtil.append(this.messages.get(channelId) ?? [], message);
3481
+ const before = this.messages.get(channelId) ?? [];
3482
+ const messages = ChannelsUtil.append(before, message);
3304
3483
  this.messages.set(channelId, messages);
3305
3484
  await this.set(Keys.messages(channelId), messages);
3485
+ if (before.length >= messages.length && before.some((item) => item.images?.length && !messages.includes(item))) {
3486
+ await this.pictures.keepOnly(channelId, messages.flatMap((item) => item.images ?? []));
3487
+ }
3306
3488
  return message;
3307
3489
  }
3308
3490
  async replace(channelId, message) {
@@ -3310,6 +3492,23 @@ class TeamStore {
3310
3492
  this.messages.set(channelId, messages);
3311
3493
  await this.set(Keys.messages(channelId), messages);
3312
3494
  }
3495
+ poster() {
3496
+ const principal = this.host.team?.current();
3497
+ if (!principal || principal === TA_OWNER) {
3498
+ return {};
3499
+ }
3500
+ return { by: principal, byName: this.host.team?.describe(principal)?.name ?? principal };
3501
+ }
3502
+ async posterOf(request) {
3503
+ const channel = ChannelsUtil.findChannel(this.team, String(request.channel ?? ""));
3504
+ const images = channel ? await this.pictures.put(channel.id, request.images) : [];
3505
+ return { ...this.poster(), ...images.length ? { images } : {} };
3506
+ }
3507
+ async picture(channelId, id) {
3508
+ const image = (await this.messagesOf(channelId)).flatMap((item) => item.images ?? []).find((each) => each.id === id);
3509
+ const data = image && await this.pictures.read(channelId, image);
3510
+ return image && data ? `data:${image.type};base64,${data}` : "";
3511
+ }
3313
3512
  async saveTeam(team) {
3314
3513
  this.team = team;
3315
3514
  this.linkSessions();
@@ -3326,7 +3525,7 @@ class TeamStore {
3326
3525
  view: (principal, key, value) => {
3327
3526
  const visible = TeamViewUtil.visible(this.team, (kind, id) => can(principal, kind, id));
3328
3527
  if (key === Keys.TEAM) {
3329
- return TeamViewUtil.team(value, visible);
3528
+ return TeamViewUtil.team(value, visible, (action2, resource) => team.can(principal, action2, resource));
3330
3529
  }
3331
3530
  if (key.startsWith(Keys.MESSAGES)) {
3332
3531
  return visible.channels.has(key.slice(Keys.MESSAGES.length)) ? value : undefined;
@@ -3428,6 +3627,10 @@ class TeamAnswers extends TeamStore {
3428
3627
  await this.setAnswered(agentId, Date.now());
3429
3628
  await this.setWorking(channel.id, agentId, false);
3430
3629
  }
3630
+ const agent = this.team.agents.find((candidate) => candidate.id === agentId);
3631
+ if (!failed && !over && agent && ChannelsText.passes(text) && ChannelsText.mayPass(this.team, channel, message, agent)) {
3632
+ return { tokens, changed };
3633
+ }
3431
3634
  const posted = await this.append(channel.id, {
3432
3635
  id: ChannelsUtil.newId("message"),
3433
3636
  author: agentId,
@@ -3451,7 +3654,8 @@ class TeamAnswers extends TeamStore {
3451
3654
  const history = await this.messagesOf(channel.id);
3452
3655
  const input = {
3453
3656
  clientMessageId: `${message.id}:${agent.id}`,
3454
- message: ChannelsText.prompt(this.team, channel, history, message, agent, { closing })
3657
+ message: ChannelsText.prompt(this.team, channel, history, message, agent, { closing }),
3658
+ ...message.images?.length ? { images: await this.pictures.images(channel.id, message.images) } : {}
3455
3659
  };
3456
3660
  await this.host.prompt(sessionId, input);
3457
3661
  const cap = (await this.limits()).answer;
@@ -3667,6 +3871,7 @@ class TeamService extends TeamAnswers {
3667
3871
  return agent;
3668
3872
  }
3669
3873
  async forget(channel) {
3874
+ await this.pictures.forget(channel.id);
3670
3875
  this.messages.delete(channel.id);
3671
3876
  this.reading.delete(channel.id);
3672
3877
  await this.set(Keys.messages(channel.id), null);
@@ -3689,20 +3894,13 @@ class TeamService extends TeamAnswers {
3689
3894
  async focus(channelId) {
3690
3895
  await this.set(Keys.FOCUS, { channelId, at: Date.now() });
3691
3896
  }
3692
- poster() {
3693
- const principal = this.host.team?.current();
3694
- if (!principal || principal === TA_OWNER) {
3695
- return {};
3696
- }
3697
- return { by: principal, byName: this.host.team?.describe(principal)?.name ?? principal };
3698
- }
3699
3897
  async post(channelRef, author, text, poster = {}) {
3700
3898
  const channel = ChannelsUtil.findChannel(this.team, channelRef);
3701
3899
  if (!channel) {
3702
3900
  const known = this.team.channels.map((other) => `#${other.name}`).join(", ") || "none yet";
3703
3901
  throw new Error(`No channel is named ${channelRef}. The channels: ${known}.`);
3704
3902
  }
3705
- if (!text.trim()) {
3903
+ if (!text.trim() && !poster.images?.length) {
3706
3904
  throw new Error("The message is empty.");
3707
3905
  }
3708
3906
  const message = await this.append(channel.id, { id: ChannelsUtil.newId("message"), author, text: text.trim(), at: Date.now(), ...poster });
@@ -3776,7 +3974,9 @@ class TeamService extends TeamAnswers {
3776
3974
  case "deleteAgent":
3777
3975
  return this.deleteAgent(request.agent);
3778
3976
  case "post":
3779
- return (await this.post(String(request.channel ?? ""), USER, String(request.text ?? ""), this.poster())).message;
3977
+ return (await this.post(String(request.channel ?? ""), USER, String(request.text ?? ""), await this.posterOf(request))).message;
3978
+ case "picture":
3979
+ return this.picture(String(request.channel ?? ""), String(request.id ?? ""));
3780
3980
  case "resumeRound":
3781
3981
  return this.resumeRound(request.channel, request.message, request.how, this.poster());
3782
3982
  case "watch":
@@ -3897,7 +4097,9 @@ class TeamService extends TeamAnswers {
3897
4097
  if (answered.changed) {
3898
4098
  this.progressed(channel.id, job.round);
3899
4099
  }
3900
- this.checkIdle(job.round, reply);
4100
+ if (reply) {
4101
+ this.checkIdle(job.round, reply);
4102
+ }
3901
4103
  }
3902
4104
  if (this.disposed) {
3903
4105
  return;