@clawchatsai/connector 0.1.34 → 0.1.36

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.
@@ -8,7 +8,7 @@ import { SessionLens } from './session-lens.js';
8
8
  const AGENTS = [{ id: 'main', identity: {} }, { id: 'dev', identity: { name: 'Dev' } }, { id: 'atlas', identity: { name: 'Atlas' } }];
9
9
 
10
10
  /** Fake gateway. `reply(agentId, prompt)` returns the agent's reply text, null (abort) or a promise. */
11
- function harness(reply, { preparedTitle = 'Team title', titler = null, remote = null } = {}) {
11
+ function harness(reply, { preparedTitle = 'Team title', titler = null, remote = null, agents = AGENTS } = {}) {
12
12
  const sessions = new Map(); // key -> [{ role, content, timestamp, __m: { id } }]
13
13
  const calls = [];
14
14
  let clock = 1_000, seq = 0;
@@ -17,7 +17,7 @@ function harness(reply, { preparedTitle = 'Team title', titler = null, remote =
17
17
  const request = async (method, params) => {
18
18
  calls.push({ method, params });
19
19
  switch (method) {
20
- case 'agents.list': return { agents: AGENTS };
20
+ case 'agents.list': return { agents };
21
21
  case 'sessions.create': sessions.set(params.key, []); return { ok: true, key: params.key };
22
22
  case 'sessions.describe': return { session: { key: params.key } };
23
23
  case 'sessions.title.prepare': return { title: preparedTitle };
@@ -467,3 +467,140 @@ test('rounds: a per-room host setting, validated', async () => {
467
467
  await h.settle();
468
468
  assert.ok(h.calls.filter(c => c.method === 'chat.send').length > 4, 'more rounds when allowed');
469
469
  });
470
+
471
+ /**
472
+ * Two gateways: Houman hosts (gwH), Kamil is a member (gwK). Kamil shares Jarvis (guest) with Houman
473
+ * as share sh1; Houman shares dev with Kamil. Each side's `remote` is a fake sharing manager whose
474
+ * personRequest calls the other coordinator's onRoom, as the peer link would.
475
+ */
476
+ function twoGateways({ devShared = true } = {}) {
477
+ const forCalls = [];
478
+ const sides = {};
479
+ const mk = (self, other, name) => ({
480
+ gatewayId: () => self,
481
+ selfName: () => name,
482
+ contact: gw => (gw === other ? { personId: other, name: other === 'gwK' ? 'Kamil' : 'Houman', email: null } : null),
483
+ personRequest: async (gw, method, params) => {
484
+ if (gw !== other) throw Object.assign(new Error('not connected'), { code: 'not_connected' });
485
+ return sides[other].team.onRoom(method, JSON.parse(JSON.stringify(params)), self);
486
+ },
487
+ ownerOf: id => (String(id).startsWith('peer:sh1:') && self === 'gwH' ? 'gwK' : null),
488
+ ownsShare: shareId => shareId === 'sh1' && self === 'gwK',
489
+ remoteAgents: () => (self === 'gwH' ? [{ agentId: 'peer:sh1:jarvis-guest', name: 'Jarvis · Kamil', ownerName: 'Kamil' }] : []),
490
+ turn: async () => ({ state: 'final', text: 'guest jarvis here' }),
491
+ abort: async () => {},
492
+ actingForName: () => 'Kamil',
493
+ localTurnFor: async (localId, who, args) => {
494
+ forCalls.push({ localId, who, args });
495
+ return devShared ? { state: 'final', text: `${localId} for kamil` } : null;
496
+ },
497
+ });
498
+ sides.gwH = harness(agent => `${agent} full`, { remote: mk('gwH', 'gwK', 'Houman Satarian') });
499
+ sides.gwK = harness(agent => `${agent} full on kamil`, {
500
+ remote: mk('gwK', 'gwH', 'Kamil Gronowski'),
501
+ agents: [{ id: 'main', identity: {} }, { id: 'jarvis', name: 'Jarvis' }, { id: 'jarvis-guest', name: 'Jarvis (guest)' }],
502
+ });
503
+ const settle = async () => {
504
+ for (let i = 0; i < 6; i++) {
505
+ await new Promise(r => setTimeout(r, 350)); // pushes are batched (300 ms)
506
+ await sides.gwH.settle(); await sides.gwK.settle();
507
+ for (const c of sides.gwK.team._syncChains?.values() || []) await c.catch(() => {});
508
+ }
509
+ };
510
+ return { H: sides.gwH, K: sides.gwK, settle, forCalls };
511
+ }
512
+
513
+ test('people: a person gets a live copy, posts back, their own agent runs on their gateway, removal ends it', async () => {
514
+ const { H, K, settle, forCalls } = twoGateways();
515
+ const { roomKey } = await H.team.createRoom({ agentIds: ['dev', 'peer:sh1:jarvis-guest'] });
516
+ await H.team.send(roomKey, { text: 'before kamil joins', userLabel: 'Houman' });
517
+ await settle();
518
+ await H.team.addPerson(roomKey, 'gwK', { history: 'all' });
519
+ await settle();
520
+
521
+ // Kamil's copy: a local room session, hosted by Houman, with the history (Houman chose "all").
522
+ const copy = K.team.rooms().find(r => r.replica);
523
+ assert.ok(copy, 'a live copy exists on Kamil\'s gateway');
524
+ assert.equal(copy.replica.hostName, 'Houman Satarian');
525
+ assert.ok(K.room(copy.roomKey).some(t => t.endsWith('before kamil joins')));
526
+ assert.deepEqual(copy.people.map(p => [p.name, !!p.host, !!p.me]), [['Houman Satarian', true, false], ['Kamil', false, true]]);
527
+ assert.ok(copy.agents.find(a => a.agentId === 'peer:sh1:jarvis-guest').mine, 'Kamil sees Jarvis as his own agent');
528
+ assert.ok(K.events.some(e => e.event === 'team-invited'));
529
+
530
+ // Kamil asks his own Jarvis: it runs at full strength on Kamil's gateway; Houman's never runs it.
531
+ const kSends = () => K.calls.filter(c => c.method === 'chat.send').map(c => c.params.sessionKey);
532
+ await K.team.send(copy.roomKey, { text: '@Jarvis-Kamil check yours' });
533
+ await settle();
534
+ assert.ok(kSends().some(k => k.startsWith('agent:jarvis:')), 'the real Jarvis ran on Kamil\'s gateway');
535
+ assert.ok(!kSends().some(k => k.startsWith('agent:jarvis-guest:')));
536
+ const hostRoom = H.room(roomKey);
537
+ assert.ok(hostRoom.includes('[Kamil]\n\n@Jarvis-Kamil check yours'), 'Kamil\'s message is in the host room under his name');
538
+ assert.ok(hostRoom.includes('[Jarvis · Kamil]\n\njarvis full on kamil'), 'Jarvis\'s reply reached the host room');
539
+ const hAuthors = H.team.room(roomKey).authors;
540
+ assert.ok(Object.values(hAuthors).some(a => a.type === 'person' && a.personId === 'gwK' && a.name === 'Kamil'));
541
+ // ...and came back to Kamil's copy, where his own message shows as his.
542
+ const kAuthors = K.team.room(copy.roomKey).authors;
543
+ assert.ok(Object.values(kAuthors).some(a => a.type === 'user'));
544
+ assert.ok(K.room(copy.roomKey).some(t => t.endsWith('jarvis full on kamil')));
545
+
546
+ // Kamil asks Houman's dev: dev answers him as the version shared with him, never at full strength.
547
+ const devFull = () => H.calls.filter(c => c.method === 'chat.send' && c.params.sessionKey.startsWith('agent:dev:')).length;
548
+ const before = devFull();
549
+ await K.team.send(copy.roomKey, { text: '@dev what version are you on?' });
550
+ await settle();
551
+ assert.equal(devFull(), before);
552
+ assert.deepEqual(forCalls.map(c => [c.localId, c.who]), [['dev', 'person:gwK']]);
553
+ assert.ok(H.room(roomKey).includes('[Dev]\n\ndev for kamil'));
554
+ assert.ok(Object.values(H.team.room(roomKey).authors).some(a => a.agentId === 'dev' && a.actingFor === 'Kamil'));
555
+
556
+ // Houman removes Kamil: Jarvis leaves with him, the copy ends, sending fails.
557
+ H.team.removePerson(roomKey, 'gwK');
558
+ await settle();
559
+ assert.ok(!H.team.room(roomKey).agents.some(a => a.agentId === 'peer:sh1:jarvis-guest'));
560
+ assert.equal(K.team.room(copy.roomKey).replica.ended, 'removed');
561
+ await assert.rejects(K.team.send(copy.roomKey, { text: 'still here?' }), /removed you/);
562
+ });
563
+
564
+ test("people: adding someone's agent brings them in; 'now' hides the earlier chat; only the host changes the room", async () => {
565
+ const { H, K, settle } = twoGateways();
566
+ const { roomKey } = await H.team.createRoom({ agentIds: ['dev', 'atlas'] });
567
+ await H.team.send(roomKey, { text: 'secret plan', userLabel: 'Houman' });
568
+ await settle();
569
+ await H.team.addAgent(roomKey, 'peer:sh1:jarvis-guest', { history: 'now' });
570
+ await settle();
571
+ assert.deepEqual(H.team.room(roomKey).people.map(p => p.personId), ['gwK']);
572
+ const copy = K.team.rooms().find(r => r.replica);
573
+ assert.ok(!K.room(copy.roomKey).some(t => t.includes('secret plan')), "'from now on': nothing earlier");
574
+ await assert.rejects(K.team.addAgent(copy.roomKey, 'main'), /host/);
575
+ await assert.rejects(K.team.onRoom('room.post', { roomId: 'agent:dev:dashboard:nope', text: 'x' }, 'gwK'), /not in this team chat/);
576
+ // A stranger can't post into the room.
577
+ await assert.rejects(H.team.onRoom('room.post', { roomId: roomKey, text: 'hi' }, 'gwX'), /not in this team chat/);
578
+ // Kamil leaves: he's out, and so is Jarvis.
579
+ await K.team.leave(copy.roomKey);
580
+ await settle();
581
+ assert.equal(H.team.room(roomKey).people.length, 0);
582
+ assert.equal(K.team.room(copy.roomKey).replica.ended, 'left');
583
+ });
584
+
585
+ test('a chat turned into a team chat: agents added later get its earlier history on their first turn, unless "now"', async () => {
586
+ const h = harness((agent, prompt) => `${agent}: ${/Earlier in this chat/.test(prompt) ? 'saw history' : 'no history'}`);
587
+ // the original chat with its history
588
+ h.sessions.set('agent:main:dashboard:orig', [
589
+ { role: 'user', content: 'how do I fix the gateway crash?', timestamp: 1, __m: { id: 'o1' } },
590
+ { role: 'assistant', content: [{ type: 'text', text: 'restart with --safe' }], timestamp: 2, __m: { id: 'o2' } },
591
+ ]);
592
+ const { roomKey } = await h.team.createRoom({ agentIds: ['dev'], sourceKey: 'agent:main:dashboard:orig' });
593
+ await h.team.addAgent(roomKey, 'atlas', { history: 'now' });
594
+ await h.team.send(roomKey, { text: '@dev thoughts?', userLabel: 'H' });
595
+ await h.settle();
596
+ const devPrompt = h.calls.find(c => c.method === 'chat.send' && c.params.sessionKey.startsWith('agent:dev:')).params.message;
597
+ assert.match(devPrompt, /Earlier in this chat[\s\S]*how do I fix the gateway crash\?[\s\S]*restart with --safe/);
598
+ assert.ok(h.room(roomKey).includes('[Dev]\n\ndev: saw history'));
599
+ // "From now on": no earlier history, and it starts at the newest entry.
600
+ const atlas = h.team.store.getRoom(roomKey).members.find(m => m.agentId === 'atlas');
601
+ assert.equal(atlas.history, 'now');
602
+ assert.equal(await h.team._earlier(h.team.store.getRoom(roomKey), atlas).then(t => !!t), true, 'history exists, the turn skips it for "now"');
603
+ // The chat's own agent already has it in its session.
604
+ const main = h.team.store.getRoom(roomKey).members.find(m => m.agentId === 'main');
605
+ assert.equal(await h.team._earlier(h.team.store.getRoom(roomKey), main), '');
606
+ });