pi-roundtable 0.44.1 → 0.45.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.
Files changed (51) hide show
  1. package/CHANGELOG.md +31 -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
@@ -2,6 +2,7 @@ import { CharacterError, resolveCharacter } from "../../characters/index.ts";
2
2
  import type { AgentSeed } from "../agents/agent-store.ts";
3
3
  import type { BackgroundTarget, ChannelClaim } from "../contract/channels.ts";
4
4
  import type { ChatCommand } from "../contract/chat-command.ts";
5
+ import type { PlaceProvider } from "../contract/places.ts";
5
6
  import type { ChatSurface } from "../contract/surface.ts";
6
7
  import {
7
8
  compileSessionPlan,
@@ -21,13 +22,13 @@ import {
21
22
  type Persona,
22
23
  type PluginContext,
23
24
  type PromptSection,
24
- pluginContext,
25
25
  type RoundtablePlugin,
26
26
  refuseRemovedEvents,
27
27
  refuseRemovedParts,
28
28
  refuseRemovedSurfaceMethods,
29
29
  type Service,
30
30
  } from "../plugin-api/plugin.ts";
31
+ import { pluginContext } from "../plugin-api/plugin-context.ts";
31
32
  import {
32
33
  type DirectChannelProvider,
33
34
  directChannelsPort,
@@ -35,9 +36,24 @@ import {
35
36
  import { refuseShellTools } from "../runtime/chat-only.ts";
36
37
  import type { ToolTierTable } from "../runtime/tool-tiers.ts";
37
38
  import { ChatCommandSources, claimChatCommand } from "./chat-commands.ts";
39
+ import {
40
+ checkSurfacePrefix,
41
+ claimDirectChannel,
42
+ claimPersona,
43
+ claimTarget,
44
+ Names,
45
+ } from "./claims.ts";
46
+ import { placePort } from "./place-port.ts";
47
+ import { PlaceClaims } from "./places.ts";
38
48
  import { ServiceRegistry } from "./services.ts";
39
49
  import { surfaceService } from "./surface-service.ts";
40
50
 
51
+ /** What a host gives `collectContributions` of the context; it makes the rest from what the plugins contribute. */
52
+ export type ContributionBase = Omit<
53
+ PluginContext,
54
+ "services" | "directChannels" | "places"
55
+ >;
56
+
41
57
  /** Everything the plugins contributed, in contribution order. */
42
58
  export interface Registry {
43
59
  services: Service[];
@@ -51,6 +67,8 @@ export interface Registry {
51
67
  channels: ChannelClaim[];
52
68
  /** The contributed chat surfaces, each also in `services` as `surface:<prefix>`. */
53
69
  surfaces: ChatSurface[];
70
+ /** The contributed place providers, in contribution order; the host adds a derived one for a room surface without one. */
71
+ places: PlaceProvider[];
54
72
  personas: Persona[];
55
73
  backgroundTargets: BackgroundTarget[];
56
74
  /** The contributed direct channels, in contribution order. */
@@ -76,6 +94,7 @@ export const emptyRegistry = (): Registry => ({
76
94
  sessionTools: [],
77
95
  channels: [],
78
96
  surfaces: [],
97
+ places: [],
79
98
  personas: [],
80
99
  backgroundTargets: [],
81
100
  directChannels: [],
@@ -165,122 +184,6 @@ function mergeSelections(
165
184
  };
166
185
  }
167
186
 
168
- /** Which plugin registered each name of one kind, so a clash names both. */
169
- class Names {
170
- readonly #owners = new Map<string, string>();
171
-
172
- constructor(private readonly kind: string) {}
173
-
174
- claim(plugin: string, name: string): void {
175
- const other = this.#owners.get(name);
176
- if (other !== undefined)
177
- throw new PluginError(
178
- `plugin ${plugin}: ${this.kind} ${name} is already registered by plugin ${other}. Rename one of the two.`,
179
- );
180
- this.#owners.set(name, plugin);
181
- }
182
- }
183
-
184
- /** The kind of a conversation the agent server runs itself, which no plugin's persona may take. */
185
- const RESERVED_KIND = "agent";
186
-
187
- /**
188
- * Refuses a persona without a kind, for the reserved agent kind, or for a kind another plugin's
189
- * persona already has, so a conversation's prompt is never a matter of plugin order.
190
- */
191
- function claimPersona(
192
- kinds: Map<string, string>,
193
- plugin: string,
194
- persona: Persona,
195
- ): void {
196
- const { kind } = persona;
197
- if (typeof kind !== "string" || kind === "")
198
- throw new PluginError(
199
- `plugin ${plugin}: a persona needs a kind, a non-empty string such as "study", and got ${JSON.stringify(kind)}.`,
200
- );
201
- if (kind === RESERVED_KIND)
202
- throw new PluginError(
203
- `plugin ${plugin}: the persona kind "${RESERVED_KIND}" is reserved for the agent server's agents. Give the persona the kind of your own conversations.`,
204
- );
205
- if (typeof persona.prompt !== "function")
206
- throw new PluginError(
207
- `plugin ${plugin}: the persona of kind "${kind}" needs a prompt() that returns its text.`,
208
- );
209
- if (
210
- persona.memory !== undefined &&
211
- persona.memory !== "speaker" &&
212
- persona.memory !== "none"
213
- )
214
- throw new PluginError(
215
- `plugin ${plugin}: the persona of kind "${kind}" has memory ${JSON.stringify(persona.memory)}; expected "speaker" or "none".`,
216
- );
217
- const other = kinds.get(kind);
218
- if (other !== undefined)
219
- throw new PluginError(
220
- `plugin ${plugin}: persona kind "${kind}" is already registered by plugin ${other}. Keep one persona per kind.`,
221
- );
222
- kinds.set(kind, plugin);
223
- }
224
-
225
- /**
226
- * Refuses a direct channel without a name, a label, or `reaches`, or whose name another plugin's
227
- * already has, so the providers a person is reached through never depend on a typo.
228
- */
229
- function claimDirectChannel(
230
- names: Names,
231
- plugin: string,
232
- provider: DirectChannelProvider,
233
- ): void {
234
- const { name } = provider;
235
- if (typeof name !== "string" || name === "")
236
- throw new PluginError(
237
- `plugin ${plugin}: a direct channel needs a name, a non-empty string such as "discord", and got ${JSON.stringify(name)}.`,
238
- );
239
- if (typeof provider.label !== "string" || provider.label === "")
240
- throw new PluginError(
241
- `plugin ${plugin}: the direct channel "${name}" needs a label, such as "a direct message on Discord", which the notify tool names it by.`,
242
- );
243
- if (typeof provider.reaches !== "function")
244
- throw new PluginError(
245
- `plugin ${plugin}: the direct channel "${name}" needs reaches(principalId), which finds a person's own conversation there.`,
246
- );
247
- names.claim(plugin, name);
248
- }
249
-
250
- /**
251
- * Refuses a background target without a name or a label, or whose name another plugin's target
252
- * already has, so a stored schedule's target never depends on plugin order.
253
- */
254
- function claimTarget(
255
- names: Map<string, string>,
256
- plugin: string,
257
- target: BackgroundTarget,
258
- ): void {
259
- const { name } = target;
260
- if (typeof name !== "string" || name === "")
261
- throw new PluginError(
262
- `plugin ${plugin}: a background target needs a name, a non-empty string such as "support", and got ${JSON.stringify(name)}.`,
263
- );
264
- if (typeof target.label !== "function")
265
- throw new PluginError(
266
- `plugin ${plugin}: the background target "${name}" needs a label(locale) that returns its name for lists.`,
267
- );
268
- const other = names.get(name);
269
- if (other !== undefined)
270
- throw new PluginError(
271
- `plugin ${plugin}: background target "${name}" is already registered by plugin ${other}. Keep one target per name.`,
272
- );
273
- names.set(name, plugin);
274
- }
275
-
276
- /** The surface's own words for what a prefix must be, checked before its service is named. */
277
- function checkSurfacePrefix(plugin: string, prefix: unknown): void {
278
- if (typeof prefix !== "string" || prefix === "" || /[:\s]/.test(prefix))
279
- throw new PluginError(
280
- `plugin ${plugin}: a surface is named by the prefix of its channel keys, a non-empty word without a colon or space, such as discord; got ${JSON.stringify(prefix)}.`,
281
- );
282
- }
283
-
284
187
  /**
285
188
  * Sets up every plugin in order and collects what each adds. A plugin that adds nothing, that
286
189
  * reuses a name, or whose setup throws or returns a part the contract does not have is refused
@@ -289,7 +192,7 @@ function checkSurfacePrefix(plugin: string, prefix: unknown): void {
289
192
  */
290
193
  export async function collectContributions(
291
194
  plugins: readonly RoundtablePlugin[],
292
- base: Omit<PluginContext, "services" | "directChannels">,
195
+ base: ContributionBase,
293
196
  tiers: ToolTierTable,
294
197
  services: ServiceRegistry = new ServiceRegistry(plugins),
295
198
  ): Promise<Registry> {
@@ -308,11 +211,19 @@ export async function collectContributions(
308
211
  base.surfaces,
309
212
  base.logger,
310
213
  ),
214
+ places: placePort(() => {
215
+ if (!collected)
216
+ throw new NotLinkedError(
217
+ "place providers are linked once every plugin is set up. Use them from a service's start, a handler, or a session tool, not during setup.",
218
+ );
219
+ return { surfaces: registry.surfaces, providers: registry.places };
220
+ }, base.surfaces),
311
221
  };
312
222
  const serviceNames = new Names("service");
313
223
  const sessionTools = new Names("session tool");
314
224
  const holds = new Names("hold rule");
315
225
  const surfaces = new Names("surface");
226
+ const placeClaims = new PlaceClaims();
316
227
  const directChannelNames = new Names("direct channel");
317
228
  const personaKinds = new Map<string, string>();
318
229
  const targetNames = new Map<string, string>();
@@ -351,6 +262,7 @@ export async function collectContributions(
351
262
  piPackages = [],
352
263
  channels = [],
353
264
  surfaces: contributedSurfaces = [],
265
+ places = [],
354
266
  personas = [],
355
267
  backgroundTargets = [],
356
268
  directChannels = [],
@@ -369,6 +281,7 @@ export async function collectContributions(
369
281
  contribution.sessionTools ?? [],
370
282
  channels,
371
283
  contributedSurfaces,
284
+ places,
372
285
  personas,
373
286
  backgroundTargets,
374
287
  directChannels,
@@ -408,6 +321,10 @@ export async function collectContributions(
408
321
  registry.services.push(service);
409
322
  registry.servicePlugins.set(service, plugin.name);
410
323
  }
324
+ for (const provider of places) {
325
+ placeClaims.claim(plugin.name, provider);
326
+ registry.places.push(provider);
327
+ }
411
328
  for (const persona of personas)
412
329
  claimPersona(personaKinds, plugin.name, persona);
413
330
  for (const target of backgroundTargets)
@@ -471,6 +388,7 @@ export async function collectContributions(
471
388
  registry.requiredTools.push(...requiredTools);
472
389
  }
473
390
  services.settle();
391
+ placeClaims.check(registry.surfaces);
474
392
  collected = true;
475
393
  return registry;
476
394
  }
@@ -0,0 +1,94 @@
1
+ import {
2
+ type PlaceAuthor,
3
+ type PlacePort,
4
+ type PlaceProvider,
5
+ roomKey,
6
+ roomRef,
7
+ } from "../contract/places.ts";
8
+ import type { ChatSurface, SurfacePort } from "../contract/surface.ts";
9
+ import { channelKey } from "../contract/surface.ts";
10
+ import type { PostAuthor } from "../domain/conversation.ts";
11
+
12
+ /** What the host collected for the port: the surfaces and the providers plugins contributed, each in contribution order. */
13
+ export interface LinkedPlaces {
14
+ surfaces: readonly ChatSurface[];
15
+ providers: readonly PlaceProvider[];
16
+ }
17
+
18
+ /** An author as a surface shows it: the name and the agent's own name, without the avatar a webhook needs. */
19
+ const asPostAuthor = (author: PlaceAuthor): PostAuthor => ({
20
+ name: author.name,
21
+ ...(author.agent === undefined ? {} : { agent: author.agent }),
22
+ });
23
+
24
+ /**
25
+ * The provider a surface that hosts agent rooms gets when no plugin contributes one: its places
26
+ * are the rooms `<surface>:agent.<name>` and `<surface>:group.<name>`, derived from the name, and
27
+ * it posts through the surface's own `sendReply` and `interim` with the agent as author.
28
+ */
29
+ export function derivedPlaces(
30
+ surface: string,
31
+ surfaces: SurfacePort,
32
+ ): PlaceProvider {
33
+ return {
34
+ surface,
35
+ storage: "derived",
36
+ key: (ref) => roomKey(surface, ref),
37
+ placeOf: (key) => roomRef(key, surface),
38
+ post: (key, post) =>
39
+ surfaces.sendReply(
40
+ // A thread of the room's surface is a channel of that surface, and speaks there.
41
+ post.thread === undefined ? key : channelKey(surface, post.thread),
42
+ {
43
+ ...(post.thinking ? { thinking: post.thinking } : {}),
44
+ chunks: post.chunks,
45
+ ...(post.files?.length ? { files: post.files } : {}),
46
+ author: asPostAuthor(post.author),
47
+ ...(post.failed ? { failed: post.failed } : {}),
48
+ ...(post.intercom ? { intercom: post.intercom } : {}),
49
+ },
50
+ ),
51
+ interim: (key, author) => surfaces.interim(key, asPostAuthor(author)),
52
+ };
53
+ }
54
+
55
+ /**
56
+ * The port over the place providers, each chosen by a surface or by the prefix of a channel's key.
57
+ * `linked` gives the surfaces and contributed providers once every plugin is set up and throws
58
+ * NotLinkedError before. A surface that hosts agent rooms and has no contributed provider gets a
59
+ * derived one. "Auto" order is the stored providers in contribution order, then the derived ones
60
+ * in surface order.
61
+ */
62
+ export function placePort(
63
+ linked: () => LinkedPlaces,
64
+ surfaces: SurfacePort,
65
+ ): PlacePort {
66
+ let cached: { all: PlaceProvider[]; stored: PlaceProvider[] } | undefined;
67
+ const read = () => {
68
+ if (cached) return cached;
69
+ const { surfaces: registered, providers } = linked();
70
+ const stored = providers.filter((p) => p.storage === "stored");
71
+ const derived = registered.flatMap((surface) => {
72
+ const own = providers.find(
73
+ (p) => p.surface === surface.surface && p.storage === "derived",
74
+ );
75
+ if (own) return [own];
76
+ const served = providers.some((p) => p.surface === surface.surface);
77
+ return surface.agentRooms === true && !served
78
+ ? [derivedPlaces(surface.surface, surfaces)]
79
+ : [];
80
+ });
81
+ cached = { all: [...stored, ...derived], stored };
82
+ return cached;
83
+ };
84
+ return {
85
+ of: (surfaceOrKey) => {
86
+ const at = surfaceOrKey.indexOf(":");
87
+ if (at === 0) return undefined;
88
+ const surface = at < 0 ? surfaceOrKey : surfaceOrKey.slice(0, at);
89
+ return read().all.find((p) => p.surface === surface);
90
+ },
91
+ stored: () => read().stored,
92
+ all: () => read().all,
93
+ };
94
+ }
@@ -0,0 +1,43 @@
1
+ import type { PlaceProvider } from "../contract/places.ts";
2
+ import type { ChatSurface } from "../contract/surface.ts";
3
+ import { PluginError } from "../kernel/errors.ts";
4
+
5
+ /**
6
+ * Which plugin contributed the place provider of each surface, so two for one surface, a provider
7
+ * that cannot work, and a provider for a surface nobody registers are refused naming the plugins.
8
+ */
9
+ export class PlaceClaims {
10
+ readonly #owners = new Map<string, string>();
11
+
12
+ claim(plugin: string, provider: PlaceProvider): void {
13
+ const { surface } = provider;
14
+ if (typeof surface !== "string" || surface === "" || /[:\s]/.test(surface))
15
+ throw new PluginError(
16
+ `plugin ${plugin}: a place provider is named by the surface whose keys its places have, a non-empty word without a colon or space, such as discord; got ${JSON.stringify(surface)}.`,
17
+ );
18
+ if (provider.storage !== "derived" && provider.storage !== "stored")
19
+ throw new PluginError(
20
+ `plugin ${plugin}: the place provider of surface ${surface} needs storage "derived" or "stored"; got ${JSON.stringify(provider.storage)}.`,
21
+ );
22
+ for (const member of ["key", "placeOf", "post"] as const)
23
+ if (typeof provider[member] !== "function")
24
+ throw new PluginError(
25
+ `plugin ${plugin}: the place provider of surface ${surface} needs ${member}(), which every provider has.`,
26
+ );
27
+ const other = this.#owners.get(surface);
28
+ if (other !== undefined)
29
+ throw new PluginError(
30
+ `plugin ${plugin}: surface ${surface} already has the place provider of plugin ${other}. Keep one provider per surface.`,
31
+ );
32
+ this.#owners.set(surface, plugin);
33
+ }
34
+
35
+ /** Refuses a provider whose surface no plugin registered, once every plugin is set up. */
36
+ check(surfaces: readonly ChatSurface[]): void {
37
+ for (const [surface, plugin] of this.#owners)
38
+ if (!surfaces.some((registered) => registered.surface === surface))
39
+ throw new PluginError(
40
+ `plugin ${plugin}: the place provider of surface ${surface}, which no plugin registers with \`surfaces\`. Contribute the surface, or remove the provider.`,
41
+ );
42
+ }
43
+ }
@@ -0,0 +1,120 @@
1
+ import type {
2
+ PlaceCheck,
3
+ PlaceProvider,
4
+ PlaceRef,
5
+ } from "../contract/places.ts";
6
+ import { channelKey, parseChannelKey } from "../contract/surface.ts";
7
+ import type { ChannelKey } from "../domain/sessions.ts";
8
+ import { PluginError } from "../kernel/errors.ts";
9
+ import type { PlaceSeen } from "./place-contract.ts";
10
+
11
+ /** The surface of the fake pager network. */
12
+ export const PAGER = "pager";
13
+
14
+ interface Page {
15
+ name: string;
16
+ topic: string;
17
+ archived: boolean;
18
+ messages: { id: number; author: string; text: string }[];
19
+ }
20
+
21
+ /**
22
+ * A chat network the core has never heard of, whose places are pages it makes with ids of its
23
+ * own: a stored provider with `ensure`, `remove`, `check`, `describe`, `onRemoved` and `mention`,
24
+ * and a person's view of it. An id that starts with `other-` is a page of another network.
25
+ */
26
+ export class FakePager {
27
+ readonly pages = new Map<string, Page>();
28
+ readonly #listeners: ((id: string) => void)[] = [];
29
+ #next = 1;
30
+ #message = 1;
31
+
32
+ readonly provider: PlaceProvider = {
33
+ surface: PAGER,
34
+ storage: "stored",
35
+ key: (_ref: PlaceRef, id?: string): ChannelKey => {
36
+ if (id === undefined)
37
+ throw new PluginError("a pager place is keyed by its page id");
38
+ return channelKey(PAGER, id);
39
+ },
40
+ placeOf: (key) => {
41
+ if (!key.includes(":")) return undefined;
42
+ const parsed = parseChannelKey(key);
43
+ return parsed.surface === PAGER ? { id: parsed.id } : undefined;
44
+ },
45
+ post: async (key, post) => {
46
+ const page = this.#page(key);
47
+ if (post.thinking) this.#add(page, post.author.name, post.thinking);
48
+ for (const chunk of post.chunks) this.#add(page, post.author.name, chunk);
49
+ },
50
+ interim: (key, author) => ({
51
+ post: async (text) => {
52
+ const page = this.#page(key);
53
+ const message = this.#add(page, author.name, text);
54
+ return {
55
+ edit: async (change) => {
56
+ message.text = change;
57
+ },
58
+ };
59
+ },
60
+ }),
61
+ ensure: async (spec) => {
62
+ const existing =
63
+ spec.existing === undefined ? undefined : this.pages.get(spec.existing);
64
+ if (spec.existing !== undefined && existing) {
65
+ existing.archived = false;
66
+ return spec.existing;
67
+ }
68
+ const id = `pg-${this.#next++}`;
69
+ this.pages.set(id, {
70
+ name: spec.name,
71
+ topic: spec.topic,
72
+ archived: false,
73
+ messages: [],
74
+ });
75
+ return id;
76
+ },
77
+ remove: async (id) => {
78
+ const page = this.pages.get(id);
79
+ if (page) page.archived = true;
80
+ return `Its page ${id} is kept.`;
81
+ },
82
+ check: async (id): Promise<PlaceCheck> => {
83
+ if (id.startsWith("other-")) return "foreign";
84
+ return this.pages.has(id) ? "ok" : "gone";
85
+ },
86
+ describe: async (id, topic) => {
87
+ const page = this.pages.get(id);
88
+ if (page) page.topic = topic;
89
+ },
90
+ onRemoved: (listener) => {
91
+ this.#listeners.push(listener);
92
+ },
93
+ mention: (key) => `page ${parseChannelKey(key).id}`,
94
+ };
95
+
96
+ /** Everything a person reading the page of `key` has seen. */
97
+ seen(key: ChannelKey): readonly PlaceSeen[] {
98
+ return (this.pages.get(parseChannelKey(key).id)?.messages ?? []).map(
99
+ ({ author, text }) => ({ author, text }),
100
+ );
101
+ }
102
+
103
+ /** A person deletes the page, and the network tells the listeners. */
104
+ delete(id: string): void {
105
+ this.pages.delete(id);
106
+ for (const listener of this.#listeners) listener(id);
107
+ }
108
+
109
+ #page(key: ChannelKey): Page {
110
+ const page = this.pages.get(parseChannelKey(key).id);
111
+ if (!page) throw new PluginError(`no page ${key}`);
112
+ return page;
113
+ }
114
+
115
+ #add(page: Page, author: string, text: string) {
116
+ const message = { id: this.#message++, author, text };
117
+ page.messages.push(message);
118
+ return message;
119
+ }
120
+ }
@@ -0,0 +1,60 @@
1
+ import type { ChatSurface } from "../contract/surface.ts";
2
+ import type { OutboundReply, PostAuthor } from "../domain/conversation.ts";
3
+ import type { InterimPosts } from "../domain/interim.ts";
4
+ import type { ChannelKey } from "../domain/sessions.ts";
5
+ import type { PlaceSeen } from "./place-contract.ts";
6
+
7
+ /** A reply the fake room surface was asked to deliver. */
8
+ export interface RoomReply {
9
+ channel: ChannelKey;
10
+ reply: OutboundReply;
11
+ }
12
+
13
+ /**
14
+ * A chat surface that hosts agent rooms and shows what it is asked to: replies by `sendReply`
15
+ * under the reply's author, and interim posts edited in place.
16
+ */
17
+ export class FakeRoomSurface implements ChatSurface {
18
+ readonly agentRooms = true;
19
+ readonly supportsFiles = true;
20
+ readonly replies: RoomReply[] = [];
21
+ readonly #shown = new Map<ChannelKey, { author: string; text: string }[]>();
22
+
23
+ constructor(readonly surface = "app") {}
24
+
25
+ async start(): Promise<void> {}
26
+
27
+ async sendReply(channel: ChannelKey, reply: OutboundReply): Promise<void> {
28
+ this.replies.push({ channel, reply });
29
+ const author = reply.author?.name ?? "assistant";
30
+ if (reply.thinking) this.#show(channel, author, reply.thinking);
31
+ for (const chunk of reply.chunks) this.#show(channel, author, chunk);
32
+ }
33
+
34
+ interim(channel: ChannelKey, as?: PostAuthor): InterimPosts {
35
+ return {
36
+ post: async (text) => {
37
+ const message = this.#show(channel, as?.name ?? "assistant", text);
38
+ return {
39
+ edit: async (change) => {
40
+ message.text = change;
41
+ },
42
+ };
43
+ },
44
+ };
45
+ }
46
+
47
+ /** What a person in the room has seen. */
48
+ seen(channel: ChannelKey): readonly PlaceSeen[] {
49
+ return (this.#shown.get(channel) ?? []).map(({ author, text }) => ({
50
+ author,
51
+ text,
52
+ }));
53
+ }
54
+
55
+ #show(channel: ChannelKey, author: string, text: string) {
56
+ const message = { author, text };
57
+ this.#shown.set(channel, [...(this.#shown.get(channel) ?? []), message]);
58
+ return message;
59
+ }
60
+ }
@@ -12,12 +12,12 @@ import type { Principal } from "../identity/principal-store.ts";
12
12
  import type { Tier } from "../identity/speakers.ts";
13
13
  import { silentLogger } from "../kernel/log.ts";
14
14
  import type { SkillStore } from "../modules/skills/skill-store.ts";
15
- import {
16
- type Contribution,
17
- type PluginContext,
18
- pluginContext,
19
- type RoundtablePlugin,
15
+ import type {
16
+ Contribution,
17
+ PluginContext,
18
+ RoundtablePlugin,
20
19
  } from "../plugin-api/plugin.ts";
20
+ import { pluginContext } from "../plugin-api/plugin-context.ts";
21
21
  import type {
22
22
  AgentServer,
23
23
  MemoryStore,