@celljs/ta-channels 0.0.0-stage → 4.6.1

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.
@@ -0,0 +1,312 @@
1
+ import { describe, expect, it } from 'bun:test';
2
+ import {
3
+ type ChannelMessage,
4
+ ChannelsUtil,
5
+ DEFAULT_ROLE,
6
+ FACES,
7
+ MAX_AGENT_REPLIES,
8
+ MAX_HOPS,
9
+ MAX_MEMBERS,
10
+ MAX_MESSAGES,
11
+ MAX_REPLIES,
12
+ SYSTEM,
13
+ TA,
14
+ USER,
15
+ } from './channels-util';
16
+
17
+ function team() {
18
+ let current = ChannelsUtil.emptyTeam();
19
+ for (const [name, role] of [
20
+ ['designer', 'You design pages.'],
21
+ ['dev', 'You write the code.'],
22
+ ['dev-lead', 'You review the code.'],
23
+ ]) {
24
+ current = ChannelsUtil.addAgent(current, { name, role }).team;
25
+ }
26
+ return ChannelsUtil.addChannel(current, { name: 'web', members: ['dev', '@designer'], topic: 'The page' });
27
+ }
28
+
29
+ const message = (author: string, text: string): ChannelMessage => ({ id: ChannelsUtil.newId('m'), author, text, at: 0 });
30
+
31
+ describe('ChannelsUtil', () => {
32
+ it('checks names, which mentions and channels use', () => {
33
+ expect(ChannelsUtil.checkName(' @code reviewer ', 'agent')).toBe('code-reviewer');
34
+ expect(ChannelsUtil.checkName('#设计', 'channel')).toBe('设计');
35
+ expect(() => ChannelsUtil.checkName('', 'agent')).toThrow('1 to 32');
36
+ expect(() => ChannelsUtil.checkName('a@b', 'agent')).toThrow('without @');
37
+ expect(() => ChannelsUtil.addAgent(team().team, { name: 'Designer', role: 'x' })).toThrow('already');
38
+ expect(() => ChannelsUtil.addAgent(team().team, { name: 'qa', role: ' ' })).toThrow('role');
39
+ });
40
+
41
+ it('marks an agent with its initials, or the first character of a Chinese name', () => {
42
+ expect(ChannelsUtil.mark('designer')).toBe('D');
43
+ expect(ChannelsUtil.mark('code-reviewer')).toBe('CR');
44
+ expect(ChannelsUtil.mark('qaLead')).toBe('QL');
45
+ expect(ChannelsUtil.mark('设计师')).toBe('设');
46
+ });
47
+
48
+ it('makes a channel of agents that exist, the first one leading', () => {
49
+ const { team: current, channel } = team();
50
+ expect(channel.members.map(id => current.agents.find(agent => agent.id === id)?.name)).toEqual(['dev', 'designer']);
51
+ expect(() => ChannelsUtil.addChannel(current, { name: 'x', members: ['nobody'] })).toThrow(
52
+ 'No agent is named nobody. The agents: designer, dev, dev-lead.',
53
+ );
54
+ expect(() => ChannelsUtil.addChannel(current, { name: 'x', members: [] })).toThrow('at least one agent');
55
+ expect(ChannelsUtil.findChannel(current, '#WEB')).toBe(channel);
56
+ });
57
+
58
+ it('adds agents to a channel after its members, once each, the lead staying', () => {
59
+ const { team: current, channel } = team();
60
+ const { team: next, channel: grown, added } = ChannelsUtil.addMembers(current, { channel: '#web', members: ['dev-lead', 'designer', 'dev-lead'] });
61
+ expect(added.map(agent => agent.name)).toEqual(['dev-lead']);
62
+ expect(grown.members.map(id => next.agents.find(agent => agent.id === id)?.name)).toEqual(['dev', 'designer', 'dev-lead']);
63
+ expect(ChannelsUtil.findChannel(next, 'web')).toBe(grown);
64
+ expect(ChannelsUtil.findChannel(current, 'web')).toBe(channel);
65
+ expect(ChannelsUtil.addMembers(next, { channel: grown.id, members: ['dev'] }).added).toEqual([]);
66
+ expect(() => ChannelsUtil.addMembers(current, { channel: 'nowhere', members: ['dev'] })).toThrow('No channel is named nowhere. The channels: #web.');
67
+ expect(() => ChannelsUtil.addMembers(current, { channel: 'web', members: ['nobody'] })).toThrow('No agent is named nobody.');
68
+ expect(() => ChannelsUtil.addMembers(current, { channel: 'web', members: [] })).toThrow('at least one agent');
69
+ });
70
+
71
+ it('finds mentions, the longest name first, once each', () => {
72
+ const { team: current } = team();
73
+ const name = (ids: string[]) => ids.map(id => current.agents.find(agent => agent.id === id)?.name);
74
+ expect(name(ChannelsUtil.mentions('@dev-lead and @dev, then @dev again; mail@devs', current.agents))).toEqual(['dev-lead', 'dev']);
75
+ expect(name(ChannelsUtil.mentions('@DESIGNER,看看', current.agents))).toEqual(['designer']);
76
+ });
77
+
78
+ it('sends a message to whom it names, else from the user or ta to the lead; an agent naming nobody ends it', () => {
79
+ const { team: current, channel } = team();
80
+ const [designer, dev, lead] = current.agents;
81
+ expect(ChannelsUtil.targets(current, channel, message(USER, 'hi'))).toEqual([dev.id]);
82
+ expect(ChannelsUtil.targets(current, channel, message(TA, '@designer look'))).toEqual([designer.id]);
83
+ // Not a member: not asked.
84
+ expect(ChannelsUtil.targets(current, channel, message(USER, '@dev-lead look'))).toEqual([dev.id]);
85
+ expect(ChannelsUtil.targets(current, channel, message(dev.id, 'done'))).toEqual([]);
86
+ expect(ChannelsUtil.targets(current, channel, message(dev.id, '@dev @designer over to you'))).toEqual([designer.id]);
87
+ expect(lead.name).toBe('dev-lead');
88
+ });
89
+
90
+ it('makes a member the lead, mutes it, lets it answer again and takes it out, never the last one', () => {
91
+ const { team: current, channel } = team();
92
+ const [designer, dev, lead] = current.agents;
93
+ const named = (next: { channel: { members: string[] } }) => next.channel.members.map(id => current.agents.find(agent => agent.id === id)?.name);
94
+ const led = ChannelsUtil.changeMember(current, { channel: 'web', agent: 'designer', change: 'lead' });
95
+ expect(named(led)).toEqual(['designer', 'dev']);
96
+ expect(ChannelsUtil.targets(led.team, led.channel, message(USER, 'hi'))).toEqual([designer.id]);
97
+ // Muted: not asked, named or not; what names nobody goes to the next member.
98
+ const muted = ChannelsUtil.changeMember(led.team, { channel: channel.id, agent: designer.id, change: 'mute' });
99
+ expect(muted.channel.muted).toEqual([designer.id]);
100
+ expect(ChannelsUtil.targets(muted.team, muted.channel, message(USER, 'hi'))).toEqual([dev.id]);
101
+ expect(ChannelsUtil.targets(muted.team, muted.channel, message(USER, '@designer look'))).toEqual([dev.id]);
102
+ expect(ChannelsUtil.targets(muted.team, muted.channel, message(dev.id, '@designer look'))).toEqual([]);
103
+ const back = ChannelsUtil.changeMember(muted.team, { channel: 'web', agent: '@designer', change: 'unmute' });
104
+ expect(back.channel.muted).toBeUndefined();
105
+ const out = ChannelsUtil.changeMember(muted.team, { channel: 'web', agent: 'designer', change: 'remove' });
106
+ expect(named(out)).toEqual(['dev']);
107
+ expect(out.channel.muted).toBeUndefined();
108
+ expect(() => ChannelsUtil.changeMember(out.team, { channel: 'web', agent: 'dev', change: 'remove' })).toThrow('the only member of #web');
109
+ expect(() => ChannelsUtil.changeMember(current, { channel: 'web', agent: lead.id, change: 'lead' })).toThrow('dev-lead is not a member of #web.');
110
+ expect(() => ChannelsUtil.changeMember(current, { channel: 'web', agent: 'dev', change: 'kick' })).toThrow('one of lead, remove, mute, unmute');
111
+ expect(() => ChannelsUtil.changeMember(current, { channel: 'nowhere', agent: 'dev', change: 'lead' })).toThrow('No channel is named nowhere.');
112
+ // The team it came from stays as it was.
113
+ expect(ChannelsUtil.findChannel(current, 'web')).toBe(channel);
114
+ });
115
+
116
+ it('deletes a channel, and lets an agent start over without its session', () => {
117
+ const { team: current, channel } = team();
118
+ const { team: next, channel: gone } = ChannelsUtil.removeChannel(current, '#web');
119
+ expect(gone).toBe(channel);
120
+ expect(next.channels).toEqual([]);
121
+ expect(() => ChannelsUtil.removeChannel(next, 'web')).toThrow('No channel is named web. The channels: none yet.');
122
+ const withFile = { ...current, agents: current.agents.map(agent => ({ ...agent, sessionFile: `/s/${agent.name}` })) };
123
+ const { team: reset, agent } = ChannelsUtil.resetAgent(withFile, 'dev');
124
+ expect(agent.sessionFile).toBeUndefined();
125
+ expect(reset.agents.map(item => item.sessionFile)).toEqual(['/s/designer', undefined, '/s/dev-lead']);
126
+ expect(() => ChannelsUtil.resetAgent(current, 'nobody')).toThrow('No agent is nobody.');
127
+ });
128
+
129
+ it('changes the name, role, face and directory of an agent, and starts it over in another directory', () => {
130
+ const { team: current } = team();
131
+ const withFile = { ...current, agents: current.agents.map(agent => ({ ...agent, sessionFile: `/s/${agent.name}` })) };
132
+ const renamed = ChannelsUtil.updateAgent(withFile, { agent: 'dev', name: 'Code Writer', role: ' You write it. ', face: 3 });
133
+ expect(renamed.agent).toMatchObject({ name: 'Code-Writer', mark: 'CW', role: 'You write it.', face: 3, sessionFile: '/s/dev' });
134
+ expect(renamed.before.name).toBe('dev');
135
+ const moved = ChannelsUtil.updateAgent(renamed.team, { agent: 'Code-Writer', cwd: '/repo' }).agent;
136
+ expect(moved.cwd).toBe('/repo');
137
+ expect(moved.sessionFile).toBeUndefined();
138
+ expect(ChannelsUtil.updateAgent(withFile, { agent: 'dev', cwd: '' }).agent.sessionFile).toBe('/s/dev');
139
+ expect(() => ChannelsUtil.updateAgent(current, { agent: 'dev', name: 'Designer' })).toThrow('already');
140
+ expect(ChannelsUtil.updateAgent(current, { agent: 'dev', name: 'DEV' }).agent.name).toBe('DEV');
141
+ expect(() => ChannelsUtil.updateAgent(current, { agent: 'dev', role: '' })).toThrow('role');
142
+ expect(() => ChannelsUtil.updateAgent(current, { agent: 'dev', face: FACES })).toThrow('A face');
143
+ expect(() => ChannelsUtil.updateAgent(current, { agent: 'nobody' })).toThrow('No agent is nobody.');
144
+ });
145
+
146
+ it('deletes an agent from its channels, and the channels it was alone in', () => {
147
+ let { team: current } = team();
148
+ current = ChannelsUtil.addChannel(current, { name: 'solo', members: ['designer'] }).team;
149
+ current = ChannelsUtil.changeMember(current, { channel: 'web', agent: 'designer', change: 'mute' }).team;
150
+ const { team: next, agent, channels } = ChannelsUtil.removeAgent(current, 'designer');
151
+ expect(agent.name).toBe('designer');
152
+ expect(channels.map(channel => channel.name)).toEqual(['solo']);
153
+ expect(next.agents.map(other => other.name)).toEqual(['dev', 'dev-lead']);
154
+ expect(next.channels).toHaveLength(1);
155
+ expect(next.channels[0].members).toEqual([ChannelsUtil.findAgent(current, 'dev')!.id]);
156
+ expect(next.channels[0].muted).toBeUndefined();
157
+ expect(() => ChannelsUtil.removeAgent(next, 'designer')).toThrow('No agent is designer.');
158
+ });
159
+
160
+ it('renames a channel, changes or clears its topic, and pins it first', () => {
161
+ let { team: current } = team();
162
+ current = ChannelsUtil.addChannel(current, { name: 'api', members: ['dev'] }).team;
163
+ const renamed = ChannelsUtil.updateChannel(current, { channel: '#web', name: ' Site ', topic: ' The new page ' });
164
+ expect(renamed.channel).toMatchObject({ name: 'Site', topic: 'The new page' });
165
+ expect(renamed.before.name).toBe('web');
166
+ expect(ChannelsUtil.updateChannel(renamed.team, { channel: 'Site', topic: ' ' }).channel.topic).toBeUndefined();
167
+ expect(() => ChannelsUtil.updateChannel(current, { channel: 'web', name: 'API' })).toThrow('There is a channel named API already.');
168
+ expect(ChannelsUtil.updateChannel(current, { channel: 'web', name: 'WEB' }).channel.name).toBe('WEB');
169
+ expect(() => ChannelsUtil.updateChannel(current, { channel: 'web', name: '#' })).toThrow('The name of the channel');
170
+ expect(() => ChannelsUtil.updateChannel(current, { channel: 'nope', pinned: true })).toThrow('No channel is named nope.');
171
+ const pinned = ChannelsUtil.updateChannel(current, { channel: 'api', pinned: true }).team;
172
+ expect(ChannelsUtil.pinnedFirst(pinned.channels).map(channel => channel.name)).toEqual(['api', 'web']);
173
+ const unpinned = ChannelsUtil.updateChannel(pinned, { channel: 'api', pinned: false }).channel;
174
+ expect('pinned' in unpinned).toBe(false);
175
+ });
176
+
177
+ it('writes the messages of a channel as Markdown, under the names of who wrote them', () => {
178
+ const { team: current, channel } = team();
179
+ const dev = ChannelsUtil.findAgent(current, 'dev')!;
180
+ const messages = [message(USER, 'Fix the header'), message(SYSTEM, 'stopped'), message(dev.id, ' Done. '), { ...message(TA, 'Hm'), failed: true }];
181
+ expect(ChannelsUtil.channelMarkdown(current, channel, messages, { user: 'You', ta: 'Ta' })).toBe(
182
+ '# #web\n\n> The page\n\n## You\n\nFix the header\n\n## dev\n\nDone.\n\n## Ta (failed)\n\nHm\n',
183
+ );
184
+ });
185
+
186
+ it('writes the conversation of an agent as Markdown, without the context of channels', () => {
187
+ const dev = ChannelsUtil.findAgent(team().team, 'dev')!;
188
+ const text = ChannelsUtil.markdown(
189
+ dev,
190
+ [
191
+ { role: 'user', content: '<context>\n[#web]\n</context>\nFix the header' },
192
+ {
193
+ role: 'assistant',
194
+ content: [
195
+ { type: 'thinking', thinking: 'hm' },
196
+ { type: 'text', text: 'Done.' },
197
+ ],
198
+ },
199
+ { role: 'toolResult', content: [{ type: 'text', text: 'ok' }] },
200
+ ],
201
+ { user: 'You' },
202
+ );
203
+ expect(text).toBe('# dev\n\n> You write the code.\n\n## You\n\nFix the header\n\n## dev\n\nDone.\n');
204
+ });
205
+
206
+ it('stops a round past the hops, the answers of the round and those of one agent, and says so once', () => {
207
+ let round = ChannelsUtil.newRound();
208
+ let step = ChannelsUtil.limit(round, ['a', 'b'], 0);
209
+ expect(step).toMatchObject({ allowed: ['a', 'b'], cut: false, note: false });
210
+ step = ChannelsUtil.limit(step.round, ['a', 'c'], MAX_HOPS);
211
+ expect(step).toMatchObject({ allowed: [], cut: true, note: true, round: { stopped: true } });
212
+ // Said once a round.
213
+ expect(ChannelsUtil.limit(step.round, ['c'], MAX_HOPS)).toMatchObject({ allowed: [], cut: true, note: false });
214
+
215
+ round = ChannelsUtil.newRound();
216
+ for (let i = 0; i < MAX_AGENT_REPLIES; i++) {
217
+ round = ChannelsUtil.limit(round, ['a'], 1).round;
218
+ }
219
+ expect(ChannelsUtil.limit(round, ['a', 'b'], 1)).toMatchObject({ allowed: ['b'], cut: true, note: true });
220
+
221
+ const many = Array.from({ length: MAX_REPLIES + 2 }, (_, i) => `agent${i}`);
222
+ const all = ChannelsUtil.limit(ChannelsUtil.newRound(), many, 0);
223
+ expect(all.allowed).toEqual(many.slice(0, MAX_REPLIES));
224
+ expect(all.round.total).toBe(MAX_REPLIES);
225
+ expect(ChannelsUtil.limit(all.round, ['agent0'], 1).allowed).toEqual([]);
226
+ });
227
+
228
+ it('sends the line of the channel to nobody', () => {
229
+ const { team: current, channel } = team();
230
+ expect(ChannelsUtil.targets(current, channel, message(SYSTEM, '@dev stopped'))).toEqual([]);
231
+ });
232
+
233
+ it('tells an agent the channel, its members, what was said and what to answer', () => {
234
+ const { team: current, channel } = team();
235
+ const [, dev] = current.agents;
236
+ const history = [message(USER, 'We need a page.'), message(dev.id, 'On it.')];
237
+ const last = message(TA, '@dev the header?');
238
+ const text = ChannelsUtil.prompt(current, channel, [...history, last], last, dev);
239
+ expect(text).toBe(
240
+ [
241
+ '<context>',
242
+ '[#web] The page',
243
+ 'Members:',
244
+ '- @dev (you): You write the code.',
245
+ '- @designer: You design pages.',
246
+ '',
247
+ 'Recent messages:',
248
+ 'user: We need a page.',
249
+ 'dev: On it.',
250
+ '',
251
+ 'Ta to you:',
252
+ '</context>',
253
+ '@dev the header?',
254
+ ].join('\n'),
255
+ );
256
+ expect(ChannelsUtil.systemPrompt(dev)).toContain('You are dev, one of the agents of Ta. Your role: You write the code.');
257
+ });
258
+
259
+ it('gives each agent a face of its own while some are free, and keeps it', () => {
260
+ let current = ChannelsUtil.emptyTeam();
261
+ for (let i = 0; i < FACES; i++) {
262
+ current = ChannelsUtil.addAgent(current, { name: `a${i}`, role: 'x' }).team;
263
+ }
264
+ const faces = current.agents.map(agent => agent.face);
265
+ expect(new Set(faces).size).toBe(FACES);
266
+ expect(faces.every(face => face !== undefined && face >= 0 && face < FACES)).toBe(true);
267
+ expect([...ChannelsUtil.faces(current.agents).values()]).toEqual(faces as number[]);
268
+ // Agents made before faces get one by their id, not one taken, and the same each time.
269
+ const old = [
270
+ { ...current.agents[0], face: 3 },
271
+ { ...current.agents[1], id: 'agent-old', face: undefined },
272
+ { ...current.agents[2], id: 'agent-older', face: undefined },
273
+ ];
274
+ const assigned = ChannelsUtil.faces(old);
275
+ expect(new Set(assigned.values()).size).toBe(3);
276
+ expect(ChannelsUtil.faces(old)).toEqual(assigned);
277
+ expect(ChannelsUtil.faces([{ ...old[0], face: 99 }]).get(old[0].id)).toBe(ChannelsUtil.hash(old[0].id) % FACES);
278
+ });
279
+
280
+ it('takes the face the user picked and finds a name no agent has', () => {
281
+ let current = ChannelsUtil.addAgent(ChannelsUtil.emptyTeam(), { name: 'new-agent', role: DEFAULT_ROLE, face: 5 }).team;
282
+ expect(current.agents[0].face).toBe(5);
283
+ expect(ChannelsUtil.addAgent(current, { name: 'x', role: 'y', face: 99 }).agent.face).not.toBe(99);
284
+ expect(ChannelsUtil.freeName(current, 'new-agent')).toBe('new-agent-2');
285
+ current = ChannelsUtil.addAgent(current, { name: 'new-agent-2', role: 'y' }).team;
286
+ expect(ChannelsUtil.freeName(current, 'new-agent')).toBe('new-agent-3');
287
+ expect(ChannelsUtil.freeName(current, 'other')).toBe('other');
288
+ expect(ChannelsUtil.freeFaceFor(current.agents, 'seed')).not.toBe(5);
289
+ });
290
+
291
+ it('holds a limited number of agents in a channel', () => {
292
+ let current = ChannelsUtil.emptyTeam();
293
+ for (let i = 0; i <= MAX_MEMBERS; i++) {
294
+ current = ChannelsUtil.addAgent(current, { name: `a${i}`, role: 'x' }).team;
295
+ }
296
+ const names = current.agents.map(agent => agent.name);
297
+ expect(() => ChannelsUtil.addChannel(current, { name: 'all', members: names })).toThrow('at most');
298
+ current = ChannelsUtil.addChannel(current, { name: 'most', members: names.slice(0, MAX_MEMBERS) }).team;
299
+ expect(() => ChannelsUtil.addMembers(current, { channel: 'most', members: [names[MAX_MEMBERS]] })).toThrow('at most');
300
+ });
301
+
302
+ it('keeps the last messages of a channel, and sums the team up for the model', () => {
303
+ let messages: ChannelMessage[] = [];
304
+ for (let i = 0; i < MAX_MESSAGES + 5; i++) {
305
+ messages = ChannelsUtil.append(messages, message(USER, String(i)));
306
+ }
307
+ expect(messages).toHaveLength(MAX_MESSAGES);
308
+ expect(messages[0].text).toBe('5');
309
+ expect(ChannelsUtil.summary(ChannelsUtil.emptyTeam())).toBe('No agents and no channels yet.');
310
+ expect(ChannelsUtil.summary(team().team)).toContain('- #web (The page): @dev, @designer');
311
+ });
312
+ });