Files
dnd-claw/agent-core/test-ws.mjs
T

63 lines
1.4 KiB
JavaScript

// 测试 agent-core WebSocket 通信
import WebSocket from 'ws'
const ws = new WebSocket('ws://localhost:8787')
ws.on('open', () => {
console.log('[测试] 已连接')
// 测试 1: 保存 LLM 配置
ws.send(JSON.stringify({
type: 'llm:save',
data: {
name: '文心千帆测试',
provider: 'qianfan_v2',
config: {
baseUrl: 'https://qianfan.baidubce.com/v2/chat/completions',
authorization: 'Bearer test-token',
model: 'ernie-4.0',
temperature: 0.7
}
},
id: '1'
}))
})
ws.on('message', (data) => {
const msg = JSON.parse(data.toString())
console.log('[收到]', JSON.stringify(msg, null, 2))
// 收到保存响应后,测试获取列表
if (msg.type === 'llm:saved') {
ws.send(JSON.stringify({ type: 'llm:list', id: '2' }))
}
if (msg.type === 'llm:list') {
// 测试设置激活
if (msg.data && msg.data[0]) {
ws.send(JSON.stringify({ type: 'llm:setActive', data: { id: msg.data[0].id }, id: '3' }))
}
}
if (msg.type === 'llm:activeSet') {
// 测试获取激活
ws.send(JSON.stringify({ type: 'llm:getActive', id: '4' }))
}
if (msg.type === 'llm:active') {
console.log('\n=== 所有测试完成 ===')
ws.close()
process.exit(0)
}
})
ws.on('error', (err) => {
console.error('[错误]', err.message)
process.exit(1)
})
setTimeout(() => {
console.log('超时退出')
process.exit(1)
}, 10000)