feat(tools): add pdf extraction for minio ingestion
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import { promisify } from 'node:util';
|
||||
import { execFile } from 'node:child_process';
|
||||
import { extname } from 'node:path';
|
||||
import { mkdtempSync, rmSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import type { BackupConfig } from '../../config/schema.js';
|
||||
import type { MemoryStore } from '../../memory/store.js';
|
||||
import type { Tool, ToolResult } from '../types.js';
|
||||
@@ -12,7 +15,7 @@ type ExecRunner = (
|
||||
file: string,
|
||||
args: string[],
|
||||
options?: { env?: NodeJS.ProcessEnv; maxBuffer?: number },
|
||||
) => Promise<{ stdout: string; stderr: string }>;
|
||||
) => Promise<{ stdout: string | Buffer; stderr: string | Buffer }>;
|
||||
|
||||
const TEXT_EXTENSIONS = new Set([
|
||||
'.txt',
|
||||
@@ -29,6 +32,9 @@ const TEXT_EXTENSIONS = new Set([
|
||||
'.html',
|
||||
'.htm',
|
||||
]);
|
||||
const EXTRACTABLE_BINARY_EXTENSIONS = new Set([
|
||||
'.pdf',
|
||||
]);
|
||||
|
||||
export interface MinioIngestDeps {
|
||||
execRunner?: ExecRunner;
|
||||
@@ -42,12 +48,40 @@ function isLikelyText(content: string): boolean {
|
||||
function isLikelyTextObject(objectKey: string): boolean {
|
||||
const ext = extname(objectKey).toLowerCase();
|
||||
if (!ext) {return true;}
|
||||
return TEXT_EXTENSIONS.has(ext);
|
||||
return TEXT_EXTENSIONS.has(ext) || EXTRACTABLE_BINARY_EXTENSIONS.has(ext);
|
||||
}
|
||||
|
||||
function isExtractableBinaryObject(objectKey: string): boolean {
|
||||
return EXTRACTABLE_BINARY_EXTENSIONS.has(extname(objectKey).toLowerCase());
|
||||
}
|
||||
|
||||
async function readObjectText(
|
||||
runner: ExecRunner,
|
||||
remotePath: string,
|
||||
objectKey: string,
|
||||
env: NodeJS.ProcessEnv,
|
||||
): Promise<string> {
|
||||
if (!isExtractableBinaryObject(objectKey)) {
|
||||
const { stdout } = await runner('mc', ['cat', remotePath], { env, maxBuffer: 20 * 1024 * 1024 });
|
||||
return toText(stdout);
|
||||
}
|
||||
|
||||
const tempDir = mkdtempSync(join(tmpdir(), 'flynn-minio-ingest-'));
|
||||
const localPath = join(tempDir, 'object.bin');
|
||||
try {
|
||||
await runner('mc', ['cp', remotePath, localPath], { env, maxBuffer: 20 * 1024 * 1024 });
|
||||
const { stdout } = await runner('pdftotext', ['-q', localPath, '-'], { maxBuffer: 20 * 1024 * 1024 });
|
||||
return toText(stdout);
|
||||
} finally {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
export const minioIngestInternals = {
|
||||
isLikelyText,
|
||||
isLikelyTextObject,
|
||||
isExtractableBinaryObject,
|
||||
readObjectText,
|
||||
};
|
||||
|
||||
interface MinioIngestArgs {
|
||||
@@ -139,10 +173,9 @@ export function createMinioIngestTool(config: BackupConfig, store: MemoryStore,
|
||||
const remotePath = `${alias}/${bucket}/${objectKey}`;
|
||||
|
||||
try {
|
||||
const { stdout } = await runner('mc', ['cat', remotePath], { env, maxBuffer: 20 * 1024 * 1024 });
|
||||
const text = typeof stdout === 'string' ? stdout : stdout.toString('utf-8');
|
||||
const text = await readObjectText(runner, remotePath, objectKey, env);
|
||||
|
||||
if (!force && !isLikelyText(text)) {
|
||||
if (!force && !isExtractableBinaryObject(objectKey) && !isLikelyText(text)) {
|
||||
return {
|
||||
success: false,
|
||||
output: '',
|
||||
@@ -180,3 +213,6 @@ export function createMinioIngestTool(config: BackupConfig, store: MemoryStore,
|
||||
},
|
||||
};
|
||||
}
|
||||
function toText(value: string | Buffer): string {
|
||||
return typeof value === 'string' ? value : value.toString('utf-8');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user