@deepseek-ai/dsh-tool-subagent 0.2.2-alpha.3 → 0.2.2-rc.2
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.
- package/README.i18n.yaml +21 -21
- package/README.md +15 -13
- package/README.zh.md +14 -12
- package/lib/index.js +50 -47
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/lib/types/list-models.d.ts.map +1 -0
- package/lib/types/list-models.js.map +1 -0
- package/lib/types/model-selection-settings.d.ts.map +1 -0
- package/lib/types/model-selection-settings.js.map +1 -0
- package/lib/types/model-selection-state.d.ts.map +1 -0
- package/lib/types/model-selection-state.js.map +1 -0
- package/lib/types/model-selection.d.ts +11 -13
- package/lib/types/model-selection.d.ts.map +1 -0
- package/lib/types/model-selection.js.map +1 -0
- package/lib/types/preset-selection.d.ts.map +1 -0
- package/lib/types/preset-selection.js.map +1 -0
- package/package.json +31 -28
- package/src/index.ts +393 -0
- package/src/list-models.ts +136 -0
- package/src/model-selection-settings.ts +95 -0
- package/src/model-selection-state.ts +60 -0
- package/src/model-selection.ts +193 -0
- package/src/preset-selection.ts +72 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/** Model-facing discovery of LLM routes available to child Agents. */
|
|
2
|
+
|
|
3
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
4
|
+
import type LlmRuntime from '@deepseek-ai/dsh-llm'
|
|
5
|
+
import type { LlmProviderInfo } from '@deepseek-ai/dsh-llm'
|
|
6
|
+
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
7
|
+
import { parentAgentOptionsForDelegation } from '@deepseek-ai/dsh-subagent'
|
|
8
|
+
import type { ToolRunContext } from '@deepseek-ai/dsh-tools'
|
|
9
|
+
import type { ProjectionHandle } from '@deepseek-ai/dsh-session-projection'
|
|
10
|
+
import type { AllowedModelRoute, ModelSelectionPolicy } from './model-selection.ts'
|
|
11
|
+
import { subagentModelSelectionPolicy } from './model-selection-state.ts'
|
|
12
|
+
|
|
13
|
+
interface ListSubagentModelsRequest {
|
|
14
|
+
readonly provider?: string
|
|
15
|
+
readonly model?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Resolve one registered provider with a model-correctable diagnostic. */
|
|
19
|
+
function registeredProvider(
|
|
20
|
+
llm: LlmRuntime,
|
|
21
|
+
policy: ModelSelectionPolicy,
|
|
22
|
+
providerId: string,
|
|
23
|
+
): LlmProviderInfo {
|
|
24
|
+
const providers = llm.listProviders()
|
|
25
|
+
const provider = providers.find(candidate => candidate.id === providerId)
|
|
26
|
+
if (provider !== undefined) return provider
|
|
27
|
+
const available = providers
|
|
28
|
+
.filter(candidate => policy.routes.some(route => route.provider === candidate.id))
|
|
29
|
+
.map(candidate => candidate.id)
|
|
30
|
+
.join(', ') || '(none)'
|
|
31
|
+
throw new Error(`LLM provider "${providerId}" is not registered; available providers: ${available}`)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Render one advertised or resolved model. */
|
|
35
|
+
function modelLine(provider: string, model: { id: string; name: string; description?: string }): string {
|
|
36
|
+
return `${provider}/${model.id} — ${model.name}${model.description === undefined ? '' : `: ${model.description}`}`
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Read the requested provider, allowed models, or exact-model efforts. */
|
|
40
|
+
async function listSubagentModels(
|
|
41
|
+
ctx: Context,
|
|
42
|
+
policyHandle: ProjectionHandle<AllowedModelRoute[] | null>,
|
|
43
|
+
request: ListSubagentModelsRequest,
|
|
44
|
+
exec: ToolRunContext,
|
|
45
|
+
): Promise<string> {
|
|
46
|
+
if (exec.agent === undefined) throw new Error('list_subagent_models requires a calling agent')
|
|
47
|
+
const routes = subagentModelSelectionPolicy(policyHandle, exec.agent.session)
|
|
48
|
+
let policy = routes === undefined ? undefined : { routes }
|
|
49
|
+
const selectionEnabled = policy !== undefined
|
|
50
|
+
if (policy === undefined) {
|
|
51
|
+
const { provider, model } = parentAgentOptionsForDelegation(exec.agent)
|
|
52
|
+
if (provider === undefined || model === undefined) {
|
|
53
|
+
throw new Error('cannot discover child LLM routes without a parent provider and model')
|
|
54
|
+
}
|
|
55
|
+
policy = { routes: [{ provider, model }] }
|
|
56
|
+
}
|
|
57
|
+
const { signal } = exec
|
|
58
|
+
const llm = ctx.get('llm')
|
|
59
|
+
if (llm === undefined) {
|
|
60
|
+
throw new Error('cannot discover child LLM routes because the `llm` service is unavailable')
|
|
61
|
+
}
|
|
62
|
+
if (request.model !== undefined && request.provider === undefined) {
|
|
63
|
+
throw new Error('`model` requires `provider`')
|
|
64
|
+
}
|
|
65
|
+
if (request.provider === undefined) {
|
|
66
|
+
const providers = llm.listProviders()
|
|
67
|
+
.filter(provider => policy.routes.some(route => route.provider === provider.id))
|
|
68
|
+
return providers.length === 0
|
|
69
|
+
? '(no LLM providers)'
|
|
70
|
+
: providers.map(provider => `${provider.id} — ${provider.name}`).join('\n')
|
|
71
|
+
}
|
|
72
|
+
if (request.provider.length === 0) throw new Error('`provider` must be non-empty')
|
|
73
|
+
const allowedRoutes = policy.routes.filter(route => route.provider === request.provider)
|
|
74
|
+
const [firstRoute] = allowedRoutes
|
|
75
|
+
if (firstRoute === undefined) {
|
|
76
|
+
throw new Error(`LLM provider "${request.provider}" is not allowed for this Session`)
|
|
77
|
+
}
|
|
78
|
+
const provider = registeredProvider(llm, policy, request.provider)
|
|
79
|
+
if (request.model === undefined) {
|
|
80
|
+
const models = selectionEnabled
|
|
81
|
+
? (await llm.listModels(provider.id)).filter(model => allowedRoutes.some(route => route.model === model.id))
|
|
82
|
+
: [await llm.resolveModelInfo(provider.id, firstRoute.model, signal)]
|
|
83
|
+
signal.throwIfAborted()
|
|
84
|
+
return models.length === 0
|
|
85
|
+
? `(no advertised models for ${provider.id})`
|
|
86
|
+
: models.map(model => modelLine(provider.id, model)).join('\n')
|
|
87
|
+
}
|
|
88
|
+
if (request.model.length === 0) throw new Error('`model` must be non-empty')
|
|
89
|
+
if (!allowedRoutes.some(route => route.model === request.model)) {
|
|
90
|
+
throw new Error(`child LLM route "${provider.id}/${request.model}" is not allowed for this Session`)
|
|
91
|
+
}
|
|
92
|
+
const model = await llm.resolveModelInfo(provider.id, request.model, signal)
|
|
93
|
+
const efforts = model.reasoning?.efforts.map(effort => (
|
|
94
|
+
`${effort.id}${model.reasoning?.defaultEffort === effort.id ? ' (default)' : ''} — ${effort.name}`
|
|
95
|
+
+ (effort.description === undefined ? '' : `: ${effort.description}`)
|
|
96
|
+
)).join('\n') || '(no advertised reasoning efforts)'
|
|
97
|
+
return `${modelLine(provider.id, model)}\nReasoning efforts:\n${efforts}`
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Register discovery of the calling Session’s authorized child models.
|
|
102
|
+
* @param ctx - Context carrying the tool registry and LLM service.
|
|
103
|
+
* @param policyHandle - retained reader for the Session’s authorized child routes.
|
|
104
|
+
*/
|
|
105
|
+
export function registerListSubagentModels(
|
|
106
|
+
ctx: Context,
|
|
107
|
+
policyHandle: ProjectionHandle<AllowedModelRoute[] | null>,
|
|
108
|
+
): void {
|
|
109
|
+
ctx.tools.register(defineTool({
|
|
110
|
+
name: 'list_subagent_models',
|
|
111
|
+
description:
|
|
112
|
+
'Inspect the routes available for explicit subagent model selection. No arguments: authorized providers. '
|
|
113
|
+
+ '`provider`: that provider\'s authorized models. `provider` + `model`: that model and its reasoning efforts. '
|
|
114
|
+
+ 'Query when choosing an explicit child route or reasoning effort. '
|
|
115
|
+
+ 'Omit optional fields rather than sending empty strings.',
|
|
116
|
+
parameters: {
|
|
117
|
+
provider: {
|
|
118
|
+
type: 'string',
|
|
119
|
+
description: 'Registered LLM provider id.',
|
|
120
|
+
},
|
|
121
|
+
model: {
|
|
122
|
+
type: 'string',
|
|
123
|
+
description: 'Exact model id; requires provider.',
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
output: {
|
|
127
|
+
schema: { type: 'string' },
|
|
128
|
+
render: (_args, result) => [{ type: 'text', text: result }],
|
|
129
|
+
},
|
|
130
|
+
// Route and model metadata reads do not mutate parent-agent state.
|
|
131
|
+
isConcurrencySafe: () => true,
|
|
132
|
+
execute(args, exec) {
|
|
133
|
+
return listSubagentModels(ctx, policyHandle, args, exec)
|
|
134
|
+
},
|
|
135
|
+
}))
|
|
136
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** Host-owned opt-in setting for model-selectable subagent delegation. */
|
|
2
|
+
import type { Volatile } from '@deepseek-ai/cordis'
|
|
3
|
+
|
|
4
|
+
import { Context, Service } from '@deepseek-ai/cordis'
|
|
5
|
+
import z from '@deepseek-ai/schemastery'
|
|
6
|
+
import { SessionSeq } from '@deepseek-ai/dsh-session'
|
|
7
|
+
import type {} from '@deepseek-ai/dsh-agent'
|
|
8
|
+
import {
|
|
9
|
+
subagentModelSelectionPolicy,
|
|
10
|
+
subagentModelSelectionProjectionDefinition,
|
|
11
|
+
} from './model-selection-state.ts'
|
|
12
|
+
import {
|
|
13
|
+
AllowedModelRouteSchema,
|
|
14
|
+
assertAllowedModelRoutes,
|
|
15
|
+
type AllowedModelRoute,
|
|
16
|
+
} from './model-selection.ts'
|
|
17
|
+
|
|
18
|
+
declare module '@deepseek-ai/cordis' {
|
|
19
|
+
interface Context {
|
|
20
|
+
/** User preference sampled during Agent creation. */
|
|
21
|
+
subagentModelSelection: SubagentModelSelectionConfig
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Stored user preference; the shipped composition defaults it off. */
|
|
26
|
+
export interface SubagentModelSelectionSettings {
|
|
27
|
+
/** Whether new top-level Sessions receive model selection. */
|
|
28
|
+
enabled: boolean
|
|
29
|
+
/** Exact child LLM routes offered to new top-level Sessions. */
|
|
30
|
+
allowedModels: AllowedModelRoute[]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Optional deployment base for the preference. */
|
|
34
|
+
export interface Config {
|
|
35
|
+
/** Initial enabled state inherited when the user document does not override it. */
|
|
36
|
+
enabled: Volatile<boolean>
|
|
37
|
+
/** Initial route list inherited when the user document does not override it. */
|
|
38
|
+
allowedModels: Volatile<AllowedModelRoute[]>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Host settings owner that records Session authorization before the first model request. */
|
|
42
|
+
export class SubagentModelSelectionConfig extends Service {
|
|
43
|
+
static inject = ['sessionProjections', 'sessions']
|
|
44
|
+
|
|
45
|
+
static Config = z.object({
|
|
46
|
+
enabled: z.boolean().default(false).volatile(),
|
|
47
|
+
allowedModels: z.array(AllowedModelRouteSchema).default([]).volatile(),
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
constructor(ctx: Context, private config: Config) {
|
|
51
|
+
super(ctx, 'subagentModelSelection')
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
protected async [Service.init](): Promise<void> {
|
|
55
|
+
const ctx = this.ctx
|
|
56
|
+
const policyReady = ctx.sessionProjections.register(subagentModelSelectionProjectionDefinition)
|
|
57
|
+
ctx.on('agent/created', async ({ agent }) => {
|
|
58
|
+
const policy = await policyReady
|
|
59
|
+
ctx.fiber.assertActive()
|
|
60
|
+
const target = agent.session
|
|
61
|
+
if (subagentModelSelectionPolicy(policy, target) !== undefined) return
|
|
62
|
+
let allowedModels: AllowedModelRoute[] | undefined
|
|
63
|
+
const parentId = target.header.origin === 'subagent' ? target.header.parentSession : undefined
|
|
64
|
+
if (parentId !== undefined) {
|
|
65
|
+
const parent = ctx.sessions.get(parentId)
|
|
66
|
+
if (parent !== undefined) allowedModels = subagentModelSelectionPolicy(policy, parent)
|
|
67
|
+
} else if (target.firstLiveSeq === 0
|
|
68
|
+
// oxlint-disable-next-line typescript/no-deprecated -- Existing Session history read; migration deferred.
|
|
69
|
+
&& target.eventAt(SessionSeq(0))?.type !== 'session/end-seed') {
|
|
70
|
+
const current = this.current()
|
|
71
|
+
allowedModels = current.enabled ? current.allowedModels : undefined
|
|
72
|
+
}
|
|
73
|
+
if (allowedModels !== undefined) target.append('subagent/model-selection-policy', { allowedModels })
|
|
74
|
+
})
|
|
75
|
+
await policyReady
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Read a detached selection preference for the next fresh top-level Session.
|
|
80
|
+
* @returns the enabled state and exact allowed routes.
|
|
81
|
+
*/
|
|
82
|
+
private current(): SubagentModelSelectionSettings {
|
|
83
|
+
const enabled = this.config.enabled.get()
|
|
84
|
+
const allowedModels = this.config.allowedModels.get()
|
|
85
|
+
assertAllowedModelRoutes(allowedModels)
|
|
86
|
+
if (enabled && allowedModels.length === 0) {
|
|
87
|
+
throw new Error('enabled subagent model selection requires at least one allowed model')
|
|
88
|
+
}
|
|
89
|
+
return { enabled, allowedModels: allowedModels.map(route => ({ ...route })) }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export const name = 'subagent-model-selection-settings'
|
|
95
|
+
export default SubagentModelSelectionConfig
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** Durable per-session state for the user-controlled model-selection opt-in. */
|
|
2
|
+
|
|
3
|
+
import { z as zod } from 'zod'
|
|
4
|
+
import type { Session } from '@deepseek-ai/dsh-session'
|
|
5
|
+
import type { ProjectionHandle } from '@deepseek-ai/dsh-session-projection'
|
|
6
|
+
import type { ProjectionDefinition } from '@deepseek-ai/dsh-session-projection'
|
|
7
|
+
import { assertAllowedModelRoutes, type AllowedModelRoute } from './model-selection.ts'
|
|
8
|
+
|
|
9
|
+
declare module '@deepseek-ai/dsh-session/types' {
|
|
10
|
+
interface SessionEventMap {
|
|
11
|
+
/**
|
|
12
|
+
* Records this Session’s authorized child provider/model routes. Appended
|
|
13
|
+
* before the first model request; absence restricts explicit selection
|
|
14
|
+
* to the current parent route. Log-only: it carries
|
|
15
|
+
* no `surfaceOp` and never enters model history.
|
|
16
|
+
*/
|
|
17
|
+
'subagent/model-selection-policy': {
|
|
18
|
+
/** Exact routes this Session may select explicitly for a child. */
|
|
19
|
+
allowedModels: AllowedModelRoute[]
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const modelSelectionPolicySchema: zod.ZodType<AllowedModelRoute[] | null> = zod.array(zod.object({
|
|
25
|
+
provider: zod.string().min(1),
|
|
26
|
+
model: zod.string().min(1),
|
|
27
|
+
}).strict()).min(1).nullable()
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Host-only projection of the durable model-selection policy.
|
|
31
|
+
* Each independently mounted consumer registers this shared definition; the registry reference-counts it.
|
|
32
|
+
*/
|
|
33
|
+
export const subagentModelSelectionProjectionDefinition = {
|
|
34
|
+
key: 'subagentModelSelectionPolicy',
|
|
35
|
+
stateVersion: 1,
|
|
36
|
+
stateSchema: modelSelectionPolicySchema,
|
|
37
|
+
init: () => null,
|
|
38
|
+
apply: (policy, event) => {
|
|
39
|
+
if (policy !== null || event.type !== 'subagent/model-selection-policy') return policy
|
|
40
|
+
const { allowedModels } = event.data
|
|
41
|
+
assertAllowedModelRoutes(allowedModels)
|
|
42
|
+
if (allowedModels.length === 0) {
|
|
43
|
+
throw new Error('subagent/model-selection-policy requires at least one route')
|
|
44
|
+
}
|
|
45
|
+
return allowedModels
|
|
46
|
+
},
|
|
47
|
+
} satisfies ProjectionDefinition<'subagentModelSelectionPolicy', AllowedModelRoute[] | null>
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Read the exact route list authorized for a Session.
|
|
51
|
+
* @param subagentModelSelectionProjectionHandle - registered policy state reader.
|
|
52
|
+
* @param session - session whose durable decision is read.
|
|
53
|
+
* @returns a detached route list, or undefined to restrict selection to the parent route.
|
|
54
|
+
*/
|
|
55
|
+
export function subagentModelSelectionPolicy(
|
|
56
|
+
subagentModelSelectionProjectionHandle: Pick<ProjectionHandle<AllowedModelRoute[] | null>, 'stateOf'>,
|
|
57
|
+
session: Session,
|
|
58
|
+
): AllowedModelRoute[] | undefined {
|
|
59
|
+
return subagentModelSelectionProjectionHandle.stateOf(session)?.map(route => ({ ...route }))
|
|
60
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/** Child LLM route selection for the subagent tool. */
|
|
2
|
+
|
|
3
|
+
import { ReasoningEffortId } from '@deepseek-ai/dsh-llm'
|
|
4
|
+
import type { LlmRuntime } from '@deepseek-ai/dsh-llm'
|
|
5
|
+
import type { AgentOptions } from '@deepseek-ai/dsh-agent'
|
|
6
|
+
import z from '@deepseek-ai/schemastery'
|
|
7
|
+
|
|
8
|
+
/** One exact child LLM route authorized by a user setting. */
|
|
9
|
+
export interface AllowedModelRoute {
|
|
10
|
+
/** Registered LLM provider id. */
|
|
11
|
+
readonly provider: string
|
|
12
|
+
/** Provider-owned exact model id. */
|
|
13
|
+
readonly model: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Schema shared by the Host setting and its deployment base. */
|
|
17
|
+
export const AllowedModelRouteSchema: z<AllowedModelRoute> = z.object({
|
|
18
|
+
provider: z.string().min(1).required(),
|
|
19
|
+
model: z.string().min(1).required(),
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
/** Exact routes authorized for one Session. */
|
|
23
|
+
export interface ModelSelectionPolicy {
|
|
24
|
+
/** Exact provider/model routes authorized for explicit selection. */
|
|
25
|
+
readonly routes: readonly AllowedModelRoute[]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Stable identity for one provider/model pair.
|
|
30
|
+
* @param route - Exact provider/model route.
|
|
31
|
+
* @returns Opaque key for equality checks.
|
|
32
|
+
*/
|
|
33
|
+
export function modelRouteKey(route: AllowedModelRoute): string {
|
|
34
|
+
return `${route.provider}\0${route.model}`
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Reject malformed or duplicate route policy entries at a durable or configuration boundary.
|
|
39
|
+
* @param routes - Candidate exact routes to validate.
|
|
40
|
+
* @returns an assertion that the candidate is a validated exact-route array.
|
|
41
|
+
*/
|
|
42
|
+
export function assertAllowedModelRoutes(routes: unknown): asserts routes is readonly AllowedModelRoute[] {
|
|
43
|
+
if (!Array.isArray(routes)) {
|
|
44
|
+
throw new Error('subagent model selection requires an array of routes')
|
|
45
|
+
}
|
|
46
|
+
const seen = new Set<string>()
|
|
47
|
+
const candidates: readonly unknown[] = routes
|
|
48
|
+
for (const candidate of candidates) {
|
|
49
|
+
if (typeof candidate !== 'object' || candidate === null || Array.isArray(candidate)
|
|
50
|
+
|| !('provider' in candidate) || typeof candidate.provider !== 'string'
|
|
51
|
+
|| !('model' in candidate) || typeof candidate.model !== 'string'
|
|
52
|
+
|| candidate.provider.length === 0 || candidate.model.length === 0) {
|
|
53
|
+
throw new Error('subagent model selection requires non-empty provider and model ids')
|
|
54
|
+
}
|
|
55
|
+
const route = { provider: candidate.provider, model: candidate.model }
|
|
56
|
+
const key = modelRouteKey(route)
|
|
57
|
+
if (seen.has(key)) {
|
|
58
|
+
throw new Error(`subagent model selection repeats route "${route.provider}/${route.model}"`)
|
|
59
|
+
}
|
|
60
|
+
seen.add(key)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Model-facing child LLM route fields. */
|
|
65
|
+
export interface DelegationModelRequest {
|
|
66
|
+
readonly provider?: string
|
|
67
|
+
readonly model?: string
|
|
68
|
+
readonly reasoning_effort?: string
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Whether a call explicitly selects any child LLM value.
|
|
73
|
+
* @param request - Model-facing route fields from the tool call.
|
|
74
|
+
* @returns Whether at least one route or effort field is present.
|
|
75
|
+
*/
|
|
76
|
+
export function hasDelegationModelRequest(request: DelegationModelRequest): boolean {
|
|
77
|
+
return request.provider !== undefined
|
|
78
|
+
|| request.model !== undefined
|
|
79
|
+
|| request.reasoning_effort !== undefined
|
|
80
|
+
}
|
|
81
|
+
|
|
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
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Validate explicit selection against the Session policy and merge it over child defaults.
|
|
91
|
+
* Explicit selection requires both provider and model; an omitted effort then
|
|
92
|
+
* uses the selected model's default, even when the route is unchanged.
|
|
93
|
+
* @param configured - Tool-instance child defaults.
|
|
94
|
+
* @param request - Model-facing route override.
|
|
95
|
+
* @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.
|
|
98
|
+
* @returns Child Agent options, preserving omission when no layer contributes one.
|
|
99
|
+
*/
|
|
100
|
+
export function requestedAgentOptions(
|
|
101
|
+
configured: AgentOptions | undefined,
|
|
102
|
+
request: DelegationModelRequest,
|
|
103
|
+
enabled: boolean,
|
|
104
|
+
policy: ModelSelectionPolicy | undefined,
|
|
105
|
+
parentOptions: AgentOptions,
|
|
106
|
+
): AgentOptions | undefined {
|
|
107
|
+
if (!hasDelegationModelRequest(request)) return configured
|
|
108
|
+
if (!enabled) {
|
|
109
|
+
throw new Error('child model selection is disabled for this tool instance')
|
|
110
|
+
}
|
|
111
|
+
assertNonEmpty(request.provider, 'provider')
|
|
112
|
+
assertNonEmpty(request.model, 'model')
|
|
113
|
+
assertNonEmpty(request.reasoning_effort, 'reasoning_effort')
|
|
114
|
+
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')
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
assertAllowedModelSelection(policy, parentOptions, { provider: request.provider, model: request.model })
|
|
121
|
+
return {
|
|
122
|
+
...configured,
|
|
123
|
+
provider: request.provider,
|
|
124
|
+
model: request.model,
|
|
125
|
+
// Explicit undefined also clears compatible parent effort during child-option merging.
|
|
126
|
+
reasoningEffort: request.reasoning_effort === undefined ? undefined : ReasoningEffortId(request.reasoning_effort),
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
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.
|
|
134
|
+
* @param route - Validated provider/model pair from the tool call.
|
|
135
|
+
*/
|
|
136
|
+
export function assertAllowedModelSelection(
|
|
137
|
+
policy: ModelSelectionPolicy | undefined,
|
|
138
|
+
parentOptions: AgentOptions,
|
|
139
|
+
route: AllowedModelRoute,
|
|
140
|
+
): void {
|
|
141
|
+
const { provider, model } = route
|
|
142
|
+
const allowed = policy === undefined
|
|
143
|
+
? provider === parentOptions.provider && model === parentOptions.model
|
|
144
|
+
: policy.routes.some(route => route.provider === provider && route.model === model)
|
|
145
|
+
if (allowed) return
|
|
146
|
+
throw new Error(`child LLM route "${provider}/${model}" is not allowed for this Session`
|
|
147
|
+
+ (policy === undefined
|
|
148
|
+
? '; without a recorded model-selection policy, explicit selection is limited to the parent route'
|
|
149
|
+
: ''))
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Whether configured Agent options require route validation before delegation.
|
|
154
|
+
* @param options - Tool-instance child defaults.
|
|
155
|
+
* @returns Whether configured provider, model, or effort values must be resolved.
|
|
156
|
+
*/
|
|
157
|
+
export function hasConfiguredLlmSelection(options: AgentOptions | undefined): boolean {
|
|
158
|
+
return options?.provider !== undefined
|
|
159
|
+
|| options?.model !== undefined
|
|
160
|
+
|| options?.reasoningEffort !== undefined
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Resolve an effective child route through its live adapter before the child is
|
|
165
|
+
* created. The LLM runtime owns provider lookup, exact-model metadata, effort
|
|
166
|
+
* validation, and adapter defaults.
|
|
167
|
+
* @param llm - Live LLM runtime.
|
|
168
|
+
* @param parentOptions - Current parent values whose compatible fields the child inherits.
|
|
169
|
+
* @param requested - Per-child options after request/config merging.
|
|
170
|
+
* @param signal - Tool-call cancellation signal.
|
|
171
|
+
* @param inheritParentReasoningEffort - Whether an omitted effort may inherit from the parent route; false for explicit model selection.
|
|
172
|
+
*/
|
|
173
|
+
export async function preflightChildLlmRoute(
|
|
174
|
+
llm: LlmRuntime,
|
|
175
|
+
parentOptions: AgentOptions,
|
|
176
|
+
requested: AgentOptions | undefined,
|
|
177
|
+
signal: AbortSignal,
|
|
178
|
+
inheritParentReasoningEffort = true,
|
|
179
|
+
): Promise<void> {
|
|
180
|
+
const provider = requested?.provider ?? parentOptions.provider
|
|
181
|
+
const model = requested?.model ?? parentOptions.model
|
|
182
|
+
if (provider === undefined || model === undefined) {
|
|
183
|
+
throw new Error('cannot select child LLM values without an effective provider and model')
|
|
184
|
+
}
|
|
185
|
+
const routeChanged = provider !== parentOptions.provider || model !== parentOptions.model
|
|
186
|
+
const reasoningEffort = requested?.reasoningEffort
|
|
187
|
+
?? (inheritParentReasoningEffort && !routeChanged ? parentOptions.reasoningEffort : undefined)
|
|
188
|
+
await llm.resolveCallConfig({
|
|
189
|
+
provider,
|
|
190
|
+
model,
|
|
191
|
+
...reasoningEffort === undefined ? {} : { reasoningEffort },
|
|
192
|
+
}, signal)
|
|
193
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/** Deployment-owned preset choices for one delegation tool. */
|
|
2
|
+
import type { SubagentProvider } from '@deepseek-ai/dsh-subagent'
|
|
3
|
+
|
|
4
|
+
/** Optional default and model-selectable preset ids for a spawn tool. */
|
|
5
|
+
export interface PresetSelectionConfig {
|
|
6
|
+
/** Default independent preset; omission inherits the parent when the model makes no selection. */
|
|
7
|
+
agentPreset?: string
|
|
8
|
+
/** Non-empty allowlist exposing agent_preset to the model; omission disables model selection. */
|
|
9
|
+
allowedAgentPresets?: string[] | undefined
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Untrusted preset selection from model/tool JSON. */
|
|
13
|
+
export interface PresetSelectionRequest {
|
|
14
|
+
readonly agent_preset?: unknown
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Validates configured choices and resolves model input against the allowlist. */
|
|
18
|
+
export class SubagentPresetSelection {
|
|
19
|
+
constructor(private readonly config: PresetSelectionConfig) {
|
|
20
|
+
const { agentPreset, allowedAgentPresets } = config
|
|
21
|
+
if (allowedAgentPresets === undefined) return
|
|
22
|
+
if (new Set(allowedAgentPresets).size !== allowedAgentPresets.length) {
|
|
23
|
+
throw new Error('tool-subagent: allowedAgentPresets contains duplicate preset ids')
|
|
24
|
+
}
|
|
25
|
+
if (agentPreset !== undefined && !allowedAgentPresets.includes(agentPreset)) {
|
|
26
|
+
throw new Error('tool-subagent: agentPreset must be included in allowedAgentPresets')
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Whether this tool requires the preset service and an independently composing provider. */
|
|
31
|
+
get configured(): boolean {
|
|
32
|
+
return this.config.agentPreset !== undefined || this.config.allowedAgentPresets !== undefined
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Preset ids advertised in the model parameter. */
|
|
36
|
+
get choices(): readonly string[] | undefined {
|
|
37
|
+
return this.config.allowedAgentPresets
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Model-visible selection and default guidance. */
|
|
41
|
+
get description(): string {
|
|
42
|
+
const fallback = this.config.agentPreset === undefined
|
|
43
|
+
? 'Omit to inherit the parent agent\'s current preset.'
|
|
44
|
+
: `Omit to use ${this.config.agentPreset}.`
|
|
45
|
+
return `Agent preset for this child. ${fallback}`
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Reject independent selection for providers that inherit conversation history or lack support.
|
|
50
|
+
* @param provider - Currently registered delegation provider.
|
|
51
|
+
*/
|
|
52
|
+
assertProvider(provider: SubagentProvider): void {
|
|
53
|
+
if (this.configured && (!provider.capabilities.agentPreset || provider.inheritsParentContext)) {
|
|
54
|
+
throw new Error(`tool-subagent: provider "${provider.name}" does not support independent agent preset selection`)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Validate model input and apply the deployment default.
|
|
60
|
+
* @param request - Model-supplied tool arguments.
|
|
61
|
+
* @returns Selected preset id, or omission to inherit the parent.
|
|
62
|
+
*/
|
|
63
|
+
resolve(request: PresetSelectionRequest): string | undefined {
|
|
64
|
+
const id = request.agent_preset
|
|
65
|
+
if (id === undefined) return this.config.agentPreset
|
|
66
|
+
if (this.choices === undefined) throw new Error('agent_preset selection is disabled for this tool instance')
|
|
67
|
+
if (typeof id !== 'string' || !this.choices.includes(id)) {
|
|
68
|
+
throw new Error(`agent_preset must be one of: ${this.choices.join(', ')}`)
|
|
69
|
+
}
|
|
70
|
+
return id
|
|
71
|
+
}
|
|
72
|
+
}
|