MCPcopy
hub / github.com/codeaashu/claude-code / handleSpawnInProcess

Function handleSpawnInProcess

src/tools/shared/spawnMultiAgent.ts:840–1032  ·  view source on GitHub ↗

* Handle spawn operation for in-process teammates. * In-process teammates run in the same Node.js process using AsyncLocalStorage.

(
  input: SpawnInput,
  context: ToolUseContext,
)

Source from the content-addressed store, hash-verified

838 * In-process teammates run in the same Node.js process using AsyncLocalStorage.
839 */
840async function handleSpawnInProcess(
841 input: SpawnInput,
842 context: ToolUseContext,
843): Promise<{ data: SpawnOutput }> {
844 const { setAppState, getAppState } = context
845 const { name, prompt, agent_type, plan_mode_required } = input
846
847 // Resolve model: 'inherit' → leader's model; undefined → default Opus
848 const model = resolveTeammateModel(input.model, getAppState().mainLoopModel)
849
850 if (!name || !prompt) {
851 throw new Error('name and prompt are required for spawn operation')
852 }
853
854 // Get team name from input or inherit from leader's team context
855 const appState = getAppState()
856 const teamName = input.team_name || appState.teamContext?.teamName
857
858 if (!teamName) {
859 throw new Error(
860 'team_name is required for spawn operation. Either provide team_name in input or call spawnTeam first to establish team context.',
861 )
862 }
863
864 // Generate unique name if duplicate exists in team
865 const uniqueName = await generateUniqueTeammateName(name, teamName)
866
867 // Sanitize the name to prevent @ in agent IDs
868 const sanitizedName = sanitizeAgentName(uniqueName)
869
870 // Generate deterministic agent ID from name and team
871 const teammateId = formatAgentId(sanitizedName, teamName)
872
873 // Assign a unique color to this teammate
874 const teammateColor = assignTeammateColor(teammateId)
875
876 // Look up custom agent definition if agent_type is provided
877 let agentDefinition: CustomAgentDefinition | undefined
878 if (agent_type) {
879 const allAgents = context.options.agentDefinitions.activeAgents
880 const foundAgent = allAgents.find(a => a.agentType === agent_type)
881 if (foundAgent && isCustomAgent(foundAgent)) {
882 agentDefinition = foundAgent
883 }
884 logForDebugging(
885 `[handleSpawnInProcess] agent_type=${agent_type}, found=${!!agentDefinition}`,
886 )
887 }
888
889 // Spawn in-process teammate
890 const config: InProcessSpawnConfig = {
891 name: sanitizedName,
892 teamName,
893 prompt,
894 color: teammateColor,
895 planModeRequired: plan_mode_required ?? false,
896 model,
897 }

Callers 1

handleSpawnFunction · 0.85

Calls 14

resolveTeammateModelFunction · 0.85
sanitizeAgentNameFunction · 0.85
formatAgentIdFunction · 0.85
assignTeammateColorFunction · 0.85
isCustomAgentFunction · 0.85
logForDebuggingFunction · 0.85
spawnInProcessTeammateFunction · 0.85
startInProcessTeammateFunction · 0.85
getCwdFunction · 0.85
readTeamFileAsyncFunction · 0.85
writeTeamFileAsyncFunction · 0.85

Tested by

no test coverage detected