MCPcopy
hub / github.com/claude-code-best/claude-code / restoreAgentFromSession

Function restoreAgentFromSession

src/utils/sessionRestore.ts:200–242  ·  view source on GitHub ↗
(
  agentSetting: string | undefined,
  currentAgentDefinition: AgentDefinition | undefined,
  agentDefinitions: AgentDefinitionsResult,
)

Source from the content-addressed store, hash-verified

198 * if no agent was restored.
199 */
200export function restoreAgentFromSession(
201 agentSetting: string | undefined,
202 currentAgentDefinition: AgentDefinition | undefined,
203 agentDefinitions: AgentDefinitionsResult,
204): {
205 agentDefinition: AgentDefinition | undefined
206 agentType: string | undefined
207} {
208 // If user already specified --agent on CLI, keep that definition
209 if (currentAgentDefinition) {
210 return { agentDefinition: currentAgentDefinition, agentType: undefined }
211 }
212
213 // If session had no agent, clear any stale bootstrap state
214 if (!agentSetting) {
215 setMainThreadAgentType(undefined)
216 return { agentDefinition: undefined, agentType: undefined }
217 }
218
219 const resumedAgent = agentDefinitions.activeAgents.find(
220 agent => agent.agentType === agentSetting,
221 )
222 if (!resumedAgent) {
223 logForDebugging(
224 `Resumed session had agent "${agentSetting}" but it is no longer available. Using default behavior.`,
225 )
226 setMainThreadAgentType(undefined)
227 return { agentDefinition: undefined, agentType: undefined }
228 }
229
230 setMainThreadAgentType(resumedAgent.agentType)
231
232 // Apply agent's model if user didn't specify one
233 if (
234 !getMainLoopModelOverride() &&
235 resumedAgent.model &&
236 resumedAgent.model !== 'inherit'
237 ) {
238 setMainLoopModelOverride(parseUserSpecifiedModel(resumedAgent.model))
239 }
240
241 return { agentDefinition: resumedAgent, agentType: resumedAgent.agentType }
242}
243
244/**
245 * Refresh agent definitions after a coordinator/normal mode switch.

Callers 4

onSelectFunction · 0.85
REPLFunction · 0.85
runHeadlessFunction · 0.85

Calls 5

setMainThreadAgentTypeFunction · 0.85
getMainLoopModelOverrideFunction · 0.85
setMainLoopModelOverrideFunction · 0.85
parseUserSpecifiedModelFunction · 0.85
logForDebuggingFunction · 0.70

Tested by

no test coverage detected