@drax/ai-back 3.28.0 → 3.31.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/dist/agents/ChatbotTaskService.js +143 -0
- package/dist/agents/ChatbotTaskTools.js +756 -0
- package/dist/agents/DraxAgent.js +450 -0
- package/dist/controllers/AgentSessionController.js +18 -0
- package/dist/controllers/DraxAgentController.js +114 -0
- package/dist/factory/services/AgentSessionServiceFactory.js +30 -0
- package/dist/index.js +15 -1
- package/dist/interfaces/IAgentSession.js +1 -0
- package/dist/interfaces/IAgentSessionRepository.js +1 -0
- package/dist/interfaces/IBuilderTool.js +1 -0
- package/dist/interfaces/IDraxAgent.js +1 -0
- package/dist/interfaces/IDraxAgentController.js +1 -0
- package/dist/interfaces/IDraxAgentRoutes.js +1 -0
- package/dist/models/AgentSessionModel.js +31 -0
- package/dist/permissions/AgentPermissions.js +6 -0
- package/dist/permissions/AgentSessionPermissions.js +10 -0
- package/dist/repository/mongo/AgentSessionMongoRepository.js +13 -0
- package/dist/repository/sqlite/AgentSessionSqliteRepository.js +34 -0
- package/dist/routes/AgentSessionRoutes.js +21 -0
- package/dist/routes/ChatbotTaskRoutes.js +8 -0
- package/dist/routes/DraxAgentRoutes.js +9 -0
- package/dist/schemas/AgentSessionSchema.js +25 -0
- package/dist/services/AgentSessionService.js +9 -0
- package/dist/tools/BuilderTool.js +248 -0
- package/dist/tools/ToolBuilder.js +243 -0
- package/package.json +4 -4
- package/src/agents/DraxAgent.ts +574 -0
- package/src/controllers/AgentSessionController.ts +29 -0
- package/src/controllers/DraxAgentController.ts +135 -0
- package/src/factory/services/AgentSessionServiceFactory.ts +41 -0
- package/src/index.ts +58 -1
- package/src/interfaces/IAIProvider.ts +8 -0
- package/src/interfaces/IAgentSession.ts +44 -0
- package/src/interfaces/IAgentSessionRepository.ts +11 -0
- package/src/interfaces/IBuilderTool.ts +51 -0
- package/src/interfaces/IDraxAgent.ts +108 -0
- package/src/interfaces/IDraxAgentController.ts +5 -0
- package/src/interfaces/IDraxAgentRoutes.ts +7 -0
- package/src/models/AgentSessionModel.ts +46 -0
- package/src/permissions/AgentPermissions.ts +10 -0
- package/src/permissions/AgentSessionPermissions.ts +14 -0
- package/src/repository/mongo/AgentSessionMongoRepository.ts +22 -0
- package/src/repository/sqlite/AgentSessionSqliteRepository.ts +42 -0
- package/src/routes/AgentSessionRoutes.ts +38 -0
- package/src/routes/DraxAgentRoutes.ts +12 -0
- package/src/schemas/AgentSessionSchema.ts +30 -0
- package/src/services/AgentSessionService.ts +20 -0
- package/src/tools/BuilderTool.ts +289 -0
- package/test/DraxAgent.test.ts +221 -0
- package/test/ToolBuilder.test.ts +90 -0
- package/tsconfig.tsbuildinfo +1 -1
- package/types/agents/ChatbotTaskService.d.ts +42 -0
- package/types/agents/ChatbotTaskService.d.ts.map +1 -0
- package/types/agents/ChatbotTaskTools.d.ts +54 -0
- package/types/agents/ChatbotTaskTools.d.ts.map +1 -0
- package/types/agents/DraxAgent.d.ts +55 -0
- package/types/agents/DraxAgent.d.ts.map +1 -0
- package/types/controllers/AgentSessionController.d.ts +8 -0
- package/types/controllers/AgentSessionController.d.ts.map +1 -0
- package/types/controllers/DraxAgentController.d.ts +16 -0
- package/types/controllers/DraxAgentController.d.ts.map +1 -0
- package/types/factory/services/AgentSessionServiceFactory.d.ts +8 -0
- package/types/factory/services/AgentSessionServiceFactory.d.ts.map +1 -0
- package/types/index.d.ts +14 -2
- package/types/index.d.ts.map +1 -1
- package/types/interfaces/IAIProvider.d.ts +7 -1
- package/types/interfaces/IAIProvider.d.ts.map +1 -1
- package/types/interfaces/IAgentSession.d.ts +39 -0
- package/types/interfaces/IAgentSession.d.ts.map +1 -0
- package/types/interfaces/IAgentSessionRepository.d.ts +6 -0
- package/types/interfaces/IAgentSessionRepository.d.ts.map +1 -0
- package/types/interfaces/IBuilderTool.d.ts +26 -0
- package/types/interfaces/IBuilderTool.d.ts.map +1 -0
- package/types/interfaces/IDraxAgent.d.ts +74 -0
- package/types/interfaces/IDraxAgent.d.ts.map +1 -0
- package/types/interfaces/IDraxAgentController.d.ts +5 -0
- package/types/interfaces/IDraxAgentController.d.ts.map +1 -0
- package/types/interfaces/IDraxAgentRoutes.d.ts +6 -0
- package/types/interfaces/IDraxAgentRoutes.d.ts.map +1 -0
- package/types/models/AgentSessionModel.d.ts +15 -0
- package/types/models/AgentSessionModel.d.ts.map +1 -0
- package/types/permissions/AgentPermissions.d.ts +6 -0
- package/types/permissions/AgentPermissions.d.ts.map +1 -0
- package/types/permissions/AgentSessionPermissions.d.ts +10 -0
- package/types/permissions/AgentSessionPermissions.d.ts.map +1 -0
- package/types/repository/mongo/AgentSessionMongoRepository.d.ts +9 -0
- package/types/repository/mongo/AgentSessionMongoRepository.d.ts.map +1 -0
- package/types/repository/sqlite/AgentSessionSqliteRepository.d.ts +23 -0
- package/types/repository/sqlite/AgentSessionSqliteRepository.d.ts.map +1 -0
- package/types/routes/AgentSessionRoutes.d.ts +4 -0
- package/types/routes/AgentSessionRoutes.d.ts.map +1 -0
- package/types/routes/ChatbotTaskRoutes.d.ts +4 -0
- package/types/routes/ChatbotTaskRoutes.d.ts.map +1 -0
- package/types/routes/DraxAgentRoutes.d.ts +4 -0
- package/types/routes/DraxAgentRoutes.d.ts.map +1 -0
- package/types/schemas/AgentSessionSchema.d.ts +51 -0
- package/types/schemas/AgentSessionSchema.d.ts.map +1 -0
- package/types/services/AgentSessionService.d.ts +10 -0
- package/types/services/AgentSessionService.d.ts.map +1 -0
- package/types/tools/BuilderTool.d.ts +35 -0
- package/types/tools/BuilderTool.d.ts.map +1 -0
- package/types/tools/ToolBuilder.d.ts +47 -0
- package/types/tools/ToolBuilder.d.ts.map +1 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {z} from "zod";
|
|
2
|
+
import {CommonController} from "@drax/common-back";
|
|
3
|
+
import {DraxAgent} from "../agents/DraxAgent.js";
|
|
4
|
+
import {AgentPermissions} from "../permissions/AgentPermissions.js";
|
|
5
|
+
|
|
6
|
+
const PromptImageSchema = z.object({
|
|
7
|
+
url: z.string().min(1),
|
|
8
|
+
detail: z.enum(["auto", "low", "high"]).optional(),
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const PromptContentPartSchema = z.discriminatedUnion("type", [
|
|
12
|
+
z.object({
|
|
13
|
+
type: z.literal("text"),
|
|
14
|
+
text: z.string(),
|
|
15
|
+
}),
|
|
16
|
+
z.object({
|
|
17
|
+
type: z.literal("image"),
|
|
18
|
+
imageUrl: z.string().min(1),
|
|
19
|
+
detail: z.enum(["auto", "low", "high"]).optional(),
|
|
20
|
+
}),
|
|
21
|
+
]);
|
|
22
|
+
|
|
23
|
+
const PromptMemorySchema = z.object({
|
|
24
|
+
key: z.string().min(1),
|
|
25
|
+
value: z.string(),
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const PromptInputFileSchema = z.object({
|
|
29
|
+
filename: z.string().optional(),
|
|
30
|
+
filepath: z.string().optional(),
|
|
31
|
+
size: z.number().nullable().optional(),
|
|
32
|
+
mimetype: z.string().optional(),
|
|
33
|
+
url: z.string().optional(),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const AgentSessionRequestSchema = z.object({
|
|
37
|
+
sessionId: z.string().optional(),
|
|
38
|
+
userId: z.string().optional().nullable(),
|
|
39
|
+
tenantId: z.string().optional().nullable(),
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const AgentMessageRequestSchema = AgentSessionRequestSchema.extend({
|
|
43
|
+
message: z.string().min(1),
|
|
44
|
+
userImages: z.array(PromptImageSchema).optional(),
|
|
45
|
+
userContent: z.array(PromptContentPartSchema).optional(),
|
|
46
|
+
inputFiles: z.array(PromptInputFileSchema).optional(),
|
|
47
|
+
memory: z.array(PromptMemorySchema).optional(),
|
|
48
|
+
memoryHeader: z.string().optional(),
|
|
49
|
+
knowledgeBase: z.array(z.string()).optional(),
|
|
50
|
+
knowledgeBaseHeader: z.string().optional(),
|
|
51
|
+
model: z.string().optional(),
|
|
52
|
+
toolMaxIterations: z.number().optional(),
|
|
53
|
+
operationTitle: z.string().optional(),
|
|
54
|
+
operationGroup: z.string().optional(),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
class DraxAgentController extends CommonController {
|
|
58
|
+
protected agent: DraxAgent;
|
|
59
|
+
protected permission: string | false;
|
|
60
|
+
|
|
61
|
+
constructor() {
|
|
62
|
+
super();
|
|
63
|
+
this.permission = AgentPermissions.Session;
|
|
64
|
+
this.agent = DraxAgent.instance();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async startSession(request, reply) {
|
|
68
|
+
try {
|
|
69
|
+
this.assertAccess(request);
|
|
70
|
+
|
|
71
|
+
const input = AgentSessionRequestSchema.parse(request.body ?? {});
|
|
72
|
+
const session = await this.agent.startSession({
|
|
73
|
+
sessionId: input.sessionId,
|
|
74
|
+
userId: this.resolveUserId(request, input.userId),
|
|
75
|
+
tenantId: this.resolveTenantId(request, input.tenantId),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
return reply.send({
|
|
79
|
+
sessionId: session.id,
|
|
80
|
+
createdAt: session.createdAt,
|
|
81
|
+
updatedAt: session.updatedAt,
|
|
82
|
+
});
|
|
83
|
+
} catch (e: any) {
|
|
84
|
+
this.handleControllerError(e, reply);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async message(request, reply) {
|
|
89
|
+
try {
|
|
90
|
+
this.assertAccess(request);
|
|
91
|
+
|
|
92
|
+
const input = AgentMessageRequestSchema.parse(request.body ?? {});
|
|
93
|
+
const response = await this.agent.sendMessage({
|
|
94
|
+
...input,
|
|
95
|
+
userId: this.resolveUserId(request, input.userId),
|
|
96
|
+
tenantId: this.resolveTenantId(request, input.tenantId),
|
|
97
|
+
ip: request.ip,
|
|
98
|
+
userAgent: request.headers["user-agent"],
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
return reply.send(response);
|
|
102
|
+
} catch (e: any) {
|
|
103
|
+
this.handleControllerError(e, reply);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
protected assertAccess(request: any) {
|
|
108
|
+
if (this.permission === false) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
request.rbac.assertPermission(this.permission);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
protected resolveUserId(request: any, userId?: string | null) {
|
|
116
|
+
return request.rbac?.userId ?? userId ?? null;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
protected resolveTenantId(request: any, tenantId?: string | null) {
|
|
120
|
+
return request.rbac?.tenantId ?? tenantId ?? null;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
protected handleControllerError(e: any, reply: any) {
|
|
124
|
+
if (e?.name === "ZodError") {
|
|
125
|
+
return reply.status(400).send({
|
|
126
|
+
message: e?.message || "Drax agent validation error",
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
this.handleError(e, reply);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export default DraxAgentController;
|
|
135
|
+
export {DraxAgentController};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
import AgentSessionMongoRepository from '../../repository/mongo/AgentSessionMongoRepository.js'
|
|
3
|
+
import AgentSessionSqliteRepository from '../../repository/sqlite/AgentSessionSqliteRepository.js'
|
|
4
|
+
import type {IAgentSessionRepository} from "../../interfaces/IAgentSessionRepository";
|
|
5
|
+
import {AgentSessionService} from '../../services/AgentSessionService.js'
|
|
6
|
+
import {AgentSessionBaseSchema, AgentSessionSchema} from "../../schemas/AgentSessionSchema.js";
|
|
7
|
+
import {COMMON, CommonConfig, DraxConfig} from "@drax/common-back";
|
|
8
|
+
|
|
9
|
+
class AgentSessionServiceFactory {
|
|
10
|
+
private static service: AgentSessionService;
|
|
11
|
+
|
|
12
|
+
public static get instance(): AgentSessionService {
|
|
13
|
+
if (!AgentSessionServiceFactory.service) {
|
|
14
|
+
|
|
15
|
+
let repository: IAgentSessionRepository
|
|
16
|
+
switch (DraxConfig.getOrLoad(CommonConfig.DbEngine)) {
|
|
17
|
+
case COMMON.DB_ENGINES.MONGODB:
|
|
18
|
+
repository = new AgentSessionMongoRepository()
|
|
19
|
+
break;
|
|
20
|
+
case COMMON.DB_ENGINES.SQLITE:
|
|
21
|
+
const dbFile = DraxConfig.getOrLoad(CommonConfig.SqliteDbFile)
|
|
22
|
+
repository = new AgentSessionSqliteRepository(dbFile, false)
|
|
23
|
+
repository.build()
|
|
24
|
+
break;
|
|
25
|
+
default:
|
|
26
|
+
throw new Error("DraxConfig.DB_ENGINE must be one of " + Object.values(COMMON.DB_ENGINES).join(", "));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const baseSchema = AgentSessionBaseSchema;
|
|
30
|
+
const fullSchema = AgentSessionSchema;
|
|
31
|
+
AgentSessionServiceFactory.service = new AgentSessionService(repository, baseSchema, fullSchema);
|
|
32
|
+
}
|
|
33
|
+
return AgentSessionServiceFactory.service;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export default AgentSessionServiceFactory
|
|
38
|
+
export {
|
|
39
|
+
AgentSessionServiceFactory
|
|
40
|
+
}
|
|
41
|
+
|
package/src/index.ts
CHANGED
|
@@ -7,15 +7,23 @@ import {OpenAiProviderFactory} from "./factory/OpenAiProviderFactory.js";
|
|
|
7
7
|
import {AiProviderFactory} from "./factory/AiProviderFactory.js";
|
|
8
8
|
import AILogServiceFactory from "./factory/services/AILogServiceFactory.js";
|
|
9
9
|
import {OpenAiProvider} from "./providers/OpenAiProvider.js";
|
|
10
|
+
import {BuilderTool} from "./tools/BuilderTool.js";
|
|
10
11
|
import {KnowledgeService} from "./services/KnowledgeService.js";
|
|
11
12
|
import {AILogService} from "./services/AILogService.js";
|
|
12
13
|
import AILogPermissions from "./permissions/AILogPermissions.js";
|
|
14
|
+
import AgentPermissions from "./permissions/AgentPermissions.js";
|
|
15
|
+
import AgentSessionPermissions from "./permissions/AgentSessionPermissions.js";
|
|
13
16
|
import AIPermissions from "./permissions/AIPermissions.js";
|
|
14
17
|
import AILogController from "./controllers/AILogController.js";
|
|
15
18
|
import AICrudController from "./controllers/AICrudController.js";
|
|
16
19
|
import AIGenericController from "./controllers/AIGenericController.js";
|
|
20
|
+
import DraxAgentController from "./controllers/DraxAgentController.js";
|
|
21
|
+
import AgentSessionController from "./controllers/AgentSessionController.js";
|
|
17
22
|
import AILogRoutes from "./routes/AILogRoutes.js";
|
|
18
23
|
import AIRoutes from "./routes/AIRoutes.js";
|
|
24
|
+
import DraxAgentRoutes from "./routes/DraxAgentRoutes.js";
|
|
25
|
+
import AgentSessionRoutes from "./routes/AgentSessionRoutes.js";
|
|
26
|
+
import {DraxAgent} from "./agents/DraxAgent.js";
|
|
19
27
|
import type {IAILogRepository} from "./interfaces/IAILogRepository.js";
|
|
20
28
|
import type {
|
|
21
29
|
IAIProvider,
|
|
@@ -30,6 +38,29 @@ import type {
|
|
|
30
38
|
IPromptResponse,
|
|
31
39
|
IPromptTool
|
|
32
40
|
} from "./interfaces/IAIProvider.js";
|
|
41
|
+
import type {
|
|
42
|
+
ToolBuilderMethod,
|
|
43
|
+
ToolBuilderOptions,
|
|
44
|
+
ToolBuilderService
|
|
45
|
+
} from "./interfaces/IBuilderTool.js";
|
|
46
|
+
import type {
|
|
47
|
+
DraxAgentControllerOptions
|
|
48
|
+
} from "./interfaces/IDraxAgentController.js";
|
|
49
|
+
import type {
|
|
50
|
+
DraxAgentFastifyRoutesOptions
|
|
51
|
+
} from "./interfaces/IDraxAgentRoutes.js";
|
|
52
|
+
import type {
|
|
53
|
+
DraxAgentConfig,
|
|
54
|
+
DraxAgentMessageInput,
|
|
55
|
+
DraxAgentMessageOutput,
|
|
56
|
+
DraxAgentPromptContext,
|
|
57
|
+
DraxAgentSession,
|
|
58
|
+
DraxAgentSessionInput,
|
|
59
|
+
DraxAgentToolBuilder,
|
|
60
|
+
DraxAgentToolBuilderSource,
|
|
61
|
+
DraxAgentToolSource,
|
|
62
|
+
DraxAgentSystemPrompt,
|
|
63
|
+
} from "./interfaces/IDraxAgent.js";
|
|
33
64
|
|
|
34
65
|
export type {
|
|
35
66
|
|
|
@@ -45,6 +76,21 @@ export type {
|
|
|
45
76
|
IPromptContentPartImage,
|
|
46
77
|
IPromptContentPartText,
|
|
47
78
|
IPromptResponse,
|
|
79
|
+
ToolBuilderMethod,
|
|
80
|
+
ToolBuilderOptions,
|
|
81
|
+
ToolBuilderService,
|
|
82
|
+
DraxAgentControllerOptions,
|
|
83
|
+
DraxAgentFastifyRoutesOptions,
|
|
84
|
+
DraxAgentConfig,
|
|
85
|
+
DraxAgentMessageInput,
|
|
86
|
+
DraxAgentMessageOutput,
|
|
87
|
+
DraxAgentPromptContext,
|
|
88
|
+
DraxAgentSession,
|
|
89
|
+
DraxAgentSessionInput,
|
|
90
|
+
DraxAgentToolBuilder,
|
|
91
|
+
DraxAgentToolBuilderSource,
|
|
92
|
+
DraxAgentToolSource,
|
|
93
|
+
DraxAgentSystemPrompt,
|
|
48
94
|
}
|
|
49
95
|
|
|
50
96
|
export {
|
|
@@ -58,13 +104,24 @@ export {
|
|
|
58
104
|
AiProviderFactory,
|
|
59
105
|
AILogServiceFactory,
|
|
60
106
|
OpenAiProvider,
|
|
107
|
+
BuilderTool,
|
|
108
|
+
//Service
|
|
61
109
|
KnowledgeService,
|
|
62
110
|
AILogService,
|
|
111
|
+
//Permissions
|
|
63
112
|
AILogPermissions,
|
|
113
|
+
AgentPermissions,
|
|
64
114
|
AIPermissions,
|
|
115
|
+
AgentSessionPermissions,
|
|
116
|
+
//Controllers
|
|
65
117
|
AILogController,
|
|
66
118
|
AICrudController,
|
|
67
119
|
AIGenericController,
|
|
120
|
+
DraxAgentController,
|
|
121
|
+
AgentSessionController,
|
|
122
|
+
DraxAgent,
|
|
68
123
|
AILogRoutes,
|
|
69
|
-
AIRoutes
|
|
124
|
+
AIRoutes,
|
|
125
|
+
DraxAgentRoutes,
|
|
126
|
+
AgentSessionRoutes
|
|
70
127
|
}
|
|
@@ -37,6 +37,13 @@ interface IPromptTool {
|
|
|
37
37
|
description: string;
|
|
38
38
|
parameters?: object;
|
|
39
39
|
execute: (args: any) => any | Promise<any>;
|
|
40
|
+
navigation?: IPromptToolNavigation;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
interface IPromptToolNavigation {
|
|
44
|
+
entityName: string;
|
|
45
|
+
method: string;
|
|
46
|
+
basePath?: string;
|
|
40
47
|
}
|
|
41
48
|
|
|
42
49
|
interface IPromptParams {
|
|
@@ -88,6 +95,7 @@ export type {
|
|
|
88
95
|
IPromptMessage,
|
|
89
96
|
IPromptMemory,
|
|
90
97
|
IPromptTool,
|
|
98
|
+
IPromptToolNavigation,
|
|
91
99
|
IPromptImage,
|
|
92
100
|
IPromptImageDetail,
|
|
93
101
|
IPromptContentPart,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
|
|
2
|
+
interface IAgentSessionBase {
|
|
3
|
+
sessionId: string
|
|
4
|
+
title?: string
|
|
5
|
+
lastMessageAt?: Date
|
|
6
|
+
messages?: Array<{
|
|
7
|
+
role: string
|
|
8
|
+
content: string
|
|
9
|
+
createdAt?: Date
|
|
10
|
+
}>
|
|
11
|
+
messageCount?: number
|
|
12
|
+
inputTokens?: number
|
|
13
|
+
outputTokens?: number
|
|
14
|
+
tokens?: number
|
|
15
|
+
tenant?: any
|
|
16
|
+
user?: any
|
|
17
|
+
createdAt?: Date
|
|
18
|
+
updatedAt?: Date
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface IAgentSession {
|
|
22
|
+
_id: string
|
|
23
|
+
sessionId: string
|
|
24
|
+
title?: string
|
|
25
|
+
lastMessageAt?: Date
|
|
26
|
+
messages?: Array<{
|
|
27
|
+
role: string
|
|
28
|
+
content: string
|
|
29
|
+
createdAt?: Date
|
|
30
|
+
}>
|
|
31
|
+
messageCount?: number
|
|
32
|
+
inputTokens?: number
|
|
33
|
+
outputTokens?: number
|
|
34
|
+
tokens?: number
|
|
35
|
+
tenant?: any
|
|
36
|
+
user?: any
|
|
37
|
+
createdAt?: Date
|
|
38
|
+
updatedAt?: Date
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type {
|
|
42
|
+
IAgentSessionBase,
|
|
43
|
+
IAgentSession
|
|
44
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
import type {IAgentSession, IAgentSessionBase} from './IAgentSession'
|
|
3
|
+
import {IDraxCrudRepository} from "@drax/crud-share";
|
|
4
|
+
|
|
5
|
+
interface IAgentSessionRepository extends IDraxCrudRepository<IAgentSession, IAgentSessionBase, IAgentSessionBase>{
|
|
6
|
+
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export {IAgentSessionRepository}
|
|
10
|
+
|
|
11
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {IDraxCrudService} from "@drax/crud-share";
|
|
2
|
+
import type {ZodObject, ZodRawShape} from "zod";
|
|
3
|
+
|
|
4
|
+
type ToolBuilderMethod =
|
|
5
|
+
| "create"
|
|
6
|
+
| "update"
|
|
7
|
+
| "updatePartial"
|
|
8
|
+
| "delete"
|
|
9
|
+
| "findById"
|
|
10
|
+
| "findByIds"
|
|
11
|
+
| "findOneBy"
|
|
12
|
+
| "findOne"
|
|
13
|
+
| "findBy"
|
|
14
|
+
| "fetchAll"
|
|
15
|
+
| "search"
|
|
16
|
+
| "find"
|
|
17
|
+
| "paginate"
|
|
18
|
+
| "groupBy";
|
|
19
|
+
|
|
20
|
+
type ToolBuilderService<T = any, C = any, U = any> = IDraxCrudService<T, C, U> & Record<string, any>;
|
|
21
|
+
|
|
22
|
+
interface ToolBuilderOptions<T = any, C = any, U = any> {
|
|
23
|
+
entityName: string;
|
|
24
|
+
entityDescription?: string;
|
|
25
|
+
navigationBasePath?: string;
|
|
26
|
+
schema: ZodObject<ZodRawShape>;
|
|
27
|
+
outputSchema?: ZodObject<ZodRawShape>;
|
|
28
|
+
service: ToolBuilderService<T, C, U>;
|
|
29
|
+
methods: ToolBuilderMethod[];
|
|
30
|
+
toolNamePrefix?: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface ToolBuilderLike {
|
|
34
|
+
inputSchema: ZodObject<ZodRawShape>;
|
|
35
|
+
partialInputSchema: ZodObject<ZodRawShape>;
|
|
36
|
+
objectParameters(schema: ZodObject<ZodRawShape>): object;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
interface ToolDefinition<T = any, C = any, U = any> {
|
|
40
|
+
description: (entityName: string) => string;
|
|
41
|
+
parameters: (builder: ToolBuilderLike) => object;
|
|
42
|
+
execute: (service: ToolBuilderService<T, C, U>, args: any) => any | Promise<any>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type {
|
|
46
|
+
ToolBuilderLike,
|
|
47
|
+
ToolBuilderMethod,
|
|
48
|
+
ToolBuilderOptions,
|
|
49
|
+
ToolBuilderService,
|
|
50
|
+
ToolDefinition,
|
|
51
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
IAIProvider,
|
|
3
|
+
IPromptContentPart,
|
|
4
|
+
IPromptImage,
|
|
5
|
+
IPromptMemory,
|
|
6
|
+
IPromptMessage,
|
|
7
|
+
IPromptTool
|
|
8
|
+
} from "./IAIProvider.js";
|
|
9
|
+
import type {AgentSessionService} from "../services/AgentSessionService.js";
|
|
10
|
+
|
|
11
|
+
type DraxAgentSystemPrompt =
|
|
12
|
+
| string
|
|
13
|
+
| ((context: DraxAgentPromptContext) => string | Promise<string>);
|
|
14
|
+
|
|
15
|
+
type DraxAgentToolSource =
|
|
16
|
+
| IPromptTool[]
|
|
17
|
+
| ((context: DraxAgentPromptContext) => IPromptTool[] | Promise<IPromptTool[]>);
|
|
18
|
+
|
|
19
|
+
interface DraxAgentToolBuilder {
|
|
20
|
+
getTools(): IPromptTool[];
|
|
21
|
+
getSystemPromptSection(): string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type DraxAgentToolBuilderSource =
|
|
25
|
+
| DraxAgentToolBuilder[]
|
|
26
|
+
| ((context: DraxAgentPromptContext) => DraxAgentToolBuilder[] | Promise<DraxAgentToolBuilder[]>);
|
|
27
|
+
|
|
28
|
+
interface DraxAgentConfig {
|
|
29
|
+
provider?: IAIProvider;
|
|
30
|
+
systemPrompt?: DraxAgentSystemPrompt;
|
|
31
|
+
toolBuilders?: DraxAgentToolBuilderSource;
|
|
32
|
+
tools?: DraxAgentToolSource;
|
|
33
|
+
historyLimit?: number;
|
|
34
|
+
toolMaxIterations?: number;
|
|
35
|
+
operationTitle?: string;
|
|
36
|
+
operationGroup?: string;
|
|
37
|
+
logToolExecution?: boolean;
|
|
38
|
+
normalizeOutput?: (output: any) => string;
|
|
39
|
+
sessionService?: AgentSessionService | false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
interface DraxAgentSessionInput {
|
|
43
|
+
sessionId?: string;
|
|
44
|
+
userId?: string | null;
|
|
45
|
+
tenantId?: string | null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface DraxAgentMessageInput extends DraxAgentSessionInput {
|
|
49
|
+
message: string;
|
|
50
|
+
ip?: string;
|
|
51
|
+
userAgent?: string;
|
|
52
|
+
model?: string;
|
|
53
|
+
userImages?: IPromptImage[];
|
|
54
|
+
userContent?: IPromptContentPart[];
|
|
55
|
+
inputFiles?: Array<{
|
|
56
|
+
filename?: string;
|
|
57
|
+
filepath?: string;
|
|
58
|
+
size?: number | null;
|
|
59
|
+
mimetype?: string;
|
|
60
|
+
url?: string;
|
|
61
|
+
}>;
|
|
62
|
+
memory?: IPromptMemory[];
|
|
63
|
+
memoryHeader?: string;
|
|
64
|
+
knowledgeBase?: string[];
|
|
65
|
+
knowledgeBaseHeader?: string;
|
|
66
|
+
toolMaxIterations?: number;
|
|
67
|
+
operationTitle?: string;
|
|
68
|
+
operationGroup?: string;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface DraxAgentMessageOutput {
|
|
72
|
+
sessionId: string;
|
|
73
|
+
message: string;
|
|
74
|
+
navigationPath?: string | null;
|
|
75
|
+
output: any;
|
|
76
|
+
tokens: number;
|
|
77
|
+
inputTokens: number;
|
|
78
|
+
outputTokens: number;
|
|
79
|
+
time: number;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface DraxAgentSession {
|
|
83
|
+
id: string;
|
|
84
|
+
recordId?: string | null;
|
|
85
|
+
userId?: string | null;
|
|
86
|
+
tenantId?: string | null;
|
|
87
|
+
messages: IPromptMessage[];
|
|
88
|
+
createdAt: Date;
|
|
89
|
+
updatedAt: Date;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
interface DraxAgentPromptContext {
|
|
93
|
+
session: DraxAgentSession;
|
|
94
|
+
input?: DraxAgentMessageInput;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type {
|
|
98
|
+
DraxAgentConfig,
|
|
99
|
+
DraxAgentMessageInput,
|
|
100
|
+
DraxAgentMessageOutput,
|
|
101
|
+
DraxAgentPromptContext,
|
|
102
|
+
DraxAgentSession,
|
|
103
|
+
DraxAgentSessionInput,
|
|
104
|
+
DraxAgentSystemPrompt,
|
|
105
|
+
DraxAgentToolBuilder,
|
|
106
|
+
DraxAgentToolBuilderSource,
|
|
107
|
+
DraxAgentToolSource,
|
|
108
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
|
|
2
|
+
import {mongoose} from '@drax/common-back';
|
|
3
|
+
import {PaginateModel} from "mongoose";
|
|
4
|
+
import uniqueValidator from 'mongoose-unique-validator';
|
|
5
|
+
import mongoosePaginate from 'mongoose-paginate-v2'
|
|
6
|
+
import type {IAgentSession} from '../interfaces/IAgentSession'
|
|
7
|
+
|
|
8
|
+
const AgentSessionSchema = new mongoose.Schema<IAgentSession>({
|
|
9
|
+
sessionId: {type: String, required: true, index: true, unique: false },
|
|
10
|
+
title: {type: String, required: false, index: false, unique: false },
|
|
11
|
+
lastMessageAt: {type: Date, required: false, index: true, unique: false },
|
|
12
|
+
messages: [{
|
|
13
|
+
role: {type: String, enum: ['user', 'assistant', 'system'], required: true, index: false, unique: false },
|
|
14
|
+
content: {type: String, required: true, index: false, unique: false },
|
|
15
|
+
createdAt: {type: Date, required: false, index: false, unique: false }
|
|
16
|
+
}],
|
|
17
|
+
messageCount: {type: Number, required: false, index: false, unique: false },
|
|
18
|
+
inputTokens: {type: Number, required: false, index: false, unique: false },
|
|
19
|
+
outputTokens: {type: Number, required: false, index: false, unique: false },
|
|
20
|
+
tokens: {type: Number, required: false, index: false, unique: false },
|
|
21
|
+
tenant: {type: mongoose.Schema.Types.ObjectId, ref: 'Tenant', required: false, index: true, unique: false },
|
|
22
|
+
user: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: false, index: true, unique: false }
|
|
23
|
+
}, {timestamps: true});
|
|
24
|
+
|
|
25
|
+
AgentSessionSchema.plugin(uniqueValidator, {message: 'validation.unique'});
|
|
26
|
+
AgentSessionSchema.plugin(mongoosePaginate);
|
|
27
|
+
|
|
28
|
+
AgentSessionSchema.virtual("id").get(function () {
|
|
29
|
+
return this._id.toString();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
AgentSessionSchema.set('toJSON', {getters: true, virtuals: true});
|
|
34
|
+
|
|
35
|
+
AgentSessionSchema.set('toObject', {getters: true, virtuals: true});
|
|
36
|
+
|
|
37
|
+
const MODEL_NAME = 'AgentSession';
|
|
38
|
+
const COLLECTION_NAME = 'AgentSession';
|
|
39
|
+
const AgentSessionModel = mongoose.model<IAgentSession, PaginateModel<IAgentSession>>(MODEL_NAME, AgentSessionSchema,COLLECTION_NAME);
|
|
40
|
+
|
|
41
|
+
export {
|
|
42
|
+
AgentSessionSchema,
|
|
43
|
+
AgentSessionModel
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default AgentSessionModel
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
enum AgentSessionPermissions {
|
|
3
|
+
|
|
4
|
+
Create = "agentsession:create",
|
|
5
|
+
Update = "agentsession:update",
|
|
6
|
+
Delete = "agentsession:delete",
|
|
7
|
+
View = "agentsession:view",
|
|
8
|
+
Manage = "agentsession:manage"
|
|
9
|
+
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export { AgentSessionPermissions };
|
|
13
|
+
export default AgentSessionPermissions;
|
|
14
|
+
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
|
|
2
|
+
import {AbstractMongoRepository} from "@drax/crud-back";
|
|
3
|
+
import {AgentSessionModel} from "../../models/AgentSessionModel.js";
|
|
4
|
+
import type {IAgentSessionRepository} from '../../interfaces/IAgentSessionRepository'
|
|
5
|
+
import type {IAgentSession, IAgentSessionBase} from "../../interfaces/IAgentSession";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AgentSessionMongoRepository extends AbstractMongoRepository<IAgentSession, IAgentSessionBase, IAgentSessionBase> implements IAgentSessionRepository {
|
|
9
|
+
|
|
10
|
+
constructor() {
|
|
11
|
+
super();
|
|
12
|
+
this._model = AgentSessionModel;
|
|
13
|
+
this._searchFields = ['sessionId', 'title'];
|
|
14
|
+
this._populateFields = ['tenant', 'user'];
|
|
15
|
+
this._lean = true
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default AgentSessionMongoRepository
|
|
21
|
+
export {AgentSessionMongoRepository}
|
|
22
|
+
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
import {AbstractSqliteRepository} from "@drax/crud-back";
|
|
3
|
+
import type {IAgentSessionRepository} from '../../interfaces/IAgentSessionRepository'
|
|
4
|
+
import type {IAgentSession, IAgentSessionBase} from "../../interfaces/IAgentSession";
|
|
5
|
+
import {SqliteTableField} from "@drax/common-back";
|
|
6
|
+
|
|
7
|
+
class AgentSessionSqliteRepository extends AbstractSqliteRepository<IAgentSession, IAgentSessionBase, IAgentSessionBase> implements IAgentSessionRepository {
|
|
8
|
+
|
|
9
|
+
protected db: any;
|
|
10
|
+
protected tableName: string = 'AgentSession';
|
|
11
|
+
protected dataBaseFile: string;
|
|
12
|
+
protected searchFields: string[] = ['sessionId', 'title'];
|
|
13
|
+
protected booleanFields: string[] = [];
|
|
14
|
+
protected jsonFields: string[] = ['messages'];
|
|
15
|
+
protected identifier: string = '_id';
|
|
16
|
+
protected populateFields = [
|
|
17
|
+
{ field: 'tenant', table: 'tenant', identifier: '_id' },
|
|
18
|
+
{ field: 'user', table: 'user', identifier: '_id' }
|
|
19
|
+
]
|
|
20
|
+
protected verbose: boolean = false;
|
|
21
|
+
protected tableFields: SqliteTableField[] = [
|
|
22
|
+
{name: "sessionId", type: "TEXT", unique: false, primary: false},
|
|
23
|
+
{name: "title", type: "TEXT", unique: false, primary: false},
|
|
24
|
+
{name: "lastMessageAt", type: "TEXT", unique: false, primary: false},
|
|
25
|
+
{name: "messages", type: "TEXT", unique: false, primary: false},
|
|
26
|
+
{name: "messageCount", type: "NUMERIC", unique: false, primary: false},
|
|
27
|
+
{name: "messageCount", type: "TEXT", unique: false, primary: false},
|
|
28
|
+
{name: "inputTokens", type: "NUMERIC", unique: false, primary: false},
|
|
29
|
+
{name: "inputTokens", type: "TEXT", unique: false, primary: false},
|
|
30
|
+
{name: "outputTokens", type: "NUMERIC", unique: false, primary: false},
|
|
31
|
+
{name: "outputTokens", type: "TEXT", unique: false, primary: false},
|
|
32
|
+
{name: "tokens", type: "NUMERIC", unique: false, primary: false},
|
|
33
|
+
{name: "tokens", type: "TEXT", unique: false, primary: false},
|
|
34
|
+
{name: "tenant", type: "TEXT", unique: false, primary: false},
|
|
35
|
+
{name: "user", type: "TEXT", unique: false, primary: false}
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default AgentSessionSqliteRepository
|
|
41
|
+
export {AgentSessionSqliteRepository}
|
|
42
|
+
|