52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
/** 读取当前会话消息的真实状态。用法:node scripts/probe-messages.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: `(async () => {
|
|
const sessions = await window.ycode.session.list();
|
|
const current = sessions[0];
|
|
if (!current) return JSON.stringify({ err: 'no session' });
|
|
const msgs = await window.ycode.session.messages(current.id);
|
|
return JSON.stringify({
|
|
sessionId: current.id,
|
|
messages: msgs.slice(-3).map((m) => ({
|
|
role: m.role,
|
|
status: m.status ?? null,
|
|
error: m.error ?? null,
|
|
len: (m.content || '').length,
|
|
tools: (m.tools || []).length
|
|
}))
|
|
});
|
|
})()`,
|
|
returnByValue: true,
|
|
awaitPromise: true
|
|
})
|
|
console.log(JSON.parse(r.result.value))
|
|
ws.close()
|
|
}
|
|
|
|
main().catch((e) => {
|
|
console.error('失败:', e.message)
|
|
process.exit(1)
|
|
})
|