@voltagent/internal 0.0.5 → 0.0.6
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 +6 -4
- package/dist/main/index.d.mts +107 -104
- package/dist/main/index.d.ts +107 -104
- package/dist/main/index.js +4 -88
- package/dist/main/index.js.map +1 -1
- package/dist/main/index.mjs +4 -86
- package/dist/main/index.mjs.map +1 -1
- package/dist/utils/index.d.mts +44 -1
- package/dist/utils/index.d.ts +44 -1
- package/dist/utils/index.js +4 -84
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +4 -84
- package/dist/utils/index.mjs.map +1 -1
- package/package.json +1 -6
- package/dist/dev/index.d.mts +0 -103
- package/dist/dev/index.d.ts +0 -103
- package/dist/dev/index.js +0 -115
- package/dist/dev/index.js.map +0 -1
- package/dist/dev/index.mjs +0 -89
- package/dist/dev/index.mjs.map +0 -1
package/dist/main/index.mjs
CHANGED
|
@@ -53,88 +53,6 @@ var __asyncGenerator = (__this, __arguments, generator) => {
|
|
|
53
53
|
};
|
|
54
54
|
var __forAwait = (obj, it, method) => (it = obj[__knownSymbol("asyncIterator")]) ? it.call(obj) : (obj = obj[__knownSymbol("iterator")](), it = {}, method = (key, fn) => (fn = obj[key]) && (it[key] = (arg) => new Promise((yes, no, done) => (arg = fn.call(obj, arg), done = arg.done, Promise.resolve(arg.value).then((value) => yes({ value, done }), no)))), method("next"), method("return"), it);
|
|
55
55
|
|
|
56
|
-
// src/dev/logger.ts
|
|
57
|
-
function createDevLogger(options) {
|
|
58
|
-
const isDev = typeof (options == null ? void 0 : options.dev) === "function" ? options.dev : () => {
|
|
59
|
-
var _a;
|
|
60
|
-
return (_a = options == null ? void 0 : options.dev) != null ? _a : isDevNodeEnv();
|
|
61
|
-
};
|
|
62
|
-
return {
|
|
63
|
-
/**
|
|
64
|
-
* Log a message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not "development".
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```typescript
|
|
68
|
-
* devLogger.info("Hello, world!");
|
|
69
|
-
*
|
|
70
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] INFO: Hello, world!
|
|
71
|
-
* ```
|
|
72
|
-
*
|
|
73
|
-
* @param message - The message to log.
|
|
74
|
-
* @param args - The arguments to log.
|
|
75
|
-
*/
|
|
76
|
-
info: /* @__PURE__ */ __name((message, ...args) => {
|
|
77
|
-
if (isDev()) {
|
|
78
|
-
console.info(formatLogPrefix("INFO"), message, ...args);
|
|
79
|
-
}
|
|
80
|
-
}, "info"),
|
|
81
|
-
/**
|
|
82
|
-
* Log a warning message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not "development".
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```typescript
|
|
86
|
-
* devLogger.warn("Hello, world!");
|
|
87
|
-
*
|
|
88
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] WARN: Hello, world!
|
|
89
|
-
* ```
|
|
90
|
-
*
|
|
91
|
-
* @param message - The message to log.
|
|
92
|
-
* @param args - The arguments to log.
|
|
93
|
-
*/
|
|
94
|
-
warn: /* @__PURE__ */ __name((message, ...args) => {
|
|
95
|
-
if (isDev()) {
|
|
96
|
-
console.warn(formatLogPrefix("WARN"), message, ...args);
|
|
97
|
-
}
|
|
98
|
-
}, "warn"),
|
|
99
|
-
/**
|
|
100
|
-
* Log a warning message to the console if the environment is development.
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
* ```typescript
|
|
104
|
-
* devLogger.error("Hello, world!");
|
|
105
|
-
*
|
|
106
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] ERROR: Hello, world!
|
|
107
|
-
* ```
|
|
108
|
-
*
|
|
109
|
-
* @param message - The message to log.
|
|
110
|
-
* @param args - The arguments to log.
|
|
111
|
-
*/
|
|
112
|
-
error: /* @__PURE__ */ __name((message, ...args) => {
|
|
113
|
-
if (isDev()) {
|
|
114
|
-
console.error(formatLogPrefix("ERROR"), message, ...args);
|
|
115
|
-
}
|
|
116
|
-
}, "error"),
|
|
117
|
-
debug: /* @__PURE__ */ __name((_message, ..._args) => {
|
|
118
|
-
return;
|
|
119
|
-
}, "debug")
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
__name(createDevLogger, "createDevLogger");
|
|
123
|
-
var logger_default = createDevLogger();
|
|
124
|
-
function isDevNodeEnv() {
|
|
125
|
-
const nodeEnv = process.env.NODE_ENV;
|
|
126
|
-
return nodeEnv !== "production" && nodeEnv !== "test" && nodeEnv !== "ci";
|
|
127
|
-
}
|
|
128
|
-
__name(isDevNodeEnv, "isDevNodeEnv");
|
|
129
|
-
function formatLogPrefix(level) {
|
|
130
|
-
return `[VoltAgent] [${timestamp()}] ${level}: `;
|
|
131
|
-
}
|
|
132
|
-
__name(formatLogPrefix, "formatLogPrefix");
|
|
133
|
-
function timestamp() {
|
|
134
|
-
return (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").slice(0, -1);
|
|
135
|
-
}
|
|
136
|
-
__name(timestamp, "timestamp");
|
|
137
|
-
|
|
138
56
|
// src/test/conversions.ts
|
|
139
57
|
function convertReadableStreamToArray(stream) {
|
|
140
58
|
return __async(this, null, function* () {
|
|
@@ -237,11 +155,13 @@ function isEmptyObject(obj) {
|
|
|
237
155
|
__name(isEmptyObject, "isEmptyObject");
|
|
238
156
|
|
|
239
157
|
// src/utils/objects.ts
|
|
240
|
-
function deepClone(obj) {
|
|
158
|
+
function deepClone(obj, logger) {
|
|
241
159
|
try {
|
|
242
160
|
return JSON.parse(JSON.stringify(obj));
|
|
243
161
|
} catch (error) {
|
|
244
|
-
|
|
162
|
+
if (logger) {
|
|
163
|
+
logger.warn("Failed to deep clone object, using shallow clone", { error });
|
|
164
|
+
}
|
|
245
165
|
if (obj === null || typeof obj !== "object") {
|
|
246
166
|
return obj;
|
|
247
167
|
}
|
|
@@ -278,9 +198,7 @@ export {
|
|
|
278
198
|
convertReadableStreamToArray,
|
|
279
199
|
convertResponseStreamToArray,
|
|
280
200
|
createAsyncIterableStream,
|
|
281
|
-
createDevLogger,
|
|
282
201
|
deepClone,
|
|
283
|
-
logger_default as devLogger,
|
|
284
202
|
hasKey,
|
|
285
203
|
isEmptyObject,
|
|
286
204
|
isFunction,
|
package/dist/main/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/dev/logger.ts","../../src/test/conversions.ts","../../src/utils/lang.ts","../../src/utils/objects.ts","../../src/utils/async-iterable-stream.ts"],"sourcesContent":["export interface DevLoggerOptions {\n dev: boolean | (() => boolean);\n}\n\n/**\n * A logger for development purposes, that will not pollute the production logs (aka if process.env.NODE_ENV is not \"development\").\n *\n * @example\n * ```typescript\n * devLogger.info(\"Hello, world!\");\n * ```\n */\nexport function createDevLogger(options?: DevLoggerOptions) {\n const isDev =\n typeof options?.dev === \"function\" ? options.dev : () => options?.dev ?? isDevNodeEnv();\n\n return {\n /**\n * Log a message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not \"development\".\n *\n * @example\n * ```typescript\n * devLogger.info(\"Hello, world!\");\n *\n * // output: [VoltAgent] [2021-01-01T00:00:00.000Z] INFO: Hello, world!\n * ```\n *\n * @param message - The message to log.\n * @param args - The arguments to log.\n */\n info: (message?: any, ...args: any[]) => {\n if (isDev()) {\n console.info(formatLogPrefix(\"INFO\"), message, ...args);\n }\n },\n /**\n * Log a warning message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not \"development\".\n *\n * @example\n * ```typescript\n * devLogger.warn(\"Hello, world!\");\n *\n * // output: [VoltAgent] [2021-01-01T00:00:00.000Z] WARN: Hello, world!\n * ```\n *\n * @param message - The message to log.\n * @param args - The arguments to log.\n */\n warn: (message?: any, ...args: any[]) => {\n if (isDev()) {\n console.warn(formatLogPrefix(\"WARN\"), message, ...args);\n }\n },\n /**\n * Log a warning message to the console if the environment is development.\n *\n * @example\n * ```typescript\n * devLogger.error(\"Hello, world!\");\n *\n * // output: [VoltAgent] [2021-01-01T00:00:00.000Z] ERROR: Hello, world!\n * ```\n *\n * @param message - The message to log.\n * @param args - The arguments to log.\n */\n error: (message?: any, ...args: any[]) => {\n if (isDev()) {\n console.error(formatLogPrefix(\"ERROR\"), message, ...args);\n }\n },\n\n debug: (_message?: any, ..._args: any[]) => {\n // todo: implement debug logging with pino\n return;\n },\n };\n}\n\nexport default createDevLogger();\n\nfunction isDevNodeEnv() {\n const nodeEnv = process.env.NODE_ENV;\n return nodeEnv !== \"production\" && nodeEnv !== \"test\" && nodeEnv !== \"ci\";\n}\n\nfunction formatLogPrefix(level: \"INFO\" | \"WARN\" | \"ERROR\" | \"DEBUG\"): string {\n return `[VoltAgent] [${timestamp()}] ${level}: `;\n}\n\nfunction timestamp(): string {\n return new Date().toISOString().replace(\"T\", \" \").slice(0, -1);\n}\n","/**\n * Convert a readable stream to an array\n * @param stream - The readable stream to convert\n * @returns The array of values\n */\nexport async function convertReadableStreamToArray<T>(stream: ReadableStream<T>): Promise<T[]> {\n const reader = stream.getReader();\n const result: T[] = [];\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n result.push(value);\n }\n\n return result;\n}\n\n/**\n * Convert an array to an async iterable\n * @param values - The array to convert\n * @returns The async iterable\n */\nexport function convertArrayToAsyncIterable<T>(values: T[]): AsyncIterable<T> {\n return {\n async *[Symbol.asyncIterator]() {\n for (const value of values) {\n yield value;\n }\n },\n };\n}\n\n/**\n * Convert an async iterable to an array\n * @param iterable - The async iterable to convert\n * @returns The array of values\n */\nexport async function convertAsyncIterableToArray<T>(iterable: AsyncIterable<T>): Promise<T[]> {\n const result: T[] = [];\n for await (const item of iterable) {\n result.push(item);\n }\n return result;\n}\n\n/**\n * Convert an array to a readable stream\n * @param values - The array to convert\n * @returns The readable stream\n */\nexport function convertArrayToReadableStream<T>(values: T[]): ReadableStream<T> {\n return new ReadableStream({\n start(controller) {\n try {\n for (const value of values) {\n controller.enqueue(value);\n }\n } finally {\n controller.close();\n }\n },\n });\n}\n\n/**\n * Convert a response stream to an array\n * @param response - The response to convert\n * @returns The array of values\n */\nexport async function convertResponseStreamToArray(response: Response): Promise<string[]> {\n // biome-ignore lint/style/noNonNullAssertion: ignore this\n return convertReadableStreamToArray(response.body!.pipeThrough(new TextDecoderStream()));\n}\n","import type { EmptyObject } from \"type-fest\";\nimport type { AnyFunction, Nil, PlainObject } from \"../types\";\n\n/**\n * Check if a value is nil\n *\n * @param obj - The value to check\n * @returns True if the value is nil, false otherwise\n */\nexport function isNil(obj: unknown): obj is Nil {\n return obj === null || obj === undefined;\n}\n\n/**\n * Check if an object is a JS object\n *\n * @param obj - The object to check\n * @returns True if the object is a JS object}\n */\nexport function isObject<T extends object>(obj: unknown): obj is T {\n return (typeof obj === \"object\" || typeof obj === \"function\") && !isNil(obj);\n}\n\n/**\n * Check if a value is a function\n *\n * @param obj - The value to check\n * @returns True if the value is a function, false otherwise\n */\nexport function isFunction<T extends AnyFunction>(obj: unknown): obj is T {\n return typeof obj === \"function\";\n}\n\n/**\n * Check if an object is a plain object (i.e. a JS object but not including arrays or functions)\n *\n * @param obj - The object to check\n * @returns True if the object is a plain object, false otherwise.\n */\nexport function isPlainObject<T extends PlainObject>(obj: unknown): obj is T {\n if (!isObject(obj)) {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(obj);\n return prototype === Object.prototype || prototype === null;\n}\n\n/**\n * Check if an object is an empty object\n *\n * @param obj - The object to check\n * @returns True if the object is an empty object, false otherwise\n */\nexport function isEmptyObject(obj: unknown): obj is EmptyObject {\n if (!isObject(obj)) {\n return false;\n }\n\n // Check for own string and symbol properties (enumerable or not)\n if (Object.getOwnPropertyNames(obj).length > 0 || Object.getOwnPropertySymbols(obj).length > 0) {\n return false;\n }\n\n return true;\n}\n","import type { SetRequired } from \"type-fest\";\nimport { devLogger } from \"../dev\";\nimport type { PlainObject } from \"../types\";\nimport { isObject } from \"./lang\";\n\n/**\n * Deep clone an object using JSON serialization with fallback to shallow clone\n *\n * @param obj - The object to clone\n * @returns A deep copy of the object, or shallow copy if JSON serialization fails\n */\nexport function deepClone<T>(obj: T): T {\n try {\n return JSON.parse(JSON.stringify(obj));\n } catch (error) {\n devLogger.warn(\"Failed to deep clone object, using shallow clone:\", error);\n // Fallback to shallow clone for primitive types and simple objects\n if (obj === null || typeof obj !== \"object\") {\n return obj;\n }\n return { ...obj } as T;\n }\n}\n\n/**\n * Check if an object has a key\n *\n * @param obj - The object to check\n * @param key - The key to check\n * @returns True if the object has the key, false otherwise\n */\nexport function hasKey<T extends PlainObject, K extends string>(\n obj: T,\n key: K,\n): obj is T & SetRequired<T, K> {\n return isObject(obj) && key in obj;\n}\n","import type { Merge } from \"type-fest\";\n\n/**\n * An async iterable stream that can be read from.\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = getStream();\n * for await (const chunk of stream) {\n * console.log(chunk);\n * }\n * ```\n */\nexport type AsyncIterableStream<T> = Merge<AsyncIterable<T>, ReadableStream<T>>;\n\n/**\n * Create an async iterable stream from a readable stream.\n *\n * This is useful for creating an async iterable stream from a readable stream.\n *\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = createAsyncIterableStream(new ReadableStream({\n * start(controller) {\n * controller.enqueue(\"Hello\");\n * controller.close();\n * },\n * }));\n * ```\n * @param source The readable stream to create an async iterable stream from.\n * @returns The async iterable stream.\n */\nexport function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream<T> {\n const stream = source.pipeThrough(new TransformStream<T, T>());\n\n (stream as AsyncIterableStream<T>)[Symbol.asyncIterator] = () => {\n const reader = stream.getReader();\n return {\n async next(): Promise<IteratorResult<T>> {\n const { done, value } = await reader.read();\n return done ? { done: true, value: undefined } : { done: false, value };\n },\n };\n };\n\n return stream as AsyncIterableStream<T>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYO,SAAS,gBAAgB,SAA4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
|
|
1
|
+
{"version":3,"sources":["../../src/test/conversions.ts","../../src/utils/lang.ts","../../src/utils/objects.ts","../../src/utils/async-iterable-stream.ts"],"sourcesContent":["/**\n * Convert a readable stream to an array\n * @param stream - The readable stream to convert\n * @returns The array of values\n */\nexport async function convertReadableStreamToArray<T>(stream: ReadableStream<T>): Promise<T[]> {\n const reader = stream.getReader();\n const result: T[] = [];\n\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n result.push(value);\n }\n\n return result;\n}\n\n/**\n * Convert an array to an async iterable\n * @param values - The array to convert\n * @returns The async iterable\n */\nexport function convertArrayToAsyncIterable<T>(values: T[]): AsyncIterable<T> {\n return {\n async *[Symbol.asyncIterator]() {\n for (const value of values) {\n yield value;\n }\n },\n };\n}\n\n/**\n * Convert an async iterable to an array\n * @param iterable - The async iterable to convert\n * @returns The array of values\n */\nexport async function convertAsyncIterableToArray<T>(iterable: AsyncIterable<T>): Promise<T[]> {\n const result: T[] = [];\n for await (const item of iterable) {\n result.push(item);\n }\n return result;\n}\n\n/**\n * Convert an array to a readable stream\n * @param values - The array to convert\n * @returns The readable stream\n */\nexport function convertArrayToReadableStream<T>(values: T[]): ReadableStream<T> {\n return new ReadableStream({\n start(controller) {\n try {\n for (const value of values) {\n controller.enqueue(value);\n }\n } finally {\n controller.close();\n }\n },\n });\n}\n\n/**\n * Convert a response stream to an array\n * @param response - The response to convert\n * @returns The array of values\n */\nexport async function convertResponseStreamToArray(response: Response): Promise<string[]> {\n // biome-ignore lint/style/noNonNullAssertion: ignore this\n return convertReadableStreamToArray(response.body!.pipeThrough(new TextDecoderStream()));\n}\n","import type { EmptyObject } from \"type-fest\";\nimport type { AnyFunction, Nil, PlainObject } from \"../types\";\n\n/**\n * Check if a value is nil\n *\n * @param obj - The value to check\n * @returns True if the value is nil, false otherwise\n */\nexport function isNil(obj: unknown): obj is Nil {\n return obj === null || obj === undefined;\n}\n\n/**\n * Check if an object is a JS object\n *\n * @param obj - The object to check\n * @returns True if the object is a JS object}\n */\nexport function isObject<T extends object>(obj: unknown): obj is T {\n return (typeof obj === \"object\" || typeof obj === \"function\") && !isNil(obj);\n}\n\n/**\n * Check if a value is a function\n *\n * @param obj - The value to check\n * @returns True if the value is a function, false otherwise\n */\nexport function isFunction<T extends AnyFunction>(obj: unknown): obj is T {\n return typeof obj === \"function\";\n}\n\n/**\n * Check if an object is a plain object (i.e. a JS object but not including arrays or functions)\n *\n * @param obj - The object to check\n * @returns True if the object is a plain object, false otherwise.\n */\nexport function isPlainObject<T extends PlainObject>(obj: unknown): obj is T {\n if (!isObject(obj)) {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(obj);\n return prototype === Object.prototype || prototype === null;\n}\n\n/**\n * Check if an object is an empty object\n *\n * @param obj - The object to check\n * @returns True if the object is an empty object, false otherwise\n */\nexport function isEmptyObject(obj: unknown): obj is EmptyObject {\n if (!isObject(obj)) {\n return false;\n }\n\n // Check for own string and symbol properties (enumerable or not)\n if (Object.getOwnPropertyNames(obj).length > 0 || Object.getOwnPropertySymbols(obj).length > 0) {\n return false;\n }\n\n return true;\n}\n","import type { SetRequired } from \"type-fest\";\nimport type { PlainObject } from \"../types\";\nimport { isObject } from \"./lang\";\nimport type { Logger } from \"../logger/types\";\n\n/**\n * Deep clone an object using JSON serialization with fallback to shallow clone\n *\n * @param obj - The object to clone\n * @param logger - Optional logger for warnings\n * @returns A deep copy of the object, or shallow copy if JSON serialization fails\n */\nexport function deepClone<T>(obj: T, logger?: Logger): T {\n try {\n return JSON.parse(JSON.stringify(obj));\n } catch (error) {\n if (logger) {\n logger.warn(\"Failed to deep clone object, using shallow clone\", { error });\n }\n // Fallback to shallow clone for primitive types and simple objects\n if (obj === null || typeof obj !== \"object\") {\n return obj;\n }\n return { ...obj } as T;\n }\n}\n\n/**\n * Check if an object has a key\n *\n * @param obj - The object to check\n * @param key - The key to check\n * @returns True if the object has the key, false otherwise\n */\nexport function hasKey<T extends PlainObject, K extends string>(\n obj: T,\n key: K,\n): obj is T & SetRequired<T, K> {\n return isObject(obj) && key in obj;\n}\n","import type { Merge } from \"type-fest\";\n\n/**\n * An async iterable stream that can be read from.\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = getStream();\n * for await (const chunk of stream) {\n * console.log(chunk);\n * }\n * ```\n */\nexport type AsyncIterableStream<T> = Merge<AsyncIterable<T>, ReadableStream<T>>;\n\n/**\n * Create an async iterable stream from a readable stream.\n *\n * This is useful for creating an async iterable stream from a readable stream.\n *\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = createAsyncIterableStream(new ReadableStream({\n * start(controller) {\n * controller.enqueue(\"Hello\");\n * controller.close();\n * },\n * }));\n * ```\n * @param source The readable stream to create an async iterable stream from.\n * @returns The async iterable stream.\n */\nexport function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream<T> {\n const stream = source.pipeThrough(new TransformStream<T, T>());\n\n (stream as AsyncIterableStream<T>)[Symbol.asyncIterator] = () => {\n const reader = stream.getReader();\n return {\n async next(): Promise<IteratorResult<T>> {\n const { done, value } = await reader.read();\n return done ? { done: true, value: undefined } : { done: false, value };\n },\n };\n };\n\n return stream as AsyncIterableStream<T>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,SAAsB,6BAAgC,QAAyC;AAAA;AAC7F,UAAM,SAAS,OAAO,UAAU;AAChC,UAAM,SAAc,CAAC;AAErB,WAAO,MAAM;AACX,YAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,UAAI,KAAM;AACV,aAAO,KAAK,KAAK;AAAA,IACnB;AAEA,WAAO;AAAA,EACT;AAAA;AAXsB;AAkBf,SAAS,4BAA+B,QAA+B;AAC5E,SAAO;AAAA,IACL,CAAQ,OAAO,aAAa,IAAI;AAAA;AAC9B,mBAAW,SAAS,QAAQ;AAC1B,gBAAM;AAAA,QACR;AAAA,MACF;AAAA;AAAA,EACF;AACF;AARgB;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AC9F,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAXgB;;;AC1CT,SAAS,UAAa,KAAQ,QAAoB;AACvD,MAAI;AACF,WAAO,KAAK,MAAM,KAAK,UAAU,GAAG,CAAC;AAAA,EACvC,SAAS,OAAO;AACd,QAAI,QAAQ;AACV,aAAO,KAAK,oDAAoD,EAAE,MAAM,CAAC;AAAA,IAC3E;AAEA,QAAI,QAAQ,QAAQ,OAAO,QAAQ,UAAU;AAC3C,aAAO;AAAA,IACT;AACA,WAAO,mBAAK;AAAA,EACd;AACF;AAbgB;AAsBT,SAAS,OACd,KACA,KAC8B;AAC9B,SAAO,SAAS,GAAG,KAAK,OAAO;AACjC;AALgB;;;ACHT,SAAS,0BAA6B,QAAmD;AAC9F,QAAM,SAAS,OAAO,YAAY,IAAI,gBAAsB,CAAC;AAE7D,EAAC,OAAkC,OAAO,aAAa,IAAI,MAAM;AAC/D,UAAM,SAAS,OAAO,UAAU;AAChC,WAAO;AAAA,MACC,OAAmC;AAAA;AACvC,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,iBAAO,OAAO,EAAE,MAAM,MAAM,OAAO,OAAU,IAAI,EAAE,MAAM,OAAO,MAAM;AAAA,QACxE;AAAA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAdgB;","names":[]}
|
package/dist/utils/index.d.mts
CHANGED
|
@@ -22,13 +22,56 @@ type AnySyncFunction = (...args: unknown[]) => unknown;
|
|
|
22
22
|
*/
|
|
23
23
|
type AnyFunction = AnyAsyncFunction | AnySyncFunction;
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Log function signatures
|
|
27
|
+
*/
|
|
28
|
+
interface LogFn {
|
|
29
|
+
(msg: string, context?: object): void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Minimal logger interface for VoltAgent
|
|
33
|
+
* This interface is implemented by @voltagent/logger and can be implemented by other logging solutions
|
|
34
|
+
*/
|
|
35
|
+
interface Logger {
|
|
36
|
+
/**
|
|
37
|
+
* Log at trace level - most detailed level
|
|
38
|
+
*/
|
|
39
|
+
trace: LogFn;
|
|
40
|
+
/**
|
|
41
|
+
* Log at debug level - detailed information for debugging
|
|
42
|
+
*/
|
|
43
|
+
debug: LogFn;
|
|
44
|
+
/**
|
|
45
|
+
* Log at info level - general informational messages
|
|
46
|
+
*/
|
|
47
|
+
info: LogFn;
|
|
48
|
+
/**
|
|
49
|
+
* Log at warn level - warning messages
|
|
50
|
+
*/
|
|
51
|
+
warn: LogFn;
|
|
52
|
+
/**
|
|
53
|
+
* Log at error level - error messages
|
|
54
|
+
*/
|
|
55
|
+
error: LogFn;
|
|
56
|
+
/**
|
|
57
|
+
* Log at fatal level - fatal error messages
|
|
58
|
+
*/
|
|
59
|
+
fatal: LogFn;
|
|
60
|
+
/**
|
|
61
|
+
* Create a child logger with additional context
|
|
62
|
+
* @param bindings - Additional context to bind to the child logger
|
|
63
|
+
*/
|
|
64
|
+
child(bindings: Record<string, any>): Logger;
|
|
65
|
+
}
|
|
66
|
+
|
|
25
67
|
/**
|
|
26
68
|
* Deep clone an object using JSON serialization with fallback to shallow clone
|
|
27
69
|
*
|
|
28
70
|
* @param obj - The object to clone
|
|
71
|
+
* @param logger - Optional logger for warnings
|
|
29
72
|
* @returns A deep copy of the object, or shallow copy if JSON serialization fails
|
|
30
73
|
*/
|
|
31
|
-
declare function deepClone<T>(obj: T): T;
|
|
74
|
+
declare function deepClone<T>(obj: T, logger?: Logger): T;
|
|
32
75
|
/**
|
|
33
76
|
* Check if an object has a key
|
|
34
77
|
*
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -22,13 +22,56 @@ type AnySyncFunction = (...args: unknown[]) => unknown;
|
|
|
22
22
|
*/
|
|
23
23
|
type AnyFunction = AnyAsyncFunction | AnySyncFunction;
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Log function signatures
|
|
27
|
+
*/
|
|
28
|
+
interface LogFn {
|
|
29
|
+
(msg: string, context?: object): void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Minimal logger interface for VoltAgent
|
|
33
|
+
* This interface is implemented by @voltagent/logger and can be implemented by other logging solutions
|
|
34
|
+
*/
|
|
35
|
+
interface Logger {
|
|
36
|
+
/**
|
|
37
|
+
* Log at trace level - most detailed level
|
|
38
|
+
*/
|
|
39
|
+
trace: LogFn;
|
|
40
|
+
/**
|
|
41
|
+
* Log at debug level - detailed information for debugging
|
|
42
|
+
*/
|
|
43
|
+
debug: LogFn;
|
|
44
|
+
/**
|
|
45
|
+
* Log at info level - general informational messages
|
|
46
|
+
*/
|
|
47
|
+
info: LogFn;
|
|
48
|
+
/**
|
|
49
|
+
* Log at warn level - warning messages
|
|
50
|
+
*/
|
|
51
|
+
warn: LogFn;
|
|
52
|
+
/**
|
|
53
|
+
* Log at error level - error messages
|
|
54
|
+
*/
|
|
55
|
+
error: LogFn;
|
|
56
|
+
/**
|
|
57
|
+
* Log at fatal level - fatal error messages
|
|
58
|
+
*/
|
|
59
|
+
fatal: LogFn;
|
|
60
|
+
/**
|
|
61
|
+
* Create a child logger with additional context
|
|
62
|
+
* @param bindings - Additional context to bind to the child logger
|
|
63
|
+
*/
|
|
64
|
+
child(bindings: Record<string, any>): Logger;
|
|
65
|
+
}
|
|
66
|
+
|
|
25
67
|
/**
|
|
26
68
|
* Deep clone an object using JSON serialization with fallback to shallow clone
|
|
27
69
|
*
|
|
28
70
|
* @param obj - The object to clone
|
|
71
|
+
* @param logger - Optional logger for warnings
|
|
29
72
|
* @returns A deep copy of the object, or shallow copy if JSON serialization fails
|
|
30
73
|
*/
|
|
31
|
-
declare function deepClone<T>(obj: T): T;
|
|
74
|
+
declare function deepClone<T>(obj: T, logger?: Logger): T;
|
|
32
75
|
/**
|
|
33
76
|
* Check if an object has a key
|
|
34
77
|
*
|
package/dist/utils/index.js
CHANGED
|
@@ -66,88 +66,6 @@ __export(index_exports, {
|
|
|
66
66
|
});
|
|
67
67
|
module.exports = __toCommonJS(index_exports);
|
|
68
68
|
|
|
69
|
-
// src/dev/logger.ts
|
|
70
|
-
function createDevLogger(options) {
|
|
71
|
-
const isDev = typeof (options == null ? void 0 : options.dev) === "function" ? options.dev : () => {
|
|
72
|
-
var _a;
|
|
73
|
-
return (_a = options == null ? void 0 : options.dev) != null ? _a : isDevNodeEnv();
|
|
74
|
-
};
|
|
75
|
-
return {
|
|
76
|
-
/**
|
|
77
|
-
* Log a message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not "development".
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```typescript
|
|
81
|
-
* devLogger.info("Hello, world!");
|
|
82
|
-
*
|
|
83
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] INFO: Hello, world!
|
|
84
|
-
* ```
|
|
85
|
-
*
|
|
86
|
-
* @param message - The message to log.
|
|
87
|
-
* @param args - The arguments to log.
|
|
88
|
-
*/
|
|
89
|
-
info: /* @__PURE__ */ __name((message, ...args) => {
|
|
90
|
-
if (isDev()) {
|
|
91
|
-
console.info(formatLogPrefix("INFO"), message, ...args);
|
|
92
|
-
}
|
|
93
|
-
}, "info"),
|
|
94
|
-
/**
|
|
95
|
-
* Log a warning message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not "development".
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* ```typescript
|
|
99
|
-
* devLogger.warn("Hello, world!");
|
|
100
|
-
*
|
|
101
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] WARN: Hello, world!
|
|
102
|
-
* ```
|
|
103
|
-
*
|
|
104
|
-
* @param message - The message to log.
|
|
105
|
-
* @param args - The arguments to log.
|
|
106
|
-
*/
|
|
107
|
-
warn: /* @__PURE__ */ __name((message, ...args) => {
|
|
108
|
-
if (isDev()) {
|
|
109
|
-
console.warn(formatLogPrefix("WARN"), message, ...args);
|
|
110
|
-
}
|
|
111
|
-
}, "warn"),
|
|
112
|
-
/**
|
|
113
|
-
* Log a warning message to the console if the environment is development.
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```typescript
|
|
117
|
-
* devLogger.error("Hello, world!");
|
|
118
|
-
*
|
|
119
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] ERROR: Hello, world!
|
|
120
|
-
* ```
|
|
121
|
-
*
|
|
122
|
-
* @param message - The message to log.
|
|
123
|
-
* @param args - The arguments to log.
|
|
124
|
-
*/
|
|
125
|
-
error: /* @__PURE__ */ __name((message, ...args) => {
|
|
126
|
-
if (isDev()) {
|
|
127
|
-
console.error(formatLogPrefix("ERROR"), message, ...args);
|
|
128
|
-
}
|
|
129
|
-
}, "error"),
|
|
130
|
-
debug: /* @__PURE__ */ __name((_message, ..._args) => {
|
|
131
|
-
return;
|
|
132
|
-
}, "debug")
|
|
133
|
-
};
|
|
134
|
-
}
|
|
135
|
-
__name(createDevLogger, "createDevLogger");
|
|
136
|
-
var logger_default = createDevLogger();
|
|
137
|
-
function isDevNodeEnv() {
|
|
138
|
-
const nodeEnv = process.env.NODE_ENV;
|
|
139
|
-
return nodeEnv !== "production" && nodeEnv !== "test" && nodeEnv !== "ci";
|
|
140
|
-
}
|
|
141
|
-
__name(isDevNodeEnv, "isDevNodeEnv");
|
|
142
|
-
function formatLogPrefix(level) {
|
|
143
|
-
return `[VoltAgent] [${timestamp()}] ${level}: `;
|
|
144
|
-
}
|
|
145
|
-
__name(formatLogPrefix, "formatLogPrefix");
|
|
146
|
-
function timestamp() {
|
|
147
|
-
return (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").slice(0, -1);
|
|
148
|
-
}
|
|
149
|
-
__name(timestamp, "timestamp");
|
|
150
|
-
|
|
151
69
|
// src/utils/lang.ts
|
|
152
70
|
function isNil(obj) {
|
|
153
71
|
return obj === null || obj === void 0;
|
|
@@ -181,11 +99,13 @@ function isEmptyObject(obj) {
|
|
|
181
99
|
__name(isEmptyObject, "isEmptyObject");
|
|
182
100
|
|
|
183
101
|
// src/utils/objects.ts
|
|
184
|
-
function deepClone(obj) {
|
|
102
|
+
function deepClone(obj, logger) {
|
|
185
103
|
try {
|
|
186
104
|
return JSON.parse(JSON.stringify(obj));
|
|
187
105
|
} catch (error) {
|
|
188
|
-
|
|
106
|
+
if (logger) {
|
|
107
|
+
logger.warn("Failed to deep clone object, using shallow clone", { error });
|
|
108
|
+
}
|
|
189
109
|
if (obj === null || typeof obj !== "object") {
|
|
190
110
|
return obj;
|
|
191
111
|
}
|
package/dist/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/index.ts","../../src/dev/logger.ts","../../src/utils/lang.ts","../../src/utils/objects.ts","../../src/utils/async-iterable-stream.ts"],"sourcesContent":["export { deepClone, hasKey } from \"./objects\";\nexport { isNil, isObject, isEmptyObject, isFunction, isPlainObject } from \"./lang\";\nexport type { AsyncIterableStream } from \"./async-iterable-stream\";\nexport { createAsyncIterableStream } from \"./async-iterable-stream\";\n","export interface DevLoggerOptions {\n dev: boolean | (() => boolean);\n}\n\n/**\n * A logger for development purposes, that will not pollute the production logs (aka if process.env.NODE_ENV is not \"development\").\n *\n * @example\n * ```typescript\n * devLogger.info(\"Hello, world!\");\n * ```\n */\nexport function createDevLogger(options?: DevLoggerOptions) {\n const isDev =\n typeof options?.dev === \"function\" ? options.dev : () => options?.dev ?? isDevNodeEnv();\n\n return {\n /**\n * Log a message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not \"development\".\n *\n * @example\n * ```typescript\n * devLogger.info(\"Hello, world!\");\n *\n * // output: [VoltAgent] [2021-01-01T00:00:00.000Z] INFO: Hello, world!\n * ```\n *\n * @param message - The message to log.\n * @param args - The arguments to log.\n */\n info: (message?: any, ...args: any[]) => {\n if (isDev()) {\n console.info(formatLogPrefix(\"INFO\"), message, ...args);\n }\n },\n /**\n * Log a warning message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not \"development\".\n *\n * @example\n * ```typescript\n * devLogger.warn(\"Hello, world!\");\n *\n * // output: [VoltAgent] [2021-01-01T00:00:00.000Z] WARN: Hello, world!\n * ```\n *\n * @param message - The message to log.\n * @param args - The arguments to log.\n */\n warn: (message?: any, ...args: any[]) => {\n if (isDev()) {\n console.warn(formatLogPrefix(\"WARN\"), message, ...args);\n }\n },\n /**\n * Log a warning message to the console if the environment is development.\n *\n * @example\n * ```typescript\n * devLogger.error(\"Hello, world!\");\n *\n * // output: [VoltAgent] [2021-01-01T00:00:00.000Z] ERROR: Hello, world!\n * ```\n *\n * @param message - The message to log.\n * @param args - The arguments to log.\n */\n error: (message?: any, ...args: any[]) => {\n if (isDev()) {\n console.error(formatLogPrefix(\"ERROR\"), message, ...args);\n }\n },\n\n debug: (_message?: any, ..._args: any[]) => {\n // todo: implement debug logging with pino\n return;\n },\n };\n}\n\nexport default createDevLogger();\n\nfunction isDevNodeEnv() {\n const nodeEnv = process.env.NODE_ENV;\n return nodeEnv !== \"production\" && nodeEnv !== \"test\" && nodeEnv !== \"ci\";\n}\n\nfunction formatLogPrefix(level: \"INFO\" | \"WARN\" | \"ERROR\" | \"DEBUG\"): string {\n return `[VoltAgent] [${timestamp()}] ${level}: `;\n}\n\nfunction timestamp(): string {\n return new Date().toISOString().replace(\"T\", \" \").slice(0, -1);\n}\n","import type { EmptyObject } from \"type-fest\";\nimport type { AnyFunction, Nil, PlainObject } from \"../types\";\n\n/**\n * Check if a value is nil\n *\n * @param obj - The value to check\n * @returns True if the value is nil, false otherwise\n */\nexport function isNil(obj: unknown): obj is Nil {\n return obj === null || obj === undefined;\n}\n\n/**\n * Check if an object is a JS object\n *\n * @param obj - The object to check\n * @returns True if the object is a JS object}\n */\nexport function isObject<T extends object>(obj: unknown): obj is T {\n return (typeof obj === \"object\" || typeof obj === \"function\") && !isNil(obj);\n}\n\n/**\n * Check if a value is a function\n *\n * @param obj - The value to check\n * @returns True if the value is a function, false otherwise\n */\nexport function isFunction<T extends AnyFunction>(obj: unknown): obj is T {\n return typeof obj === \"function\";\n}\n\n/**\n * Check if an object is a plain object (i.e. a JS object but not including arrays or functions)\n *\n * @param obj - The object to check\n * @returns True if the object is a plain object, false otherwise.\n */\nexport function isPlainObject<T extends PlainObject>(obj: unknown): obj is T {\n if (!isObject(obj)) {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(obj);\n return prototype === Object.prototype || prototype === null;\n}\n\n/**\n * Check if an object is an empty object\n *\n * @param obj - The object to check\n * @returns True if the object is an empty object, false otherwise\n */\nexport function isEmptyObject(obj: unknown): obj is EmptyObject {\n if (!isObject(obj)) {\n return false;\n }\n\n // Check for own string and symbol properties (enumerable or not)\n if (Object.getOwnPropertyNames(obj).length > 0 || Object.getOwnPropertySymbols(obj).length > 0) {\n return false;\n }\n\n return true;\n}\n","import type { SetRequired } from \"type-fest\";\nimport { devLogger } from \"../dev\";\nimport type { PlainObject } from \"../types\";\nimport { isObject } from \"./lang\";\n\n/**\n * Deep clone an object using JSON serialization with fallback to shallow clone\n *\n * @param obj - The object to clone\n * @returns A deep copy of the object, or shallow copy if JSON serialization fails\n */\nexport function deepClone<T>(obj: T): T {\n try {\n return JSON.parse(JSON.stringify(obj));\n } catch (error) {\n devLogger.warn(\"Failed to deep clone object, using shallow clone:\", error);\n // Fallback to shallow clone for primitive types and simple objects\n if (obj === null || typeof obj !== \"object\") {\n return obj;\n }\n return { ...obj } as T;\n }\n}\n\n/**\n * Check if an object has a key\n *\n * @param obj - The object to check\n * @param key - The key to check\n * @returns True if the object has the key, false otherwise\n */\nexport function hasKey<T extends PlainObject, K extends string>(\n obj: T,\n key: K,\n): obj is T & SetRequired<T, K> {\n return isObject(obj) && key in obj;\n}\n","import type { Merge } from \"type-fest\";\n\n/**\n * An async iterable stream that can be read from.\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = getStream();\n * for await (const chunk of stream) {\n * console.log(chunk);\n * }\n * ```\n */\nexport type AsyncIterableStream<T> = Merge<AsyncIterable<T>, ReadableStream<T>>;\n\n/**\n * Create an async iterable stream from a readable stream.\n *\n * This is useful for creating an async iterable stream from a readable stream.\n *\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = createAsyncIterableStream(new ReadableStream({\n * start(controller) {\n * controller.enqueue(\"Hello\");\n * controller.close();\n * },\n * }));\n * ```\n * @param source The readable stream to create an async iterable stream from.\n * @returns The async iterable stream.\n */\nexport function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream<T> {\n const stream = source.pipeThrough(new TransformStream<T, T>());\n\n (stream as AsyncIterableStream<T>)[Symbol.asyncIterator] = () => {\n const reader = stream.getReader();\n return {\n async next(): Promise<IteratorResult<T>> {\n const { done, value } = await reader.read();\n return done ? { done: true, value: undefined } : { done: false, value };\n },\n };\n };\n\n return stream as AsyncIterableStream<T>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYO,SAAS,gBAAgB,SAA4B;AAC1D,QAAM,QACJ,QAAO,mCAAS,SAAQ,aAAa,QAAQ,MAAM,MAAG;AAd1D;AAc6D,oDAAS,QAAT,YAAgB,aAAa;AAAA;AAExF,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAcL,MAAM,wBAAC,YAAkB,SAAgB;AACvC,UAAI,MAAM,GAAG;AACX,gBAAQ,KAAK,gBAAgB,MAAM,GAAG,SAAS,GAAG,IAAI;AAAA,MACxD;AAAA,IACF,GAJM;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAkBN,MAAM,wBAAC,YAAkB,SAAgB;AACvC,UAAI,MAAM,GAAG;AACX,gBAAQ,KAAK,gBAAgB,MAAM,GAAG,SAAS,GAAG,IAAI;AAAA,MACx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AE,SAAS,KAAK,OAAO,sBAAsB,GAAG,EAAE,SAAS,GAAG;AAC9F,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAXgB;;;AC3CT,SAAS,UAAa,KAAW;AACtC,MAAI;AACF,WAAO,KAAK,MAAM,KAAK,UAAU,GAAG,CAAC;AAAA,EACvC,SAAS,OAAO;AACd,mBAAU,KAAK,qDAAqD,KAAK;AAEzE,QAAI,QAAQ,QAAQ,OAAO,QAAQ,UAAU;AAC3C,aAAO;AAAA,IACT;AACA,WAAO,mBAAK;AAAA,EACd;AACF;AAXgB;AAoBT,SAAS,OACd,KACA,KAC8B;AAC9B,SAAO,SAAS,GAAG,KAAK,OAAO;AACjC;AALgB;;;ACAT,SAAS,0BAA6B,QAAmD;AAC9F,QAAM,SAAS,OAAO,YAAY,IAAI,gBAAsB,CAAC;AAE7D,EAAC,OAAkC,OAAO,aAAa,IAAI,MAAM;AAC/D,UAAM,SAAS,OAAO,UAAU;AAChC,WAAO;AAAA,MACC,OAAmC;AAAA;AACvC,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,iBAAO,OAAO,EAAE,MAAM,MAAM,OAAO,OAAU,IAAI,EAAE,MAAM,OAAO,MAAM;AAAA,QACxE;AAAA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAdgB;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/utils/index.ts","../../src/utils/lang.ts","../../src/utils/objects.ts","../../src/utils/async-iterable-stream.ts"],"sourcesContent":["export { deepClone, hasKey } from \"./objects\";\nexport { isNil, isObject, isEmptyObject, isFunction, isPlainObject } from \"./lang\";\nexport type { AsyncIterableStream } from \"./async-iterable-stream\";\nexport { createAsyncIterableStream } from \"./async-iterable-stream\";\n","import type { EmptyObject } from \"type-fest\";\nimport type { AnyFunction, Nil, PlainObject } from \"../types\";\n\n/**\n * Check if a value is nil\n *\n * @param obj - The value to check\n * @returns True if the value is nil, false otherwise\n */\nexport function isNil(obj: unknown): obj is Nil {\n return obj === null || obj === undefined;\n}\n\n/**\n * Check if an object is a JS object\n *\n * @param obj - The object to check\n * @returns True if the object is a JS object}\n */\nexport function isObject<T extends object>(obj: unknown): obj is T {\n return (typeof obj === \"object\" || typeof obj === \"function\") && !isNil(obj);\n}\n\n/**\n * Check if a value is a function\n *\n * @param obj - The value to check\n * @returns True if the value is a function, false otherwise\n */\nexport function isFunction<T extends AnyFunction>(obj: unknown): obj is T {\n return typeof obj === \"function\";\n}\n\n/**\n * Check if an object is a plain object (i.e. a JS object but not including arrays or functions)\n *\n * @param obj - The object to check\n * @returns True if the object is a plain object, false otherwise.\n */\nexport function isPlainObject<T extends PlainObject>(obj: unknown): obj is T {\n if (!isObject(obj)) {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(obj);\n return prototype === Object.prototype || prototype === null;\n}\n\n/**\n * Check if an object is an empty object\n *\n * @param obj - The object to check\n * @returns True if the object is an empty object, false otherwise\n */\nexport function isEmptyObject(obj: unknown): obj is EmptyObject {\n if (!isObject(obj)) {\n return false;\n }\n\n // Check for own string and symbol properties (enumerable or not)\n if (Object.getOwnPropertyNames(obj).length > 0 || Object.getOwnPropertySymbols(obj).length > 0) {\n return false;\n }\n\n return true;\n}\n","import type { SetRequired } from \"type-fest\";\nimport type { PlainObject } from \"../types\";\nimport { isObject } from \"./lang\";\nimport type { Logger } from \"../logger/types\";\n\n/**\n * Deep clone an object using JSON serialization with fallback to shallow clone\n *\n * @param obj - The object to clone\n * @param logger - Optional logger for warnings\n * @returns A deep copy of the object, or shallow copy if JSON serialization fails\n */\nexport function deepClone<T>(obj: T, logger?: Logger): T {\n try {\n return JSON.parse(JSON.stringify(obj));\n } catch (error) {\n if (logger) {\n logger.warn(\"Failed to deep clone object, using shallow clone\", { error });\n }\n // Fallback to shallow clone for primitive types and simple objects\n if (obj === null || typeof obj !== \"object\") {\n return obj;\n }\n return { ...obj } as T;\n }\n}\n\n/**\n * Check if an object has a key\n *\n * @param obj - The object to check\n * @param key - The key to check\n * @returns True if the object has the key, false otherwise\n */\nexport function hasKey<T extends PlainObject, K extends string>(\n obj: T,\n key: K,\n): obj is T & SetRequired<T, K> {\n return isObject(obj) && key in obj;\n}\n","import type { Merge } from \"type-fest\";\n\n/**\n * An async iterable stream that can be read from.\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = getStream();\n * for await (const chunk of stream) {\n * console.log(chunk);\n * }\n * ```\n */\nexport type AsyncIterableStream<T> = Merge<AsyncIterable<T>, ReadableStream<T>>;\n\n/**\n * Create an async iterable stream from a readable stream.\n *\n * This is useful for creating an async iterable stream from a readable stream.\n *\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = createAsyncIterableStream(new ReadableStream({\n * start(controller) {\n * controller.enqueue(\"Hello\");\n * controller.close();\n * },\n * }));\n * ```\n * @param source The readable stream to create an async iterable stream from.\n * @returns The async iterable stream.\n */\nexport function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream<T> {\n const stream = source.pipeThrough(new TransformStream<T, T>());\n\n (stream as AsyncIterableStream<T>)[Symbol.asyncIterator] = () => {\n const reader = stream.getReader();\n return {\n async next(): Promise<IteratorResult<T>> {\n const { done, value } = await reader.read();\n return done ? { done: true, value: undefined } : { done: false, value };\n },\n };\n };\n\n return stream as AsyncIterableStream<T>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACSO,SAAS,MAAM,KAA0B;AAC9C,SAAO,QAAQ,QAAQ,QAAQ;AACjC;AAFgB;AAUT,SAAS,SAA2B,KAAwB;AACjE,UAAQ,OAAO,QAAQ,YAAY,OAAO,QAAQ,eAAe,CAAC,MAAM,GAAG;AAC7E;AAFgB;AAUT,SAAS,WAAkC,KAAwB;AACxE,SAAO,OAAO,QAAQ;AACxB;AAFgB;AAUT,SAAS,c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generator) => {
|
|
|
36
36
|
});
|
|
37
37
|
};
|
|
38
38
|
|
|
39
|
-
// src/dev/logger.ts
|
|
40
|
-
function createDevLogger(options) {
|
|
41
|
-
const isDev = typeof (options == null ? void 0 : options.dev) === "function" ? options.dev : () => {
|
|
42
|
-
var _a;
|
|
43
|
-
return (_a = options == null ? void 0 : options.dev) != null ? _a : isDevNodeEnv();
|
|
44
|
-
};
|
|
45
|
-
return {
|
|
46
|
-
/**
|
|
47
|
-
* Log a message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not "development".
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```typescript
|
|
51
|
-
* devLogger.info("Hello, world!");
|
|
52
|
-
*
|
|
53
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] INFO: Hello, world!
|
|
54
|
-
* ```
|
|
55
|
-
*
|
|
56
|
-
* @param message - The message to log.
|
|
57
|
-
* @param args - The arguments to log.
|
|
58
|
-
*/
|
|
59
|
-
info: /* @__PURE__ */ __name((message, ...args) => {
|
|
60
|
-
if (isDev()) {
|
|
61
|
-
console.info(formatLogPrefix("INFO"), message, ...args);
|
|
62
|
-
}
|
|
63
|
-
}, "info"),
|
|
64
|
-
/**
|
|
65
|
-
* Log a warning message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not "development".
|
|
66
|
-
*
|
|
67
|
-
* @example
|
|
68
|
-
* ```typescript
|
|
69
|
-
* devLogger.warn("Hello, world!");
|
|
70
|
-
*
|
|
71
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] WARN: Hello, world!
|
|
72
|
-
* ```
|
|
73
|
-
*
|
|
74
|
-
* @param message - The message to log.
|
|
75
|
-
* @param args - The arguments to log.
|
|
76
|
-
*/
|
|
77
|
-
warn: /* @__PURE__ */ __name((message, ...args) => {
|
|
78
|
-
if (isDev()) {
|
|
79
|
-
console.warn(formatLogPrefix("WARN"), message, ...args);
|
|
80
|
-
}
|
|
81
|
-
}, "warn"),
|
|
82
|
-
/**
|
|
83
|
-
* Log a warning message to the console if the environment is development.
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```typescript
|
|
87
|
-
* devLogger.error("Hello, world!");
|
|
88
|
-
*
|
|
89
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] ERROR: Hello, world!
|
|
90
|
-
* ```
|
|
91
|
-
*
|
|
92
|
-
* @param message - The message to log.
|
|
93
|
-
* @param args - The arguments to log.
|
|
94
|
-
*/
|
|
95
|
-
error: /* @__PURE__ */ __name((message, ...args) => {
|
|
96
|
-
if (isDev()) {
|
|
97
|
-
console.error(formatLogPrefix("ERROR"), message, ...args);
|
|
98
|
-
}
|
|
99
|
-
}, "error"),
|
|
100
|
-
debug: /* @__PURE__ */ __name((_message, ..._args) => {
|
|
101
|
-
return;
|
|
102
|
-
}, "debug")
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
__name(createDevLogger, "createDevLogger");
|
|
106
|
-
var logger_default = createDevLogger();
|
|
107
|
-
function isDevNodeEnv() {
|
|
108
|
-
const nodeEnv = process.env.NODE_ENV;
|
|
109
|
-
return nodeEnv !== "production" && nodeEnv !== "test" && nodeEnv !== "ci";
|
|
110
|
-
}
|
|
111
|
-
__name(isDevNodeEnv, "isDevNodeEnv");
|
|
112
|
-
function formatLogPrefix(level) {
|
|
113
|
-
return `[VoltAgent] [${timestamp()}] ${level}: `;
|
|
114
|
-
}
|
|
115
|
-
__name(formatLogPrefix, "formatLogPrefix");
|
|
116
|
-
function timestamp() {
|
|
117
|
-
return (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").slice(0, -1);
|
|
118
|
-
}
|
|
119
|
-
__name(timestamp, "timestamp");
|
|
120
|
-
|
|
121
39
|
// src/utils/lang.ts
|
|
122
40
|
function isNil(obj) {
|
|
123
41
|
return obj === null || obj === void 0;
|
|
@@ -151,11 +69,13 @@ function isEmptyObject(obj) {
|
|
|
151
69
|
__name(isEmptyObject, "isEmptyObject");
|
|
152
70
|
|
|
153
71
|
// src/utils/objects.ts
|
|
154
|
-
function deepClone(obj) {
|
|
72
|
+
function deepClone(obj, logger) {
|
|
155
73
|
try {
|
|
156
74
|
return JSON.parse(JSON.stringify(obj));
|
|
157
75
|
} catch (error) {
|
|
158
|
-
|
|
76
|
+
if (logger) {
|
|
77
|
+
logger.warn("Failed to deep clone object, using shallow clone", { error });
|
|
78
|
+
}
|
|
159
79
|
if (obj === null || typeof obj !== "object") {
|
|
160
80
|
return obj;
|
|
161
81
|
}
|
package/dist/utils/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/
|
|
1
|
+
{"version":3,"sources":["../../src/utils/lang.ts","../../src/utils/objects.ts","../../src/utils/async-iterable-stream.ts"],"sourcesContent":["import type { EmptyObject } from \"type-fest\";\nimport type { AnyFunction, Nil, PlainObject } from \"../types\";\n\n/**\n * Check if a value is nil\n *\n * @param obj - The value to check\n * @returns True if the value is nil, false otherwise\n */\nexport function isNil(obj: unknown): obj is Nil {\n return obj === null || obj === undefined;\n}\n\n/**\n * Check if an object is a JS object\n *\n * @param obj - The object to check\n * @returns True if the object is a JS object}\n */\nexport function isObject<T extends object>(obj: unknown): obj is T {\n return (typeof obj === \"object\" || typeof obj === \"function\") && !isNil(obj);\n}\n\n/**\n * Check if a value is a function\n *\n * @param obj - The value to check\n * @returns True if the value is a function, false otherwise\n */\nexport function isFunction<T extends AnyFunction>(obj: unknown): obj is T {\n return typeof obj === \"function\";\n}\n\n/**\n * Check if an object is a plain object (i.e. a JS object but not including arrays or functions)\n *\n * @param obj - The object to check\n * @returns True if the object is a plain object, false otherwise.\n */\nexport function isPlainObject<T extends PlainObject>(obj: unknown): obj is T {\n if (!isObject(obj)) {\n return false;\n }\n\n const prototype = Object.getPrototypeOf(obj);\n return prototype === Object.prototype || prototype === null;\n}\n\n/**\n * Check if an object is an empty object\n *\n * @param obj - The object to check\n * @returns True if the object is an empty object, false otherwise\n */\nexport function isEmptyObject(obj: unknown): obj is EmptyObject {\n if (!isObject(obj)) {\n return false;\n }\n\n // Check for own string and symbol properties (enumerable or not)\n if (Object.getOwnPropertyNames(obj).length > 0 || Object.getOwnPropertySymbols(obj).length > 0) {\n return false;\n }\n\n return true;\n}\n","import type { SetRequired } from \"type-fest\";\nimport type { PlainObject } from \"../types\";\nimport { isObject } from \"./lang\";\nimport type { Logger } from \"../logger/types\";\n\n/**\n * Deep clone an object using JSON serialization with fallback to shallow clone\n *\n * @param obj - The object to clone\n * @param logger - Optional logger for warnings\n * @returns A deep copy of the object, or shallow copy if JSON serialization fails\n */\nexport function deepClone<T>(obj: T, logger?: Logger): T {\n try {\n return JSON.parse(JSON.stringify(obj));\n } catch (error) {\n if (logger) {\n logger.warn(\"Failed to deep clone object, using shallow clone\", { error });\n }\n // Fallback to shallow clone for primitive types and simple objects\n if (obj === null || typeof obj !== \"object\") {\n return obj;\n }\n return { ...obj } as T;\n }\n}\n\n/**\n * Check if an object has a key\n *\n * @param obj - The object to check\n * @param key - The key to check\n * @returns True if the object has the key, false otherwise\n */\nexport function hasKey<T extends PlainObject, K extends string>(\n obj: T,\n key: K,\n): obj is T & SetRequired<T, K> {\n return isObject(obj) && key in obj;\n}\n","import type { Merge } from \"type-fest\";\n\n/**\n * An async iterable stream that can be read from.\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = getStream();\n * for await (const chunk of stream) {\n * console.log(chunk);\n * }\n * ```\n */\nexport type AsyncIterableStream<T> = Merge<AsyncIterable<T>, ReadableStream<T>>;\n\n/**\n * Create an async iterable stream from a readable stream.\n *\n * This is useful for creating an async iterable stream from a readable stream.\n *\n * @example\n * ```typescript\n * const stream: AsyncIterableStream<string> = createAsyncIterableStream(new ReadableStream({\n * start(controller) {\n * controller.enqueue(\"Hello\");\n * controller.close();\n * },\n * }));\n * ```\n * @param source The readable stream to create an async iterable stream from.\n * @returns The async iterable stream.\n */\nexport function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream<T> {\n const stream = source.pipeThrough(new TransformStream<T, T>());\n\n (stream as AsyncIterableStream<T>)[Symbol.asyncIterator] = () => {\n const reader = stream.getReader();\n return {\n async next(): Promise<IteratorResult<T>> {\n const { done, value } = await reader.read();\n return done ? { done: true, value: undefined } : { done: false, value };\n },\n };\n };\n\n return stream as AsyncIterableStream<T>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AASO,SAAS,MAAM,KAA0B;AAC9C,SAAO,QAAQ,QAAQ,QAAQ;AACjC;AAFgB;AAUT,SAAS,SAA2B,KAAwB;AACjE,UAAQ,OAAO,QAAQ,YAAY,OAAO,QAAQ,eAAe,CAAC,MAAM,GAAG;AAC7E;AAFgB;AAUT,SAAS,WAAkC,KAAwB;AACxE,SAAO,OAAO,QAAQ;AACxB;AAFgB;AAUT,SAAS,cAAqC,KAAwB;AAC3E,MAAI,CAAC,SAAS,GAAG,GAAG;AAClB,WAAO;AAAA,EACT;AAEA,QAAM,YAAY,OAAO,eAAe,GAAG;AAC3C,SAAO,cAAc,OAAO,aAAa,cAAc;AACzD;AAPgB;AAeT,SAAS,cAAc,KAAkC;AAC9D,MAAI,CAAC,SAAS,GAAG,GAAG;AAClB,WAAO;AAAA,EACT;AAGA,MAAI,OAAO,oBAAoB,GAAG,EAAE,SAAS,KAAK,OAAO,sBAAsB,GAAG,EAAE,SAAS,GAAG;AAC9F,WAAO;AAAA,EACT;AAEA,SAAO;AACT;AAXgB;;;AC1CT,SAAS,UAAa,KAAQ,QAAoB;AACvD,MAAI;AACF,WAAO,KAAK,MAAM,KAAK,UAAU,GAAG,CAAC;AAAA,EACvC,SAAS,OAAO;AACd,QAAI,QAAQ;AACV,aAAO,KAAK,oDAAoD,EAAE,MAAM,CAAC;AAAA,IAC3E;AAEA,QAAI,QAAQ,QAAQ,OAAO,QAAQ,UAAU;AAC3C,aAAO;AAAA,IACT;AACA,WAAO,mBAAK;AAAA,EACd;AACF;AAbgB;AAsBT,SAAS,OACd,KACA,KAC8B;AAC9B,SAAO,SAAS,GAAG,KAAK,OAAO;AACjC;AALgB;;;ACHT,SAAS,0BAA6B,QAAmD;AAC9F,QAAM,SAAS,OAAO,YAAY,IAAI,gBAAsB,CAAC;AAE7D,EAAC,OAAkC,OAAO,aAAa,IAAI,MAAM;AAC/D,UAAM,SAAS,OAAO,UAAU;AAChC,WAAO;AAAA,MACC,OAAmC;AAAA;AACvC,gBAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAC1C,iBAAO,OAAO,EAAE,MAAM,MAAM,OAAO,OAAU,IAAI,EAAE,MAAM,OAAO,MAAM;AAAA,QACxE;AAAA;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAdgB;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@voltagent/internal",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "VoltAgent internal - an internal set of tools for the VoltAgent packages",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"exports": {
|
|
@@ -9,11 +9,6 @@
|
|
|
9
9
|
"import": "./dist/main/index.mjs",
|
|
10
10
|
"require": "./dist/main/index.js"
|
|
11
11
|
},
|
|
12
|
-
"./dev": {
|
|
13
|
-
"types": "./dist/dev/index.d.ts",
|
|
14
|
-
"import": "./dist/dev/index.mjs",
|
|
15
|
-
"require": "./dist/dev/index.js"
|
|
16
|
-
},
|
|
17
12
|
"./test": {
|
|
18
13
|
"types": "./dist/test/index.d.ts",
|
|
19
14
|
"import": "./dist/test/index.mjs",
|