@juspay/neurolink 9.1.0 → 9.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +54 -7
- package/dist/agent/directTools.d.ts +3 -3
- package/dist/cli/commands/config.d.ts +6 -6
- package/dist/image-gen/ImageGenService.d.ts +143 -0
- package/dist/image-gen/ImageGenService.js +345 -0
- package/dist/image-gen/imageGenTools.d.ts +126 -0
- package/dist/image-gen/imageGenTools.js +304 -0
- package/dist/image-gen/index.d.ts +46 -0
- package/dist/image-gen/index.js +48 -0
- package/dist/image-gen/types.d.ts +237 -0
- package/dist/image-gen/types.js +24 -0
- package/dist/lib/agent/directTools.d.ts +3 -3
- package/dist/lib/image-gen/ImageGenService.d.ts +143 -0
- package/dist/lib/image-gen/ImageGenService.js +346 -0
- package/dist/lib/image-gen/imageGenTools.d.ts +126 -0
- package/dist/lib/image-gen/imageGenTools.js +305 -0
- package/dist/lib/image-gen/index.d.ts +46 -0
- package/dist/lib/image-gen/index.js +49 -0
- package/dist/lib/image-gen/types.d.ts +237 -0
- package/dist/lib/image-gen/types.js +25 -0
- package/dist/lib/processors/base/BaseFileProcessor.d.ts +273 -0
- package/dist/lib/processors/base/BaseFileProcessor.js +614 -0
- package/dist/lib/processors/base/index.d.ts +14 -0
- package/dist/lib/processors/base/index.js +20 -0
- package/dist/lib/processors/base/types.d.ts +593 -0
- package/dist/lib/processors/base/types.js +77 -0
- package/dist/lib/processors/cli/fileProcessorCli.d.ts +163 -0
- package/dist/lib/processors/cli/fileProcessorCli.js +389 -0
- package/dist/lib/processors/cli/index.d.ts +37 -0
- package/dist/lib/processors/cli/index.js +50 -0
- package/dist/lib/processors/code/ConfigProcessor.d.ts +171 -0
- package/dist/lib/processors/code/ConfigProcessor.js +401 -0
- package/dist/lib/processors/code/SourceCodeProcessor.d.ts +174 -0
- package/dist/lib/processors/code/SourceCodeProcessor.js +305 -0
- package/dist/lib/processors/code/index.d.ts +44 -0
- package/dist/lib/processors/code/index.js +61 -0
- package/dist/lib/processors/config/fileTypes.d.ts +283 -0
- package/dist/lib/processors/config/fileTypes.js +521 -0
- package/dist/lib/processors/config/index.d.ts +32 -0
- package/dist/lib/processors/config/index.js +93 -0
- package/dist/lib/processors/config/languageMap.d.ts +66 -0
- package/dist/lib/processors/config/languageMap.js +411 -0
- package/dist/lib/processors/config/mimeTypes.d.ts +376 -0
- package/dist/lib/processors/config/mimeTypes.js +339 -0
- package/dist/lib/processors/config/sizeLimits.d.ts +194 -0
- package/dist/lib/processors/config/sizeLimits.js +247 -0
- package/dist/lib/processors/data/JsonProcessor.d.ts +122 -0
- package/dist/lib/processors/data/JsonProcessor.js +204 -0
- package/dist/lib/processors/data/XmlProcessor.d.ts +160 -0
- package/dist/lib/processors/data/XmlProcessor.js +284 -0
- package/dist/lib/processors/data/YamlProcessor.d.ts +163 -0
- package/dist/lib/processors/data/YamlProcessor.js +295 -0
- package/dist/lib/processors/data/index.d.ts +49 -0
- package/dist/lib/processors/data/index.js +77 -0
- package/dist/lib/processors/document/ExcelProcessor.d.ts +238 -0
- package/dist/lib/processors/document/ExcelProcessor.js +520 -0
- package/dist/lib/processors/document/OpenDocumentProcessor.d.ts +69 -0
- package/dist/lib/processors/document/OpenDocumentProcessor.js +211 -0
- package/dist/lib/processors/document/RtfProcessor.d.ts +152 -0
- package/dist/lib/processors/document/RtfProcessor.js +362 -0
- package/dist/lib/processors/document/WordProcessor.d.ts +168 -0
- package/dist/lib/processors/document/WordProcessor.js +354 -0
- package/dist/lib/processors/document/index.d.ts +54 -0
- package/dist/lib/processors/document/index.js +91 -0
- package/dist/lib/processors/errors/FileErrorCode.d.ts +98 -0
- package/dist/lib/processors/errors/FileErrorCode.js +256 -0
- package/dist/lib/processors/errors/errorHelpers.d.ts +151 -0
- package/dist/lib/processors/errors/errorHelpers.js +379 -0
- package/dist/lib/processors/errors/errorSerializer.d.ts +139 -0
- package/dist/lib/processors/errors/errorSerializer.js +508 -0
- package/dist/lib/processors/errors/index.d.ts +46 -0
- package/dist/lib/processors/errors/index.js +50 -0
- package/dist/lib/processors/index.d.ts +76 -0
- package/dist/lib/processors/index.js +113 -0
- package/dist/lib/processors/integration/FileProcessorIntegration.d.ts +244 -0
- package/dist/lib/processors/integration/FileProcessorIntegration.js +273 -0
- package/dist/lib/processors/integration/index.d.ts +42 -0
- package/dist/lib/processors/integration/index.js +45 -0
- package/dist/lib/processors/markup/HtmlProcessor.d.ts +169 -0
- package/dist/lib/processors/markup/HtmlProcessor.js +250 -0
- package/dist/lib/processors/markup/MarkdownProcessor.d.ts +165 -0
- package/dist/lib/processors/markup/MarkdownProcessor.js +245 -0
- package/dist/lib/processors/markup/SvgProcessor.d.ts +156 -0
- package/dist/lib/processors/markup/SvgProcessor.js +241 -0
- package/dist/lib/processors/markup/TextProcessor.d.ts +135 -0
- package/dist/lib/processors/markup/TextProcessor.js +189 -0
- package/dist/lib/processors/markup/index.d.ts +66 -0
- package/dist/lib/processors/markup/index.js +103 -0
- package/dist/lib/processors/registry/ProcessorRegistry.d.ts +334 -0
- package/dist/lib/processors/registry/ProcessorRegistry.js +609 -0
- package/dist/lib/processors/registry/index.d.ts +12 -0
- package/dist/lib/processors/registry/index.js +17 -0
- package/dist/lib/processors/registry/types.d.ts +53 -0
- package/dist/lib/processors/registry/types.js +11 -0
- package/dist/lib/providers/sagemaker/language-model.d.ts +2 -2
- package/dist/lib/server/utils/validation.d.ts +6 -6
- package/dist/lib/types/fileTypes.d.ts +51 -1
- package/dist/lib/types/index.d.ts +25 -24
- package/dist/lib/types/index.js +21 -20
- package/dist/lib/types/modelTypes.d.ts +18 -18
- package/dist/lib/types/pptTypes.d.ts +14 -2
- package/dist/lib/types/pptTypes.js +16 -0
- package/dist/lib/utils/async/delay.d.ts +40 -0
- package/dist/lib/utils/async/delay.js +43 -0
- package/dist/lib/utils/async/index.d.ts +23 -0
- package/dist/lib/utils/async/index.js +24 -0
- package/dist/lib/utils/async/retry.d.ts +141 -0
- package/dist/lib/utils/async/retry.js +172 -0
- package/dist/lib/utils/async/withTimeout.d.ts +73 -0
- package/dist/lib/utils/async/withTimeout.js +97 -0
- package/dist/lib/utils/csvProcessor.js +442 -0
- package/dist/lib/utils/fileDetector.d.ts +7 -1
- package/dist/lib/utils/fileDetector.js +91 -18
- package/dist/lib/utils/json/extract.d.ts +103 -0
- package/dist/lib/utils/json/extract.js +249 -0
- package/dist/lib/utils/json/index.d.ts +36 -0
- package/dist/lib/utils/json/index.js +37 -0
- package/dist/lib/utils/json/safeParse.d.ts +137 -0
- package/dist/lib/utils/json/safeParse.js +191 -0
- package/dist/lib/utils/messageBuilder.d.ts +2 -2
- package/dist/lib/utils/messageBuilder.js +15 -7
- package/dist/lib/utils/sanitizers/filename.d.ts +137 -0
- package/dist/lib/utils/sanitizers/filename.js +366 -0
- package/dist/lib/utils/sanitizers/html.d.ts +170 -0
- package/dist/lib/utils/sanitizers/html.js +326 -0
- package/dist/lib/utils/sanitizers/index.d.ts +26 -0
- package/dist/lib/utils/sanitizers/index.js +30 -0
- package/dist/lib/utils/sanitizers/svg.d.ts +81 -0
- package/dist/lib/utils/sanitizers/svg.js +483 -0
- package/dist/processors/base/BaseFileProcessor.d.ts +273 -0
- package/dist/processors/base/BaseFileProcessor.js +613 -0
- package/dist/processors/base/index.d.ts +14 -0
- package/dist/processors/base/index.js +19 -0
- package/dist/processors/base/types.d.ts +593 -0
- package/dist/processors/base/types.js +76 -0
- package/dist/processors/cli/fileProcessorCli.d.ts +163 -0
- package/dist/processors/cli/fileProcessorCli.js +388 -0
- package/dist/processors/cli/index.d.ts +37 -0
- package/dist/processors/cli/index.js +49 -0
- package/dist/processors/code/ConfigProcessor.d.ts +171 -0
- package/dist/processors/code/ConfigProcessor.js +400 -0
- package/dist/processors/code/SourceCodeProcessor.d.ts +174 -0
- package/dist/processors/code/SourceCodeProcessor.js +304 -0
- package/dist/processors/code/index.d.ts +44 -0
- package/dist/processors/code/index.js +60 -0
- package/dist/processors/config/fileTypes.d.ts +283 -0
- package/dist/processors/config/fileTypes.js +520 -0
- package/dist/processors/config/index.d.ts +32 -0
- package/dist/processors/config/index.js +92 -0
- package/dist/processors/config/languageMap.d.ts +66 -0
- package/dist/processors/config/languageMap.js +410 -0
- package/dist/processors/config/mimeTypes.d.ts +376 -0
- package/dist/processors/config/mimeTypes.js +338 -0
- package/dist/processors/config/sizeLimits.d.ts +194 -0
- package/dist/processors/config/sizeLimits.js +246 -0
- package/dist/processors/data/JsonProcessor.d.ts +122 -0
- package/dist/processors/data/JsonProcessor.js +203 -0
- package/dist/processors/data/XmlProcessor.d.ts +160 -0
- package/dist/processors/data/XmlProcessor.js +283 -0
- package/dist/processors/data/YamlProcessor.d.ts +163 -0
- package/dist/processors/data/YamlProcessor.js +294 -0
- package/dist/processors/data/index.d.ts +49 -0
- package/dist/processors/data/index.js +76 -0
- package/dist/processors/document/ExcelProcessor.d.ts +238 -0
- package/dist/processors/document/ExcelProcessor.js +519 -0
- package/dist/processors/document/OpenDocumentProcessor.d.ts +69 -0
- package/dist/processors/document/OpenDocumentProcessor.js +210 -0
- package/dist/processors/document/RtfProcessor.d.ts +152 -0
- package/dist/processors/document/RtfProcessor.js +361 -0
- package/dist/processors/document/WordProcessor.d.ts +168 -0
- package/dist/processors/document/WordProcessor.js +353 -0
- package/dist/processors/document/index.d.ts +54 -0
- package/dist/processors/document/index.js +90 -0
- package/dist/processors/errors/FileErrorCode.d.ts +98 -0
- package/dist/processors/errors/FileErrorCode.js +255 -0
- package/dist/processors/errors/errorHelpers.d.ts +151 -0
- package/dist/processors/errors/errorHelpers.js +378 -0
- package/dist/processors/errors/errorSerializer.d.ts +139 -0
- package/dist/processors/errors/errorSerializer.js +507 -0
- package/dist/processors/errors/index.d.ts +46 -0
- package/dist/processors/errors/index.js +49 -0
- package/dist/processors/index.d.ts +76 -0
- package/dist/processors/index.js +112 -0
- package/dist/processors/integration/FileProcessorIntegration.d.ts +244 -0
- package/dist/processors/integration/FileProcessorIntegration.js +272 -0
- package/dist/processors/integration/index.d.ts +42 -0
- package/dist/processors/integration/index.js +44 -0
- package/dist/processors/markup/HtmlProcessor.d.ts +169 -0
- package/dist/processors/markup/HtmlProcessor.js +249 -0
- package/dist/processors/markup/MarkdownProcessor.d.ts +165 -0
- package/dist/processors/markup/MarkdownProcessor.js +244 -0
- package/dist/processors/markup/SvgProcessor.d.ts +156 -0
- package/dist/processors/markup/SvgProcessor.js +240 -0
- package/dist/processors/markup/TextProcessor.d.ts +135 -0
- package/dist/processors/markup/TextProcessor.js +188 -0
- package/dist/processors/markup/index.d.ts +66 -0
- package/dist/processors/markup/index.js +102 -0
- package/dist/processors/registry/ProcessorRegistry.d.ts +334 -0
- package/dist/processors/registry/ProcessorRegistry.js +608 -0
- package/dist/processors/registry/index.d.ts +12 -0
- package/dist/processors/registry/index.js +16 -0
- package/dist/processors/registry/types.d.ts +53 -0
- package/dist/processors/registry/types.js +10 -0
- package/dist/server/utils/validation.d.ts +6 -6
- package/dist/types/fileTypes.d.ts +51 -1
- package/dist/types/index.d.ts +25 -24
- package/dist/types/index.js +21 -20
- package/dist/types/modelTypes.d.ts +10 -10
- package/dist/types/pptTypes.d.ts +14 -2
- package/dist/types/pptTypes.js +16 -0
- package/dist/utils/async/delay.d.ts +40 -0
- package/dist/utils/async/delay.js +42 -0
- package/dist/utils/async/index.d.ts +23 -0
- package/dist/utils/async/index.js +23 -0
- package/dist/utils/async/retry.d.ts +141 -0
- package/dist/utils/async/retry.js +171 -0
- package/dist/utils/async/withTimeout.d.ts +73 -0
- package/dist/utils/async/withTimeout.js +96 -0
- package/dist/utils/csvProcessor.js +442 -0
- package/dist/utils/fileDetector.d.ts +7 -1
- package/dist/utils/fileDetector.js +91 -18
- package/dist/utils/json/extract.d.ts +103 -0
- package/dist/utils/json/extract.js +248 -0
- package/dist/utils/json/index.d.ts +36 -0
- package/dist/utils/json/index.js +36 -0
- package/dist/utils/json/safeParse.d.ts +137 -0
- package/dist/utils/json/safeParse.js +190 -0
- package/dist/utils/messageBuilder.d.ts +2 -2
- package/dist/utils/messageBuilder.js +15 -7
- package/dist/utils/sanitizers/filename.d.ts +137 -0
- package/dist/utils/sanitizers/filename.js +365 -0
- package/dist/utils/sanitizers/html.d.ts +170 -0
- package/dist/utils/sanitizers/html.js +325 -0
- package/dist/utils/sanitizers/index.d.ts +26 -0
- package/dist/utils/sanitizers/index.js +29 -0
- package/dist/utils/sanitizers/svg.d.ts +81 -0
- package/dist/utils/sanitizers/svg.js +482 -0
- package/package.json +2 -2
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe JSON Parsing Utilities
|
|
3
|
+
*
|
|
4
|
+
* Centralized JSON parsing utilities that handle errors gracefully.
|
|
5
|
+
* Provides safe parsing that doesn't throw on invalid JSON.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Result type for safe JSON parsing operations
|
|
9
|
+
*/
|
|
10
|
+
export interface SafeParseResult<T> {
|
|
11
|
+
success: boolean;
|
|
12
|
+
data: T | null;
|
|
13
|
+
error: Error | null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Safely parse a JSON string without throwing.
|
|
17
|
+
*
|
|
18
|
+
* This is the preferred method for parsing JSON from external sources
|
|
19
|
+
* (user input, API responses, etc.) where invalid JSON is possible.
|
|
20
|
+
*
|
|
21
|
+
* @param str - The string to parse as JSON
|
|
22
|
+
* @returns Object with success flag and either data or error
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const result = safeParseJsonResult<UserData>(userInput);
|
|
27
|
+
* if (result.success) {
|
|
28
|
+
* console.log(result.data.name);
|
|
29
|
+
* } else {
|
|
30
|
+
* console.error('Invalid JSON:', result.error.message);
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function safeParseJsonResult<T = unknown>(str: string): SafeParseResult<T>;
|
|
35
|
+
/**
|
|
36
|
+
* Safely parse JSON with fallback value.
|
|
37
|
+
*
|
|
38
|
+
* Useful when you need a value regardless of parse success.
|
|
39
|
+
*
|
|
40
|
+
* @param str - The string to parse as JSON
|
|
41
|
+
* @param fallback - Value to return if parsing fails
|
|
42
|
+
* @returns Parsed value or fallback
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* const config = safeParseJson(configString, { theme: 'light' });
|
|
47
|
+
* // Always returns an object, never throws
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function safeParseJson<T>(str: string, fallback: T): T;
|
|
51
|
+
/**
|
|
52
|
+
* Parse JSON or return undefined if invalid.
|
|
53
|
+
*
|
|
54
|
+
* @param str - The string to parse as JSON
|
|
55
|
+
* @returns Parsed value or undefined
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const data = parseJsonOrUndefined<Config>(input);
|
|
60
|
+
* if (data) {
|
|
61
|
+
* // Use data
|
|
62
|
+
* }
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
export declare function parseJsonOrUndefined<T>(str: string): T | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* Parse JSON or return null if invalid.
|
|
68
|
+
*
|
|
69
|
+
* @param str - The string to parse as JSON
|
|
70
|
+
* @returns Parsed value or null
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const data = parseJsonOrNull<Config>(input);
|
|
75
|
+
* if (data !== null) {
|
|
76
|
+
* // Use data
|
|
77
|
+
* }
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function parseJsonOrNull<T>(str: string): T | null;
|
|
81
|
+
/**
|
|
82
|
+
* Check if a string is valid JSON.
|
|
83
|
+
*
|
|
84
|
+
* @param str - The string to validate
|
|
85
|
+
* @returns true if the string is valid JSON
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```typescript
|
|
89
|
+
* if (isValidJson(userInput)) {
|
|
90
|
+
* // Proceed with parsing
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
export declare function isValidJson(str: string): boolean;
|
|
95
|
+
/**
|
|
96
|
+
* Safe stringify with circular reference handling.
|
|
97
|
+
*
|
|
98
|
+
* Handles circular references, BigInt values, and other edge cases gracefully.
|
|
99
|
+
*
|
|
100
|
+
* @param obj - Value to stringify
|
|
101
|
+
* @param space - Optional indentation (number of spaces)
|
|
102
|
+
* @returns JSON string, with circular references replaced by "[Circular]"
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* ```typescript
|
|
106
|
+
* const obj = { name: 'test' };
|
|
107
|
+
* obj.self = obj; // Circular reference
|
|
108
|
+
*
|
|
109
|
+
* const json = safeStringify(obj);
|
|
110
|
+
* // Returns: '{"name":"test","self":"[Circular]"}'
|
|
111
|
+
*
|
|
112
|
+
* const prettyJson = safeStringify(data, 2);
|
|
113
|
+
* // Returns formatted JSON with 2-space indentation
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export declare function safeStringify(obj: unknown, space?: number): string;
|
|
117
|
+
/**
|
|
118
|
+
* Safe stringify with options for more control.
|
|
119
|
+
*
|
|
120
|
+
* @param obj - Value to stringify
|
|
121
|
+
* @param options - Stringify options
|
|
122
|
+
* @returns JSON string or fallback string on error
|
|
123
|
+
*
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* const json = safeStringifyWithOptions(data, {
|
|
127
|
+
* pretty: true,
|
|
128
|
+
* fallback: '{}',
|
|
129
|
+
* });
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export declare function safeStringifyWithOptions(obj: unknown, options?: {
|
|
133
|
+
/** Use 2-space indentation */
|
|
134
|
+
pretty?: boolean;
|
|
135
|
+
/** Value to return if stringify fails */
|
|
136
|
+
fallback?: string;
|
|
137
|
+
}): string;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe JSON Parsing Utilities
|
|
3
|
+
*
|
|
4
|
+
* Centralized JSON parsing utilities that handle errors gracefully.
|
|
5
|
+
* Provides safe parsing that doesn't throw on invalid JSON.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Safely parse a JSON string without throwing.
|
|
9
|
+
*
|
|
10
|
+
* This is the preferred method for parsing JSON from external sources
|
|
11
|
+
* (user input, API responses, etc.) where invalid JSON is possible.
|
|
12
|
+
*
|
|
13
|
+
* @param str - The string to parse as JSON
|
|
14
|
+
* @returns Object with success flag and either data or error
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* const result = safeParseJsonResult<UserData>(userInput);
|
|
19
|
+
* if (result.success) {
|
|
20
|
+
* console.log(result.data.name);
|
|
21
|
+
* } else {
|
|
22
|
+
* console.error('Invalid JSON:', result.error.message);
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export function safeParseJsonResult(str) {
|
|
27
|
+
try {
|
|
28
|
+
const data = JSON.parse(str);
|
|
29
|
+
return { success: true, data, error: null };
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
return {
|
|
33
|
+
success: false,
|
|
34
|
+
data: null,
|
|
35
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Safely parse JSON with fallback value.
|
|
41
|
+
*
|
|
42
|
+
* Useful when you need a value regardless of parse success.
|
|
43
|
+
*
|
|
44
|
+
* @param str - The string to parse as JSON
|
|
45
|
+
* @param fallback - Value to return if parsing fails
|
|
46
|
+
* @returns Parsed value or fallback
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* const config = safeParseJson(configString, { theme: 'light' });
|
|
51
|
+
* // Always returns an object, never throws
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export function safeParseJson(str, fallback) {
|
|
55
|
+
try {
|
|
56
|
+
return JSON.parse(str);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return fallback;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Parse JSON or return undefined if invalid.
|
|
64
|
+
*
|
|
65
|
+
* @param str - The string to parse as JSON
|
|
66
|
+
* @returns Parsed value or undefined
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* const data = parseJsonOrUndefined<Config>(input);
|
|
71
|
+
* if (data) {
|
|
72
|
+
* // Use data
|
|
73
|
+
* }
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
export function parseJsonOrUndefined(str) {
|
|
77
|
+
try {
|
|
78
|
+
return JSON.parse(str);
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Parse JSON or return null if invalid.
|
|
86
|
+
*
|
|
87
|
+
* @param str - The string to parse as JSON
|
|
88
|
+
* @returns Parsed value or null
|
|
89
|
+
*
|
|
90
|
+
* @example
|
|
91
|
+
* ```typescript
|
|
92
|
+
* const data = parseJsonOrNull<Config>(input);
|
|
93
|
+
* if (data !== null) {
|
|
94
|
+
* // Use data
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
export function parseJsonOrNull(str) {
|
|
99
|
+
try {
|
|
100
|
+
return JSON.parse(str);
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Check if a string is valid JSON.
|
|
108
|
+
*
|
|
109
|
+
* @param str - The string to validate
|
|
110
|
+
* @returns true if the string is valid JSON
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* if (isValidJson(userInput)) {
|
|
115
|
+
* // Proceed with parsing
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export function isValidJson(str) {
|
|
120
|
+
try {
|
|
121
|
+
JSON.parse(str);
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Safe stringify with circular reference handling.
|
|
130
|
+
*
|
|
131
|
+
* Handles circular references, BigInt values, and other edge cases gracefully.
|
|
132
|
+
*
|
|
133
|
+
* @param obj - Value to stringify
|
|
134
|
+
* @param space - Optional indentation (number of spaces)
|
|
135
|
+
* @returns JSON string, with circular references replaced by "[Circular]"
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```typescript
|
|
139
|
+
* const obj = { name: 'test' };
|
|
140
|
+
* obj.self = obj; // Circular reference
|
|
141
|
+
*
|
|
142
|
+
* const json = safeStringify(obj);
|
|
143
|
+
* // Returns: '{"name":"test","self":"[Circular]"}'
|
|
144
|
+
*
|
|
145
|
+
* const prettyJson = safeStringify(data, 2);
|
|
146
|
+
* // Returns formatted JSON with 2-space indentation
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
export function safeStringify(obj, space) {
|
|
150
|
+
const seen = new WeakSet();
|
|
151
|
+
return JSON.stringify(obj, (_key, value) => {
|
|
152
|
+
// Handle BigInt
|
|
153
|
+
if (typeof value === "bigint") {
|
|
154
|
+
return value.toString();
|
|
155
|
+
}
|
|
156
|
+
// Handle circular references
|
|
157
|
+
if (typeof value === "object" && value !== null) {
|
|
158
|
+
if (seen.has(value)) {
|
|
159
|
+
return "[Circular]";
|
|
160
|
+
}
|
|
161
|
+
seen.add(value);
|
|
162
|
+
}
|
|
163
|
+
return value;
|
|
164
|
+
}, space);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Safe stringify with options for more control.
|
|
168
|
+
*
|
|
169
|
+
* @param obj - Value to stringify
|
|
170
|
+
* @param options - Stringify options
|
|
171
|
+
* @returns JSON string or fallback string on error
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```typescript
|
|
175
|
+
* const json = safeStringifyWithOptions(data, {
|
|
176
|
+
* pretty: true,
|
|
177
|
+
* fallback: '{}',
|
|
178
|
+
* });
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export function safeStringifyWithOptions(obj, options) {
|
|
182
|
+
const { pretty = false, fallback = "[Unable to stringify]" } = options ?? {};
|
|
183
|
+
try {
|
|
184
|
+
const space = pretty ? 2 : undefined;
|
|
185
|
+
return safeStringify(obj, space);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return fallback;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
* Centralized logic for building message arrays from TextGenerationOptions
|
|
4
4
|
* Enhanced with multimodal support for images
|
|
5
5
|
*/
|
|
6
|
+
import type { CoreMessage } from "ai";
|
|
6
7
|
import type { MultimodalChatMessage } from "../types/conversation.js";
|
|
8
|
+
import type { GenerateOptions } from "../types/generateTypes.js";
|
|
7
9
|
import type { TextGenerationOptions } from "../types/index.js";
|
|
8
10
|
import type { StreamOptions } from "../types/streamTypes.js";
|
|
9
|
-
import type { GenerateOptions } from "../types/generateTypes.js";
|
|
10
|
-
import type { CoreMessage } from "ai";
|
|
11
11
|
/**
|
|
12
12
|
* Type-safe conversion from MultimodalChatMessage[] to CoreMessage[]
|
|
13
13
|
* Filters out invalid content and ensures strict CoreMessage contract compliance
|
|
@@ -3,15 +3,15 @@
|
|
|
3
3
|
* Centralized logic for building message arrays from TextGenerationOptions
|
|
4
4
|
* Enhanced with multimodal support for images
|
|
5
5
|
*/
|
|
6
|
+
import { existsSync, readFileSync } from "fs";
|
|
7
|
+
import { getGlobalDispatcher, interceptors, request } from "undici";
|
|
8
|
+
import { MultimodalLogger, ProviderImageAdapter, } from "../adapters/providerImageAdapter.js";
|
|
6
9
|
import { CONVERSATION_INSTRUCTIONS, STRUCTURED_OUTPUT_INSTRUCTIONS, } from "../config/conversationMemory.js";
|
|
7
|
-
import { ProviderImageAdapter, MultimodalLogger, } from "../adapters/providerImageAdapter.js";
|
|
8
|
-
import { logger } from "./logger.js";
|
|
9
10
|
import { FileDetector } from "./fileDetector.js";
|
|
10
|
-
import { PDFProcessor, PDFImageConverter } from "./pdfProcessor.js";
|
|
11
|
-
import { urlDownloadRateLimiter } from "./rateLimiter.js";
|
|
12
|
-
import { request, getGlobalDispatcher, interceptors } from "undici";
|
|
13
11
|
import { getImageCache } from "./imageCache.js";
|
|
14
|
-
import {
|
|
12
|
+
import { logger } from "./logger.js";
|
|
13
|
+
import { PDFImageConverter, PDFProcessor } from "./pdfProcessor.js";
|
|
14
|
+
import { urlDownloadRateLimiter } from "./rateLimiter.js";
|
|
15
15
|
/**
|
|
16
16
|
* Type guard to check if an image input has alt text
|
|
17
17
|
*/
|
|
@@ -376,7 +376,7 @@ export async function buildMultimodalMessagesArray(options, provider, model) {
|
|
|
376
376
|
try {
|
|
377
377
|
const result = await FileDetector.detectAndProcess(file, {
|
|
378
378
|
maxSize,
|
|
379
|
-
allowedTypes: ["csv", "image", "pdf"],
|
|
379
|
+
allowedTypes: ["csv", "image", "pdf", "svg"],
|
|
380
380
|
csvOptions: options.csvOptions,
|
|
381
381
|
provider: provider,
|
|
382
382
|
});
|
|
@@ -396,6 +396,14 @@ export async function buildMultimodalMessagesArray(options, provider, model) {
|
|
|
396
396
|
options.input.text += csvSection;
|
|
397
397
|
logger.info(`[FileDetector] ✅ CSV: ${filename}`);
|
|
398
398
|
}
|
|
399
|
+
else if (result.type === "svg") {
|
|
400
|
+
// SVG is processed as text content (sanitized XML markup)
|
|
401
|
+
// Inject into text prompt instead of sending as image
|
|
402
|
+
const filename = extractFilename(file);
|
|
403
|
+
const svgSection = `\n\n## SVG Content from "${filename}":\n\`\`\`xml\n${result.content}\n\`\`\`\n`;
|
|
404
|
+
options.input.text += svgSection;
|
|
405
|
+
logger.info(`[FileDetector] ✅ SVG (as text): ${filename}`);
|
|
406
|
+
}
|
|
399
407
|
else if (result.type === "image") {
|
|
400
408
|
options.input.images = [
|
|
401
409
|
...(options.input.images || []),
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Filename and Display Name Sanitization Utilities
|
|
3
|
+
* Prevents path traversal attacks and filesystem issues
|
|
4
|
+
*
|
|
5
|
+
* This module provides:
|
|
6
|
+
* - Filename sanitization for safe filesystem storage
|
|
7
|
+
* - Display name sanitization for user-facing content
|
|
8
|
+
* - Path traversal prevention
|
|
9
|
+
*
|
|
10
|
+
* @see https://cheatsheetseries.owasp.org/cheatsheets/Input_Validation_Cheat_Sheet.html
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Options for filename sanitization.
|
|
14
|
+
*/
|
|
15
|
+
export interface SanitizeFileNameOptions {
|
|
16
|
+
/** Maximum length for the filename (default: 255) */
|
|
17
|
+
maxLength?: number;
|
|
18
|
+
/** Replacement character for invalid chars (default: '_') */
|
|
19
|
+
replacement?: string;
|
|
20
|
+
/** Whether to block dangerous extensions (default: true) */
|
|
21
|
+
blockDangerousExtensions?: boolean;
|
|
22
|
+
/** Whether to allow hidden files starting with dot (default: false) */
|
|
23
|
+
allowHiddenFiles?: boolean;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Options for display name sanitization.
|
|
27
|
+
*/
|
|
28
|
+
export interface SanitizeDisplayNameOptions {
|
|
29
|
+
/** Maximum length for the name (default: 100) */
|
|
30
|
+
maxLength?: number;
|
|
31
|
+
/** Whether to allow unicode characters (default: true) */
|
|
32
|
+
allowUnicode?: boolean;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Sanitize a filename for safe filesystem storage.
|
|
36
|
+
* Removes characters that are invalid on various operating systems.
|
|
37
|
+
*
|
|
38
|
+
* @param filename - Raw filename to sanitize
|
|
39
|
+
* @param options - Sanitization options
|
|
40
|
+
* @returns Safe filename
|
|
41
|
+
* @throws Error if filename is empty after sanitization
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* sanitizeFileName('my:file<name>.txt');
|
|
45
|
+
* // Returns: 'my_file_name_.txt'
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* sanitizeFileName('../../../etc/passwd');
|
|
49
|
+
* // Returns: '______etc_passwd'
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* sanitizeFileName('malware.exe', { blockDangerousExtensions: true });
|
|
53
|
+
* // Throws: Error - dangerous extension
|
|
54
|
+
*/
|
|
55
|
+
export declare function sanitizeFileName(filename: string, options?: SanitizeFileNameOptions): string;
|
|
56
|
+
/**
|
|
57
|
+
* Sanitize a display name for safe user-facing display.
|
|
58
|
+
* Removes control characters and limits length.
|
|
59
|
+
*
|
|
60
|
+
* @param name - Raw display name to sanitize
|
|
61
|
+
* @param options - Sanitization options
|
|
62
|
+
* @returns Safe display name
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* sanitizeDisplayName(' John\x00Doe ');
|
|
66
|
+
* // Returns: 'John Doe'
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* sanitizeDisplayName('User<script>alert(1)</script>');
|
|
70
|
+
* // Returns: 'User'
|
|
71
|
+
*/
|
|
72
|
+
export declare function sanitizeDisplayName(name: string, options?: SanitizeDisplayNameOptions): string;
|
|
73
|
+
/**
|
|
74
|
+
* Validate a display name strictly.
|
|
75
|
+
* Only allows alphanumeric, spaces, and basic punctuation.
|
|
76
|
+
*
|
|
77
|
+
* @param name - Display name to validate
|
|
78
|
+
* @returns true if valid, false otherwise
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* isValidDisplayName('John Doe'); // true
|
|
82
|
+
* isValidDisplayName('John<Doe'); // false
|
|
83
|
+
*/
|
|
84
|
+
export declare function isValidDisplayName(name: string): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Validate a filename strictly.
|
|
87
|
+
* Only allows alphanumeric, dash, underscore, and period.
|
|
88
|
+
*
|
|
89
|
+
* @param filename - Filename to validate
|
|
90
|
+
* @returns true if valid, false otherwise
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* isValidFileName('my-file.txt'); // true
|
|
94
|
+
* isValidFileName('../passwd'); // false
|
|
95
|
+
*/
|
|
96
|
+
export declare function isValidFileName(filename: string): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Extract and sanitize the extension from a filename.
|
|
99
|
+
*
|
|
100
|
+
* @param filename - Filename to extract extension from
|
|
101
|
+
* @returns Lowercase extension including the dot, or empty string
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* getFileExtension('document.PDF'); // '.pdf'
|
|
105
|
+
* getFileExtension('noextension'); // ''
|
|
106
|
+
*/
|
|
107
|
+
export declare function getFileExtension(filename: string): string;
|
|
108
|
+
/**
|
|
109
|
+
* Check if a file extension is considered dangerous.
|
|
110
|
+
*
|
|
111
|
+
* @param extension - File extension to check (with or without leading dot)
|
|
112
|
+
* @returns true if extension is dangerous
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* isDangerousExtension('.exe'); // true
|
|
116
|
+
* isDangerousExtension('pdf'); // false
|
|
117
|
+
*/
|
|
118
|
+
export declare function isDangerousExtension(extension: string): boolean;
|
|
119
|
+
/**
|
|
120
|
+
* Generate a safe filename from arbitrary input.
|
|
121
|
+
* Creates a valid filename even from completely invalid input.
|
|
122
|
+
*
|
|
123
|
+
* @param input - Any string input
|
|
124
|
+
* @param defaultName - Default name if input sanitizes to empty (default: 'file')
|
|
125
|
+
* @param extension - Optional extension to append
|
|
126
|
+
* @returns Safe filename
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* generateSafeFileName('My Document!@#$'); // 'My_Document_'
|
|
130
|
+
* generateSafeFileName('', 'untitled', '.txt'); // 'untitled.txt'
|
|
131
|
+
*/
|
|
132
|
+
export declare function generateSafeFileName(input: string, defaultName?: string, extension?: string): string;
|
|
133
|
+
/**
|
|
134
|
+
* Get the list of dangerous file extensions.
|
|
135
|
+
* Useful for validation UI or documentation.
|
|
136
|
+
*/
|
|
137
|
+
export declare function getDangerousExtensions(): string[];
|