-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
190 lines (172 loc) · 7.03 KB
/
Copy pathindex.ts
File metadata and controls
190 lines (172 loc) · 7.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* OpenClaw Nutrient PDF Plugin
*
* Provides explicit, on-demand Nutrient-powered PDF extraction:
* - the `nutrient_pdf_extract` agent tool, and
* - the `openclaw nutrient-pdf` CLI.
*
* Note: OpenClaw's built-in `pdf` tool performs its own extraction via the
* bundled `document-extract` plugin (the `clawpdf` engine). OpenClaw does not
* currently expose a hook for an external plugin to replace that built-in
* extractor, so this plugin is an explicit-invocation supplement rather than a
* drop-in engine for the built-in tool.
*/
import { Type } from "@sinclair/typebox";
import { definePluginEntry, type OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry";
import { coercePluginConfig } from "./src/config.js";
import {
DEFAULT_TIMEOUT_MS,
extractWithNutrientCli,
getNutrientCliVersion,
isNutrientCliAvailable,
validatePdfPath,
type NutrientCliConfig,
} from "./src/nutrient-cli.js";
const INSTALL_HINT = "npm install -g @pspdfkit/pdf-to-markdown";
const UNAVAILABLE_MESSAGE = `Nutrient PDF CLI is not available. Install with: ${INSTALL_HINT}`;
/**
* Returns an error message when the CLI is unavailable, or null when it is ready.
* Single source of truth for the three call sites that gate on availability.
*/
async function ensureCliAvailable(command?: string): Promise<string | null> {
const available = await isNutrientCliAvailable(command);
return available ? null : UNAVAILABLE_MESSAGE;
}
export default definePluginEntry({
id: "nutrient-pdf",
name: "Nutrient PDF",
description: "Nutrient-powered PDF extraction with markdown table and heading preservation",
register(api: OpenClawPluginApi) {
const config = coercePluginConfig(api.pluginConfig);
const cliConfig: NutrientCliConfig = {
command: config.command,
timeoutMs: config.timeoutMs,
};
// ------------------------------------------------------------------
// Startup: check CLI availability and log configuration guidance
// ------------------------------------------------------------------
void (async () => {
const available = await isNutrientCliAvailable(config.command);
if (!available) {
api.logger.warn(`nutrient-pdf: pdf-to-markdown CLI not found. Install with: ${INSTALL_HINT}`);
return;
}
const version = await getNutrientCliVersion(config.command);
api.logger.info(`nutrient-pdf: CLI available${version ? ` (${version})` : ""}`);
api.logger.info(
"nutrient-pdf: use the nutrient_pdf_extract tool or `openclaw nutrient-pdf extract <file.pdf>` for on-demand extraction.",
);
})();
// ------------------------------------------------------------------
// Tool: nutrient_pdf_extract
// ------------------------------------------------------------------
api.registerTool(
{
name: "nutrient_pdf_extract",
label: "Nutrient PDF Extract",
description:
"Extract text and structure from a PDF using Nutrient's pdf-to-markdown engine. " +
"Returns clean Markdown with preserved tables, headings, and reading order. " +
"Use this when you need high-fidelity PDF extraction, especially for documents with tables or complex layouts.",
parameters: Type.Object({
pdf: Type.String({
description: "Path to a local PDF file",
}),
}),
async execute(_toolCallId, params) {
const { pdf: pdfPath } = params as { pdf: string };
const unavailable = await ensureCliAvailable(config.command);
if (unavailable) {
return {
content: [{ type: "text", text: unavailable }],
details: { error: "cli_not_available" },
};
}
try {
// Validate path: enforce .pdf extension and size cap
const { buffer } = await validatePdfPath(pdfPath);
const result = await extractWithNutrientCli(buffer, cliConfig);
return {
content: [{ type: "text", text: result.markdown }],
details: {
engine: "nutrient",
chars: result.markdown.length,
durationMs: result.durationMs,
...(result.stderrSnippet ? { stderrSnippet: result.stderrSnippet } : {}),
},
};
} catch (error) {
return {
content: [
{
type: "text",
text: `Nutrient extraction failed: ${error instanceof Error ? error.message : String(error)}`,
},
],
details: {
error: error instanceof Error ? error.message : String(error),
},
};
}
},
},
{ name: "nutrient_pdf_extract" },
);
// ------------------------------------------------------------------
// CLI: openclaw nutrient-pdf status | extract
// ------------------------------------------------------------------
api.registerCli(
({ program }) => {
const cmd = program.command("nutrient-pdf").description("Nutrient PDF extraction plugin");
cmd
.command("status")
.description("Check Nutrient CLI availability and version")
.action(async () => {
const available = await isNutrientCliAvailable(config.command);
const version = available ? await getNutrientCliVersion(config.command) : null;
console.log(`Nutrient CLI: ${available ? "available" : "not found"}`);
if (version) {
console.log(`Version: ${version}`);
}
console.log(`Command: ${config.command ?? "pdf-to-markdown (auto-resolved)"}`);
console.log(`Timeout: ${config.timeoutMs ?? DEFAULT_TIMEOUT_MS}ms`);
});
cmd
.command("extract")
.description("Extract markdown from a PDF")
.argument("<pdf>", "Path to PDF file")
.action(async (pdfPath: string) => {
const unavailable = await ensureCliAvailable(config.command);
if (unavailable) {
console.error(unavailable);
process.exitCode = 1;
return;
}
try {
const { buffer } = await validatePdfPath(pdfPath);
const result = await extractWithNutrientCli(buffer, cliConfig);
console.log(result.markdown);
} catch (error) {
console.error(
`Nutrient extraction failed: ${error instanceof Error ? error.message : String(error)}`,
);
process.exitCode = 1;
}
});
},
{ commands: ["nutrient-pdf"] },
);
// ------------------------------------------------------------------
// Service registration
// ------------------------------------------------------------------
api.registerService({
id: "nutrient-pdf",
start: () => {
api.logger.info("nutrient-pdf: service started");
},
stop: () => {
api.logger.info("nutrient-pdf: service stopped");
},
});
},
});