index.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 `depthLimit` (it constructs the child, so it can
  42. * enforce a recursion cap) and `outputSchema` (via the shared in-process
  43. * structured runtime); NOT `toolFilter` in this cut — a request that needs it
  44. * is rejected by the service before `start` runs.
  45. */
  46. class SpawnProvider implements SubagentProvider {
  47. readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: false }
  48. // Context contract: a spawned child starts fresh — it never sees the parent conversation.
  49. readonly inheritsParentContext = false
  50. constructor(readonly name: string, private readonly ctx: Context) {}
  51. start(request: SubagentStartRequest) {
  52. // Fresh child: no seed. The shared driver mints ids, stamps cwd/lineage/
  53. // depth, drives the one-shot (including the structured capture when the
  54. // request carries an outputSchema), and maps the result.
  55. return startInProcessRun(this.ctx, request, { providerName: this.name })
  56. }
  57. }
  58. export function apply(ctx: Context, config: Config): void {
  59. ctx.subagents.registerProvider(new SpawnProvider(config.providerName, ctx))
  60. }