unstructured-client
Version:
<h3 align="center"> <img src="https://raw.githubusercontent.com/Unstructured-IO/unstructured/main/img/unstructured_logo.png" height="200" > </h3>
69 lines • 2.62 kB
JavaScript
import { formatResult } from "./tools.js";
import { generalPartition } from "../funcs/generalPartition.js";
import fs from 'node:fs/promises';
import { z } from "zod";
import { Strategy, Strategy$inboundSchema } from "../sdk/models/shared/partitionparameters.js";
const FileRequest$inboundSchema = z.object({
file_content: z.string().optional(),
file_path: z.string().optional(),
file_name: z.string(),
strategy: Strategy$inboundSchema.default(Strategy.HiRes),
});
const customToolArg = {
request: FileRequest$inboundSchema
};
export const tool$generalPartitionCorrect = {
name: "correct_general-partition",
description: `use this tool to pass a file to unstructured. You must BASE64 ENCODE uploaded file content before passing to unstructured. Alternatively, if the user did not upload a file they can provide a full local file path. `,
args: customToolArg,
tool: async (client, args, ctx) => {
let data;
if (args.request.file_content) {
try {
data = new Uint8Array(Buffer.from(args.request.file_content, 'base64'));
}
catch (e) {
return {
content: [{
type: "text",
text: `You must BASE64 encode this file content then pass it to the tool.`,
}],
isError: true,
};
}
}
else if (args.request.file_path) {
data = new Uint8Array(await fs.readFile(args.request.file_path));
}
else {
return {
content: [{
type: "text",
text: `A full file path for file content must be provided`,
}],
isError: true,
};
}
const [result, apiCall] = await generalPartition(client, {
partitionParameters: {
files: {
content: data,
fileName: args.request.file_name,
},
strategy: args.request.strategy,
}
}, { fetchOptions: { signal: ctx.signal } }).$inspect();
if (!result.ok) {
return {
content: [{ type: "text", text: result.error.message }],
isError: true,
};
}
const value = result.value;
return formatResult(value, apiCall);
},
};
export function registerMCPExtensions(register) {
register.tool(tool$generalPartitionCorrect);
}
//# sourceMappingURL=server.extensions.js.map