@celljs/ta-channels 4.8.0 → 4.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +3 -2
- package/README.md +3 -2
- package/extension/index.mjs +109 -8
- package/host/index.mjs +281 -5
- package/package.json +13 -4
- package/web/index.js +992 -205
- package/web-src/agent-menu.tsx +0 -236
- package/web-src/channel-menu.tsx +0 -98
- package/web-src/channels-model.ts +0 -156
- package/web-src/channels-util.spec.ts +0 -312
- package/web-src/channels-util.ts +0 -556
- package/web-src/channels.ts +0 -235
- package/web-src/create.tsx +0 -273
- package/web-src/extension.ts +0 -91
- package/web-src/faces.tsx +0 -247
- package/web-src/host.ts +0 -12
- package/web-src/i18n.ts +0 -221
- package/web-src/index.tsx +0 -82
- package/web-src/members.tsx +0 -258
- package/web-src/more-menu.tsx +0 -113
- package/web-src/react.ts +0 -11
- package/web-src/room.tsx +0 -212
- package/web-src/sidebar.tsx +0 -295
- package/web-src/style.ts +0 -249
- package/web-src/team-tools.ts +0 -103
- package/web-src/team.spec.ts +0 -370
- package/web-src/team.ts +0 -560
- package/web-src/views.spec.ts +0 -89
- package/web-src/views.tsx +0 -239
package/web-src/channels-util.ts
DELETED
|
@@ -1,556 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AGENT_MARK,
|
|
3
|
-
type Agent,
|
|
4
|
-
type Channel,
|
|
5
|
-
type ChannelMessage,
|
|
6
|
-
CONTEXT_MESSAGES,
|
|
7
|
-
FACES,
|
|
8
|
-
MAX_AGENT_REPLIES,
|
|
9
|
-
MAX_HOPS,
|
|
10
|
-
MAX_MEMBERS,
|
|
11
|
-
MAX_MESSAGES,
|
|
12
|
-
MAX_NAME,
|
|
13
|
-
MAX_REPLIES,
|
|
14
|
-
MEMBER_CHANGES,
|
|
15
|
-
type MemberChange,
|
|
16
|
-
type Round,
|
|
17
|
-
SYSTEM,
|
|
18
|
-
TA,
|
|
19
|
-
type Team,
|
|
20
|
-
USER,
|
|
21
|
-
} from './channels-model';
|
|
22
|
-
|
|
23
|
-
export * from './channels-model';
|
|
24
|
-
|
|
25
|
-
export namespace ChannelsUtil {
|
|
26
|
-
export function newRound(): Round {
|
|
27
|
-
return { total: 0, byAgent: {}, stopped: false };
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Who of `targets` may answer a message `hops` hand-ons away from the one of the user or ta: none past
|
|
32
|
-
* {@link MAX_HOPS}, at most {@link MAX_REPLIES} in the round and {@link MAX_AGENT_REPLIES} per agent.
|
|
33
|
-
* `cut` says some were left out; `note` that the channel is to say so, once a round.
|
|
34
|
-
*/
|
|
35
|
-
export function limit(round: Round, targets: string[], hops: number): { allowed: string[]; round: Round; cut: boolean; note: boolean } {
|
|
36
|
-
const allowed: string[] = [];
|
|
37
|
-
const byAgent = { ...round.byAgent };
|
|
38
|
-
let total = round.total;
|
|
39
|
-
for (const id of targets) {
|
|
40
|
-
if (hops < MAX_HOPS && total < MAX_REPLIES && (byAgent[id] ?? 0) < MAX_AGENT_REPLIES) {
|
|
41
|
-
allowed.push(id);
|
|
42
|
-
byAgent[id] = (byAgent[id] ?? 0) + 1;
|
|
43
|
-
total++;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
const cut = allowed.length < targets.length;
|
|
47
|
-
const note = cut && !round.stopped;
|
|
48
|
-
return { allowed, round: { total, byAgent, stopped: round.stopped || cut }, cut, note };
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function emptyTeam(): Team {
|
|
52
|
-
return { agents: [], channels: [] };
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function newId(prefix: string): string {
|
|
56
|
-
return `${prefix}-${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
/** Spaces become dashes and a leading `@` or `#` goes, so that the name can be mentioned. */
|
|
60
|
-
export function checkName(value: unknown, what: string): string {
|
|
61
|
-
const name =
|
|
62
|
-
typeof value === 'string'
|
|
63
|
-
? value
|
|
64
|
-
.trim()
|
|
65
|
-
.replace(/^[@#]+/, '')
|
|
66
|
-
.replace(/\s+/g, '-')
|
|
67
|
-
: '';
|
|
68
|
-
if (!name || name.length > MAX_NAME || /[@#,]/.test(name)) {
|
|
69
|
-
throw new Error(`The name of the ${what} must be 1 to ${MAX_NAME} characters, without @, # or commas.`);
|
|
70
|
-
}
|
|
71
|
-
return name;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** The first character of a name in a script without case, as Chinese; else its initials, at most two. */
|
|
75
|
-
export function mark(name: string): string {
|
|
76
|
-
const first = Array.from(name)[0] ?? '?';
|
|
77
|
-
if (!/[A-Za-z0-9]/.test(first)) {
|
|
78
|
-
return first;
|
|
79
|
-
}
|
|
80
|
-
const words = name.split(/[-_.\s]+|(?<=[a-z])(?=[A-Z])/).filter(Boolean);
|
|
81
|
-
return words.length > 1 ? (words[0][0] + words[1][0]).toUpperCase() : first.toUpperCase();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/** The face of each agent: its own, or, for one without, one by its id that no other agent has while some are free. */
|
|
85
|
-
export function faces(agents: Agent[]): Map<string, number> {
|
|
86
|
-
const result = new Map<string, number>();
|
|
87
|
-
const taken = new Set<number>();
|
|
88
|
-
for (const agent of agents) {
|
|
89
|
-
if (isFace(agent.face)) {
|
|
90
|
-
result.set(agent.id, agent.face);
|
|
91
|
-
taken.add(agent.face);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
for (const agent of agents) {
|
|
95
|
-
if (!result.has(agent.id)) {
|
|
96
|
-
const face = freeFace(hash(agent.id), taken);
|
|
97
|
-
result.set(agent.id, face);
|
|
98
|
-
taken.add(face);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return result;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export function isFace(face: unknown): face is number {
|
|
105
|
-
return typeof face === 'number' && Number.isInteger(face) && face >= 0 && face < FACES;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/** A face no agent has yet, the first free one after that of `seed`; any when all are taken. */
|
|
109
|
-
export function freeFaceFor(agents: Agent[], seed: string): number {
|
|
110
|
-
return freeFace(hash(seed), new Set(faces(agents).values()));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** `base`, or `base-2`, `base-3`… the first no agent is named. */
|
|
114
|
-
export function freeName(team: Team, base: string): string {
|
|
115
|
-
let name = base;
|
|
116
|
-
for (let count = 2; findAgent(team, name); count++) {
|
|
117
|
-
name = `${base}-${count}`;
|
|
118
|
-
}
|
|
119
|
-
return name;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
/** `start`, or the next face after it no one has; `start` again when all are taken. */
|
|
123
|
-
function freeFace(start: number, taken: Set<number>): number {
|
|
124
|
-
for (let step = 0; step < FACES; step++) {
|
|
125
|
-
const face = (start + step) % FACES;
|
|
126
|
-
if (!taken.has(face)) {
|
|
127
|
-
return face;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
return start % FACES;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/** A small stable number of a text (FNV-1a). */
|
|
134
|
-
export function hash(text: string): number {
|
|
135
|
-
let value = 0x811c9dc5;
|
|
136
|
-
for (let index = 0; index < text.length; index++) {
|
|
137
|
-
value = Math.imul(value ^ text.charCodeAt(index), 0x01000193);
|
|
138
|
-
}
|
|
139
|
-
return value >>> 0;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
export function findAgent(team: Team, ref: string): Agent | undefined {
|
|
143
|
-
const key = ref.trim().replace(/^@/, '').toLowerCase();
|
|
144
|
-
return team.agents.find(agent => agent.id === ref || agent.name.toLowerCase() === key);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export function findChannel(team: Team, ref: string): Channel | undefined {
|
|
148
|
-
const key = ref.trim().replace(/^#/, '').toLowerCase();
|
|
149
|
-
return team.channels.find(channel => channel.id === ref || channel.name.toLowerCase() === key);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/** `face`: one of the {@link FACES}, else one no other agent has while some are free. */
|
|
153
|
-
/** A directory given, trimmed; undefined for none. */
|
|
154
|
-
export function directory(value: unknown): string | undefined {
|
|
155
|
-
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export function addAgent(team: Team, input: { name: unknown; role: unknown; cwd?: unknown; face?: unknown }): { team: Team; agent: Agent } {
|
|
159
|
-
const name = checkName(input.name, 'agent');
|
|
160
|
-
if (findAgent(team, name)) {
|
|
161
|
-
throw new Error(`There is an agent named ${name} already.`);
|
|
162
|
-
}
|
|
163
|
-
const role = typeof input.role === 'string' ? input.role.trim() : '';
|
|
164
|
-
if (!role) {
|
|
165
|
-
throw new Error(`Say what ${name} does: its role.`);
|
|
166
|
-
}
|
|
167
|
-
const cwd = directory(input.cwd);
|
|
168
|
-
const id = newId('agent');
|
|
169
|
-
const face = isFace(input.face) ? input.face : freeFace(hash(id), new Set(faces(team.agents).values()));
|
|
170
|
-
const agent: Agent = { id, name, role, mark: mark(name), face, cwd, createdAt: Date.now() };
|
|
171
|
-
return { team: { ...team, agents: [...team.agents, agent] }, agent };
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function addChannel(team: Team, input: { name: unknown; members: unknown; topic?: unknown }): { team: Team; channel: Channel } {
|
|
175
|
-
const name = checkName(input.name, 'channel');
|
|
176
|
-
if (findChannel(team, name)) {
|
|
177
|
-
throw new Error(`There is a channel named ${name} already.`);
|
|
178
|
-
}
|
|
179
|
-
const members = agentIds(team, input.members);
|
|
180
|
-
if (members.length === 0) {
|
|
181
|
-
throw new Error('A channel needs at least one agent.');
|
|
182
|
-
}
|
|
183
|
-
if (members.length > MAX_MEMBERS) {
|
|
184
|
-
throw new Error(`A channel holds at most ${MAX_MEMBERS} agents.`);
|
|
185
|
-
}
|
|
186
|
-
const topic = typeof input.topic === 'string' && input.topic.trim() ? input.topic.trim() : undefined;
|
|
187
|
-
const channel: Channel = { id: newId('channel'), name, topic, members, createdAt: Date.now() };
|
|
188
|
-
return { team: { ...team, channels: [...team.channels, channel] }, channel };
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/** Adds agents that exist to a channel, after its members: the lead stays. `added` leaves out who was there. */
|
|
192
|
-
export function addMembers(team: Team, input: { channel: unknown; members: unknown }): { team: Team; channel: Channel; added: Agent[] } {
|
|
193
|
-
const found = channelOf(team, input.channel);
|
|
194
|
-
const ids = agentIds(team, input.members);
|
|
195
|
-
if (ids.length === 0) {
|
|
196
|
-
throw new Error('Name at least one agent to add.');
|
|
197
|
-
}
|
|
198
|
-
const added = ids.filter(id => !found.members.includes(id)).map(id => team.agents.find(agent => agent.id === id) as Agent);
|
|
199
|
-
if (found.members.length + added.length > MAX_MEMBERS) {
|
|
200
|
-
throw new Error(`A channel holds at most ${MAX_MEMBERS} agents; #${found.name} has ${found.members.length}.`);
|
|
201
|
-
}
|
|
202
|
-
const channel: Channel = { ...found, members: [...found.members, ...added.map(agent => agent.id)] };
|
|
203
|
-
return { team: { ...team, channels: team.channels.map(other => (other.id === channel.id ? channel : other)) }, channel, added };
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/** The channel `ref` names; a name of none fails, with the channels there are. */
|
|
207
|
-
export function channelOf(team: Team, ref: unknown): Channel {
|
|
208
|
-
const key = typeof ref === 'string' ? ref : '';
|
|
209
|
-
const found = findChannel(team, key);
|
|
210
|
-
if (!found) {
|
|
211
|
-
const known = team.channels.map(other => `#${other.name}`).join(', ') || 'none yet';
|
|
212
|
-
throw new Error(`No channel is named ${key}. The channels: ${known}.`);
|
|
213
|
-
}
|
|
214
|
-
return found;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/** One member of a channel changed: the lead first, out (never the last one) or muted. */
|
|
218
|
-
export function changeMember(team: Team, input: { channel: unknown; agent: unknown; change: unknown }): { team: Team; channel: Channel } {
|
|
219
|
-
const found = channelOf(team, input.channel);
|
|
220
|
-
const agent = findAgent(team, typeof input.agent === 'string' ? input.agent : '');
|
|
221
|
-
if (!agent || !found.members.includes(agent.id)) {
|
|
222
|
-
throw new Error(`${agent?.name ?? String(input.agent)} is not a member of #${found.name}.`);
|
|
223
|
-
}
|
|
224
|
-
if (typeof input.change !== 'string' || !MEMBER_CHANGES.includes(input.change)) {
|
|
225
|
-
throw new Error(`A member is changed with one of ${MEMBER_CHANGES.join(', ')}.`);
|
|
226
|
-
}
|
|
227
|
-
const others = found.members.filter(id => id !== agent.id);
|
|
228
|
-
const muted = (found.muted ?? []).filter(id => id !== agent.id);
|
|
229
|
-
let channel: Channel;
|
|
230
|
-
switch (input.change as MemberChange) {
|
|
231
|
-
case 'lead':
|
|
232
|
-
channel = { ...found, members: [agent.id, ...others] };
|
|
233
|
-
break;
|
|
234
|
-
case 'remove':
|
|
235
|
-
if (others.length === 0) {
|
|
236
|
-
throw new Error(`${agent.name} is the only member of #${found.name}: delete the channel instead.`);
|
|
237
|
-
}
|
|
238
|
-
channel = { ...found, members: others, muted };
|
|
239
|
-
break;
|
|
240
|
-
case 'mute':
|
|
241
|
-
channel = { ...found, muted: [...muted, agent.id] };
|
|
242
|
-
break;
|
|
243
|
-
default:
|
|
244
|
-
channel = { ...found, muted };
|
|
245
|
-
}
|
|
246
|
-
if (channel.muted?.length === 0) {
|
|
247
|
-
delete channel.muted;
|
|
248
|
-
}
|
|
249
|
-
return { team: { ...team, channels: team.channels.map(other => (other.id === channel.id ? channel : other)) }, channel };
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
export function updateChannel(
|
|
253
|
-
team: Team,
|
|
254
|
-
input: { channel: unknown; name?: unknown; topic?: unknown; pinned?: unknown },
|
|
255
|
-
): { team: Team; channel: Channel; before: Channel } {
|
|
256
|
-
const before = channelOf(team, input.channel);
|
|
257
|
-
const channel: Channel = { ...before };
|
|
258
|
-
if (input.name !== undefined) {
|
|
259
|
-
const name = checkName(input.name, 'channel');
|
|
260
|
-
const other = findChannel(team, name);
|
|
261
|
-
if (other && other.id !== channel.id) {
|
|
262
|
-
throw new Error(`There is a channel named ${name} already.`);
|
|
263
|
-
}
|
|
264
|
-
channel.name = name;
|
|
265
|
-
}
|
|
266
|
-
if (input.topic !== undefined) {
|
|
267
|
-
const topic = typeof input.topic === 'string' ? input.topic.trim() : '';
|
|
268
|
-
if (topic) {
|
|
269
|
-
channel.topic = topic;
|
|
270
|
-
} else {
|
|
271
|
-
delete channel.topic;
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
if (input.pinned !== undefined) {
|
|
275
|
-
if (input.pinned === true) {
|
|
276
|
-
channel.pinned = true;
|
|
277
|
-
} else {
|
|
278
|
-
delete channel.pinned;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return { team: { ...team, channels: team.channels.map(other => (other.id === channel.id ? channel : other)) }, channel, before };
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/** The pinned channels first, each group in the order they were made. */
|
|
285
|
-
export function pinnedFirst<T extends Pick<Channel, 'pinned'>>(channels: readonly T[]): T[] {
|
|
286
|
-
return [...channels.filter(channel => channel.pinned), ...channels.filter(channel => !channel.pinned)];
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
export function removeChannel(team: Team, ref: unknown): { team: Team; channel: Channel } {
|
|
290
|
-
const channel = channelOf(team, ref);
|
|
291
|
-
return { team: { ...team, channels: team.channels.filter(other => other.id !== channel.id) }, channel };
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
/** The agent without its session: a new one opens the next time it answers. */
|
|
295
|
-
export function resetAgent(team: Team, ref: unknown): { team: Team; agent: Agent } {
|
|
296
|
-
const found = findAgent(team, typeof ref === 'string' ? ref : '');
|
|
297
|
-
if (!found) {
|
|
298
|
-
throw new Error(`No agent is ${String(ref)}.`);
|
|
299
|
-
}
|
|
300
|
-
const { sessionFile: _gone, ...agent } = found;
|
|
301
|
-
return { team: { ...team, agents: team.agents.map(other => (other.id === agent.id ? agent : other)) }, agent };
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* Changes what is given of an agent. A name stays unique; another directory starts a new session there,
|
|
306
|
-
* since a session works in one. A new role is read when its session opens again.
|
|
307
|
-
*/
|
|
308
|
-
export function updateAgent(
|
|
309
|
-
team: Team,
|
|
310
|
-
input: { agent: unknown; name?: unknown; role?: unknown; face?: unknown; cwd?: unknown },
|
|
311
|
-
): { team: Team; agent: Agent; before: Agent } {
|
|
312
|
-
const before = findAgent(team, typeof input.agent === 'string' ? input.agent : '');
|
|
313
|
-
if (!before) {
|
|
314
|
-
throw new Error(`No agent is ${String(input.agent)}.`);
|
|
315
|
-
}
|
|
316
|
-
const agent: Agent = { ...before };
|
|
317
|
-
if (input.name !== undefined) {
|
|
318
|
-
const name = checkName(input.name, 'agent');
|
|
319
|
-
const other = findAgent(team, name);
|
|
320
|
-
if (other && other.id !== agent.id) {
|
|
321
|
-
throw new Error(`There is an agent named ${name} already.`);
|
|
322
|
-
}
|
|
323
|
-
agent.name = name;
|
|
324
|
-
agent.mark = mark(name);
|
|
325
|
-
}
|
|
326
|
-
if (input.role !== undefined) {
|
|
327
|
-
const role = typeof input.role === 'string' ? input.role.trim() : '';
|
|
328
|
-
if (!role) {
|
|
329
|
-
throw new Error(`Say what ${agent.name} does: its role.`);
|
|
330
|
-
}
|
|
331
|
-
agent.role = role;
|
|
332
|
-
}
|
|
333
|
-
if (input.face !== undefined) {
|
|
334
|
-
if (!isFace(input.face)) {
|
|
335
|
-
throw new Error(`A face is a number from 0 to ${FACES - 1}.`);
|
|
336
|
-
}
|
|
337
|
-
agent.face = input.face;
|
|
338
|
-
}
|
|
339
|
-
if (input.cwd !== undefined) {
|
|
340
|
-
const cwd = typeof input.cwd === 'string' && input.cwd.trim() ? input.cwd.trim() : undefined;
|
|
341
|
-
if (cwd !== before.cwd) {
|
|
342
|
-
agent.cwd = cwd;
|
|
343
|
-
delete agent.sessionFile;
|
|
344
|
-
}
|
|
345
|
-
if (!cwd) {
|
|
346
|
-
delete agent.cwd;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
return { team: { ...team, agents: team.agents.map(other => (other.id === agent.id ? agent : other)) }, agent, before };
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
/** The agent out of the team and of its channels; `channels`: those it was the only member of, which go too. */
|
|
353
|
-
export function removeAgent(team: Team, ref: unknown): { team: Team; agent: Agent; channels: Channel[] } {
|
|
354
|
-
const agent = findAgent(team, typeof ref === 'string' ? ref : '');
|
|
355
|
-
if (!agent) {
|
|
356
|
-
throw new Error(`No agent is ${String(ref)}.`);
|
|
357
|
-
}
|
|
358
|
-
const gone: Channel[] = [];
|
|
359
|
-
const channels: Channel[] = [];
|
|
360
|
-
for (const channel of team.channels) {
|
|
361
|
-
if (!channel.members.includes(agent.id)) {
|
|
362
|
-
channels.push(channel);
|
|
363
|
-
continue;
|
|
364
|
-
}
|
|
365
|
-
const members = channel.members.filter(id => id !== agent.id);
|
|
366
|
-
if (members.length === 0) {
|
|
367
|
-
gone.push(channel);
|
|
368
|
-
continue;
|
|
369
|
-
}
|
|
370
|
-
const muted = channel.muted?.filter(id => id !== agent.id);
|
|
371
|
-
const next: Channel = { ...channel, members, muted };
|
|
372
|
-
if (!muted?.length) {
|
|
373
|
-
delete next.muted;
|
|
374
|
-
}
|
|
375
|
-
channels.push(next);
|
|
376
|
-
}
|
|
377
|
-
return { team: { agents: team.agents.filter(other => other.id !== agent.id), channels }, agent, channels: gone };
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
/** The ids of the agents `refs` names, once each, in order; a name of nobody fails. */
|
|
381
|
-
function agentIds(team: Team, refs: unknown): string[] {
|
|
382
|
-
const ids: string[] = [];
|
|
383
|
-
for (const ref of Array.isArray(refs) ? refs.filter((item): item is string => typeof item === 'string') : []) {
|
|
384
|
-
const agent = findAgent(team, ref);
|
|
385
|
-
if (!agent) {
|
|
386
|
-
const known = team.agents.map(other => other.name).join(', ') || 'none yet';
|
|
387
|
-
throw new Error(`No agent is named ${ref}. The agents: ${known}.`);
|
|
388
|
-
}
|
|
389
|
-
if (!ids.includes(agent.id)) {
|
|
390
|
-
ids.push(agent.id);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
return ids;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
/** Markdown of a message with the `@` names of agents in bold, but not in code. */
|
|
397
|
-
export function markMentions(text: string, agents: Agent[]): string {
|
|
398
|
-
if (agents.length === 0) {
|
|
399
|
-
return text;
|
|
400
|
-
}
|
|
401
|
-
const names = [...agents].sort((a, b) => b.name.length - a.name.length).map(agent => agent.name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
|
402
|
-
const pattern = new RegExp(`(?<![*A-Za-z0-9_])@(${names.join('|')})(?![A-Za-z0-9_-])`, 'gi');
|
|
403
|
-
return text
|
|
404
|
-
.split(/(```[\s\S]*?(?:```|$)|`[^`\n]*`)/)
|
|
405
|
-
.map((part, index) => (index % 2 === 1 ? part : part.replace(pattern, '**$&**')))
|
|
406
|
-
.join('');
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
/** The agents a text names with `@`, in order, once each; a longer name wins over a shorter one it starts with. */
|
|
410
|
-
export function mentions(text: string, agents: Agent[]): string[] {
|
|
411
|
-
if (agents.length === 0) {
|
|
412
|
-
return [];
|
|
413
|
-
}
|
|
414
|
-
const names = [...agents].sort((a, b) => b.name.length - a.name.length).map(agent => agent.name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
|
|
415
|
-
const pattern = new RegExp(`@(${names.join('|')})(?![A-Za-z0-9_-])`, 'gi');
|
|
416
|
-
const found: string[] = [];
|
|
417
|
-
for (const match of text.matchAll(pattern)) {
|
|
418
|
-
const agent = agents.find(candidate => candidate.name.toLowerCase() === match[1].toLowerCase());
|
|
419
|
-
if (agent && !found.includes(agent.id)) {
|
|
420
|
-
found.push(agent.id);
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
return found;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* Who answers a message: the members it names. A message of the user or ta that names nobody goes
|
|
428
|
-
* to the lead; one of an agent that names nobody ends there. Nobody answers themselves, and muted
|
|
429
|
-
* members not at all: what names nobody then goes to the first member not muted.
|
|
430
|
-
*/
|
|
431
|
-
export function targets(team: Team, channel: Channel, message: ChannelMessage): string[] {
|
|
432
|
-
const muted = channel.muted ?? [];
|
|
433
|
-
const members = channel.members
|
|
434
|
-
.filter(id => !muted.includes(id))
|
|
435
|
-
.map(id => team.agents.find(agent => agent.id === id))
|
|
436
|
-
.filter((agent): agent is Agent => !!agent);
|
|
437
|
-
const named = mentions(message.text, members).filter(id => id !== message.author);
|
|
438
|
-
if (message.author === SYSTEM) {
|
|
439
|
-
return [];
|
|
440
|
-
}
|
|
441
|
-
if (named.length > 0 || (message.author !== USER && message.author !== TA)) {
|
|
442
|
-
return named;
|
|
443
|
-
}
|
|
444
|
-
return members.length > 0 ? [members[0].id] : [];
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
export function authorName(team: Team, author: string, labels: { user: string; ta: string }): string {
|
|
448
|
-
if (author === SYSTEM) {
|
|
449
|
-
return 'channel';
|
|
450
|
-
}
|
|
451
|
-
if (author === USER) {
|
|
452
|
-
return labels.user;
|
|
453
|
-
}
|
|
454
|
-
if (author === TA) {
|
|
455
|
-
return labels.ta;
|
|
456
|
-
}
|
|
457
|
-
return team.agents.find(agent => agent.id === author)?.name ?? author;
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
/** What an agent's session adds to its system prompt. */
|
|
461
|
-
export function systemPrompt(agent: Agent): string {
|
|
462
|
-
return [
|
|
463
|
-
`You are ${agent.name}, ${AGENT_MARK}. Your role: ${agent.role}`,
|
|
464
|
-
'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.',
|
|
465
|
-
'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.',
|
|
466
|
-
'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.',
|
|
467
|
-
].join('\n');
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
/**
|
|
471
|
-
* The message an agent gets: where it is, who is there and what was said, in the `<context>` block
|
|
472
|
-
* that ta's pages leave out (TaMessageContext of @celljs/ta-protocol), then what to answer.
|
|
473
|
-
*/
|
|
474
|
-
export function prompt(team: Team, channel: Channel, history: ChannelMessage[], message: ChannelMessage, agent: Agent): string {
|
|
475
|
-
const labels = { user: 'user', ta: 'Ta' };
|
|
476
|
-
const members = channel.members
|
|
477
|
-
.map(id => team.agents.find(other => other.id === id))
|
|
478
|
-
.filter((other): other is Agent => !!other)
|
|
479
|
-
.map(other => `@${other.name}${other.id === agent.id ? ' (you)' : ''}: ${firstLine(other.role)}`);
|
|
480
|
-
const before = history.filter(item => item.id !== message.id && !item.failed).slice(-CONTEXT_MESSAGES);
|
|
481
|
-
const lines = [`[#${channel.name}]${channel.topic ? ` ${channel.topic}` : ''}`, 'Members:', ...members.map(line => `- ${line}`)];
|
|
482
|
-
if (before.length > 0) {
|
|
483
|
-
lines.push('', 'Recent messages:', ...before.map(item => `${authorName(team, item.author, labels)}: ${item.text}`));
|
|
484
|
-
}
|
|
485
|
-
lines.push('', `${authorName(team, message.author, labels)} to you:`);
|
|
486
|
-
return `<context>\n${lines.join('\n')}\n</context>\n${message.text}`;
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
export function append(messages: ChannelMessage[], message: ChannelMessage): ChannelMessage[] {
|
|
490
|
-
const next = [...messages, message];
|
|
491
|
-
return next.length > MAX_MESSAGES ? next.slice(next.length - MAX_MESSAGES) : next;
|
|
492
|
-
}
|
|
493
|
-
|
|
494
|
-
/** The team, as the model reads it. */
|
|
495
|
-
export function summary(team: Team): string {
|
|
496
|
-
if (team.agents.length === 0) {
|
|
497
|
-
return 'No agents and no channels yet.';
|
|
498
|
-
}
|
|
499
|
-
const name = (id: string) => team.agents.find(agent => agent.id === id)?.name ?? id;
|
|
500
|
-
const agents = team.agents.map(agent => `- @${agent.name}: ${firstLine(agent.role)}`);
|
|
501
|
-
const channels = team.channels.map(
|
|
502
|
-
channel =>
|
|
503
|
-
`- #${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(', ')}` : ''}`,
|
|
504
|
-
);
|
|
505
|
-
return ['Agents:', ...agents, '', 'Channels:', ...(channels.length > 0 ? channels : ['none yet'])].join('\n');
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
/** Answers, as the model reads them. */
|
|
509
|
-
export function transcript(team: Team, messages: ChannelMessage[]): string {
|
|
510
|
-
const labels = { user: 'user', ta: 'Ta' };
|
|
511
|
-
return messages.map(item => `${authorName(team, item.author, labels)}${item.failed ? ' (failed)' : ''}: ${item.text}`).join('\n\n');
|
|
512
|
-
}
|
|
513
|
-
|
|
514
|
-
/** The conversation of an agent's session as Markdown: what the user and the agent said, without the context block of channels. */
|
|
515
|
-
export function markdown(agent: Agent, messages: readonly unknown[], labels: { user: string }): string {
|
|
516
|
-
const parts = [`# ${agent.name}`, '', `> ${firstLine(agent.role)}`];
|
|
517
|
-
for (const message of messages) {
|
|
518
|
-
const { role, content } = (message ?? {}) as { role?: string; content?: unknown };
|
|
519
|
-
if (role !== 'user' && role !== 'assistant') {
|
|
520
|
-
continue;
|
|
521
|
-
}
|
|
522
|
-
const text = (
|
|
523
|
-
typeof content === 'string'
|
|
524
|
-
? content
|
|
525
|
-
: Array.isArray(content)
|
|
526
|
-
? content.map(block => (block?.type === 'text' ? String(block.text ?? '') : '')).join('')
|
|
527
|
-
: ''
|
|
528
|
-
)
|
|
529
|
-
.replace(/^<context>\n?[\s\S]*?\n?<\/context>\n?/, '')
|
|
530
|
-
.trim();
|
|
531
|
-
if (text) {
|
|
532
|
-
parts.push('', `## ${role === 'user' ? labels.user : agent.name}`, '', text);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
return `${parts.join('\n')}\n`;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
/** The messages of a channel as Markdown, under the names of who wrote them. */
|
|
539
|
-
export function channelMarkdown(team: Team, channel: Channel, messages: readonly ChannelMessage[], labels: { user: string; ta: string }): string {
|
|
540
|
-
const parts = [`# #${channel.name}`];
|
|
541
|
-
if (channel.topic) {
|
|
542
|
-
parts.push('', `> ${firstLine(channel.topic)}`);
|
|
543
|
-
}
|
|
544
|
-
for (const message of messages) {
|
|
545
|
-
const text = message.text.trim();
|
|
546
|
-
if (text && message.author !== SYSTEM) {
|
|
547
|
-
parts.push('', `## ${authorName(team, message.author, labels)}${message.failed ? ' (failed)' : ''}`, '', text);
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
return `${parts.join('\n')}\n`;
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
function firstLine(text: string): string {
|
|
554
|
-
return text.split('\n')[0];
|
|
555
|
-
}
|
|
556
|
-
}
|