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
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ resolver = "2"
members = [
# ── 第一层 · 共用协议层(企业版也依赖)────────────────
"crates/tw-types",
"crates/tw-wire",
"crates/tw-protocol",
"crates/tw-provider",
"crates/tw-resil",
Expand Down Expand Up @@ -43,6 +44,7 @@ version = "0.29.0"
[workspace.dependencies]
# ── 内部 crate ───────────────────────────────────────────
tw-types = { path = "crates/tw-types" }
tw-wire = { path = "crates/tw-wire" }
tw-protocol = { path = "crates/tw-protocol" }
tw-provider = { path = "crates/tw-provider" }
tw-resil = { path = "crates/tw-resil" }
Expand Down
1 change: 1 addition & 0 deletions bin/twcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ anyhow = { workspace = true }
clap = { workspace = true }
tw-adopt = { workspace = true }
tw-scan = { workspace = true }
tw-wire = { workspace = true }
serde_yaml_ng = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions bin/twcore/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@ fn cmd_serve(path: &Path, port: Option<u16>, safe: bool, parent: Option<u32>) ->
//
// body 的通道在这里建:**它是唯一同时看得见网关和存储的地方**,
// 而两边各有各的同形结构,是为了不让「观测」挂到「转发」下面。
let (body_tx, body_rx) = tokio::sync::mpsc::channel(tw_gateway::bodies::CHANNEL_CAP);
let (body_tx, body_rx) = tokio::sync::mpsc::channel(tw_wire::bodies::CHANNEL_CAP);
let store = build_store(&dir, state.bus.clone(), state.pricing.clone(), body_rx);
if store.is_some() {
state.set_body_sink(body_tx);
Expand Down Expand Up @@ -951,7 +951,7 @@ fn build_store(
}
// 两边的 body 结构在这里对接。**一次移动,不复制** —— `Bytes` 的
// 克隆是引用计数。
let (tx, rx) = tokio::sync::mpsc::channel(tw_gateway::bodies::CHANNEL_CAP);
let (tx, rx) = tokio::sync::mpsc::channel(tw_wire::bodies::CHANNEL_CAP);
let mut bodies = bodies;
tokio::spawn(async move {
while let Some(b) = bodies.recv().await {
Expand Down
1 change: 1 addition & 0 deletions crates/tw-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ rand = { workspace = true }
uuid = { workspace = true }
tw-redact = { workspace = true }
tw-dialect = { workspace = true }
tw-wire = { workspace = true }
tw-scan = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
Expand Down
6 changes: 3 additions & 3 deletions crates/tw-gateway/src/ending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@

use std::time::Instant;

use crate::bodies::{BodyKind, BodyRecord, BodySender, ResponseTap};
use crate::usage::{Sniffer, Usage};
use tw_types::{Msg, msg};
use tw_wire::bodies::{BodyKind, BodyRecord, BodySender, ResponseTap};
use tw_wire::usage::{Sniffer, Usage};

/// 一个还欠着结局的请求。
///
Expand Down Expand Up @@ -136,7 +136,7 @@ impl Ending {
self.told = true;
let (recorded, original_len) = std::mem::take(&mut self.tap).finish();
if !recorded.is_empty() {
crate::bodies::offer(
tw_wire::bodies::offer(
&self.sink,
BodyRecord {
id: self.id,
Expand Down
2 changes: 1 addition & 1 deletion crates/tw-gateway/src/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn extract(request: &Recorded, response: &Recorded) -> Extracted {
}

// ---- 响应侧:同样复用线上那个嗅探器
let mut sniffer = crate::usage::Sniffer::new();
let mut sniffer = tw_wire::usage::Sniffer::new();
sniffer.feed(response.body.as_bytes());
out.usage = sniffer
.finish()
Expand Down
5 changes: 1 addition & 4 deletions crates/tw-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

pub mod access;
pub mod auth;
pub mod bodies;
pub mod chatgpt;
pub mod client_api;
pub mod clientprobe;
Expand All @@ -33,11 +32,9 @@ pub mod server;
pub mod session;
pub mod toolwall;
pub mod translate;
pub mod usage;
pub mod ws;

pub use access::{AllowList, Cidr};
pub use bodies::{BodyKind, BodyRecord, BodySender};
pub use clientprobe::{ProbeKind, classify};
pub use error::GatewayError;
pub use health::Health;
Expand All @@ -52,7 +49,7 @@ pub use probe::{ModelList, ProbeResult, probe};
pub use quota::{Quota, from_headers as quota_from_headers};
pub use quote::Quote;
pub use server::{AppState, Runtime, client_for_provider, router, serve};
pub use usage::{Sniffer, Usage};
pub use tw_wire::{BodyKind, BodyRecord, BodySender, Sniffer, Usage};

/// 请求来自谁。**如实写 ThinkWatch** —— 我们从不把自己报成别的客户端。
pub const ORIGINATOR: &str = "thinkwatch";
Expand Down
12 changes: 6 additions & 6 deletions crates/tw-gateway/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ pub struct AppState {
/// **有界通道,满了就丢。**直接调用意味着文件 I/O 跑在转发那条路上
/// —— 一次慢磁盘写就变成一次慢请求,而观测永远不该有这个权力。
/// `None` 表示观测层没起来,那时什么都不做。
body_sink: Arc<std::sync::Mutex<Option<crate::bodies::BodySender>>>,
body_sink: Arc<std::sync::Mutex<Option<tw_wire::bodies::BodySender>>>,
/// 每个上游最近一次报的订阅额度。
///
/// **在内存里,不落库。**它是「现在还剩多少」,不是历史 —— 存一份
Expand Down Expand Up @@ -828,7 +828,7 @@ impl AppState {

/// 接上 body 的去处。**观测层起来之后才调** —— 在那之前 body 一律
/// 丢掉,而请求照常。
pub fn set_body_sink(&self, tx: crate::bodies::BodySender) {
pub fn set_body_sink(&self, tx: tw_wire::bodies::BodySender) {
if let Ok(mut g) = self.body_sink.lock() {
*g = Some(tx);
}
Expand Down Expand Up @@ -875,7 +875,7 @@ impl AppState {
}
}

fn body_sink(&self) -> Option<crate::bodies::BodySender> {
fn body_sink(&self) -> Option<tw_wire::bodies::BodySender> {
self.body_sink.lock().ok().and_then(|g| g.clone())
}

Expand Down Expand Up @@ -1690,12 +1690,12 @@ async fn pipeline(
// 请求体交给观测层。**这时候它已经完整在内存里了**,所以这一步
// 除了一次 `Bytes` 的引用计数之外没有别的成本(说过入站是要
// 整个解析的,所以本来就在)。
crate::bodies::offer(
tw_wire::bodies::offer(
&sink,
crate::bodies::BodyRecord {
tw_wire::bodies::BodyRecord {
id,
at_ms,
kind: crate::bodies::BodyKind::Request,
kind: tw_wire::bodies::BodyKind::Request,
body: body.clone(),
original_len: body.len(),
},
Expand Down
18 changes: 18 additions & 0 deletions crates/tw-wire/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "tw-wire"
version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
repository.workspace = true
description = "Sideband readers for a streaming response: usage sniffing and body capture"

[dependencies]
bytes = { workspace = true }
serde_json = { workspace = true }
# 只要一个 mpsc 发送端。**不用 workspace 那份** —— 那份带着网络、信号、
# 计时器,而这个 crate 要能被企业版直接依赖,背什么进去是它说了算
tokio = { version = "1", default-features = false, features = ["sync"] }

[dev-dependencies]
tokio = { version = "1", features = ["rt", "macros"] }
File renamed without changes.
17 changes: 17 additions & 0 deletions crates/tw-wire/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! 响应流过时顺带做的两件事。
//!
//! 两者共享同一条纪律:**不缓冲**。响应是流着出去的,为了看一眼它而把
//! 它整块攒下来,会把 SSE 变成一次性交付 —— 客户端看到的是「卡很久,然后
//! 一下全出来」。所以这里的东西都是旁路的:字节照常流向客户端,同时喂
//! 它们一份。
//!
//! [`usage`] 嗅出上游报的用量,[`bodies`] 留档请求和响应的正文。
//!
//! 这一层刻意零业务依赖 —— 它不认识配置从哪来、数据存哪里、谁在调用,
//! 所以企业版和桌面版都能直接用。

pub mod bodies;
pub mod usage;

pub use bodies::{BodyKind, BodyRecord, BodySender, ResponseTap};
pub use usage::{Sniffer, Usage};
File renamed without changes.
Loading