返回源码地图

packages/subagent/subagent-spawn-in-process/src/index.ts

main snapshot · da00f7f5358f · 正文引用章节 13;完整原文可核对,不声称全文件人工逐行审计

完整原文供逐行核对;页面收录不代表每行都经过人工语义审核。MIT 许可见 许可证。

1/**
2 * The in-process SPAWN subagent backend: registers a {@link SubagentProvider} on
3 * `ctx.subagents` that runs each child as a fresh child {@link Agent} on the same cordis
4 * context (its own session, own system prompt, zero parent context). The cheapest transport,
5 * reusing the agent factory's quiescent teardown.
6 * @module @deepseek-ai/dsh-subagent-spawn-in-process
7 */
8
9import type { Context } from '@deepseek-ai/cordis'
10import z from '@deepseek-ai/schemastery'
11import type {
12 ContinuableCreateSpec,
13 ResolvedSubagentStartRequest,
14 SubagentCapabilities,
15 SubagentProvider,
16} from '@deepseek-ai/dsh-subagent'
17import { startInProcessRun } from '@deepseek-ai/dsh-subagent-in-process-driver'
18
19export const name = 'subagent-spawn-in-process'
20// `tools` is deliberately not injected: the child factory already provides it during setup,
21// and adding it here would unnecessarily change this provider's apply timing.
22export const inject = ['subagents']
23
24/** Config: the registry name to register the provider under. */
25export interface Config {
26 /** Provider name on `ctx.subagents` (default `spawn`). */
27 providerName: string
28}
29
30export const Config: z<Config> = z.object({
31 providerName: z.string().default('spawn'),
32})
33
34/**
35 * The spawn provider. Supports every start-time capability: `depthLimit` (it
36 * constructs the child, so it can enforce a recursion cap), `outputSchema`
37 * (the scoped structured runtime), `agentOptions` (merged over the parent
38 * route), and `toolFilter`/`persona` (scoped `restrict()` and a scoped
39 * shadowing persona section, applied in the child's creation window).
40 */
41class SpawnInProcessProvider implements SubagentProvider {
42 readonly capabilities: SubagentCapabilities = {
43 agentOptions: true,
44 outputSchema: true,
45 depthLimit: true,
46 toolFilter: true,
47 persona: true,
48 }
49 // Context contract: a spawned child starts fresh — it never sees the parent conversation.
50 readonly inheritsParentContext = false
51
52 constructor(readonly name: string) {}
53
54 start(request: ResolvedSubagentStartRequest) {
55 // Fresh child: no seed. The shared driver mints ids, stamps cwd/lineage/
56 // depth, drives the one-shot (including the structured capture when the
57 // request carries an outputSchema), and maps the result.
58 return startInProcessRun(request, {})
59 }
60
61 prepareContinuable(): Promise<ContinuableCreateSpec> {
62 // A spawned child starts fresh, so it contributes no seed; the continuation
63 // manager owns every later operation on it.
64 return Promise.resolve({})
65 }
66}
67
68export function apply(ctx: Context, config: Config): void {
69 ctx.subagents.registerProvider(new SpawnInProcessProvider(config.providerName))
70}