@@ -7,6 +7,10 @@ import { getAgentTemplate } from '../../../templates/agent-registry'
77import { filterUnfinishedToolCalls , withSystemTags } from '../../../util/messages'
88
99import type { AgentTemplate } from '@codebuff/common/types/agent-template'
10+ import type {
11+ AgentRuntimeDeps ,
12+ AgentRuntimeScopedDeps ,
13+ } from '@codebuff/common/types/contracts/agent-runtime'
1014import type { Logger } from '@codebuff/common/types/contracts/logger'
1115import type {
1216 ParamsExcluding ,
@@ -19,8 +23,80 @@ import type {
1923 AgentTemplateType ,
2024 Subgoal ,
2125} from '@codebuff/common/types/session-state'
26+ import type { ProjectFileContext } from '@codebuff/common/util/file'
2227import { Message } from '@codebuff/common/types/messages/codebuff-message'
2328
29+ /**
30+ * Common context params needed for spawning subagents.
31+ * These are the params that don't change between different spawn calls
32+ * and are passed through from the parent agent runtime.
33+ */
34+ export type SubagentContextParams = AgentRuntimeDeps &
35+ AgentRuntimeScopedDeps & {
36+ clientSessionId : string
37+ fileContext : ProjectFileContext
38+ localAgentTemplates : Record < string , AgentTemplate >
39+ repoId : string | undefined
40+ repoUrl : string | undefined
41+ signal : AbortSignal
42+ userId : string | undefined
43+ }
44+
45+ /**
46+ * Extracts the common context params needed for spawning subagents.
47+ * This avoids bugs from spreading all params with `...params` which can
48+ * accidentally pass through params that should be overridden.
49+ */
50+ export function extractSubagentContextParams (
51+ params : SubagentContextParams ,
52+ ) : SubagentContextParams {
53+ return {
54+ // AgentRuntimeDeps - Environment
55+ clientEnv : params . clientEnv ,
56+ ciEnv : params . ciEnv ,
57+ // AgentRuntimeDeps - Database
58+ getUserInfoFromApiKey : params . getUserInfoFromApiKey ,
59+ fetchAgentFromDatabase : params . fetchAgentFromDatabase ,
60+ startAgentRun : params . startAgentRun ,
61+ finishAgentRun : params . finishAgentRun ,
62+ addAgentStep : params . addAgentStep ,
63+ // AgentRuntimeDeps - Billing
64+ consumeCreditsWithFallback : params . consumeCreditsWithFallback ,
65+ // AgentRuntimeDeps - LLM
66+ promptAiSdkStream : params . promptAiSdkStream ,
67+ promptAiSdk : params . promptAiSdk ,
68+ promptAiSdkStructured : params . promptAiSdkStructured ,
69+ // AgentRuntimeDeps - Mutable State
70+ databaseAgentCache : params . databaseAgentCache ,
71+ liveUserInputRecord : params . liveUserInputRecord ,
72+ sessionConnections : params . sessionConnections ,
73+ // AgentRuntimeDeps - Analytics
74+ trackEvent : params . trackEvent ,
75+ // AgentRuntimeDeps - Other
76+ logger : params . logger ,
77+ fetch : params . fetch ,
78+
79+ // AgentRuntimeScopedDeps - Client (WebSocket)
80+ handleStepsLogChunk : params . handleStepsLogChunk ,
81+ requestToolCall : params . requestToolCall ,
82+ requestMcpToolData : params . requestMcpToolData ,
83+ requestFiles : params . requestFiles ,
84+ requestOptionalFile : params . requestOptionalFile ,
85+ sendAction : params . sendAction ,
86+ sendSubagentChunk : params . sendSubagentChunk ,
87+ apiKey : params . apiKey ,
88+
89+ // Core context params
90+ clientSessionId : params . clientSessionId ,
91+ fileContext : params . fileContext ,
92+ localAgentTemplates : params . localAgentTemplates ,
93+ repoId : params . repoId ,
94+ repoUrl : params . repoUrl ,
95+ signal : params . signal ,
96+ userId : params . userId ,
97+ }
98+ }
99+
24100/**
25101 * Checks if a parent agent is allowed to spawn a child agent
26102 */
0 commit comments