@highstate/contract 0.7.3 → 0.7.6

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.
@@ -0,0 +1,5 @@
1
+ {
2
+ "sourceHashes": {
3
+ "./dist/index.js": "c144f026d937ec67e3a1cb070005d763cd3ba564785c2329275e0056d3864a3a"
4
+ }
5
+ }
package/dist/index.js CHANGED
@@ -1,8 +1,5 @@
1
- import { mapValues, pickBy, isNonNullish } from 'remeda';
2
- import { OptionalKind, Type as Type$1 } from '@sinclair/typebox';
3
- import { Ajv } from 'ajv';
4
-
5
- const boundaryInput = Symbol("boundaryInput");
1
+ // src/instance.ts
2
+ var boundaryInput = Symbol("boundaryInput");
6
3
  function getInstanceId(instanceType, instanceName) {
7
4
  return `${instanceType}:${instanceName}`;
8
5
  }
@@ -35,6 +32,20 @@ function findRequiredInput(inputs, name) {
35
32
  return input;
36
33
  }
37
34
 
35
+ // src/evaluation.ts
36
+ import { mapValues as mapValues3 } from "remeda";
37
+
38
+ // src/unit.ts
39
+ import { mapValues as mapValues2 } from "remeda";
40
+
41
+ // src/component.ts
42
+ import {
43
+ OptionalKind
44
+ } from "@sinclair/typebox";
45
+ import { isNonNullish, mapValues, pickBy } from "remeda";
46
+ import { Ajv } from "ajv";
47
+
48
+ // src/utils.ts
38
49
  function text(array, ...values) {
39
50
  const str = array.reduce(
40
51
  // eslint-disable-next-line @typescript-eslint/no-base-to-string
@@ -49,8 +60,9 @@ function trimIndentation(text2) {
49
60
  return lines.map((line) => line.slice(indent)).join("\n").trim();
50
61
  }
51
62
 
52
- const ajv = new Ajv();
53
- const originalCreate = Symbol("originalCreate");
63
+ // src/component.ts
64
+ var ajv = new Ajv();
65
+ var originalCreate = Symbol("originalCreate");
54
66
  function defineComponent(options) {
55
67
  function create(params) {
56
68
  const { name, args, inputs } = params;
@@ -154,6 +166,7 @@ function validateArgs(instanceId, model, args) {
154
166
  }
155
167
  }
156
168
 
169
+ // src/unit.ts
157
170
  function isUnitModel(model) {
158
171
  return "source" in model;
159
172
  }
@@ -174,12 +187,13 @@ function defineUnit(options) {
174
187
  }
175
188
  });
176
189
  component.model.source = options.source ?? {};
177
- component.model.secrets = mapValues(options.secrets ?? {}, mapArgument);
190
+ component.model.secrets = mapValues2(options.secrets ?? {}, mapArgument);
178
191
  return component;
179
192
  }
180
193
 
181
- const compositeInstances = /* @__PURE__ */ new Map();
182
- let currentCompositeInstance = null;
194
+ // src/evaluation.ts
195
+ var compositeInstances = /* @__PURE__ */ new Map();
196
+ var currentCompositeInstance = null;
183
197
  function resetEvaluation() {
184
198
  compositeInstances.clear();
185
199
  currentCompositeInstance = null;
@@ -201,11 +215,11 @@ function registerInstance(component, instance, fn) {
201
215
  try {
202
216
  const outputs = fn();
203
217
  instance.resolvedOutputs = outputs;
204
- instance.outputs = mapValues(
218
+ instance.outputs = mapValues3(
205
219
  outputs ?? {},
206
220
  (outputs2) => outputs2.map((output) => output[boundaryInput] ?? output)
207
221
  );
208
- return mapValues(
222
+ return mapValues3(
209
223
  outputs,
210
224
  (outputs2, outputKey) => outputs2.map((output) => ({
211
225
  ...output,
@@ -219,6 +233,7 @@ function registerInstance(component, instance, fn) {
219
233
  }
220
234
  }
221
235
 
236
+ // src/entity.ts
222
237
  function defineEntity(options) {
223
238
  return {
224
239
  meta: {},
@@ -229,12 +244,35 @@ function isEntity(value) {
229
244
  return typeof value === "object" && value !== null && "type" in value && "schema" in value && "meta" in value;
230
245
  }
231
246
 
247
+ // src/types.ts
248
+ import "@sinclair/typebox";
249
+
250
+ // src/index.ts
251
+ import {} from "@sinclair/typebox";
252
+ import { Type as BaseType } from "@sinclair/typebox";
232
253
  function StringEnum(values) {
233
254
  return Type.Union(values.map((value) => Type.Literal(value)));
234
255
  }
235
- const Type = {
236
- ...Type$1,
256
+ var Type = {
257
+ ...BaseType,
237
258
  StringEnum
238
259
  };
239
-
240
- export { Type, defineComponent, defineEntity, defineUnit, findInput, findRequiredInput, getCompositeInstances, getInstanceId, isComponent, isEntity, isUnitModel, originalCreate, parseInstanceId, resetEvaluation, text, trimIndentation };
260
+ export {
261
+ Type,
262
+ defineComponent,
263
+ defineEntity,
264
+ defineUnit,
265
+ findInput,
266
+ findRequiredInput,
267
+ getCompositeInstances,
268
+ getInstanceId,
269
+ isComponent,
270
+ isEntity,
271
+ isUnitModel,
272
+ originalCreate,
273
+ parseInstanceId,
274
+ resetEvaluation,
275
+ text,
276
+ trimIndentation
277
+ };
278
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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: typeof StringEnum }\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,IAAI,cAAc,QAAQ;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,IAAI,MAAM,mBAAmB,IAAI,cAAc;AAAA,EACvD;AAEA,SAAO;AACT;;;ACnKA,SAAS,aAAAA,kBAAiB;;;ACM1B,SAAS,aAAAC,kBAAiB;;;AC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AAG,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,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@highstate/contract",
3
- "version": "0.7.3",
3
+ "version": "0.7.6",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist",
@@ -16,7 +16,7 @@
16
16
  "access": "public"
17
17
  },
18
18
  "scripts": {
19
- "build": "pkgroll --clean --tsconfig=tsconfig.build.json",
19
+ "build": "highstate build",
20
20
  "test": "vitest run --coverage"
21
21
  },
22
22
  "dependencies": {
@@ -26,8 +26,8 @@
26
26
  },
27
27
  "devDependencies": {
28
28
  "@vitest/coverage-v8": "2.1.8",
29
- "pkgroll": "^2.5.1",
29
+ "highstate-cli-bootstrap": "npm:@highstate/cli@0.7.4",
30
30
  "vitest": "^2.1.8"
31
31
  },
32
- "gitHead": "5cf7cec27262c8fa1d96f6478833b94841459d64"
32
+ "gitHead": "5acce7f9d8a57bdd1220e381d5b76d7b484b98c2"
33
33
  }