388 lines
19 KiB
TypeScript
388 lines
19 KiB
TypeScript
/**
|
|
* Model-facing delegation through one configured `ctx.subagents` provider.
|
|
* Provider lifecycle controls tool registration and context-sensitive schema
|
|
* wording. Every delegation starts a managed activation and returns its child
|
|
* id; the subagent runtime owns result delivery and resource cleanup.
|
|
* @module @deepseek-ai/dsh-tool-subagent
|
|
*/
|
|
|
|
import type { Context } from '@deepseek-ai/cordis'
|
|
import z from '@deepseek-ai/schemastery'
|
|
import type {} from '@deepseek-ai/dsh-agent-preset-registry'
|
|
import { defineTool } from '@deepseek-ai/dsh-tools'
|
|
import type { ToolDefinition } from '@deepseek-ai/dsh-tools'
|
|
import type { AgentOptions } from '@deepseek-ai/dsh-agent'
|
|
import { ReasoningEffortId } from '@deepseek-ai/dsh-llm'
|
|
import type { ContentBlock } from '@deepseek-ai/dsh-llm'
|
|
import {
|
|
assertSubagentMaxDepth,
|
|
delegationDepthOf,
|
|
parentAgentOptionsForDelegation,
|
|
} from '@deepseek-ai/dsh-subagent'
|
|
import type { SubagentProvider } from '@deepseek-ai/dsh-subagent'
|
|
import {
|
|
hasConfiguredLlmSelection,
|
|
hasDelegationModelRequest,
|
|
preflightChildLlmRoute,
|
|
requestedAgentOptions,
|
|
} from './model-selection.ts'
|
|
import type { DelegationModelRequest } from './model-selection.ts'
|
|
import { registerListSubagentModels } from './list-models.ts'
|
|
import { SubagentPresetSelection, type PresetSelectionConfig, type PresetSelectionRequest } from './preset-selection.ts'
|
|
import {
|
|
subagentModelSelectionProjectionDefinition,
|
|
subagentModelSelectionPolicy,
|
|
} from './model-selection-state.ts'
|
|
|
|
export const name = 'tool-subagent'
|
|
export const inject = ['tools', 'subagents', 'systemPrompt', 'sessionProjections']
|
|
|
|
// Definition identity keeps shared guidance scoped to the actual visible tools,
|
|
// including scoped overrides and independently loaded tool registries.
|
|
const delegationTools = new Set<ToolDefinition>()
|
|
|
|
/** Config: which registered provider this tool delegates to, plus child defaults. */
|
|
export interface Config extends PresetSelectionConfig {
|
|
/** The `ctx.subagents` provider name to start runs on (e.g. `spawn`, `acp`). */
|
|
provider: string
|
|
/**
|
|
* Model-facing tool name (default `subagent`). Each loaded instance must use
|
|
* a distinct name.
|
|
*/
|
|
toolName?: string
|
|
/**
|
|
* Expose optional child `provider`, `model`, and `reasoning_effort` fields.
|
|
* Requires provider `agentOptions` support. Execution enforces the Session
|
|
* policy, or restricts explicit selection to the current parent route.
|
|
*/
|
|
modelSelection?: boolean
|
|
/**
|
|
* Register the shared `list_subagent_models` tool for this scope.
|
|
* Enable on only one instance per scope; defaults to false and does not
|
|
* enable model selection. Discovery is removed when this instance unloads.
|
|
*/
|
|
modelDiscovery?: boolean
|
|
/**
|
|
* Agent options applied to every child; omitted fields use child-loop defaults.
|
|
*/
|
|
agentOptions?: AgentOptions
|
|
/**
|
|
* Maximum child depth: a non-negative safe integer (`0` forbids delegation),
|
|
* or `'provider-managed'` to send no cap. A numeric cap
|
|
* requires the provider's `depthLimit` capability (mount fails loud
|
|
* otherwise). The provider checks the calling agent's current depth at every
|
|
* start; the tool remains model-visible so runtime policy owns rejection.
|
|
* `'provider-managed'` is for an out-of-process provider whose recursion
|
|
* budget belongs to the child runtime or its own deployment. Omission reads
|
|
* the current Host subagent depth setting (default `1`) at each delegation.
|
|
*/
|
|
maxDepth?: number | 'provider-managed'
|
|
}
|
|
|
|
export const Config: z<Config> = z.object({
|
|
provider: z.string().required(),
|
|
toolName: z.string().default('subagent'),
|
|
modelSelection: z.boolean().default(false),
|
|
modelDiscovery: z.boolean().default(false),
|
|
// Prevent Schemastery from materializing omitted agentOptions as `{}`.
|
|
agentOptions: z.object({
|
|
provider: z.string(),
|
|
model: z.string(),
|
|
reasoningEffort: z.string().min(1) as z<ReturnType<typeof ReasoningEffortId> | undefined>,
|
|
maxTokens: z.number().step(1).min(1).max(Number.MAX_SAFE_INTEGER),
|
|
}).default(undefined as unknown as {
|
|
provider: string
|
|
model: string
|
|
reasoningEffort: ReturnType<typeof ReasoningEffortId>
|
|
maxTokens: number
|
|
}),
|
|
agentPreset: z.string().min(1),
|
|
allowedAgentPresets: (z.array(z.string().min(1)).min(1) as z<string[] | undefined>).default(undefined),
|
|
maxDepth: z.union([z.natural().max(Number.MAX_SAFE_INTEGER), z.const('provider-managed' as const)]),
|
|
})
|
|
|
|
/**
|
|
* Model-facing wording from the provider's conversation-history descriptor
|
|
* ({@link SubagentProvider.inheritsParentContext}).
|
|
* A fresh child needs a standalone prompt; a forked child already sees the
|
|
* conversation's completed turns — telling the model to restate everything
|
|
* (or, worse, that the child "does not see this conversation") would be false
|
|
* for a fork.
|
|
* @param inheritsConversation - whether the child's conversation is seeded
|
|
* with the parent's completed turns; this says nothing about tool, service,
|
|
* scope, or authority inheritance.
|
|
* @returns the tool `description` and the `prompt` parameter description.
|
|
*/
|
|
function providerWording(inheritsConversation: boolean): { description: string; promptDescription: string } {
|
|
if (inheritsConversation) {
|
|
return {
|
|
description:
|
|
'Delegate a task to a subagent that inherits this conversation: a child agent seeded with all '
|
|
+ 'completed turns so far (it does not see the current in-flight turn). Use this when the subtask '
|
|
+ 'builds on this conversation\'s context — a follow-up analysis, '
|
|
+ 'a review, a continuation — without consuming this conversation\'s context for the work itself. '
|
|
+ 'You receive its result, not its intermediate steps.',
|
|
promptDescription:
|
|
'The task for the subagent. It already sees this conversation\'s completed turns, so build on them '
|
|
+ 'freely and state only what is new.',
|
|
}
|
|
}
|
|
return {
|
|
description:
|
|
'Delegate a self-contained task to a subagent (a separate agent that works in its own context) '
|
|
+ 'to offload focused, independent work — research, a scoped '
|
|
+ 'implementation, an analysis — so it does not consume this conversation\'s context. The subagent '
|
|
+ 'returns its result, not its intermediate steps.',
|
|
promptDescription:
|
|
'The complete, self-contained task for the subagent. It does not share this '
|
|
+ 'conversation\'s context, so include everything it needs.',
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Install one delegation-tool composition.
|
|
* The owning composition shares its registrations across Agents; child startup resolves the selected preset.
|
|
* @param ctx - Context that owns the registrations.
|
|
* @param config - delegation-tool configuration.
|
|
* @returns when the plugin is ready.
|
|
*/
|
|
export async function apply(ctx: Context, config: Config): Promise<void> {
|
|
for (const field of ['persona', 'toolFilter']) {
|
|
if (Object.hasOwn(config, field)) {
|
|
throw new Error(`tool-subagent: ${field} is no longer supported; configure persona and tool plugins in an Agent Preset and select it with agentPreset (spawn only)`)
|
|
}
|
|
}
|
|
const presets = new SubagentPresetSelection(config)
|
|
if (presets.configured && ctx.get('agentPresets') === undefined) {
|
|
throw new Error('tool-subagent: agent preset selection requires the agent-presets service')
|
|
}
|
|
// Direct apply() bypasses Schemastery's numeric constraints. A direct-apply
|
|
// omission stays capless (the schema default only runs through the loader).
|
|
if (config.maxDepth !== 'provider-managed') assertSubagentMaxDepth(config.maxDepth)
|
|
const toolName = config.toolName ?? 'subagent'
|
|
|
|
const modelSelectionEnabled = config.modelSelection === true
|
|
const subagentModelSelectionProjectionHandle = await ctx.sessionProjections.register(subagentModelSelectionProjectionDefinition)
|
|
|
|
const assertSubagentProviderConfiguration = (subagentProvider: SubagentProvider): void => {
|
|
presets.assertProvider(subagentProvider)
|
|
if (ctx.subagents.resolveMaxDepth(config.maxDepth) !== undefined && !subagentProvider.capabilities.depthLimit) {
|
|
throw new Error(
|
|
`tool-subagent: provider "${subagentProvider.name}" cannot enforce maxDepth (no depthLimit capability) — `
|
|
+ 'set maxDepth: \'provider-managed\' to leave the recursion budget to the provider',
|
|
)
|
|
}
|
|
if (config.agentOptions !== undefined && !subagentProvider.capabilities.agentOptions) {
|
|
throw new Error(
|
|
`tool-subagent: provider "${subagentProvider.name}" does not support child agentOptions`,
|
|
)
|
|
}
|
|
if (modelSelectionEnabled && !subagentProvider.capabilities.agentOptions) {
|
|
throw new Error(
|
|
`tool-subagent: provider "${subagentProvider.name}" does not support child model selection`,
|
|
)
|
|
}
|
|
}
|
|
|
|
// Validate provider-owned config outside the optional LLM binding so an
|
|
// invalid provider always rejects its registration or this plugin's load.
|
|
ctx.on('subagent/provider-added', (subagentProvider) => {
|
|
if (subagentProvider.name === config.provider) assertSubagentProviderConfiguration(subagentProvider)
|
|
})
|
|
const initialProvider = ctx.subagents.getProvider(config.provider)
|
|
if (initialProvider !== undefined) assertSubagentProviderConfiguration(initialProvider)
|
|
|
|
if (config.modelDiscovery === true) registerListSubagentModels(ctx, subagentModelSelectionProjectionHandle)
|
|
|
|
// Load order and HMR replacement can change provider availability while
|
|
// this fiber remains active.
|
|
let mounted: { subagentProvider: SubagentProvider; definition: ToolDefinition; disposeTool: () => void } | undefined
|
|
const mount = (subagentProvider: SubagentProvider): void => {
|
|
assertSubagentProviderConfiguration(subagentProvider)
|
|
const wording = providerWording(subagentProvider.inheritsParentContext)
|
|
const providerRouteDefaults = subagentProvider.agentRouteDefaults
|
|
const selectionDescription = providerRouteDefaults !== undefined
|
|
? ' Omit `provider`, `model`, and `reasoning_effort` to use child defaults, falling back to this backend\'s route defaults.'
|
|
: ' Omit `provider`, `model`, and `reasoning_effort` to use child defaults, falling back to compatible parent values.'
|
|
const definition = defineTool({
|
|
name: toolName,
|
|
description: wording.description
|
|
+ ' This tool starts an independently managed subagent and immediately returns its id. If you are still online when it finishes, the runtime automatically delivers its completion notice. The notice includes the final answer unless the child sent you a message just before finishing.'
|
|
+ (subagentProvider.prepareContinuable !== undefined
|
|
? ' Use `send_message` to steer it while running or continue its conversation after it finishes.'
|
|
: ' This backend does not accept follow-up messages.')
|
|
+ (modelSelectionEnabled ? selectionDescription : '')
|
|
+ (modelSelectionEnabled && subagentProvider.inheritsParentContext
|
|
? ' Changing the route can prevent provider-side reuse of the inherited conversation prefix.'
|
|
: ''),
|
|
parameters: {
|
|
cwd: {
|
|
type: 'string',
|
|
description: 'Initial child working directory. Relative paths use your current directory; omitted inherits it. Later directory changes in either agent are independent.',
|
|
},
|
|
description: {
|
|
type: 'string',
|
|
required: true,
|
|
description: 'A short (3-5 word) description of the delegated task, for display. '
|
|
+ "Use the language of the user's current request.",
|
|
},
|
|
prompt: {
|
|
type: 'string',
|
|
required: true,
|
|
description: wording.promptDescription,
|
|
},
|
|
...presets.choices === undefined ? {} : {
|
|
agent_preset: {
|
|
type: 'string' as const,
|
|
enum: [...presets.choices],
|
|
description: presets.description,
|
|
},
|
|
},
|
|
...modelSelectionEnabled ? {
|
|
provider: {
|
|
type: 'string' as const,
|
|
description: 'Child LLM provider id; requires `model`.',
|
|
},
|
|
model: {
|
|
type: 'string' as const,
|
|
description: 'Model id within that provider; requires `provider`.',
|
|
},
|
|
reasoning_effort: {
|
|
type: 'string' as const,
|
|
description: 'Reasoning effort id; requires `provider` and `model`. Omit to use the selected model\'s default, even when it matches the parent or configured route.',
|
|
},
|
|
} : {},
|
|
},
|
|
output: {
|
|
schema: {
|
|
type: 'object',
|
|
additionalProperties: false,
|
|
properties: {
|
|
kind: { type: 'string', required: true, const: 'activation' },
|
|
subagentId: { type: 'string', required: true },
|
|
},
|
|
},
|
|
render: (_args, value) => [{ type: 'text', text: `started subagent ${value.subagentId}` }],
|
|
presentationMeta: (_args, value) => ({ kind: value.kind, subagentId: value.subagentId }),
|
|
},
|
|
isConcurrencySafe: () => true,
|
|
async execute(args, exec) {
|
|
const parent = exec.agent
|
|
if (!parent) {
|
|
// Non-agent callers provide no parent for delegation ownership.
|
|
throw new Error('subagent tool requires a calling agent (exec.agent was undefined)')
|
|
}
|
|
|
|
const routes = subagentModelSelectionPolicy(subagentModelSelectionProjectionHandle, parent.session)
|
|
const modelSelectionPolicy = routes === undefined ? undefined : { routes }
|
|
const agentPreset = presets.resolve(args as PresetSelectionRequest)
|
|
const modelRequest = args as DelegationModelRequest
|
|
const parentOptions = parentAgentOptionsForDelegation(parent)
|
|
const requiresRoutePreflight = hasDelegationModelRequest(modelRequest)
|
|
|| hasConfiguredLlmSelection(config.agentOptions)
|
|
const configuredChildAgentOptions = requiresRoutePreflight && providerRouteDefaults !== undefined
|
|
? { ...providerRouteDefaults, ...config.agentOptions }
|
|
: config.agentOptions
|
|
const requestedChildAgentOptions = requestedAgentOptions(
|
|
configuredChildAgentOptions,
|
|
modelRequest,
|
|
modelSelectionEnabled,
|
|
modelSelectionPolicy,
|
|
parentOptions,
|
|
)
|
|
if (requiresRoutePreflight) {
|
|
const llm = ctx.get('llm')
|
|
if (llm === undefined) {
|
|
throw new Error('cannot resolve the selected child LLM route because the `llm` service is unavailable')
|
|
}
|
|
const inheritParentReasoningEffort = providerRouteDefaults === undefined && !hasDelegationModelRequest(modelRequest)
|
|
await preflightChildLlmRoute(
|
|
llm,
|
|
parentOptions,
|
|
requestedChildAgentOptions,
|
|
exec.signal,
|
|
inheritParentReasoningEffort,
|
|
)
|
|
if (ctx.subagents.getProvider(config.provider) !== subagentProvider) {
|
|
throw new Error(`subagent provider "${config.provider}" changed while resolving the child LLM route; retry the delegation`)
|
|
}
|
|
}
|
|
exec.signal.throwIfAborted()
|
|
const maxDepth = ctx.subagents.resolveMaxDepth(config.maxDepth)
|
|
const request = {
|
|
...args.cwd === undefined ? {} : { cwd: args.cwd },
|
|
prompt: [{ type: 'text', text: args.prompt }] as ContentBlock[],
|
|
parent,
|
|
...agentPreset === undefined ? {} : { agentPreset },
|
|
...requestedChildAgentOptions !== undefined ? { agentOptions: requestedChildAgentOptions } : {},
|
|
...maxDepth !== undefined ? { maxDepth } : {},
|
|
}
|
|
|
|
const started = await ctx.subagents.startActivation({
|
|
provider: config.provider,
|
|
label: args.description,
|
|
...request,
|
|
signal: exec.signal,
|
|
delivery: 'parent',
|
|
})
|
|
return { kind: 'activation' as const, subagentId: started.childId }
|
|
},
|
|
})
|
|
const disposeTool = ctx.effect(() => {
|
|
const unregister = ctx.tools.register(definition)
|
|
delegationTools.add(definition)
|
|
return () => {
|
|
delegationTools.delete(definition)
|
|
unregister()
|
|
}
|
|
})
|
|
// oxlint-disable-next-line typescript/no-misused-promises -- Tool and guidance disposal are synchronous.
|
|
mounted = { subagentProvider, definition, disposeTool }
|
|
}
|
|
|
|
// Register listeners before checking presence so no synchronous change is missed.
|
|
ctx.on('subagent/provider-added', (subagentProvider) => {
|
|
if (subagentProvider.name === config.provider && mounted === undefined) mount(subagentProvider)
|
|
})
|
|
ctx.on('subagent/provider-removed', (name) => {
|
|
if (name !== config.provider || mounted === undefined) return
|
|
mounted.disposeTool()
|
|
mounted = undefined
|
|
})
|
|
const present = ctx.subagents.getProvider(config.provider)
|
|
if (present !== undefined) {
|
|
mount(present)
|
|
} else {
|
|
// A backend fiber may activate later; a misspelled provider remains visible in this log.
|
|
ctx.logger.info(`subagent provider "${config.provider}" not registered yet; the "${config.toolName ?? 'subagent'}" tool will register when it appears`)
|
|
}
|
|
ctx.systemPrompt.section({
|
|
name: `tool:${toolName}`,
|
|
order: ctx.systemPrompt.getSectionOrder('TOOL_SUBAGENT'),
|
|
text: (context) => {
|
|
if (mounted === undefined) return ''
|
|
const visible = [...delegationTools]
|
|
.filter(tool => ctx.tools.get(tool.name, context.scope) === tool)
|
|
.map(tool => tool.name)
|
|
.sort()
|
|
if (visible[0] !== toolName) return ''
|
|
const names = visible.map(name => `\`${name}\``).join(' or ')
|
|
return `Start independent delegations with ${names} together in one assistant message and continue useful work while they run. When only waiting for their results remains, end your turn instead of sleeping or polling; the next result starts a new turn. Collect the subagent results required to complete the task before giving the user a final answer.\n\nAgents may use different working directories; changes to shared files affect other agents. Split parallel write work into disjoint file or directory scopes. Bash, formatters, code generators, and scripts can overwrite concurrent edits without conflict detection; coordinate their changes explicitly. The agent integrating the work must review the final diff and run relevant tests.`
|
|
},
|
|
})
|
|
ctx.systemPrompt.context({
|
|
name: `tool:${toolName}:depth`,
|
|
order: ctx.systemPrompt.getContextOrder('SUBAGENT_DELEGATION'),
|
|
text: (context) => {
|
|
if (context.agent === undefined || mounted === undefined
|
|
|| ctx.tools.get(toolName, context.scope) !== mounted.definition) return ''
|
|
const maxDepth = ctx.subagents.resolveMaxDepth(config.maxDepth)
|
|
if (maxDepth === undefined) return ''
|
|
const depth = delegationDepthOf(context.agent)
|
|
if (depth < maxDepth) return ''
|
|
return `Delegation depth for \`${toolName}\`: current ${depth}, maximum ${maxDepth}, remaining levels 0.`
|
|
+ ` Do not call \`${toolName}\` at this depth; carry out the work directly.`
|
|
},
|
|
})
|
|
}
|