-- file_content_review.lua -- Moderates the extracted text of uploaded attachments (.txt/.doc/.docx) -- through the same LLM review path used for user input (see response_guard.lua). -- -- Required config: -- model: reviewer model name for host.llm.call -- Optional config: -- allowed_extensions: array of extensions (default {"txt","doc","docx"}) -- max_review_bytes: per-file cap, default 65536; exceeding files are -- blocked, never silently truncated -- max_files: max reviewable attachments per request, default 16 -- prompt: override the audit prompt -- -- Contract: HTTP-equivalent of response_guard's input audit. Any extraction, -- size, or moderation failure blocks the request (fail-closed). File content -- is never logged by this plugin. local plugin = {} local settings = {} local DEFAULT_EXTENSIONS = { "txt", "doc", "docx" } local DEFAULT_MAX_REVIEW_BYTES = 65536 local DEFAULT_MAX_FILES = 16 local MAX_TOTAL_BYTES = 262144 -- combined review budget across all files local FILE_AUDIT_PROMPT = "Review the following attachment content extracted from user-uploaded " .. "files for safety before it is used in the conversation." local function is_non_empty_string(v) return type(v) == "string" and v ~= "" end -- ".PDF" -> "pdf"; no extension -> nil (not reviewable, passes by policy) local function normalize_ext(name) if type(name) ~= "string" then return nil end local ext = name:match("%.(%w+)$") if not ext then return nil end return ext:lower() end function plugin.init(ctx, options) if type(options) ~= "table" then error("file_content_review config must be an object") end if not is_non_empty_string(options.model) then error("file_content_review requires config.model") end local exts = options.allowed_extensions or DEFAULT_EXTENSIONS if type(exts) ~= "table" or #exts == 0 then error("file_content_review config.allowed_extensions must be a non-empty array") end local allowed, seen = {}, {} for _, ext in ipairs(exts) do if not is_non_empty_string(ext) then error("file_content_review config.allowed_extensions entries must be non-empty strings") end local lower = ext:lower() if seen[lower] then error("file_content_review config.allowed_extensions contains a duplicate extension") end seen[lower] = true allowed[lower] = true end local max_bytes = tonumber(options.max_review_bytes) or DEFAULT_MAX_REVIEW_BYTES if max_bytes < 1 or max_bytes % 1 ~= 0 then error("file_content_review config.max_review_bytes must be a positive integer") end local max_files = tonumber(options.max_files) or DEFAULT_MAX_FILES if max_files < 1 or max_files % 1 ~= 0 then error("file_content_review config.max_files must be a positive integer") end local prompt = is_non_empty_string(options.prompt) and options.prompt or FILE_AUDIT_PROMPT settings = { model = options.model, allowed = allowed, max_review_bytes = max_bytes, max_files = max_files, prompt = prompt, } return nil end function plugin.on_request(ctx, request) if type(request) ~= "table" or request.kind ~= "multipart" then return { action = "pass" } end local files = request.files if type(files) ~= "table" or #files == 0 then return { action = "pass" } end local texts, total_bytes = {}, 0 local reviewable = 0 for i, file in ipairs(files or {}) do if type(file) ~= "table" or not is_non_empty_string(file.id) then return { action = "block", reason = "invalid attachment metadata at index " .. i } end local ext = normalize_ext(file.name) if ext and settings.allowed[ext] then reviewable = reviewable + 1 if reviewable > settings.max_files then return { action = "block", reason = "too many reviewable attachments" } end local read_ok, text = pcall(host.files.read_text, file.id) if not read_ok or type(text) ~= "string" then return { action = "block", reason = "attachment text extraction failed (index " .. i .. ")" } end if #text == 0 then -- Extraction produced nothing for a reviewable type. That is -- itself a signal (corrupt doc, unsupported layout, empty -- wrapper hiding content), so fail closed. return { action = "block", reason = "attachment produced no reviewable text (index " .. i .. ")" } end if #text > settings.max_review_bytes then return { action = "block", reason = "attachment exceeds review size limit (index " .. i .. ")" } end total_bytes = total_bytes + #text if total_bytes > MAX_TOTAL_BYTES then return { action = "block", reason = "combined attachment content exceeds review budget" } end texts[#texts + 1] = text end -- Non-reviewable extensions pass by policy; type filtering is not -- this plugin's job. Configure a separate type-allowlist plugin if -- unknown formats must be rejected. end if #texts == 0 then return { action = "pass" } -- multipart, but nothing reviewable end -- Single combined review call (same budget pattern as response_guard's -- multi-turn user audit). Separator is cosmetic; the reviewer sees the -- full original text. local combined = table.concat(texts, "\n\n---\n\n") local ok, result = pcall(host.llm.call, settings.model, { task = "response_moderation", prompt = settings.prompt, response = combined, }) if not ok or type(result) ~= "table" or result.safety ~= "Safe" then return { action = "block", reason = "attachment content moderation failed or unsafe" } end return { action = "pass" } end return plugin