Skip to content

Commit 17c2696

Browse files
committed
sdk: implement addAgentStep
1 parent 6306e0f commit 17c2696

File tree

3 files changed

+77
-23
lines changed

3 files changed

+77
-23
lines changed

common/src/types/contracts/database.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export type FinishAgentRunFn = (params: {
7777
}) => Promise<void>
7878

7979
export type AddAgentStepFn = (params: {
80+
apiKey: string
8081
userId: string | undefined
8182
agentRunId: string
8283
stepNumber: number
@@ -87,6 +88,6 @@ export type AddAgentStepFn = (params: {
8788
errorMessage?: string
8889
startTime: Date
8990
logger: Logger
90-
}) => Promise<string>
91+
}) => Promise<string | null>
9192

9293
export type DatabaseAgentCache = Map<string, AgentTemplate | null>

packages/agent-runtime/src/run-agent-step.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,20 @@ export async function loopAgentSteps(
450450
ParamsExcluding<
451451
FinishAgentRunFn,
452452
'runId' | 'status' | 'totalSteps' | 'directCredits' | 'totalCredits'
453+
> &
454+
ParamsExcluding<
455+
typeof runAgentStep,
456+
'agentState' | 'prompt' | 'spawnParams' | 'system'
457+
> &
458+
ParamsExcluding<
459+
AddAgentStepFn,
460+
| 'agentRunId'
461+
| 'stepNumber'
462+
| 'credits'
463+
| 'childRunIds'
464+
| 'messageId'
465+
| 'status'
466+
| 'startTime'
453467
>,
454468
): Promise<{
455469
agentState: AgentState
@@ -677,14 +691,6 @@ export async function loopAgentSteps(
677691
messageId,
678692
} = await runAgentStep({
679693
...params,
680-
userId,
681-
userInputId,
682-
clientSessionId,
683-
fingerprintId,
684-
onResponseChunk,
685-
localAgentTemplates,
686-
agentType,
687-
fileContext,
688694
agentState: currentAgentState,
689695
prompt: currentPrompt,
690696
spawnParams: currentParams,
@@ -693,15 +699,14 @@ export async function loopAgentSteps(
693699

694700
if (newAgentState.runId) {
695701
await addAgentStep({
696-
userId,
702+
...params,
697703
agentRunId: newAgentState.runId,
698704
stepNumber: totalSteps,
699705
credits: newAgentState.directCreditsUsed - creditsBefore,
700706
childRunIds: newAgentState.childRunIds.slice(childrenBefore),
701707
messageId,
702708
status: 'completed',
703709
startTime,
704-
logger,
705710
})
706711
} else {
707712
logger.error('No runId found for agent state after finishing agent run')

sdk/src/impl/database.ts

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getErrorObject } from '@codebuff/common/util/error'
33
import { WEBSITE_URL } from '../constants'
44

55
import type {
6+
AddAgentStepFn,
67
FetchAgentFromDatabaseFn,
78
FinishAgentRunFn,
89
GetUserInfoFromApiKeyInput,
@@ -155,15 +156,62 @@ export async function finishAgentRun(
155156
}
156157
}
157158

158-
console.log(
159-
await finishAgentRun({
160-
apiKey: '12345',
161-
status: 'completed',
162-
totalSteps: 5,
163-
userId: undefined,
164-
runId: 'e7a129b2-feb5-40ac-b2cd-0d7c115962f5',
165-
directCredits: 12,
166-
totalCredits: 34,
167-
logger: console,
168-
}),
169-
)
159+
export async function addAgentStep(
160+
params: ParamsOf<AddAgentStepFn>,
161+
): ReturnType<AddAgentStepFn> {
162+
const {
163+
apiKey,
164+
agentRunId,
165+
stepNumber,
166+
credits,
167+
childRunIds,
168+
messageId,
169+
status = 'completed',
170+
errorMessage,
171+
startTime,
172+
logger,
173+
} = params
174+
175+
const url = new URL(`/api/v1/agent-runs/${agentRunId}/steps`, WEBSITE_URL)
176+
177+
try {
178+
const response = await fetch(url, {
179+
method: 'POST',
180+
headers: {
181+
Authorization: `Bearer ${apiKey}`,
182+
},
183+
body: JSON.stringify({
184+
stepNumber,
185+
credits,
186+
childRunIds,
187+
messageId,
188+
status,
189+
errorMessage,
190+
startTime,
191+
}),
192+
})
193+
194+
if (!response.ok) {
195+
logger.error({ response }, 'addAgentStep request failed')
196+
return null
197+
}
198+
199+
return response.json()
200+
} catch (error) {
201+
logger.error(
202+
{
203+
error: getErrorObject(error),
204+
agentRunId,
205+
stepNumber,
206+
credits,
207+
childRunIds,
208+
messageId,
209+
status,
210+
errorMessage,
211+
startTime,
212+
},
213+
'addAgentStep error',
214+
)
215+
return null
216+
}
217+
}

0 commit comments

Comments
 (0)