@tstdl/base 0.93.133 → 0.93.134

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,21 +1,24 @@
1
1
  import type { Part } from 'genkit';
2
- import type { ObjectLiteral, OneOrMany } from '../../types/index.js';
2
+ import type { ObjectLiteral } from '../../types/index.js';
3
3
  import { type Instructions } from './instructions-formatter.js';
4
- export type PromptInstructionItem = string | Instructions;
4
+ export type PromptBuilderInstructions = Record<string, Instructions>;
5
+ export type PromptBuilderContext = Record<string, PromptBuilderContextItem>;
6
+ export type PromptBuilderContextItem = ObjectLiteral;
5
7
  export declare class PromptBuilder {
6
8
  #private;
7
9
  setSystemRole(role: string): this;
8
10
  setRole(role: string): this;
9
11
  setSystemTask(task: string): this;
10
12
  setTask(task: string): this;
13
+ addSystemMedia(content: Uint8Array, mimeType: string): this;
11
14
  addMedia(content: Uint8Array, mimeType: string): this;
12
15
  addSystemInstructions(instructions: Record<string, Instructions>): this;
13
16
  addInstructions(instructions: Record<string, Instructions>): this;
14
- addSystemContext(title: string, context: string | OneOrMany<ObjectLiteral | Record<string, ObjectLiteral | null>>): this;
15
- addSystemContext(context: Record<string, OneOrMany<ObjectLiteral | Record<string, ObjectLiteral | null>>>): this;
16
- addContext(title: string, context: string | OneOrMany<ObjectLiteral | Record<string, ObjectLiteral | null>>): this;
17
- addContext(context: Record<string, OneOrMany<ObjectLiteral | Record<string, ObjectLiteral | null>>>): this;
18
- buildSystem(): Part[];
19
- buildPrompt(): Part[];
17
+ addSystemContext(title: string, context: PromptBuilderContextItem): this;
18
+ addSystemContext(context: PromptBuilderContext): this;
19
+ addContext(title: string, context: PromptBuilderContextItem): this;
20
+ addContext(context: PromptBuilderContext): this;
21
+ buildSystemPrompt(): Part[];
22
+ buildUserPrompt(): Part[];
20
23
  }
21
24
  export declare function promptBuilder(): PromptBuilder;
@@ -12,6 +12,7 @@ export class PromptBuilder {
12
12
  #instructions = {};
13
13
  #systemContextParts = {};
14
14
  #contextParts = {};
15
+ #systemMedia = [];
15
16
  #media = [];
16
17
  setSystemRole(role) {
17
18
  this.#systemRole = role;
@@ -29,10 +30,12 @@ export class PromptBuilder {
29
30
  this.#task = task;
30
31
  return this;
31
32
  }
33
+ addSystemMedia(content, mimeType) {
34
+ addMedia(content, mimeType, this.#systemMedia);
35
+ return this;
36
+ }
32
37
  addMedia(content, mimeType) {
33
- const base64Data = encodeBase64(content);
34
- const dataUrl = `data:${mimeType};base64,${base64Data}`;
35
- this.#media.push({ media: { url: dataUrl } });
38
+ addMedia(content, mimeType, this.#media);
36
39
  return this;
37
40
  }
38
41
  addSystemInstructions(instructions) {
@@ -59,51 +62,54 @@ export class PromptBuilder {
59
62
  this.#contextParts[titleOrContext] = contextOrNothing;
60
63
  return this;
61
64
  }
62
- buildSystem() {
63
- const instructions = {};
64
- if (isDefined(this.#systemRole)) {
65
- instructions['**Role**'] = this.#systemRole;
66
- }
67
- if (objectKeys(this.#systemContextParts).length > 0) {
68
- const contextEntries = objectEntries(this.#systemContextParts).map(([key, contextPart]) => {
69
- const string = isString(contextPart) ? contextPart : `\`\`\`toon\n${formatData(contextPart)}\n\`\`\``;
70
- return [key, string];
71
- });
72
- instructions['**Context**'] = sections(fromEntries(contextEntries));
73
- }
74
- if (objectKeys(this.#systemInstructions).length > 0) {
75
- instructions['**Instructions**'] = this.#systemInstructions;
76
- }
77
- if (isDefined(this.#systemTask)) {
78
- instructions['**Task**'] = this.#systemTask;
79
- }
80
- return [{ text: formatInstructions(instructions) }];
65
+ buildSystemPrompt() {
66
+ return buildPrompt({
67
+ role: this.#systemRole,
68
+ context: this.#systemContextParts,
69
+ instructions: this.#systemInstructions,
70
+ task: this.#systemTask,
71
+ media: this.#systemMedia,
72
+ });
81
73
  }
82
- buildPrompt() {
83
- const instructions = {};
84
- if (isDefined(this.#role)) {
85
- instructions['**Role**'] = this.#role;
86
- }
87
- if (objectKeys(this.#contextParts).length > 0) {
88
- const contextEntries = objectEntries(this.#contextParts).map(([key, contextPart]) => {
89
- const string = isString(contextPart) ? contextPart : `\`\`\`toon\n${formatData(contextPart)}\n\`\`\``;
90
- return [key, string];
91
- });
92
- instructions['**Context**'] = sections(fromEntries(contextEntries));
93
- }
94
- if (objectKeys(this.#instructions).length > 0) {
95
- instructions['**Instructions**'] = this.#instructions;
96
- }
97
- if (isDefined(this.#task)) {
98
- instructions['**Task**'] = this.#task;
99
- }
100
- const formatted = formatInstructions(instructions);
101
- return [
102
- ...this.#media,
103
- { text: formatted },
104
- ];
74
+ buildUserPrompt() {
75
+ return buildPrompt({
76
+ role: this.#role,
77
+ context: this.#contextParts,
78
+ instructions: this.#instructions,
79
+ task: this.#task,
80
+ media: this.#media,
81
+ });
105
82
  }
106
83
  }
107
84
  export function promptBuilder() {
108
85
  return new PromptBuilder();
109
86
  }
87
+ function addMedia(content, mimeType, targetArray) {
88
+ const base64Data = encodeBase64(content);
89
+ const dataUrl = `data:${mimeType};base64,${base64Data}`;
90
+ targetArray.push({ media: { url: dataUrl } });
91
+ }
92
+ function buildPrompt(data) {
93
+ const instructions = {};
94
+ if (isDefined(data.role)) {
95
+ instructions['**Role**'] = data.role;
96
+ }
97
+ if (isDefined(data.context) && objectKeys(data.context).length > 0) {
98
+ const contextEntries = objectEntries(data.context).map(([key, contextPart]) => {
99
+ const string = isString(contextPart) ? contextPart : `\`\`\`toon\n${formatData(contextPart)}\n\`\`\``;
100
+ return [key, string];
101
+ });
102
+ instructions['**Context**'] = sections(fromEntries(contextEntries));
103
+ }
104
+ if (isDefined(data.instructions) && (objectKeys(data.instructions).length > 0)) {
105
+ instructions['**Instructions**'] = data.instructions;
106
+ }
107
+ if (isDefined(data.task)) {
108
+ instructions['**Task**'] = data.task;
109
+ }
110
+ const formattedInstructions = formatInstructions(instructions);
111
+ return [
112
+ ...(data.media ?? []),
113
+ { text: formattedInstructions },
114
+ ];
115
+ }
@@ -79,8 +79,8 @@ async function main() {
79
79
  const dummyImage = new Uint8Array([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
80
80
  builder.addMedia(dummyImage, 'image/png');
81
81
  // 4. Build and Display
82
- const systemMessage = builder.buildSystem();
83
- const userMessage = builder.buildPrompt();
82
+ const systemMessage = builder.buildSystemPrompt();
83
+ const userMessage = builder.buildUserPrompt();
84
84
  console.log('--- SYSTEM MESSAGE ---');
85
85
  for (const part of systemMessage) {
86
86
  if (part.text) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.93.133",
3
+ "version": "0.93.134",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
package/test5.js CHANGED
@@ -6,9 +6,9 @@ import { PrettyPrintLogFormatter } from './logger/index.js';
6
6
  import { provideConsoleLogTransport } from './logger/transports/console.js';
7
7
  async function main(_cancellationSignal) {
8
8
  const x = promptBuilder();
9
- console.log(x.buildSystem()[0].text);
9
+ console.log(x.buildSystemPrompt()[0].text);
10
10
  console.log('\n\n---\n\n');
11
- console.log(x.buildPrompt()[0].text);
11
+ console.log(x.buildUserPrompt()[0].text);
12
12
  }
13
13
  Application.run('Test', [
14
14
  provideConsoleLogTransport(PrettyPrintLogFormatter),