index.ts 2.9 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 through the driver's
  13. * per-child scoped runtime: the child registers its real-schema capture tool,
  14. * prompt instruction, and enforcement listeners inside the creation setup
  15. * window, and its scope owns their lifetime.
  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 registers structured
  27. // output through the child's creation context, whose factory already requires
  28. // the tool service. Keeping it out of this backend's inject list preserves the
  29. // provider's independent apply timing.
  30. export const inject = ['subagents']
  31. /** Config: the registry name to register the provider under. */
  32. export interface Config {
  33. /** Provider name on `ctx.subagents` (default `spawn`). */
  34. providerName: string
  35. }
  36. export const Config: z<Config> = z.object({
  37. providerName: z.string().default('spawn'),
  38. })
  39. /**
  40. * The spawn provider. Supports every start-time capability: `depthLimit` (it
  41. * constructs the child, so it can enforce a recursion cap), `outputSchema`
  42. * (the scoped structured runtime), and `toolFilter`/`persona` (scoped
  43. * `restrict()` and a scoped shadowing persona section, applied in the child's
  44. * creation window).
  45. */
  46. class SpawnProvider implements SubagentProvider {
  47. readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true, persona: true }
  48. // Context contract: a spawned child starts fresh — it never sees the parent conversation.
  49. readonly inheritsParentContext = false
  50. constructor(readonly name: string) {}
  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(request, {})
  56. }
  57. }
  58. export function apply(ctx: Context, config: Config): void {
  59. ctx.subagents.registerProvider(new SpawnProvider(config.providerName))
  60. }