@tstdl/base 0.93.149 → 0.93.150

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.
@@ -1,4 +1,5 @@
1
1
  import type { Part } from 'genkit';
2
+ import type { SchemaTestable } from '../../schema/schema.js';
2
3
  import type { ObjectLiteral } from '../../types/index.js';
3
4
  import { type Instructions } from './instructions-formatter.js';
4
5
  export type PromptBuilderInstructions = Record<string, Instructions>;
@@ -10,6 +11,8 @@ export declare class PromptBuilder {
10
11
  setRole(role: string): this;
11
12
  setSystemTask(task: string): this;
12
13
  setTask(task: string): this;
14
+ setSystemOutputSchema(schema: SchemaTestable): this;
15
+ setOutputSchema(schema: SchemaTestable): this;
13
16
  addSystemMedia(content: Uint8Array, mimeType: string): this;
14
17
  addMedia(content: Uint8Array, mimeType: string): this;
15
18
  addSystemInstructions(instructions: Record<string, Instructions>): this;
@@ -1,3 +1,4 @@
1
+ import { convertToOpenApiSchema } from '../../schema/converters/open-api-converter.js';
1
2
  import { encodeBase64 } from '../../utils/base64.js';
2
3
  import { fromEntries, objectEntries, objectKeys } from '../../utils/object/index.js';
3
4
  import { assertObjectPass, isDefined, isString, isUndefined } from '../../utils/type-guards.js';
@@ -10,6 +11,8 @@ export class PromptBuilder {
10
11
  #role;
11
12
  #systemTask;
12
13
  #task;
14
+ #systemOutputSchema;
15
+ #outputSchema;
13
16
  #systemInstructions = {};
14
17
  #instructions = {};
15
18
  #systemContextParts = {};
@@ -30,6 +33,14 @@ export class PromptBuilder {
30
33
  this.#task = task;
31
34
  return this;
32
35
  }
36
+ setSystemOutputSchema(schema) {
37
+ this.#systemOutputSchema = schema;
38
+ return this;
39
+ }
40
+ setOutputSchema(schema) {
41
+ this.#outputSchema = schema;
42
+ return this;
43
+ }
33
44
  addSystemMedia(content, mimeType) {
34
45
  addMedia(content, mimeType, this.#systemMedia);
35
46
  return this;
@@ -67,6 +78,7 @@ export class PromptBuilder {
67
78
  role: this.#systemRole,
68
79
  context: this.#systemContextParts,
69
80
  instructions: this.#systemInstructions,
81
+ outputSchema: this.#systemOutputSchema,
70
82
  task: this.#systemTask,
71
83
  media: this.#systemMedia,
72
84
  });
@@ -76,6 +88,7 @@ export class PromptBuilder {
76
88
  role: this.#role,
77
89
  context: this.#contextParts,
78
90
  instructions: this.#instructions,
91
+ outputSchema: this.#outputSchema,
79
92
  task: this.#task,
80
93
  media: this.#media,
81
94
  });
@@ -104,6 +117,10 @@ function buildPrompt(data) {
104
117
  if (isDefined(data.instructions) && (objectKeys(data.instructions).length > 0)) {
105
118
  instructions['**Instructions**'] = data.instructions;
106
119
  }
120
+ if (isDefined(data.outputSchema)) {
121
+ const schema = convertToOpenApiSchema(data.outputSchema);
122
+ instructions['**Output Schema**'] = `\`\`\`json\n${JSON.stringify(schema, null, 2)}\n\`\`\``;
123
+ }
107
124
  if (isDefined(data.task)) {
108
125
  instructions['**Task**'] = data.task;
109
126
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,22 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { StringSchema } from '../../../schema/schemas/string.js';
3
+ import { promptBuilder } from '../prompt-builder.js';
4
+ describe('PromptBuilder', () => {
5
+ it('should add output schema to the prompt', () => {
6
+ const builder = promptBuilder()
7
+ .setRole('Test Role')
8
+ .setOutputSchema(new StringSchema({ description: 'Test Description' }))
9
+ .setTask('Test Task');
10
+ const prompt = builder.buildUserPrompt();
11
+ const text = prompt[0].text;
12
+ expect(text).toContain('# **Output Schema**');
13
+ expect(text).toContain('"type": "string"');
14
+ expect(text).toContain('"description": "Test Description"');
15
+ // Check order: Role -> Output Schema -> Task
16
+ const roleIndex = text.indexOf('# **Role**');
17
+ const schemaIndex = text.indexOf('# **Output Schema**');
18
+ const taskIndex = text.indexOf('# **Task**');
19
+ expect(roleIndex).toBeLessThan(schemaIndex);
20
+ expect(schemaIndex).toBeLessThan(taskIndex);
21
+ });
22
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.93.149",
3
+ "version": "0.93.150",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"