/** * 对「Python 内核」(安卓端内置的那份)做完整验收。 * 用法: node scripts/verify-pykernel.mjs [base] [mockBase] */ const BASE = process.argv[2] || 'http://127.0.0.1:5399'; const MOCK = process.argv[3] || 'http://127.0.0.1:5299/v1'; let pass = 0; let fail = 0; function check(label, ok, detail) { console.log(`${ok ? '✅' : '❌'} ${label}${detail ? ` → ${String(detail).slice(0, 220)}` : ''}`); if (ok) pass += 1; else fail += 1; } const get = async (p) => (await fetch(BASE + p)).json(); const post = async (p, body) => { const res = await fetch(BASE + p, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body ?? {}), }); return res.json(); }; /** /api/chat 是 SSE,不能按 JSON 解,这里只把流读干 */ const chatProbe = async (message) => { const res = await fetch(`${BASE}/api/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ conversationId: 'c_main', message, history: [] }), }); await res.text(); }; const put = async (p, body) => { const res = await fetch(BASE + p, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body ?? {}), }); return res.json(); }; console.log('=== 1. 状态 ==='); const state = await get('/api/state'); check('智能体 4 个', state.agents?.length === 4, state.agents?.map((a) => a.name).join(', ')); check('内置工具齐全', ['calc', 'now', 'web_fetch', 'notes', 'shell', 'python_run'].every((n) => state.toolNames.includes(n)), state.toolNames.filter((n) => !n.startsWith('mcp__')).join(', ')); console.log('\n=== 2. shell 工具 ==='); const sh = await post('/api/tools/run', { name: 'shell', args: { command: 'echo hello-shell' } }); check('执行 echo', sh.ok && String(sh.result?.stdout || '').includes('hello-shell'), JSON.stringify(sh.result || sh.error).slice(0, 160)); const shBad = await post('/api/tools/run', { name: 'shell', args: { command: 'rm -rf /' } }); check('黑名单拦截', shBad.ok === false, shBad.error); console.log('\n=== 3. python_run 工具 ==='); const code = 'import sys, platform, json\nprint(json.dumps({"py": sys.version.split()[0], "os": platform.system()}))'; const py = await post('/api/tools/run', { name: 'python_run', args: { code } }); check('进程内执行代码', py.ok && String(py.result?.stdout || '').includes('py'), JSON.stringify(py.result || py.error).slice(0, 200)); const libs = await post('/api/tools/run', { name: 'python_run', args: { action: 'libs' } }); check('libs 动作', libs.ok && Array.isArray(libs.result?.available), `可用 ${libs.result?.available?.length || 0} 个`); const net = await post('/api/tools/run', { name: 'python_run', args: { code: 'import urllib.request\nprint(urllib.request.urlopen("https://example.com", timeout=15).status)' }, }); check('python_run 能联网', net.ok && String(net.result?.stdout || '').includes('200'), JSON.stringify(net.result?.stdout || net.error).slice(0, 120)); console.log('\n=== 4. 文件工具(证明能加载 tools/*.py)==='); const calc = await post('/api/tools/run', { name: 'calc', args: { expression: 'sqrt(16)+log(100,10)' } }); check('calc', calc.ok && calc.result?.value === 6, JSON.stringify(calc.result || calc.error).slice(0, 120)); console.log('\n=== 5. MCP(含 Python 写的服务)==='); const mcpTools = state.toolNames.filter((n) => n.startsWith('mcp__')); check('MCP 工具已注册', mcpTools.length > 0, mcpTools.join(', ')); if (mcpTools.length) { const target = mcpTools[0]; const arg = target.includes('add') ? { a: 2, b: 3 } : target.includes('word_count') ? { text: 'hello 世界' } : {}; const call = await post('/api/tools/run', { name: target, args: arg }); check(`调用 ${target}`, call.ok === true, JSON.stringify(call.result ?? call.error).slice(0, 160)); } console.log('\n=== 6. 思考强度映射 ==='); for (const level of ['off', 'high']) { await put('/api/settings', { thinkingLevel: level, provider: 'ark', baseUrl: MOCK, apiKey: 't', model: 'doubao-seed-2-1-pro-260628' }); const s = await get('/api/state'); check(`设置 thinkingLevel=${level}`, s.settings.thinkingLevel === level, s.settings.thinkingLevel); await chatProbe(`probe-${level}`); } await put('/api/settings', { provider: 'openai', model: 'gpt-5-mini', thinkingLevel: 'high' }); await chatProbe('probe-openai'); console.log('\n=== 7. SSE 事件序列 ==='); await put('/api/settings', { provider: 'ark', baseUrl: MOCK, apiKey: 't', model: 'doubao-seed-2-1-pro-260628', thinkingLevel: 'auto' }); const res = await fetch(`${BASE}/api/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ conversationId: 'c_main', message: '帮我算一下 (1+2)*3', history: [] }), }); const events = []; let buf = ''; const reader = res.body.getReader(); const dec = new TextDecoder(); for (;;) { const { done, value } = await reader.read(); if (done) break; buf += dec.decode(value, { stream: true }); let i; while ((i = buf.indexOf('\n\n')) >= 0) { const block = buf.slice(0, i); buf = buf.slice(i + 2); for (const line of block.split('\n')) { const t = line.trim(); if (t.startsWith('data:')) { try { events.push(JSON.parse(t.slice(5))); } catch { /* ignore */ } } } } } const kinds = [...new Set(events.map((e) => e.type))]; check('有 start/delta/done', ['start', 'delta', 'done'].every((k) => kinds.includes(k)), kinds.join(', ')); const done = events.find((e) => e.type === 'done'); check('done 里带正文', Boolean(done?.content), (done?.content || '').slice(0, 80)); console.log('\n=== 8. 会话持久化 ==='); const convs = await get('/api/conversations'); check('会话已落库', convs.conversations?.some((c) => c.messageCount > 0), convs.conversations?.map((c) => `${c.title}(${c.messageCount})`).join(', ')); console.log(`\n结果:${pass} 通过 / ${fail} 失败`); process.exit(fail === 0 ? 0 : 1);