-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathagent.controller.ts
More file actions
61 lines (55 loc) · 2.49 KB
/
agent.controller.ts
File metadata and controls
61 lines (55 loc) · 2.49 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
import { Request, Response } from 'express'
import { createRunSchema } from './agent.validator'
import { createAgentRun, executeAgentRun, getAgentRun, getAgentRunLogs } from './agent.service'
import { SendResponse } from '../../../utils/SendResponse.utils'
import { StatusConstant } from '../../../constant/Status.constant'
class AgentController {
constructor() {
this.createRun = this.createRun.bind(this)
this.executeRun = this.executeRun.bind(this)
this.getRunStatus = this.getRunStatus.bind(this)
this.getRunLogs = this.getRunLogs.bind(this)
}
async createRun(req: Request, res: Response): Promise<void> {
try {
const validatedData = await createRunSchema.parseAsync(req.body)
// Cast the agents to any to bypass strict type checking if needed vs Validator types
const run = await createAgentRun(validatedData.runName, validatedData.agents as any)
SendResponse.success(res, 'Agent run created successfully', run, StatusConstant.CREATED)
} catch (err: any) {
SendResponse.error(res, err.message, StatusConstant.BAD_REQUEST, err)
}
}
async executeRun(req: Request, res: Response): Promise<void> {
try {
const { id } = req.params
const run = await executeAgentRun(id)
SendResponse.success(res, 'Agent run execution started', run, StatusConstant.OK)
} catch (err: any) {
SendResponse.error(res, err.message, StatusConstant.INTERNAL_SERVER_ERROR, err)
}
}
async getRunStatus(req: Request, res: Response): Promise<void> {
try {
const { id } = req.params
const run = await getAgentRun(id)
if (!run) {
SendResponse.error(res, 'Run not found', StatusConstant.NOT_FOUND)
return
}
SendResponse.success(res, 'Agent run status fetched', run, StatusConstant.OK)
} catch (err: any) {
SendResponse.error(res, err.message, StatusConstant.INTERNAL_SERVER_ERROR, err)
}
}
async getRunLogs(req: Request, res: Response): Promise<void> {
try {
const { id } = req.params
const logs = await getAgentRunLogs(id)
SendResponse.success(res, 'Agent run logs fetched', logs, StatusConstant.OK)
} catch (err: any) {
SendResponse.error(res, err.message, StatusConstant.INTERNAL_SERVER_ERROR, err)
}
}
}
export default new AgentController()