@drax/ai-back 3.32.0 → 3.35.1

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 (59) hide show
  1. package/dist/config/GoogleAiConfig.js +8 -0
  2. package/dist/config/OllamaAiConfig.js +9 -0
  3. package/dist/factory/AiProviderFactory.js +12 -3
  4. package/dist/factory/GoogleAiProviderFactory.js +14 -0
  5. package/dist/factory/OllamaAiProviderFactory.js +14 -0
  6. package/dist/index.js +7 -1
  7. package/dist/providers/GoogleAiProvider.js +367 -0
  8. package/dist/providers/OllamaAiProvider.js +342 -0
  9. package/dist/tools/BuilderTool.js +9 -0
  10. package/package.json +4 -2
  11. package/src/config/GoogleAiConfig.ts +13 -0
  12. package/src/config/OllamaAiConfig.ts +14 -0
  13. package/src/factory/AiProviderFactory.ts +12 -5
  14. package/src/factory/GoogleAiProviderFactory.ts +26 -0
  15. package/src/factory/OllamaAiProviderFactory.ts +27 -0
  16. package/src/index.ts +12 -0
  17. package/src/providers/GoogleAiProvider.ts +489 -0
  18. package/src/providers/OllamaAiProvider.ts +469 -0
  19. package/src/tools/BuilderTool.ts +12 -0
  20. package/test/GoogleAiProvider.test.ts +211 -0
  21. package/test/ToolBuilder.test.ts +48 -0
  22. package/tsconfig.tsbuildinfo +1 -1
  23. package/types/config/GoogleAiConfig.d.ts +8 -0
  24. package/types/config/GoogleAiConfig.d.ts.map +1 -0
  25. package/types/config/OllamaAiConfig.d.ts +9 -0
  26. package/types/config/OllamaAiConfig.d.ts.map +1 -0
  27. package/types/factory/AiProviderFactory.d.ts.map +1 -1
  28. package/types/factory/GoogleAiProviderFactory.d.ts +8 -0
  29. package/types/factory/GoogleAiProviderFactory.d.ts.map +1 -0
  30. package/types/factory/OllamaAiProviderFactory.d.ts +8 -0
  31. package/types/factory/OllamaAiProviderFactory.d.ts.map +1 -0
  32. package/types/index.d.ts.map +1 -1
  33. package/types/providers/GoogleAiProvider.d.ts +63 -0
  34. package/types/providers/GoogleAiProvider.d.ts.map +1 -0
  35. package/types/providers/OllamaAiProvider.d.ts +78 -0
  36. package/types/providers/OllamaAiProvider.d.ts.map +1 -0
  37. package/types/tools/BuilderTool.d.ts.map +1 -1
  38. package/.env +0 -4
  39. package/dist/agents/ChatbotTaskService.js +0 -143
  40. package/dist/agents/ChatbotTaskTools.js +0 -756
  41. package/dist/controllers/AIController.js +0 -150
  42. package/dist/interfaces/IAILog.js +0 -1
  43. package/dist/routes/ChatbotTaskRoutes.js +0 -8
  44. package/dist/tools/ToolBuilder.js +0 -243
  45. package/dist/vectors/ChromaVector.js +0 -65
  46. package/types/agents/ChatbotTaskService.d.ts +0 -42
  47. package/types/agents/ChatbotTaskService.d.ts.map +0 -1
  48. package/types/agents/ChatbotTaskTools.d.ts +0 -54
  49. package/types/agents/ChatbotTaskTools.d.ts.map +0 -1
  50. package/types/controllers/AIController.d.ts +0 -25
  51. package/types/controllers/AIController.d.ts.map +0 -1
  52. package/types/interfaces/IAILog.d.ts +0 -77
  53. package/types/interfaces/IAILog.d.ts.map +0 -1
  54. package/types/routes/ChatbotTaskRoutes.d.ts +0 -4
  55. package/types/routes/ChatbotTaskRoutes.d.ts.map +0 -1
  56. package/types/tools/ToolBuilder.d.ts +0 -47
  57. package/types/tools/ToolBuilder.d.ts.map +0 -1
  58. package/types/vectors/ChromaVector.d.ts +0 -21
  59. package/types/vectors/ChromaVector.d.ts.map +0 -1
@@ -0,0 +1,8 @@
1
+ var GoogleAiConfig;
2
+ (function (GoogleAiConfig) {
3
+ GoogleAiConfig["GoogleAiApiKey"] = "GOOGLE_AI_API_KEY";
4
+ GoogleAiConfig["GoogleAiModel"] = "GOOGLE_AI_MODEL";
5
+ GoogleAiConfig["GoogleAiVisionModel"] = "GOOGLE_AI_VISION_MODEL";
6
+ })(GoogleAiConfig || (GoogleAiConfig = {}));
7
+ export default GoogleAiConfig;
8
+ export { GoogleAiConfig };
@@ -0,0 +1,9 @@
1
+ var OllamaAiConfig;
2
+ (function (OllamaAiConfig) {
3
+ OllamaAiConfig["OllamaAiBaseUrl"] = "OLLAMA_AI_BASE_URL";
4
+ OllamaAiConfig["OllamaAiModel"] = "OLLAMA_AI_MODEL";
5
+ OllamaAiConfig["OllamaAiVisionModel"] = "OLLAMA_AI_VISION_MODEL";
6
+ OllamaAiConfig["OllamaAiEmbeddingModel"] = "OLLAMA_AI_EMBEDDING_MODEL";
7
+ })(OllamaAiConfig || (OllamaAiConfig = {}));
8
+ export default OllamaAiConfig;
9
+ export { OllamaAiConfig };
@@ -1,17 +1,26 @@
1
1
  import OpenAiProviderFactory from "./OpenAiProviderFactory.js";
2
+ import GoogleAiProviderFactory from "./GoogleAiProviderFactory.js";
3
+ import OllamaAiProviderFactory from "./OllamaAiProviderFactory.js";
2
4
  class AiProviderFactory {
3
5
  static instance(provider = 'OpenAi') {
4
- if (!AiProviderFactory.singleton) {
6
+ if (!AiProviderFactory.singletons[provider]) {
5
7
  switch (provider) {
6
8
  case 'OpenAi':
7
- AiProviderFactory.singleton = OpenAiProviderFactory.instance();
9
+ AiProviderFactory.singletons[provider] = OpenAiProviderFactory.instance();
10
+ break;
11
+ case 'GoogleAi':
12
+ AiProviderFactory.singletons[provider] = GoogleAiProviderFactory.instance();
13
+ break;
14
+ case 'OllamaAi':
15
+ AiProviderFactory.singletons[provider] = OllamaAiProviderFactory.instance();
8
16
  break;
9
17
  default:
10
18
  throw new Error(`Unsupported AI provider: ${provider}`);
11
19
  }
12
20
  }
13
- return AiProviderFactory.singleton;
21
+ return AiProviderFactory.singletons[provider];
14
22
  }
15
23
  }
24
+ AiProviderFactory.singletons = {};
16
25
  export default AiProviderFactory;
17
26
  export { AiProviderFactory };
@@ -0,0 +1,14 @@
1
+ import { DraxConfig } from "@drax/common-back";
2
+ import GoogleAiConfig from "../config/GoogleAiConfig.js";
3
+ import GoogleAiProvider from "../providers/GoogleAiProvider.js";
4
+ import AILogServiceFactory from "./services/AILogServiceFactory.js";
5
+ class GoogleAiProviderFactory {
6
+ static instance() {
7
+ if (!GoogleAiProviderFactory.singleton) {
8
+ GoogleAiProviderFactory.singleton = new GoogleAiProvider(DraxConfig.getOrLoad(GoogleAiConfig.GoogleAiApiKey), DraxConfig.getOrLoad(GoogleAiConfig.GoogleAiModel), DraxConfig.getOrLoad(GoogleAiConfig.GoogleAiVisionModel), AILogServiceFactory.instance);
9
+ }
10
+ return GoogleAiProviderFactory.singleton;
11
+ }
12
+ }
13
+ export default GoogleAiProviderFactory;
14
+ export { GoogleAiProviderFactory };
@@ -0,0 +1,14 @@
1
+ import { DraxConfig } from "@drax/common-back";
2
+ import OllamaAiConfig from "../config/OllamaAiConfig.js";
3
+ import OllamaAiProvider from "../providers/OllamaAiProvider.js";
4
+ import AILogServiceFactory from "./services/AILogServiceFactory.js";
5
+ class OllamaAiProviderFactory {
6
+ static instance() {
7
+ if (!OllamaAiProviderFactory.singleton) {
8
+ OllamaAiProviderFactory.singleton = new OllamaAiProvider(DraxConfig.getOrLoad(OllamaAiConfig.OllamaAiBaseUrl, "string", "http://localhost:11434"), DraxConfig.getOrLoad(OllamaAiConfig.OllamaAiModel), DraxConfig.getOrLoad(OllamaAiConfig.OllamaAiVisionModel), DraxConfig.getOrLoad(OllamaAiConfig.OllamaAiEmbeddingModel), AILogServiceFactory.instance);
9
+ }
10
+ return OllamaAiProviderFactory.singleton;
11
+ }
12
+ }
13
+ export default OllamaAiProviderFactory;
14
+ export { OllamaAiProviderFactory };
package/dist/index.js CHANGED
@@ -1,12 +1,18 @@
1
1
  import { OpenAiConfig } from "./config/OpenAiConfig.js";
2
+ import { GoogleAiConfig } from "./config/GoogleAiConfig.js";
3
+ import { OllamaAiConfig } from "./config/OllamaAiConfig.js";
2
4
  import { AILogSchema, AILogBaseSchema } from "./schemas/AILogSchema.js";
3
5
  import AILogModel from "./models/AILogModel.js";
4
6
  import AILogMongoRepository from "./repository/mongo/AILogMongoRepository.js";
5
7
  import AILogSqliteRepository from "./repository/sqlite/AILogSqliteRepository.js";
6
8
  import { OpenAiProviderFactory } from "./factory/OpenAiProviderFactory.js";
9
+ import { GoogleAiProviderFactory } from "./factory/GoogleAiProviderFactory.js";
10
+ import { OllamaAiProviderFactory } from "./factory/OllamaAiProviderFactory.js";
7
11
  import { AiProviderFactory } from "./factory/AiProviderFactory.js";
8
12
  import AILogServiceFactory from "./factory/services/AILogServiceFactory.js";
9
13
  import { OpenAiProvider } from "./providers/OpenAiProvider.js";
14
+ import { GoogleAiProvider } from "./providers/GoogleAiProvider.js";
15
+ import { OllamaAiProvider } from "./providers/OllamaAiProvider.js";
10
16
  import { BuilderTool } from "./tools/BuilderTool.js";
11
17
  import { KnowledgeService } from "./services/KnowledgeService.js";
12
18
  import { AILogService } from "./services/AILogService.js";
@@ -24,7 +30,7 @@ import AIRoutes from "./routes/AIRoutes.js";
24
30
  import DraxAgentRoutes from "./routes/DraxAgentRoutes.js";
25
31
  import AgentSessionRoutes from "./routes/AgentSessionRoutes.js";
26
32
  import { DraxAgent } from "./agents/DraxAgent.js";
27
- export { OpenAiConfig, AILogSchema, AILogBaseSchema, AILogModel, AILogMongoRepository, AILogSqliteRepository, OpenAiProviderFactory, AiProviderFactory, AILogServiceFactory, OpenAiProvider, BuilderTool,
33
+ export { OpenAiConfig, GoogleAiConfig, OllamaAiConfig, AILogSchema, AILogBaseSchema, AILogModel, AILogMongoRepository, AILogSqliteRepository, OpenAiProviderFactory, GoogleAiProviderFactory, OllamaAiProviderFactory, AiProviderFactory, AILogServiceFactory, OpenAiProvider, GoogleAiProvider, OllamaAiProvider, BuilderTool,
28
34
  //Service
29
35
  KnowledgeService, AILogService,
30
36
  //Permissions
@@ -0,0 +1,367 @@
1
+ import { GoogleGenAI } from "@google/genai";
2
+ import { toJSONSchema } from "zod";
3
+ class GoogleAiProvider {
4
+ constructor(apiKey, model, visionModel, aiLogService) {
5
+ if (!apiKey) {
6
+ throw new Error("Google AI apiKey required");
7
+ }
8
+ if (!model) {
9
+ throw new Error("Google AI model required");
10
+ }
11
+ this._apiKey = apiKey;
12
+ this._model = model;
13
+ this._visionModel = visionModel;
14
+ this._aiLogService = aiLogService;
15
+ }
16
+ get model() {
17
+ if (!this._model) {
18
+ throw new Error("Google AI model not found");
19
+ }
20
+ return this._model;
21
+ }
22
+ get client() {
23
+ if (!this._client) {
24
+ this._client = new GoogleGenAI({
25
+ apiKey: this._apiKey,
26
+ });
27
+ }
28
+ return this._client;
29
+ }
30
+ get visionModel() {
31
+ return this._visionModel;
32
+ }
33
+ buildUserContent(input) {
34
+ if (input.userContent && input.userContent.length > 0) {
35
+ return this.mapContentParts(input.userContent);
36
+ }
37
+ if (input.userImages && input.userImages.length > 0) {
38
+ const content = [];
39
+ if (input.userInput) {
40
+ content.push({ text: input.userInput });
41
+ }
42
+ content.push(...input.userImages.map(image => this.mapImageUrl(image.url)));
43
+ return content;
44
+ }
45
+ return input.userInput ? [{ text: input.userInput }] : [{ text: "" }];
46
+ }
47
+ mapContentParts(content) {
48
+ return content.map(part => {
49
+ if (part.type === 'text') {
50
+ return {
51
+ text: part.text
52
+ };
53
+ }
54
+ return this.mapImageUrl(part.imageUrl);
55
+ });
56
+ }
57
+ mapImageUrl(url) {
58
+ const dataUrlMatch = url.match(/^data:([^;,]+);base64,(.+)$/);
59
+ if (dataUrlMatch) {
60
+ return {
61
+ inlineData: {
62
+ mimeType: dataUrlMatch[1],
63
+ data: dataUrlMatch[2],
64
+ }
65
+ };
66
+ }
67
+ return {
68
+ fileData: {
69
+ fileUri: url,
70
+ mimeType: this.inferImageMimeType(url),
71
+ }
72
+ };
73
+ }
74
+ inferImageMimeType(url) {
75
+ const normalizedUrl = url.split("?")[0].toLowerCase();
76
+ if (normalizedUrl.endsWith(".png")) {
77
+ return "image/png";
78
+ }
79
+ if (normalizedUrl.endsWith(".webp")) {
80
+ return "image/webp";
81
+ }
82
+ if (normalizedUrl.endsWith(".gif")) {
83
+ return "image/gif";
84
+ }
85
+ if (normalizedUrl.endsWith(".bmp")) {
86
+ return "image/bmp";
87
+ }
88
+ if (normalizedUrl.endsWith(".heic")) {
89
+ return "image/heic";
90
+ }
91
+ if (normalizedUrl.endsWith(".heif")) {
92
+ return "image/heif";
93
+ }
94
+ return "image/jpeg";
95
+ }
96
+ mapHistory(history = []) {
97
+ return history.map(message => {
98
+ const parts = typeof message.content === 'string'
99
+ ? [{ text: message.content }]
100
+ : this.mapContentParts(message.content);
101
+ if (message.role === "assistant") {
102
+ return {
103
+ role: "model",
104
+ parts,
105
+ };
106
+ }
107
+ if (message.role === "system") {
108
+ return {
109
+ role: "user",
110
+ parts: [
111
+ { text: "[SYSTEM]" },
112
+ ...parts,
113
+ ],
114
+ };
115
+ }
116
+ return {
117
+ role: "user",
118
+ parts,
119
+ };
120
+ });
121
+ }
122
+ hasImageInput(input) {
123
+ if (input.userImages && input.userImages.length > 0) {
124
+ return true;
125
+ }
126
+ if (input.userContent?.some(part => part.type === 'image')) {
127
+ return true;
128
+ }
129
+ return input.history?.some(message => Array.isArray(message.content) && message.content.some(part => part.type === 'image')) ?? false;
130
+ }
131
+ serializePromptInput(input, systemPrompt) {
132
+ return JSON.stringify({
133
+ systemPrompt,
134
+ history: input.history,
135
+ userInput: input.userInput,
136
+ userContent: input.userContent,
137
+ memory: input.memory,
138
+ knowledgeBase: input.knowledgeBase,
139
+ tools: input.tools?.map(tool => ({
140
+ name: tool.name,
141
+ description: tool.description,
142
+ parameters: tool.parameters,
143
+ })),
144
+ });
145
+ }
146
+ serializePromptOutput(output) {
147
+ if (typeof output === "string") {
148
+ return output;
149
+ }
150
+ if (output === null || output === undefined) {
151
+ return undefined;
152
+ }
153
+ return JSON.stringify(output);
154
+ }
155
+ buildLogPayload(input, params) {
156
+ return {
157
+ provider: "googleai",
158
+ model: params.model,
159
+ operationTitle: input.operationTitle,
160
+ operationGroup: input.operationGroup,
161
+ ip: input.ip,
162
+ userAgent: input.userAgent,
163
+ input: this.serializePromptInput(input, params.systemPrompt),
164
+ inputImages: input.userImages?.map(image => ({
165
+ url: image.url,
166
+ })) ?? input.userContent
167
+ ?.filter(part => part.type === "image")
168
+ .map(part => ({
169
+ url: part.imageUrl,
170
+ })),
171
+ inputFiles: input.inputFiles,
172
+ inputTokens: params.inputTokens,
173
+ outputTokens: params.outputTokens,
174
+ tokens: params.tokens,
175
+ startedAt: params.startedAt,
176
+ endedAt: params.endedAt,
177
+ responseTime: params.endedAt ? `${params.endedAt.getTime() - params.startedAt.getTime()}ms` : undefined,
178
+ output: this.serializePromptOutput(params.output),
179
+ success: params.success,
180
+ errorMessage: params.errorMessage,
181
+ tenant: input.tenant,
182
+ user: input.user,
183
+ };
184
+ }
185
+ async registerPromptLog(input, params) {
186
+ if (!this._aiLogService) {
187
+ return;
188
+ }
189
+ try {
190
+ await this._aiLogService.create(this.buildLogPayload(input, params));
191
+ }
192
+ catch (e) {
193
+ console.error("Error registerPromptLog", {
194
+ name: e?.name,
195
+ message: e?.message,
196
+ stack: e?.stack,
197
+ });
198
+ }
199
+ }
200
+ async generateEmbedding({ text, model = "text-embedding-004" }) {
201
+ const response = await this.client.models.embedContent({
202
+ model,
203
+ contents: text,
204
+ });
205
+ return response.embeddings?.[0]?.values ?? [];
206
+ }
207
+ mapTools(tools = []) {
208
+ if (tools.length === 0) {
209
+ return [];
210
+ }
211
+ return [{
212
+ functionDeclarations: tools.map(tool => ({
213
+ name: tool.name,
214
+ description: tool.description,
215
+ parametersJsonSchema: tool.parameters ?? {
216
+ type: "object",
217
+ properties: {},
218
+ additionalProperties: false,
219
+ },
220
+ }))
221
+ }];
222
+ }
223
+ buildResponseConfig(input, systemPrompt) {
224
+ const config = {
225
+ systemInstruction: systemPrompt,
226
+ };
227
+ const responseJsonSchema = this.normalizeResponseJsonSchema(input);
228
+ if (responseJsonSchema) {
229
+ config.responseMimeType = "application/json";
230
+ config.responseJsonSchema = responseJsonSchema;
231
+ }
232
+ if (input.tools && input.tools.length > 0) {
233
+ config.tools = this.mapTools(input.tools);
234
+ }
235
+ return config;
236
+ }
237
+ normalizeResponseJsonSchema(input) {
238
+ if (input.zodSchema) {
239
+ return toJSONSchema(input.zodSchema, {
240
+ target: "draft-7",
241
+ });
242
+ }
243
+ if (!input.jsonSchema) {
244
+ return undefined;
245
+ }
246
+ const jsonSchema = input.jsonSchema;
247
+ if (jsonSchema.type === "json_schema" && jsonSchema.json_schema?.schema) {
248
+ return jsonSchema.json_schema.schema;
249
+ }
250
+ return jsonSchema;
251
+ }
252
+ async buildToolResponseParts(functionCalls = [], tools = []) {
253
+ const parts = [];
254
+ for (const functionCall of functionCalls) {
255
+ const toolName = functionCall.name;
256
+ const tool = tools.find(t => t.name === toolName);
257
+ if (!tool) {
258
+ throw new Error(`Tool not found: ${toolName}`);
259
+ }
260
+ const output = await tool.execute(functionCall.args ?? {});
261
+ parts.push({
262
+ functionResponse: {
263
+ id: functionCall.id,
264
+ name: toolName,
265
+ response: {
266
+ output,
267
+ },
268
+ }
269
+ });
270
+ }
271
+ return parts;
272
+ }
273
+ buildModelFunctionCallContent(functionCalls = []) {
274
+ return {
275
+ role: "model",
276
+ parts: functionCalls.map(functionCall => ({
277
+ functionCall,
278
+ }))
279
+ };
280
+ }
281
+ async prompt(input) {
282
+ if (!input.systemPrompt) {
283
+ throw new Error("systemPrompt required");
284
+ }
285
+ let systemPrompt = input.systemPrompt;
286
+ if (input.memory && input.memory.length > 0) {
287
+ systemPrompt += `\n\n ${input.memoryHeader ?? '[MEMORIA]'}\n ${input.memory.map(m => `${m.key}: ${m.value}`).join('\n')}`;
288
+ }
289
+ if (input.knowledgeBase && input.knowledgeBase.length > 0) {
290
+ systemPrompt += `\n\n${input.knowledgeBaseHeader ?? '[BASE DE CONOCIMIENTO]'}\n ${input.knowledgeBase.join('\n')}`;
291
+ }
292
+ const userInput = this.buildUserContent(input);
293
+ const model = input.model ?? (this.hasImageInput(input) ? this.visionModel ?? this.model : this.model);
294
+ const startedAt = new Date();
295
+ const startTime = performance.now();
296
+ let tokens = 0;
297
+ let inputTokens = 0;
298
+ let outputTokens = 0;
299
+ try {
300
+ const contents = [
301
+ ...this.mapHistory(input.history),
302
+ { role: 'user', parts: userInput },
303
+ ];
304
+ const tools = input.tools ?? [];
305
+ const maxIterations = input.toolMaxIterations ?? 5;
306
+ let output;
307
+ for (let iteration = 0; iteration < maxIterations; iteration++) {
308
+ const response = await this.client.models.generateContent({
309
+ model,
310
+ contents,
311
+ config: this.buildResponseConfig(input, systemPrompt),
312
+ });
313
+ tokens += response.usageMetadata?.totalTokenCount ?? 0;
314
+ inputTokens += response.usageMetadata?.promptTokenCount ?? 0;
315
+ outputTokens += response.usageMetadata?.candidatesTokenCount ?? 0;
316
+ const functionCalls = response.functionCalls ?? [];
317
+ if (functionCalls.length === 0) {
318
+ output = response.text;
319
+ break;
320
+ }
321
+ contents.push(response.candidates?.[0]?.content ?? this.buildModelFunctionCallContent(functionCalls));
322
+ contents.push({
323
+ role: "user",
324
+ parts: await this.buildToolResponseParts(functionCalls, tools),
325
+ });
326
+ }
327
+ if (output === undefined) {
328
+ throw new Error(`Tool max iterations reached: ${maxIterations}`);
329
+ }
330
+ const endTime = performance.now();
331
+ const time = endTime - startTime;
332
+ const endedAt = new Date();
333
+ await this.registerPromptLog(input, {
334
+ model,
335
+ systemPrompt,
336
+ startedAt,
337
+ endedAt,
338
+ inputTokens,
339
+ outputTokens,
340
+ tokens,
341
+ output,
342
+ success: true,
343
+ });
344
+ return {
345
+ output,
346
+ tokens,
347
+ inputTokens,
348
+ outputTokens,
349
+ time
350
+ };
351
+ }
352
+ catch (e) {
353
+ const endedAt = new Date();
354
+ await this.registerPromptLog(input, {
355
+ model,
356
+ systemPrompt,
357
+ startedAt,
358
+ endedAt,
359
+ success: false,
360
+ errorMessage: e?.message,
361
+ });
362
+ throw e;
363
+ }
364
+ }
365
+ }
366
+ export default GoogleAiProvider;
367
+ export { GoogleAiProvider };