pi-roundtable 0.43.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 +11 -0
- 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-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/builtin/agent-server.ts +10 -0
- package/src/core/db/legacy-channels.ts +92 -0
- package/src/core/db/team-move.ts +55 -20
- package/src/core/testing/fake-discord.ts +67 -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 +32 -272
|
@@ -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
|
}
|
|
@@ -14,6 +14,7 @@ import { agentOfChannel, discordKey } from "../agents/team-keys.ts";
|
|
|
14
14
|
import { ownerAttachmentDir } from "../attachments/attachment-dir.ts";
|
|
15
15
|
import { ROOM_LAYOUT } from "../contract/room-layout.ts";
|
|
16
16
|
import type { AgentSessions } from "../contract/runtime.ts";
|
|
17
|
+
import { LEGACY_CHANNEL_SURFACE } from "../db/legacy-channels.ts";
|
|
17
18
|
import { moveTeam } from "../db/team-move.ts";
|
|
18
19
|
import type { ChannelKey } from "../domain/conversation.ts";
|
|
19
20
|
import { ConfigError } from "../domain/errors.ts";
|
|
@@ -136,7 +137,16 @@ export function agentServerPlugin(
|
|
|
136
137
|
...(options.guildId === undefined
|
|
137
138
|
? { clearChannels: true }
|
|
138
139
|
: { discordGuild: options.guildId }),
|
|
140
|
+
// The legacy channels' places follow their columns: kept in the agent guild, dropped elsewhere.
|
|
141
|
+
places: [
|
|
142
|
+
{
|
|
143
|
+
surface: LEGACY_CHANNEL_SURFACE,
|
|
144
|
+
...(options.guildId === undefined ? {} : { keep: options.guildId }),
|
|
145
|
+
},
|
|
146
|
+
],
|
|
139
147
|
}),
|
|
148
|
+
// After the move, so the rows it brought in with their channel ids get their places at this boot.
|
|
149
|
+
PgAgentStore.placesConvergence(teamId),
|
|
140
150
|
],
|
|
141
151
|
provides: [AGENTS],
|
|
142
152
|
requires: [RUNTIME],
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { SQL } from "bun";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The surface whose places `agents.channel_id` and `agent_groups.channel_id` hold: those columns
|
|
5
|
+
* always kept a Discord channel or thread id. Named here once, outside the team directories, so the
|
|
6
|
+
* team code stays free of it. Until 1.0 the store writes the columns and the `agent_places` rows of
|
|
7
|
+
* this surface together, and every boot makes the rows equal the columns again.
|
|
8
|
+
*/
|
|
9
|
+
export const LEGACY_CHANNEL_SURFACE = "discord";
|
|
10
|
+
|
|
11
|
+
/** The tables whose `channel_id` column is a legacy channel, and the kind of place owner each row is. */
|
|
12
|
+
export const LEGACY_CHANNEL_TABLES = {
|
|
13
|
+
agents: "agent",
|
|
14
|
+
agent_groups: "group",
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
/** Serializes concurrent boots of the convergence; the key of the ledger's lock for this id. */
|
|
18
|
+
const LOCK = "pi-roundtable:migration:agent-server/agent-places-from-channels";
|
|
19
|
+
|
|
20
|
+
interface WantedPlace {
|
|
21
|
+
kind: string;
|
|
22
|
+
name: string;
|
|
23
|
+
external_id: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The warning for an owner whose channel id another owner of its team holds already: a place id
|
|
28
|
+
* belongs to one owner, so no place row is made for the second, and its column stays as it is.
|
|
29
|
+
*/
|
|
30
|
+
export function warnSharedChannel(
|
|
31
|
+
team: string,
|
|
32
|
+
owner: { kind: string; name: string },
|
|
33
|
+
channelId: string,
|
|
34
|
+
holder: { kind: string; name: string } | undefined,
|
|
35
|
+
): void {
|
|
36
|
+
console.warn(
|
|
37
|
+
`agent-places: ${owner.kind} "${owner.name}" of team "${team}" holds channel ${channelId}, which ${holder?.kind} "${holder?.name}" holds already; no place row is made for it`,
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Makes the legacy-surface rows of `agent_places` of one team equal the legacy columns of that
|
|
43
|
+
* team: inserts each non-null `channel_id` as `(team, kind, name, surface, channel_id)`, and
|
|
44
|
+
* deletes a row whose column is now null or different, as after a rollback to a build that wrote
|
|
45
|
+
* only the columns. It reads and writes the rows of `teamId` only, so hosts of several teams over
|
|
46
|
+
* one database never delete each other's rows, and a record that stays under a former id, which
|
|
47
|
+
* `moveTeam` could not move, gets no row. Idempotent, in one transaction under an advisory lock. A
|
|
48
|
+
* channel id that two owners of the team hold stays with the owner that has the row already, or
|
|
49
|
+
* else with the first by kind then name, and the other is skipped with a warning naming both.
|
|
50
|
+
*/
|
|
51
|
+
export function convergeLegacyChannelPlaces(
|
|
52
|
+
teamId: string,
|
|
53
|
+
): (sql: SQL) => Promise<void> {
|
|
54
|
+
const wanted = Object.entries(LEGACY_CHANNEL_TABLES)
|
|
55
|
+
.map(
|
|
56
|
+
([table, kind]) =>
|
|
57
|
+
`SELECT '${kind}' AS kind, name, channel_id AS external_id FROM ${table} WHERE guild_id = $2 AND channel_id IS NOT NULL`,
|
|
58
|
+
)
|
|
59
|
+
.join(" UNION ALL ");
|
|
60
|
+
return (sql) =>
|
|
61
|
+
sql.begin(async (tx) => {
|
|
62
|
+
await tx`SELECT pg_advisory_xact_lock(hashtextextended(${LOCK}, 0))`;
|
|
63
|
+
await tx.unsafe(
|
|
64
|
+
`DELETE FROM agent_places p
|
|
65
|
+
WHERE p.surface = $1 AND p.team_id = $2 AND NOT EXISTS (
|
|
66
|
+
SELECT 1 FROM (${wanted}) w
|
|
67
|
+
WHERE w.kind = p.kind AND w.name = p.name AND w.external_id = p.external_id)`,
|
|
68
|
+
[LEGACY_CHANNEL_SURFACE, teamId],
|
|
69
|
+
);
|
|
70
|
+
const missing: WantedPlace[] = await tx.unsafe(
|
|
71
|
+
`SELECT w.kind, w.name, w.external_id FROM (${wanted}) w
|
|
72
|
+
WHERE NOT EXISTS (
|
|
73
|
+
SELECT 1 FROM agent_places p
|
|
74
|
+
WHERE p.surface = $1 AND p.team_id = $2 AND p.kind = w.kind AND p.name = w.name)
|
|
75
|
+
ORDER BY w.kind, w.name`,
|
|
76
|
+
[LEGACY_CHANNEL_SURFACE, teamId],
|
|
77
|
+
);
|
|
78
|
+
for (const place of missing) {
|
|
79
|
+
const inserted = await tx`
|
|
80
|
+
INSERT INTO agent_places (team_id, kind, name, surface, external_id)
|
|
81
|
+
VALUES (${teamId}, ${place.kind}, ${place.name}, ${LEGACY_CHANNEL_SURFACE}, ${place.external_id})
|
|
82
|
+
ON CONFLICT DO NOTHING
|
|
83
|
+
RETURNING 1`;
|
|
84
|
+
if (inserted.length > 0) continue;
|
|
85
|
+
const [holder]: { kind: string; name: string }[] = await tx`
|
|
86
|
+
SELECT kind, name FROM agent_places
|
|
87
|
+
WHERE team_id = ${teamId} AND surface = ${LEGACY_CHANNEL_SURFACE}
|
|
88
|
+
AND external_id = ${place.external_id}`;
|
|
89
|
+
warnSharedChannel(teamId, place, place.external_id, holder);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
}
|