diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 67ad8c00..753d9808 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -62,6 +62,9 @@ jobs: - uses: dtolnay/rust-toolchain@89b12181fb390509a0842a86cc55eeb8eb928c1d # stable with: toolchain: stable + targets: wasm32-unknown-unknown + - name: Install Core WebAssembly build tools + run: sudo apt-get update && sudo apt-get install -y clang llvm # Save only on pushes (dev, main). A PR can read its own caches, its # base branch's and the default branch's, never another PR's, so a cache # saved on a PR serves only a later push to that same PR, while taking as @@ -163,6 +166,9 @@ jobs: - uses: dtolnay/rust-toolchain@89b12181fb390509a0842a86cc55eeb8eb928c1d # stable with: toolchain: stable + targets: wasm32-unknown-unknown + - name: Install Core WebAssembly build tools + run: sudo apt-get update && sudo apt-get install -y clang llvm - uses: Swatinem/rust-cache@6323deb102c322ba6fcbdcafc7e3dddab59af2b6 # v2.9.2 with: # See the `rust` job. diff --git a/CHANGELOG.md b/CHANGELOG.md index 8eb5de1d..fe3a1d3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,38 @@ target. ## [Unreleased] +## [3.12.0] — 2026-11-18 + +ThinkWatch adds hosted web tools and Codex model discovery to the enterprise +gateway. Administrators manage both features in the console. + +### Added + +- Enable hosted web search, page reading, and find-in-page for selected API + keys. Choose search sources and their order, set domain rules, and test the + saved configuration with results, timings, and diagnostics. +- Use the Codex connection guide to copy a gateway profile. The native model + catalog respects each key's model permissions and requests upstream + metadata for the client's Codex version. +- Configure model aliases and per-provider capabilities. Alias names are + checked against the model catalog, and alias targets need an active route. + +### Changed + +- Each client turn consumes one request from the user's and API key's limits. + Hosted model rounds continue to account for actual model usage. Previous web + results remain usable after hosted execution is turned off. +- Concurrent edits to hosted web settings and model discovery produce a + conflict message and preserve the console draft. +- Request body admission queues uploads in arrival order and returns a + retryable `429` when admission expires. The gateway also adapts multi-turn + Chat reasoning history and negotiates upstream streaming for eligible + HTTP requests. +- Shared ThinkWatch Core libraries move to `v0.98.1`, bringing typed + Chat/Mistral responses and improvements to secrets detection and streaming + restoration. Source builds require Rust 1.96 or newer, the + `wasm32-unknown-unknown` target, and Clang and LLVM tools. + ## [3.11.0] — 2026-11-03 Execution and buffering now adapt automatically to available CPUs and memory, @@ -2856,7 +2888,8 @@ unreleased builds should: stop the gateway, run `db/schema.sql` against PostgreSQL, restart against this tag. The schema is idempotent end-to-end, so the apply is safe to repeat. -[Unreleased]: https://github.com/ThinkWatchProject/ThinkWatch/compare/v3.11.0...HEAD +[Unreleased]: https://github.com/ThinkWatchProject/ThinkWatch/compare/v3.12.0...HEAD +[3.12.0]: https://github.com/ThinkWatchProject/ThinkWatch/releases/tag/v3.12.0 [3.11.0]: https://github.com/ThinkWatchProject/ThinkWatch/releases/tag/v3.11.0 [3.10.0]: https://github.com/ThinkWatchProject/ThinkWatch/releases/tag/v3.10.0 [3.9.1]: https://github.com/ThinkWatchProject/ThinkWatch/releases/tag/v3.9.1 diff --git a/Cargo.lock b/Cargo.lock index fcd752a6..878a33cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "addr2line" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59317f77929f0e679d39364702289274de2f0f0b22cbf50b2b8cff2169a0b27a" +dependencies = [ + "gimli", +] + [[package]] name = "adler2" version = "2.0.1" @@ -52,6 +61,21 @@ dependencies = [ "memchr", ] +[[package]] +name = "alloc-no-stdlib" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2fb6cfd47bf496ff64095c20eaba0c201404ee38714d4142fcfa1dc334fcc7a" + +[[package]] +name = "alloc-stdlib" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b5c1865780388bfa186411ab5f247819487fc4864c6e9c3106611fa347586e1" +dependencies = [ + "alloc-no-stdlib", +] + [[package]] name = "allocator-api2" version = "0.2.21" @@ -73,6 +97,12 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "arbitrary" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3bc62ac97cc33321f50863d514c3bc38a453947a8f9e781137e47c7401020aed" + [[package]] name = "arc-swap" version = "1.9.2" @@ -94,6 +124,12 @@ dependencies = [ "password-hash", ] +[[package]] +name = "arraydeque" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236" + [[package]] name = "arrayvec" version = "0.7.8" @@ -473,6 +509,19 @@ dependencies = [ "digest 0.11.3", ] +[[package]] +name = "blake3" +version = "1.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d9e454fc11f76977dc803893aff6304ed33d6a26efae8696573bea74baa27ae" +dependencies = [ + "arrayvec", + "cc", + "cfg-if", + "constant_time_eq", + "cpufeatures 0.3.1", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -521,6 +570,27 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "brotli" +version = "9.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9071f285ed78f04f37d675abaaef5de7689e2f30a216c8dc6795465d81910a13" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", + "brotli-decompressor", +] + +[[package]] +name = "brotli-decompressor" +version = "6.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc031cd87c6529082b0b65c604e86af0ed7744841b17ccfe60b1e31fa606179e" +dependencies = [ + "alloc-no-stdlib", + "alloc-stdlib", +] + [[package]] name = "bs58" version = "0.5.1" @@ -544,6 +614,9 @@ name = "bumpalo" version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +dependencies = [ + "allocator-api2", +] [[package]] name = "byteorder" @@ -703,6 +776,15 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "combine" version = "4.6.8" @@ -719,9 +801,12 @@ version = "0.4.45" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "98fc98460ba0ad5317075d3632b8dfc45d0be8c4a49347c2a38272019717614a" dependencies = [ + "brotli", "compression-core", "flate2", "memchr", + "zstd", + "zstd-safe", ] [[package]] @@ -815,6 +900,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" +[[package]] +name = "cpp_demangle" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0667304c32ea56cb4cd6d2d7c0cfe9a2f8041229db8c033af7f8d69492429def" +dependencies = [ + "cfg-if", +] + [[package]] name = "cpubits" version = "0.1.1" @@ -839,6 +933,152 @@ dependencies = [ "libc", ] +[[package]] +name = "cranelift-assembler-x64" +version = "0.136.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aecefa88696b62f833cc0c997d19c93246b0268e2aaf23e0e8692e527f6de088" +dependencies = [ + "cranelift-assembler-x64-meta", +] + +[[package]] +name = "cranelift-assembler-x64-meta" +version = "0.136.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1391a2546f5edf06a0ff5e9430d73c2713fd536aefec53200dc21846d1467f53" +dependencies = [ + "cranelift-srcgen", +] + +[[package]] +name = "cranelift-bforest" +version = "0.136.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01b3675b13214270fddbd257d5fbf4109be8b02d92fc636fe96e2829514a7235" +dependencies = [ + "cranelift-entity", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-bitset" +version = "0.136.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c1d67aa02b6e174bd2b165c113e94af9498fcc20e2892d0f4d5ab8c9618d09" +dependencies = [ + "serde", + "serde_derive", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-codegen" +version = "0.136.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26fe6f3ec5344c32602c414e986dcbe699fa501a3e3e7b02f887d9d12b14ae42" +dependencies = [ + "bumpalo", + "cranelift-assembler-x64", + "cranelift-bforest", + "cranelift-bitset", + "cranelift-codegen-meta", + "cranelift-codegen-shared", + "cranelift-control", + "cranelift-entity", + "cranelift-isle", + "gimli", + "hashbrown 0.17.1", + "libm", + "log", + "postcard", + "pulley-interpreter", + "regalloc2", + "rustc-hash", + "serde", + "serde_derive", + "sha2 0.10.9", + "smallvec", + "target-lexicon", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-codegen-meta" +version = "0.136.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96c5e6323d458486d95781defdb5016a1176fc911d8bc8e64b696adbda6e9044" +dependencies = [ + "cranelift-assembler-x64-meta", + "cranelift-codegen-shared", + "cranelift-srcgen", + "heck", + "pulley-interpreter", +] + +[[package]] +name = "cranelift-codegen-shared" +version = "0.136.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6595667b7d505cf85d5bdd014e6f57ad6128183238f8e372319d9384782472f5" + +[[package]] +name = "cranelift-control" +version = "0.136.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26ffdbfcd24c3833d730849e6d132af34a400a2b89b44446015c01eecbea21d" +dependencies = [ + "arbitrary", +] + +[[package]] +name = "cranelift-entity" +version = "0.136.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8c071188cb1c3b1803e23ae415a8d5f58d741d3a4b26a79b83dd61921112eac" +dependencies = [ + "cranelift-bitset", + "serde", + "serde_derive", + "wasmtime-internal-core", +] + +[[package]] +name = "cranelift-frontend" +version = "0.136.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2b85ba0a55d7685117b275fe6f36c819d438862e4223d28f0b97902460ff0ac" +dependencies = [ + "cranelift-codegen", + "hashbrown 0.17.1", + "log", + "smallvec", + "target-lexicon", +] + +[[package]] +name = "cranelift-isle" +version = "0.136.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20ed364edcaed769da7b2709d540f8f7d0ff257749ba193f475e22dba6a0178b" + +[[package]] +name = "cranelift-native" +version = "0.136.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418d98c3dce08f0d13526692bbd1888bff4c5f45207f5093d168b5a224fbc88c" +dependencies = [ + "cranelift-codegen", + "libc", + "target-lexicon", +] + +[[package]] +name = "cranelift-srcgen" +version = "0.136.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36508559c8839a8b6ee091f9deaa9cfe620a82f1abb5e358a0a0c65290583622" + [[package]] name = "crc" version = "3.4.0" @@ -869,6 +1109,16 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + [[package]] name = "crossbeam-epoch" version = "0.9.21" @@ -948,6 +1198,29 @@ dependencies = [ "rand_core 0.10.1", ] +[[package]] +name = "cssparser" +version = "0.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11119743ad110e8c1bdccd930d7f5c30c99e5fc76a7b63ec9807e84eef0c5f59" +dependencies = [ + "cssparser-macros", + "dtoa-short", + "itoa", + "phf", + "smallvec", +] + +[[package]] +name = "cssparser-macros" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d045de693cb712d0b22c6a64be5b953f67b3ce00ab5ad3dd5d8b441886ab8e1a" +dependencies = [ + "quote", + "syn 3.0.6", +] + [[package]] name = "ctr" version = "0.10.1" @@ -1114,6 +1387,27 @@ dependencies = [ "serde_core", ] +[[package]] +name = "derive_more" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d751e9e49156b02b44f9c1815bcb94b984cdcc4396ecc32521c739452808b134" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "799a97264921d8623a957f6c3b9011f3b5492f557bbb7a5a19b7fa6d06ba8dcb" +dependencies = [ + "proc-macro2", + "quote", + "rustc_version", + "syn 2.0.119", +] + [[package]] name = "digest" version = "0.10.7" @@ -1164,6 +1458,21 @@ version = "0.15.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" +[[package]] +name = "dtoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c3cf4824e2d5f025c7b531afcb2325364084a16806f6d47fbc1f5fbd9960590" + +[[package]] +name = "dtoa-short" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd1511a7b6a56299bd043a9c167a6d2bfb37bf84a6dfceaba651168adfb43c87" +dependencies = [ + "dtoa", +] + [[package]] name = "dunce" version = "1.0.5" @@ -1229,6 +1538,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "ego-tree" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7bd50d24b1b75aa817bb0339cb9f2e0e664bdfab1e160a69a0fda9145b77fab2" + [[package]] name = "either" version = "1.18.0" @@ -1280,6 +1595,18 @@ dependencies = [ "zeroize", ] +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + [[package]] name = "encoding_rs" version = "0.8.42" @@ -1602,6 +1929,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "getopts" +version = "0.2.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfe4fbac503b8d1f88e6676011885f34b7174f46e59956bba534ba83abded4df" +dependencies = [ + "unicode-width", +] + [[package]] name = "getrandom" version = "0.2.17" @@ -1650,6 +1986,18 @@ dependencies = [ "polyval", ] +[[package]] +name = "gimli" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf7f043f89559805f8c7cacc432749b2fa0d0a0a9ee46ce47164ed5ba7f126c" +dependencies = [ + "fnv", + "hashbrown 0.16.1", + "indexmap 2.14.2", + "stable_deref_trait", +] + [[package]] name = "group" version = "0.13.0" @@ -1734,6 +2082,8 @@ dependencies = [ "allocator-api2", "equivalent", "foldhash", + "serde", + "serde_core", ] [[package]] @@ -1799,6 +2149,17 @@ dependencies = [ "digest 0.11.3", ] +[[package]] +name = "html5ever" +version = "0.40.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456a1a377e608e555d22ddab27ac0114bc7a7b4199078108e34c2aeae6c9b130" +dependencies = [ + "log", + "markup5ever", + "memchr", +] + [[package]] name = "http" version = "1.5.0" @@ -1965,6 +2326,7 @@ checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb" dependencies = [ "displaydoc", "litemap", + "serde", "tinystr", "writeable", "zerovec", @@ -2076,6 +2438,26 @@ dependencies = [ "serde_core", ] +[[package]] +name = "inotify" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cc00ea907cab49550b7da656f80ebb97be1b997d931fbcd28d39734e17ce592" +dependencies = [ + "bitflags 2.13.2", + "inotify-sys", + "libc", +] + +[[package]] +name = "inotify-sys" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c033f80b2c113cdf91ab7a33faa9cbc014726dcad99880c8609af2a370edf37d" +dependencies = [ + "libc", +] + [[package]] name = "inout" version = "0.2.2" @@ -2100,6 +2482,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.18" @@ -2255,27 +2646,53 @@ dependencies = [ ] [[package]] -name = "lazy_static" -version = "1.5.1" +name = "kqueue" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" +checksum = "8d763e5b24120b4ddf50de6c92308156765aabfbbccebf401da7cff2d70a41ea" dependencies = [ - "spin", + "kqueue-sys", + "libc", ] [[package]] -name = "left-right" -version = "0.11.8" +name = "kqueue-sys" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bc015ded5d9b3054dbbdb63332cdd6ee42352ccef19e911e25117490e2f48ee" +checksum = "07293a4e297ac234359b510362495713f75ea345d5307140414f20c69ffeb087" dependencies = [ - "crossbeam-utils", - "loom", - "slab", + "bitflags 2.13.2", + "libc", ] [[package]] -name = "libc" +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" +dependencies = [ + "spin", +] + +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + +[[package]] +name = "left-right" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bc015ded5d9b3054dbbdb63332cdd6ee42352ccef19e911e25117490e2f48ee" +dependencies = [ + "crossbeam-utils", + "loom", + "slab", +] + +[[package]] +name = "libc" version = "0.2.190" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce5d3ddc6d3fa000eb1536d85e147bfe31aacaba692ed6a876f95cb7c855be78" @@ -2363,6 +2780,23 @@ version = "0.11.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "373f5eceeeab7925e0c1098212f2fbc4d416adec9d35051a6ab251e824c1854a" +[[package]] +name = "mach2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b" + +[[package]] +name = "markup5ever" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ab3dc68ac4a0f5719e560136778c1ee716e296030d75dbd4484e37e39e3a842" +dependencies = [ + "log", + "tendril", + "web_atoms", +] + [[package]] name = "matchers" version = "0.2.0" @@ -2394,6 +2828,15 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "memfd" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57804b2c9b69967f1536a56f86297e367a33b19e98852ed624b84551cdbc0d90" +dependencies = [ + "rustix", +] + [[package]] name = "metrics" version = "0.24.6" @@ -2484,6 +2927,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" dependencies = [ "libc", + "log", "wasi", "windows-sys 0.61.2", ] @@ -2494,6 +2938,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d" +[[package]] +name = "new_debug_unreachable" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" + [[package]] name = "nom" version = "7.1.3" @@ -2504,6 +2954,34 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "notify" +version = "8.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c41a04a5f59df9322cc657286dd2d79f90816119b22c5f847607c850110205ab" +dependencies = [ + "bitflags 2.13.2", + "inotify", + "kqueue", + "libc", + "log", + "mio", + "notify-types", + "objc2-core-foundation", + "objc2-core-services", + "walkdir", + "windows-sys 0.61.2", +] + +[[package]] +name = "notify-types" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbd044e85df3d939d8e32a1c35a3b737c7d0a4eeb7acd737bd207644e00f4e5f" +dependencies = [ + "bitflags 2.13.2", +] + [[package]] name = "ntapi" version = "0.4.3" @@ -2622,6 +3100,16 @@ dependencies = [ "bitflags 2.13.2", ] +[[package]] +name = "objc2-core-services" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "583300ad934cba24ff5292aee751ecc070f7ca6b39a574cc21b7b5e588e06a0b" +dependencies = [ + "libc", + "objc2-core-foundation", +] + [[package]] name = "objc2-io-kit" version = "0.3.2" @@ -2632,6 +3120,18 @@ dependencies = [ "objc2-core-foundation", ] +[[package]] +name = "object" +version = "0.40.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd229a0361b9d0d4396176e02d65897f487eebeab7caa6d443855ee152ca0b9c" +dependencies = [ + "crc32fast", + "hashbrown 0.17.1", + "indexmap 2.14.2", + "memchr", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -2650,7 +3150,7 @@ dependencies = [ "ed25519-dalek", "hmac 0.12.1", "http", - "itertools", + "itertools 0.10.5", "log", "oauth2", "p256 0.13.2", @@ -2811,6 +3311,59 @@ dependencies = [ "getrandom 0.4.3", ] +[[package]] +name = "phf" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "010378780309880b08997fae13be7834dba947d36393bd372f2b1556deb2a2f6" +dependencies = [ + "phf_macros", + "phf_shared", + "serde", +] + +[[package]] +name = "phf_codegen" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b585a510fb76fdebead6897982ef2a03a21d8e6cbcca904999742a4afc6ffe" +dependencies = [ + "phf_generator", + "phf_shared", +] + +[[package]] +name = "phf_generator" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aeb62e0959d5a1bebc965f4d15d9e2b7cea002b6b0f5ba8cde6cc26738467100" +dependencies = [ + "fastrand", + "phf_shared", +] + +[[package]] +name = "phf_macros" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fa8d0ca26d424d27630da600c6624696e7dec8bf7b3b492b383c5dc49e5e085" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "phf_shared" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6fd9027e2d9319be6349febd1db4e8d02aa544921200c9b777720ac34a3aa89" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project-lite" version = "0.2.17" @@ -2886,6 +3439,18 @@ dependencies = [ "portable-atomic", ] +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "serde", +] + [[package]] name = "potential_utf" version = "0.1.6" @@ -2910,6 +3475,12 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "precomputed-hash" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" + [[package]] name = "primefield" version = "0.14.0" @@ -2980,6 +3551,29 @@ dependencies = [ "psl-types", ] +[[package]] +name = "pulley-interpreter" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d74df9e20cba0f6dd07f01c3140535a6b23e40aecccfcacfb921621b41d0acb" +dependencies = [ + "cranelift-bitset", + "log", + "pulley-macros", + "wasmtime-internal-core", +] + +[[package]] +name = "pulley-macros" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8415513bfa407c06de6f11dbe9437f2352e075a5520ff491a131166d3d90aed1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "quanta" version = "0.12.6" @@ -2995,6 +3589,15 @@ dependencies = [ "winapi", ] +[[package]] +name = "quick-xml" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41b1177fdf999d2321d3fb46ff47159d9c1fb9ad66a4879f8c50a0b504615e9b" +dependencies = [ + "memchr", +] + [[package]] name = "quinn" version = "0.11.12" @@ -3008,7 +3611,7 @@ dependencies = [ "quinn-udp", "rustc-hash", "rustls", - "socket2 0.5.10", + "socket2 0.6.5", "thiserror 2.0.21", "tokio", "tracing", @@ -3047,7 +3650,7 @@ dependencies = [ "cfg_aliases", "libc", "once_cell", - "socket2 0.5.10", + "socket2 0.6.5", "tracing", "windows-sys 0.61.2", ] @@ -3185,6 +3788,26 @@ dependencies = [ "bitflags 2.13.2", ] +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redis-protocol" version = "6.0.0" @@ -3228,6 +3851,21 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "regalloc2" +version = "0.15.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34f923657880712029496c2e68bb40bef163806b2011adde559b28bbcedc82ea" +dependencies = [ + "allocator-api2", + "bumpalo", + "hashbrown 0.17.1", + "log", + "rustc-hash", + "serde", + "smallvec", +] + [[package]] name = "regex" version = "1.13.1" @@ -3326,6 +3964,7 @@ dependencies = [ "rustls-platform-verifier", "serde", "serde_json", + "serde_urlencoded", "sync_wrapper", "tokio", "tokio-rustls", @@ -3411,6 +4050,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "rustc-demangle" +version = "0.1.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b74b56ffa8bb2830709a538c2cbcae9aa062db0d2a42563bfb09bdaae44020eb" + [[package]] name = "rustc-hash" version = "2.1.3" @@ -3537,6 +4182,16 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "saphyr-parser" +version = "0.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0b1b76852c5643f558e7347e4de549474c3ecb7154295a189f4668428c1b453" +dependencies = [ + "arraydeque", + "thiserror 2.0.21", +] + [[package]] name = "schannel" version = "0.1.29" @@ -3582,6 +4237,20 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "scraper" +version = "0.29.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6844b5f49a5579f12ba4f89d9f7eec6d71889e4aa0bc7181821040ce66f08fff" +dependencies = [ + "cssparser", + "ego-tree", + "getopts", + "html5ever", + "precomputed-hash", + "selectors", +] + [[package]] name = "sec1" version = "0.7.3" @@ -3633,11 +4302,32 @@ dependencies = [ "libc", ] +[[package]] +name = "selectors" +version = "0.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8322aebea94a6cc288ff884e8a49bdbceef60206c400c6c46d22e62fc4fd91a6" +dependencies = [ + "bitflags 2.13.2", + "cssparser", + "derive_more", + "log", + "new_debug_unreachable", + "precomputed-hash", + "rustc-hash", + "servo_arc", + "smallvec", +] + [[package]] name = "semver" version = "1.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a7852d02fc848982e0c167ef163aaff9cd91dc640ba85e263cb1ce46fae51cd" +dependencies = [ + "serde", + "serde_core", +] [[package]] name = "serde" @@ -3791,6 +4481,15 @@ dependencies = [ "serde", ] +[[package]] +name = "servo_arc" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4eceb519ec34d8290aaf6e46f435102f2bfa63f22b05a144fb16ab266d3b198" +dependencies = [ + "stable_deref_trait", +] + [[package]] name = "sha1" version = "0.10.7" @@ -3920,6 +4619,12 @@ dependencies = [ "time", ] +[[package]] +name = "siphasher" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33f4fe9184a62d842c9ef383018f3306d8ba224fd9d836f56d7288308847c256" + [[package]] name = "sketches-ddsketch" version = "0.3.1" @@ -4178,6 +4883,30 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" +[[package]] +name = "string_cache" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ffa8a5dbe8b3f0bbe29d4c3225daafaeead63afdc1b65fc4c01a1384166038e6" +dependencies = [ + "new_debug_unreachable", + "parking_lot", + "phf_shared", + "precomputed-hash", +] + +[[package]] +name = "string_cache_codegen" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "928dcdf75e47626b3617a976ec205d9f057584c371c1f23b782129268d0e6edc" +dependencies = [ + "phf_generator", + "phf_shared", + "proc-macro2", + "quote", +] + [[package]] name = "stringprep" version = "0.1.5" @@ -4243,6 +4972,15 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "sys-locale" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8eab9a99a024a169fe8a903cf9d4a3b3601109bcc13bd9e3c6fff259138626c4" +dependencies = [ + "libc", +] + [[package]] name = "sysinfo" version = "0.36.1" @@ -4278,6 +5016,12 @@ dependencies = [ "libc", ] +[[package]] +name = "target-lexicon" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c447be3a2abfa84b2d91680c56600bd6b2873866de27b36ec911be705e291da" + [[package]] name = "tempfile" version = "3.27.0" @@ -4291,9 +5035,27 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "tendril" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fed54709c5b3a53d09bb1c113ea4f5ceafd1e772ddcb0030a82e1d56c087b08" +dependencies = [ + "new_debug_unreachable", +] + +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "think-watch-auth" -version = "3.11.0" +version = "3.12.0" dependencies = [ "anyhow", "argon2", @@ -4323,7 +5085,7 @@ dependencies = [ [[package]] name = "think-watch-common" -version = "3.11.0" +version = "3.12.0" dependencies = [ "aes-gcm", "anyhow", @@ -4372,7 +5134,7 @@ dependencies = [ [[package]] name = "think-watch-gateway" -version = "3.11.0" +version = "3.12.0" dependencies = [ "anyhow", "arc-swap", @@ -4382,6 +5144,7 @@ dependencies = [ "chrono", "fred", "futures", + "hex", "http", "http-body-util", "hyper", @@ -4393,6 +5156,7 @@ dependencies = [ "rust_decimal", "serde", "serde_json", + "sha2 0.11.0", "sqlx", "think-watch-auth", "think-watch-common", @@ -4400,10 +5164,14 @@ dependencies = [ "tokio", "tokio-stream", "tracing", + "tw-api", "tw-bedrock", "tw-breaker", + "tw-config", "tw-dialect", + "tw-gateway", "tw-guard", + "tw-types", "utoipa", "uuid", "xxhash-rust", @@ -4411,7 +5179,7 @@ dependencies = [ [[package]] name = "think-watch-mcp-gateway" -version = "3.11.0" +version = "3.12.0" dependencies = [ "anyhow", "arc-swap", @@ -4440,7 +5208,7 @@ dependencies = [ [[package]] name = "think-watch-server" -version = "3.11.0" +version = "3.12.0" dependencies = [ "anyhow", "arc-swap", @@ -4480,6 +5248,7 @@ dependencies = [ "tower-http 0.7.1", "tracing", "tracing-subscriber", + "tw-api", "tw-bedrock", "tw-breaker", "tw-dialect", @@ -4493,7 +5262,7 @@ dependencies = [ [[package]] name = "think-watch-test-support" -version = "3.11.0" +version = "3.12.0" dependencies = [ "anyhow", "async-stream", @@ -4622,6 +5391,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643" dependencies = [ "displaydoc", + "serde_core", "zerovec", ] @@ -4681,7 +5451,19 @@ dependencies = [ [[package]] name = "tokio-tungstenite" -version = "0.29.0" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d25a406cddcc431a75d3d9afc6a7c0f7428d4891dd973e4d54c56b46127bf857" +dependencies = [ + "futures-util", + "log", + "tokio", + "tungstenite 0.28.0", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f72a05e828585856dacd553fba484c242c46e391fb0e58917c942ee9202915c" dependencies = [ @@ -4787,12 +5569,17 @@ version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4cfcf7e2740e6fc6d4d688b4ef00650406bb94adf4731e43c096c3a19fe40840" dependencies = [ + "async-compression", "bitflags 2.13.2", "bytes", + "futures-core", "futures-util", "http", "http-body", + "http-body-util", "pin-project-lite", + "tokio", + "tokio-util", "tower", "tower-layer", "tower-service", @@ -4916,6 +5703,23 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" +[[package]] +name = "tungstenite" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8628dcc84e5a09eb3d8423d6cb682965dea9133204e8fb3efee74c2a0c259442" +dependencies = [ + "bytes", + "data-encoding", + "http", + "httparse", + "log", + "rand 0.9.5", + "sha1 0.10.7", + "thiserror 2.0.21", + "utf-8", +] + [[package]] name = "tungstenite" version = "0.29.0" @@ -4948,10 +5752,21 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "tw-api" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "serde", + "serde_json", + "tw-guard", + "tw-types", +] + [[package]] name = "tw-bedrock" -version = "0.89.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.89.0#c66a3b04ba297f608f2fc21501c73f21a78ab8ed" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" dependencies = [ "aws-credential-types", "aws-sigv4", @@ -4965,26 +5780,119 @@ dependencies = [ [[package]] name = "tw-breaker" -version = "0.89.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.89.0#c66a3b04ba297f608f2fc21501c73f21a78ab8ed" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "serde", +] + +[[package]] +name = "tw-config" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" dependencies = [ + "blake3", + "icu_locale_core", + "libc", + "rand 0.10.3", + "regex", "serde", + "serde_yaml_ng", + "sys-locale", + "thiserror 2.0.21", + "tokio", + "tw-api", + "tw-bedrock", + "tw-engine", + "tw-guard", + "tw-pricing", + "tw-secret", + "tw-types", + "tw-watch", + "tw-yaml", + "url", + "windows-sys 0.61.2", ] [[package]] name = "tw-dialect" -version = "0.89.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.89.0#c66a3b04ba297f608f2fc21501c73f21a78ab8ed" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" dependencies = [ "memchr", "serde", "serde_json", ] +[[package]] +name = "tw-engine" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "chrono", + "serde", + "serde_json", + "thiserror 2.0.21", + "tracing", + "tw-dialect", + "tw-types", +] + +[[package]] +name = "tw-gateway" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "arc-swap", + "async-stream", + "axum", + "base64 0.22.1", + "blake3", + "bytes", + "chrono", + "flate2", + "futures", + "http", + "http-body-util", + "hyper", + "hyper-util", + "memchr", + "quick-xml", + "rand 0.10.3", + "reqwest 0.13.5", + "rustls", + "rustls-platform-verifier", + "scraper", + "serde", + "serde_json", + "sha2 0.11.0", + "sysinfo", + "thiserror 2.0.21", + "tokio", + "tokio-rustls", + "tokio-tungstenite 0.28.0", + "tracing", + "tw-api", + "tw-bedrock", + "tw-breaker", + "tw-config", + "tw-dialect", + "tw-engine", + "tw-guard", + "tw-observe", + "tw-plugin", + "tw-pricing", + "tw-secret", + "tw-types", + "url", + "uuid", + "zstd", +] + [[package]] name = "tw-guard" -version = "0.89.0" -source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.89.0#c66a3b04ba297f608f2fc21501c73f21a78ab8ed" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" dependencies = [ "aho-corasick", "base64 0.22.1", @@ -5002,6 +5910,83 @@ dependencies = [ "tw-dialect", ] +[[package]] +name = "tw-observe" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "tokio", + "tracing", + "tw-api", +] + +[[package]] +name = "tw-plugin" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "libc", + "rand 0.10.3", + "serde", + "serde_json", + "sha2 0.11.0", + "thiserror 2.0.21", + "wasm-encoder", + "wasmparser", + "wasmtime", + "windows-sys 0.61.2", +] + +[[package]] +name = "tw-pricing" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "arc-swap", + "flate2", + "serde", + "serde_json", + "thiserror 2.0.21", + "tw-types", +] + +[[package]] +name = "tw-secret" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "thiserror 2.0.21", +] + +[[package]] +name = "tw-types" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "serde", +] + +[[package]] +name = "tw-watch" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "libc", + "notify", + "thiserror 2.0.21", + "tokio", +] + +[[package]] +name = "tw-yaml" +version = "0.98.1" +source = "git+https://github.com/ThinkWatchProject/ThinkWatch-Core.git?tag=v0.98.1#df1022c4d214d58c9774ce32a092854e345ea51e" +dependencies = [ + "saphyr-parser", + "thiserror 2.0.21", + "tw-types", +] + [[package]] name = "typed-path" version = "0.12.3" @@ -5047,6 +6032,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7df058c713841ad818f1dc5d3fd88063241cc61f49f5fbea4b951e8cf5a8d71d" +[[package]] +name = "unicode-width" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca718471876992059f251e33aafc334346ada08042b7661dd1fe313a9bced557" + [[package]] name = "universal-hash" version = "0.6.1" @@ -5088,6 +6079,12 @@ version = "2.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + [[package]] name = "utf8_iter" version = "1.0.4" @@ -5264,6 +6261,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "wasm-encoder" +version = "0.258.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f64765e43c000fae008a48db8b288e175e237fc9ce20a26586836878c1bee89f" +dependencies = [ + "leb128fmt", + "wasmparser", +] + [[package]] name = "wasm-streams" version = "0.5.0" @@ -5277,6 +6284,218 @@ dependencies = [ "web-sys", ] +[[package]] +name = "wasmparser" +version = "0.258.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe9269dd817e0e7021bf534ccd9761bc2d64778b6708e41228b8439d75eaa0b1" +dependencies = [ + "bitflags 2.13.2", + "hashbrown 0.17.1", + "indexmap 2.14.2", + "semver", + "serde", +] + +[[package]] +name = "wasmprinter" +version = "0.258.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14a457ba8670a3db3d357c1aeac3ed4fe378a76054a221789f0df0ea507e292b" +dependencies = [ + "anyhow", + "termcolor", + "wasmparser", +] + +[[package]] +name = "wasmtime" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d45bf3b13781b9febb0eef42e1e145146c92ef891f345fd918e0e0fa1568c992" +dependencies = [ + "addr2line", + "async-trait", + "bitflags 2.13.2", + "bumpalo", + "cc", + "futures", + "libc", + "log", + "mach2", + "memfd", + "object", + "once_cell", + "postcard", + "pulley-interpreter", + "rayon", + "rustix", + "serde", + "serde_derive", + "smallvec", + "target-lexicon", + "wasmparser", + "wasmtime-environ", + "wasmtime-internal-core", + "wasmtime-internal-cranelift", + "wasmtime-internal-fiber", + "wasmtime-internal-jit-debug", + "wasmtime-internal-jit-icache-coherence", + "wasmtime-internal-unwinder", + "wasmtime-internal-versioned-export-macros", + "wasmtime-internal-winch", + "windows-sys 0.61.2", +] + +[[package]] +name = "wasmtime-environ" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "833f00f3dc6128159284c5b71e211f24c97d45e052230a14f767fd830a6fb8b0" +dependencies = [ + "anyhow", + "cpp_demangle", + "cranelift-bforest", + "cranelift-bitset", + "cranelift-entity", + "gimli", + "hashbrown 0.17.1", + "indexmap 2.14.2", + "log", + "object", + "postcard", + "rustc-demangle", + "semver", + "serde", + "serde_derive", + "sha2 0.10.9", + "smallvec", + "target-lexicon", + "wasm-encoder", + "wasmparser", + "wasmprinter", + "wasmtime-internal-component-util", + "wasmtime-internal-core", +] + +[[package]] +name = "wasmtime-internal-component-util" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b168d39f2d6b896d89fe096afa4ccdd7a8e927b01469b590af84f02fca3126a1" + +[[package]] +name = "wasmtime-internal-core" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ad86a65d12c6a9202584ceb658c9a20cf5b25c32eed61c9be5e5f863f88f0d9" +dependencies = [ + "hashbrown 0.17.1", + "libm", + "serde", +] + +[[package]] +name = "wasmtime-internal-cranelift" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a71f0fd1dd40e4e18d4a20cdba29ff8e0eb2c0f21b12c2eb5869e0fea789dd05" +dependencies = [ + "cranelift-codegen", + "cranelift-control", + "cranelift-entity", + "cranelift-frontend", + "cranelift-native", + "gimli", + "itertools 0.14.0", + "log", + "object", + "pulley-interpreter", + "smallvec", + "target-lexicon", + "thiserror 2.0.21", + "wasmparser", + "wasmtime-environ", + "wasmtime-internal-core", + "wasmtime-internal-unwinder", + "wasmtime-internal-versioned-export-macros", +] + +[[package]] +name = "wasmtime-internal-fiber" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4dfa39e58735b24ce3d28bfc8c2b3041603cc8a941bb268395351beff90172" +dependencies = [ + "cc", + "libc", + "rustix", + "wasmtime-environ", + "wasmtime-internal-versioned-export-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "wasmtime-internal-jit-debug" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2eb98d4486c7b0163447d77dea06f3cc87e5e5ba5fc93613f8ec42e27ea181d" +dependencies = [ + "cc", + "wasmtime-internal-versioned-export-macros", +] + +[[package]] +name = "wasmtime-internal-jit-icache-coherence" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852cae234735d79db6c58fe0d04b32f4078ad01fdb3fd6d488ac366132ba9785" +dependencies = [ + "libc", + "wasmtime-internal-core", + "windows-sys 0.61.2", +] + +[[package]] +name = "wasmtime-internal-unwinder" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9289378c82b9db7e7cf933d6bb441ad968536ac48f3f38c55b7cf9d3e8d87e1" +dependencies = [ + "cranelift-codegen", + "log", + "object", + "wasmtime-environ", +] + +[[package]] +name = "wasmtime-internal-versioned-export-macros" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc5c60c732d99dbcc5d670b71ba6ccf2b015cd16fc828990eaad2c4b8805ebdb" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "wasmtime-internal-winch" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c236aa2967d9ed27babc3a76d478f6323cb5bf4c77d20102e282828226646458" +dependencies = [ + "cranelift-codegen", + "gimli", + "log", + "object", + "target-lexicon", + "wasmparser", + "wasmtime-environ", + "wasmtime-internal-cranelift", + "winch-codegen", +] + [[package]] name = "web-sys" version = "0.3.106" @@ -5297,6 +6516,18 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "web_atoms" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7572660c8890448ba236b7376f27e389c6a7e1c70195622faced601f855c0ada" +dependencies = [ + "phf", + "phf_codegen", + "string_cache", + "string_cache_codegen", +] + [[package]] name = "webpki-root-certs" version = "1.0.9" @@ -5352,6 +6583,25 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "winch-codegen" +version = "49.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98e378f1851ec513f230e3aa1b77a3b4c2e0ebed8db0e551b0c51144f43c133d" +dependencies = [ + "cranelift-assembler-x64", + "cranelift-codegen", + "gimli", + "regalloc2", + "smallvec", + "target-lexicon", + "thiserror 2.0.21", + "wasmparser", + "wasmtime-environ", + "wasmtime-internal-core", + "wasmtime-internal-cranelift", +] + [[package]] name = "windows" version = "0.61.3" @@ -5756,6 +7006,7 @@ version = "0.11.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8" dependencies = [ + "serde", "yoke", "zerofrom", "zerovec-derive", @@ -5809,3 +7060,31 @@ dependencies = [ "log", "simd-adler32", ] + +[[package]] +name = "zstd" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad5020ab8f27bfc0024ad0a07aed6bf022a16208f479686fbed4fdd0c85eb0b3" +dependencies = [ + "zstd-safe", +] + +[[package]] +name = "zstd-safe" +version = "8.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a9ea9cc2576c2fcc104e9cec04f46e3ede564f4fbc206831e4ef7e859a4140d" +dependencies = [ + "zstd-sys", +] + +[[package]] +name = "zstd-sys" +version = "2.1.3+zstd.1.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f729a06d0257428fd9d997ac8db7e1407bd0262ab016b085b36bc52957c0052d" +dependencies = [ + "cc", + "pkg-config", +] diff --git a/Cargo.toml b/Cargo.toml index 4b64cd1f..e1d1f472 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,15 +10,15 @@ members = [ ] [workspace.package] -version = "3.11.0" +version = "3.12.0" edition = "2024" # The MSRV: the newest `rust-version` among the locked dependencies -# (sqlx 0.9 and the aws-smithy crates under aws-sigv4). Without it, +# (including the Wasmtime runtime built by the shared Core gateway). Without it, # contributors on older toolchains get cryptic errors from deep in a # dependency instead of "you need rustc >= 1.94.1". The project tracks # the toolchain in rust-toolchain.toml (currently `stable`); the floor # lets CI / `cargo install` callers fail fast on incompatible versions. -rust-version = "1.94.1" +rust-version = "1.96.0" license = "BUSL-1.1" # Release profile tuned for the production server binary. `lto = true` @@ -59,8 +59,9 @@ opt-level = 3 # parsing (tw-dialect), redaction and tool-call inspection (tw-guard), the # circuit-breaker state machine (tw-breaker), and what only Amazon Bedrock # needs on the wire: SigV4 signing, eventstream unframing, its addresses and -# its model catalog (tw-bedrock). Declared once, here, so a core release is a -# one-line bump. Pinned to a tag: tracking a branch turns every core merge +# its model catalog (tw-bedrock). The Core gateway library supplies response +# negotiation and the bounded public web executor. Pin these to one tag: +# tracking a branch turns every core merge # into a coin flip on this build. # # Code flows one way: from core into this tree, never back. Anything that @@ -70,10 +71,14 @@ opt-level = 3 # never re-exported through a local shim. And the reverse: something only # this side uses (the at-rest crypto, IMDSv2 credentials, the gateway error) # lives here, not in core. -tw-bedrock = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.89.0" } -tw-breaker = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.89.0" } -tw-dialect = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.89.0" } -tw-guard = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.89.0" } +tw-bedrock = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.98.1" } +tw-breaker = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.98.1" } +tw-dialect = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.98.1" } +tw-guard = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.98.1" } +tw-api = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.98.1" } +tw-config = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.98.1" } +tw-gateway = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.98.1" } +tw-types = { git = "https://github.com/ThinkWatchProject/ThinkWatch-Core.git", tag = "v0.98.1" } # Web framework axum = { version = "0.8", features = ["macros", "ws"] } diff --git a/crates/common/src/dynamic_config.rs b/crates/common/src/dynamic_config.rs index 3a777b7d..7f4d22bc 100644 --- a/crates/common/src/dynamic_config.rs +++ b/crates/common/src/dynamic_config.rs @@ -2,7 +2,7 @@ use serde_json::Value; use sqlx::PgPool; use std::collections::HashMap; use std::sync::Arc; -use tokio::sync::RwLock; +use tokio::sync::{Mutex, RwLock}; /// Database-backed dynamic configuration with in-memory cache. /// @@ -12,6 +12,7 @@ use tokio::sync::RwLock; #[derive(Clone)] pub struct DynamicConfig { cache: Arc>>, + reload_lock: Arc>, db: PgPool, } @@ -29,12 +30,15 @@ impl DynamicConfig { Ok(Self { cache: Arc::new(RwLock::new(map)), + reload_lock: Arc::new(Mutex::new(())), db, }) } /// Reload all settings from the database. pub async fn reload(&self) -> anyhow::Result<()> { + // Serialize snapshots so an older DB read cannot replace a newer one. + let _reload = self.reload_lock.lock().await; let rows: Vec<(String, Value)> = sqlx::query_as("SELECT key, value FROM system_settings") .fetch_all(&self.db) .await?; @@ -44,18 +48,18 @@ impl DynamicConfig { map.insert(key, value); } - let mut cache = self.cache.write().await; - *cache = map; + *self.cache.write().await = map; Ok(()) } - /// Update one or more settings in the database and refresh the cache. - /// Validates values before persisting. - pub async fn update( + /// Compare the value read by the editor and replace it in the same SQL + /// statement. A conflict rolls back the whole settings patch. + pub async fn update_checked( &self, updates: &HashMap, + expected: &HashMap, updated_by: Option, - ) -> anyhow::Result<()> { + ) -> anyhow::Result> { // Validate values before persisting for (key, value) in updates { validate_setting(key, value)?; @@ -63,22 +67,29 @@ impl DynamicConfig { let mut tx = self.db.begin().await?; - for (key, value) in updates { - sqlx::query( + let mut ordered: Vec<_> = updates.iter().collect(); + ordered.sort_unstable_by_key(|(key, _)| *key); + for (key, value) in ordered { + let result = sqlx::query( r#"UPDATE system_settings SET value = $1, updated_by = $2, updated_at = now() - WHERE key = $3"#, + WHERE key = $3 AND ($4::jsonb IS NULL OR value = $4)"#, ) .bind(value) .bind(updated_by) .bind(key) + .bind(expected.get(key)) .execute(&mut *tx) .await?; + if expected.contains_key(key) && result.rows_affected() == 0 { + tx.rollback().await?; + return Ok(Err(key.clone())); + } } tx.commit().await?; self.reload().await?; - Ok(()) + Ok(Ok(())) } // --- Typed getters --- diff --git a/crates/common/src/models/api_key.rs b/crates/common/src/models/api_key.rs index 975c08ac..4418ae04 100644 --- a/crates/common/src/models/api_key.rs +++ b/crates/common/src/models/api_key.rs @@ -52,10 +52,8 @@ pub struct ApiKey { /// generations on a single index lookup. Backed by /// `idx_api_keys_lineage_id`. /// - /// Server-internal: skipped from JSON responses so the frontend - /// stays unaware of rotation generations. Per-key history is - /// rolled up by the read handlers via PG-side - /// `id → lineage_id` resolution. + /// Excluded from the shared JSON representation. The key inventory + /// projects it explicitly for settings that must survive rotation. #[serde(skip_serializing)] pub lineage_id: Uuid, /// Optional cost-center / project tag for per-subject analytics diff --git a/crates/common/src/resources.rs b/crates/common/src/resources.rs index 5f8b3987..3cbc3116 100644 --- a/crates/common/src/resources.rs +++ b/crates/common/src/resources.rs @@ -6,7 +6,7 @@ use std::sync::{Arc, Mutex, OnceLock}; use std::time::Duration; use bytes::Bytes; -use tokio::sync::{OwnedSemaphorePermit, Semaphore}; +use tokio::sync::{Mutex as AsyncMutex, Notify, OwnedSemaphorePermit, Semaphore}; #[cfg(any(target_os = "linux", test))] mod cgroup; @@ -119,6 +119,8 @@ pub async fn password_slot() -> (OwnedSemaphorePermit, OwnedSemaphorePermit) { pub struct ByteBudget { limit: usize, used: Mutex, + intake: AsyncMutex<()>, + released: Notify, } impl ByteBudget { @@ -126,6 +128,8 @@ impl ByteBudget { Arc::new(Self { limit, used: Mutex::new(0), + intake: AsyncMutex::new(()), + released: Notify::new(), }) } @@ -165,11 +169,20 @@ impl Reservation { reservation: Arc::new(self), }) } + + fn shrink(&mut self, bytes: usize) { + let released = self.charged.saturating_sub(bytes); + if released != 0 { + self.charged -= released; + *self.budget.used.lock().unwrap_or_else(|e| e.into_inner()) -= released; + self.budget.released.notify_waiters(); + } + } } impl Drop for Reservation { fn drop(&mut self) { - *self.budget.used.lock().unwrap_or_else(|e| e.into_inner()) -= self.charged; + self.shrink(0); } } diff --git a/crates/common/src/resources/http.rs b/crates/common/src/resources/http.rs index e07f7eb0..a8d2e887 100644 --- a/crates/common/src/resources/http.rs +++ b/crates/common/src/resources/http.rs @@ -3,14 +3,45 @@ use std::pin::Pin; use std::sync::Arc; use std::task::{Context, Poll}; +use std::time::Duration; use axum::body::{Body, Bytes, HttpBody}; use axum::extract::Request; use axum::middleware::Next; use axum::response::{IntoResponse, Response}; use futures::StreamExt; +use tokio::time::{Instant, timeout, timeout_at}; -use super::{ByteBudget, policy}; +use super::{BODY_HEADROOM, ByteBudget, Reservation, policy}; + +const BODY_WAIT: Duration = Duration::from_secs(30); + +async fn wait_for_bytes(budget: &ByteBudget, lease: &mut Reservation, bytes: usize) -> bool { + if bytes > budget.limit { + return false; + } + let deadline = Instant::now() + BODY_WAIT; + loop { + let released = budget.released.notified(); + tokio::pin!(released); + released.as_mut().enable(); + if lease.grow(bytes) { + return true; + } + if timeout_at(deadline, released).await.is_err() { + return false; + } + } +} + +fn busy(budget: &ByteBudget) -> Response { + tracing::warn!( + used_bytes = budget.used(), + budget_bytes = budget.limit, + "Request body admission exhausted" + ); + rejection(axum::http::StatusCode::TOO_MANY_REQUESTS) +} /// Preserve Axum's JSON errors while moving large/concurrent decoding off I/O. pub struct ScheduledJson(pub T); @@ -59,14 +90,40 @@ async fn admit_with_budget( budget: &Arc, max: usize, ) -> Response { + if request.body().is_end_stream() { + return next.run(request).await; + } let (parts, body) = request.into_parts(); - if body.size_hint().upper().is_some_and(|n| n > max as u64) { + let upper = body.size_hint().upper(); + if upper.is_some_and(|n| n > max as u64) { return rejection(axum::http::StatusCode::PAYLOAD_TOO_LARGE); } + // Known-size bodies reserve their entire allowance before reading, so + // only one partial, unknown-size body ever waits while holding memory. + let Ok(turn) = timeout(BODY_WAIT, budget.intake.lock()).await else { + return busy(budget); + }; + let mut turn = Some(turn); let mut data = Vec::new(); let mut lease = budget.reserve(); + if let Some(upper) = upper { + if !wait_for_bytes( + budget, + &mut lease, + (upper as usize).saturating_mul(BODY_HEADROOM), + ) + .await + { + return busy(budget); + } + drop(turn.take()); + } let mut chunks = body.into_data_stream(); - while let Some(chunk) = chunks.next().await { + loop { + let Ok(chunk) = timeout(BODY_WAIT, chunks.next()).await else { + return rejection(axum::http::StatusCode::REQUEST_TIMEOUT); + }; + let Some(chunk) = chunk else { break }; let chunk = match chunk { Ok(chunk) => chunk, Err(error) => { @@ -86,11 +143,19 @@ async fn admit_with_budget( if size > max { return rejection(axum::http::StatusCode::PAYLOAD_TOO_LARGE); } - if !lease.grow(size.saturating_mul(super::BODY_HEADROOM)) { - return rejection(axum::http::StatusCode::TOO_MANY_REQUESTS); + let charge = size.saturating_mul(BODY_HEADROOM); + let admitted = if turn.is_some() { + wait_for_bytes(budget, &mut lease, charge).await + } else { + lease.grow(charge) + }; + if !admitted { + return busy(budget); } data.extend_from_slice(&chunk); } + drop(turn); + lease.shrink(data.len().saturating_mul(BODY_HEADROOM)); let data = lease.wrap(data.into()); let mut response = next .run(Request::from_parts(parts, Body::from(data.clone()))) @@ -109,6 +174,8 @@ fn rejection(status: axum::http::StatusCode) -> Response { "The gateway is buffering other requests. Retry shortly." } else if status == axum::http::StatusCode::PAYLOAD_TOO_LARGE { "The request body exceeds the gateway buffering limit." + } else if status == axum::http::StatusCode::REQUEST_TIMEOUT { + "The request body stopped arriving. Send the request again." } else { "The request body could not be read." }; @@ -167,6 +234,32 @@ mod tests { )) } + #[tokio::test] + async fn a_waiting_large_upload_precedes_later_small_uploads() { + let budget = ByteBudget::new(25 * BODY_HEADROOM); + let app = router(budget, 25); + let held = app + .clone() + .oneshot(Request::post("/").body(Body::from(vec![0; 20])).unwrap()) + .await + .unwrap(); + let upload = |size| { + app.clone() + .oneshot(Request::post("/").body(Body::from(vec![0; size])).unwrap()) + }; + let large = upload(24); + let small = upload(4); + tokio::pin!(large, small); + assert!(futures::poll!(&mut large).is_pending()); + assert!(futures::poll!(&mut small).is_pending()); + drop(held); + let large = large.await.unwrap(); + assert_eq!(large.status(), 200); + assert!(futures::poll!(&mut small).is_pending()); + drop(large); + assert_eq!(small.await.unwrap().status(), 200); + } + #[tokio::test] async fn chunked_pressure_returns_retryable_429_and_frees_partial_buffers() { let budget = ByteBudget::new(25 * super::super::BODY_HEADROOM); diff --git a/crates/gateway/Cargo.toml b/crates/gateway/Cargo.toml index da2b5e87..369d8f91 100644 --- a/crates/gateway/Cargo.toml +++ b/crates/gateway/Cargo.toml @@ -8,6 +8,12 @@ tw-breaker = { workspace = true } tw-guard = { workspace = true } tw-dialect = { workspace = true } tw-bedrock = { workspace = true } +tw-api = { workspace = true } +tw-config = { workspace = true } +tw-gateway = { workspace = true } +tw-types = { workspace = true } +sha2 = { workspace = true } +hex = { workspace = true } think-watch-common = { workspace = true } think-watch-auth = { workspace = true } sqlx = { workspace = true } @@ -30,7 +36,6 @@ anyhow = { workspace = true } tracing = { workspace = true } fred = { workspace = true } utoipa = { workspace = true } -# sha2 removed — cache now uses xxhash; HMAC signing uses hmac crate in server regex = "1" rand = { workspace = true } metrics = { workspace = true } diff --git a/crates/gateway/src/lifecycle/mod.rs b/crates/gateway/src/lifecycle/mod.rs index a844499d..3c229c75 100644 --- a/crates/gateway/src/lifecycle/mod.rs +++ b/crates/gateway/src/lifecycle/mod.rs @@ -33,7 +33,7 @@ use think_watch_common::limits::RequestLimits; use tw_dialect::ir::Dialect; use crate::guards::Guards; -use crate::proxy::generate::{Wire, priced, tokens}; +use crate::proxy::generate::{UpstreamReply, Wire, priced, tokens}; use crate::proxy::min_speed::{CHECK_EVERY, MinSpeed, SpeedWatch}; use crate::proxy::retry::{KEEP_ALIVE, KEEP_ALIVE_FRAME}; use crate::proxy::shaper::{StreamShaper, rewrite_model}; @@ -171,7 +171,7 @@ pub(crate) struct ChatPostInvokeDeps { pub(crate) type OpenUpstream = Box< dyn FnMut() -> Pin< Box< - dyn std::future::Future> + dyn std::future::Future> + Send, >, > + Send, @@ -385,9 +385,16 @@ pub(crate) fn build_chat_pump( if let Ok(mut s) = sent.lock() { s.effort = wire.effort; } + let (mut source, adapted_usage) = match upstream { + UpstreamReply::Http(response) => (response.bytes_stream().boxed(), None), + UpstreamReply::Adapted { body, usage, .. } => ( + futures::stream::once(std::future::ready(Ok(body))).boxed(), usage, + ), + }; if let Ok(mut r) = readers.lock() { *r = Readers { sniffer: Some(tw_dialect::usage::Sniffer::new()), + adapted_usage, collector: Some(wire.collect.collector()), errors: Some(ErrorWatch::new(wire.dialect)), ledger: Some(wire.ledger.clone()), @@ -410,7 +417,6 @@ pub(crate) fn build_chat_pump( let mut speed = min_speed.map(|m| SpeedWatch::new(m, wire.dialect)); let mut judge = tokio::time::interval(CHECK_EVERY); judge.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay); - let mut source = upstream.bytes_stream(); loop { let next = tokio::select! { item = source.next() => Pulled::Item(item), @@ -702,7 +708,9 @@ pub(crate) fn build_chat_pump( let sniffer = r.sniffer.take(); ( sniffer.is_some(), - sniffer.and_then(|s| s.finish()), + r.adapted_usage + .take() + .or_else(|| sniffer.and_then(|s| s.finish())), r.collector.take().and_then(|c| c.finish().ok()), r.ledger.take(), ) @@ -797,6 +805,7 @@ pub(crate) fn for_the_log( #[derive(Default)] struct Readers { sniffer: Option, + adapted_usage: Option, collector: Option, /// `None` once it found an error. errors: Option, diff --git a/crates/gateway/src/proxy/generate.rs b/crates/gateway/src/proxy/generate.rs index 8321c059..8ca044c8 100644 --- a/crates/gateway/src/proxy/generate.rs +++ b/crates/gateway/src/proxy/generate.rs @@ -43,6 +43,7 @@ use axum::response::IntoResponse; use rust_decimal::Decimal; use serde_json::Value; use tw_dialect::convert::Session; +use tw_dialect::history::{HistoryFormat, HistoryTarget}; use tw_dialect::ir::{Dialect, Target}; use super::body_capture::prepare_body_capture; @@ -52,7 +53,9 @@ use super::headers::{ request_id_header, resolve_conversation_id, resolve_session_id, resolve_trace_id, }; use super::log_ctx::{LogCtx, emit_gateway_error_log, emit_gateway_log}; -use super::pipeline::{launch_stream_pump, run_buffered_post_invoke, run_preflight_stages}; +use super::pipeline::{ + AiRound, launch_stream_pump, run_buffered_post_invoke, run_preflight_stages, +}; use super::retry::Budget; use super::routing::{ Resend, build_selection_ctx, finalize_health, select_route_for_stream, @@ -60,7 +63,7 @@ use super::routing::{ }; use super::sealed::Conversation; use super::shaper::{StreamShaper, rewrite_model}; -use super::{GatewayErrorResponse, GatewayRequestIdentity, GatewayState}; +use super::{GatewayErrorResponse, GatewayRequestIdentity, GatewayState, ModelDiscovery}; use tw_dialect::params::WrittenEffort; use tw_dialect::think::EffortFix; @@ -256,10 +259,13 @@ pub(crate) struct Outbound { /// The request's reasoning: the model's setting, applied to each hop /// as it is addressed, and what the caller asked (see `super::effort`). pub reasoning: effort::Asked, + pub adapt_response_mode: bool, + pub discovery: ModelDiscovery, } /// The request as it goes out to one upstream, and what it takes to read /// the answer back. +#[derive(Clone)] pub(crate) struct Wire { pub body: Vec, pub path: String, @@ -360,6 +366,38 @@ impl Wire { .await } + async fn send_with_refusal( + &self, + upstream: &super::transport::Upstream, + call_ctx: &CallCtx, + ) -> Result { + upstream + .send_with_refusal( + self.body.clone(), + &self.path, + self.query.as_deref(), + self.dialect, + &self.headers, + call_ctx, + ) + .await + } + + fn omit_history( + &mut self, + outbound: &Outbound, + fields: &[tw_gateway::history_memory::HistoryField], + ) -> bool { + let Some(body) = tw_gateway::history_memory::omit(&self.body, fields) else { + return false; + }; + (self.body, self.ledger) = + outbound + .redaction + .replace(body, &self.ledger, Shape::Generate(self.dialect)); + true + } + /// This hop as `upstream` takes it: without the added breakpoints /// when it refused them for `model` before. fn for_upstream(mut self, upstream: &super::transport::Upstream, model: &str) -> Wire { @@ -443,11 +481,22 @@ impl Outbound { } /// Address the request to `protocol`, naming `model` upstream. - pub(crate) fn address( + #[cfg(test)] + fn address( + &self, + protocol: UpstreamProtocol, + model: &str, + official: bool, + ) -> Result { + self.address_with_history(protocol, model, official, &HistoryTarget::default()) + } + + fn address_with_history( &self, protocol: UpstreamProtocol, model: &str, official: bool, + history: &HistoryTarget, ) -> Result { let client = self.surface.dialect; // Output length when the caller set none and the upstream insists @@ -520,6 +569,16 @@ impl Outbound { self.cap_output(client, &mut body, model, official); let collect = self.forwarded_session(&body, model, &target(client)); let mut bytes = serde_json::to_vec(&body).unwrap_or_default(); + if client == Dialect::Chat && history.format != HistoryFormat::Auto { + if let Ok(decoded) = decode(&body) + && tw_dialect::history::incompatible(&decoded.request, history, true) + { + return Err(incompatible_history()); + } + if let Some(shaped) = tw_dialect::history::shape_chat(&bytes, history.format) { + bytes = shaped; + } + } // Reasoning signatures a conversion wrote earlier in this // conversation (`tw1.`-prefixed) were not issued by this // upstream, and Anthropic refuses the whole request over @@ -552,7 +611,10 @@ impl Outbound { decoded.request.model = model.to_string(); let turned_off = off_converted && effort::turn_off_converted(&mut decoded.request, protocol.dialect()); - let prepared = decoded.encode(&target(protocol.dialect())); + let prepared = decoded.encode_with_history(&target(protocol.dialect()), history); + if prepared.incompatible_history { + return Err(incompatible_history()); + } if !prepared.dropped.is_empty() { tracing::info!( from = client.slug(), @@ -602,6 +664,21 @@ impl Outbound { } } +fn incompatible_history() -> GatewayError { + GatewayError::TransformError( + "The selected upstream cannot replay this conversation's signed reasoning history.".into(), + ) +} + +pub(crate) enum UpstreamReply { + Http(reqwest::Response), + Adapted { + body: Bytes, + memory: Bytes, + usage: Option, + }, +} + /// Send to `entry`, and if the upstream rejects the dialect this route /// is configured for, try its alternates and remember whichever answers. /// @@ -626,12 +703,22 @@ pub(crate) async fn send( db: &sqlx::PgPool, redis: &fred::clients::Client, model: &str, -) -> Result<(reqwest::Response, Wire), GatewayError> { +) -> Result<(UpstreamReply, Wire), GatewayError> { + use tw_gateway::{history_memory, stream_mode}; let upstream = &entry.upstream; let official = upstream.is_official(); + let mut provider = upstream.negotiation_provider(entry.provider_id, call_ctx); + if let Some(format) = outbound + .discovery + .profile(entry.provider_id, model) + .and_then(|spec| spec.reasoning_history) + { + provider.reasoning_history = format; + } + let history = tw_gateway::translate::history_target(&provider, model); let first = resources::cpu(usize::MAX, || { outbound - .address(entry.protocol, model, official) + .address_with_history(entry.protocol, model, official, &history) .map(|wire| wire.for_upstream(upstream, model)) }) .await?; @@ -641,7 +728,7 @@ pub(crate) async fn send( } else { effort::Memory::default() }; - let first = first.remembered(&memory); + let mut first = first.remembered(&memory); if first.remembered { tracing::info!( provider = %entry.provider_name, @@ -652,16 +739,51 @@ pub(crate) async fn send( } // The format of the last request sent, to read its refusal in. let mut wire_dialect = first.dialect; - let mut last = match first.send_to(upstream, call_ctx).await { - Ok(resp) => return Ok((resp, first)), - Err(e) => e, + let now = history_memory::now_ms(); + let history_key = (first.dialect == Dialect::Chat + && history_memory::automatic(&provider, model) + && history.format != HistoryFormat::ReasoningContent + && tw_dialect::convert::decode( + outbound.surface.dialect, + &outbound.body, + &outbound.path, + internal_query(outbound.surface.dialect), + ) + .is_ok_and(|decoded| history_memory::unsigned(&decoded.request))) + .then(|| history_memory::Key::of(&provider, model, &first.path, &first.body)) + .flatten(); + let prior = history_key + .as_ref() + .map(|key| upstream.history.policy(key, now)); + if let Some(prior) = &prior { + first.omit_history(outbound, &prior.fields); + } + let mode_key = outbound + .adapt_response_mode + .then(|| stream_mode::Key::of(&provider, model, first.dialect, &first.path, &first.body)) + .flatten(); + let learned = mode_key + .as_ref() + .and_then(|key| upstream.response_modes.lookup(key, now)); + if let Some(mode) = learned.filter(|mode| *mode != outbound.stream) + && let Ok(Some(adapted)) = + send_alternate_mode(&first, outbound, upstream, call_ctx, mode).await + { + return Ok(adapted); + } + let refusal = match first.send_with_refusal(upstream, call_ctx).await { + Ok(resp) => return Ok((UpstreamReply::Http(resp), first)), + Err(refusal) => refusal, }; + let mut last = refusal.error; + let mut evidence = refusal.repair; let mut wire = first; if wire.auto_cache && super::cache_marks::is_refusal(&last, &upstream.label) && wire.drop_added_marks() { + evidence = None; upstream.cache_marks.note(model); tracing::info!( provider = %entry.provider_name, @@ -669,7 +791,7 @@ pub(crate) async fn send( "Upstream refused the cache breakpoints added on conversion — sending again without them" ); match wire.send_to(upstream, call_ctx).await { - Ok(resp) => return Ok((resp, wire)), + Ok(resp) => return Ok((UpstreamReply::Http(resp), wire)), Err(e) => last = e, } } @@ -685,6 +807,7 @@ pub(crate) async fn send( let pre = wire.pre.unwrap_or(sent); let remembered = wire.remembered; if wire.fix_effort(fix, pre, Reason::Rejected) { + evidence = None; tracing::info!( provider = %entry.provider_name, model, @@ -705,7 +828,7 @@ pub(crate) async fn send( tokio::spawn(async move { effort::remember(&redis, provider_id, dialect, &model, pre, fix).await; }); - return Ok((resp, wire)); + return Ok((UpstreamReply::Http(resp), wire)); } Err(e) => { if remembered { @@ -720,6 +843,7 @@ pub(crate) async fn send( } if super::protocol_relearn::is_protocol_mismatch(&last) { + evidence = None; for protocol in &entry.alternates { tracing::info!( provider = %entry.provider_name, @@ -730,7 +854,7 @@ pub(crate) async fn send( ); let wire = resources::cpu(usize::MAX, || { outbound - .address(*protocol, model, official) + .address_with_history(*protocol, model, official, &history) .map(|wire| wire.for_upstream(upstream, model)) }) .await?; @@ -746,7 +870,7 @@ pub(crate) async fn send( super::protocol_relearn::persist(db, entry.route_id, *protocol).await; // What it refused was said in another format. effort::forget_provider(redis, entry.provider_id).await; - return Ok((resp, wire)); + return Ok((UpstreamReply::Http(resp), wire)); } Err(e) => { let keep_going = super::protocol_relearn::is_protocol_mismatch(&e); @@ -758,6 +882,40 @@ pub(crate) async fn send( } } } + if let Some(evidence) = evidence { + let verdict = + tw_gateway::failure::classify(evidence.status, &evidence.headers, &evidence.body, now); + if let Some(key) = history_key + && matches!(verdict, tw_gateway::failure::Verdict::ClientError) + && let Some(mut fields) = + history_memory::refusal(evidence.status, &evidence.body, &wire.body) + { + let old_fields = prior + .as_ref() + .map_or(&[][..], |policy| policy.fields.as_slice()); + if wire.omit_history(outbound, &fields) + && let Ok(resp) = wire.send_to(upstream, call_ctx).await + { + for &field in old_fields { + if !fields.contains(&field) { + fields.push(field); + } + } + upstream + .history + .accepted(key, history_memory::Policy { fields }, now); + return Ok((UpstreamReply::Http(resp), wire)); + } + } else if let Some(key) = mode_key + && learned.is_none_or(|mode| mode == outbound.stream) + && stream_mode::can_probe(evidence.status, &evidence.headers, &evidence.body, verdict) + && let Ok(Some(adapted)) = + send_alternate_mode(&wire, outbound, upstream, call_ctx, !outbound.stream).await + { + upstream.response_modes.accepted(key, !outbound.stream, now); + return Ok(adapted); + } + } // A refusal of the conversation's encrypted history, said plainly. Err(super::sealed::explain_refusal( wire_dialect, @@ -767,16 +925,138 @@ pub(crate) async fn send( )) } +async fn send_alternate_mode( + wire: &Wire, + outbound: &Outbound, + upstream: &super::transport::Upstream, + call_ctx: &CallCtx, + stream: bool, +) -> Result, GatewayError> { + use futures::StreamExt; + use tw_gateway::stream_mode; + let mut wire = wire.clone(); + let Some(body) = stream_mode::rewrite( + wire.dialect, + &mut wire.path, + &mut wire.query, + &wire.body, + stream, + ) else { + return Ok(None); + }; + wire.body = body; + let resp = wire.send_to(upstream, call_ctx).await?; + let mut sniffer = tw_dialect::usage::Sniffer::new(); + let (body, memory) = if stream { + let mut raw = resp.bytes_stream(); + let mut lease = resources::body_budget().reserve(); + let mut bytes = Vec::new(); + let mut unframe = + (wire.dialect == Dialect::Bedrock).then(tw_bedrock::eventstream::Transcoder::new); + while let Some(chunk) = raw.next().await { + let chunk = chunk.map_err(super::transport::transport_error)?; + let chunk = if let Some(unframe) = &mut unframe { + Bytes::from( + unframe + .feed(&chunk) + .map_err(|e| GatewayError::ProviderInvalidResponse(e.to_string()))?, + ) + } else { + chunk + }; + let size = bytes.len().saturating_add(chunk.len()); + if size > resources::policy().max_body_bytes + || !lease.grow(size.saturating_mul(resources::BODY_HEADROOM)) + { + return Ok(None); + } + bytes.extend_from_slice(&chunk); + } + let memory = lease.wrap(bytes.into()); + let body = resources::cpu(memory.len(), || { + let mut complete = stream_mode::Completion::new(wire.dialect); + complete.feed(&memory); + if !complete.finish() { + return None; + } + sniffer.feed(&memory); + let mut collector = wire.collect.collector(); + collector.process(&memory); + collector.finish().ok() + }) + .await; + let Some(body) = body else { return Ok(None) }; + (body, memory) + } else { + let memory = match resources::read_response(resp, resources::policy().max_body_bytes).await + { + Ok(bytes) => bytes, + Err(_) => return Ok(None), + }; + let body = resources::cpu(memory.len(), || { + if !stream_mode::whole_answer(wire.dialect, &memory) { + return None; + } + sniffer.feed(&memory); + wire.collect.stream_from_whole(&memory) + }) + .await; + let Some(body) = body else { return Ok(None) }; + wire.dialect = outbound.surface.dialect; + let model = outbound.body["model"].as_str().unwrap_or_default(); + wire.collect = outbound.forwarded_session( + &outbound.body, + model, + &Target { + dialect: wire.dialect, + official: false, + default_max_tokens: 8192, + }, + ); + wire.convert = None; + (body, memory) + }; + Ok(Some(( + UpstreamReply::Adapted { + body: resources::retain(body.into(), memory.clone()), + memory, + usage: sniffer.finish(), + }, + wire, + ))) +} + /// Read a whole answer and put it in the caller's format. /// /// When the upstream reports no usage, the count is estimated from the /// request (`input_estimate`) and the answer. pub(crate) async fn read_whole( - resp: reqwest::Response, + resp: UpstreamReply, wire: &Wire, caller_model: &str, input_estimate: u64, ) -> Result { + let resp = match resp { + UpstreamReply::Http(resp) => resp, + UpstreamReply::Adapted { + body, + memory, + usage, + } => { + let (usage, usage_estimated) = + crate::usage_estimate::complete(usage, true, input_estimate, Some(&body)); + if usage_estimated { + metrics::counter!("gateway_usage_estimated_total").increment(1); + } + return Ok(Completed { + memory: Some(memory), + body: rewrite_model(&body, caller_model), + logged: None, + usage, + usage_estimated, + }); + } + }; let upstream = resources::read_response(resp, resources::policy().max_body_bytes) .await .map_err(|error| match error { @@ -844,15 +1124,41 @@ pub(crate) async fn generate( path: &str, query: Option<&str>, ) -> Result { + if matches!(surface.dialect, Dialect::Anthropic | Dialect::Responses) + && let Some(result) = (super::web_tools::HostedRequest { + state: &state, + headers: &headers, + identity: &identity, + cancel: cancel.clone(), + arrived, + body: &body, + surface, + path, + query, + }) + .execute() + .await + { + return result.map_err(|e| e.in_dialect(surface.dialect)); + } run( - state, headers, identity, cancel, arrived, body, surface, path, query, + state, + headers, + identity, + cancel, + arrived, + body, + surface, + path, + query, + AiRound::Client, ) .await .map_err(|e| e.in_dialect(surface.dialect)) } #[allow(clippy::too_many_arguments)] -async fn run( +pub(super) async fn run( state: GatewayState, headers: HeaderMap, identity: GatewayRequestIdentity, @@ -862,6 +1168,7 @@ async fn run( surface: ClientSurface, path: &str, query: Option<&str>, + round: AiRound, ) -> Result { let trace_id = resolve_trace_id(&headers); let session_id = resolve_session_id(&headers); @@ -914,7 +1221,8 @@ async fn run( || query.is_some_and(|q| q.split('&').any(|kv| kv == GEMINI_SSE)); // 1. Model aliases - let mapped_model = state.model_mapper.map(&model); + let discovery = ModelDiscovery::load(&state).await; + let mapped_model = discovery.resolve(&model); if let Some(c) = &cancel { c.model(&mapped_model); } @@ -929,7 +1237,8 @@ async fn run( // 2. Budget, model access, rate limits — each stage writes its own // audit row on short-circuit. - let preflight = run_preflight_stages(&state, &identity, &trace_id, &mapped_model).await?; + let preflight = + run_preflight_stages(&state, &identity, &trace_id, &mapped_model, round).await?; let mut metadata = RequestMetadata::extract(&headers, &raw); // One id for the request: the one its error rows and the guards' @@ -1258,6 +1567,8 @@ async fn run( ledger: ledger.clone(), max_output_tokens, reasoning, + adapt_response_mode: arrived.is_some(), + discovery, }; let snapshot = |route: &RouteEntry, sel_record| crate::lifecycle::ChatPostInvokeDeps { state: state.clone(), @@ -1591,6 +1902,8 @@ mod tests { ledger, max_output_tokens: cap, reasoning: effort::Asked::default(), + adapt_response_mode: true, + discovery: ModelDiscovery::default(), } } @@ -1608,6 +1921,101 @@ mod tests { serde_json::from_slice(&wire.body).unwrap() } + #[tokio::test] + async fn response_mode_trials_keep_client_framing_and_learn_only_completed_answers() { + use axum::{Json, Router, routing::post}; + use std::sync::Arc; + use std::sync::atomic::{AtomicUsize, Ordering}; + + let attempts = Arc::new(AtomicUsize::new(0)); + let count = attempts.clone(); + let app = Router::new().route("/v1/chat/completions", post(move |Json(body): Json| { + count.fetch_add(1, Ordering::Relaxed); + async move { + let stream = body["stream"] == true; + let model = body["model"].as_str().unwrap_or_default(); + if stream != (model == "stream-only") { + return (axum::http::StatusCode::BAD_REQUEST, Json(serde_json::json!({ + "error": {"message": "The requested stream mode is not supported"} + }))).into_response(); + } + if stream { + let text = r#"data: {"id":"one","model":"stream-only","choices":[{"index":0,"delta":{"role":"assistant","content":"example"},"finish_reason":null}]} + +data: {"id":"one","model":"stream-only","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}} + +data: [DONE] + +"#; + return ([(header::CONTENT_TYPE, "text/event-stream")], text).into_response(); + } + Json(serde_json::json!({"id":"one","model":model, + "choices":[{"index":0,"message":{"role":"assistant","content":"example"},"finish_reason":"stop"}], + "usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5} + })).into_response() + } + })); + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let url = format!("http://{}", listener.local_addr().unwrap()); + let server = tokio::spawn(async move { axum::serve(listener, app).await }); + let entry = RouteEntry { + upstream: Arc::new(crate::proxy::transport::Upstream::new( + &url, + vec![], + crate::proxy::transport::Shape::Standard, + "test", + )), + provider_id: uuid::Uuid::new_v4(), + route_id: uuid::Uuid::new_v4(), + provider_name: "test".into(), + upstream_model: None, + weight: 1, + label: None, + rpm_cap: None, + tpm_cap: None, + protocol: UpstreamProtocol::OpenAiChat, + alternates: vec![], + }; + let db = sqlx::postgres::PgPoolOptions::new() + .connect_lazy("postgres://localhost/unused") + .unwrap(); + let redis = fred::clients::Client::default(); + for (model, client_stream, expected) in [ + ("whole-only", true, 2), + ("stream-only", false, 4), + ("whole-only", true, 5), + ] { + let request = outbound( + CHAT, + "/v1/chat/completions", + serde_json::json!({ + "model":model,"stream":client_stream,"messages":[{"role":"user","content":"hello"}] + }), + None, + ); + let (response, wire) = send(&entry, &request, &CallCtx::default(), &db, &redis, model) + .await + .unwrap(); + if client_stream { + let UpstreamReply::Adapted { body, usage, .. } = response else { + panic!("expected synthesized SSE") + }; + let body = String::from_utf8_lossy(&body); + assert!(body.contains("example")); + assert!(body.contains("data: [DONE]")); + assert_eq!(usage.unwrap().output, 2); + } else { + let answer = read_whole(response, &wire, model, 0).await.unwrap(); + let body: Value = serde_json::from_slice(&answer.body).unwrap(); + assert_eq!(body["choices"][0]["message"]["content"], "example"); + assert_eq!(answer.usage.output, 2); + } + let actual = attempts.load(Ordering::Relaxed); + assert_eq!(actual, expected); + } + server.abort(); + } + #[test] fn the_reasoning_setting_goes_out_on_every_hop_in_its_format() { let ask = serde_json::json!({ diff --git a/crates/gateway/src/proxy/mod.rs b/crates/gateway/src/proxy/mod.rs index 3bd787b5..cbf3de5f 100644 --- a/crates/gateway/src/proxy/mod.rs +++ b/crates/gateway/src/proxy/mod.rs @@ -13,7 +13,6 @@ use crate::cache::ResponseCache; use crate::cost_tracker::CostTracker; use crate::error::GatewayError; use crate::health::HealthTracker; -use crate::model_mapping::ModelMapper; use crate::router::ModelRouter; use think_watch_common::dynamic_config::DynamicConfig; use think_watch_common::limits::SurfaceConstraints; @@ -29,6 +28,7 @@ mod headers; mod identity; mod log_ctx; pub mod min_speed; +mod model_discovery; mod models; mod pipeline; mod protocol_relearn; @@ -38,6 +38,7 @@ mod routing; pub(crate) mod sealed; pub mod shaper; pub mod transport; +mod web_tools; // pub(crate) re-exports — `lifecycle` module reaches in for these. pub(crate) use accounting::post_flight_account; @@ -53,8 +54,10 @@ pub use generate::{ proxy_anthropic_messages, proxy_chat_completion, proxy_gemini, proxy_responses, }; pub use identity::limits_for_ai_gateway; -pub use models::{list_gemini_models_handler, list_models_handler}; +pub use model_discovery::ModelDiscovery; +pub use models::{list_codex_models_handler, list_gemini_models_handler, list_models_handler}; pub use responses_ws::proxy_responses_ws; +pub use web_tools::{HostedWebRuntime, HostedWebSettings}; /// How long the gateway port gives a handler to produce its response /// before answering 408 (the server's `TimeoutLayer`). A stream's body is @@ -83,8 +86,8 @@ pub async fn stamp_arrival( /// Shared application state for the gateway proxy handlers. #[derive(Clone)] pub struct GatewayState { + pub hosted_web: Arc, pub router: Arc>, - pub model_mapper: Arc, /// The request guards — outbound redaction, the content filter and /// tool-call inspection. Hot-swapped whole when an admin changes a /// policy; each request runs on the snapshot it took on arrival. diff --git a/crates/gateway/src/proxy/model_discovery.rs b/crates/gateway/src/proxy/model_discovery.rs new file mode 100644 index 00000000..f1242e66 --- /dev/null +++ b/crates/gateway/src/proxy/model_discovery.rs @@ -0,0 +1,590 @@ +use std::collections::{BTreeMap, HashMap}; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +use futures::{StreamExt, stream}; +use serde::Deserialize; +use serde_json::{Value, json}; +use tokio::sync::{Mutex, Semaphore}; +use tw_config::{ModelSpec, ReasoningEffort, ReasoningLevel, ReportedSpec}; +use uuid::Uuid; + +use super::transport::Shape; +use super::{GatewayRequestIdentity, GatewayState}; +use crate::call_ctx::{CallCtx, substitute_template}; +use crate::model_mapping::ModelMapper; +use crate::router::{ModelRouter, RouteEntry}; +use think_watch_common::resources; + +#[derive(Default, Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct ModelDiscovery { + aliases: BTreeMap, + profiles: BTreeMap>, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct Alias { + target: String, + #[serde(default)] + hide_original: bool, +} + +impl ModelDiscovery { + pub const KEY: &str = "gateway.model_discovery"; + + pub fn parse(value: Value) -> Result { + let cfg: Self = serde_json::from_value(value).map_err(|e| e.to_string())?; + let valid_name = |name: &str| { + !name.is_empty() + && name.len() <= 256 + && !name.contains(char::is_whitespace) + && !name.contains('*') + && !name.contains(char::is_control) + }; + if cfg.aliases.len() > 256 || cfg.profiles.values().map(BTreeMap::len).sum::() > 1024 + { + return Err("Model discovery supports at most 256 aliases and 1024 profiles".into()); + } + for (name, alias) in &cfg.aliases { + if !valid_name(name) + || !valid_name(&alias.target) + || cfg.aliases.contains_key(&alias.target) + { + return Err( + "Aliases require exact model IDs and must point directly at a routed model" + .into(), + ); + } + } + for (provider, models) in &cfg.profiles { + let provider = provider.to_string(); + for (name, spec) in models { + if !valid_name(name) { + return Err("Model capability profiles require exact upstream model IDs".into()); + } + tw_config::check_model_spec(&provider, name, Some(spec)) + .map_err(|error| error.to_string())?; + } + } + Ok(cfg) + } + + pub fn validate_aliases( + &self, + router: &ModelRouter, + existing: &std::collections::HashSet, + ) -> Result<(), String> { + for (name, alias) in &self.aliases { + if existing.contains(name) || router.has_model_name(name) { + return Err(format!("Alias '{name}' conflicts with an existing model")); + } + if !router.can_route_alias_target(&alias.target) { + return Err(format!( + "Alias '{name}' target '{}' has no active route", + alias.target + )); + } + } + Ok(()) + } + + pub(super) async fn load(state: &GatewayState) -> Self { + let mut cfg = state + .dynamic_config + .get(Self::KEY) + .await + .and_then(|v| Self::parse(v).ok()) + .unwrap_or_default(); + let router = state.router.load(); + // A model created after an alias was saved always keeps its name. + cfg.aliases.retain(|name, alias| { + !router.has_model_name(name) && router.can_route_alias_target(&alias.target) + }); + cfg + } + + pub(super) fn resolve(&self, model: &str) -> String { + self.aliases + .get(model) + .map_or_else(|| model.into(), |a| a.target.clone()) + } + + pub(super) fn mapper(&self) -> ModelMapper { + ModelMapper::from_config( + self.aliases + .iter() + .map(|(name, a)| (name.clone(), a.target.clone())) + .collect(), + ) + } + + pub(super) fn apply_visibility( + &self, + models: &mut Vec, + router: &ModelRouter, + identity: &GatewayRequestIdentity, + ) { + for (name, alias) in &self.aliases { + if identity.may_call(&alias.target) && router.route(&alias.target).is_some() { + models.push(name.clone()); + } + } + models.retain(|name| { + !self + .aliases + .values() + .any(|a| a.hide_original && a.target == *name) + }); + models.sort_unstable(); + models.dedup(); + } + + pub(super) fn profile(&self, provider: Uuid, model: &str) -> Option<&ModelSpec> { + self.profiles.get(&provider)?.get(model) + } +} + +struct ModelMetadata { + reported: ReportedSpec, + native: Option, +} + +type Metadata = BTreeMap; +type MetadataKey = (String, String); +type MetadataEntry = (Instant, Arc); + +#[derive(Default)] +pub(crate) struct MetadataCache(Mutex>); + +impl MetadataCache { + async fn lookup(&self, key: &MetadataKey) -> Option> { + let mut cache = self.0.lock().await; + cache.retain(|_, (expires, _)| *expires > Instant::now()); + cache.get(key).map(|(_, specs)| specs.clone()) + } + + async fn store(&self, key: MetadataKey, specs: Arc, ttl: Duration) { + if ttl.is_zero() { + return; + } + let mut cache = self.0.lock().await; + if cache.len() < 16 { + cache.insert(key, (Instant::now() + ttl, specs)); + } + } +} + +static CATALOG_FETCHES: Semaphore = Semaphore::const_new(8); + +fn metadata_url( + base: &str, + dialect: tw_dialect::ir::Dialect, + version: Option<&str>, +) -> (String, bool) { + use tw_dialect::ir::Dialect; + let query = version.map(|v| format!("client_version={v}")); + let path = if dialect == Dialect::Gemini { + "/v1beta/models" + } else { + "/v1/models" + }; + let Some(versioned) = query + .as_deref() + .filter(|_| matches!(dialect, Dialect::Chat | Dialect::Responses)) + else { + return (tw_dialect::url::upstream_url(base, path, None), false); + }; + let parsed = reqwest::Url::parse(base).ok(); + let known = |host, paths: &[&str]| { + parsed.as_ref().is_some_and(|url| { + url.scheme() == "https" + && url.host_str() == Some(host) + && url.port_or_known_default() == Some(443) + && paths.contains(&url.path().trim_end_matches('/')) + && url.username().is_empty() + && url.password().is_none() + }) + }; + if known("api.openai.com", &["", "/v1"]) { + return ( + tw_dialect::url::upstream_url( + tw_gateway::chatgpt::BASE_URL, + "/models", + Some(versioned), + ), + true, + ); + } + if known("chatgpt.com", &["/backend-api/codex"]) { + return ( + tw_dialect::url::upstream_url(base, "/models", Some(versioned)), + false, + ); + } + ( + tw_dialect::url::upstream_url(base, path, Some(versioned)), + false, + ) +} + +async fn metadata(route: &RouteEntry, ctx: &CallCtx, version: Option<&str>) -> Arc { + let upstream = &route.upstream; + if !matches!(&upstream.shape, Shape::Standard) { + return Default::default(); + } + let dialect = route.protocol.dialect(); + let (url, official_cross_origin) = metadata_url(&upstream.base_url, dialect, version); + let key = ( + upstream.negotiation_provider(route.provider_id, ctx).name, + url.clone(), + ); + if let Some(specs) = upstream.model_metadata.lookup(&key).await { + return specs; + } + let Ok(_slot) = CATALOG_FETCHES.try_acquire() else { + return Default::default(); + }; + let fetch = |url: String, official_cross_origin: bool| async move { + let mut req = upstream.client.get(url); + for (name, template) in &upstream.headers { + // The fixed OpenAI catalog host only needs OpenAI auth, never + // headers intended solely for the configured origin. + if official_cross_origin && !name.eq_ignore_ascii_case("authorization") { + continue; + } + req = req.header(name, substitute_template(template, &ctx.attrs)); + } + if route.protocol.dialect() == tw_dialect::ir::Dialect::Anthropic + && !upstream + .headers + .iter() + .any(|(k, _)| k.eq_ignore_ascii_case("anthropic-version")) + { + req = req.header("anthropic-version", tw_dialect::official::ANTHROPIC_VERSION); + } + let resp = req.send().await.ok()?.error_for_status().ok()?; + let ttl = metadata_ttl(resp.headers()); + let (body, _memory): (Value, _) = resources::read_json(resp, 1024 * 1024).await.ok()?; + let rows = body + .get("data") + .or_else(|| body.get("models"))? + .as_array()?; + Some(( + rows.iter() + .take(4096) + .filter_map(|item| { + let native = is_native(item); + let name = if native { + item["slug"].as_str()? + } else { + item["id"].as_str().or_else(|| item["name"].as_str())? + }; + let id = name.strip_prefix("models/").unwrap_or(name); + (id.len() <= 256).then(|| { + ( + id.to_owned(), + ModelMetadata { + reported: reported(item), + native: native.then(|| item.clone()), + }, + ) + }) + }) + .collect::(), + ttl, + )) + }; + let (specs, ttl) = tokio::time::timeout(Duration::from_secs(3), async { + let result = fetch(url, official_cross_origin).await; + if result.is_none() + && (official_cross_origin + || version.is_some() + && matches!( + dialect, + tw_dialect::ir::Dialect::Chat | tw_dialect::ir::Dialect::Responses + )) + { + let (ordinary_url, _) = metadata_url(&upstream.base_url, dialect, None); + fetch(ordinary_url, false).await + } else { + result + } + }) + .await + .ok() + .flatten() + .unwrap_or((Metadata::new(), Duration::from_secs(15))); + let specs = Arc::new(specs); + upstream.model_metadata.store(key, specs.clone(), ttl).await; + specs +} + +fn metadata_ttl(headers: &reqwest::header::HeaderMap) -> Duration { + use reqwest::header::{AGE, CACHE_CONTROL}; + let mut ttl = 60; + for directive in headers + .get_all(CACHE_CONTROL) + .iter() + .filter_map(|v| v.to_str().ok()) + .flat_map(|v| v.split(',')) + .map(str::trim) + { + if directive.eq_ignore_ascii_case("no-cache") || directive.eq_ignore_ascii_case("no-store") + { + return Duration::ZERO; + } + if let Some((key, value)) = directive.split_once('=') + && key.trim().eq_ignore_ascii_case("max-age") + && let Ok(seconds) = value.trim().trim_matches('"').parse::() + { + ttl = ttl.min(seconds); + } + } + let age = headers + .get(AGE) + .and_then(|v| v.to_str().ok()?.parse().ok()) + .unwrap_or(0); + Duration::from_secs(ttl.saturating_sub(age)) +} + +fn is_native(model: &Value) -> bool { + model["slug"].as_str().is_some_and(|slug| !slug.is_empty()) + && model["display_name"].is_string() + && model["supported_reasoning_levels"].is_array() + && model["shell_type"].is_string() + && model["truncation_policy"].is_object() + && (model["base_instructions"].is_string() || model["model_messages"].is_object()) +} + +fn reported(model: &Value) -> ReportedSpec { + let effort = model["supported_reasoning_levels"] + .as_array() + .map(|levels| (levels, &model["default_reasoning_level"])) + .or_else(|| { + model + .pointer("/effort/supported_levels")? + .as_array() + .map(|levels| (levels, &model["effort"]["default_level"])) + }); + let reasoning_effort = effort.and_then(|(levels, default)| { + let supported_levels: Vec<_> = levels + .iter() + .filter_map(|v| v.as_str().or_else(|| v["effort"].as_str())) + .filter_map(ReasoningLevel::from_slug) + .collect(); + if !levels.is_empty() && supported_levels.is_empty() { + return None; + } + let default_level = default + .as_str() + .and_then(ReasoningLevel::from_slug) + .filter(|v| supported_levels.contains(v)); + Some(ReasoningEffort { + supported_levels, + default_level, + }) + }); + ReportedSpec { + context_window: model["context_window"] + .as_u64() + .or_else(|| model["inputTokenLimit"].as_u64()) + .filter(|n| *n > 0), + max_output_tokens: model["max_output_tokens"] + .as_u64() + .or_else(|| model["outputTokenLimit"].as_u64()) + .filter(|n| *n > 0), + reasoning: model["supports_reasoning"] + .as_bool() + .or_else(|| model["reasoning"].as_bool()) + .or_else(|| model["thinking"].as_bool()), + reasoning_effort, + image_input: model["input_modalities"] + .as_array() + .map(|a| a.iter().any(|v| v == "image")), + } +} + +pub(super) async fn codex_catalog( + router: &ModelRouter, + cfg: &ModelDiscovery, + names: &[String], + identity: &GatewayRequestIdentity, + version: Option<&str>, +) -> Value { + let ctx = CallCtx::new(None, identity.user_id.clone(), identity.user_email.clone()); + let mut routes = BTreeMap::new(); + for route in names + .iter() + .filter_map(|name| router.route(&cfg.resolve(name))) + .flatten() + { + let key = ( + route.provider_id, + route.protocol.dialect().slug().to_owned(), + ); + routes.entry(key).or_insert_with(|| route.clone()); + } + let mut requests = stream::iter(routes.into_iter().take(64).map(|(key, route)| { + let ctx = ctx.clone(); + let version = version.map(str::to_owned); + async move { (key, metadata(&route, &ctx, version.as_deref()).await) } + })) + .buffer_unordered(8); + let deadline = tokio::time::Instant::now() + Duration::from_secs(4); + let mut specs = HashMap::new(); + while let Ok(Some((key, reported))) = tokio::time::timeout_at(deadline, requests.next()).await { + specs.insert(key, reported); + } + + let models: Vec<_> = names + .iter() + .map(|name| { + let model = cfg.resolve(name); + let mut capabilities = Vec::new(); + let mut native = Vec::new(); + for route in router.route(&model).into_iter().flatten() { + let upstream_model = route.upstream_model.as_deref().unwrap_or(&model); + let dialect = route.protocol.dialect(); + let metadata = specs + .get(&(route.provider_id, dialect.slug().to_owned())) + .and_then(|m| m.get(upstream_model)); + native.push(metadata.and_then(|metadata| metadata.native.as_ref())); + let mut spec = metadata + .map(|metadata| metadata.reported.clone()) + .unwrap_or_default(); + if let Some(manual) = cfg.profile(route.provider_id, upstream_model) { + spec.context_window = + manual.context_window.map(u64::from).or(spec.context_window); + spec.max_output_tokens = manual + .max_output_tokens + .map(u64::from) + .or(spec.max_output_tokens); + spec.reasoning = manual.reasoning.or(spec.reasoning); + spec.image_input = manual.image_input.or(spec.image_input); + spec.reasoning_effort = + manual.reasoning_effort.clone().or(spec.reasoning_effort); + } + if spec.reasoning == Some(false) { + spec.reasoning_effort = Some(ReasoningEffort { + supported_levels: Vec::new(), + default_level: None, + }); + } + if let Some(effort) = &mut spec.reasoning_effort + && dialect != tw_dialect::ir::Dialect::Responses + { + effort + .supported_levels + .retain(|level| tw_dialect::think::parse_openai(level.slug()).is_some()); + } + capabilities.push(spec); + } + let same_native = native.first().copied().flatten().filter(|first| { + native + .iter() + .all(|candidate| candidate.is_some_and(|definition| definition == *first)) + }); + project(name, &capabilities, same_native) + }) + .collect(); + json!({"models": models}) +} + +fn project(name: &str, candidates: &[ReportedSpec], native: Option<&Value>) -> Value { + let profiles: Option> = candidates + .iter() + .map(|c| c.reasoning_effort.as_ref()) + .collect(); + let levels: Vec<_> = profiles.as_ref().map_or_else(Vec::new, |profiles| { + ReasoningLevel::ALL + .iter() + .copied() + .filter(|level| { + !profiles.is_empty() && profiles.iter().all(|p| p.supported_levels.contains(level)) + }) + .collect() + }); + let default = profiles.as_ref().and_then(|p| { + let first = p.first()?.default_level?; + (levels.contains(&first) && p.iter().all(|p| p.default_level == Some(first))) + .then_some(first) + }); + let image = !candidates.is_empty() && candidates.iter().all(|s| s.image_input == Some(true)); + let mut out = native.cloned().unwrap_or_else(|| json!({ + "slug": name, "display_name": name, "description": "Available through ThinkWatch", + "base_instructions": "You are a coding assistant. Inspect relevant files before editing, use the provided tools when needed, and explain changes and verification clearly.", + "shell_type": "shell_command", "visibility": "list", "supported_in_api": true, + "priority": 0, "support_verbosity": false, "experimental_supported_tools": [], + "truncation_policy": {"mode":"tokens", "limit":10000}, "upgrade": null, + "supports_search_tool": false, "node_repl_disabled": true, + "service_tiers": [], "additional_speed_tiers": [], "default_service_tier": null, + })); + if out["slug"] != name { + out["display_name"] = json!(name); + out["upgrade"] = Value::Null; + } + out["slug"] = json!(name); + out["visibility"] = json!("list"); + out["supported_in_api"] = json!(true); + let native_levels = out["supported_reasoning_levels"].as_array(); + out["supported_reasoning_levels"] = json!( + levels + .iter() + .map(|level| { + native_levels + .and_then(|values| values.iter().find(|v| v["effort"] == level.slug()).cloned()) + .unwrap_or_else(|| json!({"effort":level.slug(), "description":level.slug()})) + }) + .collect::>() + ); + out["default_reasoning_level"] = json!(default.map(ReasoningLevel::slug)); + out["input_modalities"] = if image { + json!(["text", "image"]) + } else { + json!(["text"]) + }; + if let Some(window) = candidates + .iter() + .map(|s| s.context_window) + .collect::>>() + .and_then(|v| v.into_iter().min()) + { + if out["context_window"] != window || !out["max_context_window"].is_u64() { + out["max_context_window"] = json!(window); + } + out["context_window"] = json!(window); + } + if let Some(maximum) = candidates + .iter() + .map(|s| s.max_output_tokens) + .collect::>>() + .and_then(|v| v.into_iter().min()) + { + out["max_output_tokens"] = json!(maximum); + } + out +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn publishes_only_capabilities_shared_by_every_route() { + let a = reported( + &json!({"context_window":10000,"supported_reasoning_levels":["low","high"],"default_reasoning_level":"high","input_modalities":["text","image"]}), + ); + let b = reported(&json!({"context_window":12000,"effort":{"supported_levels":["high"]}})); + let answer = project("alias", &[a.clone(), b], None); + assert_eq!(answer["context_window"], 10000); + assert_eq!(answer["supported_reasoning_levels"][0]["effort"], "high"); + assert_eq!(answer["input_modalities"], json!(["text"])); + assert!(answer["default_reasoning_level"].is_null()); + assert_eq!( + project("alias", &[a, ReportedSpec::default()], None)["supported_reasoning_levels"], + json!([]) + ); + } +} diff --git a/crates/gateway/src/proxy/models.rs b/crates/gateway/src/proxy/models.rs index a67c7eb8..be71aa74 100644 --- a/crates/gateway/src/proxy/models.rs +++ b/crates/gateway/src/proxy/models.rs @@ -4,10 +4,12 @@ use std::collections::BTreeSet; -use axum::extract::State; +use axum::extract::{Query, State}; +use axum::http::{HeaderValue, StatusCode, header}; +use axum::response::{IntoResponse, Response}; use axum::{Extension, Json}; -use super::{GatewayRequestIdentity, GatewayState}; +use super::{GatewayRequestIdentity, GatewayState, ModelDiscovery}; use crate::model_mapping::ModelMapper; use crate::router::ModelRouter; @@ -21,8 +23,14 @@ use crate::router::ModelRouter; /// let through for `gpt-4o` although no router name is `gpt-4o`, and only /// its allow-list names it. A listed model is one a request for it is let /// through for and routed, and a model the key may not use is not listed. -fn callable_models(state: &GatewayState, identity: &GatewayRequestIdentity) -> Vec { - callable_models_in(&state.router.load(), &state.model_mapper, identity) +fn callable_models( + router: &ModelRouter, + cfg: &ModelDiscovery, + identity: &GatewayRequestIdentity, +) -> Vec { + let mut models = callable_models_in(router, &cfg.mapper(), identity); + cfg.apply_visibility(&mut models, router, identity); + models } fn callable_models_in( @@ -53,17 +61,19 @@ pub async fn list_models_handler( State(state): State, Extension(identity): Extension, ) -> Json { - let model_objects: Vec = callable_models(&state, &identity) - .into_iter() - .map(|id| { - serde_json::json!({ - "id": id, - "object": "model", - "created": 0, - "owned_by": "think-watch", + let cfg = ModelDiscovery::load(&state).await; + let model_objects: Vec = + callable_models(&state.router.load(), &cfg, &identity) + .into_iter() + .map(|id| { + serde_json::json!({ + "id": id, + "object": "model", + "created": 0, + "owned_by": "think-watch", + }) }) - }) - .collect(); + .collect(); Json(serde_json::json!({ "object": "list", @@ -79,7 +89,8 @@ pub async fn list_gemini_models_handler( State(state): State, Extension(identity): Extension, ) -> Json { - let models: Vec = callable_models(&state, &identity) + let cfg = ModelDiscovery::load(&state).await; + let models: Vec = callable_models(&state.router.load(), &cfg, &identity) .into_iter() .map(|id| { serde_json::json!({ @@ -91,6 +102,57 @@ pub async fn list_gemini_models_handler( Json(serde_json::json!({ "models": models })) } +#[derive(serde::Deserialize)] +pub struct CodexModelsQuery { + client_version: Option, +} + +/// The native Codex model picker uses the same model permission as inference. +pub async fn list_codex_models_handler( + State(state): State, + Extension(identity): Extension, + Query(query): Query, +) -> Response { + if query.client_version.as_ref().is_some_and(|version| { + version.len() > 32 + || version.split('.').count() != 3 + || !version + .split('.') + .all(|part| !part.is_empty() && part.bytes().all(|b| b.is_ascii_digit())) + }) { + return ( + StatusCode::BAD_REQUEST, + "Expected client_version in major.minor.patch format", + ) + .into_response(); + } + let cfg = ModelDiscovery::load(&state).await; + let router = state.router.load_full(); + let names = callable_models(&router, &cfg, &identity); + let catalog = super::model_discovery::codex_catalog( + &router, + &cfg, + &names, + &identity, + query.client_version.as_deref(), + ) + .await; + let body = catalog.to_string(); + if body.len() > 1024 * 1024 { + return ( + StatusCode::SERVICE_UNAVAILABLE, + "Model catalog exceeds one MiB", + ) + .into_response(); + } + let mut response = ([(header::CONTENT_TYPE, "application/json")], body).into_response(); + response.headers_mut().insert( + header::CACHE_CONTROL, + HeaderValue::from_static("private, no-store"), + ); + response +} + #[cfg(test)] mod tests { use super::*; @@ -136,6 +198,19 @@ mod tests { } } + #[test] + fn hiding_an_original_requires_the_same_access_as_its_alias() { + let cfg = ModelDiscovery::parse( + serde_json::json!({"aliases":{"code":{"target":"gpt-4o","hide_original":true}}}), + ) + .unwrap(); + let router = router(); + let allowed = narrowed(Some(&["gpt-4o"])); + assert_eq!(callable_models(&router, &cfg, &allowed), ["code"]); + assert!(allowed.may_call(&cfg.resolve("gpt-4o"))); + assert!(callable_models(&router, &cfg, &narrowed(Some(&["code"]))).is_empty()); + } + #[test] fn an_unrestricted_key_lists_what_the_router_serves() { assert_eq!( diff --git a/crates/gateway/src/proxy/pipeline.rs b/crates/gateway/src/proxy/pipeline.rs index 2979054e..4e084d53 100644 --- a/crates/gateway/src/proxy/pipeline.rs +++ b/crates/gateway/src/proxy/pipeline.rs @@ -25,7 +25,7 @@ use think_watch_common::lifecycle::stages::{ check_access, check_budget, check_limits, run_post_invoke, }; use think_watch_common::lifecycle::state::{CapturedView, Invoked, LimitCheckRecord, Raw}; -use think_watch_common::limits::{RequestLimits, usage}; +use think_watch_common::limits::{RateMetric, RequestLimits, usage}; use tw_dialect::ir::Dialect; /// Pre-flight result threaded through to `ChatPostInvokeDeps` later @@ -35,6 +35,12 @@ pub(super) struct Preflight { pub(super) limits: RequestLimits, } +#[derive(Clone, Copy)] +pub(super) enum AiRound { + Client, + HostedContinuation, +} + /// Run the three shared pre-flight stages — `check_budget` → /// `check_access` → `check_limits` — that each AI surface gates on. /// `check_limits` charges the request counters, so it goes last: a @@ -52,8 +58,24 @@ pub(super) async fn run_preflight_stages( identity: &GatewayRequestIdentity, trace_id: &str, model: &str, + round: AiRound, ) -> Result { let limits = limits_for_ai_gateway(identity); + let continuing; + let rules = match round { + AiRound::Client => &limits.rules, + AiRound::HostedContinuation => { + // Each model round still checks token limits and the owner's + // remaining budget. The client request was admitted once. + continuing = limits + .rules + .iter() + .filter(|rule| rule.metric != RateMetric::Requests) + .cloned() + .collect::>(); + &continuing + } + }; let fail_closed = state.dynamic_config.rate_limit_fail_closed().await; let raw = Raw::::new( @@ -75,7 +97,7 @@ pub(super) async fn run_preflight_stages( .map_err(short_circuit_to_response)?; let _authorized = check_limits::( access_checked, - &limits.rules, + rules, limits.owner, &state.redis, fail_closed, @@ -86,7 +108,8 @@ pub(super) async fn run_preflight_stages( // Past the rate limits, the request counts on its key and its owner, // as it has on every `requests` rule. Fail-open: the counts are for // reading. - if let Some(lineage) = limits.key_lineage + if matches!(round, AiRound::Client) + && let Some(lineage) = limits.key_lineage && let Err(e) = usage::record_request(&state.redis, limits.owner, lineage, chrono::Utc::now()).await { diff --git a/crates/gateway/src/proxy/responses_ws.rs b/crates/gateway/src/proxy/responses_ws.rs index 9d67f6a9..c8fbe0cc 100644 --- a/crates/gateway/src/proxy/responses_ws.rs +++ b/crates/gateway/src/proxy/responses_ws.rs @@ -87,7 +87,7 @@ async fn serve( let mut queued: VecDeque = VecDeque::new(); // The connection's most recent response. let mut last: Option = None; - loop { + 'connection: loop { let message = match queued.pop_front() { Some(m) => m, None => match rx.next().await { @@ -115,8 +115,21 @@ async fn serve( RESPONSES, "/v1/responses", None, - ) - .await; + ); + // Hosted web turns can wait for a whole model answer before + // their first event. Keep reading the socket so a disconnect + // cancels that outbound work too. + tokio::pin!(answer); + let answer = loop { + tokio::select! { + result = &mut answer => break result, + incoming = rx.next() => match incoming { + Some(Ok(m @ (Message::Text(_) | Message::Binary(_)))) if queued.len() < 8 => queued.push_back(m), + Some(Ok(Message::Ping(_) | Message::Pong(_))) => {}, + _ => break 'connection, + }, + } + }; match answer { Ok(resp) => { let (turn, completed) = relay(resp, &mut tx, &mut rx, &mut queued).await; @@ -177,9 +190,13 @@ impl Last { // Output item ids refer to the upstream's store, which a // `store: false` turn never wrote to; sent back as input they // would be looked up and not found. `call_id` is what ties a - // tool result to its call, and stays. + // tool result to its call, and stays. Locally hosted web + // items keep their id so the gateway can restore their history. + let hosted = super::web_tools::is_hosted_responses_item(&item); if let Some(o) = item.as_object_mut() { - o.remove("id"); + if !hosted { + o.remove("id"); + } o.remove("status"); } items.push(item); @@ -350,6 +367,22 @@ mod tests { assert_eq!(turn["input"], "two"); } + #[test] + fn a_hosted_web_item_keeps_its_id_for_history_replay() { + let done = serde_json::json!({"id": "resp_1", "output": [ + {"id":"ws_tw_example", "type":"web_search_call", "status":"completed", + "action":{"type":"search","query":"docs"}}, + ]}); + let last = Last::of(Vec::new(), &done).unwrap(); + let mut next = create(serde_json::json!({"type":"response.create", "model":"m", + "previous_response_id":"resp_1", "input":"continue"})); + assert!(continue_from(&mut next, Some(&last)).is_some()); + assert_eq!(next["input"][0]["id"], "ws_tw_example"); + assert!(super::super::web_tools::is_hosted_responses_item( + &next["input"][0] + )); + } + #[test] fn a_create_frame_is_the_request_body_as_a_stream() { let v = body(r#"{"type":"response.create","model":"m","input":"hi","stream":false}"#); diff --git a/crates/gateway/src/proxy/transport.rs b/crates/gateway/src/proxy/transport.rs index 8d301b4f..c6c5037b 100644 --- a/crates/gateway/src/proxy/transport.rs +++ b/crates/gateway/src/proxy/transport.rs @@ -68,6 +68,9 @@ pub struct Upstream { /// The models this upstream refused the cache breakpoints a /// conversion adds for (see `proxy::cache_marks`). pub(crate) cache_marks: crate::proxy::cache_marks::Refused, + pub(crate) history: tw_gateway::history_memory::Cache, + pub(crate) response_modes: tw_gateway::stream_mode::Cache, + pub(crate) model_metadata: super::model_discovery::MetadataCache, /// The slowest generation a stream from this upstream may keep up /// (see `proxy::min_speed`). `None` ⇒ not measured. pub min_speed: Option, @@ -82,6 +85,9 @@ impl Upstream { shape, label: label.to_string(), cache_marks: Default::default(), + history: Default::default(), + response_modes: Default::default(), + model_metadata: Default::default(), min_speed: None, } } @@ -115,13 +121,50 @@ impl Upstream { extra: &[(String, String)], ctx: &CallCtx, ) -> Result { + self.send_with_refusal(body, path, query, dialect, extra, ctx) + .await + .map_err(|refusal| refusal.error) + } + + pub(crate) async fn send_with_refusal( + &self, + body: Vec, + path: &str, + query: Option<&str>, + dialect: tw_dialect::ir::Dialect, + extra: &[(String, String)], + ctx: &CallCtx, + ) -> Result { let resp = self .request(body, path, query, dialect, extra, ctx) .await? .send() .await .map_err(transport_error)?; - check_status(resp, &self.label).await + inspect_status(resp, &self.label).await + } + + /// Core's negotiation keys use a resolved account identity. Pass only a + /// digest of configured headers and their request-specific substitutions. + pub(crate) fn negotiation_provider( + &self, + id: uuid::Uuid, + ctx: &CallCtx, + ) -> tw_config::Provider { + use sha2::{Digest, Sha256}; + let mut hash = Sha256::new(); + for (name, template) in &self.headers { + let value = crate::call_ctx::substitute_template(template, &ctx.attrs); + for field in [name.as_str(), value.as_str()] { + hash.update(field.len().to_le_bytes()); + hash.update(field); + } + } + serde_json::from_value(serde_json::json!({ + "name": format!("{id}:{}", hex::encode(hash.finalize())), + "base_url": self.base_url, + })) + .expect("Core provider identity has the required fields") } /// The request [`send`](Self::send) sends, built but not sent. @@ -288,10 +331,40 @@ fn retry_after(headers: &reqwest::header::HeaderMap) -> Option { /// and forwarding them verbatim turns the gateway into a leak. An auth /// error keeps its truncated body too, but not in the text the caller /// sees. The full body goes to the log. +pub(crate) struct Refusal { + pub error: GatewayError, + pub repair: Option>, +} + +pub(crate) struct RepairEvidence { + pub status: u16, + pub headers: reqwest::header::HeaderMap, + pub body: bytes::Bytes, +} + +impl From for Refusal { + fn from(error: GatewayError) -> Self { + Self { + error, + repair: None, + } + } +} + +#[cfg(test)] async fn check_status( resp: reqwest::Response, label: &str, ) -> Result { + inspect_status(resp, label) + .await + .map_err(|refusal| refusal.error) +} + +async fn inspect_status( + resp: reqwest::Response, + label: &str, +) -> Result { let status = resp.status(); if status == reqwest::StatusCode::TOO_MANY_REQUESTS { // Echoed to the caller in whole seconds, as the upstream wrote it. @@ -300,14 +373,16 @@ async fn check_status( .get(reqwest::header::RETRY_AFTER) .and_then(|v| v.to_str().ok()) .and_then(crate::error::parse_retry_after_seconds); - return Err(GatewayError::UpstreamRateLimited { retry_after_secs }); + return Err(GatewayError::UpstreamRateLimited { retry_after_secs }.into()); } if !status.is_success() { let retry_after = retry_after(resp.headers()); - let body = think_watch_common::resources::read_response(resp, 64 * 1024) - .await - .map(|bytes| String::from_utf8_lossy(&bytes).into_owned()) - .unwrap_or_else(|error| error.to_string()); + let headers = matches!(status.as_u16(), 400 | 422 | 501).then(|| resp.headers().clone()); + let raw = think_watch_common::resources::read_response(resp, 64 * 1024).await; + let body = match &raw { + Ok(bytes) => String::from_utf8_lossy(bytes), + Err(error) => std::borrow::Cow::Owned(error.to_string()), + }; tracing::warn!(provider = label, status = %status, body = %body, "upstream returned non-2xx"); const CLIENT_MAX: usize = 512; let shown = if body.len() > CLIENT_MAX { @@ -318,13 +393,18 @@ async fn check_status( } format!("{}…[truncated]", &body[..end]) } else { - body + body.into_owned() }; - return Err(status_error( - status.as_u16(), - retry_after, - format!("{label}: {shown}"), - )); + return Err(Refusal { + error: status_error(status.as_u16(), retry_after, format!("{label}: {shown}")), + repair: headers.zip(raw.ok()).map(|(headers, body)| { + Box::new(RepairEvidence { + status: status.as_u16(), + headers, + body, + }) + }), + }); } Ok(resp) } diff --git a/crates/gateway/src/proxy/web_tools/LICENSE.core-web-tools b/crates/gateway/src/proxy/web_tools/LICENSE.core-web-tools new file mode 100644 index 00000000..c7526d04 --- /dev/null +++ b/crates/gateway/src/proxy/web_tools/LICENSE.core-web-tools @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 ThinkWatch + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/crates/gateway/src/proxy/web_tools/history.rs b/crates/gateway/src/proxy/web_tools/history.rs new file mode 100644 index 00000000..e33eefa3 --- /dev/null +++ b/crates/gateway/src/proxy/web_tools/history.rs @@ -0,0 +1,248 @@ +// From ThinkWatch-Core v0.98.1. Copyright (c) 2026 ThinkWatch (MIT). +// The upstream license is in LICENSE.core-web-tools. + +//! Replay our own hosted-tool items as ordinary tool exchanges on the next turn. +//! Never forward fabricated provider-encrypted search content to an upstream. + +use std::collections::HashMap; +use std::sync::Mutex; +use std::time::{Duration, Instant}; + +use serde_json::{Value, json}; +use tw_dialect::ir::Dialect; +use tw_types::Msg; + +use super::{ + error, + protocol::{Call, FIND, OPEN, SEARCH}, +}; + +#[derive(Default)] +pub(crate) struct History(Mutex>); + +pub(super) fn might_contain_hosted_items(body: &[u8]) -> bool { + [b"ws_tw_".as_slice(), b"srvtoolu_tw_", b"\\u"] + .iter() + .any(|marker| body.windows(marker.len()).any(|window| window == *marker)) +} + +pub(in crate::proxy) fn is_hosted_responses_item(item: &Value) -> bool { + item["type"] == "web_search_call" + && item["id"] + .as_str() + .is_some_and(|id| id.starts_with("ws_tw_")) +} + +struct Entry { + at: Instant, + call: Value, + result: Value, +} + +impl History { + pub fn remember( + &self, + identity: &str, + dialect: Dialect, + call: &Call, + result: &tw_api::WebToolResult, + ) { + let (name, arguments) = match &call.request { + tw_api::WebToolRequest::Search { query, .. } => (SEARCH, json!({"query":query})), + tw_api::WebToolRequest::OpenPage { url, offset } => { + (OPEN, json!({"url":url,"offset":offset})) + } + tw_api::WebToolRequest::FindInPage { url, pattern } => { + (FIND, json!({"url":url,"pattern":pattern})) + } + }; + let text = serde_json::to_string(result).unwrap_or_default(); + let (id, item, output) = if dialect == Dialect::Anthropic { + ( + format!("srvtoolu_tw_{}", call.native_id), + json!({"type":"tool_use","id":call.id,"name":name,"input":arguments}), + json!({"type":"tool_result","tool_use_id":call.id,"content":text}), + ) + } else { + ( + format!("ws_tw_{}", call.native_id), + json!({"type":"function_call","call_id":call.id,"name":name,"arguments":arguments.to_string()}), + json!({"type":"function_call_output","call_id":call.id,"output":text}), + ) + }; + let mut entries = self.0.lock().unwrap_or_else(|e| e.into_inner()); + entries.retain(|_, e| e.at.elapsed() < Duration::from_secs(1800)); + if entries.len() >= 256 + && let Some(oldest) = entries + .iter() + .min_by_key(|(_, e)| e.at) + .map(|(k, _)| k.clone()) + { + entries.remove(&oldest); + } + entries.insert( + (identity.into(), id), + Entry { + at: Instant::now(), + call: item, + result: output, + }, + ); + } + + fn get(&self, identity: &str, id: &str) -> Result<(Value, Value), Msg> { + let entries = self.0.lock().unwrap_or_else(|e| e.into_inner()); + let e = entries.get(&(identity.into(), id.into())).filter(|e| e.at.elapsed() < Duration::from_secs(1800)) + .ok_or_else(|| error("Core's web-tool history has expired; start a new conversation or resend the source text"))?; + Ok((e.call.clone(), e.result.clone())) + } + + pub fn restore( + &self, + identity: &str, + dialect: Dialect, + body: &mut Value, + tools: bool, + ) -> Result { + let mut changed = false; + if dialect == Dialect::Responses { + if let Some(items) = body["input"].as_array_mut() { + let mut restored = Vec::new(); + for item in std::mem::take(items) { + if is_hosted_responses_item(&item) { + changed = true; + let saved = self.get(identity, item["id"].as_str().unwrap_or("")).ok(); + if let Some((call, output)) = saved.as_ref().filter(|_| tools) { + restored.extend([call.clone(), output.clone()]); + } else { + restored.push(json!({"role":"assistant","content":[{"type":"output_text","text":recalled(saved.as_ref().map(|(_, out)| out), &item)}]})); + } + } else { + restored.push(item); + } + } + *items = restored; + } + } else if dialect == Dialect::Anthropic + && let Some(messages) = body["messages"].as_array_mut() + { + let mut restored = Vec::new(); + for message in std::mem::take(messages) { + let Some(blocks) = message["content"] + .as_array() + .filter(|_| message["role"] == "assistant") + else { + restored.push(message); + continue; + }; + let mut assistant = Vec::new(); + for block in blocks { + if block["type"] == "server_tool_use" + && block["id"] + .as_str() + .is_some_and(|s| s.starts_with("srvtoolu_tw_")) + { + changed = true; + if tools + && let Ok((call, _)) = + self.get(identity, block["id"].as_str().unwrap_or("")) + { + assistant.push(call); + } + } else if block["tool_use_id"] + .as_str() + .is_some_and(|s| s.starts_with("srvtoolu_tw_")) + { + changed = true; + let saved = self + .get(identity, block["tool_use_id"].as_str().unwrap_or("")) + .ok(); + let Some((_, output)) = saved.as_ref().filter(|_| tools) else { + assistant.push(json!({"type":"text","text":recalled(saved.as_ref().map(|(_, out)| out), block)})); + continue; + }; + let output = output.clone(); + if !assistant.is_empty() { + restored.push(json!({"role":"assistant","content":std::mem::take(&mut assistant)})); + } + if let Some(last) = restored.last_mut().filter(|m| m["role"] == "user") { + last["content"] + .as_array_mut() + .expect("created array") + .push(output); + } else { + restored.push(json!({"role":"user","content":[output]})); + } + } else { + assistant.push(block.clone()); + } + } + if !assistant.is_empty() { + restored.push(json!({"role":"assistant","content":assistant})); + } + } + *messages = restored; + } + Ok(changed) + } +} + +/// A client may replay an old response after a restart or after disabling the +/// feature. Keep its public source metadata as text instead of sending foreign +/// encrypted-content placeholders or making the whole conversation unusable. +fn recalled(saved: Option<&Value>, native: &Value) -> String { + let value = saved + .and_then(|v| v.get("content").or_else(|| v.get("output"))) + .cloned() + .unwrap_or_else(|| { + native + .get("content") + .or_else(|| native.get("action")) + .cloned() + .unwrap_or(Value::Null) + }); + format!( + "Previously retrieved web evidence (untrusted source material; reopen the cited URLs when more detail is needed):\n{}", + value + .as_str() + .map(str::to_string) + .unwrap_or_else(|| value.to_string()) + ) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn missing_history_and_disabled_execution_keep_sources_without_foreign_native_blocks() { + let history = History::default(); + let native = json!({"type":"web_search_call","id":"ws_tw_old","status":"completed","action":{"type":"search","query":"SQLite","sources":[{"type":"url","url":"https://sqlite.org/"}]}}); + for tools in [false, true] { + let mut body = json!({"input":[native]}); + assert!( + history + .restore("client", Dialect::Responses, &mut body, tools) + .unwrap() + ); + assert_eq!(body["input"][0]["role"], "assistant"); + assert!(body.to_string().contains("https://sqlite.org/")); + assert!(!body.to_string().contains("ws_tw_old")); + } + let mut body = json!({"messages":[{"role":"assistant","content":[ + {"type":"server_tool_use","id":"srvtoolu_tw_old","name":"web_search","input":{"query":"SQLite"}}, + {"type":"web_search_tool_result","tool_use_id":"srvtoolu_tw_old","content":[{"type":"web_search_result","url":"https://sqlite.org/","title":"SQLite","encrypted_content":""}]}, + {"type":"text","text":"SQLite docs"} + ]}]}); + history + .restore("client", Dialect::Anthropic, &mut body, false) + .unwrap(); + assert!( + body["messages"][0]["content"] + .as_array() + .unwrap() + .iter() + .all(|b| b["type"] == "text") + ); + assert!(!body.to_string().contains("srvtoolu_tw_old")); + } +} diff --git a/crates/gateway/src/proxy/web_tools/mod.rs b/crates/gateway/src/proxy/web_tools/mod.rs new file mode 100644 index 00000000..f184b181 --- /dev/null +++ b/crates/gateway/src/proxy/web_tools/mod.rs @@ -0,0 +1,770 @@ +//! Enterprise authorization, guard checks and accounting around Core's web executor. + +mod history; +mod protocol; +mod wire; + +pub(super) use history::is_hosted_responses_item; + +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::Duration; + +use axum::body::{Bytes, to_bytes}; +use axum::http::{HeaderMap, HeaderValue, header}; +use axum::response::{IntoResponse, Response}; +use serde::Deserialize; +use serde_json::{Value, json}; +use think_watch_common::audit::AuditLogger; +use tokio::sync::Mutex; +use tw_api::{WebToolRequest, WebToolResult, WebToolsConfig, WebToolsMode}; +use tw_dialect::ir::{Dialect, Usage}; +use tw_gateway::web_tools::WebTools; +use tw_types::{Msg, msg}; +use uuid::Uuid; + +use super::generate::{ClientSurface, run}; +use super::headers::{request_id_header, resolve_trace_id}; +use super::pipeline::AiRound; +use super::{EarlyCancelSlot, GatewayErrorResponse, GatewayRequestIdentity, GatewayState}; +use crate::error::GatewayError; +use crate::guards::{Caller, Guards}; +use crate::redaction::Shape; +use think_watch_common::resources; + +const MAX_BODY: usize = 8 * 1024 * 1024; + +#[derive(Default, Deserialize)] +#[serde(default, deny_unknown_fields)] +pub struct HostedWebSettings { + enabled_lineages: Vec, + execution: WebToolsConfig, +} + +impl HostedWebSettings { + pub const KEY: &str = "gateway.web_tools"; + + pub fn parse(value: Value) -> Result { + let settings: Self = serde_json::from_value(value).map_err(|e| e.to_string())?; + if settings.enabled_lineages.len() > 256 + || (settings.execution.mode == WebToolsMode::Core + && settings.enabled_lineages.is_empty()) + { + return Err("Hosted web tools require 1–256 authorized API key lineage IDs".into()); + } + core_config(&settings.execution)?; + Ok(settings) + } + + fn permits(&self, identity: &GatewayRequestIdentity) -> bool { + self.execution.mode == WebToolsMode::Core + && identity + .api_key_lineage_id + .as_deref() + .and_then(|id| Uuid::parse_str(id).ok()) + .is_some_and(|id| self.enabled_lineages.contains(&id)) + } + + pub async fn diagnose( + &self, + runtime: &HostedWebRuntime, + guards: &Guards, + audit: &AuditLogger, + user: Uuid, + mut request: WebToolRequest, + ) -> Result { + if self.execution.mode != WebToolsMode::Core { + return Err(GatewayError::TransformError( + "Save hosted web tools in Core mode before testing".into(), + )); + } + let caller = Caller { + user_id: Some(user.to_string()), + trace_id: Uuid::new_v4().to_string(), + model: "thinkwatch-web-test".into(), + ..Default::default() + }; + let identity = format!("console:{user}"); + let invocation = Invocation { + runtime, + audit, + guards, + caller: &caller, + options: &self.execution, + scoped: &self.execution, + lineage: &identity, + max_concurrent: self.execution.max_concurrent, + diagnostics: true, + }; + let mut result = invocation.execute(&mut request).await?; + for warning in &mut result.warnings { + warning.text = diagnostic_text(guards, &warning.text); + warning.args.clear(); + } + Ok(result) + } +} + +fn core_config(settings: &WebToolsConfig) -> Result { + let config = serde_json::from_value( + json!({"version": tw_config::SCHEMA_VERSION, "web_tools": settings}), + ) + .map_err(|e| e.to_string())?; + match tw_config::validate(&config) { + // This adapter only uses the executor; the enterprise gateway authenticates callers. + Ok(()) | Err(tw_config::ValidationError::NoClients) => Ok(config), + Err(error) => Err(error.to_string()), + } +} + +async fn enabled( + state: &GatewayState, + identity: &GatewayRequestIdentity, +) -> Option { + let settings = + HostedWebSettings::parse(state.dynamic_config.get(HostedWebSettings::KEY).await?).ok()?; + settings.permits(identity).then_some(settings) +} + +pub struct HostedWebRuntime { + search: Mutex)>>, + executors: Mutex)>>, + history: history::History, + active: AtomicUsize, +} + +impl Default for HostedWebRuntime { + fn default() -> Self { + Self { + search: Mutex::default(), + executors: Mutex::default(), + history: history::History::default(), + active: AtomicUsize::new(0), + } + } +} + +impl HostedWebRuntime { + fn enter(&self, max: u32) -> Result, GatewayError> { + self.active + .try_update(Ordering::AcqRel, Ordering::Acquire, |active| { + (active < max as usize).then_some(active + 1) + }) + .map_err(|_| GatewayError::rate_limited("web_tools_concurrency", 1))?; + Ok(ToolSlot(&self.active)) + } + + async fn executor(&self, options: WebToolsConfig) -> Result, GatewayError> { + let mut entries = self.executors.lock().await; + if let Some((_, executor)) = entries.iter().find(|(settings, _)| *settings == options) { + return Ok(executor.clone()); + } + let executor = make_executor(&options)?; + if entries.len() >= 8 { + entries.remove(0); + } + entries.push((options, executor.clone())); + Ok(executor) + } + + /// Engine cooldowns and minimum intervals are shared across client domain + /// filters. Only page readers require a dedicated executor for redirects. + async fn search_executor( + &self, + options: &WebToolsConfig, + ) -> Result, GatewayError> { + let mut entry = self.search.lock().await; + if let Some((stored, executor)) = &*entry + && stored == options + { + return Ok(executor.clone()); + } + let executor = make_executor(options)?; + *entry = Some((options.clone(), executor.clone())); + Ok(executor) + } +} + +fn make_executor(options: &WebToolsConfig) -> Result, GatewayError> { + let config = core_config(options).map_err(GatewayError::TransformError)?; + WebTools::new(&config) + .map(Arc::new) + .map_err(|_| GatewayError::ProviderError("Could not build the hosted web client".into())) +} + +struct ToolSlot<'a>(&'a AtomicUsize); + +impl Drop for ToolSlot<'_> { + fn drop(&mut self) { + self.0.fetch_sub(1, Ordering::Release); + } +} + +fn error(detail: impl Into) -> Msg { + msg!("gw.web_tools.failed", detail = detail.into() => "Web tools: {detail}") +} + +fn validate_domains(domains: &[String]) -> Result<(), Msg> { + let options = WebToolsConfig { + allowed_domains: domains.to_vec(), + ..WebToolsConfig::default() + }; + core_config(&options).map(|_| ()).map_err(error) +} + +fn request_policy( + mut options: WebToolsConfig, + plan: &protocol::Plan, +) -> Result { + let contains = |parent: &str, child: &str| { + parent == child || child.strip_suffix(parent).is_some_and(|s| s.ends_with('.')) + }; + let normalize = |names: &[String]| { + names + .iter() + .map(|name| name.to_ascii_lowercase()) + .collect::>() + }; + let requested = normalize(&plan.allowed); + options.allowed_domains = match (options.allowed_domains.is_empty(), requested.is_empty()) { + (true, _) => requested, + (false, true) => normalize(&options.allowed_domains), + (false, false) => { + let configured = normalize(&options.allowed_domains); + let overlap: std::collections::BTreeSet<_> = configured + .iter() + .flat_map(|a| { + requested.iter().filter_map(move |b| { + if contains(a, b) { + Some(b.clone()) + } else if contains(b, a) { + Some(a.clone()) + } else { + None + } + }) + }) + .collect(); + if overlap.is_empty() { + return Err(GatewayError::PolicyBlocked( + "Web tool domains do not match the gateway allowlist".into(), + )); + } + overlap.into_iter().collect() + } + }; + options.blocked_domains.extend(normalize(&plan.blocked)); + options.blocked_domains.sort_unstable(); + options.blocked_domains.dedup(); + Ok(options) +} + +pub(super) struct HostedRequest<'a> { + pub state: &'a GatewayState, + pub headers: &'a HeaderMap, + pub identity: &'a GatewayRequestIdentity, + pub cancel: Option, + pub arrived: Option, + pub body: &'a Bytes, + pub surface: ClientSurface, + pub path: &'a str, + pub query: Option<&'a str>, +} + +impl HostedRequest<'_> { + pub(super) async fn execute(self) -> Option> { + let lineage = self.identity.api_key_lineage_id.as_deref()?; + let settings = enabled(self.state, self.identity).await; + if settings.is_none() && !history::might_contain_hosted_items(self.body) { + return None; + } + let mut body: Value = serde_json::from_slice(self.body).ok()?; + let plan = if settings.is_some() { + match protocol::recognize(self.surface.dialect, &body) { + Ok(plan) => plan, + Err(err) => return Some(Err(GatewayError::TransformError(err.text).into())), + } + } else { + None + }; + let runtime = &self.state.hosted_web; + let standalone = plan.as_ref().and_then(|p| p.standalone(&body)); + let restored = + runtime + .history + .restore(lineage, self.surface.dialect, &mut body, plan.is_some()); + let plan = match (restored, plan) { + (Err(e), _) => return Some(Err(GatewayError::TransformError(e.text).into())), + (Ok(false), None) => return None, + (Ok(true), None) => { + return Some( + self.forward( + &body, + self.headers.clone(), + self.cancel.clone(), + AiRound::Client, + ) + .await, + ); + } + (Ok(_), Some(plan)) => plan, + }; + Some( + match tokio::time::timeout( + Duration::from_secs(110), + self.hosted(settings?.execution, plan, body, standalone, lineage), + ) + .await + { + Ok(result) => result, + Err(_) => Err(GatewayError::ProviderTimeout( + "Hosted web tools exceeded the turn deadline".into(), + ) + .into()), + }, + ) + } + + async fn forward( + &self, + body: &Value, + headers: HeaderMap, + cancel: Option, + round: AiRound, + ) -> Result { + let bytes = serde_json::to_vec(body).expect("JSON request"); + if bytes.len() > MAX_BODY.min(resources::policy().max_body_bytes) { + return Err(GatewayError::TransformError( + "Hosted web-tool conversation exceeds the request limit".into(), + ) + .into()); + } + let mut budget = resources::body_budget().reserve(); + if !budget.grow(bytes.len().saturating_mul(resources::BODY_HEADROOM)) { + return Err(GatewayError::rate_limited("gateway_memory", 1).into()); + } + run( + self.state.clone(), + headers, + self.identity.clone(), + cancel, + self.arrived, + budget.wrap(bytes.into()), + self.surface, + self.path, + self.query, + round, + ) + .await + } + + async fn forward_to_upstream_tools(&self) -> Result { + let mut body: Value = serde_json::from_slice(self.body) + .map_err(|error| GatewayError::TransformError(error.to_string()))?; + if let Some(lineage) = self.identity.api_key_lineage_id.as_deref() { + self.state + .hosted_web + .history + .restore(lineage, self.surface.dialect, &mut body, false) + .map_err(|error| GatewayError::TransformError(error.text))?; + } + let mut headers = self.headers.clone(); + headers.insert("x-trace-id", request_id_header(&Uuid::new_v4().to_string())); + self.forward(&body, headers, None, AiRound::HostedContinuation) + .await + } + + async fn hosted( + &self, + options: WebToolsConfig, + plan: protocol::Plan, + mut body: Value, + standalone: Option, + lineage: &str, + ) -> Result { + let model = body["model"] + .as_str() + .filter(|s| !s.is_empty()) + .ok_or_else(|| GatewayError::TransformError("Missing 'model' field".into()))? + .to_owned(); + let cfg = super::ModelDiscovery::load(self.state).await; + let mapped = cfg.resolve(&model); + let id = resolve_trace_id(self.headers); + let caller = Caller::of(self.identity, &id, &mapped); + let guards = self.state.guards.load_full(); + let stream = body["stream"] == true; + let fallback = options.fallback_to_upstream; + let max_concurrent = options.max_concurrent; + let max_uses = plan + .max_uses + .unwrap_or(options.max_tool_calls) + .min(options.max_tool_calls) as usize; + let runtime = &self.state.hosted_web; + let scoped = request_policy(options.clone(), &plan)?; + let invocation = Invocation { + runtime, + audit: &self.state.audit, + guards: &guards, + caller: &caller, + options: &options, + scoped: &scoped, + lineage, + max_concurrent, + diagnostics: false, + }; + + if let Some(request) = standalone { + super::pipeline::run_preflight_stages( + self.state, + self.identity, + &id, + &mapped, + AiRound::Client, + ) + .await?; + if self.state.router.load().route(&mapped).is_none() { + return Err(GatewayError::TransformError("No route for this model".into()).into()); + } + if let Some(cancel) = &self.cancel { + cancel.request(&id, None); + cancel.model(&mapped); + } + let mut call = protocol::Call { + native_id: Uuid::new_v4().simple().to_string(), + id: format!("toolu_tw_{}", Uuid::new_v4().simple()), + request, + }; + let result = match invocation.tool(&mut call).await { + Ok(result) => result, + Err(GatewayError::ProviderError(_)) if fallback => { + return self.forward_to_upstream_tools().await; + } + Err(error) => return Err(error.into()), + }; + runtime + .history + .remember(lineage, plan.dialect, &call, &result); + return render( + plan.dialect, + stream, + &id, + &protocol::standalone_response(&model, &call, &result), + ); + } + + plan.rewrite(&mut body) + .map_err(|e| GatewayError::TransformError(e.text))?; + let mut usage = Usage::default(); + let mut exchanges = Vec::new(); + let mut retained_bytes = 0usize; + let mut sources = Vec::new(); + let mut used = 0; + let key = if plan.dialect == Dialect::Anthropic { + "content" + } else { + "output" + }; + let mut headers = self.headers.clone(); + headers.insert("x-trace-id", request_id_header(&id)); + loop { + let response = self + .forward( + &body, + headers.clone(), + (used == 0).then(|| self.cancel.clone()).flatten(), + if used == 0 { + AiRound::Client + } else { + AiRound::HostedContinuation + }, + ) + .await?; + if !response.status().is_success() { + return Ok(response); + } + let bytes = to_bytes(response.into_body(), MAX_BODY) + .await + .map_err(|_| { + GatewayError::ProviderInvalidResponse( + "Web-tool model response exceeded the buffer or ended prematurely".into(), + ) + })?; + let mut answer: Value = serde_json::from_slice(&bytes).map_err(|_| { + GatewayError::ProviderInvalidResponse("Web-tool model response must be JSON".into()) + })?; + sum_usage(plan.dialect, &answer, &mut usage); + let mut calls = plan + .calls(&answer) + .map_err(|e| GatewayError::ProviderInvalidResponse(e.text))?; + if used + calls.len() > max_uses { + return Err(GatewayError::PolicyBlocked( + "Hosted web-tool call limit exceeded".into(), + ) + .into()); + } + let mut results = Vec::with_capacity(calls.len()); + for call in &mut calls { + let result = match invocation.tool(call).await { + Ok(result) => result, + Err(GatewayError::ProviderError(_)) + if used == 0 && results.is_empty() && fallback => + { + return self.forward_to_upstream_tools().await; + } + Err(error) => return Err(error.into()), + }; + runtime + .history + .remember(lineage, plan.dialect, call, &result); + sources.extend( + result + .sources + .iter() + .map(|source| (source.url.clone(), source.title.clone())), + ); + results.push((call.clone(), result)); + } + used += results.len(); + let items = answer[key].as_array().ok_or_else(|| { + GatewayError::ProviderInvalidResponse( + "Web-tool model response has no content".into(), + ) + })?; + let mut external = Vec::new(); + for item in items { + let id = if plan.dialect == Dialect::Anthropic { + &item["id"] + } else { + &item["call_id"] + }; + if let Some((call, result)) = results.iter().find(|(call, _)| *id == call.id) { + exchanges.extend(protocol::native_blocks(plan.dialect, call, result)); + } else if matches!( + item["type"].as_str(), + Some("tool_use" | "function_call" | "custom_tool_call") + ) { + external.push(item.clone()); + } else { + exchanges.push(item.clone()); + } + } + retained_bytes = retained_bytes.saturating_add(bytes.len()); + if retained_bytes > MAX_BODY { + return Err(GatewayError::ProviderInvalidResponse( + "Hosted web-tool exchanges exceed the response limit".into(), + ) + .into()); + } + if results.is_empty() || !external.is_empty() { + exchanges.extend(external); + answer[key] = json!(exchanges); + answer["usage"] = if plan.dialect == Dialect::Anthropic { + tw_dialect::anthropic::response::usage_json(&usage) + } else { + tw_dialect::responses::response::usage_json(&usage) + }; + if plan.dialect == Dialect::Responses { + citations(&mut answer, &sources); + } + return render(plan.dialect, stream, &id, &answer); + } + plan.append(&mut body, &answer, &results) + .map_err(|e| GatewayError::TransformError(e.text))?; + headers.insert("x-trace-id", request_id_header(&Uuid::new_v4().to_string())); + } + } +} + +struct Invocation<'a> { + runtime: &'a HostedWebRuntime, + audit: &'a AuditLogger, + guards: &'a Guards, + caller: &'a Caller, + options: &'a WebToolsConfig, + scoped: &'a WebToolsConfig, + lineage: &'a str, + max_concurrent: u32, + diagnostics: bool, +} + +impl Invocation<'_> { + async fn tool(&self, call: &mut protocol::Call) -> Result { + self.execute(&mut call.request).await + } + + async fn execute(&self, request: &mut WebToolRequest) -> Result { + let audit = self.audit; + let text = serde_json::to_string(request).expect("web-tool request JSON"); + let wrapped = json!({"messages":[{"role":"user", "content":text}]}).to_string(); + let screening = self + .guards + .content + .screen(Dialect::Anthropic, wrapped.as_bytes()); + crate::content_filter::record(audit, self.caller, self.guards.content.mode, &screening); + if let Some(hit) = screening.refusal() { + return Err(crate::content_filter::refusal(hit, &self.guards.redaction)); + } + let bytes = screening + .body + .map_or_else(|| wrapped.into_bytes(), |body| body.to_vec()); + let shape = Shape::Generate(Dialect::Anthropic); + let (findings, ledger) = self.guards.redaction.look(&bytes, shape); + crate::redaction::record(audit, self.caller, &self.guards.redaction, &findings); + let (protected, _) = self.guards.redaction.replace(bytes, &ledger, shape); + let wrapped: Value = serde_json::from_slice(&protected).map_err(|_| { + GatewayError::PolicyBlocked("Web-tool input changed by request protection".into()) + })?; + *request = serde_json::from_str( + wrapped["messages"][0]["content"] + .as_str() + .unwrap_or_default(), + ) + .map_err(|_| { + GatewayError::PolicyBlocked("Web-tool input changed by request protection".into()) + })?; + let runtime = self.runtime; + let _slot = runtime.enter(self.max_concurrent)?; + let (action, executor) = match request { + WebToolRequest::Search { + allowed_domains, + blocked_domains, + .. + } => { + *allowed_domains = self.scoped.allowed_domains.clone(); + *blocked_domains = self.scoped.blocked_domains.clone(); + ("search", runtime.search_executor(self.options).await?) + } + WebToolRequest::OpenPage { .. } => { + ("open_page", runtime.executor(self.scoped.clone()).await?) + } + WebToolRequest::FindInPage { .. } => { + ("find_in_page", runtime.executor(self.scoped.clone()).await?) + } + }; + let result = executor.execute(self.lineage, request).await; + let event = if self.diagnostics { + "gateway.web_tool_test" + } else { + "gateway.web_tool" + }; + audit.log(self.caller.audit(event).resource("thinkwatch-web").detail(json!({ + "trace_id": self.caller.trace_id, "action": action, "success": result.is_ok(), + "cached": result.as_ref().is_ok_and(|r| r.cached), "sources": result.as_ref().map_or(0, |r| r.sources.len()), + }))); + result.map_err(|error| { + GatewayError::ProviderError(if self.diagnostics { + diagnostic_text(self.guards, &error.text) + } else { + "Hosted web tool could not retrieve public results".into() + }) + }) + } +} + +fn diagnostic_text(guards: &Guards, text: &str) -> String { + guards.redaction.mask(text).chars().take(700).collect() +} + +fn citations(answer: &mut Value, sources: &[(String, String)]) { + for item in answer["output"].as_array_mut().into_iter().flatten() { + for part in item["content"].as_array_mut().into_iter().flatten() { + if part["type"] != "output_text" { + continue; + } + let text = part["text"].as_str().unwrap_or_default(); + let mut annotations = part["annotations"].as_array().cloned().unwrap_or_default(); + for (url, title) in sources { + if let Some(start) = text.find(url) + && !annotations.iter().any(|a| a["url"] == *url) + { + let start = text[..start].chars().count(); + let end = start + url.chars().count(); + annotations.push(json!({"type":"url_citation", "url":url, "title":title, "start_index":start, "end_index":end})); + } + } + part["annotations"] = json!(annotations); + } + } +} + +fn sum_usage(dialect: Dialect, answer: &Value, usage: &mut Usage) { + let next = if dialect == Dialect::Anthropic { + tw_dialect::anthropic::response::usage(&answer["usage"]) + } else { + tw_dialect::responses::response::usage(&answer["usage"]) + }; + usage.input = usage.input.saturating_add(next.input); + usage.output = usage.output.saturating_add(next.output); + usage.cache_read = usage.cache_read.saturating_add(next.cache_read); + usage.cache_write = usage.cache_write.saturating_add(next.cache_write); + usage.reasoning = usage.reasoning.saturating_add(next.reasoning); + usage.cache_1h |= next.cache_1h; +} + +fn render( + dialect: Dialect, + stream: bool, + id: &str, + answer: &Value, +) -> Result { + let (kind, body) = if stream { + ("text/event-stream", wire::stream(dialect, answer)) + } else { + ("application/json", answer.to_string().into_bytes()) + }; + let mut budget = resources::body_budget().reserve(); + if body.len() > MAX_BODY || !budget.grow(body.len().saturating_mul(resources::BODY_HEADROOM)) { + return Err(GatewayError::rate_limited("gateway_memory", 1).into()); + } + let mut response = ([(header::CONTENT_TYPE, kind)], budget.wrap(body.into())).into_response(); + response + .headers_mut() + .insert("x-metadata-request-id", request_id_header(id)); + if stream { + response + .headers_mut() + .insert(header::CACHE_CONTROL, HeaderValue::from_static("no-cache")); + } + Ok(response) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn core_execution_requires_an_explicit_key_lineage() { + assert!(HostedWebSettings::parse(json!({})).is_ok()); + assert!(HostedWebSettings::parse(json!({"execution": {"mode":"core"}})).is_err()); + let id = Uuid::new_v4(); + let cfg = + HostedWebSettings::parse(json!({"execution":{"mode":"core"},"enabled_lineages":[id]})) + .unwrap(); + assert!(!cfg.permits(&GatewayRequestIdentity::default())); + assert!(cfg.permits(&GatewayRequestIdentity { + api_key_lineage_id: Some(id.to_string()), + ..Default::default() + })); + } + + #[test] + fn hosted_requests_can_only_narrow_admin_domains() { + let cfg = WebToolsConfig { + allowed_domains: vec!["Example.org".into()], + ..Default::default() + }; + let mut plan = protocol::Plan { + dialect: Dialect::Responses, + search: true, + fetch: false, + allowed: vec!["docs.example.org".into(), "test.org".into()], + blocked: vec!["staff.example.org".into()], + max_uses: None, + }; + let effective = request_policy(cfg.clone(), &plan).unwrap(); + assert_eq!(effective.allowed_domains, ["docs.example.org"]); + assert_eq!(effective.blocked_domains, ["staff.example.org"]); + plan.allowed = vec!["notexample.org".into()]; + assert!(request_policy(cfg, &plan).is_err()); + } +} diff --git a/crates/gateway/src/proxy/web_tools/protocol.rs b/crates/gateway/src/proxy/web_tools/protocol.rs new file mode 100644 index 00000000..aff033c0 --- /dev/null +++ b/crates/gateway/src/proxy/web_tools/protocol.rs @@ -0,0 +1,383 @@ +// From ThinkWatch-Core v0.98.1. Copyright (c) 2026 ThinkWatch (MIT). +// The upstream license is in LICENSE.core-web-tools. + +//! Hosted-tool adapters. Ordinary client tools are never executed by Core. + +use serde_json::{Value, json}; +use tw_api::{WebToolRequest, WebToolResult}; +use tw_dialect::ir::Dialect; +use tw_types::Msg; + +use super::error; + +pub(crate) const SEARCH: &str = "thinkwatch_web_search"; +pub(crate) const OPEN: &str = "thinkwatch_open_page"; +pub(crate) const FIND: &str = "thinkwatch_find_in_page"; + +#[derive(Clone, Debug)] +pub(crate) struct Plan { + pub dialect: Dialect, + pub search: bool, + pub fetch: bool, + pub allowed: Vec, + pub blocked: Vec, + pub max_uses: Option, +} + +pub(crate) fn recognize(dialect: Dialect, body: &Value) -> Result, Msg> { + if !matches!(dialect, Dialect::Anthropic | Dialect::Responses) { + return Ok(None); + } + let Some(tools) = body["tools"].as_array() else { + return Ok(None); + }; + let mut plan = Plan { + dialect, + search: false, + fetch: false, + allowed: Vec::new(), + blocked: Vec::new(), + max_uses: None, + }; + for tool in tools { + let kind = tool["type"].as_str().unwrap_or(""); + if kind == "web_search" || kind == "web_search_preview" || kind.starts_with("web_search_") { + plan.search = true; + } else if kind.starts_with("web_fetch_") { + plan.fetch = true; + } else { + continue; + } + // These parameters change what may be searched. Never silently pretend to + // implement a requested mode or location that the collector cannot honor. + if tool.get("user_location").is_some_and(|v| !v.is_null()) + || tool.get("external_web_access") == Some(&Value::Bool(false)) + { + return Err(error( + "local search does not support user_location or cache-only external_web_access=false; use upstream execution", + )); + } + for (field, target) in [ + ("allowed_domains", &mut plan.allowed), + ("blocked_domains", &mut plan.blocked), + ] { + let value = tool + .get(field) + .or_else(|| tool.get("filters").and_then(|f| f.get(field))); + if let Some(v) = value { + let items: Vec = serde_json::from_value(v.clone()) + .map_err(|_| error("invalid domain filter"))?; + super::validate_domains(&items)?; + target.extend(items); + } + } + if let Some(n) = tool["max_uses"].as_u64() { + let n = n.min(u64::from(u32::MAX)) as u32; + plan.max_uses = Some(plan.max_uses.map_or(n, |old| old.min(n))); + } + } + if !plan.search && !plan.fetch { + return Ok(None); + } + if body["background"] == true + || body["previous_response_id"].is_string() + || body + .get("conversation") + .is_some_and(|value| !value.is_null()) + { + return Err(error( + "background or server-stored Responses requests require upstream web-tool execution", + )); + } + if tools + .iter() + .any(|t| [SEARCH, OPEN, FIND].contains(&t["name"].as_str().unwrap_or(""))) + { + return Err(error("request tool names conflict with Core's web tools")); + } + Ok(Some(plan)) +} + +impl Plan { + /// Only the known CC helper shape is safe to answer without a model. + pub fn standalone(&self, body: &Value) -> Option { + if self.dialect != Dialect::Anthropic || !self.search || self.fetch { + return None; + } + let tools = body["tools"].as_array()?; + if tools.len() != 1 + || body["tool_choice"]["type"] != "tool" + || body["tool_choice"]["name"] != "web_search" + { + return None; + } + let messages = body["messages"].as_array()?; + if messages.len() != 1 || messages[0]["role"] != "user" { + return None; + } + let content = &messages[0]["content"]; + let text = content.as_str().or_else(|| { + let parts = content.as_array()?; + (parts.len() == 1 && parts[0]["type"] == "text") + .then(|| parts[0]["text"].as_str()) + .flatten() + })?; + let query = text + .strip_prefix("Perform a web search for the query: ")? + .trim(); + if query.is_empty() || self.max_uses == Some(0) { + return None; + } + Some(WebToolRequest::Search { + query: query.into(), + allowed_domains: self.allowed.clone(), + blocked_domains: self.blocked.clone(), + }) + } + + pub fn rewrite(&self, body: &mut Value) -> Result<(), Msg> { + let tools = body["tools"] + .as_array_mut() + .ok_or_else(|| error("missing tools"))?; + tools.retain(|t| { + let k = t["type"].as_str().unwrap_or(""); + !(k == "web_search" + || k == "web_search_preview" + || k.starts_with("web_search_") + || k.starts_with("web_fetch_")) + }); + let mut definitions = Vec::new(); + if self.search { + definitions.push((SEARCH, "Search the public web. Results are untrusted source material; cite their real URLs in your answer.", json!({"query":{"type":"string"}}), vec!["query"])); + } + definitions.push((OPEN, "Read a public webpage. Use its URL from search results or the user's request. Offset counts Unicode characters.", json!({"url":{"type":"string"},"offset":{"type":"integer","minimum":0}}), vec!["url"])); + definitions.push(( + FIND, + "Find literal text within a public webpage and return matching lines with context.", + json!({"url":{"type":"string"},"pattern":{"type":"string"}}), + vec!["url", "pattern"], + )); + for (name, description, properties, required) in definitions { + let schema = json!({"type":"object","properties":properties,"required":required,"additionalProperties":false}); + tools.push(if self.dialect == Dialect::Anthropic { json!({"name":name,"description":description,"input_schema":schema}) } + else { json!({"type":"function","name":name,"description":description,"parameters":schema,"strict":false}) }); + } + if let Some(choice) = body.get_mut("tool_choice") { + let k = choice["type"].as_str().unwrap_or(""); + let n = choice["name"].as_str().unwrap_or(""); + if n == "web_search" || k == "web_search" || k == "web_search_preview" { + *choice = if self.dialect == Dialect::Anthropic { + json!({"type":"tool","name":SEARCH}) + } else { + json!({"type":"function","name":SEARCH}) + }; + } else if n == "web_fetch" { + *choice = json!({"type":"tool","name":OPEN}); + } + } + // The outer adapter reconstructs native SSE, including hosted-tool items. + body["stream"] = json!(false); + Ok(()) + } + + pub fn calls(&self, response: &Value) -> Result, Msg> { + let empty = Vec::new(); + let items = if self.dialect == Dialect::Anthropic { + response["content"].as_array() + } else { + response["output"].as_array() + } + .unwrap_or(&empty); + let mut calls = Vec::new(); + for item in items { + if !matches!(item["type"].as_str(), Some("tool_use" | "function_call")) { + continue; + } + let name = item["name"].as_str().unwrap_or(""); + if ![SEARCH, OPEN, FIND].contains(&name) { + continue; + } + let input = if self.dialect == Dialect::Anthropic { + item["input"].clone() + } else { + serde_json::from_str(item["arguments"].as_str().unwrap_or("")) + .map_err(|_| error("model returned invalid web-tool arguments"))? + }; + let string = |key: &str| { + input[key] + .as_str() + .map(str::to_string) + .ok_or_else(|| error(format!("web tool is missing {key}"))) + }; + let request = match name { + SEARCH if self.search => WebToolRequest::Search { + query: string("query")?, + allowed_domains: self.allowed.clone(), + blocked_domains: self.blocked.clone(), + }, + OPEN => WebToolRequest::OpenPage { + url: string("url")?, + offset: input["offset"] + .as_u64() + .unwrap_or(0) + .min(u64::from(u32::MAX)) as u32, + }, + FIND => WebToolRequest::FindInPage { + url: string("url")?, + pattern: string("pattern")?, + }, + _ => { + return Err(error( + "model requested a web operation that was not offered", + )); + } + }; + let id = if self.dialect == Dialect::Anthropic { + item["id"].as_str() + } else { + item["call_id"].as_str() + } + .ok_or_else(|| error("web tool call has no ID"))?; + calls.push(Call { + native_id: uuid::Uuid::new_v4().simple().to_string(), + id: id.into(), + request, + }); + } + Ok(calls) + } + + pub fn append( + &self, + body: &mut Value, + response: &Value, + results: &[(Call, WebToolResult)], + ) -> Result<(), Msg> { + let result_text = + |result: &WebToolResult| serde_json::to_string(result).unwrap_or_default(); + if self.dialect == Dialect::Anthropic { + let messages = body["messages"] + .as_array_mut() + .ok_or_else(|| error("invalid messages"))?; + messages.push(json!({"role":"assistant","content":response["content"]})); + messages.push(json!({"role":"user","content":results.iter().map(|(call, result)| json!({"type":"tool_result","tool_use_id":call.id,"content":result_text(result)})).collect::>()})); + } else { + if let Some(text) = body["input"].as_str() { + body["input"] = json!([{"role":"user","content":text}]); + } + let input = body["input"] + .as_array_mut() + .ok_or_else(|| error("invalid Responses input"))?; + input.extend(response["output"].as_array().cloned().unwrap_or_default()); + input.extend(results.iter().map(|(call, result)| json!({"type":"function_call_output","call_id":call.id,"output":result_text(result)}))); + } + // A forced first search must not force every following round to search again. + if body["tool_choice"]["name"] + .as_str() + .is_some_and(|n| [SEARCH, OPEN, FIND].contains(&n)) + { + body["tool_choice"] = if self.dialect == Dialect::Anthropic { + json!({"type":"auto"}) + } else { + json!("auto") + }; + } + Ok(()) + } +} + +#[derive(Clone, Debug)] +pub(crate) struct Call { + /// Fresh for each execution, even if an upstream reuses function call IDs. + pub native_id: String, + pub id: String, + pub request: WebToolRequest, +} + +pub(crate) fn native_blocks(dialect: Dialect, call: &Call, result: &WebToolResult) -> Vec { + let (name, input, action) = match &call.request { + WebToolRequest::Search { query, .. } => ( + "web_search", + json!({"query":query}), + json!({"type":"search","query":query,"sources":result.sources.iter().map(|s| json!({"type":"url","url":s.url})).collect::>()}), + ), + WebToolRequest::OpenPage { url, .. } => ( + "web_fetch", + json!({"url":url}), + json!({"type":"open_page","url":url}), + ), + WebToolRequest::FindInPage { url, pattern } => ( + "web_fetch", + json!({"url":url}), + json!({"type":"find_in_page","url":url,"pattern":pattern}), + ), + }; + if dialect == Dialect::Responses { + return vec![ + json!({"type":"web_search_call","id":format!("ws_tw_{}",call.native_id),"status":"completed","action":action}), + ]; + } + let id = format!("srvtoolu_tw_{}", call.native_id); + let content = if name == "web_search" { + json!({"type":"web_search_tool_result","tool_use_id":id,"content":result.sources.iter().map(|s| json!({"type":"web_search_result","url":s.url,"title":s.title,"encrypted_content":""})).collect::>()}) + } else { + let source = result.sources.first(); + json!({"type":"web_fetch_tool_result","tool_use_id":id,"content":{"type":"web_fetch_result","url":source.map(|s| s.url.as_str()).unwrap_or(""),"content":{"type":"document","title":source.map(|s| s.title.as_str()).unwrap_or(""),"source":{"type":"text","media_type":"text/plain","data":source.map(|s| s.text.as_str()).unwrap_or("")}}}}) + }; + vec![ + json!({"type":"server_tool_use","id":id,"name":name,"input":input}), + content, + ] +} + +pub(crate) fn standalone_response(model: &str, call: &Call, result: &WebToolResult) -> Value { + let mut content = native_blocks(Dialect::Anthropic, call, result); + let text = if result.sources.is_empty() { + "No results matched the search.".into() + } else { + result + .sources + .iter() + .enumerate() + .map(|(i, s)| format!("[{}] {}\nURL: {}\n{}", i + 1, s.title, s.url, s.text)) + .collect::>() + .join("\n\n") + }; + content.push(json!({"type":"text","text":text})); + json!({"id":format!("msg_tw_{}",uuid::Uuid::new_v4().simple()),"type":"message","role":"assistant","model":model,"content":content,"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn only_the_cc_helper_can_skip_the_model() { + let mut body = json!({"messages":[{"role":"user","content":"Perform a web search for the query: SQLite release"}],"tools":[{"type":"web_search_20250305","name":"web_search"}],"tool_choice":{"type":"tool","name":"web_search"}}); + let plan = recognize(Dialect::Anthropic, &body).unwrap().unwrap(); + assert!(plan.standalone(&body).is_some()); + body["messages"][0]["content"] = json!("Compare the releases and explain the changes."); + assert!(plan.standalone(&body).is_none()); + body["tools"] + .as_array_mut() + .unwrap() + .push(json!({"name":"Read","input_schema":{"type":"object"}})); + plan.rewrite(&mut body).unwrap(); + assert!( + body["tools"] + .as_array() + .unwrap() + .iter() + .any(|t| t["name"] == "Read") + ); + } + #[test] + fn domain_filters_survive_responses_adaptation() { + let mut body = json!({"input":"Find the release", "tools":[{"type":"web_search","filters":{"allowed_domains":["sqlite.org"]}}]}); + let plan = recognize(Dialect::Responses, &body).unwrap().unwrap(); + plan.rewrite(&mut body).unwrap(); + let calls = plan.calls(&json!({"output":[{"type":"function_call","name":SEARCH,"call_id":"c1","arguments":"{\"query\":\"SQLite\"}"}]})).unwrap(); + assert!( + matches!(&calls[0].request, WebToolRequest::Search { allowed_domains, .. } if allowed_domains == &["sqlite.org"]) + ); + } +} diff --git a/crates/gateway/src/proxy/web_tools/wire.rs b/crates/gateway/src/proxy/web_tools/wire.rs new file mode 100644 index 00000000..5f54723b --- /dev/null +++ b/crates/gateway/src/proxy/web_tools/wire.rs @@ -0,0 +1,241 @@ +// From ThinkWatch-Core v0.98.1. Copyright (c) 2026 ThinkWatch (MIT). +// The upstream license is in LICENSE.core-web-tools. + +//! Encode complete native responses as valid native SSE. Only hosted-tool requests +//! use this path; ordinary model traffic retains its original streaming behavior. + +use serde_json::{Value, json}; +use tw_dialect::ir::Dialect; + +pub(crate) fn stream(dialect: Dialect, response: &Value) -> Vec { + let mut out = String::new(); + let mut sequence = 0; + let mut event = |kind: &str, mut value: Value| { + value["type"] = json!(kind); + if dialect == Dialect::Responses { + value["sequence_number"] = json!(sequence); + sequence += 1; + } + out.push_str(&format!("event: {kind}\ndata: {value}\n\n")); + }; + if dialect == Dialect::Anthropic { + let mut start = response.clone(); + start["content"] = json!([]); + start["stop_reason"] = Value::Null; + start["stop_sequence"] = Value::Null; + start["usage"]["output_tokens"] = json!(0); + event("message_start", json!({"message":start})); + for (index, block) in response["content"] + .as_array() + .into_iter() + .flatten() + .enumerate() + { + let mut start = block.clone(); + let delta = match block["type"].as_str() { + Some("text") => { + start["text"] = json!(""); + Some(json!({"type":"text_delta","text":block["text"]})) + } + Some("thinking") => { + start["thinking"] = json!(""); + start["signature"] = json!(""); + Some(json!({"type":"thinking_delta","thinking":block["thinking"]})) + } + Some("tool_use" | "server_tool_use") => { + start["input"] = json!({}); + Some( + json!({"type":"input_json_delta","partial_json":block["input"].to_string()}), + ) + } + _ => None, + }; + event( + "content_block_start", + json!({"index":index,"content_block":start}), + ); + if let Some(delta) = delta { + event("content_block_delta", json!({"index":index,"delta":delta})); + } + if block["type"] == "thinking" && block["signature"].is_string() { + event( + "content_block_delta", + json!({"index":index,"delta":{"type":"signature_delta","signature":block["signature"]}}), + ); + } + event("content_block_stop", json!({"index":index})); + } + let mut delta = json!({"stop_reason":response["stop_reason"],"stop_sequence":response["stop_sequence"]}); + if let Some(results) = response.get("safeguard_results") { + delta["safeguard_results"] = results.clone(); + } + event( + "message_delta", + json!({"delta":delta,"usage":response["usage"]}), + ); + event("message_stop", json!({})); + } else { + let mut start = response.clone(); + start["output"] = json!([]); + start["status"] = json!("in_progress"); + event("response.created", json!({"response":start})); + event("response.in_progress", json!({"response":start})); + for (index, item) in response["output"] + .as_array() + .into_iter() + .flatten() + .enumerate() + { + let mut initial = item.clone(); + if item["type"] == "message" { + initial["content"] = json!([]); + initial["status"] = json!("in_progress"); + } + if item["type"] == "function_call" { + initial["arguments"] = json!(""); + } + if item["type"] == "reasoning" { + initial["summary"] = json!([]); + } + if item["type"] == "web_search_call" { + initial["status"] = json!("in_progress"); + } + event( + "response.output_item.added", + json!({"output_index":index,"item":initial}), + ); + if item["type"] == "web_search_call" { + for kind in [ + "response.web_search_call.in_progress", + "response.web_search_call.searching", + "response.web_search_call.completed", + ] { + event(kind, json!({"output_index":index,"item_id":item["id"]})); + } + } + for (part_index, part) in item["content"].as_array().into_iter().flatten().enumerate() { + let mut initial = part.clone(); + if part["type"] == "output_text" { + initial["text"] = json!(""); + initial["annotations"] = json!([]); + } + event( + "response.content_part.added", + json!({"output_index":index,"content_index":part_index,"item_id":item["id"],"part":initial}), + ); + if part["type"] == "output_text" { + event( + "response.output_text.delta", + json!({"output_index":index,"content_index":part_index,"item_id":item["id"],"delta":part["text"]}), + ); + for (annotation_index, annotation) in part["annotations"] + .as_array() + .into_iter() + .flatten() + .enumerate() + { + event( + "response.output_text.annotation.added", + json!({"output_index":index,"content_index":part_index,"item_id":item["id"],"annotation_index":annotation_index,"annotation":annotation}), + ); + } + event( + "response.output_text.done", + json!({"output_index":index,"content_index":part_index,"item_id":item["id"],"text":part["text"]}), + ); + } + event( + "response.content_part.done", + json!({"output_index":index,"content_index":part_index,"item_id":item["id"],"part":part}), + ); + } + for (summary_index, part) in + item["summary"].as_array().into_iter().flatten().enumerate() + { + event( + "response.reasoning_summary_part.added", + json!({"output_index":index,"item_id":item["id"],"summary_index":summary_index,"part":{"type":"summary_text","text":""}}), + ); + event( + "response.reasoning_summary_text.delta", + json!({"output_index":index,"item_id":item["id"],"summary_index":summary_index,"delta":part["text"]}), + ); + event( + "response.reasoning_summary_text.done", + json!({"output_index":index,"item_id":item["id"],"summary_index":summary_index,"text":part["text"]}), + ); + event( + "response.reasoning_summary_part.done", + json!({"output_index":index,"item_id":item["id"],"summary_index":summary_index,"part":part}), + ); + } + if item["type"] == "function_call" { + event( + "response.function_call_arguments.delta", + json!({"output_index":index,"item_id":item["id"],"delta":item["arguments"]}), + ); + event( + "response.function_call_arguments.done", + json!({"output_index":index,"item_id":item["id"],"arguments":item["arguments"]}), + ); + } + event( + "response.output_item.done", + json!({"output_index":index,"item":item}), + ); + } + let kind = match response["status"].as_str() { + Some("incomplete") => "response.incomplete", + Some("failed") => "response.failed", + _ => "response.completed", + }; + event(kind, json!({"response":response})); + } + out.into_bytes() +} + +#[cfg(test)] +mod tests { + use super::*; + fn events(dialect: Dialect, response: Value) -> Vec { + let bytes = stream(dialect, &response); + let mut decoder = tw_dialect::frame::Decoder::default(); + decoder + .feed(&bytes) + .into_iter() + .map(|f| serde_json::from_str(&f.data).unwrap()) + .collect() + } + #[test] + fn preserves_anthropic_signatures_and_safeguard_results() { + let response = json!({"type":"message","role":"assistant","id":"msg_test","model":"model","content":[{"type":"thinking","thinking":"Consider the sources","signature":"signed-thinking"},{"type":"text","text":"Answer"}],"stop_reason":"end_turn","usage":{"input_tokens":0,"output_tokens":0},"safeguard_results":[{"type":"dangerous_tool_use","status":{"type":"available","tool_uses":{}}}]}); + let events = events(Dialect::Anthropic, response); + assert!( + events + .iter() + .any(|e| e["delta"]["signature"] == "signed-thinking") + ); + assert_eq!( + events + .iter() + .find(|e| e["type"] == "message_delta") + .unwrap()["delta"]["safeguard_results"][0]["status"]["type"], + "available" + ); + assert_eq!(events.last().unwrap()["type"], "message_stop"); + } + #[test] + fn responses_failure_is_never_reported_as_completed_and_annotations_are_emitted() { + let response = json!({"id":"resp_test","status":"failed","output":[{"type":"message","id":"msg_test","content":[{"type":"output_text","text":"A source","annotations":[{"type":"url_citation","url":"https://example.org","title":"Example","start_index":0,"end_index":8}]}]}],"error":{"code":"server_error","message":"Failed"}}); + let events = events(Dialect::Responses, response); + assert!( + events + .iter() + .any(|e| e["type"] == "response.output_text.annotation.added") + ); + assert_eq!(events.last().unwrap()["type"], "response.failed"); + for (n, event) in events.iter().enumerate() { + assert_eq!(event["sequence_number"], n); + } + } +} diff --git a/crates/gateway/src/router.rs b/crates/gateway/src/router.rs index b733bdd9..bb2b6dd0 100644 --- a/crates/gateway/src/router.rs +++ b/crates/gateway/src/router.rs @@ -132,6 +132,7 @@ pub struct ModelRouter { /// deterministic candidate ordering (helps test stability and /// makes the decision log easier to scan). routes: HashMap>, + catalog_models: std::collections::HashSet, /// Per-model routing overrides. Lookup falls through to global /// defaults (`gateway.default_*` in system_settings) when absent. configs: HashMap, @@ -144,13 +145,29 @@ impl Default for ModelRouter { } impl ModelRouter { + pub fn has_model_name(&self, model: &str) -> bool { + self.catalog_models.contains(model) || self.routes.contains_key(model) + } + + pub fn can_route_alias_target(&self, model: &str) -> bool { + self.route(model).is_some_and(|routes| { + self.routes.contains_key(model) + || routes.iter().all(|route| route.upstream_model.is_none()) + }) + } + pub fn new() -> Self { Self { routes: HashMap::new(), + catalog_models: Default::default(), configs: HashMap::new(), } } + pub fn register_model_name(&mut self, model: &str) { + self.catalog_models.insert(model.to_owned()); + } + /// Set per-model routing config (strategy / affinity overrides). /// Called once per model at router-load time, after all routes /// have been registered. diff --git a/crates/mcp-gateway/src/proxy.rs b/crates/mcp-gateway/src/proxy.rs index 1210d6ad..433f77c8 100644 --- a/crates/mcp-gateway/src/proxy.rs +++ b/crates/mcp-gateway/src/proxy.rs @@ -135,7 +135,6 @@ pub struct McpProxy { } impl McpProxy { - #[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)] pub fn new( registry: Registry, diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index 49796768..1b3b0d50 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -21,6 +21,7 @@ think-watch-mcp-gateway = { workspace = true } tw-dialect = { workspace = true } tw-bedrock = { workspace = true } tw-guard = { workspace = true } +tw-api = { workspace = true } axum = { workspace = true } futures = { workspace = true } tower = { workspace = true } diff --git a/crates/server/src/app.rs b/crates/server/src/app.rs index 91349b97..bc5fbc37 100644 --- a/crates/server/src/app.rs +++ b/crates/server/src/app.rs @@ -21,7 +21,6 @@ use think_watch_common::config::AppConfig; use think_watch_common::dynamic_config::DynamicConfig; use think_watch_common::guard_policy::saved::SavedSecrets; use think_watch_gateway::cache::ResponseCache; -use think_watch_gateway::model_mapping::ModelMapper; use think_watch_gateway::proxy::{self as gateway_proxy, GatewayState}; use think_watch_gateway::router::{ModelRouter, RouteEntry}; use think_watch_mcp_gateway::proxy::McpProxy; @@ -68,6 +67,7 @@ pub struct AppState { /// Gateway model router. Wrapped in `ArcSwap` so route changes /// (provider/model CRUD, sync) take effect without restart. pub gateway_router: Arc>, + pub hosted_web: Arc, /// Shared model-weight cache (per-model `input_weight` / /// `output_weight` pulled from the `models` table). Lives on /// AppState so model CRUD handlers can invalidate it after a @@ -261,8 +261,8 @@ pub async fn create_gateway_app(_config: &AppConfig, state: AppState) -> anyhow: let weight_cache = state.weight_cache.clone(); let gateway_state = GatewayState { + hosted_web: state.hosted_web.clone(), router: state.gateway_router.clone(), - model_mapper: Arc::new(ModelMapper::new()), // Share the hot-swappable guards with the gateway state. guards: state.guards.clone(), cache: Arc::new(ResponseCache::new( @@ -305,6 +305,10 @@ pub async fn create_gateway_app(_config: &AppConfig, state: AppState) -> anyhow: post(gateway_proxy::proxy_responses).get(gateway_proxy::proxy_responses_ws), ) .route("/v1/models", get(gateway_proxy::list_models_handler)) + .route( + "/codex/models", + get(gateway_proxy::list_codex_models_handler), + ) // Gemini: `{model}:generateContent` / `:streamGenerateContent`. // Its SDKs call v1beta; some clients call v1. .route( @@ -313,6 +317,9 @@ pub async fn create_gateway_app(_config: &AppConfig, state: AppState) -> anyhow: ) .route("/v1beta/models/{target}", post(gateway_proxy::proxy_gemini)) .route("/v1/models/{target}", post(gateway_proxy::proxy_gemini)) + .layer(axum::middleware::from_fn( + think_watch_common::resources::http::admit, + )) .layer(axum::middleware::from_fn_with_state( state.clone(), crate::middleware::api_key_auth::require_api_key("ai_gateway"), @@ -381,6 +388,9 @@ pub async fn create_gateway_app(_config: &AppConfig, state: AppState) -> anyhow: let mcp_routes = Router::new() .route("/mcp", post(streamable_http::handle_post)) .route("/mcp", delete(streamable_http::handle_delete)) + .layer(axum::middleware::from_fn( + think_watch_common::resources::http::admit, + )) .layer(axum::middleware::from_fn_with_state( state.clone(), crate::middleware::api_key_auth::require_api_key("mcp_gateway"), @@ -440,9 +450,6 @@ pub async fn create_gateway_app(_config: &AppConfig, state: AppState) -> anyhow: .merge(ai_routes) .merge(key_routes) .merge(mcp_routes) - .layer(axum::middleware::from_fn( - think_watch_common::resources::http::admit, - )) .layer(RequestBodyLimitLayer::new(10 * 1024 * 1024)) // 10MB for large prompts // When the timeout below starts counting: a whole answer's retries // stop in time for its error to be written (see `gateway_proxy::retry`). @@ -1000,6 +1007,10 @@ pub fn create_console_app(config: &AppConfig, state: AppState) -> anyhow::Result "/api/admin/settings", get(handlers::admin::get_all_settings).patch(handlers::admin::update_settings), ) + .route( + "/api/admin/settings/web-tools/test", + post(handlers::admin::test_web_tool), + ) .route( "/api/admin/settings/category/{category}", get(handlers::admin::get_settings_by_category), @@ -1314,6 +1325,7 @@ async fn load_providers_into_router( use think_watch_gateway::router::{AffinityMode, ModelRoutingConfig}; use think_watch_gateway::strategy::RoutingStrategy; for m in &model_rows { + router.register_model_name(&m.model_id); let cfg = ModelRoutingConfig { strategy: m .routing_strategy diff --git a/crates/server/src/handlers/admin.rs b/crates/server/src/handlers/admin.rs index f2b259db..a4631b10 100644 --- a/crates/server/src/handlers/admin.rs +++ b/crates/server/src/handlers/admin.rs @@ -22,7 +22,7 @@ pub use retention::{ pub use security::{add_exception, get_security, list_exceptions, remove_exception, test_security}; pub use settings::{ AuditConfigResponse, SystemInfo, UpdateSettingsRequest, get_all_settings, get_audit_settings, - get_settings_by_category, get_system_settings, update_settings, + get_settings_by_category, get_system_settings, test_web_tool, update_settings, }; pub use users::{ CreateUserByAdminRequest, CreateUserByAdminResponse, ListUsersQuery, SuperAdminIds, diff --git a/crates/server/src/handlers/admin/settings.rs b/crates/server/src/handlers/admin/settings.rs index 7a764958..fd0f7070 100644 --- a/crates/server/src/handlers/admin/settings.rs +++ b/crates/server/src/handlers/admin/settings.rs @@ -8,6 +8,8 @@ use std::collections::HashMap; use axum::Json; use axum::extract::{Path, State}; +use axum::http::StatusCode; +use axum::response::{IntoResponse, Response}; use serde::{Deserialize, Serialize}; use think_watch_common::dynamic_config::{self, SettingEntry}; @@ -238,9 +240,58 @@ pub async fn get_settings_by_category( Ok(Json(settings)) } +pub async fn test_web_tool( + auth_user: AuthUser, + State(state): State, + Json(request): Json, +) -> Result { + auth_user + .require_global_permission(&state.db, "settings:write") + .await?; + let value = sqlx::query_scalar("SELECT value FROM system_settings WHERE key = $1") + .bind(think_watch_gateway::proxy::HostedWebSettings::KEY) + .fetch_optional(&state.db) + .await? + .ok_or_else(|| AppError::BadRequest("Hosted web tools are not configured".into()))?; + let settings = think_watch_gateway::proxy::HostedWebSettings::parse(value) + .map_err(AppError::BadRequest)?; + let guards = state.guards.load_full(); + let result = tokio::time::timeout( + std::time::Duration::from_secs(25), + settings.diagnose( + &state.hosted_web, + &guards, + &state.audit, + auth_user.claims.sub, + request, + ), + ) + .await; + let failed = |status, message: String| { + ( + status, + Json(serde_json::json!({"error":{"message":message}})), + ) + .into_response() + }; + Ok(match result { + Ok(Ok(data)) => Json(data).into_response(), + Ok(Err(error)) => failed( + StatusCode::from_u16(error.status_code() as u16).unwrap_or(StatusCode::BAD_GATEWAY), + error.to_string(), + ), + Err(_) => failed( + StatusCode::GATEWAY_TIMEOUT, + "The diagnostic stopped after 25 seconds".into(), + ), + }) +} + #[derive(Debug, Deserialize, utoipa::ToSchema)] pub struct UpdateSettingsRequest { pub settings: HashMap, + #[serde(default)] + pub expected: HashMap, } /// PATCH /api/admin/settings — update one or more settings. @@ -259,6 +310,7 @@ pub struct UpdateSettingsRequest { (status = 200, description = "Settings updated"), (status = 400, description = "Validation error"), (status = 403, description = "Forbidden"), + (status = 409, description = "A setting changed since it was loaded"), ), security(("BearerAuth" = [])) )] @@ -303,6 +355,24 @@ pub async fn update_settings( for (key, value) in &req.settings { validate_setting(key, value)?; } + use think_watch_gateway::proxy::{HostedWebSettings, ModelDiscovery}; + for key in [HostedWebSettings::KEY, ModelDiscovery::KEY] { + if req.settings.contains_key(key) && !req.expected.contains_key(key) { + return Err(AppError::BadRequest(format!( + "Include the current value in expected for {key}" + ))); + } + } + if let Some(value) = req.settings.get(ModelDiscovery::KEY) { + let cfg = ModelDiscovery::parse(value.clone()).map_err(AppError::BadRequest)?; + let existing = crate::services::model_repository::list_ids(&state.db) + .await? + .into_iter() + .map(|row| row.model_id) + .collect(); + cfg.validate_aliases(&state.gateway_router.load(), &existing) + .map_err(AppError::BadRequest)?; + } // DB-level validation for settings that reference other entities if let Some(role_val) = req.settings.get("auth.default_role") { @@ -316,9 +386,10 @@ pub async fn update_settings( state .dynamic_config - .update(&req.settings, Some(auth_user.claims.sub)) + .update_checked(&req.settings, &req.expected, Some(auth_user.claims.sub)) .await - .map_err(AppError::Internal)?; + .map_err(AppError::Internal)? + .map_err(|key| AppError::Conflict(format!("{key} changed since it was loaded")))?; // Hot-reload the request guards immediately on this instance (other // instances pick it up via the Redis Pub/Sub subscriber). @@ -730,6 +801,14 @@ fn validate_setting(key: &str, value: &serde_json::Value) -> Result<(), AppError )); } } + think_watch_gateway::proxy::ModelDiscovery::KEY => { + think_watch_gateway::proxy::ModelDiscovery::parse(value.clone()) + .map_err(AppError::BadRequest)?; + } + think_watch_gateway::proxy::HostedWebSettings::KEY => { + think_watch_gateway::proxy::HostedWebSettings::parse(value.clone()) + .map_err(AppError::BadRequest)?; + } "gateway.cb_open_secs" => { let v = value .as_i64() diff --git a/crates/server/src/handlers/api_keys.rs b/crates/server/src/handlers/api_keys.rs index 68cf3798..ad2d97f5 100644 --- a/crates/server/src/handlers/api_keys.rs +++ b/crates/server/src/handlers/api_keys.rs @@ -239,6 +239,14 @@ impl ListKeysParams { } } +#[derive(serde::Serialize)] +pub struct ListedKey { + #[serde(flatten)] + key: ApiKey, + /// Settings refer to the stable key identity across rotations. + lineage_id: Uuid, +} + /// GET /api/keys /// /// Result set is the union of: @@ -269,7 +277,7 @@ pub async fn list_keys( auth_user: AuthUser, State(state): State, Query(params): Query, -) -> Result>, AppError> { +) -> Result>, AppError> { auth_user.require_permission("api_keys:read")?; let per_page = params.per_page(); let offset = params.offset(); @@ -301,7 +309,13 @@ pub async fn list_keys( }; Ok(Json(PaginatedResponse { - data: keys, + data: keys + .into_iter() + .map(|key| ListedKey { + lineage_id: key.lineage_id, + key, + }) + .collect(), total, page: params.page.unwrap_or(1).max(1), per_page, diff --git a/crates/server/src/handlers/mcp_store.rs b/crates/server/src/handlers/mcp_store.rs index cb79b150..182d09dc 100644 --- a/crates/server/src/handlers/mcp_store.rs +++ b/crates/server/src/handlers/mcp_store.rs @@ -73,10 +73,7 @@ pub async fn list_templates( return false; } } - if q.featured == Some(true) && !t.featured { - return false; - } - true + q.featured != Some(true) || t.featured }) .map(|t| { let installed = installed_set.contains(&t.id); diff --git a/crates/server/src/init.rs b/crates/server/src/init.rs index 60903ec2..9626cdf4 100644 --- a/crates/server/src/init.rs +++ b/crates/server/src/init.rs @@ -144,6 +144,7 @@ pub async fn init_state( )), http_client: Arc::new(arc_swap::ArcSwap::from_pointee(init_http_client)), gateway_router, + hosted_web: Default::default(), weight_cache, user_token_resolver, url_validator: think_watch_common::validation::production_url_validator(), diff --git a/crates/test-support/tests/gateway_settings.rs b/crates/test-support/tests/gateway_settings.rs new file mode 100644 index 00000000..6bc445e7 --- /dev/null +++ b/crates/test-support/tests/gateway_settings.rs @@ -0,0 +1,271 @@ +use think_watch_test_support::prelude::*; +use wiremock::matchers::{method, path, query_param}; +use wiremock::{Mock, MockServer, Request, ResponseTemplate}; + +const MODEL: &str = "gateway-test-model"; +const WEB: &str = "gateway.web_tools"; +const DISCOVERY: &str = "gateway.model_discovery"; + +async fn seed_route(app: &TestApp, upstream: &MockServer, provider_type: &str) -> TestClient { + let provider = fixtures::create_provider( + &app.db, + "gateway-settings-provider", + provider_type, + &upstream.uri(), + None, + ) + .await + .unwrap(); + fixtures::create_model_and_route(&app.db, provider.id, MODEL) + .await + .unwrap(); + app.rebuild_gateway_router().await; + let user = fixtures::create_random_user(&app.db).await.unwrap(); + let key = fixtures::create_api_key( + &app.db, + user.user.id, + "settings-test", + &["ai_gateway"], + None, + None, + ) + .await + .unwrap(); + let gw = app.gateway_client(); + gw.set_bearer(&key.plaintext); + gw +} + +#[ignore = "integration test — run via `make test-it`"] +#[tokio::test] +async fn concurrent_gateway_saves_detect_conflicts_and_reject_invalid_aliases() { + let app = TestApp::spawn().await; + let upstream = MockServer::start().await; + let gw = seed_route(&app, &upstream, "openai").await; + let first = admin_session(&app).await; + let second = admin_session(&app).await; + let settings = "/api/admin/settings"; + + for (name, target, message) in [ + (MODEL, MODEL, "Aliases require exact model IDs"), + (MODEL, "missing-model", "conflicts with an existing model"), + ("friendly", "missing-model", "has no active route"), + ( + "friendly", + "gateway-test-model-extra", + "has no active route", + ), + ] { + let result = first + .patch(settings, json!({"settings":{DISCOVERY:{"aliases":{name:{"target":target}}}},"expected":{DISCOVERY:{}}})) + .await + .unwrap(); + result.assert_status(400); + assert!(result.text().contains(message), "{}", result.text()); + } + + let change_a = json!({"settings":{DISCOVERY:{"aliases":{"fast":{"target":MODEL}}}},"expected":{DISCOVERY:{}}}); + let change_b = json!({"settings":{DISCOVERY:{"aliases":{"code":{"target":MODEL}}}},"expected":{DISCOVERY:{}}}); + let (a, b) = tokio::join!( + first.patch(settings, change_a), + second.patch(settings, change_b) + ); + let (a, b) = (a.unwrap(), b.unwrap()); + let winning_name = if a.status.is_success() { + b.assert_status(409); + "fast" + } else { + a.assert_status(409); + b.assert_ok(); + "code" + }; + let saved: Json = sqlx::query_scalar("SELECT value FROM system_settings WHERE key = $1") + .bind(DISCOVERY) + .fetch_one(&app.db) + .await + .unwrap(); + assert_eq!(saved, json!({"aliases":{winning_name:{"target":MODEL}}})); + + let models: Json = gw + .get("/v1/models") + .await + .unwrap() + .assert_ok() + .json() + .unwrap(); + assert!( + models["data"] + .as_array() + .unwrap() + .iter() + .any(|model| model["id"] == winning_name) + ); + sqlx::query("INSERT INTO models (model_id, display_name) VALUES ($1, $1)") + .bind(winning_name) + .execute(&app.db) + .await + .unwrap(); + app.rebuild_gateway_router().await; + let models: Json = gw + .get("/v1/models") + .await + .unwrap() + .assert_ok() + .json() + .unwrap(); + assert!( + models["data"] + .as_array() + .unwrap() + .iter() + .all(|model| model["id"] != winning_name) + ); +} + +#[ignore = "integration test — run via `make test-it`"] +#[tokio::test] +async fn codex_catalog_preserves_native_fields_and_partitions_client_versions() { + let app = TestApp::spawn().await; + let upstream = MockServer::start().await; + let gw = seed_route(&app, &upstream, "openai").await; + for version in ["0.165.0", "0.166.0"] { + Mock::given(method("GET")) + .and(path("/v1/models")) + .and(query_param("client_version", version)) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({"models":[{ + "slug": MODEL, "display_name": "Upstream model", + "base_instructions": format!("Native instructions for {version}"), + "shell_type":"shell_command", "experimental_supported_tools":["apply_patch"], + "truncation_policy":{"mode":"tokens","limit":8000}, + "input_modalities":["text","image"], "context_window":120000, + "supported_reasoning_levels":[{"effort":"medium","description":"Upstream explanation"}], + "default_reasoning_level":"medium" + }]}))) + .expect(1) + .mount(&upstream) + .await; + } + for version in ["0.165.0", "0.166.0", "0.165.0"] { + let response = gw + .get(&format!("/codex/models?client_version={version}")) + .await + .unwrap(); + let catalog: Json = response.assert_ok().json().unwrap(); + let model = &catalog["models"][0]; + assert_eq!(model["slug"], MODEL); + assert_eq!( + model["base_instructions"], + format!("Native instructions for {version}") + ); + assert_eq!( + model["experimental_supported_tools"], + json!(["apply_patch"]) + ); + assert_eq!( + model["supported_reasoning_levels"][0]["description"], + "Upstream explanation" + ); + } +} + +#[ignore = "integration test — run via `make test-it`"] +#[tokio::test] +async fn hosted_fallback_counts_once_and_old_history_survives_disabling_tools() { + let app = TestApp::spawn().await; + let upstream = MockServer::start().await; + let gw = seed_route(&app, &upstream, "anthropic").await; + let (key, owner): (Uuid, Uuid) = + sqlx::query_as("SELECT id, user_id FROM api_keys WHERE name = 'settings-test'") + .fetch_one(&app.db) + .await + .unwrap(); + for (kind, subject) in [("user", owner), ("api_key_lineage", key)] { + fixtures::create_rate_limit_rule(&app.db, kind, subject, "ai_gateway", "requests", 60, 1) + .await + .unwrap(); + } + Mock::given(method("POST")) + .and(path("/v1/messages")) + .and(|req: &Request| String::from_utf8_lossy(&req.body).contains("\"name\":\"thinkwatch_open_page\"")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "id":"msg_tool","type":"message","role":"assistant","model":MODEL,"stop_reason":"tool_use", + "content":[{"type":"tool_use","id":"toolu_read","name":"thinkwatch_open_page","input":{"url":"http://127.0.0.1/"}}], + "usage":{"input_tokens":10,"output_tokens":5} + }))) + .expect(1) + .mount(&upstream) + .await; + Mock::given(method("POST")) + .and(path("/v1/messages")) + .and(|req: &Request| { + let raw = String::from_utf8_lossy(&req.body); + raw.contains("Previously retrieved web evidence") && !raw.contains("srvtoolu_tw_") + && !raw.contains("\"name\":\"thinkwatch_open_page\"") + }) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "id":"msg_final","type":"message","role":"assistant","model":MODEL,"stop_reason":"end_turn", + "content":[{"type":"text","text":"continued"}],"usage":{"input_tokens":15,"output_tokens":2} + }))) + .expect(2) + .mount(&upstream) + .await; + + let con = admin_session(&app).await; + let enabled = + json!({"execution":{"mode":"core","fallback_to_upstream":true},"enabled_lineages":[key]}); + con.patch( + "/api/admin/settings", + json!({"settings":{WEB:enabled},"expected":{WEB:{}}}), + ) + .await + .unwrap() + .assert_ok(); + let diagnostic = con + .post( + "/api/admin/settings/web-tools/test", + json!({"action":"open_page","url":"http://127.0.0.1/"}), + ) + .await + .unwrap(); + diagnostic.assert_status(502); + assert!( + diagnostic.text().contains("public"), + "{}", + diagnostic.text() + ); + + let mut request = json!({ + "model":MODEL,"max_tokens":128, + "tools":[{"type":"web_fetch_20250910","name":"web_fetch"}], + "messages":[ + {"role":"assistant","content":[ + {"type":"server_tool_use","id":"srvtoolu_tw_old","name":"web_search","input":{"query":"test"}}, + {"type":"web_search_tool_result","tool_use_id":"srvtoolu_tw_old","content":[{"type":"web_search_result","url":"https://example.org/","title":"Example","encrypted_content":""}]} + ]}, + {"role":"user","content":"Continue."} + ] + }); + let first = gw.post("/v1/messages", &request).await.unwrap(); + let answer: Json = first.assert_ok().json().unwrap(); + assert_eq!(answer["content"][0]["text"], "continued"); + gw.post("/v1/messages", &request) + .await + .unwrap() + .assert_status(429); + + con.patch( + "/api/admin/settings", + json!({"settings":{WEB:{}},"expected":{WEB:enabled}}), + ) + .await + .unwrap() + .assert_ok(); + sqlx::query("DELETE FROM rate_limit_rules WHERE subject_id IN ($1, $2)") + .bind(key) + .bind(owner) + .execute(&app.db) + .await + .unwrap(); + request.as_object_mut().unwrap().remove("tools"); + gw.post("/v1/messages", &request).await.unwrap().assert_ok(); +} diff --git a/crates/test-support/tests/resources.rs b/crates/test-support/tests/resources.rs index 9fe69211..1b8d8d1c 100644 --- a/crates/test-support/tests/resources.rs +++ b/crates/test-support/tests/resources.rs @@ -52,22 +52,43 @@ async fn redis_body_reads_preserve_bytes_and_reservations_until_the_last_clone() #[tokio::test] async fn http_body_pressure_is_retryable_and_releases_without_affecting_health() { let app = TestApp::spawn().await; + let model = "admission-test"; + let upstream = MockProvider::openai_chat_ok(model).await; + let provider = fixtures::create_provider(&app.db, model, "openai", &upstream.uri(), None) + .await + .unwrap(); + fixtures::create_model_and_route(&app.db, provider.id, model) + .await + .unwrap(); + app.rebuild_gateway_router().await; + let user = fixtures::create_random_user(&app.db).await.unwrap(); + let key = fixtures::create_api_key(&app.db, user.user.id, model, &["ai_gateway"], None, None) + .await + .unwrap(); + let budget = resources::body_budget(); let mut occupied = budget.reserve(); assert!(occupied.grow(resources::policy().body_bytes - budget.used())); let gateway = app.gateway_client(); + let request = json!({"model":model,"messages":[{"role":"user","content":"hello"}]}); let response = gateway - .post("/v1/chat/completions", json!({"model":"test"})) + .post("/v1/chat/completions", &request) + .await + .unwrap(); + response.assert_status(401); + gateway.get("/health/live").await.unwrap().assert_ok(); + + gateway.set_bearer(&key.plaintext); + let response = gateway + .post("/v1/chat/completions", &request) .await .unwrap(); response.assert_status(429); assert_eq!(response.headers["retry-after"], "1"); drop(occupied); - let response = gateway - .post("/v1/chat/completions", json!({"model":"test"})) + gateway + .post("/v1/chat/completions", &request) .await - .unwrap(); - response.assert_status(401); - let health = gateway.get("/health/live").await.unwrap(); - health.assert_ok(); + .unwrap() + .assert_ok(); } diff --git a/db/seeds.sql b/db/seeds.sql index d670ba24..c0173592 100644 --- a/db/seeds.sql +++ b/db/seeds.sql @@ -356,6 +356,10 @@ INSERT INTO system_settings (key, value, category, description) VALUES 'Default affinity key TTL in seconds (0-86400)'), ('gateway.retry_wait_secs', '120', 'gateway', 'How long after a request arrives it may still be sent again to upstreams that failed for the moment (overloaded, server error, broken connection) before any of the answer reached the client, in seconds (0-600; 0 = never)'), + ('gateway.model_discovery', '{}', 'gateway', + 'Exact client-facing model aliases and per-provider model capability overrides'), + ('gateway.web_tools', '{}', 'gateway', + 'Opt-in hosted web tools, authorized key lineages and outbound policy'), ('gateway.latency_strategy_k', '2.0', 'gateway', 'Exponent for the latency-strategy weighting (higher = more aggressive). Default 2.0 (aggressive).'), ('gateway.cb_enabled', 'true', 'gateway', diff --git a/deploy/docker/Dockerfile.server b/deploy/docker/Dockerfile.server index 266858b9..435a0f84 100644 --- a/deploy/docker/Dockerfile.server +++ b/deploy/docker/Dockerfile.server @@ -12,7 +12,9 @@ COPY deploy/clickhouse/ deploy/clickhouse/ # Determine musl target from the native build arch ARG TARGETARCH -RUN apt-get update && apt-get install -y musl-tools && \ +ENV TW_WASM_CLANG=/usr/bin/clang-15 TW_WASM_AR=/usr/bin/llvm-ar-15 +RUN apt-get update && apt-get install -y musl-tools clang-15 llvm-15 && \ + rustup target add wasm32-unknown-unknown && \ if [ "$TARGETARCH" = "arm64" ]; then \ rustup target add aarch64-unknown-linux-musl; \ else \ diff --git a/deploy/helm/think-watch/Chart.yaml b/deploy/helm/think-watch/Chart.yaml index 5dddbe5f..1cbec638 100644 --- a/deploy/helm/think-watch/Chart.yaml +++ b/deploy/helm/think-watch/Chart.yaml @@ -2,8 +2,8 @@ apiVersion: v2 name: think-watch description: Enterprise AI API Gateway & MCP Management Platform type: application -version: 3.11.0 -appVersion: "3.11.0" +version: 3.12.0 +appVersion: "3.12.0" keywords: - ai - gateway diff --git a/docs/operations/resources.md b/docs/operations/resources.md index 57a3e49d..c2f9fc3c 100644 --- a/docs/operations/resources.md +++ b/docs/operations/resources.md @@ -36,11 +36,14 @@ limit and use the same shared budget. MCP tool discovery, provider and registry catalogs, OAuth discovery, audit forwarder errors and S3 reads also use bounded reads, so administrative/background requests cannot bypass admission on ingress. -Admission is incremental, including chunked responses. Readers reject growth -before appending rather than waiting while holding partial bodies. Reservations -follow shared buffers through transformations, slow clients, cancellation and -detached stream audit work. Cache reads reserve before fetching and use bounded -Redis reads even if a key changes between the size check and fetch. +Request admission is FIFO. Known-length uploads reserve before reading and +then continue concurrently. A single unknown-length upload can hold partial +data while waiting for budget. Queue and budget waits are bounded at 30 +seconds, and an upload idle for 30 seconds returns 408. Reservations follow +shared buffers through transformations, slow clients, cancellation and +detached stream audit work. Upstream response reads still reject growth before +appending when space is exhausted. Cache reads reserve before fetching and use +bounded Redis reads even if a key changes between the size check and fetch. Memory pressure produces HTTP 429 with `Retry-After: 1` before streaming starts. Already-started streams terminate with an error event (MCP code `-32005`). diff --git a/web/package.json b/web/package.json index 6779652d..5c6680d7 100644 --- a/web/package.json +++ b/web/package.json @@ -1,7 +1,7 @@ { "name": "web", "private": true, - "version": "3.11.0", + "version": "3.12.0", "type": "module", "packageManager": "pnpm@12.14.0", "scripts": { diff --git a/web/scripts/check-i18n.mjs b/web/scripts/check-i18n.mjs index 28bbdd00..285789be 100644 --- a/web/scripts/check-i18n.mjs +++ b/web/scripts/check-i18n.mjs @@ -97,6 +97,7 @@ const DYNAMIC_ENUMS = { // Tags emitted by the Promise.all loader in src/routes/admin/settings.tsx. // Keep in lockstep with the `tag('', ...)` calls there. 'settingsPage.loadKey.${_}': ['serverInfo', 'auditConfig', 'settings', 'health', 'roles'], + 'settings.hostedWeb.${_}': ['maxResults', 'readPages', 'pageChars', 'timeout', 'concurrency', 'interval', 'cache', 'maxCalls'], 'mcpStore.category.${_}': [ 'developer', 'database', 'communication', 'cloud', 'utility', 'knowledge', 'productivity', diff --git a/web/src/i18n/en.json b/web/src/i18n/en.json index 9068e648..74581130 100644 --- a/web/src/i18n/en.json +++ b/web/src/i18n/en.json @@ -1706,6 +1706,86 @@ "latencyStrategyK": "Latency aggressiveness (k)", "latencyStrategyKHint": "Exponent in 1/latency^k. Higher = more aggressive bias toward fast routes. Default 2.0 (aggressive). Range 0.5–8.0.", "retryWaitSecs": "Retry window (seconds)", + "gatewayConflict": "Another administrator changed these settings. Your edits have been kept in this form.", + "gatewayDiscardReload": "Discard edits and load latest", + "hostedWeb": { + "diagnostic": "Test saved settings", + "saveToTest": "Save changes to test this configuration.", + "testAction": "Test operation", + "testSearch": "Web search", + "testOpen": "Read a web page", + "testFind": "Find text on a page", + "testQuery": "Search query", + "testUrl": "Public page URL", + "findText": "Text to find", + "runTest": "Run test", + "testFailed": "The web-tool test failed.", + "testResult": "{{count}} result(s) in {{ms}} ms", + "testCached": "Cached", + "testTruncated": "Page text truncated", + "title": "Hosted web tools", + "enable": "Run web tools in ThinkWatch", + "sources": "Search sources", + "autoSources": "Use automatic source order", + "moveUp": "Move {{engine}} up", + "moveDown": "Move {{engine}} down", + "keys": "Authorized API keys", + "searchKeys": "Search by key name or prefix", + "unavailableKey": "Unavailable key ({{id}})", + "addKey": "Authorize {{name}}", + "removeKey": "Remove authorization for {{name}}", + "keysFailed": "Could not load API keys.", + "noKeys": "No matching AI gateway keys to add.", + "requireKey": "Choose at least one API key to enable hosted web tools.", + "advanced": "Advanced options", + "allow": "Allowed domains", + "block": "Blocked domains", + "proxy": "Outbound connection", + "systemProxy": "System proxy settings", + "direct": "Direct connection", + "language": "Search language (optional)", + "region": "Search region (optional)", + "maxResults": "Maximum search results", + "readPages": "Result pages to read", + "pageChars": "Maximum characters per page", + "timeout": "Tool timeout (seconds)", + "concurrency": "Concurrent tool calls", + "interval": "Search interval (milliseconds)", + "cache": "Cache duration (seconds)", + "maxCalls": "Maximum tool calls per request", + "fallback": "Fall back to the upstream on failure", + "invalidLimits": "Enter numbers within the displayed limits." + }, + "modelDiscovery": { + "nameCollision": "These names are already in use by models: {{models}}", + "title": "Model discovery", + "aliases": "Model aliases", + "addAlias": "Add alias", + "aliasName": "Alias name", + "target": "Target model", + "hideOriginal": "Hide the original in model lists", + "profiles": "Provider capability overrides", + "addProfile": "Add override", + "provider": "Provider", + "selectProvider": "Select a provider", + "upstreamModel": "Upstream model ID", + "contextWindow": "Context window (tokens)", + "maxOutput": "Maximum output (tokens)", + "images": "Image input", + "reasoning": "Reasoning support", + "yes": "Supported", + "no": "Unsupported", + "history": "Reasoning history format", + "historyBlocks": "Thinking content blocks", + "historyDisabled": "Omit reasoning history", + "effort": "Reasoning levels", + "effortFixed": "Non-adjustable", + "effortManual": "Choose supported levels", + "effortDefault": "Default reasoning level", + "unavailableProvider": "Unavailable provider ({{id}})", + "providersFailed": "Could not load the provider list.", + "incomplete": "Use unique names and fill in each target model or provider override before saving." + }, "retryWaitSecsHint": "When every route failed for the moment (overloaded, a server error, a broken connection) before any of the answer reached the client, the request is sent again after a short wait, for up to this long after it arrived. 0 disables retries. Range 0–600.", "strategy": { "weighted": "Weighted random", @@ -1895,6 +1975,11 @@ "mcpGatewaySection": "MCP Gateway", "mcpGatewayDesc": "Connect MCP-compatible clients to access tools registered in ThinkWatch", "claudeCode": "Claude Code", + "codexDesc": "Connect Codex to ThinkWatch and let the model picker discover the models available to your API key.", + "codexEnv": "Set THINKWATCH_GATEWAY_KEY to your AI gateway key in the environment that launches Codex.", + "codexConfig": "Copy this configuration into the corresponding sections of ~/.codex/config.toml.", + "codexLaunch": "Launch Codex with codex --profile thinkwatch. Restart any running Codex session after changing its configuration.", + "codexLocalCatalog": "Remove any model_catalog_json override for this profile. Codex gives the local file precedence over the remote catalog.", "cursor": "Cursor", "continue": "Continue", "cline": "Cline", diff --git a/web/src/i18n/zh.json b/web/src/i18n/zh.json index bb3dfcef..a3f779d6 100644 --- a/web/src/i18n/zh.json +++ b/web/src/i18n/zh.json @@ -1706,6 +1706,86 @@ "latencyStrategyK": "延迟激进度 (k)", "latencyStrategyKHint": "权重 ∝ 1/延迟^k。k 越大对慢路由的惩罚越激进。默认 2.0(激进)。范围 0.5–8.0。", "retryWaitSecs": "重试时限 (秒)", + "gatewayConflict": "其他管理员已修改此配置。当前编辑仍保留在表单中。", + "gatewayDiscardReload": "放弃编辑并加载最新版本", + "hostedWeb": { + "diagnostic": "测试已保存的设置", + "saveToTest": "保存更改后即可测试。", + "testAction": "测试操作", + "testSearch": "网页搜索", + "testOpen": "读取网页", + "testFind": "页内查找", + "testQuery": "搜索词", + "testUrl": "公开网页地址", + "findText": "要查找的文本", + "runTest": "运行测试", + "testFailed": "网页工具测试失败。", + "testResult": "找到 {{count}} 条结果,耗时 {{ms}} 毫秒", + "testCached": "缓存命中", + "testTruncated": "页面正文已截断", + "title": "托管网页工具", + "enable": "由 ThinkWatch 执行网页工具", + "sources": "搜索源", + "autoSources": "使用自动搜索顺序", + "moveUp": "上移 {{engine}}", + "moveDown": "下移 {{engine}}", + "keys": "已授权的 API Key", + "searchKeys": "按 Key 名称或前缀搜索", + "unavailableKey": "不可用的 Key({{id}})", + "addKey": "授权 {{name}}", + "removeKey": "取消对 {{name}} 的授权", + "keysFailed": "无法加载 API Key 列表。", + "noKeys": "没有可添加的匹配 AI 网关 Key。", + "requireKey": "请至少选择一个 API Key,再启用托管网页工具。", + "advanced": "高级选项", + "allow": "允许的域名", + "block": "阻止的域名", + "proxy": "出网方式", + "systemProxy": "使用系统代理设置", + "direct": "直接连接", + "language": "搜索语言(选填)", + "region": "搜索地区(选填)", + "maxResults": "最多返回的搜索结果", + "readPages": "读取的结果页面数", + "pageChars": "每页最大字符数", + "timeout": "工具超时(秒)", + "concurrency": "工具并发数", + "interval": "搜索间隔(毫秒)", + "cache": "缓存时间(秒)", + "maxCalls": "每次请求最多调用次数", + "fallback": "执行失败时交给上游", + "invalidLimits": "请输入范围内的整数。" + }, + "modelDiscovery": { + "nameCollision": "这些名称已被模型使用:{{models}}", + "title": "模型发现", + "aliases": "模型别名", + "addAlias": "添加别名", + "aliasName": "别名", + "target": "目标模型", + "hideOriginal": "在模型列表中隐藏原名", + "profiles": "供应商能力覆盖", + "addProfile": "添加覆盖", + "provider": "供应商", + "selectProvider": "选择供应商", + "upstreamModel": "上游模型 ID", + "contextWindow": "上下文窗口(Token)", + "maxOutput": "最大输出(Token)", + "images": "图像输入", + "reasoning": "推理支持", + "yes": "支持", + "no": "不支持", + "history": "推理历史格式", + "historyBlocks": "思考内容块", + "historyDisabled": "省略推理历史", + "effort": "推理档位", + "effortFixed": "不可调节", + "effortManual": "选择支持的档位", + "effortDefault": "默认推理档位", + "unavailableProvider": "不可用的供应商({{id}})", + "providersFailed": "无法加载供应商列表。", + "incomplete": "保存前请使用唯一名称,并填写每个目标模型或供应商覆盖。" + }, "retryWaitSecsHint": "所有路由都暂时失败(过载、服务器错误、连接中断)且回答尚未有任何内容发给客户端时,稍等后重发请求,直到请求到达后的这段时间为止。0 = 不重试。范围 0–600。", "strategy": { "weighted": "加权随机", @@ -1895,6 +1975,11 @@ "mcpGatewaySection": "MCP 网关", "mcpGatewayDesc": "将 MCP 兼容客户端连接到 ThinkWatch 以访问注册的工具", "claudeCode": "Claude Code", + "codexDesc": "将 Codex 接入 ThinkWatch,模型选择器会自动列出当前 API Key 可用的模型。", + "codexEnv": "在启动 Codex 的环境中,将 THINKWATCH_GATEWAY_KEY 设为你的 AI 网关 Key。", + "codexConfig": "复制下方配置,合并到 ~/.codex/config.toml 的对应区块。", + "codexLaunch": "使用 codex --profile thinkwatch 启动。修改配置后,重启已运行的 Codex。", + "codexLocalCatalog": "如果此配置启用了 model_catalog_json,请移除该设置。Codex 会优先读取本地目录文件。", "cursor": "Cursor", "continue": "Continue", "cline": "Cline", diff --git a/web/src/routes/admin/settings.tsx b/web/src/routes/admin/settings.tsx index a7e7f614..d9426431 100644 --- a/web/src/routes/admin/settings.tsx +++ b/web/src/routes/admin/settings.tsx @@ -34,6 +34,8 @@ import { NumberField } from './settings/NumberField'; import { useFieldAutosave } from './settings/useFieldAutosave'; import { SaveIndicator } from './settings/SaveIndicator'; import { OidcWizardCard } from './settings/oidc/OidcWizardCard'; +import { HostedWebSettings, type HostedWebConfig } from './settings/HostedWebSettings'; +import { ModelDiscoverySettings, type ModelDiscoveryConfig } from './settings/ModelDiscoverySettings'; // One PATCH per field, keyed by the backend's dotted setting id. The // server treats the body as a merge so sending a one-key object is the @@ -42,6 +44,17 @@ async function patchOne(key: string, value: unknown): Promise { await apiPatch('/api/admin/settings', { settings: { [key]: value } }); } +async function updateGatewaySetting(key: string, value: unknown, expected: unknown) { + await apiPatch('/api/admin/settings', { settings: { [key]: value }, expected: { [key]: expected } }); +} + +async function loadGatewaySetting(key: string): Promise { + const settings = await api('/api/admin/settings/category/gateway'); + const current = settings.find((item) => item.key === key); + if (!current) throw new Error(key + ' is missing'); + return current.value as T; +} + // --------------------------------------------------------------------------- // Component // --------------------------------------------------------------------------- @@ -106,6 +119,8 @@ export function SettingsPage() { const [defaultAffinityTtlSecs, setDefaultAffinityTtlSecs] = useState(300); const [latencyStrategyK, setLatencyStrategyK] = useState(2.0); const [retryWaitSecs, setRetryWaitSecs] = useState(120); + const [modelDiscovery, setModelDiscovery] = useState(); + const [hostedWeb, setHostedWeb] = useState(); const [cbEnabled, setCbEnabled] = useState(true); const [cbErrorPct, setCbErrorPct] = useState(50); const [cbMinSamples, setCbMinSamples] = useState(10); @@ -164,6 +179,8 @@ export function SettingsPage() { setDefaultAffinityTtlSecs(num(getSettingValue(data, 'gateway', 'default_affinity_ttl_secs'), 300)); setLatencyStrategyK(Number(getSettingValue(data, 'gateway', 'latency_strategy_k')) || 2.0); setRetryWaitSecs(num(getSettingValue(data, 'gateway', 'retry_wait_secs'), 120)); + setModelDiscovery(getSettingValue(data, 'gateway', 'model_discovery') as ModelDiscoveryConfig | undefined); + setHostedWeb(getSettingValue(data, 'gateway', 'web_tools') as HostedWebConfig | undefined); setCbEnabled(getSettingValue(data, 'gateway', 'cb_enabled') !== false); setCbErrorPct(num(getSettingValue(data, 'gateway', 'cb_error_pct'), 50)); setCbMinSamples(num(getSettingValue(data, 'gateway', 'cb_min_samples'), 10)); @@ -678,6 +695,27 @@ export function SettingsPage() { + {hostedWeb && ( + { + await updateGatewaySetting('gateway.web_tools', value, hostedWeb); + setHostedWeb(value); + }} loadLatest={async () => { + const latest = await loadGatewaySetting('gateway.web_tools'); + setHostedWeb(latest); + return latest; + }} /> + )} + {modelDiscovery && ( + { + await updateGatewaySetting('gateway.model_discovery', value, modelDiscovery); + setModelDiscovery(value); + }} loadLatest={async () => { + const latest = await loadGatewaySetting('gateway.model_discovery'); + setModelDiscovery(latest); + return latest; + }} /> + )} + {/* Circuit breaker: trip routes whose recent error rate crosses the threshold so they stop receiving traffic until the cooldown elapses. Disabling skips the trip diff --git a/web/src/routes/admin/settings/GatewaySettings.test.tsx b/web/src/routes/admin/settings/GatewaySettings.test.tsx new file mode 100644 index 00000000..4914eff2 --- /dev/null +++ b/web/src/routes/admin/settings/GatewaySettings.test.tsx @@ -0,0 +1,125 @@ +import { beforeAll, expect, it, vi } from 'vitest'; +import { screen, waitFor } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { api, apiPost } from '@/lib/api'; +import { renderWithQueryClient } from '@/test/render'; +import { HostedWebSettings } from './HostedWebSettings'; +import { ModelDiscoverySettings } from './ModelDiscoverySettings'; + +vi.mock('@/lib/api', () => ({ api: vi.fn(), apiPost: vi.fn() })); + +beforeAll(() => { + Object.assign(Element.prototype, { + hasPointerCapture: () => false, + setPointerCapture: () => {}, + releasePointerCapture: () => {}, + scrollIntoView: () => {}, + }); +}); + +it('saves selected search sources in order and grants access using a rotated key’s lineage', async () => { + const persist = vi.fn().mockResolvedValue(undefined); + vi.mocked(api).mockResolvedValue({ + data: [ + { id: 'new', lineage_id: 'original', key_prefix: 'tw-console', name: 'Developer CLI', surfaces: ['ai_gateway'], is_active: true }, + { id: 'old', lineage_id: 'original', key_prefix: 'tw-old', name: 'Developer CLI', surfaces: ['ai_gateway'], is_active: true }, + { id: 'mcp', lineage_id: 'mcp', key_prefix: 'tw-mcp', name: 'MCP only', surfaces: ['mcp_gateway'], is_active: true }, + ], + total: 3, + }); + const user = userEvent.setup(); + renderWithQueryClient(); + + await user.click(screen.getByRole('switch', { name: 'Run web tools in ThinkWatch' })); + expect(screen.getByRole('button', { name: 'Save' })).toBeDisabled(); + await user.click(screen.getByRole('switch', { name: 'Use automatic source order' })); + await user.click(screen.getByRole('checkbox', { name: 'Google Docs' })); + await user.click(screen.getByRole('button', { name: 'Move Bing up' })); + await user.click(await screen.findByRole('button', { name: 'Authorize Developer CLI (tw-console)' })); + expect(screen.queryByText(/MCP only|tw-old/)).not.toBeInTheDocument(); + await user.click(screen.getByRole('button', { name: 'Save' })); + + await waitFor(() => expect(persist).toHaveBeenCalledWith({ + enabled_lineages: ['original'], + execution: { + mode: 'core', engines: ['bing', 'duckduckgo'], max_results: 12, + allowed_domains: [], blocked_domains: [], + }, + })); +}); + +it('edits model aliases and provider capabilities using controls', async () => { + const persist = vi.fn().mockResolvedValue(undefined); + vi.mocked(api).mockImplementation(async (url) => { + if (url === '/api/admin/models/ids') return [{ model_id: 'coding' }]; + if (url === '/api/admin/providers') return [{ id: 'provider-id', name: 'Engineering' }]; + if (url === '/api/admin/providers/provider-id/remote-models') return [{ id: 'upstream-code' }]; + throw new Error('Unexpected request'); + }); + const user = userEvent.setup(); + renderWithQueryClient(); + await user.click(screen.getByRole('button', { name: 'Add alias' })); + await user.type(screen.getByRole('textbox', { name: 'Alias name' }), 'fast'); + await waitFor(() => expect(api).toHaveBeenCalledWith('/api/admin/models/ids', expect.anything())); + await user.click(screen.getByRole('combobox', { name: 'Target model' })); + await user.click(await screen.findByRole('option', { name: 'coding' })); + await user.click(screen.getByRole('checkbox', { name: 'Hide the original in model lists' })); + + await user.click(screen.getByRole('button', { name: 'Add override' })); + await waitFor(() => expect(api).toHaveBeenCalledWith('/api/admin/providers', expect.anything())); + await user.click(screen.getByRole('combobox', { name: 'Provider' })); + await user.click(await screen.findByRole('option', { name: 'Engineering' })); + await user.click(screen.getByRole('combobox', { name: 'Upstream model ID' })); + await user.click(await screen.findByRole('option', { name: 'upstream-code' })); + await user.type(screen.getByRole('spinbutton', { name: 'Context window (tokens)' }), '128000'); + await user.click(screen.getByRole('button', { name: 'Save' })); + + await waitFor(() => expect(persist).toHaveBeenCalledWith({ + aliases: { fast: { target: 'coding', hide_original: true } }, + profiles: { 'provider-id': { 'upstream-code': { context_window: 128000 } } }, + })); +}); + +it('tests the saved search settings and shows sources, warnings, and failures', async () => { + vi.mocked(api).mockResolvedValue({ data: [], total: 0 }); + vi.mocked(apiPost).mockResolvedValueOnce({ + sources: [{ url: 'https://example.org/', title: 'Example guide', text: 'Page excerpt', engine: 'bing' }], + elapsed_ms: 21, cached: true, truncated: false, + warnings: [{ text: 'DuckDuckGo timed out' }], + }).mockRejectedValueOnce(new Error('All configured sources failed')); + const user = userEvent.setup(); + renderWithQueryClient(); + + await user.type(screen.getByRole('searchbox', { name: 'Search query' }), 'example guide'); + await user.click(screen.getByRole('button', { name: 'Run test' })); + expect(await screen.findByRole('link', { name: 'Example guide' })).toHaveAttribute('href', 'https://example.org/'); + expect(apiPost).toHaveBeenCalledWith('/api/admin/settings/web-tools/test', { action: 'search', query: 'example guide' }); + expect(screen.getByText('DuckDuckGo timed out')).toBeInTheDocument(); + expect(screen.getByRole('status')).toHaveTextContent('21 ms'); + expect(screen.getByRole('status')).toHaveTextContent('Cached'); + + await user.click(screen.getByRole('button', { name: 'Run test' })); + expect(await screen.findByRole('alert')).toHaveTextContent('All configured sources failed'); + expect(screen.queryByRole('link', { name: 'Example guide' })).toBeNull(); +}); + +it('keeps stale alias edits until the administrator chooses to load the latest settings', async () => { + const persist = vi.fn().mockRejectedValue(Object.assign(new Error('Conflict'), { status: 409 })); + const loadLatest = vi.fn().mockResolvedValue({ aliases: { newer: { target: 'coding' } } }); + vi.mocked(api).mockResolvedValue([{ model_id: 'coding' }]); + const user = userEvent.setup(); + renderWithQueryClient(); + + const name = screen.getByRole('textbox', { name: 'Alias name' }); + await user.clear(name); + await user.type(name, 'my-draft'); + await user.click(screen.getByRole('button', { name: 'Save' })); + expect(await screen.findByText(/Another administrator changed/)).toBeInTheDocument(); + expect(name).toHaveValue('my-draft'); + expect(screen.getByRole('button', { name: 'Save' })).toBeDisabled(); + expect(loadLatest).not.toHaveBeenCalled(); + await user.click(screen.getByRole('button', { name: 'Discard edits and load latest' })); + await waitFor(() => expect(screen.getByRole('textbox', { name: 'Alias name' })).toHaveValue('newer')); +}); diff --git a/web/src/routes/admin/settings/HostedWebDiagnostic.tsx b/web/src/routes/admin/settings/HostedWebDiagnostic.tsx new file mode 100644 index 00000000..25229717 --- /dev/null +++ b/web/src/routes/admin/settings/HostedWebDiagnostic.tsx @@ -0,0 +1,108 @@ +import { useState, type FormEvent } from 'react'; +import { useTranslation } from 'react-i18next'; +import { Button } from '@/components/ui/button'; +import { Input } from '@/components/ui/input'; +import { apiPost } from '@/lib/api'; +import { GatewayField, GatewaySelect } from './gateway-form'; + +type Action = 'search' | 'open_page' | 'find_in_page'; + +interface DiagnosticResult { + sources: { url: string; title: string; text: string; engine: string | null }[]; + elapsed_ms: number; + cached: boolean; + truncated: boolean; + warnings: { text: string }[]; +} + +const engineNames: Record = { google_docs: 'Google Docs', duckduckgo: 'DuckDuckGo', bing: 'Bing' }; + +function publicLink(value: string) { + try { + const url = new URL(value); + return url.protocol === 'https:' || url.protocol === 'http:' ? url.href : undefined; + } catch { + return undefined; + } +} + +export function HostedWebDiagnostic({ disabled }: { disabled: boolean }) { + const { t } = useTranslation(); + const [action, setAction] = useState('search'); + const [input, setInput] = useState(''); + const [pattern, setPattern] = useState(''); + const [busy, setBusy] = useState(false); + const [result, setResult] = useState(null); + const [error, setError] = useState(null); + + async function submit(event: FormEvent) { + event.preventDefault(); + if (disabled || busy || !input.trim() || (action === 'find_in_page' && !pattern.trim())) return; + const request = action === 'search' ? { action, query: input.trim() } + : action === 'open_page' ? { action, url: input.trim() } + : { action, url: input.trim(), pattern: pattern.trim() }; + setBusy(true); + setError(null); + setResult(null); + try { + setResult(await apiPost('/api/admin/settings/web-tools/test', request)); + } catch (failure) { + setError(failure instanceof Error ? failure.message : t('settings.hostedWeb.testFailed')); + } finally { + setBusy(false); + } + } + + return ( +
void submit(event)} className="mt-5 max-w-2xl space-y-3 border-t pt-4"> +

{t('settings.hostedWeb.diagnostic')}

+ {disabled &&

{t('settings.hostedWeb.saveToTest')}

} +
+
+ { + setAction(value as Action); + setInput(''); + setResult(null); + setError(null); + }} /> + + {(id) => setInput(event.target.value)} required />} + + {action === 'find_in_page' && + {(id) => setPattern(event.target.value)} required />} + } +
+ +
+ {error &&

{error}

} + {result && !disabled &&
+

+ {t('settings.hostedWeb.testResult', { count: result.sources.length, ms: result.elapsed_ms })} + {result.cached ? ' · ' + t('settings.hostedWeb.testCached') : ''} + {result.truncated ? ' · ' + t('settings.hostedWeb.testTruncated') : ''} +

+ {result.warnings.map((warning, index) =>

{warning.text}

)} +
    + {result.sources.map((source, index) => { + const url = publicLink(source.url); + return ( +
  • + {url ? {source.title || source.url} + : {source.title || source.url}} + {source.engine &&

    {engineNames[source.engine] ?? source.engine}

    } +

    {source.text}

    +
  • + ); + })} +
+
} +
+ ); +} diff --git a/web/src/routes/admin/settings/HostedWebSettings.tsx b/web/src/routes/admin/settings/HostedWebSettings.tsx new file mode 100644 index 00000000..f44e2e95 --- /dev/null +++ b/web/src/routes/admin/settings/HostedWebSettings.tsx @@ -0,0 +1,272 @@ +import { useState } from 'react'; +import { useQuery } from '@tanstack/react-query'; +import { useTranslation } from 'react-i18next'; +import { ArrowDown, ArrowUp, X } from 'lucide-react'; +import { Button } from '@/components/ui/button'; +import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'; +import { Checkbox } from '@/components/ui/checkbox'; +import { Input } from '@/components/ui/input'; +import { Label } from '@/components/ui/label'; +import { Switch } from '@/components/ui/switch'; +import { Textarea } from '@/components/ui/textarea'; +import { fetchAllPaginated } from '@/lib/paginated-fetch'; +import { GatewayField, GatewaySave, GatewaySelect } from './gateway-form'; +import { useGatewayForm } from './useGatewayForm'; +import { HostedWebDiagnostic } from './HostedWebDiagnostic'; + +const engines = ['google_docs', 'duckduckgo', 'bing'] as const; +type Engine = typeof engines[number]; +const engineNames: Record = { + google_docs: 'Google Docs', + duckduckgo: 'DuckDuckGo', + bing: 'Bing', +}; + +const limits = [ + { key: 'max_results', label: 'maxResults', min: 1, max: 20, default: 8 }, + { key: 'read_pages', label: 'readPages', min: 0, max: 5, default: 2 }, + { key: 'max_page_chars', label: 'pageChars', min: 1000, max: 50000, default: 12000 }, + { key: 'timeout_secs', label: 'timeout', min: 1, max: 120, default: 30 }, + { key: 'max_concurrent', label: 'concurrency', min: 1, max: 16, default: 4 }, + { key: 'interval_ms', label: 'interval', min: 250, max: 60000, default: 1000 }, + { key: 'cache_secs', label: 'cache', min: 0, max: 86400, default: 600 }, + { key: 'max_tool_calls', label: 'maxCalls', min: 1, max: 32, default: 8 }, +] as const; + +export interface HostedWebConfig { + enabled_lineages?: string[]; + execution?: { + mode?: 'upstream' | 'core'; + engines?: Engine[]; + proxy?: string; + language?: string; + region?: string; + allowed_domains?: string[]; + blocked_domains?: string[]; + fallback_to_upstream?: boolean; + } & Partial>; +} + +interface GatewayKey { + lineage_id: string; + key_prefix: string; + name: string; + surfaces: string[]; + is_active: boolean; + expires_at?: string | null; + grace_period_ends_at?: string | null; +} + +function keyLabel(key: GatewayKey) { + return key.name + ' (' + key.key_prefix + ')'; +} + +function KeyPicker({ + selected, onChange, +}: { + selected: string[]; + onChange: (keys: string[]) => void; +}) { + const { t } = useTranslation(); + const [search, setSearch] = useState(''); + const keys = useQuery({ + queryKey: ['gateway-web-tool-keys'], + queryFn: async () => { + const rows = await fetchAllPaginated('/api/keys', 100, 'data'); + const now = Date.now(); + const eligible = new Map(); + for (const key of rows) { + if (key.lineage_id && key.is_active && key.surfaces.includes('ai_gateway') + && (!key.expires_at || Date.parse(key.expires_at) > now) + && (!key.grace_period_ends_at || Date.parse(key.grace_period_ends_at) > now) + && !eligible.has(key.lineage_id)) { + eligible.set(key.lineage_id, key); + } + } + return eligible; + }, + }); + const available = keys.data; + const matching = [...(available?.values() ?? [])] + .filter((key) => !selected.includes(key.lineage_id)) + .filter((key) => keyLabel(key).toLowerCase().includes(search.trim().toLowerCase())); + + return ( +
+ + {(id) => setSearch(e.target.value)} />} + + {selected.length > 0 && ( +
    + {selected.map((id) => { + const key = available?.get(id); + return ( +
  • + {key ? keyLabel(key) : t('settings.hostedWeb.unavailableKey', { id: id.slice(0, 8) })} + +
  • + ); + })} +
+ )} + {keys.isPending &&

{t('common.loading')}

} + {keys.isError && ( +
+

{t('settings.hostedWeb.keysFailed')}

+ +
+ )} + {keys.isSuccess && matching.length === 0 &&

{t('settings.hostedWeb.noKeys')}

} + {matching.length > 0 && ( +
    + {matching.map((key) => ( +
  • + {keyLabel(key)} + +
  • + ))} +
+ )} + {selected.length === 0 &&

{t('settings.hostedWeb.requireKey')}

} +
+ ); +} + +function domains(text: string) { + return [...new Set(text.split(/[,\s]+/).filter(Boolean))]; +} + +function draftFrom(settings: HostedWebConfig) { + return { + enabled_lineages: settings.enabled_lineages ?? [], + execution: settings.execution ?? {}, + allow: (settings.execution?.allowed_domains ?? []).join('\n'), + block: (settings.execution?.blocked_domains ?? []).join('\n'), + }; +} + +export function HostedWebSettings({ + initial, persist, loadLatest, +}: { + initial: HostedWebConfig; + persist: (value: HostedWebConfig) => Promise; + loadLatest?: () => Promise; +}) { + const { t } = useTranslation(); + const form = useGatewayForm(draftFrom(initial), ({ enabled_lineages, execution, allow, block }) => persist({ + enabled_lineages, + execution: { ...execution, allowed_domains: domains(allow), blocked_domains: domains(block) }, + }), loadLatest && (async () => draftFrom(await loadLatest()))); + const { execution, enabled_lineages } = form.draft; + const enabled = execution.mode === 'core'; + const order = execution.engines ?? []; + const automatic = order.length === 0; + const displayed = automatic ? engines : [...order, ...engines.filter((engine) => !order.includes(engine))]; + const invalidLimits = limits.some(({ key, min, max, default: fallback }) => { + const value = execution[key] ?? fallback; + return !Number.isInteger(value) || value < min || value > max; + }); + + function update(patch: HostedWebConfig['execution']) { + form.update({ ...form.draft, execution: { ...execution, ...patch } }); + } + + function move(engine: Engine, offset: number) { + const next = [...order]; + const from = next.indexOf(engine); + next.splice(from, 1); + next.splice(from + offset, 0, engine); + update({ engines: next }); + } + + return ( + + {t('settings.hostedWeb.title')} + +
+
+ update({ mode: checked ? 'core' : 'upstream' })} /> + +
+ {enabled && <> +
+

{t('settings.hostedWeb.sources')}

+
+ update({ engines: checked ? [] : [...engines] })} /> + +
+
    + {displayed.map((engine, i) => { + const checked = automatic || order.includes(engine); + return ( +
  1. +
    + {automatic ? {i + 1}. : ( + update({ engines: use ? [...order, engine] : order.filter((id) => id !== engine) })} /> + )} + {automatic ? {engineNames[engine]} : ( + + )} +
    + {!automatic && checked && ( +
    + + +
    + )} +
  2. + ); + })} +
+
+ form.update({ ...form.draft, enabled_lineages: keys })} /> +
+ {t('settings.hostedWeb.advanced')} +
+ + {(id) =>