Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Run `veryfront <command> --help` for options:
| `studio` | Open Studio UI |

Use one or more `uploads/...` paths for remote project-upload references in `veryfront knowledge ingest`; use `./uploads/...` or `/workspace/uploads/...` to force a local sandbox path.
`veryfront knowledge ingest` parses PDF, Office, EPUB, HTML, and RTF sources through the built-in Kreuzberg document extension. Plain text, Markdown, JSON, CSV, TSV, and common code files are converted directly by the CLI.
`veryfront knowledge ingest` parses PDF, Office, EPUB, HTML, and RTF sources through the optional `@veryfront/ext-document-kreuzberg` package. The `veryfront` npm package does not include it. Install it next to veryfront with `npm install @veryfront/ext-document-kreuzberg`, or run `npx -p veryfront -p @veryfront/ext-document-kreuzberg veryfront knowledge ingest ...`. Plain text, Markdown, JSON, CSV, TSV, and common code files are converted directly by the CLI.

## Adding a New Command

Expand Down
2 changes: 1 addition & 1 deletion cli/commands/knowledge/command-help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const knowledgeHelp: CommandHelp = {
"Primary subcommand: ingest",
"`uploads/...` means a remote project upload; use `./uploads/...` or `/workspace/uploads/...` to force a local file",
"`ingest` orchestrates upload resolution, parsing, and project file writes",
"Supported PDF, Office, and HTML sources are parsed through the built-in Kreuzberg document extension",
"PDF, Office, EPUB, HTML, and RTF sources need the optional Kreuzberg package. Install it next to veryfront: npm install @veryfront/ext-document-kreuzberg (with npx: npx -p veryfront -p @veryfront/ext-document-kreuzberg veryfront knowledge ingest ...)",
Comment thread
kojiwakayama marked this conversation as resolved.
Resolved
"Plain text, Markdown, JSON, CSV, TSV, and common code files are converted directly by the CLI",
],
};
54 changes: 54 additions & 0 deletions cli/commands/knowledge/parser-install-hint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {
assertEquals,
assertRejects,
assertStrictEquals,
assertStringIncludes,
} from "#veryfront/testing/assert.ts";
import { describe, it } from "#veryfront/testing/bdd.ts";
import { type LoadDocumentExtractor, loadDocumentExtractorWithInstallHint } from "./parser.ts";

const INSTALL = "npm install @veryfront/ext-document-kreuzberg";
const NPX = "npx -p veryfront -p @veryfront/ext-document-kreuzberg veryfront knowledge ingest";

function missingPackage(specifier: string): Error {
const error = new Error(
`Cannot find package '${specifier}' imported from /app/node_modules/veryfront/esm/cli.js`,
);
(error as Error & { code: string }).code = "ERR_MODULE_NOT_FOUND";
return error;
}

function failingLoad(error: Error): LoadDocumentExtractor {
return () => Promise.reject(error);
}

describe("loadDocumentExtractorWithInstallHint", () => {
it("returns the loaded extension module", async () => {
const module = { KreuzbergDocumentExtractor: class {} } as unknown as Awaited<
ReturnType<LoadDocumentExtractor>
>;
assertStrictEquals(
await loadDocumentExtractorWithInstallHint(() => Promise.resolve(module)),
module,
);
});

it("names the install commands and keeps the cause when the package is missing", async () => {
const missing = missingPackage("@veryfront/ext-document-kreuzberg");
const error = await assertRejects(() =>
loadDocumentExtractorWithInstallHint(failingLoad(missing))
);
assertStringIncludes((error as Error).message, INSTALL);
assertStringIncludes((error as Error).message, NPX);
assertStrictEquals((error as Error).cause, missing);
});

it("passes a broken dependency inside the installed extension through unchanged", async () => {
const broken = missingPackage("jose");
const error = await assertRejects(() =>
loadDocumentExtractorWithInstallHint(failingLoad(broken))
);
assertStrictEquals(error, broken);
assertEquals((error as Error).message.includes(INSTALL), false);
});
});
50 changes: 42 additions & 8 deletions cli/commands/knowledge/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
relative,
resolve,
} from "veryfront/platform/path";
import { importFirstPartyExtensionModule } from "veryfront/extensions/first-party-import";
import {
importFirstPartyExtensionModule,
isMissingFirstPartyExtensionModule,
} from "veryfront/extensions/first-party-import";
import type {
DocumentExtractionOptions,
DocumentExtractionProgress,
Expand Down Expand Up @@ -101,10 +104,14 @@ type DocumentKreuzbergExtensionModule = {
};
};

export type LoadDocumentExtractor = () => Promise<DocumentKreuzbergExtensionModule>;

export interface RunKnowledgeParsersDeps {
allocationSession?: KnowledgeAllocationSession;
signal?: AbortSignal;
extractDocumentText?: ExtractDocumentText;
/** Loads the optional Kreuzberg package used by the default document extractor. */
loadDocumentExtractor?: LoadDocumentExtractor;
onProgress?: (event: DocumentExtractionProgressEvent) => void | Promise<void>;
}

Expand Down Expand Up @@ -2818,23 +2825,45 @@ function buildSummary(sourceType: string, stats: Record<string, unknown>): strin
return `Converted document to markdown (${stats.characters ?? 0} chars).`;
}

const KREUZBERG_PACKAGE = "@veryfront/ext-document-kreuzberg";

function loadKreuzbergExtension(): Promise<DocumentKreuzbergExtensionModule> {
return importFirstPartyExtensionModule<DocumentKreuzbergExtensionModule>(
"ext-document-kreuzberg",
KREUZBERG_PACKAGE,
);
}

/** Loads the document extractor, naming the install commands when the optional package is missing. */
export async function loadDocumentExtractorWithInstallHint(
load: LoadDocumentExtractor,
): Promise<DocumentKreuzbergExtensionModule> {
try {
return await load();
} catch (error) {
if (!isMissingFirstPartyExtensionModule(error, [KREUZBERG_PACKAGE])) throw error;
throw new Error(
`PDF, Office, EPUB, HTML and RTF parsing needs the optional package ${KREUZBERG_PACKAGE}. ` +
`Install it next to veryfront: npm install ${KREUZBERG_PACKAGE} ` +
Comment thread
kojiwakayama marked this conversation as resolved.
Resolved
Comment thread
kojiwakayama marked this conversation as resolved.
Resolved
`(with npx: npx -p veryfront -p ${KREUZBERG_PACKAGE} veryfront knowledge ingest ...).`,
{ cause: error },
);
}
}

async function defaultExtractDocumentText(
input: {
filePath: string;
mimeType: string;
onProgress?: DocumentExtractionProgress;
signal?: AbortSignal;
},
load: LoadDocumentExtractor,
): Promise<string> {
input.signal?.throwIfAborted();
const bytes = await Deno.readFile(input.filePath);
input.signal?.throwIfAborted();
const { KreuzbergDocumentExtractor } = await importFirstPartyExtensionModule<
DocumentKreuzbergExtensionModule
>(
"ext-document-kreuzberg",
"@veryfront/ext-document-kreuzberg",
);
const { KreuzbergDocumentExtractor } = await loadDocumentExtractorWithInstallHint(load);
input.signal?.throwIfAborted();
const extractor = new KreuzbergDocumentExtractor();
return await extractor.extractInWorker(
Expand Down Expand Up @@ -2871,7 +2900,12 @@ export async function runKnowledgeParsers(input: {
}

const parserDeps: ResolvedRunKnowledgeParsersDeps = {
extractDocumentText: deps.extractDocumentText ?? defaultExtractDocumentText,
extractDocumentText: deps.extractDocumentText ??
((extractInput) =>
defaultExtractDocumentText(
extractInput,
deps.loadDocumentExtractor ?? loadKreuzbergExtension,
)),
onProgress: deps.onProgress,
signal: deps.signal,
};
Expand Down
2 changes: 1 addition & 1 deletion cli/help/command-help.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("command-help", () => {
assertStringIncludes(output, "veryfront knowledge ingest <source...> [options]");
assertStringIncludes(output, "--branch, -b <name-or-id>");
assertStringIncludes(output, "Primary subcommand: ingest");
assertStringIncludes(output, "built-in Kreuzberg document extension");
assertStringIncludes(output, "npm install @veryfront/ext-document-kreuzberg");
});

it("documents push divergence and the explicit force escape hatch", () => {
Expand Down
23 changes: 19 additions & 4 deletions docs/guides/cli-knowledge-ingestion.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,21 @@ veryfront login
```

`veryfront knowledge ingest` parses PDF, Office, EPUB, HTML, and RTF sources
through the built-in Kreuzberg document extension. Plain text, Markdown, JSON,
CSV, TSV, and common code files are converted directly by the CLI.
through the optional `@veryfront/ext-document-kreuzberg` package. The
`veryfront` npm package does not include it. Install it next to veryfront:

```bash
npm install veryfront @veryfront/ext-document-kreuzberg
```

With npx, add both packages:

```bash
npx -p veryfront -p @veryfront/ext-document-kreuzberg veryfront knowledge ingest ./book.pdf --json
```

Plain text, Markdown, JSON, CSV, TSV, and common code files are converted
directly by the CLI and need no extra package.

## Import an OKF bundle

Expand Down Expand Up @@ -353,8 +366,10 @@ veryfront knowledge ingest uploads/contracts/q1.pdf --project my-project --json
### Document extraction errors

Use a supported document type and ensure the source file is readable. Rich
document formats use the built-in Kreuzberg extension, while text-like formats
are converted directly by the CLI.
document formats need the optional `@veryfront/ext-document-kreuzberg` package.
If the error says that package is missing, install it with
`npm install @veryfront/ext-document-kreuzberg`. Text-like formats are converted
directly by the CLI.

## Verify it worked

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertEquals } from "#veryfront/testing/assert.ts";
import { assertEquals, assertStringIncludes } from "#veryfront/testing/assert.ts";
import { describe, it } from "#veryfront/testing/bdd.ts";
import { makeTempDir } from "#veryfront/testing/deno-compat.ts";
import { runKnowledgeParser } from "../../../../../../cli/commands/knowledge/parser.ts";
Expand Down Expand Up @@ -36,3 +36,58 @@ describe("knowledge parser cancellation", () => {
}
});
});

describe("knowledge parser document extension install hint", () => {
const INSTALL = "npm install @veryfront/ext-document-kreuzberg";
const NPX = "npx -p veryfront -p @veryfront/ext-document-kreuzberg veryfront knowledge ingest";

function missingPackage(specifier: string): Error {
const error = new Error(
`Cannot find package '${specifier}' imported from /app/node_modules/veryfront/esm/cli.js`,
);
(error as Error & { code: string }).code = "ERR_MODULE_NOT_FOUND";
return error;
}

async function parsePdfWith(loadError: Error): Promise<Error> {
const directory = await makeTempDir();
const filePath = `${directory}/book.pdf`;
await Deno.writeTextFile(filePath, "%PDF-1.4");
try {
await runKnowledgeParser({ filePath, outputDir: `${directory}/output` }, {
loadDocumentExtractor: () => Promise.reject(loadError),
});
} catch (error) {
return error as Error;
} finally {
await Deno.remove(directory, { recursive: true });
}
throw new Error("expected the parser to fail");
}

it("names the install commands when the Kreuzberg package is missing", async () => {
const missing = missingPackage("@veryfront/ext-document-kreuzberg");
const error = await parsePdfWith(missing);
assertStringIncludes(error.message, INSTALL);
assertStringIncludes(error.message, NPX);
});

it("names the install commands for the wrapped first-party import error", async () => {
const missing = missingPackage("@veryfront/ext-document-kreuzberg");
const wrapped = new Error(
`${missing.message} First-party extension "ext-document-kreuzberg" is not installed; install @veryfront/ext-document-kreuzberg alongside veryfront to enable it.`,
{ cause: new AggregateError([missing], missing.message) },
);
const error = await parsePdfWith(wrapped);
assertStringIncludes(error.message, INSTALL);
assertStringIncludes(error.message, NPX);
});

it("passes a broken dependency inside the installed extension through unchanged", async () => {
const error = await parsePdfWith(missingPackage("jose"));
assertEquals(
error.message,
"knowledge ingest parser failed: Cannot find package 'jose' imported from /app/node_modules/veryfront/esm/cli.js",
);
});
});
Loading