-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathagent-run.ts
More file actions
127 lines (116 loc) · 2.85 KB
/
agent-run.ts
File metadata and controls
127 lines (116 loc) · 2.85 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
import { TEST_USER_ID } from '@codebuff/common/old-constants'
import db from '@codebuff/internal/db'
import * as schema from '@codebuff/internal/db/schema'
import { eq } from 'drizzle-orm'
import type {
AddAgentStepFn,
FinishAgentRunFn,
StartAgentRunFn,
} from '@codebuff/common/types/contracts/database'
import type { ParamsOf } from '@codebuff/common/types/function-params'
/**
* Starts a new agent run and creates an entry in the agent_run table
*/
export async function startAgentRun(
params: ParamsOf<StartAgentRunFn>,
): ReturnType<StartAgentRunFn> {
const { userId, agentId, ancestorRunIds, logger } = params
if (userId === TEST_USER_ID) {
return 'test-run-id'
}
const runId = crypto.randomUUID()
try {
await db.insert(schema.agentRun).values({
id: runId,
user_id: userId,
agent_id: agentId,
ancestor_run_ids: ancestorRunIds.length > 0 ? ancestorRunIds : null,
status: 'running',
created_at: new Date(),
})
return runId
} catch (error) {
logger.error(
{ error, runId, userId, agentId, ancestorRunIds },
'Failed to start agent run',
)
throw error
}
}
/**
* Completes an agent run by updating its status and metrics
*/
export async function finishAgentRun(
params: ParamsOf<FinishAgentRunFn>,
): ReturnType<FinishAgentRunFn> {
const {
userId,
runId,
status,
totalSteps,
directCredits,
totalCredits,
errorMessage,
logger,
} = params
if (userId === TEST_USER_ID) {
return
}
try {
await db
.update(schema.agentRun)
.set({
status,
completed_at: new Date(),
total_steps: totalSteps,
direct_credits: directCredits.toString(),
total_credits: totalCredits.toString(),
error_message: errorMessage,
})
.where(eq(schema.agentRun.id, runId))
} catch (error) {
logger.error({ error, runId, status }, 'Failed to finish agent run')
throw error
}
}
/**
* Adds a completed step to the agent_step table
*/
export async function addAgentStep(
params: ParamsOf<AddAgentStepFn>,
): ReturnType<AddAgentStepFn> {
const {
userId,
agentRunId,
stepNumber,
credits,
childRunIds,
messageId,
status = 'completed',
errorMessage,
startTime,
logger,
} = params
if (userId === TEST_USER_ID) {
return 'test-step-id'
}
const stepId = crypto.randomUUID()
try {
await db.insert(schema.agentStep).values({
id: stepId,
agent_run_id: agentRunId,
step_number: stepNumber,
status,
credits: credits?.toString(),
child_run_ids: childRunIds,
message_id: messageId,
error_message: errorMessage,
created_at: startTime,
completed_at: new Date(),
})
return stepId
} catch (error) {
logger.error({ error, agentRunId, stepNumber }, 'Failed to add agent step')
throw error
}
}