@highstate/contract 0.7.2 → 0.7.4

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.mjs DELETED
@@ -1,192 +0,0 @@
1
- import { OptionalKind } from '@sinclair/typebox';
2
- export { Type } from '@sinclair/typebox';
3
- import { mapValues, pickBy, isNonNullish } from 'remeda';
4
-
5
- function getInstanceId(instanceType, instanceName) {
6
- return `${instanceType}:${instanceName}`;
7
- }
8
- function parseInstanceId(instanceId) {
9
- const parts = instanceId.split(":");
10
- if (parts.length !== 2) {
11
- throw new Error(`Invalid instance key: ${instanceId}`);
12
- }
13
- return parts;
14
- }
15
-
16
- const instances = [];
17
- const compositeInstances = /* @__PURE__ */ new Map();
18
- let currentParentInstance = null;
19
- function resetEvaluation() {
20
- instances.length = 0;
21
- compositeInstances.clear();
22
- currentParentInstance = null;
23
- }
24
- function getInstances() {
25
- return instances;
26
- }
27
- function getCompositeInstances() {
28
- return Array.from(compositeInstances.values());
29
- }
30
- function registerInstance(instance, fn) {
31
- instances.push(instance);
32
- if (currentParentInstance) {
33
- instance.parentId = currentParentInstance.id;
34
- let compositeInstance = compositeInstances.get(currentParentInstance.id);
35
- if (!compositeInstance) {
36
- compositeInstance = { instance: currentParentInstance, children: [] };
37
- compositeInstances.set(currentParentInstance.id, compositeInstance);
38
- }
39
- compositeInstance.children.push(instance);
40
- }
41
- const previousParentInstance = currentParentInstance;
42
- currentParentInstance = instance;
43
- try {
44
- const [outputs, instanceOutputs] = fn();
45
- instance.outputs = instanceOutputs;
46
- return outputs;
47
- } finally {
48
- currentParentInstance = previousParentInstance;
49
- }
50
- }
51
-
52
- function defineEntity(options) {
53
- return {
54
- meta: {},
55
- ...options
56
- };
57
- }
58
- function isEntity(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);
77
- }
78
-
79
- function defineComponent(options) {
80
- function create(params) {
81
- const { name, args, inputs } = params;
82
- const id = `${options.type}:${name}`;
83
- return registerInstance(
84
- {
85
- id,
86
- type: options.type,
87
- name,
88
- args: args ?? {},
89
- inputs: inputs ?? {}
90
- },
91
- () => {
92
- const parentInputs = mapValues(pickBy(inputs ?? {}, isNonNullish), (value, key) => {
93
- return mapItemOrArray(value, () => ({ instanceId: id, output: key }));
94
- });
95
- const outputs = options.create({
96
- id,
97
- name,
98
- args: args ?? {},
99
- inputs: parentInputs
100
- }) ?? {};
101
- const substitutedOutputs = mapValues(outputs, (value, key) => {
102
- return mapItemOrArray(value, () => ({ instanceId: id, output: key }));
103
- });
104
- return [substitutedOutputs, outputs];
105
- }
106
- );
107
- }
108
- create.entities = /* @__PURE__ */ new Map();
109
- const mapInput = createInputMapper(create.entities);
110
- create.model = {
111
- type: options.type,
112
- args: mapValues(options.args ?? {}, mapArgument),
113
- inputs: mapValues(options.inputs ?? {}, mapInput),
114
- outputs: mapValues(options.outputs ?? {}, mapInput),
115
- meta: options.meta ?? {}
116
- };
117
- return create;
118
- }
119
- function isComponent(value) {
120
- return typeof value === "function" && "model" in value;
121
- }
122
- function mapArgument(value) {
123
- if ("schema" in value) {
124
- return {
125
- schema: value.schema,
126
- required: value.required ?? !value.schema[OptionalKind],
127
- meta: {
128
- displayName: value.displayName,
129
- description: value.description,
130
- color: value.color
131
- }
132
- };
133
- }
134
- return {
135
- schema: value,
136
- required: !value[OptionalKind],
137
- secret: false,
138
- meta: {}
139
- };
140
- }
141
- function createInputMapper(entities) {
142
- return (value) => {
143
- if ("entity" in value) {
144
- entities.set(value.entity.type, value.entity);
145
- return {
146
- type: value.entity.type,
147
- required: value.required ?? true,
148
- multiple: value.multiple ?? false,
149
- meta: {
150
- displayName: value.displayName,
151
- description: value.description,
152
- color: value.color
153
- }
154
- };
155
- }
156
- entities.set(value.type, value);
157
- return {
158
- type: value.type,
159
- required: true,
160
- multiple: false,
161
- meta: {}
162
- };
163
- };
164
- }
165
-
166
- function isUnitModel(model) {
167
- return "source" in model;
168
- }
169
- function defineUnit(options) {
170
- const component = defineComponent({
171
- ...options,
172
- create({ id }) {
173
- return fillUnitOutputs(id, options.outputs);
174
- }
175
- });
176
- component.model.source = options.source;
177
- component.model.secrets = mapValues(options.secrets ?? {}, mapArgument);
178
- return component;
179
- }
180
- function fillUnitOutputs(instanceId, optionsOutputs = {}) {
181
- const outputs = {};
182
- for (const key in optionsOutputs) {
183
- outputs[key] = {
184
- kind: "instance",
185
- instanceId,
186
- output: key
187
- };
188
- }
189
- return outputs;
190
- }
191
-
192
- export { defineComponent, defineEntity, defineUnit, getCompositeInstances, getInstanceId, getInstances, isComponent, isEntity, isUnitModel, parseInstanceId, resetEvaluation, text, trimIndentation };