Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
147 lines (137 loc) · 5.06 KB
/
Copy pathindex.ts
File metadata and controls
147 lines (137 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Profile-named Claude Code one-shot subagent provider. Every accepted run
* invokes the official Agent SDK in the delegating Session's workspace and
* places the SDK-spawned real CLI under the shared subprocess owner.
*
* @module @deepseek-ai/dsh-subagent-claude-code
*/
import type { Context } from '@deepseek-ai/cordis'
import z from '@deepseek-ai/schemastery'
import { MAX_TIMER_DELAY_MS } from '@deepseek-ai/dsh-timeout'
import {
assertPositiveFinite,
NO_START_CAPABILITIES,
assertUsableCwd,
type ResolvedSubagentStartRequest,
type SubagentCapabilities,
type SubagentProvider,
} from '@deepseek-ai/dsh-subagent'
import {
CLAUDE_CODE_PERMISSION_MODES,
DEFAULT_CLAUDE_CODE_PERMISSION_MODE,
DEFAULT_DISPOSE_GRACE_MS,
claudeCodeStartupFailure,
startClaudeCodeRun,
type ClaudeCodePermissionMode,
type ClaudeCodeRunSpec,
} from './run.ts'
export const name = 'subagent-claude-code'
export const inject = ['subagents', 'subprocess']
const DEFAULT_PROVIDER_NAME = 'claude-code'
/* jscpd:ignore-start -- sibling product providers intentionally expose
* overlapping deployment-owned fields without adding a shared config owner. */
/** Deployment-owned model, permission, environment, and process-release settings. */
export interface Config {
/** Provider name on `ctx.subagents` (default `claude-code`). */
providerName?: string
/** Native Claude model fixed for this instance; omitted to inherit Claude settings. */
model?: string
/**
* Explicit environment entries layered over the subprocess seam's
* credential-scrubbed parent environment.
*/
env?: Record<string, string>
/**
* Native non-interactive mode fixed for this Provider instance. Defaults to
* `dontAsk`; `acceptEdits` accepts edits, `auto` uses the native classifier,
* `plan` returns a plan without approving execution, and
* `bypassPermissions` explicitly skips permission checks.
*/
permissionMode?: ClaudeCodePermissionMode
/** Grace in milliseconds between Claude Code managed-range termination tiers. */
disposeGraceMs?: number
}
export const Config: z<Config> = z.object({
providerName: z.string().min(1).default(DEFAULT_PROVIDER_NAME),
model: z.string().min(1),
env: z.dict(z.string()).default({}),
permissionMode: z.union([...CLAUDE_CODE_PERMISSION_MODES])
.default(DEFAULT_CLAUDE_CODE_PERMISSION_MODE),
disposeGraceMs: z.number().default(DEFAULT_DISPOSE_GRACE_MS),
})
type ResolvedConfig = Omit<Required<Config>, 'model'> & Pick<Config, 'model'>
/* jscpd:ignore-end */
/* jscpd:ignore-start -- Cordis registration and shared-seam plumbing mirror
* the Codex sibling; each product's lifecycle remains package-private. */
class ClaudeCodeProvider implements SubagentProvider {
readonly capabilities: SubagentCapabilities = NO_START_CAPABILITIES
readonly inheritsParentContext = false
constructor(
readonly name: string,
private readonly ctx: Context,
private readonly config: ResolvedConfig,
) {}
async start(request: ResolvedSubagentStartRequest) {
let cwd: string
try {
cwd = assertUsableCwd('subagent-claude-code', 'child cwd', request.cwd)
} catch (error: unknown) {
if (request.signal.aborted) {
throw new Error(
'subagent-claude-code: request was aborted before SDK startup',
)
}
const failure = claudeCodeStartupFailure(error)
this.ctx.logger.warn(
`subagent-claude-code "${this.name}": child start failed: %o`,
failure,
)
throw failure
}
const spec: ClaudeCodeRunSpec = {
cwd,
...this.config.model === undefined ? {} : { model: this.config.model },
permissionMode: this.config.permissionMode,
env: this.config.env,
disposeGraceMs: this.config.disposeGraceMs,
spawn: spawnSpec => this.ctx.subprocess.spawn(spawnSpec),
onError: (error, stopReason) => {
this.ctx.logger.warn(
`subagent-claude-code "${this.name}": child run failed (${stopReason}): %o`,
error,
)
},
}
return startClaudeCodeRun(request, spec)
}
}
/**
* Register one Profile-named Claude Code provider.
* @param ctx - context carrying shared subagent and subprocess services.
* @param config - registry name, optional model, permission mode, child environment, and disposal grace.
*/
export function apply(ctx: Context, config: Config): void {
const resolved: ResolvedConfig = {
providerName: config.providerName ?? DEFAULT_PROVIDER_NAME,
...config.model === undefined ? {} : { model: config.model },
env: config.env as Record<string, string>,
permissionMode: config.permissionMode ?? DEFAULT_CLAUDE_CODE_PERMISSION_MODE,
disposeGraceMs: config.disposeGraceMs as number,
}
assertPositiveFinite(
'subagent-claude-code',
'disposeGraceMs',
resolved.disposeGraceMs,
)
if (resolved.disposeGraceMs > MAX_TIMER_DELAY_MS) {
throw new Error(
`subagent-claude-code: disposeGraceMs must be no greater than ${MAX_TIMER_DELAY_MS}`,
)
}
ctx.subagents.registerProvider(new ClaudeCodeProvider(
resolved.providerName,
ctx,
resolved,
))
}
/* jscpd:ignore-end */