42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
/** 检查工具卡与最后回复。用法:node scripts/probe-tools.mjs <port> */
|
|
|
|
const port = Number(process.argv[2] ?? 9335)
|
|
|
|
async function main() {
|
|
const list = await (await fetch(`http://127.0.0.1:${port}/json/list`)).json()
|
|
const page = list.filter((t) => t.type === 'page')[0]
|
|
const ws = new WebSocket(page.webSocketDebuggerUrl)
|
|
await new Promise((r) => ws.addEventListener('open', r))
|
|
const send = (method, params = {}) =>
|
|
new Promise((res) => {
|
|
const id = 1
|
|
const h = (e) => {
|
|
const d = JSON.parse(e.data)
|
|
if (d.id === id) {
|
|
ws.removeEventListener('message', h)
|
|
res(d.result)
|
|
}
|
|
}
|
|
ws.addEventListener('message', h)
|
|
ws.send(JSON.stringify({ id, method, params }))
|
|
})
|
|
|
|
const r = await send('Runtime.evaluate', {
|
|
expression: `JSON.stringify({
|
|
toolCards: document.querySelectorAll('.tool-card').length,
|
|
toolNames: [...document.querySelectorAll('.tool-card__name')].map((n) => n.textContent),
|
|
toolStatus: [...document.querySelectorAll('.tool-card__dot')].map((n) => n.dataset.status),
|
|
lastContent: [...document.querySelectorAll('.msg--assistant .msg__content')].pop()?.textContent.slice(0, 400),
|
|
statuses: [...document.querySelectorAll('.msg--assistant')].map((m) => m.querySelector('.msg__status')?.textContent ?? '')
|
|
})`,
|
|
returnByValue: true
|
|
})
|
|
console.log(JSON.parse(r.result.value))
|
|
ws.close()
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error('失败:', e.message)
|
|
process.exit(1)
|
|
})
|