local plugin = {} local config = {} local prompt = "" local pending = {} local text = "" function plugin.init(ctx, options) config = options end function plugin.on_request(ctx, request) local body = host.json.decode(request.body) prompt = body.message or "" end local function check_and_release() if #pending == 0 then return nil end local ok, result = pcall(host.llm.call, config.model, { task = "response_moderation", prompt = prompt, response = text, }) if not ok or result.safety == "Unsafe" then return {action = "block"} end local released = host.json.array(pending) pending = {} text = "" return {action = "replace", events = released} end function plugin.on_event(ctx, event) if event.event ~= "text_delta" then return nil end local data = host.json.decode(event.data) local content = data.data and data.data.content or "" pending[#pending + 1] = event text = text .. content if text:match("[。!?.!?]\s*$") then return check_and_release() end return {action = "hold"} end function plugin.on_end(ctx) return check_and_release() end function plugin.close(ctx) pending = {} text = "" end return plugin