Skip to content

Commit 2069d8e

Browse files
fix(cli): name the Kreuzberg install command when knowledge ingest cannot load it (#5684)
* fix(cli): name the Kreuzberg install command when knowledge ingest cannot load it PDF, Office, EPUB, HTML and RTF parsing needs the optional @veryfront/ext-document-kreuzberg package. The npm package does not include it, and the error only said to install it alongside veryfront. The parser now names the npm install and npx commands, and the help and docs no longer call the parser built-in. * test(cli): cover the Kreuzberg install hint with a hermetic unit test * fix(cli): pin the Kreuzberg hint to the running version and the project's package manager * test(cli): cover the default package-manager formatter in the Kreuzberg hint * fix(cli): base the Kreuzberg hint on --project-dir and cover global and npx installs * fix(cli): keep the Kreuzberg hint to the project command and point other installs to the guide --------- Co-authored-by: Koji Wakayama <koji@codersociety.com>
1 parent c66d52a commit 2069d8e

8 files changed

Lines changed: 248 additions & 16 deletions

File tree

‎cli/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Run `veryfront <command> --help` for options:
7777
| `studio` | Open Studio UI |
7878

7979
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.
80-
`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.
80+
`veryfront knowledge ingest` parses PDF, Office, EPUB, HTML, and RTF sources through the Kreuzberg document extension. The standalone binary includes it. The `veryfront` npm package does not: install `@veryfront/ext-document-kreuzberg` at the same version as `veryfront` with your project's package manager, or the same way as a global `veryfront`. For npx, see [CLI knowledge ingestion](../docs/guides/cli-knowledge-ingestion.md). Plain text, Markdown, JSON, CSV, TSV, and common code files are converted directly by the CLI.
8181

8282
## Adding a New Command
8383

‎cli/commands/knowledge/command-help.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const knowledgeHelp: CommandHelp = {
5353
"Primary subcommand: ingest",
5454
"`uploads/...` means a remote project upload; use `./uploads/...` or `/workspace/uploads/...` to force a local file",
5555
"`ingest` orchestrates upload resolution, parsing, and project file writes",
56-
"Supported PDF, Office, and HTML sources are parsed through the built-in Kreuzberg document extension",
56+
"PDF, Office, EPUB, HTML, and RTF sources are parsed by the Kreuzberg document extension. The standalone binary includes it; with the veryfront npm package, also install @veryfront/ext-document-kreuzberg at the same version",
5757
"Plain text, Markdown, JSON, CSV, TSV, and common code files are converted directly by the CLI",
5858
],
5959
};

‎cli/commands/knowledge/command.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ export async function ingestResolvedSources(
906906
...(okfCompanionPaths.has(okfRelativePaths.get(source) ?? "")
907907
? { okfRole: "companion" as const }
908908
: {}),
909-
}, { signal: deps.signal, allocationSession }),
909+
}, { signal: deps.signal, allocationSession, projectDir: options.projectDir }),
910910
);
911911
}
912912
}
@@ -975,6 +975,7 @@ export async function ingestResolvedSources(
975975
const parserDeps = {
976976
allocationSession,
977977
signal: deps.signal,
978+
projectDir: options.projectDir,
978979
...(eventLogger
979980
? {
980981
onProgress: (event: DocumentExtractionProgressEvent) => {
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import {
2+
assertEquals,
3+
assertRejects,
4+
assertStrictEquals,
5+
assertStringIncludes,
6+
} from "#veryfront/testing/assert.ts";
7+
import { describe, it } from "#veryfront/testing/bdd.ts";
8+
import { VERSION } from "#cli/utils";
9+
import {
10+
type LoadDocumentExtractor,
11+
loadDocumentExtractorWithInstallHint,
12+
projectInstallFormatter,
13+
} from "./parser.ts";
14+
15+
const PACKAGE = "@veryfront/ext-document-kreuzberg";
16+
const PINNED = `${PACKAGE}@${VERSION}`;
17+
const GUIDE = "see the knowledge ingestion guide";
18+
19+
function missingPackage(specifier: string): Error {
20+
const error = new Error(
21+
`Cannot find package '${specifier}' imported from /app/node_modules/veryfront/esm/cli.js`,
22+
);
23+
(error as Error & { code: string }).code = "ERR_MODULE_NOT_FOUND";
24+
return error;
25+
}
26+
27+
function failingLoad(error: Error): LoadDocumentExtractor {
28+
return () => Promise.reject(error);
29+
}
30+
31+
describe("loadDocumentExtractorWithInstallHint", () => {
32+
it("returns the loaded extension module", async () => {
33+
const module = { KreuzbergDocumentExtractor: class {} } as unknown as Awaited<
34+
ReturnType<LoadDocumentExtractor>
35+
>;
36+
assertStrictEquals(
37+
await loadDocumentExtractorWithInstallHint(() => Promise.resolve(module)),
38+
module,
39+
);
40+
});
41+
42+
it("names the project's install command pinned to the running version", async () => {
43+
const missing = missingPackage(PACKAGE);
44+
const requested: string[] = [];
45+
const error = await assertRejects(() =>
46+
loadDocumentExtractorWithInstallHint(failingLoad(missing), (spec) => {
47+
requested.push(spec);
48+
return `pnpm add ${spec}`;
49+
})
50+
);
51+
assertEquals(requested, [PINNED]);
52+
assertStringIncludes((error as Error).message, `pnpm add ${PINNED}`);
53+
assertStringIncludes((error as Error).message, GUIDE);
54+
assertStrictEquals((error as Error).cause, missing);
55+
});
56+
57+
it("formats the pinned spec with the project's package manager by default", async () => {
58+
const error = await assertRejects(() =>
59+
loadDocumentExtractorWithInstallHint(failingLoad(missingPackage(PACKAGE)))
60+
);
61+
const { formatInstallCommand } = await import("veryfront/extensions");
62+
assertStringIncludes((error as Error).message, formatInstallCommand(PINNED));
63+
});
64+
65+
it("uses the runtime's client for a project directory without a manifest", async () => {
66+
const { formatInstallCommand, runtimeInstallTarget } = await import("veryfront/extensions");
67+
assertEquals(
68+
await projectInstallFormatter("/nonexistent/veryfront-project")(PINNED),
69+
formatInstallCommand(PINNED, runtimeInstallTarget()),
70+
);
71+
});
72+
73+
it("passes a broken dependency inside the installed extension through unchanged", async () => {
74+
const broken = missingPackage("jose");
75+
const error = await assertRejects(() =>
76+
loadDocumentExtractorWithInstallHint(failingLoad(broken), () => {
77+
throw new Error("must not format an install command");
78+
})
79+
);
80+
assertStrictEquals(error, broken);
81+
});
82+
});

‎cli/commands/knowledge/parser.ts‎

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import {
2323
relative,
2424
resolve,
2525
} from "veryfront/platform/path";
26-
import { importFirstPartyExtensionModule } from "veryfront/extensions/first-party-import";
26+
import {
27+
importFirstPartyExtensionModule,
28+
isMissingFirstPartyExtensionModule,
29+
} from "veryfront/extensions/first-party-import";
2730
import type {
2831
DocumentExtractionOptions,
2932
DocumentExtractionProgress,
@@ -101,10 +104,16 @@ type DocumentKreuzbergExtensionModule = {
101104
};
102105
};
103106

107+
export type LoadDocumentExtractor = () => Promise<DocumentKreuzbergExtensionModule>;
108+
104109
export interface RunKnowledgeParsersDeps {
105110
allocationSession?: KnowledgeAllocationSession;
106111
signal?: AbortSignal;
107112
extractDocumentText?: ExtractDocumentText;
113+
/** Loads the optional Kreuzberg package used by the default document extractor. */
114+
loadDocumentExtractor?: LoadDocumentExtractor;
115+
/** Project whose package manager the missing-extension hint should use (`--project-dir`). */
116+
projectDir?: string;
108117
onProgress?: (event: DocumentExtractionProgressEvent) => void | Promise<void>;
109118
}
110119

@@ -2818,22 +2827,72 @@ function buildSummary(sourceType: string, stats: Record<string, unknown>): strin
28182827
return `Converted document to markdown (${stats.characters ?? 0} chars).`;
28192828
}
28202829

2830+
const KREUZBERG_PACKAGE = "@veryfront/ext-document-kreuzberg";
2831+
2832+
function loadKreuzbergExtension(): Promise<DocumentKreuzbergExtensionModule> {
2833+
return importFirstPartyExtensionModule<DocumentKreuzbergExtensionModule>(
2834+
"ext-document-kreuzberg",
2835+
KREUZBERG_PACKAGE,
2836+
);
2837+
}
2838+
2839+
/** Formats the install command for a package spec in the selected project. */
2840+
export type FormatPackageInstall = (packageSpec: string) => string | Promise<string>;
2841+
2842+
/** Uses the project's own package manager, so pnpm, Yarn, Bun and Deno projects keep one lockfile. */
2843+
export function projectInstallFormatter(projectDir?: string): FormatPackageInstall {
2844+
return async (packageSpec) => {
2845+
const { detectProjectInstallTarget, formatInstallCommand, runtimeInstallTarget } = await import(
2846+
"veryfront/extensions"
2847+
);
2848+
// An explicit --project-dir decides on its own; the cwd is unrelated to it.
2849+
return formatInstallCommand(
2850+
packageSpec,
2851+
projectDir === undefined
2852+
? undefined
2853+
: detectProjectInstallTarget(projectDir) ?? runtimeInstallTarget(),
2854+
);
2855+
};
2856+
}
2857+
2858+
/** Loads the document extractor, naming the install commands when the optional package is missing. */
2859+
export async function loadDocumentExtractorWithInstallHint(
2860+
load: LoadDocumentExtractor,
2861+
formatInstall: FormatPackageInstall = projectInstallFormatter(),
2862+
): Promise<DocumentKreuzbergExtensionModule> {
2863+
try {
2864+
return await load();
2865+
} catch (error) {
2866+
if (!isMissingFirstPartyExtensionModule(error, [KREUZBERG_PACKAGE])) throw error;
2867+
// Extensions peer-depend on the matching veryfront release, so pin the running version.
2868+
const packageSpec = `${KREUZBERG_PACKAGE}@${VERSION}`;
2869+
const install = await formatInstall(packageSpec);
2870+
throw new Error(
2871+
`PDF, Office, EPUB, HTML and RTF parsing needs the optional package ${packageSpec}, ` +
2872+
`installed in the same place as the veryfront you are running. ` +
2873+
`In a project that depends on veryfront, run: ${install}. ` +
2874+
`For global and npx installs, see the knowledge ingestion guide.`,
2875+
{ cause: error },
2876+
);
2877+
}
2878+
}
2879+
28212880
async function defaultExtractDocumentText(
28222881
input: {
28232882
filePath: string;
28242883
mimeType: string;
28252884
onProgress?: DocumentExtractionProgress;
28262885
signal?: AbortSignal;
28272886
},
2887+
load: LoadDocumentExtractor,
2888+
projectDir: string | undefined,
28282889
): Promise<string> {
28292890
input.signal?.throwIfAborted();
28302891
const bytes = await Deno.readFile(input.filePath);
28312892
input.signal?.throwIfAborted();
2832-
const { KreuzbergDocumentExtractor } = await importFirstPartyExtensionModule<
2833-
DocumentKreuzbergExtensionModule
2834-
>(
2835-
"ext-document-kreuzberg",
2836-
"@veryfront/ext-document-kreuzberg",
2893+
const { KreuzbergDocumentExtractor } = await loadDocumentExtractorWithInstallHint(
2894+
load,
2895+
projectInstallFormatter(projectDir),
28372896
);
28382897
input.signal?.throwIfAborted();
28392898
const extractor = new KreuzbergDocumentExtractor();
@@ -2871,7 +2930,13 @@ export async function runKnowledgeParsers(input: {
28712930
}
28722931

28732932
const parserDeps: ResolvedRunKnowledgeParsersDeps = {
2874-
extractDocumentText: deps.extractDocumentText ?? defaultExtractDocumentText,
2933+
extractDocumentText: deps.extractDocumentText ??
2934+
((extractInput) =>
2935+
defaultExtractDocumentText(
2936+
extractInput,
2937+
deps.loadDocumentExtractor ?? loadKreuzbergExtension,
2938+
deps.projectDir,
2939+
)),
28752940
onProgress: deps.onProgress,
28762941
signal: deps.signal,
28772942
};

‎cli/help/command-help.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ describe("command-help", () => {
4848
assertStringIncludes(output, "veryfront knowledge ingest <source...> [options]");
4949
assertStringIncludes(output, "--branch, -b <name-or-id>");
5050
assertStringIncludes(output, "Primary subcommand: ingest");
51-
assertStringIncludes(output, "built-in Kreuzberg document extension");
51+
assertStringIncludes(
52+
output,
53+
"with the veryfront npm package, also install @veryfront/ext-document-kreuzberg",
54+
);
5255
});
5356

5457
it("documents push divergence and the explicit force escape hatch", () => {

‎docs/guides/cli-knowledge-ingestion.md‎

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,30 @@ veryfront login
7676
```
7777

7878
`veryfront knowledge ingest` parses PDF, Office, EPUB, HTML, and RTF sources
79-
through the built-in Kreuzberg document extension. Plain text, Markdown, JSON,
80-
CSV, TSV, and common code files are converted directly by the CLI.
79+
through the Kreuzberg document extension. The standalone binary includes it.
80+
The `veryfront` npm package does not: install
81+
`@veryfront/ext-document-kreuzberg` at the same version as `veryfront`, with your
82+
project's package manager. For npm:
83+
84+
```bash
85+
npm install veryfront @veryfront/ext-document-kreuzberg
86+
```
87+
88+
For a `veryfront` installed globally with npm, install the extension the same way
89+
(with another package manager, use its global install command):
90+
91+
```bash
92+
npm install -g veryfront @veryfront/ext-document-kreuzberg
93+
```
94+
95+
With npx, outside a project that already installs `veryfront`, add both packages:
96+
97+
```bash
98+
npx -p veryfront -p @veryfront/ext-document-kreuzberg veryfront knowledge ingest ./book.pdf --json
99+
```
100+
101+
Plain text, Markdown, JSON, CSV, TSV, and common code files are converted
102+
directly by the CLI and need no extra package.
81103

82104
## Import an OKF bundle
83105

@@ -356,8 +378,10 @@ veryfront knowledge ingest uploads/contracts/q1.pdf --project my-project --json
356378
### Document extraction errors
357379

358380
Use a supported document type and ensure the source file is readable. Rich
359-
document formats use the built-in Kreuzberg extension, while text-like formats
360-
are converted directly by the CLI.
381+
document formats use the Kreuzberg document extension. If the error says
382+
`@veryfront/ext-document-kreuzberg` is missing, run the install command the
383+
error prints; it uses your project's package manager and the running
384+
`veryfront` version. Text-like formats are converted directly by the CLI.
361385

362386
## Verify it worked
363387

‎tests/integration/semantic-unit-boundary/cli/commands/knowledge/parser.test.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { assertEquals } from "#veryfront/testing/assert.ts";
1+
import { assertEquals, assertStringIncludes } from "#veryfront/testing/assert.ts";
22
import { describe, it } from "#veryfront/testing/bdd.ts";
33
import { makeTempDir } from "#veryfront/testing/deno-compat.ts";
4+
import { VERSION } from "#cli/utils";
45
import { runKnowledgeParser } from "../../../../../../cli/commands/knowledge/parser.ts";
56

67
describe("knowledge parser cancellation", () => {
@@ -36,3 +37,59 @@ describe("knowledge parser cancellation", () => {
3637
}
3738
});
3839
});
40+
41+
describe("knowledge parser document extension install hint", () => {
42+
// The install command follows the project's package manager; the spec is pinned either way.
43+
const INSTALL = `@veryfront/ext-document-kreuzberg@${VERSION}`;
44+
const NPX = "see the knowledge ingestion guide";
45+
46+
function missingPackage(specifier: string): Error {
47+
const error = new Error(
48+
`Cannot find package '${specifier}' imported from /app/node_modules/veryfront/esm/cli.js`,
49+
);
50+
(error as Error & { code: string }).code = "ERR_MODULE_NOT_FOUND";
51+
return error;
52+
}
53+
54+
async function parsePdfWith(loadError: Error): Promise<Error> {
55+
const directory = await makeTempDir();
56+
const filePath = `${directory}/book.pdf`;
57+
await Deno.writeTextFile(filePath, "%PDF-1.4");
58+
try {
59+
await runKnowledgeParser({ filePath, outputDir: `${directory}/output` }, {
60+
loadDocumentExtractor: () => Promise.reject(loadError),
61+
});
62+
} catch (error) {
63+
return error as Error;
64+
} finally {
65+
await Deno.remove(directory, { recursive: true });
66+
}
67+
throw new Error("expected the parser to fail");
68+
}
69+
70+
it("names the install commands when the Kreuzberg package is missing", async () => {
71+
const missing = missingPackage("@veryfront/ext-document-kreuzberg");
72+
const error = await parsePdfWith(missing);
73+
assertStringIncludes(error.message, INSTALL);
74+
assertStringIncludes(error.message, NPX);
75+
});
76+
77+
it("names the install commands for the wrapped first-party import error", async () => {
78+
const missing = missingPackage("@veryfront/ext-document-kreuzberg");
79+
const wrapped = new Error(
80+
`${missing.message} First-party extension "ext-document-kreuzberg" is not installed; install @veryfront/ext-document-kreuzberg alongside veryfront to enable it.`,
81+
{ cause: new AggregateError([missing], missing.message) },
82+
);
83+
const error = await parsePdfWith(wrapped);
84+
assertStringIncludes(error.message, INSTALL);
85+
assertStringIncludes(error.message, NPX);
86+
});
87+
88+
it("passes a broken dependency inside the installed extension through unchanged", async () => {
89+
const error = await parsePdfWith(missingPackage("jose"));
90+
assertEquals(
91+
error.message,
92+
"knowledge ingest parser failed: Cannot find package 'jose' imported from /app/node_modules/veryfront/esm/cli.js",
93+
);
94+
});
95+
});

0 commit comments

Comments
 (0)