pi-roundtable 0.42.0 → 0.44.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.
- package/CHANGELOG.md +35 -0
- package/README.md +1 -0
- package/README.zh-TW.md +1 -0
- package/docs/plugins.md +3 -1
- package/package.json +3 -1
- package/src/characters/catalog.ts +7 -750
- package/src/characters/class-data.ts +56 -0
- package/src/characters/classes-a.ts +235 -0
- package/src/characters/classes-b.ts +242 -0
- package/src/characters/classes-c.ts +241 -0
- package/src/core/agents/agent-claim.ts +6 -2
- package/src/core/agents/agent-group-store.ts +176 -0
- package/src/core/agents/agent-places.ts +197 -0
- package/src/core/agents/agent-schema.ts +37 -3
- package/src/core/agents/agent-statements.ts +14 -12
- package/src/core/agents/agent-store.ts +81 -211
- package/src/core/agents/agent-team-fixture.ts +9 -3
- package/src/core/agents/agent-team.ts +6 -8
- package/src/core/agents/routes.ts +24 -7
- package/src/core/agents/team-options.ts +2 -0
- package/src/core/builtin/agent-server-options.ts +101 -0
- package/src/core/builtin/agent-server.ts +35 -94
- package/src/core/config/config-schema.ts +3 -1
- package/src/core/config/config.ts +5 -0
- package/src/core/config/team.ts +30 -0
- package/src/core/contract/channels.ts +8 -0
- package/src/core/db/legacy-channels.ts +92 -0
- package/src/core/db/team-move.ts +138 -0
- package/src/core/host/define-roundtable.ts +10 -2
- package/src/core/host/drain.ts +46 -0
- package/src/core/host/host.ts +25 -8
- package/src/core/plugin-api/plugin.ts +6 -3
- package/src/core/plugin-api/services.ts +4 -5
- package/src/core/presentation/diagram-text.ts +17 -8
- package/src/core/routing/channel-queue.ts +36 -3
- package/src/core/testing/fake-discord.ts +67 -0
- package/src/core/testing/partial.ts +8 -0
- package/src/core/testing/plugin-stand-ins.ts +95 -0
- package/src/core/testing/service-pair.ts +18 -0
- package/src/core/testing/test-plugin-types.ts +110 -0
- package/src/testing.ts +36 -276
|
@@ -109,13 +109,14 @@ export class AgentStatements {
|
|
|
109
109
|
RETURNING *`;
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
/** Deletes the agent's row, its group cursors and its prompt history
|
|
112
|
+
/** Deletes the agent's row, its group cursors and its prompt history; run it in a transaction. */
|
|
113
113
|
async deleteAgent(name: string): Promise<void> {
|
|
114
|
-
await this
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
114
|
+
await this
|
|
115
|
+
.#sql`DELETE FROM agent_group_cursors WHERE guild_id = ${this.#guild} AND agent_name = ${name}`;
|
|
116
|
+
await this
|
|
117
|
+
.#sql`DELETE FROM prompt_history WHERE guild_id = ${this.#guild} AND kind = 'agent' AND name = ${name}`;
|
|
118
|
+
await this
|
|
119
|
+
.#sql`DELETE FROM agents WHERE guild_id = ${this.#guild} AND name = ${name}`;
|
|
119
120
|
}
|
|
120
121
|
|
|
121
122
|
/** Gives an agent a character and picture only while it has no character. */
|
|
@@ -183,13 +184,14 @@ export class AgentStatements {
|
|
|
183
184
|
RETURNING *`;
|
|
184
185
|
}
|
|
185
186
|
|
|
186
|
-
/** Deletes the group's row, messages and cursors, in the Discord channel's log and each room's. */
|
|
187
|
+
/** Deletes the group's row, messages and cursors, in the Discord channel's log and each room's; run it in a transaction. */
|
|
187
188
|
async deleteGroup(name: string): Promise<void> {
|
|
188
189
|
const rooms = `${name}@%`;
|
|
189
|
-
await this
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
190
|
+
await this
|
|
191
|
+
.#sql`DELETE FROM agent_group_cursors WHERE guild_id = ${this.#guild} AND (group_name = ${name} OR group_name LIKE ${rooms})`;
|
|
192
|
+
await this
|
|
193
|
+
.#sql`DELETE FROM agent_group_messages WHERE guild_id = ${this.#guild} AND (group_name = ${name} OR group_name LIKE ${rooms})`;
|
|
194
|
+
await this
|
|
195
|
+
.#sql`DELETE FROM agent_groups WHERE guild_id = ${this.#guild} AND name = ${name}`;
|
|
194
196
|
}
|
|
195
197
|
}
|
|
@@ -9,32 +9,30 @@ import {
|
|
|
9
9
|
type PromptVersion,
|
|
10
10
|
} from "../modules/prompts/prompt-history.ts";
|
|
11
11
|
import type { AgentDirectory } from "../plugin-api/services.ts";
|
|
12
|
-
import { AgentBook } from "./agent-book.ts";
|
|
13
12
|
import { AgentConversations } from "./agent-conversations.ts";
|
|
13
|
+
import { AgentGroupStore } from "./agent-group-store.ts";
|
|
14
14
|
import { AgentOutbox } from "./agent-outbox.ts";
|
|
15
|
+
import { AgentPlaces } from "./agent-places.ts";
|
|
15
16
|
import {
|
|
16
17
|
type Agent,
|
|
17
18
|
type AgentGroup,
|
|
18
19
|
type AgentSeed,
|
|
19
|
-
type Backlog,
|
|
20
20
|
checkAgentName,
|
|
21
21
|
checkAvatarPrompt,
|
|
22
22
|
checkDisplayName,
|
|
23
23
|
checkModelRef,
|
|
24
24
|
checkPrompt,
|
|
25
25
|
checkThinking,
|
|
26
|
-
type GroupMessage,
|
|
27
26
|
MIN_GROUP_MEMBERS,
|
|
28
27
|
} from "./agent-rules.ts";
|
|
29
28
|
import {
|
|
30
29
|
type AgentRow,
|
|
31
30
|
agentMigrations,
|
|
31
|
+
agentPlacesConvergence,
|
|
32
32
|
agentsMigration,
|
|
33
33
|
type GroupRow,
|
|
34
34
|
toGroup,
|
|
35
35
|
} from "./agent-schema.ts";
|
|
36
|
-
import { AgentStatements } from "./agent-statements.ts";
|
|
37
|
-
import { GroupMessages } from "./group-messages.ts";
|
|
38
36
|
import { HelperPresets } from "./helper-presets.ts";
|
|
39
37
|
|
|
40
38
|
export {
|
|
@@ -60,14 +58,7 @@ export {
|
|
|
60
58
|
* Agents, groups, and group messages, in the database. Agents and groups are also held
|
|
61
59
|
* in memory, because every inbound message asks and only this process changes them.
|
|
62
60
|
*/
|
|
63
|
-
export class PgAgentStore extends
|
|
64
|
-
readonly #sql: SQL;
|
|
65
|
-
/** The tail of the agent and group writes, which run one at a time so none reads a map another is about to change. */
|
|
66
|
-
#queue: Promise<unknown> = Promise.resolve();
|
|
67
|
-
|
|
68
|
-
readonly #guild: string;
|
|
69
|
-
readonly #statements: AgentStatements;
|
|
70
|
-
readonly #messages: GroupMessages;
|
|
61
|
+
export class PgAgentStore extends AgentGroupStore implements AgentDirectory {
|
|
71
62
|
readonly #prompts: PromptHistory;
|
|
72
63
|
/** Agent messages the host stopped before delivering them. */
|
|
73
64
|
readonly outbox: AgentOutbox;
|
|
@@ -78,38 +69,43 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
78
69
|
|
|
79
70
|
private constructor(
|
|
80
71
|
sql: SQL,
|
|
81
|
-
|
|
72
|
+
teamId: string,
|
|
82
73
|
conversations: AgentConversations,
|
|
74
|
+
places: AgentPlaces,
|
|
83
75
|
) {
|
|
84
|
-
super();
|
|
76
|
+
super(sql, teamId, places);
|
|
85
77
|
this.conversations = conversations;
|
|
86
|
-
this.#
|
|
87
|
-
this
|
|
88
|
-
this
|
|
89
|
-
this.#messages = new GroupMessages(sql, guildId);
|
|
90
|
-
this.#prompts = new PromptHistory(sql, guildId);
|
|
91
|
-
this.outbox = new AgentOutbox(sql, guildId);
|
|
92
|
-
this.presets = new HelperPresets(sql, guildId);
|
|
78
|
+
this.#prompts = new PromptHistory(sql, teamId);
|
|
79
|
+
this.outbox = new AgentOutbox(sql, teamId);
|
|
80
|
+
this.presets = new HelperPresets(sql, teamId);
|
|
93
81
|
}
|
|
94
82
|
|
|
95
83
|
/** The store's tables; the host runs this before any store attaches. */
|
|
96
84
|
static readonly migration: Migration = agentsMigration;
|
|
97
85
|
|
|
98
|
-
/** The migrations of the store's tables for the
|
|
99
|
-
static migrations(
|
|
100
|
-
return agentMigrations(
|
|
86
|
+
/** The migrations of the store's tables for the team the process serves. */
|
|
87
|
+
static migrations(teamId: string): Migration[] {
|
|
88
|
+
return agentMigrations(teamId);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** The every-boot migration that makes the place table follow the channel columns; it runs after the team move. */
|
|
92
|
+
static placesConvergence(teamId: string): Migration {
|
|
93
|
+
return agentPlacesConvergence(teamId);
|
|
101
94
|
}
|
|
102
95
|
|
|
103
|
-
/** The store over the host's migrated pool, holding the rows of one
|
|
104
|
-
static async attach(sql: SQL,
|
|
96
|
+
/** The store over the host's migrated pool, holding the rows of one team. */
|
|
97
|
+
static async attach(sql: SQL, teamId: string): Promise<PgAgentStore> {
|
|
105
98
|
// `Roundtable.check()` has no database to read the rows from: the store starts empty.
|
|
106
99
|
const checking = isCheckDatabase(sql);
|
|
107
100
|
const store = new PgAgentStore(
|
|
108
101
|
sql,
|
|
109
|
-
|
|
102
|
+
teamId,
|
|
110
103
|
checking
|
|
111
104
|
? AgentConversations.memory()
|
|
112
|
-
: await AgentConversations.load(sql,
|
|
105
|
+
: await AgentConversations.load(sql, teamId),
|
|
106
|
+
checking
|
|
107
|
+
? AgentPlaces.empty(sql, teamId)
|
|
108
|
+
: await AgentPlaces.load(sql, teamId),
|
|
113
109
|
);
|
|
114
110
|
if (!checking) await store.#reload();
|
|
115
111
|
return store;
|
|
@@ -117,9 +113,9 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
117
113
|
|
|
118
114
|
async #reload(): Promise<void> {
|
|
119
115
|
const agents: AgentRow[] = await this
|
|
120
|
-
|
|
116
|
+
.sql`SELECT * FROM agents WHERE guild_id = ${this.teamId}`;
|
|
121
117
|
const groups: GroupRow[] = await this
|
|
122
|
-
|
|
118
|
+
.sql`SELECT * FROM agent_groups WHERE guild_id = ${this.teamId}`;
|
|
123
119
|
this.agentMap.clear();
|
|
124
120
|
this.groupMap.clear();
|
|
125
121
|
this.dressed.clear();
|
|
@@ -129,14 +125,16 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
129
125
|
|
|
130
126
|
/** Inserts seed agents that do not exist yet; existing rows, edited or not, are left alone. */
|
|
131
127
|
async seed(seeds: readonly AgentSeed[]): Promise<string[]> {
|
|
132
|
-
return this
|
|
128
|
+
return this.serial(async () => {
|
|
133
129
|
const added: string[] = [];
|
|
134
130
|
for (const seed of seeds) {
|
|
135
|
-
const rows = await this
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
131
|
+
const rows = await this.places.legacy("agent", (tx) =>
|
|
132
|
+
this.statementsOn(tx).insertSeed({
|
|
133
|
+
...seed,
|
|
134
|
+
avatarPrompt: seed.avatarPrompt ?? "",
|
|
135
|
+
channelId: seed.channelId ?? null,
|
|
136
|
+
}),
|
|
137
|
+
);
|
|
140
138
|
// Only inserted rows join the map: a whole reload drops a write committed after its SELECT.
|
|
141
139
|
if (rows[0]) added.push(this.keepAgent(rows[0]).name);
|
|
142
140
|
}
|
|
@@ -144,12 +142,6 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
144
142
|
});
|
|
145
143
|
}
|
|
146
144
|
|
|
147
|
-
#serial<T>(write: () => Promise<T>): Promise<T> {
|
|
148
|
-
const run = this.#queue.then(write);
|
|
149
|
-
this.#queue = run.catch(() => undefined);
|
|
150
|
-
return run;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
145
|
async createAgent(agent: {
|
|
154
146
|
name: string;
|
|
155
147
|
displayName: string;
|
|
@@ -165,7 +157,7 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
165
157
|
origin?: string; // the conversation a temp knight was made from
|
|
166
158
|
preset?: string; // the preset it was made from
|
|
167
159
|
}): Promise<Agent> {
|
|
168
|
-
return this
|
|
160
|
+
return this.serial(async () => {
|
|
169
161
|
checkAgentName(agent.name);
|
|
170
162
|
if (agent.parent !== undefined) this.checkParent(agent.parent);
|
|
171
163
|
checkDisplayName(agent.displayName);
|
|
@@ -173,18 +165,20 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
173
165
|
const avatarPrompt = agent.avatarPrompt ?? "";
|
|
174
166
|
if (avatarPrompt !== "") checkAvatarPrompt(avatarPrompt);
|
|
175
167
|
this.checkFree(agent.name);
|
|
176
|
-
const rows = await this
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
168
|
+
const rows = await this.places.legacy("agent", (tx) =>
|
|
169
|
+
this.statementsOn(tx).insertAgent({
|
|
170
|
+
name: agent.name,
|
|
171
|
+
displayName: agent.displayName.trim(),
|
|
172
|
+
prompt: agent.prompt,
|
|
173
|
+
avatarPrompt,
|
|
174
|
+
channelId: agent.channelId ?? null,
|
|
175
|
+
avatarHash: agent.avatarHash ?? null,
|
|
176
|
+
character: agent.character ?? null,
|
|
177
|
+
parent: agent.parent ?? null,
|
|
178
|
+
origin: agent.origin ?? null,
|
|
179
|
+
preset: agent.preset ?? null,
|
|
180
|
+
}),
|
|
181
|
+
);
|
|
188
182
|
return this.keepAgent(rows[0]);
|
|
189
183
|
});
|
|
190
184
|
}
|
|
@@ -193,7 +187,7 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
193
187
|
setFinished(name: string, finished: boolean): Promise<boolean> {
|
|
194
188
|
return this.#writeOne(() =>
|
|
195
189
|
this.stampChanges(name, finished)
|
|
196
|
-
? this
|
|
190
|
+
? this.statements.setFinished(name, finished ? new Date() : null)
|
|
197
191
|
: Promise.resolve([]),
|
|
198
192
|
);
|
|
199
193
|
}
|
|
@@ -214,7 +208,7 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
214
208
|
/** Who made the change; a changed prompt is kept in the agent's prompt history under this name. */
|
|
215
209
|
by = "unknown",
|
|
216
210
|
): Promise<Agent> {
|
|
217
|
-
return this
|
|
211
|
+
return this.serial(async () => {
|
|
218
212
|
const current = this.activeAgent(name);
|
|
219
213
|
if (change.displayName !== undefined)
|
|
220
214
|
checkDisplayName(change.displayName);
|
|
@@ -242,22 +236,17 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
242
236
|
};
|
|
243
237
|
const prompt = change.prompt;
|
|
244
238
|
// A new prompt and its history row commit together, so no change goes unrecorded.
|
|
245
|
-
const rows =
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
tx,
|
|
257
|
-
);
|
|
258
|
-
return written;
|
|
259
|
-
})
|
|
260
|
-
: await this.#statements.updateAgent(name, row);
|
|
239
|
+
const rows = await this.places.legacy("agent", async (tx) => {
|
|
240
|
+
const written = await this.statementsOn(tx).updateAgent(name, row);
|
|
241
|
+
if (prompt !== undefined && prompt !== current.prompt)
|
|
242
|
+
await this.#prompts.record(
|
|
243
|
+
"agent",
|
|
244
|
+
name,
|
|
245
|
+
{ action: "set", text: prompt, previous: current.prompt, by },
|
|
246
|
+
tx,
|
|
247
|
+
);
|
|
248
|
+
return written;
|
|
249
|
+
});
|
|
261
250
|
return this.keepAgent(rows[0]);
|
|
262
251
|
});
|
|
263
252
|
}
|
|
@@ -273,19 +262,19 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
273
262
|
* once they are all active again.
|
|
274
263
|
*/
|
|
275
264
|
async archiveAgent(name: string): Promise<AgentGroup[]> {
|
|
276
|
-
return this
|
|
277
|
-
this.keepAgent((await this
|
|
265
|
+
return this.serial(async () => {
|
|
266
|
+
this.keepAgent((await this.statements.archiveAgent(name))[0]);
|
|
278
267
|
const left: AgentGroup[] = [];
|
|
279
268
|
for (const group of this.groups()) {
|
|
280
269
|
if (group.status !== "active" || !group.members.includes(name))
|
|
281
270
|
continue;
|
|
282
271
|
const members = group.members.filter((m) => m !== name);
|
|
283
272
|
if (members.length < MIN_GROUP_MEMBERS) {
|
|
284
|
-
left.push(await this
|
|
273
|
+
left.push(await this.writeGroup(group.name, { status: "archived" }));
|
|
285
274
|
continue;
|
|
286
275
|
}
|
|
287
276
|
const host = group.host === name ? (members[0] ?? name) : group.host;
|
|
288
|
-
left.push(await this
|
|
277
|
+
left.push(await this.writeGroup(group.name, { members, host }));
|
|
289
278
|
}
|
|
290
279
|
return left;
|
|
291
280
|
});
|
|
@@ -296,14 +285,16 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
296
285
|
* does not rejoin the groups it left.
|
|
297
286
|
*/
|
|
298
287
|
async restoreAgent(name: string, channelId?: string): Promise<Agent> {
|
|
299
|
-
return this
|
|
288
|
+
return this.serial(async () => {
|
|
300
289
|
const current = this.agentMap.get(name);
|
|
301
290
|
if (!current) throw new AgentError(`There is no agent "${name}".`);
|
|
302
291
|
if (current.status === "active")
|
|
303
292
|
throw new AgentError(`"${name}" is not archived.`);
|
|
304
|
-
const rows = await this
|
|
305
|
-
|
|
306
|
-
|
|
293
|
+
const rows = await this.places.legacy("agent", (tx) =>
|
|
294
|
+
this.statementsOn(tx).restoreAgent(
|
|
295
|
+
name,
|
|
296
|
+
channelId ?? current.channelId ?? null,
|
|
297
|
+
),
|
|
307
298
|
);
|
|
308
299
|
return this.keepAgent(rows[0]);
|
|
309
300
|
});
|
|
@@ -315,21 +306,23 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
315
306
|
* starts with an empty history. An active agent is never deleted: archive it first.
|
|
316
307
|
*/
|
|
317
308
|
async deleteAgent(name: string): Promise<void> {
|
|
318
|
-
return this
|
|
309
|
+
return this.serial(async () => {
|
|
319
310
|
const current = this.agentMap.get(name);
|
|
320
311
|
if (!current) throw new AgentError(`There is no agent "${name}".`);
|
|
321
312
|
if (current.status === "active")
|
|
322
313
|
throw new AgentError(
|
|
323
314
|
`"${name}" is active; archive it before deleting it.`,
|
|
324
315
|
);
|
|
325
|
-
await this
|
|
316
|
+
await this.places.dropping("agent", name, (tx) =>
|
|
317
|
+
this.statementsOn(tx).deleteAgent(name),
|
|
318
|
+
);
|
|
326
319
|
this.agentMap.delete(name);
|
|
327
320
|
this.dressed.delete(name);
|
|
328
321
|
// An archived group still lists the agent; a new agent of this name must not join it unasked.
|
|
329
322
|
for (const group of this.groups()) {
|
|
330
323
|
if (!group.members.includes(name)) continue;
|
|
331
324
|
const members = group.members.filter((m) => m !== name);
|
|
332
|
-
await this
|
|
325
|
+
await this.writeGroup(group.name, {
|
|
333
326
|
members,
|
|
334
327
|
host: group.host === name ? (members[0] ?? "") : group.host,
|
|
335
328
|
});
|
|
@@ -347,7 +340,7 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
347
340
|
avatarHash: string,
|
|
348
341
|
): Promise<boolean> {
|
|
349
342
|
return this.#writeOne(() =>
|
|
350
|
-
this
|
|
343
|
+
this.statements.dressIfUndressed(name, character, avatarHash),
|
|
351
344
|
);
|
|
352
345
|
}
|
|
353
346
|
|
|
@@ -362,139 +355,16 @@ export class PgAgentStore extends AgentBook implements AgentDirectory {
|
|
|
362
355
|
avatarHash: string,
|
|
363
356
|
): Promise<boolean> {
|
|
364
357
|
return this.#writeOne(() =>
|
|
365
|
-
this
|
|
358
|
+
this.statements.setAvatarIfLook(name, character, avatarHash),
|
|
366
359
|
);
|
|
367
360
|
}
|
|
368
361
|
|
|
369
362
|
/** Runs a conditional row write in the queue; true when it wrote a row, which joins the map. */
|
|
370
363
|
#writeOne(write: () => Promise<AgentRow[]>): Promise<boolean> {
|
|
371
|
-
return this
|
|
364
|
+
return this.serial(async () => {
|
|
372
365
|
const [row] = await write();
|
|
373
366
|
if (row) this.keepAgentRow(row);
|
|
374
367
|
return row !== undefined;
|
|
375
368
|
});
|
|
376
369
|
}
|
|
377
|
-
|
|
378
|
-
async createGroup(group: {
|
|
379
|
-
name: string;
|
|
380
|
-
displayName: string;
|
|
381
|
-
/** Absent in a team without Discord, whose groups live in rooms. */
|
|
382
|
-
channelId?: string;
|
|
383
|
-
members: string[];
|
|
384
|
-
host: string;
|
|
385
|
-
}): Promise<AgentGroup> {
|
|
386
|
-
return this.#serial(async () => {
|
|
387
|
-
this.checkNewGroup(group.name, group.members, group.host);
|
|
388
|
-
checkDisplayName(group.displayName);
|
|
389
|
-
const rows = await this.#statements.insertGroup({
|
|
390
|
-
...group,
|
|
391
|
-
displayName: group.displayName.trim(),
|
|
392
|
-
channelId: group.channelId ?? null,
|
|
393
|
-
});
|
|
394
|
-
return this.keepGroup(rows[0]);
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
async updateGroup(
|
|
399
|
-
name: string,
|
|
400
|
-
change: { displayName?: string; members?: string[]; host?: string },
|
|
401
|
-
): Promise<AgentGroup> {
|
|
402
|
-
return this.#serial(async () => {
|
|
403
|
-
const current = this.groupMap.get(name);
|
|
404
|
-
if (current?.status !== "active")
|
|
405
|
-
throw new AgentError(`There is no active group "${name}".`);
|
|
406
|
-
if (change.displayName !== undefined)
|
|
407
|
-
checkDisplayName(change.displayName);
|
|
408
|
-
const members = change.members ?? current.members;
|
|
409
|
-
const host =
|
|
410
|
-
change.host ??
|
|
411
|
-
(members.includes(current.host) ? current.host : (members[0] ?? ""));
|
|
412
|
-
this.checkMembers(members, host);
|
|
413
|
-
return this.#writeGroup(name, {
|
|
414
|
-
displayName: change.displayName?.trim() ?? current.displayName,
|
|
415
|
-
members,
|
|
416
|
-
host,
|
|
417
|
-
status: "active",
|
|
418
|
-
});
|
|
419
|
-
});
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
async archiveGroup(name: string): Promise<AgentGroup> {
|
|
423
|
-
return this.#serial(() => this.#writeGroup(name, { status: "archived" }));
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* Returns an archived group to the active ones, on a new channel when its old one is gone. Its
|
|
428
|
-
* members must all be active still and number enough; otherwise it refuses and stays archived.
|
|
429
|
-
*/
|
|
430
|
-
async restoreGroup(name: string, channelId?: string): Promise<AgentGroup> {
|
|
431
|
-
return this.#serial(async () => {
|
|
432
|
-
const current = this.checkRestorableGroup(name);
|
|
433
|
-
const rows = await this.#statements.restoreGroup(
|
|
434
|
-
name,
|
|
435
|
-
channelId ?? current.channelId ?? null,
|
|
436
|
-
);
|
|
437
|
-
return this.keepGroup(rows[0]);
|
|
438
|
-
});
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* Deletes an archived group with its messages and cursors, in the Discord channel's log and in
|
|
443
|
-
* each room's (`<group>@<surface>`); its name is free again. An active group is never deleted.
|
|
444
|
-
*/
|
|
445
|
-
async deleteGroup(name: string): Promise<void> {
|
|
446
|
-
return this.#serial(async () => {
|
|
447
|
-
const current = this.groupMap.get(name);
|
|
448
|
-
if (!current) throw new AgentError(`There is no group "${name}".`);
|
|
449
|
-
if (current.status === "active")
|
|
450
|
-
throw new AgentError(
|
|
451
|
-
`"${name}" is active; archive it before deleting it.`,
|
|
452
|
-
);
|
|
453
|
-
await this.#statements.deleteGroup(name);
|
|
454
|
-
this.groupMap.delete(name);
|
|
455
|
-
});
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
async #writeGroup(
|
|
459
|
-
name: string,
|
|
460
|
-
change: Partial<
|
|
461
|
-
Pick<AgentGroup, "displayName" | "members" | "host" | "status">
|
|
462
|
-
>,
|
|
463
|
-
): Promise<AgentGroup> {
|
|
464
|
-
const current = this.groupMap.get(name);
|
|
465
|
-
if (!current) throw new AgentError(`There is no group "${name}".`);
|
|
466
|
-
const rows = await this.#statements.updateGroup(name, {
|
|
467
|
-
displayName: change.displayName ?? current.displayName,
|
|
468
|
-
members: change.members ?? current.members,
|
|
469
|
-
host: change.host ?? current.host,
|
|
470
|
-
status: change.status ?? current.status,
|
|
471
|
-
});
|
|
472
|
-
return this.keepGroup(rows[0]);
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
appendGroupMessage(
|
|
476
|
-
group: string,
|
|
477
|
-
message: { author: string; authorName: string; text: string },
|
|
478
|
-
): Promise<number> {
|
|
479
|
-
return this.#messages.append(group, message);
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
/** The group's newest messages, oldest first. */
|
|
483
|
-
recentGroupMessages(group: string, limit: number): Promise<GroupMessage[]> {
|
|
484
|
-
return this.#messages.recent(group, limit);
|
|
485
|
-
}
|
|
486
|
-
|
|
487
|
-
/** Group messages the member has not received yet, its own excluded; the newest `limit` are carried. */
|
|
488
|
-
backlog(group: string, agent: string, limit: number): Promise<Backlog> {
|
|
489
|
-
return this.#messages.backlog(group, agent, limit);
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
advanceCursor(group: string, agent: string, lastId: number): Promise<void> {
|
|
493
|
-
return this.#messages.advanceCursor(group, agent, lastId);
|
|
494
|
-
}
|
|
495
|
-
|
|
496
|
-
/** Marks every message so far as received by every member, for a group started over. */
|
|
497
|
-
catchUp(group: AgentGroup, log?: string): Promise<void> {
|
|
498
|
-
return this.#messages.catchUp(group, log);
|
|
499
|
-
}
|
|
500
370
|
}
|
|
@@ -259,14 +259,20 @@ export const eventLog: string[] = [];
|
|
|
259
259
|
const PLUGIN_SELECTION = { tools: ["memory_add"], groups: ["workspace"] };
|
|
260
260
|
|
|
261
261
|
/**
|
|
262
|
-
* Waits until every queued channel task has settled
|
|
263
|
-
*
|
|
262
|
+
* Waits until every queued channel task has settled, every active agent has its avatar, which
|
|
263
|
+
* startup draws in the background, and every temp knight whose task ended is stamped finished
|
|
264
|
+
* (the stamp is written after the turn, off the queue; with an action held, a knight stays
|
|
265
|
+
* working); a fixed sleep was too short on a slow runner.
|
|
264
266
|
*/
|
|
265
267
|
export async function settle(): Promise<void> {
|
|
266
268
|
const deadline = Date.now() + 10_000;
|
|
267
269
|
const quiet = () =>
|
|
268
270
|
queue.busy().length === 0 &&
|
|
269
|
-
store.agents().every((a) => a.status !== "active" || a.avatarHash)
|
|
271
|
+
store.agents().every((a) => a.status !== "active" || a.avatarHash) &&
|
|
272
|
+
(runtime.held.size > 0 ||
|
|
273
|
+
store
|
|
274
|
+
.agents()
|
|
275
|
+
.every((a) => !a.parent || a.status !== "active" || a.finishedAt));
|
|
270
276
|
while (!quiet() && Date.now() < deadline) await Bun.sleep(5);
|
|
271
277
|
// Work the last task queued is still finishing.
|
|
272
278
|
await Bun.sleep(20);
|
|
@@ -91,6 +91,10 @@ export class DiscordAgentTeam implements AgentOps, AgentTeam {
|
|
|
91
91
|
return this.#options.guildId;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
+
get discordGuild(): string | undefined {
|
|
95
|
+
return this.#options.discordGuild;
|
|
96
|
+
}
|
|
97
|
+
|
|
94
98
|
/** Called when a turn starts or ends and when an agent or group changes. */
|
|
95
99
|
onChange(listener: () => void): void {
|
|
96
100
|
this.#listeners.push(listener);
|
|
@@ -105,10 +109,7 @@ export class DiscordAgentTeam implements AgentOps, AgentTeam {
|
|
|
105
109
|
return teamStatus(this.#context);
|
|
106
110
|
}
|
|
107
111
|
|
|
108
|
-
/**
|
|
109
|
-
* The agent or group that owns a channel of the agent server: by Discord channel, or by room on
|
|
110
|
-
* a surface that hosts rooms.
|
|
111
|
-
*/
|
|
112
|
+
/** The agent or group that owns a channel of the agent server, by Discord channel or by room. */
|
|
112
113
|
owns(channel: ChannelKey): "agent" | "group" | undefined {
|
|
113
114
|
const { store, surfaces } = this.#options;
|
|
114
115
|
return channelOwner(
|
|
@@ -120,10 +121,7 @@ export class DiscordAgentTeam implements AgentOps, AgentTeam {
|
|
|
120
121
|
);
|
|
121
122
|
}
|
|
122
123
|
|
|
123
|
-
/**
|
|
124
|
-
* The name of the coordinator: the agent in the entry channel, or, on a host without Discord,
|
|
125
|
-
* the one the configuration names; undefined while none is active.
|
|
126
|
-
*/
|
|
124
|
+
/** The coordinator's name: the entry channel's agent, else the configured one; undefined while none is active. */
|
|
127
125
|
coordinator(): string | undefined {
|
|
128
126
|
return coordinatorOf(this.#options.store, this.#options)?.name;
|
|
129
127
|
}
|
|
@@ -67,6 +67,11 @@ export interface RoutesOptions {
|
|
|
67
67
|
/** The conversation an agent last ran a turn in. */
|
|
68
68
|
latest?: (agent: string) => ChannelKey | undefined;
|
|
69
69
|
logger?: RoutesLogger;
|
|
70
|
+
/**
|
|
71
|
+
* Whether the team has Discord channels; default true. A team without them has no Discord place
|
|
72
|
+
* for an agent: a channel id still stored on a row, such as one a former guild left, is ignored.
|
|
73
|
+
*/
|
|
74
|
+
discordChannels?: boolean;
|
|
70
75
|
}
|
|
71
76
|
|
|
72
77
|
/** What the surfaces the host registered can hold, read when the agent server links. */
|
|
@@ -88,10 +93,24 @@ export class Routes implements RoutingTable {
|
|
|
88
93
|
readonly #routing: ResolvedRouting | undefined;
|
|
89
94
|
readonly #options: RoutesOptions;
|
|
90
95
|
readonly #warned = new Set<string>();
|
|
96
|
+
readonly #discordChannels: boolean;
|
|
91
97
|
|
|
92
98
|
constructor(options: RoutesOptions) {
|
|
93
99
|
this.#options = options;
|
|
94
100
|
this.#routing = options.routing;
|
|
101
|
+
this.#discordChannels = options.discordChannels ?? true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** The agent's places in order, its Discord channel first when the team has Discord channels. */
|
|
105
|
+
#keys(name: string): ChannelKey[] {
|
|
106
|
+
const keys = keysOf(
|
|
107
|
+
this.#options.store,
|
|
108
|
+
name,
|
|
109
|
+
this.#options.roomSurfaces(),
|
|
110
|
+
);
|
|
111
|
+
return this.#discordChannels
|
|
112
|
+
? keys
|
|
113
|
+
: keys.filter((key) => parseChannelKey(key).surface !== "discord");
|
|
95
114
|
}
|
|
96
115
|
|
|
97
116
|
/**
|
|
@@ -121,7 +140,9 @@ export class Routes implements RoutingTable {
|
|
|
121
140
|
? (rooms[0] as string)
|
|
122
141
|
: surface;
|
|
123
142
|
if (wanted === "discord")
|
|
124
|
-
return agent.channelId
|
|
143
|
+
return agent.channelId && this.#discordChannels
|
|
144
|
+
? discordKey(agent.channelId)
|
|
145
|
+
: undefined;
|
|
125
146
|
return rooms.includes(wanted) ? agentRoomKey(wanted, name) : undefined;
|
|
126
147
|
}
|
|
127
148
|
|
|
@@ -143,7 +164,7 @@ export class Routes implements RoutingTable {
|
|
|
143
164
|
"the agent has no place on its primary surface; its auto place is used",
|
|
144
165
|
);
|
|
145
166
|
}
|
|
146
|
-
return
|
|
167
|
+
return this.#keys(name)[0];
|
|
147
168
|
}
|
|
148
169
|
|
|
149
170
|
/**
|
|
@@ -225,11 +246,7 @@ export class Routes implements RoutingTable {
|
|
|
225
246
|
from === undefined ? undefined : parseChannelKey(from).surface;
|
|
226
247
|
if (to === "latest" && surface !== undefined) {
|
|
227
248
|
const last = latest?.(name);
|
|
228
|
-
const keys =
|
|
229
|
-
this.#options.store,
|
|
230
|
-
name,
|
|
231
|
-
this.#options.roomSurfaces(),
|
|
232
|
-
);
|
|
249
|
+
const keys = this.#keys(name);
|
|
233
250
|
if (
|
|
234
251
|
last !== undefined &&
|
|
235
252
|
keys.includes(last) &&
|
|
@@ -17,6 +17,8 @@ export interface AgentTeamOptions
|
|
|
17
17
|
* rows the team keeps.
|
|
18
18
|
*/
|
|
19
19
|
guildId: string;
|
|
20
|
+
/** The Discord guild the team's channels live in, which is not the team's id when the host sets one; absent without Discord channels. */
|
|
21
|
+
discordGuild?: string | undefined;
|
|
20
22
|
/** The first team, read and inserted once on start; an agent already stored is never overwritten. */
|
|
21
23
|
seeds: () => readonly AgentSeed[];
|
|
22
24
|
/** Sections the plugins add to every agent turn's system prompt, read before each run. */
|