import { OkraClient } from 'okrapdf';
const client = new OkraClient({ apiKey: process.env.OKRA_API_KEY });
const docId = 'doc-abc123';
// 1. Ask the original — gets wrong answer from textlayer
const baseline = await client.generate(docId,
'What was the effective tax rate in FY2022 vs FY2021?'
);
console.log(baseline.answer); // "(0.6)% and 14.7%" — wrong
// 2. Branch (zero-copy fork, ~2s)
const branch = await client.request('/v1/documents/' + docId + '/branch', {
method: 'POST',
});
const branchId = branch.id;
// 3. Ingest corrected table data on the branch
await client.request('/document/' + branchId + '/ingest', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
vendor: 'canonical',
mode: 'replace', // supersedes existing nodes on affected pages
data: {
pages: [{
pageNumber: 77,
blocks: [{
type: 'table',
label: 'Tax Reconciliation',
value: 'Income tax expense/(benefit) | $31 | (0.6)% | ($743) | 14.8%',
children: [
{ type: 'row', children: [
{ type: 'cell', value: 'Income tax expense/(benefit)' },
{ type: 'cell', value: '$31' },
{ type: 'cell', value: '(0.6)%' },
{ type: 'cell', value: '($743)' },
{ type: 'cell', value: '14.8%' },
]}
]
}]
}]
}
}),
});
// 4. Wait for processing
await client.wait(branchId);
// 5. Re-query — gets correct answer
const improved = await client.generate(branchId,
'What was the effective tax rate in FY2022 vs FY2021?'
);
console.log(improved.answer); // "(0.6)% and 14.8%" — correct