@workglow/ai 0.0.101 → 0.0.103

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/dist/bun.js CHANGED
@@ -31,9 +31,9 @@ class AiProviderRegistry {
31
31
  this.runFnRegistry.get(taskType).set(modelProvider, runFn);
32
32
  }
33
33
  registerAsWorkerRunFn(modelProvider, taskType) {
34
- const workerFn = async (input, model, update_progress, signal) => {
34
+ const workerFn = async (input, model, update_progress, signal, outputSchema) => {
35
35
  const workerManager = globalServiceRegistry.get(WORKER_MANAGER);
36
- const result = await workerManager.callWorkerFunction(modelProvider, taskType, [input, model], {
36
+ const result = await workerManager.callWorkerFunction(modelProvider, taskType, [input, model, outputSchema], {
37
37
  signal,
38
38
  onProgress: update_progress
39
39
  });
@@ -48,9 +48,9 @@ class AiProviderRegistry {
48
48
  this.streamFnRegistry.get(taskType).set(modelProvider, streamFn);
49
49
  }
50
50
  registerAsWorkerStreamFn(modelProvider, taskType) {
51
- const streamFn = async function* (input, model, signal) {
51
+ const streamFn = async function* (input, model, signal, outputSchema) {
52
52
  const workerManager = globalServiceRegistry.get(WORKER_MANAGER);
53
- yield* workerManager.callWorkerStreamFunction(modelProvider, taskType, [input, model], { signal });
53
+ yield* workerManager.callWorkerStreamFunction(modelProvider, taskType, [input, model, outputSchema], { signal });
54
54
  };
55
55
  this.registerStreamFn(modelProvider, taskType, streamFn);
56
56
  }
@@ -122,7 +122,7 @@ class AiJob extends Job {
122
122
  if (context.signal?.aborted) {
123
123
  throw new AbortSignalJobError("Job aborted");
124
124
  }
125
- return await fn(input.taskInput, model, context.updateProgress, context.signal);
125
+ return await fn(input.taskInput, model, context.updateProgress, context.signal, input.outputSchema);
126
126
  };
127
127
  const runFnPromise = runFn();
128
128
  return await Promise.race([runFnPromise, abortPromise]);
@@ -143,7 +143,7 @@ class AiJob extends Job {
143
143
  return;
144
144
  }
145
145
  const model = input.taskInput.model;
146
- yield* streamFn(input.taskInput, model, context.signal);
146
+ yield* streamFn(input.taskInput, model, context.signal, input.outputSchema);
147
147
  }
148
148
  }
149
149
  // src/model/InMemoryModelRepository.ts
@@ -581,7 +581,8 @@ import { convertImageDataToUseableForm } from "@workglow/util";
581
581
  // src/task/base/AiTask.ts
582
582
  import {
583
583
  JobQueueTask,
584
- TaskConfigurationError
584
+ TaskConfigurationError,
585
+ hasStructuredOutput
585
586
  } from "@workglow/task-graph";
586
587
  function schemaFormat(schema) {
587
588
  return typeof schema === "object" && schema !== null && "format" in schema ? schema.format : undefined;
@@ -600,11 +601,21 @@ class AiTask extends JobQueueTask {
600
601
  throw new TaskConfigurationError("AiTask: Model was not resolved to ModelConfig - this indicates a bug in the resolution system");
601
602
  }
602
603
  const runtype = this.constructor.runtype ?? this.constructor.type;
603
- return {
604
+ const jobInput = {
604
605
  taskType: runtype,
605
606
  aiProvider: model.provider,
606
607
  taskInput: input
607
608
  };
609
+ const inputOutputSchema = input.outputSchema;
610
+ if (inputOutputSchema && typeof inputOutputSchema === "object") {
611
+ jobInput.outputSchema = inputOutputSchema;
612
+ } else {
613
+ const taskOutputSchema = this.outputSchema();
614
+ if (hasStructuredOutput(taskOutputSchema)) {
615
+ jobInput.outputSchema = taskOutputSchema;
616
+ }
617
+ }
618
+ return jobInput;
608
619
  }
609
620
  async createJob(input, queueName) {
610
621
  const jobInput = await this.getJobInput(input);
@@ -1018,6 +1029,11 @@ var inputSchema2 = {
1018
1029
  title: "Document ID",
1019
1030
  description: "The document ID"
1020
1031
  },
1032
+ doc_title: {
1033
+ type: "string",
1034
+ title: "Document Title",
1035
+ description: "Human-readable title for the source document"
1036
+ },
1021
1037
  chunks: {
1022
1038
  type: "array",
1023
1039
  items: ChunkNodeSchema(),
@@ -1089,7 +1105,7 @@ class ChunkToVectorTask extends Task2 {
1089
1105
  return outputSchema2;
1090
1106
  }
1091
1107
  async execute(input, context) {
1092
- const { chunks, vectors } = input;
1108
+ const { chunks, vectors, doc_title } = input;
1093
1109
  const chunkArray = chunks;
1094
1110
  if (!chunkArray || !vectors) {
1095
1111
  throw new Error("Both chunks and vector are required");
@@ -1111,6 +1127,7 @@ class ChunkToVectorTask extends Task2 {
1111
1127
  depth: chunk.depth,
1112
1128
  text: chunk.text,
1113
1129
  nodePath: chunk.nodePath,
1130
+ ...doc_title ? { doc_title } : {},
1114
1131
  ...chunk.enrichment || {}
1115
1132
  });
1116
1133
  }
@@ -2012,6 +2029,8 @@ class StreamingAiTask extends AiTask {
2012
2029
  })) {
2013
2030
  if (event.type === "text-delta") {
2014
2031
  yield { ...event, port: event.port ?? defaultPort };
2032
+ } else if (event.type === "object-delta") {
2033
+ yield { ...event, port: event.port ?? defaultPort };
2015
2034
  } else {
2016
2035
  yield event;
2017
2036
  }
@@ -4514,11 +4533,82 @@ var structuralParser = (input, config) => {
4514
4533
  };
4515
4534
  Workflow29.prototype.structuralParser = CreateWorkflow29(StructuralParserTask);
4516
4535
 
4536
+ // src/task/StructuredGenerationTask.ts
4537
+ import { CreateWorkflow as CreateWorkflow30, Workflow as Workflow30 } from "@workglow/task-graph";
4538
+ var modelSchema20 = TypeModel("model:StructuredGenerationTask");
4539
+ var StructuredGenerationInputSchema = {
4540
+ type: "object",
4541
+ properties: {
4542
+ model: modelSchema20,
4543
+ prompt: {
4544
+ type: "string",
4545
+ title: "Prompt",
4546
+ description: "The prompt to generate structured output from"
4547
+ },
4548
+ outputSchema: {
4549
+ type: "object",
4550
+ title: "Output Schema",
4551
+ description: "JSON Schema describing the desired output structure",
4552
+ additionalProperties: true
4553
+ },
4554
+ maxTokens: {
4555
+ type: "number",
4556
+ title: "Max Tokens",
4557
+ description: "The maximum number of tokens to generate",
4558
+ minimum: 1,
4559
+ maximum: 4096,
4560
+ "x-ui-group": "Configuration"
4561
+ },
4562
+ temperature: {
4563
+ type: "number",
4564
+ title: "Temperature",
4565
+ description: "The temperature to use for sampling",
4566
+ minimum: 0,
4567
+ maximum: 2,
4568
+ "x-ui-group": "Configuration"
4569
+ }
4570
+ },
4571
+ required: ["model", "prompt", "outputSchema"],
4572
+ additionalProperties: false
4573
+ };
4574
+ var StructuredGenerationOutputSchema = {
4575
+ type: "object",
4576
+ properties: {
4577
+ object: {
4578
+ type: "object",
4579
+ title: "Structured Output",
4580
+ description: "The generated structured object conforming to the provided schema",
4581
+ "x-stream": "object",
4582
+ "x-structured-output": true,
4583
+ additionalProperties: true
4584
+ }
4585
+ },
4586
+ required: ["object"],
4587
+ additionalProperties: false
4588
+ };
4589
+
4590
+ class StructuredGenerationTask extends StreamingAiTask {
4591
+ static type = "StructuredGenerationTask";
4592
+ static category = "AI Text Model";
4593
+ static title = "Structured Generation";
4594
+ static description = "Generates structured JSON output conforming to a provided schema using language models";
4595
+ static inputSchema() {
4596
+ return StructuredGenerationInputSchema;
4597
+ }
4598
+ static outputSchema() {
4599
+ return StructuredGenerationOutputSchema;
4600
+ }
4601
+ }
4602
+ var structuredGeneration = (input, config) => {
4603
+ return new StructuredGenerationTask({}, config).run(input);
4604
+ };
4605
+ Workflow30.prototype.structuredGeneration = CreateWorkflow30(StructuredGenerationTask);
4606
+
4517
4607
  // src/task/TextChunkerTask.ts
4518
4608
  import {
4519
- CreateWorkflow as CreateWorkflow30,
4609
+ CreateWorkflow as CreateWorkflow31,
4520
4610
  Task as Task13,
4521
- Workflow as Workflow30
4611
+ Workflow as Workflow31
4522
4612
  } from "@workglow/task-graph";
4523
4613
  var ChunkingStrategy = {
4524
4614
  FIXED: "fixed",
@@ -4768,11 +4858,11 @@ class TextChunkerTask extends Task13 {
4768
4858
  var textChunker = (input, config) => {
4769
4859
  return new TextChunkerTask({}, config).run(input);
4770
4860
  };
4771
- Workflow30.prototype.textChunker = CreateWorkflow30(TextChunkerTask);
4861
+ Workflow31.prototype.textChunker = CreateWorkflow31(TextChunkerTask);
4772
4862
 
4773
4863
  // src/task/TextFillMaskTask.ts
4774
- import { CreateWorkflow as CreateWorkflow31, Workflow as Workflow31 } from "@workglow/task-graph";
4775
- var modelSchema20 = TypeModel("model:TextFillMaskTask");
4864
+ import { CreateWorkflow as CreateWorkflow32, Workflow as Workflow32 } from "@workglow/task-graph";
4865
+ var modelSchema21 = TypeModel("model:TextFillMaskTask");
4776
4866
  var TextFillMaskInputSchema = {
4777
4867
  type: "object",
4778
4868
  properties: {
@@ -4781,7 +4871,7 @@ var TextFillMaskInputSchema = {
4781
4871
  title: "Text",
4782
4872
  description: "The text with a mask token to fill"
4783
4873
  },
4784
- model: modelSchema20
4874
+ model: modelSchema21
4785
4875
  },
4786
4876
  required: ["text", "model"],
4787
4877
  additionalProperties: false
@@ -4836,21 +4926,21 @@ class TextFillMaskTask extends AiTask {
4836
4926
  var textFillMask = (input, config) => {
4837
4927
  return new TextFillMaskTask({}, config).run(input);
4838
4928
  };
4839
- Workflow31.prototype.textFillMask = CreateWorkflow31(TextFillMaskTask);
4929
+ Workflow32.prototype.textFillMask = CreateWorkflow32(TextFillMaskTask);
4840
4930
 
4841
4931
  // src/task/TextGenerationTask.ts
4842
- import { CreateWorkflow as CreateWorkflow32, Workflow as Workflow32 } from "@workglow/task-graph";
4932
+ import { CreateWorkflow as CreateWorkflow33, Workflow as Workflow33 } from "@workglow/task-graph";
4843
4933
  var generatedTextSchema2 = {
4844
4934
  type: "string",
4845
4935
  title: "Text",
4846
4936
  description: "The generated text",
4847
4937
  "x-stream": "append"
4848
4938
  };
4849
- var modelSchema21 = TypeModel("model:TextGenerationTask");
4939
+ var modelSchema22 = TypeModel("model:TextGenerationTask");
4850
4940
  var TextGenerationInputSchema = {
4851
4941
  type: "object",
4852
4942
  properties: {
4853
- model: modelSchema21,
4943
+ model: modelSchema22,
4854
4944
  prompt: {
4855
4945
  type: "string",
4856
4946
  title: "Prompt",
@@ -4924,11 +5014,11 @@ class TextGenerationTask extends StreamingAiTask {
4924
5014
  var textGeneration = (input, config) => {
4925
5015
  return new TextGenerationTask({}, config).run(input);
4926
5016
  };
4927
- Workflow32.prototype.textGeneration = CreateWorkflow32(TextGenerationTask);
5017
+ Workflow33.prototype.textGeneration = CreateWorkflow33(TextGenerationTask);
4928
5018
 
4929
5019
  // src/task/TextLanguageDetectionTask.ts
4930
- import { CreateWorkflow as CreateWorkflow33, Workflow as Workflow33 } from "@workglow/task-graph";
4931
- var modelSchema22 = TypeModel("model:TextLanguageDetectionTask");
5020
+ import { CreateWorkflow as CreateWorkflow34, Workflow as Workflow34 } from "@workglow/task-graph";
5021
+ var modelSchema23 = TypeModel("model:TextLanguageDetectionTask");
4932
5022
  var TextLanguageDetectionInputSchema = {
4933
5023
  type: "object",
4934
5024
  properties: {
@@ -4945,7 +5035,7 @@ var TextLanguageDetectionInputSchema = {
4945
5035
  title: "Max Languages",
4946
5036
  description: "The maximum number of languages to return"
4947
5037
  },
4948
- model: modelSchema22
5038
+ model: modelSchema23
4949
5039
  },
4950
5040
  required: ["text", "model"],
4951
5041
  additionalProperties: false
@@ -4995,10 +5085,10 @@ class TextLanguageDetectionTask extends AiTask {
4995
5085
  var textLanguageDetection = (input, config) => {
4996
5086
  return new TextLanguageDetectionTask({}, config).run(input);
4997
5087
  };
4998
- Workflow33.prototype.textLanguageDetection = CreateWorkflow33(TextLanguageDetectionTask);
5088
+ Workflow34.prototype.textLanguageDetection = CreateWorkflow34(TextLanguageDetectionTask);
4999
5089
 
5000
5090
  // src/task/TextQuestionAnswerTask.ts
5001
- import { CreateWorkflow as CreateWorkflow34, Workflow as Workflow34 } from "@workglow/task-graph";
5091
+ import { CreateWorkflow as CreateWorkflow35, Workflow as Workflow35 } from "@workglow/task-graph";
5002
5092
  var contextSchema = {
5003
5093
  type: "string",
5004
5094
  title: "Context",
@@ -5015,13 +5105,13 @@ var textSchema = {
5015
5105
  description: "The generated text",
5016
5106
  "x-stream": "append"
5017
5107
  };
5018
- var modelSchema23 = TypeModel("model:TextQuestionAnswerTask");
5108
+ var modelSchema24 = TypeModel("model:TextQuestionAnswerTask");
5019
5109
  var TextQuestionAnswerInputSchema = {
5020
5110
  type: "object",
5021
5111
  properties: {
5022
5112
  context: contextSchema,
5023
5113
  question: questionSchema,
5024
- model: modelSchema23
5114
+ model: modelSchema24
5025
5115
  },
5026
5116
  required: ["context", "question", "model"],
5027
5117
  additionalProperties: false
@@ -5050,11 +5140,11 @@ class TextQuestionAnswerTask extends StreamingAiTask {
5050
5140
  var textQuestionAnswer = (input, config) => {
5051
5141
  return new TextQuestionAnswerTask({}, config).run(input);
5052
5142
  };
5053
- Workflow34.prototype.textQuestionAnswer = CreateWorkflow34(TextQuestionAnswerTask);
5143
+ Workflow35.prototype.textQuestionAnswer = CreateWorkflow35(TextQuestionAnswerTask);
5054
5144
 
5055
5145
  // src/task/TextRewriterTask.ts
5056
- import { CreateWorkflow as CreateWorkflow35, Workflow as Workflow35 } from "@workglow/task-graph";
5057
- var modelSchema24 = TypeModel("model:TextRewriterTask");
5146
+ import { CreateWorkflow as CreateWorkflow36, Workflow as Workflow36 } from "@workglow/task-graph";
5147
+ var modelSchema25 = TypeModel("model:TextRewriterTask");
5058
5148
  var TextRewriterInputSchema = {
5059
5149
  type: "object",
5060
5150
  properties: {
@@ -5068,7 +5158,7 @@ var TextRewriterInputSchema = {
5068
5158
  title: "Prompt",
5069
5159
  description: "The prompt to direct the rewriting"
5070
5160
  },
5071
- model: modelSchema24
5161
+ model: modelSchema25
5072
5162
  },
5073
5163
  required: ["text", "prompt", "model"],
5074
5164
  additionalProperties: false
@@ -5102,11 +5192,11 @@ class TextRewriterTask extends StreamingAiTask {
5102
5192
  var textRewriter = (input, config) => {
5103
5193
  return new TextRewriterTask({}, config).run(input);
5104
5194
  };
5105
- Workflow35.prototype.textRewriter = CreateWorkflow35(TextRewriterTask);
5195
+ Workflow36.prototype.textRewriter = CreateWorkflow36(TextRewriterTask);
5106
5196
 
5107
5197
  // src/task/TextTranslationTask.ts
5108
- import { CreateWorkflow as CreateWorkflow36, Workflow as Workflow36 } from "@workglow/task-graph";
5109
- var modelSchema25 = TypeModel("model:TextTranslationTask");
5198
+ import { CreateWorkflow as CreateWorkflow37, Workflow as Workflow37 } from "@workglow/task-graph";
5199
+ var modelSchema26 = TypeModel("model:TextTranslationTask");
5110
5200
  var translationTextSchema = {
5111
5201
  type: "string",
5112
5202
  title: "Text",
@@ -5133,7 +5223,7 @@ var TextTranslationInputSchema = {
5133
5223
  minLength: 2,
5134
5224
  maxLength: 2
5135
5225
  }),
5136
- model: modelSchema25
5226
+ model: modelSchema26
5137
5227
  },
5138
5228
  required: ["text", "source_lang", "target_lang", "model"],
5139
5229
  additionalProperties: false
@@ -5168,13 +5258,13 @@ class TextTranslationTask extends StreamingAiTask {
5168
5258
  var textTranslation = (input, config) => {
5169
5259
  return new TextTranslationTask({}, config).run(input);
5170
5260
  };
5171
- Workflow36.prototype.textTranslation = CreateWorkflow36(TextTranslationTask);
5261
+ Workflow37.prototype.textTranslation = CreateWorkflow37(TextTranslationTask);
5172
5262
 
5173
5263
  // src/task/TopicSegmenterTask.ts
5174
5264
  import {
5175
- CreateWorkflow as CreateWorkflow37,
5265
+ CreateWorkflow as CreateWorkflow38,
5176
5266
  Task as Task14,
5177
- Workflow as Workflow37
5267
+ Workflow as Workflow38
5178
5268
  } from "@workglow/task-graph";
5179
5269
  var SegmentationMethod = {
5180
5270
  HEURISTIC: "heuristic",
@@ -5455,15 +5545,15 @@ class TopicSegmenterTask extends Task14 {
5455
5545
  var topicSegmenter = (input, config) => {
5456
5546
  return new TopicSegmenterTask({}, config).run(input);
5457
5547
  };
5458
- Workflow37.prototype.topicSegmenter = CreateWorkflow37(TopicSegmenterTask);
5548
+ Workflow38.prototype.topicSegmenter = CreateWorkflow38(TopicSegmenterTask);
5459
5549
 
5460
5550
  // src/task/UnloadModelTask.ts
5461
- import { CreateWorkflow as CreateWorkflow38, Workflow as Workflow38 } from "@workglow/task-graph";
5462
- var modelSchema26 = TypeModel("model");
5551
+ import { CreateWorkflow as CreateWorkflow39, Workflow as Workflow39 } from "@workglow/task-graph";
5552
+ var modelSchema27 = TypeModel("model");
5463
5553
  var UnloadModelInputSchema = {
5464
5554
  type: "object",
5465
5555
  properties: {
5466
- model: modelSchema26
5556
+ model: modelSchema27
5467
5557
  },
5468
5558
  required: ["model"],
5469
5559
  additionalProperties: false
@@ -5471,7 +5561,7 @@ var UnloadModelInputSchema = {
5471
5561
  var UnloadModelOutputSchema = {
5472
5562
  type: "object",
5473
5563
  properties: {
5474
- model: modelSchema26
5564
+ model: modelSchema27
5475
5565
  },
5476
5566
  required: ["model"],
5477
5567
  additionalProperties: false
@@ -5493,10 +5583,10 @@ class UnloadModelTask extends AiTask {
5493
5583
  var unloadModel = (input, config) => {
5494
5584
  return new UnloadModelTask({}, config).run(input);
5495
5585
  };
5496
- Workflow38.prototype.unloadModel = CreateWorkflow38(UnloadModelTask);
5586
+ Workflow39.prototype.unloadModel = CreateWorkflow39(UnloadModelTask);
5497
5587
 
5498
5588
  // src/task/VectorQuantizeTask.ts
5499
- import { CreateWorkflow as CreateWorkflow39, Task as Task15, Workflow as Workflow39 } from "@workglow/task-graph";
5589
+ import { CreateWorkflow as CreateWorkflow40, Task as Task15, Workflow as Workflow40 } from "@workglow/task-graph";
5500
5590
  import {
5501
5591
  normalizeNumberArray,
5502
5592
  TensorType,
@@ -5676,10 +5766,10 @@ class VectorQuantizeTask extends Task15 {
5676
5766
  var vectorQuantize = (input, config) => {
5677
5767
  return new VectorQuantizeTask({}, config).run(input);
5678
5768
  };
5679
- Workflow39.prototype.vectorQuantize = CreateWorkflow39(VectorQuantizeTask);
5769
+ Workflow40.prototype.vectorQuantize = CreateWorkflow40(VectorQuantizeTask);
5680
5770
 
5681
5771
  // src/task/VectorSimilarityTask.ts
5682
- import { CreateWorkflow as CreateWorkflow40, GraphAsTask, Workflow as Workflow40 } from "@workglow/task-graph";
5772
+ import { CreateWorkflow as CreateWorkflow41, GraphAsTask, Workflow as Workflow41 } from "@workglow/task-graph";
5683
5773
  import {
5684
5774
  cosineSimilarity,
5685
5775
  hammingSimilarity,
@@ -5785,7 +5875,7 @@ class VectorSimilarityTask extends GraphAsTask {
5785
5875
  var similarity = (input, config) => {
5786
5876
  return new VectorSimilarityTask({}, config).run(input);
5787
5877
  };
5788
- Workflow40.prototype.similarity = CreateWorkflow40(VectorSimilarityTask);
5878
+ Workflow41.prototype.similarity = CreateWorkflow41(VectorSimilarityTask);
5789
5879
 
5790
5880
  // src/task/index.ts
5791
5881
  var registerAiTasks = () => {
@@ -5815,6 +5905,7 @@ var registerAiTasks = () => {
5815
5905
  QueryExpanderTask,
5816
5906
  RerankerTask,
5817
5907
  StructuralParserTask,
5908
+ StructuredGenerationTask,
5818
5909
  TextChunkerTask,
5819
5910
  TextClassificationTask,
5820
5911
  TextEmbeddingTask,
@@ -5850,6 +5941,7 @@ export {
5850
5941
  textEmbedding,
5851
5942
  textClassification,
5852
5943
  textChunker,
5944
+ structuredGeneration,
5853
5945
  structuralParser,
5854
5946
  similarity,
5855
5947
  setGlobalModelRepository,
@@ -5924,6 +6016,9 @@ export {
5924
6016
  TextClassificationOutputSchema,
5925
6017
  TextClassificationInputSchema,
5926
6018
  TextChunkerTask,
6019
+ StructuredGenerationTask,
6020
+ StructuredGenerationOutputSchema,
6021
+ StructuredGenerationInputSchema,
5927
6022
  StructuralParserTask,
5928
6023
  StreamingAiTask,
5929
6024
  SimilarityFn,
@@ -5991,4 +6086,4 @@ export {
5991
6086
  AiJob
5992
6087
  };
5993
6088
 
5994
- //# debugId=8EB68A2DA4788C9664756E2164756E21
6089
+ //# debugId=B846F012C7AD687E64756E2164756E21