@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.
- package/dist/config/GoogleAiConfig.js +8 -0
- package/dist/config/OllamaAiConfig.js +9 -0
- package/dist/factory/AiProviderFactory.js +12 -3
- package/dist/factory/GoogleAiProviderFactory.js +14 -0
- package/dist/factory/OllamaAiProviderFactory.js +14 -0
- package/dist/index.js +7 -1
- package/dist/providers/GoogleAiProvider.js +367 -0
- package/dist/providers/OllamaAiProvider.js +342 -0
- package/dist/tools/BuilderTool.js +9 -0
- package/package.json +4 -2
- package/src/config/GoogleAiConfig.ts +13 -0
- package/src/config/OllamaAiConfig.ts +14 -0
- package/src/factory/AiProviderFactory.ts +12 -5
- package/src/factory/GoogleAiProviderFactory.ts +26 -0
- package/src/factory/OllamaAiProviderFactory.ts +27 -0
- package/src/index.ts +12 -0
- package/src/providers/GoogleAiProvider.ts +489 -0
- package/src/providers/OllamaAiProvider.ts +469 -0
- package/src/tools/BuilderTool.ts +12 -0
- package/test/GoogleAiProvider.test.ts +211 -0
- package/test/ToolBuilder.test.ts +48 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/types/config/GoogleAiConfig.d.ts +8 -0
- package/types/config/GoogleAiConfig.d.ts.map +1 -0
- package/types/config/OllamaAiConfig.d.ts +9 -0
- package/types/config/OllamaAiConfig.d.ts.map +1 -0
- package/types/factory/AiProviderFactory.d.ts.map +1 -1
- package/types/factory/GoogleAiProviderFactory.d.ts +8 -0
- package/types/factory/GoogleAiProviderFactory.d.ts.map +1 -0
- package/types/factory/OllamaAiProviderFactory.d.ts +8 -0
- package/types/factory/OllamaAiProviderFactory.d.ts.map +1 -0
- package/types/index.d.ts.map +1 -1
- package/types/providers/GoogleAiProvider.d.ts +63 -0
- package/types/providers/GoogleAiProvider.d.ts.map +1 -0
- package/types/providers/OllamaAiProvider.d.ts +78 -0
- package/types/providers/OllamaAiProvider.d.ts.map +1 -0
- package/types/tools/BuilderTool.d.ts.map +1 -1
- package/.env +0 -4
- package/dist/agents/ChatbotTaskService.js +0 -143
- package/dist/agents/ChatbotTaskTools.js +0 -756
- package/dist/controllers/AIController.js +0 -150
- package/dist/interfaces/IAILog.js +0 -1
- package/dist/routes/ChatbotTaskRoutes.js +0 -8
- package/dist/tools/ToolBuilder.js +0 -243
- package/dist/vectors/ChromaVector.js +0 -65
- package/types/agents/ChatbotTaskService.d.ts +0 -42
- package/types/agents/ChatbotTaskService.d.ts.map +0 -1
- package/types/agents/ChatbotTaskTools.d.ts +0 -54
- package/types/agents/ChatbotTaskTools.d.ts.map +0 -1
- package/types/controllers/AIController.d.ts +0 -25
- package/types/controllers/AIController.d.ts.map +0 -1
- package/types/interfaces/IAILog.d.ts +0 -77
- package/types/interfaces/IAILog.d.ts.map +0 -1
- package/types/routes/ChatbotTaskRoutes.d.ts +0 -4
- package/types/routes/ChatbotTaskRoutes.d.ts.map +0 -1
- package/types/tools/ToolBuilder.d.ts +0 -47
- package/types/tools/ToolBuilder.d.ts.map +0 -1
- package/types/vectors/ChromaVector.d.ts +0 -21
- package/types/vectors/ChromaVector.d.ts.map +0 -1
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
import { CommonController } from "@drax/common-back";
|
|
3
|
-
import AiProviderFactory from "../factory/AiProviderFactory.js";
|
|
4
|
-
import AIPermissions from "../permissions/AIPermissions.js";
|
|
5
|
-
const CrudAiFieldSchema = z.lazy(() => z.object({
|
|
6
|
-
name: z.string(),
|
|
7
|
-
type: z.string(),
|
|
8
|
-
label: z.string(),
|
|
9
|
-
hint: z.string().nullable().default(null),
|
|
10
|
-
placeholder: z.string().nullable().default(null),
|
|
11
|
-
readonly: z.boolean().nullable().default(null),
|
|
12
|
-
default: z.any().nullable().default(null),
|
|
13
|
-
enum: z.array(z.string()).nullable().default(null),
|
|
14
|
-
items: z.array(z.object({
|
|
15
|
-
title: z.string().nullable().default(null),
|
|
16
|
-
value: z.any().nullable().default(null),
|
|
17
|
-
})).nullable().default(null),
|
|
18
|
-
ref: z.string().nullable().default(null),
|
|
19
|
-
refDisplay: z.string().nullable().default(null),
|
|
20
|
-
objectFields: z.array(CrudAiFieldSchema).nullable().default(null),
|
|
21
|
-
}));
|
|
22
|
-
const PromptRequestSchema = z.object({
|
|
23
|
-
prompt: z.string().min(1),
|
|
24
|
-
operation: z.enum(["create", "edit"]).default("create"),
|
|
25
|
-
entity: z.object({
|
|
26
|
-
name: z.string(),
|
|
27
|
-
identifier: z.string().optional().nullable(),
|
|
28
|
-
}),
|
|
29
|
-
currentValues: z.record(z.string(), z.any()).default({}),
|
|
30
|
-
fields: z.array(CrudAiFieldSchema).min(1),
|
|
31
|
-
});
|
|
32
|
-
class AIController extends CommonController {
|
|
33
|
-
buildFieldValueSchema(field) {
|
|
34
|
-
switch (field.type) {
|
|
35
|
-
case "number":
|
|
36
|
-
return z.number().nullable().default(null);
|
|
37
|
-
case "boolean":
|
|
38
|
-
return z.boolean().nullable().default(null);
|
|
39
|
-
case "array.string":
|
|
40
|
-
case "array.enum":
|
|
41
|
-
case "array.ref":
|
|
42
|
-
return z.array(z.string()).nullable().default(null);
|
|
43
|
-
case "array.number":
|
|
44
|
-
return z.array(z.number()).nullable().default(null);
|
|
45
|
-
case "object":
|
|
46
|
-
if (field.objectFields && Array.isArray(field.objectFields) && field.objectFields.length > 0) {
|
|
47
|
-
return z.object(this.buildFieldShape(field.objectFields)).nullable().default(null);
|
|
48
|
-
}
|
|
49
|
-
return z.string().nullable().default(null);
|
|
50
|
-
case "record":
|
|
51
|
-
case "array.object":
|
|
52
|
-
case "array.record":
|
|
53
|
-
case "array.fullFile":
|
|
54
|
-
case "file":
|
|
55
|
-
case "fullFile":
|
|
56
|
-
return z.string().nullable().default(null);
|
|
57
|
-
case "id":
|
|
58
|
-
case "string":
|
|
59
|
-
case "longString":
|
|
60
|
-
case "date":
|
|
61
|
-
case "ref":
|
|
62
|
-
case "enum":
|
|
63
|
-
case "select":
|
|
64
|
-
case "password":
|
|
65
|
-
default:
|
|
66
|
-
return z.string().nullable().default(null);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
buildFieldShape(fields) {
|
|
70
|
-
return fields.reduce((acc, field) => {
|
|
71
|
-
acc[field.name] = this.buildFieldValueSchema(field);
|
|
72
|
-
return acc;
|
|
73
|
-
}, {});
|
|
74
|
-
}
|
|
75
|
-
buildSystemPrompt(input) {
|
|
76
|
-
return [
|
|
77
|
-
"Sos un asistente de formularios para un sistema CRUD.",
|
|
78
|
-
"Tu tarea es proponer valores JSON para completar o editar una entidad.",
|
|
79
|
-
"Debes respetar exactamente los nombres de campo entregados.",
|
|
80
|
-
"No inventes campos adicionales.",
|
|
81
|
-
"Si un campo no tiene una propuesta razonable, omitilo.",
|
|
82
|
-
"Usa tipos compatibles con JSON.",
|
|
83
|
-
"Para campos enum o select, elegí solamente valores válidos de la lista provista.",
|
|
84
|
-
"Para campos record, object sin estructura fija, array.object o array.record, devolve un string JSON serializado valido.",
|
|
85
|
-
"La respuesta debe describir sugerencias concretas, breves y aplicables.",
|
|
86
|
-
`Operacion actual: ${input.operation}.`,
|
|
87
|
-
`Entidad actual: ${input.entity.name}.`,
|
|
88
|
-
].join("\n");
|
|
89
|
-
}
|
|
90
|
-
buildUserInput(input) {
|
|
91
|
-
return JSON.stringify({
|
|
92
|
-
task: input.prompt,
|
|
93
|
-
operation: input.operation,
|
|
94
|
-
entity: input.entity,
|
|
95
|
-
currentValues: input.currentValues,
|
|
96
|
-
fields: input.fields,
|
|
97
|
-
expectedResponse: {
|
|
98
|
-
message: "string",
|
|
99
|
-
suggestions: "object with proposed values indexed by field name",
|
|
100
|
-
warnings: ["optional string warnings"],
|
|
101
|
-
}
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
async prompt(req, rep) {
|
|
105
|
-
try {
|
|
106
|
-
req.rbac.assertPermission(AIPermissions.PromptCrud);
|
|
107
|
-
const input = PromptRequestSchema.parse(req.body ?? {});
|
|
108
|
-
const aiProvider = AiProviderFactory.instance();
|
|
109
|
-
const responseSchema = z.object({
|
|
110
|
-
message: z.string().nullable().default(null),
|
|
111
|
-
suggestions: z.object(this.buildFieldShape(input.fields)).strict().default({}),
|
|
112
|
-
warnings: z.array(z.string()).default([]),
|
|
113
|
-
});
|
|
114
|
-
const response = await aiProvider.prompt({
|
|
115
|
-
systemPrompt: this.buildSystemPrompt(input),
|
|
116
|
-
userInput: this.buildUserInput(input),
|
|
117
|
-
zodSchema: responseSchema,
|
|
118
|
-
operationTitle: `crud-${input.operation}-assistant`,
|
|
119
|
-
operationGroup: "crud-form-assistant",
|
|
120
|
-
ip: req.ip,
|
|
121
|
-
userAgent: req.headers["user-agent"],
|
|
122
|
-
tenant: req.rbac?.tenantId ?? null,
|
|
123
|
-
user: req.rbac?.userId ?? null,
|
|
124
|
-
});
|
|
125
|
-
const parsedOutput = responseSchema.parse(typeof response.output === "string"
|
|
126
|
-
? JSON.parse(response.output)
|
|
127
|
-
: response.output);
|
|
128
|
-
return rep.send({
|
|
129
|
-
message: parsedOutput.message,
|
|
130
|
-
suggestions: parsedOutput.suggestions,
|
|
131
|
-
warnings: parsedOutput.warnings,
|
|
132
|
-
meta: {
|
|
133
|
-
tokens: response.tokens,
|
|
134
|
-
inputTokens: response.inputTokens,
|
|
135
|
-
outputTokens: response.outputTokens,
|
|
136
|
-
time: response.time,
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
catch (e) {
|
|
141
|
-
console.error("AIController.prompt error", e);
|
|
142
|
-
const statusCode = e?.name === "ZodError" ? 400 : 500;
|
|
143
|
-
return rep.status(statusCode).send({
|
|
144
|
-
message: e?.message || "AI prompt error",
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
export default AIController;
|
|
150
|
-
export { AIController };
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import ChatbotTaskController from "../controllers/ChatbotTaskController.js";
|
|
2
|
-
async function ChatbotTaskFastifyRoutes(fastify, options) {
|
|
3
|
-
const controller = new ChatbotTaskController();
|
|
4
|
-
fastify.post("/api/chatbot/session", (req, rep) => controller.startSession(req, rep));
|
|
5
|
-
fastify.post("/api/chatbot/message", (req, rep) => controller.message(req, rep));
|
|
6
|
-
}
|
|
7
|
-
export default ChatbotTaskFastifyRoutes;
|
|
8
|
-
export { ChatbotTaskFastifyRoutes };
|
|
@@ -1,243 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
const emptyParameters = {
|
|
3
|
-
type: "object",
|
|
4
|
-
properties: {},
|
|
5
|
-
additionalProperties: false,
|
|
6
|
-
};
|
|
7
|
-
const idSchema = z.object({
|
|
8
|
-
id: z.string().describe("Entity identifier"),
|
|
9
|
-
});
|
|
10
|
-
const filtersSchema = z.array(z.object({
|
|
11
|
-
field: z.string(),
|
|
12
|
-
operator: z.string(),
|
|
13
|
-
value: z.any(),
|
|
14
|
-
orGroup: z.string().optional(),
|
|
15
|
-
})).optional().describe("Optional Drax field filters");
|
|
16
|
-
const toolDefinitions = {
|
|
17
|
-
create: {
|
|
18
|
-
description: entityName => `Crear un registro de ${entityName}`,
|
|
19
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
20
|
-
data: builder.inputSchema.describe("Data for the entity to create"),
|
|
21
|
-
})),
|
|
22
|
-
execute: (service, args) => service.create?.(args.data),
|
|
23
|
-
},
|
|
24
|
-
update: {
|
|
25
|
-
description: entityName => `Reemplazar un registro de ${entityName} por id`,
|
|
26
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
27
|
-
id: z.string().describe("Entity identifier"),
|
|
28
|
-
data: builder.inputSchema.describe("Complete replacement data"),
|
|
29
|
-
})),
|
|
30
|
-
execute: (service, args) => service.update?.(args.id, args.data),
|
|
31
|
-
},
|
|
32
|
-
updatePartial: {
|
|
33
|
-
description: entityName => `Actualizar parcialmente un registro de ${entityName} por id`,
|
|
34
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
35
|
-
id: z.string().describe("Entity identifier"),
|
|
36
|
-
data: builder.partialInputSchema.describe("Partial data to update"),
|
|
37
|
-
})),
|
|
38
|
-
execute: (service, args) => service.updatePartial?.(args.id, args.data),
|
|
39
|
-
},
|
|
40
|
-
delete: {
|
|
41
|
-
description: entityName => `Eliminar un registro de ${entityName} por id`,
|
|
42
|
-
parameters: builder => builder.objectParameters(idSchema),
|
|
43
|
-
execute: (service, args) => service.delete?.(args.id),
|
|
44
|
-
},
|
|
45
|
-
findById: {
|
|
46
|
-
description: entityName => `Buscar un registro de ${entityName} por id`,
|
|
47
|
-
parameters: builder => builder.objectParameters(idSchema),
|
|
48
|
-
execute: (service, args) => service.findById?.(args.id),
|
|
49
|
-
},
|
|
50
|
-
findByIds: {
|
|
51
|
-
description: entityName => `Buscar multiples registros de ${entityName} por ids`,
|
|
52
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
53
|
-
ids: z.array(z.string()).describe("Entity identifiers"),
|
|
54
|
-
})),
|
|
55
|
-
execute: (service, args) => service.findByIds?.(args.ids),
|
|
56
|
-
},
|
|
57
|
-
findOneBy: {
|
|
58
|
-
description: entityName => `Buscar un registro de ${entityName} por valor de campo`,
|
|
59
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
60
|
-
field: z.string(),
|
|
61
|
-
value: z.any(),
|
|
62
|
-
filters: filtersSchema,
|
|
63
|
-
})),
|
|
64
|
-
execute: (service, args) => service.findOneBy?.(args.field, args.value, args.filters ?? []),
|
|
65
|
-
},
|
|
66
|
-
findOne: {
|
|
67
|
-
description: entityName => `Buscar el primer registro de ${entityName} que coincida con busqueda y filtros`,
|
|
68
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
69
|
-
search: z.string().optional(),
|
|
70
|
-
filters: filtersSchema,
|
|
71
|
-
})),
|
|
72
|
-
execute: (service, args) => service.findOne?.({
|
|
73
|
-
search: args.search ?? "",
|
|
74
|
-
filters: args.filters ?? [],
|
|
75
|
-
}),
|
|
76
|
-
},
|
|
77
|
-
findBy: {
|
|
78
|
-
description: entityName => `Buscar registros de ${entityName} por valor de campo`,
|
|
79
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
80
|
-
field: z.string(),
|
|
81
|
-
value: z.any(),
|
|
82
|
-
limit: z.number().optional(),
|
|
83
|
-
filters: filtersSchema,
|
|
84
|
-
})),
|
|
85
|
-
execute: (service, args) => service.findBy?.(args.field, args.value, args.limit ?? 1000, args.filters ?? []),
|
|
86
|
-
},
|
|
87
|
-
fetchAll: {
|
|
88
|
-
description: entityName => `Obtener todos los registros de ${entityName}`,
|
|
89
|
-
parameters: () => emptyParameters,
|
|
90
|
-
execute: service => service.fetchAll?.(),
|
|
91
|
-
},
|
|
92
|
-
search: {
|
|
93
|
-
description: entityName => `Buscar registros de ${entityName} por texto`,
|
|
94
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
95
|
-
value: z.string().describe("Search text"),
|
|
96
|
-
limit: z.number().optional(),
|
|
97
|
-
filters: filtersSchema,
|
|
98
|
-
})),
|
|
99
|
-
execute: (service, args) => service.search?.(args.value, args.limit ?? 1000, args.filters ?? []),
|
|
100
|
-
},
|
|
101
|
-
find: {
|
|
102
|
-
description: entityName => `Buscar registros de ${entityName} usando opciones de listado`,
|
|
103
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
104
|
-
orderBy: z.string().optional(),
|
|
105
|
-
order: z.union([z.enum(["asc", "desc"]), z.boolean()]).optional(),
|
|
106
|
-
search: z.string().optional(),
|
|
107
|
-
filters: filtersSchema,
|
|
108
|
-
limit: z.number().optional(),
|
|
109
|
-
})),
|
|
110
|
-
execute: (service, args) => service.find?.({
|
|
111
|
-
orderBy: args.orderBy ?? "",
|
|
112
|
-
order: args.order ?? false,
|
|
113
|
-
search: args.search ?? "",
|
|
114
|
-
filters: args.filters ?? [],
|
|
115
|
-
limit: args.limit ?? 0,
|
|
116
|
-
}),
|
|
117
|
-
},
|
|
118
|
-
paginate: {
|
|
119
|
-
description: entityName => `Paginar registros de ${entityName}`,
|
|
120
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
121
|
-
page: z.number().optional(),
|
|
122
|
-
limit: z.number().optional(),
|
|
123
|
-
orderBy: z.string().optional(),
|
|
124
|
-
order: z.enum(["asc", "desc"]).optional(),
|
|
125
|
-
search: z.string().optional(),
|
|
126
|
-
filters: filtersSchema,
|
|
127
|
-
})),
|
|
128
|
-
execute: (service, args) => service.paginate({
|
|
129
|
-
page: args.page ?? 1,
|
|
130
|
-
limit: args.limit ?? 10,
|
|
131
|
-
orderBy: args.orderBy,
|
|
132
|
-
order: args.order ?? "asc",
|
|
133
|
-
search: args.search ?? "",
|
|
134
|
-
filters: args.filters ?? [],
|
|
135
|
-
}),
|
|
136
|
-
},
|
|
137
|
-
groupBy: {
|
|
138
|
-
description: entityName => `Agrupar registros de ${entityName} por campos`,
|
|
139
|
-
parameters: builder => builder.objectParameters(z.object({
|
|
140
|
-
fields: z.array(z.string()).optional(),
|
|
141
|
-
filters: filtersSchema,
|
|
142
|
-
dateFormat: z.enum(["year", "month", "day", "hour", "minute", "second"]).optional(),
|
|
143
|
-
})),
|
|
144
|
-
execute: (service, args) => service.groupBy?.({
|
|
145
|
-
fields: args.fields ?? [],
|
|
146
|
-
filters: args.filters ?? [],
|
|
147
|
-
dateFormat: args.dateFormat ?? "day",
|
|
148
|
-
}),
|
|
149
|
-
},
|
|
150
|
-
};
|
|
151
|
-
class ToolBuilder {
|
|
152
|
-
constructor(options) {
|
|
153
|
-
this.options = options;
|
|
154
|
-
this._inputSchema = this.schemaAdapter(options.schema);
|
|
155
|
-
this._partialInputSchema = this._inputSchema.partial();
|
|
156
|
-
this._outputSchema = options.outputSchema ? this.schemaAdapter(options.outputSchema) : undefined;
|
|
157
|
-
}
|
|
158
|
-
get inputSchema() {
|
|
159
|
-
return this._inputSchema;
|
|
160
|
-
}
|
|
161
|
-
get partialInputSchema() {
|
|
162
|
-
return this._partialInputSchema;
|
|
163
|
-
}
|
|
164
|
-
get outputSchema() {
|
|
165
|
-
return this._outputSchema;
|
|
166
|
-
}
|
|
167
|
-
getTools() {
|
|
168
|
-
return this.options.methods.map(method => this.buildTool(method));
|
|
169
|
-
}
|
|
170
|
-
getSystemPromptSection() {
|
|
171
|
-
const entityDescription = this.options.entityDescription
|
|
172
|
-
? `\n${this.options.entityDescription}`
|
|
173
|
-
: "";
|
|
174
|
-
const tools = this.options.methods
|
|
175
|
-
.map(method => {
|
|
176
|
-
const definition = toolDefinitions[method];
|
|
177
|
-
return `- ${this.getToolName(method)}: ${definition.description(this.options.entityName)}`;
|
|
178
|
-
})
|
|
179
|
-
.join("\n");
|
|
180
|
-
const entitySchema = JSON.stringify(this.toJsonSchema(this._outputSchema ?? this._inputSchema));
|
|
181
|
-
return [
|
|
182
|
-
`[ENTIDAD: ${this.options.entityName}]${entityDescription}`,
|
|
183
|
-
`Schema JSON de la entidad: ${entitySchema}`,
|
|
184
|
-
"Tools disponibles:",
|
|
185
|
-
tools,
|
|
186
|
-
].join("\n");
|
|
187
|
-
}
|
|
188
|
-
objectParameters(schema) {
|
|
189
|
-
return this.toJsonSchema(this.schemaAdapter(schema));
|
|
190
|
-
}
|
|
191
|
-
buildTool(method) {
|
|
192
|
-
const definition = toolDefinitions[method];
|
|
193
|
-
const serviceMethod = this.options.service[method];
|
|
194
|
-
if (typeof serviceMethod !== "function") {
|
|
195
|
-
throw new Error(`Tool method not available on service: ${method}`);
|
|
196
|
-
}
|
|
197
|
-
return {
|
|
198
|
-
name: this.getToolName(method),
|
|
199
|
-
description: definition.description(this.options.entityName),
|
|
200
|
-
parameters: definition.parameters(this),
|
|
201
|
-
execute: async (args) => definition.execute(this.options.service, args),
|
|
202
|
-
};
|
|
203
|
-
}
|
|
204
|
-
getToolName(method) {
|
|
205
|
-
return `${this.options.toolNamePrefix ?? this.options.entityName}_${method}`;
|
|
206
|
-
}
|
|
207
|
-
toJsonSchema(schema) {
|
|
208
|
-
return z.toJSONSchema(schema, { target: "openAi" });
|
|
209
|
-
}
|
|
210
|
-
getTypeName(field) {
|
|
211
|
-
return field?.constructor?.name;
|
|
212
|
-
}
|
|
213
|
-
fieldAdapter(field) {
|
|
214
|
-
const f = field;
|
|
215
|
-
const typeName = this.getTypeName(f);
|
|
216
|
-
if (typeof f?.unwrap === "function" && typeName === "ZodOptional") {
|
|
217
|
-
return this.fieldAdapter(f.unwrap()).optional();
|
|
218
|
-
}
|
|
219
|
-
if (typeof f?.unwrap === "function" && typeName === "ZodNullable") {
|
|
220
|
-
return this.fieldAdapter(f.unwrap()).nullable();
|
|
221
|
-
}
|
|
222
|
-
if (typeName === "ZodArray" && f?.element) {
|
|
223
|
-
return z.array(this.fieldAdapter(f.element));
|
|
224
|
-
}
|
|
225
|
-
if (typeName === "ZodObject" && f?.shape) {
|
|
226
|
-
return this.schemaAdapter(f);
|
|
227
|
-
}
|
|
228
|
-
if (typeName === "ZodDate") {
|
|
229
|
-
return z.iso.datetime();
|
|
230
|
-
}
|
|
231
|
-
return f;
|
|
232
|
-
}
|
|
233
|
-
schemaAdapter(schema) {
|
|
234
|
-
const shape = schema.shape;
|
|
235
|
-
const newShape = {};
|
|
236
|
-
for (const key of Object.keys(shape)) {
|
|
237
|
-
newShape[key] = this.fieldAdapter(shape[key]);
|
|
238
|
-
}
|
|
239
|
-
return z.object(newShape);
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
export default ToolBuilder;
|
|
243
|
-
export { ToolBuilder };
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { ChromaClient } from 'chromadb';
|
|
2
|
-
class ChromaVector {
|
|
3
|
-
constructor(path = 'http://localhost:8000', database) {
|
|
4
|
-
this.client = new ChromaClient({ path, database });
|
|
5
|
-
this.collections = new Map();
|
|
6
|
-
}
|
|
7
|
-
async initializeCollection(collectionName) {
|
|
8
|
-
if (!this.collections.has(collectionName)) {
|
|
9
|
-
const collection = await this.client.createCollection({
|
|
10
|
-
name: collectionName,
|
|
11
|
-
// embeddingFunction: embeddingFunction
|
|
12
|
-
});
|
|
13
|
-
this.collections.set(collectionName, collection);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
async addDocument(collectionName, document) {
|
|
17
|
-
await this.initializeCollection(collectionName);
|
|
18
|
-
const collection = this.collections.get(collectionName);
|
|
19
|
-
if (collection) {
|
|
20
|
-
await collection.add({
|
|
21
|
-
ids: [document.id],
|
|
22
|
-
documents: [document.content],
|
|
23
|
-
metadatas: [document.metadata || {}],
|
|
24
|
-
embeddings: document.embedding ? [document.embedding] : undefined, // Add this line
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
throw new Error(`Collection ${collectionName} not found`);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
async queryCollection(collectionName, query, topK = 5) {
|
|
32
|
-
await this.initializeCollection(collectionName);
|
|
33
|
-
const collection = this.collections.get(collectionName);
|
|
34
|
-
if (collection) {
|
|
35
|
-
const results = await collection.query({
|
|
36
|
-
queryTexts: [query],
|
|
37
|
-
nResults: topK,
|
|
38
|
-
});
|
|
39
|
-
return results.documents[0] || [];
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
throw new Error(`Collection ${collectionName} not found`);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
async deleteDocument(collectionName, documentId) {
|
|
46
|
-
const collection = this.collections.get(collectionName);
|
|
47
|
-
if (collection) {
|
|
48
|
-
await collection.delete({
|
|
49
|
-
ids: [documentId],
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
throw new Error(`Collection ${collectionName} not found`);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
async listCollections() {
|
|
57
|
-
return await this.client.listCollections();
|
|
58
|
-
}
|
|
59
|
-
async deleteCollection(collectionName) {
|
|
60
|
-
await this.client.deleteCollection({ name: collectionName });
|
|
61
|
-
this.collections.delete(collectionName);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
export default ChromaVector;
|
|
65
|
-
export { ChromaVector };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
type ChatRole = "user" | "assistant" | "system";
|
|
2
|
-
interface ChatMessage {
|
|
3
|
-
role: ChatRole;
|
|
4
|
-
content: string;
|
|
5
|
-
}
|
|
6
|
-
interface ChatbotTaskMessageInput {
|
|
7
|
-
sessionId?: string;
|
|
8
|
-
message: string;
|
|
9
|
-
userId: string;
|
|
10
|
-
tenantId?: string | null;
|
|
11
|
-
ip?: string;
|
|
12
|
-
userAgent?: string;
|
|
13
|
-
}
|
|
14
|
-
interface ChatbotTaskMessageOutput {
|
|
15
|
-
sessionId: string;
|
|
16
|
-
message: string;
|
|
17
|
-
}
|
|
18
|
-
interface ChatbotTaskSession {
|
|
19
|
-
id: string;
|
|
20
|
-
userId: string;
|
|
21
|
-
messages: ChatMessage[];
|
|
22
|
-
createdAt: Date;
|
|
23
|
-
updatedAt: Date;
|
|
24
|
-
}
|
|
25
|
-
declare class ChatbotTaskService {
|
|
26
|
-
private sessions;
|
|
27
|
-
startSession(userId: string): ChatbotTaskSession;
|
|
28
|
-
private createSession;
|
|
29
|
-
sendMessage(input: ChatbotTaskMessageInput): Promise<ChatbotTaskMessageOutput>;
|
|
30
|
-
private resolveSession;
|
|
31
|
-
private getSessionKey;
|
|
32
|
-
private normalizeOutput;
|
|
33
|
-
private withToolExecutionLogs;
|
|
34
|
-
private systemPrompt;
|
|
35
|
-
private formatTaskOptionNames;
|
|
36
|
-
private formatEntityTypeOptionNames;
|
|
37
|
-
private formatOptionList;
|
|
38
|
-
}
|
|
39
|
-
export type { ChatbotTaskMessageInput, ChatbotTaskMessageOutput, ChatbotTaskSession };
|
|
40
|
-
export default ChatbotTaskService;
|
|
41
|
-
export { ChatbotTaskService };
|
|
42
|
-
//# sourceMappingURL=ChatbotTaskService.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|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;AACpF,eAAe,kBAAkB,CAAC;AAClC,OAAO,EAAC,kBAAkB,EAAC,CAAC"}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { IPromptTool } from "@drax/ai-back/types/interfaces/IAIProvider.js";
|
|
2
|
-
interface ChatbotTaskToolsContext {
|
|
3
|
-
userId: string;
|
|
4
|
-
}
|
|
5
|
-
interface TaskOptionNames {
|
|
6
|
-
sources: string[];
|
|
7
|
-
statuses: string[];
|
|
8
|
-
types: string[];
|
|
9
|
-
priorities: string[];
|
|
10
|
-
}
|
|
11
|
-
interface EntityTypeOptionNames {
|
|
12
|
-
contactTypes: string[];
|
|
13
|
-
companyTypes: string[];
|
|
14
|
-
clientTypes: string[];
|
|
15
|
-
}
|
|
16
|
-
interface LifeOpsOptionNames {
|
|
17
|
-
task: TaskOptionNames;
|
|
18
|
-
entityTypes: EntityTypeOptionNames;
|
|
19
|
-
}
|
|
20
|
-
declare class ChatbotTaskTools {
|
|
21
|
-
static build(context: ChatbotTaskToolsContext): IPromptTool[];
|
|
22
|
-
static fetchTaskOptionNames(): Promise<TaskOptionNames>;
|
|
23
|
-
static fetchLifeOpsOptionNames(): Promise<LifeOpsOptionNames>;
|
|
24
|
-
private static taskUserFilter;
|
|
25
|
-
private static compactObject;
|
|
26
|
-
private static serializeOptionNames;
|
|
27
|
-
private static serializeTask;
|
|
28
|
-
private static entityUserFilter;
|
|
29
|
-
private static serializeGoal;
|
|
30
|
-
private static serializeProject;
|
|
31
|
-
private static serializeContact;
|
|
32
|
-
private static serializeClient;
|
|
33
|
-
private static serializeCompany;
|
|
34
|
-
private static serializeFields;
|
|
35
|
-
private static findUserTaskById;
|
|
36
|
-
private static findUserEntityById;
|
|
37
|
-
private static buildEntityTools;
|
|
38
|
-
private static entityConfigs;
|
|
39
|
-
private static createEntityTool;
|
|
40
|
-
private static searchEntitiesTool;
|
|
41
|
-
private static findEntityByIdTool;
|
|
42
|
-
private static updateEntityPartialTool;
|
|
43
|
-
private static registerTaskTool;
|
|
44
|
-
private static searchTasksTool;
|
|
45
|
-
private static findTaskByIdTool;
|
|
46
|
-
private static groupTasksTool;
|
|
47
|
-
private static updateTaskPartialTool;
|
|
48
|
-
private static listTaskOptionsTool;
|
|
49
|
-
private static createTaskOptionTool;
|
|
50
|
-
}
|
|
51
|
-
export type { ChatbotTaskToolsContext, TaskOptionNames, EntityTypeOptionNames, LifeOpsOptionNames };
|
|
52
|
-
export default ChatbotTaskTools;
|
|
53
|
-
export { ChatbotTaskTools };
|
|
54
|
-
//# sourceMappingURL=ChatbotTaskTools.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"ChatbotTaskTools.d.ts","sourceRoot":"","sources":["../../src/agents/ChatbotTaskTools.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,+CAA+C,CAAC;AAe/E,UAAU,uBAAuB;IAC7B,MAAM,EAAE,MAAM,CAAC;CAClB;AA4BD,UAAU,eAAe;IACrB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,QAAQ,EAAE,MAAM,EAAE,CAAC;IACnB,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,UAAU,EAAE,MAAM,EAAE,CAAC;CACxB;AAED,UAAU,qBAAqB;IAC3B,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,WAAW,EAAE,MAAM,EAAE,CAAC;CACzB;AAED,UAAU,kBAAkB;IACxB,IAAI,EAAE,eAAe,CAAC;IACtB,WAAW,EAAE,qBAAqB,CAAC;CACtC;AAYD,cAAM,gBAAgB;IAClB,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,uBAAuB,GAAG,WAAW,EAAE;WAgBhD,oBAAoB,IAAI,OAAO,CAAC,eAAe,CAAC;WAgBhD,uBAAuB,IAAI,OAAO,CAAC,kBAAkB,CAAC;IAkBnE,OAAO,CAAC,MAAM,CAAC,cAAc;IAI7B,OAAO,CAA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
|
5
|
-
operation: z.ZodDefault<z.ZodEnum<{
|
|
6
|
-
create: "create";
|
|
7
|
-
edit: "edit";
|
|
8
|
-
}>>;
|
|
9
|
-
entity: z.ZodObject<{
|
|
10
|
-
name: z.ZodString;
|
|
11
|
-
identifier: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
12
|
-
}, z.core.$strip>;
|
|
13
|
-
currentValues: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodAny>>;
|
|
14
|
-
fields: z.ZodArray<z.ZodType<any, unknown, z.core.$ZodTypeInternals<any, unknown>>>;
|
|
15
|
-
}, z.core.$strip>;
|
|
16
|
-
declare class AIController extends CommonController {
|
|
17
|
-
protected buildFieldValueSchema(field: any): z.ZodTypeAny;
|
|
18
|
-
protected buildFieldShape(fields: any[]): Record<string, z.ZodTypeAny>;
|
|
19
|
-
protected buildSystemPrompt(input: z.infer<typeof PromptRequestSchema>): string;
|
|
20
|
-
protected buildUserInput(input: z.infer<typeof PromptRequestSchema>): string;
|
|
21
|
-
prompt(req: any, rep: any): Promise<any>;
|
|
22
|
-
}
|
|
23
|
-
export default AIController;
|
|
24
|
-
export { AIController };
|
|
25
|
-
//# sourceMappingURL=AIController.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"AIController.d.ts","sourceRoot":"","sources":["../../src/controllers/AIController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,CAAC,EAAC,MAAM,KAAK,CAAC;AACtB,OAAO,EAAC,gBAAgB,EAAC,MAAM,mBAAmB,CAAC;AAsBnD,QAAA,MAAM,mBAAmB;;;;;;;;;;;;iBASvB,CAAA;AAEF,cAAM,YAAa,SAAQ,gBAAgB;IAEvC,SAAS,CAAC,qBAAqB,CAAC,KAAK,EAAE,GAAG,GAAG,CAAC,CAAC,UAAU;IAqCzD,SAAS,CAAC,eAAe,CAAC,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,UAAU,CAAC;IAOtE,SAAS,CAAC,iBAAiB,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC;IAgBtE,SAAS,CAAC,cAAc,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,mBAAmB,CAAC;IAe7D,MAAM,CAAC,GAAG,KAAA,EAAE,GAAG,KAAA;CAqDxB;AAED,eAAe,YAAY,CAAC;AAC5B,OAAO,EAAC,YAAY,EAAC,CAAC"}
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
interface IAILogBase {
|
|
2
|
-
provider?: string;
|
|
3
|
-
model?: string;
|
|
4
|
-
operationTitle?: string;
|
|
5
|
-
operationGroup?: string;
|
|
6
|
-
ip?: string;
|
|
7
|
-
userAgent?: string;
|
|
8
|
-
input?: string;
|
|
9
|
-
inputImages?: Array<{
|
|
10
|
-
filename?: string;
|
|
11
|
-
filepath?: string;
|
|
12
|
-
size?: number;
|
|
13
|
-
mimetype?: string;
|
|
14
|
-
url?: string;
|
|
15
|
-
}>;
|
|
16
|
-
inputFiles?: Array<{
|
|
17
|
-
filename?: string;
|
|
18
|
-
filepath?: string;
|
|
19
|
-
size?: number;
|
|
20
|
-
mimetype?: string;
|
|
21
|
-
url?: string;
|
|
22
|
-
}>;
|
|
23
|
-
inputTokens?: number;
|
|
24
|
-
outputTokens?: number;
|
|
25
|
-
tokens?: number;
|
|
26
|
-
startedAt?: Date;
|
|
27
|
-
endedAt?: Date;
|
|
28
|
-
responseTime?: string;
|
|
29
|
-
output?: string;
|
|
30
|
-
success?: boolean;
|
|
31
|
-
statusCode?: number;
|
|
32
|
-
errorMessage?: string;
|
|
33
|
-
tenant?: any;
|
|
34
|
-
user?: any;
|
|
35
|
-
createdAt?: Date;
|
|
36
|
-
updatedAt?: Date;
|
|
37
|
-
}
|
|
38
|
-
interface IAILog {
|
|
39
|
-
_id: string;
|
|
40
|
-
provider?: string;
|
|
41
|
-
model?: string;
|
|
42
|
-
operationTitle?: string;
|
|
43
|
-
operationGroup?: string;
|
|
44
|
-
ip?: string;
|
|
45
|
-
userAgent?: string;
|
|
46
|
-
input?: string;
|
|
47
|
-
inputImages?: Array<{
|
|
48
|
-
filename?: string;
|
|
49
|
-
filepath?: string;
|
|
50
|
-
size?: number;
|
|
51
|
-
mimetype?: string;
|
|
52
|
-
url?: string;
|
|
53
|
-
}>;
|
|
54
|
-
inputFiles?: Array<{
|
|
55
|
-
filename?: string;
|
|
56
|
-
filepath?: string;
|
|
57
|
-
size?: number;
|
|
58
|
-
mimetype?: string;
|
|
59
|
-
url?: string;
|
|
60
|
-
}>;
|
|
61
|
-
inputTokens?: number;
|
|
62
|
-
outputTokens?: number;
|
|
63
|
-
tokens?: number;
|
|
64
|
-
startedAt?: Date;
|
|
65
|
-
endedAt?: Date;
|
|
66
|
-
responseTime?: string;
|
|
67
|
-
output?: string;
|
|
68
|
-
success?: boolean;
|
|
69
|
-
statusCode?: number;
|
|
70
|
-
errorMessage?: string;
|
|
71
|
-
tenant?: any;
|
|
72
|
-
user?: any;
|
|
73
|
-
createdAt?: Date;
|
|
74
|
-
updatedAt?: Date;
|
|
75
|
-
}
|
|
76
|
-
export type { IAILogBase, IAILog };
|
|
77
|
-
//# sourceMappingURL=IAILog.d.ts.map
|