protocol-factory.js
1.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
/**
* Protocol Factory — 注册制 Protocol 工厂
*
* 通过 name → constructor 映射表创建 Protocol 实例。
* 新增协议只需加一行注册,不需要改 AgentManager。
*/
const PROTOCOLS = {
'acp': () => require('@mas/acp-core').AcpProtocol,
'claude-cli': () => require('@mas/acp-core').ClaudeCliProtocol,
'claude-stream': () => require('@mas/acp-core').ClaudeStreamProtocol,
'codex-jsonl': () => require('@mas/acp-core').CodexJsonlProtocol,
};
/**
* 创建 Protocol 实例
*
* @param {string} protocolName - 协议名(acp / claude-stream / codex-jsonl)
* @param {object} config - { id, name }
* @param {object} [options] - 协议选项(yolo 等)
* @returns {Protocol}
*/
function createProtocol(protocolName, config, options = {}) {
const factory = PROTOCOLS[protocolName];
if (!factory) {
const available = Object.keys(PROTOCOLS).join(', ');
throw new Error(`Unknown protocol: "${protocolName}". Available: ${available}`);
}
const Cls = factory();
return new Cls(config, options);
}
/**
* 注册新协议(运行时扩展)
*
* @param {string} name - 协议名
* @param {function} factory - 返回 Protocol 构造函数的工厂
*/
function registerProtocol(name, factory) {
PROTOCOLS[name] = factory;
}
module.exports = { createProtocol, registerProtocol };