briyah 2.0.11 → 2.0.13

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 CHANGED
@@ -1364,6 +1364,32 @@ still deletes them. Every artifact now carries `createdAt` and `updatedAt`.
1364
1364
  for a host keeping its own accounting. The vector length has to match your column, so declare
1365
1365
  the column from `EMBEDDING_DIMENSIONS` and change the model and the column together.
1366
1366
 
1367
+ ### Example 14: Letting an agent make pictures
1368
+
1369
+ Give the room an image model (an entry in `image_models.json`) and grant the agent the core tool
1370
+ `create_image`. The agent writes the description and a title; the room's artist draws it exactly
1371
+ as written, once.
1372
+
1373
+ ```typescript
1374
+ const { roomId } = await app.createRoom(
1375
+ 'Assistant', goal, agentIds, 'Lisa', 'directed',
1376
+ undefined, undefined,
1377
+ 'GPT Image 2.5', // imageModel
1378
+ );
1379
+ assistant.toolNames = [...assistant.toolNames, 'create_image'];
1380
+ ```
1381
+
1382
+ The call returns at once, because drawing can outlast a tool call. The finished picture arrives
1383
+ as a `system` room message whose sender is the title and whose content is
1384
+ `![<title>](/api/rooms/<roomId>/images/<imageId>)`; a picture that could not be made arrives
1385
+ instead as a `system` notice from `Artist` saying why. Fetch the bytes with
1386
+ `app.getRoomImage(roomId, imageId)`. One picture per agent per turn.
1387
+
1388
+ `create_image` is not `illustrate`. `illustrate` asks the room's illustrator for a picture of the
1389
+ current scene, and it is built for stories: it decides whether a picture is warranted, draws the
1390
+ setting before the moment, and gives each named character a portrait. Use it for a story-like
1391
+ room, and `create_image` for an agent asked to make a picture of something.
1392
+
1367
1393
  ## Data Storage
1368
1394
 
1369
1395
  Briyah stores data in the following structure:
@@ -2086,11 +2086,15 @@ export declare class AppService {
2086
2086
  *
2087
2087
  * @param roomId - The room's ID
2088
2088
  * @param artifactId - The image's ID, as it appears in the message markdown
2089
- * @returns The bytes and MIME type, or `null` when there is no such image
2089
+ * @returns The bytes and MIME type, with the image's name and the prompt it was
2090
+ * drawn from — what a host keeping a copy needs to make it findable — or
2091
+ * `null` when there is no such image
2090
2092
  */
2091
2093
  getRoomImage(roomId: string, artifactId: string): Promise<{
2092
2094
  content: string | Buffer;
2093
2095
  mimeType: string;
2096
+ name: string;
2097
+ description: string;
2094
2098
  } | null>;
2095
2099
  /**
2096
2100
  * Returns metadata for all artifacts currently attached to an agent.
@@ -3860,7 +3860,9 @@ class AppService {
3860
3860
  *
3861
3861
  * @param roomId - The room's ID
3862
3862
  * @param artifactId - The image's ID, as it appears in the message markdown
3863
- * @returns The bytes and MIME type, or `null` when there is no such image
3863
+ * @returns The bytes and MIME type, with the image's name and the prompt it was
3864
+ * drawn from — what a host keeping a copy needs to make it findable — or
3865
+ * `null` when there is no such image
3864
3866
  */
3865
3867
  async getRoomImage(roomId, artifactId) {
3866
3868
  const room = await this.roomStore.getRoom(roomId);
@@ -3871,7 +3873,12 @@ class AppService {
3871
3873
  const content = artifacts.getArtifact(artifactId);
3872
3874
  if (!metadata || !content)
3873
3875
  return null;
3874
- return { content, mimeType: metadata.mimeType || 'application/octet-stream' };
3876
+ return {
3877
+ content,
3878
+ mimeType: metadata.mimeType || 'application/octet-stream',
3879
+ name: metadata.name || '',
3880
+ description: metadata.description || '',
3881
+ };
3875
3882
  }
3876
3883
  /**
3877
3884
  * Returns metadata for all artifacts currently attached to an agent.
@@ -174,6 +174,26 @@ export declare class IllustrationService {
174
174
  * part of the contract with `describe_scene.prompt`.
175
175
  */
176
176
  private generateSceneImage;
177
+ /**
178
+ * Draws a picture exactly as described and posts it — no illustrator, no
179
+ * reference scene, no portraits. What {@link ToolIllustration.create} queues.
180
+ *
181
+ * Never throws, like the rest of this class. A failure is posted as a notice
182
+ * rather than only logged: the caller told someone a picture was coming, and
183
+ * one that silently never arrives leaves them waiting for it.
184
+ *
185
+ * @returns The stored image's artifact id, or null when nothing was drawn
186
+ */
187
+ createImage(room: Room, target: IllustrationTarget, req: {
188
+ description: string;
189
+ title: string;
190
+ aspect?: ImageAspect;
191
+ requestedBy?: string;
192
+ }): Promise<string | null>;
193
+ /** Tells the room that a picture asked for by name could not be made. */
194
+ private postImageFailure;
195
+ /** Why the image model drew nothing, in words a person can act on. */
196
+ private describeCause;
177
197
  /**
178
198
  * Puts the finished picture in the transcript.
179
199
  *
@@ -608,6 +608,59 @@ let IllustrationService = class IllustrationService {
608
608
  return false;
609
609
  }
610
610
  }
611
+ /**
612
+ * Draws a picture exactly as described and posts it — no illustrator, no
613
+ * reference scene, no portraits. What {@link ToolIllustration.create} queues.
614
+ *
615
+ * Never throws, like the rest of this class. A failure is posted as a notice
616
+ * rather than only logged: the caller told someone a picture was coming, and
617
+ * one that silently never arrives leaves them waiting for it.
618
+ *
619
+ * @returns The stored image's artifact id, or null when nothing was drawn
620
+ */
621
+ async createImage(room, target, req) {
622
+ try {
623
+ const agents = await this.ensureAgents(target);
624
+ if (!agents) {
625
+ this.postImageFailure(room, req.title, 'no image model is configured for this room');
626
+ return null;
627
+ }
628
+ const { artist } = agents;
629
+ room.attachAgent(artist);
630
+ logger_1.logger.log(`Creating image "${req.title}" for ${req.requestedBy || target.key}`);
631
+ const imageResult = await artist.generateImage(req.description, this.getImageProperties(artist, 'generation', req.aspect));
632
+ if (imageResult.error) {
633
+ this.postImageFailure(room, req.title, this.describeCause(imageResult.error));
634
+ return null;
635
+ }
636
+ artist.save();
637
+ const artifactId = imageResult.artifactId;
638
+ target.artifacts.renameArtifact(artifactId, req.title);
639
+ target.artifacts.updateArtifactDescription(artifactId, req.description);
640
+ this.postImageToRoom(room, target, artifactId, req.title);
641
+ return artifactId;
642
+ }
643
+ catch (error) {
644
+ logger_1.logger.error(`Error creating image "${req.title}":`, error);
645
+ this.postImageFailure(room, req.title, this.describeCause(error));
646
+ return null;
647
+ }
648
+ }
649
+ /** Tells the room that a picture asked for by name could not be made. */
650
+ postImageFailure(room, title, cause) {
651
+ try {
652
+ room.addRoomMessage(new message_1.RoomMessage('Artist', app_types_1.MessageAction.SYSTEM, `Could not make the picture "${title}": ${cause}.`));
653
+ }
654
+ catch (postError) {
655
+ logger_1.logger.error('Error adding image failure message to room:', postError);
656
+ }
657
+ }
658
+ /** Why the image model drew nothing, in words a person can act on. */
659
+ describeCause(error) {
660
+ return error instanceof blank_image_1.BlankImageError
661
+ ? 'the image model returned a blank image, which usually means it refused the description'
662
+ : `the image model failed (${this.describeError(error)})`;
663
+ }
611
664
  /**
612
665
  * Puts the finished picture in the transcript.
613
666
  *
@@ -638,9 +691,7 @@ let IllustrationService = class IllustrationService {
638
691
  * is the half of the news that decides what the reader should do next
639
692
  */
640
693
  postPortraitFailure(room, characterName, error, portraitKept) {
641
- const cause = error instanceof blank_image_1.BlankImageError
642
- ? 'the image model returned a blank image, which usually means it refused the description'
643
- : `the image model failed (${this.describeError(error)})`;
694
+ const cause = this.describeCause(error);
644
695
  const outcome = portraitKept
645
696
  ? 'The existing portrait has been kept.'
646
697
  : 'They have no portrait yet.';
@@ -2145,6 +2145,25 @@ Recent Message: ${message.getContent()}
2145
2145
  });
2146
2146
  return { accepted: true, ready };
2147
2147
  },
2148
+ create: (description, title, aspect) => {
2149
+ if (!available)
2150
+ return { accepted: false, reason: 'images_disabled' };
2151
+ const used = this.imagesThisTurn.get(nickname) ?? 0;
2152
+ if (used >= Room.IMAGES_PER_TURN) {
2153
+ return { accepted: false, reason: 'turn_limit' };
2154
+ }
2155
+ // Not gated on `isBusy` either, for the portrait's reason: the lock
2156
+ // guards a room's one situation, and a picture made to order is not
2157
+ // a picture of it.
2158
+ this.imagesThisTurn.set(nickname, used + 1);
2159
+ void illustration.createImage(this, target, {
2160
+ description,
2161
+ title,
2162
+ aspect: aspect ?? this.getImageAspect(),
2163
+ requestedBy: nickname,
2164
+ });
2165
+ return { accepted: true };
2166
+ },
2148
2167
  };
2149
2168
  }
2150
2169
  /**
@@ -0,0 +1,18 @@
1
+ import { NativeToolDefinition } from '../native-tool.types';
2
+ /**
3
+ * Makes a picture to order: the calling agent describes it, the room's artist
4
+ * draws it once.
5
+ *
6
+ * Not `illustrate`, which asks the room's illustrator for a picture of the scene.
7
+ * That pipeline belongs to stories — it decides for itself whether a picture is
8
+ * warranted, draws a setting before it draws the moment, and gives everyone named
9
+ * a portrait first. An assistant asked for "a flatbed hauling steel coils, for a
10
+ * proposal cover" wants none of that, and an agent able to hold a conversation can
11
+ * write the image prompt itself, so here the description goes to the image model
12
+ * as written.
13
+ *
14
+ * Returns as soon as the request is accepted, for the same reason `illustrate`
15
+ * does: drawing can outlast a tool call. The picture arrives in the room on its
16
+ * own, as a message captioned with the title.
17
+ */
18
+ export declare const createImageTool: NativeToolDefinition;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createImageTool = void 0;
4
+ const coerce_1 = require("../shared/coerce");
5
+ /** Longest title kept: it is a caption and an artifact name, not a description. */
6
+ const MAX_TITLE_CHARS = 120;
7
+ /**
8
+ * Makes a picture to order: the calling agent describes it, the room's artist
9
+ * draws it once.
10
+ *
11
+ * Not `illustrate`, which asks the room's illustrator for a picture of the scene.
12
+ * That pipeline belongs to stories — it decides for itself whether a picture is
13
+ * warranted, draws a setting before it draws the moment, and gives everyone named
14
+ * a portrait first. An assistant asked for "a flatbed hauling steel coils, for a
15
+ * proposal cover" wants none of that, and an agent able to hold a conversation can
16
+ * write the image prompt itself, so here the description goes to the image model
17
+ * as written.
18
+ *
19
+ * Returns as soon as the request is accepted, for the same reason `illustrate`
20
+ * does: drawing can outlast a tool call. The picture arrives in the room on its
21
+ * own, as a message captioned with the title.
22
+ */
23
+ exports.createImageTool = {
24
+ name: 'create_image',
25
+ description: 'Make a picture: a photograph, illustration, icon, diagram-style graphic or anything else that can be drawn. ' +
26
+ 'Write the description the image model will draw from — the subject, the setting, the composition, the style ' +
27
+ '("a clean flat vector illustration", "a photograph in soft morning light") and any colours that matter. ' +
28
+ 'It is drawn exactly as written, so be concrete; the model knows nothing of this conversation. ' +
29
+ 'Image models render words unreliably: ask for text in the picture only when it is short, and quote it exactly. ' +
30
+ 'The picture does NOT come back in the result: it appears in the room a minute or so later, on its own. ' +
31
+ 'One picture per turn.',
32
+ parameters: [
33
+ {
34
+ name: 'description',
35
+ type: 'string',
36
+ description: 'What to draw, in full, e.g. "A photograph of a red semi-truck with a flatbed trailer hauling steel coils on a desert interstate at sunrise, three-quarter view from the roadside, warm light, sharp focus"',
37
+ required: true,
38
+ },
39
+ {
40
+ name: 'title',
41
+ type: 'string',
42
+ description: 'A short name for the picture, e.g. "Flatbed at sunrise" — it becomes the caption.',
43
+ required: true,
44
+ },
45
+ {
46
+ name: 'aspect',
47
+ type: 'string',
48
+ description: 'The shape of the picture: "landscape" (wider than tall), "portrait" (taller than wide) or "square". Optional — leave it out for the room\'s usual shape.',
49
+ required: false,
50
+ enum: ['square', 'landscape', 'portrait'],
51
+ },
52
+ ],
53
+ async handler(args, helpers) {
54
+ if (!helpers?.illustration) {
55
+ throw new Error('Making a picture works on a room and there is no room here. Run it from a room rather than the tool tester.');
56
+ }
57
+ const description = (0, coerce_1.str)(args.description, 'description').trim();
58
+ const title = (0, coerce_1.str)(args.title, 'title').trim().slice(0, MAX_TITLE_CHARS);
59
+ if (!description) {
60
+ throw new Error('description is empty. Say what the picture should show.');
61
+ }
62
+ if (!title) {
63
+ throw new Error('title is empty. Give the picture a short name.');
64
+ }
65
+ // Already checked against the enum before the handler runs.
66
+ const aspect = args.aspect ? (0, coerce_1.str)(args.aspect, 'aspect') : undefined;
67
+ const ticket = helpers.illustration.create(description, title, aspect);
68
+ if (ticket.accepted === true) {
69
+ return {
70
+ requested: true,
71
+ summary: `Picture requested: ${title}`,
72
+ note: 'It is being drawn and will appear in the room in a minute or so. Do not wait for it.',
73
+ };
74
+ }
75
+ // Refusals come back successfully rather than thrown, as `illustrate`'s do:
76
+ // they are rulings the caller acts on, not broken calls.
77
+ if (ticket.reason === 'turn_limit') {
78
+ return {
79
+ requested: false,
80
+ violation: 'turn_limit',
81
+ message: 'You have already asked for a picture this turn.',
82
+ rule: 'One picture per turn — each one costs real money.',
83
+ options: ['Make the next one on a later turn.'],
84
+ summary: 'Picture refused: one per turn',
85
+ };
86
+ }
87
+ return {
88
+ requested: false,
89
+ violation: 'images_disabled',
90
+ message: 'This room has no image model configured, so no picture can be made.',
91
+ options: ['Carry on without one, and do not offer to make pictures.'],
92
+ summary: 'Picture refused: images are off',
93
+ };
94
+ },
95
+ };
@@ -16,6 +16,7 @@
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.CORE_TOOL_GROUP = void 0;
18
18
  exports.coreTools = coreTools;
19
+ const create_image_tool_1 = require("./create-image.tool");
19
20
  const data_tools_1 = require("./data.tools");
20
21
  const document_tools_1 = require("./document.tools");
21
22
  const illustrate_tool_1 = require("./illustrate.tool");
@@ -30,5 +31,12 @@ exports.CORE_TOOL_GROUP = 'core';
30
31
  * @returns The tool definitions
31
32
  */
32
33
  function coreTools() {
33
- return [rename_tool_1.renameAgentTool, illustrate_tool_1.illustrateTool, data_tools_1.describeDataTool, data_tools_1.queryDataTool, ...document_tools_1.documentTools];
34
+ return [
35
+ rename_tool_1.renameAgentTool,
36
+ illustrate_tool_1.illustrateTool,
37
+ create_image_tool_1.createImageTool,
38
+ data_tools_1.describeDataTool,
39
+ data_tools_1.queryDataTool,
40
+ ...document_tools_1.documentTools,
41
+ ];
34
42
  }
@@ -221,6 +221,19 @@ export type PortraitTicket = {
221
221
  accepted: false;
222
222
  reason: 'images_disabled' | 'turn_limit';
223
223
  };
224
+ /**
225
+ * What {@link ToolIllustration.create} did, or why it did nothing.
226
+ *
227
+ * No `in_flight` refusal: the scene lock exists because a room has one situation
228
+ * and two scenes drawn at once would show a stale one, while a picture made to
229
+ * order has nothing to go stale.
230
+ */
231
+ export type CreateImageTicket = {
232
+ accepted: true;
233
+ } | {
234
+ accepted: false;
235
+ reason: 'images_disabled' | 'turn_limit';
236
+ };
224
237
  /**
225
238
  * Asking the room's illustrator for a picture.
226
239
  *
@@ -254,6 +267,23 @@ export interface ToolIllustration {
254
267
  * else identifies them. An empty profile draws nothing.
255
268
  */
256
269
  portrait(characterName: string, profile: string): PortraitTicket;
270
+ /**
271
+ * Draws exactly what the caller describes, once.
272
+ *
273
+ * The other half of {@link request}. That one hands a scene to the illustrator,
274
+ * which decides whether a picture is warranted and builds it from references and
275
+ * portraits — right for a story, and wrong for an assistant asked for a picture
276
+ * of a thing: it can decline, draw twice, or invent a cast. Here the caller's
277
+ * description is the image prompt, and the artist draws it as written.
278
+ *
279
+ * Returns when the work is queued; the picture arrives in the room as its own
280
+ * message, captioned with `title`, or a notice saying why it could not be made.
281
+ *
282
+ * @param description The image prompt, written by the caller
283
+ * @param title A short name for the picture: its caption and its artifact name
284
+ * @param aspect The shape to draw it in. Absent takes the room's default.
285
+ */
286
+ create(description: string, title: string, aspect?: ImageAspect): CreateImageTicket;
257
287
  }
258
288
  /** A document held in an agent's context. */
259
289
  export interface ToolHeldDocument {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "briyah",
3
- "version": "2.0.11",
3
+ "version": "2.0.13",
4
4
  "description": "SDK for AI agents, multi-agent rooms, interactive stories, and role-playing games with per-user data isolation and cost tracking",
5
5
  "private": false,
6
6
  "license": "Apache-2.0",