@foresthubai/workflow-core 0.3.0 → 0.4.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.
Files changed (77) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +14 -14
  3. package/README.md +63 -63
  4. package/dist/api/workflow.d.ts +2 -2
  5. package/dist/api/workflow.d.ts.map +1 -1
  6. package/package.json +1 -1
  7. package/src/api/index.ts +11 -11
  8. package/src/api/workflow.ts +607 -607
  9. package/src/channel/Channel.ts +11 -11
  10. package/src/channel/ChannelDefinition.ts +76 -76
  11. package/src/channel/index.ts +6 -6
  12. package/src/channel/serialization.ts +68 -68
  13. package/src/deploy/index.ts +1 -1
  14. package/src/deploy/requirements.test.ts +61 -61
  15. package/src/deploy/requirements.ts +41 -41
  16. package/src/diagnostics/__fixtures__/diagnosticFixtures.ts +158 -158
  17. package/src/diagnostics/diagnostics.test.ts +878 -878
  18. package/src/diagnostics/diagnostics.ts +936 -936
  19. package/src/diagnostics/index.ts +11 -11
  20. package/src/edge/Edge.ts +23 -23
  21. package/src/edge/EdgeDefinition.ts +45 -45
  22. package/src/edge/EdgeType.ts +19 -19
  23. package/src/edge/index.ts +8 -8
  24. package/src/edge/serialization.ts +83 -83
  25. package/src/expression/index.ts +4 -4
  26. package/src/expression/parser.ts +362 -362
  27. package/src/expression/types.ts +30 -30
  28. package/src/function/FunctionDeclaration.ts +54 -54
  29. package/src/function/index.ts +3 -3
  30. package/src/function/serialization.ts +40 -40
  31. package/src/globals.d.ts +9 -9
  32. package/src/id/index.ts +8 -8
  33. package/src/index.ts +22 -22
  34. package/src/memory/Memory.ts +15 -15
  35. package/src/memory/MemoryDefinition.ts +16 -16
  36. package/src/memory/MemoryFileDefinition.ts +37 -37
  37. package/src/memory/MemoryRegistry.ts +35 -35
  38. package/src/memory/VectorDatabaseDefinition.ts +21 -21
  39. package/src/memory/index.ts +8 -8
  40. package/src/memory/serialization.ts +47 -47
  41. package/src/migration/index.ts +4 -4
  42. package/src/migration/migrate.test.ts +44 -44
  43. package/src/migration/migrate.ts +58 -58
  44. package/src/migration/migrations.ts +24 -24
  45. package/src/migration/version.ts +9 -9
  46. package/src/model/LLMModelDefinition.ts +12 -12
  47. package/src/model/Model.ts +39 -39
  48. package/src/model/ModelDefinition.ts +15 -15
  49. package/src/model/ModelRegistry.ts +33 -33
  50. package/src/model/index.ts +7 -7
  51. package/src/model/serialization.ts +30 -30
  52. package/src/node/AgentNode.ts +82 -82
  53. package/src/node/DataNode.ts +41 -41
  54. package/src/node/FunctionNode.ts +76 -76
  55. package/src/node/InputNode.ts +185 -185
  56. package/src/node/LogicNode.ts +33 -33
  57. package/src/node/MqttNode.ts +127 -127
  58. package/src/node/Node.ts +61 -61
  59. package/src/node/NodeDefinition.ts +37 -37
  60. package/src/node/NodeRegistry.ts +85 -85
  61. package/src/node/OutputNode.ts +87 -87
  62. package/src/node/ToolNode.ts +32 -32
  63. package/src/node/TriggerNode.ts +272 -272
  64. package/src/node/constants.ts +16 -16
  65. package/src/node/index.ts +26 -26
  66. package/src/node/methods.ts +278 -278
  67. package/src/node/serialization.ts +544 -544
  68. package/src/parameter/OutputParameter.ts +68 -68
  69. package/src/parameter/Parameter.ts +243 -243
  70. package/src/parameter/index.ts +33 -33
  71. package/src/variable/Variable.ts +10 -10
  72. package/src/variable/index.ts +16 -16
  73. package/src/variable/operations.ts +106 -106
  74. package/src/workflow/Workflow.ts +41 -41
  75. package/src/workflow/index.ts +3 -3
  76. package/src/workflow/serialization.test.ts +240 -240
  77. package/src/workflow/serialization.ts +242 -242
@@ -1,68 +1,68 @@
1
- import type { Reference } from "../api";
2
- import { DataType, FromArgs, unwrapFromArgs } from "./Parameter";
3
-
4
- // ============================================================================
5
- // RUNTIME VALUES
6
- // ============================================================================
7
-
8
- /**
9
- * Runtime binding stored for a static output. `active=false` means the output
10
- * is discarded — no variable is produced or assigned. mode/name/target are kept
11
- * as draft state when inactive so the row can round-trip through an off→on
12
- * toggle without losing the user's prior choice. The slot's dataType comes from
13
- * the StaticOutput definition — not carried here.
14
- */
15
- export type OutputBinding = { active: boolean; mode: "emit"; name: string } | { active: boolean; mode: "assign"; target: Reference };
16
-
17
- /**
18
- * Runtime entry stored in a list output. Each entry is a user-authored
19
- * output declaration: either a new variable (emit) with its own uid/name/dataType,
20
- * or a routing to an existing variable (assign). `name` doubles as the JSON
21
- * property name in the LLM's structured response and (for emit) the new
22
- * variable's display name in canvas scope; it must be non-empty and unique
23
- * within the OutputList parameter (validated by diagnostics).
24
- *
25
- * Unlike OutputBinding, the slot's dataType is carried on the declaration
26
- * itself as a contract — staleness against the target (assign mode) is a
27
- * diagnostic, not a silent retype. No "discard" mode: removing the entry is
28
- * how you discard it.
29
- */
30
- export type OutputDeclaration =
31
- | { mode: "emit"; uid: string; name: string; dataType: DataType }
32
- | { mode: "assign"; name: string; dataType: DataType; target: Reference };
33
-
34
- // ============================================================================
35
- // OUTPUT PARAMETER DEFINITIONS
36
- // ============================================================================
37
-
38
- /**
39
- * A fixed output produced by every instance of this node type. The `id` is
40
- * both the field name inside `node.arguments` where the OutputBinding lives
41
- * and the default emit variable name (overridable via the binding's `name`).
42
- * The dataType may be static or an args-derived lambda.
43
- */
44
- export interface StaticOutput {
45
- id: string;
46
- label: string;
47
- type: "static";
48
- dataType: FromArgs<DataType>;
49
- }
50
-
51
- /** Resolve a StaticOutput's dataType for the current node arguments. */
52
- export function resolveStaticOutputDataType(output: StaticOutput, args: Record<string, unknown>): DataType {
53
- return unwrapFromArgs(output.dataType, args);
54
- }
55
-
56
- /**
57
- * A user-managed list of outputs. Entries live in `node.arguments[id]` as
58
- * `OutputDeclaration[]` — the UI CRUDs the list directly, and each entry
59
- * contributes one output. Used for Agent's outputDeclarations.
60
- */
61
- export interface OutputList {
62
- id: string;
63
- label: string;
64
- type: "list";
65
- }
66
-
67
- /** Output parameters mirror input parameters: intersected with ParameterBase for id/label/etc. */
68
- export type OutputParameter = StaticOutput | OutputList;
1
+ import type { Reference } from "../api";
2
+ import { DataType, FromArgs, unwrapFromArgs } from "./Parameter";
3
+
4
+ // ============================================================================
5
+ // RUNTIME VALUES
6
+ // ============================================================================
7
+
8
+ /**
9
+ * Runtime binding stored for a static output. `active=false` means the output
10
+ * is discarded — no variable is produced or assigned. mode/name/target are kept
11
+ * as draft state when inactive so the row can round-trip through an off→on
12
+ * toggle without losing the user's prior choice. The slot's dataType comes from
13
+ * the StaticOutput definition — not carried here.
14
+ */
15
+ export type OutputBinding = { active: boolean; mode: "emit"; name: string } | { active: boolean; mode: "assign"; target: Reference };
16
+
17
+ /**
18
+ * Runtime entry stored in a list output. Each entry is a user-authored
19
+ * output declaration: either a new variable (emit) with its own uid/name/dataType,
20
+ * or a routing to an existing variable (assign). `name` doubles as the JSON
21
+ * property name in the LLM's structured response and (for emit) the new
22
+ * variable's display name in canvas scope; it must be non-empty and unique
23
+ * within the OutputList parameter (validated by diagnostics).
24
+ *
25
+ * Unlike OutputBinding, the slot's dataType is carried on the declaration
26
+ * itself as a contract — staleness against the target (assign mode) is a
27
+ * diagnostic, not a silent retype. No "discard" mode: removing the entry is
28
+ * how you discard it.
29
+ */
30
+ export type OutputDeclaration =
31
+ | { mode: "emit"; uid: string; name: string; dataType: DataType }
32
+ | { mode: "assign"; name: string; dataType: DataType; target: Reference };
33
+
34
+ // ============================================================================
35
+ // OUTPUT PARAMETER DEFINITIONS
36
+ // ============================================================================
37
+
38
+ /**
39
+ * A fixed output produced by every instance of this node type. The `id` is
40
+ * both the field name inside `node.arguments` where the OutputBinding lives
41
+ * and the default emit variable name (overridable via the binding's `name`).
42
+ * The dataType may be static or an args-derived lambda.
43
+ */
44
+ export interface StaticOutput {
45
+ id: string;
46
+ label: string;
47
+ type: "static";
48
+ dataType: FromArgs<DataType>;
49
+ }
50
+
51
+ /** Resolve a StaticOutput's dataType for the current node arguments. */
52
+ export function resolveStaticOutputDataType(output: StaticOutput, args: Record<string, unknown>): DataType {
53
+ return unwrapFromArgs(output.dataType, args);
54
+ }
55
+
56
+ /**
57
+ * A user-managed list of outputs. Entries live in `node.arguments[id]` as
58
+ * `OutputDeclaration[]` — the UI CRUDs the list directly, and each entry
59
+ * contributes one output. Used for Agent's outputDeclarations.
60
+ */
61
+ export interface OutputList {
62
+ id: string;
63
+ label: string;
64
+ type: "list";
65
+ }
66
+
67
+ /** Output parameters mirror input parameters: intersected with ParameterBase for id/label/etc. */
68
+ export type OutputParameter = StaticOutput | OutputList;
@@ -1,243 +1,243 @@
1
- import type { Expression, Reference } from "../api";
2
- import type { ChannelType } from "../channel";
3
- import type { MemoryType } from "../memory";
4
- import type { ModelType, ModelCapability } from "../model";
5
- import { refToLookupKey } from "../variable";
6
- import type { Schemas } from "../api";
7
-
8
- export type DataType = Schemas["DataType"];
9
-
10
- /**
11
- * A field that is either a static value of T, or a function that derives T
12
- * from the owning node's current arguments. Scope is strictly args-only —
13
- * no access to variables, IO labels, or other canvas state. For wider
14
- * context, use a purpose-named accessor (e.g. resolveExpressionType).
15
- */
16
- export type FromArgs<T> = T | ((args: Record<string, unknown>) => T);
17
-
18
- /** Unwrap a FromArgs value against the current node arguments. */
19
- export function unwrapFromArgs<T>(value: FromArgs<T>, args: Record<string, unknown>): T {
20
- return typeof value === "function" ? (value as (args: Record<string, unknown>) => T)(args) : value;
21
- }
22
-
23
- // ============================================================================
24
- // Parameter definitions
25
- // ============================================================================
26
-
27
- export interface ParameterBase {
28
- id: string;
29
- label: string;
30
- description: string;
31
- optional?: boolean;
32
- /** Parameter is only active (visible, validated, serialized) when all rules are met. */
33
- activationRules?: ActivationRule[];
34
- }
35
-
36
- export interface BasicParam {
37
- type: "int" | "float" | "time";
38
- default?: number | string;
39
- }
40
-
41
- export interface StringParam {
42
- type: "string";
43
- multiline?: boolean;
44
- default?: string;
45
- }
46
-
47
- export interface BoolParam {
48
- type: "bool";
49
- default: boolean;
50
- }
51
-
52
- export interface WeekdaysParam {
53
- type: "weekdays";
54
- default: string[];
55
- }
56
-
57
- export interface SelectionParam {
58
- type: "selection";
59
- options: Array<{ value: string; label: string }>;
60
- default?: string;
61
- }
62
-
63
- export interface ExpressionParam {
64
- type: "expression";
65
- /** Type can be dynamic (static value or args-only lambda). */
66
- expressionType: FromArgs<DataType>;
67
- /**
68
- * Escape hatch for the variableSelect case: when set, points to the id of a sibling
69
- * variableSelect parameter. If that parameter holds a live Reference, the expressionType
70
- * is taken from the referenced variable. Falls back to `expressionType` otherwise.
71
- */
72
- fromReference?: string;
73
- /**
74
- * Required: An expression always has a value object never an unset state.
75
- */
76
- default: Expression;
77
- }
78
-
79
- // Reference-select parameters: pick an external entity by id. No `default`.
80
-
81
- export interface VariableSelectParam {
82
- type: "variableSelect";
83
- default?: never;
84
- }
85
-
86
- // Model reference parameter — selects a model id from the static catalog (props)
87
- // unioned with declared custom models, filtered by model type and capability.
88
- export interface ModelSelectParam {
89
- type: "modelSelect";
90
- /** Model types this slot accepts (e.g. ["LLMModel"]). Static list or args-derived lambda. */
91
- modelType: FromArgs<ModelType[]>;
92
- /** Optional capability filter (e.g. ["chat"]) applied to catalog and declared models alike. */
93
- capabilities?: FromArgs<ModelCapability[]>;
94
- default?: never;
95
- }
96
-
97
- export interface ChannelSelectParam {
98
- type: "channelSelect";
99
- /** Channel types this slot accepts. Static list or args-derived lambda. */
100
- channelType: FromArgs<ChannelType[]>;
101
- default?: never;
102
- }
103
-
104
- export interface MemorySelectParam {
105
- type: "memorySelect";
106
- /** Memory types this slot accepts (e.g. ["VectorDatabase"]). Static list or args-derived lambda. */
107
- memoryType: FromArgs<MemoryType[]>;
108
- default?: never;
109
- }
110
-
111
- /**
112
- * List parameter that binds an agent node to project-declared memory files,
113
- * each with an access mode (`r` = read-only, `rw` = read + write). The editor
114
- * holds the array directly; the API schema (`MemoryRef[]`) round-trips 1:1.
115
- *
116
- * `default` is required: a list param is always a
117
- * concrete array, never unset — declare `default: []` for "starts empty".
118
- */
119
- export interface MemoryRefsParam {
120
- type: "memory-refs";
121
- default: Schemas["MemoryRef"][];
122
- }
123
-
124
- /** Union of all reference-select parameter variants, used for type guards. */
125
- export type ReferenceSelectParam = VariableSelectParam | ChannelSelectParam | MemorySelectParam | ModelSelectParam;
126
-
127
- export function isReferenceSelectParam(param: Parameter): param is ParameterBase & ReferenceSelectParam {
128
- return param.type === "variableSelect" || param.type === "channelSelect" || param.type === "memorySelect" || param.type === "modelSelect";
129
- }
130
-
131
- export type Parameter =
132
- | (ParameterBase & BasicParam)
133
- | (ParameterBase & VariableSelectParam)
134
- | (ParameterBase & StringParam)
135
- | (ParameterBase & BoolParam)
136
- | (ParameterBase & WeekdaysParam)
137
- | (ParameterBase & SelectionParam)
138
- | (ParameterBase & MemorySelectParam)
139
- | (ParameterBase & ModelSelectParam)
140
- | (ParameterBase & ExpressionParam)
141
- | (ParameterBase & ChannelSelectParam)
142
- | (ParameterBase & MemoryRefsParam);
143
-
144
- // ============================================================================
145
- // Parameter activation rules
146
- // ============================================================================
147
-
148
- /** Typed union of rules that control whether a parameter is active (visible, validated, serialized). */
149
- export type ActivationRule =
150
- | { type: "parameterIn"; parameterId: string; values: unknown[] }
151
- | { type: "isControlFlow" }
152
- | { type: "isToolInput" };
153
-
154
- /**
155
- * Evaluate whether a parameter is active (visible, validated, serialized) given current context.
156
- * All rules must be satisfied (AND logic). Undefined or empty array = always active.
157
- */
158
- export function isParameterActive(param: Parameter, parameterValues: Record<string, unknown>, isToolInput: boolean): boolean {
159
- if (!param.activationRules?.length) return true;
160
- return param.activationRules.every((cond) => {
161
- switch (cond.type) {
162
- case "isControlFlow":
163
- return !isToolInput;
164
- case "isToolInput":
165
- return isToolInput;
166
- case "parameterIn":
167
- return cond.values.includes(parameterValues[cond.parameterId]);
168
- }
169
- });
170
- }
171
-
172
- /**
173
- * Whether a scalar argument value counts as "unset" for api purposes:
174
- * `undefined`, `null`, or `""`. Deliberately NOT `false`/`0` (valid values) nor
175
- * `[]` — an empty list is a real value, since list params are always
176
- * materialized to a concrete array. Shared by required-param diagnostics and
177
- * {@link pruneArguments} so "unset" has exactly one definition.
178
- */
179
- export function isEmpty(value: unknown): boolean {
180
- return value === undefined || value === null || value === "";
181
- }
182
-
183
- /**
184
- * Prune arguments that must not appear in the api, at the domain→api boundary.
185
- * Mutates `args` in place. Two reasons an arg is dropped:
186
- * 1. inactive — its parameter's activation rules aren't met for this context.
187
- * 2. the parameter {@link isEmpty}.
188
- */
189
- export function pruneArguments(args: Record<string, unknown>, parameters: readonly Parameter[], isToolInput = false): void {
190
- for (const param of parameters) {
191
- const inactive = !!param.activationRules?.length && !isParameterActive(param, args, isToolInput);
192
- if (inactive || isEmpty(args[param.id])) {
193
- delete args[param.id];
194
- }
195
- }
196
- }
197
-
198
- // ============================================================================
199
- // Parameter resolvers
200
- // ============================================================================
201
-
202
- /**
203
- * Resolve an ExpressionParam's expected dataType. Handles the `fromReference` escape hatch
204
- * (type taken from a live variableSelect sibling parameter) and falls back to the
205
- * declared FromArgs<DataType>.
206
- */
207
- export function resolveExpressionType(
208
- param: ExpressionParam,
209
- args: Record<string, unknown>,
210
- variables: Record<string, { dataType: DataType }>,
211
- ): DataType {
212
- if (param.fromReference) {
213
- const ref = args[param.fromReference] as Reference | undefined;
214
- if (ref?.varId) {
215
- const v = variables[refToLookupKey(ref)];
216
- if (v) return v.dataType;
217
- }
218
- }
219
- return unwrapFromArgs(param.expressionType, args);
220
- }
221
-
222
- /** Resolve a capability filter (ModelSelectParam) for the current node arguments. */
223
- export function resolveCapabilities(
224
- param: { capabilities?: FromArgs<ModelCapability[]> },
225
- args: Record<string, unknown>,
226
- ): ModelCapability[] | undefined {
227
- return param.capabilities === undefined ? undefined : unwrapFromArgs(param.capabilities, args);
228
- }
229
-
230
- /** Resolve the allowed channel types for a ChannelSelectParam. */
231
- export function resolveChannelTypes(param: ChannelSelectParam, args: Record<string, unknown>): ChannelType[] {
232
- return unwrapFromArgs(param.channelType, args);
233
- }
234
-
235
- /** Resolve the allowed memory types for a MemorySelectParam. */
236
- export function resolveMemoryTypes(param: MemorySelectParam, args: Record<string, unknown>): MemoryType[] {
237
- return unwrapFromArgs(param.memoryType, args);
238
- }
239
-
240
- /** Resolve the allowed model types for a ModelSelectParam. */
241
- export function resolveModelTypes(param: ModelSelectParam, args: Record<string, unknown>): ModelType[] {
242
- return unwrapFromArgs(param.modelType, args);
243
- }
1
+ import type { Expression, Reference } from "../api";
2
+ import type { ChannelType } from "../channel";
3
+ import type { MemoryType } from "../memory";
4
+ import type { ModelType, ModelCapability } from "../model";
5
+ import { refToLookupKey } from "../variable";
6
+ import type { Schemas } from "../api";
7
+
8
+ export type DataType = Schemas["DataType"];
9
+
10
+ /**
11
+ * A field that is either a static value of T, or a function that derives T
12
+ * from the owning node's current arguments. Scope is strictly args-only —
13
+ * no access to variables, IO labels, or other canvas state. For wider
14
+ * context, use a purpose-named accessor (e.g. resolveExpressionType).
15
+ */
16
+ export type FromArgs<T> = T | ((args: Record<string, unknown>) => T);
17
+
18
+ /** Unwrap a FromArgs value against the current node arguments. */
19
+ export function unwrapFromArgs<T>(value: FromArgs<T>, args: Record<string, unknown>): T {
20
+ return typeof value === "function" ? (value as (args: Record<string, unknown>) => T)(args) : value;
21
+ }
22
+
23
+ // ============================================================================
24
+ // Parameter definitions
25
+ // ============================================================================
26
+
27
+ export interface ParameterBase {
28
+ id: string;
29
+ label: string;
30
+ description: string;
31
+ optional?: boolean;
32
+ /** Parameter is only active (visible, validated, serialized) when all rules are met. */
33
+ activationRules?: ActivationRule[];
34
+ }
35
+
36
+ export interface BasicParam {
37
+ type: "int" | "float" | "time";
38
+ default?: number | string;
39
+ }
40
+
41
+ export interface StringParam {
42
+ type: "string";
43
+ multiline?: boolean;
44
+ default?: string;
45
+ }
46
+
47
+ export interface BoolParam {
48
+ type: "bool";
49
+ default: boolean;
50
+ }
51
+
52
+ export interface WeekdaysParam {
53
+ type: "weekdays";
54
+ default: string[];
55
+ }
56
+
57
+ export interface SelectionParam {
58
+ type: "selection";
59
+ options: Array<{ value: string; label: string }>;
60
+ default?: string;
61
+ }
62
+
63
+ export interface ExpressionParam {
64
+ type: "expression";
65
+ /** Type can be dynamic (static value or args-only lambda). */
66
+ expressionType: FromArgs<DataType>;
67
+ /**
68
+ * Escape hatch for the variableSelect case: when set, points to the id of a sibling
69
+ * variableSelect parameter. If that parameter holds a live Reference, the expressionType
70
+ * is taken from the referenced variable. Falls back to `expressionType` otherwise.
71
+ */
72
+ fromReference?: string;
73
+ /**
74
+ * Required: An expression always has a value object never an unset state.
75
+ */
76
+ default: Expression;
77
+ }
78
+
79
+ // Reference-select parameters: pick an external entity by id. No `default`.
80
+
81
+ export interface VariableSelectParam {
82
+ type: "variableSelect";
83
+ default?: never;
84
+ }
85
+
86
+ // Model reference parameter — selects a model id from the static catalog (props)
87
+ // unioned with declared custom models, filtered by model type and capability.
88
+ export interface ModelSelectParam {
89
+ type: "modelSelect";
90
+ /** Model types this slot accepts (e.g. ["LLMModel"]). Static list or args-derived lambda. */
91
+ modelType: FromArgs<ModelType[]>;
92
+ /** Optional capability filter (e.g. ["chat"]) applied to catalog and declared models alike. */
93
+ capabilities?: FromArgs<ModelCapability[]>;
94
+ default?: never;
95
+ }
96
+
97
+ export interface ChannelSelectParam {
98
+ type: "channelSelect";
99
+ /** Channel types this slot accepts. Static list or args-derived lambda. */
100
+ channelType: FromArgs<ChannelType[]>;
101
+ default?: never;
102
+ }
103
+
104
+ export interface MemorySelectParam {
105
+ type: "memorySelect";
106
+ /** Memory types this slot accepts (e.g. ["VectorDatabase"]). Static list or args-derived lambda. */
107
+ memoryType: FromArgs<MemoryType[]>;
108
+ default?: never;
109
+ }
110
+
111
+ /**
112
+ * List parameter that binds an agent node to project-declared memory files,
113
+ * each with an access mode (`r` = read-only, `rw` = read + write). The editor
114
+ * holds the array directly; the API schema (`MemoryRef[]`) round-trips 1:1.
115
+ *
116
+ * `default` is required: a list param is always a
117
+ * concrete array, never unset — declare `default: []` for "starts empty".
118
+ */
119
+ export interface MemoryRefsParam {
120
+ type: "memory-refs";
121
+ default: Schemas["MemoryRef"][];
122
+ }
123
+
124
+ /** Union of all reference-select parameter variants, used for type guards. */
125
+ export type ReferenceSelectParam = VariableSelectParam | ChannelSelectParam | MemorySelectParam | ModelSelectParam;
126
+
127
+ export function isReferenceSelectParam(param: Parameter): param is ParameterBase & ReferenceSelectParam {
128
+ return param.type === "variableSelect" || param.type === "channelSelect" || param.type === "memorySelect" || param.type === "modelSelect";
129
+ }
130
+
131
+ export type Parameter =
132
+ | (ParameterBase & BasicParam)
133
+ | (ParameterBase & VariableSelectParam)
134
+ | (ParameterBase & StringParam)
135
+ | (ParameterBase & BoolParam)
136
+ | (ParameterBase & WeekdaysParam)
137
+ | (ParameterBase & SelectionParam)
138
+ | (ParameterBase & MemorySelectParam)
139
+ | (ParameterBase & ModelSelectParam)
140
+ | (ParameterBase & ExpressionParam)
141
+ | (ParameterBase & ChannelSelectParam)
142
+ | (ParameterBase & MemoryRefsParam);
143
+
144
+ // ============================================================================
145
+ // Parameter activation rules
146
+ // ============================================================================
147
+
148
+ /** Typed union of rules that control whether a parameter is active (visible, validated, serialized). */
149
+ export type ActivationRule =
150
+ | { type: "parameterIn"; parameterId: string; values: unknown[] }
151
+ | { type: "isControlFlow" }
152
+ | { type: "isToolInput" };
153
+
154
+ /**
155
+ * Evaluate whether a parameter is active (visible, validated, serialized) given current context.
156
+ * All rules must be satisfied (AND logic). Undefined or empty array = always active.
157
+ */
158
+ export function isParameterActive(param: Parameter, parameterValues: Record<string, unknown>, isToolInput: boolean): boolean {
159
+ if (!param.activationRules?.length) return true;
160
+ return param.activationRules.every((cond) => {
161
+ switch (cond.type) {
162
+ case "isControlFlow":
163
+ return !isToolInput;
164
+ case "isToolInput":
165
+ return isToolInput;
166
+ case "parameterIn":
167
+ return cond.values.includes(parameterValues[cond.parameterId]);
168
+ }
169
+ });
170
+ }
171
+
172
+ /**
173
+ * Whether a scalar argument value counts as "unset" for api purposes:
174
+ * `undefined`, `null`, or `""`. Deliberately NOT `false`/`0` (valid values) nor
175
+ * `[]` — an empty list is a real value, since list params are always
176
+ * materialized to a concrete array. Shared by required-param diagnostics and
177
+ * {@link pruneArguments} so "unset" has exactly one definition.
178
+ */
179
+ export function isEmpty(value: unknown): boolean {
180
+ return value === undefined || value === null || value === "";
181
+ }
182
+
183
+ /**
184
+ * Prune arguments that must not appear in the api, at the domain→api boundary.
185
+ * Mutates `args` in place. Two reasons an arg is dropped:
186
+ * 1. inactive — its parameter's activation rules aren't met for this context.
187
+ * 2. the parameter {@link isEmpty}.
188
+ */
189
+ export function pruneArguments(args: Record<string, unknown>, parameters: readonly Parameter[], isToolInput = false): void {
190
+ for (const param of parameters) {
191
+ const inactive = !!param.activationRules?.length && !isParameterActive(param, args, isToolInput);
192
+ if (inactive || isEmpty(args[param.id])) {
193
+ delete args[param.id];
194
+ }
195
+ }
196
+ }
197
+
198
+ // ============================================================================
199
+ // Parameter resolvers
200
+ // ============================================================================
201
+
202
+ /**
203
+ * Resolve an ExpressionParam's expected dataType. Handles the `fromReference` escape hatch
204
+ * (type taken from a live variableSelect sibling parameter) and falls back to the
205
+ * declared FromArgs<DataType>.
206
+ */
207
+ export function resolveExpressionType(
208
+ param: ExpressionParam,
209
+ args: Record<string, unknown>,
210
+ variables: Record<string, { dataType: DataType }>,
211
+ ): DataType {
212
+ if (param.fromReference) {
213
+ const ref = args[param.fromReference] as Reference | undefined;
214
+ if (ref?.varId) {
215
+ const v = variables[refToLookupKey(ref)];
216
+ if (v) return v.dataType;
217
+ }
218
+ }
219
+ return unwrapFromArgs(param.expressionType, args);
220
+ }
221
+
222
+ /** Resolve a capability filter (ModelSelectParam) for the current node arguments. */
223
+ export function resolveCapabilities(
224
+ param: { capabilities?: FromArgs<ModelCapability[]> },
225
+ args: Record<string, unknown>,
226
+ ): ModelCapability[] | undefined {
227
+ return param.capabilities === undefined ? undefined : unwrapFromArgs(param.capabilities, args);
228
+ }
229
+
230
+ /** Resolve the allowed channel types for a ChannelSelectParam. */
231
+ export function resolveChannelTypes(param: ChannelSelectParam, args: Record<string, unknown>): ChannelType[] {
232
+ return unwrapFromArgs(param.channelType, args);
233
+ }
234
+
235
+ /** Resolve the allowed memory types for a MemorySelectParam. */
236
+ export function resolveMemoryTypes(param: MemorySelectParam, args: Record<string, unknown>): MemoryType[] {
237
+ return unwrapFromArgs(param.memoryType, args);
238
+ }
239
+
240
+ /** Resolve the allowed model types for a ModelSelectParam. */
241
+ export function resolveModelTypes(param: ModelSelectParam, args: Record<string, unknown>): ModelType[] {
242
+ return unwrapFromArgs(param.modelType, args);
243
+ }