@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,98 @@
1
+ /**
2
+ * Logger type definitions
3
+ *
4
+ * Self-contained types for the logger module.
5
+ * No external dependencies required.
6
+ */
7
+
8
+ /**
9
+ * LogLevel - Standard log level type
10
+ *
11
+ * Defines the severity levels for logging.
12
+ * Uses string literal types for better readability and type safety.
13
+ */
14
+ export type LogLevel = "debug" | "info" | "warn" | "error" | "silent";
15
+
16
+ /**
17
+ * Logging context metadata
18
+ */
19
+ export type LogContext = Record<string, unknown>;
20
+
21
+ /**
22
+ * Logger interface
23
+ *
24
+ * Platform-agnostic logging interface that can be implemented
25
+ * by any logging library (console, pino, winston, etc.)
26
+ *
27
+ * Similar to SLF4J's Logger interface in Java.
28
+ */
29
+ export interface Logger {
30
+ /**
31
+ * Logger name (typically class name or module path)
32
+ */
33
+ readonly name: string;
34
+
35
+ /**
36
+ * Current log level
37
+ */
38
+ readonly level: LogLevel;
39
+
40
+ /**
41
+ * Log debug message
42
+ */
43
+ debug(message: string, context?: LogContext): void;
44
+
45
+ /**
46
+ * Log info message
47
+ */
48
+ info(message: string, context?: LogContext): void;
49
+
50
+ /**
51
+ * Log warning message
52
+ */
53
+ warn(message: string, context?: LogContext): void;
54
+
55
+ /**
56
+ * Log error message or Error object
57
+ */
58
+ error(message: string | Error, context?: LogContext): void;
59
+
60
+ /**
61
+ * Check if debug level is enabled
62
+ */
63
+ isDebugEnabled(): boolean;
64
+
65
+ /**
66
+ * Check if info level is enabled
67
+ */
68
+ isInfoEnabled(): boolean;
69
+
70
+ /**
71
+ * Check if warn level is enabled
72
+ */
73
+ isWarnEnabled(): boolean;
74
+
75
+ /**
76
+ * Check if error level is enabled
77
+ */
78
+ isErrorEnabled(): boolean;
79
+ }
80
+
81
+ /**
82
+ * LoggerFactory interface
83
+ *
84
+ * Factory for creating named Logger instances.
85
+ * External implementations can provide their own LoggerFactory
86
+ * to integrate custom logging libraries (pino, winston, etc.)
87
+ *
88
+ * Similar to SLF4J's LoggerFactory in Java.
89
+ */
90
+ export interface LoggerFactory {
91
+ /**
92
+ * Get or create a logger with the specified name
93
+ *
94
+ * @param name - Logger name (typically class name or module path)
95
+ * @returns Logger instance
96
+ */
97
+ getLogger(name: string): Logger;
98
+ }
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Container - Resource isolation unit
3
+ *
4
+ * Container provides an isolated environment for Images and Agents.
5
+ * Each container can have multiple Images (conversations).
6
+ */
7
+
8
+ import { createLogger } from "commonxjs/logger";
9
+ import type { ContainerRecord } from "../persistence/types";
10
+ import type { Container, ContainerContext, ContainerCreateConfig } from "./types";
11
+
12
+ const logger = createLogger("container/Container");
13
+
14
+ /**
15
+ * ContainerImpl - Container implementation
16
+ */
17
+ export class ContainerImpl implements Container {
18
+ private constructor(
19
+ private readonly record: ContainerRecord,
20
+ private readonly context: ContainerContext
21
+ ) {}
22
+
23
+ // ==================== Getters ====================
24
+
25
+ get containerId(): string {
26
+ return this.record.containerId;
27
+ }
28
+
29
+ get createdAt(): number {
30
+ return this.record.createdAt;
31
+ }
32
+
33
+ get updatedAt(): number {
34
+ return this.record.updatedAt;
35
+ }
36
+
37
+ get config(): Record<string, unknown> | undefined {
38
+ return this.record.config;
39
+ }
40
+
41
+ // ==================== Static Factory Methods ====================
42
+
43
+ /**
44
+ * Create a new container
45
+ */
46
+ static async create(
47
+ config: ContainerCreateConfig,
48
+ context: ContainerContext
49
+ ): Promise<ContainerImpl> {
50
+ const now = Date.now();
51
+ const containerId = config.containerId ?? ContainerImpl.generateContainerId();
52
+
53
+ const record: ContainerRecord = {
54
+ containerId,
55
+ createdAt: now,
56
+ updatedAt: now,
57
+ config: config.config,
58
+ };
59
+
60
+ await context.containerRepository.saveContainer(record);
61
+
62
+ logger.info("Container created", { containerId });
63
+
64
+ return new ContainerImpl(record, context);
65
+ }
66
+
67
+ /**
68
+ * Load an existing container from storage
69
+ */
70
+ static async load(containerId: string, context: ContainerContext): Promise<ContainerImpl | null> {
71
+ const record = await context.containerRepository.findContainerById(containerId);
72
+ if (!record) {
73
+ logger.debug("Container not found", { containerId });
74
+ return null;
75
+ }
76
+
77
+ logger.debug("Container loaded", { containerId });
78
+ return new ContainerImpl(record, context);
79
+ }
80
+
81
+ /**
82
+ * Get or create a container
83
+ */
84
+ static async getOrCreate(containerId: string, context: ContainerContext): Promise<ContainerImpl> {
85
+ const existing = await ContainerImpl.load(containerId, context);
86
+ if (existing) {
87
+ return existing;
88
+ }
89
+
90
+ return ContainerImpl.create({ containerId }, context);
91
+ }
92
+
93
+ /**
94
+ * List all containers
95
+ */
96
+ static async listAll(context: ContainerContext): Promise<ContainerRecord[]> {
97
+ return context.containerRepository.findAllContainers();
98
+ }
99
+
100
+ // ==================== Instance Methods ====================
101
+
102
+ /**
103
+ * Update container configuration
104
+ */
105
+ async update(updates: { config?: Record<string, unknown> }): Promise<Container> {
106
+ const now = Date.now();
107
+ const updatedRecord: ContainerRecord = {
108
+ ...this.record,
109
+ config: updates.config ?? this.record.config,
110
+ updatedAt: now,
111
+ };
112
+
113
+ await this.context.containerRepository.saveContainer(updatedRecord);
114
+
115
+ logger.info("Container updated", { containerId: this.containerId, updates });
116
+ return new ContainerImpl(updatedRecord, this.context);
117
+ }
118
+
119
+ /**
120
+ * Delete this container and all its resources
121
+ */
122
+ async delete(): Promise<void> {
123
+ // Find all images in this container
124
+ const images = await this.context.imageRepository.findImagesByContainerId(this.containerId);
125
+
126
+ // Delete all images and their sessions
127
+ for (const image of images) {
128
+ await this.context.sessionRepository.deleteSession(image.sessionId);
129
+ await this.context.imageRepository.deleteImage(image.imageId);
130
+ }
131
+
132
+ // Delete container
133
+ await this.context.containerRepository.deleteContainer(this.containerId);
134
+
135
+ logger.info("Container deleted", {
136
+ containerId: this.containerId,
137
+ imagesDeleted: images.length,
138
+ });
139
+ }
140
+
141
+ /**
142
+ * Get the underlying record
143
+ */
144
+ toRecord(): ContainerRecord {
145
+ return { ...this.record };
146
+ }
147
+
148
+ // ==================== Private Helpers ====================
149
+
150
+ private static generateContainerId(): string {
151
+ const timestamp = Date.now().toString(36);
152
+ const random = Math.random().toString(36).substring(2, 8);
153
+ return `ctr_${timestamp}_${random}`;
154
+ }
155
+ }
156
+
157
+ /**
158
+ * Create a new Container
159
+ */
160
+ export async function createContainer(
161
+ config: ContainerCreateConfig,
162
+ context: ContainerContext
163
+ ): Promise<Container> {
164
+ return ContainerImpl.create(config, context);
165
+ }
166
+
167
+ /**
168
+ * Load an existing Container
169
+ */
170
+ export async function loadContainer(
171
+ containerId: string,
172
+ context: ContainerContext
173
+ ): Promise<Container | null> {
174
+ return ContainerImpl.load(containerId, context);
175
+ }
176
+
177
+ /**
178
+ * Get or create a Container
179
+ */
180
+ export async function getOrCreateContainer(
181
+ containerId: string,
182
+ context: ContainerContext
183
+ ): Promise<Container> {
184
+ return ContainerImpl.getOrCreate(containerId, context);
185
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Container Module
3
+ *
4
+ * Manages resource isolation containers.
5
+ *
6
+ * Usage:
7
+ * ```typescript
8
+ * import {
9
+ * createContainer,
10
+ * loadContainer,
11
+ * getOrCreateContainer,
12
+ * type ContainerRepository,
13
+ * } from "@agentxjs/core/container";
14
+ *
15
+ * const context = {
16
+ * containerRepository: myContainerRepository,
17
+ * imageRepository: myImageRepository,
18
+ * sessionRepository: mySessionRepository,
19
+ * };
20
+ *
21
+ * // Create new container
22
+ * const container = await createContainer({
23
+ * containerId: "user-123",
24
+ * config: { maxAgents: 10 },
25
+ * }, context);
26
+ *
27
+ * // Load existing container
28
+ * const existing = await loadContainer("user-123", context);
29
+ *
30
+ * // Get or create
31
+ * const container = await getOrCreateContainer("user-123", context);
32
+ * ```
33
+ */
34
+
35
+ export type {
36
+ ContainerRecord,
37
+ ContainerConfig,
38
+ ContainerRepository,
39
+ Container,
40
+ ContainerContext,
41
+ ContainerCreateConfig,
42
+ } from "./types";
43
+
44
+ export { ContainerImpl, createContainer, loadContainer, getOrCreateContainer } from "./Container";
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Container Types
3
+ *
4
+ * Container is the resource isolation unit.
5
+ * Each container provides an isolated environment for Images and Agents.
6
+ *
7
+ * Lifecycle:
8
+ * - create() → ContainerRecord (persistent)
9
+ * - Container manages multiple Images/Agents within its boundary
10
+ */
11
+
12
+ import type { ImageRepository } from "../image/types";
13
+ import type { SessionRepository } from "../session/types";
14
+
15
+ // ============================================================================
16
+ // Re-export from persistence (storage schema)
17
+ // ============================================================================
18
+
19
+ export type { ContainerRecord, ContainerConfig, ContainerRepository } from "../persistence/types";
20
+
21
+ // ============================================================================
22
+ // Container Interface
23
+ // ============================================================================
24
+
25
+ /**
26
+ * Container - Resource isolation unit
27
+ *
28
+ * Manages Images and Agents within an isolated boundary.
29
+ */
30
+ export interface Container {
31
+ readonly containerId: string;
32
+ readonly createdAt: number;
33
+ readonly updatedAt: number;
34
+ readonly config: Record<string, unknown> | undefined;
35
+
36
+ /**
37
+ * Update container configuration
38
+ */
39
+ update(updates: { config?: Record<string, unknown> }): Promise<Container>;
40
+
41
+ /**
42
+ * Delete this container and all its resources
43
+ */
44
+ delete(): Promise<void>;
45
+
46
+ /**
47
+ * Get the underlying record
48
+ */
49
+ toRecord(): import("../persistence/types").ContainerRecord;
50
+ }
51
+
52
+ // ============================================================================
53
+ // Container Configuration
54
+ // ============================================================================
55
+
56
+ /**
57
+ * Context needed by Container operations
58
+ */
59
+ export interface ContainerContext {
60
+ containerRepository: import("../persistence/types").ContainerRepository;
61
+ imageRepository: ImageRepository;
62
+ sessionRepository: SessionRepository;
63
+ }
64
+
65
+ /**
66
+ * Configuration for creating a new Container
67
+ */
68
+ export interface ContainerCreateConfig {
69
+ containerId?: string;
70
+ config?: Record<string, unknown>;
71
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * Driver Module
3
+ *
4
+ * Provides interfaces for LLM communication:
5
+ * - Driver: Single session communication with LLM
6
+ * - DriverConfig: Configuration for LLM connection
7
+ * - CreateDriver: Factory function type
8
+ *
9
+ * Key Design:
10
+ * - Clear input/output boundary (for recording/playback)
11
+ * - Single responsibility: only handle session communication
12
+ * - Configuration defined by us (capability boundary)
13
+ *
14
+ * Implementations are provided by platform packages:
15
+ * - @agentxjs/claude-driver: Claude Agent SDK driver
16
+ */
17
+
18
+ export type {
19
+ // Configuration
20
+ McpServerConfig,
21
+ DriverConfig,
22
+ DriverState,
23
+
24
+ // Core Interface
25
+ Driver,
26
+ CreateDriver,
27
+
28
+ // Stream Events
29
+ StreamEvent,
30
+ StopReason,
31
+ MessageStartEvent,
32
+ MessageStopEvent,
33
+ TextDeltaEvent,
34
+ ToolUseStartEvent,
35
+ InputJsonDeltaEvent,
36
+ ToolUseStopEvent,
37
+ ToolResultEvent,
38
+ ErrorEvent,
39
+ InterruptedEvent,
40
+ DriverStreamEvent,
41
+ DriverStreamEventType,
42
+ } from "./types";