pi-roundtable 0.44.0 → 0.45.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 (51) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/docs/plugins.md +36 -2
  3. package/package.json +2 -2
  4. package/src/core/agents/agent-claim.ts +15 -21
  5. package/src/core/agents/agent-messages.ts +10 -10
  6. package/src/core/agents/agent-prompt.ts +7 -8
  7. package/src/core/agents/agent-rules.ts +9 -2
  8. package/src/core/agents/agent-store.ts +16 -0
  9. package/src/core/agents/agent-team-fake-channels.ts +105 -0
  10. package/src/core/agents/agent-team-fixture.ts +30 -105
  11. package/src/core/agents/agent-team.ts +20 -21
  12. package/src/core/agents/group-turns.ts +8 -15
  13. package/src/core/agents/helper-modes.ts +7 -13
  14. package/src/core/agents/helper-work.ts +7 -6
  15. package/src/core/agents/message-target.ts +24 -12
  16. package/src/core/agents/room-progress.ts +8 -6
  17. package/src/core/agents/routes.ts +24 -47
  18. package/src/core/agents/team-editing.ts +5 -8
  19. package/src/core/agents/team-keys.ts +5 -190
  20. package/src/core/agents/team-lifecycle.ts +23 -39
  21. package/src/core/agents/team-options.ts +1 -1
  22. package/src/core/agents/team-places.ts +335 -0
  23. package/src/core/agents/team-posts.ts +157 -0
  24. package/src/core/agents/team-prompt.ts +3 -3
  25. package/src/core/agents/team-status.ts +18 -10
  26. package/src/core/agents/team-turn-types.ts +5 -8
  27. package/src/core/agents/team-turns.ts +26 -169
  28. package/src/core/builtin/agent-server-test-kit.ts +29 -0
  29. package/src/core/builtin/agent-server.ts +19 -12
  30. package/src/core/builtin/discord.ts +6 -0
  31. package/src/core/contract/places.ts +141 -0
  32. package/src/core/discord/agent-places.ts +215 -0
  33. package/src/core/discord/discord-surface.ts +13 -0
  34. package/src/core/host/define-roundtable.ts +1 -0
  35. package/src/core/plugin-api/agent-assets.ts +36 -0
  36. package/src/core/plugin-api/plugin-context.ts +30 -0
  37. package/src/core/plugin-api/plugin.ts +10 -28
  38. package/src/core/plugin-api/services.ts +20 -39
  39. package/src/core/registry/claims.ts +120 -0
  40. package/src/core/registry/contributions.ts +36 -118
  41. package/src/core/registry/place-port.ts +94 -0
  42. package/src/core/registry/places.ts +43 -0
  43. package/src/core/testing/fake-pager-places.ts +120 -0
  44. package/src/core/testing/fake-room-surface.ts +60 -0
  45. package/src/core/testing/modules.ts +5 -5
  46. package/src/core/testing/place-contract.ts +481 -0
  47. package/src/core/testing/team-places.ts +121 -0
  48. package/src/core/testing/test-host.ts +36 -10
  49. package/src/discord/index.ts +2 -0
  50. package/src/index.ts +12 -0
  51. package/src/testing.ts +15 -3
@@ -7,24 +7,19 @@ import { parseChannelKey } from "../contract/surface.ts";
7
7
  import type { ChannelKey } from "../domain/conversation.ts";
8
8
  import { AgentError, ConfigError } from "../domain/errors.ts";
9
9
  import type { AgentDirectory } from "../plugin-api/services.ts";
10
- import {
11
- agentRoomKey,
12
- discordKey,
13
- keysOf,
14
- roomSurfaceOf,
15
- } from "./team-keys.ts";
10
+ import type { TeamPlaces } from "./team-places.ts";
16
11
 
17
12
  /**
18
13
  * The routing table: where an agent's work lands when no person is waiting on it. An agent's
19
- * *place* on a surface is its Discord channel or its room there; its *primary place* is where
14
+ * *place* on a surface is its stored place or its room there; its *primary place* is where
20
15
  * unprompted work lands. Work a person started, or that waits on a person, is never routed.
21
16
  * Read it as `AGENTS.routes`.
22
17
  */
23
18
  export interface RoutingTable {
24
19
  /**
25
- * The agent's primary place: the surface its rules name; with `"auto"` (the default) its Discord
26
- * channel when it has one, else its room on the first surface that hosts rooms. Undefined for
27
- * an unknown agent.
20
+ * The agent's primary place: the surface its rules name; with `"auto"` (the default) its first
21
+ * stored place (its Discord channel when it has one), else its room on the first surface that
22
+ * hosts rooms. Undefined for an unknown agent.
28
23
  */
29
24
  primary(agent: string): ChannelKey | undefined;
30
25
  /** The agent's place on a surface (`"web"` for the one surface that hosts rooms); undefined when it has none there. */
@@ -62,29 +57,28 @@ export interface RoutesOptions {
62
57
  /** The checked `routing` block; without it every decision is what the host did before it existed. */
63
58
  routing?: ResolvedRouting | undefined;
64
59
  store: Pick<AgentDirectory, "agent" | "group">;
65
- /** The surfaces that host agent rooms, read each time: they are only known once the plugins linked. */
66
- roomSurfaces: () => readonly string[];
60
+ /**
61
+ * Where the agents are, read each time: the providers are only known once the plugins linked. A
62
+ * stored place of a surface no provider serves, such as a channel id a former guild left, is none.
63
+ */
64
+ places: Pick<
65
+ TeamPlaces,
66
+ "keysOf" | "placeOn" | "rooms" | "surfaces" | "roomSurfaceOf"
67
+ >;
67
68
  /** The conversation an agent last ran a turn in. */
68
69
  latest?: (agent: string) => ChannelKey | undefined;
69
70
  logger?: RoutesLogger;
70
- /**
71
- * Whether the team has Discord channels; default true. A team without them has no Discord place
72
- * for an agent: a channel id still stored on a row, such as one a former guild left, is ignored.
73
- */
74
- discordChannels?: boolean;
75
71
  }
76
72
 
77
73
  /** What the surfaces the host registered can hold, read when the agent server links. */
78
74
  export interface LinkedSurfaces {
79
75
  /** Every registered surface. */
80
76
  names: readonly string[];
81
- /** Whether the agents have Discord channels. */
82
- discordChannels: boolean;
83
77
  }
84
78
 
85
79
  /**
86
80
  * The one table that says where an agent's work lands. An agent's *place* on a surface is its
87
- * Discord channel or its room there; its *primary place* is where work no person started lands.
81
+ * stored place or its room there; its *primary place* is where work no person started lands.
88
82
  * Work a person started, or that waits on a person (a message, a card, a schedule's firing, an
89
83
  * exchange's answer), is never routed: it stays in the conversation they see. Every decision here
90
84
  * is what the host did before `routing` existed until the configuration says otherwise.
@@ -93,24 +87,10 @@ export class Routes implements RoutingTable {
93
87
  readonly #routing: ResolvedRouting | undefined;
94
88
  readonly #options: RoutesOptions;
95
89
  readonly #warned = new Set<string>();
96
- readonly #discordChannels: boolean;
97
90
 
98
91
  constructor(options: RoutesOptions) {
99
92
  this.#options = options;
100
93
  this.#routing = options.routing;
101
- this.#discordChannels = options.discordChannels ?? true;
102
- }
103
-
104
- /** The agent's places in order, its Discord channel first when the team has Discord channels. */
105
- #keys(name: string): ChannelKey[] {
106
- const keys = keysOf(
107
- this.#options.store,
108
- name,
109
- this.#options.roomSurfaces(),
110
- );
111
- return this.#discordChannels
112
- ? keys
113
- : keys.filter((key) => parseChannelKey(key).surface !== "discord");
114
94
  }
115
95
 
116
96
  /**
@@ -134,16 +114,12 @@ export class Routes implements RoutingTable {
134
114
  placeOn(name: string, surface: string): ChannelKey | undefined {
135
115
  const agent = this.#options.store.agent(name);
136
116
  if (!agent) return undefined;
137
- const rooms = this.#options.roomSurfaces();
117
+ const rooms = this.#options.places.rooms();
138
118
  const wanted =
139
119
  surface === WEB && !rooms.includes(WEB) && rooms.length === 1
140
120
  ? (rooms[0] as string)
141
121
  : surface;
142
- if (wanted === "discord")
143
- return agent.channelId && this.#discordChannels
144
- ? discordKey(agent.channelId)
145
- : undefined;
146
- return rooms.includes(wanted) ? agentRoomKey(wanted, name) : undefined;
122
+ return this.#options.places.placeOn(name, wanted);
147
123
  }
148
124
 
149
125
  /**
@@ -164,7 +140,7 @@ export class Routes implements RoutingTable {
164
140
  "the agent has no place on its primary surface; its auto place is used",
165
141
  );
166
142
  }
167
- return this.#keys(name)[0];
143
+ return this.#options.places.keysOf(name)[0];
168
144
  }
169
145
 
170
146
  /**
@@ -246,7 +222,7 @@ export class Routes implements RoutingTable {
246
222
  from === undefined ? undefined : parseChannelKey(from).surface;
247
223
  if (to === "latest" && surface !== undefined) {
248
224
  const last = latest?.(name);
249
- const keys = this.#keys(name);
225
+ const keys = this.#options.places.keysOf(name);
250
226
  if (
251
227
  last !== undefined &&
252
228
  keys.includes(last) &&
@@ -276,7 +252,8 @@ export class Routes implements RoutingTable {
276
252
  * surface hosts rooms, otherwise its primary place. Throws AgentError when it has none.
277
253
  */
278
254
  near(name: string, near?: ChannelKey): ChannelKey {
279
- const surface = near === undefined ? undefined : roomSurfaceOf(near);
255
+ const surface =
256
+ near === undefined ? undefined : this.#options.places.roomSurfaceOf(near);
280
257
  const room =
281
258
  surface === undefined ? undefined : this.placeOn(name, surface);
282
259
  const key = room ?? this.primary(name);
@@ -303,8 +280,8 @@ export class Routes implements RoutingTable {
303
280
  */
304
281
  validate(linked: LinkedSurfaces): void {
305
282
  if (!this.#routing) return;
306
- const rooms = this.#options.roomSurfaces();
307
- const holders = [...(linked.discordChannels ? ["discord"] : []), ...rooms];
283
+ const rooms = this.#options.places.rooms();
284
+ const holders = this.#options.places.surfaces();
308
285
  for (const [agent, rules] of Object.entries(this.#routing?.agents ?? {}))
309
286
  for (const key of ["primary", "integration", "agentMessage"] as const) {
310
287
  const surface = rules[key];
@@ -367,14 +344,14 @@ export function routesOf(options: {
367
344
  store: RoutesOptions["store"] & {
368
345
  conversations: { latest(agent: string): ChannelKey | undefined };
369
346
  };
370
- surfaces?: { roomSurfaces(): readonly string[] } | undefined;
347
+ places: RoutesOptions["places"];
371
348
  logger: RoutesLogger;
372
349
  }): Routes {
373
350
  return (
374
351
  options.routes ??
375
352
  new Routes({
376
353
  store: options.store,
377
- roomSurfaces: () => options.surfaces?.roomSurfaces() ?? [],
354
+ places: options.places,
378
355
  latest: (agent) => options.store.conversations.latest(agent),
379
356
  logger: options.logger,
380
357
  })
@@ -19,7 +19,6 @@ import type { Agent } from "./agent-store.ts";
19
19
  import type { AvatarMode } from "./agent-tools.ts";
20
20
  import { createHelper } from "./helper-create.ts";
21
21
  import { routesOf } from "./routes.ts";
22
- import { agentRoomKey, keysOf, seatsOf } from "./team-keys.ts";
23
22
  import { groupTopic, teamCategory } from "./team-layout.ts";
24
23
  import type { TeamContext } from "./team-options.ts";
25
24
 
@@ -242,7 +241,7 @@ export class TeamEditing {
242
241
  thinking?: string | undefined;
243
242
  } = {},
244
243
  ): Promise<{ agent: Agent; text: string }> {
245
- const { store, studio, channels, queue, skills, surfaces, logger } =
244
+ const { store, studio, channels, queue, skills, places, logger } =
246
245
  this.#ctx.options;
247
246
  this.#refuseSkillsOff(input.skills);
248
247
  const {
@@ -321,10 +320,9 @@ export class TeamEditing {
321
320
  "agent created",
322
321
  );
323
322
  this.#ctx.changed();
324
- const rooms = surfaces?.roomSurfaces() ?? [];
325
323
  // Before the opening task starts, so its turn already runs under the mode.
326
324
  if (modes)
327
- for (const key of keysOf(store, agent.name, rooms))
325
+ for (const key of places.keysOf(agent.name))
328
326
  await modes.set(key, { longRunning: true }, who.speaker);
329
327
  const avatar = `its picture is the ${character.class} look${modes ? "; long-running mode is on" : ""}`;
330
328
  const where = channelId ? ` in <#${channelId}>` : "";
@@ -340,7 +338,7 @@ export class TeamEditing {
340
338
  "opening",
341
339
  who.openIn === undefined
342
340
  ? undefined
343
- : agentRoomKey(who.openIn, agent.name),
341
+ : places.placeOn(agent.name, who.openIn),
344
342
  );
345
343
  if (!home) {
346
344
  logger.warn(
@@ -482,16 +480,15 @@ export class TeamEditing {
482
480
  name: string,
483
481
  change: { displayName?: string; members?: string[]; host?: string },
484
482
  ): Promise<string> {
485
- const { store, surfaces } = this.#ctx.options;
483
+ const { store, places } = this.#ctx.options;
486
484
  const before = store.group(name);
487
485
  const group = await store.updateGroup(name, change);
488
486
  // A member taken out starts its seat over, so an agent of that name that joins later
489
487
  // does not resume the old conversation.
490
488
  const runtime = this.#ctx.options.runtime();
491
- const rooms = surfaces?.roomSurfaces() ?? [];
492
489
  for (const left of before?.members ?? [])
493
490
  if (!group.members.includes(left))
494
- for (const seat of seatsOf(group, left, rooms))
491
+ for (const seat of places.seatsOf(group, left))
495
492
  await runtime.startFresh(seat);
496
493
  this.#ctx.retitle(group);
497
494
  this.#ctx.changed();
@@ -1,9 +1,8 @@
1
1
  import { channelKey, parseChannelKey } from "../contract/surface.ts";
2
2
  import type { ChannelKey } from "../domain/conversation.ts";
3
- import { AgentError } from "../domain/errors.ts";
4
- import type { AgentTurnScope } from "../domain/ports.ts";
5
3
  import type { AgentDirectory } from "../plugin-api/services.ts";
6
- import type { Agent, AgentGroup } from "./agent-store.ts";
4
+ import type { Agent } from "./agent-store.ts";
5
+ import type { TeamPlaces } from "./team-places.ts";
7
6
 
8
7
  /** The surface of the channels the agent server runs on. */
9
8
  const DISCORD_SURFACE = "discord";
@@ -50,98 +49,20 @@ export function roomOf(
50
49
  return { kind: match[1] as "agent" | "group", name: match[2] as string };
51
50
  }
52
51
 
53
- /** What the surfaces say of rooms: which of them host agent rooms, asked only for a key that is not a Discord one. */
54
- interface RoomHosts {
55
- roomSurfaces(): readonly string[];
56
- }
57
-
58
- /** The agent whose Discord channel or room the key is; an archived one is returned too. */
59
- export function agentOfChannel(
60
- store: Pick<AgentDirectory, "agent" | "agentByChannel">,
61
- hosts: RoomHosts | undefined,
62
- channel: ChannelKey,
63
- ): Agent | undefined {
64
- const id = discordIdOf(channel);
65
- if (id !== undefined) return store.agentByChannel(id);
66
- const room = roomOf(channel, hosts?.roomSurfaces() ?? []);
67
- return room?.kind === "agent" ? store.agent(room.name) : undefined;
68
- }
69
-
70
- /** The group whose Discord channel or room the key is; an archived one is returned too. */
71
- export function groupOfChannel(
72
- store: Pick<AgentDirectory, "group" | "groupByChannel">,
73
- hosts: RoomHosts | undefined,
74
- channel: ChannelKey,
75
- ): AgentGroup | undefined {
76
- const id = discordIdOf(channel);
77
- if (id !== undefined) return store.groupByChannel(id);
78
- const room = roomOf(channel, hosts?.roomSurfaces() ?? []);
79
- return room?.kind === "group" ? store.group(room.name) : undefined;
80
- }
81
-
82
- /** The surface of a key that is not a Discord channel's; undefined for a Discord channel. */
83
- export const roomSurfaceOf = (room: ChannelKey): string | undefined => {
84
- const at = typeof room === "string" ? room.indexOf(":") : -1;
85
- if (at <= 0) return undefined;
86
- const surface = room.slice(0, at);
87
- return surface === DISCORD_SURFACE ? undefined : surface;
88
- };
89
-
90
- /**
91
- * The channel a group seat runs in outside a running turn: the group's room on the surface of the
92
- * seat's home for a room seat, so it never falls out to the group's Discord channel; otherwise the
93
- * group's Discord channel, or the seat's home.
94
- */
95
- export function groupSeatChannel(
96
- group: Pick<AgentGroup, "name" | "channelId">,
97
- scope: Pick<AgentTurnScope, "home">,
98
- ): ChannelKey {
99
- const surface = roomSurfaceOf(scope.home);
100
- if (surface !== undefined) return groupRoomKey(surface, group.name);
101
- return group.channelId ? discordKey(group.channelId) : scope.home;
102
- }
103
-
104
- /**
105
- * An agent's conversation inside a group, apart from its own. In a Discord group channel, or
106
- * without a room, it is keyed by the channel; in a room of another surface, by the surface and the
107
- * group's name, so the room's seats are conversations of their own.
108
- */
109
- export function groupSessionKey(
110
- group: AgentGroup,
111
- agent: string,
112
- room?: ChannelKey,
113
- ): ChannelKey {
114
- const surface = room === undefined ? undefined : roomSurfaceOf(room);
115
- if (surface !== undefined)
116
- return `agentgroup:${surface}.group.${group.name}.${agent}`;
117
- // A group of a team without Discord has rooms only, so it never has a seat of its own to key.
118
- if (!group.channelId)
119
- throw new AgentError(`the group ${group.name} has no Discord channel`);
120
- return `agentgroup:${group.channelId}.${agent}`;
121
- }
122
-
123
- /**
124
- * The name a group's messages are logged under: the group's own in its Discord channel, and
125
- * `<group>@<surface>` in a room, so each conversation keeps its own history and cursors.
126
- */
127
- export function groupLogName(group: AgentGroup, room: ChannelKey): string {
128
- const surface = roomSurfaceOf(room);
129
- return surface === undefined ? group.name : `${group.name}@${surface}`;
130
- }
131
-
132
52
  /**
133
53
  * The coordinator: with Discord, the agent in the entry channel; without it, the agent the
134
54
  * configuration names. Undefined while none is active.
135
55
  */
136
56
  export function coordinatorOf(
137
- store: Pick<AgentDirectory, "agent" | "agentByChannel">,
57
+ store: Pick<AgentDirectory, "agent">,
58
+ places: Pick<TeamPlaces, "agentOf">,
138
59
  team: {
139
60
  entryChannelId?: string;
140
61
  coordinator?: string | (() => string | undefined);
141
62
  },
142
63
  ): Agent | undefined {
143
64
  if (team.entryChannelId !== undefined)
144
- return store.agentByChannel(team.entryChannelId);
65
+ return places.agentOf(discordKey(team.entryChannelId));
145
66
  const name =
146
67
  typeof team.coordinator === "function"
147
68
  ? team.coordinator()
@@ -149,109 +70,3 @@ export function coordinatorOf(
149
70
  const named = name ? store.agent(name) : undefined;
150
71
  return named?.status === "active" ? named : undefined;
151
72
  }
152
-
153
- /** An agent's own channel conversation. */
154
- export function homeScope(agent: Agent): AgentTurnScope {
155
- const home = discordKey(agent.channelId ?? "");
156
- return { name: agent.name, session: home, home };
157
- }
158
-
159
- /** An agent's conversation in its room: the room is its session and its home. */
160
- export function roomScope(agent: Agent, room: ChannelKey): AgentTurnScope {
161
- return { name: agent.name, session: room, home: room };
162
- }
163
-
164
- /** The agent's own conversation in the channel: its Discord channel's, or its room's. */
165
- export function scopeOf(agent: Agent, channel: ChannelKey): AgentTurnScope {
166
- return discordIdOf(channel) === undefined
167
- ? roomScope(agent, channel)
168
- : homeScope(agent);
169
- }
170
-
171
- /**
172
- * An agent's conversation inside a group: in a Discord group channel, or without a room, as
173
- * before; in a room, the room's seat, whose home is the agent's own room on that surface.
174
- */
175
- export function groupScope(
176
- agent: Agent,
177
- group: AgentGroup,
178
- room?: ChannelKey,
179
- ): AgentTurnScope {
180
- const surface = room === undefined ? undefined : roomSurfaceOf(room);
181
- return {
182
- name: agent.name,
183
- session: groupSessionKey(group, agent.name, room),
184
- home:
185
- surface === undefined
186
- ? discordKey(agent.channelId ?? "")
187
- : agentRoomKey(surface, agent.name),
188
- group: group.name,
189
- };
190
- }
191
-
192
- /**
193
- * The agent or group that owns a channel of the agent server: by Discord channel, or by room on
194
- * one of `roomSurfaces`. An archived agent or group owns nothing.
195
- */
196
- export function channelOwner(
197
- store: Pick<AgentDirectory, "agentByChannel" | "groupByChannel"> &
198
- Partial<Pick<AgentDirectory, "agent" | "group">>,
199
- channel: ChannelKey,
200
- roomSurfaces: readonly string[] = [],
201
- ): "agent" | "group" | undefined {
202
- const id = discordIdOf(channel);
203
- if (id === undefined) {
204
- const room = roomOf(channel, roomSurfaces);
205
- if (!room) return undefined;
206
- if (room.kind === "agent")
207
- return store.agent?.(room.name)?.status === "active"
208
- ? "agent"
209
- : undefined;
210
- return store.group?.(room.name)?.status === "active" ? "group" : undefined;
211
- }
212
- if (store.agentByChannel(id)) return "agent";
213
- if (store.groupByChannel(id)) return "group";
214
- return undefined;
215
- }
216
-
217
- /**
218
- * Every key an agent or group is addressed by: its Discord channel, when it has one, then its
219
- * room on each of `roomSurfaces`; none for a name the team does not know. Archived ones count,
220
- * so what they left behind can be cleared.
221
- */
222
- export function keysOf(
223
- store: Pick<AgentDirectory, "agent" | "group">,
224
- name: string,
225
- roomSurfaces: readonly string[],
226
- ): ChannelKey[] {
227
- const agent = store.agent(name);
228
- if (agent)
229
- return [
230
- ...(agent.channelId ? [discordKey(agent.channelId)] : []),
231
- ...roomSurfaces.map((surface) => agentRoomKey(surface, name)),
232
- ];
233
- const group = store.group(name);
234
- if (group)
235
- return [
236
- ...(group.channelId ? [discordKey(group.channelId)] : []),
237
- ...roomSurfaces.map((surface) => groupRoomKey(surface, name)),
238
- ];
239
- return [];
240
- }
241
-
242
- /**
243
- * Every conversation of an agent's seat in a group: the Discord channel's, when the group has
244
- * one, then each room's on `roomSurfaces`.
245
- */
246
- export function seatsOf(
247
- group: AgentGroup,
248
- agent: string,
249
- roomSurfaces: readonly string[],
250
- ): ChannelKey[] {
251
- return [
252
- ...(group.channelId ? [groupSessionKey(group, agent)] : []),
253
- ...roomSurfaces.map((surface) =>
254
- groupSessionKey(group, agent, groupRoomKey(surface, group.name)),
255
- ),
256
- ];
257
- }
@@ -12,18 +12,7 @@ import { isHelper, overBase } from "./agent-rules.ts";
12
12
  import type { Agent, AgentGroup } from "./agent-store.ts";
13
13
  import { refreshAvatars } from "./avatar-refresh.ts";
14
14
  import { TeamHelpers } from "./team-helpers.ts";
15
- import {
16
- agentOfChannel,
17
- coordinatorOf,
18
- discordIdOf,
19
- groupLogName,
20
- groupOfChannel,
21
- groupSessionKey,
22
- homeScope,
23
- keysOf,
24
- roomScope,
25
- seatsOf,
26
- } from "./team-keys.ts";
15
+ import { coordinatorOf } from "./team-keys.ts";
27
16
  import { categoryKind, groupTopic } from "./team-layout.ts";
28
17
  import type { TeamContext } from "./team-options.ts";
29
18
 
@@ -168,24 +157,20 @@ export class TeamLifecycle {
168
157
 
169
158
  /** Starts an agent's or every group member's conversation over; call inside the channel's queue. */
170
159
  async startFresh(channel: ChannelKey): Promise<void> {
171
- const { store, surfaces } = this.#ctx.options;
160
+ const { store, places } = this.#ctx.options;
172
161
  const runtime = this.#ctx.options.runtime();
173
- // A Discord channel and each room are conversations of their own: a room starts over alone.
174
- const agent = agentOfChannel(store, surfaces, channel);
162
+ // Each place is a conversation of its own: a room starts over alone.
163
+ const agent = places.agentOf(channel);
175
164
  if (agent?.status === "active") {
176
- await runtime.startFresh(
177
- discordIdOf(channel) === undefined
178
- ? roomScope(agent, channel).session
179
- : homeScope(agent).session,
180
- );
165
+ await runtime.startFresh(places.scopeOf(agent, channel).session);
181
166
  this.#ctx.changed();
182
167
  return;
183
168
  }
184
- const group = groupOfChannel(store, surfaces, channel);
169
+ const group = places.groupOf(channel);
185
170
  if (group?.status !== "active") return;
186
171
  for (const member of group.members)
187
- await runtime.startFresh(groupSessionKey(group, member, channel));
188
- await store.catchUp(group, groupLogName(group, channel));
172
+ await runtime.startFresh(places.seatKey(group, member, channel));
173
+ await store.catchUp(group, places.logName(group, channel));
189
174
  }
190
175
 
191
176
  // ── Archive ────────────────────────────────────────────────────────────
@@ -220,7 +205,11 @@ export class TeamLifecycle {
220
205
  );
221
206
  if (target.status !== "active")
222
207
  throw new AgentError(`"${name}" is already archived.`);
223
- if (agent && coordinatorOf(store, this.#ctx.options)?.name === name)
208
+ if (
209
+ agent &&
210
+ coordinatorOf(store, this.#ctx.options.places, this.#ctx.options)
211
+ ?.name === name
212
+ )
224
213
  throw new AgentError("The coordinator cannot be archived.");
225
214
  if (agent && name === caller?.name)
226
215
  throw new AgentError("You cannot archive yourself; ask another agent.");
@@ -328,11 +317,11 @@ export class TeamLifecycle {
328
317
  await this.remove(helper.name);
329
318
  // What a name keeps in the runtime would be picked up by the next agent or group made under it.
330
319
  const runtime = this.#ctx.options.runtime();
331
- const rooms = this.#rooms();
320
+ const { places } = this.#ctx.options;
332
321
  const seats = group
333
- ? group.members.flatMap((member) => seatsOf(group, member, rooms))
334
- : store.groups().flatMap((other) => seatsOf(other, name, rooms));
335
- const kept = [...keysOf(store, name, rooms), ...seats];
322
+ ? group.members.flatMap((member) => places.seatsOf(group, member))
323
+ : store.groups().flatMap((other) => places.seatsOf(other, name));
324
+ const kept = [...places.keysOf(name), ...seats];
336
325
  for (const key of kept) await runtime.deleteConversation(key);
337
326
  if (agent) {
338
327
  await store.deleteAgent(name);
@@ -347,19 +336,19 @@ export class TeamLifecycle {
347
336
  async #archiveAgent(agent: Agent): Promise<void> {
348
337
  const { store, schedules, logger } = this.#ctx.options;
349
338
  const runtime = this.#ctx.options.runtime();
350
- const rooms = this.#rooms();
339
+ const { places } = this.#ctx.options;
351
340
  // A parent's temp knights are archived first: they have no place of their own once it is gone.
352
341
  for (const helper of store.helpersOf(agent.name, "active"))
353
342
  await this.helpers.archiveOne(helper);
354
343
  const left = await store.archiveAgent(agent.name);
355
- // The agent's Discord channel and each of its rooms are conversations of their own.
356
- for (const key of keysOf(store, agent.name, rooms)) {
344
+ // Each of the agent's places is a conversation of its own.
345
+ for (const key of places.keysOf(agent.name)) {
357
346
  for (const schedule of await schedules.forChannel(key))
358
347
  await schedules.remove(schedule.id, key);
359
348
  await runtime.startFresh(key);
360
349
  }
361
350
  for (const group of left) {
362
- for (const seat of seatsOf(group, agent.name, rooms))
351
+ for (const seat of places.seatsOf(group, agent.name))
363
352
  await runtime.startFresh(seat);
364
353
  if (group.status === "active") this.retitle(group);
365
354
  }
@@ -373,20 +362,15 @@ export class TeamLifecycle {
373
362
  async #archiveGroup(group: AgentGroup): Promise<void> {
374
363
  const { store, logger } = this.#ctx.options;
375
364
  const runtime = this.#ctx.options.runtime();
376
- const rooms = this.#rooms();
365
+ const { places } = this.#ctx.options;
377
366
  await store.archiveGroup(group.name);
378
367
  for (const member of group.members)
379
- for (const seat of seatsOf(group, member, rooms))
368
+ for (const seat of places.seatsOf(group, member))
380
369
  await runtime.startFresh(seat);
381
370
  logger.info({ group: group.name }, "group archived");
382
371
  this.#ctx.changed();
383
372
  }
384
373
 
385
- /** The surfaces that host rooms; none for a team without a surface port. */
386
- #rooms(): readonly string[] {
387
- return this.#ctx.options.surfaces?.roomSurfaces() ?? [];
388
- }
389
-
390
374
  /** Writes a group's members and host into its channel topic, without waiting on Discord; a group without a channel has no topic. */
391
375
  retitle(group: AgentGroup): void {
392
376
  const { channels, store, logger } = this.#ctx.options;
@@ -16,7 +16,7 @@ export interface AgentTeamOptions
16
16
  * The team's id: the Discord guild, or `LOCAL_TEAM_ID` on a host without Discord. It scopes the
17
17
  * rows the team keeps.
18
18
  */
19
- guildId: string;
19
+ teamId: string;
20
20
  /** The Discord guild the team's channels live in, which is not the team's id when the host sets one; absent without Discord channels. */
21
21
  discordGuild?: string | undefined;
22
22
  /** The first team, read and inserted once on start; an agent already stored is never overwritten. */