pi-roundtable 0.45.2 → 0.47.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 (79) hide show
  1. package/CHANGELOG.md +56 -0
  2. package/docs/plugins.md +33 -8
  3. package/package.json +2 -2
  4. package/src/characters/catalog.ts +1 -0
  5. package/src/characters/characters.css +2 -1
  6. package/src/characters/compose.ts +7 -3
  7. package/src/characters/css.ts +2 -1
  8. package/src/characters/index.ts +1 -0
  9. package/src/characters/parts-face.ts +14 -1
  10. package/src/core/agents/agent-claim.ts +2 -2
  11. package/src/core/agents/agent-group-store.ts +74 -1
  12. package/src/core/agents/agent-places.ts +32 -0
  13. package/src/core/agents/agent-ports.ts +0 -94
  14. package/src/core/agents/agent-prompt.ts +3 -1
  15. package/src/core/agents/agent-team-fixture.ts +74 -14
  16. package/src/core/agents/agent-team.ts +50 -59
  17. package/src/core/agents/agent-tools.ts +34 -166
  18. package/src/core/agents/message-target.ts +3 -2
  19. package/src/core/agents/routes.ts +2 -1
  20. package/src/core/agents/team-editing.ts +121 -150
  21. package/src/core/agents/team-helpers.ts +80 -59
  22. package/src/core/agents/team-keys.ts +16 -26
  23. package/src/core/agents/team-lifecycle.ts +155 -99
  24. package/src/core/agents/team-looks.ts +102 -0
  25. package/src/core/agents/team-options.ts +12 -11
  26. package/src/core/agents/team-place-writes.ts +215 -0
  27. package/src/core/agents/team-places.ts +30 -0
  28. package/src/core/agents/team-posts.ts +42 -73
  29. package/src/core/agents/team-selection.ts +10 -2
  30. package/src/core/agents/team-status.ts +3 -2
  31. package/src/core/agents/team-text.ts +14 -43
  32. package/src/core/agents/team-turn-types.ts +11 -16
  33. package/src/core/agents/team-turns.ts +7 -6
  34. package/src/core/builtin/agent-server-options.ts +27 -6
  35. package/src/core/builtin/agent-server.ts +69 -72
  36. package/src/core/builtin/discord.ts +60 -8
  37. package/src/core/builtin/modules.ts +2 -0
  38. package/src/core/config/agent-team.ts +5 -3
  39. package/src/core/contract/agent-channels.ts +105 -0
  40. package/src/core/contract/channels.ts +10 -4
  41. package/src/core/contract/discord-key.ts +15 -0
  42. package/src/core/contract/places.ts +28 -3
  43. package/src/core/db/legacy-channels.ts +31 -0
  44. package/src/core/db/team-move.ts +2 -2
  45. package/src/core/discord/agent-dashboard-service.ts +55 -0
  46. package/src/core/{agents → discord}/agent-dashboard.ts +28 -4
  47. package/src/core/discord/agent-discord.ts +1 -1
  48. package/src/core/discord/agent-guild.ts +141 -0
  49. package/src/core/{agents/team-layout.ts → discord/agent-layout.ts} +15 -31
  50. package/src/core/discord/agent-places.ts +52 -33
  51. package/src/core/discord/agent-tools.ts +237 -0
  52. package/src/core/discord/channel-operations.ts +2 -0
  53. package/src/core/discord/connection.ts +4 -1
  54. package/src/core/discord/discord-surface.ts +9 -14
  55. package/src/core/discord/place-fake-client.ts +59 -0
  56. package/src/core/host/define-roundtable.ts +7 -5
  57. package/src/core/modules/delegation/delegator.ts +5 -0
  58. package/src/core/plugin-api/agent-ops.ts +119 -0
  59. package/src/core/plugin-api/agent-server-ids.ts +9 -0
  60. package/src/core/plugin-api/define.ts +9 -0
  61. package/src/core/plugin-api/plugin.ts +2 -0
  62. package/src/core/plugin-api/services.ts +6 -0
  63. package/src/core/registry/contributions.ts +3 -0
  64. package/src/core/registry/places.ts +31 -1
  65. package/src/core/routing/channel-queue.ts +28 -12
  66. package/src/core/runtime/pi-agent-runtime.ts +1 -1
  67. package/src/core/runtime/runtime-plugin.ts +1 -0
  68. package/src/core/runtime/runtime-types.ts +6 -0
  69. package/src/core/{agents/agent-team-fake-channels.ts → testing/fake-agent-channels.ts} +5 -1
  70. package/src/core/testing/fake-pager-places.ts +16 -2
  71. package/src/core/testing/modules.ts +2 -1
  72. package/src/core/testing/place-contract.ts +3 -2
  73. package/src/core/testing/team-places.ts +73 -7
  74. package/src/core/testing/test-host.ts +20 -16
  75. package/src/discord/index.ts +5 -0
  76. package/src/kit/channels.ts +1 -2
  77. package/src/kit/support.ts +25 -7
  78. package/templates/adapters/web/package.json.tmpl +1 -1
  79. package/templates/package.json.tmpl +1 -1
@@ -0,0 +1,215 @@
1
+ import type { PlaceProvider, PlaceRef, PlaceSpec } from "../contract/places.ts";
2
+ import type { ChannelKey } from "../domain/conversation.ts";
3
+ import type { Agent } from "./agent-store.ts";
4
+ import type { TeamContext } from "./team-options.ts";
5
+
6
+ /** A place made or brought back on a surface, by its id there. */
7
+ export interface MadePlace {
8
+ surface: string;
9
+ id: string;
10
+ }
11
+
12
+ /** What a place is made or kept for: its owner, and the texts the surface shows it under. */
13
+ export interface PlaceWish extends PlaceRef {
14
+ displayName: string;
15
+ /** An agent's display name; a group's members and host on one line. */
16
+ topic: string;
17
+ /** What the creating tool was given for the providers' own arguments, such as `category`. */
18
+ hints?: Readonly<Record<string, string>>;
19
+ }
20
+
21
+ /** Where a temp knight's place goes: under its parent's place, on the surface it was made from. */
22
+ export interface HelperOrigin {
23
+ parent: Agent;
24
+ /** The conversation it was made from; absent, every surface that has a place for the parent. */
25
+ origin?: string | undefined;
26
+ }
27
+
28
+ const specOf = (wish: PlaceWish): PlaceSpec => ({
29
+ kind: wish.kind,
30
+ name: wish.name,
31
+ displayName: wish.displayName,
32
+ topic: wish.topic,
33
+ ...(wish.hints ? { hints: wish.hints } : {}),
34
+ });
35
+
36
+ /**
37
+ * The team's writes to the places of its agents and groups, through the stored place providers
38
+ * that make them: `ensure` makes a place or brings an archived one back, `remove` keeps it where
39
+ * nobody answers, and `describe` writes a group's topic. A provider that does not make places
40
+ * (a derived one's rooms exist with the name) is left out; so is one the owner has no place on.
41
+ */
42
+ export class TeamPlaceWrites {
43
+ readonly #ctx: TeamContext;
44
+
45
+ constructor(ctx: TeamContext) {
46
+ this.#ctx = ctx;
47
+ }
48
+
49
+ /** The stored providers that make places, in contribution order. */
50
+ #makers(): PlaceProvider[] {
51
+ return this.#ctx.options.places
52
+ .stored()
53
+ .filter((provider) => provider.ensure !== undefined);
54
+ }
55
+
56
+ /**
57
+ * Makes the places of a new agent or group, one on each provider that makes them, or, for a
58
+ * temp knight, under its parent's place on each surface it was made from. Nothing is recorded:
59
+ * `record` does that once the owner exists.
60
+ */
61
+ async make(wish: PlaceWish, under?: HelperOrigin): Promise<MadePlace[]> {
62
+ const made: MadePlace[] = [];
63
+ for (const provider of this.#makers()) {
64
+ const colon = under?.origin?.indexOf(":") ?? -1;
65
+ const from = colon > 0 ? under?.origin?.slice(0, colon) : undefined;
66
+ if (from && from !== provider.surface) continue;
67
+ const place = await this.makeOn(provider.surface, wish, under?.parent);
68
+ if (place) made.push(place);
69
+ }
70
+ return made;
71
+ }
72
+
73
+ /**
74
+ * Makes the owner's place on one surface, or brings back the one it has there; for a temp
75
+ * knight, under its parent's place on that surface. None when the surface makes no places or the
76
+ * parent has none there. `stored.forget` leaves the place it has stored out, as one that is not
77
+ * the surface's own. Nothing is recorded.
78
+ */
79
+ async makeOn(
80
+ surface: string,
81
+ wish: PlaceWish,
82
+ parent?: Agent,
83
+ stored: { forget: boolean } = { forget: false },
84
+ ): Promise<MadePlace | undefined> {
85
+ const { places } = this.#ctx.options;
86
+ const provider = places.providerOf(surface);
87
+ if (provider?.storage !== "stored" || !provider.ensure) return undefined;
88
+ const spec = specOf(wish);
89
+ if (parent) {
90
+ const above = places.idOf(
91
+ { kind: "agent", name: parent.name },
92
+ provider.surface,
93
+ );
94
+ if (above === undefined) return undefined;
95
+ spec.parent = above;
96
+ }
97
+ const existing = places.idOf(wish, provider.surface);
98
+ if (existing !== undefined && !stored.forget) spec.existing = existing;
99
+ return { surface, id: await provider.ensure(spec) };
100
+ }
101
+
102
+ /** Records the places an owner now has, in place of any it had on those surfaces. */
103
+ async record(ref: PlaceRef, made: readonly MadePlace[]): Promise<void> {
104
+ const { store } = this.#ctx.options;
105
+ for (const place of made)
106
+ await store.setPlace(ref.kind, ref.name, place.surface, place.id);
107
+ }
108
+
109
+ /**
110
+ * Brings an owner's archived places back, each under its own id (a place deleted meanwhile is
111
+ * made again, and so is one the provider finds foreign: another space's, such as a channel of a
112
+ * server the team left, which is left where it is), and makes one for an agent that had none on
113
+ * a surface that makes them; a group is given none it never had. A place that cannot be checked
114
+ * is taken as it is. Recorded by the caller.
115
+ */
116
+ async bringBack(wish: PlaceWish): Promise<MadePlace[]> {
117
+ const { places } = this.#ctx.options;
118
+ const made: MadePlace[] = [];
119
+ for (const provider of this.#makers()) {
120
+ if (
121
+ wish.kind === "group" &&
122
+ places.idOf(wish, provider.surface) === undefined
123
+ )
124
+ continue;
125
+ const place = await this.makeOn(provider.surface, wish, undefined, {
126
+ forget: await this.#foreign(provider, wish),
127
+ });
128
+ if (place) made.push(place);
129
+ }
130
+ return made;
131
+ }
132
+
133
+ /** Whether the provider finds the place the owner has stored on its surface to be another space's. */
134
+ async #foreign(provider: PlaceProvider, wish: PlaceRef): Promise<boolean> {
135
+ const { places, logger } = this.#ctx.options;
136
+ const id = places.idOf(wish, provider.surface);
137
+ if (id === undefined || !provider.check) return false;
138
+ try {
139
+ return (await provider.check(id)) === "foreign";
140
+ } catch (error) {
141
+ logger.warn(
142
+ { name: wish.name, surface: provider.surface, err: error },
143
+ "archived place not checked; taken as it is",
144
+ );
145
+ return false;
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Keeps an archived owner's places where nobody answers, with their history; says, for the
151
+ * archive tool's answer, what each provider did. A provider that fails is logged, and the rest
152
+ * go on.
153
+ */
154
+ async keep(wish: PlaceWish, helper: boolean): Promise<string> {
155
+ const { places, logger } = this.#ctx.options;
156
+ let said = "";
157
+ for (const provider of places.stored()) {
158
+ const id = places.idOf(wish, provider.surface);
159
+ if (id === undefined || !provider.remove) continue;
160
+ try {
161
+ const line = await provider.remove(id, { ...specOf(wish), helper });
162
+ // A line that brings no separator of its own (a provider of the first contract) gets one.
163
+ if (line) said += /^[;,]/.test(line) ? line : `; ${line}`;
164
+ } catch (error) {
165
+ logger.warn(
166
+ { name: wish.name, surface: provider.surface, err: error },
167
+ "archived place not kept",
168
+ );
169
+ }
170
+ }
171
+ return said;
172
+ }
173
+
174
+ /** Closes the places made for an owner that was never recorded, logging a failure. */
175
+ async discard(wish: PlaceWish, made: readonly MadePlace[]): Promise<void> {
176
+ const { places, logger } = this.#ctx.options;
177
+ for (const place of made)
178
+ await places
179
+ .providerOf(place.surface)
180
+ ?.remove?.(place.id, { ...specOf(wish), helper: true })
181
+ .catch((err: unknown) =>
182
+ logger.warn({ err }, "orphan place not closed"),
183
+ );
184
+ }
185
+
186
+ /** Writes a group's topic on every surface that has a place for it, without waiting. */
187
+ describe(wish: PlaceWish): void {
188
+ const { places, logger } = this.#ctx.options;
189
+ for (const provider of places.stored()) {
190
+ const id = places.idOf(wish, provider.surface);
191
+ if (id === undefined || !provider.describe) continue;
192
+ provider
193
+ .describe(id, wish.topic)
194
+ .catch((error: unknown) =>
195
+ logger.warn(
196
+ { group: wish.name, err: error },
197
+ "group topic not updated",
198
+ ),
199
+ );
200
+ }
201
+ }
202
+
203
+ /** How the model names the made places in text, such as `<#id>`. */
204
+ mentions(ref: PlaceRef, made: readonly MadePlace[]): string {
205
+ const { places } = this.#ctx.options;
206
+ return made
207
+ .flatMap((place) => {
208
+ const key: ChannelKey | undefined = places
209
+ .providerOf(place.surface)
210
+ ?.key(ref, place.id);
211
+ return key === undefined ? [] : [places.mention(key)];
212
+ })
213
+ .join(", ");
214
+ }
215
+ }
@@ -61,6 +61,36 @@ export class TeamPlaces {
61
61
  return this.#port?.all() ?? [];
62
62
  }
63
63
 
64
+ /** The stored providers, in contribution order: the surfaces whose places the team keeps rows for. */
65
+ stored(): readonly PlaceProvider[] {
66
+ return this.#port?.stored() ?? [];
67
+ }
68
+
69
+ /** The id of the agent's or group's place on a surface, when it has one there. */
70
+ idOf(ref: PlaceRef, surface: string): string | undefined {
71
+ return this.#stored.on(ref.kind, ref.name, surface);
72
+ }
73
+
74
+ /** The key of a stored place by its id on a surface, as a provider that reports a deleted place gives only the id. */
75
+ keyOfId(surface: string, id: string): ChannelKey | undefined {
76
+ const provider = this.providerOf(surface);
77
+ // A stored key comes from the id alone; the owner does not enter into it.
78
+ return provider?.storage === "stored"
79
+ ? provider.key({ kind: "agent", name: id }, id)
80
+ : undefined;
81
+ }
82
+
83
+ /**
84
+ * The place whose agent is the coordinator, when a stored provider names one (Discord's entry
85
+ * channel); undefined when none does, and the configured `coordinator` is it.
86
+ */
87
+ entry(): ChannelKey | undefined {
88
+ for (const provider of this.stored())
89
+ if (provider.coordinatorPlace !== undefined)
90
+ return this.keyOfId(provider.surface, provider.coordinatorPlace);
91
+ return undefined;
92
+ }
93
+
64
94
  /** The surfaces that hold places, in "auto" order. */
65
95
  surfaces(): readonly string[] {
66
96
  return this.ordered().map((provider) => provider.surface);
@@ -1,22 +1,18 @@
1
- import { channelKey, parseChannelKey } from "../contract/surface.ts";
2
1
  import { supportOf } from "../domain/block-support.ts";
3
2
  import type {
4
3
  ChannelKey,
5
4
  IntercomCard,
6
- PostAuthor,
7
5
  ReplyFile,
8
6
  } from "../domain/conversation.ts";
9
7
  import { AgentError } from "../domain/errors.ts";
10
8
  import type { InterimPosts } from "../domain/interim.ts";
11
9
  import { chunksFor } from "../presentation/reply-chunks.ts";
12
10
  import type { Agent } from "./agent-store.ts";
13
- import { channelIdOf, discordIdOf } from "./team-keys.ts";
14
11
  import type { TeamTurnsOptions } from "./team-turn-types.ts";
15
12
 
16
13
  /**
17
14
  * How the team's turns reach a place: whether a reply there may carry files, how its text is cut,
18
- * and the interim posts and replies under the agent's name, which Discord's channels or a room's
19
- * surface carry.
15
+ * and the interim posts and replies under the agent's name, which the place's provider carries.
20
16
  */
21
17
  export class TeamPosts {
22
18
  readonly #options: TeamTurnsOptions;
@@ -31,14 +27,11 @@ export class TeamPosts {
31
27
  }
32
28
 
33
29
  /**
34
- * Whether a reply in the channel may carry files: Discord's webhook posts do; a room only when
35
- * its surface declares `supportsFiles`, since a reply with files that surface refuses is lost whole.
30
+ * Whether a reply in the channel may carry files: only when the surface that serves it declares
31
+ * `supportsFiles`, since a reply with files that surface refuses is lost whole.
36
32
  */
37
33
  carriesFiles(channel: ChannelKey): boolean {
38
- return (
39
- discordIdOf(channel) !== undefined ||
40
- this.#options.surfaces?.of(channel)?.supportsFiles === true
41
- );
34
+ return this.#options.surfaces?.of(channel)?.supportsFiles === true;
42
35
  }
43
36
 
44
37
  /** What the surface that serves the channel does there (see `supportOf`). */
@@ -57,49 +50,35 @@ export class TeamPosts {
57
50
 
58
51
  /**
59
52
  * The turn's interim posts under the agent's name, read as each goes out so a name changed
60
- * during the turn shows; none in a channel no agent or group owns any more.
53
+ * during the turn shows; none in a channel no agent or group owns any more, and none where the
54
+ * place's provider shows none.
61
55
  */
62
56
  interim(channel: ChannelKey, agent: Agent): InterimPosts | undefined {
63
- const { places, channels, studio, surfaces } = this.#options;
64
- if (discordIdOf(channel) === undefined) {
65
- // A room's interim posts are its surface's, under the agent as author.
66
- const first = surfaces?.interim(channel, this.author(agent));
67
- if (!surfaces || !first) return undefined;
68
- return {
69
- ...(first.tools === false ? { tools: false } : {}),
70
- // The poster cuts text for the surface the posts go to.
71
- ...(first.blocks ? { blocks: first.blocks } : {}),
72
- ...(first.blockText ? { blockText: first.blockText } : {}),
73
- post: async (text) => {
74
- if (!places.ownerKind(channel))
75
- throw new AgentError(`${channel} is archived`);
76
- const posts = surfaces.interim(
77
- channel,
78
- this.author(this.#current(agent)),
79
- );
80
- if (!posts) throw new AgentError(`${channel} shows no interim posts`);
81
- return posts.post(text);
82
- },
83
- };
84
- }
85
- if (!channels?.interim) return undefined;
86
- const interim = channels.interim.bind(channels);
57
+ const { places } = this.#options;
58
+ const provider = places.providerOf(channel);
59
+ const first = provider?.interim?.(channel, this.author(agent));
60
+ if (!provider || !first) return undefined;
87
61
  return {
62
+ ...(first.tools === false ? { tools: false } : {}),
63
+ // The poster cuts text for the surface the posts go to.
64
+ ...(first.blocks ? { blocks: first.blocks } : {}),
65
+ ...(first.blockText ? { blockText: first.blockText } : {}),
88
66
  post: async (text) => {
89
67
  if (!places.ownerKind(channel))
90
68
  throw new AgentError(`${channel} is archived`);
91
- const as = this.#current(agent);
92
- return interim(channelIdOf(channel), {
93
- name: as.displayName,
94
- avatarUrl: studio.url(as.avatarHash),
95
- }).post(text);
69
+ const posts = provider.interim?.(
70
+ channel,
71
+ this.author(this.#current(agent)),
72
+ );
73
+ if (!posts) throw new AgentError(`${channel} shows no interim posts`);
74
+ return posts.post(text);
96
75
  },
97
76
  };
98
77
  }
99
78
 
100
79
  /**
101
- * Posts under the agent's name. A channel no agent or group owns any more, archived while a
102
- * turn ran, gets nothing, so its webhook is not made again.
80
+ * Posts under the agent's name through the place's provider. A channel no agent or group owns
81
+ * any more, archived while a turn ran, gets nothing, so its webhook is not made again.
103
82
  */
104
83
  async post(
105
84
  channel: ChannelKey,
@@ -113,7 +92,7 @@ export class TeamPosts {
113
92
  intercom?: IntercomCard;
114
93
  },
115
94
  ): Promise<void> {
116
- const { places, channels, studio, surfaces, logger } = this.#options;
95
+ const { places, logger } = this.#options;
117
96
  if (!places.ownerKind(channel)) {
118
97
  logger.info(
119
98
  { channel, agent: as.name },
@@ -121,37 +100,27 @@ export class TeamPosts {
121
100
  );
122
101
  return;
123
102
  }
124
- // Discord ignores `failed`.
125
- const { failed, ...rest } = body;
126
- if (discordIdOf(channel) === undefined) {
127
- if (!surfaces)
128
- throw new AgentError(`no surface serves the room ${channel}`);
129
- const { thinking, chunks, files, threadId, intercom } = rest;
130
- // A thread of the room's surface is a channel of that surface, and speaks there.
131
- return surfaces.sendReply(
132
- threadId === undefined
133
- ? channel
134
- : channelKey(parseChannelKey(channel).surface, threadId),
135
- {
136
- ...(thinking ? { thinking } : {}),
137
- chunks,
138
- ...(files?.length ? { files } : {}),
139
- author: this.author(as),
140
- ...(failed ? { failed } : {}),
141
- ...(intercom ? { intercom } : {}),
142
- },
143
- );
144
- }
145
- if (!channels) throw new AgentError(`no Discord channels serve ${channel}`);
146
- return channels.post(channelIdOf(channel), {
147
- name: as.displayName,
148
- avatarUrl: studio.url(as.avatarHash),
149
- ...rest,
103
+ const provider = places.providerOf(channel);
104
+ if (!provider)
105
+ throw new AgentError(`no surface serves the place ${channel}`);
106
+ const { threadId, thinking, chunks, files, failed, intercom } = body;
107
+ return provider.post(channel, {
108
+ author: this.author(as),
109
+ ...(thinking ? { thinking } : {}),
110
+ chunks,
111
+ ...(files?.length ? { files } : {}),
112
+ ...(threadId === undefined ? {} : { thread: threadId }),
113
+ ...(failed ? { failed } : {}),
114
+ ...(intercom ? { intercom } : {}),
150
115
  });
151
116
  }
152
117
 
153
- /** Who speaks for the agent on a surface that shows authors. */
154
- author(agent: Agent): PostAuthor {
155
- return { name: agent.displayName, agent: agent.name };
118
+ /** Who speaks for the agent: its name and look. */
119
+ author(agent: Agent) {
120
+ return {
121
+ name: agent.displayName,
122
+ agent: agent.name,
123
+ avatarUrl: this.#options.studio.url(agent.avatarHash),
124
+ };
156
125
  }
157
126
  }
@@ -14,7 +14,11 @@ import type { AgentTeamOptions } from "./team-options.ts";
14
14
  export function teamSelection(
15
15
  options: Pick<
16
16
  AgentTeamOptions,
17
- "pluginSelection" | "pluginTools" | "agentTools" | "helpers"
17
+ | "pluginSelection"
18
+ | "pluginTools"
19
+ | "pluginWithheldFromHelpers"
20
+ | "agentTools"
21
+ | "helpers"
18
22
  > & { store?: Pick<AgentTeamOptions["store"], "agent"> },
19
23
  scope?: AgentTurnScope,
20
24
  ): TurnSelection {
@@ -24,7 +28,11 @@ export function teamSelection(
24
28
  const helper =
25
29
  scope !== undefined && isHelper(options.store?.agent(scope.name));
26
30
  const kept = (tool: string): boolean =>
27
- !(helper && WITHHELD_FROM_HELPERS.includes(tool)) &&
31
+ !(
32
+ helper &&
33
+ (WITHHELD_FROM_HELPERS.includes(tool) ||
34
+ options.pluginWithheldFromHelpers?.().includes(tool))
35
+ ) &&
28
36
  !(
29
37
  (options.helpers === false || scope?.group) &&
30
38
  (HELPER_TOOLS as readonly string[]).includes(tool)
@@ -1,6 +1,6 @@
1
+ import { legacyChannelIdOf } from "../db/legacy-channels.ts";
1
2
  import type { ThinkingSetting } from "../domain/models.ts";
2
3
  import type { ContextUse } from "./agent-ports.ts";
3
- import { discordIdOf } from "./team-keys.ts";
4
4
  import type { TeamContext } from "./team-options.ts";
5
5
  import type { TeamPlace } from "./team-places.ts";
6
6
 
@@ -63,7 +63,8 @@ export async function teamStatus(ctx: TeamContext): Promise<TeamStatus> {
63
63
  const keys = places.keysOf(agent.name);
64
64
  const working = turns.workingIn(agent.name);
65
65
  // The board links Discord channels; a turn in a room has none to link.
66
- const workingIn = working === undefined ? undefined : discordIdOf(working);
66
+ const workingIn =
67
+ working === undefined ? undefined : legacyChannelIdOf(working);
67
68
  const queued = keys.reduce((sum, key) => sum + queue.size(key), 0);
68
69
  const context = keys
69
70
  .map((key) => runtime.contextUsage?.(key))
@@ -1,37 +1,15 @@
1
1
  import { CATALOG, type CharacterSpec } from "../../characters/index.ts";
2
- import { AgentError } from "../domain/errors.ts";
3
2
  import type { ThinkingSetting } from "../domain/models.ts";
4
- import { assistantName } from "../i18n/index.ts";
5
- import { zonedStamp } from "../kernel/time.ts";
3
+ import { assistantName, messages } from "../i18n/index.ts";
6
4
  import type { AgentDirectory } from "../plugin-api/services.ts";
7
- import type { AgentChannels, ChannelMessage } from "./agent-ports.ts";
8
5
  import { characterOf, isHelper } from "./agent-rules.ts";
9
6
  import { describeThinking } from "./agent-settings.ts";
10
- import type { Agent } from "./agent-store.ts";
7
+ import type { Agent, AgentGroup } from "./agent-store.ts";
11
8
 
12
- /** Said by the tools that work on Discord channels when the team has none. */
9
+ /** What `AgentOps.arrange` and `AgentOps.read` refuse with when the team has no Discord channels. */
13
10
  export const NO_CHANNELS =
14
11
  "This host has no Discord channels: the agents live in rooms of a chat surface, so there is nothing to arrange or read here.";
15
12
 
16
- /** One message as channel_read shows it: id, local time, author, text, attachments. */
17
- export function messageLine(message: ChannelMessage): string {
18
- const files =
19
- message.attachments.length > 0
20
- ? ` [attachments: ${message.attachments.join(", ")}]`
21
- : "";
22
- return `[${message.id}] ${zonedStamp(message.at)} ${message.author}: ${message.text}${files}`;
23
- }
24
-
25
- /** A channel given as `<#id>` or its id, as its id. */
26
- export function channelIdFromRef(channel: string): string {
27
- const id = /^<#(\d+)>$/.exec(channel.trim())?.[1] ?? channel.trim();
28
- if (!/^\d+$/.test(id))
29
- throw new AgentError(
30
- "Give the channel as <#id> or its id, as in a forwarded message.",
31
- );
32
- return id;
33
- }
34
-
35
13
  /** Every agent and group as agent_list shows them, before the category layout. */
36
14
  export function teamListText(store: Pick<AgentDirectory, "agents" | "groups">) {
37
15
  const all = store.agents();
@@ -91,22 +69,15 @@ export function agentDetails(
91
69
  ].join("\n");
92
70
  }
93
71
 
94
- const READ_LIMIT = 50;
95
-
96
- /** channel_read: messages of a Discord channel, newest last, around one when given. */
97
- export async function readChannel(
98
- channels: AgentChannels | undefined,
99
- channel: string,
100
- around: string | undefined,
101
- limit: number | undefined,
102
- ): Promise<string> {
103
- if (!channels) throw new AgentError(NO_CHANNELS);
104
- const id = channelIdFromRef(channel);
105
- const messages = await channels.read(id, {
106
- limit: Math.min(Math.max(limit ?? 20, 1), READ_LIMIT),
107
- ...(around ? { around } : {}),
108
- });
109
- return messages.length > 0
110
- ? messages.map(messageLine).join("\n")
111
- : `<#${id}> has no messages there.`;
72
+ /** A group channel's topic: who is in it and who hosts. */
73
+ export function groupTopic(
74
+ group: AgentGroup,
75
+ store: Pick<AgentDirectory, "agent">,
76
+ ): string {
77
+ const shown = (name: string) => store.agent(name)?.displayName ?? name;
78
+ return messages().groupTopic(
79
+ group.displayName,
80
+ group.members.map(shown),
81
+ shown(group.host),
82
+ );
112
83
  }
@@ -16,7 +16,7 @@ import type { Logger } from "../kernel/log.ts";
16
16
  import type { EventSink } from "../plugin-api/plugin.ts";
17
17
  import type { AvatarStudio } from "../plugin-api/services.ts";
18
18
  import type { ToolTiers } from "../runtime/tool-tiers.ts";
19
- import type { AgentChannels, AgentTurnRunner } from "./agent-ports.ts";
19
+ import type { AgentTurnRunner } from "./agent-ports.ts";
20
20
  import type { Agent, PgAgentStore } from "./agent-store.ts";
21
21
  import type { RelevanceScorer } from "./group-round.ts";
22
22
  import type { HelperWork } from "./helper-work.ts";
@@ -35,12 +35,12 @@ export interface Chain {
35
35
  }
36
36
 
37
37
  export interface TeamTurnsOptions {
38
- /** The agent server's entry channel, where the coordinator lives; absent on a host without Discord. */
39
- entryChannelId?: string;
38
+ /** The agent server's entry channel, where the coordinator lives; absent on a host without one. */
39
+ entryChannel?: ChannelKey;
40
40
  /**
41
- * The coordinator's name on a host without Discord, where there is no entry channel to find it
42
- * by; a function is asked each time, for a name that is only known once the plugins linked.
43
- * Ignored when `entryChannelId` is set.
41
+ * The coordinator's name on a host without an entry channel to find it by; a function is asked
42
+ * each time, for a name that is only known once the plugins linked. Ignored when `entryChannel`
43
+ * is set.
44
44
  */
45
45
  coordinator?: string | (() => string | undefined);
46
46
  /** Who the agents work for, as their prompts and group history name them. */
@@ -48,18 +48,13 @@ export interface TeamTurnsOptions {
48
48
  store: PgAgentStore;
49
49
  /** Where the agents and groups are: their keys, and who owns a key, through the host's place providers. */
50
50
  places: TeamPlaces;
51
- /** The Discord channels the agents post in; absent on a host without Discord, where every reply goes through a room's surface. */
52
- channels?: Pick<AgentChannels, "post" | "interim">;
53
51
  /**
54
- * The surfaces that host agent rooms. A reply in a room goes through `sendReply` with the
55
- * agent as author, and its interim posts through `interim`, never through the Discord
56
- * channels; without it no key but Discord's is the team's. `of` tells whether the room's
57
- * surface carries reply files. Which surfaces host rooms is `places`'s to say.
52
+ * What the team asks of the chat surfaces beyond their places: `of` tells how a place's surface
53
+ * cuts text and whether it carries reply files, `progress` and `groupRound` show a turn's
54
+ * state in a room. Replies and interim posts go through the place providers, which post through
55
+ * the surfaces themselves.
58
56
  */
59
- surfaces?: Pick<
60
- SurfacePort,
61
- "sendReply" | "interim" | "of" | "lang" | "progress" | "groupRound"
62
- >;
57
+ surfaces?: Pick<SurfacePort, "of" | "lang" | "progress" | "groupRound">;
63
58
  studio: Pick<AvatarStudio, "url">;
64
59
  /**
65
60
  * The host's routing table, which says where work no person started lands. Without one the team
@@ -18,7 +18,7 @@ import { HelperWork } from "./helper-work.ts";
18
18
  import { roomProgress } from "./room-progress.ts";
19
19
  import { type Routes, routesOf } from "./routes.ts";
20
20
  import { mayApprove } from "./team-approval.ts";
21
- import { coordinatorOf, discordKey } from "./team-keys.ts";
21
+ import { coordinatorOf, entryChannelOf } from "./team-keys.ts";
22
22
  import { TeamPosts } from "./team-posts.ts";
23
23
  import {
24
24
  type Chain,
@@ -295,20 +295,21 @@ export class TeamTurns {
295
295
 
296
296
  /**
297
297
  * The assistant's own notice, such as a new release, posted in the coordinator's channel under
298
- * its name: the entry channel, or, without Discord, its room on the first surface that hosts rooms.
298
+ * its name: the entry channel, or, without one, its place on the first surface that has one.
299
299
  */
300
300
  async announce(text: string): Promise<void> {
301
- const { store, places, entryChannelId } = this.#options;
301
+ const { store, places } = this.#options;
302
+ const entryChannel = entryChannelOf(places, this.#options);
302
303
  const coordinator = coordinatorOf(store, places, this.#options);
303
304
  if (!coordinator)
304
305
  throw new AgentError(
305
- entryChannelId === undefined
306
+ entryChannel === undefined
306
307
  ? "no active coordinator"
307
308
  : "no active agent in the entry channel",
308
309
  );
309
310
  const home =
310
- entryChannelId !== undefined
311
- ? discordKey(entryChannelId)
311
+ entryChannel !== undefined
312
+ ? entryChannel
312
313
  : this.routes.primary(coordinator.name);
313
314
  if (!home)
314
315
  throw new AgentError(`${coordinator.name} has no room to post in`);