qidao-openclaw-plugin 1.1.0 → 1.1.3
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/package.json +1 -1
- package/src/index.js +117 -71
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 栖岛聊天 OpenClaw Channel 插件
|
|
3
|
+
* 严格按照官方文档实现: https://docs.openclaw.ai/tools/plugin
|
|
3
4
|
*/
|
|
4
5
|
|
|
5
6
|
import { QidaoChannel } from './qidao-channel.js';
|
|
@@ -7,6 +8,7 @@ import { QidaoChannel } from './qidao-channel.js';
|
|
|
7
8
|
// 存储所有账户的连接实例
|
|
8
9
|
const accountConnections = new Map();
|
|
9
10
|
|
|
11
|
+
// Channel 定义
|
|
10
12
|
const qidaoChannel = {
|
|
11
13
|
id: 'qidao',
|
|
12
14
|
|
|
@@ -19,19 +21,14 @@ const qidaoChannel = {
|
|
|
19
21
|
|
|
20
22
|
capabilities: {
|
|
21
23
|
chatTypes: ['direct', 'group'],
|
|
22
|
-
supportsMedia: true,
|
|
23
|
-
supportsReactions: false,
|
|
24
|
-
supportsThreads: false,
|
|
25
24
|
},
|
|
26
25
|
|
|
27
26
|
config: {
|
|
28
|
-
// 列出所有配置的账户ID
|
|
29
27
|
listAccountIds: (cfg) => {
|
|
30
28
|
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
31
29
|
return Object.keys(accounts);
|
|
32
30
|
},
|
|
33
31
|
|
|
34
|
-
// 解析指定账户的配置
|
|
35
32
|
resolveAccount: (cfg, accountId) => {
|
|
36
33
|
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
37
34
|
const account = accounts[accountId ?? 'default'];
|
|
@@ -50,40 +47,52 @@ const qidaoChannel = {
|
|
|
50
47
|
},
|
|
51
48
|
},
|
|
52
49
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const connection = accountConnections.get(accountId ?? 'default');
|
|
50
|
+
// 添加 setup 配置向导
|
|
51
|
+
setup: {
|
|
52
|
+
configure: async ({ cfg, prompter }) => {
|
|
53
|
+
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
54
|
+
const defaultAccount = accounts.default ?? {};
|
|
59
55
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
};
|
|
65
|
-
}
|
|
56
|
+
const chatId = await prompter.text({
|
|
57
|
+
message: '请输入栖岛 Chat ID:',
|
|
58
|
+
default: defaultAccount.chatId?.toString(),
|
|
59
|
+
});
|
|
66
60
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
};
|
|
72
|
-
}
|
|
61
|
+
const userId = await prompter.text({
|
|
62
|
+
message: '请输入栖岛 User ID (可选):',
|
|
63
|
+
default: defaultAccount.userId?.toString() || '',
|
|
64
|
+
});
|
|
73
65
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
66
|
+
const serverUrl = await prompter.text({
|
|
67
|
+
message: '请输入服务器地址:',
|
|
68
|
+
default: defaultAccount.serverUrl || 'wss://oc.qidao.chat/ws',
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const newCfg = {
|
|
72
|
+
...cfg,
|
|
73
|
+
channels: {
|
|
74
|
+
...cfg.channels,
|
|
75
|
+
qidao: {
|
|
76
|
+
accounts: {
|
|
77
|
+
default: {
|
|
78
|
+
enabled: true,
|
|
79
|
+
chatId: parseInt(chatId),
|
|
80
|
+
userId: userId ? parseInt(userId) : undefined,
|
|
81
|
+
serverUrl,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
return { cfg: newCfg, accountId: 'default' };
|
|
83
89
|
},
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
outbound: {
|
|
93
|
+
deliveryMode: 'direct',
|
|
84
94
|
|
|
85
|
-
|
|
86
|
-
sendMedia: async ({ url, accountId, chatId }) => {
|
|
95
|
+
sendText: async ({ text, accountId, chatId }) => {
|
|
87
96
|
const connection = accountConnections.get(accountId ?? 'default');
|
|
88
97
|
|
|
89
98
|
if (!connection || !connection.isConnected()) {
|
|
@@ -101,7 +110,7 @@ const qidaoChannel = {
|
|
|
101
110
|
}
|
|
102
111
|
|
|
103
112
|
try {
|
|
104
|
-
await connection.
|
|
113
|
+
await connection.sendMessage(parseInt(chatId), text);
|
|
105
114
|
return { ok: true };
|
|
106
115
|
} catch (error) {
|
|
107
116
|
return {
|
|
@@ -111,48 +120,85 @@ const qidaoChannel = {
|
|
|
111
120
|
}
|
|
112
121
|
},
|
|
113
122
|
},
|
|
114
|
-
|
|
115
|
-
// 添加 gateway 配置,用于启动和停止连接
|
|
116
|
-
gateway: {
|
|
117
|
-
start: async (cfg, accountId) => {
|
|
118
|
-
const account = qidaoChannel.config.resolveAccount(cfg, accountId);
|
|
119
|
-
|
|
120
|
-
if (!account.enabled || !account.chatId) {
|
|
121
|
-
return { ok: false, error: '账户未配置或已禁用' };
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
try {
|
|
125
|
-
const connection = new QidaoChannel({
|
|
126
|
-
serverUrl: account.serverUrl,
|
|
127
|
-
chatId: account.chatId,
|
|
128
|
-
userId: account.userId,
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
await connection.connect();
|
|
132
|
-
accountConnections.set(accountId, connection);
|
|
133
|
-
|
|
134
|
-
return { ok: true };
|
|
135
|
-
} catch (error) {
|
|
136
|
-
return { ok: false, error: error.message };
|
|
137
|
-
}
|
|
138
|
-
},
|
|
139
|
-
|
|
140
|
-
stop: async (cfg, accountId) => {
|
|
141
|
-
const connection = accountConnections.get(accountId);
|
|
142
|
-
if (connection) {
|
|
143
|
-
connection.disconnect();
|
|
144
|
-
accountConnections.delete(accountId);
|
|
145
|
-
}
|
|
146
|
-
return { ok: true };
|
|
147
|
-
},
|
|
148
|
-
},
|
|
149
123
|
};
|
|
150
124
|
|
|
125
|
+
// 插件注册函数
|
|
151
126
|
export default function register(api) {
|
|
152
127
|
api.logger.info('栖岛聊天插件加载中...');
|
|
153
128
|
|
|
154
|
-
// 注册Channel
|
|
129
|
+
// 注册 Channel
|
|
155
130
|
api.registerChannel({ plugin: qidaoChannel });
|
|
156
131
|
|
|
132
|
+
// 异步初始化连接
|
|
133
|
+
setImmediate(async () => {
|
|
134
|
+
try {
|
|
135
|
+
const cfg = api.getGatewayConfig?.() || {};
|
|
136
|
+
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
137
|
+
|
|
138
|
+
for (const [accountId, accountConfig] of Object.entries(accounts)) {
|
|
139
|
+
if (accountConfig.enabled === false) {
|
|
140
|
+
api.logger.info(`栖岛账户 ${accountId} 已禁用`);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (!accountConfig.chatId) {
|
|
145
|
+
api.logger.warn(`栖岛账户 ${accountId} 缺少chatId配置`);
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const serverUrl = accountConfig.serverUrl ?? 'wss://oc.qidao.chat/ws';
|
|
150
|
+
|
|
151
|
+
api.logger.info(`连接栖岛账户: ${accountId} (chatId: ${accountConfig.chatId})`);
|
|
152
|
+
|
|
153
|
+
try {
|
|
154
|
+
const connection = new QidaoChannel({
|
|
155
|
+
serverUrl,
|
|
156
|
+
chatId: accountConfig.chatId,
|
|
157
|
+
userId: accountConfig.userId,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// 监听连接事件
|
|
161
|
+
connection.on('connect', () => {
|
|
162
|
+
api.logger.info(`栖岛账户 ${accountId} 已连接`);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
connection.on('disconnect', () => {
|
|
166
|
+
api.logger.warn(`栖岛账户 ${accountId} 已断开`);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
connection.on('error', (error) => {
|
|
170
|
+
api.logger.error(`栖岛账户 ${accountId} 错误: ${error.message}`);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// 监听栖岛消息,转发给OpenClaw
|
|
174
|
+
connection.on('message', (message) => {
|
|
175
|
+
api.logger.debug(`栖岛账户 ${accountId} 收到消息:`, message);
|
|
176
|
+
|
|
177
|
+
if (message.type === 'new_message') {
|
|
178
|
+
api.ingestMessage({
|
|
179
|
+
channelId: 'qidao',
|
|
180
|
+
accountId: accountId,
|
|
181
|
+
senderId: message.senderId.toString(),
|
|
182
|
+
senderName: message.senderName,
|
|
183
|
+
text: message.messageText,
|
|
184
|
+
timestamp: message.timestamp,
|
|
185
|
+
chatType: message.chatType === 0 ? 'direct' : 'group',
|
|
186
|
+
chatId: message.chatId.toString(),
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
await connection.connect();
|
|
192
|
+
accountConnections.set(accountId, connection);
|
|
193
|
+
|
|
194
|
+
} catch (error) {
|
|
195
|
+
api.logger.error(`栖岛账户 ${accountId} 连接失败: ${error.message}`);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
} catch (error) {
|
|
199
|
+
api.logger.error(`栖岛聊天插件初始化失败: ${error.message}`);
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
|
|
157
203
|
api.logger.info('栖岛聊天插件加载完成');
|
|
158
204
|
}
|