@celljs/ta-channels 4.15.2 → 4.17.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,4 +1,5 @@
1
1
  // ../ta-protocol/esm/common/ta-extension-host.js
2
+ var TA_WEBHOOK_MAX_BYTES = 256 * 1024;
2
3
  var TA_HOST_EVENT = "ta:host";
3
4
  var TaHostUtil;
4
5
  (function(TaHostUtil2) {
@@ -127,18 +128,18 @@ var ChannelsUtil;
127
128
  return { total: 0, byAgent: {}, stopped: false };
128
129
  }
129
130
  ChannelsUtil.newRound = newRound;
130
- function limit(round, targets2, hops) {
131
+ function limit(round, targets, hops) {
131
132
  const allowed = [];
132
133
  const byAgent = { ...round.byAgent };
133
134
  let total = round.total;
134
- for (const id of targets2) {
135
+ for (const id of targets) {
135
136
  if (hops < MAX_HOPS && total < MAX_REPLIES && (byAgent[id] ?? 0) < MAX_AGENT_REPLIES) {
136
137
  allowed.push(id);
137
138
  byAgent[id] = (byAgent[id] ?? 0) + 1;
138
139
  total++;
139
140
  }
140
141
  }
141
- const cut = allowed.length < targets2.length;
142
+ const cut = allowed.length < targets.length;
142
143
  const note = cut && !round.stopped;
143
144
  return { allowed, round: { total, byAgent, stopped: round.stopped || cut }, cut, note };
144
145
  }
@@ -483,68 +484,6 @@ var ChannelsUtil;
483
484
  }
484
485
  return ids;
485
486
  }
486
- function markMentions(text, agents) {
487
- if (agents.length === 0) {
488
- return text;
489
- }
490
- const names = [...agents].sort((a, b) => b.name.length - a.name.length).map((agent) => agent.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
491
- const pattern = new RegExp(`(?<![*A-Za-z0-9_])@(${names.join("|")})(?![A-Za-z0-9_-])`, "gi");
492
- return text.split(/(```[\s\S]*?(?:```|$)|`[^`\n]*`)/).map((part, index) => index % 2 === 1 ? part : part.replace(pattern, "**$&**")).join("");
493
- }
494
- ChannelsUtil.markMentions = markMentions;
495
- function mentions(text, agents) {
496
- if (agents.length === 0) {
497
- return [];
498
- }
499
- const names = [...agents].sort((a, b) => b.name.length - a.name.length).map((agent) => agent.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
500
- const pattern = new RegExp(`@(${names.join("|")})(?![A-Za-z0-9_-])`, "gi");
501
- const found = [];
502
- for (const match of text.matchAll(pattern)) {
503
- const agent = agents.find((candidate) => candidate.name.toLowerCase() === match[1].toLowerCase());
504
- if (agent && !found.includes(agent.id)) {
505
- found.push(agent.id);
506
- }
507
- }
508
- return found;
509
- }
510
- ChannelsUtil.mentions = mentions;
511
- function targets(team, channel, message) {
512
- const muted = channel.muted ?? [];
513
- const members = channel.members.filter((id) => !muted.includes(id)).map((id) => team.agents.find((agent) => agent.id === id)).filter((agent) => !!agent);
514
- const named = mentions(message.text, members).filter((id) => id !== message.author);
515
- if (message.author === SYSTEM) {
516
- return [];
517
- }
518
- if (named.length > 0 || message.author !== USER && message.author !== TA) {
519
- return named;
520
- }
521
- return members.length > 0 ? [members[0].id] : [];
522
- }
523
- ChannelsUtil.targets = targets;
524
- function authorName(team, author, labels) {
525
- if (author === SYSTEM) {
526
- return "channel";
527
- }
528
- if (author === USER) {
529
- return labels.user;
530
- }
531
- if (author === TA) {
532
- return labels.ta;
533
- }
534
- return team.agents.find((agent) => agent.id === author)?.name ?? author;
535
- }
536
- ChannelsUtil.authorName = authorName;
537
- function systemPrompt(agent) {
538
- return [
539
- `You are ${agent.name}, ${AGENT_MARK}. Your role: ${agent.role}`,
540
- "You work with the user, Ta and other agents in channels. A message for you starts with a <context> block: the channel, its members, the recent messages and who speaks to you; answer what follows the block.",
541
- "Do the part of the work your role covers with your tools, then answer in a few sentences: your answer is posted to the channel as it is.",
542
- "To hand work to another member, write @ and their name in your answer; name nobody when the work is done or you need the user.",
543
- NOTICE_RULE
544
- ].join(`
545
- `);
546
- }
547
- ChannelsUtil.systemPrompt = systemPrompt;
548
487
  function setNotice(team, input) {
549
488
  const before = channelOf(team, input.channel);
550
489
  const text = typeof input.text === "string" ? input.text.replace(/\r\n/g, `
@@ -564,139 +503,12 @@ var ChannelsUtil;
564
503
  return { team: { ...team, channels: team.channels.map((other) => other.id === channel.id ? channel : other) }, channel, before, changed: true };
565
504
  }
566
505
  ChannelsUtil.setNotice = setNotice;
567
- function prompt(team, channel, history, message, agent) {
568
- const labels = { user: "user", ta: "Ta" };
569
- const members = channel.members.map((id) => team.agents.find((other) => other.id === id)).filter((other) => !!other).map((other) => `@${other.name}${other.id === agent.id ? " (you)" : ""}: ${firstLine(other.role)}`);
570
- const before = history.filter((item) => item.id !== message.id && !item.failed && !item.notice).slice(-CONTEXT_MESSAGES);
571
- const lines = [`[#${channel.name}]${channel.topic ? ` ${channel.topic}` : ""}`, "Members:", ...members.map((line) => `- ${line}`)];
572
- if (channel.notice) {
573
- lines.push("", "Notice of the channel (follow it; keep it with update_channel_notice):", channel.notice.text);
574
- }
575
- if (before.length > 0) {
576
- lines.push("", "Recent messages:", ...before.map((item) => `${authorName(team, item.author, labels)}: ${item.text}`));
577
- }
578
- lines.push("", `${authorName(team, message.author, labels)} to you:`);
579
- const speaker = message.author === USER ? "" : ` · ${authorName(team, message.author, labels).replace(/[\]\n]/g, " ")}`;
580
- lines.push(`[ta:from ${NAME} #${channel.name}${speaker}]`);
581
- return `<context>
582
- ${lines.join(`
583
- `)}
584
- </context>
585
- ${message.text}`;
586
- }
587
- ChannelsUtil.prompt = prompt;
588
506
  function append(messages, message) {
589
507
  const next = [...messages, message];
590
508
  return next.length > MAX_MESSAGES ? next.slice(next.length - MAX_MESSAGES) : next;
591
509
  }
592
510
  ChannelsUtil.append = append;
593
- function summary(team) {
594
- if (team.agents.length === 0) {
595
- return "No agents and no channels yet.";
596
- }
597
- const name = (id) => team.agents.find((agent) => agent.id === id)?.name ?? id;
598
- const agents = team.agents.map((agent) => `- @${agent.name}: ${firstLine(agent.role)}`);
599
- const channels = team.channels.map((channel) => `- #${channel.name}${channel.topic ? ` (${channel.topic})` : ""}: ${channel.members.map((id) => `@${name(id)}`).join(", ")}${channel.muted?.length ? `; muted by the user, answering nothing: ${channel.muted.map((id) => `@${name(id)}`).join(", ")}` : ""}${channel.notice ? `
600
- notice: ${channel.notice.text.replace(/\s+/g, " ").slice(0, 300)}` : ""}`);
601
- return ["Agents:", ...agents, "", "Channels:", ...channels.length > 0 ? channels : ["none yet"]].join(`
602
- `);
603
- }
604
- ChannelsUtil.summary = summary;
605
- function transcript(team, messages) {
606
- const labels = { user: "user", ta: "Ta" };
607
- return messages.map((item) => `${authorName(team, item.author, labels)}${item.failed ? " (failed)" : ""}: ${item.text}`).join(`
608
-
609
- `);
610
- }
611
- ChannelsUtil.transcript = transcript;
612
- function markdown(agent, messages, labels) {
613
- const parts = [`# ${agent.name}`, "", `> ${firstLine(agent.role)}`];
614
- for (const message of messages) {
615
- const { role, content } = message ?? {};
616
- if (role !== "user" && role !== "assistant") {
617
- continue;
618
- }
619
- const text = (typeof content === "string" ? content : Array.isArray(content) ? content.map((block) => block?.type === "text" ? String(block.text ?? "") : "").join("") : "").replace(/^<context>\n?[\s\S]*?\n?<\/context>\n?/, "").trim();
620
- if (text) {
621
- parts.push("", `## ${role === "user" ? labels.user : agent.name}`, "", text);
622
- }
623
- }
624
- return `${parts.join(`
625
- `)}
626
- `;
627
- }
628
- ChannelsUtil.markdown = markdown;
629
- function channelMarkdown(team, channel, messages, labels) {
630
- const parts = [`# #${channel.name}`];
631
- if (channel.topic) {
632
- parts.push("", `> ${firstLine(channel.topic)}`);
633
- }
634
- for (const message of messages) {
635
- const text = message.text.trim();
636
- if (text && message.author !== SYSTEM) {
637
- parts.push("", `## ${authorName(team, message.author, labels)}${message.failed ? " (failed)" : ""}`, "", text);
638
- }
639
- }
640
- return `${parts.join(`
641
- `)}
642
- `;
643
- }
644
- ChannelsUtil.channelMarkdown = channelMarkdown;
645
- function firstLine(text) {
646
- return text.split(`
647
- `)[0];
648
- }
649
511
  })(ChannelsUtil ||= {});
650
- var NOTICE_RULE = [
651
- "A channel may have a notice, in the <context> block: what its members decided, agreed and are to do. Follow it.",
652
- `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.`
653
- ].join(`
654
- `);
655
- var NoticeUtil;
656
- ((NoticeUtil) => {
657
- const PARTS = {
658
- 决定: "decisions",
659
- decisions: "decisions",
660
- 约定: "conventions",
661
- conventions: "conventions",
662
- 待办: "todo",
663
- "to do": "todo",
664
- todo: "todo",
665
- "to-do": "todo"
666
- };
667
- function counts(text) {
668
- const counts2 = { decisions: 0, conventions: 0, todo: 0, done: 0 };
669
- let part;
670
- for (const line of text.split(/\r?\n/)) {
671
- const heading = /^\s*#{1,6}\s+(.+?)\s*#*\s*$/.exec(line) ?? /^\s*\*\*(.+?)\*\*\s*[::]?\s*$/.exec(line);
672
- if (heading) {
673
- part = PARTS[heading[1].replace(/[::]$/, "").trim().toLowerCase()];
674
- continue;
675
- }
676
- const item = /^\s*(?:[-*+]|\d+[.)])\s+(\[( |x|X)\]\s+)?\S/.exec(line);
677
- if (!part || !item) {
678
- continue;
679
- }
680
- if (part === "todo" && item[2] && item[2] !== " ") {
681
- counts2.done++;
682
- } else {
683
- counts2[part]++;
684
- }
685
- }
686
- return counts2;
687
- }
688
- NoticeUtil.counts = counts;
689
- function firstLine(text) {
690
- for (const line of text.split(/\r?\n/)) {
691
- const item = /^\s*(?:[-*+]|\d+[.)])\s+(?:\[[ xX]\]\s+)?(.+)$/.exec(line);
692
- if (item) {
693
- return item[1].replace(/[*_`~]+/g, "").trim();
694
- }
695
- }
696
- return text.split(/\r?\n/).find((line) => line.trim() && !/^\s*#/.test(line))?.trim() ?? "";
697
- }
698
- NoticeUtil.firstLine = firstLine;
699
- })(NoticeUtil ||= {});
700
512
 
701
513
  // ../ta-protocol/esm/common/language.js
702
514
  var TaLanguageUtil;
@@ -976,6 +788,155 @@ var TeamAsk;
976
788
  TeamAsk.allow = allow;
977
789
  })(TeamAsk ||= {});
978
790
 
791
+ // web-src/channels-text.ts
792
+ var ChannelsText;
793
+ ((ChannelsText) => {
794
+ function markMentions(text, agents) {
795
+ if (agents.length === 0) {
796
+ return text;
797
+ }
798
+ const names = [...agents].sort((a, b) => b.name.length - a.name.length).map((agent) => agent.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
799
+ const pattern = new RegExp(`(?<![*A-Za-z0-9_])@(${names.join("|")})(?![A-Za-z0-9_-])`, "gi");
800
+ return text.split(/(```[\s\S]*?(?:```|$)|`[^`\n]*`)/).map((part, index) => index % 2 === 1 ? part : part.replace(pattern, "**$&**")).join("");
801
+ }
802
+ ChannelsText.markMentions = markMentions;
803
+ function mentions(text, agents) {
804
+ if (agents.length === 0) {
805
+ return [];
806
+ }
807
+ const names = [...agents].sort((a, b) => b.name.length - a.name.length).map((agent) => agent.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
808
+ const pattern = new RegExp(`@(${names.join("|")})(?![A-Za-z0-9_-])`, "gi");
809
+ const found = [];
810
+ for (const match of text.matchAll(pattern)) {
811
+ const agent = agents.find((candidate) => candidate.name.toLowerCase() === match[1].toLowerCase());
812
+ if (agent && !found.includes(agent.id)) {
813
+ found.push(agent.id);
814
+ }
815
+ }
816
+ return found;
817
+ }
818
+ ChannelsText.mentions = mentions;
819
+ function targets(team, channel, message) {
820
+ const muted = channel.muted ?? [];
821
+ const members = channel.members.filter((id) => !muted.includes(id)).map((id) => team.agents.find((agent) => agent.id === id)).filter((agent) => !!agent);
822
+ const named = mentions(message.text, members).filter((id) => id !== message.author);
823
+ if (message.author === SYSTEM) {
824
+ return [];
825
+ }
826
+ if (named.length > 0 || message.author !== USER && message.author !== TA) {
827
+ return named;
828
+ }
829
+ return members.length > 0 ? [members[0].id] : [];
830
+ }
831
+ ChannelsText.targets = targets;
832
+ function authorName(team, author, labels) {
833
+ if (author === SYSTEM) {
834
+ return "channel";
835
+ }
836
+ if (author === USER) {
837
+ return labels.user;
838
+ }
839
+ if (author === TA) {
840
+ return labels.ta;
841
+ }
842
+ return team.agents.find((agent) => agent.id === author)?.name ?? author;
843
+ }
844
+ ChannelsText.authorName = authorName;
845
+ function systemPrompt(agent) {
846
+ return [
847
+ `You are ${agent.name}, ${AGENT_MARK}. Your role: ${agent.role}`,
848
+ "You work with the user, Ta and other agents in channels. A message for you starts with a <context> block: the channel, its members, the recent messages and who speaks to you; answer what follows the block.",
849
+ "Do the part of the work your role covers with your tools, then answer in a few sentences: your answer is posted to the channel as it is.",
850
+ "To hand work to another member, write @ and their name in your answer; name nobody when the work is done or you need the user.",
851
+ NOTICE_RULE
852
+ ].join(`
853
+ `);
854
+ }
855
+ ChannelsText.systemPrompt = systemPrompt;
856
+ function prompt(team, channel, history, message, agent) {
857
+ const labels = { user: "user", ta: "Ta" };
858
+ const members = channel.members.map((id) => team.agents.find((other) => other.id === id)).filter((other) => !!other).map((other) => `@${other.name}${other.id === agent.id ? " (you)" : ""}: ${firstLine(other.role)}`);
859
+ const before = history.filter((item) => item.id !== message.id && !item.failed && !item.notice).slice(-CONTEXT_MESSAGES);
860
+ const lines = [`[#${channel.name}]${channel.topic ? ` ${channel.topic}` : ""}`, "Members:", ...members.map((line) => `- ${line}`)];
861
+ if (channel.notice) {
862
+ lines.push("", "Notice of the channel (follow it; keep it with update_channel_notice):", channel.notice.text);
863
+ }
864
+ if (before.length > 0) {
865
+ lines.push("", "Recent messages:", ...before.map((item) => `${authorName(team, item.author, labels)}: ${item.text}`));
866
+ }
867
+ lines.push("", `${authorName(team, message.author, labels)} to you:`);
868
+ const speaker = message.author === USER ? "" : ` · ${authorName(team, message.author, labels).replace(/[\]\n]/g, " ")}`;
869
+ lines.push(`[ta:from ${NAME} #${channel.name}${speaker}]`);
870
+ return `<context>
871
+ ${lines.join(`
872
+ `)}
873
+ </context>
874
+ ${message.text}`;
875
+ }
876
+ ChannelsText.prompt = prompt;
877
+ function summary(team) {
878
+ if (team.agents.length === 0) {
879
+ return "No agents and no channels yet.";
880
+ }
881
+ const name = (id) => team.agents.find((agent) => agent.id === id)?.name ?? id;
882
+ const agents = team.agents.map((agent) => `- @${agent.name}: ${firstLine(agent.role)}`);
883
+ const channels = team.channels.map((channel) => `- #${channel.name}${channel.topic ? ` (${channel.topic})` : ""}: ${channel.members.map((id) => `@${name(id)}`).join(", ")}${channel.muted?.length ? `; muted by the user, answering nothing: ${channel.muted.map((id) => `@${name(id)}`).join(", ")}` : ""}${channel.notice ? `
884
+ notice: ${channel.notice.text.replace(/\s+/g, " ").slice(0, 300)}` : ""}`);
885
+ return ["Agents:", ...agents, "", "Channels:", ...channels.length > 0 ? channels : ["none yet"]].join(`
886
+ `);
887
+ }
888
+ ChannelsText.summary = summary;
889
+ function transcript(team, messages) {
890
+ const labels = { user: "user", ta: "Ta" };
891
+ return messages.map((item) => `${authorName(team, item.author, labels)}${item.failed ? " (failed)" : ""}: ${item.text}`).join(`
892
+
893
+ `);
894
+ }
895
+ ChannelsText.transcript = transcript;
896
+ function markdown(agent, messages, labels) {
897
+ const parts = [`# ${agent.name}`, "", `> ${firstLine(agent.role)}`];
898
+ for (const message of messages) {
899
+ const { role, content } = message ?? {};
900
+ if (role !== "user" && role !== "assistant") {
901
+ continue;
902
+ }
903
+ const text = (typeof content === "string" ? content : Array.isArray(content) ? content.map((block) => block?.type === "text" ? String(block.text ?? "") : "").join("") : "").replace(/^<context>\n?[\s\S]*?\n?<\/context>\n?/, "").trim();
904
+ if (text) {
905
+ parts.push("", `## ${role === "user" ? labels.user : agent.name}`, "", text);
906
+ }
907
+ }
908
+ return `${parts.join(`
909
+ `)}
910
+ `;
911
+ }
912
+ ChannelsText.markdown = markdown;
913
+ function channelMarkdown(team, channel, messages, labels) {
914
+ const parts = [`# #${channel.name}`];
915
+ if (channel.topic) {
916
+ parts.push("", `> ${firstLine(channel.topic)}`);
917
+ }
918
+ for (const message of messages) {
919
+ const text = message.text.trim();
920
+ if (text && message.author !== SYSTEM) {
921
+ parts.push("", `## ${authorName(team, message.author, labels)}${message.failed ? " (failed)" : ""}`, "", text);
922
+ }
923
+ }
924
+ return `${parts.join(`
925
+ `)}
926
+ `;
927
+ }
928
+ ChannelsText.channelMarkdown = channelMarkdown;
929
+ function firstLine(text) {
930
+ return text.split(`
931
+ `)[0];
932
+ }
933
+ })(ChannelsText ||= {});
934
+ var NOTICE_RULE = [
935
+ "A channel may have a notice, in the <context> block: what its members decided, agreed and are to do. Follow it.",
936
+ `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.`
937
+ ].join(`
938
+ `);
939
+
979
940
  // web-src/team-tools.ts
980
941
  var TeamTools;
981
942
  ((TeamTools) => {
@@ -1048,7 +1009,7 @@ var TeamTools;
1048
1009
  const { channel, done } = await team.post(String(params.channel ?? ""), TA, String(params.message ?? ""));
1049
1010
  await team.focus(channel.id);
1050
1011
  const replies = await done;
1051
- return replies.length > 0 ? ChannelsUtil.transcript(team.state, replies) : "Posted; nobody was asked to answer.";
1012
+ return replies.length > 0 ? ChannelsText.transcript(team.state, replies) : "Posted; nobody was asked to answer.";
1052
1013
  }
1053
1014
  },
1054
1015
  {
@@ -1056,7 +1017,7 @@ var TeamTools;
1056
1017
  label: "List channels",
1057
1018
  description: "Lists the agents, their roles, and the channels with their members.",
1058
1019
  parameters: { type: "object", properties: {} },
1059
- run: async (team) => ChannelsUtil.summary(team.state)
1020
+ run: async (team) => ChannelsText.summary(team.state)
1060
1021
  }
1061
1022
  ];
1062
1023
  TeamTools.NAMES = TeamTools.ALL.map((tool) => tool.name);