@tstdl/base 0.93.133 → 0.93.135

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
+ }
@@ -37,7 +37,7 @@ let DocumentValidationService = DocumentValidationService_1 = class DocumentVali
37
37
  #validationExecutionService = injectRepository(DocumentValidationExecution);
38
38
  #validationExecutionRelatedDocumentService = injectRepository(DocumentValidationExecutionRelatedDocument);
39
39
  #documentTypeValidationService = injectRepository(DocumentTypeValidation);
40
- #taskQueue = inject((TaskQueue), { namespace: 'DocumentManagement:validation', visibilityTimeout: 5 * millisecondsPerMinute, maxTries: 3 });
40
+ #taskQueue = inject((TaskQueue), { namespace: 'DocumentManagement:Validation', visibilityTimeout: 5 * millisecondsPerMinute, maxTries: 3 });
41
41
  #executors = injectAll(DOCUMENT_VALIDATION_EXECUTORS);
42
42
  #logger = inject(Logger, DocumentValidationService_1.name);
43
43
  #executorMap = new Map(this.#executors.map((executor) => [executor.identifier, executor]));
@@ -37,7 +37,7 @@ let DocumentWorkflowService = DocumentWorkflowService_1 = class DocumentWorkflow
37
37
  #documentAssignmentTaskRepository = injectRepository(DocumentAssignmentTask);
38
38
  #documentAssignmentScopeRepository = injectRepository(DocumentAssignmentScope);
39
39
  #observationService = inject(DocumentManagementObservationService);
40
- #taskQueue = inject((TaskQueue), { namespace: 'DocumentManagement:DocumentWorkflow', visibilityTimeout: 5 * 60 * 1000, maxTries: 3 });
40
+ #taskQueue = inject((TaskQueue), { namespace: 'DocumentManagement:Workflow', visibilityTimeout: 5 * 60 * 1000, maxTries: 3 });
41
41
  #logger = inject(Logger, DocumentWorkflowService_1.name);
42
42
  documentService = inject(forwardRef(() => DocumentService));
43
43
  repository = injectRepository(DocumentWorkflow);
@@ -107,7 +107,7 @@ let DocumentWorkflowService = DocumentWorkflowService_1 = class DocumentWorkflow
107
107
  async initiateWorkflow(tenantId, documentId, step, skipAi) {
108
108
  const workflow = await this.repository.insert({ tenantId, documentId, step, state: 'pending', failReason: null, completeTimestamp: null, completeUserId: null, skipAi });
109
109
  if (!skipAi) {
110
- await this.#taskQueue.enqueue('workflow', { workflowId: workflow.id });
110
+ await this.#taskQueue.enqueue(step, { workflowId: workflow.id });
111
111
  }
112
112
  this.#observationService.documentChange(workflow.id, this.session);
113
113
  return workflow;
@@ -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) {
@@ -5,7 +5,7 @@ import { TaskProcessResult } from '../../../task-queue/task-queue.js';
5
5
  import { NotificationChannel } from '../../models/index.js';
6
6
  import type { ChannelProvider } from '../providers/channel-provider.js';
7
7
  export type NotificationTaskDefinitions = TaskDefinitionMap<{
8
- 'notification/deliver': TaskDefinition<{
8
+ deliver: TaskDefinition<{
9
9
  notificationId: string;
10
10
  }>;
11
11
  }>;
@@ -44,7 +44,7 @@ let NotificationService = NotificationService_1 = class NotificationService exte
44
44
  currentStep: 0,
45
45
  };
46
46
  const insertedNotification = await this.#notificationLogRepository.withTransaction(tx).insert(notificationToInsert);
47
- await this.#taskQueue.withTransaction(tx).enqueue('notification/deliver', { notificationId: insertedNotification.id });
47
+ await this.#taskQueue.withTransaction(tx).enqueue('deliver', { notificationId: insertedNotification.id });
48
48
  });
49
49
  }
50
50
  async listInApp(tenantId, userId, options = {}) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tstdl/base",
3
- "version": "0.93.133",
3
+ "version": "0.93.135",
4
4
  "author": "Patrick Hein",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -3,7 +3,7 @@ import type { Logger } from '../logger/index.js';
3
3
  import type { Transaction } from '../orm/server/index.js';
4
4
  import { TaskQueue, type EnqueueManyItem, type EnqueueOptions } from './task-queue.js';
5
5
  import type { TaskData, TaskDefinitionMap, TaskOfType, TaskResult, TaskState, TaskTypes } from './types.js';
6
- export declare class TaskContext<Definitions extends TaskDefinitionMap, Type extends TaskTypes<Definitions>> {
6
+ export declare class TaskContext<Definitions extends TaskDefinitionMap, Type extends TaskTypes<Definitions> = TaskTypes<Definitions>> {
7
7
  #private;
8
8
  constructor(queue: TaskQueue<Definitions>, task: TaskOfType<Definitions, Type>, signal: CancellationToken, logger: Logger);
9
9
  get id(): string;
@@ -104,7 +104,7 @@ export class TaskQueue extends Transactional {
104
104
  async processWorker(cancellationSignal, handler, options) {
105
105
  for await (const task of this.getConsumer(cancellationSignal, options)) {
106
106
  const taskToken = cancellationSignal.createChild();
107
- const context = new TaskContext(this, task, taskToken, this.logger);
107
+ const context = new TaskContext(this, task, taskToken, this.logger.with({ type: task.type }));
108
108
  let isTaskActive = true;
109
109
  context.logger.verbose(`Processing task`);
110
110
  void (async () => {
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),