index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * The in-process SPAWN subagent backend: registers a {@link SubagentProvider}
  3. * on `ctx.subagents` that runs each child as a FRESH child {@link Agent} on the
  4. * same cordis context (its own session, own system prompt, zero parent
  5. * context). The cheapest transport, reusing the agent factory's quiescent
  6. * teardown.
  7. *
  8. * The run mechanics live in `@deepseek-ai/dsh-subagent-inprocess`
  9. * ({@link startInProcessRun}); this backend just passes NO seed (a fresh
  10. * child). The fork backend is an independent peer over the same driver.
  11. *
  12. * Structured output (`outputSchema`) is supported via the driver's shared
  13. * structured runtime: the backend acquires it for its plugin lifetime (so the
  14. * capture tool and request-shaping listeners exist before any run), and each
  15. * structured run holds its own acquisition until it settles.
  16. *
  17. * Plugin export shape: named `name`/`inject`/`Config`/`apply`, NO default.
  18. *
  19. * @module @deepseek-ai/dsh-subagent-spawn
  20. */
  21. import type { Context } from 'cordis'
  22. import z from 'schemastery'
  23. import type { SubagentCapabilities, SubagentProvider, SubagentStartRequest } from '@deepseek-ai/dsh-subagent'
  24. import { startInProcessRun } from '@deepseek-ai/dsh-subagent-inprocess'
  25. export const name = 'subagent-spawn'
  26. // `tools` is deliberately NOT injected: the shared driver's structured runtime
  27. // (acquired per structured RUN, not at apply) gates its own capture-tool
  28. // registration on `tools` availability, so this backend's apply timing — and
  29. // with it the provider-mirroring delegation tool's position in the
  30. // model-visible tool list — stays what it was before structured output existed.
  31. export const inject = ['subagents', 'agents']
  32. /** Config: the registry name to register the provider under. */
  33. export interface Config {
  34. /** Provider name on `ctx.subagents` (default `spawn`). */
  35. providerName: string
  36. }
  37. export const Config: z<Config> = z.object({
  38. providerName: z.string().default('spawn'),
  39. })
  40. /**
  41. * The spawn provider. Supports every start-time capability: `depthLimit` (it
  42. * constructs the child, so it can enforce a recursion cap), `outputSchema`
  43. * (the scoped structured runtime), and `toolFilter`/`persona` (scoped
  44. * `restrict()` and a scoped shadowing persona section, applied in the child's
  45. * creation window).
  46. */
  47. class SpawnProvider implements SubagentProvider {
  48. readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true, persona: true }
  49. // Context contract: a spawned child starts fresh — it never sees the parent conversation.
  50. readonly inheritsParentContext = false
  51. constructor(readonly name: string, private readonly ctx: Context) {}
  52. start(request: SubagentStartRequest) {
  53. // Fresh child: no seed. The shared driver mints ids, stamps cwd/lineage/
  54. // depth, drives the one-shot (including the structured capture when the
  55. // request carries an outputSchema), and maps the result.
  56. return startInProcessRun(this.ctx, request, { providerName: this.name })
  57. }
  58. }
  59. export function apply(ctx: Context, config: Config): void {
  60. ctx.subagents.registerProvider(new SpawnProvider(config.providerName, ctx))
  61. }