runtime-resolver.js
2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Runtime Resolver — 从配置推断运行时参数
*
* 输入:agent 定义 + client 定义(camelCase,来自 _rowToClientConfig)
* 输出:protocol, transport, launcherType, launcherConfig
*
* 纯函数,无状态,无 DB 依赖。
*
* 推断规则:
* protocol = client.acpProtocol (claude → claude-stream, 其余 → acp)
* transport = deploy === 'local' ? 'pipe' : 'websocket'
* launcher = { local: 'process', remote: 'none' }[deploy]
*/
/**
* 推断运行时参数
*
* @param {object} agent - { type, client, deploy, ... }
* @param {object} client - { name, command, acpArgs, acpProtocol, ... }
* @returns {{
* protocol: string,
* transport: string,
* launcherType: string,
* command: string,
* args: string[],
* launcherConfig: object
* }}
*/
function resolve(agent, client) {
if (!client) {
throw new Error(`[RuntimeResolver] agent "${agent.name}" 没有关联 client`);
}
const deploy = agent.deploy || 'local';
// ── 推断 protocol ──
// 优先使用 agent 配置中的 protocol(per-agent 可覆盖),再用 client 级别默认
const protocol = agent.protocol || client.acpProtocol || 'acp';
// ── 推断 transport ──
const transport = deploy === 'local' ? 'pipe' : 'websocket';
// ── 推断 launcher type ──
const launcherTypeMap = { local: 'process', remote: 'none' };
const launcherType = launcherTypeMap[deploy];
if (!launcherType) {
throw new Error(`[RuntimeResolver] 未知 deploy 类型: ${deploy}`);
}
// ── 构建 launcher config(给 factory.js 用)──
const launcherConfig = buildLauncherConfig(client, deploy);
return {
protocol,
transport,
launcherType,
command: client.command,
args: client.acpArgs || [],
launcherConfig,
};
}
/**
* 构建 launcher config(兼容 factory.js 的 createConnection 参数格式)
*/
function buildLauncherConfig(client, deploy) {
switch (deploy) {
case 'local':
return {
type: 'process',
command: client.command,
args: client.acpArgs || [],
};
case 'remote':
return {
type: 'none',
url: client.remoteUrl,
};
default:
throw new Error(`[RuntimeResolver] 未知 deploy: ${deploy}`);
}
}
module.exports = { resolve, buildLauncherConfig };