//! plugin-llm —— LLM API 代理插件。 //! //! 职责:`//...` → 上游转发决策、上游 CRUD、OpenAI 兼容模型发现、 //! `rl:`/`st:` 配额执行、token 计量(body filter 旁路)、用量统计与趋势、 //! 对话审计落盘、运行期统计落盘/恢复。数据文件 `data/llm-upstreams.json` //! (敏感级别等同 users.json)与 `data/llm-stats.json`(统计快照,无凭据)。 //! //! ## 装配点(gateway) //! //! * `preconfig` —— 注册 `$llm_target` / `$llm_host` / `$llm_auth` 三件套 //! * `postconfig` —— 注册 LlmHandler(access)/ LlmHealthHandler、AuditHandler //! (log)/ body filter 链头插入(token 计量)/ 预取 `$upstream_status` 索引 //! * `zone_load` —— 全量装载 `up:` 上游表 //! * `dispatch_api` —— 认领上游 / 用量类端点(趋势端点留 gateway,需三方名册) //! //! ## 进程生命周期钩子(由 gateway 调用,不是插件自己的钩子) //! //! * `stats_persist::restore()` 在 `init_process` —— 重启后把统计快照灌回共享内存 //! * `stats_persist::dump_now()` 在 `exit_process` —— 退出前落盘,发版不丢统计 //! //! ## 与其他插件的关系 //! //! 下行鉴权与配额执行(`ukey:` / `rl:` / `st:`)在内核 `ipass_core::auth`; //! 记账原语在内核 `ipass_core::usage`——apix 插件复用同一套下行契约, //! 与本插件共用同一片用量键空间(apix 的 slot 带 `api/` 前缀天然归组)。 pub mod api; pub mod config; pub mod handlers; pub mod llm; pub mod llm_audit; pub mod models; pub mod stats; pub mod stats_persist; pub mod store; pub mod upstreams; pub mod usage_filter; use ngx::core::Status; use ngx::ffi::{ngx_conf_t, ngx_http_request_t}; use ipass_core::plugin::Plugin; pub struct LlmPlugin; impl Plugin for LlmPlugin { fn name(&self) -> &'static str { "llm" } fn preconfig(&self, cf: *mut ngx_conf_t) -> bool { // SAFETY: cf 由 nginx 传入且有效 unsafe { llm::preconfig(cf) } } fn postconfig(&self, cf: *mut ngx_conf_t) -> bool { // SAFETY: cf 由 nginx 传入且有效 let cf = unsafe { &mut *cf }; // token 计量:插进 body filter 链头,旁路扫描上游响应的 usage usage_filter::install(); if ngx::http::add_phase_handler::(cf).is_err() { return false; } if ngx::http::add_phase_handler::(cf).is_err() { return false; } if ngx::http::add_phase_handler::(cf).is_err() { return false; } // 预取 `$upstream_status` 索引(upstream 模块 preconfiguration 已注册该内建变量) handlers::prefetch_upstream_status_index(cf) } fn dispatch_api(&self, r: *mut ngx_http_request_t) -> Option { api::dispatch(r) } fn zone_load(&self) -> usize { upstreams::reload_shm() } }