index.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * Fixed Claude Code one-shot subagent provider. Every accepted run invokes
  3. * the official Agent SDK in the delegating Session's workspace and places
  4. * the SDK-spawned real CLI under the shared subprocess owner.
  5. *
  6. * @module @deepseek-ai/dsh-subagent-claude-code
  7. */
  8. import type { Context } from '@deepseek-ai/cordis'
  9. import z from '@deepseek-ai/schemastery'
  10. import { MAX_TIMER_DELAY_MS } from '@deepseek-ai/dsh-timeout'
  11. import {
  12. assertPositiveFinite,
  13. NO_START_CAPABILITIES,
  14. resolveChildCwd,
  15. type ResolvedSubagentStartRequest,
  16. type SubagentCapabilities,
  17. type SubagentProvider,
  18. } from '@deepseek-ai/dsh-subagent'
  19. import {
  20. DEFAULT_DISPOSE_GRACE_MS,
  21. startClaudeCodeRun,
  22. type ClaudeCodeRunSpec,
  23. } from './run.ts'
  24. export const name = 'subagent-claude-code'
  25. export const inject = ['subagents', 'subprocess']
  26. /* jscpd:ignore-start -- sibling product providers intentionally expose the
  27. * same two deployment-owned fields without adding a shared config owner. */
  28. /** Deployment-owned environment and process-release bound. */
  29. export interface Config {
  30. /**
  31. * Explicit environment entries layered over the subprocess seam's
  32. * credential-scrubbed parent environment.
  33. */
  34. env?: Record<string, string>
  35. /** Grace in milliseconds for Claude Code process-tree termination. */
  36. disposeGraceMs?: number
  37. }
  38. export const Config: z<Config> = z.object({
  39. env: z.dict(z.string()).default({}),
  40. disposeGraceMs: z.number().default(DEFAULT_DISPOSE_GRACE_MS),
  41. })
  42. type ResolvedConfig = Required<Config>
  43. /* jscpd:ignore-end */
  44. /* jscpd:ignore-start -- Cordis registration and shared-seam plumbing mirror
  45. * the Codex sibling; each product's lifecycle remains package-private. */
  46. class ClaudeCodeProvider implements SubagentProvider {
  47. readonly name = 'claude-code'
  48. readonly capabilities: SubagentCapabilities = NO_START_CAPABILITIES
  49. readonly inheritsParentContext = false
  50. constructor(
  51. private readonly ctx: Context,
  52. private readonly config: ResolvedConfig,
  53. ) {}
  54. async start(request: ResolvedSubagentStartRequest) {
  55. const parentCwd = request.parent.session.header.cwd
  56. if (parentCwd === undefined) {
  57. throw new Error(
  58. 'subagent-claude-code: no working directory for the child — delegate from a parent session that has one',
  59. )
  60. }
  61. const executable = await this.ctx.subprocess.resolveExecutable(
  62. 'claude',
  63. this.config.env,
  64. request.signal,
  65. )
  66. const spec: ClaudeCodeRunSpec = {
  67. cwd: resolveChildCwd(
  68. 'subagent-claude-code',
  69. undefined,
  70. parentCwd,
  71. ),
  72. executable,
  73. env: this.config.env,
  74. disposeGraceMs: this.config.disposeGraceMs,
  75. spawn: spawnSpec => this.ctx.subprocess.spawn(spawnSpec),
  76. onError: (error, stopReason) => {
  77. this.ctx.logger.warn(
  78. `subagent-claude-code: child run failed (${stopReason}): ${error.message}`,
  79. )
  80. },
  81. }
  82. return startClaudeCodeRun(request, spec)
  83. }
  84. }
  85. /**
  86. * Register the fixed `claude-code` provider.
  87. * @param ctx - context carrying shared subagent and subprocess services.
  88. * @param config - explicit child environment and disposal grace.
  89. */
  90. export function apply(ctx: Context, config: Config): void {
  91. const resolved = config as ResolvedConfig
  92. assertPositiveFinite(
  93. 'subagent-claude-code',
  94. 'disposeGraceMs',
  95. resolved.disposeGraceMs,
  96. )
  97. if (resolved.disposeGraceMs > MAX_TIMER_DELAY_MS) {
  98. throw new Error(
  99. `subagent-claude-code: disposeGraceMs must be no greater than ${MAX_TIMER_DELAY_MS}`,
  100. )
  101. }
  102. ctx.subagents.registerProvider(new ClaudeCodeProvider(ctx, resolved))
  103. }
  104. /* jscpd:ignore-end */