-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathAiModelConfig.controller.ts
More file actions
165 lines (136 loc) · 4.82 KB
/
AiModelConfig.controller.ts
File metadata and controls
165 lines (136 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { Request, Response } from 'express'
import AiModelConfigUtility from './AiModelConfig.utility'
import UserUtils from '../user/user.utils'
import { SendResponse } from '../../../utils/SendResponse.utils'
import { aiModelConfig_Constant } from './AiModelConfig.constant'
import AiModelConfigService from './AiModelConfig.service'
import { aiModelConfig_CreateSchema, aiModelConfig_UpdateSchema } from './AiModelConfig.validator'
import { IAgent } from './AiModelConfig.type'
class AiModelConfig_Controller {
public async setupAiModelConfig(req: Request, res: Response): Promise<void> {
try {
const token = req.headers.authorization?.split(' ')[1] || req.cookies?.token
if (!token) throw new Error('Authentication token missing')
const FindUserByToken = await UserUtils.VerifyUserToken(token)
if (!FindUserByToken) throw new Error('Invalid authentication token')
const ValidateData = await aiModelConfig_CreateSchema.parseAsync(req.body)
const { agents, system_prompt } = ValidateData
const existingConfig = await AiModelConfigUtility.findAiModelConfigByUserId(
String(FindUserByToken._id)
)
let CreateConfig: any = null
if (!existingConfig) {
CreateConfig = await AiModelConfigService.setupAiModelConfig({
userId: String(FindUserByToken._id),
agents: [],
system_prompt,
})
}
// Add agents without duplicates (check in-memory)
const existingModels = new Set(
(existingConfig?.agents || CreateConfig?.agents || []).map((a: IAgent) => a.model)
)
for (const agent of agents as IAgent[]) {
if (!existingModels.has(agent.model)) {
await AiModelConfigService.addAgent(String(FindUserByToken._id), agent)
existingModels.add(agent.model)
}
}
const configToReturn = await AiModelConfigUtility.findAiModelConfigByUserId(
String(FindUserByToken._id)
)
SendResponse.success(
res,
aiModelConfig_Constant.AGENT_SETUP_SUCCESS,
{ config: configToReturn },
201
)
} catch (error) {
SendResponse.error(
res,
(error as Error).message || 'Failed to set up AI Model Config',
500,
error
)
}
}
public async getAiModelConfig(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?._id || (req as any).user?.id
if (!userId) {
SendResponse.error(res, 'Authentication required', 401, null)
return
}
const config = await AiModelConfigService.getConfig(String(userId))
if (!config) {
SendResponse.error(res, 'No AI Model Configuration found', 404, null)
return
}
SendResponse.success(res, 'AI Model Configuration retrieved successfully', {
config,
})
} catch (error) {
SendResponse.error(
res,
(error as Error).message || 'Failed to retrieve AI Model Config',
500,
error
)
}
}
public async updateAiModelConfig(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?._id || (req as any).user?.id
if (!userId) {
SendResponse.error(res, 'Authentication required', 401, null)
return
}
const ValidateData = await aiModelConfig_UpdateSchema.parseAsync(req.body)
const updatedConfig = await AiModelConfigService.updateConfig(String(userId), ValidateData)
if (!updatedConfig) {
SendResponse.error(res, 'AI Model Configuration not found', 404, null)
return
}
SendResponse.success(res, 'AI Model Configuration updated successfully', {
config: updatedConfig,
})
} catch (error) {
if ((error as any).code === 'P2025') {
SendResponse.error(res, 'AI Model Configuration not found', 404, null)
return
}
SendResponse.error(
res,
(error as Error).message || 'Failed to update AI Model Config',
500,
error
)
}
}
public async removeAgent(req: Request, res: Response): Promise<void> {
try {
const userId = (req as any).user?._id || (req as any).user?.id
const { agentId } = req.params
if (!userId) {
SendResponse.error(res, 'Authentication required', 401, null)
return
}
if (!agentId) {
SendResponse.error(res, 'Agent ID is required', 400, null)
return
}
const updatedConfig = await AiModelConfigService.removeAgent(String(userId), agentId)
SendResponse.success(res, 'Agent removed successfully', {
config: updatedConfig,
})
} catch (error) {
SendResponse.error(
res,
(error as Error).message || 'Failed to remove agent',
500,
error
)
}
}
}
export default new AiModelConfig_Controller()