@workglow/task-graph 0.1.1 → 0.1.2

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 (34) hide show
  1. package/dist/browser.js +96 -5
  2. package/dist/browser.js.map +9 -8
  3. package/dist/bun.js +96 -5
  4. package/dist/bun.js.map +9 -8
  5. package/dist/node.js +96 -5
  6. package/dist/node.js.map +9 -8
  7. package/dist/storage/TaskGraphRepository.d.ts.map +1 -1
  8. package/dist/storage/TaskGraphTabularRepository.d.ts.map +1 -1
  9. package/dist/storage/TaskOutputRepository.d.ts.map +1 -1
  10. package/dist/storage/TaskOutputTabularRepository.d.ts.map +1 -1
  11. package/dist/task/ConditionalTask.d.ts.map +1 -1
  12. package/dist/task/FallbackTask.d.ts +20 -20
  13. package/dist/task/FallbackTask.d.ts.map +1 -1
  14. package/dist/task/GraphAsTask.d.ts.map +1 -1
  15. package/dist/task/InputCompactor.d.ts +37 -0
  16. package/dist/task/InputCompactor.d.ts.map +1 -0
  17. package/dist/task/InputResolver.d.ts +18 -0
  18. package/dist/task/InputResolver.d.ts.map +1 -1
  19. package/dist/task/IteratorTask.d.ts.map +1 -1
  20. package/dist/task/MapTask.d.ts.map +1 -1
  21. package/dist/task/ReduceTask.d.ts.map +1 -1
  22. package/dist/task/TaskJSON.d.ts +4 -4
  23. package/dist/task/TaskJSON.d.ts.map +1 -1
  24. package/dist/task/TaskRegistry.d.ts.map +1 -1
  25. package/dist/task/TaskTypes.d.ts.map +1 -1
  26. package/dist/task/WhileTask.d.ts.map +1 -1
  27. package/dist/task/index.d.ts +1 -0
  28. package/dist/task/index.d.ts.map +1 -1
  29. package/dist/task-graph/GraphToWorkflowCode.d.ts.map +1 -1
  30. package/dist/task-graph/TaskGraphEvents.d.ts.map +1 -1
  31. package/dist/task-graph/TaskGraphRunner.d.ts.map +1 -1
  32. package/dist/task-graph/Workflow.d.ts +5 -2
  33. package/dist/task-graph/Workflow.d.ts.map +1 -1
  34. package/package.json +7 -7
package/dist/node.js CHANGED
@@ -3658,10 +3658,11 @@ class WorkflowTask extends GraphAsTask {
3658
3658
  }
3659
3659
 
3660
3660
  class Workflow {
3661
- constructor(cache, parent, iteratorTask) {
3661
+ constructor(cache, parent, iteratorTask, registry) {
3662
3662
  this._outputCache = cache;
3663
3663
  this._parentWorkflow = parent;
3664
3664
  this._iteratorTask = iteratorTask;
3665
+ this._registry = registry ?? parent?._registry;
3665
3666
  this._graph = new TaskGraph({ outputCache: this._outputCache });
3666
3667
  if (!parent) {
3667
3668
  this._onChanged = this._onChanged.bind(this);
@@ -3672,6 +3673,7 @@ class Workflow {
3672
3673
  _dataFlows = [];
3673
3674
  _error = "";
3674
3675
  _outputCache;
3676
+ _registry;
3675
3677
  _abortController;
3676
3678
  _parentWorkflow;
3677
3679
  _iteratorTask;
@@ -3785,7 +3787,7 @@ class Workflow {
3785
3787
  const output = await this.graph.run(input, {
3786
3788
  parentSignal: this._abortController.signal,
3787
3789
  outputCache: this._outputCache,
3788
- registry: config?.registry
3790
+ registry: config?.registry ?? this._registry
3789
3791
  });
3790
3792
  const results = this.graph.mergeExecuteOutputsToRunOutput(output, PROPERTY_ARRAY);
3791
3793
  this.events.emit("complete");
@@ -3946,7 +3948,7 @@ class Workflow {
3946
3948
  return this;
3947
3949
  }
3948
3950
  addTaskToGraph(taskClass, input, config) {
3949
- const task = new taskClass(input, config);
3951
+ const task = new taskClass(input, config, this._registry ? { registry: this._registry } : undefined);
3950
3952
  const id = this.graph.addTask(task);
3951
3953
  this.events.emit("changed", id);
3952
3954
  return task;
@@ -3972,7 +3974,7 @@ class Workflow {
3972
3974
  });
3973
3975
  this._dataFlows = [];
3974
3976
  }
3975
- const loopBuilder = new Workflow(this.outputCache(), this, task);
3977
+ const loopBuilder = new Workflow(this.outputCache(), this, task, this._registry);
3976
3978
  if (parent) {
3977
3979
  loopBuilder._pendingLoopConnect = { parent, iteratorTask: task };
3978
3980
  }
@@ -4558,7 +4560,18 @@ function extractLoopConfig(task) {
4558
4560
  }
4559
4561
  return config;
4560
4562
  }
4563
+ function stripUndefined(obj) {
4564
+ if (!obj)
4565
+ return obj;
4566
+ const result = {};
4567
+ for (const [k, v] of Object.entries(obj)) {
4568
+ if (v !== undefined)
4569
+ result[k] = v;
4570
+ }
4571
+ return Object.keys(result).length > 0 ? result : undefined;
4572
+ }
4561
4573
  function buildMethodArgs(defaults, config, baseIndent = "", columnOffset = 0) {
4574
+ defaults = stripUndefined(defaults);
4562
4575
  const hasDefaults = defaults && Object.keys(defaults).length > 0;
4563
4576
  const hasConfig = Object.keys(config).length > 0;
4564
4577
  if (!hasDefaults && !hasConfig)
@@ -4833,6 +4846,68 @@ queueMicrotask(() => {
4833
4846
  };
4834
4847
  Workflow.prototype.endFallbackWith = CreateEndLoopWorkflow("endFallbackWith");
4835
4848
  });
4849
+ // src/task/InputCompactor.ts
4850
+ import { getInputCompactors } from "@workglow/util";
4851
+ function schemaAllowsString(schema) {
4852
+ if (typeof schema !== "object" || schema === null)
4853
+ return false;
4854
+ const s = schema;
4855
+ if (s.type === "string")
4856
+ return true;
4857
+ const variants = s.oneOf ?? s.anyOf;
4858
+ if (Array.isArray(variants)) {
4859
+ for (const variant of variants) {
4860
+ if (schemaAllowsString(variant))
4861
+ return true;
4862
+ }
4863
+ }
4864
+ return false;
4865
+ }
4866
+ async function compactSchemaInputs(input, schema, config) {
4867
+ if (typeof schema === "boolean")
4868
+ return input;
4869
+ const properties = schema.properties;
4870
+ if (!properties || typeof properties !== "object")
4871
+ return input;
4872
+ const compactors = getInputCompactors();
4873
+ const compacted = { ...input };
4874
+ for (const [key, propSchema] of Object.entries(properties)) {
4875
+ let value = compacted[key];
4876
+ const format = getSchemaFormat(propSchema);
4877
+ if (format) {
4878
+ let compactor = compactors.get(format);
4879
+ if (!compactor) {
4880
+ const prefix = getFormatPrefix(format);
4881
+ compactor = compactors.get(prefix);
4882
+ }
4883
+ if (compactor) {
4884
+ if (value !== null && value !== undefined && typeof value === "object" && !Array.isArray(value) && schemaAllowsString(propSchema)) {
4885
+ const id = await compactor(value, format, config.registry);
4886
+ if (id !== undefined) {
4887
+ compacted[key] = id;
4888
+ continue;
4889
+ }
4890
+ } else if (Array.isArray(value)) {
4891
+ compacted[key] = await Promise.all(value.map(async (item) => {
4892
+ if (item !== null && item !== undefined && typeof item === "object" && !Array.isArray(item)) {
4893
+ const id = await compactor(item, format, config.registry);
4894
+ return id !== undefined ? id : item;
4895
+ }
4896
+ return item;
4897
+ }));
4898
+ continue;
4899
+ }
4900
+ }
4901
+ }
4902
+ if (value !== null && value !== undefined && typeof value === "object" && !Array.isArray(value)) {
4903
+ const objectSchema = getObjectSchema(propSchema);
4904
+ if (objectSchema) {
4905
+ compacted[key] = await compactSchemaInputs(value, objectSchema, config);
4906
+ }
4907
+ }
4908
+ }
4909
+ return compacted;
4910
+ }
4836
4911
  // src/task/IteratorTaskRunner.ts
4837
4912
  import { uuid4 as uuid46 } from "@workglow/util";
4838
4913
  class IteratorTaskRunner extends GraphAsTaskRunner {
@@ -6334,6 +6409,7 @@ queueMicrotask(() => {
6334
6409
  import {
6335
6410
  createServiceToken as createServiceToken3,
6336
6411
  globalServiceRegistry as globalServiceRegistry4,
6412
+ registerInputCompactor,
6337
6413
  registerInputResolver
6338
6414
  } from "@workglow/util";
6339
6415
  var taskConstructors = new Map;
@@ -6376,6 +6452,17 @@ function resolveTaskFromRegistry(id, _format, registry) {
6376
6452
  };
6377
6453
  }
6378
6454
  registerInputResolver("tasks", resolveTaskFromRegistry);
6455
+ registerInputCompactor("tasks", (value, _format, registry) => {
6456
+ if (typeof value === "object" && value !== null && "name" in value) {
6457
+ const name = value.name;
6458
+ if (typeof name !== "string")
6459
+ return;
6460
+ const constructors = getTaskConstructors(registry);
6461
+ const ctor = constructors.get(name);
6462
+ return ctor ? name : undefined;
6463
+ }
6464
+ return;
6465
+ });
6379
6466
 
6380
6467
  // src/task/TaskJSON.ts
6381
6468
  var createSingleTaskFromJSON = (item, registry, options) => {
@@ -6684,8 +6771,10 @@ export {
6684
6771
  getTaskConstructors,
6685
6772
  getStructuredOutputSchemas,
6686
6773
  getStreamingPorts,
6774
+ getSchemaFormat,
6687
6775
  getPortStreamMode,
6688
6776
  getOutputStreamMode,
6777
+ getObjectSchema,
6689
6778
  getObjectPortId,
6690
6779
  getNestedValue,
6691
6780
  getLastTask,
@@ -6693,6 +6782,7 @@ export {
6693
6782
  getIterationContextSchemaForType,
6694
6783
  getInputModeFromSchema,
6695
6784
  getGlobalTaskConstructors,
6785
+ getFormatPrefix,
6696
6786
  getAppendPortId,
6697
6787
  formatValue,
6698
6788
  findArrayPorts,
@@ -6714,6 +6804,7 @@ export {
6714
6804
  conditionalTaskConfigSchema,
6715
6805
  computeGraphOutputSchema,
6716
6806
  computeGraphInputSchema,
6807
+ compactSchemaInputs,
6717
6808
  calculateNodeDepths,
6718
6809
  buildIterationInputSchema,
6719
6810
  addIterationContextToSchema,
@@ -6777,4 +6868,4 @@ export {
6777
6868
  ConditionalTask
6778
6869
  };
6779
6870
 
6780
- //# debugId=2758F4534C10DA3764756E2164756E21
6871
+ //# debugId=F0FF9D519957480A64756E2164756E21