index.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * The in-process FORK subagent backend: registers a {@link SubagentProvider} on
  3. * `ctx.subagents` that runs each child as a child {@link Agent} SEEDED with a prefix of the
  4. * parent's session log — so the child inherits the parent's conversation context instead of
  5. * starting fresh. The seed ends at the last `turn/end`: the current tool-call turn is
  6. * unbalanced and cannot be replayed as a valid child session.
  7. * @module @deepseek-ai/dsh-subagent-fork
  8. */
  9. import type { Context } from 'cordis'
  10. import z from 'schemastery'
  11. import type { SessionEvent } from '@deepseek-ai/dsh-session'
  12. import type { Agent } from '@deepseek-ai/dsh-agent'
  13. import type { SubagentCapabilities, SubagentProvider, SubagentStartRequest } from '@deepseek-ai/dsh-subagent'
  14. import { startInProcessRun } from '@deepseek-ai/dsh-subagent-inprocess'
  15. export const name = 'subagent-fork'
  16. // `tools` is deliberately NOT injected — same rationale as subagent-spawn: the
  17. // per-run structured runtime gates its capture-tool registration on `tools`
  18. // itself, so this backend's apply timing (and the delegation tool's position
  19. // in the model-visible tool list) is unchanged by structured output.
  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 `fork`). */
  24. providerName: string
  25. }
  26. export const Config: z<Config> = z.object({
  27. providerName: z.string().default('fork'),
  28. })
  29. /**
  30. * The balanced completed-turn prefix of `parent`'s log: every event up to and including the
  31. * last `turn/end`. The in-flight turn is excluded; before any completed turn the child starts
  32. * fresh. Because live sequence numbers equal array indexes, the result remains a valid seed
  33. * beginning at sequence zero.
  34. * @param parent - the agent whose session log to slice.
  35. * @returns the seed events, contiguous from seq 0; empty when no turn has completed.
  36. */
  37. function completedTurnPrefix(parent: Agent): SessionEvent[] {
  38. const events = parent.session.events
  39. const lastEnd = events.findLast(e => e.type === 'turn/end')
  40. if (lastEnd === undefined) return []
  41. // seq === array index (the append contract), so slice up to and including it.
  42. return events.slice(0, lastEnd.seq + 1)
  43. }
  44. /**
  45. * The fork provider. Supports `depthLimit` and `outputSchema` (via the shared
  46. * in-process structured runtime), plus `toolFilter`/`persona` (scoped
  47. * restrict() and a scoped shadowing persona section).
  48. */
  49. class ForkProvider implements SubagentProvider {
  50. readonly capabilities: SubagentCapabilities = { outputSchema: true, depthLimit: true, toolFilter: true, persona: true }
  51. // Context contract: a forked child IS seeded with the parent's completed-turn prefix.
  52. readonly inheritsParentContext = true
  53. constructor(readonly name: string) {}
  54. start(request: SubagentStartRequest) {
  55. const seed = completedTurnPrefix(request.parent)
  56. return startInProcessRun(request, {
  57. // Only pass a seed when there's a completed turn to inherit; an empty seed
  58. // is equivalent to a fresh child, so omit it to keep the session unseeded.
  59. ...seed.length > 0 ? { seed } : {},
  60. })
  61. }
  62. }
  63. export function apply(ctx: Context, config: Config): void {
  64. ctx.subagents.registerProvider(new ForkProvider(config.providerName))
  65. }