@yitom/acp-client 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +119 -0
- package/dist/auth/auth-method-order.d.ts +9 -0
- package/dist/auth/connect-auth-decision.d.ts +18 -0
- package/dist/auth/connect-auth-gate.d.ts +35 -0
- package/dist/client/acp-client.d.ts +110 -0
- package/dist/client/acp-fs.d.ts +15 -0
- package/dist/client/acp-paths.d.ts +8 -0
- package/dist/client/acp-proxy-service.d.ts +14 -0
- package/dist/client/acp-terminal.d.ts +36 -0
- package/dist/client/client-handlers.d.ts +27 -0
- package/dist/client/sdk-client.d.ts +32 -0
- package/dist/client/session-open.d.ts +39 -0
- package/dist/contracts/index.d.ts +277 -0
- package/dist/index.cjs +3279 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +3242 -0
- package/dist/mcp/inkdown-mcp-rpc.d.ts +14 -0
- package/dist/mcp/inkdown-mcp-toc-tools.d.ts +3 -0
- package/dist/mcp/inkdown-mcp-tools.d.ts +31 -0
- package/dist/process/process-manager.d.ts +59 -0
- package/dist/runtimes/agy/index.d.ts +19 -0
- package/dist/runtimes/claude/index.d.ts +13 -0
- package/dist/runtimes/codex/codex-auth-preflight.d.ts +10 -0
- package/dist/runtimes/codex/codex-provider-home.d.ts +20 -0
- package/dist/runtimes/codex/codex-provider.d.ts +13 -0
- package/dist/runtimes/codex/index.d.ts +16 -0
- package/dist/runtimes/copilot/index.d.ts +14 -0
- package/dist/runtimes/cursor/index.d.ts +66 -0
- package/dist/runtimes/deepseek/index.d.ts +34 -0
- package/dist/runtimes/gemini/index.d.ts +13 -0
- package/dist/runtimes/index.d.ts +54 -0
- package/dist/runtimes/opencode/index.d.ts +28 -0
- package/dist/session/agent-registry.d.ts +4 -0
- package/dist/session/config-options.d.ts +17 -0
- package/dist/session/session-capabilities.d.ts +10 -0
- package/dist/transport/terminal-output-buffer.d.ts +11 -0
- package/docs/adding-an-agent.md +121 -0
- package/docs/api-reference.md +104 -0
- package/docs/getting-started.md +164 -0
- package/docs/tauri-integration.md +107 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# @yitom/acp-client — Headless ACP (Agent Client Protocol) SDK
|
|
2
|
+
|
|
3
|
+
通用的 **ACP (Agent Client Protocol)** 纯 TypeScript / Node.js 客户端 SDK。零 UI / 零 Electron 强依赖,可独立打包发布到 NPM,在任何命令行 CLI、Node.js 后端服务、Electron 或 Tauri 桌面端中跨项目直接调用。
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 📚 详细文档导航
|
|
8
|
+
|
|
9
|
+
为了方便在不同场景下深入接入与定制,我们在 [`docs/`](./docs/) 目录下提供了专项详细指南:
|
|
10
|
+
|
|
11
|
+
| 文档 | 链接 | 内容说明 |
|
|
12
|
+
| :--- | :--- | :--- |
|
|
13
|
+
| **快速入门** | [Getting Started](./docs/getting-started.md) | 在 CLI 工具、Web/后端服务、Electron 主进程中接入的标准范式 |
|
|
14
|
+
| **API 参考** | [API Reference](./docs/api-reference.md) | `AcpClient` 构造参数、方法、事件回调接口与会话控制详述 |
|
|
15
|
+
| **新增 Agent 适配** | [Adding an Agent](./docs/adding-an-agent.md) | 插件式适配器开发指南(如何为新 Agent 编写凭证探测与 CLI 接线) |
|
|
16
|
+
| **Tauri 集成实战** | [Tauri Integration](./docs/tauri-integration.md) | 通过 Bun 编译独立单文件二进制,作为 Tauri Sidecar 伴生进程运行 |
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
## ⚡ 核心特性
|
|
21
|
+
|
|
22
|
+
- **多 Agent 统一适配(8 大主流运行时)**:
|
|
23
|
+
开箱即用支持 `codex-acp` (ChatGPT)、`claude`、`cursor-cli`、`deepseek`、`gemini`、`copilot`、`opencode`、`agy`。新增 Agent 只需在该包实现一个适配器,全局所有项目自动生效。
|
|
24
|
+
- **纯 Node.js / Headless 架构**:
|
|
25
|
+
不依赖任何 Electron API、React 或特定前端框架。配置目录跨平台自动检测回落(支持自定义 `dataDir`)。
|
|
26
|
+
- **生命周期全托管**:
|
|
27
|
+
负责跨平台后台进程拉起(Windows PID 级联杀进程)、Stdio ndJson 传输桥接、协议初始化(Initialize 握手)、温进程池长驻复用、错误诊断(stderr 尾部日志聚合)。
|
|
28
|
+
- **会话状态机与容错**:
|
|
29
|
+
自动处理 `resume` → `load` → `new` 会话恢复流程,处理模型方言(models 数组/对象)、无 MCP 自动降级与网络抖动重试。
|
|
30
|
+
- **解耦的事件钩子与审批**:
|
|
31
|
+
工具调用审批、权限请求、会话流式更新采用标准的回调与事件驱动,宿主自由决定如何确认(命令行交互 / 桌面弹窗 / Web 消息)。
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
35
|
+
## 🤖 运行时与认证支持矩阵
|
|
36
|
+
|
|
37
|
+
| 运行时 ID | 显示名称 | 本机凭据探测路径 / 环境变量 | 预检 / 启动特性 |
|
|
38
|
+
| :--- | :--- | :--- | :--- |
|
|
39
|
+
| `codex-acp` | ChatGPT | `~/.codex/auth.json` / 自定义供应商 API Key | 支持隔离 `CODEX_HOME` 生成 `config.toml` |
|
|
40
|
+
| `claude` | Claude | `~/.claude.json` / `ANTHROPIC_API_KEY` | 自动复用本机 Claude Code CLI 登录态 |
|
|
41
|
+
| `cursor-cli` | Cursor | `CURSOR_API_KEY` / 系统 Keychain 登录态 | 自动探测 `%LOCALAPPDATA%\cursor-agent\agent.cmd` |
|
|
42
|
+
| `deepseek` | DeepSeek | `DEEPSEEK_API_KEY` | 缺 key 提前拦截判停,避免子进程秒退 |
|
|
43
|
+
| `gemini` | Gemini | `~/.gemini/oauth_creds.json` / `GEMINI_API_KEY` | 自动探测 OAuth 凭证与系统 Key |
|
|
44
|
+
| `copilot` | Copilot | `~/.copilot/config.json` / `GH_TOKEN` | 自动提取 `loggedInUsers` 凭证列表 |
|
|
45
|
+
| `opencode` | OpenCode | `$XDG_DATA_HOME/opencode/auth.json` | 遵循 XDG / `%LOCALAPPDATA%` 规范 |
|
|
46
|
+
| `agy` | agy | 官方桥接器免密直连 | `tryDirectSessionFirst` 免鉴权直通 |
|
|
47
|
+
|
|
48
|
+
---
|
|
49
|
+
|
|
50
|
+
## 🚀 快速上手 (1 分钟代码)
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { AcpClient } from '@yitom/acp-client'
|
|
54
|
+
|
|
55
|
+
// 1. 初始化客户端
|
|
56
|
+
const client = new AcpClient({
|
|
57
|
+
runtime: 'cursor-cli', // 或 'claude' | 'codex-acp' | 'deepseek' 等
|
|
58
|
+
workspaceRoot: process.cwd(),
|
|
59
|
+
clientInfo: {
|
|
60
|
+
name: 'my-custom-cli',
|
|
61
|
+
version: '1.0.0',
|
|
62
|
+
},
|
|
63
|
+
callbacks: {
|
|
64
|
+
onStatusChange: (status, detail) => {
|
|
65
|
+
console.log(`[Status] -> ${status}`, detail ?? '')
|
|
66
|
+
},
|
|
67
|
+
onPermissionRequest: async ({ toolCall }) => {
|
|
68
|
+
console.log(`Agent 请求调用工具:`, toolCall)
|
|
69
|
+
return { outcome: 'approved' } // 交互式批准或自动放行
|
|
70
|
+
},
|
|
71
|
+
onSessionUpdate: (event) => {
|
|
72
|
+
console.log(`[Stream Event]:`, event)
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
// 2. 启动并连接 Agent
|
|
78
|
+
const connectResult = await client.connect()
|
|
79
|
+
if (!connectResult.ok) {
|
|
80
|
+
console.error('连接失败:', connectResult.error)
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 3. 创建/恢复会话
|
|
85
|
+
const sessionResult = await client.createSession()
|
|
86
|
+
if (sessionResult.ok) {
|
|
87
|
+
const session = sessionResult.data
|
|
88
|
+
// 4. 发送提示词 / 消息交互
|
|
89
|
+
await session.prompt('请帮我分析当前项目目录结构')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 5. 随时安全断开连接(清理孤儿进程)
|
|
93
|
+
await client.disconnect()
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## 📦 打包构建
|
|
99
|
+
|
|
100
|
+
在本包根目录一键构建生成 `dist`(ESM / CJS / `.d.ts`):
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
bun run build:acp
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
产物说明:
|
|
107
|
+
- `dist/index.js`:ESM 格式,适合现代构建工具(Vite、Webpack)与 ESM Node.js 环境;
|
|
108
|
+
- `dist/index.cjs`:CommonJS 格式,适合传统 Node.js 环境(`require`);
|
|
109
|
+
- `dist/index.d.ts`:完整 TypeScript 类型声明,支持 IDE 智能感知。
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## 🧪 运行独立验证示例
|
|
114
|
+
|
|
115
|
+
我们在 [`examples/standalone-acp-demo.ts`](./examples/standalone-acp-demo.ts) 提供了可在终端直接运行的验证脚本:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
bun run packages/acp/examples/standalone-acp-demo.ts
|
|
119
|
+
```
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AcpAuthMethod } from "../contracts";
|
|
2
|
+
import type { CodexAuthPreflight } from "../contracts";
|
|
3
|
+
/**
|
|
4
|
+
* 已有本机登录时,静默 authenticate 的尝试顺序。
|
|
5
|
+
* auth.json → 优先 ChatGPT;环境变量 API Key → 优先 API Key。
|
|
6
|
+
* 避免「列表第一项是 API Key」却已有 ChatGPT 登录时误弹窗。
|
|
7
|
+
*/
|
|
8
|
+
export declare function orderSilentAuthMethodIds(methods: AcpAuthMethod[], preflight: Pick<CodexAuthPreflight, 'hasAuthFile' | 'hasApiKeyEnv' | 'looksLoggedIn'>): string[];
|
|
9
|
+
export declare function shouldPromptAuthWizard(methods: AcpAuthMethod[], preflight: Pick<CodexAuthPreflight, 'looksLoggedIn'>): boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { AcpAuthMethod } from "../contracts";
|
|
2
|
+
import type { CodexAuthPreflight } from "../contracts";
|
|
3
|
+
export type ConnectAuthDecision = {
|
|
4
|
+
action: 'skip_auth';
|
|
5
|
+
} | {
|
|
6
|
+
action: 'needs_auth';
|
|
7
|
+
methods: AcpAuthMethod[];
|
|
8
|
+
} | {
|
|
9
|
+
action: 'silent';
|
|
10
|
+
methodIds: string[];
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* 连接阶段认证决策(纯函数,便于单测 / 后续 E2E mock 对齐)。
|
|
14
|
+
* - 无 authMethods → 跳过
|
|
15
|
+
* - 未登录 → 弹向导(方法按偏好排序)
|
|
16
|
+
* - 已登录 → 静默按序尝试 methodIds
|
|
17
|
+
*/
|
|
18
|
+
export declare function decideConnectAuth(methods: AcpAuthMethod[], preflight: Pick<CodexAuthPreflight, 'looksLoggedIn' | 'hasAuthFile' | 'hasApiKeyEnv'>): ConnectAuthDecision;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { AcpAuthMethod } from "../contracts";
|
|
2
|
+
import type { CodexAuthPreflight } from "../contracts";
|
|
3
|
+
export type ConnectAuthGateResult = {
|
|
4
|
+
outcome: 'skip_auth';
|
|
5
|
+
} | {
|
|
6
|
+
outcome: 'needs_auth';
|
|
7
|
+
methods: AcpAuthMethod[];
|
|
8
|
+
} | {
|
|
9
|
+
outcome: 'authenticated';
|
|
10
|
+
methodId: string;
|
|
11
|
+
}
|
|
12
|
+
/** 静默认证全失败,但 session/new 仍可用(部分 Agent 允许无显式 authenticate) */
|
|
13
|
+
| {
|
|
14
|
+
outcome: 'session_without_auth';
|
|
15
|
+
};
|
|
16
|
+
export interface ConnectAuthGateDeps {
|
|
17
|
+
authenticate: (methodId: string) => Promise<void>;
|
|
18
|
+
/** 静默认证全失败后的兜底;返回 true 表示已建会话 */
|
|
19
|
+
tryOpenSessionWithoutAuth?: () => Promise<boolean>;
|
|
20
|
+
/**
|
|
21
|
+
* 是否优先尝试直接建立会话复用本机认证(例如 codex-acp 本地已有 auth.json 时 session/new 即可复用,避免 authenticate 唤起浏览器)
|
|
22
|
+
*/
|
|
23
|
+
preferDirectSession?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* keychain 化兜底(cursor / opencode):文件探针未命中不断言未登录,
|
|
26
|
+
* 在 needs_auth 分支前先试一次直接建会话,成功则免弹向导。
|
|
27
|
+
* 缺省视为 false,其余 runtime 不动。
|
|
28
|
+
*/
|
|
29
|
+
tryDirectSessionFirst?: boolean;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* 连接阶段认证门闩(mock Agent + preflight 可测)。
|
|
33
|
+
* 对齐:有 auth.json → 静默复用;无登录痕迹 → 弹向导。
|
|
34
|
+
*/
|
|
35
|
+
export declare function runConnectAuthGate(authMethods: AcpAuthMethod[], preflight: Pick<CodexAuthPreflight, 'looksLoggedIn' | 'hasAuthFile' | 'hasApiKeyEnv'>, deps: ConnectAuthGateDeps): Promise<ConnectAuthGateResult>;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { type InitializeResponse } from '@agentclientprotocol/sdk';
|
|
2
|
+
import type { AcpConnectionStatus, AcpPermissionOutcome, AcpProxySettings, AcpRuntimeInfo, AcpSessionRestoreMethod, AppError, InkdownVirtualResource } from "../contracts";
|
|
3
|
+
import { type Result } from "../contracts";
|
|
4
|
+
export interface AcpClientCallbacks {
|
|
5
|
+
/** 权限审批回调:当 Agent 尝试执行工具调用或敏感操作时触发 */
|
|
6
|
+
onPermissionRequest?: (payload: {
|
|
7
|
+
sessionId?: string;
|
|
8
|
+
toolCall?: {
|
|
9
|
+
id?: string;
|
|
10
|
+
title?: string;
|
|
11
|
+
kind?: string;
|
|
12
|
+
};
|
|
13
|
+
params: Record<string, unknown>;
|
|
14
|
+
}) => Promise<AcpPermissionOutcome>;
|
|
15
|
+
/** 会话内容流式/状态更新通知 */
|
|
16
|
+
onSessionUpdate?: (params: Record<string, unknown>) => void;
|
|
17
|
+
/** 连接状态机变化通知 */
|
|
18
|
+
onStatusChange?: (status: AcpConnectionStatus, detail?: string) => void;
|
|
19
|
+
/** Agent 进程 stderr 输出行(供排错与诊断) */
|
|
20
|
+
onStderrLine?: (line: string) => void;
|
|
21
|
+
/** 虚拟文件读取(默认不支持,除非宿主提供) */
|
|
22
|
+
readSnapshot?: (resource: InkdownVirtualResource) => Promise<string>;
|
|
23
|
+
/** 日志打印 */
|
|
24
|
+
onLog?: (level: 'info' | 'warn' | 'error', message: string, meta?: unknown) => void;
|
|
25
|
+
}
|
|
26
|
+
export interface AcpClientOptions {
|
|
27
|
+
/** 运行时标识,如 'codex-acp', 'claude', 'cursor-cli', 'deepseek', 'gemini' 等 */
|
|
28
|
+
runtime?: string | AcpRuntimeInfo;
|
|
29
|
+
/** 工作区根目录路径(必需) */
|
|
30
|
+
workspaceRoot: string;
|
|
31
|
+
/** 客户端信息声明(向 Agent 表明调用方身份) */
|
|
32
|
+
clientInfo?: {
|
|
33
|
+
name: string;
|
|
34
|
+
version?: string;
|
|
35
|
+
};
|
|
36
|
+
/** 数据持久化目录(缺省自动根据操作系统返回标准路径) */
|
|
37
|
+
dataDir?: string;
|
|
38
|
+
/** 代理配置(覆盖默认配置文件) */
|
|
39
|
+
proxy?: Partial<AcpProxySettings>;
|
|
40
|
+
/** 附加环境变量 */
|
|
41
|
+
env?: NodeJS.ProcessEnv;
|
|
42
|
+
/** 事件与审批回调 */
|
|
43
|
+
callbacks?: AcpClientCallbacks;
|
|
44
|
+
}
|
|
45
|
+
export declare class AcpSessionHandle {
|
|
46
|
+
readonly sessionId: string;
|
|
47
|
+
private readonly client;
|
|
48
|
+
readonly restoreMethod: AcpSessionRestoreMethod;
|
|
49
|
+
readonly configOptions?: unknown;
|
|
50
|
+
constructor(sessionId: string, client: AcpClient, restoreMethod: AcpSessionRestoreMethod, configOptions?: unknown);
|
|
51
|
+
/** 向当前会话发送 Prompt / 消息交互 */
|
|
52
|
+
prompt(promptText: string, options?: {
|
|
53
|
+
timeoutMs?: number;
|
|
54
|
+
}): Promise<Result<unknown, AppError>>;
|
|
55
|
+
/** 修改会话配置项(如模型 ID、模式等) */
|
|
56
|
+
setConfigOption(configId: string, value: string | boolean): Promise<Result<unknown, AppError>>;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 独立的 Headless ACP 客户端:
|
|
60
|
+
* - 纯 TypeScript / Node.js 实现,零 Electron 依赖;
|
|
61
|
+
* - 统一管理多种 Agent 运行时(Codex, Claude, Cursor, DeepSeek 等);
|
|
62
|
+
* - 自动负责子进程生命周期、Stdio 传输桥接、协议握手与断线清理;
|
|
63
|
+
* - 可在任意 Node.js 项目、CLI 命令行工具或 Web 服务中直接复用。
|
|
64
|
+
*/
|
|
65
|
+
export declare class AcpClient {
|
|
66
|
+
private status;
|
|
67
|
+
private runtimeInfo;
|
|
68
|
+
private adapter;
|
|
69
|
+
private readonly workspaceRoot;
|
|
70
|
+
private readonly clientInfo;
|
|
71
|
+
private readonly dataDir;
|
|
72
|
+
private readonly callbacks;
|
|
73
|
+
private readonly terminals;
|
|
74
|
+
private processHandle;
|
|
75
|
+
private sdkApp;
|
|
76
|
+
private sdkConnection;
|
|
77
|
+
private streamHandle;
|
|
78
|
+
private initResponse;
|
|
79
|
+
constructor(options: AcpClientOptions);
|
|
80
|
+
get currentStatus(): AcpConnectionStatus;
|
|
81
|
+
get isConnected(): boolean;
|
|
82
|
+
/** 获取底层当前 Agent 的 RPC 客户端句柄 */
|
|
83
|
+
get agent(): import("@agentclientprotocol/sdk").ClientContext | null;
|
|
84
|
+
/**
|
|
85
|
+
* 启动并连接 Agent:
|
|
86
|
+
* 1. 预检判停(如缺少 API Key、未安装 CLI);
|
|
87
|
+
* 2. 跨平台拉起后台守护子进程;
|
|
88
|
+
* 3. 建立 Stdio pass-through 与 ndJsonStream 桥接;
|
|
89
|
+
* 4. 注册权限与 FS 回调;
|
|
90
|
+
* 5. 执行 initialize 协议握手。
|
|
91
|
+
*/
|
|
92
|
+
connect(): Promise<Result<{
|
|
93
|
+
status: AcpConnectionStatus;
|
|
94
|
+
init: InitializeResponse;
|
|
95
|
+
}, AppError>>;
|
|
96
|
+
/** 创建或恢复会话 */
|
|
97
|
+
createSession(options?: {
|
|
98
|
+
resumeSessionId?: string | null;
|
|
99
|
+
mcpServers?: unknown[];
|
|
100
|
+
}): Promise<Result<AcpSessionHandle, AppError>>;
|
|
101
|
+
/** 向指定会话发送消息 */
|
|
102
|
+
promptSession(sessionId: string, promptText: string, options?: {
|
|
103
|
+
timeoutMs?: number;
|
|
104
|
+
}): Promise<Result<unknown, AppError>>;
|
|
105
|
+
/** 动态更新会话配置项(如模型 ID) */
|
|
106
|
+
setSessionConfigOption(sessionId: string, configId: string, value: string | boolean): Promise<Result<unknown, AppError>>;
|
|
107
|
+
/** 断开连接并清理资源 */
|
|
108
|
+
disconnect(reason?: string): Promise<void>;
|
|
109
|
+
private setStatus;
|
|
110
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function isBinaryDocumentPath(filePath: string): boolean;
|
|
2
|
+
export declare function assertPathInsideWorkspace(filePath: string, workspaceRoot: string): string;
|
|
3
|
+
export declare function acpReadTextFile(params: {
|
|
4
|
+
path: string;
|
|
5
|
+
workspaceRoot: string;
|
|
6
|
+
line?: number;
|
|
7
|
+
limit?: number;
|
|
8
|
+
}): Promise<{
|
|
9
|
+
content: string;
|
|
10
|
+
}>;
|
|
11
|
+
export declare function acpWriteTextFile(params: {
|
|
12
|
+
path: string;
|
|
13
|
+
content: string;
|
|
14
|
+
workspaceRoot: string;
|
|
15
|
+
}): Promise<Record<string, never>>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 跨平台默认 ACP 数据与配置存储目录(纯 Node 运行环境):
|
|
3
|
+
* - 优先环境变量 INKDOWN_DATA_DIR / ACP_DATA_DIR
|
|
4
|
+
* - Windows: %LOCALAPPDATA%\inkdown (或 %APPDATA%\inkdown)
|
|
5
|
+
* - macOS: ~/Library/Application Support/inkdown
|
|
6
|
+
* - Linux: $XDG_DATA_HOME/inkdown (或 ~/.local/share/inkdown)
|
|
7
|
+
*/
|
|
8
|
+
export declare function getDefaultAgentDataDir(): string;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { AcpProxySettings, AppError } from "../contracts";
|
|
2
|
+
import { type Result } from "../contracts";
|
|
3
|
+
/**
|
|
4
|
+
* ACP 子进程代理设置:读写 userData/agent/proxy.json。
|
|
5
|
+
* 仅影响 spawn 的 Agent 进程(HTTP_PROXY 等环境变量),应用自身网络不受影响。
|
|
6
|
+
*/
|
|
7
|
+
export declare const DEFAULT_ACP_PROXY_SETTINGS: AcpProxySettings;
|
|
8
|
+
/** 文件缺失/损坏回落默认值(enabled=false 保证未知状态直连) */
|
|
9
|
+
export declare function readAcpProxySettings(dir?: string): Promise<AcpProxySettings>;
|
|
10
|
+
export declare function saveAcpProxySettings(payload: AcpProxySettings, dir?: string): Promise<Result<AcpProxySettings, AppError>>;
|
|
11
|
+
export declare function buildAcpProxySpawnEnv(settings: AcpProxySettings): {
|
|
12
|
+
env: NodeJS.ProcessEnv;
|
|
13
|
+
envRemove: string[];
|
|
14
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
export interface AcpTerminalExitStatus {
|
|
2
|
+
exitCode: number | null;
|
|
3
|
+
signal: string | null;
|
|
4
|
+
}
|
|
5
|
+
export interface AcpTerminalOutputSnapshot {
|
|
6
|
+
output: string;
|
|
7
|
+
truncated: boolean;
|
|
8
|
+
exitStatus?: AcpTerminalExitStatus;
|
|
9
|
+
}
|
|
10
|
+
export interface CreateAcpTerminalParams {
|
|
11
|
+
sessionId: string;
|
|
12
|
+
command: string;
|
|
13
|
+
args?: string[];
|
|
14
|
+
env?: Array<{
|
|
15
|
+
name: string;
|
|
16
|
+
value: string;
|
|
17
|
+
}>;
|
|
18
|
+
cwd?: string;
|
|
19
|
+
outputByteLimit?: number;
|
|
20
|
+
workspaceRoot: string;
|
|
21
|
+
}
|
|
22
|
+
/** ACP Client 侧无头终端:spawn 命令,供 Agent 轮询 output / wait / kill / release */
|
|
23
|
+
export declare class AcpTerminalManager {
|
|
24
|
+
private terminals;
|
|
25
|
+
create(params: CreateAcpTerminalParams): {
|
|
26
|
+
terminalId: string;
|
|
27
|
+
};
|
|
28
|
+
getOutput(terminalId: string): AcpTerminalOutputSnapshot;
|
|
29
|
+
waitForExit(terminalId: string): Promise<AcpTerminalExitStatus>;
|
|
30
|
+
kill(terminalId: string): Record<string, never>;
|
|
31
|
+
release(terminalId: string): Record<string, never>;
|
|
32
|
+
releaseAll(): void;
|
|
33
|
+
private forceKill;
|
|
34
|
+
private require;
|
|
35
|
+
private markExited;
|
|
36
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type AcpPermissionOutcome, type InkdownVirtualResource } from "../contracts";
|
|
2
|
+
import { type ClientApp } from '@agentclientprotocol/sdk';
|
|
3
|
+
import type { AcpTerminalManager } from './acp-terminal';
|
|
4
|
+
export interface AcpClientHandlerDeps {
|
|
5
|
+
getWorkspaceRoot: () => string | null;
|
|
6
|
+
terminals: AcpTerminalManager;
|
|
7
|
+
/** 读取 Inkdown 虚拟文件:向渲染进程要内存快照,不碰磁盘 */
|
|
8
|
+
readSnapshot: (resource: InkdownVirtualResource) => Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Agent 审批:直接返回 bridge 结果(直返,无待决 Map)。
|
|
11
|
+
* 无 bridge 时由 SDK 层直接 cancelled,禁静默 allow。
|
|
12
|
+
*/
|
|
13
|
+
onPermission: (payload: {
|
|
14
|
+
sessionId?: string;
|
|
15
|
+
params: Record<string, unknown>;
|
|
16
|
+
}) => Promise<AcpPermissionOutcome>;
|
|
17
|
+
/** session/update 通知透传(回放压制由调用方做) */
|
|
18
|
+
onSessionUpdate: (params: Record<string, unknown>) => void;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Agent → Client 回调:permission + fs + terminal(声明能力后才会收到)。
|
|
22
|
+
* SDK 原生签名:onRequest/onNotification 注册;抛 RequestError 即回错误,
|
|
23
|
+
* 无需手写 transport.respond/respondError。
|
|
24
|
+
*/
|
|
25
|
+
export declare function registerAcpClientHandlers(app: ClientApp, deps: AcpClientHandlerDeps): void;
|
|
26
|
+
/** 从 permission options 中挑一个「允许」类 optionId,否则 cancelled */
|
|
27
|
+
export declare function pickAllowOptionId(params: Record<string, unknown>): string | null;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ChildProcessWithoutNullStreams } from 'node:child_process';
|
|
2
|
+
import { type ClientApp, type ClientConnection, type ClientContext, type Stream } from '@agentclientprotocol/sdk';
|
|
3
|
+
/** SDK 请求超时:沿用旧手搓传输的 120s,显式透传 */
|
|
4
|
+
export declare const ACP_SDK_REQUEST_TIMEOUT_MS = 120000;
|
|
5
|
+
export interface SdkStreamHandle {
|
|
6
|
+
stream: Stream;
|
|
7
|
+
/** 摘掉桥接(温进程复用时保住子进程 stdio,不关 fd) */
|
|
8
|
+
dispose: () => void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* stdio 桥接:Node 侧先经 PassThrough 再 toWeb,最后 ndJsonStream(output, input)。
|
|
12
|
+
* 注意参数序:output(写往 Agent,即 child.stdin 侧)在前,input(读自 Agent)在后。
|
|
13
|
+
*
|
|
14
|
+
* 垫 PassThrough 的原因:SDK connection.close() 会一路 cancel 到字节流,
|
|
15
|
+
* 直连 child stdio 会把温进程的 fd 一并销毁,下次复用即死;垫层让 close
|
|
16
|
+
* 只吃掉桥,child stdio 原样保留给下次 connect。
|
|
17
|
+
*/
|
|
18
|
+
export declare function createSdkStreamHandle(child: ChildProcessWithoutNullStreams): SdkStreamHandle;
|
|
19
|
+
/**
|
|
20
|
+
* 长驻连接:client({ name: 'inkdown' }).connect(),持有 ClientConnection。
|
|
21
|
+
* 不用 connectWith 作用域式;调用方用返回的 connection.agent 发请求,
|
|
22
|
+
* 断开时 connection.close() 取消在途请求。
|
|
23
|
+
*/
|
|
24
|
+
export declare function connectSdkClient(child: ChildProcessWithoutNullStreams, configure: (app: ClientApp) => void,
|
|
25
|
+
/** 所属运行时 id:仅日志/隔离用,不改变 SDK 建连形态;缺省向后兼容单 codex 路径 */
|
|
26
|
+
runtimeId?: string): {
|
|
27
|
+
app: ClientApp;
|
|
28
|
+
connection: ClientConnection;
|
|
29
|
+
streamHandle: SdkStreamHandle;
|
|
30
|
+
};
|
|
31
|
+
/** 带显式超时的 agent.request:超时 abort(触发 $/cancel_request)并拒绝 */
|
|
32
|
+
export declare function sdkRequest<Response, Params>(agent: ClientContext, method: string, params: Params, timeoutMs?: number): Promise<Response>;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { AcpSessionRestoreAttempt, AcpSessionRestoreMethod } from "../contracts";
|
|
2
|
+
export type AcpRpcRequest = (method: string, params?: unknown) => Promise<unknown>;
|
|
3
|
+
export interface RestoreOrCreateSessionInput {
|
|
4
|
+
/** SDK 调用源:(method, params) => agent.request(method, params)(超时由 sdk-client 显式透传) */
|
|
5
|
+
request: AcpRpcRequest;
|
|
6
|
+
cwd: string;
|
|
7
|
+
resumeSessionId: string | null;
|
|
8
|
+
resumeSupported: boolean;
|
|
9
|
+
loadSupported: boolean;
|
|
10
|
+
/** 客户端自带的 MCP server(Inkdown 工具);Agent 不支持 HTTP 传输时传空数组 */
|
|
11
|
+
mcpServers?: unknown[];
|
|
12
|
+
/** session/load 回放期间回调(主进程用来压制 UI 更新) */
|
|
13
|
+
onSuppressUpdates?: (suppress: boolean) => void;
|
|
14
|
+
/** 测试可设为 0 */
|
|
15
|
+
retryDelayMs?: number;
|
|
16
|
+
log?: (level: 'info' | 'warn', message: string, data?: Record<string, unknown>) => void;
|
|
17
|
+
}
|
|
18
|
+
export interface RestoreOrCreateSessionResult {
|
|
19
|
+
sessionId: string;
|
|
20
|
+
configOptions: unknown;
|
|
21
|
+
restoreMethod: AcpSessionRestoreMethod;
|
|
22
|
+
sessionRestored: boolean;
|
|
23
|
+
requestedSessionId?: string;
|
|
24
|
+
restoreAttempts: AcpSessionRestoreAttempt[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* SDK 瞬时错误判定:连接层故障(关闭/中断/超时/管道)可重试 1 次;
|
|
28
|
+
* 协议层拒绝(cancelled / method-not-found / invalid-params / 业务 not-found)
|
|
29
|
+
* 不重试,直接落到下一恢复手段。
|
|
30
|
+
*
|
|
31
|
+
* 注意:SDK 会把 Agent 侧抛出的 Error 归一化为 RequestError(-32603),原文
|
|
32
|
+
* 藏进 data.details,因此连带 data 一起判定。
|
|
33
|
+
*/
|
|
34
|
+
export declare function isTransientAcpTransportError(error: unknown): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* 优先 resume → load → session/new。
|
|
37
|
+
* resume/load 对瞬时传输错误最多再试 1 次;失败明细写入 restoreAttempts。
|
|
38
|
+
*/
|
|
39
|
+
export declare function restoreOrCreateAcpSession(input: RestoreOrCreateSessionInput): Promise<RestoreOrCreateSessionResult>;
|