@highstate/contract 0.7.11 → 0.9.0

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,5 +1,5 @@
1
1
  {
2
2
  "sourceHashes": {
3
- "./dist/index.js": "c144f026d937ec67e3a1cb070005d763cd3ba564785c2329275e0056d3864a3a"
3
+ "./dist/index.js": "f7b0cc65c29d5fdc3bbfca9b60237a9a7b3afed6364ce7e0a4179c6fc739de15"
4
4
  }
5
5
  }
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ function findInput(inputs, name) {
17
17
  if (matchedInputs.length === 0) {
18
18
  return null;
19
19
  }
20
- if (1 < matchedInputs.length) {
20
+ if (matchedInputs.length > 1) {
21
21
  throw new Error(
22
22
  `Multiple inputs found for "${name}": ${matchedInputs.map((input) => input.instanceId).join(", ")}. Specify the full instance id to disambiguate.`
23
23
  );
@@ -31,6 +31,12 @@ function findRequiredInput(inputs, name) {
31
31
  }
32
32
  return input;
33
33
  }
34
+ function findInputs(inputs, names) {
35
+ return names.map((name) => findInput(inputs, name)).filter(Boolean);
36
+ }
37
+ function findRequiredInputs(inputs, names) {
38
+ return names.map((name) => findRequiredInput(inputs, name));
39
+ }
34
40
 
35
41
  // src/evaluation.ts
36
42
  import { mapValues as mapValues3 } from "remeda";
@@ -61,13 +67,12 @@ function trimIndentation(text2) {
61
67
  }
62
68
 
63
69
  // src/component.ts
64
- var ajv = new Ajv();
70
+ var ajv = new Ajv({ strict: false });
65
71
  var originalCreate = Symbol("originalCreate");
66
72
  function defineComponent(options) {
67
73
  function create(params) {
68
74
  const { name, args, inputs } = params;
69
75
  const id = `${options.type}:${name}`;
70
- validateArgs(id, create.model, args ?? {});
71
76
  const flatInputs = mapValues(pickBy(inputs ?? {}, isNonNullish), (inputs2) => [inputs2].flat(2));
72
77
  return registerInstance(
73
78
  create.model,
@@ -90,7 +95,11 @@ function defineComponent(options) {
90
95
  const outputs = options.create({
91
96
  id,
92
97
  name,
93
- args: args ?? {},
98
+ args: validateArgsAndFillDefaults(
99
+ id,
100
+ create.model,
101
+ args ? { ...args } : {}
102
+ ),
94
103
  inputs: markedInputs
95
104
  }) ?? {};
96
105
  return mapValues(pickBy(outputs, isNonNullish), (outputs2) => [outputs2].flat(2));
@@ -154,16 +163,21 @@ function createInputMapper(entities) {
154
163
  };
155
164
  };
156
165
  }
157
- function validateArgs(instanceId, model, args) {
166
+ function validateArgsAndFillDefaults(instanceId, model, args) {
158
167
  for (const [key, argModel] of Object.entries(model.args)) {
159
- const value = args[key];
160
- if (!value && argModel.required) {
161
- throw new Error(`Missing required argument "${key}" for instance "${instanceId}"`);
168
+ let value = args[key];
169
+ if (!value) {
170
+ if (argModel.required) {
171
+ throw new Error(`Missing required argument "${key}" for instance "${instanceId}"`);
172
+ }
173
+ value = argModel.schema.default;
174
+ args[key] = value;
162
175
  }
163
176
  if (value && !ajv.validate(argModel.schema, value)) {
164
177
  throw new Error(`Invalid argument "${key}" for instance "${instanceId}": ${ajv.errorsText()}`);
165
178
  }
166
179
  }
180
+ return args;
167
181
  }
168
182
 
169
183
  // src/unit.ts
@@ -249,13 +263,19 @@ import "@sinclair/typebox";
249
263
 
250
264
  // src/index.ts
251
265
  import {} from "@sinclair/typebox";
252
- import { Type as BaseType } from "@sinclair/typebox";
266
+ import {
267
+ Type as BaseType
268
+ } from "@sinclair/typebox";
253
269
  function StringEnum(values) {
254
270
  return Type.Union(values.map((value) => Type.Literal(value)));
255
271
  }
272
+ function Default(schema, defaultValue) {
273
+ return { ...schema, default: defaultValue };
274
+ }
256
275
  var Type = {
257
276
  ...BaseType,
258
- StringEnum
277
+ StringEnum,
278
+ Default
259
279
  };
260
280
  export {
261
281
  Type,
@@ -263,7 +283,9 @@ export {
263
283
  defineEntity,
264
284
  defineUnit,
265
285
  findInput,
286
+ findInputs,
266
287
  findRequiredInput,
288
+ findRequiredInputs,
267
289
  getCompositeInstances,
268
290
  getInstanceId,
269
291
  isComponent,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/instance.ts","../src/evaluation.ts","../src/unit.ts","../src/component.ts","../src/utils.ts","../src/entity.ts","../src/types.ts","../src/index.ts"],"sourcesContent":["declare const type: unique symbol\n\nexport type InstanceInput<TType extends string = string> = {\n [type]?: TType\n [boundaryInput]?: InstanceInput\n instanceId: string\n output: string\n}\n\nexport const boundaryInput = Symbol(\"boundaryInput\")\n\nexport type HubInput = {\n hubId: string\n}\n\nexport type Position = {\n x: number\n y: number\n}\n\nexport type InstanceModel = {\n /**\n * The id of the instance unique within the project.\n *\n * The format is `${instanceType}:${instanceName}`.\n */\n id: string\n\n /**\n * The type of the instance.\n */\n type: string\n\n /**\n * The name of the instance.\n *\n * Must be unique within instances of the same type in the project.\n */\n name: string\n\n /**\n * The static arguments passed to the instance.\n */\n args?: Record<string, unknown>\n\n /**\n * The direct instances passed as inputs to the instance.\n */\n inputs?: Record<string, InstanceInput[]>\n\n /**\n * The resolved unit inputs for the instance.\n *\n * Only for computed composite instances.\n */\n resolvedInputs?: Record<string, InstanceInput[]>\n\n /**\n * The inputs passed to the instance from the hubs.\n *\n * Only for designer-first instances.\n */\n hubInputs?: Record<string, HubInput[]>\n\n /**\n * The inputs injected to the instance from the hubs.\n *\n * While `hubInputs` allows to pass hubs to distinct inputs,\n * `injectionInputs` allows to pass hubs to the instance as a whole filling all inputs with matching types.\n *\n * Only for designer-first instances.\n */\n injectionInputs?: HubInput[]\n\n /**\n * The position of the instance on the canvas.\n *\n * Only for designer-first instances.\n */\n position?: Position\n\n /**\n * The id of the top level parent instance.\n *\n * Only for child instances of the composite instances.\n */\n parentId?: string\n\n /**\n * The direct instance inputs and same-level children outputs returned by the instance as outputs.\n *\n * Only for computed composite instances.\n */\n outputs?: Record<string, InstanceInput[]>\n\n /**\n * The resolved unit outputs for the instance.\n *\n * Only for computed composite instances.\n */\n resolvedOutputs?: Record<string, InstanceInput[]>\n}\n\n/**\n * Formats the instance id from the instance type and instance name.\n *\n * @param instanceType The type of the instance.\n * @param instanceName The name of the instance.\n *\n * @returns The formatted instance id.\n */\nexport function getInstanceId(instanceType: string, instanceName: string): string {\n return `${instanceType}:${instanceName}`\n}\n\n/**\n * Parses the instance id into the instance type and instance name.\n *\n * @param instanceId The instance id to parse.\n *\n * @returns The instance type and instance name.\n */\nexport function parseInstanceId(instanceId: string): [instanceType: string, instanceName: string] {\n const parts = instanceId.split(\":\")\n\n if (parts.length !== 2) {\n throw new Error(`Invalid instance key: ${instanceId}`)\n }\n\n return parts as [string, string]\n}\n\nexport function findInput<T extends string>(\n inputs: InstanceInput<T>[],\n name: string,\n): InstanceInput<T> | null {\n const matchedInputs = inputs.filter(\n input => parseInstanceId(input.instanceId)[1] === name || input.instanceId === name,\n )\n\n if (matchedInputs.length === 0) {\n return null\n }\n\n if (1 < matchedInputs.length) {\n throw new Error(\n `Multiple inputs found for \"${name}\": ${matchedInputs.map(input => input.instanceId).join(\", \")}. Specify the full instance id to disambiguate.`,\n )\n }\n\n return matchedInputs[0]\n}\n\nexport function findRequiredInput<T extends string>(\n inputs: InstanceInput<T>[],\n name: string,\n): InstanceInput<T> {\n const input = findInput(inputs, name)\n\n if (input === null) {\n throw new Error(`Required input \"${name}\" not found.`)\n }\n\n return input\n}\n","import type { ComponentModel } from \"./component\"\nimport { mapValues } from \"remeda\"\nimport { boundaryInput, type InstanceInput, type InstanceModel } from \"./instance\"\nimport { isUnitModel } from \"./unit\"\n\nexport type CompositeInstance = {\n instance: InstanceModel\n children: InstanceModel[]\n}\n\nconst compositeInstances: Map<string, CompositeInstance> = new Map()\nlet currentCompositeInstance: CompositeInstance | null = null\n\nexport function resetEvaluation(): void {\n compositeInstances.clear()\n currentCompositeInstance = null\n}\n\nexport function getCompositeInstances(): CompositeInstance[] {\n return Array.from(compositeInstances.values())\n}\n\nexport function registerInstance<T>(\n component: ComponentModel,\n instance: InstanceModel,\n fn: () => T,\n): T {\n if (currentCompositeInstance) {\n instance.parentId = currentCompositeInstance.instance.id\n currentCompositeInstance.children.push(instance)\n }\n\n let previousParentInstance: CompositeInstance | null = null\n if (!isUnitModel(component)) {\n previousParentInstance = currentCompositeInstance\n currentCompositeInstance = { instance, children: [] }\n compositeInstances.set(currentCompositeInstance.instance.id, currentCompositeInstance)\n }\n\n try {\n const outputs = fn() as Record<string, InstanceInput[]>\n\n instance.resolvedOutputs = outputs\n instance.outputs = mapValues(outputs ?? {}, outputs =>\n outputs.map(output => output[boundaryInput] ?? output),\n )\n\n // mark all outputs with the boundary input of the instance\n return mapValues(outputs, (outputs, outputKey) =>\n outputs.map(output => ({\n ...output,\n [boundaryInput]: { instanceId: instance.id, output: outputKey },\n })),\n ) as T\n } finally {\n if (previousParentInstance) {\n currentCompositeInstance = previousParentInstance\n }\n }\n}\n","/* eslint-disable @typescript-eslint/no-unsafe-return */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\n\nimport type { ArgumentValue } from \"./types\"\nimport type { InstanceInput } from \"./instance\"\nimport { mapValues } from \"remeda\"\nimport {\n type ArgumentOptionsMapToStatic,\n type Component,\n type ComponentArgument,\n type ComponentArgumentOptions,\n type ComponentInputOptions,\n type ComponentInputOptionsMapToSpecMap,\n type ComponentInputSpec,\n type ComponentModel,\n type ComponentOptions,\n defineComponent,\n mapArgument,\n} from \"./component\"\n\ntype UnitOptions<\n TArgs extends Record<string, ComponentArgumentOptions>,\n TInputs extends Record<string, ComponentInputOptions>,\n TOutputs extends Record<string, ComponentInputOptions>,\n TSecrets extends Record<string, ComponentArgumentOptions>,\n> = Omit<ComponentOptions<TArgs, TInputs, TOutputs>, \"create\"> & {\n source: UnitSource\n\n secrets?: TSecrets\n}\n\nexport type UnitSource = {\n package: string\n path?: string\n version?: string\n}\n\nexport type UnitModel = ComponentModel & {\n /**\n * The source of the unit.\n */\n source: UnitSource\n\n /**\n * The record of the secret argument schemas.\n */\n secrets: Record<string, ComponentArgument>\n}\n\nexport type Unit<\n TArgs extends Record<string, ArgumentValue> = Record<string, never>,\n TInputs extends Record<string, ComponentInputSpec> = Record<string, never>,\n TOutputs extends Record<string, ComponentInputSpec> = Record<string, never>,\n TSecrets extends Record<string, ArgumentValue> = Record<string, never>,\n> = Component<TArgs, TInputs, TOutputs> & {\n __secrets: TSecrets\n\n model: UnitModel\n}\n\nexport function isUnitModel(model: ComponentModel): model is UnitModel {\n return \"source\" in model\n}\n\nexport function defineUnit<\n TArgs extends Record<string, ComponentArgumentOptions> = Record<string, never>,\n TInputs extends Record<string, ComponentInputOptions> = Record<string, never>,\n TOutputs extends Record<string, ComponentInputOptions> = Record<string, never>,\n TSecrets extends Record<string, ComponentArgumentOptions> = Record<string, never>,\n>(\n options: UnitOptions<TArgs, TInputs, TOutputs, TSecrets>,\n): Unit<\n ArgumentOptionsMapToStatic<TArgs>,\n ComponentInputOptionsMapToSpecMap<TInputs>,\n ComponentInputOptionsMapToSpecMap<TOutputs>,\n ArgumentOptionsMapToStatic<TSecrets>\n> {\n const component = defineComponent<TArgs, TInputs, TOutputs>({\n ...options,\n\n create({ id }) {\n const outputs: Record<string, InstanceInput[]> = {}\n for (const key in options.outputs ?? {}) {\n outputs[key] = [\n {\n instanceId: id,\n output: key,\n },\n ]\n }\n\n return outputs as any\n },\n }) as any\n\n component.model.source = options.source ?? {}\n component.model.secrets = mapValues(options.secrets ?? {}, mapArgument)\n\n return component\n}\n","/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\n/* eslint-disable @typescript-eslint/no-unsafe-return */\n\nimport type { Entity } from \"./entity\"\nimport type { ArgumentValue, ArgumentValueSchema, Meta } from \"./types\"\nimport {\n type Static,\n type TObject,\n type TOptional,\n type TSchema,\n OptionalKind,\n} from \"@sinclair/typebox\"\nimport { isNonNullish, mapValues, pickBy } from \"remeda\"\nimport { Ajv } from \"ajv\"\nimport { boundaryInput, type InstanceInput } from \"./instance\"\nimport { type OptionalUndefinedFields, type OptionalEmptyRecords } from \"./utils\"\nimport { registerInstance } from \"./evaluation\"\n\nconst ajv = new Ajv()\n\n// Argument\nexport type ComponentArgument = {\n schema: ArgumentValueSchema\n required: boolean\n meta: Meta\n}\n\ntype ComponentArgumentFullOptions = Meta & {\n schema: ArgumentValueSchema\n required?: boolean\n}\n\nexport type ComponentArgumentOptions = ArgumentValueSchema | ComponentArgumentFullOptions\n\nexport type ComponentArgumentOptionsToSchema<T extends ComponentArgumentOptions> = T extends TSchema\n ? T\n : T[\"required\"] extends false\n ? TOptional<T[\"schema\"]>\n : T[\"schema\"]\n\ntype ComponentArgumentMapToValue<T extends Record<string, ComponentArgumentOptions>> = {\n [K in keyof T]: Static<ComponentArgumentOptionsToSchema<T[K]>>\n}\n\n// Input\nexport type ComponentInput = {\n type: string\n required: boolean\n multiple: boolean\n meta: Meta\n}\n\ntype ComponentInputFullOptions = Meta & {\n entity: Entity\n required?: boolean\n multiple?: boolean\n}\n\nexport type ComponentInputOptions = Entity | ComponentInputFullOptions\n\ntype ComponentInputOptionsToOutputRef<T extends ComponentInputOptions> = T extends Entity\n ? InstanceInput<T[\"type\"]>\n : T extends ComponentInputFullOptions\n ? T[\"required\"] extends false\n ? T[\"multiple\"] extends true\n ? InstanceInput<T[\"entity\"][\"type\"]>[] | undefined\n : InstanceInput<T[\"entity\"][\"type\"]> | undefined\n : T[\"multiple\"] extends true\n ? InstanceInput<T[\"entity\"][\"type\"]>[]\n : InstanceInput<T[\"entity\"][\"type\"]>\n : never\n\nexport type ComponentInputSpec = [entity: Entity, required: boolean, multiple: boolean]\n\nexport type ComponentInputOptionsToSpec<T extends ComponentInputOptions> = T extends Entity\n ? [T, true, false] // [Entity, required, multiple]\n : T extends ComponentInputFullOptions\n ? T[\"required\"] extends false\n ? T[\"multiple\"] extends true\n ? [T[\"entity\"], false, true]\n : [T[\"entity\"], false, false]\n : T[\"multiple\"] extends true\n ? [T[\"entity\"], true, true]\n : [T[\"entity\"], true, false]\n : never\n\nexport type ComponentInputOptionsMapToSpecMap<T extends Record<string, ComponentInputOptions>> =\n T extends Record<string, never>\n ? Record<string, never>\n : { [K in keyof T]: ComponentInputOptionsToSpec<T[K]> }\n\ntype ComponentInputMapToValue<T extends Record<string, ComponentInputOptions>> =\n OptionalUndefinedFields<{\n [K in keyof T]: ComponentInputOptionsToOutputRef<T[K]>\n }>\n\ntype ComponentInputMapToReturnType<T extends Record<string, ComponentInputOptions>> =\n T extends Record<string, never> ? void : ComponentInputMapToValue<T>\n\n// Params & Options\nexport type ComponentParams<\n TArgs extends Record<string, ComponentArgumentOptions>,\n TInputs extends Record<string, ComponentInputOptions>,\n> = {\n id: string\n name: string\n args: ComponentArgumentMapToValue<TArgs>\n inputs: ComponentInputMapToValue<TInputs>\n}\n\nexport type InputComponentParams<\n TArgs extends Record<string, ArgumentValue>,\n TInputs extends Record<string, unknown>,\n> = {\n name: string\n} & OptionalEmptyRecords<{\n args: TArgs\n inputs: TInputs\n}>\n\nexport type ComponentOptions<\n TArgs extends Record<string, ComponentArgumentOptions>,\n TInputs extends Record<string, ComponentInputOptions>,\n TOutputs extends Record<string, ComponentInputOptions>,\n> = {\n type: string\n meta?: ComponentMeta\n\n args?: TArgs\n inputs?: TInputs\n outputs?: TOutputs\n\n create: (params: ComponentParams<TArgs, TInputs>) => ComponentInputMapToReturnType<TOutputs>\n}\n\n// Models\nexport type ComponentMeta = Meta & {\n primaryIcon?: string\n primaryIconColor?: string\n secondaryIcon?: string\n secondaryIconColor?: string\n category?: string\n defaultNamePrefix?: string\n}\n\nexport type ComponentModel = {\n /**\n * The type of the component.\n */\n type: string\n\n /**\n * The record of the argument schemas.\n */\n args: Record<string, ComponentArgument>\n\n /**\n * The record of the input schemas.\n */\n inputs: Record<string, ComponentInput>\n\n /**\n * The record of the output schemas.\n */\n outputs: Record<string, ComponentInput>\n\n /**\n * The extra metadata of the component.\n */\n meta: ComponentMeta\n\n /**\n * The hash of the component definition.\n */\n definitionHash: string\n}\n\ntype InputSpecToOutputRef<T extends ComponentInputSpec> = T[1] extends true\n ? T[2] extends true\n ? InstanceInput<T[0][\"type\"]>[]\n : InstanceInput<T[0][\"type\"]>\n : T[2] extends true\n ? InstanceInput<T[0][\"type\"]>[] | undefined\n : InstanceInput<T[0][\"type\"]> | undefined\n\nexport type OutputRefMap<TInputs extends Record<string, ComponentInputSpec>> =\n TInputs extends Record<string, [string, never, never]>\n ? Record<string, never>\n : { [K in keyof TInputs]: InputSpecToOutputRef<TInputs[K]> }\n\nexport const originalCreate = Symbol(\"originalCreate\")\n\nexport type Component<\n TArgs extends Record<string, ArgumentValue> = Record<string, never>,\n TInputs extends Record<string, ComponentInputSpec> = Record<string, never>,\n TOutputs extends Record<string, ComponentInputSpec> = Record<string, never>,\n> = {\n /**\n * The non-generic model of the component.\n */\n model: ComponentModel\n\n /**\n * The entities used in the inputs or outputs of the component.\n */\n entities: Map<string, Entity>\n\n /**\n * Creates the component at the evaluation time.\n */\n (context: InputComponentParams<TArgs, OutputRefMap<TInputs>>): OutputRefMap<TOutputs>\n\n /**\n * The original create function.\n *\n * Used to calculate the definition hash.\n */\n [originalCreate]: (params: InputComponentParams<any, any>) => any\n}\n\nexport type ArgumentOptionsMapToStatic<T extends Record<string, ComponentArgumentOptions>> =\n T extends Record<string, never>\n ? Record<string, never>\n : Static<TObject<{ [K in keyof T]: ComponentArgumentOptionsToSchema<T[K]> }>>\n\nexport function defineComponent<\n TArgs extends Record<string, ComponentArgumentOptions> = Record<string, never>,\n TInputs extends Record<string, ComponentInputOptions> = Record<string, never>,\n TOutputs extends Record<string, ComponentInputOptions> = Record<string, never>,\n>(\n options: ComponentOptions<TArgs, TInputs, TOutputs>,\n): Component<\n ArgumentOptionsMapToStatic<TArgs>,\n ComponentInputOptionsMapToSpecMap<TInputs>,\n ComponentInputOptionsMapToSpecMap<TOutputs>\n> {\n function create(params: InputComponentParams<any, any>): any {\n const { name, args, inputs } = params\n const id = `${options.type}:${name}`\n\n validateArgs(id, create.model as ComponentModel, args ?? {})\n\n const flatInputs = mapValues(pickBy(inputs ?? {}, isNonNullish), inputs => [inputs].flat(2))\n\n return registerInstance(\n create.model as ComponentModel,\n {\n id,\n type: options.type,\n name,\n args: args ?? {},\n inputs: mapValues(flatInputs, inputs => inputs.map(input => input[boundaryInput] ?? input)),\n resolvedInputs: flatInputs,\n },\n () => {\n const markedInputs = mapValues(flatInputs, (inputs, key) => {\n const result = inputs.map(input => ({\n ...input,\n [boundaryInput]: { instanceId: id, output: key },\n }))\n\n return (create.model as ComponentModel).inputs?.[key]?.multiple === false\n ? result[0]\n : result\n })\n\n const outputs: Record<string, InstanceInput[]> =\n options.create({\n id,\n name,\n args: (args as any) ?? {},\n inputs: markedInputs as any,\n }) ?? {}\n\n return mapValues(pickBy(outputs, isNonNullish), outputs => [outputs].flat(2))\n },\n )\n }\n\n create.entities = new Map<string, Entity>()\n const mapInput = createInputMapper(create.entities)\n\n create.model = {\n type: options.type,\n args: mapValues(options.args ?? {}, mapArgument),\n inputs: mapValues(options.inputs ?? {}, mapInput),\n outputs: mapValues(options.outputs ?? {}, mapInput),\n meta: options.meta ?? {},\n }\n\n create[originalCreate] = options.create\n\n return create as any\n}\n\nexport function isComponent(value: unknown): value is Component {\n return typeof value === \"function\" && \"model\" in value\n}\n\nexport function mapArgument(value: ComponentArgumentOptions) {\n if (\"schema\" in value) {\n return {\n schema: value.schema,\n required: value.required ?? (!value.schema[OptionalKind] && !value.schema.default),\n meta: {\n displayName: value.displayName,\n description: value.description,\n color: value.color,\n },\n }\n }\n\n return {\n schema: value,\n required: !value[OptionalKind] && !value.default,\n meta: {},\n }\n}\n\nexport function createInputMapper(entities: Map<string, Entity>) {\n return (value: ComponentInputOptions) => {\n if (\"entity\" in value) {\n entities.set(value.entity.type, value.entity)\n\n return {\n type: value.entity.type,\n required: value.required ?? true,\n multiple: value.multiple ?? false,\n meta: {\n displayName: value.displayName,\n description: value.description,\n color: value.color,\n },\n }\n }\n\n entities.set(value.type, value)\n\n return {\n type: value.type,\n required: true,\n multiple: false,\n meta: {},\n }\n }\n}\n\nfunction validateArgs(instanceId: string, model: ComponentModel, args: Record<string, unknown>) {\n for (const [key, argModel] of Object.entries(model.args)) {\n const value = args[key]\n if (!value && argModel.required) {\n throw new Error(`Missing required argument \"${key}\" for instance \"${instanceId}\"`)\n }\n\n if (value && !ajv.validate(argModel.schema, value)) {\n throw new Error(`Invalid argument \"${key}\" for instance \"${instanceId}\": ${ajv.errorsText()}`)\n }\n }\n}\n","export type EntityMap<T> = Record<string, T> & {\n default?: T\n}\n\nexport type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>\nexport type RequiredKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>\n\n// TODO: refactor this shit\n\ntype PickUndefinedKeys<T extends Record<string, unknown>> = T extends Record<string, never>\n ? never\n : Exclude<\n {\n [K in keyof T]: undefined extends T[K] ? K : never\n }[keyof T],\n undefined\n >\n\ntype AllOptionalKeys<T extends Record<string, unknown>> = T extends Record<string, never>\n ? never\n : Exclude<\n {\n [K in keyof T]: undefined extends T[K] ? K : never\n }[keyof T],\n undefined\n >\n\ntype HasRequired<T extends Record<string, unknown>> = T extends Record<string, never>\n ? false\n : [keyof T] extends [AllOptionalKeys<T>]\n ? false\n : true\n\ntype PickRecordsWithAnyRequired<T extends Record<string, Record<string, unknown>>> =\n T extends Record<string, never>\n ? never\n : Exclude<\n {\n [K in keyof T]: HasRequired<T[K]> extends true ? K : never\n }[keyof T],\n undefined\n >\n\nexport type OptionalEmptyRecords<T extends Record<string, Record<string, unknown>>> = {\n [K in Exclude<keyof T, PickRecordsWithAnyRequired<T>>]?: OptionalUndefinedFields<T[K]>\n} & {\n [K in PickRecordsWithAnyRequired<T>]: OptionalUndefinedFields<T[K]>\n}\n\nexport type OptionalUndefinedFields<T extends Record<string, unknown>> = {\n [K in PickUndefinedKeys<T>]?: T[K]\n} & {\n [K in Exclude<keyof T, PickUndefinedKeys<T>>]: T[K]\n}\n\n/**\n * Formats a multiline string and trims the indentation.\n *\n * @param str The string to trim.\n * @returns The trimmed string.\n */\nexport function text(array: TemplateStringsArray, ...values: unknown[]): string {\n const str = array.reduce(\n // eslint-disable-next-line @typescript-eslint/no-base-to-string\n (result, part, i) => result + part + (values[i] ? String(values[i]) : \"\"),\n \"\",\n )\n\n return trimIndentation(str)\n}\n\n/**\n * Removes the indentation from a multiline string.\n *\n * @param text The text to trim.\n * @returns The trimmed text.\n */\nexport function trimIndentation(text: string): string {\n const lines = text.split(\"\\n\")\n const indent = lines\n .filter(line => line.trim() !== \"\")\n .map(line => line.match(/^\\s*/)?.[0].length ?? 0)\n .reduce((min, indent) => Math.min(min, indent), Infinity)\n\n return lines\n .map(line => line.slice(indent))\n .join(\"\\n\")\n .trim()\n}\n","import type { TAnySchema, TSchema } from \"@sinclair/typebox\"\nimport type { Meta } from \"./types\"\nimport type { PartialKeys } from \"./utils\"\n\n/**\n * The entity is some abstract object which can be passed from one component to another through their inputs and outputs.\n * Every entity must have a type.\n * Every component inputs and outputs will reference such types and only entities of the same type can be passed.\n */\nexport type Entity<TType extends string = string, TEntitySchema extends TSchema = TSchema> = {\n /**\n * The static type of the entity.\n */\n type: TType\n\n /**\n * The JSON schema of the entity value.\n */\n schema: TEntitySchema\n\n /**\n * The extra metadata of the entity.\n */\n meta: Meta\n\n /**\n * The hash of the entity definition.\n */\n definitionHash: string\n}\n\nexport type EntityOptions<TType extends string, TSchema extends TAnySchema> = Omit<\n PartialKeys<Entity<TType, TSchema>, \"meta\">,\n \"definitionHash\"\n>\n\nexport function defineEntity<TType extends string, TSchema extends TAnySchema>(\n options: EntityOptions<TType, TSchema>,\n): Entity<TType, TSchema> {\n return {\n meta: {},\n ...options,\n } as Entity<TType, TSchema>\n}\n\nexport function isEntity(value: unknown): value is Entity {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"type\" in value &&\n \"schema\" in value &&\n \"meta\" in value\n )\n}\n","import {\n type Static,\n type TAny,\n type TArray,\n type TBoolean,\n type TLiteral,\n type TNumber,\n type TRecordOrObject,\n type TString,\n type TUnion,\n} from \"@sinclair/typebox\"\n\nexport type ArgumentValueSchema =\n | TString\n | TNumber\n | TBoolean\n | TArray<TString>\n | TArray<TNumber>\n | TUnion<TLiteral[]>\n | TRecordOrObject<TString, TAny>\n | TArray\n\nexport type ArgumentValue = Static<ArgumentValueSchema>\n\n/**\n * The generic metadata of some contract.\n */\nexport type Meta = {\n /**\n * The display name of the entity which can be shown in the UI.\n */\n displayName?: string\n\n /**\n * The description of the entity which can be shown in the UI.\n */\n description?: string\n\n /**\n * The hex color of the entity which can be used in the UI.\n *\n * @example \"#ff0000\"\n */\n color?: string\n}\n","export {\n type InstanceInput,\n type HubInput,\n type InstanceModel,\n type Position,\n getInstanceId,\n parseInstanceId,\n findInput,\n findRequiredInput,\n} from \"./instance\"\nexport { getCompositeInstances, resetEvaluation, type CompositeInstance } from \"./evaluation\"\nexport { type Entity, defineEntity, isEntity } from \"./entity\"\nexport {\n type Component,\n type ComponentModel,\n type ComponentMeta,\n type ComponentArgument,\n type ComponentInput,\n type ComponentInputSpec,\n defineComponent,\n isComponent,\n originalCreate,\n} from \"./component\"\nexport { type Unit, type UnitModel, type UnitSource, defineUnit, isUnitModel } from \"./unit\"\nexport { type RequiredKeys, type PartialKeys, text, trimIndentation } from \"./utils\"\nexport { type ArgumentValue, type ArgumentValueSchema } from \"./types\"\nexport { type Static } from \"@sinclair/typebox\"\n\nimport { Type as BaseType, type TLiteral, type TUnion } from \"@sinclair/typebox\"\n\ntype MapToLiteral<T extends readonly string[]> = {\n [K in keyof T]: T[K] extends string ? TLiteral<T[K]> : never\n}\n\nfunction StringEnum<T extends string[]>(values: [...T]): TUnion<MapToLiteral<T>> {\n return Type.Union(values.map(value => Type.Literal(value))) as TUnion<MapToLiteral<T>>\n}\n\nexport const Type = {\n ...BaseType,\n StringEnum,\n} as typeof BaseType & { StringEnum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AAU,SACV,YAAY,SACZ,UAAU;AAEd;;;ACrDA,OAUO;;;ACgBP,eAA4B;AAE5B,SAAS,QAAQ,gBAA4C;AAM7D,SAAS,WAA+B,QAAyC;AAC/E,SAAO,KAAK,MAAM,OAAO,IAAI,WAAS,KAAK,QAAQ,KAAK,CAAC,CAAC;AAC5D;AAEO,IAAM,OAAO;AAAA,EAClB,GAAG;AAAA,EACH;AACF;","names":["mapValues","mapValues","text","indent","inputs","outputs","mapValues","mapValues","outputs"]}
1
+ {"version":3,"sources":["../src/instance.ts","../src/evaluation.ts","../src/unit.ts","../src/component.ts","../src/utils.ts","../src/entity.ts","../src/types.ts","../src/index.ts"],"sourcesContent":["declare const type: unique symbol\n\nexport type InstanceInput<TType extends string = string> = {\n [type]?: TType\n [boundaryInput]?: InstanceInput\n instanceId: string\n output: string\n}\n\nexport const boundaryInput = Symbol(\"boundaryInput\")\n\nexport type HubInput = {\n hubId: string\n}\n\nexport type Position = {\n x: number\n y: number\n}\n\nexport type InstanceModel = {\n /**\n * The id of the instance unique within the project.\n *\n * The format is `${instanceType}:${instanceName}`.\n */\n id: string\n\n /**\n * The type of the instance.\n */\n type: string\n\n /**\n * The name of the instance.\n *\n * Must be unique within instances of the same type in the project.\n */\n name: string\n\n /**\n * The static arguments passed to the instance.\n */\n args?: Record<string, unknown>\n\n /**\n * The direct instances passed as inputs to the instance.\n */\n inputs?: Record<string, InstanceInput[]>\n\n /**\n * The resolved unit inputs for the instance.\n *\n * Only for computed composite instances.\n */\n resolvedInputs?: Record<string, InstanceInput[]>\n\n /**\n * The inputs passed to the instance from the hubs.\n *\n * Only for designer-first instances.\n */\n hubInputs?: Record<string, HubInput[]>\n\n /**\n * The inputs injected to the instance from the hubs.\n *\n * While `hubInputs` allows to pass hubs to distinct inputs,\n * `injectionInputs` allows to pass hubs to the instance as a whole filling all inputs with matching types.\n *\n * Only for designer-first instances.\n */\n injectionInputs?: HubInput[]\n\n /**\n * The position of the instance on the canvas.\n *\n * Only for designer-first instances.\n */\n position?: Position\n\n /**\n * The id of the top level parent instance.\n *\n * Only for child instances of the composite instances.\n */\n parentId?: string\n\n /**\n * The direct instance inputs and same-level children outputs returned by the instance as outputs.\n *\n * Only for computed composite instances.\n */\n outputs?: Record<string, InstanceInput[]>\n\n /**\n * The resolved unit outputs for the instance.\n *\n * Only for computed composite instances.\n */\n resolvedOutputs?: Record<string, InstanceInput[]>\n}\n\n/**\n * Formats the instance id from the instance type and instance name.\n *\n * @param instanceType The type of the instance.\n * @param instanceName The name of the instance.\n *\n * @returns The formatted instance id.\n */\nexport function getInstanceId(instanceType: string, instanceName: string): string {\n return `${instanceType}:${instanceName}`\n}\n\n/**\n * Parses the instance id into the instance type and instance name.\n *\n * @param instanceId The instance id to parse.\n *\n * @returns The instance type and instance name.\n */\nexport function parseInstanceId(instanceId: string): [instanceType: string, instanceName: string] {\n const parts = instanceId.split(\":\")\n\n if (parts.length !== 2) {\n throw new Error(`Invalid instance key: ${instanceId}`)\n }\n\n return parts as [string, string]\n}\n\nexport function findInput<T extends string>(\n inputs: InstanceInput<T>[],\n name: string,\n): InstanceInput<T> | null {\n const matchedInputs = inputs.filter(\n input => parseInstanceId(input.instanceId)[1] === name || input.instanceId === name,\n )\n\n if (matchedInputs.length === 0) {\n return null\n }\n\n if (matchedInputs.length > 1) {\n throw new Error(\n `Multiple inputs found for \"${name}\": ${matchedInputs.map(input => input.instanceId).join(\", \")}. Specify the full instance id to disambiguate.`,\n )\n }\n\n return matchedInputs[0]\n}\n\nexport function findRequiredInput<T extends string>(\n inputs: InstanceInput<T>[],\n name: string,\n): InstanceInput<T> {\n const input = findInput(inputs, name)\n\n if (input === null) {\n throw new Error(`Required input \"${name}\" not found.`)\n }\n\n return input\n}\n\nexport function findInputs<T extends string>(\n inputs: InstanceInput<T>[],\n names: string[],\n): InstanceInput<T>[] {\n return names.map(name => findInput(inputs, name)).filter(Boolean) as InstanceInput<T>[]\n}\n\nexport function findRequiredInputs<T extends string>(\n inputs: InstanceInput<T>[],\n names: string[],\n): InstanceInput<T>[] {\n return names.map(name => findRequiredInput(inputs, name))\n}\n","import type { ComponentModel } from \"./component\"\nimport { mapValues } from \"remeda\"\nimport { boundaryInput, type InstanceInput, type InstanceModel } from \"./instance\"\nimport { isUnitModel } from \"./unit\"\n\nexport type CompositeInstance = {\n instance: InstanceModel\n children: InstanceModel[]\n}\n\nconst compositeInstances: Map<string, CompositeInstance> = new Map()\nlet currentCompositeInstance: CompositeInstance | null = null\n\nexport function resetEvaluation(): void {\n compositeInstances.clear()\n currentCompositeInstance = null\n}\n\nexport function getCompositeInstances(): CompositeInstance[] {\n return Array.from(compositeInstances.values())\n}\n\nexport function registerInstance<T>(\n component: ComponentModel,\n instance: InstanceModel,\n fn: () => T,\n): T {\n if (currentCompositeInstance) {\n instance.parentId = currentCompositeInstance.instance.id\n currentCompositeInstance.children.push(instance)\n }\n\n let previousParentInstance: CompositeInstance | null = null\n if (!isUnitModel(component)) {\n previousParentInstance = currentCompositeInstance\n currentCompositeInstance = { instance, children: [] }\n compositeInstances.set(currentCompositeInstance.instance.id, currentCompositeInstance)\n }\n\n try {\n const outputs = fn() as Record<string, InstanceInput[]>\n\n instance.resolvedOutputs = outputs\n instance.outputs = mapValues(outputs ?? {}, outputs =>\n outputs.map(output => output[boundaryInput] ?? output),\n )\n\n // mark all outputs with the boundary input of the instance\n return mapValues(outputs, (outputs, outputKey) =>\n outputs.map(output => ({\n ...output,\n [boundaryInput]: { instanceId: instance.id, output: outputKey },\n })),\n ) as T\n } finally {\n if (previousParentInstance) {\n currentCompositeInstance = previousParentInstance\n }\n }\n}\n","/* eslint-disable @typescript-eslint/no-unsafe-return */\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\n\nimport type { ArgumentValue } from \"./types\"\nimport type { InstanceInput } from \"./instance\"\nimport { mapValues } from \"remeda\"\nimport {\n type ComponentArgumentSpec,\n type Component,\n type ComponentArgument,\n type ComponentArgumentOptions,\n type ComponentInputOptions,\n type ComponentInputOptionsMapToSpecMap,\n type ComponentInputSpec,\n type ComponentModel,\n type ComponentOptions,\n defineComponent,\n mapArgument,\n type ComponentArgumentOptionsMapToSpecMap,\n} from \"./component\"\n\ntype UnitOptions<\n TArgs extends Record<string, ComponentArgumentOptions>,\n TInputs extends Record<string, ComponentInputOptions>,\n TOutputs extends Record<string, ComponentInputOptions>,\n TSecrets extends Record<string, ComponentArgumentOptions>,\n> = Omit<ComponentOptions<TArgs, TInputs, TOutputs>, \"create\"> & {\n source: UnitSource\n\n secrets?: TSecrets\n}\n\nexport type UnitSource = {\n package: string\n path?: string\n version?: string\n}\n\nexport type UnitModel = ComponentModel & {\n /**\n * The source of the unit.\n */\n source: UnitSource\n\n /**\n * The record of the secret argument schemas.\n */\n secrets: Record<string, ComponentArgument>\n}\n\nexport type Unit<\n TArgs extends Record<string, ComponentArgumentSpec> = Record<string, never>,\n TInputs extends Record<string, ComponentInputSpec> = Record<string, never>,\n TOutputs extends Record<string, ComponentInputSpec> = Record<string, never>,\n TSecrets extends Record<string, ArgumentValue> = Record<string, never>,\n> = Component<TArgs, TInputs, TOutputs> & {\n __secrets: TSecrets\n\n model: UnitModel\n}\n\nexport function isUnitModel(model: ComponentModel): model is UnitModel {\n return \"source\" in model\n}\n\nexport function defineUnit<\n TArgs extends Record<string, ComponentArgumentOptions> = Record<string, never>,\n TInputs extends Record<string, ComponentInputOptions> = Record<string, never>,\n TOutputs extends Record<string, ComponentInputOptions> = Record<string, never>,\n TSecrets extends Record<string, ComponentArgumentOptions> = Record<string, never>,\n>(\n options: UnitOptions<TArgs, TInputs, TOutputs, TSecrets>,\n): Unit<\n ComponentArgumentOptionsMapToSpecMap<TArgs>,\n ComponentInputOptionsMapToSpecMap<TInputs>,\n ComponentInputOptionsMapToSpecMap<TOutputs>,\n ComponentArgumentOptionsMapToSpecMap<TSecrets>\n> {\n const component = defineComponent<TArgs, TInputs, TOutputs>({\n ...options,\n\n create({ id }) {\n const outputs: Record<string, InstanceInput[]> = {}\n for (const key in options.outputs ?? {}) {\n outputs[key] = [\n {\n instanceId: id,\n output: key,\n },\n ]\n }\n\n return outputs as any\n },\n }) as any\n\n component.model.source = options.source ?? {}\n component.model.secrets = mapValues(options.secrets ?? {}, mapArgument)\n\n return component\n}\n","/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable @typescript-eslint/no-unsafe-assignment */\n/* eslint-disable @typescript-eslint/no-unsafe-return */\n\nimport type { Entity } from \"./entity\"\nimport type { ArgumentValue, ArgumentValueSchema, Meta } from \"./types\"\nimport {\n type Static,\n type TObject,\n type TOptional,\n type TSchema,\n OptionalKind,\n} from \"@sinclair/typebox\"\nimport { isNonNullish, mapValues, pickBy } from \"remeda\"\nimport { Ajv } from \"ajv\"\nimport { boundaryInput, type InstanceInput } from \"./instance\"\nimport { type OptionalUndefinedFields, type OptionalEmptyRecords } from \"./utils\"\nimport { registerInstance } from \"./evaluation\"\n\nconst ajv = new Ajv({ strict: false })\n\n// Argument\nexport type ComponentArgument = {\n schema: ArgumentValueSchema\n required: boolean\n meta: Meta\n}\n\ntype ComponentArgumentFullOptions = Meta & {\n schema: ArgumentValueSchema\n required?: boolean\n}\n\nexport type ComponentArgumentOptions = ArgumentValueSchema | ComponentArgumentFullOptions\nexport type ComponentArgumentSpec = [schema: ArgumentValueSchema, required: boolean]\n\nexport type ComponentArgumentOptionsToSpec<T extends ComponentArgumentOptions> =\n T extends ArgumentValueSchema\n ? T extends { default: unknown }\n ? [T, false]\n : [T, true]\n : T extends ComponentArgumentFullOptions\n ? T[\"required\"] extends false\n ? [T[\"schema\"], false]\n : [T[\"schema\"], true]\n : never\n\nexport type ComponentArgumentOptionsMapToSpecMap<\n T extends Record<string, ComponentArgumentOptions>,\n> =\n T extends Record<string, never>\n ? Record<string, never>\n : { [K in keyof T]: ComponentArgumentOptionsToSpec<T[K]> }\n\nexport type ComponentArgumentSpecMapToInputStatic<T extends Record<string, ComponentArgumentSpec>> =\n T extends Record<string, never>\n ? Record<string, never>\n : Static<TObject<{ [K in keyof T]: T[K][1] extends true ? T[K][0] : TOptional<T[K][0]> }>>\n\nexport type ComponentArgumentSpecToStatic<T extends Record<string, ComponentArgumentSpec>> =\n T extends Record<string, never>\n ? Record<string, never>\n : Static<TObject<{ [K in keyof T]: T[K][0] }>>\n\nexport type ComponentArgumentOptionsToSchema<T extends ComponentArgumentOptions> = T extends TSchema\n ? T\n : T[\"required\"] extends false\n ? TOptional<T[\"schema\"]>\n : T[\"schema\"]\n\ntype ComponentArgumentMapToValue<T extends Record<string, ComponentArgumentOptions>> = {\n [K in keyof T]: Static<ComponentArgumentOptionsToSchema<T[K]>>\n}\n\n// Input\nexport type ComponentInput = {\n type: string\n required: boolean\n multiple: boolean\n meta: Meta\n}\n\ntype ComponentInputFullOptions = Meta & {\n entity: Entity\n required?: boolean\n multiple?: boolean\n}\n\nexport type ComponentInputOptions = Entity | ComponentInputFullOptions\n\ntype ComponentInputOptionsToOutputRef<T extends ComponentInputOptions> = T extends Entity\n ? InstanceInput<T[\"type\"]>\n : T extends ComponentInputFullOptions\n ? T[\"required\"] extends false\n ? T[\"multiple\"] extends true\n ? InstanceInput<T[\"entity\"][\"type\"]>[] | undefined\n : InstanceInput<T[\"entity\"][\"type\"]> | undefined\n : T[\"multiple\"] extends true\n ? InstanceInput<T[\"entity\"][\"type\"]>[]\n : InstanceInput<T[\"entity\"][\"type\"]>\n : never\n\nexport type ComponentInputSpec = [entity: Entity, required: boolean, multiple: boolean]\n\nexport type ComponentInputOptionsToSpec<T extends ComponentInputOptions> = T extends Entity\n ? [T, true, false] // [Entity, required, multiple]\n : T extends ComponentInputFullOptions\n ? T[\"required\"] extends false\n ? T[\"multiple\"] extends true\n ? [T[\"entity\"], false, true]\n : [T[\"entity\"], false, false]\n : T[\"multiple\"] extends true\n ? [T[\"entity\"], true, true]\n : [T[\"entity\"], true, false]\n : never\n\nexport type ComponentInputOptionsMapToSpecMap<T extends Record<string, ComponentInputOptions>> =\n T extends Record<string, never>\n ? Record<string, never>\n : { [K in keyof T]: ComponentInputOptionsToSpec<T[K]> }\n\ntype ComponentInputMapToValue<T extends Record<string, ComponentInputOptions>> =\n OptionalUndefinedFields<{\n [K in keyof T]: ComponentInputOptionsToOutputRef<T[K]>\n }>\n\ntype ComponentInputMapToReturnType<T extends Record<string, ComponentInputOptions>> =\n T extends Record<string, never> ? void : ComponentInputMapToValue<T>\n\n// Params & Options\nexport type ComponentParams<\n TArgs extends Record<string, ComponentArgumentOptions>,\n TInputs extends Record<string, ComponentInputOptions>,\n> = {\n id: string\n name: string\n args: ComponentArgumentMapToValue<TArgs>\n inputs: ComponentInputMapToValue<TInputs>\n}\n\nexport type InputComponentParams<\n TArgs extends Record<string, ArgumentValue>,\n TInputs extends Record<string, unknown>,\n> = {\n name: string\n} & OptionalEmptyRecords<{\n args: TArgs\n inputs: TInputs\n}>\n\nexport type ComponentOptions<\n TArgs extends Record<string, ComponentArgumentOptions>,\n TInputs extends Record<string, ComponentInputOptions>,\n TOutputs extends Record<string, ComponentInputOptions>,\n> = {\n type: string\n meta?: ComponentMeta\n\n args?: TArgs\n inputs?: TInputs\n outputs?: TOutputs\n\n create: (params: ComponentParams<TArgs, TInputs>) => ComponentInputMapToReturnType<TOutputs>\n}\n\n// Models\nexport type ComponentMeta = Meta & {\n primaryIcon?: string\n primaryIconColor?: string\n secondaryIcon?: string\n secondaryIconColor?: string\n category?: string\n defaultNamePrefix?: string\n}\n\nexport type ComponentModel = {\n /**\n * The type of the component.\n */\n type: string\n\n /**\n * The record of the argument schemas.\n */\n args: Record<string, ComponentArgument>\n\n /**\n * The record of the input schemas.\n */\n inputs: Record<string, ComponentInput>\n\n /**\n * The record of the output schemas.\n */\n outputs: Record<string, ComponentInput>\n\n /**\n * The extra metadata of the component.\n */\n meta: ComponentMeta\n\n /**\n * The hash of the component definition.\n */\n definitionHash: string\n}\n\ntype InputSpecToInputRef<T extends ComponentInputSpec> = T[1] extends true\n ? T[2] extends true\n ? InstanceInput<T[0][\"type\"]>[]\n : InstanceInput<T[0][\"type\"]>\n : T[2] extends true\n ? InstanceInput<T[0][\"type\"]>[] | undefined\n : InstanceInput<T[0][\"type\"]> | undefined\n\ntype InputSpecToOutputRef<T extends ComponentInputSpec> = T[2] extends true\n ? InstanceInput<T[0][\"type\"]>[]\n : InstanceInput<T[0][\"type\"]>\n\nexport type InputRefMap<TInputs extends Record<string, ComponentInputSpec>> =\n TInputs extends Record<string, [string, never, never]>\n ? Record<string, never>\n : { [K in keyof TInputs]: InputSpecToInputRef<TInputs[K]> }\n\nexport type OutputRefMap<TInputs extends Record<string, ComponentInputSpec>> =\n TInputs extends Record<string, [string, never, never]>\n ? Record<string, never>\n : { [K in keyof TInputs]: InputSpecToOutputRef<TInputs[K]> }\n\nexport const originalCreate = Symbol(\"originalCreate\")\n\nexport type Component<\n TArgs extends Record<string, ComponentArgumentSpec> = Record<string, never>,\n TInputs extends Record<string, ComponentInputSpec> = Record<string, never>,\n TOutputs extends Record<string, ComponentInputSpec> = Record<string, never>,\n> = {\n /**\n * The non-generic model of the component.\n */\n model: ComponentModel\n\n /**\n * The entities used in the inputs or outputs of the component.\n */\n entities: Map<string, Entity>\n\n /**\n * Creates the component at the evaluation time.\n */\n (\n context: InputComponentParams<\n ComponentArgumentSpecMapToInputStatic<TArgs>,\n InputRefMap<TInputs>\n >,\n ): OutputRefMap<TOutputs>\n\n /**\n * The original create function.\n *\n * Used to calculate the definition hash.\n */\n [originalCreate]: (params: InputComponentParams<any, any>) => any\n}\n\nexport function defineComponent<\n TArgs extends Record<string, ComponentArgumentOptions> = Record<string, never>,\n TInputs extends Record<string, ComponentInputOptions> = Record<string, never>,\n TOutputs extends Record<string, ComponentInputOptions> = Record<string, never>,\n>(\n options: ComponentOptions<TArgs, TInputs, TOutputs>,\n): Component<\n ComponentArgumentOptionsMapToSpecMap<TArgs>,\n ComponentInputOptionsMapToSpecMap<TInputs>,\n ComponentInputOptionsMapToSpecMap<TOutputs>\n> {\n function create(params: InputComponentParams<any, any>): any {\n const { name, args, inputs } = params\n const id = `${options.type}:${name}`\n\n const flatInputs = mapValues(pickBy(inputs ?? {}, isNonNullish), inputs => [inputs].flat(2))\n\n return registerInstance(\n create.model as ComponentModel,\n {\n id,\n type: options.type,\n name,\n args: args ?? {},\n inputs: mapValues(flatInputs, inputs => inputs.map(input => input[boundaryInput] ?? input)),\n resolvedInputs: flatInputs,\n },\n () => {\n const markedInputs = mapValues(flatInputs, (inputs, key) => {\n const result = inputs.map(input => ({\n ...input,\n [boundaryInput]: { instanceId: id, output: key },\n }))\n\n return (create.model as ComponentModel).inputs?.[key]?.multiple === false\n ? result[0]\n : result\n })\n\n const outputs: Record<string, InstanceInput[]> =\n options.create({\n id,\n name,\n args: validateArgsAndFillDefaults(\n id,\n create.model as ComponentModel,\n args ? { ...args } : {},\n ) as any,\n inputs: markedInputs as any,\n }) ?? {}\n\n return mapValues(pickBy(outputs, isNonNullish), outputs => [outputs].flat(2))\n },\n )\n }\n\n create.entities = new Map<string, Entity>()\n const mapInput = createInputMapper(create.entities)\n\n create.model = {\n type: options.type,\n args: mapValues(options.args ?? {}, mapArgument),\n inputs: mapValues(options.inputs ?? {}, mapInput),\n outputs: mapValues(options.outputs ?? {}, mapInput),\n meta: options.meta ?? {},\n }\n\n create[originalCreate] = options.create\n\n return create as any\n}\n\nexport function isComponent(value: unknown): value is Component {\n return typeof value === \"function\" && \"model\" in value\n}\n\nexport function mapArgument(value: ComponentArgumentOptions) {\n if (\"schema\" in value) {\n return {\n schema: value.schema,\n required: value.required ?? (!value.schema[OptionalKind] && !value.schema.default),\n meta: {\n displayName: value.displayName,\n description: value.description,\n color: value.color,\n },\n }\n }\n\n return {\n schema: value,\n required: !value[OptionalKind] && !value.default,\n meta: {},\n }\n}\n\nexport function createInputMapper(entities: Map<string, Entity>) {\n return (value: ComponentInputOptions) => {\n if (\"entity\" in value) {\n entities.set(value.entity.type, value.entity)\n\n return {\n type: value.entity.type,\n required: value.required ?? true,\n multiple: value.multiple ?? false,\n meta: {\n displayName: value.displayName,\n description: value.description,\n color: value.color,\n },\n }\n }\n\n entities.set(value.type, value)\n\n return {\n type: value.type,\n required: true,\n multiple: false,\n meta: {},\n }\n }\n}\n\nfunction validateArgsAndFillDefaults(\n instanceId: string,\n model: ComponentModel,\n args: Record<string, unknown>,\n) {\n for (const [key, argModel] of Object.entries(model.args)) {\n let value = args[key]\n if (!value) {\n if (argModel.required) {\n throw new Error(`Missing required argument \"${key}\" for instance \"${instanceId}\"`)\n }\n\n value = argModel.schema.default\n args[key] = value\n }\n\n if (value && !ajv.validate(argModel.schema, value)) {\n throw new Error(`Invalid argument \"${key}\" for instance \"${instanceId}\": ${ajv.errorsText()}`)\n }\n }\n\n return args as Record<string, ArgumentValue>\n}\n","export type EntityMap<T> = Record<string, T> & {\n default?: T\n}\n\nexport type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>\nexport type RequiredKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>\n\n// TODO: refactor this shit\n\ntype PickUndefinedKeys<T extends Record<string, unknown>> = T extends Record<string, never>\n ? never\n : Exclude<\n {\n [K in keyof T]: undefined extends T[K] ? K : never\n }[keyof T],\n undefined\n >\n\ntype AllOptionalKeys<T extends Record<string, unknown>> = T extends Record<string, never>\n ? never\n : Exclude<\n {\n [K in keyof T]: undefined extends T[K] ? K : never\n }[keyof T],\n undefined\n >\n\ntype HasRequired<T extends Record<string, unknown>> = T extends Record<string, never>\n ? false\n : [keyof T] extends [AllOptionalKeys<T>]\n ? false\n : true\n\ntype PickRecordsWithAnyRequired<T extends Record<string, Record<string, unknown>>> =\n T extends Record<string, never>\n ? never\n : Exclude<\n {\n [K in keyof T]: HasRequired<T[K]> extends true ? K : never\n }[keyof T],\n undefined\n >\n\nexport type OptionalEmptyRecords<T extends Record<string, Record<string, unknown>>> = {\n [K in Exclude<keyof T, PickRecordsWithAnyRequired<T>>]?: OptionalUndefinedFields<T[K]>\n} & {\n [K in PickRecordsWithAnyRequired<T>]: OptionalUndefinedFields<T[K]>\n}\n\nexport type OptionalUndefinedFields<T extends Record<string, unknown>> = {\n [K in PickUndefinedKeys<T>]?: T[K]\n} & {\n [K in Exclude<keyof T, PickUndefinedKeys<T>>]: T[K]\n}\n\n/**\n * Formats a multiline string and trims the indentation.\n *\n * @param str The string to trim.\n * @returns The trimmed string.\n */\nexport function text(array: TemplateStringsArray, ...values: unknown[]): string {\n const str = array.reduce(\n // eslint-disable-next-line @typescript-eslint/no-base-to-string\n (result, part, i) => result + part + (values[i] ? String(values[i]) : \"\"),\n \"\",\n )\n\n return trimIndentation(str)\n}\n\n/**\n * Removes the indentation from a multiline string.\n *\n * @param text The text to trim.\n * @returns The trimmed text.\n */\nexport function trimIndentation(text: string): string {\n const lines = text.split(\"\\n\")\n const indent = lines\n .filter(line => line.trim() !== \"\")\n .map(line => line.match(/^\\s*/)?.[0].length ?? 0)\n .reduce((min, indent) => Math.min(min, indent), Infinity)\n\n return lines\n .map(line => line.slice(indent))\n .join(\"\\n\")\n .trim()\n}\n","import type { TAnySchema, TSchema } from \"@sinclair/typebox\"\nimport type { Meta } from \"./types\"\nimport type { PartialKeys } from \"./utils\"\n\n/**\n * The entity is some abstract object which can be passed from one component to another through their inputs and outputs.\n * Every entity must have a type.\n * Every component inputs and outputs will reference such types and only entities of the same type can be passed.\n */\nexport type Entity<TType extends string = string, TEntitySchema extends TSchema = TSchema> = {\n /**\n * The static type of the entity.\n */\n type: TType\n\n /**\n * The JSON schema of the entity value.\n */\n schema: TEntitySchema\n\n /**\n * The extra metadata of the entity.\n */\n meta: Meta\n\n /**\n * The hash of the entity definition.\n */\n definitionHash: string\n}\n\nexport type EntityOptions<TType extends string, TSchema extends TAnySchema> = Omit<\n PartialKeys<Entity<TType, TSchema>, \"meta\">,\n \"definitionHash\"\n>\n\nexport function defineEntity<TType extends string, TSchema extends TAnySchema>(\n options: EntityOptions<TType, TSchema>,\n): Entity<TType, TSchema> {\n return {\n meta: {},\n ...options,\n } as Entity<TType, TSchema>\n}\n\nexport function isEntity(value: unknown): value is Entity {\n return (\n typeof value === \"object\" &&\n value !== null &&\n \"type\" in value &&\n \"schema\" in value &&\n \"meta\" in value\n )\n}\n","import {\n type Static,\n type TAny,\n type TArray,\n type TBoolean,\n type TLiteral,\n type TNumber,\n type TObject,\n type TRecordOrObject,\n type TString,\n type TUnion,\n} from \"@sinclair/typebox\"\n\nexport type ArgumentValueSchema =\n | TString\n | TNumber\n | TBoolean\n | TArray<TString>\n | TArray<TNumber>\n | TUnion<TLiteral[]>\n | TRecordOrObject<TString, TAny>\n | TObject\n | TArray\n | TUnion<TObject[]>\n\nexport type ArgumentValue = Static<ArgumentValueSchema>\n\n/**\n * The generic metadata of some contract.\n */\nexport type Meta = {\n /**\n * The display name of the entity which can be shown in the UI.\n */\n displayName?: string\n\n /**\n * The description of the entity which can be shown in the UI.\n */\n description?: string\n\n /**\n * The hex color of the entity which can be used in the UI.\n *\n * @example \"#ff0000\"\n */\n color?: string\n}\n","export {\n type InstanceInput,\n type HubInput,\n type InstanceModel,\n type Position,\n getInstanceId,\n parseInstanceId,\n findInput,\n findRequiredInput,\n findInputs,\n findRequiredInputs,\n} from \"./instance\"\nexport { getCompositeInstances, resetEvaluation, type CompositeInstance } from \"./evaluation\"\nexport { type Entity, defineEntity, isEntity } from \"./entity\"\nexport {\n type Component,\n type ComponentModel,\n type ComponentMeta,\n type ComponentArgument,\n type ComponentInput,\n type ComponentInputSpec,\n type ComponentArgumentSpec,\n type ComponentArgumentSpecToStatic,\n defineComponent,\n isComponent,\n originalCreate,\n} from \"./component\"\nexport { type Unit, type UnitModel, type UnitSource, defineUnit, isUnitModel } from \"./unit\"\nexport { type RequiredKeys, type PartialKeys, text, trimIndentation } from \"./utils\"\nexport { type ArgumentValue, type ArgumentValueSchema } from \"./types\"\nexport { type Static } from \"@sinclair/typebox\"\n\nimport {\n Type as BaseType,\n type Static,\n type TLiteral,\n type TSchema,\n type TUnion,\n} from \"@sinclair/typebox\"\n\ntype MapToLiteral<T extends readonly string[]> = {\n [K in keyof T]: T[K] extends string ? TLiteral<T[K]> : never\n}\n\nfunction StringEnum<T extends string[]>(values: [...T]): TUnion<MapToLiteral<T>> {\n return Type.Union(values.map(value => Type.Literal(value))) as TUnion<MapToLiteral<T>>\n}\n\nfunction Default<T extends TSchema>(\n schema: T,\n defaultValue: Static<T>,\n): T & { default: Static<T> } {\n return { ...schema, default: defaultValue }\n}\n\nexport const Type = {\n ...BaseType,\n StringEnum,\n Default,\n} as typeof BaseType & {\n StringEnum: typeof StringEnum\n Default: typeof Default\n}\n"],"mappings":";AASO,IAAM,gBAAgB,OAAO,eAAe;AAsG5C,SAAS,cAAc,cAAsB,cAA8B;AAChF,SAAO,GAAG,YAAY,IAAI,YAAY;AACxC;AASO,SAAS,gBAAgB,YAAkE;AAChG,QAAM,QAAQ,WAAW,MAAM,GAAG;AAElC,MAAI,MAAM,WAAW,GAAG;AACtB,UAAM,IAAI,MAAM,yBAAyB,UAAU,EAAE;AAAA,EACvD;AAEA,SAAO;AACT;AAEO,SAAS,UACd,QACA,MACyB;AACzB,QAAM,gBAAgB,OAAO;AAAA,IAC3B,WAAS,gBAAgB,MAAM,UAAU,EAAE,CAAC,MAAM,QAAQ,MAAM,eAAe;AAAA,EACjF;AAEA,MAAI,cAAc,WAAW,GAAG;AAC9B,WAAO;AAAA,EACT;AAEA,MAAI,cAAc,SAAS,GAAG;AAC5B,UAAM,IAAI;AAAA,MACR,8BAA8B,IAAI,MAAM,cAAc,IAAI,WAAS,MAAM,UAAU,EAAE,KAAK,IAAI,CAAC;AAAA,IACjG;AAAA,EACF;AAEA,SAAO,cAAc,CAAC;AACxB;AAEO,SAAS,kBACd,QACA,MACkB;AAClB,QAAM,QAAQ,UAAU,QAAQ,IAAI;AAEpC,MAAI,UAAU,MAAM;AAClB,UAAM,IAA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AAC,YAAY,SAAS,GAAG;AAC3B,6BAAyB;AACzB,+BAA2B,EAAE,UAAU,UAAU,CAAC,EAAE;AACpD,uBAAmB,IAAI,yBAAyB,SAAS,IAAI,wBAAwB;AAAA,EACvF;AAEA,MAAI;AACF,UAAM,UAAU,GAAG;AAEnB,aAAS,kBAAkB;AAC3B,aAAS,UAAUC;AAAA,MAAU,WAAW,CAAC;AAAA,MAAG,CAAAC,aAC1CA,SAAQ,IAAI,YAAU,OAAO,aAAa,KAAK,MAAM;AAAA,IACvD;AAGA,WAAOD;AAAA,MAAU;AAAA,MAAS,CAACC,UAAS,cAClCA,SAAQ,IAAI,aAAW;AAAA,QACrB,GAAG;AAAA,QACH,CAAC,aAAa,GAAG,EAAE,YAAY,SAAS,IAAI,QAAQ,UAAU;AAAA,MAChE,EAAE;AAAA,IACJ;AAAA,EACF,UAAE;AACA,QAAI,wBAAwB;AAC1B,iCAA2B;AAAA,IAC7B;AAAA,EACF;AACF;;;AIvBO,SAAS,aACd,SACwB;AACxB,SAAO;AAAA,IACL,MAAM,CAAC;AAAA,IACP,GAAG;AAAA,EACL;AACF;AAEO,SAAS,SAAS,OAAiC;AACxD,SACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACV,YAAY,SACZ,UAAU;AAEd;;;ACrDA,OAWO;;;ACmBP,eAA4B;AAE5B;AAAA,EACE,QAAQ;AAAA,OAKH;AAMP,SAAS,WAA+B,QAAyC;AAC/E,SAAO,KAAK,MAAM,OAAO,IAAI,WAAS,KAAK,QAAQ,KAAK,CAAC,CAAC;AAC5D;AAEA,SAAS,QACP,QACA,cAC4B;AAC5B,SAAO,EAAE,GAAG,QAAQ,SAAS,aAAa;AAC5C;AAEO,IAAM,OAAO;AAAA,EAClB,GAAG;AAAA,EACH;AAAA,EACA;AACF;","names":["mapValues","mapValues","text","indent","inputs","outputs","mapValues","mapValues","outputs"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@highstate/contract",
3
- "version": "0.7.11",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -29,5 +29,5 @@
29
29
  "highstate-cli-bootstrap": "npm:@highstate/cli@0.7.6",
30
30
  "vitest": "^2.1.8"
31
31
  },
32
- "gitHead": "f425f2be2d5800fdee3512c848129adab1b0186e"
32
+ "gitHead": "b64df0df33afcadf019936eaa2a2ca7ffc82940b"
33
33
  }
package/src/component.ts CHANGED
@@ -18,7 +18,7 @@ import { boundaryInput, type InstanceInput } from "./instance"
18
18
  import { type OptionalUndefinedFields, type OptionalEmptyRecords } from "./utils"
19
19
  import { registerInstance } from "./evaluation"
20
20
 
21
- const ajv = new Ajv()
21
+ const ajv = new Ajv({ strict: false })
22
22
 
23
23
  // Argument
24
24
  export type ComponentArgument = {
@@ -33,6 +33,35 @@ type ComponentArgumentFullOptions = Meta & {
33
33
  }
34
34
 
35
35
  export type ComponentArgumentOptions = ArgumentValueSchema | ComponentArgumentFullOptions
36
+ export type ComponentArgumentSpec = [schema: ArgumentValueSchema, required: boolean]
37
+
38
+ export type ComponentArgumentOptionsToSpec<T extends ComponentArgumentOptions> =
39
+ T extends ArgumentValueSchema
40
+ ? T extends { default: unknown }
41
+ ? [T, false]
42
+ : [T, true]
43
+ : T extends ComponentArgumentFullOptions
44
+ ? T["required"] extends false
45
+ ? [T["schema"], false]
46
+ : [T["schema"], true]
47
+ : never
48
+
49
+ export type ComponentArgumentOptionsMapToSpecMap<
50
+ T extends Record<string, ComponentArgumentOptions>,
51
+ > =
52
+ T extends Record<string, never>
53
+ ? Record<string, never>
54
+ : { [K in keyof T]: ComponentArgumentOptionsToSpec<T[K]> }
55
+
56
+ export type ComponentArgumentSpecMapToInputStatic<T extends Record<string, ComponentArgumentSpec>> =
57
+ T extends Record<string, never>
58
+ ? Record<string, never>
59
+ : Static<TObject<{ [K in keyof T]: T[K][1] extends true ? T[K][0] : TOptional<T[K][0]> }>>
60
+
61
+ export type ComponentArgumentSpecToStatic<T extends Record<string, ComponentArgumentSpec>> =
62
+ T extends Record<string, never>
63
+ ? Record<string, never>
64
+ : Static<TObject<{ [K in keyof T]: T[K][0] }>>
36
65
 
37
66
  export type ComponentArgumentOptionsToSchema<T extends ComponentArgumentOptions> = T extends TSchema
38
67
  ? T
@@ -177,7 +206,7 @@ export type ComponentModel = {
177
206
  definitionHash: string
178
207
  }
179
208
 
180
- type InputSpecToOutputRef<T extends ComponentInputSpec> = T[1] extends true
209
+ type InputSpecToInputRef<T extends ComponentInputSpec> = T[1] extends true
181
210
  ? T[2] extends true
182
211
  ? InstanceInput<T[0]["type"]>[]
183
212
  : InstanceInput<T[0]["type"]>
@@ -185,6 +214,15 @@ type InputSpecToOutputRef<T extends ComponentInputSpec> = T[1] extends true
185
214
  ? InstanceInput<T[0]["type"]>[] | undefined
186
215
  : InstanceInput<T[0]["type"]> | undefined
187
216
 
217
+ type InputSpecToOutputRef<T extends ComponentInputSpec> = T[2] extends true
218
+ ? InstanceInput<T[0]["type"]>[]
219
+ : InstanceInput<T[0]["type"]>
220
+
221
+ export type InputRefMap<TInputs extends Record<string, ComponentInputSpec>> =
222
+ TInputs extends Record<string, [string, never, never]>
223
+ ? Record<string, never>
224
+ : { [K in keyof TInputs]: InputSpecToInputRef<TInputs[K]> }
225
+
188
226
  export type OutputRefMap<TInputs extends Record<string, ComponentInputSpec>> =
189
227
  TInputs extends Record<string, [string, never, never]>
190
228
  ? Record<string, never>
@@ -193,7 +231,7 @@ export type OutputRefMap<TInputs extends Record<string, ComponentInputSpec>> =
193
231
  export const originalCreate = Symbol("originalCreate")
194
232
 
195
233
  export type Component<
196
- TArgs extends Record<string, ArgumentValue> = Record<string, never>,
234
+ TArgs extends Record<string, ComponentArgumentSpec> = Record<string, never>,
197
235
  TInputs extends Record<string, ComponentInputSpec> = Record<string, never>,
198
236
  TOutputs extends Record<string, ComponentInputSpec> = Record<string, never>,
199
237
  > = {
@@ -210,7 +248,12 @@ export type Component<
210
248
  /**
211
249
  * Creates the component at the evaluation time.
212
250
  */
213
- (context: InputComponentParams<TArgs, OutputRefMap<TInputs>>): OutputRefMap<TOutputs>
251
+ (
252
+ context: InputComponentParams<
253
+ ComponentArgumentSpecMapToInputStatic<TArgs>,
254
+ InputRefMap<TInputs>
255
+ >,
256
+ ): OutputRefMap<TOutputs>
214
257
 
215
258
  /**
216
259
  * The original create function.
@@ -220,11 +263,6 @@ export type Component<
220
263
  [originalCreate]: (params: InputComponentParams<any, any>) => any
221
264
  }
222
265
 
223
- export type ArgumentOptionsMapToStatic<T extends Record<string, ComponentArgumentOptions>> =
224
- T extends Record<string, never>
225
- ? Record<string, never>
226
- : Static<TObject<{ [K in keyof T]: ComponentArgumentOptionsToSchema<T[K]> }>>
227
-
228
266
  export function defineComponent<
229
267
  TArgs extends Record<string, ComponentArgumentOptions> = Record<string, never>,
230
268
  TInputs extends Record<string, ComponentInputOptions> = Record<string, never>,
@@ -232,7 +270,7 @@ export function defineComponent<
232
270
  >(
233
271
  options: ComponentOptions<TArgs, TInputs, TOutputs>,
234
272
  ): Component<
235
- ArgumentOptionsMapToStatic<TArgs>,
273
+ ComponentArgumentOptionsMapToSpecMap<TArgs>,
236
274
  ComponentInputOptionsMapToSpecMap<TInputs>,
237
275
  ComponentInputOptionsMapToSpecMap<TOutputs>
238
276
  > {
@@ -240,8 +278,6 @@ export function defineComponent<
240
278
  const { name, args, inputs } = params
241
279
  const id = `${options.type}:${name}`
242
280
 
243
- validateArgs(id, create.model as ComponentModel, args ?? {})
244
-
245
281
  const flatInputs = mapValues(pickBy(inputs ?? {}, isNonNullish), inputs => [inputs].flat(2))
246
282
 
247
283
  return registerInstance(
@@ -270,7 +306,11 @@ export function defineComponent<
270
306
  options.create({
271
307
  id,
272
308
  name,
273
- args: (args as any) ?? {},
309
+ args: validateArgsAndFillDefaults(
310
+ id,
311
+ create.model as ComponentModel,
312
+ args ? { ...args } : {},
313
+ ) as any,
274
314
  inputs: markedInputs as any,
275
315
  }) ?? {}
276
316
 
@@ -347,15 +387,26 @@ export function createInputMapper(entities: Map<string, Entity>) {
347
387
  }
348
388
  }
349
389
 
350
- function validateArgs(instanceId: string, model: ComponentModel, args: Record<string, unknown>) {
390
+ function validateArgsAndFillDefaults(
391
+ instanceId: string,
392
+ model: ComponentModel,
393
+ args: Record<string, unknown>,
394
+ ) {
351
395
  for (const [key, argModel] of Object.entries(model.args)) {
352
- const value = args[key]
353
- if (!value && argModel.required) {
354
- throw new Error(`Missing required argument "${key}" for instance "${instanceId}"`)
396
+ let value = args[key]
397
+ if (!value) {
398
+ if (argModel.required) {
399
+ throw new Error(`Missing required argument "${key}" for instance "${instanceId}"`)
400
+ }
401
+
402
+ value = argModel.schema.default
403
+ args[key] = value
355
404
  }
356
405
 
357
406
  if (value && !ajv.validate(argModel.schema, value)) {
358
407
  throw new Error(`Invalid argument "${key}" for instance "${instanceId}": ${ajv.errorsText()}`)
359
408
  }
360
409
  }
410
+
411
+ return args as Record<string, ArgumentValue>
361
412
  }
package/src/index.ts CHANGED
@@ -7,6 +7,8 @@ export {
7
7
  parseInstanceId,
8
8
  findInput,
9
9
  findRequiredInput,
10
+ findInputs,
11
+ findRequiredInputs,
10
12
  } from "./instance"
11
13
  export { getCompositeInstances, resetEvaluation, type CompositeInstance } from "./evaluation"
12
14
  export { type Entity, defineEntity, isEntity } from "./entity"
@@ -17,6 +19,8 @@ export {
17
19
  type ComponentArgument,
18
20
  type ComponentInput,
19
21
  type ComponentInputSpec,
22
+ type ComponentArgumentSpec,
23
+ type ComponentArgumentSpecToStatic,
20
24
  defineComponent,
21
25
  isComponent,
22
26
  originalCreate,
@@ -26,7 +30,13 @@ export { type RequiredKeys, type PartialKeys, text, trimIndentation } from "./ut
26
30
  export { type ArgumentValue, type ArgumentValueSchema } from "./types"
27
31
  export { type Static } from "@sinclair/typebox"
28
32
 
29
- import { Type as BaseType, type TLiteral, type TUnion } from "@sinclair/typebox"
33
+ import {
34
+ Type as BaseType,
35
+ type Static,
36
+ type TLiteral,
37
+ type TSchema,
38
+ type TUnion,
39
+ } from "@sinclair/typebox"
30
40
 
31
41
  type MapToLiteral<T extends readonly string[]> = {
32
42
  [K in keyof T]: T[K] extends string ? TLiteral<T[K]> : never
@@ -36,7 +46,18 @@ function StringEnum<T extends string[]>(values: [...T]): TUnion<MapToLiteral<T>>
36
46
  return Type.Union(values.map(value => Type.Literal(value))) as TUnion<MapToLiteral<T>>
37
47
  }
38
48
 
49
+ function Default<T extends TSchema>(
50
+ schema: T,
51
+ defaultValue: Static<T>,
52
+ ): T & { default: Static<T> } {
53
+ return { ...schema, default: defaultValue }
54
+ }
55
+
39
56
  export const Type = {
40
57
  ...BaseType,
41
58
  StringEnum,
42
- } as typeof BaseType & { StringEnum: typeof StringEnum }
59
+ Default,
60
+ } as typeof BaseType & {
61
+ StringEnum: typeof StringEnum
62
+ Default: typeof Default
63
+ }
package/src/instance.ts CHANGED
@@ -142,7 +142,7 @@ export function findInput<T extends string>(
142
142
  return null
143
143
  }
144
144
 
145
- if (1 < matchedInputs.length) {
145
+ if (matchedInputs.length > 1) {
146
146
  throw new Error(
147
147
  `Multiple inputs found for "${name}": ${matchedInputs.map(input => input.instanceId).join(", ")}. Specify the full instance id to disambiguate.`,
148
148
  )
@@ -163,3 +163,17 @@ export function findRequiredInput<T extends string>(
163
163
 
164
164
  return input
165
165
  }
166
+
167
+ export function findInputs<T extends string>(
168
+ inputs: InstanceInput<T>[],
169
+ names: string[],
170
+ ): InstanceInput<T>[] {
171
+ return names.map(name => findInput(inputs, name)).filter(Boolean) as InstanceInput<T>[]
172
+ }
173
+
174
+ export function findRequiredInputs<T extends string>(
175
+ inputs: InstanceInput<T>[],
176
+ names: string[],
177
+ ): InstanceInput<T>[] {
178
+ return names.map(name => findRequiredInput(inputs, name))
179
+ }
package/src/types.ts CHANGED
@@ -5,6 +5,7 @@ import {
5
5
  type TBoolean,
6
6
  type TLiteral,
7
7
  type TNumber,
8
+ type TObject,
8
9
  type TRecordOrObject,
9
10
  type TString,
10
11
  type TUnion,
@@ -18,7 +19,9 @@ export type ArgumentValueSchema =
18
19
  | TArray<TNumber>
19
20
  | TUnion<TLiteral[]>
20
21
  | TRecordOrObject<TString, TAny>
22
+ | TObject
21
23
  | TArray
24
+ | TUnion<TObject[]>
22
25
 
23
26
  export type ArgumentValue = Static<ArgumentValueSchema>
24
27
 
package/src/unit.ts CHANGED
@@ -7,7 +7,7 @@ import type { ArgumentValue } from "./types"
7
7
  import type { InstanceInput } from "./instance"
8
8
  import { mapValues } from "remeda"
9
9
  import {
10
- type ArgumentOptionsMapToStatic,
10
+ type ComponentArgumentSpec,
11
11
  type Component,
12
12
  type ComponentArgument,
13
13
  type ComponentArgumentOptions,
@@ -18,6 +18,7 @@ import {
18
18
  type ComponentOptions,
19
19
  defineComponent,
20
20
  mapArgument,
21
+ type ComponentArgumentOptionsMapToSpecMap,
21
22
  } from "./component"
22
23
 
23
24
  type UnitOptions<
@@ -50,7 +51,7 @@ export type UnitModel = ComponentModel & {
50
51
  }
51
52
 
52
53
  export type Unit<
53
- TArgs extends Record<string, ArgumentValue> = Record<string, never>,
54
+ TArgs extends Record<string, ComponentArgumentSpec> = Record<string, never>,
54
55
  TInputs extends Record<string, ComponentInputSpec> = Record<string, never>,
55
56
  TOutputs extends Record<string, ComponentInputSpec> = Record<string, never>,
56
57
  TSecrets extends Record<string, ArgumentValue> = Record<string, never>,
@@ -72,10 +73,10 @@ export function defineUnit<
72
73
  >(
73
74
  options: UnitOptions<TArgs, TInputs, TOutputs, TSecrets>,
74
75
  ): Unit<
75
- ArgumentOptionsMapToStatic<TArgs>,
76
+ ComponentArgumentOptionsMapToSpecMap<TArgs>,
76
77
  ComponentInputOptionsMapToSpecMap<TInputs>,
77
78
  ComponentInputOptionsMapToSpecMap<TOutputs>,
78
- ArgumentOptionsMapToStatic<TSecrets>
79
+ ComponentArgumentOptionsMapToSpecMap<TSecrets>
79
80
  > {
80
81
  const component = defineComponent<TArgs, TInputs, TOutputs>({
81
82
  ...options,