briyah 2.0.6 → 2.0.7

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.
Files changed (40) hide show
  1. package/README.md +105 -0
  2. package/data/packs/dnd5e/pack.json +7 -1
  3. package/dist-sdk/server/src/ai/agent.d.ts +27 -0
  4. package/dist-sdk/server/src/ai/agent.js +54 -13
  5. package/dist-sdk/server/src/app/user-service-factory.js +3 -0
  6. package/dist-sdk/server/src/app.controller.d.ts +7 -1
  7. package/dist-sdk/server/src/app.controller.js +55 -0
  8. package/dist-sdk/server/src/app.service.d.ts +114 -2
  9. package/dist-sdk/server/src/app.service.js +181 -4
  10. package/dist-sdk/server/src/documents/document-conversion.d.ts +70 -0
  11. package/dist-sdk/server/src/documents/document-conversion.js +341 -0
  12. package/dist-sdk/server/src/documents/library/chunking.d.ts +65 -0
  13. package/dist-sdk/server/src/documents/library/chunking.js +263 -0
  14. package/dist-sdk/server/src/documents/library/document-guide.builder.d.ts +2 -0
  15. package/dist-sdk/server/src/documents/library/document-guide.builder.js +58 -0
  16. package/dist-sdk/server/src/documents/library/document-registry.d.ts +22 -0
  17. package/dist-sdk/server/src/documents/library/document-registry.js +44 -0
  18. package/dist-sdk/server/src/documents/library/document.types.d.ts +108 -0
  19. package/dist-sdk/server/src/documents/library/document.types.js +20 -0
  20. package/dist-sdk/server/src/documents/library/embedding.d.ts +57 -0
  21. package/dist-sdk/server/src/documents/library/embedding.js +151 -0
  22. package/dist-sdk/server/src/packs/pack-manifest.d.ts +46 -0
  23. package/dist-sdk/server/src/packs/pack-manifest.js +46 -1
  24. package/dist-sdk/server/src/room/artifact-store.service.js +44 -11
  25. package/dist-sdk/server/src/room/artifact.d.ts +8 -0
  26. package/dist-sdk/server/src/room/room.js +7 -1
  27. package/dist-sdk/server/src/rpg/rpg-roster.d.ts +70 -29
  28. package/dist-sdk/server/src/rpg/rpg-roster.js +152 -41
  29. package/dist-sdk/server/src/rpg/rpg.service.d.ts +66 -29
  30. package/dist-sdk/server/src/rpg/rpg.service.js +140 -65
  31. package/dist-sdk/server/src/sdk/index.d.ts +12 -2
  32. package/dist-sdk/server/src/sdk/index.js +27 -1
  33. package/dist-sdk/server/src/tools/core/document.tools.d.ts +39 -0
  34. package/dist-sdk/server/src/tools/core/document.tools.js +391 -0
  35. package/dist-sdk/server/src/tools/core/index.js +2 -1
  36. package/dist-sdk/server/src/tools/native-tool.types.d.ts +37 -0
  37. package/dist-sdk/server/src/tools/tool-execution.service.d.ts +23 -2
  38. package/dist-sdk/server/src/tools/tool-execution.service.js +30 -6
  39. package/dist-sdk/shared/types/app.types.d.ts +39 -5
  40. package/package.json +172 -167
@@ -1,71 +1,182 @@
1
1
  "use strict";
2
2
  /**
3
- * What a campaign's party may be, decided before anything is created.
3
+ * What a table is, and what a party may be.
4
4
  *
5
- * Pure on purpose: no filesystem, no Nest, no `AppService`. Every rule here is one
6
- * the room would otherwise enforce badly or late, and the whole point is to refuse a
7
- * party the room could not route while refusing costs nothing.
5
+ * A table is one installed pack's `table` block — see {@link PackTable} — resolved
6
+ * against the templates it names. That resolution is the whole of this module: the
7
+ * pack declares which room to build and which agent template is a seat, and
8
+ * everything else the RPG page needs is derived from those two, so a pack never says
9
+ * the same thing twice. The prompt folder comes from the seat template; the crew and
10
+ * which of them are expensive come from the room template; the seat placeholders come
11
+ * from the pack's noun.
12
+ *
13
+ * No filesystem and no Nest: a {@link LoadedPack} arrives already read, and every rule
14
+ * about the shape of a party is one the room would otherwise enforce badly or late.
15
+ * The point of refusing here is that refusing costs nothing.
8
16
  */
9
17
  Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.RPG_SEAT_NAMES = exports.RPG_MAX_PLAYERS = exports.RPG_PLAYER_AGENT_KEY = exports.RPG_PLAYER_FOLDER = exports.RPG_ROOM_KEY = exports.RPG_PACK = void 0;
18
+ exports.seatNames = seatNames;
19
+ exports.resolveRpgTable = resolveRpgTable;
20
+ exports.toRpgPackOption = toRpgPackOption;
21
+ exports.pickDefaultTable = pickDefaultTable;
11
22
  exports.assertSeatable = assertSeatable;
23
+ const prompt_resolver_1 = require("../config/prompt-resolver");
24
+ const pack_manifest_1 = require("../packs/pack-manifest");
12
25
  const errors_1 = require("../common/errors");
13
- /** The pack a campaign is built from. */
14
- exports.RPG_PACK = 'dnd5e';
15
- /** The pack's player-less room template — the crew, and no seats. */
16
- exports.RPG_ROOM_KEY = 'campaign';
17
- /** The prompt folder every player is created from, pack-qualified. */
18
- exports.RPG_PLAYER_FOLDER = `${exports.RPG_PACK}:dnd_player`;
19
- /** The pack agent template a player is filed under, and the roster discriminator. */
20
- exports.RPG_PLAYER_AGENT_KEY = 'player';
26
+ const logger_1 = require("../common/logger");
27
+ /**
28
+ * Seat ordinals, so a placeholder reads as a name rather than an index.
29
+ *
30
+ * Runs out before `MAX_TABLE_SEATS` does on purpose: past eight the word stops being
31
+ * easier to read than the numeral, and {@link seatNames} falls back to it.
32
+ */
33
+ const SEAT_ORDINALS = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight'];
21
34
  /**
22
- * How many players one table seats.
35
+ * The seats a table of this size offers.
36
+ *
37
+ * Generated rather than listed, so a pack can ask for any number of seats without a
38
+ * matching list of names having to exist. Unique by construction, which is what the
39
+ * room needs: two agents in one room may not share a nickname, and a nickname is
40
+ * derived from the name.
23
41
  *
24
- * Not a technical limit — the room does not care. Six is a table, and every player
25
- * beyond it is another agent perceiving every message.
42
+ * @param noun What one player is called
43
+ * @param count How many seats
44
+ * @returns The placeholders, in seating order
26
45
  */
27
- exports.RPG_MAX_PLAYERS = 6;
46
+ function seatNames(noun, count) {
47
+ return Array.from({ length: count }, (_, index) => `${noun} ${SEAT_ORDINALS[index] ?? index + 1}`);
48
+ }
28
49
  /**
29
- * The names a party is seated under, in order.
50
+ * Resolves a pack's declared table.
30
51
  *
31
- * A seat is a placeholder, not a character. Players choose a character during
32
- * character creation and are renamed to it then — which is the only point at which
33
- * they know what the classes and races on offer are. Generated here rather than
34
- * asked for, so they are unique by construction and nothing has to be refused.
52
+ * @param pack A pack, already read
53
+ * @returns The table, or null when the pack declares none, or names a template it does
54
+ * not ship, or points at a room that already seats a player. Null is the ordinary
55
+ * case — every pack that is not a game is one — so only the misconfigurations warn.
35
56
  */
36
- exports.RPG_SEAT_NAMES = [
37
- 'Player One',
38
- 'Player Two',
39
- 'Player Three',
40
- 'Player Four',
41
- 'Player Five',
42
- 'Player Six',
43
- ];
57
+ function resolveRpgTable(pack) {
58
+ if (!pack.table)
59
+ return null;
60
+ const { room: roomKey, playerAgent: playerAgentKey, playerNoun, maxPlayers } = pack.table;
61
+ const room = pack.rooms.find((template) => template.key === roomKey);
62
+ if (!room) {
63
+ logger_1.logger.warn(`Pack "${pack.name}" declares its table in room "${roomKey}", which it does not ship`);
64
+ return null;
65
+ }
66
+ const player = pack.agents.find((template) => template.key === playerAgentKey);
67
+ if (!player) {
68
+ logger_1.logger.warn(`Pack "${pack.name}" declares "${playerAgentKey}" as its player, which it does not ship`);
69
+ return null;
70
+ }
71
+ // The RPG page seats the party itself, one agent at a time, so each AI player can
72
+ // have its own model and personality. A room template that already seats a player
73
+ // would bring the party twice — once under a name the page did not choose and does
74
+ // not show in the roster.
75
+ if (room.agents.some((entry) => entry.key === playerAgentKey)) {
76
+ logger_1.logger.warn(`Pack "${pack.name}" declares room "${roomKey}" as its table, but that template seats ` +
77
+ `"${playerAgentKey}". The RPG page seats the party itself, so its table has to be player-less.`);
78
+ return null;
79
+ }
80
+ const { primaryRoles, economyRoles } = crewRoles(pack, room);
81
+ return {
82
+ pack: pack.name,
83
+ title: pack.title,
84
+ ...(pack.description && { description: pack.description }),
85
+ roomKey,
86
+ playerAgentKey,
87
+ playerFolder: (0, prompt_resolver_1.isPackFolder)(player.promptFolder)
88
+ ? player.promptFolder
89
+ : (0, prompt_resolver_1.formatPromptFolder)({ pack: pack.name, folder: player.promptFolder }),
90
+ playerOverrides: player.overrides ?? {},
91
+ playerNoun,
92
+ seatNames: seatNames(playerNoun, maxPlayers),
93
+ primaryRoles,
94
+ economyRoles,
95
+ };
96
+ }
97
+ /** A table as the client is offered one: the labels, and none of the keys. */
98
+ function toRpgPackOption(table, isDefault) {
99
+ return {
100
+ pack: table.pack,
101
+ title: table.title,
102
+ ...(table.description && { description: table.description }),
103
+ playerNoun: table.playerNoun,
104
+ seatNames: table.seatNames,
105
+ primaryRoles: table.primaryRoles,
106
+ economyRoles: table.economyRoles,
107
+ isDefault,
108
+ };
109
+ }
110
+ /**
111
+ * The table to preselect: the one named by DEFAULT_RPG_PACK, else the first.
112
+ *
113
+ * Mirrors `pickDefaultEntry`, down to warning about a name that is not installed
114
+ * rather than offering nothing — a host who mistyped the variable should still get a
115
+ * working dialog.
116
+ *
117
+ * @param tables Every table this deployment offers, in the order they are listed
118
+ * @returns The one to preselect, or null when the deployment ships no game pack
119
+ */
120
+ function pickDefaultTable(tables) {
121
+ if (tables.length === 0)
122
+ return null;
123
+ const fromEnv = process.env.DEFAULT_RPG_PACK?.trim();
124
+ if (fromEnv) {
125
+ const named = tables.find((table) => table.pack.toLowerCase() === fromEnv.toLowerCase());
126
+ if (named)
127
+ return named;
128
+ logger_1.logger.warn(`DEFAULT_RPG_PACK "${fromEnv}" is not an installed game pack; using the first`);
129
+ }
130
+ return tables[0];
131
+ }
44
132
  /**
45
133
  * Refuses a party the room could not seat, before a single agent exists.
46
134
  *
47
- * Much smaller than it was. It used to police character names — length, punctuation,
48
- * collisions with the crew and with each other — because the party was named here.
49
- * Now the party is seated under {@link RPG_SEAT_NAMES} and named in the game, so the
50
- * only collision left is one a player invents at the table, and
51
- * `Room.renameAgent` refuses that where it happens.
135
+ * Every message uses the pack's own noun, because "a table seats 4 students" is a rule
136
+ * of the game the user chose rather than a limit of the software.
52
137
  *
53
138
  * @param players The requested party, in seating order
139
+ * @param table The table they are being seated at
54
140
  * @throws {ValidationError} Naming the offending seat and why
55
141
  */
56
- function assertSeatable(players) {
142
+ function assertSeatable(players, table) {
143
+ const one = table.playerNoun.toLowerCase();
57
144
  if (!players || players.length === 0) {
58
- throw new errors_1.ValidationError('A campaign needs at least one player.');
145
+ throw new errors_1.ValidationError(`A campaign needs at least one ${one}.`);
59
146
  }
60
- if (players.length > exports.RPG_MAX_PLAYERS) {
61
- throw new errors_1.ValidationError(`A table seats ${exports.RPG_MAX_PLAYERS} players.`);
147
+ if (players.length > table.seatNames.length) {
148
+ throw new errors_1.ValidationError(`A table seats ${table.seatNames.length} ${one}s.`);
62
149
  }
63
150
  if (!players.some((player) => player.isHuman)) {
64
- throw new errors_1.ValidationError('At least one player has to be a person.');
151
+ throw new errors_1.ValidationError(`At least one ${one} has to be a person.`);
65
152
  }
66
153
  players.forEach((player, index) => {
67
154
  if (!player.isHuman && !player.modelName?.trim()) {
68
- throw new errors_1.ValidationError(`${exports.RPG_SEAT_NAMES[index]} is an AI player and needs a model.`);
155
+ throw new errors_1.ValidationError(`${table.seatNames[index]} is an AI ${one} and needs a model.`);
69
156
  }
70
157
  });
71
158
  }
159
+ /**
160
+ * Which crew the quality tier's model runs, and which the economy one does.
161
+ *
162
+ * Labels rather than keys, and deduped in roster order: this is what the dialog says
163
+ * the quality choice buys, so it has to read as the names of the agents at the table.
164
+ * The role rule is `PackInstantiationService.modelFor`'s — a template naming no role is
165
+ * primary — repeated here rather than shared because that function answers with a
166
+ * model and this one with a name.
167
+ */
168
+ function crewRoles(pack, room) {
169
+ const byKey = new Map(pack.agents.map((agent) => [agent.key, agent]));
170
+ const primary = new Set();
171
+ const economy = new Set();
172
+ for (const entry of room.agents) {
173
+ const template = byKey.get(entry.key);
174
+ if (!template) {
175
+ logger_1.logger.warn(`Room "${room.key}" in pack "${pack.name}" names agent "${(0, pack_manifest_1.roomAgentKey)(entry)}", ` +
176
+ 'which the pack does not ship');
177
+ continue;
178
+ }
179
+ (template.modelRole === 'economy' ? economy : primary).add(template.label ?? template.key);
180
+ }
181
+ return { primaryRoles: [...primary], economyRoles: [...economy] };
182
+ }
@@ -1,19 +1,28 @@
1
1
  /**
2
- * Campaigns: a streamlined way to start and hold a fifth-edition game.
2
+ * Campaigns: a streamlined way to start and hold a game.
3
3
  *
4
- * A campaign is not a new kind of thing. It is an ordinary room built from the dnd5e
5
- * pack's `campaign` template — the Dungeon Master, Battle Master, Character Creator,
6
- * Campaign Designer and Table Moderator — with a party seated on top and the whole
7
- * thing published, so the link the party plays through exists from the first moment.
4
+ * A campaign is not a new kind of thing. It is an ordinary room built from a pack's
5
+ * table template — the crew that runs the game — with a party seated on top and the
6
+ * whole thing published, so the link the party plays through exists from the first
7
+ * moment.
8
+ *
9
+ * ## Which games exist
10
+ *
11
+ * Whichever installed packs say so. A pack declares a `table` block naming the room to
12
+ * build and the agent template to seat a party from, and {@link RpgTable} resolves that
13
+ * against its templates; this service asks for the list and never names a pack itself.
14
+ * That is what lets a host drop in a second ruleset — the dnd5e pack seats six players
15
+ * behind five crew agents, the acts pack four students behind three — without the RPG
16
+ * page learning anything about either.
8
17
  *
9
18
  * ## Why there is no campaign store
10
19
  *
11
20
  * Everything a campaign is, a room already holds: its roster, its turn mode, its
12
- * leader and moderator, its artifacts, its transcript and its cost. Which agent is a
13
- * player is `isControlledByHuman`; a character's personality is `privateContext`; a
14
- * crew member's role is `packAgentKey`. A store would be a second copy of all of that,
15
- * and it would drift the moment somebody renamed an agent from the Agents page. So a
16
- * campaign is discovered by filtering rooms, and its details are derived per request.
21
+ * leader and moderator, its artifacts, its transcript and its cost. Which pack it plays
22
+ * by is `pack`; which agent is a player is `packAgentKey`; a character's personality is
23
+ * `privateContext`. A store would be a second copy of all of that, and it would drift
24
+ * the moment somebody renamed an agent from the Agents page. So a campaign is
25
+ * discovered by filtering rooms, and its details are derived per request.
17
26
  *
18
27
  * ## Why every write goes through AppService
19
28
  *
@@ -22,7 +31,7 @@
22
31
  * uses — the same discipline `PackInstantiationService` keeps, and what makes the
23
32
  * rollback below correct rather than approximate.
24
33
  */
25
- import { CreateRpgCampaignRequest, CreateRpgCampaignResponse, RpgCampaignDetails, RpgCampaignInfo, RpgModelOption } from '../../../shared/types/app.types';
34
+ import { CreateRpgCampaignRequest, CreateRpgCampaignResponse, RpgCampaignDetails, RpgCampaignInfo, RpgModelOption, RpgPackOption } from '../../../shared/types/app.types';
26
35
  import { AppService } from '../app.service';
27
36
  import { UserConfigService } from '../config/user-config.service';
28
37
  import { UserPathsService } from '../config/user-paths.service';
@@ -36,6 +45,14 @@ export declare class RpgService {
36
45
  constructor(app: AppService, packs: PackService, userConfig: UserConfigService, userPaths: UserPathsService, userId: string);
37
46
  /** The models a campaign may be built with, as the client is offered them. */
38
47
  listModels(): RpgModelOption[];
48
+ /**
49
+ * The games a campaign may be built from, as the client is offered them.
50
+ *
51
+ * Installed rather than enabled, because {@link ensurePackEnabled} turns the chosen
52
+ * one on: a user on the RPG surface has no Packs page, so offering only what they
53
+ * had already enabled would offer them nothing at all on their first visit.
54
+ */
55
+ listPacks(): RpgPackOption[];
39
56
  /**
40
57
  * Builds a campaign and publishes it.
41
58
  *
@@ -43,14 +60,14 @@ export declare class RpgService {
43
60
  * before the first side effect; then the crew; then the party and the publish. Any
44
61
  * failure past the first agent deletes the room, which takes its agents with it.
45
62
  *
46
- * Synchronous throughout, unlike story creation: nothing in the dnd5e pack fires a
47
- * prompt at instantiation, so this is file I/O for a dozen agents. It either
48
- * returns a campaign that can be played or leaves nothing behind.
63
+ * Synchronous throughout, unlike story creation: no pack fires a prompt at
64
+ * instantiation, so this is file I/O for a dozen agents. It either returns a
65
+ * campaign that can be played or leaves nothing behind.
49
66
  *
50
- * @param request The campaign's name, quality tier and party
67
+ * @param request The campaign's name, game, quality tier and party
51
68
  * @returns The campaign, its public link, and anything that did not go to plan
52
- * @throws {ValidationError} If the party or the quality tier is not acceptable
53
- * @throws {NotFoundError} If this deployment does not ship the dnd5e pack
69
+ * @throws {ValidationError} If the game, the party or the quality tier is not acceptable
70
+ * @throws {NotFoundError} If this deployment ships no game pack at all
54
71
  */
55
72
  createCampaign(request: CreateRpgCampaignRequest): Promise<CreateRpgCampaignResponse>;
56
73
  /** Every campaign this user has, newest first. */
@@ -82,24 +99,44 @@ export declare class RpgService {
82
99
  /** The economy tier a human player rides, which is the cheapest seat available. */
83
100
  private economyModel;
84
101
  /**
85
- * Makes sure the pack a campaign is built from is available and on.
102
+ * Every game this deployment ships, in the order the client is offered them.
103
+ *
104
+ * Read from disk on every call, as `rpg_models.json` is, so a host who drops a pack
105
+ * in does not have to restart the server for it to appear.
106
+ */
107
+ private tables;
108
+ /**
109
+ * The table a request asked for, or the default.
110
+ *
111
+ * An unknown name is refused rather than falling back, for `resolveRpgModel`'s
112
+ * reason: a name this deployment does not have means the client and the packs
113
+ * directory disagree, and quietly building some other game is worse than saying so.
86
114
  *
87
- * Enabling it here rather than in `PackInstantiationService`: self-enabling
88
- * instantiation would change behaviour for every pack to suit one feature. A user
89
- * on the RPG surface asking for a campaign has unambiguously asked for the dnd5e
90
- * content, and has no Packs page to be sent to.
115
+ * @param packName A pack name, or absent to take the default
116
+ * @throws {NotFoundError} If this deployment ships no game pack at all
117
+ * @throws {ValidationError} If it ships some, but not that one
118
+ */
119
+ private requireTable;
120
+ /**
121
+ * Turns the chosen pack on if it is not already.
91
122
  *
92
- * @throws {NotFoundError} If the deployment does not ship the pack
123
+ * Here rather than in `PackInstantiationService`: self-enabling instantiation would
124
+ * change behaviour for every pack to suit one feature. A user on the RPG surface
125
+ * who picked a game has unambiguously asked for that pack's content, and has no
126
+ * Packs page to be sent to.
93
127
  */
94
- private ensurePackReady;
128
+ private ensurePackEnabled;
95
129
  /**
96
- * A room built from the campaign template.
130
+ * The table a room is a campaign of, or null when it is not one.
131
+ *
132
+ * Both halves matter: the pack alone would also match a game built by hand from the
133
+ * Packs page, which seats a player the RPG page did not create and does not know
134
+ * how to show.
97
135
  *
98
- * Both halves matter: `pack` alone would also match a table built by hand from the
99
- * Packs page, which has a player seat the RPG page did not create and does not
100
- * know how to show.
136
+ * @param room Any room
137
+ * @param tables The tables to match against, resolved by the caller
101
138
  */
102
- private isCampaign;
139
+ private tableFor;
103
140
  /** @throws {NotFoundError} If the room is missing or is not a campaign */
104
141
  private requireCampaign;
105
142
  /** The listing shape, including the published link the party plays through. */