Skip to content

Commit 6f22913

Browse files
committed
change tool handlers to have required state fields
1 parent 4dfaa6b commit 6f22913

16 files changed

+135
-152
lines changed

packages/agent-runtime/src/tools/handlers/handler-function-type.ts

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,59 +23,61 @@ import type { ProjectFileContext } from '@codebuff/common/util/file'
2323
type PresentOrAbsent<K extends PropertyKey, V> =
2424
| { [P in K]: V }
2525
| { [P in K]: never }
26+
export type State = {
27+
agentStepId: string
28+
clientSessionId: string
29+
userInputId: string
30+
creditsUsed?: number | Promise<number>
31+
fingerprintId: string
32+
userId: string | undefined
33+
repoId: string | undefined
34+
agentTemplate: AgentTemplate
35+
localAgentTemplates: Record<string, AgentTemplate>
36+
sendSubagentChunk: SendSubagentChunkFn
37+
agentState: AgentState
38+
prompt: string | undefined
39+
fullResponse: string | undefined
40+
agentContext: Record<
41+
string,
42+
{
43+
logs: string[]
44+
objective?: string | undefined
45+
status?:
46+
| 'NOT_STARTED'
47+
| 'IN_PROGRESS'
48+
| 'COMPLETE'
49+
| 'ABORTED'
50+
| undefined
51+
plan?: string | undefined
52+
}
53+
>
54+
messages: Message[]
55+
system: string
56+
logger: Logger
57+
} & FileProcessingState
2658

2759
export type CodebuffToolHandlerFunction<T extends ToolName = ToolName> = (
2860
params: {
2961
previousToolCallFinished: Promise<void>
3062
toolCall: CodebuffToolCall<T>
3163

32-
runId: string
3364
agentStepId: string
65+
ancestorRunIds: string[]
66+
apiKey: string
3467
clientSessionId: string
35-
userInputId: string
36-
repoUrl: string | undefined
37-
repoId: string | undefined
3868
fileContext: ProjectFileContext
39-
apiKey: string
40-
69+
fullResponse: string
70+
repoId: string | undefined
71+
repoUrl: string | undefined
72+
runId: string
4173
signal: AbortSignal
74+
state: State
75+
userInputId: string
4276

43-
ancestorRunIds: string[]
44-
45-
fullResponse: string
4677
fetch: typeof globalThis.fetch
47-
48-
writeToClient: (chunk: string | PrintModeEvent) => void
78+
getLatestState: () => State
4979
trackEvent: TrackEventFn
50-
51-
getLatestState: () => any
52-
state: {
53-
creditsUsed?: number | Promise<number>
54-
fingerprintId: string
55-
userId: string | undefined
56-
repoId: string | undefined
57-
agentTemplate: AgentTemplate
58-
localAgentTemplates: Record<string, AgentTemplate>
59-
sendSubagentChunk: SendSubagentChunkFn
60-
agentState: AgentState
61-
agentContext: Record<
62-
string,
63-
{
64-
logs: string[]
65-
objective?: string | undefined
66-
status?:
67-
| 'NOT_STARTED'
68-
| 'IN_PROGRESS'
69-
| 'COMPLETE'
70-
| 'ABORTED'
71-
| undefined
72-
plan?: string | undefined
73-
}
74-
>
75-
messages: Message[]
76-
system: string
77-
logger: Logger
78-
} & FileProcessingState
80+
writeToClient: (chunk: string | PrintModeEvent) => void
7981
} & PresentOrAbsent<
8082
'requestClientToolCall',
8183
(
@@ -89,7 +91,6 @@ export type CodebuffToolHandlerFunction<T extends ToolName = ToolName> = (
8991
state?: Partial<
9092
{
9193
creditsUsed?: number | Promise<number>
92-
fingerprintId: string
9394
userId: string | undefined
9495
repoId: string | undefined
9596
agentTemplate: AgentTemplate

packages/agent-runtime/src/tools/handlers/tool/add-subgoal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type { Subgoal } from '@codebuff/common/types/session-state'
1010
export const handleAddSubgoal = ((params: {
1111
previousToolCallFinished: Promise<void>
1212
toolCall: CodebuffToolCall<'add_subgoal'>
13-
state: { agentContext?: Record<string, Subgoal> }
13+
state: { agentContext: Record<string, Subgoal> }
1414
}): {
1515
result: Promise<CodebuffToolOutput<'add_subgoal'>>
1616
state: { agentContext: Record<string, Subgoal> }

packages/agent-runtime/src/tools/handlers/tool/create-plan.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
33
import { getFileProcessingValues, postStreamProcessing } from './write-file'
44

55
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
6-
import type {
7-
FileProcessingState,
8-
OptionalFileProcessingState,
9-
} from './write-file'
6+
import type { FileProcessingState } from './write-file'
107
import type {
118
ClientToolCall,
129
CodebuffToolCall,
@@ -27,13 +24,13 @@ export const handleCreatePlan = ((params: {
2724

2825
getLatestState: () => FileProcessingState
2926
state: {
30-
agentStepId?: string
31-
clientSessionId?: string
32-
fingerprintId?: string
33-
userId?: string
34-
userInputId?: string
35-
repoId?: string
36-
} & OptionalFileProcessingState
27+
agentStepId: string
28+
clientSessionId: string
29+
fingerprintId: string
30+
userId: string | undefined
31+
userInputId: string
32+
repoId: string | undefined
33+
} & FileProcessingState
3734
}): {
3835
result: Promise<CodebuffToolOutput<'create_plan'>>
3936
state: FileProcessingState

packages/agent-runtime/src/tools/handlers/tool/find-files.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ export const handleFindFiles = ((
3737
userInputId: string
3838

3939
state: {
40-
fingerprintId?: string
41-
userId?: string
42-
repoId?: string
43-
messages?: Message[]
40+
fingerprintId: string
41+
userId: string | undefined
42+
repoId: string | undefined
43+
messages: Message[]
4444
}
4545
} & ParamsExcluding<
4646
typeof requestRelevantFiles,

packages/agent-runtime/src/tools/handlers/tool/read-docs.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { fetchContext7LibraryDocumentation } from '../../../llm-api/context7-api'
21
import { callDocsSearchAPI } from '../../../llm-api/codebuff-web-api'
32

3+
import type { fetchContext7LibraryDocumentation } from '../../../llm-api/context7-api'
44
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
55
import type {
66
CodebuffToolCall,
@@ -20,9 +20,9 @@ export const handleReadDocs = ((
2020
userInputId: string
2121

2222
state: {
23-
userId?: string
24-
fingerprintId?: string
25-
repoId?: string
23+
fingerprintId: string
24+
userId: string | undefined
25+
repoId: string | undefined
2626
}
2727
} & ParamsExcluding<
2828
typeof fetchContext7LibraryDocumentation,
@@ -43,7 +43,7 @@ export const handleReadDocs = ((
4343
fetch,
4444
} = params
4545
const { libraryTitle, topic, max_tokens } = toolCall.input
46-
const { userId, fingerprintId, repoId } = state
46+
const { userId, repoId, fingerprintId } = state
4747

4848
const docsStartTime = Date.now()
4949
const docsContext = {

packages/agent-runtime/src/tools/handlers/tool/read-files.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ export const handleReadFiles = ((
2020
fileContext: ProjectFileContext
2121

2222
state: {
23-
userId?: string
24-
fingerprintId?: string
25-
repoId?: string
26-
messages?: Message[]
23+
userId: string | undefined
24+
fingerprintId: string
25+
repoId: string | undefined
26+
messages: Message[]
2727
}
2828
} & ParamsExcluding<typeof getFileReadingUpdates, 'requestedFiles'>,
2929
): {

packages/agent-runtime/src/tools/handlers/tool/set-output.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
import { getAgentTemplate } from '../../../templates/agent-registry'
22

33
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
4+
import type { CodebuffToolCall } from '@codebuff/common/tools/list'
5+
import type {
6+
AgentTemplate,
7+
Logger,
8+
} from '@codebuff/common/types/agent-template'
9+
import type { FetchAgentFromDatabaseFn } from '@codebuff/common/types/contracts/database'
10+
import type { AgentState } from '@codebuff/common/types/session-state'
411

512
type ToolName = 'set_output'
6-
export const handleSetOutput: CodebuffToolHandlerFunction<ToolName> = (
7-
params,
8-
) => {
13+
export const handleSetOutput = ((params: {
14+
previousToolCallFinished: Promise<void>
15+
toolCall: CodebuffToolCall<ToolName>
16+
state: {
17+
agentState: AgentState
18+
localAgentTemplates: Record<string, AgentTemplate>
19+
}
20+
fetchAgentFromDatabase: FetchAgentFromDatabaseFn
21+
databaseAgentCache: Map<string, AgentTemplate | null>
22+
logger: Logger
23+
apiKey: string
24+
}) => {
925
const { previousToolCallFinished, toolCall, state, logger } = params
1026
const output = toolCall.input
1127
const { agentState, localAgentTemplates } = state
@@ -70,4 +86,4 @@ export const handleSetOutput: CodebuffToolHandlerFunction<ToolName> = (
7086
})(),
7187
state: { agentState: agentState },
7288
}
73-
}
89+
}) satisfies CodebuffToolHandlerFunction<ToolName>

packages/agent-runtime/src/tools/handlers/tool/spawn-agent-inline.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ export const handleSpawnAgentInline = ((
3232

3333
getLatestState: () => { messages: Message[] }
3434
state: {
35-
fingerprintId?: string
36-
userId?: string
37-
agentTemplate?: AgentTemplate
38-
localAgentTemplates?: Record<string, AgentTemplate>
39-
messages?: Message[]
40-
agentState?: AgentState
41-
system?: string
35+
fingerprintId: string
36+
userId: string | undefined
37+
agentTemplate: AgentTemplate
38+
localAgentTemplates: Record<string, AgentTemplate>
39+
messages: Message[]
40+
agentState: AgentState
41+
system: string
4242
}
4343
logger: Logger
4444
} & ParamsExcluding<

packages/agent-runtime/src/tools/handlers/tool/spawn-agent-utils.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ export interface SpawnAgentParams {
2727
}
2828

2929
export interface BaseSpawnState {
30-
fingerprintId?: string
31-
userId?: string
32-
agentTemplate?: AgentTemplate
33-
localAgentTemplates?: Record<string, AgentTemplate>
34-
messages?: Message[]
35-
agentState?: AgentState
36-
system?: string
30+
fingerprintId: string
31+
userId: string | undefined
32+
agentTemplate: AgentTemplate
33+
localAgentTemplates: Record<string, AgentTemplate>
34+
messages: Message[]
35+
agentState: AgentState
36+
system: string
3737
}
3838

3939
export interface SpawnContext {

packages/agent-runtime/src/tools/handlers/tool/spawn-agents.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ export const handleSpawnAgents = ((
3939

4040
getLatestState: () => { messages: Message[] }
4141
state: {
42-
fingerprintId?: string
43-
userId?: string
44-
agentTemplate?: AgentTemplate
45-
localAgentTemplates?: Record<string, AgentTemplate>
46-
sendSubagentChunk?: SendSubagentChunk
47-
messages?: Message[]
48-
agentState?: AgentState
49-
system?: string
42+
fingerprintId: string
43+
userId: string | undefined
44+
agentTemplate: AgentTemplate
45+
localAgentTemplates: Record<string, AgentTemplate>
46+
sendSubagentChunk: SendSubagentChunk
47+
messages: Message[]
48+
agentState: AgentState
49+
system: string
5050
}
5151
logger: Logger
5252
} & ParamsExcluding<

0 commit comments

Comments
 (0)