chain-functions-behavior 1.0.0 → 1.0.1

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,334 @@
1
+ type BehaviorInput = Record<string, unknown>;
2
+ type BehaviorProps = Record<string, unknown>;
3
+ type BehaviorConfig = {
4
+ version?: 1;
5
+ strategies: Record<string, BehaviorStrategy>;
6
+ entrypoints?: Record<string, string>;
7
+ };
8
+ type BehaviorStrategy = {
9
+ fn: string;
10
+ props?: BehaviorProps;
11
+ when?: BehaviorConditionExpression;
12
+ then?: BehaviorNext[];
13
+ catch?: BehaviorNext[];
14
+ mode?: BehaviorMode;
15
+ terminal?: boolean;
16
+ tags?: string[];
17
+ description?: string;
18
+ };
19
+ type BehaviorMode = 'sequence' | 'selector' | 'parallel';
20
+ type BehaviorNext = string | {
21
+ id?: string;
22
+ strategy: string;
23
+ props?: BehaviorProps;
24
+ when?: BehaviorConditionExpression;
25
+ };
26
+ type BehaviorConditionExpression = boolean | [operator: string, ...args: unknown[]];
27
+ type BehaviorAction<TContext, TPatch = unknown> = (args: BehaviorActionArgs<TContext>) => BehaviorActionResult<TContext, TPatch> | Promise<BehaviorActionResult<TContext, TPatch>>;
28
+ type BehaviorActionArgs<TContext> = {
29
+ context: TContext;
30
+ props: BehaviorProps;
31
+ input: BehaviorInput;
32
+ signal: AbortSignal;
33
+ runtime: BehaviorRuntime;
34
+ };
35
+ type BehaviorActionResult<TContext, TPatch = unknown> = void | false | BehaviorActionSuccess<TContext, TPatch> | BehaviorActionSkip | BehaviorActionStop<TPatch> | BehaviorActionFail;
36
+ type BehaviorActionSuccess<TContext, TPatch> = {
37
+ type?: 'success';
38
+ context?: TContext;
39
+ data?: Record<string, unknown>;
40
+ patch?: TPatch | TPatch[];
41
+ events?: BehaviorEvent[];
42
+ continue?: boolean;
43
+ };
44
+ type BehaviorActionSkip = {
45
+ type: 'skip';
46
+ reason?: string;
47
+ data?: Record<string, unknown>;
48
+ };
49
+ type BehaviorActionStop<TPatch> = {
50
+ type: 'stop';
51
+ reason?: string;
52
+ patch?: TPatch | TPatch[];
53
+ events?: BehaviorEvent[];
54
+ };
55
+ type BehaviorActionFail = {
56
+ type: 'fail';
57
+ reason?: string;
58
+ error?: unknown;
59
+ data?: Record<string, unknown>;
60
+ };
61
+ type BehaviorEvent = {
62
+ type: string;
63
+ payload?: unknown;
64
+ };
65
+ type BehaviorEventMap = Record<string, unknown>;
66
+ type BehaviorEventName<TEvents extends object> = Extract<keyof TEvents, string>;
67
+ type BehaviorBusEvent<TPayload = unknown> = {
68
+ id: string;
69
+ topic: string;
70
+ occurredAt: number;
71
+ origin?: string;
72
+ parsed: TPayload;
73
+ serialized: string;
74
+ };
75
+ type BehaviorEventHandler<TEvents extends object, TEvent extends BehaviorEventName<TEvents>> = (event: BehaviorBusEvent<TEvents[TEvent]>) => void;
76
+ type BehaviorBusErrorEvent<TEvents extends object> = {
77
+ type: 'serialization';
78
+ topic: BehaviorEventName<TEvents>;
79
+ payload: TEvents[BehaviorEventName<TEvents>];
80
+ origin?: string;
81
+ error: unknown;
82
+ } | {
83
+ type: 'subscriber';
84
+ event: BehaviorBusEvent<TEvents[BehaviorEventName<TEvents>]>;
85
+ error: unknown;
86
+ };
87
+ type BehaviorBusEmitOptions = {
88
+ origin?: string;
89
+ };
90
+ type BehaviorBusOptions<TEvents extends object> = {
91
+ onError?: (event: BehaviorBusErrorEvent<TEvents>) => void;
92
+ };
93
+ type BehaviorBus<TEvents extends object = BehaviorEventMap> = {
94
+ on<TEvent extends BehaviorEventName<TEvents>>(event: TEvent, handler: BehaviorEventHandler<TEvents, TEvent>): () => void;
95
+ off<TEvent extends BehaviorEventName<TEvents>>(event: TEvent, handler?: BehaviorEventHandler<TEvents, TEvent>): void;
96
+ emit<TEvent extends BehaviorEventName<TEvents>>(topic: TEvent, payload: TEvents[TEvent], options?: BehaviorBusEmitOptions): BehaviorBusEvent<TEvents[TEvent]>;
97
+ dispatch(event: unknown): BehaviorBusEvent | undefined;
98
+ };
99
+ type BehaviorWsSocket = {
100
+ readyState: number;
101
+ send(data: string): void;
102
+ close(): void;
103
+ addEventListener(type: 'open' | 'close' | 'error' | 'message', listener: EventListener): void;
104
+ removeEventListener(type: 'open' | 'close' | 'error' | 'message', listener: EventListener): void;
105
+ };
106
+ type BehaviorWsRetryOptions = {
107
+ initialDelay?: number;
108
+ maxDelay?: number;
109
+ multiplier?: number;
110
+ jitter?: boolean;
111
+ };
112
+ type BehaviorWsOptions<TEvents extends object = BehaviorEventMap> = {
113
+ bus: BehaviorBus<TEvents>;
114
+ createSocket: () => BehaviorWsSocket;
115
+ inboundTopics?: BehaviorEventName<TEvents>[];
116
+ outboundTopics?: BehaviorEventName<TEvents>[];
117
+ origin?: string;
118
+ retry?: BehaviorWsRetryOptions;
119
+ };
120
+ type BehaviorWsStatus = 'idle' | 'connecting' | 'connected' | 'retrying' | 'stopped';
121
+ type BehaviorWs = {
122
+ start(): void;
123
+ stop(): void;
124
+ reconnect(): void;
125
+ status(): BehaviorWsStatus;
126
+ };
127
+ type BehaviorBindingEventMap = Record<string, BehaviorInput>;
128
+ type BehaviorBusBindingKey<TEvents extends object> = {
129
+ [TEvent in BehaviorEventName<TEvents>]: TEvents[TEvent] extends BehaviorInput ? `[bus] ${TEvent}` : never;
130
+ }[BehaviorEventName<TEvents>];
131
+ type BehaviorConcurrencyMode = 'parallel' | 'latest' | 'queue' | 'drop';
132
+ type BehaviorQueueOverflow = 'drop-oldest' | 'drop-newest';
133
+ type BehaviorConcurrencyOptions<TPayload = BehaviorInput> = {
134
+ mode?: BehaviorConcurrencyMode;
135
+ key?: (payload: TPayload) => string;
136
+ maxQueueSize?: number;
137
+ overflow?: BehaviorQueueOverflow;
138
+ };
139
+ type BehaviorBusBinding<TPayload = BehaviorInput> = {
140
+ entrypoint: string;
141
+ options?: {
142
+ concurrency?: BehaviorConcurrencyOptions<TPayload>;
143
+ };
144
+ };
145
+ type BehaviorBusBindings<TEvents extends object> = {
146
+ [TBinding in BehaviorBusBindingKey<TEvents>]?: BehaviorBusBinding<TEvents[Extract<TBinding extends `[bus] ${infer TEvent}` ? TEvent : never, BehaviorEventName<TEvents>>]>;
147
+ };
148
+ type BehaviorDomBindingKey = `[dom] ${string}:${string}`;
149
+ type BehaviorDomForm = Record<string, FormDataEntryValue | FormDataEntryValue[]>;
150
+ type BehaviorDomInput = BehaviorInput & {
151
+ type: string;
152
+ value?: string;
153
+ dataset: Record<string, string>;
154
+ form?: BehaviorDomForm;
155
+ };
156
+ type BehaviorDomBinding = {
157
+ entrypoint: string;
158
+ options?: {
159
+ preventDefault?: boolean;
160
+ stopPropagation?: boolean;
161
+ capture?: boolean;
162
+ once?: boolean;
163
+ input?: (scope: {
164
+ event: Event;
165
+ element: Element;
166
+ defaultInput: BehaviorDomInput;
167
+ }) => BehaviorInput;
168
+ concurrency?: BehaviorConcurrencyOptions<BehaviorDomInput>;
169
+ };
170
+ };
171
+ type BehaviorDomBindings = Partial<Record<BehaviorDomBindingKey, BehaviorDomBinding>>;
172
+ type ChainBehaviorDefinition<TContext, TPatch = unknown, TEvents extends object = BehaviorBindingEventMap> = {
173
+ config: BehaviorConfig;
174
+ actions?: Record<string, BehaviorAction<TContext, TPatch>>;
175
+ conditions?: Record<string, BehaviorConditionFn<TContext>>;
176
+ events?: BehaviorBusBindings<TEvents> & BehaviorDomBindings;
177
+ };
178
+ type ChainBehaviorOptions<TContext, TPatch = unknown, TEvents extends object = BehaviorBindingEventMap> = BehaviorRunnerOptions<TContext, TPatch> & {
179
+ context: TContext | (() => TContext);
180
+ bus?: BehaviorBus<TEvents>;
181
+ root?: Document | Element;
182
+ concurrency?: BehaviorConcurrencyOptions;
183
+ onRunnerError?: (event: BehaviorRunnerErrorEvent<TContext, TPatch>) => void;
184
+ };
185
+ type BehaviorRunnerErrorEvent<TContext, TPatch = unknown> = {
186
+ error: BehaviorError;
187
+ result: BehaviorRunResult<TContext, TPatch>;
188
+ binding: string;
189
+ entrypoint: string;
190
+ runId: string;
191
+ key?: string;
192
+ };
193
+ type BehaviorInactiveBinding = {
194
+ binding: string;
195
+ reason: 'unsupported-source' | 'dom-unavailable';
196
+ };
197
+ type BehaviorStartResult = {
198
+ active: string[];
199
+ inactive: BehaviorInactiveBinding[];
200
+ validation: BehaviorValidationResult;
201
+ };
202
+ type ChainBehavior<TContext, TPatch = unknown> = {
203
+ runner: BehaviorRunner<TContext, TPatch>;
204
+ start(): BehaviorStartResult;
205
+ stop(options?: {
206
+ force?: boolean;
207
+ }): void;
208
+ };
209
+ type BehaviorRunResult<TContext, TPatch = unknown> = {
210
+ status: 'success' | 'stopped' | 'failed' | 'skipped';
211
+ context: TContext;
212
+ data: Record<string, unknown>;
213
+ patches: TPatch[];
214
+ events: BehaviorEvent[];
215
+ error?: BehaviorError;
216
+ trace?: BehaviorTraceEntry[];
217
+ steps: number;
218
+ };
219
+ type BehaviorRuntime = {
220
+ get(path: string): unknown;
221
+ getData(path: string): unknown;
222
+ setData(path: string, value: unknown): void;
223
+ resolve(value: unknown): unknown;
224
+ signal: AbortSignal;
225
+ emit(event: BehaviorEvent): void;
226
+ patch(patch: unknown): void;
227
+ stop(reason?: string): BehaviorActionStop<unknown>;
228
+ fail(reason?: string, data?: Record<string, unknown>): BehaviorActionFail;
229
+ };
230
+ type BehaviorRunnerOptions<TContext, TPatch> = {
231
+ maxSteps?: number;
232
+ maxDepth?: number;
233
+ timeoutMs?: number;
234
+ trace?: boolean | BehaviorTraceSink;
235
+ onError?: BehaviorErrorReporter<TContext, TPatch>;
236
+ mergeData?: (current: Record<string, unknown>, next: Record<string, unknown>) => Record<string, unknown>;
237
+ };
238
+ type BehaviorRunOptions = {
239
+ signal?: AbortSignal;
240
+ };
241
+ type BehaviorTraceSink = {
242
+ push(entry: BehaviorTraceEntry): void;
243
+ entries?(): BehaviorTraceEntry[];
244
+ };
245
+ type BehaviorTraceEntry = {
246
+ step: number;
247
+ depth: number;
248
+ strategy: string;
249
+ fn: string;
250
+ mode: BehaviorMode | undefined;
251
+ status: 'matched' | 'skipped' | 'success' | 'stopped' | 'failed';
252
+ input: BehaviorInput;
253
+ props: BehaviorProps;
254
+ dataBefore: Record<string, unknown>;
255
+ dataAfter: Record<string, unknown>;
256
+ durationMs: number;
257
+ reason?: string;
258
+ };
259
+ type BehaviorError = {
260
+ code: string;
261
+ message: string;
262
+ strategy?: string;
263
+ fn?: string;
264
+ stage?: BehaviorErrorStage;
265
+ cause?: unknown;
266
+ };
267
+ type BehaviorErrorStage = {
268
+ phase: 'entrypoint' | 'condition' | 'action' | 'catch' | 'limit';
269
+ entrypoint?: string | undefined;
270
+ strategy?: string | undefined;
271
+ fn?: string | undefined;
272
+ mode?: BehaviorMode | undefined;
273
+ step?: number | undefined;
274
+ depth?: number | undefined;
275
+ };
276
+ type BehaviorErrorEvent<TContext, TPatch = unknown> = {
277
+ error: BehaviorError;
278
+ context: TContext;
279
+ input: BehaviorInput;
280
+ data: Record<string, unknown>;
281
+ patches: TPatch[];
282
+ events: BehaviorEvent[];
283
+ trace?: BehaviorTraceEntry[];
284
+ };
285
+ type BehaviorErrorReporter<TContext, TPatch = unknown> = (event: BehaviorErrorEvent<TContext, TPatch>) => void;
286
+ type BehaviorErrorReporterHandlers<TContext, TPatch = unknown> = {
287
+ report: BehaviorErrorReporter<TContext, TPatch>;
288
+ };
289
+ type BehaviorValidationResult = {
290
+ ok: boolean;
291
+ errors: BehaviorValidationIssue[];
292
+ warnings: BehaviorValidationIssue[];
293
+ };
294
+ type BehaviorValidationIssue = {
295
+ code: string;
296
+ message: string;
297
+ path?: string;
298
+ strategy?: string;
299
+ };
300
+ type BehaviorConditionFn<TContext> = (context: {
301
+ context: TContext;
302
+ input: BehaviorInput;
303
+ data: Record<string, unknown>;
304
+ runtime: Pick<BehaviorRuntime, 'resolve' | 'get' | 'getData'>;
305
+ }, ...conditionArgs: unknown[]) => boolean;
306
+ type BehaviorRunner<TContext, TPatch = unknown> = {
307
+ registerAction(name: string, action: BehaviorAction<TContext, TPatch>): void;
308
+ registerActions(actions: Record<string, BehaviorAction<TContext, TPatch>>): void;
309
+ registerCondition(name: string, condition: BehaviorConditionFn<TContext>): void;
310
+ registerConditions(conditions: Record<string, BehaviorConditionFn<TContext>>): void;
311
+ loadConfig(config: BehaviorConfig): BehaviorValidationResult;
312
+ validateConfig(config?: BehaviorConfig): BehaviorValidationResult;
313
+ run(entrypoint: string, context: TContext, input?: BehaviorInput, options?: BehaviorRunOptions): Promise<BehaviorRunResult<TContext, TPatch>>;
314
+ runSync(entrypoint: string, context: TContext, input?: BehaviorInput, options?: BehaviorRunOptions): BehaviorRunResult<TContext, TPatch>;
315
+ };
316
+
317
+ declare const defineBehaviorConfig: <TConfig extends BehaviorConfig>(config: TConfig) => TConfig;
318
+
319
+ declare const createBehaviorRunner: <TContext, TPatch = unknown>(options?: BehaviorRunnerOptions<TContext, TPatch>) => BehaviorRunner<TContext, TPatch>;
320
+
321
+ declare const createMemoryTraceSink: () => BehaviorTraceSink;
322
+
323
+ declare const defineErrorReporter: <TContext, TPatch = unknown>(handlers: BehaviorErrorReporterHandlers<TContext, TPatch> | BehaviorErrorReporter<TContext, TPatch>) => BehaviorErrorReporter<TContext, TPatch>;
324
+
325
+ declare const createPubSubBehavior: <TEvents extends object = BehaviorEventMap>(options?: BehaviorBusOptions<TEvents>) => BehaviorBus<TEvents>;
326
+ declare const PubSubBehavior: BehaviorBus<BehaviorEventMap>;
327
+
328
+ declare const createChainBehavior: <TContext, TPatch = unknown, TEvents extends object = BehaviorBindingEventMap>(definition: ChainBehaviorDefinition<TContext, TPatch, TEvents>, options: ChainBehaviorOptions<TContext, TPatch, TEvents>) => ChainBehavior<TContext, TPatch>;
329
+
330
+ declare const createBehaviorWs: <TEvents extends object = BehaviorEventMap>(options: BehaviorWsOptions<TEvents>) => BehaviorWs;
331
+
332
+ declare const catchError: <T>(callback: () => T | PromiseLike<T>) => Promise<T>;
333
+
334
+ export { type BehaviorAction, type BehaviorActionArgs, type BehaviorActionFail, type BehaviorActionResult, type BehaviorActionSkip, type BehaviorActionStop, type BehaviorActionSuccess, type BehaviorBindingEventMap, type BehaviorBus, type BehaviorBusBinding, type BehaviorBusBindingKey, type BehaviorBusBindings, type BehaviorBusEmitOptions, type BehaviorBusErrorEvent, type BehaviorBusEvent, type BehaviorBusOptions, type BehaviorConcurrencyMode, type BehaviorConcurrencyOptions, type BehaviorConditionExpression, type BehaviorConditionFn, type BehaviorConfig, type BehaviorDomBinding, type BehaviorDomBindingKey, type BehaviorDomBindings, type BehaviorDomForm, type BehaviorDomInput, type BehaviorError, type BehaviorErrorEvent, type BehaviorErrorReporter, type BehaviorErrorReporterHandlers, type BehaviorErrorStage, type BehaviorEvent, type BehaviorEventHandler, type BehaviorEventMap, type BehaviorEventName, type BehaviorInactiveBinding, type BehaviorInput, type BehaviorMode, type BehaviorNext, type BehaviorProps, type BehaviorQueueOverflow, type BehaviorRunOptions, type BehaviorRunResult, type BehaviorRunner, type BehaviorRunnerErrorEvent, type BehaviorRunnerOptions, type BehaviorRuntime, type BehaviorStartResult, type BehaviorStrategy, type BehaviorTraceEntry, type BehaviorTraceSink, type BehaviorValidationIssue, type BehaviorValidationResult, type BehaviorWs, type BehaviorWsOptions, type BehaviorWsRetryOptions, type BehaviorWsSocket, type BehaviorWsStatus, type ChainBehavior, type ChainBehaviorDefinition, type ChainBehaviorOptions, PubSubBehavior, catchError, createBehaviorRunner, createBehaviorWs, createChainBehavior, createMemoryTraceSink, createPubSubBehavior, defineBehaviorConfig, defineErrorReporter };
@@ -0,0 +1,334 @@
1
+ type BehaviorInput = Record<string, unknown>;
2
+ type BehaviorProps = Record<string, unknown>;
3
+ type BehaviorConfig = {
4
+ version?: 1;
5
+ strategies: Record<string, BehaviorStrategy>;
6
+ entrypoints?: Record<string, string>;
7
+ };
8
+ type BehaviorStrategy = {
9
+ fn: string;
10
+ props?: BehaviorProps;
11
+ when?: BehaviorConditionExpression;
12
+ then?: BehaviorNext[];
13
+ catch?: BehaviorNext[];
14
+ mode?: BehaviorMode;
15
+ terminal?: boolean;
16
+ tags?: string[];
17
+ description?: string;
18
+ };
19
+ type BehaviorMode = 'sequence' | 'selector' | 'parallel';
20
+ type BehaviorNext = string | {
21
+ id?: string;
22
+ strategy: string;
23
+ props?: BehaviorProps;
24
+ when?: BehaviorConditionExpression;
25
+ };
26
+ type BehaviorConditionExpression = boolean | [operator: string, ...args: unknown[]];
27
+ type BehaviorAction<TContext, TPatch = unknown> = (args: BehaviorActionArgs<TContext>) => BehaviorActionResult<TContext, TPatch> | Promise<BehaviorActionResult<TContext, TPatch>>;
28
+ type BehaviorActionArgs<TContext> = {
29
+ context: TContext;
30
+ props: BehaviorProps;
31
+ input: BehaviorInput;
32
+ signal: AbortSignal;
33
+ runtime: BehaviorRuntime;
34
+ };
35
+ type BehaviorActionResult<TContext, TPatch = unknown> = void | false | BehaviorActionSuccess<TContext, TPatch> | BehaviorActionSkip | BehaviorActionStop<TPatch> | BehaviorActionFail;
36
+ type BehaviorActionSuccess<TContext, TPatch> = {
37
+ type?: 'success';
38
+ context?: TContext;
39
+ data?: Record<string, unknown>;
40
+ patch?: TPatch | TPatch[];
41
+ events?: BehaviorEvent[];
42
+ continue?: boolean;
43
+ };
44
+ type BehaviorActionSkip = {
45
+ type: 'skip';
46
+ reason?: string;
47
+ data?: Record<string, unknown>;
48
+ };
49
+ type BehaviorActionStop<TPatch> = {
50
+ type: 'stop';
51
+ reason?: string;
52
+ patch?: TPatch | TPatch[];
53
+ events?: BehaviorEvent[];
54
+ };
55
+ type BehaviorActionFail = {
56
+ type: 'fail';
57
+ reason?: string;
58
+ error?: unknown;
59
+ data?: Record<string, unknown>;
60
+ };
61
+ type BehaviorEvent = {
62
+ type: string;
63
+ payload?: unknown;
64
+ };
65
+ type BehaviorEventMap = Record<string, unknown>;
66
+ type BehaviorEventName<TEvents extends object> = Extract<keyof TEvents, string>;
67
+ type BehaviorBusEvent<TPayload = unknown> = {
68
+ id: string;
69
+ topic: string;
70
+ occurredAt: number;
71
+ origin?: string;
72
+ parsed: TPayload;
73
+ serialized: string;
74
+ };
75
+ type BehaviorEventHandler<TEvents extends object, TEvent extends BehaviorEventName<TEvents>> = (event: BehaviorBusEvent<TEvents[TEvent]>) => void;
76
+ type BehaviorBusErrorEvent<TEvents extends object> = {
77
+ type: 'serialization';
78
+ topic: BehaviorEventName<TEvents>;
79
+ payload: TEvents[BehaviorEventName<TEvents>];
80
+ origin?: string;
81
+ error: unknown;
82
+ } | {
83
+ type: 'subscriber';
84
+ event: BehaviorBusEvent<TEvents[BehaviorEventName<TEvents>]>;
85
+ error: unknown;
86
+ };
87
+ type BehaviorBusEmitOptions = {
88
+ origin?: string;
89
+ };
90
+ type BehaviorBusOptions<TEvents extends object> = {
91
+ onError?: (event: BehaviorBusErrorEvent<TEvents>) => void;
92
+ };
93
+ type BehaviorBus<TEvents extends object = BehaviorEventMap> = {
94
+ on<TEvent extends BehaviorEventName<TEvents>>(event: TEvent, handler: BehaviorEventHandler<TEvents, TEvent>): () => void;
95
+ off<TEvent extends BehaviorEventName<TEvents>>(event: TEvent, handler?: BehaviorEventHandler<TEvents, TEvent>): void;
96
+ emit<TEvent extends BehaviorEventName<TEvents>>(topic: TEvent, payload: TEvents[TEvent], options?: BehaviorBusEmitOptions): BehaviorBusEvent<TEvents[TEvent]>;
97
+ dispatch(event: unknown): BehaviorBusEvent | undefined;
98
+ };
99
+ type BehaviorWsSocket = {
100
+ readyState: number;
101
+ send(data: string): void;
102
+ close(): void;
103
+ addEventListener(type: 'open' | 'close' | 'error' | 'message', listener: EventListener): void;
104
+ removeEventListener(type: 'open' | 'close' | 'error' | 'message', listener: EventListener): void;
105
+ };
106
+ type BehaviorWsRetryOptions = {
107
+ initialDelay?: number;
108
+ maxDelay?: number;
109
+ multiplier?: number;
110
+ jitter?: boolean;
111
+ };
112
+ type BehaviorWsOptions<TEvents extends object = BehaviorEventMap> = {
113
+ bus: BehaviorBus<TEvents>;
114
+ createSocket: () => BehaviorWsSocket;
115
+ inboundTopics?: BehaviorEventName<TEvents>[];
116
+ outboundTopics?: BehaviorEventName<TEvents>[];
117
+ origin?: string;
118
+ retry?: BehaviorWsRetryOptions;
119
+ };
120
+ type BehaviorWsStatus = 'idle' | 'connecting' | 'connected' | 'retrying' | 'stopped';
121
+ type BehaviorWs = {
122
+ start(): void;
123
+ stop(): void;
124
+ reconnect(): void;
125
+ status(): BehaviorWsStatus;
126
+ };
127
+ type BehaviorBindingEventMap = Record<string, BehaviorInput>;
128
+ type BehaviorBusBindingKey<TEvents extends object> = {
129
+ [TEvent in BehaviorEventName<TEvents>]: TEvents[TEvent] extends BehaviorInput ? `[bus] ${TEvent}` : never;
130
+ }[BehaviorEventName<TEvents>];
131
+ type BehaviorConcurrencyMode = 'parallel' | 'latest' | 'queue' | 'drop';
132
+ type BehaviorQueueOverflow = 'drop-oldest' | 'drop-newest';
133
+ type BehaviorConcurrencyOptions<TPayload = BehaviorInput> = {
134
+ mode?: BehaviorConcurrencyMode;
135
+ key?: (payload: TPayload) => string;
136
+ maxQueueSize?: number;
137
+ overflow?: BehaviorQueueOverflow;
138
+ };
139
+ type BehaviorBusBinding<TPayload = BehaviorInput> = {
140
+ entrypoint: string;
141
+ options?: {
142
+ concurrency?: BehaviorConcurrencyOptions<TPayload>;
143
+ };
144
+ };
145
+ type BehaviorBusBindings<TEvents extends object> = {
146
+ [TBinding in BehaviorBusBindingKey<TEvents>]?: BehaviorBusBinding<TEvents[Extract<TBinding extends `[bus] ${infer TEvent}` ? TEvent : never, BehaviorEventName<TEvents>>]>;
147
+ };
148
+ type BehaviorDomBindingKey = `[dom] ${string}:${string}`;
149
+ type BehaviorDomForm = Record<string, FormDataEntryValue | FormDataEntryValue[]>;
150
+ type BehaviorDomInput = BehaviorInput & {
151
+ type: string;
152
+ value?: string;
153
+ dataset: Record<string, string>;
154
+ form?: BehaviorDomForm;
155
+ };
156
+ type BehaviorDomBinding = {
157
+ entrypoint: string;
158
+ options?: {
159
+ preventDefault?: boolean;
160
+ stopPropagation?: boolean;
161
+ capture?: boolean;
162
+ once?: boolean;
163
+ input?: (scope: {
164
+ event: Event;
165
+ element: Element;
166
+ defaultInput: BehaviorDomInput;
167
+ }) => BehaviorInput;
168
+ concurrency?: BehaviorConcurrencyOptions<BehaviorDomInput>;
169
+ };
170
+ };
171
+ type BehaviorDomBindings = Partial<Record<BehaviorDomBindingKey, BehaviorDomBinding>>;
172
+ type ChainBehaviorDefinition<TContext, TPatch = unknown, TEvents extends object = BehaviorBindingEventMap> = {
173
+ config: BehaviorConfig;
174
+ actions?: Record<string, BehaviorAction<TContext, TPatch>>;
175
+ conditions?: Record<string, BehaviorConditionFn<TContext>>;
176
+ events?: BehaviorBusBindings<TEvents> & BehaviorDomBindings;
177
+ };
178
+ type ChainBehaviorOptions<TContext, TPatch = unknown, TEvents extends object = BehaviorBindingEventMap> = BehaviorRunnerOptions<TContext, TPatch> & {
179
+ context: TContext | (() => TContext);
180
+ bus?: BehaviorBus<TEvents>;
181
+ root?: Document | Element;
182
+ concurrency?: BehaviorConcurrencyOptions;
183
+ onRunnerError?: (event: BehaviorRunnerErrorEvent<TContext, TPatch>) => void;
184
+ };
185
+ type BehaviorRunnerErrorEvent<TContext, TPatch = unknown> = {
186
+ error: BehaviorError;
187
+ result: BehaviorRunResult<TContext, TPatch>;
188
+ binding: string;
189
+ entrypoint: string;
190
+ runId: string;
191
+ key?: string;
192
+ };
193
+ type BehaviorInactiveBinding = {
194
+ binding: string;
195
+ reason: 'unsupported-source' | 'dom-unavailable';
196
+ };
197
+ type BehaviorStartResult = {
198
+ active: string[];
199
+ inactive: BehaviorInactiveBinding[];
200
+ validation: BehaviorValidationResult;
201
+ };
202
+ type ChainBehavior<TContext, TPatch = unknown> = {
203
+ runner: BehaviorRunner<TContext, TPatch>;
204
+ start(): BehaviorStartResult;
205
+ stop(options?: {
206
+ force?: boolean;
207
+ }): void;
208
+ };
209
+ type BehaviorRunResult<TContext, TPatch = unknown> = {
210
+ status: 'success' | 'stopped' | 'failed' | 'skipped';
211
+ context: TContext;
212
+ data: Record<string, unknown>;
213
+ patches: TPatch[];
214
+ events: BehaviorEvent[];
215
+ error?: BehaviorError;
216
+ trace?: BehaviorTraceEntry[];
217
+ steps: number;
218
+ };
219
+ type BehaviorRuntime = {
220
+ get(path: string): unknown;
221
+ getData(path: string): unknown;
222
+ setData(path: string, value: unknown): void;
223
+ resolve(value: unknown): unknown;
224
+ signal: AbortSignal;
225
+ emit(event: BehaviorEvent): void;
226
+ patch(patch: unknown): void;
227
+ stop(reason?: string): BehaviorActionStop<unknown>;
228
+ fail(reason?: string, data?: Record<string, unknown>): BehaviorActionFail;
229
+ };
230
+ type BehaviorRunnerOptions<TContext, TPatch> = {
231
+ maxSteps?: number;
232
+ maxDepth?: number;
233
+ timeoutMs?: number;
234
+ trace?: boolean | BehaviorTraceSink;
235
+ onError?: BehaviorErrorReporter<TContext, TPatch>;
236
+ mergeData?: (current: Record<string, unknown>, next: Record<string, unknown>) => Record<string, unknown>;
237
+ };
238
+ type BehaviorRunOptions = {
239
+ signal?: AbortSignal;
240
+ };
241
+ type BehaviorTraceSink = {
242
+ push(entry: BehaviorTraceEntry): void;
243
+ entries?(): BehaviorTraceEntry[];
244
+ };
245
+ type BehaviorTraceEntry = {
246
+ step: number;
247
+ depth: number;
248
+ strategy: string;
249
+ fn: string;
250
+ mode: BehaviorMode | undefined;
251
+ status: 'matched' | 'skipped' | 'success' | 'stopped' | 'failed';
252
+ input: BehaviorInput;
253
+ props: BehaviorProps;
254
+ dataBefore: Record<string, unknown>;
255
+ dataAfter: Record<string, unknown>;
256
+ durationMs: number;
257
+ reason?: string;
258
+ };
259
+ type BehaviorError = {
260
+ code: string;
261
+ message: string;
262
+ strategy?: string;
263
+ fn?: string;
264
+ stage?: BehaviorErrorStage;
265
+ cause?: unknown;
266
+ };
267
+ type BehaviorErrorStage = {
268
+ phase: 'entrypoint' | 'condition' | 'action' | 'catch' | 'limit';
269
+ entrypoint?: string | undefined;
270
+ strategy?: string | undefined;
271
+ fn?: string | undefined;
272
+ mode?: BehaviorMode | undefined;
273
+ step?: number | undefined;
274
+ depth?: number | undefined;
275
+ };
276
+ type BehaviorErrorEvent<TContext, TPatch = unknown> = {
277
+ error: BehaviorError;
278
+ context: TContext;
279
+ input: BehaviorInput;
280
+ data: Record<string, unknown>;
281
+ patches: TPatch[];
282
+ events: BehaviorEvent[];
283
+ trace?: BehaviorTraceEntry[];
284
+ };
285
+ type BehaviorErrorReporter<TContext, TPatch = unknown> = (event: BehaviorErrorEvent<TContext, TPatch>) => void;
286
+ type BehaviorErrorReporterHandlers<TContext, TPatch = unknown> = {
287
+ report: BehaviorErrorReporter<TContext, TPatch>;
288
+ };
289
+ type BehaviorValidationResult = {
290
+ ok: boolean;
291
+ errors: BehaviorValidationIssue[];
292
+ warnings: BehaviorValidationIssue[];
293
+ };
294
+ type BehaviorValidationIssue = {
295
+ code: string;
296
+ message: string;
297
+ path?: string;
298
+ strategy?: string;
299
+ };
300
+ type BehaviorConditionFn<TContext> = (context: {
301
+ context: TContext;
302
+ input: BehaviorInput;
303
+ data: Record<string, unknown>;
304
+ runtime: Pick<BehaviorRuntime, 'resolve' | 'get' | 'getData'>;
305
+ }, ...conditionArgs: unknown[]) => boolean;
306
+ type BehaviorRunner<TContext, TPatch = unknown> = {
307
+ registerAction(name: string, action: BehaviorAction<TContext, TPatch>): void;
308
+ registerActions(actions: Record<string, BehaviorAction<TContext, TPatch>>): void;
309
+ registerCondition(name: string, condition: BehaviorConditionFn<TContext>): void;
310
+ registerConditions(conditions: Record<string, BehaviorConditionFn<TContext>>): void;
311
+ loadConfig(config: BehaviorConfig): BehaviorValidationResult;
312
+ validateConfig(config?: BehaviorConfig): BehaviorValidationResult;
313
+ run(entrypoint: string, context: TContext, input?: BehaviorInput, options?: BehaviorRunOptions): Promise<BehaviorRunResult<TContext, TPatch>>;
314
+ runSync(entrypoint: string, context: TContext, input?: BehaviorInput, options?: BehaviorRunOptions): BehaviorRunResult<TContext, TPatch>;
315
+ };
316
+
317
+ declare const defineBehaviorConfig: <TConfig extends BehaviorConfig>(config: TConfig) => TConfig;
318
+
319
+ declare const createBehaviorRunner: <TContext, TPatch = unknown>(options?: BehaviorRunnerOptions<TContext, TPatch>) => BehaviorRunner<TContext, TPatch>;
320
+
321
+ declare const createMemoryTraceSink: () => BehaviorTraceSink;
322
+
323
+ declare const defineErrorReporter: <TContext, TPatch = unknown>(handlers: BehaviorErrorReporterHandlers<TContext, TPatch> | BehaviorErrorReporter<TContext, TPatch>) => BehaviorErrorReporter<TContext, TPatch>;
324
+
325
+ declare const createPubSubBehavior: <TEvents extends object = BehaviorEventMap>(options?: BehaviorBusOptions<TEvents>) => BehaviorBus<TEvents>;
326
+ declare const PubSubBehavior: BehaviorBus<BehaviorEventMap>;
327
+
328
+ declare const createChainBehavior: <TContext, TPatch = unknown, TEvents extends object = BehaviorBindingEventMap>(definition: ChainBehaviorDefinition<TContext, TPatch, TEvents>, options: ChainBehaviorOptions<TContext, TPatch, TEvents>) => ChainBehavior<TContext, TPatch>;
329
+
330
+ declare const createBehaviorWs: <TEvents extends object = BehaviorEventMap>(options: BehaviorWsOptions<TEvents>) => BehaviorWs;
331
+
332
+ declare const catchError: <T>(callback: () => T | PromiseLike<T>) => Promise<T>;
333
+
334
+ export { type BehaviorAction, type BehaviorActionArgs, type BehaviorActionFail, type BehaviorActionResult, type BehaviorActionSkip, type BehaviorActionStop, type BehaviorActionSuccess, type BehaviorBindingEventMap, type BehaviorBus, type BehaviorBusBinding, type BehaviorBusBindingKey, type BehaviorBusBindings, type BehaviorBusEmitOptions, type BehaviorBusErrorEvent, type BehaviorBusEvent, type BehaviorBusOptions, type BehaviorConcurrencyMode, type BehaviorConcurrencyOptions, type BehaviorConditionExpression, type BehaviorConditionFn, type BehaviorConfig, type BehaviorDomBinding, type BehaviorDomBindingKey, type BehaviorDomBindings, type BehaviorDomForm, type BehaviorDomInput, type BehaviorError, type BehaviorErrorEvent, type BehaviorErrorReporter, type BehaviorErrorReporterHandlers, type BehaviorErrorStage, type BehaviorEvent, type BehaviorEventHandler, type BehaviorEventMap, type BehaviorEventName, type BehaviorInactiveBinding, type BehaviorInput, type BehaviorMode, type BehaviorNext, type BehaviorProps, type BehaviorQueueOverflow, type BehaviorRunOptions, type BehaviorRunResult, type BehaviorRunner, type BehaviorRunnerErrorEvent, type BehaviorRunnerOptions, type BehaviorRuntime, type BehaviorStartResult, type BehaviorStrategy, type BehaviorTraceEntry, type BehaviorTraceSink, type BehaviorValidationIssue, type BehaviorValidationResult, type BehaviorWs, type BehaviorWsOptions, type BehaviorWsRetryOptions, type BehaviorWsSocket, type BehaviorWsStatus, type ChainBehavior, type ChainBehaviorDefinition, type ChainBehaviorOptions, PubSubBehavior, catchError, createBehaviorRunner, createBehaviorWs, createChainBehavior, createMemoryTraceSink, createPubSubBehavior, defineBehaviorConfig, defineErrorReporter };