Files
deepseek-harness/packages/subagent/tool-subagent-control/src/index.ts
T

114 lines
4.0 KiB
TypeScript

/**
* Shared messaging, interruption, and child discovery tools.
* They perform no lifecycle routing of their own —
* residency, cold resume, and interrupt authorization belong to the subagent
* service — and they live apart from the provider-bound
* `@deepseek-ai/dsh-tool-subagent` instances so multiple delegation tools share
* one control API.
* @module @deepseek-ai/dsh-tool-subagent-control
*/
import type { Context } from '@deepseek-ai/cordis'
import { brandString } from '@deepseek-ai/dsh-brand'
import { defineTool } from '@deepseek-ai/dsh-tools'
import type { ContentBlock } from '@deepseek-ai/dsh-llm'
import type { SessionId } from '@deepseek-ai/dsh-session'
import type {} from '@deepseek-ai/dsh-subagent'
import { markAdjacentAgentSendMessageTool } from '@deepseek-ai/dsh-subagent/internal'
import { registerListAgents } from './list-agents.ts'
export const name = 'tool-subagent-control'
export const inject = ['tools', 'subagents', 'agents']
/**
* Register the shared subagent control and discovery tools.
* @param ctx - context carrying tools, subagents, and live Agents.
*/
export function apply(ctx: Context): void {
registerListAgents(ctx)
ctx.tools.register(markAdjacentAgentSendMessageTool(defineTool({
name: 'send_message',
description:
'Send a message to an agent. A working agent receives it at its next step; an idle agent starts a new turn with it. '
+ 'Returns delivery confirmation, not the agent\'s answer.',
parameters: {
agent_id: {
type: 'string',
required: true,
description: 'The agent id of your direct continuable child, or your direct parent when you are a resident continuable child.',
},
message: {
type: 'string',
required: true,
description: 'The message to deliver to the agent.',
},
},
output: {
schema: {
type: 'object',
additionalProperties: false,
properties: {
messageId: { type: 'string', required: true },
},
},
render: (args, _value) => [{
type: 'text',
text: `message delivered to agent ${args.agent_id}`,
}],
},
async execute(args, exec) {
const sender = exec.agent
if (!sender) {
throw new Error('send_message requires a calling agent (exec.agent was undefined)')
}
const message: ContentBlock[] = [{ type: 'text', text: args.message }]
const messageId = await ctx.subagents.sendMessage(
sender,
brandString<SessionId>(args.agent_id),
message,
{ signal: exec.signal },
)
return { messageId }
},
})))
ctx.tools.register(defineTool({
name: 'interrupt_agent',
description:
'Ask a subagent to stop its current work. This call returns without waiting for it to stop. '
+ 'You can continue a local direct child\'s conversation later with send_message. '
+ 'External executions stop permanently and cannot receive follow-ups. Subagents it started will keep running.',
parameters: {
agent_id: {
type: 'string',
required: true,
description: 'The id of an agent created under you: your direct child or a deeper descendant.',
},
},
output: {
schema: {
type: 'object',
additionalProperties: false,
properties: {
accepted: { type: 'boolean', required: true },
},
},
render: (args, _value) => [{
type: 'text',
text: `interrupt requested for agent ${args.agent_id}`,
}],
},
execute(args, exec) {
const caller = exec.agent
if (!caller) {
// Ancestor authority requires an exact live calling agent.
throw new Error('interrupt_agent requires a calling agent (exec.agent was undefined)')
}
// The service authorizes the exact live caller against the target's
// recorded lineage; the tool adds no authority of its own.
ctx.subagents.interrupt(brandString<SessionId>(args.agent_id), { kind: 'ancestor', agent: caller })
return Promise.resolve({ accepted: true })
},
}))
}