@highstate/contract 0.4.3 → 0.4.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,30 +1,102 @@
1
- import { TString, TNumber, TBoolean, TArray, TUnion, TLiteral, TRecordOrObject, TAny, Static, TSchema, TAnySchema, TObject, TOptional } from '@sinclair/typebox';
1
+ import { Static, TString, TNumber, TBoolean, TArray, TUnion, TLiteral, TRecordOrObject, TAny, TSchema, TAnySchema, TOptional, TObject } from '@sinclair/typebox';
2
2
  export { Static, Type } from '@sinclair/typebox';
3
3
 
4
+ declare const InstanceType: unique symbol;
4
5
  type InstanceInput<TType extends string = string> = {
5
- type: TType;
6
+ [InstanceType]?: TType;
6
7
  instanceId: string;
7
8
  output: string;
8
9
  };
9
- type InstanceInputMap = Record<string, InstanceInput | InstanceInput[]>;
10
- type BaseInstanceModel = {
10
+ type HubInput = {
11
+ hubId: string;
12
+ };
13
+ type Position = {
14
+ x: number;
15
+ y: number;
16
+ };
17
+ type InstanceModel = {
18
+ /**
19
+ * The id of the instance unique within the project.
20
+ *
21
+ * The format is `${instanceType}:${instanceName}`.
22
+ */
11
23
  id: string;
24
+ /**
25
+ * The type of the instance.
26
+ */
12
27
  type: string;
28
+ /**
29
+ * The name of the instance.
30
+ *
31
+ * Must be unique within instances of the same type in the project.
32
+ */
13
33
  name: string;
34
+ /**
35
+ * The static arguments passed to the instance.
36
+ */
37
+ args?: Record<string, unknown>;
38
+ /**
39
+ * The direct instances passed as inputs to the instance.
40
+ */
41
+ inputs?: Record<string, InstanceInput[]>;
42
+ /**
43
+ * The inputs passed to the instance from the hubs.
44
+ *
45
+ * Only for designer-first instances.
46
+ */
47
+ hubInputs?: Record<string, HubInput[]>;
48
+ /**
49
+ * The inputs injected to the instance from the hubs.
50
+ *
51
+ * While `hubInputs` allows to pass hubs to distinct inputs,
52
+ * `injectionInputs` allows to pass hubs to the instance as a whole filling all inputs with matching types.
53
+ *
54
+ * Only for designer-first instances.
55
+ */
56
+ injectionInputs?: HubInput[];
57
+ /**
58
+ * The position of the instance on the canvas.
59
+ *
60
+ * Only for designer-first instances.
61
+ */
62
+ position?: Position;
63
+ /**
64
+ * The id of the parent instance.
65
+ *
66
+ * Only for child instances of the composite instances.
67
+ */
14
68
  parentId?: string;
15
- args: Record<string, unknown>;
16
- inputs: InstanceInputMap;
17
- outputs?: InstanceInputMap;
69
+ /**
70
+ * The direct instance inputs which instance returns as outputs.
71
+ *
72
+ * Only for computed composite instances.
73
+ */
74
+ outputs?: Record<string, InstanceInput[]>;
18
75
  };
76
+ /**
77
+ * Formats the instance id from the instance type and instance name.
78
+ *
79
+ * @param instanceType The type of the instance.
80
+ * @param instanceName The name of the instance.
81
+ *
82
+ * @returns The formatted instance id.
83
+ */
19
84
  declare function getInstanceId(instanceType: string, instanceName: string): string;
20
- declare function parseInstanceId(key: string): [string, string];
85
+ /**
86
+ * Parses the instance id into the instance type and instance name.
87
+ *
88
+ * @param instanceId The instance id to parse.
89
+ *
90
+ * @returns The instance type and instance name.
91
+ */
92
+ declare function parseInstanceId(instanceId: string): [instanceType: string, instanceName: string];
21
93
 
22
94
  type CompositeInstance = {
23
- instance: BaseInstanceModel;
24
- children: BaseInstanceModel[];
95
+ instance: InstanceModel;
96
+ children: InstanceModel[];
25
97
  };
26
98
  declare function resetEvaluation(): void;
27
- declare function getInstances(): readonly BaseInstanceModel[];
99
+ declare function getInstances(): readonly InstanceModel[];
28
100
  declare function getCompositeInstances(): readonly CompositeInstance[];
29
101
 
30
102
  type ArgumentValueSchema = TString | TNumber | TBoolean | TArray<TString> | TArray<TNumber> | TUnion<TLiteral[]> | TRecordOrObject<TString, TAny> | TArray;
@@ -50,6 +122,7 @@ type Meta = {
50
122
  };
51
123
 
52
124
  type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
125
+ type RequiredKeys<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
53
126
  type ListRequiredKeys<T extends Record<string, unknown>> = T extends Record<string, never> ? never : {
54
127
  [K in keyof T]-?: undefined extends T[K] ? never : K;
55
128
  }[keyof T];
@@ -73,12 +146,19 @@ type OptionalUndefinedFields<T extends Record<string, unknown>> = {
73
146
  [K in Exclude<keyof T, PickUndefinedKeys<T>>]: T[K];
74
147
  };
75
148
  /**
76
- * Removes the indentation from a multiline string.
149
+ * Formats a multiline string and trims the indentation.
77
150
  *
78
151
  * @param str The string to trim.
79
152
  * @returns The trimmed string.
80
153
  */
81
154
  declare function text(array: TemplateStringsArray, ...values: unknown[]): string;
155
+ /**
156
+ * Removes the indentation from a multiline string.
157
+ *
158
+ * @param text The text to trim.
159
+ * @returns The trimmed text.
160
+ */
161
+ declare function trimIndentation(text: string): string;
82
162
 
83
163
  /**
84
164
  * The entity is some abstract object which can be passed from one component to another through their inputs and outputs.
@@ -94,16 +174,12 @@ type Entity<TType extends string = string, TEntitySchema extends TSchema = TSche
94
174
  * The JSON schema of the entity value.
95
175
  */
96
176
  schema: TEntitySchema;
97
- /**
98
- * Whether the content of the entity is sensitive and should be hidden from the UI.
99
- */
100
- sensitive: boolean;
101
177
  /**
102
178
  * The extra metadata of the entity.
103
179
  */
104
180
  meta: Meta;
105
181
  };
106
- type EntityOptions<TType extends string, TSchema extends TAnySchema> = PartialKeys<Entity<TType, TSchema>, "sensitive" | "meta">;
182
+ type EntityOptions<TType extends string, TSchema extends TAnySchema> = PartialKeys<Entity<TType, TSchema>, "meta">;
107
183
  declare function defineEntity<TType extends string, TSchema extends TAnySchema>(options: EntityOptions<TType, TSchema>): Entity<TType, TSchema>;
108
184
  declare function isEntity(value: unknown): value is Entity;
109
185
 
@@ -218,7 +294,7 @@ declare function defineComponent<TArgs extends Record<string, ComponentArgumentO
218
294
  declare function isComponent(value: unknown): value is Component;
219
295
 
220
296
  type UnitOptions<TArgs extends Record<string, ComponentArgumentOptions>, TInputs extends Record<string, ComponentInputOptions>, TOutputs extends Record<string, ComponentInputOptions>, TSecrets extends Record<string, ComponentArgumentOptions>> = Omit<ComponentOptions<TArgs, TInputs, TOutputs>, "create"> & {
221
- source: UnitSource;
297
+ source?: UnitSource;
222
298
  secrets?: TSecrets;
223
299
  };
224
300
  type UnitSource = {
@@ -247,4 +323,4 @@ type Unit<TArgs extends Record<string, ArgumentValue> = Record<string, never>, T
247
323
  declare function isUnitModel(model: ComponentModel): model is UnitModel;
248
324
  declare function defineUnit<TArgs extends Record<string, ComponentArgumentOptions> = Record<string, never>, TInputs extends Record<string, ComponentInputOptions> = Record<string, never>, TOutputs extends Record<string, ComponentInputOptions> = Record<string, never>, TSecrets extends Record<string, ComponentArgumentOptions> = Record<string, never>>(options: UnitOptions<TArgs, TInputs, TOutputs, TSecrets>): Unit<ArgumentOptionsMapToStatic<TArgs>, ComponentInputOptionsMapToSpecMap<TInputs>, ComponentInputOptionsMapToSpecMap<TOutputs>, ArgumentOptionsMapToStatic<TSecrets>>;
249
325
 
250
- export { type ArgumentValue, type ArgumentValueSchema, type BaseInstanceModel, type Component, type ComponentArgument, type ComponentInput, type ComponentInputSpec, type ComponentMeta, type ComponentModel, type CompositeInstance, type Entity, type InstanceInput, type InstanceInputMap, type Unit, type UnitModel, type UnitSource, defineComponent, defineEntity, defineUnit, getCompositeInstances, getInstanceId, getInstances, isComponent, isEntity, isUnitModel, parseInstanceId, resetEvaluation, text };
326
+ export { type ArgumentValue, type ArgumentValueSchema, type Component, type ComponentArgument, type ComponentInput, type ComponentInputSpec, type ComponentMeta, type ComponentModel, type CompositeInstance, type Entity, type HubInput, type InstanceInput, type InstanceModel, type PartialKeys, type Position, type RequiredKeys, type Unit, type UnitModel, type UnitSource, defineComponent, defineEntity, defineUnit, getCompositeInstances, getInstanceId, getInstances, isComponent, isEntity, isUnitModel, parseInstanceId, resetEvaluation, text, trimIndentation };
package/dist/index.mjs CHANGED
@@ -1,14 +1,14 @@
1
1
  import { OptionalKind } from '@sinclair/typebox';
2
2
  export { Type } from '@sinclair/typebox';
3
- import { mapValues } from 'remeda';
3
+ import { mapValues, pickBy, isNonNullish } from 'remeda';
4
4
 
5
5
  function getInstanceId(instanceType, instanceName) {
6
6
  return `${instanceType}:${instanceName}`;
7
7
  }
8
- function parseInstanceId(key) {
9
- const parts = key.split(":");
8
+ function parseInstanceId(instanceId) {
9
+ const parts = instanceId.split(":");
10
10
  if (parts.length !== 2) {
11
- throw new Error(`Invalid instance key: ${key}`);
11
+ throw new Error(`Invalid instance key: ${instanceId}`);
12
12
  }
13
13
  return parts;
14
14
  }
@@ -51,13 +51,29 @@ function registerInstance(instance, fn) {
51
51
 
52
52
  function defineEntity(options) {
53
53
  return {
54
- sensitive: false,
55
54
  meta: {},
56
55
  ...options
57
56
  };
58
57
  }
59
58
  function isEntity(value) {
60
- return typeof value === "object" && value !== null && "type" in value && "schema" in value && "sensitive" in value && "meta" in value;
59
+ return typeof value === "object" && value !== null && "type" in value && "schema" in value && "meta" in value;
60
+ }
61
+
62
+ function text(array, ...values) {
63
+ const str = array.reduce(
64
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
65
+ (result, part, i) => result + part + (values[i] ? String(values[i]) : ""),
66
+ ""
67
+ );
68
+ return trimIndentation(str);
69
+ }
70
+ function trimIndentation(text2) {
71
+ const lines = text2.split("\n");
72
+ const indent = lines.filter((line) => line.trim() !== "").map((line) => line.match(/^\s*/)?.[0].length ?? 0).reduce((min, indent2) => Math.min(min, indent2), Infinity);
73
+ return lines.map((line) => line.slice(indent)).join("\n").trim();
74
+ }
75
+ function mapItemOrArray(item, fn) {
76
+ return Array.isArray(item) ? item.map(fn) : fn(item);
61
77
  }
62
78
 
63
79
  function defineComponent(options) {
@@ -73,22 +89,17 @@ function defineComponent(options) {
73
89
  inputs: inputs ?? {}
74
90
  },
75
91
  () => {
92
+ const parentInputs = mapValues(pickBy(inputs ?? {}, isNonNullish), (value, key) => {
93
+ return mapItemOrArray(value, () => ({ instanceId: id, output: key }));
94
+ });
76
95
  const outputs = options.create({
77
96
  id,
78
97
  name,
79
98
  args: args ?? {},
80
- inputs: mapValues(inputs ?? {}, (value, key) => {
81
- if (Array.isArray(value)) {
82
- return value.map((x) => substituteInstanceInput(id, x, key));
83
- }
84
- return substituteInstanceInput(id, value, key);
85
- })
99
+ inputs: parentInputs
86
100
  }) ?? {};
87
101
  const substitutedOutputs = mapValues(outputs, (value, key) => {
88
- if (Array.isArray(value)) {
89
- return value.map((x) => substituteInstanceInput(id, x, key));
90
- }
91
- return substituteInstanceInput(id, value, key);
102
+ return mapItemOrArray(value, () => ({ instanceId: id, output: key }));
92
103
  });
93
104
  return [substitutedOutputs, outputs];
94
105
  }
@@ -105,13 +116,6 @@ function defineComponent(options) {
105
116
  };
106
117
  return create;
107
118
  }
108
- function substituteInstanceInput(instanceId, input, outputKey) {
109
- return {
110
- type: input.type,
111
- instanceId,
112
- output: outputKey
113
- };
114
- }
115
119
  function isComponent(value) {
116
120
  return typeof value === "function" && "model" in value;
117
121
  }
@@ -176,9 +180,8 @@ function defineUnit(options) {
176
180
  function fillUnitOutputs(instanceId, optionsOutputs = {}) {
177
181
  const outputs = {};
178
182
  for (const key in optionsOutputs) {
179
- const entity = "entity" in optionsOutputs[key] ? optionsOutputs[key].entity : optionsOutputs[key];
180
183
  outputs[key] = {
181
- type: entity.type,
184
+ kind: "instance",
182
185
  instanceId,
183
186
  output: key
184
187
  };
@@ -186,11 +189,4 @@ function fillUnitOutputs(instanceId, optionsOutputs = {}) {
186
189
  return outputs;
187
190
  }
188
191
 
189
- function text(array, ...values) {
190
- const str = array.reduce((result, part, i) => result + part + (String(values[i]) || ""), "");
191
- const lines = str.split("\n");
192
- const indent = lines.filter((line) => line.trim() !== "").map((line) => line.match(/^\s*/)?.[0].length ?? 0).reduce((min, indent2) => Math.min(min, indent2), Infinity);
193
- return lines.map((line) => line.slice(indent)).join("\n").trim();
194
- }
195
-
196
- export { defineComponent, defineEntity, defineUnit, getCompositeInstances, getInstanceId, getInstances, isComponent, isEntity, isUnitModel, parseInstanceId, resetEvaluation, text };
192
+ export { defineComponent, defineEntity, defineUnit, getCompositeInstances, getInstanceId, getInstances, isComponent, isEntity, isUnitModel, parseInstanceId, resetEvaluation, text, trimIndentation };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@highstate/contract",
3
- "version": "0.4.3",
3
+ "version": "0.4.5",
4
4
  "type": "module",
5
5
  "module": "dist/index.mjs",
6
6
  "types": "dist/index.d.ts",
@@ -22,12 +22,12 @@
22
22
  },
23
23
  "dependencies": {
24
24
  "@sinclair/typebox": "^0.34.11",
25
- "remeda": "^2.17.4"
25
+ "remeda": "^2.21.0"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@vitest/coverage-v8": "2.1.8",
29
29
  "pkgroll": "^2.5.1",
30
30
  "vitest": "^2.1.8"
31
31
  },
32
- "gitHead": "5b99600bd04872a4817c97b26501e1f22c8a0780"
32
+ "gitHead": "61f6f1b0ff3f97bd7ed5e37c26302f094b892392"
33
33
  }