index.ts 2.2 KB

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