@agentxjs/common 0.1.0 → 1.1.3
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/README.md +293 -0
- package/dist/index.cjs +23 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +23 -15
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
# @agentxjs/common
|
|
2
|
+
|
|
3
|
+
> Shared infrastructure for AgentX internal packages
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
`@agentxjs/common` provides shared utilities used by AgentX internal packages (`@agentxjs/agent`, `@agentxjs/runtime`, etc.). This is an internal package - application code should not depend on it directly.
|
|
8
|
+
|
|
9
|
+
**Key Features:**
|
|
10
|
+
|
|
11
|
+
- **Logger Facade** - Lazy-initialized logging with pluggable backends
|
|
12
|
+
- **Zero Runtime Overhead** - Safe to use at module level
|
|
13
|
+
- **Pluggable Architecture** - Custom loggers via Runtime injection
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pnpm add @agentxjs/common
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
> **Note**: This package is typically installed transitively as a dependency of other AgentX packages.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
## Logger
|
|
26
|
+
|
|
27
|
+
### Quick Start
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { createLogger } from "@agentxjs/common";
|
|
31
|
+
|
|
32
|
+
// Safe to use at module level (before Runtime configured)
|
|
33
|
+
const logger = createLogger("agent/AgentEngine");
|
|
34
|
+
|
|
35
|
+
// Later, at runtime
|
|
36
|
+
logger.info("Agent created", { agentId: "agent_123" });
|
|
37
|
+
logger.debug("Processing message", { messageId: "msg_456" });
|
|
38
|
+
logger.error("Failed to process", { error: err.message });
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Features
|
|
42
|
+
|
|
43
|
+
#### 1. Lazy Initialization
|
|
44
|
+
|
|
45
|
+
Loggers can be created at module level without errors:
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
// At module level (before Runtime exists)
|
|
49
|
+
const logger = createLogger("engine/MealyMachine");
|
|
50
|
+
|
|
51
|
+
// No-op until LoggerFactory is configured
|
|
52
|
+
logger.info("This is buffered");
|
|
53
|
+
|
|
54
|
+
// Later, when Runtime initializes
|
|
55
|
+
setLoggerFactory(new LoggerFactoryImpl());
|
|
56
|
+
|
|
57
|
+
// Now logs are emitted
|
|
58
|
+
logger.info("This is logged");
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
#### 2. Structured Logging
|
|
62
|
+
|
|
63
|
+
All loggers support structured context:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
logger.info("User logged in", {
|
|
67
|
+
userId: "user_123",
|
|
68
|
+
timestamp: Date.now(),
|
|
69
|
+
ip: "192.168.1.1",
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Output (with ConsoleLogger):
|
|
73
|
+
// [INFO] agent/UserService: User logged in {"userId":"user_123",...}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
#### 3. Log Levels
|
|
77
|
+
|
|
78
|
+
Supported log levels (in order of severity):
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
logger.debug("Detailed debug info"); // Development only
|
|
82
|
+
logger.info("Normal operation"); // General info
|
|
83
|
+
logger.warn("Warning condition"); // Potential issues
|
|
84
|
+
logger.error("Error occurred"); // Errors
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### API
|
|
88
|
+
|
|
89
|
+
#### `createLogger(category: string): Logger`
|
|
90
|
+
|
|
91
|
+
Creates a new logger instance.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
const logger = createLogger("agent/MyAgent");
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Parameters:**
|
|
98
|
+
|
|
99
|
+
- `category` - Logger category (e.g., `"engine/AgentEngine"`, `"runtime/Container"`)
|
|
100
|
+
|
|
101
|
+
**Returns:** `Logger` instance
|
|
102
|
+
|
|
103
|
+
#### `setLoggerFactory(factory: LoggerFactory): void`
|
|
104
|
+
|
|
105
|
+
Sets the global logger factory (called by Runtime).
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { LoggerFactoryImpl, setLoggerFactory } from "@agentxjs/common";
|
|
109
|
+
|
|
110
|
+
setLoggerFactory(
|
|
111
|
+
new LoggerFactoryImpl({
|
|
112
|
+
level: "debug",
|
|
113
|
+
enableTimestamp: true,
|
|
114
|
+
})
|
|
115
|
+
);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Logger Interface
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
interface Logger {
|
|
122
|
+
debug(message: string, context?: LogContext): void;
|
|
123
|
+
info(message: string, context?: LogContext): void;
|
|
124
|
+
warn(message: string, context?: LogContext): void;
|
|
125
|
+
error(message: string, context?: LogContext): void;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
type LogContext = Record<string, unknown>;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
## Built-in Implementations
|
|
134
|
+
|
|
135
|
+
### ConsoleLogger
|
|
136
|
+
|
|
137
|
+
Default console-based logger with formatting.
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
import { ConsoleLogger } from "@agentxjs/common";
|
|
141
|
+
|
|
142
|
+
const logger = new ConsoleLogger("my-category", {
|
|
143
|
+
level: "debug",
|
|
144
|
+
enableTimestamp: true,
|
|
145
|
+
enableColor: true,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
logger.info("Hello", { foo: "bar" });
|
|
149
|
+
// Output: [INFO] my-category: Hello {"foo":"bar"}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
**Options:**
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
interface ConsoleLoggerOptions {
|
|
156
|
+
level?: LogLevel; // Minimum log level (default: "info")
|
|
157
|
+
enableTimestamp?: boolean; // Show timestamps (default: false)
|
|
158
|
+
enableColor?: boolean; // Colorize output (default: true in TTY)
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### LoggerFactoryImpl
|
|
163
|
+
|
|
164
|
+
Default factory that creates `ConsoleLogger` instances.
|
|
165
|
+
|
|
166
|
+
```typescript
|
|
167
|
+
import { LoggerFactoryImpl } from "@agentxjs/common";
|
|
168
|
+
|
|
169
|
+
const factory = new LoggerFactoryImpl({
|
|
170
|
+
level: "info",
|
|
171
|
+
enableTimestamp: false,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
const logger = factory.createLogger("test");
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
---
|
|
178
|
+
|
|
179
|
+
## Custom Logger Implementation
|
|
180
|
+
|
|
181
|
+
You can provide custom loggers for advanced use cases:
|
|
182
|
+
|
|
183
|
+
```typescript
|
|
184
|
+
import { Logger, LoggerFactory, setLoggerFactory } from "@agentxjs/common";
|
|
185
|
+
|
|
186
|
+
// Custom logger (e.g., sends logs to external service)
|
|
187
|
+
class RemoteLogger implements Logger {
|
|
188
|
+
constructor(private category: string) {}
|
|
189
|
+
|
|
190
|
+
debug(message: string, context?: LogContext) {
|
|
191
|
+
this.send("debug", message, context);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
info(message: string, context?: LogContext) {
|
|
195
|
+
this.send("info", message, context);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
warn(message: string, context?: LogContext) {
|
|
199
|
+
this.send("warn", message, context);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
error(message: string, context?: LogContext) {
|
|
203
|
+
this.send("error", message, context);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
private send(level: string, message: string, context?: LogContext) {
|
|
207
|
+
fetch("/api/logs", {
|
|
208
|
+
method: "POST",
|
|
209
|
+
body: JSON.stringify({ level, category: this.category, message, context }),
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Custom factory
|
|
215
|
+
class RemoteLoggerFactory implements LoggerFactory {
|
|
216
|
+
createLogger(category: string): Logger {
|
|
217
|
+
return new RemoteLogger(category);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Use custom factory
|
|
222
|
+
setLoggerFactory(new RemoteLoggerFactory());
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## Design Decisions
|
|
228
|
+
|
|
229
|
+
### Why Lazy Initialization?
|
|
230
|
+
|
|
231
|
+
AgentX packages create loggers at module level:
|
|
232
|
+
|
|
233
|
+
```typescript
|
|
234
|
+
// engine/AgentEngine.ts
|
|
235
|
+
import { createLogger } from "@agentxjs/common";
|
|
236
|
+
|
|
237
|
+
const logger = createLogger("engine/AgentEngine");
|
|
238
|
+
|
|
239
|
+
export class AgentEngine {
|
|
240
|
+
// ...
|
|
241
|
+
}
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
If loggers required immediate factory, this would fail before Runtime initializes. Lazy initialization solves this.
|
|
245
|
+
|
|
246
|
+
### Why Pluggable?
|
|
247
|
+
|
|
248
|
+
Different environments need different logging:
|
|
249
|
+
|
|
250
|
+
- **Development** - Console with colors
|
|
251
|
+
- **Production** - Structured JSON logs
|
|
252
|
+
- **Browser** - Send to analytics service
|
|
253
|
+
- **Testing** - Silent or mock
|
|
254
|
+
|
|
255
|
+
Runtime provides the logger factory appropriate for the environment.
|
|
256
|
+
|
|
257
|
+
### Why Not Use console.log Directly?
|
|
258
|
+
|
|
259
|
+
Direct `console.log` usage causes:
|
|
260
|
+
|
|
261
|
+
1. **No control** - Can't disable/filter logs
|
|
262
|
+
2. **No structure** - Context is hard to parse
|
|
263
|
+
3. **No routing** - Can't send logs to services
|
|
264
|
+
|
|
265
|
+
The logger facade solves all these issues.
|
|
266
|
+
|
|
267
|
+
---
|
|
268
|
+
|
|
269
|
+
## Package Dependencies
|
|
270
|
+
|
|
271
|
+
```text
|
|
272
|
+
@agentxjs/types Type definitions
|
|
273
|
+
↑
|
|
274
|
+
@agentxjs/common This package (logger facade)
|
|
275
|
+
↑
|
|
276
|
+
@agentxjs/agent Uses logger
|
|
277
|
+
↑
|
|
278
|
+
@agentxjs/runtime Uses logger + provides factory
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
---
|
|
282
|
+
|
|
283
|
+
## Related Packages
|
|
284
|
+
|
|
285
|
+
- **[@agentxjs/types](../types)** - Type definitions
|
|
286
|
+
- **[@agentxjs/agent](../agent)** - Agent runtime (uses logger)
|
|
287
|
+
- **[@agentxjs/runtime](../runtime)** - Runtime implementation (provides factory)
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
## License
|
|
292
|
+
|
|
293
|
+
MIT
|
package/dist/index.cjs
CHANGED
|
@@ -23,16 +23,13 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
23
23
|
var index_exports = {};
|
|
24
24
|
__export(index_exports, {
|
|
25
25
|
ConsoleLogger: () => ConsoleLogger,
|
|
26
|
-
LogLevel: () => import_types3.LogLevel,
|
|
27
26
|
LoggerFactoryImpl: () => LoggerFactoryImpl,
|
|
28
27
|
createLogger: () => createLogger,
|
|
29
28
|
setLoggerFactory: () => setLoggerFactory
|
|
30
29
|
});
|
|
31
30
|
module.exports = __toCommonJS(index_exports);
|
|
32
|
-
var import_types3 = require("@agentxjs/types");
|
|
33
31
|
|
|
34
32
|
// src/logger/ConsoleLogger.ts
|
|
35
|
-
var import_types = require("@agentxjs/types");
|
|
36
33
|
var _ConsoleLogger = class _ConsoleLogger {
|
|
37
34
|
constructor(name, options = {}) {
|
|
38
35
|
__publicField(this, "name");
|
|
@@ -40,7 +37,7 @@ var _ConsoleLogger = class _ConsoleLogger {
|
|
|
40
37
|
__publicField(this, "colors");
|
|
41
38
|
__publicField(this, "timestamps");
|
|
42
39
|
this.name = name;
|
|
43
|
-
this.level = options.level ??
|
|
40
|
+
this.level = options.level ?? "info";
|
|
44
41
|
this.colors = options.colors ?? this.isNodeEnvironment();
|
|
45
42
|
this.timestamps = options.timestamps ?? true;
|
|
46
43
|
}
|
|
@@ -69,16 +66,26 @@ var _ConsoleLogger = class _ConsoleLogger {
|
|
|
69
66
|
}
|
|
70
67
|
}
|
|
71
68
|
isDebugEnabled() {
|
|
72
|
-
return this.level <=
|
|
69
|
+
return this.getLevelValue(this.level) <= this.getLevelValue("debug");
|
|
73
70
|
}
|
|
74
71
|
isInfoEnabled() {
|
|
75
|
-
return this.level <=
|
|
72
|
+
return this.getLevelValue(this.level) <= this.getLevelValue("info");
|
|
76
73
|
}
|
|
77
74
|
isWarnEnabled() {
|
|
78
|
-
return this.level <=
|
|
75
|
+
return this.getLevelValue(this.level) <= this.getLevelValue("warn");
|
|
79
76
|
}
|
|
80
77
|
isErrorEnabled() {
|
|
81
|
-
return this.level <=
|
|
78
|
+
return this.getLevelValue(this.level) <= this.getLevelValue("error");
|
|
79
|
+
}
|
|
80
|
+
getLevelValue(level) {
|
|
81
|
+
const levels = {
|
|
82
|
+
debug: 0,
|
|
83
|
+
info: 1,
|
|
84
|
+
warn: 2,
|
|
85
|
+
error: 3,
|
|
86
|
+
silent: 4
|
|
87
|
+
};
|
|
88
|
+
return levels[level];
|
|
82
89
|
}
|
|
83
90
|
log(level, message, context) {
|
|
84
91
|
const parts = [];
|
|
@@ -129,8 +136,8 @@ __publicField(_ConsoleLogger, "COLORS", {
|
|
|
129
136
|
var ConsoleLogger = _ConsoleLogger;
|
|
130
137
|
|
|
131
138
|
// src/logger/LoggerFactoryImpl.ts
|
|
132
|
-
var import_types2 = require("@agentxjs/types");
|
|
133
139
|
var externalFactory = null;
|
|
140
|
+
var factoryVersion = 0;
|
|
134
141
|
var LoggerFactoryImpl = class {
|
|
135
142
|
static getLogger(nameOrClass) {
|
|
136
143
|
const name = typeof nameOrClass === "string" ? nameOrClass : nameOrClass.name;
|
|
@@ -146,20 +153,23 @@ var LoggerFactoryImpl = class {
|
|
|
146
153
|
}
|
|
147
154
|
static reset() {
|
|
148
155
|
this.loggers.clear();
|
|
149
|
-
this.config = { defaultLevel:
|
|
156
|
+
this.config = { defaultLevel: "info" };
|
|
150
157
|
externalFactory = null;
|
|
158
|
+
factoryVersion++;
|
|
151
159
|
}
|
|
152
160
|
static createLazyLogger(name) {
|
|
153
161
|
let realLogger = null;
|
|
162
|
+
let loggerVersion = -1;
|
|
154
163
|
const getRealLogger = () => {
|
|
155
|
-
if (!realLogger) {
|
|
164
|
+
if (!realLogger || loggerVersion !== factoryVersion) {
|
|
156
165
|
realLogger = this.createLogger(name);
|
|
166
|
+
loggerVersion = factoryVersion;
|
|
157
167
|
}
|
|
158
168
|
return realLogger;
|
|
159
169
|
};
|
|
160
170
|
return {
|
|
161
171
|
name,
|
|
162
|
-
level: this.config.defaultLevel ||
|
|
172
|
+
level: this.config.defaultLevel || "info",
|
|
163
173
|
debug: (message, context) => getRealLogger().debug(message, context),
|
|
164
174
|
info: (message, context) => getRealLogger().info(message, context),
|
|
165
175
|
warn: (message, context) => getRealLogger().warn(message, context),
|
|
@@ -185,7 +195,7 @@ var LoggerFactoryImpl = class {
|
|
|
185
195
|
};
|
|
186
196
|
__publicField(LoggerFactoryImpl, "loggers", /* @__PURE__ */ new Map());
|
|
187
197
|
__publicField(LoggerFactoryImpl, "config", {
|
|
188
|
-
defaultLevel:
|
|
198
|
+
defaultLevel: "info"
|
|
189
199
|
});
|
|
190
200
|
function setLoggerFactory(factory) {
|
|
191
201
|
externalFactory = factory;
|
|
@@ -198,7 +208,6 @@ function createLogger(name) {
|
|
|
198
208
|
// Annotate the CommonJS export names for ESM import in node:
|
|
199
209
|
0 && (module.exports = {
|
|
200
210
|
ConsoleLogger,
|
|
201
|
-
LogLevel,
|
|
202
211
|
LoggerFactoryImpl,
|
|
203
212
|
createLogger,
|
|
204
213
|
setLoggerFactory
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/logger/ConsoleLogger.ts","../src/logger/LoggerFactoryImpl.ts"],"sourcesContent":["/**\n * AgentX Common\n *\n * Internal common infrastructure for AgentX platform.\n * Provides shared utilities used by internal packages (engine, agent, etc.)\n *\n * ## Module Structure\n *\n * | Module | Purpose |\n * |----------|----------------------------------------------|\n * | logger/ | Lazy-initialized logging with pluggable backends |\n *\n * ## Design Principles\n *\n * 1. **Internal Use**: For AgentX internal packages only\n * 2. **Lazy Initialization**: Safe to use at module level\n * 3. **Pluggable**: External implementations via Runtime injection\n *\n * @example\n * ```typescript\n * import { createLogger } from \"@agentxjs/common\";\n *\n * const logger = createLogger(\"engine/AgentEngine\");\n * logger.info(\"Hello\");\n * ```\n *\n * @packageDocumentation\n */\n\n// Re-export types from agentx-types for convenience\nexport { LogLevel } from \"@agentxjs/types\";\nexport type { Logger, LogContext, LoggerFactory } from \"@agentxjs/types\";\n\n// Logger implementation\nexport {\n ConsoleLogger,\n type ConsoleLoggerOptions,\n LoggerFactoryImpl,\n type LoggerFactoryConfig,\n setLoggerFactory,\n createLogger,\n} from \"./logger\";\n","/**\n * ConsoleLogger - Default logger implementation\n *\n * Simple console-based logger with color support.\n * Used as fallback when no custom LoggerFactory is provided.\n */\n\nimport type { Logger, LogContext, LogLevel } from \"@agentxjs/types\";\nimport { LogLevel as LogLevelEnum } from \"@agentxjs/types\";\n\nexport interface ConsoleLoggerOptions {\n level?: LogLevel;\n colors?: boolean;\n timestamps?: boolean;\n}\n\nexport class ConsoleLogger implements Logger {\n readonly name: string;\n readonly level: LogLevel;\n private readonly colors: boolean;\n private readonly timestamps: boolean;\n\n private static readonly COLORS = {\n DEBUG: \"\\x1b[36m\",\n INFO: \"\\x1b[32m\",\n WARN: \"\\x1b[33m\",\n ERROR: \"\\x1b[31m\",\n RESET: \"\\x1b[0m\",\n };\n\n constructor(name: string, options: ConsoleLoggerOptions = {}) {\n this.name = name;\n this.level = options.level ?? LogLevelEnum.INFO;\n this.colors = options.colors ?? this.isNodeEnvironment();\n this.timestamps = options.timestamps ?? true;\n }\n\n debug(message: string, context?: LogContext): void {\n if (this.isDebugEnabled()) {\n this.log(\"DEBUG\", message, context);\n }\n }\n\n info(message: string, context?: LogContext): void {\n if (this.isInfoEnabled()) {\n this.log(\"INFO\", message, context);\n }\n }\n\n warn(message: string, context?: LogContext): void {\n if (this.isWarnEnabled()) {\n this.log(\"WARN\", message, context);\n }\n }\n\n error(message: string | Error, context?: LogContext): void {\n if (this.isErrorEnabled()) {\n if (message instanceof Error) {\n this.log(\"ERROR\", message.message, { ...context, stack: message.stack });\n } else {\n this.log(\"ERROR\", message, context);\n }\n }\n }\n\n isDebugEnabled(): boolean {\n return this.level <= LogLevelEnum.DEBUG;\n }\n\n isInfoEnabled(): boolean {\n return this.level <= LogLevelEnum.INFO;\n }\n\n isWarnEnabled(): boolean {\n return this.level <= LogLevelEnum.WARN;\n }\n\n isErrorEnabled(): boolean {\n return this.level <= LogLevelEnum.ERROR;\n }\n\n private log(level: string, message: string, context?: LogContext): void {\n const parts: string[] = [];\n\n if (this.timestamps) {\n parts.push(new Date().toISOString());\n }\n\n if (this.colors) {\n const color = ConsoleLogger.COLORS[level as keyof typeof ConsoleLogger.COLORS];\n parts.push(`${color}${level.padEnd(5)}${ConsoleLogger.COLORS.RESET}`);\n } else {\n parts.push(level.padEnd(5));\n }\n\n parts.push(`[${this.name}]`);\n parts.push(message);\n\n const logLine = parts.join(\" \");\n const consoleMethod = this.getConsoleMethod(level);\n\n if (context && Object.keys(context).length > 0) {\n consoleMethod(logLine, context);\n } else {\n consoleMethod(logLine);\n }\n }\n\n private getConsoleMethod(level: string): (...args: unknown[]) => void {\n switch (level) {\n case \"DEBUG\":\n return console.debug.bind(console);\n case \"INFO\":\n return console.info.bind(console);\n case \"WARN\":\n return console.warn.bind(console);\n case \"ERROR\":\n return console.error.bind(console);\n default:\n return console.log.bind(console);\n }\n }\n\n private isNodeEnvironment(): boolean {\n return typeof process !== \"undefined\" && process.versions?.node !== undefined;\n }\n}\n","/**\n * LoggerFactoryImpl - Central factory for creating logger instances\n *\n * Implements lazy initialization pattern:\n * - createLogger() can be called at module level (before config)\n * - Real logger is created on first use\n * - External LoggerFactory can be injected via Runtime\n */\n\nimport type { Logger, LoggerFactory, LogContext, LogLevel } from \"@agentxjs/types\";\nimport { LogLevel as LogLevelEnum } from \"@agentxjs/types\";\nimport { ConsoleLogger, type ConsoleLoggerOptions } from \"./ConsoleLogger\";\n\n// External factory injected via Runtime\nlet externalFactory: LoggerFactory | null = null;\n\nexport interface LoggerFactoryConfig {\n defaultImplementation?: (name: string) => Logger;\n defaultLevel?: LogLevel;\n consoleOptions?: Omit<ConsoleLoggerOptions, \"level\">;\n}\n\n/**\n * Internal LoggerFactory implementation\n *\n * Uses lazy proxy pattern to allow module-level createLogger() calls.\n */\nexport class LoggerFactoryImpl {\n private static loggers: Map<string, Logger> = new Map();\n private static config: LoggerFactoryConfig = {\n defaultLevel: LogLevelEnum.INFO,\n };\n\n static getLogger(nameOrClass: string | (new (...args: unknown[]) => unknown)): Logger {\n const name = typeof nameOrClass === \"string\" ? nameOrClass : nameOrClass.name;\n\n if (this.loggers.has(name)) {\n return this.loggers.get(name)!;\n }\n\n const lazyLogger = this.createLazyLogger(name);\n this.loggers.set(name, lazyLogger);\n return lazyLogger;\n }\n\n static configure(config: LoggerFactoryConfig): void {\n this.config = { ...this.config, ...config };\n }\n\n static reset(): void {\n this.loggers.clear();\n this.config = { defaultLevel: LogLevelEnum.INFO };\n externalFactory = null;\n }\n\n private static createLazyLogger(name: string): Logger {\n let realLogger: Logger | null = null;\n\n const getRealLogger = (): Logger => {\n if (!realLogger) {\n realLogger = this.createLogger(name);\n }\n return realLogger;\n };\n\n return {\n name,\n level: this.config.defaultLevel || LogLevelEnum.INFO,\n debug: (message: string, context?: LogContext) => getRealLogger().debug(message, context),\n info: (message: string, context?: LogContext) => getRealLogger().info(message, context),\n warn: (message: string, context?: LogContext) => getRealLogger().warn(message, context),\n error: (message: string | Error, context?: LogContext) =>\n getRealLogger().error(message, context),\n isDebugEnabled: () => getRealLogger().isDebugEnabled(),\n isInfoEnabled: () => getRealLogger().isInfoEnabled(),\n isWarnEnabled: () => getRealLogger().isWarnEnabled(),\n isErrorEnabled: () => getRealLogger().isErrorEnabled(),\n };\n }\n\n private static createLogger(name: string): Logger {\n if (externalFactory) {\n return externalFactory.getLogger(name);\n }\n\n if (this.config.defaultImplementation) {\n return this.config.defaultImplementation(name);\n }\n\n return new ConsoleLogger(name, {\n level: this.config.defaultLevel,\n ...this.config.consoleOptions,\n });\n }\n}\n\n/**\n * Set external LoggerFactory (called by Runtime initialization)\n */\nexport function setLoggerFactory(factory: LoggerFactory): void {\n externalFactory = factory;\n LoggerFactoryImpl.reset();\n externalFactory = factory;\n}\n\n/**\n * Create a logger instance\n *\n * Safe to call at module level before Runtime is configured.\n * Uses lazy initialization - actual logger is created on first use.\n *\n * @param name - Logger name (hierarchical, e.g., \"engine/AgentEngine\")\n * @returns Logger instance (lazy proxy)\n */\nexport function createLogger(name: string): Logger {\n return LoggerFactoryImpl.getLogger(name);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA8BA,IAAAA,gBAAyB;;;ACtBzB,mBAAyC;AAQlC,IAAM,iBAAN,MAAM,eAAgC;AAAA,EAc3C,YAAY,MAAc,UAAgC,CAAC,GAAG;AAb9D,wBAAS;AACT,wBAAS;AACT,wBAAiB;AACjB,wBAAiB;AAWf,SAAK,OAAO;AACZ,SAAK,QAAQ,QAAQ,SAAS,aAAAC,SAAa;AAC3C,SAAK,SAAS,QAAQ,UAAU,KAAK,kBAAkB;AACvD,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AAA,MACnB,GAAG,KAAK,OAAO;AAAA,IACjB,CAAC;AAAA,EACH;AACF;AAlEE,cADW,mBACI,WAA+B,oBAAI,IAAI;AACtD,cAFW,mBAEI,UAA8B;AAAA,EAC3C,cAAc,cAAAA,SAAa;AAC7B;AAoEK,SAAS,iBAAiB,SAA8B;AAC7D,oBAAkB;AAClB,oBAAkB,MAAM;AACxB,oBAAkB;AACpB;AAWO,SAAS,aAAa,MAAsB;AACjD,SAAO,kBAAkB,UAAU,IAAI;AACzC;","names":["import_types","LogLevelEnum","import_types","LogLevelEnum"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/logger/ConsoleLogger.ts","../src/logger/LoggerFactoryImpl.ts"],"sourcesContent":["/**\n * AgentX Common\n *\n * Internal common infrastructure for AgentX platform.\n * Provides shared utilities used by internal packages (engine, agent, etc.)\n *\n * ## Module Structure\n *\n * | Module | Purpose |\n * |----------|----------------------------------------------|\n * | logger/ | Lazy-initialized logging with pluggable backends |\n *\n * ## Design Principles\n *\n * 1. **Internal Use**: For AgentX internal packages only\n * 2. **Lazy Initialization**: Safe to use at module level\n * 3. **Pluggable**: External implementations via Runtime injection\n *\n * @example\n * ```typescript\n * import { createLogger } from \"@agentxjs/common\";\n *\n * const logger = createLogger(\"engine/AgentEngine\");\n * logger.info(\"Hello\");\n * ```\n *\n * @packageDocumentation\n */\n\n// Re-export types from agentx-types for convenience\nexport type { LogLevel, Logger, LogContext, LoggerFactory } from \"@agentxjs/types\";\n\n// Logger implementation\nexport {\n ConsoleLogger,\n type ConsoleLoggerOptions,\n LoggerFactoryImpl,\n type LoggerFactoryConfig,\n setLoggerFactory,\n createLogger,\n} from \"./logger\";\n","/**\n * ConsoleLogger - Default logger implementation\n *\n * Simple console-based logger with color support.\n * Used as fallback when no custom LoggerFactory is provided.\n */\n\nimport type { Logger, LogContext, LogLevel } from \"@agentxjs/types\";\n\nexport interface ConsoleLoggerOptions {\n level?: LogLevel;\n colors?: boolean;\n timestamps?: boolean;\n}\n\nexport class ConsoleLogger implements Logger {\n readonly name: string;\n readonly level: LogLevel;\n private readonly colors: boolean;\n private readonly timestamps: boolean;\n\n private static readonly COLORS = {\n DEBUG: \"\\x1b[36m\",\n INFO: \"\\x1b[32m\",\n WARN: \"\\x1b[33m\",\n ERROR: \"\\x1b[31m\",\n RESET: \"\\x1b[0m\",\n };\n\n constructor(name: string, options: ConsoleLoggerOptions = {}) {\n this.name = name;\n this.level = options.level ?? \"info\";\n this.colors = options.colors ?? this.isNodeEnvironment();\n this.timestamps = options.timestamps ?? true;\n }\n\n debug(message: string, context?: LogContext): void {\n if (this.isDebugEnabled()) {\n this.log(\"DEBUG\", message, context);\n }\n }\n\n info(message: string, context?: LogContext): void {\n if (this.isInfoEnabled()) {\n this.log(\"INFO\", message, context);\n }\n }\n\n warn(message: string, context?: LogContext): void {\n if (this.isWarnEnabled()) {\n this.log(\"WARN\", message, context);\n }\n }\n\n error(message: string | Error, context?: LogContext): void {\n if (this.isErrorEnabled()) {\n if (message instanceof Error) {\n this.log(\"ERROR\", message.message, { ...context, stack: message.stack });\n } else {\n this.log(\"ERROR\", message, context);\n }\n }\n }\n\n isDebugEnabled(): boolean {\n return this.getLevelValue(this.level) <= this.getLevelValue(\"debug\");\n }\n\n isInfoEnabled(): boolean {\n return this.getLevelValue(this.level) <= this.getLevelValue(\"info\");\n }\n\n isWarnEnabled(): boolean {\n return this.getLevelValue(this.level) <= this.getLevelValue(\"warn\");\n }\n\n isErrorEnabled(): boolean {\n return this.getLevelValue(this.level) <= this.getLevelValue(\"error\");\n }\n\n private getLevelValue(level: LogLevel): number {\n const levels: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n silent: 4,\n };\n return levels[level];\n }\n\n private log(level: string, message: string, context?: LogContext): void {\n const parts: string[] = [];\n\n if (this.timestamps) {\n parts.push(new Date().toISOString());\n }\n\n if (this.colors) {\n const color = ConsoleLogger.COLORS[level as keyof typeof ConsoleLogger.COLORS];\n parts.push(`${color}${level.padEnd(5)}${ConsoleLogger.COLORS.RESET}`);\n } else {\n parts.push(level.padEnd(5));\n }\n\n parts.push(`[${this.name}]`);\n parts.push(message);\n\n const logLine = parts.join(\" \");\n const consoleMethod = this.getConsoleMethod(level);\n\n if (context && Object.keys(context).length > 0) {\n consoleMethod(logLine, context);\n } else {\n consoleMethod(logLine);\n }\n }\n\n private getConsoleMethod(level: string): (...args: unknown[]) => void {\n switch (level) {\n case \"DEBUG\":\n return console.debug.bind(console);\n case \"INFO\":\n return console.info.bind(console);\n case \"WARN\":\n return console.warn.bind(console);\n case \"ERROR\":\n return console.error.bind(console);\n default:\n return console.log.bind(console);\n }\n }\n\n private isNodeEnvironment(): boolean {\n return typeof process !== \"undefined\" && process.versions?.node !== undefined;\n }\n}\n","/**\n * LoggerFactoryImpl - Central factory for creating logger instances\n *\n * Implements lazy initialization pattern:\n * - createLogger() can be called at module level (before config)\n * - Real logger is created on first use\n * - External LoggerFactory can be injected via Runtime\n */\n\nimport type { Logger, LoggerFactory, LogContext, LogLevel } from \"@agentxjs/types\";\nimport { ConsoleLogger, type ConsoleLoggerOptions } from \"./ConsoleLogger\";\n\n// External factory injected via Runtime\nlet externalFactory: LoggerFactory | null = null;\n\n// Version counter to invalidate cached real loggers\nlet factoryVersion = 0;\n\nexport interface LoggerFactoryConfig {\n defaultImplementation?: (name: string) => Logger;\n defaultLevel?: LogLevel;\n consoleOptions?: Omit<ConsoleLoggerOptions, \"level\">;\n}\n\n/**\n * Internal LoggerFactory implementation\n *\n * Uses lazy proxy pattern to allow module-level createLogger() calls.\n */\nexport class LoggerFactoryImpl {\n private static loggers: Map<string, Logger> = new Map();\n private static config: LoggerFactoryConfig = {\n defaultLevel: \"info\",\n };\n\n static getLogger(nameOrClass: string | (new (...args: unknown[]) => unknown)): Logger {\n const name = typeof nameOrClass === \"string\" ? nameOrClass : nameOrClass.name;\n\n if (this.loggers.has(name)) {\n return this.loggers.get(name)!;\n }\n\n const lazyLogger = this.createLazyLogger(name);\n this.loggers.set(name, lazyLogger);\n return lazyLogger;\n }\n\n static configure(config: LoggerFactoryConfig): void {\n this.config = { ...this.config, ...config };\n }\n\n static reset(): void {\n this.loggers.clear();\n this.config = { defaultLevel: \"info\" };\n externalFactory = null;\n factoryVersion++; // Invalidate all cached real loggers\n }\n\n private static createLazyLogger(name: string): Logger {\n let realLogger: Logger | null = null;\n let loggerVersion = -1; // Track which factory version created this logger\n\n const getRealLogger = (): Logger => {\n // Recreate logger if factory version changed (setLoggerFactory was called)\n if (!realLogger || loggerVersion !== factoryVersion) {\n realLogger = this.createLogger(name);\n loggerVersion = factoryVersion;\n }\n return realLogger;\n };\n\n return {\n name,\n level: this.config.defaultLevel || \"info\",\n debug: (message: string, context?: LogContext) => getRealLogger().debug(message, context),\n info: (message: string, context?: LogContext) => getRealLogger().info(message, context),\n warn: (message: string, context?: LogContext) => getRealLogger().warn(message, context),\n error: (message: string | Error, context?: LogContext) =>\n getRealLogger().error(message, context),\n isDebugEnabled: () => getRealLogger().isDebugEnabled(),\n isInfoEnabled: () => getRealLogger().isInfoEnabled(),\n isWarnEnabled: () => getRealLogger().isWarnEnabled(),\n isErrorEnabled: () => getRealLogger().isErrorEnabled(),\n };\n }\n\n private static createLogger(name: string): Logger {\n if (externalFactory) {\n return externalFactory.getLogger(name);\n }\n\n if (this.config.defaultImplementation) {\n return this.config.defaultImplementation(name);\n }\n\n return new ConsoleLogger(name, {\n level: this.config.defaultLevel,\n ...this.config.consoleOptions,\n });\n }\n}\n\n/**\n * Set external LoggerFactory (called by Runtime initialization)\n */\nexport function setLoggerFactory(factory: LoggerFactory): void {\n externalFactory = factory;\n LoggerFactoryImpl.reset();\n externalFactory = factory;\n}\n\n/**\n * Create a logger instance\n *\n * Safe to call at module level before Runtime is configured.\n * Uses lazy initialization - actual logger is created on first use.\n *\n * @param name - Logger name (hierarchical, e.g., \"engine/AgentEngine\")\n * @returns Logger instance (lazy proxy)\n */\nexport function createLogger(name: string): Logger {\n return LoggerF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AA,MACxF,MAAM,CAAC,SAAiB,YAAyB,cAAc,EAAE,KAAK,SAAS,OAAO;AAAA,MACtF,MAAM,CAAC,SAAiB,YAAyB,cAAc,EAAE,KAAK,SAAS,OAAO;AAAA,MACtF,OAAO,CAAC,SAAyB,YAC/B,cAAc,EAAE,MAAM,SAAS,OAAO;AAAA,MACxC,gBAAgB,MAAM,cAAc,EAAE,eAAe;AAAA,MACrD,eAAe,MAAM,cAAc,EAAE,cAAc;AAAA,MACnD,eAAe,MAAM,cAAc,EAAE,cAAc;AAAA,MACnD,gBAAgB,MAAM,cAAc,EAAE,eAAe;AAAA,IACvD;AAAA,EACF;AAAA,EAEA,OAAe,aAAa,MAAsB;AAChD,QAAI,iBAAiB;AACnB,aAAO,gBAAgB,UAAU,IAAI;AAAA,IACvC;AAEA,QAAI,KAAK,OAAO,uBAAuB;AACrC,aAAO,KAAK,OAAO,sBAAsB,IAAI;AAAA,IAC/C;AAEA,WAAO,IAAI,cAAc,MAAM;AAAA,MAC7B,OAAO,KAAK,OAAO;AAAA,MACnB,GAAG,KAAK,OAAO;AAAA,IACjB,CAAC;AAAA,EACH;AACF;AAtEE,cADW,mBACI,WAA+B,oBAAI,IAAI;AACtD,cAFW,mBAEI,UAA8B;AAAA,EAC3C,cAAc;AAChB;AAwEK,SAAS,iBAAiB,SAA8B;AAC7D,oBAAkB;AAClB,oBAAkB,MAAM;AACxB,oBAAkB;AACpB;AAWO,SAAS,aAAa,MAAsB;AACjD,SAAO,kBAAkB,UAAU,IAAI;AACzC;","names":[]}
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -2,11 +2,7 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
4
|
|
|
5
|
-
// src/index.ts
|
|
6
|
-
import { LogLevel } from "@agentxjs/types";
|
|
7
|
-
|
|
8
5
|
// src/logger/ConsoleLogger.ts
|
|
9
|
-
import { LogLevel as LogLevelEnum } from "@agentxjs/types";
|
|
10
6
|
var _ConsoleLogger = class _ConsoleLogger {
|
|
11
7
|
constructor(name, options = {}) {
|
|
12
8
|
__publicField(this, "name");
|
|
@@ -14,7 +10,7 @@ var _ConsoleLogger = class _ConsoleLogger {
|
|
|
14
10
|
__publicField(this, "colors");
|
|
15
11
|
__publicField(this, "timestamps");
|
|
16
12
|
this.name = name;
|
|
17
|
-
this.level = options.level ??
|
|
13
|
+
this.level = options.level ?? "info";
|
|
18
14
|
this.colors = options.colors ?? this.isNodeEnvironment();
|
|
19
15
|
this.timestamps = options.timestamps ?? true;
|
|
20
16
|
}
|
|
@@ -43,16 +39,26 @@ var _ConsoleLogger = class _ConsoleLogger {
|
|
|
43
39
|
}
|
|
44
40
|
}
|
|
45
41
|
isDebugEnabled() {
|
|
46
|
-
return this.level <=
|
|
42
|
+
return this.getLevelValue(this.level) <= this.getLevelValue("debug");
|
|
47
43
|
}
|
|
48
44
|
isInfoEnabled() {
|
|
49
|
-
return this.level <=
|
|
45
|
+
return this.getLevelValue(this.level) <= this.getLevelValue("info");
|
|
50
46
|
}
|
|
51
47
|
isWarnEnabled() {
|
|
52
|
-
return this.level <=
|
|
48
|
+
return this.getLevelValue(this.level) <= this.getLevelValue("warn");
|
|
53
49
|
}
|
|
54
50
|
isErrorEnabled() {
|
|
55
|
-
return this.level <=
|
|
51
|
+
return this.getLevelValue(this.level) <= this.getLevelValue("error");
|
|
52
|
+
}
|
|
53
|
+
getLevelValue(level) {
|
|
54
|
+
const levels = {
|
|
55
|
+
debug: 0,
|
|
56
|
+
info: 1,
|
|
57
|
+
warn: 2,
|
|
58
|
+
error: 3,
|
|
59
|
+
silent: 4
|
|
60
|
+
};
|
|
61
|
+
return levels[level];
|
|
56
62
|
}
|
|
57
63
|
log(level, message, context) {
|
|
58
64
|
const parts = [];
|
|
@@ -103,8 +109,8 @@ __publicField(_ConsoleLogger, "COLORS", {
|
|
|
103
109
|
var ConsoleLogger = _ConsoleLogger;
|
|
104
110
|
|
|
105
111
|
// src/logger/LoggerFactoryImpl.ts
|
|
106
|
-
import { LogLevel as LogLevelEnum2 } from "@agentxjs/types";
|
|
107
112
|
var externalFactory = null;
|
|
113
|
+
var factoryVersion = 0;
|
|
108
114
|
var LoggerFactoryImpl = class {
|
|
109
115
|
static getLogger(nameOrClass) {
|
|
110
116
|
const name = typeof nameOrClass === "string" ? nameOrClass : nameOrClass.name;
|
|
@@ -120,20 +126,23 @@ var LoggerFactoryImpl = class {
|
|
|
120
126
|
}
|
|
121
127
|
static reset() {
|
|
122
128
|
this.loggers.clear();
|
|
123
|
-
this.config = { defaultLevel:
|
|
129
|
+
this.config = { defaultLevel: "info" };
|
|
124
130
|
externalFactory = null;
|
|
131
|
+
factoryVersion++;
|
|
125
132
|
}
|
|
126
133
|
static createLazyLogger(name) {
|
|
127
134
|
let realLogger = null;
|
|
135
|
+
let loggerVersion = -1;
|
|
128
136
|
const getRealLogger = () => {
|
|
129
|
-
if (!realLogger) {
|
|
137
|
+
if (!realLogger || loggerVersion !== factoryVersion) {
|
|
130
138
|
realLogger = this.createLogger(name);
|
|
139
|
+
loggerVersion = factoryVersion;
|
|
131
140
|
}
|
|
132
141
|
return realLogger;
|
|
133
142
|
};
|
|
134
143
|
return {
|
|
135
144
|
name,
|
|
136
|
-
level: this.config.defaultLevel ||
|
|
145
|
+
level: this.config.defaultLevel || "info",
|
|
137
146
|
debug: (message, context) => getRealLogger().debug(message, context),
|
|
138
147
|
info: (message, context) => getRealLogger().info(message, context),
|
|
139
148
|
warn: (message, context) => getRealLogger().warn(message, context),
|
|
@@ -159,7 +168,7 @@ var LoggerFactoryImpl = class {
|
|
|
159
168
|
};
|
|
160
169
|
__publicField(LoggerFactoryImpl, "loggers", /* @__PURE__ */ new Map());
|
|
161
170
|
__publicField(LoggerFactoryImpl, "config", {
|
|
162
|
-
defaultLevel:
|
|
171
|
+
defaultLevel: "info"
|
|
163
172
|
});
|
|
164
173
|
function setLoggerFactory(factory) {
|
|
165
174
|
externalFactory = factory;
|
|
@@ -171,7 +180,6 @@ function createLogger(name) {
|
|
|
171
180
|
}
|
|
172
181
|
export {
|
|
173
182
|
ConsoleLogger,
|
|
174
|
-
LogLevel,
|
|
175
183
|
LoggerFactoryImpl,
|
|
176
184
|
createLogger,
|
|
177
185
|
setLoggerFactory
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/logger/ConsoleLogger.ts","../src/logger/LoggerFactoryImpl.ts"],"sourcesContent":["/**\n * AgentX Common\n *\n * Internal common infrastructure for AgentX platform.\n * Provides shared utilities used by internal packages (engine, agent, etc.)\n *\n * ## Module Structure\n *\n * | Module | Purpose |\n * |----------|----------------------------------------------|\n * | logger/ | Lazy-initialized logging with pluggable backends |\n *\n * ## Design Principles\n *\n * 1. **Internal Use**: For AgentX internal packages only\n * 2. **Lazy Initialization**: Safe to use at module level\n * 3. **Pluggable**: External implementations via Runtime injection\n *\n * @example\n * ```typescript\n * import { createLogger } from \"@agentxjs/common\";\n *\n * const logger = createLogger(\"engine/AgentEngine\");\n * logger.info(\"Hello\");\n * ```\n *\n * @packageDocumentation\n */\n\n// Re-export types from agentx-types for convenience\nexport { LogLevel } from \"@agentxjs/types\";\nexport type { Logger, LogContext, LoggerFactory } from \"@agentxjs/types\";\n\n// Logger implementation\nexport {\n ConsoleLogger,\n type ConsoleLoggerOptions,\n LoggerFactoryImpl,\n type LoggerFactoryConfig,\n setLoggerFactory,\n createLogger,\n} from \"./logger\";\n","/**\n * ConsoleLogger - Default logger implementation\n *\n * Simple console-based logger with color support.\n * Used as fallback when no custom LoggerFactory is provided.\n */\n\nimport type { Logger, LogContext, LogLevel } from \"@agentxjs/types\";\nimport { LogLevel as LogLevelEnum } from \"@agentxjs/types\";\n\nexport interface ConsoleLoggerOptions {\n level?: LogLevel;\n colors?: boolean;\n timestamps?: boolean;\n}\n\nexport class ConsoleLogger implements Logger {\n readonly name: string;\n readonly level: LogLevel;\n private readonly colors: boolean;\n private readonly timestamps: boolean;\n\n private static readonly COLORS = {\n DEBUG: \"\\x1b[36m\",\n INFO: \"\\x1b[32m\",\n WARN: \"\\x1b[33m\",\n ERROR: \"\\x1b[31m\",\n RESET: \"\\x1b[0m\",\n };\n\n constructor(name: string, options: ConsoleLoggerOptions = {}) {\n this.name = name;\n this.level = options.level ?? LogLevelEnum.INFO;\n this.colors = options.colors ?? this.isNodeEnvironment();\n this.timestamps = options.timestamps ?? true;\n }\n\n debug(message: string, context?: LogContext): void {\n if (this.isDebugEnabled()) {\n this.log(\"DEBUG\", message, context);\n }\n }\n\n info(message: string, context?: LogContext): void {\n if (this.isInfoEnabled()) {\n this.log(\"INFO\", message, context);\n }\n }\n\n warn(message: string, context?: LogContext): void {\n if (this.isWarnEnabled()) {\n this.log(\"WARN\", message, context);\n }\n }\n\n error(message: string | Error, context?: LogContext): void {\n if (this.isErrorEnabled()) {\n if (message instanceof Error) {\n this.log(\"ERROR\", message.message, { ...context, stack: message.stack });\n } else {\n this.log(\"ERROR\", message, context);\n }\n }\n }\n\n isDebugEnabled(): boolean {\n return this.level <= LogLevelEnum.DEBUG;\n }\n\n isInfoEnabled(): boolean {\n return this.level <= LogLevelEnum.INFO;\n }\n\n isWarnEnabled(): boolean {\n return this.level <= LogLevelEnum.WARN;\n }\n\n isErrorEnabled(): boolean {\n return this.level <= LogLevelEnum.ERROR;\n }\n\n private log(level: string, message: string, context?: LogContext): void {\n const parts: string[] = [];\n\n if (this.timestamps) {\n parts.push(new Date().toISOString());\n }\n\n if (this.colors) {\n const color = ConsoleLogger.COLORS[level as keyof typeof ConsoleLogger.COLORS];\n parts.push(`${color}${level.padEnd(5)}${ConsoleLogger.COLORS.RESET}`);\n } else {\n parts.push(level.padEnd(5));\n }\n\n parts.push(`[${this.name}]`);\n parts.push(message);\n\n const logLine = parts.join(\" \");\n const consoleMethod = this.getConsoleMethod(level);\n\n if (context && Object.keys(context).length > 0) {\n consoleMethod(logLine, context);\n } else {\n consoleMethod(logLine);\n }\n }\n\n private getConsoleMethod(level: string): (...args: unknown[]) => void {\n switch (level) {\n case \"DEBUG\":\n return console.debug.bind(console);\n case \"INFO\":\n return console.info.bind(console);\n case \"WARN\":\n return console.warn.bind(console);\n case \"ERROR\":\n return console.error.bind(console);\n default:\n return console.log.bind(console);\n }\n }\n\n private isNodeEnvironment(): boolean {\n return typeof process !== \"undefined\" && process.versions?.node !== undefined;\n }\n}\n","/**\n * LoggerFactoryImpl - Central factory for creating logger instances\n *\n * Implements lazy initialization pattern:\n * - createLogger() can be called at module level (before config)\n * - Real logger is created on first use\n * - External LoggerFactory can be injected via Runtime\n */\n\nimport type { Logger, LoggerFactory, LogContext, LogLevel } from \"@agentxjs/types\";\nimport { LogLevel as LogLevelEnum } from \"@agentxjs/types\";\nimport { ConsoleLogger, type ConsoleLoggerOptions } from \"./ConsoleLogger\";\n\n// External factory injected via Runtime\nlet externalFactory: LoggerFactory | null = null;\n\nexport interface LoggerFactoryConfig {\n defaultImplementation?: (name: string) => Logger;\n defaultLevel?: LogLevel;\n consoleOptions?: Omit<ConsoleLoggerOptions, \"level\">;\n}\n\n/**\n * Internal LoggerFactory implementation\n *\n * Uses lazy proxy pattern to allow module-level createLogger() calls.\n */\nexport class LoggerFactoryImpl {\n private static loggers: Map<string, Logger> = new Map();\n private static config: LoggerFactoryConfig = {\n defaultLevel: LogLevelEnum.INFO,\n };\n\n static getLogger(nameOrClass: string | (new (...args: unknown[]) => unknown)): Logger {\n const name = typeof nameOrClass === \"string\" ? nameOrClass : nameOrClass.name;\n\n if (this.loggers.has(name)) {\n return this.loggers.get(name)!;\n }\n\n const lazyLogger = this.createLazyLogger(name);\n this.loggers.set(name, lazyLogger);\n return lazyLogger;\n }\n\n static configure(config: LoggerFactoryConfig): void {\n this.config = { ...this.config, ...config };\n }\n\n static reset(): void {\n this.loggers.clear();\n this.config = { defaultLevel: LogLevelEnum.INFO };\n externalFactory = null;\n }\n\n private static createLazyLogger(name: string): Logger {\n let realLogger: Logger | null = null;\n\n const getRealLogger = (): Logger => {\n if (!realLogger) {\n realLogger = this.createLogger(name);\n }\n return realLogger;\n };\n\n return {\n name,\n level: this.config.defaultLevel || LogLevelEnum.INFO,\n debug: (message: string, context?: LogContext) => getRealLogger().debug(message, context),\n info: (message: string, context?: LogContext) => getRealLogger().info(message, context),\n warn: (message: string, context?: LogContext) => getRealLogger().warn(message, context),\n error: (message: string | Error, context?: LogContext) =>\n getRealLogger().error(message, context),\n isDebugEnabled: () => getRealLogger().isDebugEnabled(),\n isInfoEnabled: () => getRealLogger().isInfoEnabled(),\n isWarnEnabled: () => getRealLogger().isWarnEnabled(),\n isErrorEnabled: () => getRealLogger().isErrorEnabled(),\n };\n }\n\n private static createLogger(name: string): Logger {\n if (externalFactory) {\n return externalFactory.getLogger(name);\n }\n\n if (this.config.defaultImplementation) {\n return this.config.defaultImplementation(name);\n }\n\n return new ConsoleLogger(name, {\n level: this.config.defaultLevel,\n ...this.config.consoleOptions,\n });\n }\n}\n\n/**\n * Set external LoggerFactory (called by Runtime initialization)\n */\nexport function setLoggerFactory(factory: LoggerFactory): void {\n externalFactory = factory;\n LoggerFactoryImpl.reset();\n externalFactory = factory;\n}\n\n/**\n * Create a logger instance\n *\n * Safe to call at module level before Runtime is configured.\n * Uses lazy initialization - actual logger is created on first use.\n *\n * @param name - Logger name (hierarchical, e.g., \"engine/AgentEngine\")\n * @returns Logger instance (lazy proxy)\n */\nexport function createLogger(name: string): Logger {\n return LoggerFactoryImpl.getLogger(name);\n}\n"],"mappings":";;;;;AA8BA,SAAS,gBAAgB;;;ACtBzB,SAAS,YAAY,oBAAoB;AAQlC,IAAM,iBAAN,MAAM,eAAgC;AAAA,EAc3C,YAAY,MAAc,UAAgC,CAAC,GAAG;AAb9D,w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AAuB;AACrC,aAAO,KAAK,OAAO,sBAAsB,IAAI;AAAA,IAC/C;AAEA,WAAO,IAAI,cAAc,MAAM;AAAA,MAC7B,OAAO,KAAK,OAAO;AAAA,MACnB,GAAG,KAAK,OAAO;AAAA,IACjB,CAAC;AAAA,EACH;AACF;AAlEE,cADW,mBACI,WAA+B,oBAAI,IAAI;AACtD,cAFW,mBAEI,UAA8B;AAAA,EAC3C,cAAcA,cAAa;AAC7B;AAoEK,SAAS,iBAAiB,SAA8B;AAC7D,oBAAkB;AAClB,oBAAkB,MAAM;AACxB,oBAAkB;AACpB;AAWO,SAAS,aAAa,MAAsB;AACjD,SAAO,kBAAkB,UAAU,IAAI;AACzC;","names":["LogLevelEnum","LogLevelEnum"]}
|
|
1
|
+
{"version":3,"sources":["../src/logger/ConsoleLogger.ts","../src/logger/LoggerFactoryImpl.ts"],"sourcesContent":["/**\n * ConsoleLogger - Default logger implementation\n *\n * Simple console-based logger with color support.\n * Used as fallback when no custom LoggerFactory is provided.\n */\n\nimport type { Logger, LogContext, LogLevel } from \"@agentxjs/types\";\n\nexport interface ConsoleLoggerOptions {\n level?: LogLevel;\n colors?: boolean;\n timestamps?: boolean;\n}\n\nexport class ConsoleLogger implements Logger {\n readonly name: string;\n readonly level: LogLevel;\n private readonly colors: boolean;\n private readonly timestamps: boolean;\n\n private static readonly COLORS = {\n DEBUG: \"\\x1b[36m\",\n INFO: \"\\x1b[32m\",\n WARN: \"\\x1b[33m\",\n ERROR: \"\\x1b[31m\",\n RESET: \"\\x1b[0m\",\n };\n\n constructor(name: string, options: ConsoleLoggerOptions = {}) {\n this.name = name;\n this.level = options.level ?? \"info\";\n this.colors = options.colors ?? this.isNodeEnvironment();\n this.timestamps = options.timestamps ?? true;\n }\n\n debug(message: string, context?: LogContext): void {\n if (this.isDebugEnabled()) {\n this.log(\"DEBUG\", message, context);\n }\n }\n\n info(message: string, context?: LogContext): void {\n if (this.isInfoEnabled()) {\n this.log(\"INFO\", message, context);\n }\n }\n\n warn(message: string, context?: LogContext): void {\n if (this.isWarnEnabled()) {\n this.log(\"WARN\", message, context);\n }\n }\n\n error(message: string | Error, context?: LogContext): void {\n if (this.isErrorEnabled()) {\n if (message instanceof Error) {\n this.log(\"ERROR\", message.message, { ...context, stack: message.stack });\n } else {\n this.log(\"ERROR\", message, context);\n }\n }\n }\n\n isDebugEnabled(): boolean {\n return this.getLevelValue(this.level) <= this.getLevelValue(\"debug\");\n }\n\n isInfoEnabled(): boolean {\n return this.getLevelValue(this.level) <= this.getLevelValue(\"info\");\n }\n\n isWarnEnabled(): boolean {\n return this.getLevelValue(this.level) <= this.getLevelValue(\"warn\");\n }\n\n isErrorEnabled(): boolean {\n return this.getLevelValue(this.level) <= this.getLevelValue(\"error\");\n }\n\n private getLevelValue(level: LogLevel): number {\n const levels: Record<LogLevel, number> = {\n debug: 0,\n info: 1,\n warn: 2,\n error: 3,\n silent: 4,\n };\n return levels[level];\n }\n\n private log(level: string, message: string, context?: LogContext): void {\n const parts: string[] = [];\n\n if (this.timestamps) {\n parts.push(new Date().toISOString());\n }\n\n if (this.colors) {\n const color = ConsoleLogger.COLORS[level as keyof typeof ConsoleLogger.COLORS];\n parts.push(`${color}${level.padEnd(5)}${ConsoleLogger.COLORS.RESET}`);\n } else {\n parts.push(level.padEnd(5));\n }\n\n parts.push(`[${this.name}]`);\n parts.push(message);\n\n const logLine = parts.join(\" \");\n const consoleMethod = this.getConsoleMethod(level);\n\n if (context && Object.keys(context).length > 0) {\n consoleMethod(logLine, context);\n } else {\n consoleMethod(logLine);\n }\n }\n\n private getConsoleMethod(level: string): (...args: unknown[]) => void {\n switch (level) {\n case \"DEBUG\":\n return console.debug.bind(console);\n case \"INFO\":\n return console.info.bind(console);\n case \"WARN\":\n return console.warn.bind(console);\n case \"ERROR\":\n return console.error.bind(console);\n default:\n return console.log.bind(console);\n }\n }\n\n private isNodeEnvironment(): boolean {\n return typeof process !== \"undefined\" && process.versions?.node !== undefined;\n }\n}\n","/**\n * LoggerFactoryImpl - Central factory for creating logger instances\n *\n * Implements lazy initialization pattern:\n * - createLogger() can be called at module level (before config)\n * - Real logger is created on first use\n * - External LoggerFactory can be injected via Runtime\n */\n\nimport type { Logger, LoggerFactory, LogContext, LogLevel } from \"@agentxjs/types\";\nimport { ConsoleLogger, type ConsoleLoggerOptions } from \"./ConsoleLogger\";\n\n// External factory injected via Runtime\nlet externalFactory: LoggerFactory | null = null;\n\n// Version counter to invalidate cached real loggers\nlet factoryVersion = 0;\n\nexport interface LoggerFactoryConfig {\n defaultImplementation?: (name: string) => Logger;\n defaultLevel?: LogLevel;\n consoleOptions?: Omit<ConsoleLoggerOptions, \"level\">;\n}\n\n/**\n * Internal LoggerFactory implementation\n *\n * Uses lazy proxy pattern to allow module-level createLogger() calls.\n */\nexport class LoggerFactoryImpl {\n private static loggers: Map<string, Logger> = new Map();\n private static config: LoggerFactoryConfig = {\n defaultLevel: \"info\",\n };\n\n static getLogger(nameOrClass: string | (new (...args: unknown[]) => unknown)): Logger {\n const name = typeof nameOrClass === \"string\" ? nameOrClass : nameOrClass.name;\n\n if (this.loggers.has(name)) {\n return this.loggers.get(name)!;\n }\n\n const lazyLogger = this.createLazyLogger(name);\n this.loggers.set(name, lazyLogger);\n return lazyLogger;\n }\n\n static configure(config: LoggerFactoryConfig): void {\n this.config = { ...this.config, ...config };\n }\n\n static reset(): void {\n this.loggers.clear();\n this.config = { defaultLevel: \"info\" };\n externalFactory = null;\n factoryVersion++; // Invalidate all cached real loggers\n }\n\n private static createLazyLogger(name: string): Logger {\n let realLogger: Logger | null = null;\n let loggerVersion = -1; // Track which factory version created this logger\n\n const getRealLogger = (): Logger => {\n // Recreate logger if factory version changed (setLoggerFactory was called)\n if (!realLogger || loggerVersion !== factoryVersion) {\n realLogger = this.createLogger(name);\n loggerVersion = factoryVersion;\n }\n return realLogger;\n };\n\n return {\n name,\n level: this.config.defaultLevel || \"info\",\n debug: (message: string, context?: LogContext) => getRealLogger().debug(message, context),\n info: (message: string, context?: LogContext) => getRealLogger().info(message, context),\n warn: (message: string, context?: LogContext) => getRealLogger().warn(message, context),\n error: (message: string | Error, context?: LogContext) =>\n getRealLogger().error(message, context),\n isDebugEnabled: () => getRealLogger().isDebugEnabled(),\n isInfoEnabled: () => getRealLogger().isInfoEnabled(),\n isWarnEnabled: () => getRealLogger().isWarnEnabled(),\n isErrorEnabled: () => getRealLogger().isErrorEnabled(),\n };\n }\n\n private static createLogger(name: string): Logger {\n if (externalFactory) {\n return externalFactory.getLogger(name);\n }\n\n if (this.config.defaultImplementation) {\n return this.config.defaultImplementation(name);\n }\n\n return new ConsoleLogger(name, {\n level: this.config.defaultLevel,\n ...this.config.consoleOptions,\n });\n }\n}\n\n/**\n * Set external LoggerFactory (called by Runtime initialization)\n */\nexport function setLoggerFactory(factory: LoggerFactory): void {\n externalFactory = factory;\n LoggerFactoryImpl.reset();\n externalFactory = factory;\n}\n\n/**\n * Create a logger instance\n *\n * Safe to call at module level before Runtime is configured.\n * Uses lazy initialization - actual logger is created on first use.\n *\n * @param name - Logger name (hierarchical, e.g., \"engine/AgentEngine\")\n * @returns Logger instance (lazy proxy)\n */\nexport function createLogger(name: string): Logger {\n return LoggerFactoryImpl.getLogger(name);\n}\n"],"mappings":";;;;;AAeO,IAAM,iBAAN,MAAM,eAAgC;AAAA,EAc3C,YAAY,MAAc,UAAgC,CAAC,GAAG;AAb9D,wBAAS;AACT,wBAAS;AACT,wBAAiB;AACjB,wBAAiB;AAWf,SAAK,OAAO;AACZ,SAAK,QAAQ,QAAQ,SAAS;AAC9B,SAAK,SAAS,QAAQ,UAAU,KAAK,kBAAkB;AACvD,SAAK,aAAa,QAAQ,cAAc;AAAA,EAC1C;AAAA,EAEA,MAAM,SAAiB,SAA4B;AACjD,QAAI,KAAK,eAAe,GAAG;AACzB,WAAK,IAAI,SAAS,SAAS,OAAO;AAAA,IACpC;AAAA,EACF;AAAA,EAEA,KAAK,SAAiB,SAA4B;AAChD,QAAI,KAAK,cAAc,GAAG;AACx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AA,IAC/C;AAEA,WAAO,IAAI,cAAc,MAAM;AAAA,MAC7B,OAAO,KAAK,OAAO;AAAA,MACnB,GAAG,KAAK,OAAO;AAAA,IACjB,CAAC;AAAA,EACH;AACF;AAtEE,cADW,mBACI,WAA+B,oBAAI,IAAI;AACtD,cAFW,mBAEI,UAA8B;AAAA,EAC3C,cAAc;AAChB;AAwEK,SAAS,iBAAiB,SAA8B;AAC7D,oBAAkB;AAClB,oBAAkB,MAAM;AACxB,oBAAkB;AACpB;AAWO,SAAS,aAAa,MAAsB;AACjD,SAAO,kBAAkB,UAAU,IAAI;AACzC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentxjs/common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.3",
|
|
4
4
|
"description": "Common utilities for AgentX platform - Logger facade and shared infrastructure",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agentx",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"README.md"
|
|
31
31
|
],
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@agentxjs/types": "
|
|
33
|
+
"@agentxjs/types": "1.1.3"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@types/node": "^22.19.1",
|