Compare commits
3 Commits
feature/pd
...
22be09c101
| Author | SHA1 | Date | |
|---|---|---|---|
| 22be09c101 | |||
|
|
78c4730196 | ||
| f2d0c91488 |
@@ -145,7 +145,10 @@ export async function convertPdfToStatement(
|
||||
export type ProgressStage = 'pdf' | 'llm' | 'import';
|
||||
export type OnProgress = (stage: ProgressStage, progress: number, message: string) => void;
|
||||
|
||||
const EXPECTED_CHARS = 15_000;
|
||||
const LLM_PROGRESS_MIN = 10;
|
||||
const LLM_PROGRESS_MAX = 85;
|
||||
const LLM_PROGRESS_RANGE = LLM_PROGRESS_MAX - LLM_PROGRESS_MIN;
|
||||
const THROTTLE_MS = 300;
|
||||
|
||||
export async function convertPdfToStatementStreaming(
|
||||
buffer: Buffer,
|
||||
@@ -202,22 +205,34 @@ export async function convertPdfToStatementStreaming(
|
||||
stream: true,
|
||||
});
|
||||
|
||||
// Estimate expected output size as ~2x the input PDF text length, clamped
|
||||
const expectedChars = Math.max(2_000, Math.min(text.length * 2, 30_000));
|
||||
|
||||
let accumulated = '';
|
||||
let charsReceived = 0;
|
||||
let lastEmitTime = 0;
|
||||
|
||||
for await (const chunk of stream) {
|
||||
const delta = chunk.choices[0]?.delta?.content;
|
||||
if (delta) {
|
||||
accumulated += delta;
|
||||
charsReceived += delta.length;
|
||||
const llmProgress = Math.min(
|
||||
85,
|
||||
Math.round((charsReceived / EXPECTED_CHARS) * 75 + 10),
|
||||
);
|
||||
onProgress('llm', llmProgress, 'Конвертация через LLM...');
|
||||
|
||||
const now = Date.now();
|
||||
if (now - lastEmitTime >= THROTTLE_MS) {
|
||||
const ratio = Math.min(1, charsReceived / expectedChars);
|
||||
const llmProgress = Math.min(
|
||||
LLM_PROGRESS_MAX,
|
||||
Math.round(ratio * LLM_PROGRESS_RANGE + LLM_PROGRESS_MIN),
|
||||
);
|
||||
onProgress('llm', llmProgress, 'Конвертация через LLM...');
|
||||
lastEmitTime = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onProgress('llm', LLM_PROGRESS_MAX, 'LLM завершил, обработка результата...');
|
||||
|
||||
const content = accumulated.trim();
|
||||
if (!content) {
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user