forked from sanbuphy/learn-coding-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ts
More file actions
157 lines (144 loc) · 5.45 KB
/
agent.ts
File metadata and controls
157 lines (144 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import type { PermissionMode } from '../permissions/PermissionMode.js'
import { capitalize } from '../stringUtils.js'
import { MODEL_ALIASES, type ModelAlias } from './aliases.js'
import { applyBedrockRegionPrefix, getBedrockRegionPrefix } from './bedrock.js'
import {
getCanonicalName,
getRuntimeMainLoopModel,
parseUserSpecifiedModel,
} from './model.js'
import { getAPIProvider } from './providers.js'
export const AGENT_MODEL_OPTIONS = [...MODEL_ALIASES, 'inherit'] as const
export type AgentModelAlias = (typeof AGENT_MODEL_OPTIONS)[number]
export type AgentModelOption = {
value: AgentModelAlias
label: string
description: string
}
/**
* Get the default subagent model. Returns 'inherit' so subagents inherit
* the model from the parent thread.
*/
export function getDefaultSubagentModel(): string {
return 'inherit'
}
/**
* Get the effective model string for an agent.
*
* For Bedrock, if the parent model uses a cross-region inference prefix (e.g., "eu.", "us."),
* that prefix is inherited by subagents using alias models (e.g., "sonnet", "haiku", "opus").
* This ensures subagents use the same region as the parent, which is necessary when
* IAM permissions are scoped to specific cross-region inference profiles.
*/
export function getAgentModel(
agentModel: string | undefined,
parentModel: string,
toolSpecifiedModel?: ModelAlias,
permissionMode?: PermissionMode,
): string {
if (process.env.CLAUDE_CODE_SUBAGENT_MODEL) {
return parseUserSpecifiedModel(process.env.CLAUDE_CODE_SUBAGENT_MODEL)
}
// Extract Bedrock region prefix from parent model to inherit for subagents.
// This ensures subagents use the same cross-region inference profile (e.g., "eu.", "us.")
// as the parent, which is required when IAM permissions only allow specific regions.
const parentRegionPrefix = getBedrockRegionPrefix(parentModel)
// Helper to apply parent region prefix for Bedrock models.
// `originalSpec` is the raw model string before resolution (alias or full ID).
// If the user explicitly specified a full model ID that already carries its own
// region prefix (e.g., "eu.anthropic.…"), we preserve it instead of overwriting
// with the parent's prefix. This prevents silent data-residency violations when
// an agent config intentionally pins to a different region than the parent.
const applyParentRegionPrefix = (
resolvedModel: string,
originalSpec: string,
): string => {
if (parentRegionPrefix && getAPIProvider() === 'bedrock') {
if (getBedrockRegionPrefix(originalSpec)) return resolvedModel
return applyBedrockRegionPrefix(resolvedModel, parentRegionPrefix)
}
return resolvedModel
}
// Prioritize tool-specified model if provided
if (toolSpecifiedModel) {
if (aliasMatchesParentTier(toolSpecifiedModel, parentModel)) {
return parentModel
}
const model = parseUserSpecifiedModel(toolSpecifiedModel)
return applyParentRegionPrefix(model, toolSpecifiedModel)
}
const agentModelWithExp = agentModel ?? getDefaultSubagentModel()
if (agentModelWithExp === 'inherit') {
// Apply runtime model resolution for inherit to get the effective model
// This ensures agents using 'inherit' get opusplan→Opus resolution in plan mode
return getRuntimeMainLoopModel({
permissionMode: permissionMode ?? 'default',
mainLoopModel: parentModel,
exceeds200kTokens: false,
})
}
if (aliasMatchesParentTier(agentModelWithExp, parentModel)) {
return parentModel
}
const model = parseUserSpecifiedModel(agentModelWithExp)
return applyParentRegionPrefix(model, agentModelWithExp)
}
/**
* Check if a bare family alias (opus/sonnet/haiku) matches the parent model's
* tier. When it does, the subagent inherits the parent's exact model string
* instead of resolving the alias to a provider default.
*
* Prevents surprising downgrades: a Vertex user on Opus 4.6 (via /model) who
* spawns a subagent with `model: opus` should get Opus 4.6, not whatever
* getDefaultOpusModel() returns for 3P.
* See https://github.com/anthropics/claude-code/issues/30815.
*
* Only bare family aliases match. `opus[1m]`, `best`, `opusplan` fall through
* since they carry semantics beyond "same tier as parent".
*/
function aliasMatchesParentTier(alias: string, parentModel: string): boolean {
const canonical = getCanonicalName(parentModel)
switch (alias.toLowerCase()) {
case 'opus':
return canonical.includes('opus')
case 'sonnet':
return canonical.includes('sonnet')
case 'haiku':
return canonical.includes('haiku')
default:
return false
}
}
export function getAgentModelDisplay(model: string | undefined): string {
// When model is omitted, getDefaultSubagentModel() returns 'inherit' at runtime
if (!model) return 'Inherit from parent (default)'
if (model === 'inherit') return 'Inherit from parent'
return capitalize(model)
}
/**
* Get available model options for agents
*/
export function getAgentModelOptions(): AgentModelOption[] {
return [
{
value: 'sonnet',
label: 'Sonnet',
description: 'Balanced performance - best for most agents',
},
{
value: 'opus',
label: 'Opus',
description: 'Most capable for complex reasoning tasks',
},
{
value: 'haiku',
label: 'Haiku',
description: 'Fast and efficient for simple tasks',
},
{
value: 'inherit',
label: 'Inherit from parent',
description: 'Use the same model as the main conversation',
},
]
}