-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathAiModelConfig.service.ts
More file actions
51 lines (42 loc) · 1.36 KB
/
AiModelConfig.service.ts
File metadata and controls
51 lines (42 loc) · 1.36 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
import AiModelConfig from './AiModelConfig.model'
import { IAiModelConfig, IAgent } from './AiModelConfig.type'
class AiModelConfig_Service {
async setupAiModelConfig(data: IAiModelConfig): Promise<IAiModelConfig> {
return await AiModelConfig.create(data)
}
async addAgent(userId: string, agent: IAgent): Promise<IAiModelConfig | null> {
return await AiModelConfig.findOneAndUpdate(
{ userId },
{ $addToSet: { agents: agent } },
{ new: true }
)
}
async removeAgent(userId: string, agentId: string): Promise<IAiModelConfig | null> {
if (!agentId) {
throw new Error('Agent ID is required to remove an agent.')
}
const config = await AiModelConfig.findOneAndUpdate(
{ userId },
{ $pull: { agents: { _id: agentId } } },
{ new: true }
).exec()
if (!config) {
throw new Error('AI Model Config not found for the user.')
}
return config
}
async getConfig(userId: string): Promise<IAiModelConfig | null> {
return await AiModelConfig.findOne({ userId }).select('-agents.key').exec()
}
async updateConfig(
userId: string,
updates: Partial<IAiModelConfig>
): Promise<IAiModelConfig | null> {
return await AiModelConfig.findOneAndUpdate({ userId }, updates, {
new: true,
})
.select('-agents.key')
.exec()
}
}
export default new AiModelConfig_Service()