import assert from 'node:assert/strict'; import { createHash } from 'node:crypto'; export const hash = bytes => createHash('sha256').update(bytes).digest('hex'); export function historicalAttempts(count = 1107) { return Array.from({ length: count }, (_, index) => ({ id: 'owned-history-' + index, connectionId: index % 2 ? 'owned-beta' : 'owned-alpha', model: 'owned-history-model-' + (index % 3), protocol: 'chat-completions', runId: 'owned-run-' + index, taskId: 'owned-task-' + Math.floor(index / 3), sessionId: 'owned-session-' + Math.floor(index / 10), projectId: 'owned-project', ordinal: 1, status: index % 7 ? 'completed' : 'failed', startedAt: '2026-11-01T00:00:00Z', finishedAt: '2026-11-01T00:00:01Z', ...(index % 7 ? {} : { reason: '历史上游原错误 HTTP 400 · 未自动重发' }), usageStatus: index % 5 ? 'reported' : 'unknown', usage: index % 5 ? { prompt_tokens: index, completion_tokens: 10, total_tokens: index + 10, protocol: 'chat-completions', raw: { prompt_tokens: index, completion_tokens: 10, total_tokens: index + 10, prompt_cache_hit_tokens: 0, custom_old: ['原字段', index] } } : null })); } export function inspectExport(bytes) { const lines = Buffer.isBuffer(bytes) ? bytes.toString('utf8').split('\n') : bytes.split('\n'); assert.equal(lines.pop(), ''); const values = lines.map(line => JSON.parse(line)), header = values.shift(), footer = values.pop(); assert.equal(header.format, 'orven-model-attempts-ndjson'); assert.equal(header.type, 'header'); assert.equal(footer.type, 'footer'); assert.equal(footer.complete, true); assert.equal(header.expectedRecords, values.length); assert.equal(footer.records, values.length); assert.equal(footer.recordLinesSha256, hash(lines.slice(1, -1).map(line => line + '\n').join(''))); for (const value of values) { assert.equal(value.type, 'attempt'); assert.equal(hash(JSON.stringify(value.item)), value.reference.payloadSha256); assert.equal(hash(JSON.stringify(value.initial.item)), value.reference.initialSha256); assert.equal(value.initial.sha256, value.reference.initialSha256); } return { header, footer, records: values }; }