@juspay/neurolink 9.1.1 → 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.
Files changed (237) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +54 -7
  3. package/dist/agent/directTools.d.ts +3 -3
  4. package/dist/cli/commands/config.d.ts +6 -6
  5. package/dist/image-gen/ImageGenService.d.ts +143 -0
  6. package/dist/image-gen/ImageGenService.js +345 -0
  7. package/dist/image-gen/imageGenTools.d.ts +126 -0
  8. package/dist/image-gen/imageGenTools.js +304 -0
  9. package/dist/image-gen/index.d.ts +46 -0
  10. package/dist/image-gen/index.js +48 -0
  11. package/dist/image-gen/types.d.ts +237 -0
  12. package/dist/image-gen/types.js +24 -0
  13. package/dist/lib/agent/directTools.d.ts +3 -3
  14. package/dist/lib/image-gen/ImageGenService.d.ts +143 -0
  15. package/dist/lib/image-gen/ImageGenService.js +346 -0
  16. package/dist/lib/image-gen/imageGenTools.d.ts +126 -0
  17. package/dist/lib/image-gen/imageGenTools.js +305 -0
  18. package/dist/lib/image-gen/index.d.ts +46 -0
  19. package/dist/lib/image-gen/index.js +49 -0
  20. package/dist/lib/image-gen/types.d.ts +237 -0
  21. package/dist/lib/image-gen/types.js +25 -0
  22. package/dist/lib/processors/base/BaseFileProcessor.d.ts +273 -0
  23. package/dist/lib/processors/base/BaseFileProcessor.js +614 -0
  24. package/dist/lib/processors/base/index.d.ts +14 -0
  25. package/dist/lib/processors/base/index.js +20 -0
  26. package/dist/lib/processors/base/types.d.ts +593 -0
  27. package/dist/lib/processors/base/types.js +77 -0
  28. package/dist/lib/processors/cli/fileProcessorCli.d.ts +163 -0
  29. package/dist/lib/processors/cli/fileProcessorCli.js +389 -0
  30. package/dist/lib/processors/cli/index.d.ts +37 -0
  31. package/dist/lib/processors/cli/index.js +50 -0
  32. package/dist/lib/processors/code/ConfigProcessor.d.ts +171 -0
  33. package/dist/lib/processors/code/ConfigProcessor.js +401 -0
  34. package/dist/lib/processors/code/SourceCodeProcessor.d.ts +174 -0
  35. package/dist/lib/processors/code/SourceCodeProcessor.js +305 -0
  36. package/dist/lib/processors/code/index.d.ts +44 -0
  37. package/dist/lib/processors/code/index.js +61 -0
  38. package/dist/lib/processors/config/fileTypes.d.ts +283 -0
  39. package/dist/lib/processors/config/fileTypes.js +521 -0
  40. package/dist/lib/processors/config/index.d.ts +32 -0
  41. package/dist/lib/processors/config/index.js +93 -0
  42. package/dist/lib/processors/config/languageMap.d.ts +66 -0
  43. package/dist/lib/processors/config/languageMap.js +411 -0
  44. package/dist/lib/processors/config/mimeTypes.d.ts +376 -0
  45. package/dist/lib/processors/config/mimeTypes.js +339 -0
  46. package/dist/lib/processors/config/sizeLimits.d.ts +194 -0
  47. package/dist/lib/processors/config/sizeLimits.js +247 -0
  48. package/dist/lib/processors/data/JsonProcessor.d.ts +122 -0
  49. package/dist/lib/processors/data/JsonProcessor.js +204 -0
  50. package/dist/lib/processors/data/XmlProcessor.d.ts +160 -0
  51. package/dist/lib/processors/data/XmlProcessor.js +284 -0
  52. package/dist/lib/processors/data/YamlProcessor.d.ts +163 -0
  53. package/dist/lib/processors/data/YamlProcessor.js +295 -0
  54. package/dist/lib/processors/data/index.d.ts +49 -0
  55. package/dist/lib/processors/data/index.js +77 -0
  56. package/dist/lib/processors/document/ExcelProcessor.d.ts +238 -0
  57. package/dist/lib/processors/document/ExcelProcessor.js +520 -0
  58. package/dist/lib/processors/document/OpenDocumentProcessor.d.ts +69 -0
  59. package/dist/lib/processors/document/OpenDocumentProcessor.js +211 -0
  60. package/dist/lib/processors/document/RtfProcessor.d.ts +152 -0
  61. package/dist/lib/processors/document/RtfProcessor.js +362 -0
  62. package/dist/lib/processors/document/WordProcessor.d.ts +168 -0
  63. package/dist/lib/processors/document/WordProcessor.js +354 -0
  64. package/dist/lib/processors/document/index.d.ts +54 -0
  65. package/dist/lib/processors/document/index.js +91 -0
  66. package/dist/lib/processors/errors/FileErrorCode.d.ts +98 -0
  67. package/dist/lib/processors/errors/FileErrorCode.js +256 -0
  68. package/dist/lib/processors/errors/errorHelpers.d.ts +151 -0
  69. package/dist/lib/processors/errors/errorHelpers.js +379 -0
  70. package/dist/lib/processors/errors/errorSerializer.d.ts +139 -0
  71. package/dist/lib/processors/errors/errorSerializer.js +508 -0
  72. package/dist/lib/processors/errors/index.d.ts +46 -0
  73. package/dist/lib/processors/errors/index.js +50 -0
  74. package/dist/lib/processors/index.d.ts +76 -0
  75. package/dist/lib/processors/index.js +113 -0
  76. package/dist/lib/processors/integration/FileProcessorIntegration.d.ts +244 -0
  77. package/dist/lib/processors/integration/FileProcessorIntegration.js +273 -0
  78. package/dist/lib/processors/integration/index.d.ts +42 -0
  79. package/dist/lib/processors/integration/index.js +45 -0
  80. package/dist/lib/processors/markup/HtmlProcessor.d.ts +169 -0
  81. package/dist/lib/processors/markup/HtmlProcessor.js +250 -0
  82. package/dist/lib/processors/markup/MarkdownProcessor.d.ts +165 -0
  83. package/dist/lib/processors/markup/MarkdownProcessor.js +245 -0
  84. package/dist/lib/processors/markup/SvgProcessor.d.ts +156 -0
  85. package/dist/lib/processors/markup/SvgProcessor.js +241 -0
  86. package/dist/lib/processors/markup/TextProcessor.d.ts +135 -0
  87. package/dist/lib/processors/markup/TextProcessor.js +189 -0
  88. package/dist/lib/processors/markup/index.d.ts +66 -0
  89. package/dist/lib/processors/markup/index.js +103 -0
  90. package/dist/lib/processors/registry/ProcessorRegistry.d.ts +334 -0
  91. package/dist/lib/processors/registry/ProcessorRegistry.js +609 -0
  92. package/dist/lib/processors/registry/index.d.ts +12 -0
  93. package/dist/lib/processors/registry/index.js +17 -0
  94. package/dist/lib/processors/registry/types.d.ts +53 -0
  95. package/dist/lib/processors/registry/types.js +11 -0
  96. package/dist/lib/providers/sagemaker/language-model.d.ts +2 -2
  97. package/dist/lib/server/utils/validation.d.ts +6 -6
  98. package/dist/lib/types/fileTypes.d.ts +1 -1
  99. package/dist/lib/types/index.d.ts +25 -24
  100. package/dist/lib/types/index.js +21 -20
  101. package/dist/lib/types/modelTypes.d.ts +18 -18
  102. package/dist/lib/types/pptTypes.d.ts +14 -2
  103. package/dist/lib/types/pptTypes.js +16 -0
  104. package/dist/lib/utils/async/delay.d.ts +40 -0
  105. package/dist/lib/utils/async/delay.js +43 -0
  106. package/dist/lib/utils/async/index.d.ts +23 -0
  107. package/dist/lib/utils/async/index.js +24 -0
  108. package/dist/lib/utils/async/retry.d.ts +141 -0
  109. package/dist/lib/utils/async/retry.js +172 -0
  110. package/dist/lib/utils/async/withTimeout.d.ts +73 -0
  111. package/dist/lib/utils/async/withTimeout.js +97 -0
  112. package/dist/lib/utils/fileDetector.d.ts +7 -1
  113. package/dist/lib/utils/fileDetector.js +91 -18
  114. package/dist/lib/utils/json/extract.d.ts +103 -0
  115. package/dist/lib/utils/json/extract.js +249 -0
  116. package/dist/lib/utils/json/index.d.ts +36 -0
  117. package/dist/lib/utils/json/index.js +37 -0
  118. package/dist/lib/utils/json/safeParse.d.ts +137 -0
  119. package/dist/lib/utils/json/safeParse.js +191 -0
  120. package/dist/lib/utils/messageBuilder.d.ts +2 -2
  121. package/dist/lib/utils/messageBuilder.js +15 -7
  122. package/dist/lib/utils/sanitizers/filename.d.ts +137 -0
  123. package/dist/lib/utils/sanitizers/filename.js +366 -0
  124. package/dist/lib/utils/sanitizers/html.d.ts +170 -0
  125. package/dist/lib/utils/sanitizers/html.js +326 -0
  126. package/dist/lib/utils/sanitizers/index.d.ts +26 -0
  127. package/dist/lib/utils/sanitizers/index.js +30 -0
  128. package/dist/lib/utils/sanitizers/svg.d.ts +81 -0
  129. package/dist/lib/utils/sanitizers/svg.js +483 -0
  130. package/dist/processors/base/BaseFileProcessor.d.ts +273 -0
  131. package/dist/processors/base/BaseFileProcessor.js +613 -0
  132. package/dist/processors/base/index.d.ts +14 -0
  133. package/dist/processors/base/index.js +19 -0
  134. package/dist/processors/base/types.d.ts +593 -0
  135. package/dist/processors/base/types.js +76 -0
  136. package/dist/processors/cli/fileProcessorCli.d.ts +163 -0
  137. package/dist/processors/cli/fileProcessorCli.js +388 -0
  138. package/dist/processors/cli/index.d.ts +37 -0
  139. package/dist/processors/cli/index.js +49 -0
  140. package/dist/processors/code/ConfigProcessor.d.ts +171 -0
  141. package/dist/processors/code/ConfigProcessor.js +400 -0
  142. package/dist/processors/code/SourceCodeProcessor.d.ts +174 -0
  143. package/dist/processors/code/SourceCodeProcessor.js +304 -0
  144. package/dist/processors/code/index.d.ts +44 -0
  145. package/dist/processors/code/index.js +60 -0
  146. package/dist/processors/config/fileTypes.d.ts +283 -0
  147. package/dist/processors/config/fileTypes.js +520 -0
  148. package/dist/processors/config/index.d.ts +32 -0
  149. package/dist/processors/config/index.js +92 -0
  150. package/dist/processors/config/languageMap.d.ts +66 -0
  151. package/dist/processors/config/languageMap.js +410 -0
  152. package/dist/processors/config/mimeTypes.d.ts +376 -0
  153. package/dist/processors/config/mimeTypes.js +338 -0
  154. package/dist/processors/config/sizeLimits.d.ts +194 -0
  155. package/dist/processors/config/sizeLimits.js +246 -0
  156. package/dist/processors/data/JsonProcessor.d.ts +122 -0
  157. package/dist/processors/data/JsonProcessor.js +203 -0
  158. package/dist/processors/data/XmlProcessor.d.ts +160 -0
  159. package/dist/processors/data/XmlProcessor.js +283 -0
  160. package/dist/processors/data/YamlProcessor.d.ts +163 -0
  161. package/dist/processors/data/YamlProcessor.js +294 -0
  162. package/dist/processors/data/index.d.ts +49 -0
  163. package/dist/processors/data/index.js +76 -0
  164. package/dist/processors/document/ExcelProcessor.d.ts +238 -0
  165. package/dist/processors/document/ExcelProcessor.js +519 -0
  166. package/dist/processors/document/OpenDocumentProcessor.d.ts +69 -0
  167. package/dist/processors/document/OpenDocumentProcessor.js +210 -0
  168. package/dist/processors/document/RtfProcessor.d.ts +152 -0
  169. package/dist/processors/document/RtfProcessor.js +361 -0
  170. package/dist/processors/document/WordProcessor.d.ts +168 -0
  171. package/dist/processors/document/WordProcessor.js +353 -0
  172. package/dist/processors/document/index.d.ts +54 -0
  173. package/dist/processors/document/index.js +90 -0
  174. package/dist/processors/errors/FileErrorCode.d.ts +98 -0
  175. package/dist/processors/errors/FileErrorCode.js +255 -0
  176. package/dist/processors/errors/errorHelpers.d.ts +151 -0
  177. package/dist/processors/errors/errorHelpers.js +378 -0
  178. package/dist/processors/errors/errorSerializer.d.ts +139 -0
  179. package/dist/processors/errors/errorSerializer.js +507 -0
  180. package/dist/processors/errors/index.d.ts +46 -0
  181. package/dist/processors/errors/index.js +49 -0
  182. package/dist/processors/index.d.ts +76 -0
  183. package/dist/processors/index.js +112 -0
  184. package/dist/processors/integration/FileProcessorIntegration.d.ts +244 -0
  185. package/dist/processors/integration/FileProcessorIntegration.js +272 -0
  186. package/dist/processors/integration/index.d.ts +42 -0
  187. package/dist/processors/integration/index.js +44 -0
  188. package/dist/processors/markup/HtmlProcessor.d.ts +169 -0
  189. package/dist/processors/markup/HtmlProcessor.js +249 -0
  190. package/dist/processors/markup/MarkdownProcessor.d.ts +165 -0
  191. package/dist/processors/markup/MarkdownProcessor.js +244 -0
  192. package/dist/processors/markup/SvgProcessor.d.ts +156 -0
  193. package/dist/processors/markup/SvgProcessor.js +240 -0
  194. package/dist/processors/markup/TextProcessor.d.ts +135 -0
  195. package/dist/processors/markup/TextProcessor.js +188 -0
  196. package/dist/processors/markup/index.d.ts +66 -0
  197. package/dist/processors/markup/index.js +102 -0
  198. package/dist/processors/registry/ProcessorRegistry.d.ts +334 -0
  199. package/dist/processors/registry/ProcessorRegistry.js +608 -0
  200. package/dist/processors/registry/index.d.ts +12 -0
  201. package/dist/processors/registry/index.js +16 -0
  202. package/dist/processors/registry/types.d.ts +53 -0
  203. package/dist/processors/registry/types.js +10 -0
  204. package/dist/server/utils/validation.d.ts +6 -6
  205. package/dist/types/fileTypes.d.ts +1 -1
  206. package/dist/types/index.d.ts +25 -24
  207. package/dist/types/index.js +21 -20
  208. package/dist/types/modelTypes.d.ts +10 -10
  209. package/dist/types/pptTypes.d.ts +14 -2
  210. package/dist/types/pptTypes.js +16 -0
  211. package/dist/utils/async/delay.d.ts +40 -0
  212. package/dist/utils/async/delay.js +42 -0
  213. package/dist/utils/async/index.d.ts +23 -0
  214. package/dist/utils/async/index.js +23 -0
  215. package/dist/utils/async/retry.d.ts +141 -0
  216. package/dist/utils/async/retry.js +171 -0
  217. package/dist/utils/async/withTimeout.d.ts +73 -0
  218. package/dist/utils/async/withTimeout.js +96 -0
  219. package/dist/utils/fileDetector.d.ts +7 -1
  220. package/dist/utils/fileDetector.js +91 -18
  221. package/dist/utils/json/extract.d.ts +103 -0
  222. package/dist/utils/json/extract.js +248 -0
  223. package/dist/utils/json/index.d.ts +36 -0
  224. package/dist/utils/json/index.js +36 -0
  225. package/dist/utils/json/safeParse.d.ts +137 -0
  226. package/dist/utils/json/safeParse.js +190 -0
  227. package/dist/utils/messageBuilder.d.ts +2 -2
  228. package/dist/utils/messageBuilder.js +15 -7
  229. package/dist/utils/sanitizers/filename.d.ts +137 -0
  230. package/dist/utils/sanitizers/filename.js +365 -0
  231. package/dist/utils/sanitizers/html.d.ts +170 -0
  232. package/dist/utils/sanitizers/html.js +325 -0
  233. package/dist/utils/sanitizers/index.d.ts +26 -0
  234. package/dist/utils/sanitizers/index.js +29 -0
  235. package/dist/utils/sanitizers/svg.d.ts +81 -0
  236. package/dist/utils/sanitizers/svg.js +482 -0
  237. package/package.json +2 -2
@@ -0,0 +1,346 @@
1
+ /**
2
+ * Image Generation Service
3
+ *
4
+ * Handles AI image generation using NeuroLink SDK with configurable providers
5
+ * and models. Supports reference images and PDFs for contextual generation.
6
+ *
7
+ * @packageDocumentation
8
+ * @module @juspay/neurolink/image-gen
9
+ * @category ImageGeneration
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { ImageGenService } from '@juspay/neurolink';
14
+ *
15
+ * const service = new ImageGenService();
16
+ *
17
+ * const result = await service.generate({
18
+ * prompt: 'A serene mountain landscape at sunset',
19
+ * style: 'photorealistic',
20
+ * aspectRatio: '16:9'
21
+ * });
22
+ *
23
+ * if (result.success && result.imageBuffer) {
24
+ * fs.writeFileSync('output.png', result.imageBuffer);
25
+ * }
26
+ * ```
27
+ */
28
+ import { withTimeout } from "../utils/errorHandling.js";
29
+ import { DEFAULT_IMAGE_GEN_CONFIG } from "./types.js";
30
+ /**
31
+ * Image generation service for AI-powered image creation
32
+ *
33
+ * Uses NeuroLink SDK to generate images with support for:
34
+ * - Multiple providers (Vertex AI, OpenAI, etc.)
35
+ * - Reference images for style guidance
36
+ * - PDF documents for contextual generation
37
+ * - Configurable aspect ratios and styles
38
+ *
39
+ * @example Basic usage
40
+ * ```typescript
41
+ * const service = new ImageGenService();
42
+ * const result = await service.generate({
43
+ * prompt: 'A cute robot playing chess'
44
+ * });
45
+ * ```
46
+ *
47
+ * @example With custom configuration
48
+ * ```typescript
49
+ * const service = new ImageGenService({
50
+ * defaultProvider: 'openai',
51
+ * defaultModel: 'dall-e-3',
52
+ * timeout: 60000
53
+ * });
54
+ * ```
55
+ */
56
+ export class ImageGenService {
57
+ config;
58
+ neurolinkInstance = null;
59
+ instanceId;
60
+ /**
61
+ * Create a new ImageGenService instance
62
+ *
63
+ * @param config - Optional configuration overrides
64
+ */
65
+ constructor(config) {
66
+ this.config = {
67
+ ...DEFAULT_IMAGE_GEN_CONFIG,
68
+ ...config,
69
+ };
70
+ this.instanceId = `ImageGenService-${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
71
+ }
72
+ /**
73
+ * Get or create the NeuroLink instance
74
+ * Uses dynamic import to avoid circular dependencies
75
+ *
76
+ * @returns NeuroLink instance for image generation
77
+ */
78
+ async getNeuroLink() {
79
+ if (!this.neurolinkInstance) {
80
+ // Dynamically import to avoid circular dependencies
81
+ const { NeuroLink } = await import("../neurolink.js");
82
+ this.neurolinkInstance = new NeuroLink({
83
+ conversationMemory: { enabled: false },
84
+ enableOrchestration: false,
85
+ });
86
+ }
87
+ return this.neurolinkInstance;
88
+ }
89
+ /**
90
+ * Generate an image from a text prompt
91
+ *
92
+ * @param options - Generation options including prompt, style, etc.
93
+ * @returns Promise resolving to generation result
94
+ *
95
+ * @example Simple generation
96
+ * ```typescript
97
+ * const result = await service.generate({
98
+ * prompt: 'A futuristic cityscape'
99
+ * });
100
+ * ```
101
+ *
102
+ * @example With reference images
103
+ * ```typescript
104
+ * const referenceImage = fs.readFileSync('style-reference.jpg');
105
+ * const result = await service.generate({
106
+ * prompt: 'A portrait in this style',
107
+ * images: [referenceImage],
108
+ * aspectRatio: '1:1'
109
+ * });
110
+ * ```
111
+ */
112
+ async generate(options) {
113
+ const startTime = Date.now();
114
+ // Check if service is enabled
115
+ if (!this.config.enabled) {
116
+ return {
117
+ success: false,
118
+ error: "Image generation is disabled",
119
+ };
120
+ }
121
+ // Validate prompt
122
+ if (!options.prompt || options.prompt.trim().length === 0) {
123
+ return {
124
+ success: false,
125
+ error: "Prompt is required for image generation",
126
+ };
127
+ }
128
+ try {
129
+ const neurolink = await this.getNeuroLink();
130
+ // Build enhanced prompt with style and negative prompt
131
+ let enhancedPrompt = options.prompt;
132
+ if (options.style) {
133
+ enhancedPrompt = `${options.style} style: ${enhancedPrompt}`;
134
+ }
135
+ if (options.negativePrompt) {
136
+ enhancedPrompt = `${enhancedPrompt}. Avoid: ${options.negativePrompt}`;
137
+ }
138
+ // Build input with optional reference images/PDFs
139
+ const input = { text: enhancedPrompt };
140
+ // Process reference images
141
+ if (options.images?.length) {
142
+ const maxImages = this.config.maxReferenceImages ?? 5;
143
+ const imagesToUse = options.images.slice(0, maxImages);
144
+ // Convert Buffers to base64 strings if needed
145
+ input.images = imagesToUse.map((img) => Buffer.isBuffer(img) ? img.toString("base64") : img);
146
+ }
147
+ // Process reference PDFs
148
+ if (options.pdfFiles?.length) {
149
+ const maxPdfs = this.config.maxReferencePdfs ?? 1;
150
+ input.pdfFiles = options.pdfFiles.slice(0, maxPdfs);
151
+ }
152
+ // Determine provider and model
153
+ const provider = options.provider ?? this.config.defaultProvider;
154
+ const model = options.model ?? this.config.defaultModel;
155
+ const region = options.region ?? this.config.defaultRegion;
156
+ // Build generation parameters
157
+ const generateParams = {
158
+ input,
159
+ provider,
160
+ model,
161
+ disableTools: true,
162
+ temperature: options.temperature ?? this.config.defaultTemperature ?? 0.75,
163
+ timeout: this.config.timeout,
164
+ };
165
+ // Add region if specified (for Vertex AI)
166
+ if (region) {
167
+ generateParams.region = region;
168
+ }
169
+ // Add number of images
170
+ const maxImages = this.config.maxImages ?? 4;
171
+ const numberOfImages = Math.min(options.numberOfImages ?? 1, maxImages);
172
+ generateParams.numberOfImages = numberOfImages;
173
+ // Add aspect ratio if specified
174
+ if (options.aspectRatio) {
175
+ generateParams.aspectRatio = options.aspectRatio;
176
+ }
177
+ // Call NeuroLink generate with timeout protection
178
+ const result = await withTimeout(neurolink.generate(generateParams), this.config.timeout, new Error(`Image generation timed out after ${this.config.timeout}ms`));
179
+ // Extract image from result
180
+ const imageOutput = this.extractImageFromResult(result);
181
+ const generationTimeMs = Date.now() - startTime;
182
+ if (imageOutput) {
183
+ return {
184
+ success: true,
185
+ imageBuffer: imageOutput.imageBuffer,
186
+ base64: imageOutput.base64,
187
+ mimeType: imageOutput.mimeType,
188
+ model,
189
+ provider,
190
+ generationTimeMs,
191
+ };
192
+ }
193
+ return {
194
+ success: false,
195
+ error: "No image generated",
196
+ model,
197
+ provider,
198
+ generationTimeMs,
199
+ };
200
+ }
201
+ catch (error) {
202
+ const errorMessage = error instanceof Error ? error.message : String(error);
203
+ return {
204
+ success: false,
205
+ error: `Generation failed: ${errorMessage}`,
206
+ generationTimeMs: Date.now() - startTime,
207
+ };
208
+ }
209
+ }
210
+ /**
211
+ * Extract image data from various result formats
212
+ *
213
+ * Handles multiple output formats:
214
+ * - result.imageOutput?.base64
215
+ * - result.images?.[0]
216
+ * - data:image URI in content
217
+ * - Buffer directly
218
+ *
219
+ * @param result - Raw result from NeuroLink generate
220
+ * @returns Extracted image data or null
221
+ */
222
+ extractImageFromResult(result) {
223
+ if (!result || typeof result !== "object") {
224
+ return null;
225
+ }
226
+ const res = result;
227
+ // Check for imageOutput with base64
228
+ const imageOutput = res.imageOutput ?? res.images?.[0];
229
+ if (imageOutput && typeof imageOutput === "object") {
230
+ const img = imageOutput;
231
+ if (typeof img.base64 === "string") {
232
+ const base64 = img.base64;
233
+ const mimeType = img.mimeType ?? "image/png";
234
+ return {
235
+ imageBuffer: Buffer.from(base64, "base64"),
236
+ base64,
237
+ mimeType,
238
+ };
239
+ }
240
+ }
241
+ // Check if imageOutput is a Buffer
242
+ if (Buffer.isBuffer(imageOutput)) {
243
+ const base64 = imageOutput.toString("base64");
244
+ return {
245
+ imageBuffer: imageOutput,
246
+ base64,
247
+ mimeType: "image/png",
248
+ };
249
+ }
250
+ // Check for data URI format in content
251
+ const content = res.content;
252
+ if (typeof content === "string" && content.startsWith("data:image")) {
253
+ const match = content.match(/^data:image\/(\w+);base64,(.+)$/);
254
+ if (match?.[2]) {
255
+ const mimeType = `image/${match[1]}`;
256
+ const base64 = match[2];
257
+ return {
258
+ imageBuffer: Buffer.from(base64, "base64"),
259
+ base64,
260
+ mimeType,
261
+ };
262
+ }
263
+ }
264
+ // Check for raw base64 in content
265
+ if (typeof content === "string" && !content.startsWith("data:")) {
266
+ // Try to detect if it's base64 encoded image data
267
+ try {
268
+ const buffer = Buffer.from(content, "base64");
269
+ // Check for PNG magic bytes
270
+ if (buffer[0] === 0x89 && buffer[1] === 0x50) {
271
+ return {
272
+ imageBuffer: buffer,
273
+ base64: content,
274
+ mimeType: "image/png",
275
+ };
276
+ }
277
+ // Check for JPEG magic bytes
278
+ if (buffer[0] === 0xff && buffer[1] === 0xd8) {
279
+ return {
280
+ imageBuffer: buffer,
281
+ base64: content,
282
+ mimeType: "image/jpeg",
283
+ };
284
+ }
285
+ }
286
+ catch {
287
+ // Not valid base64, ignore
288
+ }
289
+ }
290
+ return null;
291
+ }
292
+ /**
293
+ * Check if image generation is enabled
294
+ */
295
+ isEnabled() {
296
+ return this.config.enabled;
297
+ }
298
+ /**
299
+ * Get the default model
300
+ */
301
+ getModel() {
302
+ return this.config.defaultModel;
303
+ }
304
+ /**
305
+ * Get the default provider
306
+ */
307
+ getProvider() {
308
+ return this.config.defaultProvider;
309
+ }
310
+ /**
311
+ * Get the service configuration
312
+ */
313
+ getConfig() {
314
+ return { ...this.config };
315
+ }
316
+ /**
317
+ * Get the service instance ID (for debugging)
318
+ */
319
+ getInstanceId() {
320
+ return this.instanceId;
321
+ }
322
+ /**
323
+ * Update service configuration
324
+ *
325
+ * @param config - Partial configuration to merge
326
+ */
327
+ updateConfig(config) {
328
+ this.config = {
329
+ ...this.config,
330
+ ...config,
331
+ };
332
+ }
333
+ /**
334
+ * Enable image generation
335
+ */
336
+ enable() {
337
+ this.config.enabled = true;
338
+ }
339
+ /**
340
+ * Disable image generation
341
+ */
342
+ disable() {
343
+ this.config.enabled = false;
344
+ }
345
+ }
346
+ //# sourceMappingURL=ImageGenService.js.map
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Image Generation Tools
3
+ *
4
+ * Tool definitions for AI model use - enables AI models to generate
5
+ * images as part of their response workflow.
6
+ *
7
+ * @packageDocumentation
8
+ * @module @juspay/neurolink/image-gen
9
+ * @category ImageGeneration
10
+ *
11
+ * @example
12
+ * ```typescript
13
+ * import { getImageGenTools } from '@juspay/neurolink';
14
+ *
15
+ * // Get tools for AI model registration
16
+ * const tools = getImageGenTools();
17
+ *
18
+ * // Use with NeuroLink
19
+ * const neurolink = new NeuroLink();
20
+ * neurolink.registerCustomTools(tools);
21
+ * ```
22
+ */
23
+ import { ImageGenService } from "./ImageGenService.js";
24
+ import type { ImageGenConfig, ImageGenToolContext, ImageGenToolParams, ImageGenToolResponse } from "./types.js";
25
+ /**
26
+ * Tool definition interface compatible with AI SDK / MCP
27
+ */
28
+ export interface ImageGenToolDefinition {
29
+ name: string;
30
+ description: string;
31
+ inputSchema: {
32
+ type: "object";
33
+ properties: Record<string, {
34
+ type: string;
35
+ description: string;
36
+ enum?: string[];
37
+ }>;
38
+ required: string[];
39
+ };
40
+ execute: (params: ImageGenToolParams, context?: ImageGenToolContext) => Promise<ImageGenToolResponse>;
41
+ }
42
+ /**
43
+ * Create an image generation tool for use with AI models
44
+ *
45
+ * This tool allows AI models to generate images based on text prompts.
46
+ * It integrates with the ImageGenService for actual generation.
47
+ *
48
+ * @param service - ImageGenService instance to use
49
+ * @returns Tool definition compatible with MCP/AI SDK
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * const service = new ImageGenService();
54
+ * const tool = createImageGenTool(service);
55
+ *
56
+ * // Tool can now be registered with NeuroLink
57
+ * neurolink.registerTool(tool.name, {
58
+ * description: tool.description,
59
+ * parameters: tool.inputSchema,
60
+ * execute: tool.execute
61
+ * });
62
+ * ```
63
+ */
64
+ export declare function createImageGenTool(service: ImageGenService): ImageGenToolDefinition;
65
+ /**
66
+ * Create an image editing/variation tool for use with AI models
67
+ *
68
+ * This tool allows AI models to create variations of existing images
69
+ * or edit images based on reference images and prompts.
70
+ *
71
+ * @param service - ImageGenService instance to use
72
+ * @returns Tool definition compatible with MCP/AI SDK
73
+ */
74
+ export declare function createImageVariationTool(service: ImageGenService): ImageGenToolDefinition;
75
+ /**
76
+ * Get all image generation tools as an array
77
+ *
78
+ * Creates a shared ImageGenService instance and returns all
79
+ * image generation tools configured to use it.
80
+ *
81
+ * @param config - Optional configuration for the ImageGenService
82
+ * @returns Array of tool definitions
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * import { getImageGenTools, NeuroLink } from '@juspay/neurolink';
87
+ *
88
+ * const tools = getImageGenTools({
89
+ * defaultProvider: 'openai',
90
+ * defaultModel: 'dall-e-3'
91
+ * });
92
+ *
93
+ * const neurolink = new NeuroLink();
94
+ * for (const tool of tools) {
95
+ * neurolink.registerTool(tool.name, {
96
+ * description: tool.description,
97
+ * parameters: tool.inputSchema,
98
+ * execute: tool.execute
99
+ * });
100
+ * }
101
+ * ```
102
+ */
103
+ export declare function getImageGenTools(configOrService?: Partial<ImageGenConfig> | ImageGenService): ImageGenToolDefinition[];
104
+ /**
105
+ * Get only the basic image generation tool
106
+ *
107
+ * @param config - Optional configuration for the ImageGenService
108
+ * @returns Single tool definition for basic image generation
109
+ */
110
+ export declare function getBasicImageGenTool(configOrService?: Partial<ImageGenConfig> | ImageGenService): ImageGenToolDefinition;
111
+ /**
112
+ * Create a custom image generation tool with specific service configuration
113
+ *
114
+ * @param serviceConfig - Configuration for the ImageGenService
115
+ * @param toolConfig - Optional customizations for the tool definition
116
+ * @returns Customized tool definition
117
+ *
118
+ * @example
119
+ * ```typescript
120
+ * const customTool = createCustomImageGenTool(
121
+ * { defaultProvider: 'vertex', defaultModel: 'imagen-3.0-generate-001' },
122
+ * { name: 'vertex_generate_image', description: 'Generate images using Vertex AI Imagen' }
123
+ * );
124
+ * ```
125
+ */
126
+ export declare function createCustomImageGenTool(serviceConfig?: Partial<ImageGenConfig>, toolConfig?: Partial<Pick<ImageGenToolDefinition, "name" | "description">>): ImageGenToolDefinition;