index.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. import type { Context } from '@deepseek-ai/cordis'
  9. import z from '@deepseek-ai/schemastery'
  10. import type {
  11. ContinuableCreateSpec,
  12. ResolvedSubagentStartRequest,
  13. SubagentCapabilities,
  14. SubagentProvider,
  15. } from '@deepseek-ai/dsh-subagent'
  16. import { startInProcessRun } from '@deepseek-ai/dsh-subagent-in-process-driver'
  17. export const name = 'subagent-spawn-in-process'
  18. // `tools` is deliberately not injected: the child factory already provides it during setup,
  19. // and adding it here would unnecessarily change this provider's apply timing.
  20. export const inject = ['subagents']
  21. /** Config: the registry name to register the provider under. */
  22. export interface Config {
  23. /** Provider name on `ctx.subagents` (default `spawn`). */
  24. providerName: string
  25. }
  26. export const Config: z<Config> = z.object({
  27. providerName: z.string().default('spawn'),
  28. })
  29. /**
  30. * The spawn provider. Supports every start-time capability: `depthLimit` (it
  31. * constructs the child, so it can enforce a recursion cap), `outputSchema`
  32. * (the scoped structured runtime), and `toolFilter`/`persona` (scoped
  33. * `restrict()` and a scoped shadowing persona section, applied in the child's
  34. * creation window).
  35. */
  36. class SpawnInProcessProvider implements SubagentProvider {
  37. readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true, persona: true }
  38. // Context contract: a spawned child starts fresh — it never sees the parent conversation.
  39. readonly inheritsParentContext = false
  40. constructor(readonly name: string) {}
  41. start(request: ResolvedSubagentStartRequest) {
  42. // Fresh child: no seed. The shared driver mints ids, stamps cwd/lineage/
  43. // depth, drives the one-shot (including the structured capture when the
  44. // request carries an outputSchema), and maps the result.
  45. return startInProcessRun(request, {})
  46. }
  47. prepareContinuable(): Promise<ContinuableCreateSpec> {
  48. // A spawned child starts fresh, so it contributes no seed; the continuation
  49. // manager owns every later operation on it.
  50. return Promise.resolve({})
  51. }
  52. }
  53. export function apply(ctx: Context, config: Config): void {
  54. ctx.subagents.registerProvider(new SpawnInProcessProvider(config.providerName))
  55. }