@voltagent/internal 0.0.4 → 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 +138 -105
- package/dist/main/index.d.ts +138 -105
- package/dist/main/index.js +24 -88
- package/dist/main/index.js.map +1 -1
- package/dist/main/index.mjs +23 -86
- package/dist/main/index.mjs.map +1 -1
- package/dist/utils/index.d.mts +76 -3
- package/dist/utils/index.d.ts +76 -3
- package/dist/utils/index.js +44 -84
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +41 -82
- 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.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts","../../src/dev/logger.ts","../../src/test/conversions.ts","../../src/utils/lang.ts","../../src/utils/objects.ts"],"sourcesContent":["export * from \"./dev\";\nexport * from \"./test\";\nexport * from \"./utils\";\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","/**\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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;AAA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AAG;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;;;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;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts","../../src/test/conversions.ts","../../src/utils/lang.ts","../../src/utils/objects.ts","../../src/utils/async-iterable-stream.ts"],"sourcesContent":["export * from \"./test\";\nexport * from \"./utils\";\nexport * from \"./logger/types\";\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 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;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,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;AAehB,SAAsB,4BAA+B,UAA0C;AAAA;AAC7F,UAAM,SAAc,CAAC;AACrB;AAAA,iCAAyB,WAAzB,0EAAmC;AAAxB,cAAM,OAAjB;AACE,eAAO,KAAK,IAAI;AAAA,MAClB;AAAA,aAFA,MAxCF;AAwCE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,WAAO;AAAA,EACT;AAAA;AANsB;AAaf,SAAS,6BAAgC,QAAgC;AAC9E,SAAO,IAAI,eAAe;AAAA,IACxB,MAAM,YAAY;AAChB,UAAI;AACF,mBAAW,SAAS,QA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ACA,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/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
|
}
|
|
@@ -253,15 +173,32 @@ function hasKey(obj, key) {
|
|
|
253
173
|
return isObject(obj) && key in obj;
|
|
254
174
|
}
|
|
255
175
|
__name(hasKey, "hasKey");
|
|
176
|
+
|
|
177
|
+
// src/utils/async-iterable-stream.ts
|
|
178
|
+
function createAsyncIterableStream(source) {
|
|
179
|
+
const stream = source.pipeThrough(new TransformStream());
|
|
180
|
+
stream[Symbol.asyncIterator] = () => {
|
|
181
|
+
const reader = stream.getReader();
|
|
182
|
+
return {
|
|
183
|
+
next() {
|
|
184
|
+
return __async(this, null, function* () {
|
|
185
|
+
const { done, value } = yield reader.read();
|
|
186
|
+
return done ? { done: true, value: void 0 } : { done: false, value };
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
return stream;
|
|
192
|
+
}
|
|
193
|
+
__name(createAsyncIterableStream, "createAsyncIterableStream");
|
|
256
194
|
export {
|
|
257
195
|
convertArrayToAsyncIterable,
|
|
258
196
|
convertArrayToReadableStream,
|
|
259
197
|
convertAsyncIterableToArray,
|
|
260
198
|
convertReadableStreamToArray,
|
|
261
199
|
convertResponseStreamToArray,
|
|
262
|
-
|
|
200
|
+
createAsyncIterableStream,
|
|
263
201
|
deepClone,
|
|
264
|
-
logger_default as devLogger,
|
|
265
202
|
hasKey,
|
|
266
203
|
isEmptyObject,
|
|
267
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"],"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"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYO,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;A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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;;;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;","names":[]}
|
|
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;AAehB,SAAsB,4BAA+B,UAA0C;AAAA;AAC7F,UAAM,SAAc,CAAC;AACrB;AAAA,iCAAyB,WAAzB,0EAAmC;AAAxB,cAAM,OAAjB;AACE,eAAO,KAAK,IAAI;AAAA,MAClB;AAAA,aAFA,MAxCF;AAwCE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAGA,WAAO;AAAA,EACT;AAAA;AANsB;AAaf,SAAS,6BAAgC,QAAgC;AAC9E,SAAO,IAAI,eAAe;AAAA,IACxB,MAAM,YAAY;AAChB,UAAI;AACF,mBAAW,SAAS,QAAQ;AAC1B,qBAAW,QAAQ,KAAK;AAAA,QAC1B;AAAA,MACF,UAAE;AACA,mBAAW,MAAM;AAAA,MACnB;AAAA,IACF;AAAA,EACF,CAAC;AACH;AAZgB;AAmBhB,SAAsB,6BAA6B,UAAuC;AAAA;AAExF,WAAO,6BAA6B,SAAS,KAAM,YAAY,IAAI,kBAAkB,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,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
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SetRequired, EmptyObject } from 'type-fest';
|
|
1
|
+
import { SetRequired, EmptyObject, Merge } from 'type-fest';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A plain object is an object that has no special properties or methods,
|
|
@@ -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
|
*
|
|
@@ -74,4 +117,34 @@ declare function isPlainObject<T extends PlainObject>(obj: unknown): obj is T;
|
|
|
74
117
|
*/
|
|
75
118
|
declare function isEmptyObject(obj: unknown): obj is EmptyObject;
|
|
76
119
|
|
|
77
|
-
|
|
120
|
+
/**
|
|
121
|
+
* An async iterable stream that can be read from.
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const stream: AsyncIterableStream<string> = getStream();
|
|
125
|
+
* for await (const chunk of stream) {
|
|
126
|
+
* console.log(chunk);
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
type AsyncIterableStream<T> = Merge<AsyncIterable<T>, ReadableStream<T>>;
|
|
131
|
+
/**
|
|
132
|
+
* Create an async iterable stream from a readable stream.
|
|
133
|
+
*
|
|
134
|
+
* This is useful for creating an async iterable stream from a readable stream.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const stream: AsyncIterableStream<string> = createAsyncIterableStream(new ReadableStream({
|
|
139
|
+
* start(controller) {
|
|
140
|
+
* controller.enqueue("Hello");
|
|
141
|
+
* controller.close();
|
|
142
|
+
* },
|
|
143
|
+
* }));
|
|
144
|
+
* ```
|
|
145
|
+
* @param source The readable stream to create an async iterable stream from.
|
|
146
|
+
* @returns The async iterable stream.
|
|
147
|
+
*/
|
|
148
|
+
declare function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream<T>;
|
|
149
|
+
|
|
150
|
+
export { type AsyncIterableStream, createAsyncIterableStream, deepClone, hasKey, isEmptyObject, isFunction, isNil, isObject, isPlainObject };
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SetRequired, EmptyObject } from 'type-fest';
|
|
1
|
+
import { SetRequired, EmptyObject, Merge } from 'type-fest';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* A plain object is an object that has no special properties or methods,
|
|
@@ -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
|
*
|
|
@@ -74,4 +117,34 @@ declare function isPlainObject<T extends PlainObject>(obj: unknown): obj is T;
|
|
|
74
117
|
*/
|
|
75
118
|
declare function isEmptyObject(obj: unknown): obj is EmptyObject;
|
|
76
119
|
|
|
77
|
-
|
|
120
|
+
/**
|
|
121
|
+
* An async iterable stream that can be read from.
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const stream: AsyncIterableStream<string> = getStream();
|
|
125
|
+
* for await (const chunk of stream) {
|
|
126
|
+
* console.log(chunk);
|
|
127
|
+
* }
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
type AsyncIterableStream<T> = Merge<AsyncIterable<T>, ReadableStream<T>>;
|
|
131
|
+
/**
|
|
132
|
+
* Create an async iterable stream from a readable stream.
|
|
133
|
+
*
|
|
134
|
+
* This is useful for creating an async iterable stream from a readable stream.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const stream: AsyncIterableStream<string> = createAsyncIterableStream(new ReadableStream({
|
|
139
|
+
* start(controller) {
|
|
140
|
+
* controller.enqueue("Hello");
|
|
141
|
+
* controller.close();
|
|
142
|
+
* },
|
|
143
|
+
* }));
|
|
144
|
+
* ```
|
|
145
|
+
* @param source The readable stream to create an async iterable stream from.
|
|
146
|
+
* @returns The async iterable stream.
|
|
147
|
+
*/
|
|
148
|
+
declare function createAsyncIterableStream<T>(source: ReadableStream<T>): AsyncIterableStream<T>;
|
|
149
|
+
|
|
150
|
+
export { type AsyncIterableStream, createAsyncIterableStream, deepClone, hasKey, isEmptyObject, isFunction, isNil, isObject, isPlainObject };
|
package/dist/utils/index.js
CHANGED
|
@@ -31,10 +31,31 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
31
31
|
return to;
|
|
32
32
|
};
|
|
33
33
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
34
|
+
var __async = (__this, __arguments, generator) => {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
var fulfilled = (value) => {
|
|
37
|
+
try {
|
|
38
|
+
step(generator.next(value));
|
|
39
|
+
} catch (e) {
|
|
40
|
+
reject(e);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
var rejected = (value) => {
|
|
44
|
+
try {
|
|
45
|
+
step(generator.throw(value));
|
|
46
|
+
} catch (e) {
|
|
47
|
+
reject(e);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
51
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
52
|
+
});
|
|
53
|
+
};
|
|
34
54
|
|
|
35
55
|
// src/utils/index.ts
|
|
36
56
|
var index_exports = {};
|
|
37
57
|
__export(index_exports, {
|
|
58
|
+
createAsyncIterableStream: () => createAsyncIterableStream,
|
|
38
59
|
deepClone: () => deepClone,
|
|
39
60
|
hasKey: () => hasKey,
|
|
40
61
|
isEmptyObject: () => isEmptyObject,
|
|
@@ -45,88 +66,6 @@ __export(index_exports, {
|
|
|
45
66
|
});
|
|
46
67
|
module.exports = __toCommonJS(index_exports);
|
|
47
68
|
|
|
48
|
-
// src/dev/logger.ts
|
|
49
|
-
function createDevLogger(options) {
|
|
50
|
-
const isDev = typeof (options == null ? void 0 : options.dev) === "function" ? options.dev : () => {
|
|
51
|
-
var _a;
|
|
52
|
-
return (_a = options == null ? void 0 : options.dev) != null ? _a : isDevNodeEnv();
|
|
53
|
-
};
|
|
54
|
-
return {
|
|
55
|
-
/**
|
|
56
|
-
* Log a message to the console if the environment is development. This will NOT be logged if process.env.NODE_ENV is not "development".
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* devLogger.info("Hello, world!");
|
|
61
|
-
*
|
|
62
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] INFO: Hello, world!
|
|
63
|
-
* ```
|
|
64
|
-
*
|
|
65
|
-
* @param message - The message to log.
|
|
66
|
-
* @param args - The arguments to log.
|
|
67
|
-
*/
|
|
68
|
-
info: /* @__PURE__ */ __name((message, ...args) => {
|
|
69
|
-
if (isDev()) {
|
|
70
|
-
console.info(formatLogPrefix("INFO"), message, ...args);
|
|
71
|
-
}
|
|
72
|
-
}, "info"),
|
|
73
|
-
/**
|
|
74
|
-
* 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".
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* ```typescript
|
|
78
|
-
* devLogger.warn("Hello, world!");
|
|
79
|
-
*
|
|
80
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] WARN: Hello, world!
|
|
81
|
-
* ```
|
|
82
|
-
*
|
|
83
|
-
* @param message - The message to log.
|
|
84
|
-
* @param args - The arguments to log.
|
|
85
|
-
*/
|
|
86
|
-
warn: /* @__PURE__ */ __name((message, ...args) => {
|
|
87
|
-
if (isDev()) {
|
|
88
|
-
console.warn(formatLogPrefix("WARN"), message, ...args);
|
|
89
|
-
}
|
|
90
|
-
}, "warn"),
|
|
91
|
-
/**
|
|
92
|
-
* Log a warning message to the console if the environment is development.
|
|
93
|
-
*
|
|
94
|
-
* @example
|
|
95
|
-
* ```typescript
|
|
96
|
-
* devLogger.error("Hello, world!");
|
|
97
|
-
*
|
|
98
|
-
* // output: [VoltAgent] [2021-01-01T00:00:00.000Z] ERROR: Hello, world!
|
|
99
|
-
* ```
|
|
100
|
-
*
|
|
101
|
-
* @param message - The message to log.
|
|
102
|
-
* @param args - The arguments to log.
|
|
103
|
-
*/
|
|
104
|
-
error: /* @__PURE__ */ __name((message, ...args) => {
|
|
105
|
-
if (isDev()) {
|
|
106
|
-
console.error(formatLogPrefix("ERROR"), message, ...args);
|
|
107
|
-
}
|
|
108
|
-
}, "error"),
|
|
109
|
-
debug: /* @__PURE__ */ __name((_message, ..._args) => {
|
|
110
|
-
return;
|
|
111
|
-
}, "debug")
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
__name(createDevLogger, "createDevLogger");
|
|
115
|
-
var logger_default = createDevLogger();
|
|
116
|
-
function isDevNodeEnv() {
|
|
117
|
-
const nodeEnv = process.env.NODE_ENV;
|
|
118
|
-
return nodeEnv !== "production" && nodeEnv !== "test" && nodeEnv !== "ci";
|
|
119
|
-
}
|
|
120
|
-
__name(isDevNodeEnv, "isDevNodeEnv");
|
|
121
|
-
function formatLogPrefix(level) {
|
|
122
|
-
return `[VoltAgent] [${timestamp()}] ${level}: `;
|
|
123
|
-
}
|
|
124
|
-
__name(formatLogPrefix, "formatLogPrefix");
|
|
125
|
-
function timestamp() {
|
|
126
|
-
return (/* @__PURE__ */ new Date()).toISOString().replace("T", " ").slice(0, -1);
|
|
127
|
-
}
|
|
128
|
-
__name(timestamp, "timestamp");
|
|
129
|
-
|
|
130
69
|
// src/utils/lang.ts
|
|
131
70
|
function isNil(obj) {
|
|
132
71
|
return obj === null || obj === void 0;
|
|
@@ -160,11 +99,13 @@ function isEmptyObject(obj) {
|
|
|
160
99
|
__name(isEmptyObject, "isEmptyObject");
|
|
161
100
|
|
|
162
101
|
// src/utils/objects.ts
|
|
163
|
-
function deepClone(obj) {
|
|
102
|
+
function deepClone(obj, logger) {
|
|
164
103
|
try {
|
|
165
104
|
return JSON.parse(JSON.stringify(obj));
|
|
166
105
|
} catch (error) {
|
|
167
|
-
|
|
106
|
+
if (logger) {
|
|
107
|
+
logger.warn("Failed to deep clone object, using shallow clone", { error });
|
|
108
|
+
}
|
|
168
109
|
if (obj === null || typeof obj !== "object") {
|
|
169
110
|
return obj;
|
|
170
111
|
}
|
|
@@ -176,8 +117,27 @@ function hasKey(obj, key) {
|
|
|
176
117
|
return isObject(obj) && key in obj;
|
|
177
118
|
}
|
|
178
119
|
__name(hasKey, "hasKey");
|
|
120
|
+
|
|
121
|
+
// src/utils/async-iterable-stream.ts
|
|
122
|
+
function createAsyncIterableStream(source) {
|
|
123
|
+
const stream = source.pipeThrough(new TransformStream());
|
|
124
|
+
stream[Symbol.asyncIterator] = () => {
|
|
125
|
+
const reader = stream.getReader();
|
|
126
|
+
return {
|
|
127
|
+
next() {
|
|
128
|
+
return __async(this, null, function* () {
|
|
129
|
+
const { done, value } = yield reader.read();
|
|
130
|
+
return done ? { done: true, value: void 0 } : { done: false, value };
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
return stream;
|
|
136
|
+
}
|
|
137
|
+
__name(createAsyncIterableStream, "createAsyncIterableStream");
|
|
179
138
|
// Annotate the CommonJS export names for ESM import in node:
|
|
180
139
|
0 && (module.exports = {
|
|
140
|
+
createAsyncIterableStream,
|
|
181
141
|
deepClone,
|
|
182
142
|
hasKey,
|
|
183
143
|
isEmptyObject,
|
package/dist/utils/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utils/index.ts","../../src/
|
|
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 st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