pi-roundtable 0.45.2 → 0.46.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/docs/plugins.md +32 -7
  3. package/package.json +1 -1
  4. package/src/characters/compose.ts +7 -3
  5. package/src/characters/index.ts +1 -0
  6. package/src/characters/parts-face.ts +4 -1
  7. package/src/core/agents/agent-claim.ts +2 -2
  8. package/src/core/agents/agent-group-store.ts +74 -1
  9. package/src/core/agents/agent-places.ts +32 -0
  10. package/src/core/agents/agent-ports.ts +0 -94
  11. package/src/core/agents/agent-prompt.ts +3 -1
  12. package/src/core/agents/agent-team-fixture.ts +48 -7
  13. package/src/core/agents/agent-team.ts +54 -59
  14. package/src/core/agents/agent-tools.ts +34 -166
  15. package/src/core/agents/message-target.ts +3 -2
  16. package/src/core/agents/routes.ts +2 -1
  17. package/src/core/agents/team-editing.ts +121 -150
  18. package/src/core/agents/team-helpers.ts +80 -59
  19. package/src/core/agents/team-keys.ts +16 -26
  20. package/src/core/agents/team-lifecycle.ts +155 -99
  21. package/src/core/agents/team-looks.ts +102 -0
  22. package/src/core/agents/team-options.ts +12 -11
  23. package/src/core/agents/team-place-writes.ts +193 -0
  24. package/src/core/agents/team-places.ts +30 -0
  25. package/src/core/agents/team-posts.ts +42 -73
  26. package/src/core/agents/team-selection.ts +10 -2
  27. package/src/core/agents/team-status.ts +3 -2
  28. package/src/core/agents/team-text.ts +13 -46
  29. package/src/core/agents/team-turn-types.ts +11 -16
  30. package/src/core/agents/team-turns.ts +7 -6
  31. package/src/core/builtin/agent-server-options.ts +27 -6
  32. package/src/core/builtin/agent-server.ts +65 -69
  33. package/src/core/builtin/discord.ts +58 -8
  34. package/src/core/builtin/modules.ts +2 -0
  35. package/src/core/contract/agent-channels.ts +97 -0
  36. package/src/core/contract/channels.ts +10 -4
  37. package/src/core/contract/discord-key.ts +15 -0
  38. package/src/core/contract/places.ts +8 -2
  39. package/src/core/db/legacy-channels.ts +31 -0
  40. package/src/core/discord/agent-dashboard-service.ts +55 -0
  41. package/src/core/{agents → discord}/agent-dashboard.ts +7 -2
  42. package/src/core/discord/agent-discord.ts +1 -1
  43. package/src/core/discord/agent-guild.ts +87 -0
  44. package/src/core/{agents/team-layout.ts → discord/agent-layout.ts} +9 -29
  45. package/src/core/discord/agent-places.ts +52 -33
  46. package/src/core/discord/agent-tools.ts +210 -0
  47. package/src/core/discord/connection.ts +4 -1
  48. package/src/core/discord/discord-surface.ts +9 -14
  49. package/src/core/discord/place-fake-client.ts +59 -0
  50. package/src/core/host/define-roundtable.ts +7 -5
  51. package/src/core/modules/delegation/delegator.ts +5 -0
  52. package/src/core/plugin-api/agent-ops.ts +123 -0
  53. package/src/core/plugin-api/agent-server-ids.ts +9 -0
  54. package/src/core/plugin-api/define.ts +9 -0
  55. package/src/core/plugin-api/plugin.ts +2 -0
  56. package/src/core/plugin-api/services.ts +6 -0
  57. package/src/core/registry/contributions.ts +3 -0
  58. package/src/core/routing/channel-queue.ts +28 -12
  59. package/src/core/runtime/pi-agent-runtime.ts +1 -1
  60. package/src/core/runtime/runtime-plugin.ts +1 -0
  61. package/src/core/runtime/runtime-types.ts +6 -0
  62. package/src/core/{agents/agent-team-fake-channels.ts → testing/fake-agent-channels.ts} +5 -1
  63. package/src/core/testing/fake-pager-places.ts +16 -2
  64. package/src/core/testing/modules.ts +2 -1
  65. package/src/core/testing/place-contract.ts +3 -2
  66. package/src/core/testing/team-places.ts +75 -7
  67. package/src/core/testing/test-host.ts +20 -16
  68. package/src/discord/index.ts +5 -0
  69. package/src/kit/channels.ts +1 -2
  70. package/src/kit/support.ts +9 -5
@@ -6,15 +6,21 @@ import { answersTo, helperSurface, isHelper } from "./agent-rules.ts";
6
6
  import type { Agent } from "./agent-store.ts";
7
7
  import type { HelpersEnded } from "./helper-ops.ts";
8
8
  import type { TeamContext } from "./team-options.ts";
9
+ import {
10
+ type MadePlace,
11
+ type PlaceWish,
12
+ TeamPlaceWrites,
13
+ } from "./team-place-writes.ts";
9
14
 
10
15
  /** How often the host looks for finished temp knights past their keep. */
11
16
  const SWEEP_EVERY_MS = 3_600_000;
12
17
 
13
18
  /**
14
19
  * The team's temp knights after they are made: which a knight has, which are finished, and their
15
- * archive, restore, and sweep. A temp knight made on Discord lives in a thread of its parent's
16
- * channel; one made in the web app gets that thread only once someone reaches for it on Discord.
17
- * Archiving one closes its thread, when it has one; an archived one keeps its history.
20
+ * archive, restore, and sweep. A temp knight made on a surface with stored places lives in a thread
21
+ * of its parent's place there; one made in a room gets that thread only once someone reaches for
22
+ * it on that surface. Archiving one closes its thread, when it has one; an archived one keeps its
23
+ * history.
18
24
  */
19
25
  export class TeamHelpers {
20
26
  readonly #ctx: TeamContext;
@@ -22,11 +28,13 @@ export class TeamHelpers {
22
28
  readonly #archiveAgent: (agent: Agent) => Promise<void>;
23
29
  readonly #ended: ((event: HelpersEnded) => void)[] = [];
24
30
  #sweeper: ReturnType<typeof setInterval> | undefined;
25
- /** The threads being made for web-born temp knights, by name, so two reaches make one. */
31
+ /** The threads being made for temp knights born in a room, by name and surface, so two reaches make one. */
26
32
  readonly #placing = new Map<string, Promise<void>>();
33
+ readonly #places: TeamPlaceWrites;
27
34
 
28
35
  constructor(ctx: TeamContext, archiveAgent: (agent: Agent) => Promise<void>) {
29
36
  this.#ctx = ctx;
37
+ this.#places = new TeamPlaceWrites(ctx);
30
38
  this.#archiveAgent = archiveAgent;
31
39
  }
32
40
 
@@ -90,8 +98,7 @@ export class TeamHelpers {
90
98
  }
91
99
 
92
100
  /**
93
- * Archives temp knights: each stops answering, keeps its history, and its Discord thread is
94
- * closed. Names that are no active temp knight are passed over. Says which were archived.
101
+ * Archives temp knights: each stops answering, keeps its history, and its thread is closed. Names that are no active temp knight are passed over. Says which were archived.
95
102
  */
96
103
  async archive(names: readonly string[]): Promise<string[]> {
97
104
  const { store } = this.#ctx.options;
@@ -105,19 +112,20 @@ export class TeamHelpers {
105
112
  return archived;
106
113
  }
107
114
 
108
- /** Archives a temp knight and closes its thread, when it has one; a thread that will not close is logged. */
115
+ /** Archives a temp knight and closes its thread, when it has one; a thread that will not close is logged by the provider. */
109
116
  async archiveOne(agent: Agent): Promise<void> {
110
- const { channels, logger } = this.#ctx.options;
111
117
  await this.#archiveAgent(agent);
112
- if (!agent.channelId || !channels?.closeThread) return;
113
- try {
114
- await channels.closeThread(agent.channelId);
115
- } catch (error) {
116
- logger.warn(
117
- { agent: agent.name, err: error },
118
- "temp knight's thread not closed; it stays silent where it is",
119
- );
120
- }
118
+ await this.#places.keep(this.#wish(agent), true);
119
+ }
120
+
121
+ /** The temp knight as a place owner: its name, shown under its display name. */
122
+ #wish(agent: Agent): PlaceWish {
123
+ return {
124
+ kind: "agent",
125
+ name: agent.name,
126
+ displayName: agent.displayName,
127
+ topic: agent.displayName,
128
+ };
121
129
  }
122
130
 
123
131
  /**
@@ -139,55 +147,63 @@ export class TeamHelpers {
139
147
  }
140
148
 
141
149
  /**
142
- * A `message_agent` call that names "discord", or names nothing from a Discord conversation, gives
143
- * a web-born temp knight its thread first.
150
+ * A `message_agent` call that names a surface with stored places, or names nothing from a
151
+ * conversation of one, gives a temp knight born in a room its thread there first.
144
152
  */
145
153
  async reach(
146
154
  caller: AgentTurnScope,
147
155
  to: string,
148
156
  conversation?: string,
149
157
  ): Promise<void> {
150
- const { store } = this.#ctx.options;
158
+ const { store, places } = this.#ctx.options;
151
159
  const { turns } = this.#ctx;
152
160
  const target = store.agent(to);
153
161
  const speaker = turns.speakerOf(caller);
154
162
  const from = turns.turnChannel(caller);
155
- // Unnamed, a message stays on the sender's surface, so one sent from Discord goes there too.
163
+ // Unnamed, a message stays on the sender's surface, so one sent from there goes there too.
156
164
  const wanted = conversation?.trim() || surfaceOf(from);
157
- if (wanted !== "discord" || !target || !speaker) return;
165
+ const provider = places.providerOf(wanted);
166
+ if (
167
+ provider?.surface !== wanted ||
168
+ provider.storage !== "stored" ||
169
+ !target ||
170
+ !speaker
171
+ )
172
+ return;
158
173
  if (answersTo(target, caller.name, from, speaker))
159
- await this.placeOnDiscord(to);
174
+ await this.placeOn(to, wanted);
160
175
  }
161
176
 
162
177
  /**
163
- * Gives an active temp knight made in the web app its thread under the parent's channel, the
164
- * first time someone reaches for it on Discord. One made on Discord, or before the origin was
165
- * kept, already has its place, and a host without Discord threads makes none; never rejects.
178
+ * Gives an active temp knight born in a room its thread under the parent's place on a surface,
179
+ * the first time someone reaches for it there. One made on that surface, or before the origin
180
+ * was kept, already has its place, and a surface that makes no threads gives none; never rejects.
166
181
  */
167
- placeOnDiscord(name: string): Promise<void> {
168
- const pending = this.#placing.get(name);
182
+ placeOn(name: string, surface: string): Promise<void> {
183
+ const key = `${name}@${surface}`;
184
+ const pending = this.#placing.get(key);
169
185
  if (pending) return pending;
170
- const placing = this.#placeOnDiscord(name).finally(() =>
171
- this.#placing.delete(name),
186
+ const placing = this.#placeOn(name, surface).finally(() =>
187
+ this.#placing.delete(key),
172
188
  );
173
- this.#placing.set(name, placing);
189
+ this.#placing.set(key, placing);
174
190
  return placing;
175
191
  }
176
192
 
177
- async #placeOnDiscord(name: string): Promise<void> {
178
- const { store, channels, logger } = this.#ctx.options;
193
+ async #placeOn(name: string, surface: string): Promise<void> {
194
+ const { store, places, logger } = this.#ctx.options;
179
195
  const agent = store.agent(name);
180
- const surface = helperSurface(agent);
181
- if (agent?.status !== "active" || agent.channelId) return;
182
- if (surface === undefined || surface === "discord") return;
196
+ const born = helperSurface(agent);
197
+ if (agent?.status !== "active" || places.idOf(this.#wish(agent), surface))
198
+ return;
199
+ if (born === undefined || born === surface) return;
183
200
  const parent = store.agent(agent.parent as string);
184
- if (!parent?.channelId || !channels?.createThread) return;
201
+ if (!parent) return;
202
+ const wish = this.#wish(agent);
203
+ let made: MadePlace | undefined;
185
204
  try {
186
- const channelId = await channels.createThread(
187
- parent.channelId,
188
- agent.displayName,
189
- );
190
- await store.updateAgent(name, { channelId });
205
+ made = await this.#places.makeOn(surface, wish, parent);
206
+ if (made) await this.#places.record(wish, [made]);
191
207
  } catch (error) {
192
208
  logger.warn(
193
209
  { agent: name, err: error },
@@ -195,6 +211,7 @@ export class TeamHelpers {
195
211
  );
196
212
  return;
197
213
  }
214
+ if (!made) return;
198
215
  logger.info(
199
216
  { agent: name, parent: parent.name },
200
217
  "temp knight's thread made",
@@ -204,33 +221,37 @@ export class TeamHelpers {
204
221
 
205
222
  /**
206
223
  * Restores one archived temp knight: its thread opens again, or a new one is made when the old
207
- * is gone. One made in the web app never gets a new thread: its old one reopens, if it had one
208
- * that is still there. It comes back finished, with nothing running.
224
+ * is gone. One born in a room never gets a new thread on another surface: its old one reopens,
225
+ * if it had one that is still there. It comes back finished, with nothing running.
209
226
  */
210
227
  async restoreOne(agent: Agent): Promise<void> {
211
- const { store, channels, logger } = this.#ctx.options;
228
+ const { store, places, logger } = this.#ctx.options;
212
229
  const parent = store.agent(agent.parent as string);
213
230
  if (parent?.status !== "active")
214
231
  throw new AgentError(
215
232
  `"${agent.name}" belongs to "${agent.parent}", which is archived; restore it first.`,
216
233
  );
217
- let { channelId } = agent;
218
- const surface = helperSurface(agent);
219
- const bornOnDiscord = surface === undefined || surface === "discord";
220
- if (channels?.createThread && parent.channelId) {
234
+ const wish = this.#wish(agent);
235
+ const born = helperSurface(agent);
236
+ const made: MadePlace[] = [];
237
+ for (const provider of places.stored()) {
238
+ const { surface } = provider;
239
+ const own = places.idOf(wish, surface);
240
+ const bornHere = born === undefined || born === surface;
241
+ if (own === undefined && !bornHere) continue;
242
+ // Away from where it was born, only a thread that is still there is opened again.
221
243
  if (
222
- channelId &&
223
- channels.openThread &&
224
- (await channels.exists(channelId))
244
+ own !== undefined &&
245
+ !bornHere &&
246
+ provider.check &&
247
+ (await provider.check(own).catch(() => "unknown")) !== "ok"
225
248
  )
226
- await channels.openThread(channelId);
227
- else if (bornOnDiscord)
228
- channelId = await channels.createThread(
229
- parent.channelId,
230
- agent.displayName,
231
- );
249
+ continue;
250
+ const place = await this.#places.makeOn(surface, wish, parent);
251
+ if (place) made.push(place);
232
252
  }
233
- await store.restoreAgent(agent.name, channelId);
253
+ await this.#places.record(wish, made);
254
+ await store.restoreAgent(agent.name);
234
255
  logger.info(
235
256
  { name: agent.name, parent: parent.name },
236
257
  "temp knight restored",
@@ -1,25 +1,9 @@
1
- import { channelKey, parseChannelKey } from "../contract/surface.ts";
1
+ import { channelKey } from "../contract/surface.ts";
2
2
  import type { ChannelKey } from "../domain/conversation.ts";
3
3
  import type { AgentDirectory } from "../plugin-api/services.ts";
4
4
  import type { Agent } from "./agent-store.ts";
5
5
  import type { TeamPlaces } from "./team-places.ts";
6
6
 
7
- /** The surface of the channels the agent server runs on. */
8
- const DISCORD_SURFACE = "discord";
9
-
10
- /** The key of a Discord channel. */
11
- export const discordKey = (channelId: string): ChannelKey =>
12
- channelKey(DISCORD_SURFACE, channelId);
13
-
14
- /** The Discord channel id of a key, or undefined when the key belongs to another surface. */
15
- export function discordIdOf(channel: ChannelKey): string | undefined {
16
- const { surface, id } = parseChannelKey(channel);
17
- return surface === DISCORD_SURFACE ? id : undefined;
18
- }
19
-
20
- /** The id part of a key the caller already knows is a Discord one, because the agent server owns it. */
21
- export const channelIdOf = (channel: ChannelKey) => parseChannelKey(channel).id;
22
-
23
7
  /** The key of an agent's room on a surface that serves agent rooms. */
24
8
  export const agentRoomKey = (surface: string, agent: string): ChannelKey =>
25
9
  channelKey(surface, `agent.${agent}`);
@@ -33,7 +17,6 @@ const ROOM_ID = /^(agent|group)\.([a-z0-9-]{1,32})$/;
33
17
  /**
34
18
  * Which agent's or group's room a key is, when its surface is one of `roomSurfaces` and its id has
35
19
  * the exact shape of a room; undefined for anything else, so no other key is ever taken for one.
36
- * Discord never hosts rooms.
37
20
  */
38
21
  export function roomOf(
39
22
  key: ChannelKey,
@@ -42,27 +25,34 @@ export function roomOf(
42
25
  const at = typeof key === "string" ? key.indexOf(":") : -1;
43
26
  if (at <= 0) return undefined;
44
27
  const surface = key.slice(0, at);
45
- if (surface === DISCORD_SURFACE || !roomSurfaces.includes(surface))
46
- return undefined;
28
+ if (!roomSurfaces.includes(surface)) return undefined;
47
29
  const match = ROOM_ID.exec(key.slice(at + 1));
48
30
  if (!match) return undefined;
49
31
  return { kind: match[1] as "agent" | "group", name: match[2] as string };
50
32
  }
51
33
 
34
+ /** The entry channel: the place a provider names for the coordinator, else the team's own option; undefined on a host without one. */
35
+ export function entryChannelOf(
36
+ places: Pick<TeamPlaces, "entry">,
37
+ team: { entryChannel?: ChannelKey },
38
+ ): ChannelKey | undefined {
39
+ return team.entryChannel ?? places.entry();
40
+ }
41
+
52
42
  /**
53
- * The coordinator: with Discord, the agent in the entry channel; without it, the agent the
54
- * configuration names. Undefined while none is active.
43
+ * The coordinator: the agent whose place is the entry channel, when the host has one; otherwise
44
+ * the agent the configuration names. Undefined while none is active.
55
45
  */
56
46
  export function coordinatorOf(
57
47
  store: Pick<AgentDirectory, "agent">,
58
- places: Pick<TeamPlaces, "agentOf">,
48
+ places: Pick<TeamPlaces, "agentOf" | "entry">,
59
49
  team: {
60
- entryChannelId?: string;
50
+ entryChannel?: ChannelKey;
61
51
  coordinator?: string | (() => string | undefined);
62
52
  },
63
53
  ): Agent | undefined {
64
- if (team.entryChannelId !== undefined)
65
- return places.agentOf(discordKey(team.entryChannelId));
54
+ const entry = entryChannelOf(places, team);
55
+ if (entry !== undefined) return places.agentOf(entry);
66
56
  const name =
67
57
  typeof team.coordinator === "function"
68
58
  ? team.coordinator()