import fs from 'node:fs/promises'; import path from 'node:path'; // Own application's local receipt metadata only; never emit key fingerprints or credentials. const config = JSON.parse(await fs.readFile(process.argv[2], 'utf8')); const accounts = path.join(config.broker.profile, 'jiansun', 'accounts'); const results = []; for (const scope of await fs.readdir(accounts)) { const root = path.join(accounts, scope, 'pi-validation'); let projects; try { projects = await fs.readdir(root); } catch (e) { if (e.code === 'ENOENT') continue; throw e; } for (const project of projects) { const dir = path.join(root, project, 'requests'); for (const name of await fs.readdir(dir)) { if (!name.endsWith('.json')) continue; const r = JSON.parse(await fs.readFile(path.join(dir, name), 'utf8')); const raw = Buffer.from(r.raw || '', 'base64').toString('utf8'); let content = '', reasoning = '', usage, responseId; const finish = [], calls = []; for (const line of raw.split(/\r?\n/)) { if (!line.startsWith('data:')) continue; let v; try { v = JSON.parse(line.slice(5).trim()); } catch { continue; } responseId ||= v.id; if (v.usage) usage = v.usage; for (const c of v.choices || []) { content += c.delta?.content || ''; reasoning += c.delta?.reasoning_content || c.delta?.reasoning || ''; if (c.finish_reason) finish.push(c.finish_reason); for (const t of c.delta?.tool_calls || []) calls.push({ index: t.index, name: t.function?.name, argumentBytes: t.function?.arguments?.length }); } } let jsonValid; if (content.trim().startsWith('{')) { try { JSON.parse(content); jsonValid = true; } catch { jsonValid = false; } } results.push({ id: r.id, project, requestId: r.requestId, phase: r.phase, createdAt: new Date(r.createdAt * 1000).toISOString(), model: r.model?.id, bytes: raw.length, terminal: r.terminal, responseId, finish, usage, contentBytes: content.length, reasoningBytes: reasoning.length, jsonValid, endHasEos: content.endsWith('<|eos|>'), toolCallChunks: calls.length, toolNames: [...new Set(calls.map(c => c.name).filter(Boolean))], contentTail: content.slice(-100).replace(/sk-[\w-]+/g, '[REDACTED]') }); } } } results.sort((a, b) => a.createdAt.localeCompare(b.createdAt)); const output = path.resolve(process.argv[3] || 'output/pi-compatibility-20261021/receipt-analysis.json'); await fs.mkdir(path.dirname(output), { recursive: true }); await fs.writeFile(output, JSON.stringify({ at: new Date().toISOString(), inferencePosts: 0, records: results }, null, 2)); for (const r of results) console.log(JSON.stringify(r));