Compare commits
12
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ed17bf091 | ||
|
|
ab0269df1a | ||
|
|
a99edc116a | ||
|
|
a3f6fd6378 | ||
|
|
607bd88ae3 | ||
|
|
d4a846f827 | ||
|
|
d83f8c84b7 | ||
|
|
9ed278bcb5 | ||
|
|
4118279da1 | ||
|
|
a5af873924 | ||
|
|
5ac646b3c6 | ||
|
|
420f00494f |
Generated
+583
-4
@@ -29,6 +29,18 @@ dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "allocator-api2"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.103"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a4385e2e34eb35d6b3efe798b9eb88096925d87726c0798709bf56d9ed84af3"
|
||||
|
||||
[[package]]
|
||||
name = "async-compression"
|
||||
version = "0.4.42"
|
||||
@@ -115,6 +127,21 @@ version = "1.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
|
||||
|
||||
[[package]]
|
||||
name = "cassowary"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53"
|
||||
|
||||
[[package]]
|
||||
name = "castaway"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dec551ab6e7578819132c713a93c022a05d60159dc86e7a7050223577484c55a"
|
||||
dependencies = [
|
||||
"rustversion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.2.66"
|
||||
@@ -148,6 +175,20 @@ dependencies = [
|
||||
"rand_core 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "compact_str"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fd622ebbb56a5b2ccb651b32b911cdeb2a9b4b11776b2473bf26a26a286244e"
|
||||
dependencies = [
|
||||
"castaway",
|
||||
"cfg-if",
|
||||
"itoa",
|
||||
"rustversion",
|
||||
"ryu",
|
||||
"static_assertions",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "compression-codecs"
|
||||
version = "0.4.38"
|
||||
@@ -165,6 +206,17 @@ version = "0.4.32"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cc14f565cf027a105f7a44ccf9e5b424348421a1d8952a8fc9d499d313107789"
|
||||
|
||||
[[package]]
|
||||
name = "console"
|
||||
version = "0.16.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4fe5f465a4f6fee88fad41b85d990f84c835335e85b5d9e6e63e0d06d28cba7c"
|
||||
dependencies = [
|
||||
"encode_unicode",
|
||||
"libc",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cpufeatures"
|
||||
version = "0.3.0"
|
||||
@@ -183,6 +235,15 @@ dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d85363c37faeca707aef026efa9f3b34d077bce547e48f770770625c6013679e"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-deque"
|
||||
version = "0.8.7"
|
||||
@@ -208,6 +269,72 @@ version = "0.8.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17"
|
||||
|
||||
[[package]]
|
||||
name = "crossterm"
|
||||
version = "0.28.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "829d955a0bb380ef178a640b91779e3987da38c9aea133b20614cfed8cdea9c6"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"crossterm_winapi",
|
||||
"futures-core",
|
||||
"mio",
|
||||
"parking_lot",
|
||||
"rustix 0.38.44",
|
||||
"signal-hook",
|
||||
"signal-hook-mio",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossterm_winapi"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling"
|
||||
version = "0.23.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "25ae13da2f202d56bd7f91c25fba009e7717a1e4a1cc98a76d844b65ae912e9d"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"darling_macro",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling_core"
|
||||
version = "0.23.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9865a50f7c335f53564bb694ef660825eb8610e0a53d3e11bf1b0d3df31e03b0"
|
||||
dependencies = [
|
||||
"ident_case",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"strsim",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "darling_macro"
|
||||
version = "0.23.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3984ec7bd6cfa798e62b4a642426a5be0e68f9401cfc2a01e3fa9ea2fcdb8d"
|
||||
dependencies = [
|
||||
"darling_core",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deranged"
|
||||
version = "0.5.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c"
|
||||
|
||||
[[package]]
|
||||
name = "dirs"
|
||||
version = "5.0.1"
|
||||
@@ -246,6 +373,18 @@ version = "1.0.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555"
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"
|
||||
|
||||
[[package]]
|
||||
name = "encode_unicode"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0"
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.35"
|
||||
@@ -264,6 +403,12 @@ dependencies = [
|
||||
"encoding_rs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||
|
||||
[[package]]
|
||||
name = "errno"
|
||||
version = "0.3.14"
|
||||
@@ -319,6 +464,12 @@ dependencies = [
|
||||
"miniz_oxide",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2"
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.2.2"
|
||||
@@ -416,6 +567,15 @@ dependencies = [
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getopts"
|
||||
version = "0.2.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cfe4fbac503b8d1f88e6676011885f34b7174f46e59956bba534ba83abded4df"
|
||||
dependencies = [
|
||||
"unicode-width 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.17"
|
||||
@@ -519,6 +679,7 @@ dependencies = [
|
||||
"thiserror 2.0.18",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -563,12 +724,14 @@ dependencies = [
|
||||
"async-stream",
|
||||
"async-trait",
|
||||
"bytes",
|
||||
"dirs",
|
||||
"eventsource-stream",
|
||||
"futures",
|
||||
"harness-core",
|
||||
"reqwest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tempfile",
|
||||
"thiserror 2.0.18",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
@@ -602,9 +765,22 @@ dependencies = [
|
||||
name = "harness-tui"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"crossterm",
|
||||
"dirs",
|
||||
"futures",
|
||||
"harness-app",
|
||||
"harness-core",
|
||||
"insta",
|
||||
"pulldown-cmark",
|
||||
"ratatui",
|
||||
"serde_json",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"tracing",
|
||||
"tracing-appender",
|
||||
"tracing-subscriber",
|
||||
"tui-textarea",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -616,15 +792,32 @@ dependencies = [
|
||||
"ahash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashbrown"
|
||||
version = "0.15.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1"
|
||||
dependencies = [
|
||||
"allocator-api2",
|
||||
"equivalent",
|
||||
"foldhash",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hashlink"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ba4ff7128dee98c7dc9794b6a411377e1404dba1c97deb8d1a55297bd25d8af"
|
||||
dependencies = [
|
||||
"hashbrown",
|
||||
"hashbrown 0.14.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
|
||||
|
||||
[[package]]
|
||||
name = "http"
|
||||
version = "1.4.2"
|
||||
@@ -805,6 +998,12 @@ dependencies = [
|
||||
"zerovec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ident_case"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "1.1.0"
|
||||
@@ -842,12 +1041,55 @@ dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "2.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "79cf5c93f93228cf8efb3ba362535fb11199ac548a09ce117c9b1adc3030d706"
|
||||
dependencies = [
|
||||
"rustversion",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "insta"
|
||||
version = "1.48.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86f0f8fee8c926415c58d6ae43a08523a26faccb2323f5e6b644fe7dd4ef6b82"
|
||||
dependencies = [
|
||||
"console",
|
||||
"once_cell",
|
||||
"similar",
|
||||
"tempfile",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "instability"
|
||||
version = "0.3.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5eb2d60ef19920a3a9193c3e371f726ec1dafc045dac788d0fb3704272458971"
|
||||
dependencies = [
|
||||
"darling",
|
||||
"indoc",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ipnet"
|
||||
version = "2.12.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d98f6fed1fde3f8c21bc40a1abb88dd75e67924f9cffc3ef95607bad8017f8e2"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
@@ -874,6 +1116,12 @@ dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.186"
|
||||
@@ -900,6 +1148,12 @@ dependencies = [
|
||||
"vcpkg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.4.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab"
|
||||
|
||||
[[package]]
|
||||
name = "linux-raw-sys"
|
||||
version = "0.12.1"
|
||||
@@ -927,12 +1181,30 @@ version = "0.4.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
|
||||
|
||||
[[package]]
|
||||
name = "lru"
|
||||
version = "0.12.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "234cf4f4a04dc1f57e24b96cc0cd600cf2af460d4161ac5ecdd0af8e1f3b2a38"
|
||||
dependencies = [
|
||||
"hashbrown 0.15.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lru-slab"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154"
|
||||
|
||||
[[package]]
|
||||
name = "matchers"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9"
|
||||
dependencies = [
|
||||
"regex-automata",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
@@ -971,6 +1243,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "02bd0af71c67b473010cbbc60715ee815645a4dc942899111f494b4b737d6fda"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"log",
|
||||
"wasi",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
@@ -985,6 +1258,21 @@ dependencies = [
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "nu-ansi-term"
|
||||
version = "0.50.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5"
|
||||
dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-conv"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441"
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.21.4"
|
||||
@@ -1020,6 +1308,12 @@ dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "paste"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.3.2"
|
||||
@@ -1047,6 +1341,12 @@ dependencies = [
|
||||
"zerovec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "powerfmt"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.21"
|
||||
@@ -1065,6 +1365,25 @@ dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pulldown-cmark"
|
||||
version = "0.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f86ba2052aebccc42cbbb3ed234b8b13ce76f75c3551a303cb2bcffcff12bb14"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"getopts",
|
||||
"memchr",
|
||||
"pulldown-cmark-escape",
|
||||
"unicase",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pulldown-cmark-escape"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"
|
||||
|
||||
[[package]]
|
||||
name = "quinn"
|
||||
version = "0.11.11"
|
||||
@@ -1197,6 +1516,27 @@ dependencies = [
|
||||
"rand_core 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ratatui"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eabd94c2f37801c20583fc49dd5cd6b0ba68c716787c2dd6ed18571e1e63117b"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cassowary",
|
||||
"compact_str",
|
||||
"crossterm",
|
||||
"indoc",
|
||||
"instability",
|
||||
"itertools",
|
||||
"lru",
|
||||
"paste",
|
||||
"strum",
|
||||
"unicode-segmentation",
|
||||
"unicode-truncate",
|
||||
"unicode-width 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.18"
|
||||
@@ -1309,6 +1649,19 @@ version = "2.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d"
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys 0.4.15",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "1.1.4"
|
||||
@@ -1318,7 +1671,7 @@ dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
"libc",
|
||||
"linux-raw-sys",
|
||||
"linux-raw-sys 0.12.1",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
@@ -1474,6 +1827,15 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sharded-slab"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shell-words"
|
||||
version = "1.1.1"
|
||||
@@ -1486,6 +1848,27 @@ version = "2.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook"
|
||||
version = "0.3.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d881a16cf4426aa584979d30bd82cb33429027e42122b169753d6ef1085ed6e2"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"signal-hook-registry",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-mio"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b75a19a7a740b25bc7944bdee6172368f988763b744e3d4dfe753f6b4ece40cc"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"mio",
|
||||
"signal-hook",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.8"
|
||||
@@ -1536,12 +1919,52 @@ version = "1.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
|
||||
|
||||
[[package]]
|
||||
name = "strum"
|
||||
version = "0.26.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8fec0f0aef304996cf250b31b5a10dee7980c85da9d759361292b8bca5a18f06"
|
||||
dependencies = [
|
||||
"strum_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strum_macros"
|
||||
version = "0.26.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"rustversion",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "subtle"
|
||||
version = "2.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
||||
|
||||
[[package]]
|
||||
name = "symlink"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7973cce6668464ea31f176d85b13c7ab3bba2cb3b77a2ed26abd7801688010a"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.118"
|
||||
@@ -1580,9 +2003,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
|
||||
dependencies = [
|
||||
"fastrand",
|
||||
"getrandom 0.3.4",
|
||||
"getrandom 0.4.3",
|
||||
"once_cell",
|
||||
"rustix",
|
||||
"rustix 1.1.4",
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
@@ -1626,6 +2049,45 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thread_local"
|
||||
version = "1.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "18dfaaeddcb932337b5e7866ee7d0ce9b76d2fd092997146f187ec09b4558a50"
|
||||
dependencies = [
|
||||
"deranged",
|
||||
"num-conv",
|
||||
"powerfmt",
|
||||
"serde_core",
|
||||
"time-core",
|
||||
"time-macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time-core"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109"
|
||||
|
||||
[[package]]
|
||||
name = "time-macros"
|
||||
version = "0.2.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c431b87111666e491a90baa837f914fb45cd5dc3c268591b0220ff5057f2085f"
|
||||
dependencies = [
|
||||
"num-conv",
|
||||
"time-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinystr"
|
||||
version = "0.8.3"
|
||||
@@ -1763,6 +2225,19 @@ dependencies = [
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-appender"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "050686193eb999b4bb3bc2acfa891a13da00f79734704c4b8b4ef1a10b368a3c"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"symlink",
|
||||
"thiserror 2.0.18",
|
||||
"time",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-attributes"
|
||||
version = "0.1.31"
|
||||
@@ -1781,6 +2256,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
|
||||
dependencies = [
|
||||
"once_cell",
|
||||
"valuable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-log"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3"
|
||||
dependencies = [
|
||||
"log",
|
||||
"once_cell",
|
||||
"tracing-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319"
|
||||
dependencies = [
|
||||
"matchers",
|
||||
"nu-ansi-term",
|
||||
"once_cell",
|
||||
"regex-automata",
|
||||
"sharded-slab",
|
||||
"smallvec",
|
||||
"thread_local",
|
||||
"tracing",
|
||||
"tracing-core",
|
||||
"tracing-log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -1789,6 +2294,17 @@ version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
|
||||
|
||||
[[package]]
|
||||
name = "tui-textarea"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0a5318dd619ed73c52a9417ad19046724effc1287fb75cdcc4eca1d6ac1acbae"
|
||||
dependencies = [
|
||||
"crossterm",
|
||||
"ratatui",
|
||||
"unicode-width 0.2.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ulid"
|
||||
version = "1.2.1"
|
||||
@@ -1800,12 +2316,47 @@ dependencies = [
|
||||
"web-time",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicase"
|
||||
version = "2.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbc4bc3a9f746d862c45cb89d705aa10f187bb96c76001afab07a0d35ce60142"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.13.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6f5d3c3b1bf09027a88a6bc961fc00497d651009560b5463668dc81b0fa87a8"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-truncate"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3644627a5af5fa321c95b9b235a72fd24cd29c648c2c379431e6628655627bf"
|
||||
dependencies = [
|
||||
"itertools",
|
||||
"unicode-segmentation",
|
||||
"unicode-width 0.1.14",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fc81956842c57dac11422a97c3b8195a1ff727f06e85c84ed2e8aa277c9a0fd"
|
||||
|
||||
[[package]]
|
||||
name = "untrusted"
|
||||
version = "0.9.0"
|
||||
@@ -1830,6 +2381,12 @@ version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
|
||||
|
||||
[[package]]
|
||||
name = "valuable"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
@@ -1973,6 +2530,22 @@ dependencies = [
|
||||
"rustls-pki-types",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.11"
|
||||
@@ -1982,6 +2555,12 @@ dependencies = [
|
||||
"windows-sys 0.61.2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
|
||||
@@ -14,6 +14,7 @@ tokio = { workspace = true }
|
||||
tokio-util = { workspace = true }
|
||||
dirs = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = { workspace = true }
|
||||
|
||||
+411
-68
@@ -2,18 +2,25 @@
|
||||
//! config, store, event bus, permission service, tool registry, provider registry — and
|
||||
//! exposes a small headless API (`run_prompt`) used by the `harness run -p` debug command.
|
||||
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::collections::HashMap;
|
||||
use std::hash::Hasher;
|
||||
use std::path::Path;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use harness_core::config::{self, Config, ConfigError};
|
||||
use harness_core::engine::{run_session, RunConfig, StepContext};
|
||||
use harness_core::event::{EventBus, RunOutcome};
|
||||
use harness_core::permission::{spawn_auto_approve, PermissionService};
|
||||
use harness_core::event::{AppEvent, EventBus, RunOutcome};
|
||||
use harness_core::permission::{PermissionReply, PermissionService};
|
||||
use harness_core::store::{Store, StoreError};
|
||||
use harness_core::tool::ToolRegistry;
|
||||
use harness_core::types::{Message, ModelRef, Part, PartBody, PartId, Session, SessionId};
|
||||
use harness_providers::{AnthropicProvider, ProviderRegistry};
|
||||
use harness_core::types::{
|
||||
Message, MessageId, ModelRef, Part, PartBody, PartId, Session, SessionId,
|
||||
};
|
||||
use harness_providers::{AnthropicProvider, ModelCatalog, OpenAiProvider, ProviderRegistry};
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
/// Placeholder until M4's markdown agent registry lands (`assets/agents/orchestrator.md`).
|
||||
@@ -34,6 +41,8 @@ pub enum AppError {
|
||||
InvalidModelRef(String),
|
||||
#[error("no provider registered for {0:?} (missing API key?)")]
|
||||
UnknownProvider(String),
|
||||
#[error("session {0} already has an active run")]
|
||||
SessionRunning(SessionId),
|
||||
}
|
||||
|
||||
fn now_ms() -> i64 {
|
||||
@@ -43,26 +52,67 @@ fn now_ms() -> i64 {
|
||||
.as_millis() as i64
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
pub config: Config,
|
||||
pub store: Store,
|
||||
pub bus: EventBus,
|
||||
pub permissions: Arc<PermissionService>,
|
||||
pub tools: ToolRegistry,
|
||||
pub providers: ProviderRegistry,
|
||||
pub cwd: PathBuf,
|
||||
data_dir: PathBuf,
|
||||
fn db_path(cwd: &Path) -> PathBuf {
|
||||
let base = dirs::data_dir()
|
||||
.unwrap_or_else(std::env::temp_dir)
|
||||
.join("ai-harness")
|
||||
.join("db");
|
||||
let cwd_str = cwd.to_string_lossy();
|
||||
let sanitized: String = cwd_str
|
||||
.chars()
|
||||
.map(|c| if c.is_alphanumeric() { c } else { '_' })
|
||||
.collect();
|
||||
let mut hasher = DefaultHasher::new();
|
||||
hasher.write(cwd_str.as_bytes());
|
||||
let hash = hasher.finish();
|
||||
let hash_hex = format!("{:016x}", hash);
|
||||
let slug = format!("{}_{}", sanitized, &hash_hex[..8]);
|
||||
base.join(format!("{}.sqlite", slug))
|
||||
}
|
||||
|
||||
impl App {
|
||||
/// In-memory store, auto-approve permission frontend — the M1 headless configuration.
|
||||
/// A persistent SQLite-backed store and a real TUI permission modal land in M2.
|
||||
/// Non-blocking, multi-turn engine API used by the TUI.
|
||||
#[derive(Clone)]
|
||||
pub struct EngineHandle {
|
||||
inner: Arc<EngineInner>,
|
||||
}
|
||||
|
||||
struct EngineInner {
|
||||
config: Config,
|
||||
store: Store,
|
||||
bus: EventBus,
|
||||
permissions: Arc<PermissionService>,
|
||||
tools: ToolRegistry,
|
||||
providers: ProviderRegistry,
|
||||
catalog: ModelCatalog,
|
||||
cwd: PathBuf,
|
||||
data_dir: PathBuf,
|
||||
runs: Mutex<HashMap<SessionId, RunHandle>>,
|
||||
}
|
||||
|
||||
struct RunHandle {
|
||||
cancel: CancellationToken,
|
||||
}
|
||||
|
||||
impl EngineHandle {
|
||||
/// Persistent SQLite-backed store. Used by the TUI and the default headless `App`.
|
||||
pub fn init(cwd: PathBuf) -> Result<Self, AppError> {
|
||||
let path = db_path(&cwd);
|
||||
let store = Store::open(&path)?;
|
||||
let handle = Self::new(cwd, store)?;
|
||||
handle.spawn_catalog_refresh();
|
||||
Ok(handle)
|
||||
}
|
||||
|
||||
/// In-memory store — useful for tests and ephemeral sessions.
|
||||
pub fn init_in_memory(cwd: PathBuf) -> Result<Self, AppError> {
|
||||
let store = Store::open_in_memory()?;
|
||||
Self::new(cwd, store)
|
||||
}
|
||||
|
||||
fn new(cwd: PathBuf, store: Store) -> Result<Self, AppError> {
|
||||
let config = config::load(&cwd)?;
|
||||
let bus = EventBus::new();
|
||||
let store = Store::open_in_memory()?;
|
||||
let permissions = Arc::new(PermissionService::new(bus.clone()));
|
||||
spawn_auto_approve(bus.clone(), permissions.clone());
|
||||
|
||||
let mut tools = ToolRegistry::new();
|
||||
harness_tools::register_builtins(&mut tools);
|
||||
@@ -75,71 +125,142 @@ impl App {
|
||||
{
|
||||
providers.register(Arc::new(AnthropicProvider::new(key)));
|
||||
}
|
||||
if let Some(key) = config
|
||||
.providers
|
||||
.get("openai")
|
||||
.and_then(|p| p.api_key.clone())
|
||||
{
|
||||
let provider = match config
|
||||
.providers
|
||||
.get("openai")
|
||||
.and_then(|p| p.base_url.clone())
|
||||
{
|
||||
Some(base_url) => OpenAiProvider::with_base_url(key, base_url),
|
||||
None => OpenAiProvider::new(key),
|
||||
};
|
||||
providers.register(Arc::new(provider));
|
||||
}
|
||||
|
||||
let data_dir = dirs::data_dir()
|
||||
.unwrap_or_else(std::env::temp_dir)
|
||||
.join("ai-harness")
|
||||
.join("tool-output");
|
||||
|
||||
// No network at construction: use the cached copy if fresh, else the baked snapshot.
|
||||
// `init` warms the cache in the background for the next launch.
|
||||
let catalog = ModelCatalog::load_cached_or_baked(&ModelCatalog::default_cache_path());
|
||||
|
||||
Ok(Self {
|
||||
config,
|
||||
store,
|
||||
bus,
|
||||
permissions,
|
||||
tools,
|
||||
providers,
|
||||
cwd,
|
||||
data_dir,
|
||||
inner: Arc::new(EngineInner {
|
||||
config,
|
||||
store,
|
||||
bus,
|
||||
permissions,
|
||||
tools,
|
||||
providers,
|
||||
catalog,
|
||||
cwd,
|
||||
data_dir,
|
||||
runs: Mutex::new(HashMap::new()),
|
||||
}),
|
||||
})
|
||||
}
|
||||
|
||||
/// Runs a single headless turn: creates a root session, appends `prompt` as the user
|
||||
/// message, and drives the engine loop to completion. Returns the outcome plus the
|
||||
/// session id so the caller can fetch the transcript via `final_text`.
|
||||
pub async fn run_prompt(
|
||||
/// Fire-and-forget refresh of the models.dev cache so the next launch has current pricing.
|
||||
fn spawn_catalog_refresh(&self) {
|
||||
tokio::spawn(async {
|
||||
if let Err(e) = ModelCatalog::refresh_default_cache().await {
|
||||
tracing::debug!(error = %e, "models.dev refresh failed; using cached/baked metadata");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn bus(&self) -> EventBus {
|
||||
self.inner.bus.clone()
|
||||
}
|
||||
|
||||
pub fn permissions(&self) -> Arc<PermissionService> {
|
||||
self.inner.permissions.clone()
|
||||
}
|
||||
|
||||
pub fn config(&self) -> Config {
|
||||
self.inner.config.clone()
|
||||
}
|
||||
|
||||
pub async fn new_session(&self, agent: &str, model_ref: &str) -> Result<SessionId, AppError> {
|
||||
let (provider_id, model_id) = model_ref
|
||||
.split_once('/')
|
||||
.ok_or_else(|| AppError::InvalidModelRef(model_ref.to_string()))?;
|
||||
let model = ModelRef::new(provider_id, model_id);
|
||||
let now = now_ms();
|
||||
let session = Session::new_root(agent, model, now);
|
||||
let session_id = session.id.clone();
|
||||
self.inner.store.upsert_session(session.clone()).await?;
|
||||
self.inner.bus.publish(AppEvent::SessionCreated { session });
|
||||
Ok(session_id)
|
||||
}
|
||||
|
||||
pub async fn prompt(
|
||||
&self,
|
||||
prompt: String,
|
||||
session_id: SessionId,
|
||||
text: String,
|
||||
model_ref: &str,
|
||||
) -> Result<(RunOutcome, SessionId), AppError> {
|
||||
) -> Result<(), AppError> {
|
||||
let (provider_id, model_id) = model_ref
|
||||
.split_once('/')
|
||||
.ok_or_else(|| AppError::InvalidModelRef(model_ref.to_string()))?;
|
||||
let provider = self
|
||||
.inner
|
||||
.providers
|
||||
.get(provider_id)
|
||||
.ok_or_else(|| AppError::UnknownProvider(provider_id.to_string()))?;
|
||||
let model = ModelRef::new(provider_id, model_id);
|
||||
|
||||
let now = now_ms();
|
||||
let session = Session::new_root("orchestrator", model.clone(), now);
|
||||
let session_id = session.id.clone();
|
||||
self.store.upsert_session(session).await?;
|
||||
{
|
||||
let runs = self.inner.runs.lock().unwrap();
|
||||
if runs.contains_key(&session_id) {
|
||||
return Err(AppError::SessionRunning(session_id));
|
||||
}
|
||||
}
|
||||
|
||||
let now = now_ms();
|
||||
let user_message = Message::new_user(session_id.clone(), now);
|
||||
self.store.upsert_message(user_message.clone()).await?;
|
||||
self.store
|
||||
.upsert_part(Part {
|
||||
id: PartId::new(),
|
||||
message_id: user_message.id,
|
||||
session_id: session_id.clone(),
|
||||
idx: 0,
|
||||
body: PartBody::Text {
|
||||
text: prompt,
|
||||
synthetic: false,
|
||||
},
|
||||
})
|
||||
self.inner
|
||||
.store
|
||||
.upsert_message(user_message.clone())
|
||||
.await?;
|
||||
// The store actor does not emit events, so publish the user message and its part
|
||||
// ourselves — otherwise the TUI (which builds its live transcript purely from bus
|
||||
// events) never shows the prompt the user just typed until the session is reloaded.
|
||||
self.inner.bus.publish(AppEvent::MessageCreated {
|
||||
message: user_message.clone(),
|
||||
});
|
||||
|
||||
let user_part = Part {
|
||||
id: PartId::new(),
|
||||
message_id: user_message.id.clone(),
|
||||
session_id: session_id.clone(),
|
||||
idx: 0,
|
||||
body: PartBody::Text {
|
||||
text,
|
||||
synthetic: false,
|
||||
},
|
||||
};
|
||||
self.inner.store.upsert_part(user_part.clone()).await?;
|
||||
self.inner
|
||||
.bus
|
||||
.publish(AppEvent::PartUpdated { part: user_part });
|
||||
|
||||
let ctx = StepContext {
|
||||
store: self.store.clone(),
|
||||
bus: self.bus.clone(),
|
||||
tools: self.tools.clone(),
|
||||
permissions: self.permissions.clone(),
|
||||
static_rules: self.config.permissions.clone(),
|
||||
store: self.inner.store.clone(),
|
||||
bus: self.inner.bus.clone(),
|
||||
tools: self.inner.tools.clone(),
|
||||
permissions: self.inner.permissions.clone(),
|
||||
static_rules: self.inner.config.permissions.clone(),
|
||||
extra_rules: Arc::new(Mutex::new(Vec::new())),
|
||||
session_id: session_id.clone(),
|
||||
cwd: self.cwd.clone(),
|
||||
data_dir: self.data_dir.join(session_id.to_string()),
|
||||
cwd: self.inner.cwd.clone(),
|
||||
data_dir: self.inner.data_dir.join(session_id.to_string()),
|
||||
cancel: CancellationToken::new(),
|
||||
now,
|
||||
};
|
||||
@@ -149,21 +270,80 @@ impl App {
|
||||
model,
|
||||
temperature: None,
|
||||
max_steps: DEFAULT_MAX_STEPS,
|
||||
instructions: self.config.instructions.clone(),
|
||||
instructions: self.inner.config.instructions.clone(),
|
||||
cost: Some(self.inner.catalog.cost(provider_id, model_id)),
|
||||
};
|
||||
|
||||
let outcome = run_session(provider, ctx, &run_config, now_ms).await;
|
||||
Ok((outcome, session_id))
|
||||
// Reserve the run slot *before* spawning. If we inserted after spawning, a run that
|
||||
// finished quickly could remove its (not-yet-inserted) entry first, and our later
|
||||
// insert would then strand the session as permanently "running".
|
||||
let cancel = ctx.cancel.clone();
|
||||
{
|
||||
let mut runs = self.inner.runs.lock().unwrap();
|
||||
if runs.contains_key(&session_id) {
|
||||
return Err(AppError::SessionRunning(session_id));
|
||||
}
|
||||
runs.insert(
|
||||
session_id.clone(),
|
||||
RunHandle {
|
||||
cancel: cancel.clone(),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
self.inner.bus.publish(AppEvent::RunStarted {
|
||||
session_id: session_id.clone(),
|
||||
});
|
||||
|
||||
let inner = self.inner.clone();
|
||||
let spawn_session_id = session_id.clone();
|
||||
tokio::spawn(async move {
|
||||
let outcome = run_session(provider, ctx, &run_config, now_ms).await;
|
||||
inner.bus.publish(AppEvent::RunFinished {
|
||||
session_id: spawn_session_id.clone(),
|
||||
outcome: outcome.clone(),
|
||||
});
|
||||
let mut runs = inner.runs.lock().unwrap();
|
||||
runs.remove(&spawn_session_id);
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn abort(&self, session_id: &SessionId) {
|
||||
let runs = self.inner.runs.lock().unwrap();
|
||||
if let Some(run) = runs.get(session_id) {
|
||||
run.cancel.cancel();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn permission_reply(&self, id: &str, reply: PermissionReply) -> bool {
|
||||
self.inner.permissions.reply(id, reply)
|
||||
}
|
||||
|
||||
pub async fn list_sessions(&self) -> Result<Vec<Session>, AppError> {
|
||||
Ok(self.inner.store.sessions().await?)
|
||||
}
|
||||
|
||||
pub async fn session_messages(&self, session_id: SessionId) -> Result<Vec<Message>, AppError> {
|
||||
Ok(self.inner.store.messages(session_id).await?)
|
||||
}
|
||||
|
||||
pub async fn message_parts(&self, message_id: MessageId) -> Result<Vec<Part>, AppError> {
|
||||
Ok(self.inner.store.parts(message_id).await?)
|
||||
}
|
||||
|
||||
pub fn is_running(&self, session_id: &SessionId) -> bool {
|
||||
let runs = self.inner.runs.lock().unwrap();
|
||||
runs.contains_key(session_id)
|
||||
}
|
||||
|
||||
/// Concatenates the `Text` parts of the last message in the session — the final
|
||||
/// assistant reply for a `harness run` invocation to print.
|
||||
pub async fn final_text(&self, session_id: &SessionId) -> Result<String, AppError> {
|
||||
let messages = self.store.messages(session_id.clone()).await?;
|
||||
let messages = self.inner.store.messages(session_id.clone()).await?;
|
||||
let Some(last) = messages.last() else {
|
||||
return Ok(String::new());
|
||||
};
|
||||
let parts = self.store.parts(last.id.clone()).await?;
|
||||
let parts = self.inner.store.parts(last.id.clone()).await?;
|
||||
let text = parts
|
||||
.iter()
|
||||
.filter_map(|p| match &p.body {
|
||||
@@ -176,6 +356,94 @@ impl App {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
engine: EngineHandle,
|
||||
// Keeps the auto-approve task alive for the lifetime of the App.
|
||||
_auto_approve_handle: Option<JoinHandle<()>>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
/// Persistent SQLite-backed store with an auto-approve permission frontend — the default
|
||||
/// headless configuration used by `harness run -p`.
|
||||
pub fn init(cwd: PathBuf) -> Result<Self, AppError> {
|
||||
let engine = EngineHandle::init(cwd)?;
|
||||
let _auto_approve_handle = Some(spawn_auto_approve_task(&engine));
|
||||
Ok(Self {
|
||||
engine,
|
||||
_auto_approve_handle,
|
||||
})
|
||||
}
|
||||
|
||||
/// In-memory store with an auto-approve permission frontend — useful for tests.
|
||||
pub fn init_in_memory(cwd: PathBuf) -> Result<Self, AppError> {
|
||||
let engine = EngineHandle::init_in_memory(cwd)?;
|
||||
let _auto_approve_handle = Some(spawn_auto_approve_task(&engine));
|
||||
Ok(Self {
|
||||
engine,
|
||||
_auto_approve_handle,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn engine(&self) -> &EngineHandle {
|
||||
&self.engine
|
||||
}
|
||||
|
||||
pub fn config(&self) -> Config {
|
||||
self.engine.config()
|
||||
}
|
||||
|
||||
/// Runs a single headless turn: creates a root session, appends `prompt` as the user
|
||||
/// message, and drives the engine loop to completion. Returns the outcome plus the
|
||||
/// session id so the caller can fetch the transcript via `final_text`.
|
||||
pub async fn run_prompt(
|
||||
&self,
|
||||
prompt: String,
|
||||
model_ref: &str,
|
||||
) -> Result<(RunOutcome, SessionId), AppError> {
|
||||
let session_id = self.engine.new_session("orchestrator", model_ref).await?;
|
||||
let mut rx = self.engine.bus().subscribe();
|
||||
// Subscribe before starting the run so we cannot miss the RunFinished event
|
||||
// even for an instant (mock) provider.
|
||||
self.engine
|
||||
.prompt(session_id.clone(), prompt, model_ref)
|
||||
.await?;
|
||||
|
||||
while let Ok(event) = rx.recv().await {
|
||||
if let AppEvent::RunFinished {
|
||||
session_id: sid,
|
||||
outcome,
|
||||
} = event
|
||||
{
|
||||
if sid == session_id {
|
||||
return Ok((outcome, session_id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Bus closed unexpectedly; report a stopped run so the caller can continue.
|
||||
Ok((RunOutcome::Stopped, session_id))
|
||||
}
|
||||
|
||||
/// Concatenates the `Text` parts of the last message in the session — the final
|
||||
/// assistant reply for a `harness run` invocation to print.
|
||||
pub async fn final_text(&self, session_id: &SessionId) -> Result<String, AppError> {
|
||||
self.engine.final_text(session_id).await
|
||||
}
|
||||
}
|
||||
|
||||
fn spawn_auto_approve_task(engine: &EngineHandle) -> JoinHandle<()> {
|
||||
let bus = engine.bus();
|
||||
let permissions = engine.permissions();
|
||||
tokio::spawn(async move {
|
||||
let mut rx = bus.subscribe();
|
||||
while let Ok(event) = rx.recv().await {
|
||||
if let AppEvent::PermissionAsked { request } = event {
|
||||
permissions.reply(&request.id, PermissionReply::Once);
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -184,16 +452,16 @@ mod tests {
|
||||
async fn init_loads_defaults_with_no_providers_configured() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
std::env::remove_var("ANTHROPIC_API_KEY");
|
||||
let app = App::init(dir.path().to_path_buf()).unwrap();
|
||||
assert!(app.providers.get("anthropic").is_none());
|
||||
assert_eq!(app.tools.all().len(), 6);
|
||||
let app = App::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
assert!(app.engine.inner.providers.get("anthropic").is_none());
|
||||
assert_eq!(app.engine.inner.tools.all().len(), 6);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn run_prompt_errors_on_unregistered_provider() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
std::env::remove_var("ANTHROPIC_API_KEY");
|
||||
let app = App::init(dir.path().to_path_buf()).unwrap();
|
||||
let app = App::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
let err = app
|
||||
.run_prompt("hi".into(), "anthropic/claude-sonnet-4-5")
|
||||
.await
|
||||
@@ -204,7 +472,7 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn run_prompt_errors_on_malformed_model_ref() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let app = App::init(dir.path().to_path_buf()).unwrap();
|
||||
let app = App::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
let err = app.run_prompt("hi".into(), "no-slash").await.unwrap_err();
|
||||
assert!(matches!(err, AppError::InvalidModelRef(_)));
|
||||
}
|
||||
@@ -212,8 +480,83 @@ mod tests {
|
||||
#[tokio::test]
|
||||
async fn final_text_is_empty_for_unknown_session() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let app = App::init(dir.path().to_path_buf()).unwrap();
|
||||
let app = App::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
let text = app.final_text(&SessionId::new()).await.unwrap();
|
||||
assert_eq!(text, "");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn engine_init_in_memory_creates_handle() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let engine = EngineHandle::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
assert!(!engine.config().providers.contains_key("anthropic"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn engine_new_session_persists_session() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let engine = EngineHandle::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
let id = engine
|
||||
.new_session("orchestrator", "anthropic/claude")
|
||||
.await
|
||||
.unwrap();
|
||||
let sessions = engine.list_sessions().await.unwrap();
|
||||
assert_eq!(sessions.len(), 1);
|
||||
assert_eq!(sessions[0].id, id);
|
||||
assert_eq!(sessions[0].agent, "orchestrator");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn engine_list_sessions_returns_persisted_sessions() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let engine = EngineHandle::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
let id1 = engine.new_session("a", "anthropic/claude").await.unwrap();
|
||||
let id2 = engine.new_session("b", "anthropic/claude").await.unwrap();
|
||||
let sessions = engine.list_sessions().await.unwrap();
|
||||
assert_eq!(sessions.len(), 2);
|
||||
assert!(sessions.iter().any(|s| s.id == id1));
|
||||
assert!(sessions.iter().any(|s| s.id == id2));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn engine_prompt_errors_on_unregistered_provider() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
std::env::remove_var("ANTHROPIC_API_KEY");
|
||||
let engine = EngineHandle::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
let id = engine
|
||||
.new_session("orchestrator", "anthropic/claude")
|
||||
.await
|
||||
.unwrap();
|
||||
let err = engine
|
||||
.prompt(id, "hi".into(), "anthropic/claude")
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, AppError::UnknownProvider(_)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn engine_prompt_errors_on_malformed_model_ref() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let engine = EngineHandle::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
let id = engine
|
||||
.new_session("orchestrator", "anthropic/claude")
|
||||
.await
|
||||
.unwrap();
|
||||
let err = engine
|
||||
.prompt(id, "hi".into(), "no-slash")
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(matches!(err, AppError::InvalidModelRef(_)));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn engine_is_running_false_for_inactive_session() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let engine = EngineHandle::init_in_memory(dir.path().to_path_buf()).unwrap();
|
||||
let id = engine
|
||||
.new_session("orchestrator", "anthropic/claude")
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(!engine.is_running(&id));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,33 @@ pub struct RunConfig {
|
||||
pub temperature: Option<f32>,
|
||||
pub max_steps: u32,
|
||||
pub instructions: Vec<String>,
|
||||
/// Pricing for `model`, from models.dev metadata. `None` leaves cost at 0.
|
||||
pub cost: Option<crate::types::ModelCost>,
|
||||
}
|
||||
|
||||
/// Adds a step's usage/cost onto the persisted session and republishes it. Cost accounting is
|
||||
/// best-effort: a store error here is logged, not surfaced as a run failure.
|
||||
async fn accumulate_session_usage(
|
||||
ctx: &StepContext,
|
||||
usage: &crate::types::TokenUsage,
|
||||
cost: f64,
|
||||
now: i64,
|
||||
) {
|
||||
match ctx.store.session(ctx.session_id.clone()).await {
|
||||
Ok(Some(mut session)) => {
|
||||
session.usage.add(usage);
|
||||
session.cost += cost;
|
||||
session.updated_at = now;
|
||||
if let Err(e) = ctx.store.upsert_session(session.clone()).await {
|
||||
tracing::warn!(error = %e, "failed to persist session usage");
|
||||
return;
|
||||
}
|
||||
ctx.bus
|
||||
.publish(crate::event::AppEvent::SessionUpdated { session });
|
||||
}
|
||||
Ok(None) => {}
|
||||
Err(e) => tracing::warn!(error = %e, "failed to load session for usage accounting"),
|
||||
}
|
||||
}
|
||||
|
||||
/// opencode's exit condition: keep stepping while the last assistant turn asked for more
|
||||
@@ -221,17 +248,24 @@ pub async fn run_session(
|
||||
&ctx,
|
||||
run_config.model.clone(),
|
||||
&run_config.agent_name,
|
||||
run_config.cost,
|
||||
&mut doomloop,
|
||||
)
|
||||
.await;
|
||||
|
||||
match step {
|
||||
Ok(outcome) if outcome.aborted => return RunOutcome::Aborted,
|
||||
Ok(outcome) => match outcome.result {
|
||||
StepResult::Continue => continue,
|
||||
StepResult::Stop => return RunOutcome::Stopped,
|
||||
StepResult::Compact => return RunOutcome::Stopped, // stub until M6
|
||||
},
|
||||
Ok(outcome) if outcome.aborted => {
|
||||
accumulate_session_usage(&ctx, &outcome.usage, outcome.cost, now_fn()).await;
|
||||
return RunOutcome::Aborted;
|
||||
}
|
||||
Ok(outcome) => {
|
||||
accumulate_session_usage(&ctx, &outcome.usage, outcome.cost, now_fn()).await;
|
||||
match outcome.result {
|
||||
StepResult::Continue => continue,
|
||||
StepResult::Stop => return RunOutcome::Stopped,
|
||||
StepResult::Compact => return RunOutcome::Stopped, // stub until M6
|
||||
}
|
||||
}
|
||||
Err(step_err) if matches!(step_err.source, ProviderError::Cancelled) => {
|
||||
return RunOutcome::Aborted;
|
||||
}
|
||||
@@ -422,11 +456,28 @@ mod tests {
|
||||
temperature: None,
|
||||
max_steps: 10,
|
||||
instructions: Vec::new(),
|
||||
// $3/1M input, $15/1M output.
|
||||
cost: Some(crate::types::ModelCost {
|
||||
input: 3.0,
|
||||
output: 15.0,
|
||||
..Default::default()
|
||||
}),
|
||||
};
|
||||
|
||||
let outcome = run_session(Arc::new(provider), ctx, &run_config, || 2).await;
|
||||
assert!(matches!(outcome, RunOutcome::Stopped));
|
||||
|
||||
// Both steps' usage and cost accumulate onto the session.
|
||||
let session = store.session(session_id.clone()).await.unwrap().unwrap();
|
||||
assert_eq!(session.usage.input, 30);
|
||||
assert_eq!(session.usage.output, 13);
|
||||
// (10*3 + 5*15)/1e6 + (20*3 + 8*15)/1e6 = 0.000105 + 0.00018
|
||||
assert!(
|
||||
(session.cost - 0.000_285).abs() < 1e-9,
|
||||
"cost = {}",
|
||||
session.cost
|
||||
);
|
||||
|
||||
let messages = store.messages(session_id.clone()).await.unwrap();
|
||||
assert_eq!(
|
||||
messages.len(),
|
||||
@@ -517,6 +568,7 @@ mod tests {
|
||||
temperature: None,
|
||||
max_steps: 10,
|
||||
instructions: Vec::new(),
|
||||
cost: None,
|
||||
};
|
||||
|
||||
let outcome = run_session(Arc::new(provider), ctx, &run_config, || 2).await;
|
||||
|
||||
@@ -26,6 +26,8 @@ pub struct StepOutcome {
|
||||
pub result: StepResult,
|
||||
pub message_id: Option<MessageId>,
|
||||
pub usage: TokenUsage,
|
||||
/// Dollar cost of this step's usage (0.0 when no pricing is available).
|
||||
pub cost: f64,
|
||||
pub aborted: bool,
|
||||
}
|
||||
|
||||
@@ -513,6 +515,7 @@ impl<'a> Run<'a> {
|
||||
&mut self,
|
||||
reason: FinishReason,
|
||||
usage: TokenUsage,
|
||||
cost: f64,
|
||||
) -> Result<StepResult, ProviderError> {
|
||||
let part = Part {
|
||||
id: PartId::new(),
|
||||
@@ -521,7 +524,7 @@ impl<'a> Run<'a> {
|
||||
idx: self.next_idx,
|
||||
body: PartBody::StepFinish {
|
||||
usage,
|
||||
cost: 0.0,
|
||||
cost,
|
||||
reason: reason.clone(),
|
||||
},
|
||||
};
|
||||
@@ -567,10 +570,12 @@ pub async fn process_step(
|
||||
ctx: &StepContext,
|
||||
model: crate::types::ModelRef,
|
||||
agent: &str,
|
||||
cost: Option<crate::types::ModelCost>,
|
||||
doomloop: &mut DoomLoopGuard,
|
||||
) -> Result<StepOutcome, StepError> {
|
||||
let mut run = Run::new(ctx);
|
||||
let mut usage = TokenUsage::default();
|
||||
let mut step_cost = 0.0;
|
||||
let mut result = StepResult::Stop;
|
||||
|
||||
loop {
|
||||
@@ -582,6 +587,7 @@ pub async fn process_step(
|
||||
result: StepResult::Stop,
|
||||
message_id: run.assistant.as_ref().map(|m| m.id.clone()),
|
||||
usage,
|
||||
cost: step_cost,
|
||||
aborted: true,
|
||||
});
|
||||
}
|
||||
@@ -630,7 +636,8 @@ pub async fn process_step(
|
||||
usage: finish_usage,
|
||||
} => {
|
||||
usage = finish_usage;
|
||||
match run.on_finish(reason, finish_usage).await {
|
||||
step_cost = cost.map(|c| c.cost_of(&finish_usage)).unwrap_or(0.0);
|
||||
match run.on_finish(reason, finish_usage, step_cost).await {
|
||||
Ok(r) => {
|
||||
result = r;
|
||||
Ok(())
|
||||
@@ -652,6 +659,7 @@ pub async fn process_step(
|
||||
result,
|
||||
message_id: run.assistant.as_ref().map(|m| m.id.clone()),
|
||||
usage,
|
||||
cost: step_cost,
|
||||
aborted: false,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ pub enum StoreCmd {
|
||||
UpsertSession(Session, Reply<()>),
|
||||
UpsertMessage(Message, Reply<()>),
|
||||
UpsertPart(Part, Reply<()>),
|
||||
Session(SessionId, Reply<Option<Session>>),
|
||||
Sessions(Reply<Vec<Session>>),
|
||||
Messages(SessionId, Reply<Vec<Message>>),
|
||||
Parts(MessageId, Reply<Vec<Part>>),
|
||||
@@ -69,6 +70,15 @@ fn upsert_part(conn: &Connection, part: &Part) -> Result<(), StoreError> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_session(conn: &Connection, id: &SessionId) -> Result<Option<Session>, StoreError> {
|
||||
let mut stmt = conn.prepare("SELECT data FROM session WHERE id = ?1")?;
|
||||
let mut rows = stmt.query_map(params![id.as_ref()], |row| row.get::<_, String>(0))?;
|
||||
match rows.next() {
|
||||
Some(data) => Ok(Some(serde_json::from_str(&data?)?)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn list_sessions(conn: &Connection) -> Result<Vec<Session>, StoreError> {
|
||||
let mut stmt = conn.prepare("SELECT data FROM session ORDER BY id")?;
|
||||
let rows = stmt
|
||||
@@ -116,6 +126,9 @@ pub fn run(conn: Connection, mut rx: tokio::sync::mpsc::Receiver<StoreCmd>) {
|
||||
StoreCmd::UpsertPart(part, reply) => {
|
||||
let _ = reply.send(upsert_part(&conn, &part));
|
||||
}
|
||||
StoreCmd::Session(id, reply) => {
|
||||
let _ = reply.send(get_session(&conn, &id));
|
||||
}
|
||||
StoreCmd::Sessions(reply) => {
|
||||
let _ = reply.send(list_sessions(&conn));
|
||||
}
|
||||
|
||||
@@ -77,6 +77,11 @@ impl Store {
|
||||
self.call(|reply| StoreCmd::UpsertPart(part, reply)).await
|
||||
}
|
||||
|
||||
pub async fn session(&self, session_id: SessionId) -> Result<Option<Session>, StoreError> {
|
||||
self.call(|reply| StoreCmd::Session(session_id, reply))
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn sessions(&self) -> Result<Vec<Session>, StoreError> {
|
||||
self.call(StoreCmd::Sessions).await
|
||||
}
|
||||
|
||||
@@ -6,6 +6,6 @@ pub mod session;
|
||||
|
||||
pub use ids::{MessageId, PartId, SessionId};
|
||||
pub use message::{Message, MessageError, Role};
|
||||
pub use model::{ModelInfo, ModelRef, TokenUsage};
|
||||
pub use model::{ModelCost, ModelInfo, ModelRef, TokenUsage};
|
||||
pub use part::{Part, PartBody, ToolState};
|
||||
pub use session::Session;
|
||||
|
||||
@@ -34,11 +34,71 @@ impl TokenUsage {
|
||||
}
|
||||
}
|
||||
|
||||
// Full cost/context-limit metadata is populated by harness-providers (models.dev) in M1/M3;
|
||||
// this placeholder only carries what harness-core needs to key on.
|
||||
/// Per-model pricing in USD per **one million** tokens. Populated from models.dev metadata
|
||||
/// by `harness-providers`.
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ModelCost {
|
||||
pub input: f64,
|
||||
pub output: f64,
|
||||
pub cache_read: f64,
|
||||
pub cache_write: f64,
|
||||
}
|
||||
|
||||
impl ModelCost {
|
||||
/// Dollar cost of a usage sample. Reasoning tokens are billed within `output` by the
|
||||
/// providers we support (OpenAI reports them as a subset of `output_tokens`; Anthropic
|
||||
/// counts thinking in output), so they are intentionally not charged separately here.
|
||||
pub fn cost_of(&self, usage: &TokenUsage) -> f64 {
|
||||
let per_million = |tokens: u64, rate: f64| (tokens as f64) * rate / 1_000_000.0;
|
||||
per_million(usage.input, self.input)
|
||||
+ per_million(usage.output, self.output)
|
||||
+ per_million(usage.cache_read, self.cache_read)
|
||||
+ per_million(usage.cache_write, self.cache_write)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cost_of_sums_components_per_million_excluding_reasoning() {
|
||||
let cost = ModelCost {
|
||||
input: 3.0,
|
||||
output: 15.0,
|
||||
cache_read: 0.30,
|
||||
cache_write: 3.75,
|
||||
};
|
||||
let usage = TokenUsage {
|
||||
input: 1_000_000,
|
||||
output: 1_000_000,
|
||||
reasoning: 500_000, // billed within output — must not add extra cost
|
||||
cache_read: 1_000_000,
|
||||
cache_write: 1_000_000,
|
||||
};
|
||||
// 3 + 15 + 0.30 + 3.75, with reasoning contributing nothing.
|
||||
assert!((cost.cost_of(&usage) - 22.05).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_cost_is_zero() {
|
||||
assert_eq!(ModelCost::default().cost_of(&TokenUsage::default()), 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
/// Model metadata, keyed on `model`. Cost/limits are populated from models.dev by
|
||||
/// `harness-providers`; `reasoning`/`tool_call`/`attachment` are capability flags.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct ModelInfo {
|
||||
pub model: ModelRef,
|
||||
pub context_limit: u64,
|
||||
pub output_limit: u64,
|
||||
#[serde(default)]
|
||||
pub cost: ModelCost,
|
||||
#[serde(default)]
|
||||
pub reasoning: bool,
|
||||
#[serde(default)]
|
||||
pub tool_call: bool,
|
||||
#[serde(default)]
|
||||
pub attachment: bool,
|
||||
}
|
||||
|
||||
@@ -18,9 +18,11 @@ serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
dirs = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
tokio = { workspace = true, features = ["test-util", "macros"] }
|
||||
tempfile = { workspace = true }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
{
|
||||
"anthropic": {
|
||||
"id": "anthropic",
|
||||
"name": "Anthropic",
|
||||
"models": {
|
||||
"claude-sonnet-4-5": {
|
||||
"id": "claude-sonnet-4-5",
|
||||
"reasoning": true,
|
||||
"tool_call": true,
|
||||
"attachment": true,
|
||||
"cost": { "input": 3, "output": 15, "cache_read": 0.3, "cache_write": 3.75 },
|
||||
"limit": { "context": 200000, "output": 64000 }
|
||||
},
|
||||
"claude-opus-4-1": {
|
||||
"id": "claude-opus-4-1",
|
||||
"reasoning": true,
|
||||
"tool_call": true,
|
||||
"attachment": true,
|
||||
"cost": { "input": 15, "output": 75, "cache_read": 1.5, "cache_write": 18.75 },
|
||||
"limit": { "context": 200000, "output": 32000 }
|
||||
},
|
||||
"claude-haiku-4-5": {
|
||||
"id": "claude-haiku-4-5",
|
||||
"reasoning": true,
|
||||
"tool_call": true,
|
||||
"attachment": true,
|
||||
"cost": { "input": 1, "output": 5, "cache_read": 0.1, "cache_write": 1.25 },
|
||||
"limit": { "context": 200000, "output": 64000 }
|
||||
}
|
||||
}
|
||||
},
|
||||
"openai": {
|
||||
"id": "openai",
|
||||
"name": "OpenAI",
|
||||
"models": {
|
||||
"gpt-4o": {
|
||||
"id": "gpt-4o",
|
||||
"tool_call": true,
|
||||
"attachment": true,
|
||||
"cost": { "input": 2.5, "output": 10, "cache_read": 1.25 },
|
||||
"limit": { "context": 128000, "output": 16384 }
|
||||
},
|
||||
"gpt-4o-mini": {
|
||||
"id": "gpt-4o-mini",
|
||||
"tool_call": true,
|
||||
"attachment": true,
|
||||
"cost": { "input": 0.15, "output": 0.6, "cache_read": 0.075 },
|
||||
"limit": { "context": 128000, "output": 16384 }
|
||||
},
|
||||
"gpt-5": {
|
||||
"id": "gpt-5",
|
||||
"reasoning": true,
|
||||
"tool_call": true,
|
||||
"attachment": true,
|
||||
"cost": { "input": 1.25, "output": 10, "cache_read": 0.125 },
|
||||
"limit": { "context": 400000, "output": 128000 }
|
||||
},
|
||||
"o3": {
|
||||
"id": "o3",
|
||||
"reasoning": true,
|
||||
"tool_call": true,
|
||||
"cost": { "input": 2, "output": 8, "cache_read": 0.5 },
|
||||
"limit": { "context": 200000, "output": 100000 }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
//! Credential storage for providers that authenticate outside the config file.
|
||||
//!
|
||||
//! A single JSON file (`~/.local/share/ai-harness/auth.json`, mode `0600`) keyed by provider
|
||||
//! id. OAuth providers (Copilot) store the token triple and refresh it in place; API-key
|
||||
//! providers store the bare key. Config-supplied keys take precedence over this file — this
|
||||
//! is only for interactively-obtained credentials.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "snake_case")]
|
||||
pub enum AuthRecord {
|
||||
/// OAuth credentials. `expires` is a unix-ms timestamp; `0` means the token never expires
|
||||
/// (opencode's direct-Bearer Copilot mode).
|
||||
OAuth {
|
||||
access: String,
|
||||
refresh: String,
|
||||
expires: i64,
|
||||
},
|
||||
Api {
|
||||
key: String,
|
||||
},
|
||||
}
|
||||
|
||||
impl AuthRecord {
|
||||
/// True when an OAuth token is at or past `expires` (with a safety skew), given `now_ms`.
|
||||
/// API keys and never-expiring OAuth tokens (`expires == 0`) are never considered expired.
|
||||
pub fn is_expired(&self, now_ms: i64, skew_ms: i64) -> bool {
|
||||
match self {
|
||||
AuthRecord::OAuth { expires, .. } if *expires > 0 => now_ms + skew_ms >= *expires,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Read/modify/write access to the auth file. Cheap to construct; every operation reloads so
|
||||
/// concurrent writers (a refresh in one provider, a login in another) don't clobber each other.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AuthStorage {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
impl AuthStorage {
|
||||
pub fn new(path: PathBuf) -> Self {
|
||||
Self { path }
|
||||
}
|
||||
|
||||
/// `~/.local/share/ai-harness/auth.json` (falling back to the temp dir if there is no
|
||||
/// data directory).
|
||||
pub fn default_path() -> PathBuf {
|
||||
dirs::data_dir()
|
||||
.unwrap_or_else(std::env::temp_dir)
|
||||
.join("ai-harness")
|
||||
.join("auth.json")
|
||||
}
|
||||
|
||||
pub fn with_default_path() -> Self {
|
||||
Self::new(Self::default_path())
|
||||
}
|
||||
|
||||
/// Loads all records. A missing file yields an empty map; a corrupt file is an error.
|
||||
pub fn load(&self) -> io::Result<HashMap<String, AuthRecord>> {
|
||||
match std::fs::read(&self.path) {
|
||||
Ok(bytes) => serde_json::from_slice(&bytes)
|
||||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e)),
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(HashMap::new()),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, provider: &str) -> io::Result<Option<AuthRecord>> {
|
||||
Ok(self.load()?.remove(provider))
|
||||
}
|
||||
|
||||
pub fn set(&self, provider: &str, record: AuthRecord) -> io::Result<()> {
|
||||
let mut records = self.load()?;
|
||||
records.insert(provider.to_string(), record);
|
||||
self.write(&records)
|
||||
}
|
||||
|
||||
pub fn remove(&self, provider: &str) -> io::Result<()> {
|
||||
let mut records = self.load()?;
|
||||
if records.remove(provider).is_some() {
|
||||
self.write(&records)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Serializes `records` and writes them with `0600` permissions via a temp-file rename so
|
||||
/// a partial write can never leave a truncated auth file behind.
|
||||
fn write(&self, records: &HashMap<String, AuthRecord>) -> io::Result<()> {
|
||||
if let Some(parent) = self.path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
let json = serde_json::to_vec_pretty(records)
|
||||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
|
||||
|
||||
let tmp = self.path.with_extension("json.tmp");
|
||||
std::fs::write(&tmp, &json)?;
|
||||
set_owner_only(&tmp)?;
|
||||
std::fs::rename(&tmp, &self.path)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn set_owner_only(path: &Path) -> io::Result<()> {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn set_owner_only(_path: &Path) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn storage() -> (tempfile::TempDir, AuthStorage) {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("nested").join("auth.json");
|
||||
(dir, AuthStorage::new(path))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_file_loads_empty() {
|
||||
let (_dir, store) = storage();
|
||||
assert!(store.load().unwrap().is_empty());
|
||||
assert_eq!(store.get("anthropic").unwrap(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_get_roundtrip_and_overwrite() {
|
||||
let (_dir, store) = storage();
|
||||
store
|
||||
.set("anthropic", AuthRecord::Api { key: "k1".into() })
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
store.get("anthropic").unwrap(),
|
||||
Some(AuthRecord::Api { key: "k1".into() })
|
||||
);
|
||||
|
||||
store
|
||||
.set("anthropic", AuthRecord::Api { key: "k2".into() })
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
store.get("anthropic").unwrap(),
|
||||
Some(AuthRecord::Api { key: "k2".into() })
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_providers_coexist() {
|
||||
let (_dir, store) = storage();
|
||||
store
|
||||
.set("openai", AuthRecord::Api { key: "sk".into() })
|
||||
.unwrap();
|
||||
store
|
||||
.set(
|
||||
"github-copilot",
|
||||
AuthRecord::OAuth {
|
||||
access: "a".into(),
|
||||
refresh: "r".into(),
|
||||
expires: 0,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
let all = store.load().unwrap();
|
||||
assert_eq!(all.len(), 2);
|
||||
assert!(all.contains_key("openai"));
|
||||
assert!(all.contains_key("github-copilot"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_deletes_only_named_provider() {
|
||||
let (_dir, store) = storage();
|
||||
store
|
||||
.set("openai", AuthRecord::Api { key: "sk".into() })
|
||||
.unwrap();
|
||||
store
|
||||
.set("anthropic", AuthRecord::Api { key: "an".into() })
|
||||
.unwrap();
|
||||
store.remove("openai").unwrap();
|
||||
assert_eq!(store.get("openai").unwrap(), None);
|
||||
assert!(store.get("anthropic").unwrap().is_some());
|
||||
// Removing an absent provider is a no-op, not an error.
|
||||
store.remove("openai").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oauth_expiry_respects_skew_and_never_expires() {
|
||||
let never = AuthRecord::OAuth {
|
||||
access: "a".into(),
|
||||
refresh: "r".into(),
|
||||
expires: 0,
|
||||
};
|
||||
assert!(!never.is_expired(i64::MAX, 0));
|
||||
|
||||
let expiring = AuthRecord::OAuth {
|
||||
access: "a".into(),
|
||||
refresh: "r".into(),
|
||||
expires: 1_000,
|
||||
};
|
||||
assert!(!expiring.is_expired(800, 120));
|
||||
assert!(expiring.is_expired(880, 120)); // within skew window
|
||||
assert!(expiring.is_expired(1_000, 0));
|
||||
|
||||
assert!(!AuthRecord::Api { key: "k".into() }.is_expired(i64::MAX, 0));
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn file_is_written_owner_only() {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let (_dir, store) = storage();
|
||||
store
|
||||
.set("openai", AuthRecord::Api { key: "sk".into() })
|
||||
.unwrap();
|
||||
let mode = std::fs::metadata(&store.path).unwrap().permissions().mode();
|
||||
assert_eq!(mode & 0o777, 0o600);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
pub mod anthropic;
|
||||
pub mod openai_chat;
|
||||
pub mod openai_responses;
|
||||
|
||||
@@ -0,0 +1,502 @@
|
||||
//! Request builder + SSE decoder for OpenAI's `/chat/completions` streaming API.
|
||||
//!
|
||||
//! Also used as the fallback codec for OpenAI-compatible endpoints (including Copilot models
|
||||
//! whose `supported_endpoints` list `/chat/completions`).
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use async_stream::try_stream;
|
||||
use eventsource_stream::Eventsource;
|
||||
use futures::Stream;
|
||||
use harness_core::llm::{
|
||||
FinishReason, LlmEvent, LlmEventStream, LlmRequest, ProviderError, ReasoningEffort, Role,
|
||||
WireContent,
|
||||
};
|
||||
use harness_core::types::TokenUsage;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
fn effort_str(effort: ReasoningEffort) -> &'static str {
|
||||
match effort {
|
||||
ReasoningEffort::Low => "low",
|
||||
ReasoningEffort::Medium => "medium",
|
||||
ReasoningEffort::High => "high",
|
||||
}
|
||||
}
|
||||
|
||||
fn role_str(role: Role) -> &'static str {
|
||||
match role {
|
||||
Role::System => "system",
|
||||
Role::User => "user",
|
||||
Role::Assistant => "assistant",
|
||||
Role::Tool => "tool",
|
||||
}
|
||||
}
|
||||
|
||||
/// Flattens our grouped `WireMessage`s into the flat chat-completions message list. Tool
|
||||
/// results become their own `role: "tool"` messages (one per result), which is what the API
|
||||
/// expects regardless of how the engine grouped them.
|
||||
fn build_messages(system: &[String], messages: &[harness_core::llm::WireMessage]) -> Vec<Value> {
|
||||
let mut out: Vec<Value> = Vec::new();
|
||||
|
||||
if !system.is_empty() {
|
||||
out.push(json!({"role": "system", "content": system.join("\n\n")}));
|
||||
}
|
||||
|
||||
for m in messages {
|
||||
// Tool results are always emitted as standalone `tool` messages.
|
||||
for c in &m.content {
|
||||
if let WireContent::ToolResult {
|
||||
call_id, output, ..
|
||||
} = c
|
||||
{
|
||||
out.push(json!({
|
||||
"role": "tool",
|
||||
"tool_call_id": call_id,
|
||||
"content": output,
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
let mut text = String::new();
|
||||
let mut images: Vec<Value> = Vec::new();
|
||||
let mut tool_calls: Vec<Value> = Vec::new();
|
||||
for c in &m.content {
|
||||
match c {
|
||||
WireContent::Text { text: t } => {
|
||||
if !text.is_empty() {
|
||||
text.push('\n');
|
||||
}
|
||||
text.push_str(t);
|
||||
}
|
||||
WireContent::ToolCall {
|
||||
call_id,
|
||||
name,
|
||||
input,
|
||||
} => tool_calls.push(json!({
|
||||
"id": call_id,
|
||||
"type": "function",
|
||||
"function": {"name": name, "arguments": input.to_string()},
|
||||
})),
|
||||
WireContent::Image { mime_type, data } => images.push(json!({
|
||||
"type": "image_url",
|
||||
"image_url": {"url": format!("data:{mime_type};base64,{data}")},
|
||||
})),
|
||||
WireContent::ToolResult { .. } => {} // handled above
|
||||
}
|
||||
}
|
||||
|
||||
// A message that carried nothing but tool results contributes no further entry.
|
||||
if text.is_empty() && images.is_empty() && tool_calls.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut msg = json!({"role": role_str(m.role)});
|
||||
if images.is_empty() {
|
||||
msg["content"] = json!(text);
|
||||
} else {
|
||||
let mut parts = vec![json!({"type": "text", "text": text})];
|
||||
parts.extend(images);
|
||||
msg["content"] = json!(parts);
|
||||
}
|
||||
if !tool_calls.is_empty() {
|
||||
msg["tool_calls"] = json!(tool_calls);
|
||||
}
|
||||
out.push(msg);
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
pub fn build_request(req: &LlmRequest) -> Value {
|
||||
let mut body = json!({
|
||||
"model": req.model,
|
||||
"messages": build_messages(&req.system, &req.messages),
|
||||
"stream": true,
|
||||
"stream_options": {"include_usage": true},
|
||||
});
|
||||
|
||||
if !req.tools.is_empty() {
|
||||
body["tools"] = Value::Array(
|
||||
req.tools
|
||||
.iter()
|
||||
.map(|t| {
|
||||
json!({
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": t.name,
|
||||
"description": t.description,
|
||||
"parameters": t.parameters,
|
||||
},
|
||||
})
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
if let Some(temp) = req.temperature {
|
||||
body["temperature"] = json!(temp);
|
||||
}
|
||||
if let Some(max) = req.max_tokens {
|
||||
body["max_completion_tokens"] = json!(max);
|
||||
}
|
||||
if let Some(effort) = req.reasoning.as_ref().and_then(|r| r.effort) {
|
||||
body["reasoning_effort"] = json!(effort_str(effort));
|
||||
}
|
||||
body
|
||||
}
|
||||
|
||||
fn map_finish_reason(reason: &str) -> FinishReason {
|
||||
match reason {
|
||||
"stop" => FinishReason::Stop,
|
||||
"tool_calls" | "function_call" => FinishReason::ToolCalls,
|
||||
"length" => FinishReason::Length,
|
||||
"content_filter" => FinishReason::ContentFilter,
|
||||
other => FinishReason::Unknown(other.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct ToolAccum {
|
||||
call_id: String,
|
||||
name: String,
|
||||
args: String,
|
||||
}
|
||||
|
||||
/// Decodes a chat-completions SSE byte stream into normalized `LlmEvent`s. Tool-call deltas
|
||||
/// are accumulated by their `index` and flushed as `ToolCall`s once the stream ends.
|
||||
pub fn decode<S, E>(byte_stream: S) -> LlmEventStream
|
||||
where
|
||||
S: Stream<Item = Result<bytes::Bytes, E>> + Send + 'static,
|
||||
E: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
let events = byte_stream.eventsource();
|
||||
Box::pin(try_stream! {
|
||||
futures::pin_mut!(events);
|
||||
let mut usage = TokenUsage::default();
|
||||
let mut reason = FinishReason::Stop;
|
||||
let mut text_open = false;
|
||||
let mut reasoning_open = false;
|
||||
let mut tools: HashMap<u64, ToolAccum> = HashMap::new();
|
||||
let mut tool_order: Vec<u64> = Vec::new();
|
||||
|
||||
while let Some(item) = futures::StreamExt::next(&mut events).await {
|
||||
let event = item.map_err(|e| ProviderError::Decode(e.to_string()))?;
|
||||
let data = event.data.trim();
|
||||
if data.is_empty() {
|
||||
continue;
|
||||
}
|
||||
if data == "[DONE]" {
|
||||
break;
|
||||
}
|
||||
let value: Value = serde_json::from_str(data)
|
||||
.map_err(|e| ProviderError::Decode(format!("{e}: {data}")))?;
|
||||
|
||||
if let Some(u) = value.get("usage").filter(|u| u.is_object()) {
|
||||
usage.input = u["prompt_tokens"].as_u64().unwrap_or(usage.input);
|
||||
usage.output = u["completion_tokens"].as_u64().unwrap_or(usage.output);
|
||||
usage.reasoning = u["completion_tokens_details"]["reasoning_tokens"]
|
||||
.as_u64()
|
||||
.unwrap_or(usage.reasoning);
|
||||
usage.cache_read = u["prompt_tokens_details"]["cached_tokens"]
|
||||
.as_u64()
|
||||
.unwrap_or(usage.cache_read);
|
||||
}
|
||||
|
||||
let choice = &value["choices"][0];
|
||||
let delta = &choice["delta"];
|
||||
|
||||
if let Some(rc) = delta["reasoning_content"].as_str().filter(|s| !s.is_empty()) {
|
||||
if !reasoning_open {
|
||||
reasoning_open = true;
|
||||
yield LlmEvent::ReasoningStart { id: "reasoning".into() };
|
||||
}
|
||||
yield LlmEvent::ReasoningDelta { id: "reasoning".into(), text: rc.to_string() };
|
||||
}
|
||||
|
||||
if let Some(text) = delta["content"].as_str().filter(|s| !s.is_empty()) {
|
||||
if reasoning_open {
|
||||
reasoning_open = false;
|
||||
yield LlmEvent::ReasoningEnd { id: "reasoning".into(), signature: None };
|
||||
}
|
||||
if !text_open {
|
||||
text_open = true;
|
||||
yield LlmEvent::TextStart { id: "0".into() };
|
||||
}
|
||||
yield LlmEvent::TextDelta { id: "0".into(), text: text.to_string() };
|
||||
}
|
||||
|
||||
if let Some(calls) = delta["tool_calls"].as_array() {
|
||||
for call in calls {
|
||||
let index = call["index"].as_u64().unwrap_or(0);
|
||||
let entry = tools.entry(index).or_insert_with(|| {
|
||||
tool_order.push(index);
|
||||
ToolAccum::default()
|
||||
});
|
||||
if let Some(id) = call["id"].as_str().filter(|s| !s.is_empty()) {
|
||||
entry.call_id = id.to_string();
|
||||
}
|
||||
if let Some(name) = call["function"]["name"].as_str().filter(|s| !s.is_empty()) {
|
||||
entry.name = name.to_string();
|
||||
yield LlmEvent::ToolInputStart {
|
||||
call_id: entry.call_id.clone(),
|
||||
name: entry.name.clone(),
|
||||
};
|
||||
}
|
||||
if let Some(args) = call["function"]["arguments"].as_str().filter(|s| !s.is_empty()) {
|
||||
entry.args.push_str(args);
|
||||
yield LlmEvent::ToolInputDelta {
|
||||
call_id: entry.call_id.clone(),
|
||||
json: args.to_string(),
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(fr) = choice["finish_reason"].as_str() {
|
||||
reason = map_finish_reason(fr);
|
||||
}
|
||||
}
|
||||
|
||||
if reasoning_open {
|
||||
yield LlmEvent::ReasoningEnd { id: "reasoning".into(), signature: None };
|
||||
}
|
||||
if text_open {
|
||||
yield LlmEvent::TextEnd { id: "0".into() };
|
||||
}
|
||||
for index in tool_order {
|
||||
if let Some(entry) = tools.remove(&index) {
|
||||
let input: Value = if entry.args.trim().is_empty() {
|
||||
json!({})
|
||||
} else {
|
||||
serde_json::from_str(&entry.args).unwrap_or(Value::Null)
|
||||
};
|
||||
yield LlmEvent::ToolCall { call_id: entry.call_id, name: entry.name, input };
|
||||
}
|
||||
}
|
||||
yield LlmEvent::Finish { reason, usage };
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use futures::StreamExt;
|
||||
use harness_core::llm::{Initiator, ReasoningOpts, ToolSchema, WireMessage};
|
||||
|
||||
fn sse_stream(raw: &'static str) -> LlmEventStream {
|
||||
let chunks: Vec<Result<bytes::Bytes, std::io::Error>> =
|
||||
vec![Ok(bytes::Bytes::from_static(raw.as_bytes()))];
|
||||
decode(futures::stream::iter(chunks))
|
||||
}
|
||||
|
||||
fn req(model: &str) -> LlmRequest {
|
||||
LlmRequest {
|
||||
model: model.into(),
|
||||
system: vec![],
|
||||
messages: vec![],
|
||||
tools: vec![],
|
||||
temperature: None,
|
||||
max_tokens: None,
|
||||
reasoning: None,
|
||||
initiator: Initiator::User,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn decodes_text_only_response() {
|
||||
let raw = concat!(
|
||||
"data: {\"choices\":[{\"delta\":{\"content\":\"Hel\"},\"finish_reason\":null}]}\n\n",
|
||||
"data: {\"choices\":[{\"delta\":{\"content\":\"lo\"},\"finish_reason\":null}]}\n\n",
|
||||
"data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n",
|
||||
"data: {\"choices\":[],\"usage\":{\"prompt_tokens\":10,\"completion_tokens\":5}}\n\n",
|
||||
"data: [DONE]\n\n",
|
||||
);
|
||||
let events: Vec<LlmEvent> = sse_stream(raw).map(|e| e.unwrap()).collect().await;
|
||||
assert_eq!(
|
||||
events,
|
||||
vec![
|
||||
LlmEvent::TextStart { id: "0".into() },
|
||||
LlmEvent::TextDelta {
|
||||
id: "0".into(),
|
||||
text: "Hel".into()
|
||||
},
|
||||
LlmEvent::TextDelta {
|
||||
id: "0".into(),
|
||||
text: "lo".into()
|
||||
},
|
||||
LlmEvent::TextEnd { id: "0".into() },
|
||||
LlmEvent::Finish {
|
||||
reason: FinishReason::Stop,
|
||||
usage: TokenUsage {
|
||||
input: 10,
|
||||
output: 5,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn decodes_tool_call_accumulated_by_index() {
|
||||
let raw = concat!(
|
||||
"data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"id\":\"call_1\",\"function\":{\"name\":\"read\",\"arguments\":\"\"}}]}}]}\n\n",
|
||||
"data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\"{\\\"file\\\"\"}}]}}]}\n\n",
|
||||
"data: {\"choices\":[{\"delta\":{\"tool_calls\":[{\"index\":0,\"function\":{\"arguments\":\":\\\"a.txt\\\"}\"}}]}}]}\n\n",
|
||||
"data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"tool_calls\"}],\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":8}}\n\n",
|
||||
"data: [DONE]\n\n",
|
||||
);
|
||||
let events: Vec<LlmEvent> = sse_stream(raw).map(|e| e.unwrap()).collect().await;
|
||||
assert_eq!(
|
||||
events,
|
||||
vec![
|
||||
LlmEvent::ToolInputStart {
|
||||
call_id: "call_1".into(),
|
||||
name: "read".into()
|
||||
},
|
||||
LlmEvent::ToolInputDelta {
|
||||
call_id: "call_1".into(),
|
||||
json: "{\"file\"".into()
|
||||
},
|
||||
LlmEvent::ToolInputDelta {
|
||||
call_id: "call_1".into(),
|
||||
json: ":\"a.txt\"}".into()
|
||||
},
|
||||
LlmEvent::ToolCall {
|
||||
call_id: "call_1".into(),
|
||||
name: "read".into(),
|
||||
input: json!({"file": "a.txt"}),
|
||||
},
|
||||
LlmEvent::Finish {
|
||||
reason: FinishReason::ToolCalls,
|
||||
usage: TokenUsage {
|
||||
input: 1,
|
||||
output: 8,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn decodes_reasoning_content_before_text() {
|
||||
let raw = concat!(
|
||||
"data: {\"choices\":[{\"delta\":{\"reasoning_content\":\"hmm\"}}]}\n\n",
|
||||
"data: {\"choices\":[{\"delta\":{\"content\":\"answer\"}}]}\n\n",
|
||||
"data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n\n",
|
||||
"data: [DONE]\n\n",
|
||||
);
|
||||
let events: Vec<LlmEvent> = sse_stream(raw).map(|e| e.unwrap()).collect().await;
|
||||
assert_eq!(
|
||||
events,
|
||||
vec![
|
||||
LlmEvent::ReasoningStart {
|
||||
id: "reasoning".into()
|
||||
},
|
||||
LlmEvent::ReasoningDelta {
|
||||
id: "reasoning".into(),
|
||||
text: "hmm".into()
|
||||
},
|
||||
LlmEvent::ReasoningEnd {
|
||||
id: "reasoning".into(),
|
||||
signature: None
|
||||
},
|
||||
LlmEvent::TextStart { id: "0".into() },
|
||||
LlmEvent::TextDelta {
|
||||
id: "0".into(),
|
||||
text: "answer".into()
|
||||
},
|
||||
LlmEvent::TextEnd { id: "0".into() },
|
||||
LlmEvent::Finish {
|
||||
reason: FinishReason::Stop,
|
||||
usage: TokenUsage::default()
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_request_wraps_tools_in_function_envelope() {
|
||||
let mut r = req("gpt-4o");
|
||||
r.tools = vec![ToolSchema {
|
||||
name: "read".into(),
|
||||
description: "reads a file".into(),
|
||||
parameters: json!({"type": "object"}),
|
||||
}];
|
||||
let body = build_request(&r);
|
||||
assert_eq!(body["tools"][0]["type"], "function");
|
||||
assert_eq!(body["tools"][0]["function"]["name"], "read");
|
||||
assert_eq!(
|
||||
body["tools"][0]["function"]["parameters"],
|
||||
json!({"type": "object"})
|
||||
);
|
||||
assert_eq!(body["stream_options"]["include_usage"], true);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_request_prepends_system_message() {
|
||||
let mut r = req("gpt-4o");
|
||||
r.system = vec!["env".into(), "agent".into()];
|
||||
r.messages = vec![WireMessage {
|
||||
role: Role::User,
|
||||
content: vec![WireContent::Text { text: "hi".into() }],
|
||||
}];
|
||||
let body = build_request(&r);
|
||||
let msgs = body["messages"].as_array().unwrap();
|
||||
assert_eq!(msgs[0]["role"], "system");
|
||||
assert_eq!(msgs[0]["content"], "env\n\nagent");
|
||||
assert_eq!(msgs[1]["role"], "user");
|
||||
assert_eq!(msgs[1]["content"], "hi");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_request_expands_tool_results_to_tool_messages() {
|
||||
let mut r = req("gpt-4o");
|
||||
r.messages = vec![WireMessage {
|
||||
role: Role::Tool,
|
||||
content: vec![WireContent::ToolResult {
|
||||
call_id: "call_1".into(),
|
||||
output: "contents".into(),
|
||||
is_error: false,
|
||||
}],
|
||||
}];
|
||||
let body = build_request(&r);
|
||||
let msgs = body["messages"].as_array().unwrap();
|
||||
assert_eq!(msgs.len(), 1);
|
||||
assert_eq!(msgs[0]["role"], "tool");
|
||||
assert_eq!(msgs[0]["tool_call_id"], "call_1");
|
||||
assert_eq!(msgs[0]["content"], "contents");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_request_emits_assistant_tool_calls() {
|
||||
let mut r = req("gpt-4o");
|
||||
r.messages = vec![WireMessage {
|
||||
role: Role::Assistant,
|
||||
content: vec![WireContent::ToolCall {
|
||||
call_id: "call_1".into(),
|
||||
name: "read".into(),
|
||||
input: json!({"file": "a.txt"}),
|
||||
}],
|
||||
}];
|
||||
let body = build_request(&r);
|
||||
let msgs = body["messages"].as_array().unwrap();
|
||||
assert_eq!(msgs[0]["tool_calls"][0]["id"], "call_1");
|
||||
assert_eq!(msgs[0]["tool_calls"][0]["function"]["name"], "read");
|
||||
assert_eq!(
|
||||
msgs[0]["tool_calls"][0]["function"]["arguments"],
|
||||
"{\"file\":\"a.txt\"}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_request_sets_reasoning_effort() {
|
||||
let mut r = req("o3");
|
||||
r.reasoning = Some(ReasoningOpts {
|
||||
effort: Some(ReasoningEffort::High),
|
||||
budget_tokens: None,
|
||||
});
|
||||
let body = build_request(&r);
|
||||
assert_eq!(body["reasoning_effort"], "high");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
//! Request builder + SSE decoder for OpenAI's `/responses` streaming API.
|
||||
//!
|
||||
//! The responses API is the preferred surface for `gpt-*` / `o-*` models: it carries
|
||||
//! reasoning items natively and reports reasoning-token usage separately.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use async_stream::try_stream;
|
||||
use eventsource_stream::Eventsource;
|
||||
use futures::Stream;
|
||||
use harness_core::llm::{
|
||||
FinishReason, LlmEvent, LlmEventStream, LlmRequest, ProviderError, ReasoningEffort, Role,
|
||||
WireContent,
|
||||
};
|
||||
use harness_core::types::TokenUsage;
|
||||
use serde_json::{json, Value};
|
||||
|
||||
fn effort_str(effort: ReasoningEffort) -> &'static str {
|
||||
match effort {
|
||||
ReasoningEffort::Low => "low",
|
||||
ReasoningEffort::Medium => "medium",
|
||||
ReasoningEffort::High => "high",
|
||||
}
|
||||
}
|
||||
|
||||
/// Builds the `input` item list. Text/image content become `message` items; tool calls and
|
||||
/// their results become `function_call` / `function_call_output` items (the responses API
|
||||
/// keeps these as top-level items rather than nesting them inside messages).
|
||||
fn build_input(messages: &[harness_core::llm::WireMessage]) -> Vec<Value> {
|
||||
let mut out: Vec<Value> = Vec::new();
|
||||
for m in messages {
|
||||
let (role, text_type) = match m.role {
|
||||
Role::Assistant => ("assistant", "output_text"),
|
||||
_ => ("user", "input_text"),
|
||||
};
|
||||
let mut content_parts: Vec<Value> = Vec::new();
|
||||
for c in &m.content {
|
||||
match c {
|
||||
WireContent::Text { text } => {
|
||||
content_parts.push(json!({"type": text_type, "text": text}));
|
||||
}
|
||||
WireContent::Image { mime_type, data } => {
|
||||
content_parts.push(json!({
|
||||
"type": "input_image",
|
||||
"image_url": format!("data:{mime_type};base64,{data}"),
|
||||
}));
|
||||
}
|
||||
WireContent::ToolCall {
|
||||
call_id,
|
||||
name,
|
||||
input,
|
||||
} => out.push(json!({
|
||||
"type": "function_call",
|
||||
"call_id": call_id,
|
||||
"name": name,
|
||||
"arguments": input.to_string(),
|
||||
})),
|
||||
WireContent::ToolResult {
|
||||
call_id, output, ..
|
||||
} => out.push(json!({
|
||||
"type": "function_call_output",
|
||||
"call_id": call_id,
|
||||
"output": output,
|
||||
})),
|
||||
}
|
||||
}
|
||||
if !content_parts.is_empty() {
|
||||
out.push(json!({"type": "message", "role": role, "content": content_parts}));
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
pub fn build_request(req: &LlmRequest) -> Value {
|
||||
let mut body = json!({
|
||||
"model": req.model,
|
||||
"input": build_input(&req.messages),
|
||||
"stream": true,
|
||||
"store": false,
|
||||
});
|
||||
|
||||
if !req.system.is_empty() {
|
||||
body["instructions"] = json!(req.system.join("\n\n"));
|
||||
}
|
||||
if !req.tools.is_empty() {
|
||||
body["tools"] = Value::Array(
|
||||
req.tools
|
||||
.iter()
|
||||
.map(|t| {
|
||||
json!({
|
||||
"type": "function",
|
||||
"name": t.name,
|
||||
"description": t.description,
|
||||
"parameters": t.parameters,
|
||||
})
|
||||
})
|
||||
.collect(),
|
||||
);
|
||||
}
|
||||
if let Some(temp) = req.temperature {
|
||||
body["temperature"] = json!(temp);
|
||||
}
|
||||
if let Some(max) = req.max_tokens {
|
||||
body["max_output_tokens"] = json!(max);
|
||||
}
|
||||
if let Some(effort) = req.reasoning.as_ref().and_then(|r| r.effort) {
|
||||
body["reasoning"] = json!({"effort": effort_str(effort), "summary": "auto"});
|
||||
}
|
||||
body
|
||||
}
|
||||
|
||||
fn map_status(status: Option<&str>, had_tool_call: bool) -> FinishReason {
|
||||
match status {
|
||||
Some("completed") if had_tool_call => FinishReason::ToolCalls,
|
||||
Some("completed") => FinishReason::Stop,
|
||||
Some("incomplete") => FinishReason::Length,
|
||||
Some(other) => FinishReason::Unknown(other.to_string()),
|
||||
None if had_tool_call => FinishReason::ToolCalls,
|
||||
None => FinishReason::Stop,
|
||||
}
|
||||
}
|
||||
|
||||
/// Tracks which normalized stream an `item_id` belongs to so deltas route correctly.
|
||||
enum ItemKind {
|
||||
Text,
|
||||
Reasoning,
|
||||
FunctionCall { call_id: String, name: String },
|
||||
}
|
||||
|
||||
/// Decodes a `/responses` SSE byte stream into normalized `LlmEvent`s.
|
||||
pub fn decode<S, E>(byte_stream: S) -> LlmEventStream
|
||||
where
|
||||
S: Stream<Item = Result<bytes::Bytes, E>> + Send + 'static,
|
||||
E: std::error::Error + Send + Sync + 'static,
|
||||
{
|
||||
let events = byte_stream.eventsource();
|
||||
Box::pin(try_stream! {
|
||||
futures::pin_mut!(events);
|
||||
let mut items: HashMap<String, ItemKind> = HashMap::new();
|
||||
let mut fn_args: HashMap<String, String> = HashMap::new();
|
||||
let mut usage = TokenUsage::default();
|
||||
let mut had_tool_call = false;
|
||||
|
||||
while let Some(item) = futures::StreamExt::next(&mut events).await {
|
||||
let event = item.map_err(|e| ProviderError::Decode(e.to_string()))?;
|
||||
if event.data.trim().is_empty() {
|
||||
continue;
|
||||
}
|
||||
let value: Value = serde_json::from_str(&event.data)
|
||||
.map_err(|e| ProviderError::Decode(format!("{e}: {}", event.data)))?;
|
||||
let kind = value["type"].as_str().unwrap_or_default();
|
||||
|
||||
match kind {
|
||||
"response.output_item.added" => {
|
||||
let item = &value["item"];
|
||||
let id = item["id"].as_str().unwrap_or_default().to_string();
|
||||
match item["type"].as_str().unwrap_or_default() {
|
||||
"message" => {
|
||||
items.insert(id.clone(), ItemKind::Text);
|
||||
yield LlmEvent::TextStart { id };
|
||||
}
|
||||
"reasoning" => {
|
||||
items.insert(id.clone(), ItemKind::Reasoning);
|
||||
yield LlmEvent::ReasoningStart { id };
|
||||
}
|
||||
"function_call" => {
|
||||
let call_id = item["call_id"].as_str().unwrap_or_default().to_string();
|
||||
let name = item["name"].as_str().unwrap_or_default().to_string();
|
||||
fn_args.insert(id.clone(), String::new());
|
||||
items.insert(id, ItemKind::FunctionCall { call_id: call_id.clone(), name: name.clone() });
|
||||
had_tool_call = true;
|
||||
yield LlmEvent::ToolInputStart { call_id, name };
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
"response.output_text.delta" => {
|
||||
let id = value["item_id"].as_str().unwrap_or_default().to_string();
|
||||
let text = value["delta"].as_str().unwrap_or_default().to_string();
|
||||
yield LlmEvent::TextDelta { id, text };
|
||||
}
|
||||
"response.reasoning_summary_text.delta" => {
|
||||
let id = value["item_id"].as_str().unwrap_or_default().to_string();
|
||||
let text = value["delta"].as_str().unwrap_or_default().to_string();
|
||||
yield LlmEvent::ReasoningDelta { id, text };
|
||||
}
|
||||
"response.function_call_arguments.delta" => {
|
||||
let id = value["item_id"].as_str().unwrap_or_default().to_string();
|
||||
let delta = value["delta"].as_str().unwrap_or_default();
|
||||
if let (Some(buf), Some(ItemKind::FunctionCall { call_id, .. })) =
|
||||
(fn_args.get_mut(&id), items.get(&id))
|
||||
{
|
||||
buf.push_str(delta);
|
||||
yield LlmEvent::ToolInputDelta { call_id: call_id.clone(), json: delta.to_string() };
|
||||
}
|
||||
}
|
||||
"response.output_item.done" => {
|
||||
let id = value["item"]["id"].as_str().unwrap_or_default().to_string();
|
||||
match items.remove(&id) {
|
||||
Some(ItemKind::Text) => yield LlmEvent::TextEnd { id },
|
||||
Some(ItemKind::Reasoning) => yield LlmEvent::ReasoningEnd { id, signature: None },
|
||||
Some(ItemKind::FunctionCall { call_id, name }) => {
|
||||
let raw = fn_args.remove(&id).unwrap_or_default();
|
||||
let input: Value = if raw.trim().is_empty() {
|
||||
json!({})
|
||||
} else {
|
||||
serde_json::from_str(&raw).unwrap_or(Value::Null)
|
||||
};
|
||||
yield LlmEvent::ToolCall { call_id, name, input };
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
"response.completed" | "response.incomplete" => {
|
||||
let response = &value["response"];
|
||||
let u = &response["usage"];
|
||||
usage.input = u["input_tokens"].as_u64().unwrap_or(0);
|
||||
usage.output = u["output_tokens"].as_u64().unwrap_or(0);
|
||||
usage.reasoning = u["output_tokens_details"]["reasoning_tokens"].as_u64().unwrap_or(0);
|
||||
usage.cache_read = u["input_tokens_details"]["cached_tokens"].as_u64().unwrap_or(0);
|
||||
let status = response["status"].as_str();
|
||||
yield LlmEvent::Finish { reason: map_status(status, had_tool_call), usage };
|
||||
}
|
||||
"error" | "response.failed" => {
|
||||
let message = value["response"]["error"]["message"]
|
||||
.as_str()
|
||||
.or_else(|| value["message"].as_str())
|
||||
.unwrap_or("unknown error")
|
||||
.to_string();
|
||||
Err(ProviderError::Http { status: 0, body: message })?;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use futures::StreamExt;
|
||||
use harness_core::llm::{Initiator, ReasoningOpts, ToolSchema, WireMessage};
|
||||
|
||||
fn sse_stream(raw: &'static str) -> LlmEventStream {
|
||||
let chunks: Vec<Result<bytes::Bytes, std::io::Error>> =
|
||||
vec![Ok(bytes::Bytes::from_static(raw.as_bytes()))];
|
||||
decode(futures::stream::iter(chunks))
|
||||
}
|
||||
|
||||
fn req(model: &str) -> LlmRequest {
|
||||
LlmRequest {
|
||||
model: model.into(),
|
||||
system: vec![],
|
||||
messages: vec![],
|
||||
tools: vec![],
|
||||
temperature: None,
|
||||
max_tokens: None,
|
||||
reasoning: None,
|
||||
initiator: Initiator::User,
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn decodes_text_response() {
|
||||
let raw = concat!(
|
||||
"data: {\"type\":\"response.output_item.added\",\"item\":{\"id\":\"msg_1\",\"type\":\"message\"}}\n\n",
|
||||
"data: {\"type\":\"response.output_text.delta\",\"item_id\":\"msg_1\",\"delta\":\"Hello\"}\n\n",
|
||||
"data: {\"type\":\"response.output_item.done\",\"item\":{\"id\":\"msg_1\"}}\n\n",
|
||||
"data: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"usage\":{\"input_tokens\":10,\"output_tokens\":5}}}\n\n",
|
||||
);
|
||||
let events: Vec<LlmEvent> = sse_stream(raw).map(|e| e.unwrap()).collect().await;
|
||||
assert_eq!(
|
||||
events,
|
||||
vec![
|
||||
LlmEvent::TextStart { id: "msg_1".into() },
|
||||
LlmEvent::TextDelta {
|
||||
id: "msg_1".into(),
|
||||
text: "Hello".into()
|
||||
},
|
||||
LlmEvent::TextEnd { id: "msg_1".into() },
|
||||
LlmEvent::Finish {
|
||||
reason: FinishReason::Stop,
|
||||
usage: TokenUsage {
|
||||
input: 10,
|
||||
output: 5,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn decodes_function_call_with_reasoning_tokens() {
|
||||
let raw = concat!(
|
||||
"data: {\"type\":\"response.output_item.added\",\"item\":{\"id\":\"fc_1\",\"type\":\"function_call\",\"call_id\":\"call_1\",\"name\":\"read\"}}\n\n",
|
||||
"data: {\"type\":\"response.function_call_arguments.delta\",\"item_id\":\"fc_1\",\"delta\":\"{\\\"file\\\":\"}\n\n",
|
||||
"data: {\"type\":\"response.function_call_arguments.delta\",\"item_id\":\"fc_1\",\"delta\":\"\\\"a.txt\\\"}\"}\n\n",
|
||||
"data: {\"type\":\"response.output_item.done\",\"item\":{\"id\":\"fc_1\"}}\n\n",
|
||||
"data: {\"type\":\"response.completed\",\"response\":{\"status\":\"completed\",\"usage\":{\"input_tokens\":3,\"output_tokens\":9,\"output_tokens_details\":{\"reasoning_tokens\":4}}}}\n\n",
|
||||
);
|
||||
let events: Vec<LlmEvent> = sse_stream(raw).map(|e| e.unwrap()).collect().await;
|
||||
assert_eq!(
|
||||
events,
|
||||
vec![
|
||||
LlmEvent::ToolInputStart {
|
||||
call_id: "call_1".into(),
|
||||
name: "read".into()
|
||||
},
|
||||
LlmEvent::ToolInputDelta {
|
||||
call_id: "call_1".into(),
|
||||
json: "{\"file\":".into()
|
||||
},
|
||||
LlmEvent::ToolInputDelta {
|
||||
call_id: "call_1".into(),
|
||||
json: "\"a.txt\"}".into()
|
||||
},
|
||||
LlmEvent::ToolCall {
|
||||
call_id: "call_1".into(),
|
||||
name: "read".into(),
|
||||
input: json!({"file": "a.txt"}),
|
||||
},
|
||||
LlmEvent::Finish {
|
||||
reason: FinishReason::ToolCalls,
|
||||
usage: TokenUsage {
|
||||
input: 3,
|
||||
output: 9,
|
||||
reasoning: 4,
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn failed_response_surfaces_as_err() {
|
||||
let raw = "data: {\"type\":\"response.failed\",\"response\":{\"error\":{\"message\":\"boom\"}}}\n\n";
|
||||
let events: Vec<Result<LlmEvent, ProviderError>> = sse_stream(raw).collect().await;
|
||||
assert!(
|
||||
matches!(events.last(), Some(Err(ProviderError::Http { body, .. })) if body == "boom")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_request_puts_system_in_instructions() {
|
||||
let mut r = req("gpt-5");
|
||||
r.system = vec!["env".into(), "agent".into()];
|
||||
let body = build_request(&r);
|
||||
assert_eq!(body["instructions"], "env\n\nagent");
|
||||
assert!(body.get("messages").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_request_flattens_tool_calls_and_results_to_items() {
|
||||
let mut r = req("gpt-5");
|
||||
r.messages = vec![
|
||||
WireMessage {
|
||||
role: Role::Assistant,
|
||||
content: vec![WireContent::ToolCall {
|
||||
call_id: "call_1".into(),
|
||||
name: "read".into(),
|
||||
input: json!({"file": "a.txt"}),
|
||||
}],
|
||||
},
|
||||
WireMessage {
|
||||
role: Role::Tool,
|
||||
content: vec![WireContent::ToolResult {
|
||||
call_id: "call_1".into(),
|
||||
output: "contents".into(),
|
||||
is_error: false,
|
||||
}],
|
||||
},
|
||||
];
|
||||
let body = build_request(&r);
|
||||
let input = body["input"].as_array().unwrap();
|
||||
assert_eq!(input[0]["type"], "function_call");
|
||||
assert_eq!(input[0]["call_id"], "call_1");
|
||||
assert_eq!(input[0]["arguments"], "{\"file\":\"a.txt\"}");
|
||||
assert_eq!(input[1]["type"], "function_call_output");
|
||||
assert_eq!(input[1]["output"], "contents");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_request_uses_flat_function_tool_shape_and_reasoning() {
|
||||
let mut r = req("o3");
|
||||
r.tools = vec![ToolSchema {
|
||||
name: "read".into(),
|
||||
description: "reads a file".into(),
|
||||
parameters: json!({"type": "object"}),
|
||||
}];
|
||||
r.reasoning = Some(ReasoningOpts {
|
||||
effort: Some(ReasoningEffort::Medium),
|
||||
budget_tokens: None,
|
||||
});
|
||||
let body = build_request(&r);
|
||||
assert_eq!(body["tools"][0]["type"], "function");
|
||||
assert_eq!(body["tools"][0]["name"], "read");
|
||||
assert_eq!(body["reasoning"]["effort"], "medium");
|
||||
assert_eq!(body["reasoning"]["summary"], "auto");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
//! GitHub OAuth device flow (RFC 8628) for Copilot login.
|
||||
//!
|
||||
//! ai-harness must register its own GitHub OAuth app and supply its client id (we do not
|
||||
//! hardcode opencode's). The client id is passed in by the caller — see [`request_device_code`].
|
||||
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
const DEVICE_CODE_URL: &str = "https://github.com/login/device/code";
|
||||
const ACCESS_TOKEN_URL: &str = "https://github.com/login/oauth/access_token";
|
||||
const GRANT_TYPE: &str = "urn:ietf:params:oauth:grant-type:device_code";
|
||||
/// Minimum scope needed to call the Copilot token-exchange endpoint.
|
||||
pub const SCOPE: &str = "read:user";
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct DeviceCode {
|
||||
pub device_code: String,
|
||||
pub user_code: String,
|
||||
pub verification_uri: String,
|
||||
/// Seconds between polls; GitHub requires honoring this and any `slow_down` bumps.
|
||||
#[serde(default = "default_interval")]
|
||||
pub interval: u64,
|
||||
#[serde(default)]
|
||||
pub expires_in: u64,
|
||||
}
|
||||
|
||||
fn default_interval() -> u64 {
|
||||
5
|
||||
}
|
||||
|
||||
/// Result of one poll of the access-token endpoint.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum PollOutcome {
|
||||
/// The user hasn't authorized yet; keep polling at the current interval.
|
||||
Pending,
|
||||
/// GitHub asked us to slow down; add 5s to the interval (RFC 8628).
|
||||
SlowDown,
|
||||
/// Authorization complete.
|
||||
Success { access_token: String },
|
||||
/// Terminal failure (expired code, denied, unknown error) with a human-readable reason.
|
||||
Failed(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum DeviceFlowError {
|
||||
#[error("network: {0}")]
|
||||
Network(String),
|
||||
#[error("unexpected response: {0}")]
|
||||
Unexpected(String),
|
||||
}
|
||||
|
||||
/// Pure mapping of an access-token poll response body to a [`PollOutcome`], so the state
|
||||
/// machine is testable without a live GitHub.
|
||||
pub fn parse_poll_response(body: &Value) -> PollOutcome {
|
||||
if let Some(token) = body["access_token"].as_str() {
|
||||
return PollOutcome::Success {
|
||||
access_token: token.to_string(),
|
||||
};
|
||||
}
|
||||
match body["error"].as_str() {
|
||||
Some("authorization_pending") => PollOutcome::Pending,
|
||||
Some("slow_down") => PollOutcome::SlowDown,
|
||||
Some(other) => {
|
||||
let desc = body["error_description"].as_str().unwrap_or(other);
|
||||
PollOutcome::Failed(desc.to_string())
|
||||
}
|
||||
None => PollOutcome::Failed("no access_token and no error in response".to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Applies a `slow_down` to a poll interval per RFC 8628 (+5s).
|
||||
pub fn bump_interval(interval: u64) -> u64 {
|
||||
interval + 5
|
||||
}
|
||||
|
||||
/// Step 1: request a device + user code for `client_id`.
|
||||
pub async fn request_device_code(
|
||||
client: &reqwest::Client,
|
||||
client_id: &str,
|
||||
) -> Result<DeviceCode, DeviceFlowError> {
|
||||
let resp = client
|
||||
.post(DEVICE_CODE_URL)
|
||||
.header("accept", "application/json")
|
||||
.json(&serde_json::json!({"client_id": client_id, "scope": SCOPE}))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| DeviceFlowError::Network(e.to_string()))?;
|
||||
let value: Value = resp
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| DeviceFlowError::Network(e.to_string()))?;
|
||||
serde_json::from_value(value.clone())
|
||||
.map_err(|_| DeviceFlowError::Unexpected(value.to_string()))
|
||||
}
|
||||
|
||||
/// Step 2 (single poll): exchange the device code for an access token, once.
|
||||
pub async fn poll_once(
|
||||
client: &reqwest::Client,
|
||||
client_id: &str,
|
||||
device_code: &str,
|
||||
) -> Result<PollOutcome, DeviceFlowError> {
|
||||
let resp = client
|
||||
.post(ACCESS_TOKEN_URL)
|
||||
.header("accept", "application/json")
|
||||
.json(&serde_json::json!({
|
||||
"client_id": client_id,
|
||||
"device_code": device_code,
|
||||
"grant_type": GRANT_TYPE,
|
||||
}))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| DeviceFlowError::Network(e.to_string()))?;
|
||||
let value: Value = resp
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| DeviceFlowError::Network(e.to_string()))?;
|
||||
Ok(parse_poll_response(&value))
|
||||
}
|
||||
|
||||
/// Step 2 (full loop): polls until success or terminal failure, honoring `interval` and
|
||||
/// `slow_down`. `sleep` is injected so tests can drive it without real time.
|
||||
pub async fn poll_for_token<S, Fut>(
|
||||
client: &reqwest::Client,
|
||||
client_id: &str,
|
||||
device: &DeviceCode,
|
||||
sleep: S,
|
||||
) -> Result<String, DeviceFlowError>
|
||||
where
|
||||
S: Fn(Duration) -> Fut,
|
||||
Fut: std::future::Future<Output = ()>,
|
||||
{
|
||||
let mut interval = device.interval;
|
||||
loop {
|
||||
sleep(Duration::from_secs(interval)).await;
|
||||
match poll_once(client, client_id, &device.device_code).await? {
|
||||
PollOutcome::Pending => {}
|
||||
PollOutcome::SlowDown => interval = bump_interval(interval),
|
||||
PollOutcome::Success { access_token } => return Ok(access_token),
|
||||
PollOutcome::Failed(reason) => return Err(DeviceFlowError::Unexpected(reason)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn parses_success() {
|
||||
let outcome =
|
||||
parse_poll_response(&json!({"access_token": "gho_abc", "token_type": "bearer"}));
|
||||
assert_eq!(
|
||||
outcome,
|
||||
PollOutcome::Success {
|
||||
access_token: "gho_abc".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_pending_and_slow_down() {
|
||||
assert_eq!(
|
||||
parse_poll_response(&json!({"error": "authorization_pending"})),
|
||||
PollOutcome::Pending
|
||||
);
|
||||
assert_eq!(
|
||||
parse_poll_response(&json!({"error": "slow_down", "interval": 10})),
|
||||
PollOutcome::SlowDown
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_terminal_errors_with_description() {
|
||||
match parse_poll_response(
|
||||
&json!({"error": "expired_token", "error_description": "code expired"}),
|
||||
) {
|
||||
PollOutcome::Failed(msg) => assert_eq!(msg, "code expired"),
|
||||
other => panic!("expected Failed, got {other:?}"),
|
||||
}
|
||||
assert!(matches!(
|
||||
parse_poll_response(&json!({"error": "access_denied"})),
|
||||
PollOutcome::Failed(_)
|
||||
));
|
||||
assert!(matches!(
|
||||
parse_poll_response(&json!({})),
|
||||
PollOutcome::Failed(_)
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn slow_down_adds_five_seconds() {
|
||||
assert_eq!(bump_interval(5), 10);
|
||||
assert_eq!(bump_interval(10), 15);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn device_code_defaults_interval() {
|
||||
let dc: DeviceCode = serde_json::from_value(json!({
|
||||
"device_code": "d",
|
||||
"user_code": "WXYZ-1234",
|
||||
"verification_uri": "https://github.com/login/device"
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(dc.interval, 5);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
//! GitHub Copilot: OAuth device-flow login, token exchange, and a provider that multiplexes
|
||||
//! the chat/responses/anthropic codecs behind `api.githubcopilot.com`.
|
||||
|
||||
pub mod device_flow;
|
||||
pub mod provider;
|
||||
pub mod token;
|
||||
|
||||
pub use device_flow::{DeviceCode, PollOutcome};
|
||||
pub use provider::{CopilotCodec, CopilotModel, CopilotProvider};
|
||||
pub use token::{CopilotToken, TokenProvider};
|
||||
@@ -0,0 +1,296 @@
|
||||
//! Copilot provider: multiplexes all three wire codecs behind `api.githubcopilot.com`.
|
||||
//!
|
||||
//! Each model's `supported_endpoints` (from `GET /models`) decides which codec/endpoint to
|
||||
//! use. The token comes from [`super::token::TokenProvider`]. Network paths here are the
|
||||
//! flagged live-verification risk — the routing/header logic is unit-tested; end-to-end
|
||||
//! streaming must be checked against the live API.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use harness_core::llm::{
|
||||
Initiator, LlmEventStream, LlmRequest, Provider, ProviderError, WireContent,
|
||||
};
|
||||
use harness_core::types::ModelInfo;
|
||||
use serde_json::Value;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::codec::{anthropic, openai_chat, openai_responses};
|
||||
|
||||
use super::token::TokenProvider;
|
||||
|
||||
pub const DEFAULT_BASE_URL: &str = "https://api.githubcopilot.com";
|
||||
const API_VERSION: &str = "2026-06-01";
|
||||
const ANTHROPIC_BETA: &str = "interleaved-thinking-2025-05-14";
|
||||
const USER_AGENT: &str = concat!("ai-harness/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
/// Which wire codec a Copilot model speaks.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum CopilotCodec {
|
||||
Chat,
|
||||
Responses,
|
||||
Anthropic,
|
||||
}
|
||||
|
||||
impl CopilotCodec {
|
||||
fn path(self) -> &'static str {
|
||||
match self {
|
||||
CopilotCodec::Chat => "/chat/completions",
|
||||
CopilotCodec::Responses => "/responses",
|
||||
CopilotCodec::Anthropic => "/v1/messages",
|
||||
}
|
||||
}
|
||||
|
||||
fn build_request(self, req: &LlmRequest) -> Value {
|
||||
match self {
|
||||
CopilotCodec::Chat => openai_chat::build_request(req),
|
||||
CopilotCodec::Responses => openai_responses::build_request(req),
|
||||
CopilotCodec::Anthropic => anthropic::build_request(req),
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(
|
||||
self,
|
||||
stream: impl futures::Stream<Item = reqwest::Result<bytes::Bytes>> + Send + 'static,
|
||||
) -> LlmEventStream {
|
||||
match self {
|
||||
CopilotCodec::Chat => openai_chat::decode(stream),
|
||||
CopilotCodec::Responses => openai_responses::decode(stream),
|
||||
CopilotCodec::Anthropic => anthropic::decode(stream),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A single model as advertised by `GET /models`.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CopilotModel {
|
||||
pub id: String,
|
||||
pub codec: CopilotCodec,
|
||||
/// Only `model_picker_enabled` models are offered in the UI picker.
|
||||
pub picker_enabled: bool,
|
||||
}
|
||||
|
||||
/// Picks a codec from a model's `supported_endpoints`, preferring the responses API, then the
|
||||
/// Anthropic messages API, then chat completions.
|
||||
pub fn codec_for_endpoints(endpoints: &[String]) -> CopilotCodec {
|
||||
let has = |needle: &str| endpoints.iter().any(|e| e.contains(needle));
|
||||
if has("/responses") {
|
||||
CopilotCodec::Responses
|
||||
} else if has("/v1/messages") || has("/messages") {
|
||||
CopilotCodec::Anthropic
|
||||
} else {
|
||||
CopilotCodec::Chat
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a Copilot `GET /models` response body.
|
||||
pub fn parse_models(body: &Value) -> Vec<CopilotModel> {
|
||||
body["data"]
|
||||
.as_array()
|
||||
.map(|arr| {
|
||||
arr.iter()
|
||||
.filter_map(|m| {
|
||||
let id = m["id"].as_str()?.to_string();
|
||||
let endpoints: Vec<String> = m["supported_endpoints"]
|
||||
.as_array()
|
||||
.map(|a| {
|
||||
a.iter()
|
||||
.filter_map(|e| e.as_str().map(String::from))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
Some(CopilotModel {
|
||||
id,
|
||||
codec: codec_for_endpoints(&endpoints),
|
||||
picker_enabled: m["model_picker_enabled"].as_bool().unwrap_or(false),
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
fn has_images(req: &LlmRequest) -> bool {
|
||||
req.messages
|
||||
.iter()
|
||||
.flat_map(|m| &m.content)
|
||||
.any(|c| matches!(c, WireContent::Image { .. }))
|
||||
}
|
||||
|
||||
fn initiator_header(initiator: Initiator) -> &'static str {
|
||||
match initiator {
|
||||
Initiator::User => "user",
|
||||
Initiator::Agent => "agent",
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CopilotProvider {
|
||||
tokens: TokenProvider,
|
||||
base_url: String,
|
||||
client: reqwest::Client,
|
||||
/// model id → codec, from `GET /models`. Unknown models default to chat completions.
|
||||
codecs: HashMap<String, CopilotCodec>,
|
||||
}
|
||||
|
||||
impl CopilotProvider {
|
||||
pub fn new(oauth_token: impl Into<String>, models: Vec<CopilotModel>) -> Self {
|
||||
Self::with_base_url(oauth_token, DEFAULT_BASE_URL, models)
|
||||
}
|
||||
|
||||
pub fn with_base_url(
|
||||
oauth_token: impl Into<String>,
|
||||
base_url: impl Into<String>,
|
||||
models: Vec<CopilotModel>,
|
||||
) -> Self {
|
||||
let client = reqwest::Client::new();
|
||||
let codecs = models.into_iter().map(|m| (m.id, m.codec)).collect();
|
||||
Self {
|
||||
tokens: TokenProvider::new(oauth_token, client.clone()),
|
||||
base_url: base_url.into(),
|
||||
client,
|
||||
codecs,
|
||||
}
|
||||
}
|
||||
|
||||
fn codec_for(&self, model: &str) -> CopilotCodec {
|
||||
self.codecs
|
||||
.get(model)
|
||||
.copied()
|
||||
.unwrap_or(CopilotCodec::Chat)
|
||||
}
|
||||
|
||||
fn classify_error(status: reqwest::StatusCode, body: String) -> ProviderError {
|
||||
match status.as_u16() {
|
||||
401 | 403 => ProviderError::Auth(body),
|
||||
429 => ProviderError::RateLimited { retry_after: None },
|
||||
s if (500..600).contains(&s) => ProviderError::Overloaded,
|
||||
s => ProviderError::Http { status: s, body },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Provider for CopilotProvider {
|
||||
fn id(&self) -> &str {
|
||||
"github-copilot"
|
||||
}
|
||||
|
||||
async fn list_models(&self) -> Result<Vec<ModelInfo>, ProviderError> {
|
||||
// Cost/limit metadata is layered on by models.dev; routing metadata lives in `codecs`.
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
req: LlmRequest,
|
||||
cancel: CancellationToken,
|
||||
) -> Result<LlmEventStream, ProviderError> {
|
||||
let codec = self.codec_for(&req.model);
|
||||
let body = codec.build_request(&req);
|
||||
let url = format!("{}{}", self.base_url, codec.path());
|
||||
|
||||
let now_secs = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap_or_default()
|
||||
.as_secs() as i64;
|
||||
let token = self.tokens.token(now_secs).await;
|
||||
|
||||
let mut builder = self
|
||||
.client
|
||||
.post(&url)
|
||||
.header("authorization", format!("Bearer {}", token.token))
|
||||
.header("user-agent", USER_AGENT)
|
||||
.header("x-github-api-version", API_VERSION)
|
||||
.header("openai-intent", "conversation-edits")
|
||||
.header("x-initiator", initiator_header(req.initiator));
|
||||
if has_images(&req) {
|
||||
builder = builder.header("copilot-vision-request", "true");
|
||||
}
|
||||
if codec == CopilotCodec::Anthropic {
|
||||
builder = builder.header("anthropic-beta", ANTHROPIC_BETA);
|
||||
}
|
||||
|
||||
let send = builder.json(&body).send();
|
||||
let response = tokio::select! {
|
||||
result = send => result.map_err(|e| ProviderError::Network(e.to_string()))?,
|
||||
_ = cancel.cancelled() => return Err(ProviderError::Cancelled),
|
||||
};
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
return Err(Self::classify_error(status, body));
|
||||
}
|
||||
|
||||
Ok(codec.decode(response.bytes_stream()))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn codec_routing_prefers_responses_then_anthropic_then_chat() {
|
||||
assert_eq!(
|
||||
codec_for_endpoints(&["/chat/completions".into(), "/responses".into()]),
|
||||
CopilotCodec::Responses
|
||||
);
|
||||
assert_eq!(
|
||||
codec_for_endpoints(&["/v1/messages".into()]),
|
||||
CopilotCodec::Anthropic
|
||||
);
|
||||
assert_eq!(
|
||||
codec_for_endpoints(&["/chat/completions".into()]),
|
||||
CopilotCodec::Chat
|
||||
);
|
||||
assert_eq!(codec_for_endpoints(&[]), CopilotCodec::Chat);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_models_with_endpoints_and_picker_flag() {
|
||||
let body = json!({
|
||||
"data": [
|
||||
{"id": "gpt-4o", "supported_endpoints": ["/chat/completions"], "model_picker_enabled": true},
|
||||
{"id": "claude-sonnet-4-5", "supported_endpoints": ["/v1/messages"], "model_picker_enabled": true},
|
||||
{"id": "o3", "supported_endpoints": ["/responses"], "model_picker_enabled": false},
|
||||
{"id": "no-endpoints"}
|
||||
]
|
||||
});
|
||||
let models = parse_models(&body);
|
||||
assert_eq!(models.len(), 4);
|
||||
assert_eq!(models[0].codec, CopilotCodec::Chat);
|
||||
assert!(models[0].picker_enabled);
|
||||
assert_eq!(models[1].codec, CopilotCodec::Anthropic);
|
||||
assert_eq!(models[2].codec, CopilotCodec::Responses);
|
||||
assert!(!models[2].picker_enabled);
|
||||
// Missing supported_endpoints → default chat, picker false.
|
||||
assert_eq!(models[3].codec, CopilotCodec::Chat);
|
||||
assert!(!models[3].picker_enabled);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unknown_model_defaults_to_chat_codec() {
|
||||
let provider = CopilotProvider::new("oauth", vec![]);
|
||||
assert_eq!(provider.codec_for("whatever"), CopilotCodec::Chat);
|
||||
assert_eq!(provider.id(), "github-copilot");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn known_model_uses_mapped_codec() {
|
||||
let provider = CopilotProvider::new(
|
||||
"oauth",
|
||||
vec![CopilotModel {
|
||||
id: "claude-sonnet-4-5".into(),
|
||||
codec: CopilotCodec::Anthropic,
|
||||
picker_enabled: true,
|
||||
}],
|
||||
);
|
||||
assert_eq!(
|
||||
provider.codec_for("claude-sonnet-4-5"),
|
||||
CopilotCodec::Anthropic
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
//! Copilot API token strategy (flagged-risk area — verify against the live API).
|
||||
//!
|
||||
//! opencode sends the GitHub OAuth token directly as the Bearer (`expires: 0`). The classic
|
||||
//! Copilot API instead wants a short-lived token from `copilot_internal/v2/token`. We try the
|
||||
//! exchange first and cache it until shortly before expiry; if the endpoint rejects us, we fall
|
||||
//! back to the direct-Bearer behavior. Refresh is single-flight under a `tokio::Mutex`.
|
||||
|
||||
use serde::Deserialize;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
const EXCHANGE_URL: &str = "https://api.github.com/copilot_internal/v2/token";
|
||||
/// Refresh this many seconds before the reported expiry.
|
||||
const EXPIRY_SKEW_SECS: i64 = 120;
|
||||
const USER_AGENT: &str = concat!("ai-harness/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct CopilotToken {
|
||||
pub token: String,
|
||||
/// Unix seconds; `0` means "never expires" (direct-Bearer fallback).
|
||||
pub expires_at: i64,
|
||||
}
|
||||
|
||||
impl CopilotToken {
|
||||
/// True when the token should be refreshed at `now_secs` (never, for `expires_at == 0`).
|
||||
pub fn needs_refresh(&self, now_secs: i64) -> bool {
|
||||
self.expires_at != 0 && now_secs + EXPIRY_SKEW_SECS >= self.expires_at
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum TokenError {
|
||||
#[error("network: {0}")]
|
||||
Network(String),
|
||||
/// The exchange endpoint is unavailable/unauthorized — the caller should fall back to the
|
||||
/// direct-Bearer strategy.
|
||||
#[error("exchange unsupported (status {0})")]
|
||||
ExchangeUnsupported(u16),
|
||||
#[error("unexpected response: {0}")]
|
||||
Unexpected(String),
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ExchangeResponse {
|
||||
token: String,
|
||||
#[serde(default)]
|
||||
expires_at: i64,
|
||||
}
|
||||
|
||||
/// Performs the token exchange once. `ExchangeUnsupported` signals the caller to fall back.
|
||||
pub async fn exchange(
|
||||
client: &reqwest::Client,
|
||||
oauth_token: &str,
|
||||
) -> Result<CopilotToken, TokenError> {
|
||||
let resp = client
|
||||
.get(EXCHANGE_URL)
|
||||
.header("authorization", format!("token {oauth_token}"))
|
||||
.header("accept", "application/json")
|
||||
.header("user-agent", USER_AGENT)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| TokenError::Network(e.to_string()))?;
|
||||
|
||||
let status = resp.status();
|
||||
if !status.is_success() {
|
||||
// 401/403/404 → this deployment doesn't support the exchange; fall back to direct Bearer.
|
||||
if matches!(status.as_u16(), 401 | 403 | 404) {
|
||||
return Err(TokenError::ExchangeUnsupported(status.as_u16()));
|
||||
}
|
||||
return Err(TokenError::Network(format!("status {}", status.as_u16())));
|
||||
}
|
||||
|
||||
let parsed: ExchangeResponse = resp
|
||||
.json()
|
||||
.await
|
||||
.map_err(|e| TokenError::Unexpected(e.to_string()))?;
|
||||
Ok(CopilotToken {
|
||||
token: parsed.token,
|
||||
expires_at: parsed.expires_at,
|
||||
})
|
||||
}
|
||||
|
||||
/// Caches the exchanged Copilot token and refreshes it single-flight. Falls back to using the
|
||||
/// OAuth token directly (never-expiring) when the exchange endpoint rejects the request.
|
||||
pub struct TokenProvider {
|
||||
oauth_token: String,
|
||||
client: reqwest::Client,
|
||||
cached: Mutex<Option<CopilotToken>>,
|
||||
}
|
||||
|
||||
impl TokenProvider {
|
||||
pub fn new(oauth_token: impl Into<String>, client: reqwest::Client) -> Self {
|
||||
Self {
|
||||
oauth_token: oauth_token.into(),
|
||||
client,
|
||||
cached: Mutex::new(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a usable Bearer token, refreshing/exchanging as needed. `now_secs` is injected
|
||||
/// for testability.
|
||||
pub async fn token(&self, now_secs: i64) -> CopilotToken {
|
||||
let mut guard = self.cached.lock().await;
|
||||
if let Some(tok) = guard.as_ref() {
|
||||
if !tok.needs_refresh(now_secs) {
|
||||
return tok.clone();
|
||||
}
|
||||
}
|
||||
let fresh = match exchange(&self.client, &self.oauth_token).await {
|
||||
Ok(tok) => tok,
|
||||
Err(_) => CopilotToken {
|
||||
// Direct-Bearer fallback: use the OAuth token itself, never expiring.
|
||||
token: self.oauth_token.clone(),
|
||||
expires_at: 0,
|
||||
},
|
||||
};
|
||||
*guard = Some(fresh.clone());
|
||||
fresh
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn never_expiring_token_does_not_refresh() {
|
||||
let tok = CopilotToken {
|
||||
token: "t".into(),
|
||||
expires_at: 0,
|
||||
};
|
||||
assert!(!tok.needs_refresh(i64::MAX));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refresh_triggers_within_skew_window() {
|
||||
let tok = CopilotToken {
|
||||
token: "t".into(),
|
||||
expires_at: 1_000,
|
||||
};
|
||||
assert!(!tok.needs_refresh(800)); // 800 + 120 < 1000
|
||||
assert!(tok.needs_refresh(881)); // 881 + 120 >= 1000
|
||||
assert!(tok.needs_refresh(1_000));
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,14 @@
|
||||
pub mod anthropic;
|
||||
pub mod auth;
|
||||
pub mod codec;
|
||||
pub mod copilot;
|
||||
pub mod modelsdev;
|
||||
pub mod openai;
|
||||
pub mod registry;
|
||||
|
||||
pub use anthropic::AnthropicProvider;
|
||||
pub use auth::{AuthRecord, AuthStorage};
|
||||
pub use copilot::CopilotProvider;
|
||||
pub use modelsdev::ModelCatalog;
|
||||
pub use openai::OpenAiProvider;
|
||||
pub use registry::ProviderRegistry;
|
||||
|
||||
@@ -0,0 +1,296 @@
|
||||
//! models.dev metadata: per-model context/output limits, pricing, and capability flags.
|
||||
//!
|
||||
//! At runtime we prefer a cached copy of `https://models.dev/api.json` (refreshed every 24h);
|
||||
//! if the cache is missing/stale and the network is unavailable, we fall back to a baked
|
||||
//! snapshot so cost display and limits still work offline.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::{Duration, SystemTime};
|
||||
|
||||
use harness_core::types::{ModelCost, ModelInfo, ModelRef};
|
||||
use serde::Deserialize;
|
||||
|
||||
const API_URL: &str = "https://models.dev/api.json";
|
||||
const CACHE_TTL: Duration = Duration::from_secs(24 * 60 * 60);
|
||||
/// Baked fallback, refreshed manually. Keeps cost/limits working with no cache and no network.
|
||||
const SNAPSHOT: &str = include_str!("../assets/models-snapshot.json");
|
||||
|
||||
/// models.dev top-level shape: `{ provider_id: { models: { model_id: {...} } } }`. Unknown
|
||||
/// keys (provider metadata, per-model extras) are ignored.
|
||||
#[derive(Deserialize)]
|
||||
struct ApiProvider {
|
||||
#[serde(default)]
|
||||
models: HashMap<String, ApiModel>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ApiModel {
|
||||
#[serde(default)]
|
||||
reasoning: bool,
|
||||
#[serde(default)]
|
||||
tool_call: bool,
|
||||
#[serde(default)]
|
||||
attachment: bool,
|
||||
#[serde(default)]
|
||||
cost: ApiCost,
|
||||
#[serde(default)]
|
||||
limit: ApiLimit,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
struct ApiCost {
|
||||
#[serde(default)]
|
||||
input: f64,
|
||||
#[serde(default)]
|
||||
output: f64,
|
||||
#[serde(default)]
|
||||
cache_read: f64,
|
||||
#[serde(default)]
|
||||
cache_write: f64,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
struct ApiLimit {
|
||||
#[serde(default)]
|
||||
context: u64,
|
||||
#[serde(default)]
|
||||
output: u64,
|
||||
}
|
||||
|
||||
/// Parsed metadata keyed by `(provider_id, model_id)`.
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct ModelCatalog {
|
||||
models: HashMap<(String, String), ModelInfo>,
|
||||
}
|
||||
|
||||
impl ModelCatalog {
|
||||
/// Parses a models.dev `api.json` document.
|
||||
pub fn from_api_json(bytes: &[u8]) -> Result<Self, serde_json::Error> {
|
||||
let raw: HashMap<String, ApiProvider> = serde_json::from_slice(bytes)?;
|
||||
let mut models = HashMap::new();
|
||||
for (provider_id, provider) in raw {
|
||||
for (model_id, m) in provider.models {
|
||||
let info = ModelInfo {
|
||||
model: ModelRef::new(provider_id.clone(), model_id.clone()),
|
||||
context_limit: m.limit.context,
|
||||
output_limit: m.limit.output,
|
||||
cost: ModelCost {
|
||||
input: m.cost.input,
|
||||
output: m.cost.output,
|
||||
cache_read: m.cost.cache_read,
|
||||
cache_write: m.cost.cache_write,
|
||||
},
|
||||
reasoning: m.reasoning,
|
||||
tool_call: m.tool_call,
|
||||
attachment: m.attachment,
|
||||
};
|
||||
models.insert((provider_id.clone(), model_id), info);
|
||||
}
|
||||
}
|
||||
Ok(Self { models })
|
||||
}
|
||||
|
||||
/// The baked fallback snapshot — always available, never fails.
|
||||
pub fn baked() -> Self {
|
||||
Self::from_api_json(SNAPSHOT.as_bytes()).expect("baked models snapshot must parse")
|
||||
}
|
||||
|
||||
pub fn get(&self, provider: &str, model: &str) -> Option<&ModelInfo> {
|
||||
self.models.get(&(provider.to_string(), model.to_string()))
|
||||
}
|
||||
|
||||
/// Pricing for a model, or zero-cost if unknown.
|
||||
pub fn cost(&self, provider: &str, model: &str) -> ModelCost {
|
||||
self.get(provider, model)
|
||||
.map(|m| m.cost)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.models.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.models.is_empty()
|
||||
}
|
||||
|
||||
/// Default cache location: `~/.cache/ai-harness/models.json`.
|
||||
pub fn default_cache_path() -> PathBuf {
|
||||
dirs::cache_dir()
|
||||
.unwrap_or_else(std::env::temp_dir)
|
||||
.join("ai-harness")
|
||||
.join("models.json")
|
||||
}
|
||||
|
||||
/// Loads from the cache if present and younger than [`CACHE_TTL`]; otherwise returns the
|
||||
/// baked snapshot. Never touches the network — call [`ModelCatalog::refresh`] for that.
|
||||
pub fn load_cached_or_baked(cache_path: &Path) -> Self {
|
||||
if cache_fresh(cache_path, CACHE_TTL, SystemTime::now()) {
|
||||
if let Ok(bytes) = std::fs::read(cache_path) {
|
||||
if let Ok(catalog) = Self::from_api_json(&bytes) {
|
||||
if !catalog.is_empty() {
|
||||
return catalog;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Self::baked()
|
||||
}
|
||||
|
||||
/// Refreshes the default cache location with a fresh HTTP client. Convenience wrapper for
|
||||
/// callers (the app) that don't want to depend on `reqwest` directly.
|
||||
pub async fn refresh_default_cache() -> Result<Self, RefreshError> {
|
||||
let client = reqwest::Client::new();
|
||||
Self::refresh(&client, &Self::default_cache_path()).await
|
||||
}
|
||||
|
||||
/// Fetches the latest metadata and writes it to `cache_path` (best-effort). Returns the
|
||||
/// freshly-parsed catalog. Intended to run in the background so the *next* launch is current.
|
||||
pub async fn refresh(
|
||||
client: &reqwest::Client,
|
||||
cache_path: &Path,
|
||||
) -> Result<Self, RefreshError> {
|
||||
let bytes = client
|
||||
.get(API_URL)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| RefreshError::Network(e.to_string()))?
|
||||
.error_for_status()
|
||||
.map_err(|e| RefreshError::Network(e.to_string()))?
|
||||
.bytes()
|
||||
.await
|
||||
.map_err(|e| RefreshError::Network(e.to_string()))?;
|
||||
|
||||
let catalog =
|
||||
Self::from_api_json(&bytes).map_err(|e| RefreshError::Parse(e.to_string()))?;
|
||||
|
||||
if let Some(parent) = cache_path.parent() {
|
||||
let _ = std::fs::create_dir_all(parent);
|
||||
}
|
||||
if let Err(e) = std::fs::write(cache_path, &bytes) {
|
||||
tracing::warn!(error = %e, "failed to cache models.dev metadata");
|
||||
}
|
||||
Ok(catalog)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum RefreshError {
|
||||
#[error("network: {0}")]
|
||||
Network(String),
|
||||
#[error("parse: {0}")]
|
||||
Parse(String),
|
||||
}
|
||||
|
||||
/// True when `path` exists and was modified within `ttl` of `now`.
|
||||
fn cache_fresh(path: &Path, ttl: Duration, now: SystemTime) -> bool {
|
||||
let Ok(modified) = std::fs::metadata(path).and_then(|m| m.modified()) else {
|
||||
return false;
|
||||
};
|
||||
match now.duration_since(modified) {
|
||||
Ok(age) => age < ttl,
|
||||
Err(_) => true, // modified in the future (clock skew) — treat as fresh
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
const FIXTURE: &str = r#"{
|
||||
"anthropic": {
|
||||
"id": "anthropic",
|
||||
"name": "Anthropic",
|
||||
"models": {
|
||||
"claude-sonnet-4-5": {
|
||||
"id": "claude-sonnet-4-5",
|
||||
"reasoning": true,
|
||||
"tool_call": true,
|
||||
"attachment": true,
|
||||
"cost": {"input": 3, "output": 15, "cache_read": 0.3, "cache_write": 3.75},
|
||||
"limit": {"context": 200000, "output": 64000}
|
||||
}
|
||||
}
|
||||
},
|
||||
"openai": {
|
||||
"id": "openai",
|
||||
"models": {
|
||||
"gpt-4o": {
|
||||
"id": "gpt-4o",
|
||||
"tool_call": true,
|
||||
"cost": {"input": 2.5, "output": 10},
|
||||
"limit": {"context": 128000, "output": 16384}
|
||||
}
|
||||
}
|
||||
}
|
||||
}"#;
|
||||
|
||||
#[test]
|
||||
fn parses_providers_and_models() {
|
||||
let catalog = ModelCatalog::from_api_json(FIXTURE.as_bytes()).unwrap();
|
||||
assert_eq!(catalog.len(), 2);
|
||||
|
||||
let sonnet = catalog.get("anthropic", "claude-sonnet-4-5").unwrap();
|
||||
assert_eq!(sonnet.context_limit, 200_000);
|
||||
assert_eq!(sonnet.output_limit, 64_000);
|
||||
assert_eq!(sonnet.cost.input, 3.0);
|
||||
assert_eq!(sonnet.cost.cache_write, 3.75);
|
||||
assert!(sonnet.reasoning && sonnet.tool_call && sonnet.attachment);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_cost_fields_default_to_zero() {
|
||||
let catalog = ModelCatalog::from_api_json(FIXTURE.as_bytes()).unwrap();
|
||||
let gpt = catalog.get("openai", "gpt-4o").unwrap();
|
||||
assert_eq!(gpt.cost.cache_read, 0.0);
|
||||
assert_eq!(gpt.cost.cache_write, 0.0);
|
||||
assert!(!gpt.reasoning); // absent → false
|
||||
assert!(gpt.tool_call);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cost_helper_is_zero_for_unknown_model() {
|
||||
let catalog = ModelCatalog::from_api_json(FIXTURE.as_bytes()).unwrap();
|
||||
assert_eq!(catalog.cost("openai", "nonexistent"), ModelCost::default());
|
||||
assert_eq!(catalog.cost("anthropic", "claude-sonnet-4-5").input, 3.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn baked_snapshot_parses_and_is_nonempty() {
|
||||
let catalog = ModelCatalog::baked();
|
||||
assert!(!catalog.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cache_freshness_respects_ttl() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("models.json");
|
||||
assert!(!cache_fresh(&path, CACHE_TTL, SystemTime::now())); // missing
|
||||
|
||||
std::fs::write(&path, "{}").unwrap();
|
||||
let now = SystemTime::now();
|
||||
assert!(cache_fresh(&path, CACHE_TTL, now));
|
||||
// A "now" far in the future makes the file look stale.
|
||||
let future = now + Duration::from_secs(48 * 60 * 60);
|
||||
assert!(!cache_fresh(&path, CACHE_TTL, future));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_cached_or_baked_falls_back_when_cache_absent() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("models.json");
|
||||
let catalog = ModelCatalog::load_cached_or_baked(&path);
|
||||
assert!(!catalog.is_empty()); // baked fallback
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_cached_or_baked_reads_fresh_cache() {
|
||||
let dir = tempfile::tempdir().unwrap();
|
||||
let path = dir.path().join("models.json");
|
||||
std::fs::write(&path, FIXTURE).unwrap();
|
||||
let catalog = ModelCatalog::load_cached_or_baked(&path);
|
||||
assert_eq!(catalog.len(), 2);
|
||||
assert!(catalog.get("anthropic", "claude-sonnet-4-5").is_some());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
use std::time::Duration;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use harness_core::llm::{LlmEventStream, LlmRequest, Provider, ProviderError};
|
||||
use harness_core::types::ModelInfo;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
use crate::codec::{openai_chat, openai_responses};
|
||||
|
||||
const DEFAULT_BASE_URL: &str = "https://api.openai.com/v1";
|
||||
|
||||
/// Which OpenAI wire format to speak for a given model.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
enum ApiFlavor {
|
||||
Responses,
|
||||
Chat,
|
||||
}
|
||||
|
||||
/// `gpt-*` and `o-*` models use the responses API; everything else (including
|
||||
/// OpenAI-compatible third-party endpoints) falls back to chat completions.
|
||||
fn flavor_for(model: &str) -> ApiFlavor {
|
||||
let is_native = model.starts_with("gpt-")
|
||||
|| model.starts_with("o1")
|
||||
|| model.starts_with("o3")
|
||||
|| model.starts_with("o4")
|
||||
|| model == "o1"
|
||||
|| model == "o3";
|
||||
if is_native {
|
||||
ApiFlavor::Responses
|
||||
} else {
|
||||
ApiFlavor::Chat
|
||||
}
|
||||
}
|
||||
|
||||
pub struct OpenAiProvider {
|
||||
api_key: String,
|
||||
base_url: String,
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
impl OpenAiProvider {
|
||||
pub fn new(api_key: impl Into<String>) -> Self {
|
||||
Self {
|
||||
api_key: api_key.into(),
|
||||
base_url: DEFAULT_BASE_URL.to_string(),
|
||||
client: reqwest::Client::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_base_url(api_key: impl Into<String>, base_url: impl Into<String>) -> Self {
|
||||
Self {
|
||||
api_key: api_key.into(),
|
||||
base_url: base_url.into(),
|
||||
client: reqwest::Client::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn classify_error(
|
||||
status: reqwest::StatusCode,
|
||||
body: String,
|
||||
retry_after: Option<Duration>,
|
||||
) -> ProviderError {
|
||||
match status.as_u16() {
|
||||
400 if body.contains("context_length_exceeded")
|
||||
|| body.contains("maximum context length") =>
|
||||
{
|
||||
ProviderError::ContextOverflow
|
||||
}
|
||||
401 | 403 => ProviderError::Auth(body),
|
||||
429 => ProviderError::RateLimited { retry_after },
|
||||
s if (500..600).contains(&s) => ProviderError::Overloaded,
|
||||
s => ProviderError::Http { status: s, body },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Provider for OpenAiProvider {
|
||||
fn id(&self) -> &str {
|
||||
"openai"
|
||||
}
|
||||
|
||||
async fn list_models(&self) -> Result<Vec<ModelInfo>, ProviderError> {
|
||||
// models.dev metadata is layered on top by the caller (see `modelsdev.rs`).
|
||||
Ok(Vec::new())
|
||||
}
|
||||
|
||||
async fn stream(
|
||||
&self,
|
||||
req: LlmRequest,
|
||||
cancel: CancellationToken,
|
||||
) -> Result<LlmEventStream, ProviderError> {
|
||||
let (path, body) = match flavor_for(&req.model) {
|
||||
ApiFlavor::Responses => ("/responses", openai_responses::build_request(&req)),
|
||||
ApiFlavor::Chat => ("/chat/completions", openai_chat::build_request(&req)),
|
||||
};
|
||||
let url = format!("{}{}", self.base_url, path);
|
||||
|
||||
let send = self
|
||||
.client
|
||||
.post(&url)
|
||||
.header("authorization", format!("Bearer {}", self.api_key))
|
||||
.json(&body)
|
||||
.send();
|
||||
|
||||
let response = tokio::select! {
|
||||
result = send => result.map_err(|e| ProviderError::Network(e.to_string()))?,
|
||||
_ = cancel.cancelled() => return Err(ProviderError::Cancelled),
|
||||
};
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let retry_after = response
|
||||
.headers()
|
||||
.get("retry-after")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.and_then(|s| s.parse::<u64>().ok())
|
||||
.map(Duration::from_secs);
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
return Err(Self::classify_error(status, body, retry_after));
|
||||
}
|
||||
|
||||
Ok(match flavor_for(&req.model) {
|
||||
ApiFlavor::Responses => openai_responses::decode(response.bytes_stream()),
|
||||
ApiFlavor::Chat => openai_chat::decode(response.bytes_stream()),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn routes_gpt_and_o_series_to_responses() {
|
||||
assert_eq!(flavor_for("gpt-4o"), ApiFlavor::Responses);
|
||||
assert_eq!(flavor_for("gpt-5"), ApiFlavor::Responses);
|
||||
assert_eq!(flavor_for("o1"), ApiFlavor::Responses);
|
||||
assert_eq!(flavor_for("o3-mini"), ApiFlavor::Responses);
|
||||
assert_eq!(flavor_for("o4-mini"), ApiFlavor::Responses);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn routes_other_models_to_chat() {
|
||||
assert_eq!(flavor_for("llama-3.1-70b"), ApiFlavor::Chat);
|
||||
assert_eq!(flavor_for("deepseek-chat"), ApiFlavor::Chat);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn id_is_openai() {
|
||||
assert_eq!(OpenAiProvider::new("k").id(), "openai");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn classifies_context_overflow_from_400_body() {
|
||||
assert!(matches!(
|
||||
OpenAiProvider::classify_error(
|
||||
reqwest::StatusCode::BAD_REQUEST,
|
||||
"context_length_exceeded".into(),
|
||||
None,
|
||||
),
|
||||
ProviderError::ContextOverflow
|
||||
));
|
||||
assert!(matches!(
|
||||
OpenAiProvider::classify_error(
|
||||
reqwest::StatusCode::BAD_REQUEST,
|
||||
"some other error".into(),
|
||||
None,
|
||||
),
|
||||
ProviderError::Http { status: 400, .. }
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,21 @@ path = "src/main.rs"
|
||||
harness-app = { workspace = true }
|
||||
harness-core = { workspace = true }
|
||||
tokio = { workspace = true }
|
||||
ratatui = { workspace = true }
|
||||
crossterm = { workspace = true }
|
||||
tui-textarea = { workspace = true }
|
||||
pulldown-cmark = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
tracing-subscriber = { workspace = true }
|
||||
tracing-appender = { workspace = true }
|
||||
dirs = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
futures = { workspace = true }
|
||||
tokio-util = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
insta = "1"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crossterm::event::EventStream;
|
||||
use futures::StreamExt;
|
||||
use harness_app::EngineHandle;
|
||||
use harness_core::event::AppEvent;
|
||||
use ratatui::backend::CrosstermBackend;
|
||||
use ratatui::Terminal;
|
||||
use tokio::sync::broadcast;
|
||||
use tokio::time::{interval, Duration};
|
||||
|
||||
use crate::input::{apply_action, handle_event};
|
||||
use crate::render::render;
|
||||
use crate::state::AppState;
|
||||
use crate::terminal::TerminalGuard;
|
||||
|
||||
const DEFAULT_MODEL: &str = "anthropic/claude-sonnet-4-5";
|
||||
const DEFAULT_AGENT: &str = "orchestrator";
|
||||
const RENDER_TICK_MS: u64 = 33;
|
||||
|
||||
pub struct App {
|
||||
engine: EngineHandle,
|
||||
state: AppState,
|
||||
_terminal_guard: TerminalGuard,
|
||||
terminal: Terminal<CrosstermBackend<io::Stdout>>,
|
||||
events: EventStream,
|
||||
bus_rx: broadcast::Receiver<AppEvent>,
|
||||
render_interval: tokio::time::Interval,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub async fn new(cwd: PathBuf) -> anyhow::Result<Self> {
|
||||
let engine = EngineHandle::init(cwd)?;
|
||||
let bus_rx = engine.bus().subscribe();
|
||||
|
||||
let config = engine.config();
|
||||
let model_ref = config
|
||||
.model
|
||||
.clone()
|
||||
.unwrap_or_else(|| DEFAULT_MODEL.to_string());
|
||||
let agent = DEFAULT_AGENT.to_string();
|
||||
|
||||
let mut state = AppState::new(model_ref, agent);
|
||||
|
||||
// Create a default session so the user can start typing immediately.
|
||||
match engine.new_session(&state.agent, &state.model_ref).await {
|
||||
Ok(session_id) => {
|
||||
state.session_id = Some(session_id);
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(error = %e, "failed to create default session");
|
||||
}
|
||||
}
|
||||
|
||||
let terminal_guard = TerminalGuard::enter()?;
|
||||
let backend = CrosstermBackend::new(io::stdout());
|
||||
let terminal = Terminal::new(backend)?;
|
||||
let events = EventStream::new();
|
||||
let render_interval = interval(Duration::from_millis(RENDER_TICK_MS));
|
||||
|
||||
Ok(Self {
|
||||
engine,
|
||||
state,
|
||||
_terminal_guard: terminal_guard,
|
||||
terminal,
|
||||
events,
|
||||
bus_rx,
|
||||
render_interval,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> anyhow::Result<()> {
|
||||
self.state.dirty = true;
|
||||
while !self.state.quit {
|
||||
tokio::select! {
|
||||
biased;
|
||||
|
||||
maybe_event = self.events.next() => {
|
||||
match maybe_event {
|
||||
Some(Ok(event)) => {
|
||||
let action = handle_event(event, &mut self.state, &self.engine);
|
||||
apply_action(action, &mut self.state, &self.engine).await;
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
tracing::error!(error = %e, "input error");
|
||||
}
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(event) = self.bus_rx.recv() => {
|
||||
self.state.apply_event(event);
|
||||
}
|
||||
|
||||
_ = self.render_interval.tick() => {
|
||||
if self.state.dirty {
|
||||
self.terminal.draw(|frame| render(frame, &mut self.state))?;
|
||||
self.state.dirty = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,264 @@
|
||||
use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
||||
use harness_app::EngineHandle;
|
||||
use harness_core::permission::PermissionReply;
|
||||
|
||||
use crate::modal;
|
||||
use crate::state::{AppState, ModalState};
|
||||
|
||||
/// Possible high-level actions produced by key input.
|
||||
#[derive(Debug)]
|
||||
pub enum InputAction {
|
||||
None,
|
||||
Submit { text: String },
|
||||
Abort,
|
||||
Quit,
|
||||
LoadSessions,
|
||||
NewSession,
|
||||
SetModel(String),
|
||||
SetAgent(String),
|
||||
CloseModal,
|
||||
ScrollUp(u16),
|
||||
ScrollDown(u16),
|
||||
PermissionReply(PermissionReply),
|
||||
SessionPickerUp,
|
||||
SessionPickerDown,
|
||||
SessionPickerSelect,
|
||||
}
|
||||
|
||||
/// Translate a crossterm event into an action and/or mutate `state` directly.
|
||||
pub fn handle_event(event: Event, state: &mut AppState, _engine: &EngineHandle) -> InputAction {
|
||||
match event {
|
||||
Event::Key(key) => {
|
||||
let action = handle_key(key, state);
|
||||
// Any keypress can change the input buffer or cursor; force a redraw so typed
|
||||
// characters appear immediately rather than only when some other event sets dirty.
|
||||
state.dirty = true;
|
||||
action
|
||||
}
|
||||
Event::Resize(_, _) => {
|
||||
state.dirty = true;
|
||||
InputAction::None
|
||||
}
|
||||
Event::Mouse(_) | Event::FocusGained | Event::FocusLost | Event::Paste(_) => {
|
||||
InputAction::None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_key(key: KeyEvent, state: &mut AppState) -> InputAction {
|
||||
match &state.modal {
|
||||
ModalState::Permission { .. } => handle_permission_key(key, state),
|
||||
ModalState::SessionPicker { .. } => handle_session_picker_key(key, state),
|
||||
ModalState::None => handle_normal_key(key, state),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_permission_key(key: KeyEvent, _state: &mut AppState) -> InputAction {
|
||||
match key.code {
|
||||
KeyCode::Char('y') | KeyCode::Char('Y') => {
|
||||
InputAction::PermissionReply(PermissionReply::Once)
|
||||
}
|
||||
KeyCode::Char('a') | KeyCode::Char('A') => {
|
||||
InputAction::PermissionReply(PermissionReply::Always)
|
||||
}
|
||||
KeyCode::Char('n') | KeyCode::Char('N') => {
|
||||
InputAction::PermissionReply(PermissionReply::Reject)
|
||||
}
|
||||
// Esc rejects the pending request rather than merely hiding the modal — closing
|
||||
// without a reply would leave the tool call blocked on its oneshot forever.
|
||||
KeyCode::Esc => InputAction::PermissionReply(PermissionReply::Reject),
|
||||
_ => InputAction::None,
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_session_picker_key(key: KeyEvent, _state: &mut AppState) -> InputAction {
|
||||
match key.code {
|
||||
KeyCode::Up => InputAction::SessionPickerUp,
|
||||
KeyCode::Down => InputAction::SessionPickerDown,
|
||||
KeyCode::Enter => InputAction::SessionPickerSelect,
|
||||
KeyCode::Esc => InputAction::CloseModal,
|
||||
_ => InputAction::None,
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_normal_key(key: KeyEvent, state: &mut AppState) -> InputAction {
|
||||
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
|
||||
|
||||
// Reset the double-Ctrl+C guard on any key that isn't another Ctrl+C.
|
||||
if !(matches!(key.code, KeyCode::Char('c') | KeyCode::Char('C')) && ctrl) {
|
||||
state.ctrl_c_pressed = false;
|
||||
}
|
||||
|
||||
match key.code {
|
||||
KeyCode::Enter => {
|
||||
let text = state.input.lines().join("\n");
|
||||
state.input = tui_textarea::TextArea::default();
|
||||
state
|
||||
.input
|
||||
.set_cursor_line_style(ratatui::style::Style::default());
|
||||
if let Some(action) = parse_slash_command(&text) {
|
||||
action
|
||||
} else {
|
||||
InputAction::Submit { text }
|
||||
}
|
||||
}
|
||||
KeyCode::Char('c') if ctrl => {
|
||||
if state.ctrl_c_pressed {
|
||||
InputAction::Quit
|
||||
} else {
|
||||
state.ctrl_c_pressed = true;
|
||||
state.dirty = true;
|
||||
InputAction::None
|
||||
}
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
if state.running {
|
||||
InputAction::Abort
|
||||
} else {
|
||||
InputAction::None
|
||||
}
|
||||
}
|
||||
KeyCode::Char('s') if ctrl => InputAction::LoadSessions,
|
||||
KeyCode::Up => {
|
||||
let (row, _) = state.input.cursor();
|
||||
if row == 0 {
|
||||
InputAction::ScrollUp(3)
|
||||
} else {
|
||||
state.input.input(key);
|
||||
InputAction::None
|
||||
}
|
||||
}
|
||||
KeyCode::Down => {
|
||||
let (row, _) = state.input.cursor();
|
||||
let last_line = state.input.lines().len().saturating_sub(1);
|
||||
if row == last_line {
|
||||
InputAction::ScrollDown(3)
|
||||
} else {
|
||||
state.input.input(key);
|
||||
InputAction::None
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
state.input.input(key);
|
||||
InputAction::None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_slash_command(text: &str) -> Option<InputAction> {
|
||||
let trimmed = text.trim();
|
||||
if !trimmed.starts_with('/') {
|
||||
return None;
|
||||
}
|
||||
let mut parts = trimmed.split_whitespace();
|
||||
let cmd = parts.next()?;
|
||||
let rest: String = parts.collect::<Vec<_>>().join(" ");
|
||||
match cmd {
|
||||
"/new" => Some(InputAction::NewSession),
|
||||
"/model" if !rest.is_empty() => Some(InputAction::SetModel(rest)),
|
||||
"/agent" if !rest.is_empty() => Some(InputAction::SetAgent(rest)),
|
||||
"/sessions" => Some(InputAction::LoadSessions),
|
||||
"/quit" => Some(InputAction::Quit),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply an action that needs async engine calls. Non-async decisions are applied inline.
|
||||
pub async fn apply_action(action: InputAction, state: &mut AppState, engine: &EngineHandle) {
|
||||
match action {
|
||||
InputAction::Submit { text } => {
|
||||
if let Some(session_id) = state.session_id.clone() {
|
||||
if let Err(e) = engine.prompt(session_id, text, &state.model_ref).await {
|
||||
tracing::error!(error = %e, "prompt failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
InputAction::Abort => {
|
||||
if let Some(session_id) = state.session_id.clone() {
|
||||
engine.abort(&session_id);
|
||||
}
|
||||
}
|
||||
InputAction::LoadSessions => match engine.list_sessions().await {
|
||||
Ok(sessions) => modal::open_session_picker(state, sessions),
|
||||
Err(e) => tracing::error!(error = %e, "failed to list sessions"),
|
||||
},
|
||||
InputAction::NewSession => match engine.new_session(&state.agent, &state.model_ref).await {
|
||||
Ok(id) => {
|
||||
state.session_id = Some(id);
|
||||
state.messages.clear();
|
||||
state.scroll_offset = 0;
|
||||
state.session_cost = 0.0;
|
||||
state.session_tokens = 0;
|
||||
state.dirty = true;
|
||||
}
|
||||
Err(e) => tracing::error!(error = %e, "failed to create session"),
|
||||
},
|
||||
InputAction::SetModel(model_ref) => {
|
||||
state.model_ref = model_ref;
|
||||
state.dirty = true;
|
||||
}
|
||||
InputAction::SetAgent(agent) => {
|
||||
state.agent = agent;
|
||||
state.dirty = true;
|
||||
}
|
||||
InputAction::Quit => state.quit = true,
|
||||
InputAction::CloseModal => state.close_modal(),
|
||||
InputAction::ScrollUp(n) => state.scroll_up(n),
|
||||
InputAction::ScrollDown(n) => state.scroll_down(n),
|
||||
InputAction::PermissionReply(reply) => {
|
||||
modal::resolve_permission(state, engine, reply);
|
||||
}
|
||||
InputAction::SessionPickerUp => {
|
||||
if let ModalState::SessionPicker {
|
||||
selected,
|
||||
sessions: _,
|
||||
} = &mut state.modal
|
||||
{
|
||||
*selected = selected.saturating_sub(1);
|
||||
}
|
||||
state.dirty = true;
|
||||
}
|
||||
InputAction::SessionPickerDown => {
|
||||
if let ModalState::SessionPicker { selected, sessions } = &mut state.modal {
|
||||
if *selected + 1 < sessions.len() {
|
||||
*selected += 1;
|
||||
}
|
||||
}
|
||||
state.dirty = true;
|
||||
}
|
||||
InputAction::SessionPickerSelect => {
|
||||
if let ModalState::SessionPicker { sessions, selected } = &state.modal {
|
||||
if let Some(session) = sessions.get(*selected).cloned() {
|
||||
if let Err(e) = modal::select_session(state, engine, session).await {
|
||||
tracing::error!(error = %e, "failed to load session");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
InputAction::None => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::state::AppState;
|
||||
use crossterm::event::KeyEvent;
|
||||
|
||||
#[tokio::test]
|
||||
async fn typing_marks_frame_dirty_and_updates_input() {
|
||||
let engine = EngineHandle::init_in_memory(std::env::temp_dir()).unwrap();
|
||||
let mut state = AppState::new("anthropic/claude".into(), "orchestrator".into());
|
||||
state.dirty = false;
|
||||
|
||||
let action = handle_event(
|
||||
Event::Key(KeyEvent::from(KeyCode::Char('x'))),
|
||||
&mut state,
|
||||
&engine,
|
||||
);
|
||||
|
||||
assert!(matches!(action, InputAction::None));
|
||||
assert!(state.dirty, "a keystroke must request a redraw");
|
||||
assert_eq!(state.input.lines().join("\n"), "x");
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,19 @@
|
||||
mod app;
|
||||
mod input;
|
||||
mod markdown;
|
||||
mod modal;
|
||||
mod render;
|
||||
mod state;
|
||||
mod terminal;
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use harness_core::event::RunOutcome;
|
||||
|
||||
const DEFAULT_MODEL: &str = "anthropic/claude-sonnet-4-5";
|
||||
|
||||
fn print_usage() {
|
||||
eprintln!("usage: harness run -p \"<prompt>\" [-m provider/model]");
|
||||
eprintln!("usage: harness [run -p \"<prompt>\" [-m provider/model]] | [tui]");
|
||||
}
|
||||
|
||||
fn parse_run_args(args: &[String]) -> Option<(String, Option<String>)> {
|
||||
@@ -26,7 +36,7 @@ fn parse_run_args(args: &[String]) -> Option<(String, Option<String>)> {
|
||||
prompt.map(|p| (p, model))
|
||||
}
|
||||
|
||||
async fn run(args: &[String]) -> i32 {
|
||||
async fn run_headless(args: &[String]) -> i32 {
|
||||
let Some((prompt, model_arg)) = parse_run_args(args) else {
|
||||
print_usage();
|
||||
return 2;
|
||||
@@ -49,7 +59,7 @@ async fn run(args: &[String]) -> i32 {
|
||||
};
|
||||
|
||||
let model_ref = model_arg
|
||||
.or_else(|| app.config.model.clone())
|
||||
.or_else(|| app.config().model.clone())
|
||||
.unwrap_or_else(|| DEFAULT_MODEL.to_string());
|
||||
|
||||
match app.run_prompt(prompt, &model_ref).await {
|
||||
@@ -73,14 +83,74 @@ async fn run(args: &[String]) -> i32 {
|
||||
}
|
||||
}
|
||||
|
||||
fn log_dir() -> PathBuf {
|
||||
dirs::data_dir()
|
||||
.unwrap_or_else(std::env::temp_dir)
|
||||
.join("ai-harness")
|
||||
.join("log")
|
||||
}
|
||||
|
||||
fn setup_tracing() -> anyhow::Result<tracing_appender::non_blocking::WorkerGuard> {
|
||||
let log_dir = log_dir();
|
||||
std::fs::create_dir_all(&log_dir)?;
|
||||
let file_appender = tracing_appender::rolling::daily(log_dir, "harness-tui.log");
|
||||
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
|
||||
tracing_subscriber::fmt()
|
||||
.with_writer(non_blocking)
|
||||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
|
||||
.init();
|
||||
Ok(guard)
|
||||
}
|
||||
|
||||
async fn run_tui() -> i32 {
|
||||
let cwd = match std::env::current_dir() {
|
||||
Ok(cwd) => cwd,
|
||||
Err(e) => {
|
||||
eprintln!("error: could not read cwd: {e}");
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
let _guard = match setup_tracing() {
|
||||
Ok(guard) => guard,
|
||||
Err(e) => {
|
||||
eprintln!("error: could not initialize logging: {e}");
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
let mut app = match crate::app::App::new(cwd).await {
|
||||
Ok(app) => app,
|
||||
Err(e) => {
|
||||
tracing::error!(error = %e, "failed to start TUI");
|
||||
eprintln!("error: {e}");
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(e) = app.run().await {
|
||||
tracing::error!(error = %e, "TUI error");
|
||||
eprintln!("error: {e}");
|
||||
return 1;
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let exit_code = if args.first().map(String::as_str) == Some("run") {
|
||||
run(&args[1..]).await
|
||||
} else {
|
||||
println!("harness {}", env!("CARGO_PKG_VERSION"));
|
||||
0
|
||||
let exit_code = match args.first().map(String::as_str) {
|
||||
Some("run") => run_headless(&args[1..]).await,
|
||||
Some("tui") | None => run_tui().await,
|
||||
Some("help") | Some("--help") | Some("-h") => {
|
||||
print_usage();
|
||||
0
|
||||
}
|
||||
Some(cmd) => {
|
||||
eprintln!("unknown command: {cmd}");
|
||||
print_usage();
|
||||
2
|
||||
}
|
||||
};
|
||||
std::process::exit(exit_code);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,255 @@
|
||||
use pulldown_cmark::{CodeBlockKind, Event, HeadingLevel, Parser, Tag, TagEnd};
|
||||
use ratatui::style::{Color, Modifier, Style};
|
||||
use ratatui::text::{Line, Span};
|
||||
|
||||
/// Render a markdown string into wrapped ratatui lines.
|
||||
pub fn render_markdown(text: &str, width: u16) -> Vec<Line<'static>> {
|
||||
let mut lines: Vec<Line<'static>> = Vec::new();
|
||||
let mut current_spans: Vec<Span<'static>> = Vec::new();
|
||||
let mut current_text = String::new();
|
||||
|
||||
let mut in_bold = false;
|
||||
let mut in_italic = false;
|
||||
let mut in_code_block = false;
|
||||
let mut code_block_language = String::new();
|
||||
let mut list_stack: Vec<u64> = Vec::new();
|
||||
|
||||
let flush = |current: &mut String, spans: &mut Vec<Span<'static>>, bold, italic| {
|
||||
if current.is_empty() {
|
||||
return;
|
||||
}
|
||||
let style = base_style(bold, italic);
|
||||
let span = Span::styled(std::mem::take(current), style);
|
||||
spans.push(span);
|
||||
};
|
||||
|
||||
for event in Parser::new(text) {
|
||||
match event {
|
||||
Event::Start(tag) => match tag {
|
||||
Tag::Paragraph => {
|
||||
if !lines.is_empty() && !current_text.is_empty() {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
wrap_spans(&mut lines, &mut current_spans, width);
|
||||
}
|
||||
}
|
||||
Tag::Heading { level, .. } => {
|
||||
let level_num = heading_level_from(level);
|
||||
current_text.push_str(&"#".repeat(level_num as usize));
|
||||
current_text.push(' ');
|
||||
}
|
||||
Tag::Strong => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
in_bold = true;
|
||||
}
|
||||
Tag::Emphasis => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
in_italic = true;
|
||||
}
|
||||
Tag::List(start) => {
|
||||
list_stack.push(start.unwrap_or(1));
|
||||
}
|
||||
Tag::Item => {
|
||||
let prefix = if let Some(n) = list_stack.last_mut() {
|
||||
let p = format!("{n}. ");
|
||||
*n += 1;
|
||||
p
|
||||
} else {
|
||||
"• ".to_string()
|
||||
};
|
||||
current_text.push_str(&prefix);
|
||||
}
|
||||
Tag::CodeBlock(lang) => {
|
||||
in_code_block = true;
|
||||
code_block_language = match lang {
|
||||
CodeBlockKind::Fenced(name) => name.to_string(),
|
||||
CodeBlockKind::Indented => String::new(),
|
||||
};
|
||||
if !current_text.is_empty() || !current_spans.is_empty() {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
wrap_spans(&mut lines, &mut current_spans, width);
|
||||
}
|
||||
let border = if code_block_language.is_empty() {
|
||||
"┌────".to_string()
|
||||
} else {
|
||||
format!("┌──── {code_block_language}")
|
||||
};
|
||||
lines.push(Line::from(Span::styled(border, code_style())));
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Event::End(tag_end) => match tag_end {
|
||||
TagEnd::Heading(_) => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
let mut spans = std::mem::take(&mut current_spans);
|
||||
for span in &mut spans {
|
||||
span.style = span.style.add_modifier(Modifier::BOLD);
|
||||
}
|
||||
wrap_spans(&mut lines, &mut spans, width);
|
||||
}
|
||||
TagEnd::Paragraph => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
wrap_spans(&mut lines, &mut current_spans, width);
|
||||
}
|
||||
TagEnd::Strong => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
in_bold = false;
|
||||
}
|
||||
TagEnd::Emphasis => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
in_italic = false;
|
||||
}
|
||||
TagEnd::List(_) => {
|
||||
list_stack.pop();
|
||||
}
|
||||
TagEnd::CodeBlock => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
if !current_spans.is_empty() {
|
||||
for line in wrap_spans_to_lines(&mut current_spans, width) {
|
||||
lines.push(prefix_code_block_line(line));
|
||||
}
|
||||
}
|
||||
lines.push(Line::from(Span::styled("└────", code_style())));
|
||||
in_code_block = false;
|
||||
code_block_language.clear();
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Event::Text(t) => {
|
||||
if in_code_block {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
current_spans.push(Span::styled(t.to_string(), code_style()));
|
||||
} else {
|
||||
current_text.push_str(&t);
|
||||
}
|
||||
}
|
||||
Event::Code(c) => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
current_spans.push(Span::styled(c.to_string(), code_style()));
|
||||
}
|
||||
Event::Html(h) | Event::InlineHtml(h) => {
|
||||
current_text.push_str(&h);
|
||||
}
|
||||
Event::SoftBreak | Event::HardBreak => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
wrap_spans(&mut lines, &mut current_spans, width);
|
||||
}
|
||||
Event::Rule => {
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
wrap_spans(&mut lines, &mut current_spans, width);
|
||||
lines.push(Line::from("─".repeat(width as usize)));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
flush(&mut current_text, &mut current_spans, in_bold, in_italic);
|
||||
if in_code_block {
|
||||
for line in wrap_spans_to_lines(&mut current_spans, width) {
|
||||
lines.push(prefix_code_block_line(line));
|
||||
}
|
||||
lines.push(Line::from(Span::styled("└────", code_style())));
|
||||
} else {
|
||||
wrap_spans(&mut lines, &mut current_spans, width);
|
||||
}
|
||||
|
||||
if lines.is_empty() {
|
||||
lines.push(Line::default());
|
||||
}
|
||||
lines
|
||||
}
|
||||
|
||||
fn heading_level_from(level: HeadingLevel) -> u8 {
|
||||
match level {
|
||||
HeadingLevel::H1 => 1,
|
||||
HeadingLevel::H2 => 2,
|
||||
HeadingLevel::H3 => 3,
|
||||
HeadingLevel::H4 => 4,
|
||||
HeadingLevel::H5 => 5,
|
||||
HeadingLevel::H6 => 6,
|
||||
}
|
||||
}
|
||||
|
||||
fn base_style(bold: bool, italic: bool) -> Style {
|
||||
let mut style = Style::new();
|
||||
if bold {
|
||||
style = style.add_modifier(Modifier::BOLD);
|
||||
}
|
||||
if italic {
|
||||
style = style.add_modifier(Modifier::ITALIC);
|
||||
}
|
||||
style
|
||||
}
|
||||
|
||||
fn code_style() -> Style {
|
||||
Style::new().fg(Color::Yellow)
|
||||
}
|
||||
|
||||
fn prefix_code_block_line(line: Line<'static>) -> Line<'static> {
|
||||
let mut spans = vec![Span::styled("│ ", code_style())];
|
||||
spans.extend(line.spans);
|
||||
Line::from(spans)
|
||||
}
|
||||
|
||||
/// Flush accumulated spans into `lines`, wrapping to `width`.
|
||||
fn wrap_spans(lines: &mut Vec<Line<'static>>, spans: &mut Vec<Span<'static>>, width: u16) {
|
||||
if spans.is_empty() {
|
||||
return;
|
||||
}
|
||||
for line in wrap_spans_to_lines(spans, width) {
|
||||
lines.push(line);
|
||||
}
|
||||
spans.clear();
|
||||
}
|
||||
|
||||
fn wrap_spans_to_lines(spans: &mut Vec<Span<'static>>, width: u16) -> Vec<Line<'static>> {
|
||||
let width = width.max(1) as usize;
|
||||
let mut out: Vec<Line<'static>> = Vec::new();
|
||||
let mut current_line: Vec<Span<'static>> = Vec::new();
|
||||
let mut current_width = 0usize;
|
||||
|
||||
for span in spans.drain(..) {
|
||||
for word in span.content.split(' ') {
|
||||
let word_width = word.chars().count();
|
||||
let sep = if current_width == 0 { 0 } else { 1 };
|
||||
if current_width + sep + word_width > width && current_width > 0 {
|
||||
out.push(Line::from(std::mem::take(&mut current_line)));
|
||||
current_width = 0;
|
||||
}
|
||||
if current_width > 0 {
|
||||
current_line.push(Span::styled(" ", span.style));
|
||||
current_width += 1;
|
||||
}
|
||||
current_line.push(Span::styled(word.to_string(), span.style));
|
||||
current_width += word_width;
|
||||
}
|
||||
}
|
||||
|
||||
if !current_line.is_empty() {
|
||||
out.push(Line::from(current_line));
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn renders_heading_and_bold() {
|
||||
let lines = render_markdown("# Hello\n\n**bold** text", 40);
|
||||
assert!(!lines.is_empty());
|
||||
let first: String = lines[0]
|
||||
.spans
|
||||
.iter()
|
||||
.map(|s| s.content.to_string())
|
||||
.collect();
|
||||
assert!(first.contains("# Hello"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn wraps_long_paragraphs() {
|
||||
let text = "a ".repeat(50);
|
||||
let lines = render_markdown(&text, 20);
|
||||
assert!(lines.len() > 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
//! Modal helpers for permission dialogs and the session picker.
|
||||
//!
|
||||
//! Rendering lives in `render.rs`; key handling lives in `input.rs`. This module
|
||||
//! provides shared utilities for modal state transitions.
|
||||
|
||||
use harness_app::EngineHandle;
|
||||
use harness_core::permission::PermissionReply;
|
||||
use harness_core::types::Session;
|
||||
|
||||
use crate::state::{AppState, ModalState};
|
||||
|
||||
/// Resolve the current permission request with `reply`.
|
||||
pub fn resolve_permission(state: &mut AppState, engine: &EngineHandle, reply: PermissionReply) {
|
||||
state.handle_permission_reply(reply, engine);
|
||||
}
|
||||
|
||||
/// Open the session picker with the supplied sessions.
|
||||
pub fn open_session_picker(state: &mut AppState, sessions: Vec<Session>) {
|
||||
state.open_session_picker(sessions);
|
||||
}
|
||||
|
||||
/// Load a session's messages and parts into state.
|
||||
pub async fn select_session(
|
||||
state: &mut AppState,
|
||||
engine: &EngineHandle,
|
||||
session: Session,
|
||||
) -> Result<(), harness_app::AppError> {
|
||||
let session_id = session.id.clone();
|
||||
let messages = engine.session_messages(session_id).await?;
|
||||
let mut all_parts = Vec::new();
|
||||
for message in &messages {
|
||||
let parts = engine.message_parts(message.id.clone()).await?;
|
||||
all_parts.extend(parts);
|
||||
}
|
||||
state.set_session(session, messages, all_parts);
|
||||
state.modal = ModalState::None;
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,511 @@
|
||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||
use ratatui::style::{Color, Modifier, Style};
|
||||
use ratatui::text::{Line, Span};
|
||||
use ratatui::widgets::{Block, Borders, Clear, Paragraph, Wrap};
|
||||
use ratatui::Frame;
|
||||
|
||||
use crate::markdown::render_markdown;
|
||||
use crate::state::{AppState, ModalState, PartView, ToolStateView};
|
||||
use harness_core::types::Role;
|
||||
|
||||
/// Render the full UI into the terminal frame.
|
||||
pub fn render(frame: &mut Frame, state: &mut AppState) {
|
||||
let area = frame.area();
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Length(1),
|
||||
Constraint::Min(3),
|
||||
Constraint::Length(3),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.split(area);
|
||||
|
||||
render_header(frame, state, chunks[0]);
|
||||
render_chat(frame, state, chunks[1]);
|
||||
render_input(frame, state, chunks[2]);
|
||||
render_status(frame, state, chunks[3]);
|
||||
|
||||
match &state.modal {
|
||||
ModalState::Permission { requests } => {
|
||||
render_permission_modal(frame, &requests[0], area);
|
||||
}
|
||||
ModalState::SessionPicker { sessions, selected } => {
|
||||
render_session_picker(frame, sessions, *selected, area);
|
||||
}
|
||||
ModalState::None => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn render_header(frame: &mut Frame, state: &AppState, area: Rect) {
|
||||
let title = if state.session_title.is_empty() {
|
||||
"new session"
|
||||
} else {
|
||||
state.session_title.as_str()
|
||||
};
|
||||
let text = format!("{} · {} · {}", title, state.agent, state.model_ref);
|
||||
let paragraph = Paragraph::new(text)
|
||||
.style(Style::new().add_modifier(Modifier::BOLD))
|
||||
.alignment(ratatui::layout::Alignment::Center);
|
||||
frame.render_widget(paragraph, area);
|
||||
}
|
||||
|
||||
fn render_chat(frame: &mut Frame, state: &mut AppState, area: Rect) {
|
||||
let effective_width = area.width.saturating_sub(4).max(1);
|
||||
// Cached lines are wrapped to a specific width, so a resize must drop them.
|
||||
if state.render_width != effective_width {
|
||||
state.invalidate_caches();
|
||||
state.render_width = effective_width;
|
||||
}
|
||||
|
||||
let mut lines: Vec<Line<'static>> = Vec::new();
|
||||
for message in &mut state.messages {
|
||||
let role_style = match message.role {
|
||||
Role::User => Style::new().fg(Color::Cyan),
|
||||
Role::Assistant => Style::new(),
|
||||
};
|
||||
let prefix = match message.role {
|
||||
Role::User => "> ",
|
||||
Role::Assistant => "",
|
||||
};
|
||||
let mut first = true;
|
||||
for part in &mut message.parts {
|
||||
let part_lines = render_part(part, effective_width, role_style, prefix, first);
|
||||
lines.extend(part_lines);
|
||||
first = false;
|
||||
}
|
||||
lines.push(Line::default());
|
||||
}
|
||||
|
||||
let paragraph = Paragraph::new(lines)
|
||||
.block(Block::default().borders(Borders::ALL).title(" chat "))
|
||||
.scroll((state.scroll_offset, 0));
|
||||
frame.render_widget(paragraph, area);
|
||||
}
|
||||
|
||||
fn render_part(
|
||||
part: &mut PartView,
|
||||
effective_width: u16,
|
||||
role_style: Style,
|
||||
prefix: &'static str,
|
||||
first: bool,
|
||||
) -> Vec<Line<'static>> {
|
||||
match part {
|
||||
PartView::Text {
|
||||
text, cached_lines, ..
|
||||
} => {
|
||||
// Cache the prefix-free markdown render; the `> ` prefix is cheap to re-apply
|
||||
// to the clone each frame and would otherwise poison a shared cache.
|
||||
let base = cached_lines.get_or_insert_with(|| render_markdown(text, effective_width));
|
||||
let mut rendered = base.clone();
|
||||
if first && !prefix.is_empty() {
|
||||
if let Some(first_line) = rendered.first_mut() {
|
||||
let mut spans = vec![Span::styled(prefix, role_style)];
|
||||
spans.extend(first_line.spans.clone());
|
||||
*first_line = Line::from(spans);
|
||||
}
|
||||
}
|
||||
rendered
|
||||
}
|
||||
PartView::Reasoning { text, .. } => {
|
||||
let style = Style::new()
|
||||
.fg(Color::DarkGray)
|
||||
.add_modifier(Modifier::ITALIC);
|
||||
render_markdown(text, effective_width)
|
||||
.into_iter()
|
||||
.map(|line| {
|
||||
let mut spans = vec![Span::styled("🧠 ", style)];
|
||||
spans.extend(line.spans);
|
||||
Line::from(spans)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
PartView::Tool {
|
||||
name,
|
||||
state,
|
||||
cached_lines,
|
||||
..
|
||||
} => {
|
||||
if let Some(cached) = cached_lines {
|
||||
return cached.clone();
|
||||
}
|
||||
let status = state.status_label();
|
||||
let icon = match state {
|
||||
ToolStateView::Pending { .. } => "⏳",
|
||||
ToolStateView::Running { .. } => "⚙",
|
||||
ToolStateView::Completed { .. } => "✓",
|
||||
ToolStateView::Error { .. } => "✗",
|
||||
};
|
||||
let title = format!("{icon} {name} — {status}");
|
||||
let mut lines = vec![Line::from(title)];
|
||||
match state {
|
||||
ToolStateView::Pending { partial_input } => {
|
||||
lines.extend(render_markdown(partial_input, effective_width));
|
||||
}
|
||||
ToolStateView::Completed { output, .. } => {
|
||||
lines.extend(render_markdown(output, effective_width));
|
||||
}
|
||||
ToolStateView::Error { error } => {
|
||||
lines.extend(render_markdown(error, effective_width));
|
||||
}
|
||||
ToolStateView::Running { .. } => {}
|
||||
}
|
||||
*cached_lines = Some(lines.clone());
|
||||
lines
|
||||
}
|
||||
PartView::StepFinish { .. } => vec![Line::from("─── step ───")],
|
||||
}
|
||||
}
|
||||
|
||||
fn render_input(frame: &mut Frame, state: &mut AppState, area: Rect) {
|
||||
let block = Block::default().borders(Borders::ALL).title(" input ");
|
||||
state.input.set_block(block);
|
||||
frame.render_widget(&state.input, area);
|
||||
}
|
||||
|
||||
fn render_status(frame: &mut Frame, state: &AppState, area: Rect) {
|
||||
let spinner = if state.running { "⠋ " } else { "" };
|
||||
let status = if state.running { "running" } else { "idle" };
|
||||
let hints = if state.ctrl_c_pressed {
|
||||
"Press Ctrl+C again to quit"
|
||||
} else {
|
||||
"Ctrl+S: sessions | Esc: abort | Ctrl+C: quit"
|
||||
};
|
||||
let usage = format!(
|
||||
"{} · ${:.4}",
|
||||
format_tokens(state.session_tokens),
|
||||
state.session_cost
|
||||
);
|
||||
let text = format!("{spinner}{status} · {usage} · {hints}");
|
||||
let paragraph = Paragraph::new(text).style(Style::new().fg(Color::Gray));
|
||||
frame.render_widget(paragraph, area);
|
||||
}
|
||||
|
||||
/// Compact token count: `1234` → `1.2k`, `2_000_000` → `2.0M`.
|
||||
fn format_tokens(n: u64) -> String {
|
||||
match n {
|
||||
0..=999 => format!("{n} tok"),
|
||||
1_000..=999_999 => format!("{:.1}k tok", n as f64 / 1_000.0),
|
||||
_ => format!("{:.1}M tok", n as f64 / 1_000_000.0),
|
||||
}
|
||||
}
|
||||
|
||||
fn render_permission_modal(
|
||||
frame: &mut Frame,
|
||||
request: &harness_core::event::PermissionRequest,
|
||||
area: Rect,
|
||||
) {
|
||||
let popup = centered_rect(60, 60, area);
|
||||
frame.render_widget(Clear, popup);
|
||||
|
||||
let text = vec![
|
||||
Line::from("Permission required").style(Style::new().add_modifier(Modifier::BOLD)),
|
||||
Line::default(),
|
||||
Line::from(format!("permission: {}", request.permission)),
|
||||
Line::from(format!("pattern: {}", request.pattern)),
|
||||
Line::default(),
|
||||
Line::from("y: allow once a: allow always n: reject"),
|
||||
];
|
||||
|
||||
let block = Block::default().borders(Borders::ALL).title(" permission ");
|
||||
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: true });
|
||||
frame.render_widget(paragraph, popup);
|
||||
}
|
||||
|
||||
fn render_session_picker(
|
||||
frame: &mut Frame,
|
||||
sessions: &[harness_core::types::Session],
|
||||
selected: usize,
|
||||
area: Rect,
|
||||
) {
|
||||
let popup = centered_rect(60, 60, area);
|
||||
frame.render_widget(Clear, popup);
|
||||
|
||||
let mut text: Vec<Line<'static>> =
|
||||
vec![Line::from("Select session").style(Style::new().add_modifier(Modifier::BOLD))];
|
||||
for (i, session) in sessions.iter().enumerate() {
|
||||
let marker = if i == selected { "> " } else { " " };
|
||||
let line = format!(
|
||||
"{}{} · {} · {}/{}",
|
||||
marker, session.id, session.agent, session.model.provider_id, session.model.model_id
|
||||
);
|
||||
let style = if i == selected {
|
||||
Style::new().bg(Color::Blue).fg(Color::White)
|
||||
} else {
|
||||
Style::new()
|
||||
};
|
||||
text.push(Line::from(Span::styled(line, style)));
|
||||
}
|
||||
|
||||
let block = Block::default().borders(Borders::ALL).title(" sessions ");
|
||||
let paragraph = Paragraph::new(text).block(block).wrap(Wrap { trim: false });
|
||||
frame.render_widget(paragraph, popup);
|
||||
}
|
||||
|
||||
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
||||
let popup_layout = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
Constraint::Percentage(percent_y),
|
||||
Constraint::Percentage((100 - percent_y) / 2),
|
||||
])
|
||||
.split(r);
|
||||
|
||||
Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
Constraint::Percentage(percent_x),
|
||||
Constraint::Percentage((100 - percent_x) / 2),
|
||||
])
|
||||
.split(popup_layout[1])[1]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ratatui::backend::TestBackend;
|
||||
use ratatui::Terminal;
|
||||
|
||||
use super::*;
|
||||
use crate::state::{AppState, MessageView, PartView, ToolStateView};
|
||||
use harness_core::event::PermissionRequest;
|
||||
use harness_core::types::{MessageId, ModelRef, PartId, Role, Session, SessionId};
|
||||
|
||||
fn buffer_to_string(backend: &TestBackend) -> String {
|
||||
let buffer = backend.buffer();
|
||||
let area = buffer.area;
|
||||
let mut result = String::new();
|
||||
for y in 0..area.height {
|
||||
for x in 0..area.width {
|
||||
result.push_str(buffer[(x, y)].symbol());
|
||||
}
|
||||
while result.ends_with(' ') {
|
||||
result.pop();
|
||||
}
|
||||
result.push('\n');
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_empty_session() {
|
||||
let backend = TestBackend::new(80, 24);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
let mut state = AppState::new(
|
||||
"anthropic/claude-sonnet-4-5".to_string(),
|
||||
"orchestrator".to_string(),
|
||||
);
|
||||
terminal.draw(|frame| render(frame, &mut state)).unwrap();
|
||||
insta::assert_snapshot!(buffer_to_string(terminal.backend()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_user_and_assistant_messages() {
|
||||
let backend = TestBackend::new(80, 24);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
let mut state = AppState::new(
|
||||
"anthropic/claude-sonnet-4-5".to_string(),
|
||||
"orchestrator".to_string(),
|
||||
);
|
||||
state.session_id = Some(SessionId("ses_test_001".to_string()));
|
||||
state.messages.push(MessageView {
|
||||
id: MessageId("msg_test_001".to_string()),
|
||||
role: Role::User,
|
||||
parts: vec![PartView::Text {
|
||||
id: PartId("prt_test_001".to_string()),
|
||||
text: "Hello, can you read foo.txt?".to_string(),
|
||||
cached_lines: None,
|
||||
}],
|
||||
});
|
||||
state.messages.push(MessageView {
|
||||
id: MessageId("msg_test_002".to_string()),
|
||||
role: Role::Assistant,
|
||||
parts: vec![PartView::Text {
|
||||
id: PartId("prt_test_002".to_string()),
|
||||
text: "I'll read that file for you.\n\n```rust\nlet x = 42;\n```".to_string(),
|
||||
cached_lines: None,
|
||||
}],
|
||||
});
|
||||
terminal.draw(|frame| render(frame, &mut state)).unwrap();
|
||||
insta::assert_snapshot!(buffer_to_string(terminal.backend()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_tool_card_completed() {
|
||||
let backend = TestBackend::new(80, 24);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
let mut state = AppState::new(
|
||||
"anthropic/claude-sonnet-4-5".to_string(),
|
||||
"orchestrator".to_string(),
|
||||
);
|
||||
state.session_id = Some(SessionId("ses_test_001".to_string()));
|
||||
state.messages.push(MessageView {
|
||||
id: MessageId("msg_test_003".to_string()),
|
||||
role: Role::Assistant,
|
||||
parts: vec![PartView::Tool {
|
||||
id: PartId("prt_test_003".to_string()),
|
||||
name: "read".to_string(),
|
||||
state: ToolStateView::Completed {
|
||||
title: "read foo.txt".to_string(),
|
||||
output: "file contents here".to_string(),
|
||||
},
|
||||
cached_lines: None,
|
||||
}],
|
||||
});
|
||||
terminal.draw(|frame| render(frame, &mut state)).unwrap();
|
||||
insta::assert_snapshot!(buffer_to_string(terminal.backend()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_tool_card_running() {
|
||||
let backend = TestBackend::new(80, 24);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
let mut state = AppState::new(
|
||||
"anthropic/claude-sonnet-4-5".to_string(),
|
||||
"orchestrator".to_string(),
|
||||
);
|
||||
state.session_id = Some(SessionId("ses_test_001".to_string()));
|
||||
state.messages.push(MessageView {
|
||||
id: MessageId("msg_test_004".to_string()),
|
||||
role: Role::Assistant,
|
||||
parts: vec![PartView::Tool {
|
||||
id: PartId("prt_test_004".to_string()),
|
||||
name: "read".to_string(),
|
||||
state: ToolStateView::Running {
|
||||
title: Some("read foo.txt".to_string()),
|
||||
},
|
||||
cached_lines: None,
|
||||
}],
|
||||
});
|
||||
terminal.draw(|frame| render(frame, &mut state)).unwrap();
|
||||
insta::assert_snapshot!(buffer_to_string(terminal.backend()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_tool_card_error() {
|
||||
let backend = TestBackend::new(80, 24);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
let mut state = AppState::new(
|
||||
"anthropic/claude-sonnet-4-5".to_string(),
|
||||
"orchestrator".to_string(),
|
||||
);
|
||||
state.session_id = Some(SessionId("ses_test_001".to_string()));
|
||||
state.messages.push(MessageView {
|
||||
id: MessageId("msg_test_005".to_string()),
|
||||
role: Role::Assistant,
|
||||
parts: vec![PartView::Tool {
|
||||
id: PartId("prt_test_005".to_string()),
|
||||
name: "read".to_string(),
|
||||
state: ToolStateView::Error {
|
||||
error: "file not found".to_string(),
|
||||
},
|
||||
cached_lines: None,
|
||||
}],
|
||||
});
|
||||
terminal.draw(|frame| render(frame, &mut state)).unwrap();
|
||||
insta::assert_snapshot!(buffer_to_string(terminal.backend()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_permission_modal() {
|
||||
let backend = TestBackend::new(80, 24);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
let mut state = AppState::new(
|
||||
"anthropic/claude-sonnet-4-5".to_string(),
|
||||
"orchestrator".to_string(),
|
||||
);
|
||||
let request = PermissionRequest {
|
||||
id: "test".to_string(),
|
||||
session_id: SessionId("ses_test_001".to_string()),
|
||||
permission: "bash".to_string(),
|
||||
pattern: "rm -rf /".to_string(),
|
||||
always_pattern: "rm *".to_string(),
|
||||
metadata: serde_json::Value::Null,
|
||||
};
|
||||
state.modal = ModalState::Permission {
|
||||
requests: vec![request],
|
||||
};
|
||||
terminal.draw(|frame| render(frame, &mut state)).unwrap();
|
||||
insta::assert_snapshot!(buffer_to_string(terminal.backend()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn snapshot_session_picker_modal() {
|
||||
let backend = TestBackend::new(80, 24);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
let mut state = AppState::new(
|
||||
"anthropic/claude-sonnet-4-5".to_string(),
|
||||
"orchestrator".to_string(),
|
||||
);
|
||||
let sessions = vec![
|
||||
Session {
|
||||
id: SessionId("ses_test_001".to_string()),
|
||||
parent_id: None,
|
||||
depth: 0,
|
||||
title: "Session One".to_string(),
|
||||
agent: "orchestrator".to_string(),
|
||||
model: ModelRef::new("anthropic", "claude-sonnet-4-5"),
|
||||
usage: harness_core::types::TokenUsage::default(),
|
||||
cost: 0.0,
|
||||
extra_rules: Vec::new(),
|
||||
created_at: 1,
|
||||
updated_at: 1,
|
||||
},
|
||||
Session {
|
||||
id: SessionId("ses_test_002".to_string()),
|
||||
parent_id: None,
|
||||
depth: 0,
|
||||
title: "Session Two".to_string(),
|
||||
agent: "coder".to_string(),
|
||||
model: ModelRef::new("openai", "gpt-4o"),
|
||||
usage: harness_core::types::TokenUsage::default(),
|
||||
cost: 0.0,
|
||||
extra_rules: Vec::new(),
|
||||
created_at: 2,
|
||||
updated_at: 2,
|
||||
},
|
||||
Session {
|
||||
id: SessionId("ses_test_003".to_string()),
|
||||
parent_id: None,
|
||||
depth: 0,
|
||||
title: "Session Three".to_string(),
|
||||
agent: "reviewer".to_string(),
|
||||
model: ModelRef::new("google", "gemini-2.5"),
|
||||
usage: harness_core::types::TokenUsage::default(),
|
||||
cost: 0.0,
|
||||
extra_rules: Vec::new(),
|
||||
created_at: 3,
|
||||
updated_at: 3,
|
||||
},
|
||||
];
|
||||
state.modal = ModalState::SessionPicker {
|
||||
sessions,
|
||||
selected: 1,
|
||||
};
|
||||
terminal.draw(|frame| render(frame, &mut state)).unwrap();
|
||||
insta::assert_snapshot!(buffer_to_string(terminal.backend()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_empty_state_produces_frame() {
|
||||
let backend = TestBackend::new(80, 24);
|
||||
let mut terminal = Terminal::new(backend).unwrap();
|
||||
let mut state = AppState::new("anthropic/claude".to_string(), "orchestrator".to_string());
|
||||
terminal.draw(|frame| render(frame, &mut state)).unwrap();
|
||||
let buffer = terminal.backend().buffer().clone();
|
||||
assert_eq!(buffer.area.width, 80);
|
||||
assert_eq!(buffer.area.height, 24);
|
||||
// Header should contain the agent/model line.
|
||||
let header_row: String = buffer
|
||||
.content
|
||||
.chunks(80)
|
||||
.next()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|c| c.symbol())
|
||||
.collect();
|
||||
assert!(header_row.contains("orchestrator"));
|
||||
assert!(header_row.contains("anthropic/claude"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
---
|
||||
source: crates/harness-tui/src/render.rs
|
||||
assertion_line: 300
|
||||
expression: buffer_to_string(terminal.backend())
|
||||
---
|
||||
new session · orchestrator · anthropic/claude-sonnet-4-5
|
||||
┌ chat ────────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
┌ input ───────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
idle · 0 tok · $0.0000 · Ctrl+S: sessions | Esc: abort | Ctrl+C: quit
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
---
|
||||
source: crates/harness-tui/src/render.rs
|
||||
assertion_line: 430
|
||||
expression: buffer_to_string(terminal.backend())
|
||||
---
|
||||
new session · orchestrator · anthropic/claude-sonnet-4-5
|
||||
┌ chat ────────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ ┌ permission ──────────────────────────────────┐ │
|
||||
│ │Permission required │ │
|
||||
│ │ │ │
|
||||
│ │permission: bash │ │
|
||||
│ │pattern: rm -rf / │ │
|
||||
│ │ │ │
|
||||
│ │y: allow once a: allow always n: reject │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ └──────────────────────────────────────────────┘ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
┌ input ───────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
idle · 0 tok · $0.0000 · Ctrl+S: sessions | Esc: abort | Ctrl+C: quit
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
---
|
||||
source: crates/harness-tui/src/render.rs
|
||||
assertion_line: 487
|
||||
expression: buffer_to_string(terminal.backend())
|
||||
---
|
||||
new session · orchestrator · anthropic/claude-sonnet-4-5
|
||||
┌ chat ────────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ ┌ sessions ────────────────────────────────────┐ │
|
||||
│ │Select session │ │
|
||||
│ │ ses_test_001 · orchestrator · │ │
|
||||
│ │anthropic/claude-sonnet-4-5 │ │
|
||||
│ │> ses_test_002 · coder · openai/gpt-4o │ │
|
||||
│ │ ses_test_003 · reviewer · google/gemini-2.5 │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ │ │ │
|
||||
│ └──────────────────────────────────────────────┘ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
┌ input ───────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
idle · 0 tok · $0.0000 · Ctrl+S: sessions | Esc: abort | Ctrl+C: quit
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
---
|
||||
source: crates/harness-tui/src/render.rs
|
||||
assertion_line: 357
|
||||
expression: buffer_to_string(terminal.backend())
|
||||
---
|
||||
new session · orchestrator · anthropic/claude-sonnet-4-5
|
||||
┌ chat ────────────────────────────────────────────────────────────────────────┐
|
||||
│✓ read — read foo.txt │
|
||||
│file contents here │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
┌ input ───────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
idle · 0 tok · $0.0000 · Ctrl+S: sessions | Esc: abort | Ctrl+C: quit
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
---
|
||||
source: crates/harness-tui/src/render.rs
|
||||
assertion_line: 407
|
||||
expression: buffer_to_string(terminal.backend())
|
||||
---
|
||||
new session · orchestrator · anthropic/claude-sonnet-4-5
|
||||
┌ chat ────────────────────────────────────────────────────────────────────────┐
|
||||
│✗ read — error: file not found │
|
||||
│file not found │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
┌ input ───────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
idle · 0 tok · $0.0000 · Ctrl+S: sessions | Esc: abort | Ctrl+C: quit
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
---
|
||||
source: crates/harness-tui/src/render.rs
|
||||
assertion_line: 382
|
||||
expression: buffer_to_string(terminal.backend())
|
||||
---
|
||||
new session · orchestrator · anthropic/claude-sonnet-4-5
|
||||
┌ chat ────────────────────────────────────────────────────────────────────────┐
|
||||
│⚙ read — read foo.txt │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
┌ input ───────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
idle · 0 tok · $0.0000 · Ctrl+S: sessions | Esc: abort | Ctrl+C: quit
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
---
|
||||
source: crates/harness-tui/src/render.rs
|
||||
assertion_line: 331
|
||||
expression: buffer_to_string(terminal.backend())
|
||||
---
|
||||
new session · orchestrator · anthropic/claude-sonnet-4-5
|
||||
┌ chat ────────────────────────────────────────────────────────────────────────┐
|
||||
│> Hello, can you read foo.txt? │
|
||||
│ │
|
||||
│I'll read that file for you. │
|
||||
│┌──── rust │
|
||||
││ let x = 42; │
|
||||
│└──── │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
┌ input ───────────────────────────────────────────────────────────────────────┐
|
||||
│ │
|
||||
└──────────────────────────────────────────────────────────────────────────────┘
|
||||
idle · 0 tok · $0.0000 · Ctrl+S: sessions | Esc: abort | Ctrl+C: quit
|
||||
@@ -0,0 +1,406 @@
|
||||
use harness_app::EngineHandle;
|
||||
use harness_core::event::{AppEvent, PermissionRequest, RunOutcome};
|
||||
use harness_core::permission::PermissionReply;
|
||||
use harness_core::types::{Message, Part, PartBody, PartId, Role, Session, SessionId, ToolState};
|
||||
use ratatui::text::Line;
|
||||
|
||||
/// Cached view of a message in the chat viewport.
|
||||
#[derive(Debug)]
|
||||
pub struct MessageView {
|
||||
pub id: harness_core::types::MessageId,
|
||||
pub role: Role,
|
||||
pub parts: Vec<PartView>,
|
||||
}
|
||||
|
||||
/// Cached view of a part. The optional `cached_lines` field stores a pre-rendered markdown
|
||||
/// representation; it is invalidated whenever the underlying text changes.
|
||||
#[derive(Debug)]
|
||||
pub enum PartView {
|
||||
Text {
|
||||
id: PartId,
|
||||
text: String,
|
||||
cached_lines: Option<Vec<Line<'static>>>,
|
||||
},
|
||||
Reasoning {
|
||||
id: PartId,
|
||||
text: String,
|
||||
},
|
||||
Tool {
|
||||
id: PartId,
|
||||
name: String,
|
||||
state: ToolStateView,
|
||||
cached_lines: Option<Vec<Line<'static>>>,
|
||||
},
|
||||
StepFinish {
|
||||
id: PartId,
|
||||
},
|
||||
}
|
||||
|
||||
/// Simplified tool state for rendering.
|
||||
#[derive(Debug)]
|
||||
pub enum ToolStateView {
|
||||
Pending { partial_input: String },
|
||||
Running { title: Option<String> },
|
||||
Completed { title: String, output: String },
|
||||
Error { error: String },
|
||||
}
|
||||
|
||||
impl ToolStateView {
|
||||
fn from_tool_state(state: &ToolState) -> Self {
|
||||
match state {
|
||||
ToolState::Pending { partial_input } => Self::Pending {
|
||||
partial_input: partial_input.clone(),
|
||||
},
|
||||
ToolState::Running { title, .. } => Self::Running {
|
||||
title: title.clone(),
|
||||
},
|
||||
ToolState::Completed { title, output, .. } => Self::Completed {
|
||||
title: title.clone(),
|
||||
output: output.clone(),
|
||||
},
|
||||
ToolState::Error { error, .. } => Self::Error {
|
||||
error: error.clone(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status_label(&self) -> String {
|
||||
match self {
|
||||
Self::Pending { .. } => "pending".to_string(),
|
||||
Self::Running { title } => title.clone().unwrap_or_else(|| "running".to_string()),
|
||||
Self::Completed { title, .. } => title.clone(),
|
||||
Self::Error { error } => format!("error: {error}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Active modal overlay state.
|
||||
#[derive(Debug, Default)]
|
||||
pub enum ModalState {
|
||||
#[default]
|
||||
None,
|
||||
Permission {
|
||||
requests: Vec<PermissionRequest>,
|
||||
},
|
||||
SessionPicker {
|
||||
sessions: Vec<Session>,
|
||||
selected: usize,
|
||||
},
|
||||
}
|
||||
|
||||
/// All mutable UI state lives here.
|
||||
#[derive(Debug)]
|
||||
pub struct AppState {
|
||||
pub session_id: Option<SessionId>,
|
||||
pub session_title: String,
|
||||
pub messages: Vec<MessageView>,
|
||||
pub modal: ModalState,
|
||||
pub input: tui_textarea::TextArea<'static>,
|
||||
pub scroll_offset: u16,
|
||||
pub running: bool,
|
||||
pub model_ref: String,
|
||||
pub agent: String,
|
||||
pub quit: bool,
|
||||
pub dirty: bool,
|
||||
pub ctrl_c_pressed: bool,
|
||||
/// Width the currently cached part lines were wrapped to; a change invalidates them.
|
||||
pub render_width: u16,
|
||||
/// Accumulated session cost in USD, from `SessionCreated`/`SessionUpdated` events.
|
||||
pub session_cost: f64,
|
||||
/// Accumulated session tokens (input + output), for the status bar.
|
||||
pub session_tokens: u64,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new(model_ref: String, agent: String) -> Self {
|
||||
let mut input = tui_textarea::TextArea::default();
|
||||
input.set_cursor_line_style(ratatui::style::Style::default());
|
||||
Self {
|
||||
session_id: None,
|
||||
session_title: String::new(),
|
||||
messages: Vec::new(),
|
||||
modal: ModalState::None,
|
||||
input,
|
||||
scroll_offset: 0,
|
||||
running: false,
|
||||
model_ref,
|
||||
agent,
|
||||
quit: false,
|
||||
dirty: true,
|
||||
ctrl_c_pressed: false,
|
||||
render_width: 0,
|
||||
session_cost: 0.0,
|
||||
session_tokens: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Drop every part's cached render (e.g. after a resize changes the wrap width).
|
||||
pub fn invalidate_caches(&mut self) {
|
||||
for message in &mut self.messages {
|
||||
for part in &mut message.parts {
|
||||
match part {
|
||||
PartView::Text { cached_lines, .. } | PartView::Tool { cached_lines, .. } => {
|
||||
*cached_lines = None
|
||||
}
|
||||
PartView::Reasoning { .. } | PartView::StepFinish { .. } => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_session(&mut self, session: Session, messages: Vec<Message>, parts: Vec<Part>) {
|
||||
self.session_id = Some(session.id.clone());
|
||||
self.session_title = session.title;
|
||||
self.agent = session.agent;
|
||||
self.model_ref = format!("{}/{}", session.model.provider_id, session.model.model_id);
|
||||
self.session_cost = session.cost;
|
||||
self.session_tokens = session.usage.input + session.usage.output;
|
||||
self.messages.clear();
|
||||
|
||||
let mut messages = messages;
|
||||
messages.sort_by_key(|m| m.created_at);
|
||||
for message in messages {
|
||||
let message_parts: Vec<Part> = parts
|
||||
.iter()
|
||||
.filter(|p| p.message_id == message.id)
|
||||
.cloned()
|
||||
.collect();
|
||||
self.messages.push(message_view(message, message_parts));
|
||||
}
|
||||
self.scroll_offset = 0;
|
||||
self.dirty = true;
|
||||
}
|
||||
|
||||
pub fn apply_event(&mut self, event: AppEvent) {
|
||||
match event {
|
||||
AppEvent::SessionCreated { session } | AppEvent::SessionUpdated { session } => {
|
||||
if self.session_id.as_ref() == Some(&session.id) {
|
||||
self.session_title = session.title;
|
||||
self.agent = session.agent;
|
||||
self.model_ref =
|
||||
format!("{}/{}", session.model.provider_id, session.model.model_id);
|
||||
self.session_cost = session.cost;
|
||||
self.session_tokens = session.usage.input + session.usage.output;
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
AppEvent::MessageCreated { message } => {
|
||||
if self.session_id.as_ref() == Some(&message.session_id) {
|
||||
self.messages.push(message_view(message, Vec::new()));
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
AppEvent::MessageUpdated { message } => {
|
||||
if let Some(view) = self.messages.iter_mut().find(|m| m.id == message.id) {
|
||||
view.role = message.role;
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
AppEvent::PartUpdated { part } => {
|
||||
if self.session_id.as_ref() == Some(&part.session_id) {
|
||||
self.update_or_insert_part(part);
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
AppEvent::PartDelta {
|
||||
part_id,
|
||||
message_id,
|
||||
delta,
|
||||
} => {
|
||||
self.apply_delta(part_id, message_id, delta);
|
||||
self.dirty = true;
|
||||
}
|
||||
AppEvent::RunStarted { session_id } => {
|
||||
if self.session_id.as_ref() == Some(&session_id) {
|
||||
self.running = true;
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
AppEvent::RunFinished {
|
||||
session_id,
|
||||
outcome,
|
||||
} => {
|
||||
if self.session_id.as_ref() == Some(&session_id) {
|
||||
self.running = false;
|
||||
if matches!(outcome, RunOutcome::Errored { .. }) {
|
||||
self.messages.push(MessageView {
|
||||
id: harness_core::types::MessageId::new(),
|
||||
role: Role::Assistant,
|
||||
parts: vec![PartView::Text {
|
||||
id: PartId::new(),
|
||||
text: match outcome {
|
||||
RunOutcome::Errored { message } => message,
|
||||
_ => String::new(),
|
||||
},
|
||||
cached_lines: None,
|
||||
}],
|
||||
});
|
||||
}
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
AppEvent::PermissionAsked { request } => {
|
||||
if let ModalState::Permission { requests } = &mut self.modal {
|
||||
requests.push(request);
|
||||
} else {
|
||||
self.modal = ModalState::Permission {
|
||||
requests: vec![request],
|
||||
};
|
||||
}
|
||||
self.dirty = true;
|
||||
}
|
||||
AppEvent::PermissionResolved { id } => {
|
||||
if let ModalState::Permission { requests } = &mut self.modal {
|
||||
requests.retain(|r| r.id != id);
|
||||
if requests.is_empty() {
|
||||
self.modal = ModalState::None;
|
||||
}
|
||||
}
|
||||
self.dirty = true;
|
||||
}
|
||||
AppEvent::JobUpdated { .. }
|
||||
| AppEvent::AuthPrompt { .. }
|
||||
| AppEvent::ServerNotice { .. } => {
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn scroll_up(&mut self, n: u16) {
|
||||
self.scroll_offset = self.scroll_offset.saturating_sub(n);
|
||||
self.dirty = true;
|
||||
}
|
||||
|
||||
pub fn scroll_down(&mut self, n: u16) {
|
||||
self.scroll_offset = self.scroll_offset.saturating_add(n);
|
||||
self.dirty = true;
|
||||
}
|
||||
|
||||
pub fn handle_permission_reply(&mut self, reply: PermissionReply, engine: &EngineHandle) {
|
||||
let request_id = if let ModalState::Permission { requests } = &self.modal {
|
||||
requests.first().map(|r| r.id.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if let Some(id) = request_id {
|
||||
engine.permission_reply(&id, reply);
|
||||
if let ModalState::Permission { requests } = &mut self.modal {
|
||||
requests.retain(|r| r.id != id);
|
||||
if requests.is_empty() {
|
||||
self.modal = ModalState::None;
|
||||
}
|
||||
}
|
||||
self.dirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open_session_picker(&mut self, sessions: Vec<Session>) {
|
||||
self.modal = ModalState::SessionPicker {
|
||||
sessions,
|
||||
selected: 0,
|
||||
};
|
||||
self.dirty = true;
|
||||
}
|
||||
|
||||
pub fn close_modal(&mut self) {
|
||||
self.modal = ModalState::None;
|
||||
self.dirty = true;
|
||||
}
|
||||
|
||||
fn update_or_insert_part(&mut self, part: Part) {
|
||||
let target_id = part.id.clone();
|
||||
let Some(message_view) = self.messages.iter_mut().find(|m| m.id == part.message_id) else {
|
||||
return;
|
||||
};
|
||||
|
||||
let new_view = part_view(part);
|
||||
if let Some(existing) = message_view
|
||||
.parts
|
||||
.iter_mut()
|
||||
.find(|p| view_part_id(p) == target_id)
|
||||
{
|
||||
*existing = new_view;
|
||||
} else {
|
||||
message_view.parts.push(new_view);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_delta(
|
||||
&mut self,
|
||||
target_id: PartId,
|
||||
message_id: harness_core::types::MessageId,
|
||||
delta: String,
|
||||
) {
|
||||
let Some(message_view) = self.messages.iter_mut().find(|m| m.id == message_id) else {
|
||||
return;
|
||||
};
|
||||
|
||||
for part in &mut message_view.parts {
|
||||
if view_part_id(part) == target_id {
|
||||
match part {
|
||||
PartView::Text {
|
||||
text, cached_lines, ..
|
||||
} => {
|
||||
text.push_str(&delta);
|
||||
*cached_lines = None;
|
||||
}
|
||||
PartView::Reasoning { text, .. } => {
|
||||
text.push_str(&delta);
|
||||
}
|
||||
PartView::Tool { cached_lines, .. } => {
|
||||
*cached_lines = None;
|
||||
}
|
||||
PartView::StepFinish { .. } => {}
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn message_view(message: Message, mut parts: Vec<Part>) -> MessageView {
|
||||
parts.sort_by_key(|p| p.idx);
|
||||
MessageView {
|
||||
id: message.id,
|
||||
role: message.role,
|
||||
parts: parts.into_iter().map(part_view).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn part_view(part: Part) -> PartView {
|
||||
match part.body {
|
||||
PartBody::Text { text, .. } => PartView::Text {
|
||||
id: part.id,
|
||||
text,
|
||||
cached_lines: None,
|
||||
},
|
||||
PartBody::Reasoning { text, .. } => PartView::Reasoning { id: part.id, text },
|
||||
PartBody::Tool { name, state, .. } => PartView::Tool {
|
||||
id: part.id,
|
||||
name,
|
||||
state: ToolStateView::from_tool_state(&state),
|
||||
cached_lines: None,
|
||||
},
|
||||
PartBody::StepStart | PartBody::StepFinish { .. } => PartView::StepFinish { id: part.id },
|
||||
PartBody::Subtask { description, .. } => PartView::Text {
|
||||
id: part.id,
|
||||
text: description,
|
||||
cached_lines: None,
|
||||
},
|
||||
PartBody::Compaction { summary, .. } => PartView::Text {
|
||||
id: part.id,
|
||||
text: format!("_Compaction summary: {summary}_"),
|
||||
cached_lines: None,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn view_part_id(part: &PartView) -> PartId {
|
||||
match part {
|
||||
PartView::Text { id, .. }
|
||||
| PartView::Reasoning { id, .. }
|
||||
| PartView::Tool { id, .. }
|
||||
| PartView::StepFinish { id } => id.clone(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
use std::io;
|
||||
use std::panic;
|
||||
|
||||
use crossterm::cursor::Show;
|
||||
use crossterm::execute;
|
||||
use crossterm::terminal::{
|
||||
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
||||
};
|
||||
|
||||
/// Restores the terminal when dropped or on panic.
|
||||
pub struct TerminalGuard;
|
||||
|
||||
impl TerminalGuard {
|
||||
/// Enables raw mode, enters the alternate screen, and installs a panic hook
|
||||
/// that restores the terminal before printing panic info.
|
||||
pub fn enter() -> io::Result<Self> {
|
||||
enable_raw_mode()?;
|
||||
let mut stdout = io::stdout();
|
||||
execute!(stdout, EnterAlternateScreen, Show)?;
|
||||
|
||||
let original_hook = panic::take_hook();
|
||||
panic::set_hook(Box::new(move |info| {
|
||||
let _ = restore();
|
||||
original_hook(info);
|
||||
}));
|
||||
|
||||
Ok(Self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TerminalGuard {
|
||||
fn drop(&mut self) {
|
||||
let _ = restore();
|
||||
}
|
||||
}
|
||||
|
||||
fn restore() -> io::Result<()> {
|
||||
disable_raw_mode()?;
|
||||
let mut stdout = io::stdout();
|
||||
execute!(stdout, LeaveAlternateScreen, Show)
|
||||
}
|
||||
Reference in New Issue
Block a user