qidao-openclaw-plugin 1.3.2 → 1.3.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/auth-cli.js +157 -9
- package/src/index.js +28 -12
package/package.json
CHANGED
package/src/auth-cli.js
CHANGED
|
@@ -2,6 +2,9 @@
|
|
|
2
2
|
* 栖岛聊天认证 CLI 命令
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
+
import ws from 'ws';
|
|
6
|
+
import { spawn } from 'child_process';
|
|
7
|
+
|
|
5
8
|
export function registerAuthCommand(api) {
|
|
6
9
|
api.registerCli(
|
|
7
10
|
({ program }) => {
|
|
@@ -9,17 +12,162 @@ export function registerAuthCommand(api) {
|
|
|
9
12
|
.command('qidao-auth')
|
|
10
13
|
.description('栖岛聊天二维码认证')
|
|
11
14
|
.action(async () => {
|
|
12
|
-
console.log('🔐 开始栖岛聊天认证流程...');
|
|
13
15
|
console.log('');
|
|
14
|
-
console.log('
|
|
15
|
-
console.log('
|
|
16
|
-
console.log('
|
|
17
|
-
console.log('
|
|
16
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
17
|
+
console.log('');
|
|
18
|
+
console.log(' 🔐 栖岛聊天二维码认证');
|
|
19
|
+
console.log('');
|
|
20
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
18
21
|
console.log('');
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
// 创建临时连接到 Server 进行认证
|
|
25
|
+
const tempWs = new ws('wss://oc.qidao.chat/ws');
|
|
26
|
+
|
|
27
|
+
const chatId = await new Promise((resolve, reject) => {
|
|
28
|
+
const timeout = setTimeout(() => {
|
|
29
|
+
tempWs.close();
|
|
30
|
+
reject(new Error('连接超时'));
|
|
31
|
+
}, 10000);
|
|
32
|
+
|
|
33
|
+
tempWs.on('open', () => {
|
|
34
|
+
clearTimeout(timeout);
|
|
35
|
+
console.log('✅ 已连接到认证服务器');
|
|
36
|
+
console.log('');
|
|
37
|
+
|
|
38
|
+
// 请求生成二维码
|
|
39
|
+
tempWs.send(JSON.stringify({ action: 'startAuth' }));
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
tempWs.on('message', async (data) => {
|
|
43
|
+
try {
|
|
44
|
+
const message = JSON.parse(data.toString());
|
|
45
|
+
|
|
46
|
+
// 兼容Server的响应格式
|
|
47
|
+
// 格式1: { id, code: 1, msg, data: { code, qrUrl } }
|
|
48
|
+
// 格式2: { action: 'authStarted', code, qrUrl }
|
|
49
|
+
if ((message.code === 1 && message.data && message.data.qrUrl) ||
|
|
50
|
+
(message.action === 'authStarted')) {
|
|
51
|
+
const code = message.data?.code || message.code;
|
|
52
|
+
const qrUrl = message.data?.qrUrl || message.qrUrl;
|
|
53
|
+
|
|
54
|
+
console.log('📱 请使用栖岛 APP 扫描二维码:');
|
|
55
|
+
console.log('');
|
|
56
|
+
console.log(` ${qrUrl}`);
|
|
57
|
+
console.log('');
|
|
58
|
+
console.log('正在打开浏览器...');
|
|
59
|
+
console.log('');
|
|
60
|
+
|
|
61
|
+
// 打开浏览器显示二维码
|
|
62
|
+
const openCommand = process.platform === 'win32' ? 'start' :
|
|
63
|
+
process.platform === 'darwin' ? 'open' : 'xdg-open';
|
|
64
|
+
spawn(openCommand, [qrUrl], { shell: true, detached: true });
|
|
65
|
+
|
|
66
|
+
console.log('⏳ 等待扫码...');
|
|
67
|
+
console.log('');
|
|
68
|
+
|
|
69
|
+
// 开始轮询扫码状态
|
|
70
|
+
const pollInterval = setInterval(() => {
|
|
71
|
+
tempWs.send(JSON.stringify({ action: 'checkAuthStatus', code }));
|
|
72
|
+
}, 2000);
|
|
73
|
+
|
|
74
|
+
// 保存轮询定时器以便后续清理
|
|
75
|
+
tempWs._pollInterval = pollInterval;
|
|
76
|
+
tempWs._code = code;
|
|
77
|
+
|
|
78
|
+
} else if ((message.code === 1 && message.data && message.data.status) ||
|
|
79
|
+
(message.action === 'authStatus')) {
|
|
80
|
+
// 兼容两种格式的扫码状态响应
|
|
81
|
+
const status = message.data?.status || message.status;
|
|
82
|
+
const uid = message.data?.uid || message.uid;
|
|
83
|
+
|
|
84
|
+
if (status === 'success') {
|
|
85
|
+
console.log('✅ 扫码成功!');
|
|
86
|
+
console.log('');
|
|
87
|
+
|
|
88
|
+
// 请求完成认证获取 chatId
|
|
89
|
+
tempWs.send(JSON.stringify({
|
|
90
|
+
action: 'completeAuth',
|
|
91
|
+
uid,
|
|
92
|
+
code: tempWs._code
|
|
93
|
+
}));
|
|
94
|
+
|
|
95
|
+
} else if (status === 'waiting') {
|
|
96
|
+
// 继续等待
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
} else if ((message.code === 1 && message.data && message.data.chatId) ||
|
|
100
|
+
(message.action === 'authCompleted')) {
|
|
101
|
+
// 清理轮询定时器
|
|
102
|
+
if (tempWs._pollInterval) {
|
|
103
|
+
clearInterval(tempWs._pollInterval);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const receivedChatId = message.data?.chatId || message.chatId;
|
|
107
|
+
console.log(`🎉 认证完成!您的 chatId: ${receivedChatId}`);
|
|
108
|
+
console.log('');
|
|
109
|
+
|
|
110
|
+
tempWs.close();
|
|
111
|
+
resolve(receivedChatId);
|
|
112
|
+
|
|
113
|
+
} else if (message.code === 0 || message.action === 'error') {
|
|
114
|
+
if (tempWs._pollInterval) {
|
|
115
|
+
clearInterval(tempWs._pollInterval);
|
|
116
|
+
}
|
|
117
|
+
tempWs.close();
|
|
118
|
+
const errorMsg = message.msg || message.message || '认证失败';
|
|
119
|
+
reject(new Error(errorMsg));
|
|
120
|
+
}
|
|
121
|
+
} catch (err) {
|
|
122
|
+
console.error('处理消息失败:', err);
|
|
123
|
+
reject(err);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
tempWs.on('error', (error) => {
|
|
128
|
+
clearTimeout(timeout);
|
|
129
|
+
if (tempWs._pollInterval) {
|
|
130
|
+
clearInterval(tempWs._pollInterval);
|
|
131
|
+
}
|
|
132
|
+
reject(error);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
// 自动保存配置
|
|
137
|
+
console.log('正在保存配置...');
|
|
138
|
+
console.log('');
|
|
139
|
+
|
|
140
|
+
const { execSync } = await import('child_process');
|
|
141
|
+
execSync(`openclaw config set channels.qidao.chatId ${chatId}`, { stdio: 'inherit' });
|
|
142
|
+
execSync(`openclaw config set channels.qidao.enabled true`, { stdio: 'inherit' });
|
|
143
|
+
|
|
144
|
+
console.log('');
|
|
145
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
146
|
+
console.log('');
|
|
147
|
+
console.log(' ✅ 配置完成!');
|
|
148
|
+
console.log('');
|
|
149
|
+
console.log(' 请重启 Gateway 以应用配置:');
|
|
150
|
+
console.log('');
|
|
151
|
+
console.log(' openclaw gateway restart');
|
|
152
|
+
console.log('');
|
|
153
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
154
|
+
console.log('');
|
|
155
|
+
|
|
156
|
+
} catch (error) {
|
|
157
|
+
console.error('');
|
|
158
|
+
console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
159
|
+
console.error('');
|
|
160
|
+
console.error(' ❌ 认证失败:', error.message);
|
|
161
|
+
console.error('');
|
|
162
|
+
console.error(' 请重试或手动配置:');
|
|
163
|
+
console.error('');
|
|
164
|
+
console.error(' openclaw config set channels.qidao.chatId <您的chatId>');
|
|
165
|
+
console.error(' openclaw config set channels.qidao.enabled true');
|
|
166
|
+
console.error('');
|
|
167
|
+
console.error('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
168
|
+
console.error('');
|
|
169
|
+
process.exit(1);
|
|
170
|
+
}
|
|
23
171
|
});
|
|
24
172
|
},
|
|
25
173
|
{ commands: ['qidao-auth'] }
|
package/src/index.js
CHANGED
|
@@ -46,7 +46,7 @@ const qidaoChannel = {
|
|
|
46
46
|
enabled: qidaoConfig.enabled !== false,
|
|
47
47
|
chatId: qidaoConfig.chatId,
|
|
48
48
|
userId: qidaoConfig.userId,
|
|
49
|
-
serverUrl:
|
|
49
|
+
serverUrl: 'wss://oc.qidao.chat/ws', // 固定的服务器地址
|
|
50
50
|
};
|
|
51
51
|
},
|
|
52
52
|
},
|
|
@@ -81,40 +81,56 @@ const qidaoChannel = {
|
|
|
81
81
|
|
|
82
82
|
// 提示用户
|
|
83
83
|
console.log('');
|
|
84
|
-
console.log('
|
|
85
|
-
console.log('
|
|
84
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
85
|
+
console.log('');
|
|
86
|
+
console.log(' 📱 栖岛聊天配置');
|
|
87
|
+
console.log('');
|
|
88
|
+
console.log(' 推荐使用二维码认证(自动获取 chatId):');
|
|
89
|
+
console.log('');
|
|
90
|
+
console.log(' 请退出此向导(按 Ctrl+C),然后运行:');
|
|
91
|
+
console.log('');
|
|
92
|
+
console.log(' openclaw qidao-auth');
|
|
93
|
+
console.log('');
|
|
94
|
+
console.log(' 或者继续手动输入 chatId');
|
|
95
|
+
console.log('');
|
|
96
|
+
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
|
|
86
97
|
console.log('');
|
|
87
98
|
|
|
88
99
|
const chatIdInput = await prompter.text({
|
|
89
|
-
message: '请输入栖岛 Chat ID:',
|
|
100
|
+
message: '请输入栖岛 Chat ID (或按 Ctrl+C 退出后使用 openclaw qidao-auth):',
|
|
90
101
|
default: qidaoConfig.chatId?.toString() || '',
|
|
91
102
|
});
|
|
92
103
|
|
|
93
104
|
// 检查输入是否为空或只有空格
|
|
94
105
|
if (!chatIdInput || chatIdInput.trim() === '') {
|
|
106
|
+
console.log('');
|
|
95
107
|
console.log('未输入 chatId,取消配置');
|
|
108
|
+
console.log('');
|
|
109
|
+
console.log('提示:运行 openclaw qidao-auth 使用二维码认证');
|
|
110
|
+
console.log('');
|
|
96
111
|
return { cfg };
|
|
97
112
|
}
|
|
98
113
|
|
|
99
|
-
const
|
|
100
|
-
message: '请输入服务器地址:',
|
|
101
|
-
default: qidaoConfig.serverUrl || 'wss://oc.qidao.chat/ws',
|
|
102
|
-
});
|
|
114
|
+
const chatId = parseInt(chatIdInput.trim());
|
|
103
115
|
|
|
104
|
-
// 构建新配置
|
|
116
|
+
// 构建新配置
|
|
105
117
|
const newCfg = {
|
|
106
118
|
...cfg,
|
|
107
119
|
channels: {
|
|
108
120
|
...cfg.channels,
|
|
109
121
|
qidao: {
|
|
110
122
|
enabled: true,
|
|
111
|
-
chatId
|
|
112
|
-
serverUrl: (serverUrl && serverUrl.trim()) || 'wss://oc.qidao.chat/ws',
|
|
123
|
+
chatId,
|
|
113
124
|
},
|
|
114
125
|
},
|
|
115
126
|
};
|
|
116
127
|
|
|
117
|
-
console.log('
|
|
128
|
+
console.log('');
|
|
129
|
+
console.log('✅ 配置已保存:', newCfg.channels.qidao);
|
|
130
|
+
console.log('');
|
|
131
|
+
console.log('请重启 Gateway 以应用配置:');
|
|
132
|
+
console.log(' openclaw gateway restart');
|
|
133
|
+
console.log('');
|
|
118
134
|
|
|
119
135
|
return { cfg: newCfg, accountId: 'default' };
|
|
120
136
|
},
|