@kybernesis/buzz 0.4.2 → 0.4.4

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.
Files changed (2) hide show
  1. package/dist/bridge.js +37 -6
  2. package/package.json +1 -1
package/dist/bridge.js CHANGED
@@ -24,7 +24,29 @@ export function buzzBridge(options) {
24
24
  const key = loadKey(options.keyFile);
25
25
  const identity = channelIdentity({ issuer: options.issuer, credential: options.credential });
26
26
  const log = options.onLog ?? ((message) => console.log(new Date().toISOString().slice(11, 19), message));
27
- /** One conversation per channel, which is how a channel already behaves. */
27
+ /**
28
+ * One conversation per channel — the SESSION ID, not a handle to it.
29
+ *
30
+ * @remarks
31
+ * This distinction is the difference between multiplayer working and quietly
32
+ * not. A session handle carries the client that made it, and that client
33
+ * carries one person's bearer token. Cache the handle and every later turn in
34
+ * the channel runs as whoever spoke first: a second person asks about their
35
+ * own mail and is shown the first person's, with nothing anywhere reporting
36
+ * an error.
37
+ *
38
+ * So the id is shared and the credentials are not. Each turn attaches to the
39
+ * same conversation through a client built for the person sending it, which
40
+ * is what the runtime reads to decide whose memory and whose connections the
41
+ * turn may touch.
42
+ *
43
+ * The stream position is kept beside the id for a reason that cost an
44
+ * afternoon: attaching defaults to index 0, so a turn reads the conversation
45
+ * from its beginning and reports the FIRST completed message it finds — the
46
+ * previous person's answer. The turn itself runs correctly, as the right
47
+ * person, against the right connections; only the text posted back is
48
+ * somebody else's, which is the most convincing way to look broken.
49
+ */
28
50
  const sessions = new Map();
29
51
  /** Who has already been sent a link, so a room full of strangers is not a room full of spam. */
30
52
  const invited = new Map();
@@ -37,11 +59,17 @@ export function buzzBridge(options) {
37
59
  headers: bundle ? { "x-kybernesis-bundle": bundle } : {},
38
60
  });
39
61
  async function ask(channel, text, token, bundle) {
62
+ // Built per turn, for the person whose turn it is.
63
+ const client = clientFor(token, bundle);
40
64
  const existing = sessions.get(channel);
41
65
  if (existing) {
42
66
  try {
43
- const response = await existing.send(text);
44
- return (await response.result()).message ?? "";
67
+ const session = client.sessions.attach(existing.id, { streamIndex: existing.streamIndex });
68
+ const response = await session.send(text);
69
+ const result = await response.result();
70
+ // Where this turn left the conversation, so the next one starts after it.
71
+ sessions.set(channel, { id: existing.id, streamIndex: session.state.streamIndex });
72
+ return result.message ?? "";
45
73
  }
46
74
  catch (error) {
47
75
  // A session the agent no longer holds cannot be resumed. Starting a new one loses the
@@ -50,10 +78,13 @@ export function buzzBridge(options) {
50
78
  sessions.delete(channel);
51
79
  }
52
80
  }
53
- const client = clientFor(token, bundle);
54
81
  const created = await client.sessions.create({ message: text });
55
- sessions.set(channel, created.session);
56
- return (await created.response.result()).message ?? "";
82
+ const message = (await created.response.result()).message ?? "";
83
+ sessions.set(channel, {
84
+ id: created.session.state.sessionId,
85
+ streamIndex: created.session.state.streamIndex,
86
+ });
87
+ return message;
57
88
  }
58
89
  /**
59
90
  * Send someone the link that makes them known, privately.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kybernesis/buzz",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "description": "Put an eve agent in a Buzz workspace as a member: per-speaker verified identity resolved through the control plane, self-service identity linking, presence, typing and seen signals.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",