pi-roundtable 0.36.0 → 0.37.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 +11 -0
- package/docs/plugins.md +2 -0
- package/package.json +1 -1
- package/src/core/agents/agent-team.ts +12 -15
- package/src/core/agents/helper-ops.ts +13 -0
- package/src/core/agents/helper-preset-ops.ts +1 -1
- package/src/core/agents/helper-preset-overview.ts +80 -0
- package/src/core/agents/helper-presets.ts +74 -0
- package/src/index.ts +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,17 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|
|
5
5
|
|
|
6
6
|
## [Unreleased]
|
|
7
7
|
|
|
8
|
+
## [0.37.1] - 2026-11-07
|
|
9
|
+
|
|
10
|
+
- Release in lockstep with pi-roundtable-web 0.37.1 (an archived temp knight's or a dismissed thread's unread count leaves the tab title and dot without a reload); no core behavior changes.
|
|
11
|
+
|
|
12
|
+
## [0.37.0] - 2026-11-06
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- `AgentTeam.allPresets()` lists every preset of every active knight that is no temp knight, by knight and then name, each with `sharedWith` and `recruits`: the knights that made temp knights from it, each with how many are active and archived and when the latest was made (`last`). `PresetOverview` and `PresetRecruits` are exported.
|
|
17
|
+
- `AgentTeam.transferPreset(agent, name, to)` gives a preset to another active knight, keeping its content and sharing. The new owner leaves the shared list. The old owner joins it when the preset was shared with none or with a list, so the old owner can still recruit from it; the owner removes it from the list to take that away. A preset shared with every knight stays so. The transfer refuses the same knight, a name the new owner already has, a new owner at 20 presets, and an unknown preset. Temp knights made from the preset before keep the name they recorded, so `allPresets` does not count them among the moved preset's recruits.
|
|
18
|
+
|
|
8
19
|
## [0.36.0] - 2026-11-06
|
|
9
20
|
|
|
10
21
|
### Added
|
package/docs/plugins.md
CHANGED
|
@@ -4535,6 +4535,8 @@ Import from the entries listed below; source area files are internal.
|
|
|
4535
4535
|
| `HeldCall` | `pi-roundtable` | type |
|
|
4536
4536
|
| `HelperPreset` | `pi-roundtable` | type |
|
|
4537
4537
|
| `OwnedPreset` | `pi-roundtable` | type |
|
|
4538
|
+
| `PresetOverview` | `pi-roundtable` | type |
|
|
4539
|
+
| `PresetRecruits` | `pi-roundtable` | type |
|
|
4538
4540
|
| `PresetSharing` | `pi-roundtable` | type |
|
|
4539
4541
|
| `UsablePreset` | `pi-roundtable` | type |
|
|
4540
4542
|
| `HelperTeam` | `pi-roundtable` | type |
|
package/package.json
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
} from "./agent-tools.ts";
|
|
20
20
|
import type { HelperTeam } from "./helper-ops.ts";
|
|
21
21
|
import { ownerPresets, presetAction } from "./helper-preset-ops.ts";
|
|
22
|
+
import { allPresets, transferPreset } from "./helper-preset-overview.ts";
|
|
22
23
|
import { type Routes, routesOf } from "./routes.ts";
|
|
23
24
|
import { TeamEditing } from "./team-editing.ts";
|
|
24
25
|
import type { TeamHelpers } from "./team-helpers.ts";
|
|
@@ -408,21 +409,17 @@ export class DiscordAgentTeam implements AgentOps, AgentTeam {
|
|
|
408
409
|
}
|
|
409
410
|
|
|
410
411
|
/** An owner manages a knight's presets; see `HelperTeam.presets`. */
|
|
411
|
-
presets
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
deletePreset(agent: string, name: string): Promise<boolean> {
|
|
424
|
-
return ownerPresets(this.#context).delete(agent, name);
|
|
425
|
-
}
|
|
412
|
+
readonly presets: HelperTeam["presets"] = (agent) =>
|
|
413
|
+
ownerPresets(this.#context).list(agent);
|
|
414
|
+
readonly usablePresets: HelperTeam["usablePresets"] = (agent) =>
|
|
415
|
+
ownerPresets(this.#context).usable(agent);
|
|
416
|
+
readonly savePreset: HelperTeam["savePreset"] = (...args) =>
|
|
417
|
+
ownerPresets(this.#context).save(...args);
|
|
418
|
+
readonly deletePreset: HelperTeam["deletePreset"] = (agent, name) =>
|
|
419
|
+
ownerPresets(this.#context).delete(agent, name);
|
|
420
|
+
readonly allPresets = () => allPresets(this.#context);
|
|
421
|
+
readonly transferPreset: HelperTeam["transferPreset"] = (...args) =>
|
|
422
|
+
transferPreset(this.#context, ...args);
|
|
426
423
|
|
|
427
424
|
/** A knight archives its own temp knights; the pages that show the parent are told. */
|
|
428
425
|
helperEnd(
|
|
@@ -2,6 +2,7 @@ import type { Agent } from "./agent-rules.ts";
|
|
|
2
2
|
import type {
|
|
3
3
|
HelperPreset,
|
|
4
4
|
OwnedPreset,
|
|
5
|
+
PresetOverview,
|
|
5
6
|
PresetSharing,
|
|
6
7
|
UsablePreset,
|
|
7
8
|
} from "./helper-presets.ts";
|
|
@@ -62,4 +63,16 @@ export interface HelperTeam {
|
|
|
62
63
|
): Promise<{ preset: OwnedPreset; replaced: boolean }>;
|
|
63
64
|
/** Deletes one of a knight's presets; says whether there was one. */
|
|
64
65
|
deletePreset(agent: string, name: string): Promise<boolean>;
|
|
66
|
+
/**
|
|
67
|
+
* Every preset of every active knight, by knight and then name, each with whom it is shared and
|
|
68
|
+
* the knights that recruited temp knights from it. Throws AgentError when temp knights are off.
|
|
69
|
+
*/
|
|
70
|
+
allPresets(): Promise<PresetOverview[]>;
|
|
71
|
+
/**
|
|
72
|
+
* Moves a knight's preset to another active knight that is no temp knight, keeping its content
|
|
73
|
+
* and sharing: `to` leaves the shared list, and the old owner joins it unless the preset is
|
|
74
|
+
* shared with every knight. Refuses the same knight, a name `to` already has, `to` at 20
|
|
75
|
+
* presets, and an unknown preset. Temp knights made from it keep the name they recorded.
|
|
76
|
+
*/
|
|
77
|
+
transferPreset(agent: string, name: string, to: string): Promise<OwnedPreset>;
|
|
65
78
|
}
|
|
@@ -204,7 +204,7 @@ export function validatePreset(
|
|
|
204
204
|
* The knight whose presets an owner manages from outside any agent turn: refused when temp knights
|
|
205
205
|
* are off, for an unknown or archived agent, and for a temp knight, which keeps none.
|
|
206
206
|
*/
|
|
207
|
-
function presetOwner(ctx: TeamContext, name: string): Agent {
|
|
207
|
+
export function presetOwner(ctx: TeamContext, name: string): Agent {
|
|
208
208
|
if (!helpersOf(ctx.options.helpers))
|
|
209
209
|
throw new AgentError("Temp knights are switched off on this host.");
|
|
210
210
|
const agent = ctx.options.store.activeAgent(name);
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { helpersOf } from "../config/helpers.ts";
|
|
2
|
+
import { AgentError } from "../domain/errors.ts";
|
|
3
|
+
import { isHelper } from "./agent-rules.ts";
|
|
4
|
+
import { presetOwner } from "./helper-preset-ops.ts";
|
|
5
|
+
import type {
|
|
6
|
+
OwnedPreset,
|
|
7
|
+
PresetOverview,
|
|
8
|
+
PresetRecruits,
|
|
9
|
+
} from "./helper-presets.ts";
|
|
10
|
+
import type { TeamContext } from "./team-options.ts";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Every preset of an active knight, with the knights that recruited from it: a temp knight
|
|
14
|
+
* records its own parent's preset by name and another's as `<agent>/<name>`.
|
|
15
|
+
*/
|
|
16
|
+
export async function allPresets(ctx: TeamContext): Promise<PresetOverview[]> {
|
|
17
|
+
if (!helpersOf(ctx.options.helpers))
|
|
18
|
+
throw new AgentError("Temp knights are switched off on this host.");
|
|
19
|
+
const { store } = ctx.options;
|
|
20
|
+
const [presets, rows] = await Promise.all([
|
|
21
|
+
store.presets.all(),
|
|
22
|
+
store.presets.recruits(),
|
|
23
|
+
]);
|
|
24
|
+
const byPreset = new Map<string, Map<string, PresetRecruits>>();
|
|
25
|
+
for (const row of rows) {
|
|
26
|
+
const slash = row.preset.indexOf("/");
|
|
27
|
+
const key = slash < 0 ? `${row.parent}/${row.preset}` : row.preset;
|
|
28
|
+
const knights = byPreset.get(key) ?? new Map<string, PresetRecruits>();
|
|
29
|
+
byPreset.set(key, knights);
|
|
30
|
+
const seen = knights.get(row.parent) ?? {
|
|
31
|
+
knight: row.parent,
|
|
32
|
+
active: 0,
|
|
33
|
+
archived: 0,
|
|
34
|
+
};
|
|
35
|
+
knights.set(row.parent, seen);
|
|
36
|
+
seen[row.status] += row.count;
|
|
37
|
+
const last = row.last?.toISOString();
|
|
38
|
+
if (last && (!seen.last || last > seen.last)) seen.last = last;
|
|
39
|
+
}
|
|
40
|
+
return presets
|
|
41
|
+
.filter((preset) => {
|
|
42
|
+
const agent = store.agent(preset.agent);
|
|
43
|
+
return agent?.status === "active" && !isHelper(agent);
|
|
44
|
+
})
|
|
45
|
+
.map((preset) => ({
|
|
46
|
+
...preset,
|
|
47
|
+
recruits: [
|
|
48
|
+
...(byPreset.get(`${preset.agent}/${preset.name}`)?.values() ?? []),
|
|
49
|
+
].sort((a, b) => a.knight.localeCompare(b.knight)),
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Moves a knight's preset to another active knight, keeping its content and sharing: the new
|
|
55
|
+
* agent leaves the shared list, and the old one joins it unless the preset is shared with all.
|
|
56
|
+
*/
|
|
57
|
+
export async function transferPreset(
|
|
58
|
+
ctx: TeamContext,
|
|
59
|
+
agent: string,
|
|
60
|
+
name: string,
|
|
61
|
+
to: string,
|
|
62
|
+
): Promise<OwnedPreset> {
|
|
63
|
+
const from = presetOwner(ctx, agent).name;
|
|
64
|
+
if (to === from)
|
|
65
|
+
throw new AgentError(`The preset "${name}" is already ${from}'s.`);
|
|
66
|
+
const target = ctx.options.store.activeAgent(to);
|
|
67
|
+
if (isHelper(target))
|
|
68
|
+
throw new AgentError(`"${to}" is a temp knight and keeps no presets.`);
|
|
69
|
+
const { presets } = ctx.options.store;
|
|
70
|
+
const preset = await presets.get(from, name);
|
|
71
|
+
if (!preset) throw new AgentError(`${from} has no preset "${name}".`);
|
|
72
|
+
const shared = preset.sharedWith;
|
|
73
|
+
const sharing =
|
|
74
|
+
shared === "all"
|
|
75
|
+
? ["*"]
|
|
76
|
+
: [...new Set([...(shared ?? []), from])].filter((n) => n !== to);
|
|
77
|
+
if (!(await presets.transfer(from, name, to, sharing)))
|
|
78
|
+
throw new AgentError(`${from} has no preset "${name}".`);
|
|
79
|
+
return (await presets.get(to, name)) ?? preset;
|
|
80
|
+
}
|
|
@@ -59,6 +59,31 @@ const toPreset = (row: Row): UsablePreset => ({
|
|
|
59
59
|
: {}),
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
+
/** The temp knights one knight recruited from a preset, by their status. */
|
|
63
|
+
export interface PresetRecruits {
|
|
64
|
+
/** The knight that made them. */
|
|
65
|
+
knight: string;
|
|
66
|
+
active: number;
|
|
67
|
+
archived: number;
|
|
68
|
+
/** When the latest of them was made, as an ISO time. */
|
|
69
|
+
last?: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** A preset as the presets page lists it: with its agent and who recruited from it. */
|
|
73
|
+
export interface PresetOverview extends UsablePreset {
|
|
74
|
+
/** The knights that made temp knights from it, by name. */
|
|
75
|
+
recruits: PresetRecruits[];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The temp knights of one parent made from one recorded preset, with one status. */
|
|
79
|
+
export interface RecruitRow {
|
|
80
|
+
parent: string;
|
|
81
|
+
preset: string;
|
|
82
|
+
status: "active" | "archived";
|
|
83
|
+
count: number;
|
|
84
|
+
last: Date | null;
|
|
85
|
+
}
|
|
86
|
+
|
|
62
87
|
const owned = ({ agent: _agent, ...preset }: UsablePreset): OwnedPreset =>
|
|
63
88
|
preset;
|
|
64
89
|
|
|
@@ -172,6 +197,55 @@ export class HelperPresets {
|
|
|
172
197
|
});
|
|
173
198
|
}
|
|
174
199
|
|
|
200
|
+
/** Every agent's presets, by agent and then name. */
|
|
201
|
+
async all(): Promise<UsablePreset[]> {
|
|
202
|
+
const rows: Row[] = await this.#sql`
|
|
203
|
+
SELECT * FROM helper_presets WHERE guild_id = ${this.#guild} ORDER BY agent, name`;
|
|
204
|
+
return rows.map(toPreset);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** The temp knights made from a preset, counted by parent, recorded preset, and status. */
|
|
208
|
+
recruits(): Promise<RecruitRow[]> {
|
|
209
|
+
return this.#sql`
|
|
210
|
+
SELECT parent, preset, status, count(*)::int AS count, max(created_at) AS last
|
|
211
|
+
FROM agents
|
|
212
|
+
WHERE guild_id = ${this.#guild} AND parent IS NOT NULL AND preset IS NOT NULL
|
|
213
|
+
GROUP BY parent, preset, status`;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Moves a preset to another agent with the sharing given, refusing when that agent has one of
|
|
218
|
+
* the name or the cap of presets; says whether there was a preset to move.
|
|
219
|
+
*/
|
|
220
|
+
async transfer(
|
|
221
|
+
from: string,
|
|
222
|
+
name: string,
|
|
223
|
+
to: string,
|
|
224
|
+
sharing: string[],
|
|
225
|
+
): Promise<boolean> {
|
|
226
|
+
const guild = this.#guild;
|
|
227
|
+
return this.#sql.begin(async (tx) => {
|
|
228
|
+
// The lock `set` takes for the receiving agent, so the cap holds against its saves.
|
|
229
|
+
await tx`SELECT pg_advisory_xact_lock(hashtext(${`helper-presets:${guild}:${to}`}))`;
|
|
230
|
+
const [counted]: { total: number; same: number }[] = await tx`
|
|
231
|
+
SELECT count(*)::int AS total, count(*) FILTER (WHERE name = ${name})::int AS same
|
|
232
|
+
FROM helper_presets WHERE guild_id = ${guild} AND agent = ${to}`;
|
|
233
|
+
if ((counted?.same ?? 0) > 0)
|
|
234
|
+
throw new AgentError(`${to} already has a preset "${name}".`);
|
|
235
|
+
if ((counted?.total ?? 0) >= MAX_HELPER_PRESETS)
|
|
236
|
+
throw new AgentError(
|
|
237
|
+
`${to} has ${MAX_HELPER_PRESETS} presets, the most one may keep; it must delete one first.`,
|
|
238
|
+
);
|
|
239
|
+
const moved = await tx`
|
|
240
|
+
UPDATE helper_presets
|
|
241
|
+
SET agent = ${to}, updated_at = now(),
|
|
242
|
+
shared_with = ${sharing.length > 0 ? sharing : null}::jsonb
|
|
243
|
+
WHERE guild_id = ${guild} AND agent = ${from} AND name = ${name}
|
|
244
|
+
RETURNING name`;
|
|
245
|
+
return moved.length > 0;
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
|
|
175
249
|
/** Deletes one of the agent's presets; says whether there was one. */
|
|
176
250
|
async delete(agent: string, name: string): Promise<boolean> {
|
|
177
251
|
const rows = await this.#sql`
|