pi-roundtable 0.43.0 → 0.44.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 +17 -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
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import type { ClassId } from "./catalog.ts";
|
|
2
|
+
import { type ClassEntry, preset } from "./class-data.ts";
|
|
3
|
+
|
|
4
|
+
/** Some of the classes of the catalog, by id. */
|
|
5
|
+
export const CLASSES_C = {
|
|
6
|
+
envoy: {
|
|
7
|
+
label: "Envoy",
|
|
8
|
+
jobs: "email, partner comms, notifications",
|
|
9
|
+
keywords: [
|
|
10
|
+
"email",
|
|
11
|
+
"mail",
|
|
12
|
+
"partner",
|
|
13
|
+
"notif",
|
|
14
|
+
"message",
|
|
15
|
+
"outreach",
|
|
16
|
+
"comms",
|
|
17
|
+
"信件",
|
|
18
|
+
"郵件",
|
|
19
|
+
"通知",
|
|
20
|
+
"聯絡",
|
|
21
|
+
],
|
|
22
|
+
preset: preset({
|
|
23
|
+
outfit: "tunic",
|
|
24
|
+
hat: "cavalier",
|
|
25
|
+
prop: "seal",
|
|
26
|
+
hair: "bun",
|
|
27
|
+
beard: "none",
|
|
28
|
+
accessory: "none",
|
|
29
|
+
build: "regular",
|
|
30
|
+
skin: 1,
|
|
31
|
+
hairColor: "#aeb4c3",
|
|
32
|
+
palette: "purpure",
|
|
33
|
+
}),
|
|
34
|
+
},
|
|
35
|
+
rogue: {
|
|
36
|
+
label: "Rogue",
|
|
37
|
+
jobs: "pen-tester, auditor, intel",
|
|
38
|
+
keywords: [
|
|
39
|
+
"pentest",
|
|
40
|
+
"pen-test",
|
|
41
|
+
"penetration",
|
|
42
|
+
"auditor",
|
|
43
|
+
"audit",
|
|
44
|
+
"intel",
|
|
45
|
+
"exploit",
|
|
46
|
+
"osint",
|
|
47
|
+
"滲透",
|
|
48
|
+
"稽核",
|
|
49
|
+
"情報",
|
|
50
|
+
"紅隊",
|
|
51
|
+
],
|
|
52
|
+
preset: preset({
|
|
53
|
+
outfit: "cloak",
|
|
54
|
+
hat: "cowl",
|
|
55
|
+
prop: "dagger",
|
|
56
|
+
hair: "none",
|
|
57
|
+
beard: "none",
|
|
58
|
+
accessory: "scar",
|
|
59
|
+
build: "slim",
|
|
60
|
+
skin: 0,
|
|
61
|
+
hairColor: "#2b2233",
|
|
62
|
+
palette: "sable",
|
|
63
|
+
}),
|
|
64
|
+
},
|
|
65
|
+
monk: {
|
|
66
|
+
label: "Monk",
|
|
67
|
+
jobs: "long-running jobs, batch, patience",
|
|
68
|
+
keywords: [
|
|
69
|
+
"long-running",
|
|
70
|
+
"batch",
|
|
71
|
+
"patient",
|
|
72
|
+
"background",
|
|
73
|
+
"overnight",
|
|
74
|
+
"meditat",
|
|
75
|
+
"cron",
|
|
76
|
+
"長時間",
|
|
77
|
+
"批次",
|
|
78
|
+
"批量",
|
|
79
|
+
"耐心",
|
|
80
|
+
],
|
|
81
|
+
preset: preset({
|
|
82
|
+
outfit: "habit",
|
|
83
|
+
hat: "headband",
|
|
84
|
+
prop: "beads",
|
|
85
|
+
hair: "none",
|
|
86
|
+
beard: "none",
|
|
87
|
+
accessory: "none",
|
|
88
|
+
build: "regular",
|
|
89
|
+
skin: 3,
|
|
90
|
+
hairColor: "#2b2233",
|
|
91
|
+
palette: "amber",
|
|
92
|
+
}),
|
|
93
|
+
},
|
|
94
|
+
falconer: {
|
|
95
|
+
label: "Falconer",
|
|
96
|
+
jobs: "retrieval, fetch, RAG lookup",
|
|
97
|
+
keywords: [
|
|
98
|
+
"retriev",
|
|
99
|
+
"fetch",
|
|
100
|
+
"rag",
|
|
101
|
+
"lookup",
|
|
102
|
+
"vector",
|
|
103
|
+
"embedding",
|
|
104
|
+
"檢索",
|
|
105
|
+
"擷取",
|
|
106
|
+
"抓取",
|
|
107
|
+
"查詢",
|
|
108
|
+
],
|
|
109
|
+
preset: preset({
|
|
110
|
+
outfit: "cloak",
|
|
111
|
+
hat: "fcap",
|
|
112
|
+
prop: "bird",
|
|
113
|
+
hair: "short",
|
|
114
|
+
beard: "goatee",
|
|
115
|
+
accessory: "none",
|
|
116
|
+
build: "slim",
|
|
117
|
+
skin: 4,
|
|
118
|
+
hairColor: "#2b2233",
|
|
119
|
+
palette: "vert",
|
|
120
|
+
}),
|
|
121
|
+
},
|
|
122
|
+
shipwright: {
|
|
123
|
+
label: "Shipwright",
|
|
124
|
+
jobs: "release, build pipeline, packaging",
|
|
125
|
+
keywords: [
|
|
126
|
+
"release",
|
|
127
|
+
"ship",
|
|
128
|
+
"packag",
|
|
129
|
+
"publish",
|
|
130
|
+
"deploy",
|
|
131
|
+
"pipeline",
|
|
132
|
+
"發版",
|
|
133
|
+
"發布",
|
|
134
|
+
"打包",
|
|
135
|
+
"上線",
|
|
136
|
+
"部署",
|
|
137
|
+
],
|
|
138
|
+
preset: preset({
|
|
139
|
+
outfit: "sailor",
|
|
140
|
+
hat: "sailor",
|
|
141
|
+
prop: "anchor",
|
|
142
|
+
hair: "short",
|
|
143
|
+
beard: "goatee",
|
|
144
|
+
accessory: "none",
|
|
145
|
+
build: "stout",
|
|
146
|
+
skin: 2,
|
|
147
|
+
hairColor: "#e0b45a",
|
|
148
|
+
palette: "azure",
|
|
149
|
+
}),
|
|
150
|
+
},
|
|
151
|
+
jester: {
|
|
152
|
+
label: "Jester",
|
|
153
|
+
jobs: "brainstorm, ideation, humor",
|
|
154
|
+
keywords: [
|
|
155
|
+
"brainstorm",
|
|
156
|
+
"ideation",
|
|
157
|
+
"idea",
|
|
158
|
+
"humor",
|
|
159
|
+
"joke",
|
|
160
|
+
"creative",
|
|
161
|
+
"fun",
|
|
162
|
+
"腦力激盪",
|
|
163
|
+
"發想",
|
|
164
|
+
"創意",
|
|
165
|
+
"幽默",
|
|
166
|
+
"點子",
|
|
167
|
+
],
|
|
168
|
+
preset: preset({
|
|
169
|
+
outfit: "motley",
|
|
170
|
+
hat: "jester",
|
|
171
|
+
prop: "marotte",
|
|
172
|
+
hair: "none",
|
|
173
|
+
beard: "none",
|
|
174
|
+
accessory: "freckles",
|
|
175
|
+
build: "slim",
|
|
176
|
+
skin: 0,
|
|
177
|
+
hairColor: "#b0562f",
|
|
178
|
+
palette: "gules",
|
|
179
|
+
}),
|
|
180
|
+
},
|
|
181
|
+
paladin: {
|
|
182
|
+
label: "Paladin",
|
|
183
|
+
jobs: "compliance, policy, approvals",
|
|
184
|
+
keywords: [
|
|
185
|
+
"compliance",
|
|
186
|
+
"policy",
|
|
187
|
+
"approval",
|
|
188
|
+
"approve",
|
|
189
|
+
"govern",
|
|
190
|
+
"legal",
|
|
191
|
+
"ethic",
|
|
192
|
+
"合規",
|
|
193
|
+
"法遵",
|
|
194
|
+
"核准",
|
|
195
|
+
"政策",
|
|
196
|
+
"規範",
|
|
197
|
+
],
|
|
198
|
+
preset: preset({
|
|
199
|
+
outfit: "plate",
|
|
200
|
+
hat: "winghelm",
|
|
201
|
+
prop: "sword",
|
|
202
|
+
hair: "none",
|
|
203
|
+
beard: "none",
|
|
204
|
+
accessory: "none",
|
|
205
|
+
build: "regular",
|
|
206
|
+
skin: 5,
|
|
207
|
+
hairColor: "#2b2233",
|
|
208
|
+
palette: "amber",
|
|
209
|
+
}),
|
|
210
|
+
},
|
|
211
|
+
archivist: {
|
|
212
|
+
label: "Archivist",
|
|
213
|
+
jobs: "knowledge base, indexing, memory",
|
|
214
|
+
keywords: [
|
|
215
|
+
"knowledge",
|
|
216
|
+
"archive",
|
|
217
|
+
"index",
|
|
218
|
+
"memory",
|
|
219
|
+
"wiki",
|
|
220
|
+
"catalog",
|
|
221
|
+
"library",
|
|
222
|
+
"知識",
|
|
223
|
+
"索引",
|
|
224
|
+
"記憶",
|
|
225
|
+
"歸檔",
|
|
226
|
+
"典藏",
|
|
227
|
+
],
|
|
228
|
+
preset: preset({
|
|
229
|
+
outfit: "scholar",
|
|
230
|
+
hat: "none",
|
|
231
|
+
prop: "tome",
|
|
232
|
+
hair: "bun",
|
|
233
|
+
beard: "none",
|
|
234
|
+
accessory: "glasses",
|
|
235
|
+
build: "regular",
|
|
236
|
+
skin: 2,
|
|
237
|
+
hairColor: "#e4e7f0",
|
|
238
|
+
palette: "teal",
|
|
239
|
+
}),
|
|
240
|
+
},
|
|
241
|
+
} satisfies Partial<Record<ClassId, ClassEntry>>;
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import type { SQL } from "bun";
|
|
2
|
+
import { AgentError } from "../domain/errors.ts";
|
|
3
|
+
import { AgentBook } from "./agent-book.ts";
|
|
4
|
+
import type { AgentPlaces } from "./agent-places.ts";
|
|
5
|
+
import {
|
|
6
|
+
type AgentGroup,
|
|
7
|
+
type Backlog,
|
|
8
|
+
checkDisplayName,
|
|
9
|
+
type GroupMessage,
|
|
10
|
+
} from "./agent-rules.ts";
|
|
11
|
+
import { AgentStatements } from "./agent-statements.ts";
|
|
12
|
+
import { GroupMessages } from "./group-messages.ts";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The write queue and the group half of the agent store: groups, their messages and their
|
|
16
|
+
* cursors, in the database and in the memory the book holds. The agent half is `PgAgentStore`.
|
|
17
|
+
*/
|
|
18
|
+
export abstract class AgentGroupStore extends AgentBook {
|
|
19
|
+
protected readonly sql: SQL;
|
|
20
|
+
protected readonly teamId: string;
|
|
21
|
+
protected readonly statements: AgentStatements;
|
|
22
|
+
/** The stored places of the team's agents and groups. */
|
|
23
|
+
readonly places: AgentPlaces;
|
|
24
|
+
readonly #messages: GroupMessages;
|
|
25
|
+
/** The tail of the agent and group writes, which run one at a time so none reads a map another is about to change. */
|
|
26
|
+
#queue: Promise<unknown> = Promise.resolve();
|
|
27
|
+
|
|
28
|
+
protected constructor(sql: SQL, teamId: string, places: AgentPlaces) {
|
|
29
|
+
super();
|
|
30
|
+
this.sql = sql;
|
|
31
|
+
this.teamId = teamId;
|
|
32
|
+
this.places = places;
|
|
33
|
+
this.statements = new AgentStatements(sql, teamId);
|
|
34
|
+
this.#messages = new GroupMessages(sql, teamId);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The statements over a transaction, for a write that commits with its place rows. */
|
|
38
|
+
protected statementsOn(tx: SQL): AgentStatements {
|
|
39
|
+
return new AgentStatements(tx, this.teamId);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
protected serial<T>(write: () => Promise<T>): Promise<T> {
|
|
43
|
+
const run = this.#queue.then(write);
|
|
44
|
+
this.#queue = run.catch(() => undefined);
|
|
45
|
+
return run;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async createGroup(group: {
|
|
49
|
+
name: string;
|
|
50
|
+
displayName: string;
|
|
51
|
+
/** Absent in a team without Discord, whose groups live in rooms. */
|
|
52
|
+
channelId?: string;
|
|
53
|
+
members: string[];
|
|
54
|
+
host: string;
|
|
55
|
+
}): Promise<AgentGroup> {
|
|
56
|
+
return this.serial(async () => {
|
|
57
|
+
this.checkNewGroup(group.name, group.members, group.host);
|
|
58
|
+
checkDisplayName(group.displayName);
|
|
59
|
+
const rows = await this.places.legacy("group", (tx) =>
|
|
60
|
+
this.statementsOn(tx).insertGroup({
|
|
61
|
+
...group,
|
|
62
|
+
displayName: group.displayName.trim(),
|
|
63
|
+
channelId: group.channelId ?? null,
|
|
64
|
+
}),
|
|
65
|
+
);
|
|
66
|
+
return this.keepGroup(rows[0]);
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async updateGroup(
|
|
71
|
+
name: string,
|
|
72
|
+
change: { displayName?: string; members?: string[]; host?: string },
|
|
73
|
+
): Promise<AgentGroup> {
|
|
74
|
+
return this.serial(async () => {
|
|
75
|
+
const current = this.groupMap.get(name);
|
|
76
|
+
if (current?.status !== "active")
|
|
77
|
+
throw new AgentError(`There is no active group "${name}".`);
|
|
78
|
+
if (change.displayName !== undefined)
|
|
79
|
+
checkDisplayName(change.displayName);
|
|
80
|
+
const members = change.members ?? current.members;
|
|
81
|
+
const host =
|
|
82
|
+
change.host ??
|
|
83
|
+
(members.includes(current.host) ? current.host : (members[0] ?? ""));
|
|
84
|
+
this.checkMembers(members, host);
|
|
85
|
+
return this.writeGroup(name, {
|
|
86
|
+
displayName: change.displayName?.trim() ?? current.displayName,
|
|
87
|
+
members,
|
|
88
|
+
host,
|
|
89
|
+
status: "active",
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async archiveGroup(name: string): Promise<AgentGroup> {
|
|
95
|
+
return this.serial(() => this.writeGroup(name, { status: "archived" }));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Returns an archived group to the active ones, on a new channel when its old one is gone. Its
|
|
100
|
+
* members must all be active still and number enough; otherwise it refuses and stays archived.
|
|
101
|
+
*/
|
|
102
|
+
async restoreGroup(name: string, channelId?: string): Promise<AgentGroup> {
|
|
103
|
+
return this.serial(async () => {
|
|
104
|
+
const current = this.checkRestorableGroup(name);
|
|
105
|
+
const rows = await this.places.legacy("group", (tx) =>
|
|
106
|
+
this.statementsOn(tx).restoreGroup(
|
|
107
|
+
name,
|
|
108
|
+
channelId ?? current.channelId ?? null,
|
|
109
|
+
),
|
|
110
|
+
);
|
|
111
|
+
return this.keepGroup(rows[0]);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Deletes an archived group with its messages and cursors, in the Discord channel's log and in
|
|
117
|
+
* each room's (`<group>@<surface>`); its name is free again. An active group is never deleted.
|
|
118
|
+
*/
|
|
119
|
+
async deleteGroup(name: string): Promise<void> {
|
|
120
|
+
return this.serial(async () => {
|
|
121
|
+
const current = this.groupMap.get(name);
|
|
122
|
+
if (!current) throw new AgentError(`There is no group "${name}".`);
|
|
123
|
+
if (current.status === "active")
|
|
124
|
+
throw new AgentError(
|
|
125
|
+
`"${name}" is active; archive it before deleting it.`,
|
|
126
|
+
);
|
|
127
|
+
await this.places.dropping("group", name, (tx) =>
|
|
128
|
+
this.statementsOn(tx).deleteGroup(name),
|
|
129
|
+
);
|
|
130
|
+
this.groupMap.delete(name);
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
protected async writeGroup(
|
|
135
|
+
name: string,
|
|
136
|
+
change: Partial<
|
|
137
|
+
Pick<AgentGroup, "displayName" | "members" | "host" | "status">
|
|
138
|
+
>,
|
|
139
|
+
): Promise<AgentGroup> {
|
|
140
|
+
const current = this.groupMap.get(name);
|
|
141
|
+
if (!current) throw new AgentError(`There is no group "${name}".`);
|
|
142
|
+
const rows = await this.statements.updateGroup(name, {
|
|
143
|
+
displayName: change.displayName ?? current.displayName,
|
|
144
|
+
members: change.members ?? current.members,
|
|
145
|
+
host: change.host ?? current.host,
|
|
146
|
+
status: change.status ?? current.status,
|
|
147
|
+
});
|
|
148
|
+
return this.keepGroup(rows[0]);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
appendGroupMessage(
|
|
152
|
+
group: string,
|
|
153
|
+
message: { author: string; authorName: string; text: string },
|
|
154
|
+
): Promise<number> {
|
|
155
|
+
return this.#messages.append(group, message);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** The group's newest messages, oldest first. */
|
|
159
|
+
recentGroupMessages(group: string, limit: number): Promise<GroupMessage[]> {
|
|
160
|
+
return this.#messages.recent(group, limit);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Group messages the member has not received yet, its own excluded; the newest `limit` are carried. */
|
|
164
|
+
backlog(group: string, agent: string, limit: number): Promise<Backlog> {
|
|
165
|
+
return this.#messages.backlog(group, agent, limit);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
advanceCursor(group: string, agent: string, lastId: number): Promise<void> {
|
|
169
|
+
return this.#messages.advanceCursor(group, agent, lastId);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Marks every message so far as received by every member, for a group started over. */
|
|
173
|
+
catchUp(group: AgentGroup, log?: string): Promise<void> {
|
|
174
|
+
return this.#messages.catchUp(group, log);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import type { SQL } from "bun";
|
|
2
|
+
import {
|
|
3
|
+
LEGACY_CHANNEL_SURFACE,
|
|
4
|
+
warnSharedChannel,
|
|
5
|
+
} from "../db/legacy-channels.ts";
|
|
6
|
+
|
|
7
|
+
/** What a place belongs to: an agent or a group, by name. */
|
|
8
|
+
export type PlaceOwnerKind = "agent" | "group";
|
|
9
|
+
|
|
10
|
+
/** A stored place: the id the surface gave it, for one owner on one surface. */
|
|
11
|
+
export interface StoredPlace {
|
|
12
|
+
kind: PlaceOwnerKind;
|
|
13
|
+
name: string;
|
|
14
|
+
surface: string;
|
|
15
|
+
externalId: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface PlaceRow {
|
|
19
|
+
kind: PlaceOwnerKind;
|
|
20
|
+
name: string;
|
|
21
|
+
surface: string;
|
|
22
|
+
external_id: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** A row a write returned, which carries the legacy `channel_id` the place follows. */
|
|
26
|
+
interface LegacyRow {
|
|
27
|
+
name: string;
|
|
28
|
+
channel_id: string | null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const ownerKey = (kind: PlaceOwnerKind, name: string, surface: string) =>
|
|
32
|
+
`${kind}\0${name}\0${surface}`;
|
|
33
|
+
const idKey = (surface: string, externalId: string) =>
|
|
34
|
+
`${surface}\0${externalId}`;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* The stored places of one team, in the database and in memory, so an inbound message's owner
|
|
38
|
+
* lookup reads no database. A team's agents and groups have at most one place per surface.
|
|
39
|
+
*
|
|
40
|
+
* Until 1.0 the legacy `channel_id` columns hold the same place as the row of the legacy surface:
|
|
41
|
+
* `legacy` and `dropping` write both in the transaction of the write that changed the column, and
|
|
42
|
+
* the memory follows once it commits.
|
|
43
|
+
*/
|
|
44
|
+
export class AgentPlaces {
|
|
45
|
+
readonly #sql: SQL;
|
|
46
|
+
readonly #team: string;
|
|
47
|
+
readonly #byOwner = new Map<string, StoredPlace>();
|
|
48
|
+
readonly #byId = new Map<string, StoredPlace>();
|
|
49
|
+
|
|
50
|
+
private constructor(sql: SQL, teamId: string) {
|
|
51
|
+
this.#sql = sql;
|
|
52
|
+
this.#team = teamId;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The places of the team, read from the table. */
|
|
56
|
+
static async load(sql: SQL, teamId: string): Promise<AgentPlaces> {
|
|
57
|
+
const places = new AgentPlaces(sql, teamId);
|
|
58
|
+
await places.reload();
|
|
59
|
+
return places;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** No place read: `Roundtable.check()` has no database to read them from. */
|
|
63
|
+
static empty(sql: SQL, teamId: string): AgentPlaces {
|
|
64
|
+
return new AgentPlaces(sql, teamId);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async reload(): Promise<void> {
|
|
68
|
+
const rows: PlaceRow[] = await this
|
|
69
|
+
.#sql`SELECT kind, name, surface, external_id FROM agent_places WHERE team_id = ${this.#team}`;
|
|
70
|
+
this.#byOwner.clear();
|
|
71
|
+
this.#byId.clear();
|
|
72
|
+
for (const row of rows)
|
|
73
|
+
this.#remember({
|
|
74
|
+
kind: row.kind,
|
|
75
|
+
name: row.name,
|
|
76
|
+
surface: row.surface,
|
|
77
|
+
externalId: row.external_id,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Every place of the agent or group, one per surface. */
|
|
82
|
+
of(kind: PlaceOwnerKind, name: string): StoredPlace[] {
|
|
83
|
+
return [...this.#byOwner.values()].filter(
|
|
84
|
+
(place) => place.kind === kind && place.name === name,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The id of the owner's place on the surface. */
|
|
89
|
+
on(kind: PlaceOwnerKind, name: string, surface: string): string | undefined {
|
|
90
|
+
return this.#byOwner.get(ownerKey(kind, name, surface))?.externalId;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The owner of the place with this id on the surface. */
|
|
94
|
+
ownerOf(
|
|
95
|
+
surface: string,
|
|
96
|
+
externalId: string,
|
|
97
|
+
): { kind: PlaceOwnerKind; name: string } | undefined {
|
|
98
|
+
const place = this.#byId.get(idKey(surface, externalId));
|
|
99
|
+
return place && { kind: place.kind, name: place.name };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Runs a write that sets or keeps a legacy `channel_id` and returns the rows it wrote; in the same
|
|
104
|
+
* transaction each row's legacy-surface place is made equal to its column, a null column
|
|
105
|
+
* deleting the place.
|
|
106
|
+
*/
|
|
107
|
+
async legacy<R extends LegacyRow>(
|
|
108
|
+
kind: PlaceOwnerKind,
|
|
109
|
+
write: (tx: SQL) => Promise<R[]>,
|
|
110
|
+
): Promise<R[]> {
|
|
111
|
+
const applied: (() => void)[] = [];
|
|
112
|
+
const rows = await this.#sql.begin(async (tx) => {
|
|
113
|
+
const written = await write(tx);
|
|
114
|
+
for (const row of written)
|
|
115
|
+
applied.push(await this.#follow(tx, kind, row.name, row.channel_id));
|
|
116
|
+
return written;
|
|
117
|
+
});
|
|
118
|
+
for (const apply of applied) apply();
|
|
119
|
+
return rows;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Runs a write that deletes the owner's rows and, in the same transaction, every place it had. */
|
|
123
|
+
async dropping(
|
|
124
|
+
kind: PlaceOwnerKind,
|
|
125
|
+
name: string,
|
|
126
|
+
write: (tx: SQL) => Promise<void>,
|
|
127
|
+
): Promise<void> {
|
|
128
|
+
await this.#sql.begin(async (tx) => {
|
|
129
|
+
await write(tx);
|
|
130
|
+
await tx`
|
|
131
|
+
DELETE FROM agent_places
|
|
132
|
+
WHERE team_id = ${this.#team} AND kind = ${kind} AND name = ${name}`;
|
|
133
|
+
});
|
|
134
|
+
for (const place of this.of(kind, name)) this.#forget(place);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async #follow(
|
|
138
|
+
tx: SQL,
|
|
139
|
+
kind: PlaceOwnerKind,
|
|
140
|
+
name: string,
|
|
141
|
+
channelId: string | null,
|
|
142
|
+
): Promise<() => void> {
|
|
143
|
+
const owner = { kind, name, surface: LEGACY_CHANNEL_SURFACE };
|
|
144
|
+
if (channelId === null) {
|
|
145
|
+
await tx`
|
|
146
|
+
DELETE FROM agent_places
|
|
147
|
+
WHERE team_id = ${this.#team} AND kind = ${kind} AND name = ${name}
|
|
148
|
+
AND surface = ${owner.surface}`;
|
|
149
|
+
return () => {
|
|
150
|
+
const place = this.#byOwner.get(
|
|
151
|
+
ownerKey(kind, name, LEGACY_CHANNEL_SURFACE),
|
|
152
|
+
);
|
|
153
|
+
if (place) this.#forget(place);
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
// A place id belongs to one owner: the column keeps the id, and the owner has no row for it.
|
|
157
|
+
const [holder]: { kind: PlaceOwnerKind; name: string }[] = await tx`
|
|
158
|
+
SELECT kind, name FROM agent_places
|
|
159
|
+
WHERE team_id = ${this.#team} AND surface = ${owner.surface} AND external_id = ${channelId}
|
|
160
|
+
AND NOT (kind = ${kind} AND name = ${name})`;
|
|
161
|
+
if (holder) {
|
|
162
|
+
warnSharedChannel(this.#team, owner, channelId, holder);
|
|
163
|
+
await tx`
|
|
164
|
+
DELETE FROM agent_places
|
|
165
|
+
WHERE team_id = ${this.#team} AND kind = ${kind} AND name = ${name}
|
|
166
|
+
AND surface = ${owner.surface}`;
|
|
167
|
+
return () => {
|
|
168
|
+
const place = this.#byOwner.get(ownerKey(kind, name, owner.surface));
|
|
169
|
+
if (place) this.#forget(place);
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
await tx`
|
|
173
|
+
INSERT INTO agent_places (team_id, kind, name, surface, external_id)
|
|
174
|
+
VALUES (${this.#team}, ${kind}, ${name}, ${owner.surface}, ${channelId})
|
|
175
|
+
ON CONFLICT (team_id, kind, name, surface)
|
|
176
|
+
DO UPDATE SET external_id = EXCLUDED.external_id`;
|
|
177
|
+
return () => this.#replace({ ...owner, externalId: channelId });
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
#replace(place: StoredPlace): void {
|
|
181
|
+
const current = this.#byOwner.get(
|
|
182
|
+
ownerKey(place.kind, place.name, place.surface),
|
|
183
|
+
);
|
|
184
|
+
if (current) this.#forget(current);
|
|
185
|
+
this.#remember(place);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#remember(place: StoredPlace): void {
|
|
189
|
+
this.#byOwner.set(ownerKey(place.kind, place.name, place.surface), place);
|
|
190
|
+
this.#byId.set(idKey(place.surface, place.externalId), place);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
#forget(place: StoredPlace): void {
|
|
194
|
+
this.#byOwner.delete(ownerKey(place.kind, place.name, place.surface));
|
|
195
|
+
this.#byId.delete(idKey(place.surface, place.externalId));
|
|
196
|
+
}
|
|
197
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { readCharacter } from "../../characters/index.ts";
|
|
2
2
|
import { scopeToGuild } from "../db/guild-scope.ts";
|
|
3
|
+
import { convergeLegacyChannelPlaces } from "../db/legacy-channels.ts";
|
|
3
4
|
import type { Migration } from "../db/migrations.ts";
|
|
4
5
|
import type { ThinkingLevel } from "../domain/models.ts";
|
|
5
6
|
import { promptHistoryMigration } from "../modules/prompts/prompt-history.ts";
|
|
@@ -113,18 +114,36 @@ export const agentsMigration: Migration = {
|
|
|
113
114
|
},
|
|
114
115
|
};
|
|
115
116
|
|
|
117
|
+
/** The table of stored places: one row per agent or group per surface, under the team's id. */
|
|
118
|
+
export const agentPlacesMigration: Migration = {
|
|
119
|
+
name: "agent-places",
|
|
120
|
+
up: async (sql) => {
|
|
121
|
+
await sql`
|
|
122
|
+
CREATE TABLE IF NOT EXISTS agent_places (
|
|
123
|
+
team_id text NOT NULL,
|
|
124
|
+
kind text NOT NULL CHECK (kind IN ('agent', 'group')),
|
|
125
|
+
name text NOT NULL,
|
|
126
|
+
surface text NOT NULL,
|
|
127
|
+
external_id text NOT NULL,
|
|
128
|
+
created_at timestamptz NOT NULL DEFAULT now(),
|
|
129
|
+
PRIMARY KEY (team_id, kind, name, surface),
|
|
130
|
+
UNIQUE (team_id, surface, external_id)
|
|
131
|
+
)`;
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
|
|
116
135
|
/**
|
|
117
136
|
* The migrations of the store's tables for the guild the process serves, or the local team
|
|
118
|
-
* without Discord: the tables, then each row's guild, with existing rows given `
|
|
137
|
+
* without Discord: the tables, then each row's guild, with existing rows given `teamId`, then
|
|
119
138
|
* the group channel made optional.
|
|
120
139
|
*/
|
|
121
|
-
export function agentMigrations(
|
|
140
|
+
export function agentMigrations(teamId: string): Migration[] {
|
|
122
141
|
return [
|
|
123
142
|
agentsMigration,
|
|
124
143
|
{
|
|
125
144
|
name: "agents-guild",
|
|
126
145
|
up: (sql) =>
|
|
127
|
-
scopeToGuild(sql,
|
|
146
|
+
scopeToGuild(sql, teamId, [
|
|
128
147
|
{ table: "agents", key: ["name"] },
|
|
129
148
|
{ table: "agent_groups", key: ["name"] },
|
|
130
149
|
{ table: "agent_group_messages", key: ["id"] },
|
|
@@ -178,5 +197,20 @@ export function agentMigrations(guildId: string): Migration[] {
|
|
|
178
197
|
},
|
|
179
198
|
// Whom a preset is shared with, besides the agent that saved it.
|
|
180
199
|
HelperPresets.sharingMigration,
|
|
200
|
+
// The places of agents and groups on the surfaces that make or bind their own; stored places only.
|
|
201
|
+
agentPlacesMigration,
|
|
181
202
|
];
|
|
182
203
|
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Until 1.0 the channel columns stay the source a rollback reads: the table follows them at every
|
|
207
|
+
* boot, for this team's rows. Declared after `moveTeam`, which moves the rows of a former id in
|
|
208
|
+
* with their channel ids, so the rows it moved get their places at that same boot.
|
|
209
|
+
*/
|
|
210
|
+
export function agentPlacesConvergence(teamId: string): Migration {
|
|
211
|
+
return {
|
|
212
|
+
name: "agent-places-from-channels",
|
|
213
|
+
runs: "every-boot",
|
|
214
|
+
up: convergeLegacyChannelPlaces(teamId),
|
|
215
|
+
};
|
|
216
|
+
}
|