3307d09b79
Add dependency validation, batching, and progress feedback to OCR pipeline Improve error handling and cross-platform compatibility Refactor GLM-OCR client to use file-based requests with auth Remove base64 dependency and use native Node.js file operations Optimize notebook detection and page file discovery Cache sync tracker data to reduce disk I/O Update default settings to use localhost endpoints Fix manifest.json and increase build size to 17 KB
54 lines
1.5 KiB
TypeScript
54 lines
1.5 KiB
TypeScript
import { readFile, writeFile, unlink } from "fs/promises";
|
|
import { join } from "path";
|
|
import { tmpdir } from "os";
|
|
import { GlmOcrResponse } from "../types";
|
|
import RemarkablePlugin from "../main";
|
|
import { runCommand } from "../utils/process";
|
|
|
|
export class GlmOcrClient {
|
|
plugin: RemarkablePlugin;
|
|
|
|
constructor(plugin: RemarkablePlugin) {
|
|
this.plugin = plugin;
|
|
}
|
|
|
|
async parseImages(pngPaths: string[]): Promise<GlmOcrResponse> {
|
|
const url = `${this.plugin.settings.glmocrServerUrl}/glmocr/parse`;
|
|
const apiKey = this.plugin.settings.glmocrApiKey;
|
|
|
|
const images: string[] = [];
|
|
for (const pngPath of pngPaths) {
|
|
const data = await readFile(pngPath);
|
|
images.push(`data:image/png;base64,${data.toString("base64")}`);
|
|
}
|
|
|
|
const bodyPath = join(tmpdir(), `glmocr-body-${Date.now()}.json`);
|
|
await writeFile(bodyPath, JSON.stringify({ images }));
|
|
|
|
try {
|
|
const args = [
|
|
"-s",
|
|
"-X", "POST",
|
|
"-H", "Content-Type: application/json",
|
|
"-H", `Authorization: Bearer ${apiKey}`,
|
|
"--max-time", "60",
|
|
"--data-binary", `@${bodyPath}`,
|
|
url,
|
|
];
|
|
|
|
const { stdout, stderr, code } = await runCommand("curl", args);
|
|
if (code !== 0) {
|
|
throw new Error(`GLM-OCR request failed: ${stderr || stdout}`);
|
|
}
|
|
|
|
try {
|
|
return JSON.parse(stdout) as GlmOcrResponse;
|
|
} catch (e) {
|
|
throw new Error(`Failed to parse GLM-OCR response: ${e}\nRaw: ${stdout}`);
|
|
}
|
|
} finally {
|
|
await unlink(bodyPath).catch(() => {});
|
|
}
|
|
}
|
|
}
|