Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions crates/tw-api/msg-codes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ gw.oauth.status
gw.oauth.unreachable
gw.output_limit.cut
gw.output_limit.withheld
gw.plugin.failed
gw.probe.aws_token_expired
gw.probe.bedrock_list_denied
gw.probe.connect
Expand Down Expand Up @@ -378,8 +379,10 @@ security.unknown_action
security.unknown_content_action
security.unknown_rule
t.auth test
t.boom test
t.broke test
t.limited test
t.plugin_threw test
t.upstream test
t.x test
yaml.anchor_or_alias
Expand Down
164 changes: 163 additions & 1 deletion crates/tw-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,10 @@ pub const MSG_CODES: &str = include_str!("../msg-codes.txt");
/// 31 起出站脱敏有**身份证号和银行卡号**两条出厂就开的规则:[`Matcher`] 多了
/// `cn-resident-id` 和 `bank-card`([`CardNetwork`]、[`CardPrefix`]),[`SecretKind`] 多了
/// `personal`。照 30 写的界面说不出这两条规则按什么认。
pub const CONTROL_API_VERSION: u32 = 31;
///
/// **32 起有脚本插件**:事件多了 [`Event::PluginFailed`](插件在请求上出错,或者文件变了、
/// 加载不了而停用)。照 31 写的界面不认这个事件。
pub const CONTROL_API_VERSION: u32 = 32;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
Expand Down Expand Up @@ -1303,6 +1306,23 @@ pub enum Event {
probe: ProbeClass,
at_ms: u64,
},
/// 一个插件没能把事情做成:在一个请求上运行出错(超时、超内存、抛了异常、交回的
/// 东西不合规矩),或者它的文件变了、加载不了,从此不再运行。
///
/// **给通知用。**请求上的每一次运行都记在那条请求上(`RequestDetail::plugins`),
/// 这条只说出了错的;停用那一种只在变成停用的那一刻说一次。`id` 和别的通知一样
/// 是新取的号,出错的那个请求是 `request_id`。
PluginFailed {
id: u64,
plugin_id: String,
/// 插件自己起的名字。**插件写的字**,界面当纯文本显示
plugin_name: String,
/// 在哪个请求上出的错。停用(文件变了、加载不了)不挂在请求上,没有
#[serde(default, skip_serializing_if = "Option::is_none")]
request_id: Option<u64>,
message: Msg,
at_ms: u64,
},
/// 这个订阅者跟不上,事件流丢了它 `count` 条事件。
///
/// **只发给掉队的那一个**,不进总线:别的订阅者什么都没丢。收到它就说明
Expand Down Expand Up @@ -1489,6 +1509,7 @@ impl Event {
| Event::CredentialRotated { id, .. }
| Event::CredentialExpired { id, .. }
| Event::LoginFinished { id, .. }
| Event::PluginFailed { id, .. }
| Event::RequestRouted { id, .. } => *id,
}
}
Expand Down Expand Up @@ -4674,6 +4695,131 @@ pub struct SecurityTestResult {
pub hits: Vec<SecurityTestHit>,
}

// ---------------------------------------------------------------- 脚本插件

slug_enum! {
/// 一个插件要的权限:它能看、能改请求和回答的哪一部分。
///
/// **插件文件里写成 `reply.text`、`reply.tool_calls`**(作者写的那种),线上是下划线
pub enum Permission {
/// 开头的那段系统指令
System = "system",
/// 对话里的消息
Messages = "messages",
/// 工具定义
Tools = "tools",
/// 模型名、`max_tokens` 这类参数。改了模型名,路由按新的走
Params = "params",
/// 回答里的文字
ReplyText = "reply_text",
/// 回答里的工具调用。**高风险**:改出来的调用照样过工具调用审查
ReplyToolCalls = "reply_tool_calls",
}
}

slug_enum! {
/// 插件出错(运行出错、文件变了、加载不了)时这个请求怎么办。
pub enum OnError {
/// 拒绝这个请求。出厂就是它:插件管不了的请求不该悄悄照原样发出去
Reject = "reject",
/// 跳过这个插件,请求照常
Skip = "skip",
}
}

slug_enum! {
/// 改回答文字的插件怎么拿到文字。
pub enum ReplyMode {
/// 一段文字整段交给插件,改完才发给客户端
Block = "block",
/// 边到边交,插件可以先压着一部分
Stream = "stream",
}
}

slug_enum! {
/// 插件设置项的类型。
pub enum SettingKind {
String = "string",
Number = "number",
Boolean = "boolean",
}
}

slug_enum! {
/// 插件在一个请求的哪一段上跑。
pub enum PluginHook {
/// 请求发往上游之前
Request = "request",
/// 回答到达客户端之前
Reply = "reply",
}
}

slug_enum! {
/// 一个插件在一个请求上的结果。
pub enum PluginOutcome {
/// 跑了,没改
Unchanged = "unchanged",
/// 跑了,改了
Changed = "changed",
/// 插件拒绝了这个请求(`reject()`)
Rejected = "rejected",
/// 出错了:超时、超内存、抛了异常、交回的东西不合规矩,或者插件没加载起来
Error = "error",
/// 没跑:插件没加载起来(文件变了、加载出错),而它设的是出错时跳过
Skipped = "skipped",
}
}

slug_enum! {
/// 插件日志一行的级别,`console.log` / `info` / `warn` / `error` 各一个。
pub enum PluginLogLevel {
Log = "log",
Info = "info",
Warn = "warn",
Error = "error",
}
}

/// 插件日志的一行。**原样是插件写的**:界面一律当纯文本显示。
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
pub struct PluginLogEntry {
pub at_ms: u64,
/// 哪个请求上写的。和请求记录的号是同一个
pub request_id: Option<u64>,
pub hook: PluginHook,
pub level: PluginLogLevel,
pub text: String,
}

/// 一个插件从 core 这次启动以来跑得怎么样。**只在内存里**:重启就从零数起。
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
pub struct PluginStats {
/// 真的跑了几次(没跑的「跳过」不算)。请求上一次、一个回答一次
pub calls: u64,
/// 其中改了东西的
pub changed: u64,
/// 其中插件拒绝了请求的
pub rejected: u64,
/// 其中出错的
pub errors: u64,
/// 平均每次用了多少 CPU,微秒。没跑过是 0
pub avg_cpu_us: u64,
/// 最近一次出错
pub last_error: Option<PluginLastError>,
}

/// 插件最近一次出错。
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "ts", derive(ts_rs::TS))]
pub struct PluginLastError {
pub at_ms: u64,
pub message: Msg,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -4787,8 +4933,24 @@ mod tests {
);
check(Guard::ALL, Guard::slug, Guard::from_slug);
check(RuleAction::ALL, RuleAction::slug, RuleAction::from_slug);
check(Permission::ALL, Permission::slug, Permission::from_slug);
check(OnError::ALL, OnError::slug, OnError::from_slug);
check(ReplyMode::ALL, ReplyMode::slug, ReplyMode::from_slug);
check(SettingKind::ALL, SettingKind::slug, SettingKind::from_slug);
check(PluginHook::ALL, PluginHook::slug, PluginHook::from_slug);
check(
PluginOutcome::ALL,
PluginOutcome::slug,
PluginOutcome::from_slug,
);
check(
PluginLogLevel::ALL,
PluginLogLevel::slug,
PluginLogLevel::from_slug,
);
assert_eq!(GroupKind::LoadBalance.slug(), "load-balance");
assert_eq!(Guard::InspectTools.slug(), "inspect_tools");
assert_eq!(Permission::ReplyToolCalls.slug(), "reply_tool_calls");
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions crates/tw-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub mod live;
pub mod models;
pub mod oauth;
pub mod outbound;
pub mod plugin;
pub mod probe;
pub mod quota;
pub mod quote;
Expand Down
137 changes: 137 additions & 0 deletions crates/tw-gateway/src/plugin/engine.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//! 网关要从插件运行时那里拿到的东西:把一份源码编成插件,读出它的 manifest。
//!
//! **这里是一道接缝**:沙箱在 `tw-plugin` 里,网关只认这个 trait。真的运行时接上之前,
//! 由 [`Unavailable`] 顶着 —— 每个插件都「加载不了」,有一个就拒一个请求(出错时
//! 拒绝是出厂的做法),而不是悄悄放过。测试拿一个假的引擎接在这里。

use std::sync::Arc;

use crate::plugin::host::PluginHost;
use crate::plugin::set::Scope;

/// 插件文件里 `manifest` 写的东西,加上它导出了哪些钩子。**由运行时读出来、校验过**:
/// 权限和钩子对得上、设置项不超过上限,这里拿到的都是合规的。
#[derive(Debug, Clone, PartialEq)]
pub struct Manifest {
/// 插件自己起的名字。**插件写的字**:界面当纯文本显示
pub name: String,
/// 插件接口的版本。只有 1
pub api: u32,
pub description: Option<String>,
/// 按 [`tw_api::Permission::ALL`] 的顺序,不重复
pub permissions: Vec<tw_api::Permission>,
/// 插件建议的范围。装上时照它填进配置,之后以配置为准
pub scope: Scope,
pub reply_mode: tw_api::ReplyMode,
/// 按插件写的顺序
pub settings: Vec<SettingSpec>,
pub hooks: Hooks,
}

/// 一个设置项。
#[derive(Debug, Clone, PartialEq)]
pub struct SettingSpec {
pub key: String,
pub kind: tw_api::SettingKind,
/// 插件写的字
pub label: String,
/// 和 `kind` 同一种类型
pub default: serde_json::Value,
}

/// 插件导出了哪些钩子。
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Hooks {
/// `onRequest`
pub request: bool,
/// `onReplyText`
pub reply_text: bool,
/// `onReplyTextEnd`,只在 stream 模式下有
pub reply_text_end: bool,
/// `onToolCall`
pub tool_call: bool,
}

impl Hooks {
/// 回答那一段有没有它的事
pub fn on_reply(&self) -> bool {
self.reply_text || self.tool_call
}
}

/// 编不成的原因。
#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum LoadError {
#[error("the plugin file is larger than the limit")]
TooLarge,
/// 语法错。行列从 1 起,运行时说得出来才有
#[error("{message}")]
Syntax {
message: String,
line: Option<u32>,
column: Option<u32>,
},
/// manifest 不合规矩:缺字段、权限和钩子对不上、设置项写错了……原话是运行时的
#[error("{0}")]
Manifest(String),
#[error("the plugin is written for plugin API {0}, and only API 1 is supported")]
UnsupportedApi(u32),
/// 运行时自己出了问题,或者根本没有运行时(见 [`Unavailable`])
#[error("{0}")]
Engine(String),
}

/// 插件运行时。**一个进程一个**,所有插件共用。
pub trait Engine: Send + Sync {
/// 用**正好这些字节**编一个插件(不变式 I9:跑的就是哈希过、比对过的那一份)。
fn load(&self, source: &[u8]) -> Result<Arc<dyn PluginHost>, LoadError>;
}

/// 没有运行时:每个插件都加载不了,原因就是 `reason`。
#[derive(Debug, Clone)]
pub struct Unavailable(pub String);

impl Default for Unavailable {
fn default() -> Self {
Self("the plugin engine is not available in this build".into())
}
}

impl Engine for Unavailable {
fn load(&self, _source: &[u8]) -> Result<Arc<dyn PluginHost>, LoadError> {
Err(LoadError::Engine(self.0.clone()))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn without_an_engine_every_plugin_fails_to_load_and_says_why() {
let e = Unavailable::default();
let Err(LoadError::Engine(why)) = e.load(b"export const manifest = {}") else {
panic!("the stand-in engine loaded something");
};
assert!(why.contains("not available"), "{why}");
}

#[test]
fn only_text_and_tool_call_hooks_make_a_plugin_part_of_the_reply() {
assert!(!Hooks::default().on_reply());
assert!(
!Hooks {
request: true,
..Default::default()
}
.on_reply()
);
assert!(
Hooks {
tool_call: true,
..Default::default()
}
.on_reply()
);
}
}
13 changes: 13 additions & 0 deletions crates/tw-gateway/src/plugin/host.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! 一个编好的插件。**数据面通过它跑钩子**(请求钩子、回答钩子),由运行时的适配层
//! 实现;测试有自己的替身。
//!
//! 这里先只有加载和展示要用的那两样,跑钩子的方法由数据面那一侧补上。

use crate::plugin::engine::Manifest;

pub trait PluginHost: Send + Sync {
/// 编译时读到的 manifest,校验过的
fn manifest(&self) -> &Manifest;
/// 编出它的那一份字节的 SHA-256(不变式 I9 比对的就是它)
fn sha256(&self) -> [u8; 32];
}
Loading
Loading