@deepseek-ai/dsh-tool-subagent 0.2.2-rc.5 → 0.2.3-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,8 +1,11 @@
1
1
  /** Child LLM route selection for the subagent tool. */
2
2
 
3
- import { ReasoningEffortId } from '@deepseek-ai/dsh-llm'
3
+ import { brandString } from '@deepseek-ai/dsh-brand'
4
+ import type { ReasoningEffortId } from '@deepseek-ai/dsh-llm/types'
4
5
  import type { LlmRuntime } from '@deepseek-ai/dsh-llm'
5
6
  import type { AgentOptions } from '@deepseek-ai/dsh-agent'
7
+ import { SubagentError } from '@deepseek-ai/dsh-subagent'
8
+ import { ToolArgsError, ToolContextUnavailableError } from '@deepseek-ai/dsh-tools'
6
9
  import z from '@deepseek-ai/schemastery'
7
10
 
8
11
  /** One exact child LLM route authorized by a user setting. */
@@ -19,7 +22,7 @@ export const AllowedModelRouteSchema: z<AllowedModelRoute> = z.object({
19
22
  model: z.string().min(1).required(),
20
23
  })
21
24
 
22
- /** Exact routes authorized for one Session. */
25
+ /** Exact routes the Host setting currently authorizes. */
23
26
  export interface ModelSelectionPolicy {
24
27
  /** Exact provider/model routes authorized for explicit selection. */
25
28
  readonly routes: readonly AllowedModelRoute[]
@@ -38,10 +41,11 @@ export function modelRouteKey(route: AllowedModelRoute): string {
38
41
  * Reject malformed or duplicate route policy entries at a durable or configuration boundary.
39
42
  * @param routes - Candidate exact routes to validate.
40
43
  * @returns an assertion that the candidate is a validated exact-route array.
44
+ * @throws {SubagentError} `INVALID_MODEL_SELECTION_SETTINGS` for malformed or repeated routes.
41
45
  */
42
46
  export function assertAllowedModelRoutes(routes: unknown): asserts routes is readonly AllowedModelRoute[] {
43
47
  if (!Array.isArray(routes)) {
44
- throw new Error('subagent model selection requires an array of routes')
48
+ throw new SubagentError('subagent model selection requires an array of routes', 'INVALID_MODEL_SELECTION_SETTINGS')
45
49
  }
46
50
  const seen = new Set<string>()
47
51
  const candidates: readonly unknown[] = routes
@@ -50,12 +54,12 @@ export function assertAllowedModelRoutes(routes: unknown): asserts routes is rea
50
54
  || !('provider' in candidate) || typeof candidate.provider !== 'string'
51
55
  || !('model' in candidate) || typeof candidate.model !== 'string'
52
56
  || candidate.provider.length === 0 || candidate.model.length === 0) {
53
- throw new Error('subagent model selection requires non-empty provider and model ids')
57
+ throw new SubagentError('subagent model selection requires non-empty provider and model ids', 'INVALID_MODEL_SELECTION_SETTINGS')
54
58
  }
55
59
  const route = { provider: candidate.provider, model: candidate.model }
56
60
  const key = modelRouteKey(route)
57
61
  if (seen.has(key)) {
58
- throw new Error(`subagent model selection repeats route "${route.provider}/${route.model}"`)
62
+ throw new SubagentError(`subagent model selection repeats route "${route.provider}/${route.model}"`, 'INVALID_MODEL_SELECTION_SETTINGS')
59
63
  }
60
64
  seen.add(key)
61
65
  }
@@ -68,9 +72,32 @@ export interface DelegationModelRequest {
68
72
  readonly reasoning_effort?: string
69
73
  }
70
74
 
75
+ /**
76
+ * Whether an optional string tool argument carries a value. Some models send
77
+ * `""` for optional fields they leave unset.
78
+ * @param value - Parsed argument value.
79
+ * @returns Whether the value is present and non-empty.
80
+ */
81
+ export function hasText(value: string | undefined): value is string {
82
+ return value !== undefined && value !== ''
83
+ }
84
+
85
+ /**
86
+ * Read model-facing route fields from tool arguments, treating an empty string as an omitted field.
87
+ * @param args - Parsed tool arguments carrying the route fields.
88
+ * @returns The non-empty route fields.
89
+ */
90
+ export function delegationModelRequest(args: DelegationModelRequest): DelegationModelRequest {
91
+ return {
92
+ ...hasText(args.provider) ? { provider: args.provider } : {},
93
+ ...hasText(args.model) ? { model: args.model } : {},
94
+ ...hasText(args.reasoning_effort) ? { reasoning_effort: args.reasoning_effort } : {},
95
+ }
96
+ }
97
+
71
98
  /**
72
99
  * Whether a call explicitly selects any child LLM value.
73
- * @param request - Model-facing route fields from the tool call.
100
+ * @param request - Route fields read by {@link delegationModelRequest}.
74
101
  * @returns Whether at least one route or effort field is present.
75
102
  */
76
103
  export function hasDelegationModelRequest(request: DelegationModelRequest): boolean {
@@ -79,23 +106,23 @@ export function hasDelegationModelRequest(request: DelegationModelRequest): bool
79
106
  || request.reasoning_effort !== undefined
80
107
  }
81
108
 
82
- /** Reject an empty model-facing route value at the tool JSON boundary. */
83
- function assertNonEmpty(value: string | undefined, field: keyof DelegationModelRequest): void {
84
- if (value !== undefined && value.length === 0) {
85
- throw new Error(`child LLM \`${field}\` must be non-empty`)
86
- }
87
- }
109
+ /** Model-facing pointer to route discovery, appended to route-selection rejections. */
110
+ const ROUTE_DISCOVERY_HINT = ' Call `list_subagent_models` to see the routes you may select.'
88
111
 
89
112
  /**
90
- * Validate explicit selection against the Session policy and merge it over child defaults.
113
+ * Validate explicit selection against the current Host setting and merge it over child defaults.
91
114
  * Explicit selection requires both provider and model; an omitted effort then
92
115
  * uses the selected model's default, even when the route is unchanged.
93
116
  * @param configured - Tool-instance child defaults.
94
- * @param request - Model-facing route override.
117
+ * @param request - Route fields read by {@link delegationModelRequest}.
95
118
  * @param enabled - Whether this tool instance permits model-facing selection.
96
- * @param policy - Session allowlist; omission permits only the current parent route.
97
- * @param parentOptions - Current parent route used when no policy is recorded.
119
+ * @param policy - Current Host allowlist; omission permits only the current parent route.
120
+ * @param parentOptions - Current parent route used when the Host setting is disabled or absent.
121
+ * @param routeDiscoveryVisible - Whether the caller can call `list_subagent_models`; rejections then point to it.
98
122
  * @returns Child Agent options, preserving omission when no layer contributes one.
123
+ * @throws {SubagentError} `MODEL_SELECTION_DISABLED` when this tool instance
124
+ * forbids selection, or `MODEL_ROUTE_NOT_ALLOWED` when the allowlist excludes the route.
125
+ * @throws {ToolArgsError} when `provider` and `model` are not supplied together.
99
126
  */
100
127
  export function requestedAgentOptions(
101
128
  configured: AgentOptions | undefined,
@@ -103,50 +130,53 @@ export function requestedAgentOptions(
103
130
  enabled: boolean,
104
131
  policy: ModelSelectionPolicy | undefined,
105
132
  parentOptions: AgentOptions,
133
+ routeDiscoveryVisible: boolean,
106
134
  ): AgentOptions | undefined {
107
135
  if (!hasDelegationModelRequest(request)) return configured
108
136
  if (!enabled) {
109
- throw new Error('child model selection is disabled for this tool instance')
137
+ throw new SubagentError('child model selection is disabled for this tool instance', 'MODEL_SELECTION_DISABLED')
110
138
  }
111
- assertNonEmpty(request.provider, 'provider')
112
- assertNonEmpty(request.model, 'model')
113
- assertNonEmpty(request.reasoning_effort, 'reasoning_effort')
139
+ const hint = routeDiscoveryVisible ? ROUTE_DISCOVERY_HINT : ''
114
140
  if (request.provider === undefined || request.model === undefined) {
115
- throw new Error(request.reasoning_effort === undefined
116
- ? 'child LLM `provider` and `model` must be supplied together'
117
- : 'child LLM `reasoning_effort` requires both `provider` and `model`; supply both or omit all three fields')
141
+ throw new ToolArgsError([(request.reasoning_effort === undefined
142
+ ? 'child LLM `provider` and `model` must be supplied together.'
143
+ : 'child LLM `reasoning_effort` requires both `provider` and `model`; supply both or omit all three fields.') + hint])
118
144
  }
119
145
 
120
- assertAllowedModelSelection(policy, parentOptions, { provider: request.provider, model: request.model })
146
+ assertAllowedModelSelection(policy, parentOptions, { provider: request.provider, model: request.model }, routeDiscoveryVisible)
121
147
  return {
122
148
  ...configured,
123
149
  provider: request.provider,
124
150
  model: request.model,
125
151
  // Explicit undefined also clears compatible parent effort during child-option merging.
126
- reasoningEffort: request.reasoning_effort === undefined ? undefined : ReasoningEffortId(request.reasoning_effort),
152
+ reasoningEffort: request.reasoning_effort === undefined ? undefined : brandString<ReasoningEffortId>(request.reasoning_effort),
127
153
  }
128
154
  }
129
155
 
130
156
  /**
131
- * Enforce Session authorization for a complete, explicitly selected child route.
132
- * @param policy - Session allowlist; omission permits only the current parent route.
133
- * @param parentOptions - Current parent route used when no policy is recorded.
157
+ * Enforce Host authorization for a complete, explicitly selected child route.
158
+ * @param policy - Current Host allowlist; omission permits only the current parent route.
159
+ * @param parentOptions - Current parent route used when the Host setting is disabled or absent.
134
160
  * @param route - Validated provider/model pair from the tool call.
161
+ * @param routeDiscoveryVisible - Whether the caller can call `list_subagent_models`; the rejection then points to it.
162
+ * @throws {SubagentError} `MODEL_ROUTE_NOT_ALLOWED` when the allowlist excludes the route.
135
163
  */
136
164
  export function assertAllowedModelSelection(
137
165
  policy: ModelSelectionPolicy | undefined,
138
166
  parentOptions: AgentOptions,
139
167
  route: AllowedModelRoute,
168
+ routeDiscoveryVisible: boolean,
140
169
  ): void {
141
170
  const { provider, model } = route
142
171
  const allowed = policy === undefined
143
172
  ? provider === parentOptions.provider && model === parentOptions.model
144
173
  : policy.routes.some(route => route.provider === provider && route.model === model)
145
174
  if (allowed) return
146
- throw new Error(`child LLM route "${provider}/${model}" is not allowed for this Session`
175
+ throw new SubagentError(`child LLM route "${provider}/${model}" is not allowed by the current subagent model selection setting`
147
176
  + (policy === undefined
148
- ? '; without a recorded model-selection policy, explicit selection is limited to the parent route'
149
- : ''))
177
+ ? '; while subagent model selection is disabled, explicit selection is limited to the parent route'
178
+ : '')
179
+ + '.' + (routeDiscoveryVisible ? ROUTE_DISCOVERY_HINT : ''), 'MODEL_ROUTE_NOT_ALLOWED')
150
180
  }
151
181
 
152
182
  /**
@@ -180,7 +210,7 @@ export async function preflightChildLlmRoute(
180
210
  const provider = requested?.provider ?? parentOptions.provider
181
211
  const model = requested?.model ?? parentOptions.model
182
212
  if (provider === undefined || model === undefined) {
183
- throw new Error('cannot select child LLM values without an effective provider and model')
213
+ throw new ToolContextUnavailableError('cannot select child LLM values without an effective provider and model')
184
214
  }
185
215
  const routeChanged = provider !== parentOptions.provider || model !== parentOptions.model
186
216
  const reasoningEffort = requested?.reasoningEffort
@@ -1,4 +1,5 @@
1
1
  /** Deployment-owned preset choices for one delegation tool. */
2
+ import { SubagentError } from '@deepseek-ai/dsh-subagent'
2
3
  import type { SubagentProvider } from '@deepseek-ai/dsh-subagent'
3
4
 
4
5
  /** Optional default and model-selectable preset ids for a spawn tool. */
@@ -59,13 +60,17 @@ export class SubagentPresetSelection {
59
60
  * Validate model input and apply the deployment default.
60
61
  * @param request - Model-supplied tool arguments.
61
62
  * @returns Selected preset id, or omission to inherit the parent.
63
+ * @throws {SubagentError} `PRESET_SELECTION_DISABLED` when this tool exposes no
64
+ * preset choice, or `PRESET_NOT_ALLOWED` when the id is outside the allowlist.
62
65
  */
63
66
  resolve(request: PresetSelectionRequest): string | undefined {
64
67
  const id = request.agent_preset
65
68
  if (id === undefined) return this.config.agentPreset
66
- if (this.choices === undefined) throw new Error('agent_preset selection is disabled for this tool instance')
69
+ if (this.choices === undefined) {
70
+ throw new SubagentError('agent_preset selection is disabled for this tool instance', 'PRESET_SELECTION_DISABLED')
71
+ }
67
72
  if (typeof id !== 'string' || !this.choices.includes(id)) {
68
- throw new Error(`agent_preset must be one of: ${this.choices.join(', ')}`)
73
+ throw new SubagentError(`agent_preset must be one of: ${this.choices.join(', ')}`, 'PRESET_NOT_ALLOWED')
69
74
  }
70
75
  return id
71
76
  }