@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
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { callInkdownMcpTool, type InkdownMcpToolContext, type InkdownMcpToolDefinition } from './inkdown-mcp-tools';
|
|
2
|
+
export interface McpRpcMessage {
|
|
3
|
+
jsonrpc?: string;
|
|
4
|
+
id?: string | number | null;
|
|
5
|
+
method?: string;
|
|
6
|
+
params?: Record<string, unknown>;
|
|
7
|
+
}
|
|
8
|
+
export type McpRpcResponse = Record<string, unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* 最小 MCP 服务端分发:initialize / tools/list / tools/call / ping。
|
|
11
|
+
* 返回 null 表示这是通知,HTTP 层应回 202 且无 body。
|
|
12
|
+
* tools/call 允许注入子表(目录副会话只挂目录工具,主表不受影响)。
|
|
13
|
+
*/
|
|
14
|
+
export declare function handleInkdownMcpRpc(message: McpRpcMessage, context: InkdownMcpToolContext, tools?: readonly InkdownMcpToolDefinition[], call?: typeof callInkdownMcpTool): Promise<McpRpcResponse | null>;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { InkdownMcpToolContext, InkdownMcpToolDefinition, InkdownMcpToolResult } from './inkdown-mcp-tools';
|
|
2
|
+
export declare const INKDOWN_TOC_MCP_TOOLS: InkdownMcpToolDefinition[];
|
|
3
|
+
export declare function callInkdownTocTool(name: string, context: InkdownMcpToolContext, args?: Record<string, unknown>): Promise<InkdownMcpToolResult>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { InkdownSnapshotArgs, InkdownSnapshotResource } from "../contracts";
|
|
2
|
+
export interface InkdownMcpToolContext {
|
|
3
|
+
/** 复用 ACP 快照回路:向渲染进程要内存快照 */
|
|
4
|
+
readSnapshot: (resource: InkdownSnapshotResource, args?: InkdownSnapshotArgs) => Promise<string>;
|
|
5
|
+
}
|
|
6
|
+
export interface InkdownMcpToolDefinition {
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
inputSchema: Record<string, unknown>;
|
|
10
|
+
/** MCP tool annotations consumed by capable agents for safety/parallelism hints. */
|
|
11
|
+
annotations?: InkdownMcpToolAnnotations;
|
|
12
|
+
}
|
|
13
|
+
/** MCP ToolAnnotations (hints, not an authorization mechanism). */
|
|
14
|
+
export interface InkdownMcpToolAnnotations {
|
|
15
|
+
title?: string;
|
|
16
|
+
readOnlyHint?: boolean;
|
|
17
|
+
destructiveHint?: boolean;
|
|
18
|
+
idempotentHint?: boolean;
|
|
19
|
+
openWorldHint?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export type InkdownReadScope = 'toc' | 'viewport' | 'current' | 'chapter' | 'search';
|
|
22
|
+
export type InkdownMarkListFilter = 'all' | 'highlights' | 'bookmarks';
|
|
23
|
+
export declare const INKDOWN_MCP_TOOLS: InkdownMcpToolDefinition[];
|
|
24
|
+
export interface InkdownMcpToolResult {
|
|
25
|
+
content: Array<{
|
|
26
|
+
type: 'text';
|
|
27
|
+
text: string;
|
|
28
|
+
}>;
|
|
29
|
+
isError?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export declare function callInkdownMcpTool(name: string, context: InkdownMcpToolContext, args?: Record<string, unknown>): Promise<InkdownMcpToolResult>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type ChildProcessWithoutNullStreams } from 'node:child_process';
|
|
2
|
+
import type { AcpRuntimeInfo } from "../contracts";
|
|
3
|
+
export interface SpawnedAcpProcess {
|
|
4
|
+
runtimeId: string;
|
|
5
|
+
child: ChildProcessWithoutNullStreams;
|
|
6
|
+
kill: () => void;
|
|
7
|
+
/** spawn 时间戳(保留供诊断/排序,stderr 归因已去窗口化,不再参与判定) */
|
|
8
|
+
spawnedAt: number;
|
|
9
|
+
/** stderr 最后 N 行活引用(连接失败自诊断用,上限见 ACP_EARLY_EXIT_STDERR_TAIL_LINES) */
|
|
10
|
+
stderrTail: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* @deprecated 早退判定窗口已去窗口化(dsh 依赖解析慢退漏归因教训):
|
|
14
|
+
* 保留导出仅防旧引用,`getAcpEarlyExitStderrDetail` 不再读取它。
|
|
15
|
+
*/
|
|
16
|
+
export declare const ACP_EARLY_EXIT_WINDOW_MS = 3000;
|
|
17
|
+
/** 连接失败时拼进错误的 stderr 尾部行数上限。单处常量。 */
|
|
18
|
+
export declare const ACP_EARLY_EXIT_STDERR_TAIL_LINES = 20;
|
|
19
|
+
export interface SpawnAcpOptions {
|
|
20
|
+
runtime: AcpRuntimeInfo;
|
|
21
|
+
cwd: string;
|
|
22
|
+
env?: NodeJS.ProcessEnv;
|
|
23
|
+
/** 需要从继承环境里剔除的变量名(如关闭代理时清理 HTTP(S)_PROXY) */
|
|
24
|
+
envRemove?: string[];
|
|
25
|
+
onStderrLine?: (line: string) => void;
|
|
26
|
+
onExit?: (code: number | null, signal: NodeJS.Signals | null) => void;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* 进程是否仍存活。exitCode 非 null 表示已退出;killed 仅表示收到过 kill 信号,
|
|
30
|
+
* 必须两者结合判断,避免对已退出(PID 可能已被系统复用)的进程再下手。
|
|
31
|
+
*/
|
|
32
|
+
export declare function isSpawnedAcpProcessAlive(handle: SpawnedAcpProcess): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* 多运行时温进程隔离:`active` 按 runtime.id 分键,绝不跨 runtime 串用。
|
|
35
|
+
* connect 前一律经 `getLiveAcpProcess(runtime.id)` 取同 runtime 温进程;
|
|
36
|
+
* 不同 runtime 即使 command 相同也各持独立句柄。
|
|
37
|
+
*/
|
|
38
|
+
export declare function spawnAcpProcess(options: SpawnAcpOptions): SpawnedAcpProcess;
|
|
39
|
+
/**
|
|
40
|
+
* 连接失败自诊断(去窗口化):进程已死且连接失败时,一律取 stderr 尾部
|
|
41
|
+
* 供连接层拼进失败错误,替代裸 `ACP connection closed`。
|
|
42
|
+
* 背景:dsh 自解析依赖耗时远超旧 3s 窗口,慢退漏归因;故不再限早退窗口,
|
|
43
|
+
* 慢退/长会话失败同样受益;正常成功路径不调用此处,零变化。
|
|
44
|
+
* 无 stderr 尾返回 null,调用方保持原错误不变;
|
|
45
|
+
* killProcess/温复用路径不经此处,不受影响。
|
|
46
|
+
* `now` 参数保留仅防旧调用(已忽略)。
|
|
47
|
+
*/
|
|
48
|
+
export declare function getAcpEarlyExitStderrDetail(handle: Pick<SpawnedAcpProcess, 'spawnedAt' | 'stderrTail'> | null | undefined, _now?: number): string | null;
|
|
49
|
+
/** 失败尾部拼进连接失败错误(已含尾部时不重复拼)。函数名保留早退字样仅防旧引用。 */
|
|
50
|
+
export declare function withAcpEarlyExitDetail(message: string, detail: string | null): string;
|
|
51
|
+
export declare function disposeAllAcpProcesses(): void;
|
|
52
|
+
export declare function getActiveAcpProcess(runtimeId: string): SpawnedAcpProcess | undefined;
|
|
53
|
+
/** 当前仍存活的温进程 runtime 清单(调试/测试用,不暴露 child 句柄细节)。 */
|
|
54
|
+
export declare function listLiveAcpRuntimeIds(): string[];
|
|
55
|
+
/**
|
|
56
|
+
* 取同 runtime 下仍存活的常驻进程(复用时用)。已退出或被外部杀掉的返回 undefined,
|
|
57
|
+
* 调用方应走冷启动并顺手清扫其残留临时目录。
|
|
58
|
+
*/
|
|
59
|
+
export declare function getLiveAcpProcess(runtimeId: string): SpawnedAcpProcess | undefined;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { CodexAuthPreflight } from "../../contracts";
|
|
2
|
+
import type { GenericRuntimeAdapter } from '../index';
|
|
3
|
+
/**
|
|
4
|
+
* agy 运行时适配器(`bunx -y @yitom/agy-acp-map`,无参 stdio 服务)。
|
|
5
|
+
*
|
|
6
|
+
* 形态照抄 codex 条目:模板直调官方桥 JS 入口(`agy-acp → dist/bin.js`,
|
|
7
|
+
* src/sdk-server.ts 构建,target node),免安装、跨平台(win/mac/linux
|
|
8
|
+
* 有 bun 即行;JS 入口无旧 exe 的 win-only 限制)。
|
|
9
|
+
*
|
|
10
|
+
* 语义(对齐同源参考实现,不拷贝架构):
|
|
11
|
+
* - initialize 跑 `agy models`/`agents`(10s 超时,进程级缓存);
|
|
12
|
+
* - 模型经 session/new 的 configOptions 到达,中途换模型走
|
|
13
|
+
* session/set_config_option(configId 含 model|effort|mode|agent|…),
|
|
14
|
+
* 即我方现有 setModel 通道直通,无需 set_model 逃生口;
|
|
15
|
+
* - authMethods 为 [](直连即可);
|
|
16
|
+
* - 历史回放刻意极简(本地 zustand 仍是显示真相源)。
|
|
17
|
+
*/
|
|
18
|
+
export declare function probeAgyAuth(): CodexAuthPreflight;
|
|
19
|
+
export declare const agyAdapter: GenericRuntimeAdapter;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CodexAuthPreflight } from "../../contracts";
|
|
2
|
+
import type { GenericRuntimeAdapter } from '../index';
|
|
3
|
+
/**
|
|
4
|
+
* Claude 运行时适配器(`claude-agent-acp`)。
|
|
5
|
+
*
|
|
6
|
+
* 凭证复用:Claude Code 本机登录(`~/.claude.json` 的 oauthAccount /
|
|
7
|
+
* Linux/Win `~/.claude/.credentials.json`,macOS 走 Keychain 故仅文件侧探测)
|
|
8
|
+
* 或 `ANTHROPIC_API_KEY` / `CLAUDE_CODE_OAUTH_TOKEN` 环境透传。
|
|
9
|
+
* 只做布尔级存在性探测,不读密钥内容。
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveClaudeHome(): string;
|
|
12
|
+
export declare function probeClaudeAuth(): CodexAuthPreflight;
|
|
13
|
+
export declare const claudeAdapter: GenericRuntimeAdapter;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { CodexAuthPreflight } from "../../contracts";
|
|
2
|
+
export type { CodexAuthPreflight };
|
|
3
|
+
export declare function emptyAcpAuthPreflight(): CodexAuthPreflight;
|
|
4
|
+
export declare function isCodexPreflightRuntime(runtimeId?: string): boolean;
|
|
5
|
+
export declare function resolveCodexHome(): string;
|
|
6
|
+
/**
|
|
7
|
+
* 仅做布尔级探测,对齐 VS Code/Zed「复用本机 Codex 登录」:
|
|
8
|
+
* 不把 auth.json / token 读进渲染进程。
|
|
9
|
+
*/
|
|
10
|
+
export declare function probeCodexAuth(): CodexAuthPreflight;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AcpProviderConfig } from "../../contracts";
|
|
2
|
+
/**
|
|
3
|
+
* 自定义供应商专用 CODEX_HOME 内生成的 provider id。
|
|
4
|
+
* 固定值,便于测试与日志识别;与用户真实 ~/.codex 的 provider 命名空间隔离。
|
|
5
|
+
*/
|
|
6
|
+
export declare const INKDOWN_PROVIDER_ID = "inkdown-custom";
|
|
7
|
+
/** 自定义供应商专用环境变量名:config.toml 的 env_key 指向它 */
|
|
8
|
+
export declare const INKDOWN_PROVIDER_API_KEY_ENV = "INKDOWN_PROVIDER_API_KEY";
|
|
9
|
+
/**
|
|
10
|
+
* 生成隔离 CODEX_HOME 的完整 config.toml。
|
|
11
|
+
* 该目录由本应用独占创建,整文件重写安全(无用户手工内容需合并)。
|
|
12
|
+
*/
|
|
13
|
+
export declare function buildCustomProviderConfigToml(config: AcpProviderConfig): string;
|
|
14
|
+
/**
|
|
15
|
+
* 自定义供应商模式的 spawn 环境覆盖项:
|
|
16
|
+
* - CODEX_HOME 指向隔离目录(适配器读 config.toml,找不到 auth.json)
|
|
17
|
+
* - INKDOWN_PROVIDER_API_KEY 承载明文 Key(config.toml env_key 引用)
|
|
18
|
+
* 仅影响子进程;主进程与用户 ~/.codex 不受影响。
|
|
19
|
+
*/
|
|
20
|
+
export declare function buildCustomProviderSpawnEnv(config: AcpProviderConfig, apiKey: string, codexHome: string): NodeJS.ProcessEnv;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AcpProviderConfig, AcpProviderSavePayload, AcpProviderStatus, AppError } from "../../contracts";
|
|
2
|
+
import { type Result } from "../../contracts";
|
|
3
|
+
export interface StoredAcpProvider extends AcpProviderConfig {
|
|
4
|
+
apiKey: string;
|
|
5
|
+
}
|
|
6
|
+
/** 读取完整配置(含 Key),仅内部使用;文件缺失/损坏返回 null */
|
|
7
|
+
export declare function readStoredAcpProvider(dir?: string): Promise<StoredAcpProvider | null>;
|
|
8
|
+
/** 供 UI 的脱敏状态(Key 不回传) */
|
|
9
|
+
export declare function getAcpProviderStatus(dir?: string): Promise<AcpProviderStatus>;
|
|
10
|
+
export declare function saveAcpProvider(payload: AcpProviderSavePayload, dir?: string): Promise<Result<AcpProviderStatus, AppError>>;
|
|
11
|
+
export declare function clearAcpProvider(dir?: string): Promise<Result<void, AppError>>;
|
|
12
|
+
/** 隔离 CODEX_HOME 目录(spawn 前落 config.toml) */
|
|
13
|
+
export declare function getAcpProviderCodexHome(dir?: string): string;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CodexAuthPreflight } from "../../contracts";
|
|
2
|
+
import { type StoredAcpProvider } from './codex-provider';
|
|
3
|
+
export * from './codex-auth-preflight';
|
|
4
|
+
export * from './codex-provider';
|
|
5
|
+
export * from './codex-provider-home';
|
|
6
|
+
export declare function provisionCustomProviderCodexHome(provider: StoredAcpProvider, codexHome: string): Promise<boolean>;
|
|
7
|
+
export interface CodexRuntimeAdapter {
|
|
8
|
+
id: 'codex-acp';
|
|
9
|
+
probeAuth: () => CodexAuthPreflight;
|
|
10
|
+
getCustomProvider: () => Promise<{
|
|
11
|
+
provider: StoredAcpProvider | null;
|
|
12
|
+
customEnv: NodeJS.ProcessEnv;
|
|
13
|
+
isCustom: boolean;
|
|
14
|
+
}>;
|
|
15
|
+
}
|
|
16
|
+
export declare const codexAdapter: CodexRuntimeAdapter;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { CodexAuthPreflight } from "../../contracts";
|
|
2
|
+
import type { GenericRuntimeAdapter } from '../index';
|
|
3
|
+
/**
|
|
4
|
+
* Copilot 运行时适配器(本机 `copilot --acp --stdio`)。
|
|
5
|
+
*
|
|
6
|
+
* 凭证复用:Copilot CLI 自身 GitHub 登录(OAuth token 默认进系统 keychain,
|
|
7
|
+
* 无 keychain 时落 `~/.copilot/config.json` 明文;`COPILOT_HOME` 可改目录)
|
|
8
|
+
* 或 `COPILOT_GITHUB_TOKEN` / `GH_TOKEN` / `GITHUB_TOKEN` 环境透传。
|
|
9
|
+
* `config.json` 读到 `loggedInUsers` 非空才算已登录(文件本身首启即建,
|
|
10
|
+
* 存在性无意义);异常一律按未登录处理。
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolveCopilotHome(): string;
|
|
13
|
+
export declare function probeCopilotAuth(): CodexAuthPreflight;
|
|
14
|
+
export declare const copilotAdapter: GenericRuntimeAdapter;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { CodexAuthPreflight } from "../../contracts";
|
|
2
|
+
import type { GenericRuntimeAdapter } from '../index';
|
|
3
|
+
/**
|
|
4
|
+
* Cursor 运行时适配器(Cursor 官方 CLI `agent acp`,运行时 id `cursor-cli`)。
|
|
5
|
+
*
|
|
6
|
+
* 安装路径探测(我方实现,对标官方安装位置,不拷贝第三方网关代码):
|
|
7
|
+
* Windows 优先 `%LOCALAPPDATA%\\cursor-agent\\agent.cmd`(存在才用),
|
|
8
|
+
* posix 优先 `~/.local/bin/agent`(存在才用),否则回落 PATH 中的
|
|
9
|
+
* `agent.cmd`(win)/ `agent`(posix),与 contracts 模板 fallback 一致。
|
|
10
|
+
*
|
|
11
|
+
* 预 spawn 接线(`resolveSpawnCommand`,见 `runtimes/index.ts`):
|
|
12
|
+
* 直接路径与 PATH 均无 CLI 时返回 `null` 表示未安装,
|
|
13
|
+
* 由 `acp-connection.ts` 在 spawn 前判停并返回带安装指引的
|
|
14
|
+
* `ACP_SPAWN_ERROR`,不再把裸 `agent[.cmd]` 丢给 spawn
|
|
15
|
+
*(否则 Windows 上 `cmd` 报“不是内部或外部命令”,子进程秒退,
|
|
16
|
+
* UI 只剩一句看不懂的 `ACP connection closed`)。
|
|
17
|
+
*
|
|
18
|
+
* 凭证复用:本机 Cursor 登录(`agent login`;ACP 侧方法 `cursor_login`)
|
|
19
|
+
* 或 `CURSOR_API_KEY` / `CURSOR_AUTH_TOKEN` 环境透传。
|
|
20
|
+
* 文件探针为 hint 级别(见 probeCursorAuth):命中→已登录,未命中不下结论,
|
|
21
|
+
* 由 gate 侧 `tryDirectSessionFirst` 兜底直连试一次(keychain 化会漏检)。
|
|
22
|
+
*/
|
|
23
|
+
export interface CursorPathOverrides {
|
|
24
|
+
platform?: NodeJS.Platform;
|
|
25
|
+
env?: NodeJS.ProcessEnv;
|
|
26
|
+
home?: string;
|
|
27
|
+
}
|
|
28
|
+
export declare function resolveCursorCommand(overrides?: CursorPathOverrides): {
|
|
29
|
+
command: string;
|
|
30
|
+
args: string[];
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* 预 spawn 接线点:直接路径命中则透传绝对路径,PATH 命中则透传
|
|
34
|
+
* 裸命令(与 contracts 模板一致),两处皆无返回 `null`(未安装)。
|
|
35
|
+
* `null` 由 `acp-connection.ts` 转为带安装指引的 `ACP_SPAWN_ERROR`。
|
|
36
|
+
*/
|
|
37
|
+
export declare function resolveCursorSpawnCommand(overrides?: CursorPathOverrides): {
|
|
38
|
+
command: string;
|
|
39
|
+
args: string[];
|
|
40
|
+
} | null;
|
|
41
|
+
/** 未安装时的用户可读指引(分 win/posix 话术),调用方原样透传为 error message。 */
|
|
42
|
+
export declare function buildCursorMissingCliMessage(overrides?: CursorPathOverrides): string;
|
|
43
|
+
/** 登录态候选落盘位置(按优先级排序,调用方按序取首个命中)。 */
|
|
44
|
+
export declare function resolveCursorAuthCandidates(overrides?: CursorPathOverrides): string[];
|
|
45
|
+
/** auth.json 有效性:非空可解析且 accessToken / refreshToken 任一非空。 */
|
|
46
|
+
export declare function isValidCursorAuthFile(authFile: string): boolean;
|
|
47
|
+
/** storage.json hint 级别有效性:存在且非空可解析为非空对象即算命中。 */
|
|
48
|
+
export declare function isHintCursorStorageFile(authFile: string): boolean;
|
|
49
|
+
export declare function probeCursorAuth(overrides?: CursorPathOverrides): CodexAuthPreflight;
|
|
50
|
+
export declare const cursorAdapter: GenericRuntimeAdapter;
|
|
51
|
+
/**
|
|
52
|
+
* `agent models` 输出纯解析:逐行取首 token 为 id(形如 `grok-4.7-high-fast`)。
|
|
53
|
+
* - 空行跳过;行首 `*`(默认模型标记,见第三方 `agent models` 解析惯例)剥掉后取首 token。
|
|
54
|
+
* - `auto` 等保留原样,由调用方决定取舍;方括号 variants 行(含 `[`)同样保留
|
|
55
|
+
* (目录本应全是横杠 canonical,混入时由匹配侧按无 `[` 过滤)。
|
|
56
|
+
* 非字符串输入一律返回空数组(调用方判空即 null)。
|
|
57
|
+
*/
|
|
58
|
+
export declare function parseCursorCatalogOutput(output: unknown): string[];
|
|
59
|
+
/** 测试用:清空进程级目录缓存。 */
|
|
60
|
+
export declare function clearCursorCatalogCache(): void;
|
|
61
|
+
/**
|
|
62
|
+
* Cursor 横杠 canonical 目录:复用已解析的 agent 命令(`resolveCursorSpawnCommand`,
|
|
63
|
+
* 无则返回 null 表示未安装),以 `--list-models`(`agent models` 同源)spawnSync 拉取。
|
|
64
|
+
* 超时 6s;空输出 / 超时 / 非零退出一律 null(调用方吞掉记 dev 日志,不阻断连接)。
|
|
65
|
+
*/
|
|
66
|
+
export declare function getCursorCatalogIds(execFile?: string, overrides?: CursorPathOverrides): string[] | null;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { CodexAuthPreflight } from "../../contracts";
|
|
2
|
+
import type { GenericRuntimeAdapter } from '../index';
|
|
3
|
+
/**
|
|
4
|
+
* DeepSeek 运行时适配器(`bunx @deepseek-ai/dsh --profile acp`)。
|
|
5
|
+
*
|
|
6
|
+
* 凭证复用:harness 自身凭证(`DEEPSEEK_API_KEY` / harness 配置,
|
|
7
|
+
* 全部留在官方位置,我方不存)。harness 的 ACP `authMethods` 为空,
|
|
8
|
+
* 连接门闩天然走 `skip_auth`;此处探测仅供诊断展示。
|
|
9
|
+
* 模型经 session 到达(顶层 `models` 方言由 session-open 合成 model 选项),无需额外动作。
|
|
10
|
+
*
|
|
11
|
+
* 缺 key 预检(仿 cursor `resolveSpawnCommand` 为 null 即拦的 precedent):
|
|
12
|
+
* 本机缺 key 时 harness 极可能直接退出(stdio 关闭→传输层报 connection
|
|
13
|
+
* closed),故经 `resolveSpawnBlocker` 在 spawn 前判停,回带中文动作指引。
|
|
14
|
+
* 同族 `DSH_*` 仅认文档明确的变量——当前仅认 `DEEPSEEK_API_KEY`(预检)
|
|
15
|
+
* 与 `DSH_PACKAGE`(包定位符覆盖,见 `resolveDeepseekSpawnCommand`)。
|
|
16
|
+
*
|
|
17
|
+
* 上游坏包逃生(实锤:dsh 0.1.5-rc.2 内依赖 `…-documentpreview@^0.1.5-rc.3`,
|
|
18
|
+
* 而该包最高仅 0.1.6-alpha.1,上游缺 rc.3,自解析依赖失败退出):
|
|
19
|
+
* 设 `DSH_PACKAGE` pin 旧版(如 `@deepseek-ai/dsh@0.1.4`)即可绕过。
|
|
20
|
+
*/
|
|
21
|
+
export declare function buildDeepseekMissingKeyMessage(): string;
|
|
22
|
+
export declare function resolveDeepseekSpawnBlocker(env?: NodeJS.ProcessEnv): string | null;
|
|
23
|
+
/**
|
|
24
|
+
* 包定位符覆盖(`DSH_PACKAGE`,缺省 `@deepseek-ai/dsh`,沿 contracts 常量口径):
|
|
25
|
+
* 最小校验——非空、无空白、无 shell 元字符(`;&|$()` 等),非法回落缺省并 dev warn。
|
|
26
|
+
* 经 `resolveSpawnCommand` 风格返回 spawn 目标覆盖(precedent 照抄 cursor)。
|
|
27
|
+
*/
|
|
28
|
+
export declare function resolveDeepseekPackage(env?: NodeJS.ProcessEnv): string;
|
|
29
|
+
export declare function resolveDeepseekSpawnCommand(env?: NodeJS.ProcessEnv): {
|
|
30
|
+
command: string;
|
|
31
|
+
args: string[];
|
|
32
|
+
};
|
|
33
|
+
export declare function probeDeepseekAuth(): CodexAuthPreflight;
|
|
34
|
+
export declare const deepseekAdapter: GenericRuntimeAdapter;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { CodexAuthPreflight } from "../../contracts";
|
|
2
|
+
import type { GenericRuntimeAdapter } from '../index';
|
|
3
|
+
/**
|
|
4
|
+
* Gemini 运行时适配器(本机 `gemini --acp`)。
|
|
5
|
+
*
|
|
6
|
+
* 凭证复用:Gemini CLI 本机登录(`~/.gemini/oauth_creds.json`;
|
|
7
|
+
* 新版可能迁入系统 Keychain,故文件缺失不等于未登录)或
|
|
8
|
+
* `GEMINI_API_KEY` / `GOOGLE_API_KEY` 环境透传。
|
|
9
|
+
* 只做布尔级存在性探测,不读密钥内容。
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveGeminiHome(): string;
|
|
12
|
+
export declare function probeGeminiAuth(): CodexAuthPreflight;
|
|
13
|
+
export declare const geminiAdapter: GenericRuntimeAdapter;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { AcpAuthMethod, AcpProxySettings, CodexAuthPreflight } from "../contracts";
|
|
2
|
+
import { codexAdapter } from './codex';
|
|
3
|
+
export * from './codex';
|
|
4
|
+
export * from './claude';
|
|
5
|
+
export * from './gemini';
|
|
6
|
+
export * from './copilot';
|
|
7
|
+
export * from './opencode';
|
|
8
|
+
export * from './cursor';
|
|
9
|
+
export * from './deepseek';
|
|
10
|
+
export * from './agy';
|
|
11
|
+
export interface GenericRuntimeAdapter {
|
|
12
|
+
id: string;
|
|
13
|
+
beforeSpawn?: () => Promise<void>;
|
|
14
|
+
/** 仅冷启动(无温进程可用、即将 spawn)时调用一次;各 runtime 自理副作用 */
|
|
15
|
+
onColdStart?: () => Promise<void>;
|
|
16
|
+
probeAuth: () => CodexAuthPreflight;
|
|
17
|
+
/**
|
|
18
|
+
* 预 spawn 接线点(冷启动 spawn 前调用一次):
|
|
19
|
+
* 返回 `{ command, args }` 则覆盖模板的 spawn 目标(可透传绝对路径);
|
|
20
|
+
* 返回 `null` 表示 CLI 未安装,`acp-connection.ts` 直接回带安装指引的
|
|
21
|
+
* `ACP_SPAWN_ERROR`,不触达 spawn(省掉“不是内部或外部命令”秒退)。
|
|
22
|
+
* 缺省(undefined)表示无需预检,沿用模板 command/args。
|
|
23
|
+
* 抛错时按缺省处理(防御性回落),不断连接。
|
|
24
|
+
*/
|
|
25
|
+
resolveSpawnCommand?: () => {
|
|
26
|
+
command: string;
|
|
27
|
+
args: string[];
|
|
28
|
+
} | null;
|
|
29
|
+
/**
|
|
30
|
+
* 预检判停(缺 key 等,仿 resolveSpawnCommand 的可选 + 缺省兼容做法):
|
|
31
|
+
* 返回非空字符串表示直接判停,`acp-connection.ts` 以该文案回带
|
|
32
|
+
* `ACP_SPAWN_ERROR`,不触达 spawn(如 deepseek 缺 DEEPSEEK_API_KEY 时
|
|
33
|
+
* harness 秒退,stdio 关闭即裸 `ACP connection closed`)。
|
|
34
|
+
* 缺省(undefined)/返回空表示放行;抛错时按放行处理(防御性回落),不断连接。
|
|
35
|
+
*/
|
|
36
|
+
resolveSpawnBlocker?: () => string | null | undefined;
|
|
37
|
+
getSpawnEnv?: (proxySettings?: Partial<AcpProxySettings>) => {
|
|
38
|
+
env: NodeJS.ProcessEnv;
|
|
39
|
+
envRemove: string[];
|
|
40
|
+
};
|
|
41
|
+
orderAuthMethods?: (methods: AcpAuthMethod[]) => AcpAuthMethod[];
|
|
42
|
+
canSkipInteractiveAuth?: (methodId: string, force?: boolean) => boolean;
|
|
43
|
+
/**
|
|
44
|
+
* keychain 化凭据兜底:文件探针只能当 hint(命中→已登录,未命中不断言),
|
|
45
|
+
* 置 true 的 runtime 在 gate 判定 needs_auth 前先试一次直接建会话,
|
|
46
|
+
* 成功则免弹向导(session_without_auth),失败再回落 needs_auth。
|
|
47
|
+
* 缺省(undefined)视为 false,其余 runtime 不动。
|
|
48
|
+
*/
|
|
49
|
+
tryDirectSessionFirst?: boolean;
|
|
50
|
+
}
|
|
51
|
+
export type AcpRuntimeAdapter = GenericRuntimeAdapter & {
|
|
52
|
+
getCustomProvider?: typeof codexAdapter.getCustomProvider;
|
|
53
|
+
};
|
|
54
|
+
export declare function getAcpRuntimeAdapter(runtimeId: string): AcpRuntimeAdapter;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CodexAuthPreflight } from "../../contracts";
|
|
2
|
+
import type { GenericRuntimeAdapter } from '../index';
|
|
3
|
+
/**
|
|
4
|
+
* OpenCode 运行时适配器(本机 `opencode acp`)。
|
|
5
|
+
*
|
|
6
|
+
* 凭证复用:`opencode auth login` 写入的 `auth.json`(legacy 位置;
|
|
7
|
+
* v2 起凭据已迁 SQLite,auth.json 仅作兼容残留)。
|
|
8
|
+
* 探测顺序:`$XDG_DATA_HOME/opencode/auth.json`(XDG 优先,含 Windows)
|
|
9
|
+
* > Windows `%LOCALAPPDATA%\\opencode\\auth.json`
|
|
10
|
+
* > Windows `%USERPROFILE%\.local\share\opencode\auth.json`(回落;
|
|
11
|
+
* posix 下为 `~/.local/share/opencode/auth.json`)。
|
|
12
|
+
* 注意:旧实现曾误用 `%APPDATA%\\opencode\\auth.json`(Roaming),
|
|
13
|
+
* 与 opencode 文档/社区 issue 确认的实际落盘位置不符,已修正。
|
|
14
|
+
* 另有空白文件损坏先例:文件存在但去空白后为空、JSON 不可解析、
|
|
15
|
+
* 或解析后无任何 key 时,一律按未登录处理,只做布尔级探测,不读密钥内容。
|
|
16
|
+
* 环境变量无需探测:spawn 继承父进程 env,透传天然生效。
|
|
17
|
+
*/
|
|
18
|
+
export interface OpencodePathOverrides {
|
|
19
|
+
platform?: NodeJS.Platform;
|
|
20
|
+
env?: NodeJS.ProcessEnv;
|
|
21
|
+
home?: string;
|
|
22
|
+
}
|
|
23
|
+
export declare function resolveOpencodeAuthCandidates(overrides?: OpencodePathOverrides): string[];
|
|
24
|
+
export declare function resolveOpencodeAuthFile(): string;
|
|
25
|
+
/** 空白/损坏/空对象一律判无效:存在 ≠ 已登录。 */
|
|
26
|
+
export declare function isValidOpencodeAuthFile(authFile: string): boolean;
|
|
27
|
+
export declare function probeOpencodeAuth(overrides?: OpencodePathOverrides): CodexAuthPreflight;
|
|
28
|
+
export declare const opencodeAdapter: GenericRuntimeAdapter;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AcpConfigOption } from "../contracts";
|
|
2
|
+
export declare function parseAcpConfigOptions(raw: unknown): AcpConfigOption[];
|
|
3
|
+
export declare function findModelConfigOption(options: AcpConfigOption[]): AcpConfigOption | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* dsh 方言兼容:session/new(及 load)返回顶层 `models`,而 SDK
|
|
6
|
+
* NewSessionResponse schema 仅有 sessionId/modes/configOptions。
|
|
7
|
+
* 顶层 models 属于 agent 方言:数组(string 或 {id|value/name} 对象),
|
|
8
|
+
* 或 {availableModels, currentModelId} 对象形。
|
|
9
|
+
* 无 model 类 configOption 时合成一项 {configId:'model', category:'model'},
|
|
10
|
+
* 已有则不覆盖(调用方先 parse 再判定)。
|
|
11
|
+
*/
|
|
12
|
+
export declare function synthesizeModelOptionFromTopLevelModels(models: unknown, response?: Record<string, unknown>): AcpConfigOption | null;
|
|
13
|
+
/**
|
|
14
|
+
* session/new(及 load/resume)响应合并:已有 model 类 configOption 直接返回原样;
|
|
15
|
+
* 否则顶层 `models` 存在时合成一项追加。返回仍为未知形状,由调用方再 parse。
|
|
16
|
+
*/
|
|
17
|
+
export declare function mergeTopLevelModelsIntoConfigOptions(configOptionsRaw: unknown, response: Record<string, unknown> | null | undefined): unknown;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/** 从 initialize 的 agentCapabilities 解析会话恢复与 prompt 能力 */
|
|
2
|
+
import type { AcpPromptCapabilities } from "../contracts";
|
|
3
|
+
export declare function parseLoadSessionSupported(caps: Record<string, unknown>): boolean;
|
|
4
|
+
/**
|
|
5
|
+
* ACP:`sessionCapabilities.resume` 可为 `true` 或非空对象(codex-acp 用 `{}`)。
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseResumeSessionSupported(caps: Record<string, unknown>): boolean;
|
|
8
|
+
/** ACP:HTTP 传输的 MCP server 是可选能力,Agent 未声明时不能注入 */
|
|
9
|
+
export declare function parseMcpHttpSupported(caps: Record<string, unknown>): boolean;
|
|
10
|
+
export declare function parsePromptCapabilities(caps: Record<string, unknown>): AcpPromptCapabilities;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 终端输出环形缓冲:按 UTF-8 字节上限从开头截断(落在码点边界)。
|
|
3
|
+
*/
|
|
4
|
+
export declare function truncateOutputFromStart(text: string, byteLimit: number): {
|
|
5
|
+
text: string;
|
|
6
|
+
truncated: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare function appendTerminalOutput(current: string, chunk: string, byteLimit: number): {
|
|
9
|
+
output: string;
|
|
10
|
+
truncated: boolean;
|
|
11
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# 新增 Agent 运行时适配器指南 (Adding an Agent)
|
|
2
|
+
|
|
3
|
+
`@inkdown/acp` 采用插件化适配器(Adapter Pattern)架构。如果你想接入一个新的 Agent 运行时,**只需要在一个地方新增适配器文件并注册**,所有调用该 SDK 的项目(CLI、Electron、Tauri、Web 等)即可全局自动支持该 Agent!
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## 适配器接口规范
|
|
8
|
+
|
|
9
|
+
所有运行时适配器必须实现 `GenericRuntimeAdapter` 接口:
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
export interface GenericRuntimeAdapter {
|
|
13
|
+
/** 唯一运行时标识,如 'my-agent' */
|
|
14
|
+
id: string
|
|
15
|
+
|
|
16
|
+
/** 凭证存在性探测(判断用户是否已在本机登录或配置了 Key) */
|
|
17
|
+
probeAuth: () => CodexAuthPreflight
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* [可选] 启动前 CLI 命令探测:
|
|
21
|
+
* 返回实际执行的 { command, args },若返回 null 表示未安装,提前提示安装指引
|
|
22
|
+
*/
|
|
23
|
+
resolveSpawnCommand?: () => { command: string; args: string[] } | null
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* [可选] 启动前预检拦截:
|
|
27
|
+
* 返回非空字符串表示拦截并提示用户(例如缺少必要环境变量时阻止 spawn)
|
|
28
|
+
*/
|
|
29
|
+
resolveSpawnBlocker?: () => string | null | undefined
|
|
30
|
+
|
|
31
|
+
/** [可选] 环境变量定制 */
|
|
32
|
+
getSpawnEnv?: (proxySettings?: Partial<AcpProxySettings>) => {
|
|
33
|
+
env: NodeJS.ProcessEnv
|
|
34
|
+
envRemove: string[]
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** [可选] 认证方式重新排序 */
|
|
38
|
+
orderAuthMethods?: (methods: AcpAuthMethod[]) => AcpAuthMethod[]
|
|
39
|
+
|
|
40
|
+
/** [可选] 是否尝试直接建立会话(针对把 Token 保存在系统 Keychain 的 CLI) */
|
|
41
|
+
tryDirectSessionFirst?: boolean
|
|
42
|
+
}
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## 步骤教学:以接入 `ollama-acp` 为例
|
|
48
|
+
|
|
49
|
+
### 步骤 1:新建适配器目录与文件
|
|
50
|
+
|
|
51
|
+
在 `packages/acp/src/runtimes/` 下新建目录 `ollama/index.ts`:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
// packages/acp/src/runtimes/ollama/index.ts
|
|
55
|
+
import type { CodexAuthPreflight } from '@yitom/acp-client'
|
|
56
|
+
import type { GenericRuntimeAdapter } from '../index'
|
|
57
|
+
|
|
58
|
+
export function probeOllamaAuth(): CodexAuthPreflight {
|
|
59
|
+
// 检查本机是否已运行 Ollama 服务或配置了 OLLAMA_HOST
|
|
60
|
+
const hasHostEnv = Boolean(process.env.OLLAMA_HOST?.trim())
|
|
61
|
+
return {
|
|
62
|
+
hasCodexHome: false,
|
|
63
|
+
hasAuthFile: false,
|
|
64
|
+
hasApiKeyEnv: hasHostEnv,
|
|
65
|
+
looksLoggedIn: true, // 本地开源模型通常无需登录
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const ollamaAdapter: GenericRuntimeAdapter = {
|
|
70
|
+
id: 'ollama-acp',
|
|
71
|
+
probeAuth: () => probeOllamaAuth(),
|
|
72
|
+
resolveSpawnBlocker: () => {
|
|
73
|
+
// 检查服务是否存活,或者放行
|
|
74
|
+
return null
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 步骤 2:在适配器矩阵中注册
|
|
80
|
+
|
|
81
|
+
打开 `packages/acp/src/runtimes/index.ts`,导出并加入工厂分发:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
import { ollamaAdapter } from './ollama'
|
|
85
|
+
|
|
86
|
+
export * from './ollama'
|
|
87
|
+
|
|
88
|
+
export function getAcpRuntimeAdapter(runtimeId: string): AcpRuntimeAdapter {
|
|
89
|
+
switch (runtimeId) {
|
|
90
|
+
// ... 原有适配器
|
|
91
|
+
case ollamaAdapter.id:
|
|
92
|
+
return ollamaAdapter
|
|
93
|
+
default:
|
|
94
|
+
return { id: runtimeId, probeAuth: emptyAcpAuthPreflight }
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 步骤 3:验证与测试
|
|
100
|
+
|
|
101
|
+
编写对应的单元测试 `ollama/index.test.ts`:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { describe, expect, it } from 'vitest'
|
|
105
|
+
import { getAcpRuntimeAdapter } from '../index'
|
|
106
|
+
|
|
107
|
+
describe('ollama runtime adapter', () => {
|
|
108
|
+
it('正确派发 ollama 适配器', () => {
|
|
109
|
+
const adapter = getAcpRuntimeAdapter('ollama-acp')
|
|
110
|
+
expect(adapter.id).toBe('ollama-acp')
|
|
111
|
+
expect(adapter.probeAuth().looksLoggedIn).toBe(true)
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
运行测试:
|
|
117
|
+
```bash
|
|
118
|
+
bunx vitest run packages/acp/src/runtimes/ollama/index.test.ts
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
完成!此后任何项目使用 `new AcpClient({ runtime: 'ollama-acp', ... })` 即可开箱即用。
|