qidao-openclaw-plugin 1.0.7 → 1.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/openclaw.plugin.json +4 -5
- package/package.json +1 -1
- package/src/index.js +35 -74
package/openclaw.plugin.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"id": "qidao-openclaw-plugin",
|
|
3
|
-
"
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
"author": "Qidao Team",
|
|
7
|
-
"license": "MIT",
|
|
3
|
+
"channels": [
|
|
4
|
+
"qidao"
|
|
5
|
+
],
|
|
8
6
|
"configSchema": {
|
|
9
7
|
"type": "object",
|
|
8
|
+
"additionalProperties": false,
|
|
10
9
|
"properties": {}
|
|
11
10
|
}
|
|
12
11
|
}
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -111,6 +111,41 @@ const qidaoChannel = {
|
|
|
111
111
|
}
|
|
112
112
|
},
|
|
113
113
|
},
|
|
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
|
+
},
|
|
114
149
|
};
|
|
115
150
|
|
|
116
151
|
export default function register(api) {
|
|
@@ -119,79 +154,5 @@ export default function register(api) {
|
|
|
119
154
|
// 注册Channel
|
|
120
155
|
api.registerChannel({ plugin: qidaoChannel });
|
|
121
156
|
|
|
122
|
-
// 异步初始化账户连接
|
|
123
|
-
setImmediate(async () => {
|
|
124
|
-
try {
|
|
125
|
-
const cfg = api.getGatewayConfig?.() || {};
|
|
126
|
-
const accounts = cfg.channels?.qidao?.accounts ?? {};
|
|
127
|
-
|
|
128
|
-
// 为每个启用的账户创建连接
|
|
129
|
-
Object.entries(accounts).forEach(([accountId, accountConfig]) => {
|
|
130
|
-
if (accountConfig.enabled === false) {
|
|
131
|
-
api.logger.info(`栖岛账户 ${accountId} 已禁用`);
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
if (!accountConfig.chatId) {
|
|
136
|
-
api.logger.warn(`栖岛账户 ${accountId} 缺少chatId配置`);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const serverUrl = accountConfig.serverUrl ?? 'wss://oc.qidao.chat/ws';
|
|
141
|
-
|
|
142
|
-
api.logger.info(`连接栖岛账户: ${accountId} (chatId: ${accountConfig.chatId})`);
|
|
143
|
-
|
|
144
|
-
// 创建连接
|
|
145
|
-
const connection = new QidaoChannel({
|
|
146
|
-
serverUrl,
|
|
147
|
-
chatId: accountConfig.chatId,
|
|
148
|
-
userId: accountConfig.userId,
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
// 监听连接事件
|
|
152
|
-
connection.on('connect', () => {
|
|
153
|
-
api.logger.info(`栖岛账户 ${accountId} 已连接`);
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
connection.on('disconnect', () => {
|
|
157
|
-
api.logger.warn(`栖岛账户 ${accountId} 已断开`);
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
connection.on('error', (error) => {
|
|
161
|
-
api.logger.error(`栖岛账户 ${accountId} 错误: ${error.message}`);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
// 监听栖岛消息,转发给OpenClaw
|
|
165
|
-
connection.on('message', (message) => {
|
|
166
|
-
api.logger.debug(`栖岛账户 ${accountId} 收到消息:`, message);
|
|
167
|
-
|
|
168
|
-
// 将栖岛消息转换为OpenClaw消息格式
|
|
169
|
-
if (message.type === 'new_message') {
|
|
170
|
-
api.ingestMessage({
|
|
171
|
-
channelId: 'qidao',
|
|
172
|
-
accountId: accountId,
|
|
173
|
-
senderId: message.senderId.toString(),
|
|
174
|
-
senderName: message.senderName,
|
|
175
|
-
text: message.messageText,
|
|
176
|
-
timestamp: message.timestamp,
|
|
177
|
-
chatType: message.chatType === 0 ? 'direct' : 'group',
|
|
178
|
-
chatId: message.chatId.toString(),
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
// 连接到服务器
|
|
184
|
-
connection.connect().catch((error) => {
|
|
185
|
-
api.logger.error(`栖岛账户 ${accountId} 连接失败: ${error.message}`);
|
|
186
|
-
});
|
|
187
|
-
|
|
188
|
-
// 保存连接实例
|
|
189
|
-
accountConnections.set(accountId, connection);
|
|
190
|
-
});
|
|
191
|
-
} catch (error) {
|
|
192
|
-
api.logger.error(`栖岛聊天插件初始化失败: ${error.message}`);
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
|
|
196
157
|
api.logger.info('栖岛聊天插件加载完成');
|
|
197
158
|
}
|