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:
claude-code-best
2026-06-19 15:39:01 +08:00
parent 35768837a7
commit 65f81de52b
32 changed files with 5481 additions and 4591 deletions

View File

@@ -0,0 +1,54 @@
/**
* Internal accessors for AcpAgent private fields and session-state helpers,
* shared across the prototype-augmentation modules (createSessionMethod /
* sessionLifecycle / promptFlow).
*
* AcpAgent's `conn` and `clientCapabilities` fields are declared `private`
* on the shell class. TS-only privacy (no #) means bracket access still
* works at runtime, but we cast through `unknown` to keep tsc strict happy
* without widening the public API surface of the class.
*/
import type {
AgentSideConnection,
ClientCapabilities,
} from '@agentclientprotocol/sdk'
import type { AcpAgent } from './AcpAgent.js'
import type { AcpSession } from './sessionTypes.js'
type AcpAgentInternals = {
conn: AgentSideConnection
clientCapabilities: ClientCapabilities | undefined
}
export function getConnection(agent: AcpAgent): AgentSideConnection {
return (agent as unknown as AcpAgentInternals).conn
}
export function readClientCapabilities(
agent: AcpAgent,
): ClientCapabilities | undefined {
return (agent as unknown as AcpAgentInternals).clientCapabilities
}
/**
* Update the session's current mode/model id based on the configId.
*
* This logic was originally the private `AcpAgent.syncSessionConfigState`
* method on the shell class. It is called by the prototype-augmented
* `updateConfigOption` (sessionLifecycle.ts) and `setSessionConfigOption`
* (promptFlow.ts). Moving it here keeps it next to its only callers and
* avoids the `noUnusedPrivateClassMembers` false positive that the
* cast-based access would otherwise trigger on the shell.
*/
export function syncSessionConfigState(
_agent: AcpAgent,
session: AcpSession,
configId: string,
value: string,
): void {
if (configId === 'mode') {
session.modes = { ...session.modes, currentModeId: value }
} else if (configId === 'model') {
session.models = { ...session.models, currentModelId: value }
}
}