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
@@ -0,0 +1,481 @@
1
+ import { describe, test } from "bun:test";
2
+ import type {
3
+ PlaceAuthor,
4
+ PlaceCheck,
5
+ PlaceProvider,
6
+ PlaceRef,
7
+ PlaceSpec,
8
+ } from "../contract/places.ts";
9
+ import { parseChannelKey } from "../contract/surface.ts";
10
+ import type { ChannelKey } from "../domain/sessions.ts";
11
+ import { ContractBroken, expectThat } from "./surface-contract-support.ts";
12
+
13
+ /** One message a person watching a place has seen. */
14
+ export interface PlaceSeen {
15
+ /** The name it was posted under. */
16
+ author: string;
17
+ text: string;
18
+ }
19
+
20
+ /**
21
+ * A place provider under the contract, and what a person watching its surface sees. The contract
22
+ * makes the places it needs through `ensure`, or, for a provider that cannot make them, asks
23
+ * `bound` for one that exists.
24
+ */
25
+ export interface PlaceContractSubject {
26
+ provider: PlaceProvider;
27
+ /** Every message a person watching the place has seen so far, oldest first, edits applied. */
28
+ seen(key: ChannelKey): Promise<readonly PlaceSeen[]> | readonly PlaceSeen[];
29
+ /** A stored place of the surface that exists, for a provider without `ensure`; the stored checks are skipped without it. */
30
+ bound?(ref: PlaceRef): Promise<string>;
31
+ /**
32
+ * A person deletes the place on the surface, and the surface reports it as it would: through the
33
+ * provider's `onRemoved` listeners. Without it the checks of a gone place are skipped.
34
+ */
35
+ delete?(id: string): Promise<void>;
36
+ /** An id that is no place of the provider's space, such as a channel of another server; without it the `foreign` check is skipped. */
37
+ foreign?(): Promise<string>;
38
+ close?(): Promise<void>;
39
+ }
40
+
41
+ /** One way the subject broke the contract. */
42
+ export interface PlaceContractFailure {
43
+ name: string;
44
+ error: unknown;
45
+ }
46
+
47
+ interface Check {
48
+ name: string;
49
+ /** Whether the check applies to this subject; a check that does not is skipped. */
50
+ applies?(subject: PlaceContractSubject): boolean;
51
+ run(subject: PlaceContractSubject): Promise<void>;
52
+ }
53
+
54
+ const AGENT: PlaceRef = { kind: "agent", name: "knight" };
55
+ const OTHER_AGENT: PlaceRef = { kind: "agent", name: "scribe" };
56
+ const GROUP: PlaceRef = { kind: "group", name: "round-table" };
57
+ const AUTHOR: PlaceAuthor = {
58
+ name: "Sir Knight",
59
+ agent: "knight",
60
+ avatarUrl: "https://example.test/knight.png",
61
+ };
62
+
63
+ const specOf = (ref: PlaceRef): PlaceSpec => ({
64
+ ...ref,
65
+ displayName: ref === AGENT ? "Sir Knight" : ref.name,
66
+ topic: ref.kind === "agent" ? "Sir Knight" : "Knight and Scribe",
67
+ });
68
+
69
+ /** Waits until `done` holds, polling; what a person sees on a network can lag the call that showed it. */
70
+ async function until(
71
+ done: () => boolean | Promise<boolean>,
72
+ what: string,
73
+ ms = 2000,
74
+ ): Promise<void> {
75
+ const deadline = Date.now() + ms;
76
+ while (!(await done())) {
77
+ if (Date.now() > deadline)
78
+ throw new ContractBroken(`timed out waiting until ${what}`);
79
+ await Bun.sleep(10);
80
+ }
81
+ }
82
+
83
+ const stored = (subject: PlaceContractSubject) =>
84
+ subject.provider.storage === "stored";
85
+
86
+ /** The id of a stored place for `ref`, made through `ensure` or bound by the subject; undefined when the subject offers neither. */
87
+ async function placeFor(
88
+ subject: PlaceContractSubject,
89
+ ref: PlaceRef,
90
+ ): Promise<string | undefined> {
91
+ if (subject.provider.ensure) return subject.provider.ensure(specOf(ref));
92
+ return subject.bound?.(ref);
93
+ }
94
+
95
+ /** The key of a place of `ref`: from the name when derived, from a stored place's id otherwise; undefined without a place. */
96
+ async function keyFor(
97
+ subject: PlaceContractSubject,
98
+ ref: PlaceRef,
99
+ ): Promise<{ key: ChannelKey; id?: string } | undefined> {
100
+ const { provider } = subject;
101
+ if (!stored(subject)) return { key: provider.key(ref) };
102
+ const id = await placeFor(subject, ref);
103
+ return id === undefined ? undefined : { key: provider.key(ref, id), id };
104
+ }
105
+
106
+ async function seenIn(
107
+ subject: PlaceContractSubject,
108
+ key: ChannelKey,
109
+ ): Promise<readonly PlaceSeen[]> {
110
+ return subject.seen(key);
111
+ }
112
+
113
+ function sameRef(found: unknown, ref: PlaceRef): boolean {
114
+ return (
115
+ typeof found === "object" &&
116
+ found !== null &&
117
+ (found as PlaceRef).kind === ref.kind &&
118
+ (found as PlaceRef).name === ref.name
119
+ );
120
+ }
121
+
122
+ const CHECKS: readonly Check[] = [
123
+ {
124
+ name: "names a surface without a colon, and a stored one says so",
125
+ run: async ({ provider }) => {
126
+ expectThat(
127
+ provider.surface !== "" && !provider.surface.includes(":"),
128
+ `"${provider.surface}" is not a surface name`,
129
+ );
130
+ expectThat(
131
+ provider.storage === "derived" || provider.storage === "stored",
132
+ `storage is ${String(provider.storage)}, not derived or stored`,
133
+ );
134
+ expectThat(
135
+ provider.storage === "stored" || provider.ensure === undefined,
136
+ "a derived provider makes no places, so it has no ensure",
137
+ );
138
+ },
139
+ },
140
+ {
141
+ name: "a derived key comes from the name and reads back as it",
142
+ applies: (subject) => !stored(subject),
143
+ run: async ({ provider }) => {
144
+ for (const ref of [AGENT, GROUP]) {
145
+ const key = provider.key(ref);
146
+ expectThat(
147
+ parseChannelKey(key).surface === provider.surface,
148
+ `${key} is no key of ${provider.surface}`,
149
+ );
150
+ expectThat(
151
+ sameRef(provider.placeOf(key), ref),
152
+ `${key} reads back as ${JSON.stringify(provider.placeOf(key))}, not ${ref.kind} ${ref.name}`,
153
+ );
154
+ }
155
+ expectThat(
156
+ provider.key(AGENT) !== provider.key(OTHER_AGENT) &&
157
+ provider.key(AGENT) !== provider.key({ ...AGENT, kind: "group" }),
158
+ "two places share a key",
159
+ );
160
+ },
161
+ },
162
+ {
163
+ name: "a stored key carries the place's id and reads back as it",
164
+ applies: (subject) => stored(subject),
165
+ run: async ({ provider }) => {
166
+ const key = provider.key(AGENT, "p-1");
167
+ expectThat(
168
+ parseChannelKey(key).surface === provider.surface,
169
+ `${key} is no key of ${provider.surface}`,
170
+ );
171
+ const found = provider.placeOf(key);
172
+ expectThat(
173
+ typeof found === "object" && found !== null && "id" in found,
174
+ `${key} reads back as ${JSON.stringify(found)}, not an id`,
175
+ );
176
+ expectThat(provider.key(AGENT, "p-2") !== key, "two ids share a key");
177
+ expectThat(
178
+ provider.placeOf(provider.key(GROUP, "p-1")) !== undefined,
179
+ "a group's key does not read back",
180
+ );
181
+ let refused = false;
182
+ try {
183
+ provider.key(AGENT);
184
+ } catch {
185
+ refused = true;
186
+ }
187
+ expectThat(refused, "a stored key was made with no id");
188
+ },
189
+ },
190
+ {
191
+ name: "reads no key of another surface, and none that is no key, as its place",
192
+ run: async ({ provider }) => {
193
+ expectThat(
194
+ provider.placeOf("other-surface:agent.knight") === undefined,
195
+ "a key of another surface read as a place",
196
+ );
197
+ expectThat(
198
+ provider.placeOf("not a key" as ChannelKey) === undefined,
199
+ "a text that is no key read as a place",
200
+ );
201
+ },
202
+ },
203
+ {
204
+ name: "posts under the author's name, with every chunk and the thinking",
205
+ run: async (subject) => {
206
+ const place = await keyFor(subject, AGENT);
207
+ if (!place) return;
208
+ await subject.provider.post(place.key, {
209
+ author: AUTHOR,
210
+ thinking: "mulling it over",
211
+ chunks: ["first part", "second part"],
212
+ });
213
+ await until(
214
+ async () => (await seenIn(subject, place.key)).length > 0,
215
+ "the person sees the post",
216
+ );
217
+ const shown = await seenIn(subject, place.key);
218
+ const text = shown.map((m) => m.text).join("\n");
219
+ for (const part of ["mulling it over", "first part", "second part"])
220
+ expectThat(text.includes(part), `the person did not see "${part}"`);
221
+ expectThat(
222
+ shown.every((m) => m.author === AUTHOR.name),
223
+ `posted under ${[...new Set(shown.map((m) => m.author))].join(", ")}, not ${AUTHOR.name}`,
224
+ );
225
+ },
226
+ },
227
+ {
228
+ name: "posts a group's place apart from an agent's",
229
+ run: async (subject) => {
230
+ const agent = await keyFor(subject, AGENT);
231
+ const group = await keyFor(subject, GROUP);
232
+ if (!agent || !group) return;
233
+ await subject.provider.post(group.key, {
234
+ author: AUTHOR,
235
+ chunks: ["for the table"],
236
+ });
237
+ await until(
238
+ async () => (await seenIn(subject, group.key)).length > 0,
239
+ "the person sees the group's post",
240
+ );
241
+ expectThat(
242
+ (await seenIn(subject, agent.key)).length === 0,
243
+ "a group's post showed in an agent's place",
244
+ );
245
+ },
246
+ },
247
+ {
248
+ name: "interim posts speak under the author and are edited in place",
249
+ applies: (subject) => subject.provider.interim !== undefined,
250
+ run: async (subject) => {
251
+ const place = await keyFor(subject, AGENT);
252
+ if (!place) return;
253
+ const posts = subject.provider.interim?.(place.key, AUTHOR);
254
+ if (!posts) return;
255
+ const message = await posts.post("working on it");
256
+ await until(
257
+ async () =>
258
+ (await seenIn(subject, place.key)).some(
259
+ (m) => m.text === "working on it",
260
+ ),
261
+ "the person sees the interim post",
262
+ );
263
+ await message.edit("nearly done");
264
+ await until(
265
+ async () =>
266
+ (await seenIn(subject, place.key)).some(
267
+ (m) => m.text === "nearly done",
268
+ ),
269
+ "the person sees the edit",
270
+ );
271
+ const shown = await seenIn(subject, place.key);
272
+ expectThat(
273
+ !shown.some((m) => m.text === "working on it"),
274
+ "the edit left the first text beside it",
275
+ );
276
+ expectThat(
277
+ shown.every((m) => m.author === AUTHOR.name),
278
+ "an interim post was not under the author's name",
279
+ );
280
+ },
281
+ },
282
+ {
283
+ name: "ensure makes a place of its own for each owner, with a key that reads back",
284
+ applies: (subject) => subject.provider.ensure !== undefined,
285
+ run: async (subject) => {
286
+ const { provider } = subject;
287
+ const first = await provider.ensure?.(specOf(AGENT));
288
+ const second = await provider.ensure?.(specOf(OTHER_AGENT));
289
+ expectThat(
290
+ typeof first === "string" && first !== "",
291
+ "ensure did not resolve to an id",
292
+ );
293
+ expectThat(
294
+ typeof second === "string" && second !== "" && second !== first,
295
+ "two owners got one place",
296
+ );
297
+ const found = provider.placeOf(provider.key(AGENT, first as string));
298
+ expectThat(
299
+ typeof found === "object" &&
300
+ found !== null &&
301
+ "id" in found &&
302
+ found.id === first,
303
+ `the key of ${first} reads back as ${JSON.stringify(found)}`,
304
+ );
305
+ },
306
+ },
307
+ {
308
+ name: "ensure brings an existing place back under its own id",
309
+ applies: (subject) => subject.provider.ensure !== undefined,
310
+ run: async ({ provider }) => {
311
+ const id = await provider.ensure?.(specOf(AGENT));
312
+ if (id === undefined) return;
313
+ const again = await provider.ensure?.({ ...specOf(AGENT), existing: id });
314
+ expectThat(again === id, `ensure with existing ${id} made ${again}`);
315
+ },
316
+ },
317
+ {
318
+ name: "remove keeps the place, and ensure brings it back",
319
+ applies: (subject) =>
320
+ subject.provider.ensure !== undefined &&
321
+ subject.provider.remove !== undefined,
322
+ run: async ({ provider }) => {
323
+ const spec = specOf(AGENT);
324
+ const id = await provider.ensure?.(spec);
325
+ if (id === undefined) return;
326
+ const line = await provider.remove?.(id, { ...spec, helper: false });
327
+ expectThat(
328
+ line === undefined || typeof line === "string",
329
+ "remove resolved to something that is no line of text",
330
+ );
331
+ if (provider.check)
332
+ expectThat(
333
+ (await provider.check(id)) !== "gone",
334
+ "remove deleted the place; it should keep it with its history",
335
+ );
336
+ const back = await provider.ensure?.({ ...spec, existing: id });
337
+ expectThat(back === id, `the restored place is ${back}, not ${id}`);
338
+ },
339
+ },
340
+ {
341
+ name: "check finds a place that is there ok",
342
+ applies: (subject) => subject.provider.check !== undefined,
343
+ run: async (subject) => {
344
+ const id = await placeFor(subject, AGENT);
345
+ if (id === undefined) return;
346
+ const found = await subject.provider.check?.(id);
347
+ expectThat(found === "ok", `check of a place that is there: ${found}`);
348
+ },
349
+ },
350
+ {
351
+ name: "check finds a place a person deleted gone, and tells the listeners",
352
+ applies: (subject) =>
353
+ subject.provider.check !== undefined && subject.delete !== undefined,
354
+ run: async (subject) => {
355
+ const id = await placeFor(subject, AGENT);
356
+ if (id === undefined) return;
357
+ const heard: string[] = [];
358
+ subject.provider.onRemoved?.((removed) => heard.push(removed));
359
+ await subject.delete?.(id);
360
+ const found: PlaceCheck | undefined = await subject.provider.check?.(id);
361
+ expectThat(found === "gone", `check of a deleted place: ${found}`);
362
+ if (subject.provider.onRemoved)
363
+ await until(
364
+ () => heard.includes(id),
365
+ "the provider reports the deleted place",
366
+ );
367
+ },
368
+ },
369
+ {
370
+ name: "check finds a place of another space foreign, not gone",
371
+ applies: (subject) =>
372
+ subject.provider.check !== undefined && subject.foreign !== undefined,
373
+ run: async (subject) => {
374
+ const id = await subject.foreign?.();
375
+ if (id === undefined) return;
376
+ const found = await subject.provider.check?.(id);
377
+ expectThat(found === "foreign", `check of a foreign place: ${found}`);
378
+ },
379
+ },
380
+ {
381
+ name: "describe writes a topic without failing",
382
+ applies: (subject) => subject.provider.describe !== undefined,
383
+ run: async (subject) => {
384
+ const id = await placeFor(subject, GROUP);
385
+ if (id === undefined) return;
386
+ await subject.provider.describe?.(
387
+ id,
388
+ "Knight and Scribe, hosted by Knight",
389
+ );
390
+ },
391
+ },
392
+ {
393
+ name: "mentions a place in words",
394
+ applies: (subject) => subject.provider.mention !== undefined,
395
+ run: async (subject) => {
396
+ const place = await keyFor(subject, AGENT);
397
+ if (!place) return;
398
+ const mention = subject.provider.mention?.(place.key);
399
+ expectThat(
400
+ typeof mention === "string" && mention !== "",
401
+ "a mention is no text",
402
+ );
403
+ },
404
+ },
405
+ {
406
+ name: "a group's seats and log are conversations of their own",
407
+ applies: (subject) => subject.provider.conversations !== undefined,
408
+ run: async ({ provider }) => {
409
+ const forms = provider.conversations;
410
+ if (!forms) return;
411
+ const seat = forms.seat("g-1", "knight");
412
+ const log = forms.log("round-table");
413
+ expectThat(
414
+ forms.seat("g-1", "knight") === seat &&
415
+ forms.log("round-table") === log,
416
+ "a seat's key or a log name changes between calls",
417
+ );
418
+ expectThat(
419
+ seat !== forms.seat("g-1", "scribe") &&
420
+ seat !== forms.seat("g-2", "knight") &&
421
+ log !== forms.log("other-table"),
422
+ "two seats or two logs share a key",
423
+ );
424
+ },
425
+ },
426
+ ];
427
+
428
+ async function runCheck(
429
+ check: Check,
430
+ make: () => Promise<PlaceContractSubject>,
431
+ ): Promise<"passed" | "skipped"> {
432
+ const subject = await make();
433
+ try {
434
+ if (check.applies && !check.applies(subject)) return "skipped";
435
+ await check.run(subject);
436
+ return "passed";
437
+ } finally {
438
+ await subject.close?.();
439
+ }
440
+ }
441
+
442
+ /**
443
+ * Runs the place provider contract on fresh subjects from `make`, one per check, and returns the
444
+ * checks the provider broke. `placeProviderContract` registers the same checks as tests.
445
+ */
446
+ export async function checkPlaceProviderContract(
447
+ make: () => Promise<PlaceContractSubject>,
448
+ ): Promise<PlaceContractFailure[]> {
449
+ const failures: PlaceContractFailure[] = [];
450
+ for (const check of CHECKS) {
451
+ try {
452
+ await runCheck(check, make);
453
+ } catch (error) {
454
+ failures.push({ name: check.name, error });
455
+ }
456
+ }
457
+ return failures;
458
+ }
459
+
460
+ /**
461
+ * Registers the place provider contract as one test per check, under `name`: the provider names
462
+ * a surface and a storage, its keys read back as the places they came from (a derived one's from
463
+ * the name, a stored one's from the id) and no foreign key does, a post shows under the author's
464
+ * name with every chunk, and, where it offers them, interim posts are edited in place, `ensure`
465
+ * makes a place for each owner and brings an existing one back under its id, `remove` keeps the
466
+ * place, `check` tells a place that is there from one deleted (`gone`) or of another space
467
+ * (`foreign`), a deleted place is reported to `onRemoved`, and a group's seats and log are
468
+ * conversations of their own. Each check gets a fresh subject from `make`; a check for an
469
+ * optional part the provider lacks passes.
470
+ */
471
+ export function placeProviderContract(
472
+ name: string,
473
+ make: () => Promise<PlaceContractSubject>,
474
+ ): void {
475
+ describe(`${name} keeps the place provider contract`, () => {
476
+ for (const check of CHECKS)
477
+ test(check.name, async () => {
478
+ await runCheck(check, make);
479
+ });
480
+ });
481
+ }
@@ -0,0 +1,121 @@
1
+ import type { StoredPlace } from "../agents/agent-places.ts";
2
+ import { type StoredPlaces, TeamPlaces } from "../agents/team-places.ts";
3
+ import type { PlacePort, PlaceProvider } from "../contract/places.ts";
4
+ import type { SurfacePort } from "../contract/surface.ts";
5
+ import { discordPlaces } from "../discord/agent-places.ts";
6
+ import { silentLogger } from "../kernel/log.ts";
7
+ import { derivedPlaces } from "../registry/place-port.ts";
8
+
9
+ /** The surface of the legacy channel columns: what a store's `channelId` is a place on. */
10
+ const LEGACY = "discord";
11
+
12
+ /** The stored places of the given rows, read each time, so a test may change them. */
13
+ export function storedFrom(rows: () => readonly StoredPlace[]): StoredPlaces {
14
+ return {
15
+ on: (kind, name, surface) =>
16
+ rows().find(
17
+ (row) =>
18
+ row.kind === kind && row.name === name && row.surface === surface,
19
+ )?.externalId,
20
+ ownerOf: (surface, externalId) => {
21
+ const row = rows().find(
22
+ (r) => r.surface === surface && r.externalId === externalId,
23
+ );
24
+ return row && { kind: row.kind, name: row.name };
25
+ },
26
+ };
27
+ }
28
+
29
+ /** The Discord rows a store's `channelId` columns stand for. */
30
+ export function legacyRows(
31
+ agents: readonly { name: string; channelId?: string | undefined }[],
32
+ groups: readonly { name: string; channelId?: string | undefined }[] = [],
33
+ ): StoredPlace[] {
34
+ const row = (
35
+ kind: StoredPlace["kind"],
36
+ owner: { name: string; channelId?: string | undefined },
37
+ ): StoredPlace[] =>
38
+ owner.channelId === undefined
39
+ ? []
40
+ : [
41
+ {
42
+ kind,
43
+ name: owner.name,
44
+ surface: LEGACY,
45
+ externalId: owner.channelId,
46
+ },
47
+ ];
48
+ return [
49
+ ...agents.flatMap((a) => row("agent", a)),
50
+ ...groups.flatMap((g) => row("group", g)),
51
+ ];
52
+ }
53
+
54
+ /** Discord's own provider, with the keys and forms a host gives it, over no channels: for the tests that only read places. */
55
+ export function discordTestPlaces(guild = "900000000000000001"): PlaceProvider {
56
+ return discordPlaces({
57
+ // SAFETY: a provider that is only read never asks the client or the channels.
58
+ client: undefined as never,
59
+ guild,
60
+ channels: undefined as never,
61
+ onRemoved: () => undefined,
62
+ logger: silentLogger(),
63
+ });
64
+ }
65
+
66
+ /**
67
+ * A port over the providers a test names: `stored` first, then a derived provider for each of
68
+ * `rooms`, both read each time, so a test may change the surfaces that host rooms.
69
+ */
70
+ export function testPlacePort(options: {
71
+ rooms?: () => readonly string[];
72
+ stored?: () => readonly PlaceProvider[];
73
+ /** What the derived providers post through; absent = a post fails. */
74
+ surfaces?: SurfacePort;
75
+ }): PlacePort {
76
+ const surfaces =
77
+ options.surfaces ??
78
+ // SAFETY: only a post reads it, and a test that posts gives its own.
79
+ (undefined as never);
80
+ const all = () => [
81
+ ...(options.stored?.() ?? []),
82
+ ...(options.rooms?.() ?? []).map((surface) =>
83
+ derivedPlaces(surface, surfaces),
84
+ ),
85
+ ];
86
+ return {
87
+ of: (surfaceOrKey) => {
88
+ const at = surfaceOrKey.indexOf(":");
89
+ if (at === 0) return undefined;
90
+ const surface = at < 0 ? surfaceOrKey : surfaceOrKey.slice(0, at);
91
+ return all().find((p) => p.surface === surface);
92
+ },
93
+ stored: () => options.stored?.() ?? [],
94
+ all,
95
+ };
96
+ }
97
+
98
+ /** The places of a team over fakes: Discord's provider when `discord`, a room on each of `rooms`, and `stored` rows. */
99
+ export function testPlaces(options: {
100
+ store: ConstructorParameters<typeof TeamPlaces>[0];
101
+ rows?: () => readonly StoredPlace[];
102
+ rooms?: () => readonly string[];
103
+ discord?: boolean;
104
+ stored?: () => readonly PlaceProvider[];
105
+ }): TeamPlaces {
106
+ const discord = options.discord === false ? [] : [discordTestPlaces()];
107
+ return new TeamPlaces(
108
+ options.store,
109
+ storedFrom(options.rows ?? (() => [])),
110
+ testPlacePort({
111
+ ...(options.rooms ? { rooms: options.rooms } : {}),
112
+ stored: () => [...discord, ...(options.stored?.() ?? [])],
113
+ }),
114
+ );
115
+ }
116
+
117
+ /** The `spaceOf` of an agent claim whose Discord channels live in `guild`: the space of every Discord key, none for another surface's. */
118
+ export const discordSpaceOf =
119
+ (guild: string | undefined) =>
120
+ (channel: string): string | undefined =>
121
+ channel.startsWith("discord:") ? guild : undefined;
@@ -11,6 +11,7 @@ import { type RoundtableConfig, resolveConfig } from "../config/config.ts";
11
11
  import type { ConversationPort } from "../contract/channels.ts";
12
12
  import type { AgentRuntime } from "../contract/runtime.ts";
13
13
  import type { ChatSurface } from "../contract/surface.ts";
14
+ import { discordPlaceProvider } from "../discord/agent-places.ts";
14
15
  import { CommandCollection } from "../discord/command-collection.ts";
15
16
  import type { ComposedCommands } from "../discord/compose-commands.ts";
16
17
  import type { DiscordConnection } from "../discord/connection.ts";
@@ -183,6 +184,8 @@ export function standInDiscord(
183
184
  ownerId: string,
184
185
  /** What `DISCORD.channelContext` answers; by default no context, as on a host with it off. */
185
186
  channelContext: DiscordServices["channelContext"] = async () => undefined,
187
+ /** The agent guild, which makes the stand-in contribute Discord's place provider over `agentChannels`, as the built-in plugin does with one. */
188
+ agentGuild?: string,
186
189
  ): RoundtablePlugin {
187
190
  const surface: ChatSurface = {
188
191
  surface: "discord",
@@ -207,22 +210,36 @@ export function standInDiscord(
207
210
  ownerId,
208
211
  });
209
212
  // SAFETY: the plugins after it read only these members while they set up; the rest is read when a tool runs, which a test host does not do.
213
+ const connection = {
214
+ agentChannels: () => ({}),
215
+ agentDashboard: () => ({ show: async () => undefined }),
216
+ onChannelDeleted: () => undefined,
217
+ ownerChannel: async () => "discord:owner-dm",
218
+ ownerOperations: () => ({}),
219
+ ...overrides,
220
+ };
210
221
  services.provide(DISCORD, {
211
- connection: {
212
- agentChannels: () => ({}),
213
- agentDashboard: () => ({ show: async () => undefined }),
214
- onChannelDeleted: () => undefined,
215
- ownerChannel: async () => "discord:owner-dm",
216
- ownerOperations: () => ({}),
217
- ...overrides,
218
- },
222
+ connection,
219
223
  commands: commands.registrar,
220
224
  guard,
221
225
  threads: { open: async () => undefined, sweep: async () => undefined },
222
226
  poster: poster ?? fakeDiscordPoster(),
223
227
  channelContext,
224
228
  } as unknown as DiscordServices);
225
- return { surfaces: [surface], directChannels: [direct] };
229
+ return {
230
+ surfaces: [surface],
231
+ directChannels: [direct],
232
+ places: agentGuild
233
+ ? [
234
+ discordPlaceProvider({
235
+ guild: agentGuild,
236
+ // SAFETY: a stand-in's provider is read for keys; nothing asks the channels.
237
+ connection: connection as never,
238
+ logger: silentLogger(),
239
+ }),
240
+ ]
241
+ : [],
242
+ };
226
243
  },
227
244
  };
228
245
  }
@@ -282,7 +299,16 @@ export async function testHost(
282
299
  ...base,
283
300
  plugins: [
284
301
  ...(withDiscord
285
- ? [standInDiscord(commands, guard, given, discordOwnerOf(resolved).id)]
302
+ ? [
303
+ standInDiscord(
304
+ commands,
305
+ guard,
306
+ given,
307
+ discordOwnerOf(resolved).id,
308
+ undefined,
309
+ resolved.discord?.guild,
310
+ ),
311
+ ]
286
312
  : []),
287
313
  {
288
314
  name: "test-runtime",
@@ -21,6 +21,8 @@ export type {
21
21
  AgentPanelOptions,
22
22
  } from "../core/discord/agent-panel.ts";
23
23
  export { agentPanel } from "../core/discord/agent-panel.ts";
24
+ export type { DiscordPlaceProviderOptions } from "../core/discord/agent-places.ts";
25
+ export { discordPlaceProvider } from "../core/discord/agent-places.ts";
24
26
  export {
25
27
  CHANNEL_CONTEXT_DEFAULTS,
26
28
  formatChannelContext,