UNPKG

@baruchiro/paperless-mcp

Version:

Model Context Protocol (MCP) server for interacting with Paperless-NGX document management system. Enables AI assistants to manage documents, tags, correspondents, and document types through the Paperless-NGX API.

92 lines (91 loc) 4.23 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveSelectCustomFieldValue = resolveSelectCustomFieldValue; exports.resolveSelectCustomFieldValues = resolveSelectCustomFieldValues; function normalizeSelectOptions(field) { var _a; const rawOptions = (_a = field.extra_data) === null || _a === void 0 ? void 0 : _a.select_options; if (!Array.isArray(rawOptions)) { return []; } return rawOptions.map((option, index) => { if (option && typeof option === "object") { const { id, label } = option; return { index, label: typeof label === "string" ? label : String(label), id: typeof id === "string" ? id : undefined, }; } return { index, label: String(option) }; }); } function findSelectOption(options, value) { if (typeof value === "string") { const byLabel = options.find((option) => option.label === value); const byId = options.find((option) => option.id === value); if (byLabel && byId && byLabel.index !== byId.index) { throw new Error(`Ambiguous select value ${JSON.stringify(value)}: it matches one ` + `option's label and a different option's id.`); } return byLabel !== null && byLabel !== void 0 ? byLabel : byId; } if (typeof value === "number" && Number.isInteger(value)) { return options.find((option) => option.index === value); } return undefined; } /** Translates a select value (label, option id, or index) to the `encoding` Paperless expects; throws on no match. */ function resolveSelectCustomFieldValue(field, value, encoding = "index") { var _a; if (field.data_type !== "select" || value === null) { return value; } const options = normalizeSelectOptions(field); if (options.length === 0) { return value; } const option = findSelectOption(options, value); if (!option) { const optionList = options .map((o) => `${JSON.stringify(o.label)} (index ${o.index})`) .join(", "); throw new Error(`Invalid value ${JSON.stringify(value)} for select custom field ` + `"${field.name}" (id ${field.id}). Pass one of the option labels. ` + `Valid options: ${optionList}.`); } return encoding === "stored" ? (_a = option.id) !== null && _a !== void 0 ? _a : option.index : option.index; } function resolveSelectCustomFieldValues(api_1, customFields_1) { return __awaiter(this, arguments, void 0, function* (api, customFields, encoding = "index") { if (!customFields || customFields.length === 0) { return customFields; } const uniqueFieldIds = [...new Set(customFields.map((cf) => cf.field))]; const definitions = new Map(); yield Promise.all(uniqueFieldIds.map((id) => __awaiter(this, void 0, void 0, function* () { try { definitions.set(id, yield api.getCustomField(id)); } catch (_a) { // Definition unavailable; leave the value for Paperless to validate. } }))); return customFields.map((cf) => { const field = definitions.get(cf.field); if (!field || field.data_type !== "select") { return cf; } return Object.assign(Object.assign({}, cf), { value: resolveSelectCustomFieldValue(field, cf.value, encoding) }); }); }); }