-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
81 lines (74 loc) · 2.7 KB
/
Copy pathindex.ts
File metadata and controls
81 lines (74 loc) · 2.7 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
import { makeClient } from './src/client.js';
import { JsonlCreditTracker } from './src/credits.js';
import { ConfigError } from './src/errors.js';
import type { ToolContext, ToolDefinition, NutrientClient } from './src/types.js';
// Import all tool definitions
import { convertToPdfTool } from './src/tools/convert-to-pdf.js';
import { convertToImageTool } from './src/tools/convert-to-image.js';
import { convertToOfficeTool } from './src/tools/convert-to-office.js';
import { nutrient_extract_text } from './src/tools/extract-text.js';
import { ocrTool } from './src/tools/ocr.js';
import { watermarkTool } from './src/tools/watermark.js';
import { nutrient_redact } from './src/tools/redact.js';
import { nutrient_ai_redact } from './src/tools/ai-redact.js';
import { signTool } from './src/tools/sign.js';
import { checkCreditsTool } from './src/tools/check-credits.js';
/**
* Nutrient Document Processing plugin for OpenClaw.
*
* Registers 10 document processing tools powered by the Nutrient DWS API.
* Config: apiKey (required), sandboxDir (optional).
*/
export default function nutrientPlugin(api: any) {
const config = api.pluginConfig ?? {};
// Resolve API key: config → env → lazy error on first use
const apiKey = config.apiKey || process.env.NUTRIENT_API_KEY;
let client: NutrientClient;
if (apiKey) {
client = makeClient(apiKey);
} else {
// Lazy proxy — plugin loads fine, first API call fails with a clear message
client = {
post: async () => {
throw new ConfigError(
'NUTRIENT_API_KEY not configured. Set it in plugin settings or NUTRIENT_API_KEY env var. ' +
'Get a key at https://dashboard.nutrient.io/sign_up'
);
},
};
}
const sandboxDir = config.sandboxDir || undefined;
const credits = new JsonlCreditTracker(sandboxDir);
const ctx: ToolContext = { client, credits, sandboxDir };
const tools: ToolDefinition[] = [
convertToPdfTool,
convertToImageTool,
convertToOfficeTool,
nutrient_extract_text,
ocrTool,
watermarkTool,
nutrient_redact,
nutrient_ai_redact,
signTool,
checkCreditsTool,
];
for (const tool of tools) {
api.registerTool({
name: tool.name,
label: tool.description,
description: tool.description,
parameters: tool.parameters,
async execute(_toolCallId: string, params: any) {
const result = await tool.execute(params, ctx);
// Adapt ToolResponse → AgentToolResult format
const text = result.success
? (result.output ?? 'Done.')
: `Error: ${result.error ?? 'Unknown error'}`;
return {
content: [{ type: 'text' as const, text }],
details: result,
};
},
});
}
}