briyah 2.0.4 → 2.0.5
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/README.md +12 -1
- package/data/common/config/markup +1 -1
- package/data/common/config/story_models.json +3 -3
- package/data/common/prompts/illustrator/describe_scene.prompt +4 -4
- package/data/common/prompts/perceive.prompt +1 -1
- package/data/packs/dnd5e/prompts/battle_master/perceive.prompt +1 -1
- package/data/packs/dnd5e/prompts/campaign_designer/perceive.prompt +1 -1
- package/data/packs/dnd5e/prompts/character_creator/perceive.prompt +795 -787
- package/data/packs/dnd5e/prompts/dungeon_master/defaults_config.json +2 -1
- package/data/packs/dnd5e/prompts/dungeon_master/perceive.prompt +1 -1
- package/data/packs/dnd5e/rooms/campaign.json +18 -15
- package/data/packs/dnd5e/rooms/table.json +17 -16
- package/dist-sdk/server/src/ai/LLM/anthropic.service.js +1 -1
- package/dist-sdk/server/src/ai/LLM/base-ai.service.d.ts +22 -0
- package/dist-sdk/server/src/ai/LLM/base-ai.service.js +60 -16
- package/dist-sdk/server/src/ai/LLM/blank-image.d.ts +36 -0
- package/dist-sdk/server/src/ai/LLM/blank-image.js +80 -0
- package/dist-sdk/server/src/ai/LLM/deepseek.service.js +1 -1
- package/dist-sdk/server/src/ai/LLM/fal.service.js +1 -1
- package/dist-sdk/server/src/ai/LLM/googleai.service.js +1 -1
- package/dist-sdk/server/src/ai/LLM/grok.service.js +1 -1
- package/dist-sdk/server/src/ai/LLM/openai.service.js +1 -1
- package/dist-sdk/server/src/ai/LLM/self-hosted.service.js +1 -1
- package/dist-sdk/server/src/ai/LLM/together.service.js +1 -1
- package/dist-sdk/server/src/ai/agent-config.d.ts +0 -1
- package/dist-sdk/server/src/ai/agent-store.service.js +2 -4
- package/dist-sdk/server/src/ai/agent.d.ts +1 -2
- package/dist-sdk/server/src/ai/agent.js +1 -3
- package/dist-sdk/server/src/app/user-service-factory.js +8 -2
- package/dist-sdk/server/src/app.controller.d.ts +33 -0
- package/dist-sdk/server/src/app.controller.js +93 -2
- package/dist-sdk/server/src/app.service.d.ts +49 -4
- package/dist-sdk/server/src/app.service.js +101 -16
- package/dist-sdk/server/src/auth/jwt-auth.guard.js +26 -0
- package/dist-sdk/server/src/packs/pack-instantiation.service.js +5 -0
- package/dist-sdk/server/src/packs/pack-manifest.d.ts +14 -0
- package/dist-sdk/server/src/packs/pack-manifest.js +11 -0
- package/dist-sdk/server/src/room/illustration/illustration.service.d.ts +211 -0
- package/dist-sdk/server/src/room/illustration/illustration.service.js +771 -0
- package/dist-sdk/server/src/room/illustration/illustration.types.d.ts +101 -0
- package/dist-sdk/server/src/room/illustration/illustration.types.js +2 -0
- package/dist-sdk/server/src/room/room-config.d.ts +58 -1
- package/dist-sdk/server/src/room/room-config.js +19 -0
- package/dist-sdk/server/src/room/room-factory.d.ts +8 -0
- package/dist-sdk/server/src/room/room-factory.js +11 -0
- package/dist-sdk/server/src/room/room-store.service.d.ts +8 -0
- package/dist-sdk/server/src/room/room-store.service.js +43 -2
- package/dist-sdk/server/src/room/room.d.ts +103 -1
- package/dist-sdk/server/src/room/room.js +240 -4
- package/dist-sdk/server/src/rpg/rpg.service.js +5 -1
- package/dist-sdk/server/src/story/story.service.d.ts +24 -65
- package/dist-sdk/server/src/story/story.service.js +105 -458
- package/dist-sdk/server/src/tools/core/illustrate.tool.d.ts +14 -0
- package/dist-sdk/server/src/tools/core/illustrate.tool.js +80 -0
- package/dist-sdk/server/src/tools/core/index.js +2 -1
- package/dist-sdk/server/src/tools/native-tool.types.d.ts +90 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/sheet.d.ts +9 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/engine/sheet.js +5 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/shared.d.ts +30 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/shared.js +75 -0
- package/dist-sdk/server/src/tools/packs/dnd5e/tools/sheet.tools.js +11 -2
- package/dist-sdk/server/src/tools/tool-execution.service.d.ts +36 -5
- package/dist-sdk/server/src/tools/tool-execution.service.js +91 -21
- package/dist-sdk/shared/types/app.types.d.ts +43 -0
- package/dist-sdk/shared/types/app.types.js +15 -1
- package/package.json +1 -1
|
@@ -59,7 +59,7 @@ class ToolExecutionService {
|
|
|
59
59
|
* @param room Access to the calling agent's room, exposed as helpers.room
|
|
60
60
|
* @returns The tool run result
|
|
61
61
|
*/
|
|
62
|
-
async runForAgent(name, contentJson, grantedNames, context, room) {
|
|
62
|
+
async runForAgent(name, contentJson, grantedNames, context, room, illustration) {
|
|
63
63
|
if (!grantedNames.includes(name)) {
|
|
64
64
|
return { ok: false, error: `Tool "${name}" is not available to you` };
|
|
65
65
|
}
|
|
@@ -75,7 +75,7 @@ class ToolExecutionService {
|
|
|
75
75
|
return { ok: false, error: 'Arguments must be a JSON object of named parameters' };
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
-
return this.runValidated(name, args, context, room);
|
|
78
|
+
return this.runValidated(name, args, context, room, illustration);
|
|
79
79
|
}
|
|
80
80
|
/**
|
|
81
81
|
* Runs a tool for the API test endpoint. Same validation as the agent path,
|
|
@@ -99,14 +99,14 @@ class ToolExecutionService {
|
|
|
99
99
|
* @param room Access to the calling agent's room, if any
|
|
100
100
|
* @returns The tool run result
|
|
101
101
|
*/
|
|
102
|
-
async runValidated(name, args, context, room) {
|
|
102
|
+
async runValidated(name, args, context, room, illustration) {
|
|
103
103
|
// Native tools take precedence and are always available.
|
|
104
104
|
const nativeTool = this.nativeRegistry.getTool(name);
|
|
105
105
|
if (nativeTool) {
|
|
106
|
-
const
|
|
107
|
-
if (
|
|
108
|
-
return { ok: false, error:
|
|
109
|
-
return this.execute(name, nativeTool.timeoutMs, (helpers) => nativeTool.handler(args, helpers, context), room);
|
|
106
|
+
const invalid = ToolExecutionService.validateParams(nativeTool.parameters, args);
|
|
107
|
+
if (invalid)
|
|
108
|
+
return { ok: false, error: invalid };
|
|
109
|
+
return this.execute(name, nativeTool.timeoutMs, nativeTool.maxResultChars, (helpers) => nativeTool.handler(args, helpers, context), room, illustration);
|
|
110
110
|
}
|
|
111
111
|
if (!this.allowUserAuthoredTools) {
|
|
112
112
|
return { ok: false, error: `Tool "${name}" does not exist` };
|
|
@@ -115,9 +115,9 @@ class ToolExecutionService {
|
|
|
115
115
|
if (!tool) {
|
|
116
116
|
return { ok: false, error: `Tool "${name}" does not exist` };
|
|
117
117
|
}
|
|
118
|
-
const
|
|
119
|
-
if (
|
|
120
|
-
return { ok: false, error:
|
|
118
|
+
const invalid = ToolExecutionService.validateParams(tool.parameters, args);
|
|
119
|
+
if (invalid)
|
|
120
|
+
return { ok: false, error: invalid };
|
|
121
121
|
let fn;
|
|
122
122
|
try {
|
|
123
123
|
fn = this.getCompiled(tool);
|
|
@@ -125,12 +125,35 @@ class ToolExecutionService {
|
|
|
125
125
|
catch (e) {
|
|
126
126
|
return { ok: false, error: `Tool code does not compile: ${e.message}` };
|
|
127
127
|
}
|
|
128
|
-
return this.execute(name, tool.timeoutMs, (helpers) => fn(args, helpers, context), room);
|
|
128
|
+
return this.execute(name, tool.timeoutMs, tool.maxResultChars, (helpers) => fn(args, helpers, context), room, illustration);
|
|
129
129
|
}
|
|
130
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Checks the LLM-supplied arguments against the declared parameters:
|
|
132
|
+
* required presence first, then any declared `enum`. Returns a ready-to-send
|
|
133
|
+
* error message, or null when the arguments are acceptable.
|
|
134
|
+
*
|
|
135
|
+
* An enum failure names the allowed values, so the agent can correct itself
|
|
136
|
+
* on its next turn rather than guessing again — the same self-correcting
|
|
137
|
+
* contract the rest of runForAgent uses. An omitted optional parameter is
|
|
138
|
+
* not enum-checked; `null` is treated as omitted, since models routinely
|
|
139
|
+
* send it for "not applicable".
|
|
140
|
+
* @param parameters The tool's declared parameters
|
|
141
|
+
* @param args The named arguments the agent supplied
|
|
142
|
+
* @returns An error message, or null when the arguments are acceptable
|
|
143
|
+
*/
|
|
144
|
+
static validateParams(parameters, args) {
|
|
131
145
|
for (const param of parameters || []) {
|
|
132
|
-
|
|
133
|
-
|
|
146
|
+
const value = args[param.name];
|
|
147
|
+
if (value === undefined) {
|
|
148
|
+
if (param.required)
|
|
149
|
+
return `Missing required parameter "${param.name}"`;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
if (value === null)
|
|
153
|
+
continue;
|
|
154
|
+
if (param.enum && param.enum.length > 0 && !param.enum.includes(String(value))) {
|
|
155
|
+
return `Parameter "${param.name}" must be one of: ${param.enum.join(', ')}`;
|
|
156
|
+
}
|
|
134
157
|
}
|
|
135
158
|
return null;
|
|
136
159
|
}
|
|
@@ -139,16 +162,18 @@ class ToolExecutionService {
|
|
|
139
162
|
* made through helpers.fetch are aborted when the timeout fires.
|
|
140
163
|
* @param name The tool name (for logging)
|
|
141
164
|
* @param toolTimeoutMs The tool's configured timeout, if any
|
|
165
|
+
* @param toolMaxResultChars The tool's configured result budget, if any
|
|
142
166
|
* @param invoke Runs the tool with the provided helpers
|
|
143
167
|
* @param room Access to the calling agent's room, omitted when there is none
|
|
144
168
|
* @returns The tool run result
|
|
145
169
|
*/
|
|
146
|
-
async execute(name, toolTimeoutMs, invoke, room) {
|
|
170
|
+
async execute(name, toolTimeoutMs, toolMaxResultChars, invoke, room, illustration) {
|
|
147
171
|
const timeoutMs = Math.min(toolTimeoutMs || DEFAULT_TIMEOUT_MS, MAX_TIMEOUT_MS);
|
|
148
172
|
const controller = new AbortController();
|
|
149
173
|
const helpers = {
|
|
150
174
|
fetch: (url, init) => fetch(url, { ...init, signal: controller.signal }),
|
|
151
175
|
...(room ? { room } : {}),
|
|
176
|
+
...(illustration ? { illustration } : {}),
|
|
152
177
|
...(this.packDocuments ? { packDocuments: this.packDocuments } : {}),
|
|
153
178
|
};
|
|
154
179
|
let timeoutHandle;
|
|
@@ -158,9 +183,10 @@ class ToolExecutionService {
|
|
|
158
183
|
reject(new Error(`Tool timed out after ${timeoutMs}ms`));
|
|
159
184
|
}, timeoutMs);
|
|
160
185
|
});
|
|
186
|
+
const maxResultChars = toolMaxResultChars && toolMaxResultChars > 0 ? toolMaxResultChars : MAX_RESULT_CHARS;
|
|
161
187
|
try {
|
|
162
188
|
const result = await Promise.race([invoke(helpers), timeout]);
|
|
163
|
-
return { ok: true, result: ToolExecutionService.truncateResult(result) };
|
|
189
|
+
return { ok: true, result: ToolExecutionService.truncateResult(result, maxResultChars) };
|
|
164
190
|
}
|
|
165
191
|
catch (e) {
|
|
166
192
|
logger_1.logger.error(`Tool "${name}" failed:`, e.message);
|
|
@@ -180,16 +206,60 @@ class ToolExecutionService {
|
|
|
180
206
|
return fn;
|
|
181
207
|
}
|
|
182
208
|
/**
|
|
183
|
-
* Caps oversized
|
|
209
|
+
* Caps an oversized result while keeping it valid JSON of the same shape.
|
|
210
|
+
*
|
|
211
|
+
* This used to serialize, slice at the budget, and return the resulting
|
|
212
|
+
* string. That handed the agent unparseable half-JSON with the explanation
|
|
213
|
+
* stranded at the end, past the cut — and because the object had become a
|
|
214
|
+
* string it also defeated Room.describeToolResult's `detail`/`summary`
|
|
215
|
+
* lookup, so the room log lost its readable line for exactly the results a
|
|
216
|
+
* person most wanted to read. Instead:
|
|
217
|
+
*
|
|
218
|
+
* - An object keeps its keys. Its longest string properties are clipped, in
|
|
219
|
+
* order, until the whole thing fits, and `_truncated`/`_originalChars`
|
|
220
|
+
* record what happened so the agent can say so or ask for less.
|
|
221
|
+
* - A bare string is clipped in place with the same marker appended.
|
|
222
|
+
* - Anything that cannot be clipped that way — a long array, say — is
|
|
223
|
+
* replaced by a small object saying so, which is at least parseable.
|
|
224
|
+
*
|
|
184
225
|
* @param result The raw tool result
|
|
185
|
-
* @
|
|
226
|
+
* @param maxChars The serialized-JSON budget for this tool
|
|
227
|
+
* @returns A result that serializes within budget
|
|
186
228
|
*/
|
|
187
|
-
static truncateResult(result) {
|
|
229
|
+
static truncateResult(result, maxChars) {
|
|
188
230
|
const serialized = JSON.stringify(result);
|
|
189
|
-
if (serialized === undefined || serialized.length <=
|
|
231
|
+
if (serialized === undefined || serialized.length <= maxChars) {
|
|
190
232
|
return result;
|
|
191
233
|
}
|
|
192
|
-
|
|
234
|
+
const originalChars = serialized.length;
|
|
235
|
+
const marker = `…(clipped from ${originalChars} characters)`;
|
|
236
|
+
if (typeof result === 'string') {
|
|
237
|
+
const keep = Math.max(0, maxChars - marker.length - 2);
|
|
238
|
+
return `${result.slice(0, keep)}${marker}`;
|
|
239
|
+
}
|
|
240
|
+
if (result && typeof result === 'object' && !Array.isArray(result)) {
|
|
241
|
+
const clipped = { ...result, _truncated: true, _originalChars: originalChars };
|
|
242
|
+
const stringKeys = Object.keys(clipped)
|
|
243
|
+
.filter((key) => typeof clipped[key] === 'string')
|
|
244
|
+
.sort((a, b) => clipped[b].length - clipped[a].length);
|
|
245
|
+
for (const key of stringKeys) {
|
|
246
|
+
const over = JSON.stringify(clipped).length - maxChars;
|
|
247
|
+
if (over <= 0)
|
|
248
|
+
break;
|
|
249
|
+
const value = clipped[key];
|
|
250
|
+
const keep = Math.max(0, value.length - over - marker.length);
|
|
251
|
+
clipped[key] = `${value.slice(0, keep)}${marker}`;
|
|
252
|
+
}
|
|
253
|
+
if (JSON.stringify(clipped).length <= maxChars) {
|
|
254
|
+
return clipped;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return {
|
|
258
|
+
_truncated: true,
|
|
259
|
+
_originalChars: originalChars,
|
|
260
|
+
error: `The result was ${originalChars} characters, over this tool's ${maxChars}-character ` +
|
|
261
|
+
'budget, and could not be clipped to fit. Call the tool again for less at a time.',
|
|
262
|
+
};
|
|
193
263
|
}
|
|
194
264
|
/** @returns The native tool definitions registered in this process. */
|
|
195
265
|
listNativeTools() {
|
|
@@ -114,6 +114,13 @@ export interface ToolParameter {
|
|
|
114
114
|
type: 'string' | 'number' | 'boolean' | 'object' | 'array';
|
|
115
115
|
description: string;
|
|
116
116
|
required: boolean;
|
|
117
|
+
/**
|
|
118
|
+
* The exact set of values this parameter accepts. Rendered into the agent's
|
|
119
|
+
* prompt beside the parameter and enforced before the tool runs, so a bad
|
|
120
|
+
* value comes back as a correctable error instead of reaching the handler.
|
|
121
|
+
* Compared case-sensitively against the string form of the argument.
|
|
122
|
+
*/
|
|
123
|
+
enum?: string[];
|
|
117
124
|
}
|
|
118
125
|
export interface ToolDefinition {
|
|
119
126
|
name: string;
|
|
@@ -121,6 +128,8 @@ export interface ToolDefinition {
|
|
|
121
128
|
parameters: ToolParameter[];
|
|
122
129
|
code: string;
|
|
123
130
|
timeoutMs?: number;
|
|
131
|
+
/** Per-call result budget in characters. See {@link NativeToolDefinition.maxResultChars}. */
|
|
132
|
+
maxResultChars?: number;
|
|
124
133
|
}
|
|
125
134
|
export interface ToolSummary {
|
|
126
135
|
name: string;
|
|
@@ -160,6 +169,31 @@ export interface RoomInfo {
|
|
|
160
169
|
* which is what lets a leader agent control the order of a turn-based room.
|
|
161
170
|
*/
|
|
162
171
|
export type RoomTurnMode = 'broadcast' | 'directed';
|
|
172
|
+
/**
|
|
173
|
+
* When a room asks its illustrator for a picture.
|
|
174
|
+
* - 'off': never.
|
|
175
|
+
* - 'on-request': only when an agent calls the `illustrate` tool. The default.
|
|
176
|
+
* - 'auto': also whenever the scene changes, which is how stories behave.
|
|
177
|
+
*
|
|
178
|
+
* 'auto' is opt-in rather than the default because a game master that restates
|
|
179
|
+
* the situation on every response — as the dnd5e Dungeon Master is told to —
|
|
180
|
+
* would otherwise buy an image on every tool call.
|
|
181
|
+
*/
|
|
182
|
+
export type RoomIllustrationMode = 'off' | 'on-request' | 'auto';
|
|
183
|
+
/**
|
|
184
|
+
* The `image_models.json` entry that means "draw nothing".
|
|
185
|
+
*
|
|
186
|
+
* A real entry rather than a blank, because turning pictures off for one room is a
|
|
187
|
+
* different intent from not choosing a model — a blank falls back to the server's
|
|
188
|
+
* `DEFAULT_IMAGE_MODEL`, and this does not.
|
|
189
|
+
*
|
|
190
|
+
* The client spells the same string out itself rather than importing this. A value
|
|
191
|
+
* import from here is not safe there: Vite resolves `.js` before `.ts` and this
|
|
192
|
+
* directory is gitignored for compiled output, so a stale `app.types.js` would hand
|
|
193
|
+
* the client `undefined` while type-checking passed against the `.ts`. Change this
|
|
194
|
+
* and `RoomSettings.svelte` has to change with it.
|
|
195
|
+
*/
|
|
196
|
+
export declare const NO_IMAGES = "No images";
|
|
163
197
|
export interface RoomDetails {
|
|
164
198
|
id: string;
|
|
165
199
|
name: string;
|
|
@@ -171,12 +205,21 @@ export interface RoomDetails {
|
|
|
171
205
|
pack?: string;
|
|
172
206
|
roomLeader?: string;
|
|
173
207
|
turnMode?: RoomTurnMode;
|
|
208
|
+
/** Entry in image_models.json. Absent uses DEFAULT_IMAGE_MODEL; {@link NO_IMAGES} disables. */
|
|
209
|
+
imageModel?: string;
|
|
210
|
+
/** Absent means 'on-request'. See {@link RoomIllustrationMode}. */
|
|
211
|
+
illustrationMode?: RoomIllustrationMode;
|
|
174
212
|
/**
|
|
175
213
|
* Nickname of the agent that places messages it cannot route mechanically.
|
|
176
214
|
* Only consulted in 'moderated' mode. Absent falls back to the first agent in
|
|
177
215
|
* the room, which is how story rooms have always chosen their moderator.
|
|
178
216
|
*/
|
|
179
217
|
moderator?: string;
|
|
218
|
+
/**
|
|
219
|
+
* What Begin sends to the room leader to open the conversation. Blank or absent
|
|
220
|
+
* means the room offers no Begin. See `RoomConfig.beginInstruction`.
|
|
221
|
+
*/
|
|
222
|
+
beginInstruction?: string;
|
|
180
223
|
isPublished?: boolean;
|
|
181
224
|
templateRoomId?: string;
|
|
182
225
|
/**
|
|
@@ -1,7 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MessageAction = exports.BRIYAH_MODES = void 0;
|
|
3
|
+
exports.MessageAction = exports.NO_IMAGES = exports.BRIYAH_MODES = void 0;
|
|
4
4
|
exports.BRIYAH_MODES = ['agents', 'stories', 'rpg'];
|
|
5
|
+
/**
|
|
6
|
+
* The `image_models.json` entry that means "draw nothing".
|
|
7
|
+
*
|
|
8
|
+
* A real entry rather than a blank, because turning pictures off for one room is a
|
|
9
|
+
* different intent from not choosing a model — a blank falls back to the server's
|
|
10
|
+
* `DEFAULT_IMAGE_MODEL`, and this does not.
|
|
11
|
+
*
|
|
12
|
+
* The client spells the same string out itself rather than importing this. A value
|
|
13
|
+
* import from here is not safe there: Vite resolves `.js` before `.ts` and this
|
|
14
|
+
* directory is gitignored for compiled output, so a stale `app.types.js` would hand
|
|
15
|
+
* the client `undefined` while type-checking passed against the `.ts`. Change this
|
|
16
|
+
* and `RoomSettings.svelte` has to change with it.
|
|
17
|
+
*/
|
|
18
|
+
exports.NO_IMAGES = 'No images';
|
|
5
19
|
/**
|
|
6
20
|
* Represents the action type of a message
|
|
7
21
|
*/
|
package/package.json
CHANGED