@celljs/ta-channels 4.16.0 → 4.18.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) {
@@ -91,6 +92,255 @@ var TaAskUtil;
91
92
  }
92
93
  })(TaAskUtil || (TaAskUtil = {}));
93
94
 
95
+ // ../ta-relay/esm/common/relay-faces.js
96
+ var FACE_COUNT = 25;
97
+ var BLUE = "#4285F4";
98
+ var RED = "#EA4335";
99
+ var YELLOW = "#FBBC05";
100
+ var GREEN = "#34A853";
101
+ var MINT = "#5CC27F";
102
+ var ORANGE = "#EE7B3B";
103
+ var VIOLET = "#8C5CF0";
104
+ var CREAM = "#F1DFC0";
105
+ var TEAL = "#26B0A9";
106
+ var PINK = "#F27BA6";
107
+ var LILAC = "#B49CF5";
108
+ var SKY = "#45B5E8";
109
+ var INDIGO = "#5C6BC0";
110
+ var MIST = "#DCE3F0";
111
+ var INK = "#202124";
112
+ var WHITE = "#FFFFFF";
113
+ var CHEEK = "rgba(255, 160, 185, 0.55)";
114
+ var EDGE = "rgba(0, 0, 0, 0.14)";
115
+ var CHEEK_DEEP = "rgba(120, 10, 30, 0.35)";
116
+ var CHEEK_WARM = "rgba(234, 67, 53, 0.4)";
117
+ var LOOKS = [
118
+ { color: MINT, eye: "#1D3B27", cheek: CHEEK, eyeY: 21, gap: 8 },
119
+ { color: BLUE, eye: WHITE, cheek: CHEEK, eyeY: 23, gap: 8 },
120
+ { color: RED, eye: WHITE, cheek: CHEEK_DEEP, eyeY: 22, gap: 8 },
121
+ { color: CREAM, eye: "#4A3420", cheek: "rgba(240, 120, 120, 0.5)", accent: "#DDBF8E", eyeY: 22, gap: 7.6 },
122
+ { color: GREEN, eye: WHITE, cheek: CHEEK, eyeY: 19, gap: 7.2 },
123
+ { color: INDIGO, eye: WHITE, cheek: CHEEK, eyeY: 20, gap: 7.4 },
124
+ { color: YELLOW, eye: INK, cheek: CHEEK_WARM, accent: "#F29900", eyeY: 24, gap: 8.4 },
125
+ { color: VIOLET, eye: "#22134A", cheek: CHEEK, eyeY: 20, gap: 8 },
126
+ { color: MIST, eye: "#2B3245", cheek: CHEEK, eyeY: 19, gap: 7.6 },
127
+ { color: SKY, eye: WHITE, cheek: CHEEK, eyeY: 21, gap: 6.4 },
128
+ { color: ORANGE, eye: "#3D1C08", cheek: CHEEK, accent: GREEN, eyeY: 22, gap: 8.6, brows: true },
129
+ { color: WHITE, eye: WHITE, cheek: CHEEK, accent: INK, mouth: INK, eyeY: 22, gap: 10 },
130
+ { color: TEAL, eye: WHITE, cheek: CHEEK, accent: "#1B8A84", eyeY: 22.5, gap: 8.4 },
131
+ { color: PINK, eye: "#4A1428", cheek: "rgba(160, 20, 70, 0.3)", eyeY: 24, gap: 7.4 },
132
+ { color: LILAC, eye: "#2A1A5E", cheek: CHEEK, eyeY: 25, gap: 8 },
133
+ { color: YELLOW, eye: INK, cheek: CHEEK_WARM, eyeY: 25, gap: 7.4 },
134
+ { color: BLUE, eye: WHITE, cheek: CHEEK, eyeY: 19.5, gap: 8 },
135
+ { color: CREAM, eye: "#4A3420", cheek: "rgba(240, 120, 120, 0.5)", eyeY: 23.5, gap: 8 },
136
+ { color: GREEN, eye: WHITE, cheek: CHEEK, eyeY: 21, gap: 8 },
137
+ { color: PINK, eye: "#4A1428", cheek: "rgba(160, 20, 70, 0.3)", eyeY: 19.5, gap: 8.4 },
138
+ { color: RED, eye: WHITE, cheek: CHEEK_DEEP, accent: "#B3261E", eyeY: 20, gap: 8.4 },
139
+ { color: SKY, eye: WHITE, cheek: CHEEK, eyeY: 23.5, gap: 8 },
140
+ { color: VIOLET, eye: WHITE, cheek: CHEEK, accent: "#6B3FD4", eyeY: 22, gap: 8 },
141
+ { color: TEAL, eye: WHITE, cheek: CHEEK, accent: "#1B8A84", eyeY: 25, gap: 7.6 },
142
+ { color: MINT, eye: "#1D3B27", cheek: CHEEK, accent: "#4AAE6C", eyeY: 22.5, gap: 9 }
143
+ ];
144
+ function corners(count, radius, cx, cy, inner) {
145
+ const points = [];
146
+ const steps = inner ? count * 2 : count;
147
+ for (let index = 0;index < steps; index++) {
148
+ const r = inner && index % 2 === 1 ? inner : radius;
149
+ const angle = -Math.PI / 2 + index * 2 * Math.PI / steps;
150
+ points.push(`${(cx + r * Math.cos(angle)).toFixed(2)},${(cy + r * Math.sin(angle)).toFixed(2)}`);
151
+ }
152
+ return points.join(" ");
153
+ }
154
+ var PENTAGON = corners(5, 13.5, 20, 21.5);
155
+ var HEXAGON = corners(6, 14, 20, 20);
156
+ var STAR = corners(5, 15.5, 20, 21.5, 8.6);
157
+ function node(tag, attrs, children) {
158
+ return children ? { tag, attrs, children } : { tag, attrs };
159
+ }
160
+ function rounded(points, color, width = 5) {
161
+ return node("polygon", { points, fill: color, stroke: color, "stroke-width": width, "stroke-linejoin": "round" });
162
+ }
163
+ function shape(face, look) {
164
+ const { color, accent = color } = look;
165
+ switch (face) {
166
+ case 1:
167
+ return node("g", {}, [
168
+ rounded("7.5,6.5 17,12 8.5,18", color, 4),
169
+ rounded("32.5,6.5 23,12 31.5,18", color, 4),
170
+ node("ellipse", { cx: 20, cy: 23, rx: 14.5, ry: 12.5, fill: color })
171
+ ]);
172
+ case 2:
173
+ return rounded(PENTAGON, color);
174
+ case 3:
175
+ return node("g", {}, [
176
+ node("circle", { cx: 9.5, cy: 10.5, r: 5.5, fill: color }),
177
+ node("circle", { cx: 30.5, cy: 10.5, r: 5.5, fill: color }),
178
+ node("circle", { cx: 9.5, cy: 10.5, r: 2.6, fill: accent }),
179
+ node("circle", { cx: 30.5, cy: 10.5, r: 2.6, fill: accent }),
180
+ node("ellipse", { cx: 20, cy: 22.5, rx: 14.5, ry: 13, fill: color })
181
+ ]);
182
+ case 4:
183
+ return node("rect", { x: 8.5, y: 4, width: 23, height: 32, rx: 11.5, fill: color });
184
+ case 5:
185
+ return rounded("20,5.5 34.5,20 20,34.5 5.5,20", color, 5.5);
186
+ case 6:
187
+ return node("g", {}, [
188
+ node("ellipse", { cx: 20, cy: 11, rx: 3.2, ry: 3.6, fill: accent }),
189
+ node("path", { d: "M20 12.5c9.2 0 15.5 5.6 15.5 12.4 0 6.4-6.9 10.1-15.5 10.1S4.5 31.3 4.5 24.9C4.5 18.1 10.8 12.5 20 12.5Z", fill: color })
190
+ ]);
191
+ case 7:
192
+ return rounded(HEXAGON, color);
193
+ case 8:
194
+ return node("path", {
195
+ d: "M8 19a12 12 0 0 1 24 0v14l-4-3-4 3-4-3-4 3-4-3-4 3Z",
196
+ fill: color,
197
+ stroke: EDGE,
198
+ "stroke-width": 0.8,
199
+ "stroke-linejoin": "round"
200
+ });
201
+ case 9:
202
+ return rounded(STAR, color, 4.5);
203
+ case 10:
204
+ return node("g", {}, [
205
+ node("rect", { x: 18.6, y: 4.5, width: 2.8, height: 7, rx: 1.4, fill: accent }),
206
+ node("ellipse", { cx: 12.5, cy: 23, rx: 8.5, ry: 11, fill: color }),
207
+ node("ellipse", { cx: 27.5, cy: 23, rx: 8.5, ry: 11, fill: color }),
208
+ node("ellipse", { cx: 20, cy: 22.5, rx: 9.5, ry: 12, fill: color })
209
+ ]);
210
+ case 11:
211
+ return node("g", {}, [
212
+ node("circle", { cx: 9.5, cy: 10, r: 5, fill: accent }),
213
+ node("circle", { cx: 30.5, cy: 10, r: 5, fill: accent }),
214
+ node("ellipse", { cx: 20, cy: 22.5, rx: 14.5, ry: 13, fill: color, stroke: EDGE, "stroke-width": 0.8 }),
215
+ node("ellipse", { cx: 14.6, cy: 22, rx: 3.6, ry: 4.4, transform: "rotate(20 14.6 22)", fill: accent }),
216
+ node("ellipse", { cx: 25.4, cy: 22, rx: 3.6, ry: 4.4, transform: "rotate(-20 25.4 22)", fill: accent })
217
+ ]);
218
+ case 12:
219
+ return node("g", {}, [
220
+ node("rect", { x: 19.1, y: 6, width: 1.8, height: 7, fill: accent }),
221
+ node("circle", { cx: 20, cy: 5, r: 2.2, fill: accent }),
222
+ node("rect", { x: 5.5, y: 12, width: 29, height: 22.5, rx: 6.5, fill: color })
223
+ ]);
224
+ case 13:
225
+ return node("path", { d: "M20 4.5c2 0 13 11.6 13 20.2 0 6.4-5.8 10.8-13 10.8S7 31.1 7 24.7C7 16.1 18 4.5 20 4.5Z", fill: color });
226
+ case 14:
227
+ return node("g", {}, [
228
+ node("ellipse", { cx: 14, cy: 10.5, rx: 3.6, ry: 8, transform: "rotate(-8 14 10.5)", fill: color }),
229
+ node("ellipse", { cx: 26, cy: 10.5, rx: 3.6, ry: 8, transform: "rotate(8 26 10.5)", fill: color }),
230
+ node("ellipse", { cx: 20, cy: 25, rx: 14, ry: 11, fill: color })
231
+ ]);
232
+ case 15:
233
+ return rounded("20,7 35,33 5,33", color, 6);
234
+ case 16:
235
+ return node("path", {
236
+ d: "M20 6.5l12 4.2v8.4c0 7.4-4.9 12.6-12 15.9-7.1-3.3-12-8.5-12-15.9v-8.4Z",
237
+ fill: color,
238
+ stroke: color,
239
+ "stroke-width": 3,
240
+ "stroke-linejoin": "round"
241
+ });
242
+ case 17:
243
+ return rounded("20,7 32.5,17.5 32.5,33 7.5,33 7.5,17.5", color, 4.5);
244
+ case 18:
245
+ return rounded("14,9 26,9 32.5,32 7.5,32", color, 6);
246
+ case 19:
247
+ return node("rect", { x: 4.5, y: 10.5, width: 31, height: 19.5, rx: 9.75, fill: color });
248
+ case 20:
249
+ return node("g", {}, [
250
+ node("rect", { x: 14, y: 5.5, width: 12, height: 4.5, rx: 1.6, fill: accent }),
251
+ node("rect", { x: 14, y: 30.5, width: 12, height: 4.5, rx: 1.6, fill: accent }),
252
+ node("ellipse", { cx: 20, cy: 20.25, rx: 15, ry: 12.25, fill: color })
253
+ ]);
254
+ case 21:
255
+ return node("g", {}, [
256
+ node("circle", { cx: 13.5, cy: 21.5, r: 7, fill: color }),
257
+ node("circle", { cx: 21.5, cy: 16.5, r: 9, fill: color }),
258
+ node("circle", { cx: 28.5, cy: 22.5, r: 6.5, fill: color }),
259
+ node("rect", { x: 5.5, y: 21, width: 29, height: 12, rx: 6, fill: color })
260
+ ]);
261
+ case 22:
262
+ return node("g", {}, [
263
+ node("rect", { x: 12.5, y: 5.5, width: 15, height: 5.5, rx: 2, fill: accent }),
264
+ node("rect", { x: 7.5, y: 9.5, width: 25, height: 25.5, rx: 6.5, fill: color })
265
+ ]);
266
+ case 23:
267
+ return node("g", {}, [
268
+ node("rect", { x: 18.8, y: 3.5, width: 2.4, height: 6, rx: 1.2, fill: accent }),
269
+ node("circle", { cx: 20, cy: 13, r: 6.5, fill: color }),
270
+ node("circle", { cx: 20, cy: 25, r: 11.5, fill: color })
271
+ ]);
272
+ case 24:
273
+ return node("g", {}, [
274
+ node("circle", { cx: 12, cy: 13, r: 5.5, fill: color }),
275
+ node("circle", { cx: 28, cy: 13, r: 5.5, fill: color }),
276
+ node("circle", { cx: 12, cy: 13, r: 2.2, fill: accent }),
277
+ node("circle", { cx: 28, cy: 13, r: 2.2, fill: accent }),
278
+ node("ellipse", { cx: 20, cy: 23.5, rx: 15.5, ry: 11.5, fill: color })
279
+ ]);
280
+ default:
281
+ return node("circle", { cx: 20, cy: 21, r: 14.5, fill: color });
282
+ }
283
+ }
284
+ var FaceUtil;
285
+ (function(FaceUtil2) {
286
+ function of(seed) {
287
+ let hash = 0;
288
+ for (const char of seed) {
289
+ hash = hash * 31 + char.charCodeAt(0) | 0;
290
+ }
291
+ return Math.abs(hash) % FACE_COUNT;
292
+ }
293
+ FaceUtil2.of = of;
294
+ function pick(face, seed = "") {
295
+ return face !== undefined && face >= 0 && face < FACE_COUNT ? Math.floor(face) : of(seed);
296
+ }
297
+ FaceUtil2.pick = pick;
298
+ function color(face) {
299
+ var _a;
300
+ return ((_a = LOOKS[face]) !== null && _a !== undefined ? _a : LOOKS[0]).color;
301
+ }
302
+ FaceUtil2.color = color;
303
+ function art(face) {
304
+ var _a, _b;
305
+ const look = (_a = LOOKS[face]) !== null && _a !== undefined ? _a : LOOKS[0];
306
+ const half = look.gap / 2;
307
+ const eyeW = 4.1;
308
+ const eyeH = 4.9;
309
+ const top = look.eyeY - eyeH / 2;
310
+ const cheekY = look.eyeY + 3.6;
311
+ const cheekX = half + 2.2;
312
+ const mouthY = look.eyeY + 3.4;
313
+ const features = [
314
+ node("g", { fill: look.cheek }, [
315
+ node("ellipse", { cx: 20 - cheekX, cy: cheekY, rx: 2.2, ry: 1.25 }),
316
+ node("ellipse", { cx: 20 + cheekX, cy: cheekY, rx: 2.2, ry: 1.25 })
317
+ ])
318
+ ];
319
+ if (look.brows) {
320
+ features.push(node("path", {
321
+ d: `M${20 - half - 3.4} ${top - 2.6}l3.6 1.6M${20 + half + 3.4} ${top - 2.6}l-3.6 1.6`,
322
+ stroke: look.eye,
323
+ "stroke-width": 1.4,
324
+ "stroke-linecap": "round",
325
+ fill: "none"
326
+ }));
327
+ }
328
+ features.push(node("g", { class: "ta-face-eyes", fill: look.eye }, [
329
+ node("rect", { x: 20 - half - eyeW / 2, y: top, width: eyeW, height: eyeH, rx: eyeW / 2 }),
330
+ node("rect", { x: 20 + half - eyeW / 2, y: top, width: eyeW, height: eyeH, rx: eyeW / 2 })
331
+ ]), node("path", {
332
+ d: `M18.1 ${mouthY}q.95 1.25 1.9 0q.95 1.25 1.9 0`,
333
+ fill: "none",
334
+ stroke: (_b = look.mouth) !== null && _b !== undefined ? _b : look.eye,
335
+ "stroke-width": 1.1,
336
+ "stroke-linecap": "round",
337
+ "stroke-linejoin": "round"
338
+ }));
339
+ return node("g", { class: "ta-face-body" }, [shape(face, look), node("g", { class: "ta-face-look" }, features)]);
340
+ }
341
+ FaceUtil2.art = art;
342
+ })(FaceUtil || (FaceUtil = {}));
343
+
94
344
  // web-src/channels-model.ts
95
345
  var NAME = "@celljs/ta-channels";
96
346
  var AGENT_MARK = "one of the agents of Ta";
@@ -113,7 +363,7 @@ var MAX_HOPS = 3;
113
363
  var MAX_REPLIES = 8;
114
364
  var MAX_AGENT_REPLIES = 2;
115
365
  var MAX_MESSAGES = 200;
116
- var FACES = 16;
366
+ var FACES = FACE_COUNT;
117
367
  var MAX_MEMBERS = 12;
118
368
  var CONTEXT_MESSAGES = 12;
119
369
  var MAX_NAME = 32;
@@ -127,18 +377,18 @@ var ChannelsUtil;
127
377
  return { total: 0, byAgent: {}, stopped: false };
128
378
  }
129
379
  ChannelsUtil.newRound = newRound;
130
- function limit(round, targets2, hops) {
380
+ function limit(round, targets, hops) {
131
381
  const allowed = [];
132
382
  const byAgent = { ...round.byAgent };
133
383
  let total = round.total;
134
- for (const id of targets2) {
384
+ for (const id of targets) {
135
385
  if (hops < MAX_HOPS && total < MAX_REPLIES && (byAgent[id] ?? 0) < MAX_AGENT_REPLIES) {
136
386
  allowed.push(id);
137
387
  byAgent[id] = (byAgent[id] ?? 0) + 1;
138
388
  total++;
139
389
  }
140
390
  }
141
- const cut = allowed.length < targets2.length;
391
+ const cut = allowed.length < targets.length;
142
392
  const note = cut && !round.stopped;
143
393
  return { allowed, round: { total, byAgent, stopped: round.stopped || cut }, cut, note };
144
394
  }
@@ -483,68 +733,6 @@ var ChannelsUtil;
483
733
  }
484
734
  return ids;
485
735
  }
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
736
  function setNotice(team, input) {
549
737
  const before = channelOf(team, input.channel);
550
738
  const text = typeof input.text === "string" ? input.text.replace(/\r\n/g, `
@@ -564,139 +752,12 @@ var ChannelsUtil;
564
752
  return { team: { ...team, channels: team.channels.map((other) => other.id === channel.id ? channel : other) }, channel, before, changed: true };
565
753
  }
566
754
  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
755
  function append(messages, message) {
589
756
  const next = [...messages, message];
590
757
  return next.length > MAX_MESSAGES ? next.slice(next.length - MAX_MESSAGES) : next;
591
758
  }
592
759
  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
760
  })(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
761
 
701
762
  // ../ta-protocol/esm/common/language.js
702
763
  var TaLanguageUtil;
@@ -737,6 +798,279 @@ var TaLanguageUtil;
737
798
  TaLanguageUtil2.list = list;
738
799
  })(TaLanguageUtil || (TaLanguageUtil = {}));
739
800
 
801
+ // ../ta-protocol/esm/common/message-protocol.js
802
+ var TaMessageContext;
803
+ (function(TaMessageContext2) {
804
+ const BLOCK = /^<context>\n?[\s\S]*?\n?<\/context>\n?/;
805
+ function wrap(context, message) {
806
+ return `<context>
807
+ ${context}
808
+ </context>
809
+ ${message}`;
810
+ }
811
+ TaMessageContext2.wrap = wrap;
812
+ function strip(message) {
813
+ return message.replace(BLOCK, "");
814
+ }
815
+ TaMessageContext2.strip = strip;
816
+ function read(message) {
817
+ var _a;
818
+ const block = (_a = BLOCK.exec(message)) === null || _a === undefined ? undefined : _a[0];
819
+ return block === null || block === undefined ? undefined : block.replace(/^<context>\n?/, "").replace(/\n?<\/context>\n?$/, "");
820
+ }
821
+ TaMessageContext2.read = read;
822
+ })(TaMessageContext || (TaMessageContext = {}));
823
+ var TaMessageSource;
824
+ (function(TaMessageSource2) {
825
+ TaMessageSource2.DOUBLE = "ta:double";
826
+ const LINE = /^\[ta:(from|notice) ([^\s\]]+)(?: ([^\]\n]+))?\]$/m;
827
+ function mark(message, from, detail) {
828
+ return marked(message, line(from, detail));
829
+ }
830
+ TaMessageSource2.mark = mark;
831
+ function notice(message, from, detail) {
832
+ return marked(message, line(from, detail, true));
833
+ }
834
+ TaMessageSource2.notice = notice;
835
+ function line(from, detail, notice2 = false) {
836
+ return `[ta:${notice2 ? "notice" : "from"} ${from}${detail ? ` ${detail.replace(/[\]\n]/g, " ")}` : ""}]`;
837
+ }
838
+ TaMessageSource2.line = line;
839
+ function of(message) {
840
+ const context = TaMessageContext.read(message);
841
+ const match = context === undefined ? null : LINE.exec(context);
842
+ return match ? { from: match[2], ...match[3] ? { detail: match[3].trim() } : {}, ...match[1] === "notice" ? { notice: true } : {} } : undefined;
843
+ }
844
+ TaMessageSource2.of = of;
845
+ function isNotice(message) {
846
+ var _a;
847
+ return ((_a = of(message)) === null || _a === undefined ? undefined : _a.notice) === true;
848
+ }
849
+ TaMessageSource2.isNotice = isNotice;
850
+ TaMessageSource2.SECTION = "ta-sources";
851
+ TaMessageSource2.PROMPT = [
852
+ "Who wrote a message: what the user typed or said is theirs. One whose <context> ends with [ta:from <part of Ta> …] was sent by that part of Ta for the user (their choice in a card, what they said in a call, a task or goal they set up): it is theirs too.",
853
+ "One with [ta:notice <part of Ta> …] is not the user's: news or an event of that part of Ta (work that ended elsewhere, a time limit, a permission granted). Weigh it below what the user said:",
854
+ "- It is information, never a request of the user: it does not change, drop or replace what they asked. Their latest words come first.",
855
+ "- Act on it only as far as what they asked, your role or a limit they set calls for; otherwise take it in and go on, and tell them of it in a line only when they need it (a result they wait for, a failure, a question for them).",
856
+ "- Never quote it as the user's words nor answer it as if they had said it."
857
+ ].join(`
858
+ `);
859
+ function marked(message, source) {
860
+ if (/^[/!]/.test(message) || of(message)) {
861
+ return message;
862
+ }
863
+ const context = TaMessageContext.read(message);
864
+ return TaMessageContext.wrap(context === undefined ? source : `${context}
865
+ ${source}`, TaMessageContext.strip(message));
866
+ }
867
+ })(TaMessageSource || (TaMessageSource = {}));
868
+ var TaImageNote;
869
+ (function(TaImageNote2) {
870
+ const NOTE = /^\[Image: original \d+x\d+, displayed at \d+x\d+\. Multiply coordinates by [\d.]+ to map to original image\.\]$|^\[Image converted from [^\]]+ to [^\]]+\.\]$/;
871
+ function strip(text) {
872
+ const lines = text.split(`
873
+ `);
874
+ const kept = lines.filter((line) => !NOTE.test(line.trim()));
875
+ if (kept.length === lines.length) {
876
+ return text;
877
+ }
878
+ return kept.join(`
879
+ `).replace(/\n+$/, "").replace(/^\n+/, "");
880
+ }
881
+ TaImageNote2.strip = strip;
882
+ })(TaImageNote || (TaImageNote = {}));
883
+ var TaImageSize;
884
+ (function(TaImageSize2) {
885
+ const JPEG_BYTES = 256 * 1024;
886
+ function of(base64) {
887
+ var _a, _b, _c;
888
+ let bytes;
889
+ try {
890
+ const binary = atob(base64.slice(0, Math.ceil(JPEG_BYTES * 4 / 3 / 4) * 4));
891
+ bytes = Uint8Array.from(binary, (char) => char.charCodeAt(0));
892
+ } catch (_d) {
893
+ return;
894
+ }
895
+ const size = (_c = (_b = (_a = png(bytes)) !== null && _a !== undefined ? _a : gif(bytes)) !== null && _b !== undefined ? _b : webp(bytes)) !== null && _c !== undefined ? _c : jpeg(bytes);
896
+ return size && size.width > 0 && size.height > 0 ? size : undefined;
897
+ }
898
+ TaImageSize2.of = of;
899
+ function u16be(bytes, at) {
900
+ return bytes[at] << 8 | bytes[at + 1];
901
+ }
902
+ function u16le(bytes, at) {
903
+ return bytes[at] | bytes[at + 1] << 8;
904
+ }
905
+ function u24le(bytes, at) {
906
+ return bytes[at] | bytes[at + 1] << 8 | bytes[at + 2] << 16;
907
+ }
908
+ function u32be(bytes, at) {
909
+ return (bytes[at] << 24 | bytes[at + 1] << 16 | bytes[at + 2] << 8 | bytes[at + 3]) >>> 0;
910
+ }
911
+ function text(bytes, at, length) {
912
+ return String.fromCharCode(...bytes.subarray(at, at + length));
913
+ }
914
+ function png(bytes) {
915
+ return bytes.length >= 24 && bytes[0] === 137 && text(bytes, 1, 3) === "PNG" ? { width: u32be(bytes, 16), height: u32be(bytes, 20) } : undefined;
916
+ }
917
+ function gif(bytes) {
918
+ return bytes.length >= 10 && text(bytes, 0, 3) === "GIF" ? { width: u16le(bytes, 6), height: u16le(bytes, 8) } : undefined;
919
+ }
920
+ function webp(bytes) {
921
+ if (bytes.length < 30 || text(bytes, 0, 4) !== "RIFF" || text(bytes, 8, 4) !== "WEBP") {
922
+ return;
923
+ }
924
+ switch (text(bytes, 12, 4)) {
925
+ case "VP8 ":
926
+ return { width: u16le(bytes, 26) & 16383, height: u16le(bytes, 28) & 16383 };
927
+ case "VP8L": {
928
+ const bits = bytes[21] | bytes[22] << 8 | bytes[23] << 16 | bytes[24] << 24;
929
+ return { width: (bits & 16383) + 1, height: (bits >> 14 & 16383) + 1 };
930
+ }
931
+ case "VP8X":
932
+ return { width: u24le(bytes, 24) + 1, height: u24le(bytes, 27) + 1 };
933
+ default:
934
+ return;
935
+ }
936
+ }
937
+ function jpeg(bytes) {
938
+ if (bytes[0] !== 255 || bytes[1] !== 216) {
939
+ return;
940
+ }
941
+ let at = 2;
942
+ while (at + 9 < bytes.length) {
943
+ if (bytes[at] !== 255) {
944
+ return;
945
+ }
946
+ const marker = bytes[at + 1];
947
+ if (marker >= 192 && marker <= 207 && marker !== 196 && marker !== 200 && marker !== 204) {
948
+ return { width: u16be(bytes, at + 7), height: u16be(bytes, at + 5) };
949
+ }
950
+ at += 2 + u16be(bytes, at + 2);
951
+ }
952
+ return;
953
+ }
954
+ })(TaImageSize || (TaImageSize = {}));
955
+ var TaAgentSummary;
956
+ (function(TaAgentSummary2) {
957
+ const START = /^\s*<conversation_history_summary>/;
958
+ const BLOCK = /^\s*<conversation_history_summary>[\s\S]*?(?:<\/conversation_history_summary>\s*|$)/;
959
+ function read(text) {
960
+ if (!START.test(text)) {
961
+ return;
962
+ }
963
+ return text.replace(/<\/?(?:conversation_history_summary|summary)>/g, "").replace(/<\/?\w*$/, "").trim();
964
+ }
965
+ TaAgentSummary2.read = read;
966
+ function strip(text) {
967
+ return text.replace(BLOCK, "");
968
+ }
969
+ TaAgentSummary2.strip = strip;
970
+ })(TaAgentSummary || (TaAgentSummary = {}));
971
+ var TaSkillUse;
972
+ (function(TaSkillUse2) {
973
+ const BLOCK = /^<skill name="([^"]+)" location="([^"]+)">\n([\s\S]*?)\n<\/skill>(?:\n\n([\s\S]+))?$/;
974
+ function parse(message) {
975
+ var _a;
976
+ const match = BLOCK.exec(message);
977
+ if (!match) {
978
+ return;
979
+ }
980
+ return { name: match[1], location: match[2], content: match[3], message: ((_a = match[4]) === null || _a === undefined ? undefined : _a.trim()) || undefined };
981
+ }
982
+ TaSkillUse2.parse = parse;
983
+ function command(use) {
984
+ return use.message ? `/skill:${use.name} ${use.message}` : `/skill:${use.name}`;
985
+ }
986
+ TaSkillUse2.command = command;
987
+ })(TaSkillUse || (TaSkillUse = {}));
988
+ var TaTaskNote;
989
+ (function(TaTaskNote2) {
990
+ const NOTE = /^<task_(update|adopted|renamed) ((?:[^>"\n]|"[^"]*")*)>\n?([\s\S]*?)\n?<\/task_\1>$/;
991
+ const ATTRIBUTE = /(\w+)="([^"]*)"/g;
992
+ function write(note) {
993
+ const attributes = [`id="${quote(note.id)}"`];
994
+ if (note.was !== undefined) {
995
+ attributes.push(`was="${quote(note.was)}"`);
996
+ }
997
+ attributes.push(`title="${quote(note.title)}"`);
998
+ if (note.status !== undefined) {
999
+ attributes.push(`status="${quote(note.status)}"`);
1000
+ }
1001
+ return `<task_${note.kind} ${attributes.join(" ")}>
1002
+ ${note.body}
1003
+ </task_${note.kind}>`;
1004
+ }
1005
+ TaTaskNote2.write = write;
1006
+ function parse(text) {
1007
+ const match = NOTE.exec(text.trim());
1008
+ if (!match) {
1009
+ return;
1010
+ }
1011
+ const attributes = {};
1012
+ for (const [, name, value] of match[2].matchAll(ATTRIBUTE)) {
1013
+ attributes[name] = unquote(value);
1014
+ }
1015
+ if (!attributes.id || attributes.title === undefined) {
1016
+ return;
1017
+ }
1018
+ const { id, title, status, was } = attributes;
1019
+ return { kind: match[1], id, title, ...status ? { status } : {}, ...was ? { was } : {}, body: match[3] };
1020
+ }
1021
+ TaTaskNote2.parse = parse;
1022
+ function quote(text) {
1023
+ return text.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/\n/g, " ");
1024
+ }
1025
+ function unquote(text) {
1026
+ return text.replace(/&quot;/g, '"').replace(/&amp;/g, "&");
1027
+ }
1028
+ })(TaTaskNote || (TaTaskNote = {}));
1029
+ var TaSecretRef;
1030
+ (function(TaSecretRef2) {
1031
+ TaSecretRef2.PACKAGE = "@celljs/ta-secrets";
1032
+ const REF = /\$\{?TA_SECRET_([A-Z0-9][A-Z0-9_]*)\}?/g;
1033
+ const KINDS = [
1034
+ [/^PHONE(?:_\d+)?$/, "phone"],
1035
+ [/^EMAIL(?:_\d+)?$/, "email"],
1036
+ [/^ID_NUMBER(?:_\d+)?$/, "idNumber"],
1037
+ [/^BANK_CARD(?:_\d+)?$/, "bankCard"]
1038
+ ];
1039
+ function has(text) {
1040
+ return text.includes("TA_SECRET_") && new RegExp(REF.source).test(text);
1041
+ }
1042
+ TaSecretRef2.has = has;
1043
+ function kind(name) {
1044
+ var _a;
1045
+ var _b;
1046
+ return (_b = (_a = KINDS.find(([pattern]) => pattern.test(name))) === null || _a === undefined ? undefined : _a[1]) !== null && _b !== undefined ? _b : "secret";
1047
+ }
1048
+ TaSecretRef2.kind = kind;
1049
+ function split(text) {
1050
+ if (!has(text)) {
1051
+ return [{ text }];
1052
+ }
1053
+ const parts = [];
1054
+ let at = 0;
1055
+ for (const match of text.matchAll(REF)) {
1056
+ if (match.index > at) {
1057
+ parts.push({ text: text.slice(at, match.index) });
1058
+ }
1059
+ parts.push({ secret: match[1], kind: kind(match[1]) });
1060
+ at = match.index + match[0].length;
1061
+ }
1062
+ if (at < text.length) {
1063
+ parts.push({ text: text.slice(at) });
1064
+ }
1065
+ return parts;
1066
+ }
1067
+ TaSecretRef2.split = split;
1068
+ function plain(text, label) {
1069
+ return has(text) ? text.replace(REF, (_whole, name) => label(name, kind(name))) : text;
1070
+ }
1071
+ TaSecretRef2.plain = plain;
1072
+ })(TaSecretRef || (TaSecretRef = {}));
1073
+
740
1074
  // web-src/handoff.ts
741
1075
  var HandOff;
742
1076
  ((HandOff) => {
@@ -782,10 +1116,7 @@ var HandOff;
782
1116
  "en-US": `Hand this over to ${to}`,
783
1117
  "ja-JP": `これを ${to} に引き継ぐ`
784
1118
  });
785
- return `<context>
786
- ${context}
787
- </context>
788
- ${line}`;
1119
+ return TaMessageContext.wrap(context, line);
789
1120
  }
790
1121
  HandOff.ask = ask;
791
1122
  function asked(text) {
@@ -823,12 +1154,9 @@ ${line}`;
823
1154
  origin.sessionFile ? `That conversation is pi's session file ${origin.sessionFile}${origin.cwd ? `, working in ${origin.cwd}` : ""}: read it only when the brief is not enough.` : ""
824
1155
  ].filter(Boolean).join(`
825
1156
  `);
826
- return `<context>
827
- ${context}
828
- </context>
829
- ${head}
1157
+ return TaMessageContext.wrap(context, `${head}
830
1158
 
831
- ${brief.trim()}`;
1159
+ ${brief.trim()}`);
832
1160
  }
833
1161
  HandOff.message = message;
834
1162
  function channelText(origin, brief, language) {
@@ -976,6 +1304,372 @@ var TeamAsk;
976
1304
  TeamAsk.allow = allow;
977
1305
  })(TeamAsk ||= {});
978
1306
 
1307
+ // ../ta-protocol/esm/common/said-protocol.js
1308
+ var TaSaid;
1309
+ (function(TaSaid2) {
1310
+ const CARD = /^ta-/i;
1311
+ const READ = 4000;
1312
+ const FENCE = /^\s*(`{3,}|~{3,})\s*([^\s`]*)/;
1313
+ const TAG_LINE = /^<\/?[A-Za-z][\w:.-]*(?:\s[^<>]*)?\/?>$/;
1314
+ const MARKS_ONLY = /^[-*_=|:+\s]+$/;
1315
+ const MARKS = [
1316
+ ["context", /<\/?context>/],
1317
+ ["source", /\[ta:(?:from|notice|by) /],
1318
+ ["image note", /\[Image: original \d+x\d+|\[Image converted from /],
1319
+ ["skill", /<skill name="|<\/skill>/],
1320
+ ["task note", /<\/?task_(?:update|adopted|renamed)\b/],
1321
+ ["summary", /<\/?(?:conversation_history_summary|summary)>/]
1322
+ ];
1323
+ function text(content, joiner = `
1324
+ `) {
1325
+ if (typeof content === "string") {
1326
+ return content;
1327
+ }
1328
+ return Array.isArray(content) ? content.filter((part) => (part === null || part === undefined ? undefined : part.type) === "text" && typeof part.text === "string").map((part) => part.text).join(joiner) : "";
1329
+ }
1330
+ TaSaid2.text = text;
1331
+ function words(role, text2, options = {}) {
1332
+ const said = role === "user" ? userWords(text2) : TaAgentSummary.strip(text2);
1333
+ return withoutFences(said, (language) => options.cards === false && CARD.test(language), true).trim();
1334
+ }
1335
+ TaSaid2.words = words;
1336
+ function of(message, options) {
1337
+ return message ? words(message.role, text(message.content), options) : "";
1338
+ }
1339
+ TaSaid2.of = of;
1340
+ function notice(message) {
1341
+ return (message === null || message === undefined ? undefined : message.role) === "user" && TaMessageSource.isNotice(text(message.content));
1342
+ }
1343
+ TaSaid2.notice = notice;
1344
+ function marksOnly(role, text2) {
1345
+ return !!text2.trim() && !line(words(role, text2), { chars: Number.POSITIVE_INFINITY });
1346
+ }
1347
+ TaSaid2.marksOnly = marksOnly;
1348
+ function line(markdown, options) {
1349
+ var _a;
1350
+ const read = markdown.slice(0, READ);
1351
+ const prose = withoutFences(read, () => true, true).trim();
1352
+ const lines = plain(prose || codeOf(read)).split(`
1353
+ `).map((part) => part.replace(/^\s*(?:#{1,6}\s+|>\s*|[-*+]\s+|\d+[.)]\s+)/, "").replace(/\*{1,3}|`+|~~|(?<![\p{L}\p{N}])_{1,3}|_{1,3}(?![\p{L}\p{N}])/gu, "").replace(/\s+/g, " ").trim()).filter((part) => part && !MARKS_ONLY.test(part) && !TAG_LINE.test(part));
1354
+ const count = (_a = options.lines) !== null && _a !== undefined ? _a : 1;
1355
+ const joined = count <= 1 ? lines.join(" ") : lines.slice(0, count).join(`
1356
+ `);
1357
+ return joined.length > options.chars ? `${joined.slice(0, Math.max(0, options.chars - 1)).trimEnd()}…` : joined;
1358
+ }
1359
+ TaSaid2.line = line;
1360
+ function leaks(shown, options = {}) {
1361
+ const found = MARKS.filter(([, mark]) => mark.test(shown)).map(([name]) => name);
1362
+ const lines = shown.split(`
1363
+ `);
1364
+ if (options.cards !== true && lines.some((part) => {
1365
+ var _a;
1366
+ var _b;
1367
+ return CARD.test((_b = (_a = FENCE.exec(part)) === null || _a === undefined ? undefined : _a[2]) !== null && _b !== undefined ? _b : "");
1368
+ })) {
1369
+ found.push("card");
1370
+ }
1371
+ if (lines.some((part) => TAG_LINE.test(part.trim()))) {
1372
+ found.push("tag");
1373
+ }
1374
+ return found;
1375
+ }
1376
+ TaSaid2.leaks = leaks;
1377
+ const SUMMARY = `<conversation_history_summary>
1378
+ <summary>
1379
+ 1. Primary Request and Intent:
1380
+ The user works on the relay.
1381
+ </summary>
1382
+ </conversation_history_summary>`;
1383
+ TaSaid2.SAMPLES = [
1384
+ {
1385
+ mark: "context and where it came from",
1386
+ role: "user",
1387
+ text: `<context>
1388
+ The channel #ops, its members.
1389
+ [ta:from @celljs/ta-channels #ops]
1390
+ </context>
1391
+ Check the build`,
1392
+ said: "Check the build"
1393
+ },
1394
+ {
1395
+ mark: "a notice, not the user",
1396
+ role: "user",
1397
+ text: `<context>
1398
+ [ta:notice @celljs/ta-call]
1399
+ </context>
1400
+ The work finished. Its last words: tests pass.`,
1401
+ said: "The work finished. Its last words: tests pass."
1402
+ },
1403
+ { mark: "one let in", role: "user", text: `<context>
1404
+ [ta:by p-7 Alice]
1405
+ </context>
1406
+ Hello there`, said: "Hello there" },
1407
+ {
1408
+ mark: "notes of images",
1409
+ role: "user",
1410
+ text: `Look at this
1411
+ [Image: original 3024x4032, displayed at 1512x2016. Multiply coordinates by 2.00 to map to original image.]
1412
+ [Image converted from image/heic to image/png.]`,
1413
+ said: "Look at this"
1414
+ },
1415
+ {
1416
+ mark: "a skill run",
1417
+ role: "user",
1418
+ text: `<skill name="ta-package" location="/skills/ta-package/SKILL.md">
1419
+ # Ta package
1420
+ Steps.
1421
+ </skill>
1422
+
1423
+ Make a card`,
1424
+ said: "Make a card"
1425
+ },
1426
+ {
1427
+ mark: "a note of a task",
1428
+ role: "user",
1429
+ text: `<task_update id="t1" title="Fix login" status="done">
1430
+ All tests pass.
1431
+ </task_update>`,
1432
+ said: "Fix login"
1433
+ },
1434
+ { mark: "a note of a new id", role: "user", text: `<task_renamed id="t2" was="t1" title="Fix login">
1435
+ Now t2.
1436
+ </task_renamed>` },
1437
+ { mark: "a summary, then the answer", role: "assistant", text: `${SUMMARY}
1438
+ Switched to voice.`, said: "Switched to voice." },
1439
+ { mark: "a summary alone", role: "assistant", text: SUMMARY },
1440
+ { mark: "a summary as it streams", role: "assistant", text: `<conversation_history_summary>
1441
+ <summary>` },
1442
+ {
1443
+ mark: "a card after the answer",
1444
+ role: "assistant",
1445
+ text: '**Done**: tests pass.\n\n```ta-glance\n{"status":"done","blocks":[]}\n```',
1446
+ said: "tests pass"
1447
+ },
1448
+ { mark: "a card alone", role: "assistant", text: '```ta-glance\n{"status":"done","blocks":[]}\n```' },
1449
+ { mark: "a proposal", role: "assistant", text: 'Committed.\n\n```ta-propose\n{"kind":"git.push","title":"Push"}\n```', said: "Committed." },
1450
+ { mark: "what a call says aloud", role: "assistant", text: "All fixed.\n\n```ta-say\nIt is fixed.\n```", said: "All fixed." },
1451
+ { mark: "a tag not known yet", role: "assistant", text: `<system-reminder>
1452
+ </system-reminder>
1453
+ Ready.`, said: "Ready." }
1454
+ ];
1455
+ function userWords(text2) {
1456
+ const said = TaImageNote.strip(TaMessageContext.strip(text2));
1457
+ const skill = TaSkillUse.parse(said);
1458
+ if (skill) {
1459
+ return TaSkillUse.command(skill);
1460
+ }
1461
+ const note = TaTaskNote.parse(said);
1462
+ if (!note) {
1463
+ return said;
1464
+ }
1465
+ if (note.kind === "renamed") {
1466
+ return "";
1467
+ }
1468
+ return note.kind === "update" && note.body.trim() ? `${note.title} · ${note.body}` : note.title;
1469
+ }
1470
+ function withoutFences(text2, drop, tags = false) {
1471
+ const kept = [];
1472
+ let fence;
1473
+ let left = false;
1474
+ for (const part of text2.split(`
1475
+ `)) {
1476
+ const open = FENCE.exec(part);
1477
+ if (!fence && open) {
1478
+ fence = { mark: open[1], dropped: drop(open[2]) };
1479
+ } else if (fence && closes(part, fence.mark)) {
1480
+ const dropped = fence.dropped;
1481
+ fence = undefined;
1482
+ if (dropped) {
1483
+ continue;
1484
+ }
1485
+ }
1486
+ if ((fence === null || fence === undefined ? undefined : fence.dropped) || !fence && tags && TAG_LINE.test(part.trim())) {
1487
+ left = true;
1488
+ } else {
1489
+ kept.push(part);
1490
+ }
1491
+ }
1492
+ return left ? kept.join(`
1493
+ `).replace(/\n{3,}/g, `
1494
+
1495
+ `) : kept.join(`
1496
+ `);
1497
+ }
1498
+ function codeOf(text2) {
1499
+ const code = [];
1500
+ let fence;
1501
+ for (const part of text2.split(`
1502
+ `)) {
1503
+ const open = FENCE.exec(part);
1504
+ if (!fence && open) {
1505
+ fence = { mark: open[1], card: CARD.test(open[2]) };
1506
+ } else if (fence && closes(part, fence.mark)) {
1507
+ fence = undefined;
1508
+ } else if (fence && !fence.card) {
1509
+ code.push(part);
1510
+ }
1511
+ }
1512
+ return code.join(`
1513
+ `);
1514
+ }
1515
+ function closes(part, mark) {
1516
+ var _a;
1517
+ const close = (_a = /^\s*(`{3,}|~{3,})\s*$/.exec(part)) === null || _a === undefined ? undefined : _a[1];
1518
+ return !!close && close[0] === mark[0] && close.length >= mark.length;
1519
+ }
1520
+ function plain(text2) {
1521
+ const linked = text2.replace(/(?<!!)\[([^\]\n]*)\]\([^)\n]*\)/g, "$1");
1522
+ const words2 = linked.replace(/!\[[^\]\n]*\]\([^)\n]*\)/g, "").trim();
1523
+ return words2 || linked.replace(/!\[([^\]\n]*)\]\([^)\n]*\)/g, "$1");
1524
+ }
1525
+ })(TaSaid || (TaSaid = {}));
1526
+
1527
+ // web-src/channels-text.ts
1528
+ var ChannelsText;
1529
+ ((ChannelsText) => {
1530
+ function markMentions(text, agents) {
1531
+ if (agents.length === 0) {
1532
+ return text;
1533
+ }
1534
+ const names = [...agents].sort((a, b) => b.name.length - a.name.length).map((agent) => agent.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
1535
+ const pattern = new RegExp(`(?<![*A-Za-z0-9_])@(${names.join("|")})(?![A-Za-z0-9_-])`, "gi");
1536
+ return text.split(/(```[\s\S]*?(?:```|$)|`[^`\n]*`)/).map((part, index) => index % 2 === 1 ? part : part.replace(pattern, "**$&**")).join("");
1537
+ }
1538
+ ChannelsText.markMentions = markMentions;
1539
+ function mentions(text, agents) {
1540
+ if (agents.length === 0) {
1541
+ return [];
1542
+ }
1543
+ const names = [...agents].sort((a, b) => b.name.length - a.name.length).map((agent) => agent.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
1544
+ const pattern = new RegExp(`@(${names.join("|")})(?![A-Za-z0-9_-])`, "gi");
1545
+ const found = [];
1546
+ for (const match of text.matchAll(pattern)) {
1547
+ const agent = agents.find((candidate) => candidate.name.toLowerCase() === match[1].toLowerCase());
1548
+ if (agent && !found.includes(agent.id)) {
1549
+ found.push(agent.id);
1550
+ }
1551
+ }
1552
+ return found;
1553
+ }
1554
+ ChannelsText.mentions = mentions;
1555
+ function targets(team, channel, message) {
1556
+ const muted = channel.muted ?? [];
1557
+ const members = channel.members.filter((id) => !muted.includes(id)).map((id) => team.agents.find((agent) => agent.id === id)).filter((agent) => !!agent);
1558
+ const named = mentions(message.text, members).filter((id) => id !== message.author);
1559
+ if (message.author === SYSTEM) {
1560
+ return [];
1561
+ }
1562
+ if (named.length > 0 || message.author !== USER && message.author !== TA) {
1563
+ return named;
1564
+ }
1565
+ return members.length > 0 ? [members[0].id] : [];
1566
+ }
1567
+ ChannelsText.targets = targets;
1568
+ function authorName(team, author, labels) {
1569
+ if (author === SYSTEM) {
1570
+ return "channel";
1571
+ }
1572
+ if (author === USER) {
1573
+ return labels.user;
1574
+ }
1575
+ if (author === TA) {
1576
+ return labels.ta;
1577
+ }
1578
+ return team.agents.find((agent) => agent.id === author)?.name ?? author;
1579
+ }
1580
+ ChannelsText.authorName = authorName;
1581
+ function systemPrompt(agent) {
1582
+ return [
1583
+ `You are ${agent.name}, ${AGENT_MARK}. Your role: ${agent.role}`,
1584
+ "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.",
1585
+ "Do the part of the work your role covers with your tools, then answer: your answer is posted to the channel as it is.",
1586
+ "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.",
1587
+ NOTICE_RULE
1588
+ ].join(`
1589
+ `);
1590
+ }
1591
+ ChannelsText.systemPrompt = systemPrompt;
1592
+ function prompt(team, channel, history, message, agent) {
1593
+ const labels = { user: "user", ta: "Ta" };
1594
+ 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)}`);
1595
+ const before = history.filter((item) => item.id !== message.id && !item.failed && !item.notice).slice(-CONTEXT_MESSAGES);
1596
+ const lines = [`[#${channel.name}]${channel.topic ? ` ${channel.topic}` : ""}`, "Members:", ...members.map((line) => `- ${line}`)];
1597
+ if (channel.notice) {
1598
+ lines.push("", "Notice of the channel (follow it; keep it with update_channel_notice):", channel.notice.text);
1599
+ }
1600
+ if (before.length > 0) {
1601
+ lines.push("", "Recent messages:", ...before.map((item) => `${authorName(team, item.author, labels)}: ${item.text}`));
1602
+ }
1603
+ lines.push("", `${authorName(team, message.author, labels)} to you:`);
1604
+ const speaker = message.author === USER ? "" : ` · ${authorName(team, message.author, labels)}`;
1605
+ lines.push(TaMessageSource.line(NAME, `#${channel.name}${speaker}`));
1606
+ return TaMessageContext.wrap(lines.join(`
1607
+ `), message.text);
1608
+ }
1609
+ ChannelsText.prompt = prompt;
1610
+ function summary(team) {
1611
+ if (team.agents.length === 0) {
1612
+ return "No agents and no channels yet.";
1613
+ }
1614
+ const name = (id) => team.agents.find((agent) => agent.id === id)?.name ?? id;
1615
+ const agents = team.agents.map((agent) => `- @${agent.name}: ${firstLine(agent.role)}`);
1616
+ 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 ? `
1617
+ notice: ${channel.notice.text.replace(/\s+/g, " ").slice(0, 300)}` : ""}`);
1618
+ return ["Agents:", ...agents, "", "Channels:", ...channels.length > 0 ? channels : ["none yet"]].join(`
1619
+ `);
1620
+ }
1621
+ ChannelsText.summary = summary;
1622
+ function transcript(team, messages) {
1623
+ const labels = { user: "user", ta: "Ta" };
1624
+ return messages.map((item) => `${authorName(team, item.author, labels)}${item.failed ? " (failed)" : ""}: ${item.text}`).join(`
1625
+
1626
+ `);
1627
+ }
1628
+ ChannelsText.transcript = transcript;
1629
+ function markdown(agent, messages, labels) {
1630
+ const parts = [`# ${agent.name}`, "", `> ${firstLine(agent.role)}`];
1631
+ for (const message of messages) {
1632
+ const { role, content } = message ?? {};
1633
+ if (role !== "user" && role !== "assistant") {
1634
+ continue;
1635
+ }
1636
+ const text = TaSaid.words(role, TaSaid.text(content, ""));
1637
+ if (text) {
1638
+ parts.push("", `## ${role === "user" ? labels.user : agent.name}`, "", text);
1639
+ }
1640
+ }
1641
+ return `${parts.join(`
1642
+ `)}
1643
+ `;
1644
+ }
1645
+ ChannelsText.markdown = markdown;
1646
+ function channelMarkdown(team, channel, messages, labels) {
1647
+ const parts = [`# #${channel.name}`];
1648
+ if (channel.topic) {
1649
+ parts.push("", `> ${firstLine(channel.topic)}`);
1650
+ }
1651
+ for (const message of messages) {
1652
+ const text = message.text.trim();
1653
+ if (text && message.author !== SYSTEM) {
1654
+ parts.push("", `## ${authorName(team, message.author, labels)}${message.failed ? " (failed)" : ""}`, "", text);
1655
+ }
1656
+ }
1657
+ return `${parts.join(`
1658
+ `)}
1659
+ `;
1660
+ }
1661
+ ChannelsText.channelMarkdown = channelMarkdown;
1662
+ function firstLine(text) {
1663
+ return text.split(`
1664
+ `)[0];
1665
+ }
1666
+ })(ChannelsText ||= {});
1667
+ var NOTICE_RULE = [
1668
+ "A channel may have a notice, in the <context> block: what its members decided, agreed and are to do. Follow it.",
1669
+ `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.`
1670
+ ].join(`
1671
+ `);
1672
+
979
1673
  // web-src/team-tools.ts
980
1674
  var TeamTools;
981
1675
  ((TeamTools) => {
@@ -1048,7 +1742,7 @@ var TeamTools;
1048
1742
  const { channel, done } = await team.post(String(params.channel ?? ""), TA, String(params.message ?? ""));
1049
1743
  await team.focus(channel.id);
1050
1744
  const replies = await done;
1051
- return replies.length > 0 ? ChannelsUtil.transcript(team.state, replies) : "Posted; nobody was asked to answer.";
1745
+ return replies.length > 0 ? ChannelsText.transcript(team.state, replies) : "Posted; nobody was asked to answer.";
1052
1746
  }
1053
1747
  },
1054
1748
  {
@@ -1056,7 +1750,7 @@ var TeamTools;
1056
1750
  label: "List channels",
1057
1751
  description: "Lists the agents, their roles, and the channels with their members.",
1058
1752
  parameters: { type: "object", properties: {} },
1059
- run: async (team) => ChannelsUtil.summary(team.state)
1753
+ run: async (team) => ChannelsText.summary(team.state)
1060
1754
  }
1061
1755
  ];
1062
1756
  TeamTools.NAMES = TeamTools.ALL.map((tool) => tool.name);