mirror of
https://github.com/claude-code-best/claude-code.git
synced 2026-06-24 17:15:50 +00:00
refactor: 拆分 3 个过大 ACP 文件为模块化子文件(每个 <500 行)
通过 4 阶段 workflow(分析 → 计划 → 重构 → 验证)将 3 个超大的 ACP 源文件拆分为 28 个模块化子文件,每个均严格小于 500 行,且完整保留 所有公共 API(barrel 模式重导出)。 变更概要: - packages/acp-link/src/server.ts: 1800 → 20 行(barrel),新增 11 个子模块 (server/types、payload-decode、permission-mode、runtime-state、dispatch、 handlers-agent、handlers-session、acp-client、client-send、start-server、 testing-internals) - src/services/acp/agent.ts: 1297 → 33 行(barrel),新增 9 个子模块 (agent/AcpAgent、sessionTypes、permissionMode、configOptions、promptQueue、 internalAccessors、createSessionMethod、sessionLifecycle、promptFlow) - src/services/acp/bridge.ts: 1516 → 29 行(barrel),新增 8 个子模块 (bridge/types、paths、contentBlocks、toolInfo、toolResults、modelUsage、 notifications、forwarding) 验证: - bun run precheck 全通过(typecheck + lint + 5851 tests) - ACP service tests: 176 pass / 0 fail - ACP link tests: 47 pass / 0 fail - 所有外部消费者(entry.ts、permissions.ts、__tests__/)的 import 路径不变 - 测试文件零修改 迁移计划详见 docs/acp-refactor-plan.md。 Co-Authored-By: glm-5.2 <zai-org@claude-code-best.win>
This commit is contained in:
293
src/services/acp/agent/promptFlow.ts
Normal file
293
src/services/acp/agent/promptFlow.ts
Normal file
@@ -0,0 +1,293 @@
|
||||
/**
|
||||
* Prompt-flow methods for AcpAgent, attached to the prototype via
|
||||
* Object.assign. Kept in a sibling module to keep AcpAgent.ts under the
|
||||
* 500-line budget. The barrel (./index.ts) imports this module for its
|
||||
* side effect so the prototype is populated before any instance is built.
|
||||
*
|
||||
* Methods attached: prompt, setSessionConfigOption.
|
||||
*/
|
||||
import { randomUUID } from 'node:crypto'
|
||||
import type {
|
||||
PromptRequest,
|
||||
PromptResponse,
|
||||
SetSessionConfigOptionRequest,
|
||||
SetSessionConfigOptionResponse,
|
||||
} from '@agentclientprotocol/sdk'
|
||||
import type { SessionId } from '../../../types/ids.js'
|
||||
import {
|
||||
switchSession,
|
||||
getSessionProjectDir,
|
||||
} from '../../../bootstrap/state.js'
|
||||
import { forwardSessionUpdates } from '../bridge.js'
|
||||
import type { ToolUseCache } from '../bridge.js'
|
||||
import { promptToQueryInput } from '../promptConversion.js'
|
||||
import { sanitizeTitle } from '../utils.js'
|
||||
import { AcpAgent } from './AcpAgent.js'
|
||||
import type { AcpSession } from './sessionTypes.js'
|
||||
import { flattenConfigOptionValues } from './configOptions.js'
|
||||
import { popNextPendingPrompt } from './promptQueue.js'
|
||||
import {
|
||||
getConnection,
|
||||
readClientCapabilities,
|
||||
syncSessionConfigState,
|
||||
} from './internalAccessors.js'
|
||||
|
||||
// ── prompt ───────────────────────────────────────────────────────
|
||||
|
||||
async function prompt(
|
||||
this: AcpAgent,
|
||||
params: PromptRequest,
|
||||
): Promise<PromptResponse> {
|
||||
const session = this.sessions.get(params.sessionId)
|
||||
if (!session) {
|
||||
throw new Error(`Session ${params.sessionId} not found`)
|
||||
}
|
||||
|
||||
// Extract text/image content from the prompt
|
||||
const promptInput = promptToQueryInput(params.prompt)
|
||||
|
||||
// Per prompt-turn.mdx, `prompt` is a required ContentBlock[] and an
|
||||
// effectively-empty prompt is malformed input — reject it with an
|
||||
// invalid_params error rather than fabricating a successful end_turn.
|
||||
if (!promptInput.trim()) {
|
||||
throw new Error('Prompt content is empty')
|
||||
}
|
||||
|
||||
const promptCancelGeneration = session.cancelGeneration
|
||||
|
||||
// Handle prompt queuing — if a prompt is already running, queue this one
|
||||
if (session.promptRunning) {
|
||||
const promptUuid = randomUUID()
|
||||
const cancelled = await new Promise<boolean>(resolve => {
|
||||
session.pendingQueue.push(promptUuid)
|
||||
session.pendingMessages.set(promptUuid, { resolve })
|
||||
})
|
||||
if (cancelled) {
|
||||
return { stopReason: 'cancelled' }
|
||||
}
|
||||
}
|
||||
|
||||
if (session.cancelGeneration !== promptCancelGeneration) {
|
||||
return { stopReason: 'cancelled' }
|
||||
}
|
||||
|
||||
// Reset cancellation only when this prompt is about to run. Queued prompts
|
||||
// must not clear the cancellation state for the active prompt.
|
||||
session.cancelled = false
|
||||
session.promptRunning = true
|
||||
|
||||
try {
|
||||
// Reset the query engine's abort controller for a fresh query.
|
||||
// After a previous interrupt(), the internal controller is stuck in
|
||||
// aborted state — without this, submitMessage() fails immediately.
|
||||
session.queryEngine.resetAbortController()
|
||||
// Switch global session state so recordTranscript writes to the correct
|
||||
// session file. Without this, multi-session scenarios (or creating a new
|
||||
// session after another) write transcript data to the wrong file.
|
||||
switchSession(params.sessionId as SessionId, getSessionProjectDir())
|
||||
|
||||
const sdkMessages = session.queryEngine.submitMessage(promptInput)
|
||||
|
||||
const { stopReason, usage } = await forwardSessionUpdates(
|
||||
params.sessionId,
|
||||
sdkMessages,
|
||||
getConnection(this),
|
||||
session.queryEngine.getAbortSignal(),
|
||||
session.toolUseCache,
|
||||
readClientCapabilities(this),
|
||||
session.cwd,
|
||||
() => session.cancelled,
|
||||
)
|
||||
|
||||
// If the session was cancelled during processing, return cancelled
|
||||
if (session.cancelled) {
|
||||
return { stopReason: 'cancelled' }
|
||||
}
|
||||
|
||||
// Emit a session_info_update so Clients learn the session's display
|
||||
// title / last-activity timestamp via the stable v1 session/update
|
||||
// channel. The title is derived from the first user prompt.
|
||||
await emitSessionInfoUpdate(this, params.sessionId, promptInput)
|
||||
|
||||
// Per extensibility.mdx:39 the root of PromptResponse is reserved —
|
||||
// stable v1 defines only `stopReason` (+ optional `_meta`). Token usage
|
||||
// is therefore carried under the `_meta.claudeCode.usage` extension
|
||||
// namespace rather than as a non-spec root field. thoughtTokens are
|
||||
// included in totalTokens so reported totals match billable tokens;
|
||||
// until bridge.ts tracks them they are reported as 0.
|
||||
if (usage) {
|
||||
const thoughtTokens = 0
|
||||
return {
|
||||
stopReason,
|
||||
_meta: {
|
||||
claudeCode: {
|
||||
usage: {
|
||||
inputTokens: usage.inputTokens,
|
||||
outputTokens: usage.outputTokens,
|
||||
cachedReadTokens: usage.cachedReadTokens,
|
||||
cachedWriteTokens: usage.cachedWriteTokens,
|
||||
thoughtTokens,
|
||||
totalTokens:
|
||||
usage.inputTokens +
|
||||
usage.outputTokens +
|
||||
usage.cachedReadTokens +
|
||||
usage.cachedWriteTokens +
|
||||
thoughtTokens,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
return { stopReason }
|
||||
} catch (err: unknown) {
|
||||
// Treat AbortError / cancellation-shaped errors as a turn cancellation
|
||||
// regardless of the session.cancelled flag, to close the race window
|
||||
// between interrupt() firing and cancel() setting the flag. Per
|
||||
// prompt-turn.mdx the Agent MUST return `cancelled` for aborts.
|
||||
const isAbort =
|
||||
err instanceof Error &&
|
||||
(err.name === 'AbortError' ||
|
||||
/abort|cancelled|interrupt/i.test(err.message))
|
||||
if (session.cancelled || isAbort) {
|
||||
return { stopReason: 'cancelled' }
|
||||
}
|
||||
|
||||
// Check for process death errors
|
||||
if (
|
||||
err instanceof Error &&
|
||||
(err.message.includes('terminated') ||
|
||||
err.message.includes('process exited'))
|
||||
) {
|
||||
await this.teardownSession(params.sessionId)
|
||||
throw new Error(
|
||||
'The Claude Agent process exited unexpectedly. Please start a new session.',
|
||||
)
|
||||
}
|
||||
|
||||
throw err
|
||||
} finally {
|
||||
// Resolve next pending prompt if any
|
||||
const nextPrompt = popNextPendingPrompt(session)
|
||||
if (nextPrompt) {
|
||||
session.promptRunning = true
|
||||
nextPrompt.resolve(false)
|
||||
} else {
|
||||
session.promptRunning = false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── setSessionConfigOption ───────────────────────────────────────
|
||||
|
||||
async function setSessionConfigOption(
|
||||
this: AcpAgent,
|
||||
params: SetSessionConfigOptionRequest,
|
||||
): Promise<SetSessionConfigOptionResponse> {
|
||||
const session = this.sessions.get(params.sessionId)
|
||||
if (!session) {
|
||||
throw new Error('Session not found')
|
||||
}
|
||||
if (typeof params.value !== 'string') {
|
||||
throw new Error(
|
||||
`Invalid value for config option ${params.configId}: ${String(params.value)}`,
|
||||
)
|
||||
}
|
||||
|
||||
const option = session.configOptions.find(o => o.id === params.configId)
|
||||
if (!option) {
|
||||
throw new Error(`Unknown config option: ${params.configId}`)
|
||||
}
|
||||
|
||||
// Per session-config-options.mdx: value MUST be one of the values listed
|
||||
// in the option's options array. Reject unknown values with an error
|
||||
// rather than silently persisting them. Only `select` options carry an
|
||||
// options array; `boolean` options have no enumerated values.
|
||||
if (option.type === 'select') {
|
||||
const validValues = flattenConfigOptionValues(
|
||||
(option as { options?: unknown }).options,
|
||||
)
|
||||
if (!validValues.includes(params.value)) {
|
||||
throw new Error(
|
||||
`Invalid value '${params.value}' for config option ${params.configId}; must be one of: ${validValues.join(', ')}`,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
const value = params.value
|
||||
|
||||
if (params.configId === 'mode') {
|
||||
this.applySessionMode(params.sessionId, value)
|
||||
await getConnection(this).sessionUpdate({
|
||||
sessionId: params.sessionId,
|
||||
update: {
|
||||
sessionUpdate: 'current_mode_update',
|
||||
currentModeId: value,
|
||||
},
|
||||
})
|
||||
} else if (params.configId === 'model') {
|
||||
session.queryEngine.setModel(value)
|
||||
}
|
||||
|
||||
syncSessionConfigState(this, session, params.configId, value)
|
||||
|
||||
session.configOptions = session.configOptions.map(o =>
|
||||
o.id === params.configId && typeof o.currentValue === 'string'
|
||||
? { ...o, currentValue: value }
|
||||
: o,
|
||||
)
|
||||
|
||||
return { configOptions: session.configOptions }
|
||||
}
|
||||
|
||||
// ── Private-field accessors ──────────────────────────────────────
|
||||
//
|
||||
// getConnection / readClientCapabilities / syncSessionConfigState are
|
||||
// imported from ./internalAccessors.js (shared with sessionLifecycle.ts and
|
||||
// createSessionMethod.ts). The session_info_update helper below is local to
|
||||
// this module because it is only called from prompt().
|
||||
|
||||
/**
|
||||
* Emit a session_info_update notification carrying a derived session title
|
||||
* (truncated first user prompt) and the current last-activity timestamp.
|
||||
* Sent once per session — subsequent turns reuse the same title.
|
||||
*
|
||||
* This logic was originally the private `AcpAgent.maybeEmitSessionInfoUpdate`
|
||||
* method on the shell class. It is only called from the prompt flow, so it
|
||||
* lives here to avoid the `noUnusedPrivateClassMembers` false positive that
|
||||
* cast-based access would otherwise trigger on the shell.
|
||||
*/
|
||||
async function emitSessionInfoUpdate(
|
||||
agent: AcpAgent,
|
||||
sessionId: string,
|
||||
firstPrompt: string,
|
||||
): Promise<void> {
|
||||
const session = agent.sessions.get(sessionId)
|
||||
if (!session) return
|
||||
// sessionInfoTitleSent is tracked via toolUseCache to avoid reshaping
|
||||
// AcpSession; use a dedicated per-session flag instead.
|
||||
const cache = session.toolUseCache as ToolUseCache & {
|
||||
__sessionInfoTitleSent?: boolean
|
||||
}
|
||||
if (cache.__sessionInfoTitleSent) return
|
||||
cache.__sessionInfoTitleSent = true
|
||||
const title = sanitizeTitle(firstPrompt).slice(0, 100)
|
||||
try {
|
||||
await getConnection(agent).sessionUpdate({
|
||||
sessionId,
|
||||
update: {
|
||||
sessionUpdate: 'session_info_update',
|
||||
...(title ? { title } : {}),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('[ACP] Failed to send session_info_update:', err)
|
||||
}
|
||||
}
|
||||
|
||||
// ── Prototype attachment ─────────────────────────────────────────
|
||||
|
||||
Object.assign(AcpAgent.prototype, {
|
||||
prompt,
|
||||
setSessionConfigOption,
|
||||
})
|
||||
Reference in New Issue
Block a user