Compare commits
3 Commits
45a6f3d374
...
fix/sse-ev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db4d5e4d00 | ||
| 358fcaeff5 | |||
|
|
67fed57118 |
@@ -32,15 +32,6 @@ function sseWrite(res: import('express').Response, data: Record<string, unknown>
|
|||||||
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
res.write(`data: ${JSON.stringify(data)}\n\n`);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Send a 2 KB comment block to push past any proxy buffering threshold.
|
|
||||||
* Nginx and other reverse proxies often buffer the first few KB before
|
|
||||||
* starting to stream — this padding forces the initial flush.
|
|
||||||
*/
|
|
||||||
function ssePadding(res: import('express').Response) {
|
|
||||||
res.write(`: ${' '.repeat(2048)}\n\n`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.post(
|
router.post(
|
||||||
@@ -71,7 +62,6 @@ router.post(
|
|||||||
res.setHeader('X-Accel-Buffering', 'no');
|
res.setHeader('X-Accel-Buffering', 'no');
|
||||||
res.socket?.setNoDelay(true);
|
res.socket?.setNoDelay(true);
|
||||||
res.flushHeaders();
|
res.flushHeaders();
|
||||||
ssePadding(res);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const converted = await convertPdfToStatementStreaming(
|
const converted = await convertPdfToStatementStreaming(
|
||||||
|
|||||||
@@ -150,6 +150,10 @@ const LLM_PROGRESS_MAX = 98;
|
|||||||
const LLM_PROGRESS_RANGE = LLM_PROGRESS_MAX - LLM_PROGRESS_MIN;
|
const LLM_PROGRESS_RANGE = LLM_PROGRESS_MAX - LLM_PROGRESS_MIN;
|
||||||
const THROTTLE_MS = 300;
|
const THROTTLE_MS = 300;
|
||||||
|
|
||||||
|
function yieldToEventLoop(): Promise<void> {
|
||||||
|
return new Promise(resolve => setImmediate(resolve));
|
||||||
|
}
|
||||||
|
|
||||||
export async function convertPdfToStatementStreaming(
|
export async function convertPdfToStatementStreaming(
|
||||||
buffer: Buffer,
|
buffer: Buffer,
|
||||||
onProgress: OnProgress,
|
onProgress: OnProgress,
|
||||||
@@ -163,6 +167,7 @@ export async function convertPdfToStatementStreaming(
|
|||||||
}
|
}
|
||||||
|
|
||||||
onProgress('pdf', 2, 'Извлечение текста из PDF...');
|
onProgress('pdf', 2, 'Извлечение текста из PDF...');
|
||||||
|
await yieldToEventLoop();
|
||||||
|
|
||||||
let text: string;
|
let text: string;
|
||||||
try {
|
try {
|
||||||
@@ -186,6 +191,7 @@ export async function convertPdfToStatementStreaming(
|
|||||||
}
|
}
|
||||||
|
|
||||||
onProgress('pdf', 8, 'Текст извлечён, отправка в LLM...');
|
onProgress('pdf', 8, 'Текст извлечён, отправка в LLM...');
|
||||||
|
await yieldToEventLoop();
|
||||||
|
|
||||||
const openai = new OpenAI({
|
const openai = new OpenAI({
|
||||||
apiKey: config.llmApiKey,
|
apiKey: config.llmApiKey,
|
||||||
@@ -205,7 +211,6 @@ export async function convertPdfToStatementStreaming(
|
|||||||
stream: true,
|
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));
|
const expectedChars = Math.max(2_000, Math.min(text.length * 2, 30_000));
|
||||||
|
|
||||||
let accumulated = '';
|
let accumulated = '';
|
||||||
@@ -227,11 +232,14 @@ export async function convertPdfToStatementStreaming(
|
|||||||
);
|
);
|
||||||
onProgress('llm', llmProgress, 'Конвертация через LLM...');
|
onProgress('llm', llmProgress, 'Конвертация через LLM...');
|
||||||
lastEmitTime = now;
|
lastEmitTime = now;
|
||||||
|
// Let the event loop flush socket writes to the network
|
||||||
|
await yieldToEventLoop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onProgress('llm', LLM_PROGRESS_MAX, 'LLM завершил, обработка результата...');
|
onProgress('llm', LLM_PROGRESS_MAX, 'LLM завершил, обработка результата...');
|
||||||
|
await yieldToEventLoop();
|
||||||
|
|
||||||
const content = accumulated.trim();
|
const content = accumulated.trim();
|
||||||
if (!content) {
|
if (!content) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ server {
|
|||||||
proxy_connect_timeout 5s;
|
proxy_connect_timeout 5s;
|
||||||
proxy_read_timeout 600s;
|
proxy_read_timeout 600s;
|
||||||
proxy_buffering off;
|
proxy_buffering off;
|
||||||
|
gzip off;
|
||||||
client_max_body_size 15m;
|
client_max_body_size 15m;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user