@cloudbase/agent-shared 0.0.13 → 0.0.15
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/CHANGELOG.md +18 -0
- package/dist/index.d.mts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +6 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @cloudbase/agent-shared
|
|
2
2
|
|
|
3
|
+
## 1.0.1-alpha.8
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- alpha release 0.1.2-alpha.1
|
|
8
|
+
- Update all public packages to version 0.1.2-alpha.1
|
|
9
|
+
- Trigger automated alpha release workflow
|
|
10
|
+
- Includes latest features and improvements
|
|
11
|
+
|
|
12
|
+
## 1.0.1-alpha.7
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- alpha release 0.1.2-alpha.1
|
|
17
|
+
- Update all public packages to version 0.1.2-alpha.1
|
|
18
|
+
- Trigger automated alpha release workflow
|
|
19
|
+
- Includes latest features and improvements
|
|
20
|
+
|
|
3
21
|
## 1.0.1-alpha.6
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/index.d.mts
CHANGED
|
@@ -124,6 +124,13 @@ declare const noopLogger: Logger;
|
|
|
124
124
|
*/
|
|
125
125
|
declare function createConsoleLogger(): Logger;
|
|
126
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Type guard to check if an error has a code property.
|
|
129
|
+
*/
|
|
130
|
+
declare function isErrorWithCode(error: unknown): error is Error & {
|
|
131
|
+
code: string;
|
|
132
|
+
};
|
|
133
|
+
|
|
127
134
|
type Schema = z3.ZodTypeAny | z4.$ZodType;
|
|
128
135
|
type ZodInfer<T> = T extends z3.ZodTypeAny ? z3.infer<T> : z4.infer<T>;
|
|
129
136
|
declare function isV4Schema(schema: Schema): schema is z4.$ZodType;
|
|
@@ -246,4 +253,4 @@ type AssistantMessage = z.infer<typeof assistantMessageSchema>;
|
|
|
246
253
|
type Tool = z.infer<typeof toolSchema>;
|
|
247
254
|
type SendMessageEvent = z.infer<typeof sendMessageEventSchema>;
|
|
248
255
|
|
|
249
|
-
export { type AssistantMessage, type ClientMessage, type LogFn, type Logger, type Schema, type SendMessageEvent, type SendMessageInput, type SystemMessage, type Tool, type ToolMessage, type UserMessage, type ZodInfer, assistantMessageSchema, clientMessageSchema, createConsoleLogger, isV4Schema, noopLogger, sendMessageEventSchema, sendMessageInputSchema, systemMessageSchema, toolMessageSchema, toolSchema, userMessageSchema };
|
|
256
|
+
export { type AssistantMessage, type ClientMessage, type LogFn, type Logger, type Schema, type SendMessageEvent, type SendMessageInput, type SystemMessage, type Tool, type ToolMessage, type UserMessage, type ZodInfer, assistantMessageSchema, clientMessageSchema, createConsoleLogger, isErrorWithCode, isV4Schema, noopLogger, sendMessageEventSchema, sendMessageInputSchema, systemMessageSchema, toolMessageSchema, toolSchema, userMessageSchema };
|
package/dist/index.d.ts
CHANGED
|
@@ -124,6 +124,13 @@ declare const noopLogger: Logger;
|
|
|
124
124
|
*/
|
|
125
125
|
declare function createConsoleLogger(): Logger;
|
|
126
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Type guard to check if an error has a code property.
|
|
129
|
+
*/
|
|
130
|
+
declare function isErrorWithCode(error: unknown): error is Error & {
|
|
131
|
+
code: string;
|
|
132
|
+
};
|
|
133
|
+
|
|
127
134
|
type Schema = z3.ZodTypeAny | z4.$ZodType;
|
|
128
135
|
type ZodInfer<T> = T extends z3.ZodTypeAny ? z3.infer<T> : z4.infer<T>;
|
|
129
136
|
declare function isV4Schema(schema: Schema): schema is z4.$ZodType;
|
|
@@ -246,4 +253,4 @@ type AssistantMessage = z.infer<typeof assistantMessageSchema>;
|
|
|
246
253
|
type Tool = z.infer<typeof toolSchema>;
|
|
247
254
|
type SendMessageEvent = z.infer<typeof sendMessageEventSchema>;
|
|
248
255
|
|
|
249
|
-
export { type AssistantMessage, type ClientMessage, type LogFn, type Logger, type Schema, type SendMessageEvent, type SendMessageInput, type SystemMessage, type Tool, type ToolMessage, type UserMessage, type ZodInfer, assistantMessageSchema, clientMessageSchema, createConsoleLogger, isV4Schema, noopLogger, sendMessageEventSchema, sendMessageInputSchema, systemMessageSchema, toolMessageSchema, toolSchema, userMessageSchema };
|
|
256
|
+
export { type AssistantMessage, type ClientMessage, type LogFn, type Logger, type Schema, type SendMessageEvent, type SendMessageInput, type SystemMessage, type Tool, type ToolMessage, type UserMessage, type ZodInfer, assistantMessageSchema, clientMessageSchema, createConsoleLogger, isErrorWithCode, isV4Schema, noopLogger, sendMessageEventSchema, sendMessageInputSchema, systemMessageSchema, toolMessageSchema, toolSchema, userMessageSchema };
|
package/dist/index.js
CHANGED
|
@@ -33,6 +33,7 @@ __export(index_exports, {
|
|
|
33
33
|
assistantMessageSchema: () => assistantMessageSchema,
|
|
34
34
|
clientMessageSchema: () => clientMessageSchema,
|
|
35
35
|
createConsoleLogger: () => createConsoleLogger,
|
|
36
|
+
isErrorWithCode: () => isErrorWithCode,
|
|
36
37
|
isV4Schema: () => isV4Schema,
|
|
37
38
|
noopLogger: () => noopLogger,
|
|
38
39
|
sendMessageEventSchema: () => sendMessageEventSchema,
|
|
@@ -97,6 +98,11 @@ function createChildConsoleLogger(bindings) {
|
|
|
97
98
|
};
|
|
98
99
|
}
|
|
99
100
|
|
|
101
|
+
// src/errors.ts
|
|
102
|
+
function isErrorWithCode(error) {
|
|
103
|
+
return error instanceof Error && "code" in error && typeof error.code === "string";
|
|
104
|
+
}
|
|
105
|
+
|
|
100
106
|
// src/index.ts
|
|
101
107
|
function isV4Schema(schema) {
|
|
102
108
|
return "_zod" in schema;
|
|
@@ -184,6 +190,7 @@ var sendMessageEventSchema = z.union([
|
|
|
184
190
|
assistantMessageSchema,
|
|
185
191
|
clientMessageSchema,
|
|
186
192
|
createConsoleLogger,
|
|
193
|
+
isErrorWithCode,
|
|
187
194
|
isV4Schema,
|
|
188
195
|
noopLogger,
|
|
189
196
|
sendMessageEventSchema,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/logger.ts"],"sourcesContent":["import * as z from \"zod/v4\";\nimport * as z3 from \"zod/v3\";\nimport * as z4 from \"zod/v4/core\";\n\n// Logger exports\nexport { type Logger, type LogFn, noopLogger, createConsoleLogger } from \"./logger\";\n\n// https://zod.dev/library-authors#how-to-support-zod-3-and-zod-4-simultaneously\nexport type Schema = z3.ZodTypeAny | z4.$ZodType;\nexport type ZodInfer<T> = T extends z3.ZodTypeAny ? z3.infer<T> : z4.infer<T>;\nexport function isV4Schema(schema: Schema): schema is z4.$ZodType {\n return \"_zod\" in schema;\n}\n\nexport const systemMessageSchema = z.object({\n role: z.literal(\"system\"),\n content: z.string(),\n});\n\nexport const userMessageSchema = z.object({\n role: z.literal(\"user\"),\n content: z.string(),\n});\n\nexport const toolMessageSchema = z.object({\n role: z.literal(\"tool\"),\n content: z.string(),\n toolCallId: z.string(),\n});\n\nexport const assistantMessageSchema = z.object({\n role: z.literal(\"assistant\"),\n content: z.string().optional(),\n toolCalls: z\n .array(\n z.object({\n id: z.string(),\n type: z.literal(\"function\"),\n function: z.object({\n name: z.string(),\n arguments: z.string(),\n }),\n })\n )\n .optional(),\n});\n\nexport const clientMessageSchema = z.union([\n systemMessageSchema,\n userMessageSchema,\n toolMessageSchema,\n assistantMessageSchema,\n]);\n\nexport const toolSchema = z.object({\n name: z.string(),\n description: z.string(),\n parameters: z.any(),\n});\n\nexport const sendMessageInputSchema = z.object({\n conversationId: z.string(),\n messages: z.array(clientMessageSchema).optional(),\n resume: z\n .object({\n interruptId: z.string(),\n payload: z.any(),\n })\n .optional(),\n tools: z.array(toolSchema).optional(),\n});\n\nexport const sendMessageEventSchema = z.union([\n z.object({\n type: z.literal(\"text\"),\n content: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-start\"),\n toolCallId: z.string(),\n toolCallName: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-args\"),\n toolCallId: z.string(),\n delta: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-end\"),\n toolCallId: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-result\"),\n toolCallId: z.string(),\n result: z.string(),\n }),\n z.object({\n type: z.literal(\"interrupt\"),\n id: z.string(),\n reason: z.string(),\n payload: z.any(),\n }),\n]);\n\nexport type SendMessageInput = z.infer<typeof sendMessageInputSchema>;\nexport type ClientMessage = z.infer<typeof clientMessageSchema>;\nexport type SystemMessage = z.infer<typeof systemMessageSchema>;\nexport type UserMessage = z.infer<typeof userMessageSchema>;\nexport type ToolMessage = z.infer<typeof toolMessageSchema>;\nexport type AssistantMessage = z.infer<typeof assistantMessageSchema>;\nexport type Tool = z.infer<typeof toolSchema>;\nexport type SendMessageEvent = z.infer<typeof sendMessageEventSchema>;\n","/**\n * Logger interface\n *\n * This follows the \"Bring Your Own Logger\" (BYOL) pattern:\n * - Users can inject their own logger (Pino, Winston, console, etc.)\n * - All methods are optional - if a method exists, we use it; if not, we skip it\n * - The interface is a \"slot\" for users to plug in their logger\n *\n * The signature supports two common calling conventions:\n * 1. Printf style (Winston/Console): logger.info(\"Port %d\", 8080)\n * 2. Structured style (Pino): logger.info({ port: 8080 }, \"Server started\")\n */\n\n/**\n * Log method signature supporting both structured and printf-style logging.\n *\n * @example\n * // Printf style\n * logger.info(\"Hello %s\", \"world\");\n *\n * // Structured style (Pino-compatible)\n * logger.info({ userId: 123 }, \"User logged in\");\n *\n * // Simple message\n * logger.info(\"Server started\");\n */\nexport interface LogFn {\n // Printf style: message with format args\n (msg: string, ...args: unknown[]): void;\n // Structured style: object with optional message and format args\n (obj: object, msg?: string, ...args: unknown[]): void;\n}\n\n/**\n * Logger interface - a minimal \"slot\" for users to plug in their logger.\n *\n * All methods are optional. If a method exists on the user's logger, we use it;\n * if not, we simply skip it. This allows maximum compatibility with any logger.\n *\n * Compatible with: Pino, Winston, Bunyan, console, or any custom logger.\n *\n * @example\n * // Inject Pino\n * import pino from 'pino';\n * createExpressServer({ createAgent, logger: pino() });\n *\n * // Inject Winston\n * import winston from 'winston';\n * createExpressServer({ createAgent, logger: winston.createLogger() });\n *\n * // Inject console (for development)\n * createExpressServer({ createAgent, logger: console });\n */\nexport interface Logger {\n /**\n * Fatal level - system is unusable, immediate action required.\n * Use for: unrecoverable errors, process about to crash.\n */\n fatal?: LogFn;\n\n /**\n * Error level - failures requiring attention.\n * Use for: 5xx errors, unhandled exceptions, critical failures.\n */\n error?: LogFn;\n\n /**\n * Warn level - recoverable issues, deprecation notices.\n * Use for: 4xx errors, missing config with fallback, retry attempts.\n */\n warn?: LogFn;\n\n /**\n * Info level - key lifecycle events (production default).\n * Use for: server started, client connected, request completed.\n */\n info?: LogFn;\n\n /**\n * Debug level - diagnostic information for logic flow.\n * Use for: request received, state changes, configuration loaded.\n */\n debug?: LogFn;\n\n /**\n * Trace level - extremely verbose, for library internals debugging.\n * Use for: raw bytes, loop iterations, function entry/exit.\n */\n trace?: LogFn;\n\n /**\n * Creates a child logger with additional context.\n * The child inherits all parent context and adds its own.\n *\n * @example\n * const requestLogger = logger.child({ requestId: 'req-123' });\n * requestLogger.info(\"Processing request\"); // includes requestId in output\n */\n child?(bindings: Record<string, unknown>): Logger;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nconst noop: LogFn = (_msgOrObj: unknown, ..._args: unknown[]) => {};\n\n/**\n * No-op logger implementation (Abstract Logging pattern).\n *\n * This is the default logger - it does nothing.\n * This ensures:\n * 1. The library is silent by default (respects user's console)\n * 2. No performance overhead from console.log in production\n * 3. No need for `if (logger)` checks throughout the code\n *\n * @example\n * // Default: silent\n * createExpressServer({ createAgent });\n *\n * // Development: see logs\n * createExpressServer({ createAgent, logger: console });\n *\n * // Production: structured JSON logs\n * import pino from 'pino';\n * createExpressServer({ createAgent, logger: pino() });\n */\nexport const noopLogger: Logger = {\n fatal: noop,\n error: noop,\n warn: noop,\n info: noop,\n debug: noop,\n trace: noop,\n child: () => noopLogger,\n};\n\n/**\n * Creates a console-based logger that wraps console methods.\n * Useful for development when you want simple console output.\n *\n * Note: console.log is synchronous and blocking - avoid in high-throughput production.\n *\n * @example\n * createExpressServer({ createAgent, logger: createConsoleLogger() });\n */\nexport function createConsoleLogger(): Logger {\n return createChildConsoleLogger({});\n}\n\nfunction createChildConsoleLogger(bindings: Record<string, unknown>): Logger {\n const hasBindings = Object.keys(bindings).length > 0;\n\n function createLogMethod(\n level: \"fatal\" | \"error\" | \"warn\" | \"info\" | \"debug\" | \"trace\"\n ): LogFn {\n // Map fatal to error, trace to debug for console\n const consoleMethod =\n level === \"fatal\"\n ? \"error\"\n : level === \"trace\"\n ? \"debug\"\n : level;\n const consoleFn = console[consoleMethod] || console.log;\n const prefix = `[${level.toUpperCase()}]`;\n\n return (msgOrObj: unknown, ...args: unknown[]) => {\n if (typeof msgOrObj === \"string\") {\n // Printf style: logger.info(\"Hello %s\", \"world\")\n if (hasBindings) {\n consoleFn(prefix, bindings, msgOrObj, ...args);\n } else {\n consoleFn(prefix, msgOrObj, ...args);\n }\n } else {\n // Structured style: logger.info({ userId: 1 }, \"User logged in\")\n const [msg, ...restArgs] = args;\n const mergedObj = hasBindings\n ? { ...bindings, ...(msgOrObj as object) }\n : msgOrObj;\n if (msg) {\n consoleFn(prefix, mergedObj, msg, ...restArgs);\n } else {\n consoleFn(prefix, mergedObj);\n }\n }\n };\n }\n\n return {\n fatal: createLogMethod(\"fatal\"),\n error: createLogMethod(\"error\"),\n warn: createLogMethod(\"warn\"),\n info: createLogMethod(\"info\"),\n debug: createLogMethod(\"debug\"),\n trace: createLogMethod(\"trace\"),\n child: (newBindings: Record<string, unknown>) => {\n return createChildConsoleLogger({ ...bindings, ...newBindings });\n },\n };\n}\n\n"],"mappings":";;;;;;;;;;;;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AAW,MAAI;AAAA,EACjB,CAAC,EACA,SAAS;AAAA,EACZ,OAAS,QAAM,UAAU,EAAE,SAAS;AACtC,CAAC;AAEM,IAAM,yBAA2B,QAAM;AAAA,EAC1C,SAAO;AAAA,IACP,MAAQ,UAAQ,MAAM;AAAA,IACtB,SAAW,SAAO;AAAA,EACpB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,iBAAiB;AAAA,IACjC,YAAc,SAAO;AAAA,IACrB,cAAgB,SAAO;AAAA,EACzB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,gBAAgB;AAAA,IAChC,YAAc,SAAO;AAAA,IACrB,OAAS,SAAO;AAAA,EAClB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,eAAe;AAAA,IAC/B,YAAc,SAAO;AAAA,EACvB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,aAAa;AAAA,IAC7B,YAAc,SAAO;AAAA,IACrB,QAAU,SAAO;AAAA,EACnB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,WAAW;AAAA,IAC3B,IAAM,SAAO;AAAA,IACb,QAAU,SAAO;AAAA,IACjB,SAAW,MAAI;AAAA,EACjB,CAAC;AACH,CAAC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/logger.ts","../src/errors.ts"],"sourcesContent":["import * as z from \"zod/v4\";\nimport * as z3 from \"zod/v3\";\nimport * as z4 from \"zod/v4/core\";\n\n// Logger exports\nexport { type Logger, type LogFn, noopLogger, createConsoleLogger } from \"./logger\";\n\n// Error handling exports\nexport { isErrorWithCode } from \"./errors\";\n\n// https://zod.dev/library-authors#how-to-support-zod-3-and-zod-4-simultaneously\nexport type Schema = z3.ZodTypeAny | z4.$ZodType;\nexport type ZodInfer<T> = T extends z3.ZodTypeAny ? z3.infer<T> : z4.infer<T>;\nexport function isV4Schema(schema: Schema): schema is z4.$ZodType {\n return \"_zod\" in schema;\n}\n\nexport const systemMessageSchema = z.object({\n role: z.literal(\"system\"),\n content: z.string(),\n});\n\nexport const userMessageSchema = z.object({\n role: z.literal(\"user\"),\n content: z.string(),\n});\n\nexport const toolMessageSchema = z.object({\n role: z.literal(\"tool\"),\n content: z.string(),\n toolCallId: z.string(),\n});\n\nexport const assistantMessageSchema = z.object({\n role: z.literal(\"assistant\"),\n content: z.string().optional(),\n toolCalls: z\n .array(\n z.object({\n id: z.string(),\n type: z.literal(\"function\"),\n function: z.object({\n name: z.string(),\n arguments: z.string(),\n }),\n })\n )\n .optional(),\n});\n\nexport const clientMessageSchema = z.union([\n systemMessageSchema,\n userMessageSchema,\n toolMessageSchema,\n assistantMessageSchema,\n]);\n\nexport const toolSchema = z.object({\n name: z.string(),\n description: z.string(),\n parameters: z.any(),\n});\n\nexport const sendMessageInputSchema = z.object({\n conversationId: z.string(),\n messages: z.array(clientMessageSchema).optional(),\n resume: z\n .object({\n interruptId: z.string(),\n payload: z.any(),\n })\n .optional(),\n tools: z.array(toolSchema).optional(),\n});\n\nexport const sendMessageEventSchema = z.union([\n z.object({\n type: z.literal(\"text\"),\n content: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-start\"),\n toolCallId: z.string(),\n toolCallName: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-args\"),\n toolCallId: z.string(),\n delta: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-end\"),\n toolCallId: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-result\"),\n toolCallId: z.string(),\n result: z.string(),\n }),\n z.object({\n type: z.literal(\"interrupt\"),\n id: z.string(),\n reason: z.string(),\n payload: z.any(),\n }),\n]);\n\nexport type SendMessageInput = z.infer<typeof sendMessageInputSchema>;\nexport type ClientMessage = z.infer<typeof clientMessageSchema>;\nexport type SystemMessage = z.infer<typeof systemMessageSchema>;\nexport type UserMessage = z.infer<typeof userMessageSchema>;\nexport type ToolMessage = z.infer<typeof toolMessageSchema>;\nexport type AssistantMessage = z.infer<typeof assistantMessageSchema>;\nexport type Tool = z.infer<typeof toolSchema>;\nexport type SendMessageEvent = z.infer<typeof sendMessageEventSchema>;\n","/**\n * Logger interface\n *\n * This follows the \"Bring Your Own Logger\" (BYOL) pattern:\n * - Users can inject their own logger (Pino, Winston, console, etc.)\n * - All methods are optional - if a method exists, we use it; if not, we skip it\n * - The interface is a \"slot\" for users to plug in their logger\n *\n * The signature supports two common calling conventions:\n * 1. Printf style (Winston/Console): logger.info(\"Port %d\", 8080)\n * 2. Structured style (Pino): logger.info({ port: 8080 }, \"Server started\")\n */\n\n/**\n * Log method signature supporting both structured and printf-style logging.\n *\n * @example\n * // Printf style\n * logger.info(\"Hello %s\", \"world\");\n *\n * // Structured style (Pino-compatible)\n * logger.info({ userId: 123 }, \"User logged in\");\n *\n * // Simple message\n * logger.info(\"Server started\");\n */\nexport interface LogFn {\n // Printf style: message with format args\n (msg: string, ...args: unknown[]): void;\n // Structured style: object with optional message and format args\n (obj: object, msg?: string, ...args: unknown[]): void;\n}\n\n/**\n * Logger interface - a minimal \"slot\" for users to plug in their logger.\n *\n * All methods are optional. If a method exists on the user's logger, we use it;\n * if not, we simply skip it. This allows maximum compatibility with any logger.\n *\n * Compatible with: Pino, Winston, Bunyan, console, or any custom logger.\n *\n * @example\n * // Inject Pino\n * import pino from 'pino';\n * createExpressServer({ createAgent, logger: pino() });\n *\n * // Inject Winston\n * import winston from 'winston';\n * createExpressServer({ createAgent, logger: winston.createLogger() });\n *\n * // Inject console (for development)\n * createExpressServer({ createAgent, logger: console });\n */\nexport interface Logger {\n /**\n * Fatal level - system is unusable, immediate action required.\n * Use for: unrecoverable errors, process about to crash.\n */\n fatal?: LogFn;\n\n /**\n * Error level - failures requiring attention.\n * Use for: 5xx errors, unhandled exceptions, critical failures.\n */\n error?: LogFn;\n\n /**\n * Warn level - recoverable issues, deprecation notices.\n * Use for: 4xx errors, missing config with fallback, retry attempts.\n */\n warn?: LogFn;\n\n /**\n * Info level - key lifecycle events (production default).\n * Use for: server started, client connected, request completed.\n */\n info?: LogFn;\n\n /**\n * Debug level - diagnostic information for logic flow.\n * Use for: request received, state changes, configuration loaded.\n */\n debug?: LogFn;\n\n /**\n * Trace level - extremely verbose, for library internals debugging.\n * Use for: raw bytes, loop iterations, function entry/exit.\n */\n trace?: LogFn;\n\n /**\n * Creates a child logger with additional context.\n * The child inherits all parent context and adds its own.\n *\n * @example\n * const requestLogger = logger.child({ requestId: 'req-123' });\n * requestLogger.info(\"Processing request\"); // includes requestId in output\n */\n child?(bindings: Record<string, unknown>): Logger;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nconst noop: LogFn = (_msgOrObj: unknown, ..._args: unknown[]) => {};\n\n/**\n * No-op logger implementation (Abstract Logging pattern).\n *\n * This is the default logger - it does nothing.\n * This ensures:\n * 1. The library is silent by default (respects user's console)\n * 2. No performance overhead from console.log in production\n * 3. No need for `if (logger)` checks throughout the code\n *\n * @example\n * // Default: silent\n * createExpressServer({ createAgent });\n *\n * // Development: see logs\n * createExpressServer({ createAgent, logger: console });\n *\n * // Production: structured JSON logs\n * import pino from 'pino';\n * createExpressServer({ createAgent, logger: pino() });\n */\nexport const noopLogger: Logger = {\n fatal: noop,\n error: noop,\n warn: noop,\n info: noop,\n debug: noop,\n trace: noop,\n child: () => noopLogger,\n};\n\n/**\n * Creates a console-based logger that wraps console methods.\n * Useful for development when you want simple console output.\n *\n * Note: console.log is synchronous and blocking - avoid in high-throughput production.\n *\n * @example\n * createExpressServer({ createAgent, logger: createConsoleLogger() });\n */\nexport function createConsoleLogger(): Logger {\n return createChildConsoleLogger({});\n}\n\nfunction createChildConsoleLogger(bindings: Record<string, unknown>): Logger {\n const hasBindings = Object.keys(bindings).length > 0;\n\n function createLogMethod(\n level: \"fatal\" | \"error\" | \"warn\" | \"info\" | \"debug\" | \"trace\"\n ): LogFn {\n // Map fatal to error, trace to debug for console\n const consoleMethod =\n level === \"fatal\"\n ? \"error\"\n : level === \"trace\"\n ? \"debug\"\n : level;\n const consoleFn = console[consoleMethod] || console.log;\n const prefix = `[${level.toUpperCase()}]`;\n\n return (msgOrObj: unknown, ...args: unknown[]) => {\n if (typeof msgOrObj === \"string\") {\n // Printf style: logger.info(\"Hello %s\", \"world\")\n if (hasBindings) {\n consoleFn(prefix, bindings, msgOrObj, ...args);\n } else {\n consoleFn(prefix, msgOrObj, ...args);\n }\n } else {\n // Structured style: logger.info({ userId: 1 }, \"User logged in\")\n const [msg, ...restArgs] = args;\n const mergedObj = hasBindings\n ? { ...bindings, ...(msgOrObj as object) }\n : msgOrObj;\n if (msg) {\n consoleFn(prefix, mergedObj, msg, ...restArgs);\n } else {\n consoleFn(prefix, mergedObj);\n }\n }\n };\n }\n\n return {\n fatal: createLogMethod(\"fatal\"),\n error: createLogMethod(\"error\"),\n warn: createLogMethod(\"warn\"),\n info: createLogMethod(\"info\"),\n debug: createLogMethod(\"debug\"),\n trace: createLogMethod(\"trace\"),\n child: (newBindings: Record<string, unknown>) => {\n return createChildConsoleLogger({ ...bindings, ...newBindings });\n },\n };\n}\n\n","/**\n * Type guard to check if an error has a code property.\n */\nexport function isErrorWithCode(\n error: unknown\n): error is Error & { code: string } {\n return (\n error instanceof Error &&\n \"code\" in error &&\n typeof error.code === \"string\"\n );\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,QAAmB;;;ACsGnB,IAAM,OAAc,CAAC,cAAuB,UAAqB;AAAC;AAsB3D,IAAM,aAAqB;AAAA,EAChC,OAAO;AAAA,EACP,OAAO;AAAA,EACP,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO,MAAM;AACf;AAWO,SAAS,sBAA8B;AAC5C,SAAO,yBAAyB,CAAC,CAAC;AACpC;AAEA,SAAS,yBAA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A,EACzB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,gBAAgB;AAAA,IAChC,YAAc,SAAO;AAAA,IACrB,OAAS,SAAO;AAAA,EAClB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,eAAe;AAAA,IAC/B,YAAc,SAAO;AAAA,EACvB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,aAAa;AAAA,IAC7B,YAAc,SAAO;AAAA,IACrB,QAAU,SAAO;AAAA,EACnB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,WAAW;AAAA,IAC3B,IAAM,SAAO;AAAA,IACb,QAAU,SAAO;AAAA,IACjB,SAAW,MAAI;AAAA,EACjB,CAAC;AACH,CAAC;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -53,6 +53,11 @@ function createChildConsoleLogger(bindings) {
|
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
// src/errors.ts
|
|
57
|
+
function isErrorWithCode(error) {
|
|
58
|
+
return error instanceof Error && "code" in error && typeof error.code === "string";
|
|
59
|
+
}
|
|
60
|
+
|
|
56
61
|
// src/index.ts
|
|
57
62
|
function isV4Schema(schema) {
|
|
58
63
|
return "_zod" in schema;
|
|
@@ -139,6 +144,7 @@ export {
|
|
|
139
144
|
assistantMessageSchema,
|
|
140
145
|
clientMessageSchema,
|
|
141
146
|
createConsoleLogger,
|
|
147
|
+
isErrorWithCode,
|
|
142
148
|
isV4Schema,
|
|
143
149
|
noopLogger,
|
|
144
150
|
sendMessageEventSchema,
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/logger.ts"],"sourcesContent":["import * as z from \"zod/v4\";\nimport * as z3 from \"zod/v3\";\nimport * as z4 from \"zod/v4/core\";\n\n// Logger exports\nexport { type Logger, type LogFn, noopLogger, createConsoleLogger } from \"./logger\";\n\n// https://zod.dev/library-authors#how-to-support-zod-3-and-zod-4-simultaneously\nexport type Schema = z3.ZodTypeAny | z4.$ZodType;\nexport type ZodInfer<T> = T extends z3.ZodTypeAny ? z3.infer<T> : z4.infer<T>;\nexport function isV4Schema(schema: Schema): schema is z4.$ZodType {\n return \"_zod\" in schema;\n}\n\nexport const systemMessageSchema = z.object({\n role: z.literal(\"system\"),\n content: z.string(),\n});\n\nexport const userMessageSchema = z.object({\n role: z.literal(\"user\"),\n content: z.string(),\n});\n\nexport const toolMessageSchema = z.object({\n role: z.literal(\"tool\"),\n content: z.string(),\n toolCallId: z.string(),\n});\n\nexport const assistantMessageSchema = z.object({\n role: z.literal(\"assistant\"),\n content: z.string().optional(),\n toolCalls: z\n .array(\n z.object({\n id: z.string(),\n type: z.literal(\"function\"),\n function: z.object({\n name: z.string(),\n arguments: z.string(),\n }),\n })\n )\n .optional(),\n});\n\nexport const clientMessageSchema = z.union([\n systemMessageSchema,\n userMessageSchema,\n toolMessageSchema,\n assistantMessageSchema,\n]);\n\nexport const toolSchema = z.object({\n name: z.string(),\n description: z.string(),\n parameters: z.any(),\n});\n\nexport const sendMessageInputSchema = z.object({\n conversationId: z.string(),\n messages: z.array(clientMessageSchema).optional(),\n resume: z\n .object({\n interruptId: z.string(),\n payload: z.any(),\n })\n .optional(),\n tools: z.array(toolSchema).optional(),\n});\n\nexport const sendMessageEventSchema = z.union([\n z.object({\n type: z.literal(\"text\"),\n content: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-start\"),\n toolCallId: z.string(),\n toolCallName: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-args\"),\n toolCallId: z.string(),\n delta: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-end\"),\n toolCallId: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-result\"),\n toolCallId: z.string(),\n result: z.string(),\n }),\n z.object({\n type: z.literal(\"interrupt\"),\n id: z.string(),\n reason: z.string(),\n payload: z.any(),\n }),\n]);\n\nexport type SendMessageInput = z.infer<typeof sendMessageInputSchema>;\nexport type ClientMessage = z.infer<typeof clientMessageSchema>;\nexport type SystemMessage = z.infer<typeof systemMessageSchema>;\nexport type UserMessage = z.infer<typeof userMessageSchema>;\nexport type ToolMessage = z.infer<typeof toolMessageSchema>;\nexport type AssistantMessage = z.infer<typeof assistantMessageSchema>;\nexport type Tool = z.infer<typeof toolSchema>;\nexport type SendMessageEvent = z.infer<typeof sendMessageEventSchema>;\n","/**\n * Logger interface\n *\n * This follows the \"Bring Your Own Logger\" (BYOL) pattern:\n * - Users can inject their own logger (Pino, Winston, console, etc.)\n * - All methods are optional - if a method exists, we use it; if not, we skip it\n * - The interface is a \"slot\" for users to plug in their logger\n *\n * The signature supports two common calling conventions:\n * 1. Printf style (Winston/Console): logger.info(\"Port %d\", 8080)\n * 2. Structured style (Pino): logger.info({ port: 8080 }, \"Server started\")\n */\n\n/**\n * Log method signature supporting both structured and printf-style logging.\n *\n * @example\n * // Printf style\n * logger.info(\"Hello %s\", \"world\");\n *\n * // Structured style (Pino-compatible)\n * logger.info({ userId: 123 }, \"User logged in\");\n *\n * // Simple message\n * logger.info(\"Server started\");\n */\nexport interface LogFn {\n // Printf style: message with format args\n (msg: string, ...args: unknown[]): void;\n // Structured style: object with optional message and format args\n (obj: object, msg?: string, ...args: unknown[]): void;\n}\n\n/**\n * Logger interface - a minimal \"slot\" for users to plug in their logger.\n *\n * All methods are optional. If a method exists on the user's logger, we use it;\n * if not, we simply skip it. This allows maximum compatibility with any logger.\n *\n * Compatible with: Pino, Winston, Bunyan, console, or any custom logger.\n *\n * @example\n * // Inject Pino\n * import pino from 'pino';\n * createExpressServer({ createAgent, logger: pino() });\n *\n * // Inject Winston\n * import winston from 'winston';\n * createExpressServer({ createAgent, logger: winston.createLogger() });\n *\n * // Inject console (for development)\n * createExpressServer({ createAgent, logger: console });\n */\nexport interface Logger {\n /**\n * Fatal level - system is unusable, immediate action required.\n * Use for: unrecoverable errors, process about to crash.\n */\n fatal?: LogFn;\n\n /**\n * Error level - failures requiring attention.\n * Use for: 5xx errors, unhandled exceptions, critical failures.\n */\n error?: LogFn;\n\n /**\n * Warn level - recoverable issues, deprecation notices.\n * Use for: 4xx errors, missing config with fallback, retry attempts.\n */\n warn?: LogFn;\n\n /**\n * Info level - key lifecycle events (production default).\n * Use for: server started, client connected, request completed.\n */\n info?: LogFn;\n\n /**\n * Debug level - diagnostic information for logic flow.\n * Use for: request received, state changes, configuration loaded.\n */\n debug?: LogFn;\n\n /**\n * Trace level - extremely verbose, for library internals debugging.\n * Use for: raw bytes, loop iterations, function entry/exit.\n */\n trace?: LogFn;\n\n /**\n * Creates a child logger with additional context.\n * The child inherits all parent context and adds its own.\n *\n * @example\n * const requestLogger = logger.child({ requestId: 'req-123' });\n * requestLogger.info(\"Processing request\"); // includes requestId in output\n */\n child?(bindings: Record<string, unknown>): Logger;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nconst noop: LogFn = (_msgOrObj: unknown, ..._args: unknown[]) => {};\n\n/**\n * No-op logger implementation (Abstract Logging pattern).\n *\n * This is the default logger - it does nothing.\n * This ensures:\n * 1. The library is silent by default (respects user's console)\n * 2. No performance overhead from console.log in production\n * 3. No need for `if (logger)` checks throughout the code\n *\n * @example\n * // Default: silent\n * createExpressServer({ createAgent });\n *\n * // Development: see logs\n * createExpressServer({ createAgent, logger: console });\n *\n * // Production: structured JSON logs\n * import pino from 'pino';\n * createExpressServer({ createAgent, logger: pino() });\n */\nexport const noopLogger: Logger = {\n fatal: noop,\n error: noop,\n warn: noop,\n info: noop,\n debug: noop,\n trace: noop,\n child: () => noopLogger,\n};\n\n/**\n * Creates a console-based logger that wraps console methods.\n * Useful for development when you want simple console output.\n *\n * Note: console.log is synchronous and blocking - avoid in high-throughput production.\n *\n * @example\n * createExpressServer({ createAgent, logger: createConsoleLogger() });\n */\nexport function createConsoleLogger(): Logger {\n return createChildConsoleLogger({});\n}\n\nfunction createChildConsoleLogger(bindings: Record<string, unknown>): Logger {\n const hasBindings = Object.keys(bindings).length > 0;\n\n function createLogMethod(\n level: \"fatal\" | \"error\" | \"warn\" | \"info\" | \"debug\" | \"trace\"\n ): LogFn {\n // Map fatal to error, trace to debug for console\n const consoleMethod =\n level === \"fatal\"\n ? \"error\"\n : level === \"trace\"\n ? \"debug\"\n : level;\n const consoleFn = console[consoleMethod] || console.log;\n const prefix = `[${level.toUpperCase()}]`;\n\n return (msgOrObj: unknown, ...args: unknown[]) => {\n if (typeof msgOrObj === \"string\") {\n // Printf style: logger.info(\"Hello %s\", \"world\")\n if (hasBindings) {\n consoleFn(prefix, bindings, msgOrObj, ...args);\n } else {\n consoleFn(prefix, msgOrObj, ...args);\n }\n } else {\n // Structured style: logger.info({ userId: 1 }, \"User logged in\")\n const [msg, ...restArgs] = args;\n const mergedObj = hasBindings\n ? { ...bindings, ...(msgOrObj as object) }\n : msgOrObj;\n if (msg) {\n consoleFn(prefix, mergedObj, msg, ...restArgs);\n } else {\n consoleFn(prefix, mergedObj);\n }\n }\n };\n }\n\n return {\n fatal: createLogMethod(\"fatal\"),\n error: createLogMethod(\"error\"),\n warn: createLogMethod(\"warn\"),\n info: createLogMethod(\"info\"),\n debug: createLogMethod(\"debug\"),\n trace: createLogMethod(\"trace\"),\n child: (newBindings: Record<string, unknown>) => {\n return createChildConsoleLogger({ ...bindings, ...newBindings });\n },\n };\n}\n\n"],"mappings":";AAAA,YAAY,OAAO;;;ACsGnB,IAAM,OAAc,CAAC,cAAuB,UAAqB;AAAC;AAsB3D,IAAM,aAAqB;AAAA,EAChC,OAAO;AAAA,EACP,OAAO;AAAA,EACP,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO,MAAM;AACf;AAWO,SAAS,sBAA8B;AAC5C,SAAO,yBAAyB,CAAC,CAAC;AACpC;AAEA,SAAS,yBAAyB,UAA2C;AAC3E,QAAM,cAAc,OAAO,KAAK,QAAQ,EAAE,SAAS;AAEnD,WAAS,gB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,UAAQ,eAAe;AAAA,IAC/B,YAAc,SAAO;AAAA,EACvB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,aAAa;AAAA,IAC7B,YAAc,SAAO;AAAA,IACrB,QAAU,SAAO;AAAA,EACnB,CAAC;AAAA,EACC,SAAO;AAAA,IACP,MAAQ,UAAQ,WAAW;AAAA,IAC3B,IAAM,SAAO;AAAA,IACb,QAAU,SAAO;AAAA,IACjB,SAAW,MAAI;AAAA,EACjB,CAAC;AACH,CAAC;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/logger.ts","../src/errors.ts"],"sourcesContent":["import * as z from \"zod/v4\";\nimport * as z3 from \"zod/v3\";\nimport * as z4 from \"zod/v4/core\";\n\n// Logger exports\nexport { type Logger, type LogFn, noopLogger, createConsoleLogger } from \"./logger\";\n\n// Error handling exports\nexport { isErrorWithCode } from \"./errors\";\n\n// https://zod.dev/library-authors#how-to-support-zod-3-and-zod-4-simultaneously\nexport type Schema = z3.ZodTypeAny | z4.$ZodType;\nexport type ZodInfer<T> = T extends z3.ZodTypeAny ? z3.infer<T> : z4.infer<T>;\nexport function isV4Schema(schema: Schema): schema is z4.$ZodType {\n return \"_zod\" in schema;\n}\n\nexport const systemMessageSchema = z.object({\n role: z.literal(\"system\"),\n content: z.string(),\n});\n\nexport const userMessageSchema = z.object({\n role: z.literal(\"user\"),\n content: z.string(),\n});\n\nexport const toolMessageSchema = z.object({\n role: z.literal(\"tool\"),\n content: z.string(),\n toolCallId: z.string(),\n});\n\nexport const assistantMessageSchema = z.object({\n role: z.literal(\"assistant\"),\n content: z.string().optional(),\n toolCalls: z\n .array(\n z.object({\n id: z.string(),\n type: z.literal(\"function\"),\n function: z.object({\n name: z.string(),\n arguments: z.string(),\n }),\n })\n )\n .optional(),\n});\n\nexport const clientMessageSchema = z.union([\n systemMessageSchema,\n userMessageSchema,\n toolMessageSchema,\n assistantMessageSchema,\n]);\n\nexport const toolSchema = z.object({\n name: z.string(),\n description: z.string(),\n parameters: z.any(),\n});\n\nexport const sendMessageInputSchema = z.object({\n conversationId: z.string(),\n messages: z.array(clientMessageSchema).optional(),\n resume: z\n .object({\n interruptId: z.string(),\n payload: z.any(),\n })\n .optional(),\n tools: z.array(toolSchema).optional(),\n});\n\nexport const sendMessageEventSchema = z.union([\n z.object({\n type: z.literal(\"text\"),\n content: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-start\"),\n toolCallId: z.string(),\n toolCallName: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-args\"),\n toolCallId: z.string(),\n delta: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-call-end\"),\n toolCallId: z.string(),\n }),\n z.object({\n type: z.literal(\"tool-result\"),\n toolCallId: z.string(),\n result: z.string(),\n }),\n z.object({\n type: z.literal(\"interrupt\"),\n id: z.string(),\n reason: z.string(),\n payload: z.any(),\n }),\n]);\n\nexport type SendMessageInput = z.infer<typeof sendMessageInputSchema>;\nexport type ClientMessage = z.infer<typeof clientMessageSchema>;\nexport type SystemMessage = z.infer<typeof systemMessageSchema>;\nexport type UserMessage = z.infer<typeof userMessageSchema>;\nexport type ToolMessage = z.infer<typeof toolMessageSchema>;\nexport type AssistantMessage = z.infer<typeof assistantMessageSchema>;\nexport type Tool = z.infer<typeof toolSchema>;\nexport type SendMessageEvent = z.infer<typeof sendMessageEventSchema>;\n","/**\n * Logger interface\n *\n * This follows the \"Bring Your Own Logger\" (BYOL) pattern:\n * - Users can inject their own logger (Pino, Winston, console, etc.)\n * - All methods are optional - if a method exists, we use it; if not, we skip it\n * - The interface is a \"slot\" for users to plug in their logger\n *\n * The signature supports two common calling conventions:\n * 1. Printf style (Winston/Console): logger.info(\"Port %d\", 8080)\n * 2. Structured style (Pino): logger.info({ port: 8080 }, \"Server started\")\n */\n\n/**\n * Log method signature supporting both structured and printf-style logging.\n *\n * @example\n * // Printf style\n * logger.info(\"Hello %s\", \"world\");\n *\n * // Structured style (Pino-compatible)\n * logger.info({ userId: 123 }, \"User logged in\");\n *\n * // Simple message\n * logger.info(\"Server started\");\n */\nexport interface LogFn {\n // Printf style: message with format args\n (msg: string, ...args: unknown[]): void;\n // Structured style: object with optional message and format args\n (obj: object, msg?: string, ...args: unknown[]): void;\n}\n\n/**\n * Logger interface - a minimal \"slot\" for users to plug in their logger.\n *\n * All methods are optional. If a method exists on the user's logger, we use it;\n * if not, we simply skip it. This allows maximum compatibility with any logger.\n *\n * Compatible with: Pino, Winston, Bunyan, console, or any custom logger.\n *\n * @example\n * // Inject Pino\n * import pino from 'pino';\n * createExpressServer({ createAgent, logger: pino() });\n *\n * // Inject Winston\n * import winston from 'winston';\n * createExpressServer({ createAgent, logger: winston.createLogger() });\n *\n * // Inject console (for development)\n * createExpressServer({ createAgent, logger: console });\n */\nexport interface Logger {\n /**\n * Fatal level - system is unusable, immediate action required.\n * Use for: unrecoverable errors, process about to crash.\n */\n fatal?: LogFn;\n\n /**\n * Error level - failures requiring attention.\n * Use for: 5xx errors, unhandled exceptions, critical failures.\n */\n error?: LogFn;\n\n /**\n * Warn level - recoverable issues, deprecation notices.\n * Use for: 4xx errors, missing config with fallback, retry attempts.\n */\n warn?: LogFn;\n\n /**\n * Info level - key lifecycle events (production default).\n * Use for: server started, client connected, request completed.\n */\n info?: LogFn;\n\n /**\n * Debug level - diagnostic information for logic flow.\n * Use for: request received, state changes, configuration loaded.\n */\n debug?: LogFn;\n\n /**\n * Trace level - extremely verbose, for library internals debugging.\n * Use for: raw bytes, loop iterations, function entry/exit.\n */\n trace?: LogFn;\n\n /**\n * Creates a child logger with additional context.\n * The child inherits all parent context and adds its own.\n *\n * @example\n * const requestLogger = logger.child({ requestId: 'req-123' });\n * requestLogger.info(\"Processing request\"); // includes requestId in output\n */\n child?(bindings: Record<string, unknown>): Logger;\n}\n\n// eslint-disable-next-line @typescript-eslint/no-unused-vars\nconst noop: LogFn = (_msgOrObj: unknown, ..._args: unknown[]) => {};\n\n/**\n * No-op logger implementation (Abstract Logging pattern).\n *\n * This is the default logger - it does nothing.\n * This ensures:\n * 1. The library is silent by default (respects user's console)\n * 2. No performance overhead from console.log in production\n * 3. No need for `if (logger)` checks throughout the code\n *\n * @example\n * // Default: silent\n * createExpressServer({ createAgent });\n *\n * // Development: see logs\n * createExpressServer({ createAgent, logger: console });\n *\n * // Production: structured JSON logs\n * import pino from 'pino';\n * createExpressServer({ createAgent, logger: pino() });\n */\nexport const noopLogger: Logger = {\n fatal: noop,\n error: noop,\n warn: noop,\n info: noop,\n debug: noop,\n trace: noop,\n child: () => noopLogger,\n};\n\n/**\n * Creates a console-based logger that wraps console methods.\n * Useful for development when you want simple console output.\n *\n * Note: console.log is synchronous and blocking - avoid in high-throughput production.\n *\n * @example\n * createExpressServer({ createAgent, logger: createConsoleLogger() });\n */\nexport function createConsoleLogger(): Logger {\n return createChildConsoleLogger({});\n}\n\nfunction createChildConsoleLogger(bindings: Record<string, unknown>): Logger {\n const hasBindings = Object.keys(bindings).length > 0;\n\n function createLogMethod(\n level: \"fatal\" | \"error\" | \"warn\" | \"info\" | \"debug\" | \"trace\"\n ): LogFn {\n // Map fatal to error, trace to debug for console\n const consoleMethod =\n level === \"fatal\"\n ? \"error\"\n : level === \"trace\"\n ? \"debug\"\n : level;\n const consoleFn = console[consoleMethod] || console.log;\n const prefix = `[${level.toUpperCase()}]`;\n\n return (msgOrObj: unknown, ...args: unknown[]) => {\n if (typeof msgOrObj === \"string\") {\n // Printf style: logger.info(\"Hello %s\", \"world\")\n if (hasBindings) {\n consoleFn(prefix, bindings, msgOrObj, ...args);\n } else {\n consoleFn(prefix, msgOrObj, ...args);\n }\n } else {\n // Structured style: logger.info({ userId: 1 }, \"User logged in\")\n const [msg, ...restArgs] = args;\n const mergedObj = hasBindings\n ? { ...bindings, ...(msgOrObj as object) }\n : msgOrObj;\n if (msg) {\n consoleFn(prefix, mergedObj, msg, ...restArgs);\n } else {\n consoleFn(prefix, mergedObj);\n }\n }\n };\n }\n\n return {\n fatal: createLogMethod(\"fatal\"),\n error: createLogMethod(\"error\"),\n warn: createLogMethod(\"warn\"),\n info: createLogMethod(\"info\"),\n debug: createLogMethod(\"debug\"),\n trace: createLogMethod(\"trace\"),\n child: (newBindings: Record<string, unknown>) => {\n return createChildConsoleLogger({ ...bindings, ...newBindings });\n },\n };\n}\n\n","/**\n * Type guard to check if an error has a code property.\n */\nexport function isErrorWithCode(\n error: unknown\n): error is Error & { code: string } {\n return (\n error instanceof Error &&\n \"code\" in error &&\n typeof error.code === \"string\"\n );\n}\n\n"],"mappings":";AAAA,YAAY,OAAO;;;ACsGnB,IAAM,OAAc,CAAC,cAAuB,UAAqB;AAAC;AAsB3D,IAAM,aAAqB;AAAA,EAChC,OAAO;AAAA,EACP,OAAO;AAAA,EACP,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA,EACP,OAAO;AAAA,EACP,OAAO,MAAM;AACf;AAWO,SAAS,sBAA8B;AAC5C,SAAO,yBAAyB,CAAC,CAAC;AACpC;AAEA,SAAS,yBAAyB,UAA2C;AAC3E,QAAM,cAAc,OAAO,KAAK,QAAQ,EAAE,SAAS;AAEnD,WAAS,gBACP,OACO;AAEP,UAAM,gBACJ,UAAU,UACN,UACA,UAAU,UACR,UACA;AACR,UAAM,YAAY,QAAQ,aAAa,KAAK,QAAQ;AACpD,UAAM,SAAS,IAAI,MAAM,YAAY,CAAC;AAEtC,WAAO,CAAC,aAAsB,SAAoB;AAChD,UAAI,OAAO,aAAa,UAAU;AAEhC,YAAI,aAAa;AACf,oBAAU,QAAQ,UAAU,UAAU,GAAG,IAAI;AAAA,QAC/C,OAAO;AACL,oBAAU,QAAQ,UAAU,GAAG,IAAI;AAAA,QACrC;AAAA,MACF,OAAO;AAEL,cAAM,CAAC,KAAK,GAAG,QAAQ,IAAI;AAC3B,cAAM,YAAY,cACd,EAAE,GAAG,UAAU,GAAI,SAAoB,IACvC;AACJ,YAAI,KAAK;AACP,oBAAU,QAAQ,WAAW,KAAK,GAAG,QAAQ;AAAA,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