(
request: SDKControlInitializeRequest,
requestId: string,
initialized: boolean,
output: Stream<StdoutMessage>,
commands: Command[],
modelInfos: ModelInfo[],
structuredIO: StructuredIO,
enableAuthStatus: boolean,
options: {
systemPrompt: string | undefined
appendSystemPrompt: string | undefined
agent?: string | undefined
userSpecifiedModel?: string | undefined
[key: string]: unknown
},
agents: AgentDefinition[],
getAppState: () => AppState,
)
| 4334 | } |
| 4335 | |
| 4336 | async function handleInitializeRequest( |
| 4337 | request: SDKControlInitializeRequest, |
| 4338 | requestId: string, |
| 4339 | initialized: boolean, |
| 4340 | output: Stream<StdoutMessage>, |
| 4341 | commands: Command[], |
| 4342 | modelInfos: ModelInfo[], |
| 4343 | structuredIO: StructuredIO, |
| 4344 | enableAuthStatus: boolean, |
| 4345 | options: { |
| 4346 | systemPrompt: string | undefined |
| 4347 | appendSystemPrompt: string | undefined |
| 4348 | agent?: string | undefined |
| 4349 | userSpecifiedModel?: string | undefined |
| 4350 | [key: string]: unknown |
| 4351 | }, |
| 4352 | agents: AgentDefinition[], |
| 4353 | getAppState: () => AppState, |
| 4354 | ): Promise<void> { |
| 4355 | if (initialized) { |
| 4356 | output.enqueue({ |
| 4357 | type: 'control_response', |
| 4358 | response: { |
| 4359 | subtype: 'error', |
| 4360 | error: 'Already initialized', |
| 4361 | request_id: requestId, |
| 4362 | pending_permission_requests: |
| 4363 | structuredIO.getPendingPermissionRequests(), |
| 4364 | }, |
| 4365 | }) |
| 4366 | return |
| 4367 | } |
| 4368 | |
| 4369 | // Apply systemPrompt/appendSystemPrompt from stdin to avoid ARG_MAX limits |
| 4370 | if (request.systemPrompt !== undefined) { |
| 4371 | options.systemPrompt = request.systemPrompt |
| 4372 | } |
| 4373 | if (request.appendSystemPrompt !== undefined) { |
| 4374 | options.appendSystemPrompt = request.appendSystemPrompt |
| 4375 | } |
| 4376 | if (request.promptSuggestions !== undefined) { |
| 4377 | options.promptSuggestions = request.promptSuggestions |
| 4378 | } |
| 4379 | |
| 4380 | // Merge agents from stdin to avoid ARG_MAX limits |
| 4381 | if (request.agents) { |
| 4382 | const stdinAgents = parseAgentsFromJson(request.agents, 'flagSettings') |
| 4383 | agents.push(...stdinAgents) |
| 4384 | } |
| 4385 | |
| 4386 | // Re-evaluate main thread agent after SDK agents are merged |
| 4387 | // This allows --agent to reference agents defined via SDK |
| 4388 | if (options.agent) { |
| 4389 | // If main.tsx already found this agent (filesystem-defined), it already |
| 4390 | // applied systemPrompt/model/initialPrompt. Skip to avoid double-apply. |
| 4391 | const alreadyResolved = getMainThreadAgentType() === options.agent |
| 4392 | const mainThreadAgent = agents.find(a => a.agentType === options.agent) |
| 4393 | if (mainThreadAgent && !alreadyResolved) { |
no test coverage detected