@stll/folio-agents
Version:
Framework-neutral LLM tool layer over folio's ai-edits engine: function-calling tools so an agent can read and mutate .docx documents through @stll/folio-core.
53 lines (52 loc) • 3.28 kB
JavaScript
import { FOLIO_DOCUMENT_OPERATION_TYPES } from "@stll/folio-core/server";
import { TaggedError } from "better-result";
//#region src/suggest-changes-options.ts
/**
* Host-facing configuration of the `suggest_changes` tool. One resolved
* options object drives the tool's JSON Schema (`tools.ts`), its parser
* (`parse.ts`), and its capability description, so a host that needs a
* narrower or stricter surface configures instead of re-deriving the
* contract.
*/
const DEFAULT_EXCLUDED_OPERATION_TYPES = /* @__PURE__ */ new Set(["commentOnBlock", "insertSignatureTable"]);
/** The operation types `suggest_changes` exposes when a host passes no `operationTypes`. */
const DEFAULT_SUGGEST_CHANGES_OPERATION_TYPES = FOLIO_DOCUMENT_OPERATION_TYPES.filter((type) => !DEFAULT_EXCLUDED_OPERATION_TYPES.has(type));
const DEFAULT_MAX_OPERATIONS_PER_CALL = 50;
const MAX_OPERATIONS_PER_CALL_LIMIT = 200;
/** Programmer misuse of {@link FolioSuggestChangesOptions}; never reaches the model. */
var InvalidFolioSuggestChangesOptionsError = class extends TaggedError("InvalidFolioSuggestChangesOptionsError") {};
const invalidOption = (option, message) => {
throw new InvalidFolioSuggestChangesOptionsError({
message: `Invalid suggest_changes option \`${option}\`: ${message}.`,
option
});
};
const isContractOperationType = (value) => typeof value === "string" && FOLIO_DOCUMENT_OPERATION_TYPES.includes(value);
const resolveOperationTypes = (requested) => {
if (requested === void 0) return DEFAULT_SUGGEST_CHANGES_OPERATION_TYPES;
if (!Array.isArray(requested)) return invalidOption("operationTypes", "expected an array of operation types");
if (requested.length === 0) return invalidOption("operationTypes", "expected at least one operation type");
const unknown = requested.find((type) => !isContractOperationType(type));
if (unknown !== void 0) return invalidOption("operationTypes", `"${String(unknown)}" is not a contract operation type`);
const wanted = new Set(requested);
return FOLIO_DOCUMENT_OPERATION_TYPES.filter((type) => wanted.has(type));
};
const resolveMaxOperations = (requested) => {
if (requested === void 0) return 50;
if (!Number.isInteger(requested) || requested < 1 || requested > 200) return invalidOption("maxOperations", `expected an integer from 1 to 200`);
return requested;
};
const resolveDocumentVersion = (requested) => {
if (requested === void 0) return null;
if (typeof requested !== "object" || requested === null || typeof requested.current !== "string" || requested.current.length === 0) return invalidOption("documentVersion", "expected a non-empty `current` version token");
return { current: requested.current };
};
/** Validate and default a host's options; throws {@link InvalidFolioSuggestChangesOptionsError} on misuse. */
const resolveSuggestChangesOptions = (options = {}) => ({
operationTypes: resolveOperationTypes(options.operationTypes),
reviewMeta: options.reviewMeta ?? "optional",
maxOperations: resolveMaxOperations(options.maxOperations),
documentVersion: resolveDocumentVersion(options.documentVersion)
});
//#endregion
export { DEFAULT_MAX_OPERATIONS_PER_CALL, DEFAULT_SUGGEST_CHANGES_OPERATION_TYPES, InvalidFolioSuggestChangesOptionsError, MAX_OPERATIONS_PER_CALL_LIMIT, resolveSuggestChangesOptions };