@workglow/ai 0.0.89 → 0.0.91

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.
Files changed (35) hide show
  1. package/README.md +16 -12
  2. package/dist/browser.js +175 -13
  3. package/dist/browser.js.map +16 -13
  4. package/dist/bun.js +175 -13
  5. package/dist/bun.js.map +16 -13
  6. package/dist/common.d.ts +1 -0
  7. package/dist/common.d.ts.map +1 -1
  8. package/dist/job/AiJob.d.ts +6 -1
  9. package/dist/job/AiJob.d.ts.map +1 -1
  10. package/dist/model/ModelRepository.d.ts +2 -26
  11. package/dist/model/ModelRepository.d.ts.map +1 -1
  12. package/dist/node.js +175 -13
  13. package/dist/node.js.map +16 -13
  14. package/dist/provider/AiProvider.d.ts +140 -0
  15. package/dist/provider/AiProvider.d.ts.map +1 -0
  16. package/dist/provider/AiProviderRegistry.d.ts +44 -1
  17. package/dist/provider/AiProviderRegistry.d.ts.map +1 -1
  18. package/dist/queue/createDefaultQueue.d.ts +17 -0
  19. package/dist/queue/createDefaultQueue.d.ts.map +1 -0
  20. package/dist/task/TextGenerationTask.d.ts +3 -2
  21. package/dist/task/TextGenerationTask.d.ts.map +1 -1
  22. package/dist/task/TextQuestionAnswerTask.d.ts +3 -2
  23. package/dist/task/TextQuestionAnswerTask.d.ts.map +1 -1
  24. package/dist/task/TextRewriterTask.d.ts +3 -2
  25. package/dist/task/TextRewriterTask.d.ts.map +1 -1
  26. package/dist/task/TextSummaryTask.d.ts +3 -2
  27. package/dist/task/TextSummaryTask.d.ts.map +1 -1
  28. package/dist/task/TextTranslationTask.d.ts +3 -2
  29. package/dist/task/TextTranslationTask.d.ts.map +1 -1
  30. package/dist/task/VectorQuantizeTask.d.ts.map +1 -1
  31. package/dist/task/base/StreamingAiTask.d.ts +31 -0
  32. package/dist/task/base/StreamingAiTask.d.ts.map +1 -0
  33. package/dist/task/index.d.ts +1 -0
  34. package/dist/task/index.d.ts.map +1 -1
  35. package/package.json +11 -11
package/dist/bun.js CHANGED
@@ -12,6 +12,17 @@ import { globalServiceRegistry, WORKER_MANAGER } from "@workglow/util";
12
12
 
13
13
  class AiProviderRegistry {
14
14
  runFnRegistry = new Map;
15
+ streamFnRegistry = new Map;
16
+ providers = new Map;
17
+ registerProvider(provider) {
18
+ this.providers.set(provider.name, provider);
19
+ }
20
+ getProvider(name) {
21
+ return this.providers.get(name);
22
+ }
23
+ getProviders() {
24
+ return new Map(this.providers);
25
+ }
15
26
  registerRunFn(modelProvider, taskType, runFn) {
16
27
  if (!this.runFnRegistry.has(taskType)) {
17
28
  this.runFnRegistry.set(taskType, new Map);
@@ -29,6 +40,23 @@ class AiProviderRegistry {
29
40
  };
30
41
  this.registerRunFn(modelProvider, taskType, workerFn);
31
42
  }
43
+ registerStreamFn(modelProvider, taskType, streamFn) {
44
+ if (!this.streamFnRegistry.has(taskType)) {
45
+ this.streamFnRegistry.set(taskType, new Map);
46
+ }
47
+ this.streamFnRegistry.get(taskType).set(modelProvider, streamFn);
48
+ }
49
+ registerAsWorkerStreamFn(modelProvider, taskType) {
50
+ const streamFn = async function* (input, model, signal) {
51
+ const workerManager = globalServiceRegistry.get(WORKER_MANAGER);
52
+ yield* workerManager.callWorkerStreamFunction(modelProvider, taskType, [input, model], { signal });
53
+ };
54
+ this.registerStreamFn(modelProvider, taskType, streamFn);
55
+ }
56
+ getStreamFn(modelProvider, taskType) {
57
+ const taskTypeMap = this.streamFnRegistry.get(taskType);
58
+ return taskTypeMap?.get(modelProvider);
59
+ }
32
60
  getDirectRunFn(modelProvider, taskType) {
33
61
  const taskTypeMap = this.runFnRegistry.get(taskType);
34
62
  const runFn = taskTypeMap?.get(modelProvider);
@@ -82,6 +110,19 @@ class AiJob extends Job {
82
110
  }
83
111
  }
84
112
  }
113
+ async* executeStream(input, context) {
114
+ if (context.signal.aborted || this.status === JobStatus.ABORTING) {
115
+ throw new AbortSignalJobError("Abort signal aborted before streaming execution of job");
116
+ }
117
+ const streamFn = getAiProviderRegistry().getStreamFn(input.aiProvider, input.taskType);
118
+ if (!streamFn) {
119
+ const result = await this.execute(input, context);
120
+ yield { type: "finish", data: result };
121
+ return;
122
+ }
123
+ const model = input.taskInput.model;
124
+ yield* streamFn(input.taskInput, model, context.signal);
125
+ }
85
126
  }
86
127
  // src/model/InMemoryModelRepository.ts
87
128
  import { InMemoryTabularStorage } from "@workglow/storage";
@@ -239,6 +280,100 @@ async function resolveModelFromRegistry(id, format, registry) {
239
280
  return model;
240
281
  }
241
282
  registerInputResolver("model", resolveModelFromRegistry);
283
+ // src/provider/AiProvider.ts
284
+ import { globalServiceRegistry as globalServiceRegistry3, WORKER_MANAGER as WORKER_MANAGER2 } from "@workglow/util";
285
+
286
+ // src/queue/createDefaultQueue.ts
287
+ import { ConcurrencyLimiter, JobQueueClient, JobQueueServer } from "@workglow/job-queue";
288
+ import { InMemoryQueueStorage } from "@workglow/storage";
289
+ import { getTaskQueueRegistry } from "@workglow/task-graph";
290
+ async function createDefaultQueue(providerName, concurrency) {
291
+ const storage = new InMemoryQueueStorage(providerName);
292
+ await storage.setupDatabase();
293
+ const server = new JobQueueServer(AiJob, {
294
+ storage,
295
+ queueName: providerName,
296
+ limiter: new ConcurrencyLimiter(concurrency, 100)
297
+ });
298
+ const client = new JobQueueClient({
299
+ storage,
300
+ queueName: providerName
301
+ });
302
+ client.attach(server);
303
+ getTaskQueueRegistry().registerQueue({ server, client, storage });
304
+ await server.start();
305
+ }
306
+
307
+ // src/provider/AiProvider.ts
308
+ class AiProvider {
309
+ tasks;
310
+ streamTasks;
311
+ constructor(tasks, streamTasks) {
312
+ this.tasks = tasks;
313
+ this.streamTasks = streamTasks;
314
+ }
315
+ get supportedTaskTypes() {
316
+ return this.taskTypes;
317
+ }
318
+ getRunFn(taskType) {
319
+ return this.tasks?.[taskType];
320
+ }
321
+ getStreamFn(taskType) {
322
+ return this.streamTasks?.[taskType];
323
+ }
324
+ async register(options = { mode: "inline" }) {
325
+ await this.onInitialize(options);
326
+ if (options.mode === "worker") {
327
+ if (!options.worker) {
328
+ throw new Error(`AiProvider "${this.name}": worker is required when mode is "worker". ` + `Pass a Web Worker instance, e.g. register({ mode: "worker", worker: new Worker(...) }).`);
329
+ }
330
+ } else {
331
+ if (!this.tasks) {
332
+ throw new Error(`AiProvider "${this.name}": tasks must be provided via the constructor for inline mode. ` + `Pass the tasks record when constructing the provider, e.g. new MyProvider(MY_TASKS).`);
333
+ }
334
+ }
335
+ const registry = getAiProviderRegistry();
336
+ if (options.mode === "worker" && options.worker) {
337
+ const workerManager = globalServiceRegistry3.get(WORKER_MANAGER2);
338
+ workerManager.registerWorker(this.name, options.worker);
339
+ for (const taskType of this.taskTypes) {
340
+ registry.registerAsWorkerRunFn(this.name, taskType);
341
+ registry.registerAsWorkerStreamFn(this.name, taskType);
342
+ }
343
+ } else {
344
+ for (const [taskType, fn] of Object.entries(this.tasks)) {
345
+ registry.registerRunFn(this.name, taskType, fn);
346
+ }
347
+ if (this.streamTasks) {
348
+ for (const [taskType, fn] of Object.entries(this.streamTasks)) {
349
+ registry.registerStreamFn(this.name, taskType, fn);
350
+ }
351
+ }
352
+ }
353
+ registry.registerProvider(this);
354
+ if (options.queue?.autoCreate !== false) {
355
+ await this.createQueue(options.queue?.concurrency ?? 1);
356
+ }
357
+ }
358
+ registerOnWorkerServer(workerServer) {
359
+ if (!this.tasks) {
360
+ throw new Error(`AiProvider "${this.name}": tasks must be provided via the constructor for worker server registration. ` + `Pass the tasks record when constructing the provider, e.g. new MyProvider(MY_TASKS).`);
361
+ }
362
+ for (const [taskType, fn] of Object.entries(this.tasks)) {
363
+ workerServer.registerFunction(taskType, fn);
364
+ }
365
+ if (this.streamTasks) {
366
+ for (const [taskType, fn] of Object.entries(this.streamTasks)) {
367
+ workerServer.registerStreamFunction(taskType, fn);
368
+ }
369
+ }
370
+ }
371
+ async onInitialize(_options) {}
372
+ async dispose() {}
373
+ async createQueue(concurrency) {
374
+ await createDefaultQueue(this.name, concurrency);
375
+ }
376
+ }
242
377
  // src/task/index.ts
243
378
  import { TaskRegistry } from "@workglow/task-graph";
244
379
 
@@ -1706,6 +1841,26 @@ Workflow9.prototype.textNamedEntityRecognition = CreateWorkflow9(TextNamedEntity
1706
1841
 
1707
1842
  // src/task/TextSummaryTask.ts
1708
1843
  import { CreateWorkflow as CreateWorkflow10, Workflow as Workflow10 } from "@workglow/task-graph";
1844
+
1845
+ // src/task/base/StreamingAiTask.ts
1846
+ class StreamingAiTask extends AiTask {
1847
+ static type = "StreamingAiTask";
1848
+ async* executeStream(input, context) {
1849
+ const jobInput = await this.getJobInput(input);
1850
+ const queueName = await this.getDefaultQueueName(input);
1851
+ const job = new AiJob({
1852
+ queueName: queueName ?? this.type,
1853
+ jobRunId: this.config.runnerId,
1854
+ input: jobInput
1855
+ });
1856
+ yield* job.executeStream(jobInput, {
1857
+ signal: context.signal,
1858
+ updateProgress: context.updateProgress.bind(this)
1859
+ });
1860
+ }
1861
+ }
1862
+
1863
+ // src/task/TextSummaryTask.ts
1709
1864
  var modelSchema4 = TypeModel("model:TextSummaryTask");
1710
1865
  var TextSummaryInputSchema = {
1711
1866
  type: "object",
@@ -1726,14 +1881,15 @@ var TextSummaryOutputSchema = {
1726
1881
  text: {
1727
1882
  type: "string",
1728
1883
  title: "Text",
1729
- description: "The summarized text"
1884
+ description: "The summarized text",
1885
+ "x-stream": "append"
1730
1886
  }
1731
1887
  },
1732
1888
  required: ["text"],
1733
1889
  additionalProperties: false
1734
1890
  };
1735
1891
 
1736
- class TextSummaryTask extends AiTask {
1892
+ class TextSummaryTask extends StreamingAiTask {
1737
1893
  static type = "TextSummaryTask";
1738
1894
  static category = "AI Text Model";
1739
1895
  static title = "Text Summary";
@@ -4494,7 +4650,8 @@ import { CreateWorkflow as CreateWorkflow31, Workflow as Workflow31 } from "@wor
4494
4650
  var generatedTextSchema2 = {
4495
4651
  type: "string",
4496
4652
  title: "Text",
4497
- description: "The generated text"
4653
+ description: "The generated text",
4654
+ "x-stream": "append"
4498
4655
  };
4499
4656
  var modelSchema18 = TypeModel("model:TextGenerationTask");
4500
4657
  var TextGenerationInputSchema = {
@@ -4559,7 +4716,7 @@ var TextGenerationOutputSchema = {
4559
4716
  additionalProperties: false
4560
4717
  };
4561
4718
 
4562
- class TextGenerationTask extends AiTask {
4719
+ class TextGenerationTask extends StreamingAiTask {
4563
4720
  static type = "TextGenerationTask";
4564
4721
  static category = "AI Text Model";
4565
4722
  static title = "Text Generation";
@@ -4662,7 +4819,8 @@ var questionSchema = {
4662
4819
  var textSchema = {
4663
4820
  type: "string",
4664
4821
  title: "Text",
4665
- description: "The generated text"
4822
+ description: "The generated text",
4823
+ "x-stream": "append"
4666
4824
  };
4667
4825
  var modelSchema20 = TypeModel("model:TextQuestionAnswerTask");
4668
4826
  var TextQuestionAnswerInputSchema = {
@@ -4684,7 +4842,7 @@ var TextQuestionAnswerOutputSchema = {
4684
4842
  additionalProperties: false
4685
4843
  };
4686
4844
 
4687
- class TextQuestionAnswerTask extends AiTask {
4845
+ class TextQuestionAnswerTask extends StreamingAiTask {
4688
4846
  static type = "TextQuestionAnswerTask";
4689
4847
  static category = "AI Text Model";
4690
4848
  static title = "Text Question Answer";
@@ -4728,14 +4886,15 @@ var TextRewriterOutputSchema = {
4728
4886
  text: {
4729
4887
  type: "string",
4730
4888
  title: "Text",
4731
- description: "The rewritten text"
4889
+ description: "The rewritten text",
4890
+ "x-stream": "append"
4732
4891
  }
4733
4892
  },
4734
4893
  required: ["text"],
4735
4894
  additionalProperties: false
4736
4895
  };
4737
4896
 
4738
- class TextRewriterTask extends AiTask {
4897
+ class TextRewriterTask extends StreamingAiTask {
4739
4898
  static type = "TextRewriterTask";
4740
4899
  static category = "AI Text Model";
4741
4900
  static title = "Text Rewriter";
@@ -4758,7 +4917,8 @@ var modelSchema22 = TypeModel("model:TextTranslationTask");
4758
4917
  var translationTextSchema = {
4759
4918
  type: "string",
4760
4919
  title: "Text",
4761
- description: "The translated text"
4920
+ description: "The translated text",
4921
+ "x-stream": "replace"
4762
4922
  };
4763
4923
  var TextTranslationInputSchema = {
4764
4924
  type: "object",
@@ -4800,7 +4960,7 @@ var TextTranslationOutputSchema = {
4800
4960
  additionalProperties: false
4801
4961
  };
4802
4962
 
4803
- class TextTranslationTask extends AiTask {
4963
+ class TextTranslationTask extends StreamingAiTask {
4804
4964
  static type = "TextTranslationTask";
4805
4965
  static category = "AI Text Model";
4806
4966
  static title = "Text Translation";
@@ -5225,8 +5385,8 @@ var outputSchema15 = {
5225
5385
 
5226
5386
  class VectorQuantizeTask extends Task15 {
5227
5387
  static type = "VectorQuantizeTask";
5228
- static category = "Vector Processing";
5229
- static title = "Quantize Vector";
5388
+ static category = "Vector";
5389
+ static title = "Quantize";
5230
5390
  static description = "Quantize vectors to reduce storage and improve performance";
5231
5391
  static cacheable = true;
5232
5392
  static inputSchema() {
@@ -5570,6 +5730,7 @@ export {
5570
5730
  TextClassificationInputSchema,
5571
5731
  TextChunkerTask,
5572
5732
  StructuralParserTask,
5733
+ StreamingAiTask,
5573
5734
  SimilarityFn,
5574
5735
  SegmentationMethod,
5575
5736
  RerankerTask,
@@ -5628,7 +5789,8 @@ export {
5628
5789
  BackgroundRemovalInputSchema,
5629
5790
  AiTask,
5630
5791
  AiProviderRegistry,
5792
+ AiProvider,
5631
5793
  AiJob
5632
5794
  };
5633
5795
 
5634
- //# debugId=2BE731EEB6308C6864756E2164756E21
5796
+ //# debugId=DA970B9D5234412964756E2164756E21