Skip to main content

vulcan_luaskills/host/
options.rs

1use crate::host::database::{LuaRuntimeDatabaseCallbackMode, LuaRuntimeDatabaseProviderMode};
2use crate::runtime_context::RuntimeRequestContext;
3use crate::skill::manager::SkillProtectionConfig;
4use crate::tool_cache::ToolCacheConfig;
5use serde::{Deserialize, Serialize};
6use serde_json::{Map, Value};
7use std::path::PathBuf;
8
9/// Process mode used when the runtime auto-spawns one local space controller process.
10/// 运行时自动拉起本地空间控制器进程时使用的进程模式。
11#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default)]
12#[serde(rename_all = "snake_case")]
13pub enum LuaRuntimeSpaceControllerProcessMode {
14    /// Service mode keeps the controller process alive until an external stop happens.
15    /// Service 模式会让控制器进程持续存活,直到外部显式停止。
16    Service,
17    /// Managed mode allows the controller process to stop itself after idle timeouts.
18    /// Managed 模式允许控制器进程在空闲超时后自行停止。
19    #[default]
20    Managed,
21}
22
23/// Host-provided controller client options used when one database backend chooses `space_controller`.
24/// 当数据库后端选择 `space_controller` 时使用的宿主侧控制器客户端选项。
25#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
26pub struct LuaRuntimeSpaceControllerOptions {
27    /// Optional explicit controller endpoint; when omitted the shared default endpoint is used.
28    /// 可选的显式控制器端点;缺失时使用共享默认端点。
29    pub endpoint: Option<String>,
30    /// Whether the runtime may auto-spawn the controller when the endpoint is unavailable.
31    /// 当控制器端点不可用时,运行时是否允许自动唤起控制器。
32    pub auto_spawn: bool,
33    /// Optional local executable path copied and managed by the host.
34    /// 由宿主复制并管理的可选本地可执行文件路径。
35    pub executable_path: Option<PathBuf>,
36    /// Process mode used when auto-spawning one controller process.
37    /// 自动唤起控制器进程时使用的进程模式。
38    pub process_mode: LuaRuntimeSpaceControllerProcessMode,
39    /// Minimum uptime in seconds passed to one auto-spawned controller.
40    /// 传递给自动唤起控制器的最小存活秒数。
41    pub minimum_uptime_secs: u64,
42    /// Idle timeout in seconds passed to one auto-spawned controller.
43    /// 传递给自动唤起控制器的空闲超时秒数。
44    pub idle_timeout_secs: u64,
45    /// Default lease TTL in seconds passed to one auto-spawned controller.
46    /// 传递给自动唤起控制器的默认租约 TTL 秒数。
47    pub default_lease_ttl_secs: u64,
48    /// Transport connect timeout in seconds used by the controller client proxy.
49    /// 控制器客户端代理使用的传输连接超时秒数。
50    pub connect_timeout_secs: u64,
51    /// Startup timeout in seconds used while waiting for one spawned controller to become ready.
52    /// 等待已唤起控制器就绪时使用的启动超时秒数。
53    pub startup_timeout_secs: u64,
54    /// Startup retry interval in milliseconds used while polling one spawned controller.
55    /// 轮询已唤起控制器时使用的启动重试间隔毫秒数。
56    pub startup_retry_interval_ms: u64,
57    /// Lease renew interval in seconds used by the background controller client task.
58    /// 后台控制器客户端任务使用的租约续约间隔秒数。
59    pub lease_renew_interval_secs: u64,
60}
61
62impl Default for LuaRuntimeSpaceControllerOptions {
63    /// Return one safe-by-default shared controller configuration.
64    /// 返回一套默认安全且面向共享控制器的配置。
65    fn default() -> Self {
66        Self {
67            endpoint: None,
68            auto_spawn: true,
69            executable_path: None,
70            process_mode: LuaRuntimeSpaceControllerProcessMode::Managed,
71            minimum_uptime_secs: 300,
72            idle_timeout_secs: 900,
73            default_lease_ttl_secs: 120,
74            connect_timeout_secs: 5,
75            startup_timeout_secs: 15,
76            startup_retry_interval_ms: 250,
77            lease_renew_interval_secs: 30,
78        }
79    }
80}
81
82/// Host-controlled toggles for optional Lua-exposed runtime bridges.
83/// 宿主控制的可选 Lua 暴露运行时桥接开关集合。
84#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
85pub struct LuaRuntimeCapabilityOptions {
86    /// Whether `vulcan.runtime.skills.*` management bridges are exposed to Lua.
87    /// 是否将 `vulcan.runtime.skills.*` 管理桥接暴露给 Lua。
88    pub enable_skill_management_bridge: bool,
89}
90
91impl Default for LuaRuntimeCapabilityOptions {
92    /// Return one secure-by-default capability set.
93    /// 返回一组默认安全的能力开关集合。
94    fn default() -> Self {
95        Self {
96            enable_skill_management_bridge: false,
97        }
98    }
99}
100
101/// One named skill root injected by the host, used to build ordered override environments.
102/// 由宿主注入的单个命名技能根,用于构建有序覆盖环境。
103#[derive(Debug, Clone, PartialEq, Eq, Serialize, serde::Deserialize)]
104pub struct RuntimeSkillRoot {
105    /// Stable root name such as ROOT, USER, or one project identifier.
106    /// 稳定根名称,例如 ROOT、USER 或某个项目标识符。
107    pub name: String,
108    /// Physical skills directory represented by the current named root.
109    /// 当前命名根所代表的物理 skills 目录。
110    pub skills_dir: PathBuf,
111}
112
113/// Host-provided pool configuration for isolated runlua VMs.
114/// 宿主提供的隔离 runlua 虚拟机池配置。
115#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
116pub struct LuaRuntimeRunLuaPoolConfig {
117    /// Minimum number of isolated runlua VMs kept warm.
118    /// 隔离 runlua 虚拟机需要常驻保温的最小数量。
119    pub min_size: usize,
120    /// Maximum number of isolated runlua VMs allowed in the pool.
121    /// 隔离 runlua 虚拟机池允许存在的最大数量。
122    pub max_size: usize,
123    /// Idle TTL in seconds before one excess isolated runlua VM may be retired.
124    /// 多余隔离 runlua 虚拟机在空闲多少秒后允许回收。
125    pub idle_ttl_secs: u64,
126}
127
128/// Host-provided filesystem and runtime paths consumed by the LuaSkills library.
129/// 宿主提供给 LuaSkills 库消费的文件系统与运行时路径集合。
130#[derive(Debug, Clone, Default, Serialize, Deserialize)]
131pub struct LuaRuntimeHostOptions {
132    /// Host-managed temporary directory used by luaexec spill files and similar transient artifacts.
133    /// 宿主管理的临时目录,供 luaexec 请求文件等短生命周期产物使用。
134    pub temp_dir: Option<PathBuf>,
135    /// Optional host-managed resources directory exposed to Lua as `vulcan.runtime.resources_dir`.
136    /// 以 `vulcan.runtime.resources_dir` 形式暴露给 Lua 的可选宿主管理资源目录。
137    pub resources_dir: Option<PathBuf>,
138    /// Optional lua_packages root used to build `package.path` and `package.cpath`.
139    /// 用于拼接 `package.path` 与 `package.cpath` 的可选 lua_packages 根目录。
140    pub lua_packages_dir: Option<PathBuf>,
141    /// Host-managed root directory used only to probe host-provided tool dependencies.
142    /// 仅用于探测宿主提供工具依赖的宿主管理根目录。
143    pub host_provided_tool_root: Option<PathBuf>,
144    /// Host-managed root directory used only to probe host-provided Lua package dependencies.
145    /// 仅用于探测宿主提供 Lua 包依赖的宿主管理根目录。
146    pub host_provided_lua_root: Option<PathBuf>,
147    /// Host-managed root directory used only to probe host-provided FFI/native dependencies.
148    /// 仅用于探测宿主提供 FFI/原生依赖的宿主管理根目录。
149    pub host_provided_ffi_root: Option<PathBuf>,
150    /// Host-managed cache directory used for downloaded archives and remote manifests.
151    /// 宿主管理的下载缓存目录,用于归档文件和远程清单缓存。
152    pub download_cache_root: Option<PathBuf>,
153    /// Fixed sibling directory name used under one skill-root parent to store dependencies.
154    /// 在单个技能根父目录下存放依赖时使用的固定兄弟目录名称。
155    pub dependency_dir_name: String,
156    /// Fixed sibling directory name used under one skill-root parent to store skill state.
157    /// 在单个技能根父目录下存放技能状态时使用的固定兄弟目录名称。
158    pub state_dir_name: String,
159    /// Fixed sibling directory name used under one skill-root parent to store skill databases.
160    /// 在单个技能根父目录下存放技能数据库时使用的固定兄弟目录名称。
161    pub database_dir_name: String,
162    /// Optional unified skill config file path owned by the host.
163    /// 由宿主拥有的可选统一技能配置文件路径。
164    pub skill_config_file_path: Option<PathBuf>,
165    /// Host-provided protected skill identifiers reserved for the system plane.
166    /// 由宿主提供、保留给 system 平面的受保护技能标识符。
167    pub protection: SkillProtectionConfig,
168    /// Whether the runtime is allowed to perform network downloads while installing dependencies.
169    /// 运行时在安装依赖时是否允许执行网络下载。
170    pub allow_network_download: bool,
171    /// Optional GitHub site base URL override used to rewrite browser download URLs.
172    /// 可选的 GitHub 站点基址覆盖,用于重写浏览器下载地址。
173    pub github_base_url: Option<String>,
174    /// Optional GitHub API base URL override used to resolve release metadata.
175    /// 可选的 GitHub API 基址覆盖,用于解析 release 元数据。
176    pub github_api_base_url: Option<String>,
177    /// Explicit SQLite dynamic-library path owned by the host.
178    /// 由宿主显式提供的 SQLite 动态库路径。
179    pub sqlite_library_path: Option<PathBuf>,
180    /// SQLite database provider mode selected by the host.
181    /// 宿主为 SQLite 数据库选择的 provider 模式。
182    #[serde(default)]
183    pub sqlite_provider_mode: LuaRuntimeDatabaseProviderMode,
184    /// SQLite callback transport mode selected by the host when provider mode is `host_callback`.
185    /// 当 provider 模式为 `host_callback` 时,宿主为 SQLite 选择的回调传输模式。
186    #[serde(default)]
187    pub sqlite_callback_mode: LuaRuntimeDatabaseCallbackMode,
188    /// Explicit LanceDB dynamic-library path owned by the host.
189    /// 由宿主显式提供的 LanceDB 动态库路径。
190    pub lancedb_library_path: Option<PathBuf>,
191    /// LanceDB database provider mode selected by the host.
192    /// 宿主为 LanceDB 数据库选择的 provider 模式。
193    #[serde(default)]
194    pub lancedb_provider_mode: LuaRuntimeDatabaseProviderMode,
195    /// LanceDB callback transport mode selected by the host when provider mode is `host_callback`.
196    /// 当 provider 模式为 `host_callback` 时,宿主为 LanceDB 选择的回调传输模式。
197    #[serde(default)]
198    pub lancedb_callback_mode: LuaRuntimeDatabaseCallbackMode,
199    /// Shared controller client options used when one database backend selects `space_controller`.
200    /// 当数据库后端选择 `space_controller` 时所使用的共享控制器客户端选项。
201    #[serde(default)]
202    pub space_controller: LuaRuntimeSpaceControllerOptions,
203    /// Host-provided transient cache policy consumed by `vulcan.cache`.
204    /// 由宿主提供并供 `vulcan.cache` 消费的临时缓存策略。
205    pub cache_config: Option<ToolCacheConfig>,
206    /// Optional dedicated pool configuration for isolated `vulcan.runtime.lua.exec` VMs.
207    /// 供隔离 `vulcan.runtime.lua.exec` 虚拟机使用的可选独立池配置。
208    #[serde(default)]
209    pub runlua_pool_config: Option<LuaRuntimeRunLuaPoolConfig>,
210    /// Host-reserved public entry names that LuaSkills canonical name generation must never occupy directly.
211    /// 宿主保留的公开入口名称集合,LuaSkills 在生成 canonical 名称时必须直接避开这些名称。
212    pub reserved_entry_names: Vec<String>,
213    /// Host-forced skill identifiers that must be skipped before dependency or database setup.
214    /// 宿主强制跳过的技能标识符列表,会在依赖或数据库初始化前生效。
215    #[serde(default)]
216    pub ignored_skill_ids: Vec<String>,
217    /// Host-controlled optional runtime capability toggles.
218    /// 由宿主控制的可选运行时能力开关集合。
219    #[serde(default)]
220    pub capabilities: LuaRuntimeCapabilityOptions,
221}
222
223/// Host-injected invocation context delivered alongside one skill or runlua call.
224/// 宿主在单次 skill 或 runlua 调用时一并注入的调用上下文。
225#[derive(Debug, Clone, Default, Serialize, Deserialize)]
226pub struct LuaInvocationContext {
227    /// Optional transport/request metadata preserved for Lua consumption.
228    /// 供 Lua 消费的可选传输层/请求层元数据。
229    pub request_context: Option<RuntimeRequestContext>,
230    /// Host-resolved client budget object injected into `vulcan.context.client_budget`.
231    /// 宿主解析后的客户端预算对象,将被注入到 `vulcan.context.client_budget`。
232    pub client_budget: Value,
233    /// Host-resolved tool configuration object injected into `vulcan.context.tool_config`.
234    /// 宿主解析后的工具配置对象,将被注入到 `vulcan.context.tool_config`。
235    pub tool_config: Value,
236}
237
238/// Host-resolved effective budget scope used by host-side render logic.
239/// 供宿主侧渲染逻辑使用的宿主已解析生效预算场景结构。
240#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq)]
241pub struct EffectiveBudgetScope {
242    /// Effective byte limit for the current scope.
243    /// 当前场景的生效字节上限。
244    pub bytes: u64,
245    /// Effective line limit for the current scope. `-1` means unlimited.
246    /// 当前场景的生效行数上限,`-1` 表示不限。
247    pub lines: i64,
248}
249
250/// Host-resolved client budget snapshot consumed by host-side overflow rendering.
251/// 供宿主侧超限渲染消费的宿主已解析客户端预算快照。
252#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
253pub struct ClientBudgetSnapshot {
254    /// Optional client name of the active caller.
255    /// 当前调用方的可选客户端名称。
256    pub client_name: Option<String>,
257    /// Optional resolved tool name.
258    /// 可选的已解析工具名称。
259    pub tool_name: Option<String>,
260    /// Optional resolved skill name.
261    /// 可选的已解析 skill 名称。
262    pub skill_name: Option<String>,
263    /// Optional matched client pattern decided by the host.
264    /// 宿主判定出的可选客户端匹配模式。
265    pub matched_client_pattern: Option<String>,
266    /// Effective tool-result budget.
267    /// 工具正文输出的生效预算。
268    pub tool_result: EffectiveBudgetScope,
269    /// Effective file-read budget.
270    /// 文件读取场景的生效预算。
271    pub file_read: EffectiveBudgetScope,
272    /// Tool-scoped host configuration snapshot.
273    /// 工具作用域下的宿主配置快照。
274    pub tool_config: Value,
275}
276
277impl LuaInvocationContext {
278    /// Construct one invocation context and normalize non-object JSON payloads into empty objects.
279    /// 构造一次调用上下文,并把非对象类型的 JSON 载荷归一化为空对象。
280    pub fn new(
281        request_context: Option<RuntimeRequestContext>,
282        client_budget: Value,
283        tool_config: Value,
284    ) -> Self {
285        Self {
286            request_context,
287            client_budget: normalize_context_object(client_budget),
288            tool_config: normalize_context_object(tool_config),
289        }
290    }
291
292    /// Return an empty invocation context with stable empty-object payloads.
293    /// 返回一个空调用上下文,并使用稳定的空对象载荷。
294    pub fn empty() -> Self {
295        Self::default()
296    }
297}
298
299/// Normalize one host context payload so the runtime always sees an object.
300/// 归一化单个宿主上下文载荷,确保运行时始终看到对象结构。
301fn normalize_context_object(value: Value) -> Value {
302    match value {
303        Value::Object(_) => value,
304        _ => Value::Object(Map::new()),
305    }
306}