2026-08-04 20:57:55 +08:00
|
|
|
/**
|
2026-08-18 01:16:29 +08:00
|
|
|
* 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.
|
2026-08-04 20:57:55 +08:00
|
|
|
*
|
|
|
|
|
* @module @deepseek-ai/dsh-subagent-claude-code
|
|
|
|
|
*/
|
|
|
|
|
|
build(vendor): rescope the vendored Cordis packages into @deepseek-ai
Machine-produced by `pnpm run rescope-vendor --apply` plus the regeneration it
prints: `pnpm install` for the lockfile, `pnpm run gen-third-party-notices`,
`verify-translation-pairing --write` for the touched bilingual pairs,
`gen-doc-graphs`, and one typert snapshot whose ids embed character offsets.
`pnpm run rescope-vendor --check` verifies the result.
Renames nine vendored packages (cordis, cosmokit, schemastery and the six
@cordisjs plugins) and every reference that resolves them: manifest names and
dependency keys, module specifiers including declare-module merges, cordis.yml
plugin names, tsconfig paths, every Markdown fence, and `docs/` prose.
Directory names, upstream versions, and dependency ranges are unchanged, so
vendor/README.md still reads as an upstream snapshot; its manifest table gains
an upstream-name column so THIRD_PARTY_NOTICES keeps MIT attribution pointed
at each fork's origin.
The tutorial tier follows the rename end to end: its yaml fences named plugins
the Loader can no longer resolve, its `ts ignore-check` fences disagreed with
the compiled fences beside them, and its prose quoted both. The contracts that
told readers to keep upstream names — the root convention and the vendoring
cookbook's tree comment and manifest invariant — now say to rescope instead.
Two rules read `@deepseek-ai/` as "another workspace plugin": the client bundle
purity gate now names the vendored libraries a browser bundle inlines, and the
files where a bare `cordis` is an agent-preset id keep that product data.
2026-08-10 22:04:06 +08:00
|
|
|
import type { Context } from '@deepseek-ai/cordis'
|
|
|
|
|
import z from '@deepseek-ai/schemastery'
|
2026-08-05 04:41:27 +08:00
|
|
|
import { MAX_TIMER_DELAY_MS } from '@deepseek-ai/dsh-timeout'
|
2026-08-04 20:57:55 +08:00
|
|
|
import {
|
|
|
|
|
assertPositiveFinite,
|
|
|
|
|
NO_START_CAPABILITIES,
|
|
|
|
|
resolveChildCwd,
|
|
|
|
|
type ResolvedSubagentStartRequest,
|
|
|
|
|
type SubagentCapabilities,
|
|
|
|
|
type SubagentProvider,
|
|
|
|
|
} from '@deepseek-ai/dsh-subagent'
|
|
|
|
|
import {
|
2026-08-15 16:36:32 +08:00
|
|
|
CLAUDE_CODE_PERMISSION_MODES,
|
|
|
|
|
DEFAULT_CLAUDE_CODE_PERMISSION_MODE,
|
2026-08-04 20:57:55 +08:00
|
|
|
DEFAULT_DISPOSE_GRACE_MS,
|
2026-08-18 01:43:20 +08:00
|
|
|
claudeCodeStartupFailure,
|
2026-08-04 20:57:55 +08:00
|
|
|
startClaudeCodeRun,
|
2026-08-15 16:36:32 +08:00
|
|
|
type ClaudeCodePermissionMode,
|
2026-08-04 20:57:55 +08:00
|
|
|
type ClaudeCodeRunSpec,
|
|
|
|
|
} from './run.ts'
|
|
|
|
|
|
|
|
|
|
export const name = 'subagent-claude-code'
|
|
|
|
|
export const inject = ['subagents', 'subprocess']
|
|
|
|
|
|
2026-08-18 01:16:29 +08:00
|
|
|
const DEFAULT_PROVIDER_NAME = 'claude-code'
|
|
|
|
|
|
2026-08-17 17:05:18 +08:00
|
|
|
/* jscpd:ignore-start -- sibling product providers intentionally expose
|
|
|
|
|
* overlapping deployment-owned fields without adding a shared config owner. */
|
2026-08-15 16:36:32 +08:00
|
|
|
/** Deployment-owned permission, environment, and process-release settings. */
|
2026-08-04 20:57:55 +08:00
|
|
|
export interface Config {
|
2026-08-18 01:16:29 +08:00
|
|
|
/** Provider name on `ctx.subagents` (default `claude-code`). */
|
|
|
|
|
providerName?: string
|
2026-08-04 20:57:55 +08:00
|
|
|
/**
|
|
|
|
|
* Explicit environment entries layered over the subprocess seam's
|
|
|
|
|
* credential-scrubbed parent environment.
|
|
|
|
|
*/
|
|
|
|
|
env?: Record<string, string>
|
2026-08-15 17:46:13 +08:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
2026-08-15 16:36:32 +08:00
|
|
|
permissionMode?: ClaudeCodePermissionMode
|
2026-08-04 20:57:55 +08:00
|
|
|
/** Grace in milliseconds for Claude Code process-tree termination. */
|
|
|
|
|
disposeGraceMs?: number
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const Config: z<Config> = z.object({
|
2026-08-18 01:16:29 +08:00
|
|
|
providerName: z.string().min(1).default(DEFAULT_PROVIDER_NAME),
|
2026-08-04 20:57:55 +08:00
|
|
|
env: z.dict(z.string()).default({}),
|
2026-08-15 16:36:32 +08:00
|
|
|
permissionMode: z.union([...CLAUDE_CODE_PERMISSION_MODES])
|
|
|
|
|
.default(DEFAULT_CLAUDE_CODE_PERMISSION_MODE),
|
2026-08-04 20:57:55 +08:00
|
|
|
disposeGraceMs: z.number().default(DEFAULT_DISPOSE_GRACE_MS),
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
type ResolvedConfig = Required<Config>
|
|
|
|
|
/* 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(
|
2026-08-18 01:16:29 +08:00
|
|
|
readonly name: string,
|
2026-08-04 20:57:55 +08:00
|
|
|
private readonly ctx: Context,
|
|
|
|
|
private readonly config: ResolvedConfig,
|
|
|
|
|
) {}
|
|
|
|
|
|
2026-08-10 12:45:05 +08:00
|
|
|
async start(request: ResolvedSubagentStartRequest) {
|
2026-08-05 07:32:27 +08:00
|
|
|
const parentCwd = request.parent.session.header.cwd
|
|
|
|
|
if (parentCwd === undefined) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'subagent-claude-code: no working directory for the child — delegate from a parent session that has one',
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-08-18 01:43:20 +08:00
|
|
|
let cwd: string
|
|
|
|
|
try {
|
|
|
|
|
cwd = resolveChildCwd(
|
2026-08-04 20:57:55 +08:00
|
|
|
'subagent-claude-code',
|
|
|
|
|
undefined,
|
2026-08-05 07:32:27 +08:00
|
|
|
parentCwd,
|
2026-08-18 01:43:20 +08:00
|
|
|
)
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
if (request.signal.aborted) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'subagent-claude-code: request was aborted before SDK startup',
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-08-18 05:17:47 +08:00
|
|
|
const failure = claudeCodeStartupFailure(error)
|
|
|
|
|
this.ctx.logger.warn(
|
2026-08-18 19:36:16 +08:00
|
|
|
`subagent-claude-code "${this.name}": child start failed: %o`,
|
2026-08-18 05:17:47 +08:00
|
|
|
failure,
|
|
|
|
|
)
|
|
|
|
|
throw failure
|
2026-08-18 01:43:20 +08:00
|
|
|
}
|
|
|
|
|
const spec: ClaudeCodeRunSpec = {
|
|
|
|
|
cwd,
|
2026-08-15 16:36:32 +08:00
|
|
|
permissionMode: this.config.permissionMode,
|
2026-08-04 20:57:55 +08:00
|
|
|
env: this.config.env,
|
|
|
|
|
disposeGraceMs: this.config.disposeGraceMs,
|
|
|
|
|
spawn: spawnSpec => this.ctx.subprocess.spawn(spawnSpec),
|
|
|
|
|
onError: (error, stopReason) => {
|
|
|
|
|
this.ctx.logger.warn(
|
2026-08-18 19:36:16 +08:00
|
|
|
`subagent-claude-code "${this.name}": child run failed (${stopReason}): %o`,
|
2026-08-18 01:43:20 +08:00
|
|
|
error,
|
2026-08-04 20:57:55 +08:00
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
return startClaudeCodeRun(request, spec)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-08-18 01:16:29 +08:00
|
|
|
* Register one Profile-named Claude Code provider.
|
2026-08-04 20:57:55 +08:00
|
|
|
* @param ctx - context carrying shared subagent and subprocess services.
|
2026-08-18 01:16:29 +08:00
|
|
|
* @param config - registry name, permission mode, child environment, and disposal grace.
|
2026-08-04 20:57:55 +08:00
|
|
|
*/
|
|
|
|
|
export function apply(ctx: Context, config: Config): void {
|
2026-08-15 16:36:32 +08:00
|
|
|
const resolved: ResolvedConfig = {
|
2026-08-18 01:16:29 +08:00
|
|
|
providerName: config.providerName ?? DEFAULT_PROVIDER_NAME,
|
2026-08-15 16:36:32 +08:00
|
|
|
env: config.env as Record<string, string>,
|
|
|
|
|
permissionMode: config.permissionMode ?? DEFAULT_CLAUDE_CODE_PERMISSION_MODE,
|
|
|
|
|
disposeGraceMs: config.disposeGraceMs as number,
|
|
|
|
|
}
|
2026-08-04 20:57:55 +08:00
|
|
|
assertPositiveFinite(
|
|
|
|
|
'subagent-claude-code',
|
|
|
|
|
'disposeGraceMs',
|
|
|
|
|
resolved.disposeGraceMs,
|
|
|
|
|
)
|
2026-08-05 04:41:27 +08:00
|
|
|
if (resolved.disposeGraceMs > MAX_TIMER_DELAY_MS) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
`subagent-claude-code: disposeGraceMs must be no greater than ${MAX_TIMER_DELAY_MS}`,
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-08-18 01:16:29 +08:00
|
|
|
ctx.subagents.registerProvider(new ClaudeCodeProvider(
|
|
|
|
|
resolved.providerName,
|
|
|
|
|
ctx,
|
|
|
|
|
resolved,
|
|
|
|
|
))
|
2026-08-04 20:57:55 +08:00
|
|
|
}
|
|
|
|
|
/* jscpd:ignore-end */
|