@agentxjs/core 1.9.5-dev → 1.9.7-dev
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/Processor-DT0N1qI6.d.ts +64 -0
- package/dist/agent/engine/internal/index.d.ts +223 -0
- package/dist/agent/engine/internal/index.js +24 -0
- package/dist/agent/engine/internal/index.js.map +1 -0
- package/dist/agent/engine/mealy/index.d.ts +157 -0
- package/dist/agent/engine/mealy/index.js +26 -0
- package/dist/agent/engine/mealy/index.js.map +1 -0
- package/dist/agent/index.d.ts +244 -0
- package/dist/agent/index.js +66 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/agent/types/index.d.ts +322 -0
- package/dist/agent/types/index.js +12 -0
- package/dist/agent/types/index.js.map +1 -0
- package/dist/base-m40r3Qgu.d.ts +157 -0
- package/dist/bus-uF1DM2ox.d.ts +906 -0
- package/dist/chunk-7D4SUZUM.js +38 -0
- package/dist/chunk-7D4SUZUM.js.map +1 -0
- package/dist/chunk-7ZDX3O6I.js +173 -0
- package/dist/chunk-7ZDX3O6I.js.map +1 -0
- package/dist/chunk-E5FPOAPO.js +123 -0
- package/dist/chunk-E5FPOAPO.js.map +1 -0
- package/dist/chunk-EKHT54KN.js +272 -0
- package/dist/chunk-EKHT54KN.js.map +1 -0
- package/dist/chunk-I7GYR3MN.js +502 -0
- package/dist/chunk-I7GYR3MN.js.map +1 -0
- package/dist/chunk-K6WXQ2RW.js +38 -0
- package/dist/chunk-K6WXQ2RW.js.map +1 -0
- package/dist/chunk-RL3JRNXM.js +3 -0
- package/dist/chunk-RL3JRNXM.js.map +1 -0
- package/dist/chunk-TBU7FFZT.js +543 -0
- package/dist/chunk-TBU7FFZT.js.map +1 -0
- package/dist/combinators-nEa5dD0T.d.ts +271 -0
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.js +2 -0
- package/dist/common/index.js.map +1 -0
- package/dist/common/logger/index.d.ts +163 -0
- package/dist/common/logger/index.js +184 -0
- package/dist/common/logger/index.js.map +1 -0
- package/dist/container/index.d.ts +110 -0
- package/dist/container/index.js +127 -0
- package/dist/container/index.js.map +1 -0
- package/dist/driver/index.d.ts +266 -0
- package/dist/driver/index.js +1 -0
- package/dist/driver/index.js.map +1 -0
- package/dist/event/index.d.ts +55 -0
- package/dist/event/index.js +60 -0
- package/dist/event/index.js.map +1 -0
- package/dist/event/types/index.d.ts +1149 -0
- package/dist/event/types/index.js +56 -0
- package/dist/event/types/index.js.map +1 -0
- package/dist/event-CDuTzs__.d.ts +296 -0
- package/dist/image/index.d.ts +112 -0
- package/dist/image/index.js +151 -0
- package/dist/image/index.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +67 -0
- package/dist/index.js.map +1 -0
- package/dist/message-BMrMm1pq.d.ts +305 -0
- package/dist/mq/index.d.ts +165 -0
- package/dist/mq/index.js +37 -0
- package/dist/mq/index.js.map +1 -0
- package/dist/network/index.d.ts +567 -0
- package/dist/network/index.js +435 -0
- package/dist/network/index.js.map +1 -0
- package/dist/persistence/index.d.ts +155 -0
- package/dist/persistence/index.js +1 -0
- package/dist/persistence/index.js.map +1 -0
- package/dist/runtime/index.d.ts +240 -0
- package/dist/runtime/index.js +347 -0
- package/dist/runtime/index.js.map +1 -0
- package/dist/session/index.d.ts +92 -0
- package/dist/session/index.js +56 -0
- package/dist/session/index.js.map +1 -0
- package/dist/workspace/index.d.ts +111 -0
- package/dist/workspace/index.js +1 -0
- package/dist/workspace/index.js.map +1 -0
- package/dist/wrapper-Y3UTVU2E.js +3635 -0
- package/dist/wrapper-Y3UTVU2E.js.map +1 -0
- package/package.json +73 -14
- package/tsconfig.json +0 -10
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// src/agent/engine/mealy/Store.ts
|
|
2
|
+
var MemoryStore = class {
|
|
3
|
+
states = /* @__PURE__ */ new Map();
|
|
4
|
+
get(id) {
|
|
5
|
+
return this.states.get(id);
|
|
6
|
+
}
|
|
7
|
+
set(id, state) {
|
|
8
|
+
this.states.set(id, state);
|
|
9
|
+
}
|
|
10
|
+
delete(id) {
|
|
11
|
+
this.states.delete(id);
|
|
12
|
+
}
|
|
13
|
+
has(id) {
|
|
14
|
+
return this.states.has(id);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Clear all stored states
|
|
18
|
+
*/
|
|
19
|
+
clear() {
|
|
20
|
+
this.states.clear();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get the number of stored states
|
|
24
|
+
*/
|
|
25
|
+
get size() {
|
|
26
|
+
return this.states.size;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Get all stored IDs
|
|
30
|
+
*/
|
|
31
|
+
keys() {
|
|
32
|
+
return this.states.keys();
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// src/agent/engine/mealy/Mealy.ts
|
|
37
|
+
import { createLogger } from "commonxjs/logger";
|
|
38
|
+
var logger = createLogger("engine/Mealy");
|
|
39
|
+
var Mealy = class {
|
|
40
|
+
processor;
|
|
41
|
+
store;
|
|
42
|
+
initialState;
|
|
43
|
+
sinks;
|
|
44
|
+
recursive;
|
|
45
|
+
maxDepth;
|
|
46
|
+
constructor(config) {
|
|
47
|
+
this.processor = config.processor;
|
|
48
|
+
this.store = config.store;
|
|
49
|
+
this.initialState = config.initialState;
|
|
50
|
+
this.sinks = config.sinks ?? [];
|
|
51
|
+
this.recursive = config.recursive ?? true;
|
|
52
|
+
this.maxDepth = config.maxDepth ?? 100;
|
|
53
|
+
logger.debug("Mealy instance created", {
|
|
54
|
+
sinkCount: this.sinks.length,
|
|
55
|
+
recursive: this.recursive,
|
|
56
|
+
maxDepth: this.maxDepth
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Process an input through the Mealy Machine
|
|
61
|
+
*
|
|
62
|
+
* @param id - Unique identifier (e.g., agentId)
|
|
63
|
+
* @param input - The input to process
|
|
64
|
+
* @returns Result containing new state and all outputs
|
|
65
|
+
*/
|
|
66
|
+
process(id, input) {
|
|
67
|
+
return this.processInternal(id, input, 0);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Internal process with depth tracking
|
|
71
|
+
*/
|
|
72
|
+
processInternal(id, input, depth) {
|
|
73
|
+
if (depth >= this.maxDepth) {
|
|
74
|
+
logger.warn("Max recursion depth reached", {
|
|
75
|
+
id,
|
|
76
|
+
maxDepth: this.maxDepth,
|
|
77
|
+
depth
|
|
78
|
+
});
|
|
79
|
+
return {
|
|
80
|
+
state: this.store.get(id) ?? this.initialState,
|
|
81
|
+
outputs: [],
|
|
82
|
+
processCount: 0
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
let state = this.store.get(id);
|
|
86
|
+
if (state === void 0) {
|
|
87
|
+
state = this.initialState;
|
|
88
|
+
}
|
|
89
|
+
const [newState, outputs] = this.processor(state, input);
|
|
90
|
+
this.store.set(id, newState);
|
|
91
|
+
const allOutputs = [...outputs];
|
|
92
|
+
let processCount = 1;
|
|
93
|
+
if (outputs.length > 0) {
|
|
94
|
+
this.sendToSinks(id, outputs);
|
|
95
|
+
}
|
|
96
|
+
if (this.recursive) {
|
|
97
|
+
for (const output of outputs) {
|
|
98
|
+
const result = this.processInternal(id, output, depth + 1);
|
|
99
|
+
allOutputs.push(...result.outputs);
|
|
100
|
+
processCount += result.processCount;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
state: newState,
|
|
105
|
+
outputs: allOutputs,
|
|
106
|
+
processCount
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Send outputs to all sinks
|
|
111
|
+
*/
|
|
112
|
+
sendToSinks(id, outputs) {
|
|
113
|
+
for (const sink of this.sinks) {
|
|
114
|
+
if (typeof sink === "function") {
|
|
115
|
+
try {
|
|
116
|
+
const result = sink(id, outputs);
|
|
117
|
+
if (result instanceof Promise) {
|
|
118
|
+
result.catch((error) => {
|
|
119
|
+
logger.error("Sink error (async)", { id, error });
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
} catch (error) {
|
|
123
|
+
logger.error("Sink error (sync)", { id, error });
|
|
124
|
+
}
|
|
125
|
+
} else {
|
|
126
|
+
const filteredOutputs = sink.filter ? outputs.filter(sink.filter) : outputs;
|
|
127
|
+
if (filteredOutputs.length === 0) {
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
const result = sink.sink(id, filteredOutputs);
|
|
132
|
+
if (result instanceof Promise) {
|
|
133
|
+
result.catch((error) => {
|
|
134
|
+
logger.error("Named sink error (async)", {
|
|
135
|
+
id,
|
|
136
|
+
sinkName: sink.name,
|
|
137
|
+
error
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
} catch (error) {
|
|
142
|
+
logger.error("Named sink error (sync)", {
|
|
143
|
+
id,
|
|
144
|
+
sinkName: sink.name,
|
|
145
|
+
error
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get current state for an ID (without processing)
|
|
153
|
+
*/
|
|
154
|
+
getState(id) {
|
|
155
|
+
return this.store.get(id);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Check if state exists for an ID
|
|
159
|
+
*/
|
|
160
|
+
hasState(id) {
|
|
161
|
+
return this.store.has(id);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Delete state for an ID (cleanup)
|
|
165
|
+
*/
|
|
166
|
+
cleanup(id) {
|
|
167
|
+
logger.debug("Cleaning up state", { id });
|
|
168
|
+
this.store.delete(id);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Add a sink at runtime
|
|
172
|
+
*/
|
|
173
|
+
addSink(sink) {
|
|
174
|
+
const sinkName = typeof sink === "function" ? "(anonymous)" : sink.name;
|
|
175
|
+
logger.debug("Adding sink", { sinkName });
|
|
176
|
+
this.sinks.push(sink);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Remove a sink by name (only works for SinkDefinitions)
|
|
180
|
+
*/
|
|
181
|
+
removeSink(name) {
|
|
182
|
+
const index = this.sinks.findIndex((s) => typeof s !== "function" && s.name === name);
|
|
183
|
+
if (index !== -1) {
|
|
184
|
+
this.sinks.splice(index, 1);
|
|
185
|
+
logger.debug("Removed sink", { name });
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
logger.debug("Sink not found for removal", { name });
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
function createMealy(config) {
|
|
193
|
+
return new Mealy(config);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// src/agent/engine/mealy/combinators.ts
|
|
197
|
+
function combineProcessors(processors) {
|
|
198
|
+
return (state, event) => {
|
|
199
|
+
const newState = {};
|
|
200
|
+
const allOutputs = [];
|
|
201
|
+
for (const key in processors) {
|
|
202
|
+
const processor = processors[key];
|
|
203
|
+
const subState = state[key];
|
|
204
|
+
const [newSubState, outputs] = processor(subState, event);
|
|
205
|
+
newState[key] = newSubState;
|
|
206
|
+
allOutputs.push(...outputs);
|
|
207
|
+
}
|
|
208
|
+
return [newState, allOutputs];
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
function combineInitialStates(initialStates) {
|
|
212
|
+
return () => {
|
|
213
|
+
const state = {};
|
|
214
|
+
for (const key in initialStates) {
|
|
215
|
+
state[key] = initialStates[key]();
|
|
216
|
+
}
|
|
217
|
+
return state;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
function chainProcessors(...processors) {
|
|
221
|
+
return (state, event) => {
|
|
222
|
+
let currentState = state;
|
|
223
|
+
const finalOutputs = [];
|
|
224
|
+
for (const processor of processors) {
|
|
225
|
+
const [newState, outputs] = processor(currentState, event);
|
|
226
|
+
currentState = newState;
|
|
227
|
+
finalOutputs.push(...outputs);
|
|
228
|
+
}
|
|
229
|
+
return [currentState, finalOutputs];
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
function filterProcessor(predicate, processor) {
|
|
233
|
+
return (state, event) => {
|
|
234
|
+
if (predicate(event)) {
|
|
235
|
+
return processor(state, event);
|
|
236
|
+
}
|
|
237
|
+
return [state, []];
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
function mapOutput(processor, mapper) {
|
|
241
|
+
return (state, event) => {
|
|
242
|
+
const [newState, outputs] = processor(state, event);
|
|
243
|
+
return [newState, outputs.map(mapper)];
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
function withLogging(processor, name, logger2 = console) {
|
|
247
|
+
return (state, event) => {
|
|
248
|
+
logger2.debug(`[${name}] Input:`, { event, state });
|
|
249
|
+
const [newState, outputs] = processor(state, event);
|
|
250
|
+
logger2.debug(`[${name}] Output:`, { newState, outputs });
|
|
251
|
+
return [newState, outputs];
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function identityProcessor() {
|
|
255
|
+
return (state, _event) => {
|
|
256
|
+
return [state, []];
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export {
|
|
261
|
+
MemoryStore,
|
|
262
|
+
Mealy,
|
|
263
|
+
createMealy,
|
|
264
|
+
combineProcessors,
|
|
265
|
+
combineInitialStates,
|
|
266
|
+
chainProcessors,
|
|
267
|
+
filterProcessor,
|
|
268
|
+
mapOutput,
|
|
269
|
+
withLogging,
|
|
270
|
+
identityProcessor
|
|
271
|
+
};
|
|
272
|
+
//# sourceMappingURL=chunk-EKHT54KN.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/agent/engine/mealy/Store.ts","../src/agent/engine/mealy/Mealy.ts","../src/agent/engine/mealy/combinators.ts"],"sourcesContent":["/**\n * Store - State storage interface for stream processing\n *\n * A Store abstracts state persistence, allowing processors to be stateless\n * while maintaining state externally.\n *\n * @template T - The state type to store\n *\n * @example\n * ```typescript\n * const store = new MemoryStore<AgentState>();\n * store.set('agent_123', { count: 0 });\n * const state = store.get('agent_123');\n * ```\n */\nexport interface Store<T> {\n /**\n * Get state by ID\n * @param id - Unique identifier (e.g., agentId, sessionId)\n * @returns The stored state or undefined if not found\n */\n get(id: string): T | undefined;\n\n /**\n * Set state for an ID\n * @param id - Unique identifier\n * @param state - The state to store\n */\n set(id: string, state: T): void;\n\n /**\n * Delete state for an ID\n * @param id - Unique identifier\n */\n delete(id: string): void;\n\n /**\n * Check if state exists for an ID\n * @param id - Unique identifier\n * @returns True if state exists\n */\n has(id: string): boolean;\n}\n\n/**\n * MemoryStore - In-memory implementation of Store\n *\n * Stores state in a Map. Suitable for development and single-process deployments.\n * For production multi-process scenarios, use RedisStore or PostgresStore.\n *\n * @template T - The state type to store\n *\n * @example\n * ```typescript\n * const store = new MemoryStore<MyState>();\n * store.set('session_1', { count: 0 });\n * ```\n */\nexport class MemoryStore<T> implements Store<T> {\n private states = new Map<string, T>();\n\n get(id: string): T | undefined {\n return this.states.get(id);\n }\n\n set(id: string, state: T): void {\n this.states.set(id, state);\n }\n\n delete(id: string): void {\n this.states.delete(id);\n }\n\n has(id: string): boolean {\n return this.states.has(id);\n }\n\n /**\n * Clear all stored states\n */\n clear(): void {\n this.states.clear();\n }\n\n /**\n * Get the number of stored states\n */\n get size(): number {\n return this.states.size;\n }\n\n /**\n * Get all stored IDs\n */\n keys(): IterableIterator<string> {\n return this.states.keys();\n }\n}\n","/**\n * Mealy - The Mealy Machine runtime\n *\n * A Mealy Machine is a finite-state machine where outputs depend on\n * both the current state AND the input: (state, input) => (state, output)\n *\n * This runtime orchestrates the complete processing pipeline:\n * 1. Sources receive external input (side effects)\n * 2. Processors process inputs (pure Mealy transition functions)\n * 3. Sinks produce output effects (side effects)\n *\n * Architecture:\n * - Inputs enter through Sources (input adapters)\n * - Processors transform inputs (pure functions, state is means)\n * - Sinks produce actions (output adapters)\n *\n * @template TState - The state type (accumulator, means to an end)\n * @template TInput - The input/output type for Processors\n *\n * @example\n * ```typescript\n * const mealy = createMealy({\n * processor: messageProcessor,\n * store: new MemoryStore(),\n * initialState: { text: '' },\n * sinks: [sseSink, logSink],\n * });\n *\n * // Process an input\n * mealy.process('agent_123', input);\n * ```\n */\n\nimport type { Processor } from \"./Processor\";\nimport type { Store } from \"./Store\";\nimport type { Sink, SinkDefinition } from \"./Sink\";\nimport { createLogger } from \"commonxjs/logger\";\n\nconst logger = createLogger(\"engine/Mealy\");\n\n/**\n * MealyConfig - Configuration for creating a Mealy instance\n */\nexport interface MealyConfig<TState, TInput> {\n /**\n * The processor function to execute (pure Mealy transition function)\n */\n processor: Processor<TState, TInput, TInput>;\n\n /**\n * The store for state persistence\n */\n store: Store<TState>;\n\n /**\n * Initial state for new IDs\n */\n initialState: TState;\n\n /**\n * Sinks to receive outputs\n * Can be simple Sink functions or SinkDefinitions with filter/name\n */\n sinks?: (Sink<TInput> | SinkDefinition<TInput>)[];\n\n /**\n * Whether to recursively process outputs\n * If true, outputs are fed back into the processor\n *\n * @default true\n */\n recursive?: boolean;\n\n /**\n * Maximum recursion depth to prevent infinite loops\n *\n * @default 100\n */\n maxDepth?: number;\n}\n\n/**\n * ProcessResult - Result of processing an input\n */\nexport interface ProcessResult<TState, TOutput> {\n /**\n * The new state after processing\n */\n state: TState;\n\n /**\n * All outputs produced (including from recursion)\n */\n outputs: TOutput[];\n\n /**\n * Number of processor invocations (including recursion)\n */\n processCount: number;\n}\n\n/**\n * Mealy - Mealy Machine runtime\n *\n * Implements the Mealy Machine pattern: (state, input) => (state, output)\n * where output depends on both current state and input.\n */\nexport class Mealy<TState, TInput> {\n private readonly processor: Processor<TState, TInput, TInput>;\n private readonly store: Store<TState>;\n private readonly initialState: TState;\n private readonly sinks: (Sink<TInput> | SinkDefinition<TInput>)[];\n private readonly recursive: boolean;\n private readonly maxDepth: number;\n\n constructor(config: MealyConfig<TState, TInput>) {\n this.processor = config.processor;\n this.store = config.store;\n this.initialState = config.initialState;\n this.sinks = config.sinks ?? [];\n this.recursive = config.recursive ?? true;\n this.maxDepth = config.maxDepth ?? 100;\n\n logger.debug(\"Mealy instance created\", {\n sinkCount: this.sinks.length,\n recursive: this.recursive,\n maxDepth: this.maxDepth,\n });\n }\n\n /**\n * Process an input through the Mealy Machine\n *\n * @param id - Unique identifier (e.g., agentId)\n * @param input - The input to process\n * @returns Result containing new state and all outputs\n */\n process(id: string, input: TInput): ProcessResult<TState, TInput> {\n return this.processInternal(id, input, 0);\n }\n\n /**\n * Internal process with depth tracking\n */\n private processInternal(id: string, input: TInput, depth: number): ProcessResult<TState, TInput> {\n // Guard against infinite recursion\n if (depth >= this.maxDepth) {\n logger.warn(\"Max recursion depth reached\", {\n id,\n maxDepth: this.maxDepth,\n depth,\n });\n return {\n state: this.store.get(id) ?? this.initialState,\n outputs: [],\n processCount: 0,\n };\n }\n\n // 1. Get current state (or initialize)\n let state = this.store.get(id);\n if (state === undefined) {\n state = this.initialState;\n }\n\n // 2. Execute pure processor function (Mealy transition)\n const [newState, outputs] = this.processor(state, input);\n\n // 3. Save new state to store\n this.store.set(id, newState);\n\n // 4. Collect all outputs\n const allOutputs: TInput[] = [...outputs];\n let processCount = 1;\n\n // 5. Send outputs to sinks\n if (outputs.length > 0) {\n this.sendToSinks(id, outputs);\n }\n\n // 6. Optionally recurse on outputs\n if (this.recursive) {\n for (const output of outputs) {\n const result = this.processInternal(id, output, depth + 1);\n allOutputs.push(...result.outputs);\n processCount += result.processCount;\n }\n }\n\n return {\n state: newState,\n outputs: allOutputs,\n processCount,\n };\n }\n\n /**\n * Send outputs to all sinks\n */\n private sendToSinks(id: string, outputs: TInput[]): void {\n for (const sink of this.sinks) {\n // Check if sink is a function or SinkDefinition\n if (typeof sink === \"function\") {\n // Simple Sink function: (id, outputs) => void\n try {\n const result = sink(id, outputs);\n if (result instanceof Promise) {\n result.catch((error) => {\n logger.error(\"Sink error (async)\", { id, error });\n });\n }\n } catch (error) {\n logger.error(\"Sink error (sync)\", { id, error });\n }\n } else {\n // SinkDefinition with filter/name\n const filteredOutputs = sink.filter ? outputs.filter(sink.filter) : outputs;\n\n if (filteredOutputs.length === 0) {\n continue;\n }\n\n try {\n const result = sink.sink(id, filteredOutputs);\n if (result instanceof Promise) {\n result.catch((error) => {\n logger.error(\"Named sink error (async)\", {\n id,\n sinkName: sink.name,\n error,\n });\n });\n }\n } catch (error) {\n logger.error(\"Named sink error (sync)\", {\n id,\n sinkName: sink.name,\n error,\n });\n }\n }\n }\n }\n\n /**\n * Get current state for an ID (without processing)\n */\n getState(id: string): TState | undefined {\n return this.store.get(id);\n }\n\n /**\n * Check if state exists for an ID\n */\n hasState(id: string): boolean {\n return this.store.has(id);\n }\n\n /**\n * Delete state for an ID (cleanup)\n */\n cleanup(id: string): void {\n logger.debug(\"Cleaning up state\", { id });\n this.store.delete(id);\n }\n\n /**\n * Add a sink at runtime\n */\n addSink(sink: Sink<TInput> | SinkDefinition<TInput>): void {\n const sinkName = typeof sink === \"function\" ? \"(anonymous)\" : sink.name;\n logger.debug(\"Adding sink\", { sinkName });\n this.sinks.push(sink);\n }\n\n /**\n * Remove a sink by name (only works for SinkDefinitions)\n */\n removeSink(name: string): boolean {\n const index = this.sinks.findIndex((s) => typeof s !== \"function\" && s.name === name);\n if (index !== -1) {\n this.sinks.splice(index, 1);\n logger.debug(\"Removed sink\", { name });\n return true;\n }\n logger.debug(\"Sink not found for removal\", { name });\n return false;\n }\n}\n\n/**\n * createMealy - Factory function for creating Mealy Machine instances\n *\n * @example\n * ```typescript\n * const mealy = createMealy({\n * processor: myProcessor,\n * store: new MemoryStore(),\n * initialState: { count: 0 },\n * sinks: [logSink],\n * });\n * ```\n */\nexport function createMealy<TState, TInput>(\n config: MealyConfig<TState, TInput>\n): Mealy<TState, TInput> {\n return new Mealy(config);\n}\n","/**\n * Combinators - Functions to compose multiple Processors\n *\n * These utilities allow building complex stream processing pipelines\n * from simple, composable Processor functions.\n */\n\nimport type { Processor } from \"./Processor\";\n\n/**\n * combineProcessors - Combine multiple processors into one\n *\n * Each sub-processor manages its own slice of state.\n * All processors receive the same event and their outputs are merged.\n *\n * @example\n * ```typescript\n * interface CombinedState {\n * message: MessageState;\n * state: StateMachineState;\n * turn: TurnState;\n * }\n *\n * const combinedProcessor = combineProcessors<CombinedState, Event, Event>({\n * message: messageProcessor,\n * state: stateMachineProcessor,\n * turn: turnTrackerProcessor,\n * });\n * ```\n */\nexport function combineProcessors<\n TState extends Record<string, unknown>,\n TInput,\n TOutput,\n>(processors: {\n [K in keyof TState]: Processor<TState[K], TInput, TOutput>;\n}): Processor<TState, TInput, TOutput> {\n return (state: Readonly<TState>, event: TInput): [TState, TOutput[]] => {\n const newState = {} as TState;\n const allOutputs: TOutput[] = [];\n\n for (const key in processors) {\n const processor = processors[key];\n const subState = state[key];\n const [newSubState, outputs] = processor(subState, event);\n\n newState[key] = newSubState;\n allOutputs.push(...outputs);\n }\n\n return [newState, allOutputs];\n };\n}\n\n/**\n * combineInitialStates - Helper to create initial state for combined processors\n */\nexport function combineInitialStates<TState extends Record<string, unknown>>(initialStates: {\n [K in keyof TState]: () => TState[K];\n}): () => TState {\n return () => {\n const state = {} as TState;\n for (const key in initialStates) {\n state[key] = initialStates[key]();\n }\n return state;\n };\n}\n\n/**\n * chainProcessors - Chain processors where output of one feeds into the next\n *\n * Useful for layered event processing:\n * Stream Events → Message Events → Turn Events\n *\n * @example\n * ```typescript\n * const pipeline = chainProcessors(\n * streamToMessageProcessor,\n * messageToTurnProcessor,\n * );\n * ```\n */\nexport function chainProcessors<TState, TEvent>(\n ...processors: Processor<TState, TEvent, TEvent>[]\n): Processor<TState, TEvent, TEvent> {\n return (state: Readonly<TState>, event: TEvent): [TState, TEvent[]] => {\n let currentState = state as TState;\n const finalOutputs: TEvent[] = [];\n\n // Run the event through all processors\n for (const processor of processors) {\n const [newState, outputs] = processor(currentState, event);\n currentState = newState;\n finalOutputs.push(...outputs);\n }\n\n return [currentState, finalOutputs];\n };\n}\n\n/**\n * filterProcessor - Create a processor that only processes certain events\n *\n * @example\n * ```typescript\n * const textOnlyProcessor = filterProcessor(\n * (event) => event.type === 'text_delta',\n * textProcessor,\n * );\n * ```\n */\nexport function filterProcessor<TState, TInput, TOutput>(\n predicate: (event: TInput) => boolean,\n processor: Processor<TState, TInput, TOutput>\n): Processor<TState, TInput, TOutput> {\n return (state: Readonly<TState>, event: TInput): [TState, TOutput[]] => {\n if (predicate(event)) {\n return processor(state, event);\n }\n return [state as TState, []];\n };\n}\n\n/**\n * mapOutput - Transform output events\n *\n * @example\n * ```typescript\n * const withTimestamp = mapOutput(\n * myProcessor,\n * (output) => ({ ...output, processedAt: Date.now() }),\n * );\n * ```\n */\nexport function mapOutput<TState, TInput, TOutput, TMapped>(\n processor: Processor<TState, TInput, TOutput>,\n mapper: (output: TOutput) => TMapped\n): Processor<TState, TInput, TMapped> {\n return (state: Readonly<TState>, event: TInput): [TState, TMapped[]] => {\n const [newState, outputs] = processor(state, event);\n return [newState, outputs.map(mapper)];\n };\n}\n\n/**\n * withLogging - Add logging to a processor (for debugging)\n *\n * @example\n * ```typescript\n * const debugProcessor = withLogging(myProcessor, 'MyProcessor');\n * ```\n */\nexport function withLogging<TState, TInput, TOutput>(\n processor: Processor<TState, TInput, TOutput>,\n name: string,\n logger: {\n debug: (message: string, data?: unknown) => void;\n } = console\n): Processor<TState, TInput, TOutput> {\n return (state: Readonly<TState>, event: TInput): [TState, TOutput[]] => {\n logger.debug(`[${name}] Input:`, { event, state });\n const [newState, outputs] = processor(state, event);\n logger.debug(`[${name}] Output:`, { newState, outputs });\n return [newState, outputs];\n };\n}\n\n/**\n * identityProcessor - A processor that does nothing (useful as default)\n */\nexport function identityProcessor<TState, TEvent>(): Processor<TState, TEvent, TEvent> {\n return (state: Readonly<TState>, _event: TEvent): [TState, TEvent[]] => {\n return [state as TState, []];\n };\n}\n"],"mappings":";AA0DO,IAAM,cAAN,MAAyC;AAAA,EACtC,SAAS,oBAAI,IA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