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.
- package/CHANGELOG.md +31 -0
- package/docs/plugins.md +36 -2
- package/package.json +2 -2
- package/src/core/agents/agent-claim.ts +15 -21
- package/src/core/agents/agent-messages.ts +10 -10
- package/src/core/agents/agent-prompt.ts +7 -8
- package/src/core/agents/agent-rules.ts +9 -2
- package/src/core/agents/agent-store.ts +16 -0
- package/src/core/agents/agent-team-fake-channels.ts +105 -0
- package/src/core/agents/agent-team-fixture.ts +30 -105
- package/src/core/agents/agent-team.ts +20 -21
- package/src/core/agents/group-turns.ts +8 -15
- package/src/core/agents/helper-modes.ts +7 -13
- package/src/core/agents/helper-work.ts +7 -6
- package/src/core/agents/message-target.ts +24 -12
- package/src/core/agents/room-progress.ts +8 -6
- package/src/core/agents/routes.ts +24 -47
- package/src/core/agents/team-editing.ts +5 -8
- package/src/core/agents/team-keys.ts +5 -190
- package/src/core/agents/team-lifecycle.ts +23 -39
- package/src/core/agents/team-options.ts +1 -1
- package/src/core/agents/team-places.ts +335 -0
- package/src/core/agents/team-posts.ts +157 -0
- package/src/core/agents/team-prompt.ts +3 -3
- package/src/core/agents/team-status.ts +18 -10
- package/src/core/agents/team-turn-types.ts +5 -8
- package/src/core/agents/team-turns.ts +26 -169
- package/src/core/builtin/agent-server-test-kit.ts +29 -0
- package/src/core/builtin/agent-server.ts +19 -12
- package/src/core/builtin/discord.ts +6 -0
- package/src/core/contract/places.ts +141 -0
- package/src/core/discord/agent-places.ts +215 -0
- package/src/core/discord/discord-surface.ts +13 -0
- package/src/core/host/define-roundtable.ts +1 -0
- package/src/core/plugin-api/agent-assets.ts +36 -0
- package/src/core/plugin-api/plugin-context.ts +30 -0
- package/src/core/plugin-api/plugin.ts +10 -28
- package/src/core/plugin-api/services.ts +20 -39
- package/src/core/registry/claims.ts +120 -0
- package/src/core/registry/contributions.ts +36 -118
- package/src/core/registry/place-port.ts +94 -0
- package/src/core/registry/places.ts +43 -0
- package/src/core/testing/fake-pager-places.ts +120 -0
- package/src/core/testing/fake-room-surface.ts +60 -0
- package/src/core/testing/modules.ts +5 -5
- package/src/core/testing/place-contract.ts +481 -0
- package/src/core/testing/team-places.ts +121 -0
- package/src/core/testing/test-host.ts +36 -10
- package/src/discord/index.ts +2 -0
- package/src/index.ts +12 -0
- package/src/testing.ts +15 -3
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { type Client, DiscordAPIError } from "discord.js";
|
|
2
|
+
import { Type } from "typebox";
|
|
3
|
+
import type { AgentChannels } from "../agents/agent-ports.ts";
|
|
4
|
+
import { categoryKind, teamCategory } from "../agents/team-layout.ts";
|
|
5
|
+
import type { PlaceCheck, PlaceProvider } from "../contract/places.ts";
|
|
6
|
+
import { channelKey, parseChannelKey } from "../contract/surface.ts";
|
|
7
|
+
import { AgentError } from "../domain/errors.ts";
|
|
8
|
+
import { PluginError } from "../kernel/errors.ts";
|
|
9
|
+
import type { Logger } from "../kernel/log.ts";
|
|
10
|
+
import type { DiscordConnection } from "./connection.ts";
|
|
11
|
+
|
|
12
|
+
const SURFACE = "discord";
|
|
13
|
+
|
|
14
|
+
/** Discord's error code for a channel that does not exist. */
|
|
15
|
+
const UNKNOWN_CHANNEL = 10003;
|
|
16
|
+
|
|
17
|
+
export interface DiscordPlacesOptions {
|
|
18
|
+
/** The discord.js client that can ask Discord whether a stored channel still exists; without one the provider has no `check`, and a stored channel is taken as it is. */
|
|
19
|
+
client?: Client;
|
|
20
|
+
/** The agent guild: the only server whose channels are the team's places. */
|
|
21
|
+
guild: string;
|
|
22
|
+
channels: AgentChannels;
|
|
23
|
+
/** Reports a server channel deleted, by id; as `DiscordConnection.onChannelDeleted`. */
|
|
24
|
+
onRemoved(listener: (channelId: string) => void): void;
|
|
25
|
+
logger: Logger;
|
|
26
|
+
/** The channel the coordinator lives in; absent = the configured `coordinator`. */
|
|
27
|
+
entryChannel?: string;
|
|
28
|
+
/** The name of the agent or group whose channel it is, for `layoutText`; a channel nobody owns shows as a mention. */
|
|
29
|
+
nameOf?(channelId: string): string | undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const category = (kind: "Agents" | "Groups") =>
|
|
33
|
+
Type.Optional(
|
|
34
|
+
Type.String({
|
|
35
|
+
description: `Discord only: the category its channel opens under, ${kind} (the default) or ${kind}-<suffix>, such as ${kind}-work; made when missing. Keep to the categories agent_list shows unless a new one fits better. The app's room groups follow group.`,
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Discord's places: a channel for each agent and group in the agent guild, under the `Agents` or
|
|
41
|
+
* `Groups` category, and a thread of its parent's channel for a temp knight. The channel's id is
|
|
42
|
+
* the place's, and the keys, a group's seats and its log keep the forms they had before places
|
|
43
|
+
* were providers, so no stored session, schedule or held action moves.
|
|
44
|
+
*/
|
|
45
|
+
export function discordPlaces(options: DiscordPlacesOptions): PlaceProvider {
|
|
46
|
+
const { client, guild, channels, logger } = options;
|
|
47
|
+
const mention = (id: string) => `<#${id}>`;
|
|
48
|
+
const checkWith =
|
|
49
|
+
(asked: Client) =>
|
|
50
|
+
async (id: string): Promise<PlaceCheck> => {
|
|
51
|
+
// Fetched fresh: a cached channel says nothing of one deleted since.
|
|
52
|
+
const found = await asked.channels
|
|
53
|
+
.fetch(id, { force: true })
|
|
54
|
+
.catch((error: unknown) => {
|
|
55
|
+
// Only Discord's own "unknown" says gone. Missing Access (50001) says nothing of whose
|
|
56
|
+
// guild the channel is in: a hidden channel of the agent guild answers it too, so it throws.
|
|
57
|
+
if (
|
|
58
|
+
error instanceof DiscordAPIError &&
|
|
59
|
+
error.code === UNKNOWN_CHANNEL
|
|
60
|
+
)
|
|
61
|
+
return "gone" as const;
|
|
62
|
+
throw error;
|
|
63
|
+
});
|
|
64
|
+
if (found === "gone") return found;
|
|
65
|
+
// A channel discord.js cannot build is none of the agent guild's; only Discord's own "unknown" says gone.
|
|
66
|
+
if (found === null) return "foreign";
|
|
67
|
+
return !found.isDMBased() && found.guildId === guild ? "ok" : "foreign";
|
|
68
|
+
};
|
|
69
|
+
return {
|
|
70
|
+
surface: SURFACE,
|
|
71
|
+
storage: "stored",
|
|
72
|
+
space: guild,
|
|
73
|
+
...(options.entryChannel === undefined
|
|
74
|
+
? {}
|
|
75
|
+
: { coordinatorPlace: options.entryChannel }),
|
|
76
|
+
key: (_ref, id) => {
|
|
77
|
+
if (id === undefined)
|
|
78
|
+
throw new PluginError("a Discord place is keyed by its channel id");
|
|
79
|
+
return channelKey(SURFACE, id);
|
|
80
|
+
},
|
|
81
|
+
placeOf: (key) => {
|
|
82
|
+
if (typeof key !== "string" || !key.includes(":")) return undefined;
|
|
83
|
+
const parsed = parseChannelKey(key);
|
|
84
|
+
return parsed.surface === SURFACE ? { id: parsed.id } : undefined;
|
|
85
|
+
},
|
|
86
|
+
post: (key, post) =>
|
|
87
|
+
channels.post(parseChannelKey(key).id, {
|
|
88
|
+
name: post.author.name,
|
|
89
|
+
avatarUrl: post.author.avatarUrl,
|
|
90
|
+
...(post.thinking ? { thinking: post.thinking } : {}),
|
|
91
|
+
chunks: post.chunks,
|
|
92
|
+
...(post.files?.length ? { files: post.files } : {}),
|
|
93
|
+
...(post.thread === undefined ? {} : { threadId: post.thread }),
|
|
94
|
+
...(post.intercom ? { intercom: post.intercom } : {}),
|
|
95
|
+
}),
|
|
96
|
+
interim: (key, author) =>
|
|
97
|
+
channels.interim?.(parseChannelKey(key).id, {
|
|
98
|
+
name: author.name,
|
|
99
|
+
avatarUrl: author.avatarUrl,
|
|
100
|
+
}),
|
|
101
|
+
ensure: async (spec) => {
|
|
102
|
+
// A temp knight's place is a thread of its parent's channel, opened again when it is kept.
|
|
103
|
+
if (spec.parent !== undefined) {
|
|
104
|
+
if (
|
|
105
|
+
spec.existing !== undefined &&
|
|
106
|
+
channels.openThread &&
|
|
107
|
+
(await channels.exists(spec.existing))
|
|
108
|
+
) {
|
|
109
|
+
await channels.openThread(spec.existing);
|
|
110
|
+
return spec.existing;
|
|
111
|
+
}
|
|
112
|
+
if (!channels.createThread)
|
|
113
|
+
throw new AgentError("this Discord connection makes no threads");
|
|
114
|
+
return channels.createThread(spec.parent, spec.displayName);
|
|
115
|
+
}
|
|
116
|
+
const under = teamCategory(
|
|
117
|
+
spec.hints?.category,
|
|
118
|
+
spec.kind === "agent" ? "Agents" : "Groups",
|
|
119
|
+
);
|
|
120
|
+
if (
|
|
121
|
+
spec.existing !== undefined &&
|
|
122
|
+
(await channels.exists(spec.existing))
|
|
123
|
+
) {
|
|
124
|
+
await channels.placeIn(spec.existing, under);
|
|
125
|
+
return spec.existing;
|
|
126
|
+
}
|
|
127
|
+
return channels.createChannel(spec.name, spec.topic, under);
|
|
128
|
+
},
|
|
129
|
+
remove: async (id, spec) => {
|
|
130
|
+
// A thread is closed rather than moved; one that will not close stays silent where it is.
|
|
131
|
+
if (spec.helper) {
|
|
132
|
+
try {
|
|
133
|
+
await channels.closeThread?.(id);
|
|
134
|
+
} catch (error) {
|
|
135
|
+
logger.warn(
|
|
136
|
+
{ agent: spec.name, err: error },
|
|
137
|
+
"temp knight's thread not closed; it stays silent where it is",
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
return undefined;
|
|
141
|
+
}
|
|
142
|
+
try {
|
|
143
|
+
await channels.removeWebhook(id);
|
|
144
|
+
} catch (error) {
|
|
145
|
+
logger.warn(
|
|
146
|
+
{ name: spec.name, err: error },
|
|
147
|
+
"archived channel's webhook not removed",
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
try {
|
|
151
|
+
await channels.placeIn(id, "Archive");
|
|
152
|
+
} catch (error) {
|
|
153
|
+
logger.error(
|
|
154
|
+
{ name: spec.name, err: error },
|
|
155
|
+
"archived channel not moved",
|
|
156
|
+
);
|
|
157
|
+
return `its channel ${mention(id)} could not be moved to Archive (${error instanceof Error ? error.message : String(error)}); it stays silent where it is.`;
|
|
158
|
+
}
|
|
159
|
+
return `its channel ${mention(id)} is kept under Archive.`;
|
|
160
|
+
},
|
|
161
|
+
...(client ? { check: checkWith(client) } : {}),
|
|
162
|
+
describe: (id, topic) => channels.setTopic(id, topic),
|
|
163
|
+
onRemoved: (listener) => options.onRemoved(listener),
|
|
164
|
+
mention: (key) => mention(parseChannelKey(key).id),
|
|
165
|
+
// The forms Discord's groups have always had, so their sessions and logs carry over.
|
|
166
|
+
conversations: {
|
|
167
|
+
seat: (groupId, agent) => `agentgroup:${groupId}.${agent}`,
|
|
168
|
+
log: (group) => group,
|
|
169
|
+
},
|
|
170
|
+
createArguments: {
|
|
171
|
+
agent: { category: category("Agents") },
|
|
172
|
+
group: { category: category("Groups") },
|
|
173
|
+
},
|
|
174
|
+
layoutText: async () => {
|
|
175
|
+
const layout = await channels.layout();
|
|
176
|
+
const lines = layout.flatMap((c) => {
|
|
177
|
+
if (!categoryKind(c.name)) return [];
|
|
178
|
+
const shown = c.channelIds.map(
|
|
179
|
+
(id) => options.nameOf?.(id) ?? mention(id),
|
|
180
|
+
);
|
|
181
|
+
return [`- ${c.name}: ${shown.join(", ") || "empty"}`];
|
|
182
|
+
});
|
|
183
|
+
return `Categories, in server order:\n${lines.join("\n") || "none"}`;
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export interface DiscordPlaceProviderOptions {
|
|
189
|
+
/** The agent guild: the only server whose channels are the team's places. */
|
|
190
|
+
guild: string;
|
|
191
|
+
/** The Discord the host uses, as `DISCORD` provides it: the provider reads the guild's channels and deleted channels from it. */
|
|
192
|
+
connection: Pick<DiscordConnection, "agentChannels" | "onChannelDeleted">;
|
|
193
|
+
logger: Logger;
|
|
194
|
+
/** The discord.js client, so the team can check at start that its stored channels still exist; absent = they are taken as they are. */
|
|
195
|
+
client?: Client;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Discord's place provider for a plugin that replaces `DISCORD` with a Discord of its own: the
|
|
200
|
+
* agent guild's channels as the team's places, with the keys, seats and log names the built-in
|
|
201
|
+
* Discord plugin gives them, so no stored session, schedule or held action moves. Contribute it
|
|
202
|
+
* with `places` from the replacing plugin whenever the host has `discord.guild`.
|
|
203
|
+
*/
|
|
204
|
+
export function discordPlaceProvider(
|
|
205
|
+
options: DiscordPlaceProviderOptions,
|
|
206
|
+
): PlaceProvider {
|
|
207
|
+
const { guild, connection } = options;
|
|
208
|
+
return discordPlaces({
|
|
209
|
+
...(options.client ? { client: options.client } : {}),
|
|
210
|
+
guild,
|
|
211
|
+
channels: connection.agentChannels(guild),
|
|
212
|
+
onRemoved: (listener) => connection.onChannelDeleted(listener),
|
|
213
|
+
logger: options.logger,
|
|
214
|
+
});
|
|
215
|
+
}
|
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
ChannelContext,
|
|
14
14
|
ChannelContextOptions,
|
|
15
15
|
} from "../contract/channel-context.ts";
|
|
16
|
+
import type { PlaceProvider } from "../contract/places.ts";
|
|
16
17
|
import type {
|
|
17
18
|
ChatSurface,
|
|
18
19
|
SurfaceMessageEvents,
|
|
@@ -33,6 +34,7 @@ import type { MemberJoinedEvent } from "../plugin-api/event-types.ts";
|
|
|
33
34
|
import { chunksFor } from "../presentation/reply-chunks.ts";
|
|
34
35
|
import { threadsOfHost } from "../threads/host-threads.ts";
|
|
35
36
|
import { DiscordAgentChannels, DiscordDashboard } from "./agent-discord.ts";
|
|
37
|
+
import { discordPlaces } from "./agent-places.ts";
|
|
36
38
|
import {
|
|
37
39
|
type ContextSourceMessage,
|
|
38
40
|
contextSourceOf,
|
|
@@ -170,6 +172,17 @@ export class DiscordSurface
|
|
|
170
172
|
);
|
|
171
173
|
}
|
|
172
174
|
|
|
175
|
+
/** The agent team's places in the agent guild: its channels and threads, as a place provider. */
|
|
176
|
+
agentPlaces(guildId: string): PlaceProvider {
|
|
177
|
+
return discordPlaces({
|
|
178
|
+
client: this.#client,
|
|
179
|
+
guild: guildId,
|
|
180
|
+
channels: this.agentChannels(guildId),
|
|
181
|
+
onRemoved: (listener) => this.onChannelDeleted(listener),
|
|
182
|
+
logger: this.#options.logger,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
173
186
|
/** Threads for background dispatches, in the channel that started each. */
|
|
174
187
|
threadHost(): DiscordThreadHost {
|
|
175
188
|
return new DiscordThreadHost(this.#client);
|
|
@@ -153,6 +153,7 @@ function discordAssembly(
|
|
|
153
153
|
ownerId: owner.id,
|
|
154
154
|
ownerName: owner.name,
|
|
155
155
|
rootCommand: discord.rootCommand,
|
|
156
|
+
...(discord.guild ? { agentGuild: discord.guild } : {}),
|
|
156
157
|
dataDir: config.dataDir,
|
|
157
158
|
...(discord.refusalHint === undefined
|
|
158
159
|
? {}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { CharacterSpec } from "../../characters/index.ts";
|
|
2
|
+
import type { HttpRoute } from "../http/listeners.ts";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The directories the agents work in, as configured. Every agent shares them: the core has no
|
|
6
|
+
* workspace per agent. Read-only data for a plugin that shows or serves the files; they may not exist yet.
|
|
7
|
+
*/
|
|
8
|
+
export interface AgentWorkspace {
|
|
9
|
+
/** The shell's shared working directory (`config.workDir`), where agents write without a hold. */
|
|
10
|
+
readonly workDir: string;
|
|
11
|
+
/** The agents' scratch dir (TMPDIR), when the host sets one; writes there are unheld too. */
|
|
12
|
+
readonly scratchDir?: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Agent pictures: rendered from the agent's character, kept and served by content hash. */
|
|
16
|
+
export interface AvatarStudio {
|
|
17
|
+
/** @deprecated Agents are no longer drawn, so this only tells whether an image provider is configured (it gates the generate_image hint); removed in 1.0. */
|
|
18
|
+
readonly canDraw: boolean;
|
|
19
|
+
/** The public URL of a picture, or of the default when there is none. */
|
|
20
|
+
url(hash: string | undefined): string;
|
|
21
|
+
/**
|
|
22
|
+
* Renders a character as the agent's picture (512 px PNG, modern light disc, idle face), stores
|
|
23
|
+
* it by content hash, and returns the hash; the same spec gives the same hash.
|
|
24
|
+
*/
|
|
25
|
+
render(spec: CharacterSpec): Promise<string>;
|
|
26
|
+
/** @deprecated Agents are composed characters and no longer drawn; removed in 1.0. Draws a new picture from an avatar prompt; returns its hash. */
|
|
27
|
+
draw(avatarPrompt: string): Promise<string>;
|
|
28
|
+
/** @deprecated Removed in 1.0. Edits a stored picture by an instruction; returns the new picture's hash. */
|
|
29
|
+
edit(hash: string | undefined, instruction: string): Promise<string>;
|
|
30
|
+
/** @deprecated Removed in 1.0. A picture made from an agent's display name and name, for a host without an image provider; returns its hash. */
|
|
31
|
+
fallback(displayName: string, name: string): Promise<string>;
|
|
32
|
+
/** The pictures, public on the named listener. */
|
|
33
|
+
route(listener: string): HttpRoute;
|
|
34
|
+
/** Serves `/avatars/<hash>.png`; undefined for any other path. */
|
|
35
|
+
serve(pathname: string): Response | undefined;
|
|
36
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { Services } from "../contract/services.ts";
|
|
2
|
+
import { PluginError } from "../kernel/errors.ts";
|
|
3
|
+
import type { PluginContext, RoundtablePlugin } from "./plugin.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The context one plugin's setup gets: the host's parts and that plugin's view of the services.
|
|
7
|
+
* `context.core` of 0.1.0 is gone; reading it throws a PluginError naming `context.services`, where
|
|
8
|
+
* a plugin written for 0.1.0 meets it on its first line, instead of getting `undefined`.
|
|
9
|
+
*/
|
|
10
|
+
export function pluginContext(
|
|
11
|
+
plugin: RoundtablePlugin,
|
|
12
|
+
base: Omit<PluginContext, "services">,
|
|
13
|
+
services: Services,
|
|
14
|
+
): PluginContext {
|
|
15
|
+
// Every line a plugin logs carries its name.
|
|
16
|
+
const context = {
|
|
17
|
+
...base,
|
|
18
|
+
logger: base.logger.child({ plugin: plugin.name }),
|
|
19
|
+
services,
|
|
20
|
+
};
|
|
21
|
+
Object.defineProperty(context, "core", {
|
|
22
|
+
enumerable: false,
|
|
23
|
+
get() {
|
|
24
|
+
throw new PluginError(
|
|
25
|
+
`plugin ${plugin.name}: context.core was removed in 0.2.0; read a service with context.services.get(KEY), for example services.get(SCHEDULES), and provide one with services.provide(KEY, value) after listing KEY in the plugin's provides.`,
|
|
26
|
+
);
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
return context;
|
|
30
|
+
}
|
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
QueuePort,
|
|
12
12
|
} from "../contract/channels.ts";
|
|
13
13
|
import type { ChatCommand } from "../contract/chat-command.ts";
|
|
14
|
+
import type { PlacePort, PlaceProvider } from "../contract/places.ts";
|
|
14
15
|
import type { Providers, ResolvedProviders } from "../contract/providers.ts";
|
|
15
16
|
import type { ServiceKey, Services } from "../contract/services.ts";
|
|
16
17
|
import type { ChatSurface, SurfacePort } from "../contract/surface.ts";
|
|
@@ -26,7 +27,6 @@ import type {
|
|
|
26
27
|
import type { HttpRoute, ListenerAddress } from "../http/listeners.ts";
|
|
27
28
|
import type { Locale } from "../i18n/index.ts";
|
|
28
29
|
import type { Speaker, Tier } from "../identity/speakers.ts";
|
|
29
|
-
import { PluginError } from "../kernel/errors.ts";
|
|
30
30
|
import type { Logger } from "../kernel/log.ts";
|
|
31
31
|
import type {
|
|
32
32
|
DirectChannelProvider,
|
|
@@ -198,6 +198,12 @@ export interface Contribution {
|
|
|
198
198
|
* service named `surface:<prefix>` before the plugin's own services.
|
|
199
199
|
*/
|
|
200
200
|
surfaces?: readonly ChatSurface[];
|
|
201
|
+
/**
|
|
202
|
+
* What the plugin's surfaces hold of the agent team's places, one provider per surface across
|
|
203
|
+
* every plugin. A surface that hosts agent rooms (`agentRooms: true`) needs none: the host
|
|
204
|
+
* derives one from it. A provider for a surface no plugin registers is refused.
|
|
205
|
+
*/
|
|
206
|
+
places?: readonly PlaceProvider[];
|
|
201
207
|
/**
|
|
202
208
|
* The system prompts of conversation kinds the plugin owns, one per kind. A plugin that owns the
|
|
203
209
|
* owner's own conversations contributes the `"owner"` kind's prompt; without one those
|
|
@@ -255,6 +261,7 @@ export const CONTRIBUTION_KEYS = [
|
|
|
255
261
|
"sessionTools",
|
|
256
262
|
"channels",
|
|
257
263
|
"surfaces",
|
|
264
|
+
"places",
|
|
258
265
|
"personas",
|
|
259
266
|
"backgroundTargets",
|
|
260
267
|
"directChannels",
|
|
@@ -370,6 +377,8 @@ export interface PluginContext {
|
|
|
370
377
|
surfaces: SurfacePort;
|
|
371
378
|
/** Every plugin's direct channels, which reach a person on their own; calls during setup throw NotLinkedError. */
|
|
372
379
|
directChannels: DirectChannels;
|
|
380
|
+
/** The place providers of the agent team, chosen by surface or by a channel's prefix; calls during setup throw NotLinkedError. */
|
|
381
|
+
places: PlacePort;
|
|
373
382
|
/** Runs conversation turns of any kind over the runtime and the surfaces; calls during setup throw NotLinkedError. */
|
|
374
383
|
turns: ConversationTurns;
|
|
375
384
|
/** The host's one connection pool, migrated before any setup; throws PluginError without a database. */
|
|
@@ -465,33 +474,6 @@ export interface RoundtablePlugin {
|
|
|
465
474
|
preflight?(): Promise<void> | void;
|
|
466
475
|
}
|
|
467
476
|
|
|
468
|
-
/**
|
|
469
|
-
* The context one plugin's setup gets: the host's parts and that plugin's view of the services.
|
|
470
|
-
* `context.core` of 0.1.0 is gone; reading it throws a PluginError naming `context.services`, where
|
|
471
|
-
* a plugin written for 0.1.0 meets it on its first line, instead of getting `undefined`.
|
|
472
|
-
*/
|
|
473
|
-
export function pluginContext(
|
|
474
|
-
plugin: RoundtablePlugin,
|
|
475
|
-
base: Omit<PluginContext, "services">,
|
|
476
|
-
services: Services,
|
|
477
|
-
): PluginContext {
|
|
478
|
-
// Every line a plugin logs carries its name.
|
|
479
|
-
const context = {
|
|
480
|
-
...base,
|
|
481
|
-
logger: base.logger.child({ plugin: plugin.name }),
|
|
482
|
-
services,
|
|
483
|
-
};
|
|
484
|
-
Object.defineProperty(context, "core", {
|
|
485
|
-
enumerable: false,
|
|
486
|
-
get() {
|
|
487
|
-
throw new PluginError(
|
|
488
|
-
`plugin ${plugin.name}: context.core was removed in 0.2.0; read a service with context.services.get(KEY), for example services.get(SCHEDULES), and provide one with services.provide(KEY, value) after listing KEY in the plugin's provides.`,
|
|
489
|
-
);
|
|
490
|
-
},
|
|
491
|
-
});
|
|
492
|
-
return context;
|
|
493
|
-
}
|
|
494
|
-
|
|
495
477
|
export {
|
|
496
478
|
refuseRemovedEvents,
|
|
497
479
|
refuseRemovedFields,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ExtensionFactory } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { CharacterInput
|
|
2
|
+
import type { CharacterInput } from "../../characters/index.ts";
|
|
3
3
|
import type { Agent, AgentGroup } from "../agents/agent-rules.ts";
|
|
4
4
|
import type { AvatarMode } from "../agents/agent-tools.ts";
|
|
5
5
|
import type { HelperTeam } from "../agents/helper-ops.ts";
|
|
@@ -16,7 +16,6 @@ import type { ConversationRegistry } from "../conversations/conversation-registr
|
|
|
16
16
|
import type { PendingConfirmation } from "../domain/conversation.ts";
|
|
17
17
|
import type { ThinkingLevel, ThinkingSetting } from "../domain/models.ts";
|
|
18
18
|
import type { AgentTurnScope, ChannelKey } from "../domain/sessions.ts";
|
|
19
|
-
import type { HttpRoute } from "../http/listeners.ts";
|
|
20
19
|
import type { IdentityLinks } from "../identity/identity-links.ts";
|
|
21
20
|
import type { IdentityService } from "../identity/identity-service.ts";
|
|
22
21
|
import type { PersonProfiles } from "../identity/profiles.ts";
|
|
@@ -46,6 +45,7 @@ import type {
|
|
|
46
45
|
} from "../modules/schedules/schedule-store.ts";
|
|
47
46
|
import type { SkillRegistry } from "../modules/skills/skill-contract.ts";
|
|
48
47
|
import type { DispatchThreads } from "../threads/dispatch-threads.ts";
|
|
48
|
+
import type { AgentWorkspace, AvatarStudio } from "./agent-assets.ts";
|
|
49
49
|
|
|
50
50
|
// The services the built-in plugins provide, and the ports they keep to. Each port is an
|
|
51
51
|
// interface, so an object with the same methods satisfies it without being one of the built-in
|
|
@@ -186,50 +186,26 @@ export interface AgentServer {
|
|
|
186
186
|
routes?: RoutingTable; // where work no person started lands; the built-in server sets it
|
|
187
187
|
}
|
|
188
188
|
|
|
189
|
-
/**
|
|
190
|
-
* The directories the agents work in, as configured. Every agent shares them: the core has no
|
|
191
|
-
* workspace per agent. Read-only data for a plugin that shows or serves the files; they may not exist yet.
|
|
192
|
-
*/
|
|
193
|
-
export interface AgentWorkspace {
|
|
194
|
-
/** The shell's shared working directory (`config.workDir`), where agents write without a hold. */
|
|
195
|
-
readonly workDir: string;
|
|
196
|
-
/** The agents' scratch dir (TMPDIR), when the host sets one; writes there are unheld too. */
|
|
197
|
-
readonly scratchDir?: string;
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
/** Agent pictures: rendered from the agent's character, kept and served by content hash. */
|
|
201
|
-
export interface AvatarStudio {
|
|
202
|
-
/** @deprecated Agents are no longer drawn, so this only tells whether an image provider is configured (it gates the generate_image hint); removed in 1.0. */
|
|
203
|
-
readonly canDraw: boolean;
|
|
204
|
-
/** The public URL of a picture, or of the default when there is none. */
|
|
205
|
-
url(hash: string | undefined): string;
|
|
206
|
-
/**
|
|
207
|
-
* Renders a character as the agent's picture (512 px PNG, modern light disc, idle face), stores
|
|
208
|
-
* it by content hash, and returns the hash; the same spec gives the same hash.
|
|
209
|
-
*/
|
|
210
|
-
render(spec: CharacterSpec): Promise<string>;
|
|
211
|
-
/** @deprecated Agents are composed characters and no longer drawn; removed in 1.0. Draws a new picture from an avatar prompt; returns its hash. */
|
|
212
|
-
draw(avatarPrompt: string): Promise<string>;
|
|
213
|
-
/** @deprecated Removed in 1.0. Edits a stored picture by an instruction; returns the new picture's hash. */
|
|
214
|
-
edit(hash: string | undefined, instruction: string): Promise<string>;
|
|
215
|
-
/** @deprecated Removed in 1.0. A picture made from an agent's display name and name, for a host without an image provider; returns its hash. */
|
|
216
|
-
fallback(displayName: string, name: string): Promise<string>;
|
|
217
|
-
/** The pictures, public on the named listener. */
|
|
218
|
-
route(listener: string): HttpRoute;
|
|
219
|
-
/** Serves `/avatars/<hash>.png`; undefined for any other path. */
|
|
220
|
-
serve(pathname: string): Response | undefined;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
189
|
/** The read half of the agent store: every inbound message asks, and only this process changes them. */
|
|
224
190
|
export interface AgentDirectory {
|
|
225
191
|
agents(): Agent[];
|
|
226
192
|
agent(name: string): Agent | undefined;
|
|
227
193
|
/** The agent, or throws AgentError when it is unknown or archived. */
|
|
228
194
|
activeAgent(name: string): Agent;
|
|
195
|
+
/** @deprecated Finds an agent by its Discord channel only; read `placeOwner`, which takes a place of any surface. Removed in 1.0. */
|
|
229
196
|
agentByChannel(channelId: string): Agent | undefined;
|
|
230
197
|
groups(): AgentGroup[];
|
|
231
198
|
group(name: string): AgentGroup | undefined;
|
|
199
|
+
/** @deprecated Finds a group by its Discord channel only; read `placeOwner`, which takes a place of any surface. Removed in 1.0. */
|
|
232
200
|
groupByChannel(channelId: string): AgentGroup | undefined;
|
|
201
|
+
/**
|
|
202
|
+
* The agent or group whose place the conversation key is, on any surface that holds places and
|
|
203
|
+
* whatever the owner's status; undefined for a key that is nobody's place. Optional, because a
|
|
204
|
+
* directory written before places were providers has none.
|
|
205
|
+
*/
|
|
206
|
+
placeOwner?(
|
|
207
|
+
key: ChannelKey,
|
|
208
|
+
): { kind: "agent" | "group"; name: string } | undefined;
|
|
233
209
|
}
|
|
234
210
|
|
|
235
211
|
/** What an owner gives to create an agent: the fields of `agent_create`, with the settings `update` takes. */
|
|
@@ -264,9 +240,11 @@ export interface AgentChange {
|
|
|
264
240
|
|
|
265
241
|
/** The agent team as plugins use it: who is in it, what model they run, and their channels. */
|
|
266
242
|
export interface AgentTeam extends HelperTeam {
|
|
267
|
-
/** The team's id: what its rows are stored under, the Discord guild's id unless the host sets one, else `"local"`. */
|
|
243
|
+
/** The team's id: what its rows are stored under, the Discord guild's id unless the host sets one, else `"local"`. Optional, because a team written before places were providers has only `guildId`. */
|
|
244
|
+
readonly teamId?: string;
|
|
245
|
+
/** @deprecated The team's id, which is not always a guild; read `teamId`. Removed in 1.0. */
|
|
268
246
|
readonly guildId: string;
|
|
269
|
-
/** The Discord guild the
|
|
247
|
+
/** @deprecated The Discord guild is the space of Discord's place provider, which the team no longer needs to know. Removed in 1.0. */
|
|
270
248
|
readonly discordGuild?: string | undefined;
|
|
271
249
|
/** Called when a turn starts or ends and when an agent or group changes. */
|
|
272
250
|
onChange(listener: () => void): void;
|
|
@@ -282,8 +260,10 @@ export interface AgentTeam extends HelperTeam {
|
|
|
282
260
|
* Discord, the one `coordinator` names (the first seed by default); undefined while none is active.
|
|
283
261
|
*/
|
|
284
262
|
coordinator(): string | undefined;
|
|
285
|
-
/** Every key an agent or group is addressed by: its Discord channel, then its room on each `agentRooms` surface; empty for an unknown name. */
|
|
263
|
+
/** Every key an agent or group is addressed by: its place on each surface that holds one, in the order "auto" routing prefers (its Discord channel first), then its room on each `agentRooms` surface; empty for an unknown name. */
|
|
286
264
|
roomsOf(name: string): ChannelKey[];
|
|
265
|
+
/** Where an agent or group is: its place on each surface that holds one, with that surface's name; empty for an unknown name. Optional, because a team written before places were providers has only `roomsOf`. */
|
|
266
|
+
placesOf?(name: string): { surface: string; key: ChannelKey }[];
|
|
287
267
|
/** The model and thinking setting of an agent's next run: its own, or the assistant's. */
|
|
288
268
|
modelOf(name: string): { model: string; thinking: ThinkingSetting };
|
|
289
269
|
/** The assistant's model and thinking setting, which agents without their own follow. */
|
|
@@ -497,3 +477,4 @@ export interface SpeakerMemory {
|
|
|
497
477
|
}
|
|
498
478
|
|
|
499
479
|
export type { SkillRegistry } from "../modules/skills/skill-contract.ts";
|
|
480
|
+
export type { AgentWorkspace, AvatarStudio } from "./agent-assets.ts";
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import type { BackgroundTarget } from "../contract/channels.ts";
|
|
2
|
+
import { PluginError } from "../kernel/errors.ts";
|
|
3
|
+
import type { Persona } from "../plugin-api/plugin.ts";
|
|
4
|
+
import type { DirectChannelProvider } from "../presence/direct-channels.ts";
|
|
5
|
+
|
|
6
|
+
/** Which plugin registered each name of one kind, so a clash names both. */
|
|
7
|
+
export class Names {
|
|
8
|
+
readonly #owners = new Map<string, string>();
|
|
9
|
+
|
|
10
|
+
constructor(private readonly kind: string) {}
|
|
11
|
+
|
|
12
|
+
claim(plugin: string, name: string): void {
|
|
13
|
+
const other = this.#owners.get(name);
|
|
14
|
+
if (other !== undefined)
|
|
15
|
+
throw new PluginError(
|
|
16
|
+
`plugin ${plugin}: ${this.kind} ${name} is already registered by plugin ${other}. Rename one of the two.`,
|
|
17
|
+
);
|
|
18
|
+
this.#owners.set(name, plugin);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The kind of a conversation the agent server runs itself, which no plugin's persona may take. */
|
|
23
|
+
const RESERVED_KIND = "agent";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Refuses a persona without a kind, for the reserved agent kind, or for a kind another plugin's
|
|
27
|
+
* persona already has, so a conversation's prompt is never a matter of plugin order.
|
|
28
|
+
*/
|
|
29
|
+
export function claimPersona(
|
|
30
|
+
kinds: Map<string, string>,
|
|
31
|
+
plugin: string,
|
|
32
|
+
persona: Persona,
|
|
33
|
+
): void {
|
|
34
|
+
const { kind } = persona;
|
|
35
|
+
if (typeof kind !== "string" || kind === "")
|
|
36
|
+
throw new PluginError(
|
|
37
|
+
`plugin ${plugin}: a persona needs a kind, a non-empty string such as "study", and got ${JSON.stringify(kind)}.`,
|
|
38
|
+
);
|
|
39
|
+
if (kind === RESERVED_KIND)
|
|
40
|
+
throw new PluginError(
|
|
41
|
+
`plugin ${plugin}: the persona kind "${RESERVED_KIND}" is reserved for the agent server's agents. Give the persona the kind of your own conversations.`,
|
|
42
|
+
);
|
|
43
|
+
if (typeof persona.prompt !== "function")
|
|
44
|
+
throw new PluginError(
|
|
45
|
+
`plugin ${plugin}: the persona of kind "${kind}" needs a prompt() that returns its text.`,
|
|
46
|
+
);
|
|
47
|
+
if (
|
|
48
|
+
persona.memory !== undefined &&
|
|
49
|
+
persona.memory !== "speaker" &&
|
|
50
|
+
persona.memory !== "none"
|
|
51
|
+
)
|
|
52
|
+
throw new PluginError(
|
|
53
|
+
`plugin ${plugin}: the persona of kind "${kind}" has memory ${JSON.stringify(persona.memory)}; expected "speaker" or "none".`,
|
|
54
|
+
);
|
|
55
|
+
const other = kinds.get(kind);
|
|
56
|
+
if (other !== undefined)
|
|
57
|
+
throw new PluginError(
|
|
58
|
+
`plugin ${plugin}: persona kind "${kind}" is already registered by plugin ${other}. Keep one persona per kind.`,
|
|
59
|
+
);
|
|
60
|
+
kinds.set(kind, plugin);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Refuses a direct channel without a name, a label, or `reaches`, or whose name another plugin's
|
|
65
|
+
* already has, so the providers a person is reached through never depend on a typo.
|
|
66
|
+
*/
|
|
67
|
+
export function claimDirectChannel(
|
|
68
|
+
names: Names,
|
|
69
|
+
plugin: string,
|
|
70
|
+
provider: DirectChannelProvider,
|
|
71
|
+
): void {
|
|
72
|
+
const { name } = provider;
|
|
73
|
+
if (typeof name !== "string" || name === "")
|
|
74
|
+
throw new PluginError(
|
|
75
|
+
`plugin ${plugin}: a direct channel needs a name, a non-empty string such as "discord", and got ${JSON.stringify(name)}.`,
|
|
76
|
+
);
|
|
77
|
+
if (typeof provider.label !== "string" || provider.label === "")
|
|
78
|
+
throw new PluginError(
|
|
79
|
+
`plugin ${plugin}: the direct channel "${name}" needs a label, such as "a direct message on Discord", which the notify tool names it by.`,
|
|
80
|
+
);
|
|
81
|
+
if (typeof provider.reaches !== "function")
|
|
82
|
+
throw new PluginError(
|
|
83
|
+
`plugin ${plugin}: the direct channel "${name}" needs reaches(principalId), which finds a person's own conversation there.`,
|
|
84
|
+
);
|
|
85
|
+
names.claim(plugin, name);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Refuses a background target without a name or a label, or whose name another plugin's target
|
|
90
|
+
* already has, so a stored schedule's target never depends on plugin order.
|
|
91
|
+
*/
|
|
92
|
+
export function claimTarget(
|
|
93
|
+
names: Map<string, string>,
|
|
94
|
+
plugin: string,
|
|
95
|
+
target: BackgroundTarget,
|
|
96
|
+
): void {
|
|
97
|
+
const { name } = target;
|
|
98
|
+
if (typeof name !== "string" || name === "")
|
|
99
|
+
throw new PluginError(
|
|
100
|
+
`plugin ${plugin}: a background target needs a name, a non-empty string such as "support", and got ${JSON.stringify(name)}.`,
|
|
101
|
+
);
|
|
102
|
+
if (typeof target.label !== "function")
|
|
103
|
+
throw new PluginError(
|
|
104
|
+
`plugin ${plugin}: the background target "${name}" needs a label(locale) that returns its name for lists.`,
|
|
105
|
+
);
|
|
106
|
+
const other = names.get(name);
|
|
107
|
+
if (other !== undefined)
|
|
108
|
+
throw new PluginError(
|
|
109
|
+
`plugin ${plugin}: background target "${name}" is already registered by plugin ${other}. Keep one target per name.`,
|
|
110
|
+
);
|
|
111
|
+
names.set(name, plugin);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The surface's own words for what a prefix must be, checked before its service is named. */
|
|
115
|
+
export function checkSurfacePrefix(plugin: string, prefix: unknown): void {
|
|
116
|
+
if (typeof prefix !== "string" || prefix === "" || /[:\s]/.test(prefix))
|
|
117
|
+
throw new PluginError(
|
|
118
|
+
`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)}.`,
|
|
119
|
+
);
|
|
120
|
+
}
|