@agentxjs/core 1.9.1-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.
Files changed (77) hide show
  1. package/package.json +31 -0
  2. package/src/agent/AgentStateMachine.ts +151 -0
  3. package/src/agent/README.md +296 -0
  4. package/src/agent/__tests__/AgentStateMachine.test.ts +346 -0
  5. package/src/agent/__tests__/createAgent.test.ts +728 -0
  6. package/src/agent/__tests__/engine/internal/messageAssemblerProcessor.test.ts +567 -0
  7. package/src/agent/__tests__/engine/internal/stateEventProcessor.test.ts +315 -0
  8. package/src/agent/__tests__/engine/internal/turnTrackerProcessor.test.ts +340 -0
  9. package/src/agent/__tests__/engine/mealy/Mealy.test.ts +370 -0
  10. package/src/agent/__tests__/engine/mealy/Store.test.ts +123 -0
  11. package/src/agent/__tests__/engine/mealy/combinators.test.ts +322 -0
  12. package/src/agent/createAgent.ts +467 -0
  13. package/src/agent/engine/AgentProcessor.ts +106 -0
  14. package/src/agent/engine/MealyMachine.ts +184 -0
  15. package/src/agent/engine/internal/index.ts +35 -0
  16. package/src/agent/engine/internal/messageAssemblerProcessor.ts +550 -0
  17. package/src/agent/engine/internal/stateEventProcessor.ts +313 -0
  18. package/src/agent/engine/internal/turnTrackerProcessor.ts +239 -0
  19. package/src/agent/engine/mealy/Mealy.ts +308 -0
  20. package/src/agent/engine/mealy/Processor.ts +70 -0
  21. package/src/agent/engine/mealy/Sink.ts +56 -0
  22. package/src/agent/engine/mealy/Source.ts +51 -0
  23. package/src/agent/engine/mealy/Store.ts +98 -0
  24. package/src/agent/engine/mealy/combinators.ts +176 -0
  25. package/src/agent/engine/mealy/index.ts +45 -0
  26. package/src/agent/index.ts +106 -0
  27. package/src/agent/types/engine.ts +395 -0
  28. package/src/agent/types/event.ts +478 -0
  29. package/src/agent/types/index.ts +197 -0
  30. package/src/agent/types/message.ts +387 -0
  31. package/src/common/index.ts +8 -0
  32. package/src/common/logger/ConsoleLogger.ts +137 -0
  33. package/src/common/logger/LoggerFactoryImpl.ts +123 -0
  34. package/src/common/logger/index.ts +26 -0
  35. package/src/common/logger/types.ts +98 -0
  36. package/src/container/Container.ts +185 -0
  37. package/src/container/index.ts +44 -0
  38. package/src/container/types.ts +71 -0
  39. package/src/driver/index.ts +42 -0
  40. package/src/driver/types.ts +363 -0
  41. package/src/event/EventBus.ts +260 -0
  42. package/src/event/README.md +237 -0
  43. package/src/event/__tests__/EventBus.test.ts +251 -0
  44. package/src/event/index.ts +46 -0
  45. package/src/event/types/agent.ts +512 -0
  46. package/src/event/types/base.ts +241 -0
  47. package/src/event/types/bus.ts +429 -0
  48. package/src/event/types/command.ts +749 -0
  49. package/src/event/types/container.ts +471 -0
  50. package/src/event/types/driver.ts +452 -0
  51. package/src/event/types/index.ts +26 -0
  52. package/src/event/types/session.ts +314 -0
  53. package/src/image/Image.ts +203 -0
  54. package/src/image/index.ts +36 -0
  55. package/src/image/types.ts +77 -0
  56. package/src/index.ts +20 -0
  57. package/src/mq/OffsetGenerator.ts +48 -0
  58. package/src/mq/README.md +166 -0
  59. package/src/mq/__tests__/OffsetGenerator.test.ts +121 -0
  60. package/src/mq/index.ts +18 -0
  61. package/src/mq/types.ts +172 -0
  62. package/src/network/RpcClient.ts +455 -0
  63. package/src/network/index.ts +76 -0
  64. package/src/network/jsonrpc.ts +336 -0
  65. package/src/network/protocol.ts +90 -0
  66. package/src/network/types.ts +284 -0
  67. package/src/persistence/index.ts +27 -0
  68. package/src/persistence/types.ts +226 -0
  69. package/src/runtime/AgentXRuntime.ts +501 -0
  70. package/src/runtime/index.ts +56 -0
  71. package/src/runtime/types.ts +236 -0
  72. package/src/session/Session.ts +71 -0
  73. package/src/session/index.ts +25 -0
  74. package/src/session/types.ts +77 -0
  75. package/src/workspace/index.ts +27 -0
  76. package/src/workspace/types.ts +131 -0
  77. package/tsconfig.json +10 -0
@@ -0,0 +1,322 @@
1
+ /**
2
+ * combinators.test.ts - Unit tests for Mealy processor combinators
3
+ */
4
+
5
+ import { describe, it, expect } from "bun:test";
6
+ import {
7
+ combineProcessors,
8
+ combineInitialStates,
9
+ chainProcessors,
10
+ filterProcessor,
11
+ mapOutput,
12
+ withLogging,
13
+ identityProcessor,
14
+ } from "../../../engine/mealy/combinators";
15
+ import type { Processor } from "../../../engine/mealy/Processor";
16
+
17
+ // Test event types
18
+ interface TestEvent {
19
+ type: string;
20
+ value: number;
21
+ }
22
+
23
+ // Test state types
24
+ interface CounterState {
25
+ count: number;
26
+ }
27
+
28
+ interface AccumulatorState {
29
+ total: number;
30
+ }
31
+
32
+ // Test processors
33
+ const counterProcessor: Processor<CounterState, TestEvent, TestEvent> = (state, event) => {
34
+ if (event.type === "increment") {
35
+ return [
36
+ { count: state.count + event.value },
37
+ [{ type: "counted", value: state.count + event.value }],
38
+ ];
39
+ }
40
+ return [state, []];
41
+ };
42
+
43
+ const accumulatorProcessor: Processor<AccumulatorState, TestEvent, TestEvent> = (state, event) => {
44
+ if (event.type === "accumulate") {
45
+ const newTotal = state.total + event.value;
46
+ return [{ total: newTotal }, [{ type: "accumulated", value: newTotal }]];
47
+ }
48
+ return [state, []];
49
+ };
50
+
51
+ describe("combineProcessors", () => {
52
+ interface CombinedState {
53
+ counter: CounterState;
54
+ accumulator: AccumulatorState;
55
+ }
56
+
57
+ const combinedProcessor = combineProcessors<CombinedState, TestEvent, TestEvent>({
58
+ counter: counterProcessor,
59
+ accumulator: accumulatorProcessor,
60
+ });
61
+
62
+ it("should process event through all sub-processors", () => {
63
+ const state: CombinedState = {
64
+ counter: { count: 0 },
65
+ accumulator: { total: 0 },
66
+ };
67
+
68
+ // Event handled by counter
69
+ const [newState1, outputs1] = combinedProcessor(state, { type: "increment", value: 5 });
70
+ expect(newState1.counter.count).toBe(5);
71
+ expect(newState1.accumulator.total).toBe(0);
72
+ expect(outputs1).toHaveLength(1);
73
+ expect(outputs1[0]).toEqual({ type: "counted", value: 5 });
74
+
75
+ // Event handled by accumulator
76
+ const [newState2, outputs2] = combinedProcessor(newState1, { type: "accumulate", value: 10 });
77
+ expect(newState2.counter.count).toBe(5);
78
+ expect(newState2.accumulator.total).toBe(10);
79
+ expect(outputs2).toHaveLength(1);
80
+ expect(outputs2[0]).toEqual({ type: "accumulated", value: 10 });
81
+ });
82
+
83
+ it("should merge outputs from all processors", () => {
84
+ // Create processors that both respond to the same event type
85
+ const proc1: Processor<{ a: number }, TestEvent, TestEvent> = (state, event) => {
86
+ if (event.type === "both") {
87
+ return [{ a: state.a + 1 }, [{ type: "from_a", value: 1 }]];
88
+ }
89
+ return [state, []];
90
+ };
91
+
92
+ const proc2: Processor<{ b: number }, TestEvent, TestEvent> = (state, event) => {
93
+ if (event.type === "both") {
94
+ return [{ b: state.b + 2 }, [{ type: "from_b", value: 2 }]];
95
+ }
96
+ return [state, []];
97
+ };
98
+
99
+ const combined = combineProcessors<
100
+ { a: { a: number }; b: { b: number } },
101
+ TestEvent,
102
+ TestEvent
103
+ >({
104
+ a: proc1,
105
+ b: proc2,
106
+ });
107
+
108
+ const [newState, outputs] = combined({ a: { a: 0 }, b: { b: 0 } }, { type: "both", value: 0 });
109
+
110
+ expect(newState.a.a).toBe(1);
111
+ expect(newState.b.b).toBe(2);
112
+ expect(outputs).toHaveLength(2);
113
+ expect(outputs).toContainEqual({ type: "from_a", value: 1 });
114
+ expect(outputs).toContainEqual({ type: "from_b", value: 2 });
115
+ });
116
+
117
+ it("should return unchanged state when no processor handles event", () => {
118
+ const state: CombinedState = {
119
+ counter: { count: 5 },
120
+ accumulator: { total: 10 },
121
+ };
122
+
123
+ const [newState, outputs] = combinedProcessor(state, { type: "unknown", value: 100 });
124
+
125
+ expect(newState.counter.count).toBe(5);
126
+ expect(newState.accumulator.total).toBe(10);
127
+ expect(outputs).toHaveLength(0);
128
+ });
129
+ });
130
+
131
+ describe("combineInitialStates", () => {
132
+ it("should create initial state from multiple factories", () => {
133
+ const createInitialState = combineInitialStates({
134
+ counter: () => ({ count: 0 }),
135
+ accumulator: () => ({ total: 100 }),
136
+ });
137
+
138
+ const state = createInitialState();
139
+
140
+ expect(state.counter.count).toBe(0);
141
+ expect(state.accumulator.total).toBe(100);
142
+ });
143
+
144
+ it("should create fresh state on each call", () => {
145
+ const createInitialState = combineInitialStates({
146
+ counter: () => ({ count: 0 }),
147
+ });
148
+
149
+ const state1 = createInitialState();
150
+ const state2 = createInitialState();
151
+
152
+ state1.counter.count = 999;
153
+
154
+ expect(state2.counter.count).toBe(0);
155
+ });
156
+ });
157
+
158
+ describe("chainProcessors", () => {
159
+ // Processors that pass through and accumulate
160
+ const addOneProcessor: Processor<CounterState, TestEvent, TestEvent> = (state, event) => {
161
+ const newCount = state.count + 1;
162
+ return [{ count: newCount }, [{ ...event, value: event.value + 1 }]];
163
+ };
164
+
165
+ const multiplyTwoProcessor: Processor<CounterState, TestEvent, TestEvent> = (state, event) => {
166
+ const newCount = state.count * 2;
167
+ return [{ count: newCount }, [{ ...event, value: event.value * 2 }]];
168
+ };
169
+
170
+ it("should chain processors sequentially", () => {
171
+ const chained = chainProcessors(addOneProcessor, multiplyTwoProcessor);
172
+
173
+ const [newState, outputs] = chained({ count: 1 }, { type: "test", value: 10 });
174
+
175
+ // State: (1 + 1) * 2 = 4
176
+ expect(newState.count).toBe(4);
177
+
178
+ // Outputs from both processors
179
+ expect(outputs).toHaveLength(2);
180
+ expect(outputs[0]).toEqual({ type: "test", value: 11 }); // 10 + 1
181
+ expect(outputs[1]).toEqual({ type: "test", value: 20 }); // 10 * 2
182
+ });
183
+
184
+ it("should handle empty processor chain", () => {
185
+ const chained = chainProcessors<CounterState, TestEvent>();
186
+
187
+ const [newState, outputs] = chained({ count: 5 }, { type: "test", value: 10 });
188
+
189
+ expect(newState.count).toBe(5);
190
+ expect(outputs).toHaveLength(0);
191
+ });
192
+
193
+ it("should handle single processor", () => {
194
+ const chained = chainProcessors(addOneProcessor);
195
+
196
+ const [newState, outputs] = chained({ count: 1 }, { type: "test", value: 10 });
197
+
198
+ expect(newState.count).toBe(2);
199
+ expect(outputs).toHaveLength(1);
200
+ expect(outputs[0]).toEqual({ type: "test", value: 11 });
201
+ });
202
+ });
203
+
204
+ describe("filterProcessor", () => {
205
+ const incrementOnlyProcessor = filterProcessor<CounterState, TestEvent, TestEvent>(
206
+ (event) => event.type === "increment",
207
+ counterProcessor
208
+ );
209
+
210
+ it("should process events matching the predicate", () => {
211
+ const [newState, outputs] = incrementOnlyProcessor(
212
+ { count: 0 },
213
+ { type: "increment", value: 5 }
214
+ );
215
+
216
+ expect(newState.count).toBe(5);
217
+ expect(outputs).toHaveLength(1);
218
+ });
219
+
220
+ it("should skip events not matching the predicate", () => {
221
+ const [newState, outputs] = incrementOnlyProcessor(
222
+ { count: 10 },
223
+ { type: "decrement", value: 5 }
224
+ );
225
+
226
+ expect(newState.count).toBe(10);
227
+ expect(outputs).toHaveLength(0);
228
+ });
229
+ });
230
+
231
+ describe("mapOutput", () => {
232
+ interface EnrichedEvent extends TestEvent {
233
+ timestamp: number;
234
+ }
235
+
236
+ it("should transform output events", () => {
237
+ const withTimestamp = mapOutput<CounterState, TestEvent, TestEvent, EnrichedEvent>(
238
+ counterProcessor,
239
+ (output) => ({ ...output, timestamp: 12345 })
240
+ );
241
+
242
+ const [newState, outputs] = withTimestamp({ count: 0 }, { type: "increment", value: 5 });
243
+
244
+ expect(newState.count).toBe(5);
245
+ expect(outputs).toHaveLength(1);
246
+ expect(outputs[0]).toEqual({ type: "counted", value: 5, timestamp: 12345 });
247
+ });
248
+
249
+ it("should handle empty outputs", () => {
250
+ const withTimestamp = mapOutput<CounterState, TestEvent, TestEvent, EnrichedEvent>(
251
+ counterProcessor,
252
+ (output) => ({ ...output, timestamp: 12345 })
253
+ );
254
+
255
+ const [newState, outputs] = withTimestamp({ count: 0 }, { type: "unknown", value: 0 });
256
+
257
+ expect(newState.count).toBe(0);
258
+ expect(outputs).toHaveLength(0);
259
+ });
260
+ });
261
+
262
+ describe("withLogging", () => {
263
+ it("should call logger with input and output", () => {
264
+ const logs: unknown[] = [];
265
+ const mockLogger = {
266
+ debug: (message: string, data?: unknown) => {
267
+ logs.push({ message, data });
268
+ },
269
+ };
270
+
271
+ const loggingProcessor = withLogging(counterProcessor, "Counter", mockLogger);
272
+
273
+ const [newState, outputs] = loggingProcessor({ count: 0 }, { type: "increment", value: 5 });
274
+
275
+ expect(newState.count).toBe(5);
276
+ expect(outputs).toHaveLength(1);
277
+
278
+ // Should have 2 log entries: input and output
279
+ expect(logs).toHaveLength(2);
280
+ expect(logs[0]).toMatchObject({ message: "[Counter] Input:" });
281
+ expect(logs[1]).toMatchObject({ message: "[Counter] Output:" });
282
+ });
283
+
284
+ it("should preserve processor behavior", () => {
285
+ const logs: unknown[] = [];
286
+ const mockLogger = {
287
+ debug: (_msg: string, _data?: unknown) => {
288
+ logs.push({});
289
+ },
290
+ };
291
+
292
+ const loggingProcessor = withLogging(counterProcessor, "Counter", mockLogger);
293
+
294
+ // Test multiple calls
295
+ let [state, outputs] = loggingProcessor({ count: 0 }, { type: "increment", value: 1 });
296
+ expect(state.count).toBe(1);
297
+
298
+ [state, outputs] = loggingProcessor(state, { type: "increment", value: 2 });
299
+ expect(state.count).toBe(3);
300
+ expect(outputs[0]).toEqual({ type: "counted", value: 3 });
301
+ });
302
+ });
303
+
304
+ describe("identityProcessor", () => {
305
+ it("should return unchanged state and empty outputs", () => {
306
+ const identity = identityProcessor<CounterState, TestEvent>();
307
+
308
+ const [newState, outputs] = identity({ count: 42 }, { type: "any", value: 100 });
309
+
310
+ expect(newState.count).toBe(42);
311
+ expect(outputs).toHaveLength(0);
312
+ });
313
+
314
+ it("should work with any event type", () => {
315
+ const identity = identityProcessor<{ data: string }, { anything: boolean }>();
316
+
317
+ const [newState, outputs] = identity({ data: "test" }, { anything: true });
318
+
319
+ expect(newState.data).toBe("test");
320
+ expect(outputs).toHaveLength(0);
321
+ });
322
+ });