(
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,
)
| 4556 | } |
| 4557 | |
| 4558 | async function handleInitializeRequest( |
| 4559 | request: SDKControlInitializeRequest, |
| 4560 | requestId: string, |
| 4561 | initialized: boolean, |
| 4562 | output: Stream<StdoutMessage>, |
| 4563 | commands: Command[], |
| 4564 | modelInfos: ModelInfo[], |
| 4565 | structuredIO: StructuredIO, |
| 4566 | enableAuthStatus: boolean, |
| 4567 | options: { |
| 4568 | systemPrompt: string | undefined |
| 4569 | appendSystemPrompt: string | undefined |
| 4570 | agent?: string | undefined |
| 4571 | userSpecifiedModel?: string | undefined |
| 4572 | [key: string]: unknown |
| 4573 | }, |
| 4574 | agents: AgentDefinition[], |
| 4575 | getAppState: () => AppState, |
| 4576 | ): Promise<void> { |
| 4577 | if (initialized) { |
| 4578 | output.enqueue({ |
| 4579 | type: 'control_response', |
| 4580 | response: { |
| 4581 | subtype: 'error', |
| 4582 | error: 'Already initialized', |
| 4583 | request_id: requestId, |
| 4584 | pending_permission_requests: |
| 4585 | structuredIO.getPendingPermissionRequests(), |
| 4586 | }, |
| 4587 | }) |
| 4588 | return |
| 4589 | } |
| 4590 | |
| 4591 | // Apply systemPrompt/appendSystemPrompt from stdin to avoid ARG_MAX limits |
| 4592 | if (request.systemPrompt !== undefined) { |
| 4593 | options.systemPrompt = request.systemPrompt |
| 4594 | } |
| 4595 | if (request.appendSystemPrompt !== undefined) { |
| 4596 | options.appendSystemPrompt = request.appendSystemPrompt |
| 4597 | } |
| 4598 | if (request.promptSuggestions !== undefined) { |
| 4599 | options.promptSuggestions = request.promptSuggestions |
| 4600 | } |
| 4601 | |
| 4602 | // Merge agents from stdin to avoid ARG_MAX limits |
| 4603 | if (request.agents) { |
| 4604 | const stdinAgents = parseAgentsFromJson(request.agents, 'flagSettings') |
| 4605 | agents.push(...stdinAgents) |
| 4606 | } |
| 4607 | |
| 4608 | // Re-evaluate main thread agent after SDK agents are merged |
| 4609 | // This allows --agent to reference agents defined via SDK |
| 4610 | if (options.agent) { |
| 4611 | // If main.tsx already found this agent (filesystem-defined), it already |
| 4612 | // applied systemPrompt/model/initialPrompt. Skip to avoid double-apply. |
| 4613 | const alreadyResolved = getMainThreadAgentType() === options.agent |
| 4614 | const mainThreadAgent = agents.find(a => a.agentType === options.agent) |
| 4615 | if (mainThreadAgent && !alreadyResolved) { |
no test coverage detected