README Source History Refs Reviews Compare Notes Search Review guide Give the skeleton the vocabulary its seams are written in (#131) ac7bfdc350f6cad7aa8506dbc9235ce787fd3e62
Oliver Jan Krylow <oliver@bugabinga.net > · 26 Jul 2026
SSH signature · not checked
Browse files Compare with HEAD
Walking-skeleton steps 3 through 7 all speak in the same shapes — a message, a
provider event, a tool result — and none of them could start while those shapes
did not exist. This defines exactly the §5.1-§5.4 and §5.6 surface the skeleton
consumes and nothing else; the Lua runtime, model resolution, and the full error
taxonomy stay unwritten until the slices that own them are built against.
`AgentTool::execute` returns a boxed future rather than reading as an `async
fn`, because the registry stores tools as shared handles by name (§6.10) so the
trait must survive `dyn`, and the usual fix — an `async-trait` dependency — is
absent from the §2.3 registry, where adding a name is the spec owner's decision.
`Thinking.provider_metadata` got no transport invented for it either: §5.2's
stream is text-only at the reasoning variant, so that gap is filed as #132.
`smith/src`, six modules, 20 tests covering the CBOR and JSON round-trips the
session codec depends on.
Closes #119 Changed files (10) Diff layout Auto Unified Split
+319 / −1 text lines
8 + source = "registry+https://github.com/rust-lang/crates.io-index" 9 + checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" 14 + source = "registry+https://github.com/rust-lang/crates.io-index" 15 + checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" 20 + source = "registry+https://github.com/rust-lang/crates.io-index" 21 + checksum = "42e69ffd6f0917f5c029256a24d0161db17cea3997d185db0d35926308770f0e" 31 + source = "registry+https://github.com/rust-lang/crates.io-index" 32 + checksum = "05afea1e0a06c9be33d539b876f1ce3692f4afea2cb41f740e7743225ed1c757" 37 + source = "registry+https://github.com/rust-lang/crates.io-index" 38 + checksum = "57663b653d948a338bfb3eeba9bb2fd5fcfaecb9e199e87e1eda4d9e8b240fd9" 47 + source = "registry+https://github.com/rust-lang/crates.io-index" 48 + checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" 53 + source = "registry+https://github.com/rust-lang/crates.io-index" 54 + checksum = "a88cf1f829d945f548cf8fec32c61b1f202b6d93b45848602fc02af4b12ad218" 66 + name = "futures-channel" 68 + source = "registry+https://github.com/rust-lang/crates.io-index" 69 + checksum = "262590f4fe6afeb0bc83be1daa64e52657fe185690a958af7f3ad0e92085c5ae" 78 + source = "registry+https://github.com/rust-lang/crates.io-index" 79 + checksum = "2cd50c473c80f6d7c3670a752354b8e569b1a7cbfdc0419ec88e5edad85e0dc7" 82 + name = "futures-executor" 84 + source = "registry+https://github.com/rust-lang/crates.io-index" 85 + checksum = "6754879cc9f2c66f88c6e5c35344bb0bdb0708b0352b1201815667c7eabc7458" 95 + source = "registry+https://github.com/rust-lang/crates.io-index" 96 + checksum = "4577ecaa3c4f96589d473f679a71b596316f6641bc350038b962a5daf0085d7a" 99 + name = "futures-macro" 101 + source = "registry+https://github.com/rust-lang/crates.io-index" 102 + checksum = "2d6d3cde68c518367be28956066ddfef33813991b77a55005a69dae04bf3b10b" 110 + name = "futures-sink" 112 + source = "registry+https://github.com/rust-lang/crates.io-index" 113 + checksum = "e34418ac499d6305c2fb5ad0ed2f6ac998c5f8ca209b4510f7f94242c647e307" 116 + name = "futures-task" 118 + source = "registry+https://github.com/rust-lang/crates.io-index" 119 + checksum = "b231ed28831efb4a61a08580c4bc233ec56bc009f4cd8f52da2c3cb97df0c109" 122 + name = "futures-util" 124 + source = "registry+https://github.com/rust-lang/crates.io-index" 125 + checksum = "a77a90a256fce34da66415271e30f94ee91c57b04b8a2c042d9cf3220179deaa" 141 + source = "registry+https://github.com/rust-lang/crates.io-index" 142 + checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" 152 + source = "registry+https://github.com/rust-lang/crates.io-index" 153 + checksum = "6ea2d84b969582b4b1864a92dc5d27cd2b77b622a8d79306834f1be5ba20d84b" 5 160 [[package ]][[package ]]6 161 name = "itoa" name = "itoa" 7 162 version = "1.0.18" version = "1.0.18" 8 163 source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index" 9 164 checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
169 + source = "registry+https://github.com/rust-lang/crates.io-index" 170 + checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102" 180 + source = "registry+https://github.com/rust-lang/crates.io-index" 181 + checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" 11 183 [[package ]][[package ]]12 184 name = "memchr" name = "memchr" 13 185 version = "2.8.3" version = "2.8.3" 14 186 source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index" 15 187 checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
192 + source = "registry+https://github.com/rust-lang/crates.io-index" 193 + checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" 196 + name = "pin-project-lite" 198 + source = "registry+https://github.com/rust-lang/crates.io-index" 199 + checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" 17 201 [[package ]][[package ]]18 202 name = "proc-macro2" name = "proc-macro2" 19 203 version = "1.0.107" version = "1.0.107" 32 216 "proc-macro2" , "proc-macro2" ,
222 + source = "registry+https://github.com/rust-lang/crates.io-index" 223 + checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" 228 + source = "registry+https://github.com/rust-lang/crates.io-index" 229 + checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" 35 231 [[package ]][[package ]]36 232 name = "serde" name = "serde" 37 233 version = "1.0.229" version = "1.0.229" 39 235 checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" 40 236 dependencies = [dependencies = [41 237 "serde_core" , "serde_core" ,44 241 [[package ]][[package ]]58 255 dependencies = [dependencies = [59 256 "proc-macro2" , "proc-macro2" ,64 261 [[package ]][[package ]]
277 + source = "registry+https://github.com/rust-lang/crates.io-index" 278 + checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" 77 280 [[package ]][[package ]]78 281 name = "smith" name = "smith" 79 282 version = "0.1.0" version = "0.1.0" 81 292 [[package ]][[package ]]82 293 name = "smith-ai" name = "smith-ai" 98 309 name = "smith-tui" name = "smith-tui" 99 310 version = "0.1.0" version = "0.1.0"
315 + source = "registry+https://github.com/rust-lang/crates.io-index" 316 + checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" 101 323 [[package ]][[package ]]102 324 name = "syn" name = "syn" 103 325 version = "3.0.3" version = "3.0.3" 109 331 "unicode-ident" , "unicode-ident" ,
337 + source = "registry+https://github.com/rust-lang/crates.io-index" 338 + checksum = "09a43598840e33d5b0331f38c5e30d13bb11c11210a4b58f0d9b18a5a5eefcd9" 344 + name = "thiserror-impl" 346 + source = "registry+https://github.com/rust-lang/crates.io-index" 347 + checksum = "43cbfe0cf76104d42a574802844187e84a305e531ed54455f11fbde0f10541cd" 112 354 [[package ]][[package ]]113 355 name = "unicode-ident" name = "unicode-ident" 114 356 version = "1.0.24" version = "1.0.24" 115 357 source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index" 116 358 checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75" checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
363 + source = "registry+https://github.com/rust-lang/crates.io-index" 364 + checksum = "bf3923a6f5c4c6382e0b653c4117f48d631ea17f38ed86e2a828e6f7412f5239" 372 + name = "wasm-bindgen" 374 + source = "registry+https://github.com/rust-lang/crates.io-index" 375 + checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4" 380 + "wasm-bindgen-macro" ,381 + "wasm-bindgen-shared" ,385 + name = "wasm-bindgen-macro" 387 + source = "registry+https://github.com/rust-lang/crates.io-index" 388 + checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1" 391 + "wasm-bindgen-macro-support" ,395 + name = "wasm-bindgen-macro-support" 397 + source = "registry+https://github.com/rust-lang/crates.io-index" 398 + checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e" 404 + "wasm-bindgen-shared" ,408 + name = "wasm-bindgen-shared" 410 + source = "registry+https://github.com/rust-lang/crates.io-index" 411 + checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24" 118 416 [[package ]][[package ]]119 417 name = "xtask" name = "xtask" 120 418 version = "0.1.0" version = "0.1.0" 122 420 "serde_json" , "serde_json" ,
426 + source = "registry+https://github.com/rust-lang/crates.io-index" 427 + checksum = "b5a105cd7b140f6eeec8acff2ea38135d3cab283ada58540f629fe51e46696eb" 433 + name = "zerocopy-derive" 435 + source = "registry+https://github.com/rust-lang/crates.io-index" 436 + checksum = "0fe976fb70c78cd64cccfe3a6fc142244e8a77b70959b30faf9d0ac37ee228eb" 125 443 [[package ]][[package ]]126 444 name = "zmij" name = "zmij" 127 445 version = "1.0.23" version = "1.0.23" +12 / −0 text lines
22 22 version = "0.1.0" version = "0.1.0" 23 23 license = "Apache-2.0" license = "Apache-2.0"
29 + [workspace .dependencies ]30 + serde = { version = "1" , features = ["derive" ] }32 + uuid = { version = "1" , features = ["v7" ] }+12 / −0 text lines
8 8 path = "src/lib.rs" path = "src/lib.rs"
11 + serde = { workspace = true }12 + serde_json = { workspace = true }13 + uuid = { workspace = true }14 + futures = { workspace = true }15 + thiserror = { workspace = true }20 + ciborium = { workspace = true }11 23 workspace = true workspace = true new file mode 100644+71 / −0 text lines
9 + use serde::{Deserialize, Serialize};22 + #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]28 + pub tools: Vec <String >,34 + reason = "Test fixtures use unwrap so setup failures report their exact operation." 40 + fn a_partial_config_fills_from_defaults () {44 + serde_json::from_str(r #"{ "model": "anthropic/claude-sonnet-4" }"# ).unwrap ();45 + assert_eq! (config.model, "anthropic/claude-sonnet-4" );46 + assert! (config.tools.is_empty ());50 + fn an_unknown_top_level_key_does_not_reject_the_config () {55 + let config: Config = serde_json::from_str(56 + r #"{ "model": "anthropic/claude-sonnet-4", "theme": "catppuccin" }"# ,59 + assert_eq! (config.model, "anthropic/claude-sonnet-4" );63 + fn a_config_round_trips () {64 + let config = Config {65 + model: "anthropic/claude-sonnet-4" .to_owned (),66 + tools: vec! ["read" .to_owned (), "bash" .to_owned ()],68 + let json = serde_json::to_string(& config).unwrap ();69 + assert_eq! (serde_json::from_str::<Config>(& json).unwrap (), config);new file mode 100644+165 / −0 text lines
15 + use serde::{Deserialize, Serialize};22 + macro_rules! identifier {23 + ($ (#[$meta:meta ])* $name:ident ) => {25 + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]26 + #[serde(transparent)]27 + pub struct $name(String );36 + pub const fn from_string (value: String) -> Self {42 + pub fn as_str (& self) -> &str {48 + pub fn into_string (self) -> String {53 + impl std::fmt::Display for $name {54 + fn fmt (& self, f: &mut std::fmt::Formatter<'_ >) -> std::fmt::Result {62 + macro_rules! generated_identifier {63 + ($ (#[$meta:meta ])* $name:ident ) => {64 + identifier! { $ (#[$meta])* $name }69 + pub fn new () -> Self {70 + Self (Uuid::now_v7().to_string ())74 + impl Default for $name {75 + fn default () -> Self {82 + generated_identifier! {87 + generated_identifier! {112 + clippy::unwrap_used,113 + reason = "Test fixtures use unwrap so setup failures report their exact operation." 116 + use super ::{EntryId, SecretId, SessionId};119 + fn generated_ids_are_unique () {120 + assert_ne! (EntryId::new(), EntryId::new());124 + fn generated_ids_sort_by_creation_order () {128 + let mut previous = EntryId::new();130 + let next = EntryId::new();132 + previous.as_str () <= next.as_str (),133 + "v7 ids must be non-decreasing: {previous} then {next}" 140 + fn an_id_survives_a_string_round_trip () {141 + let id = SessionId::new();142 + let restored = SessionId::from_string(id.as_str ().to_owned ());143 + assert_eq! (id, restored);147 + fn a_secret_id_carries_the_allocator_s_value_verbatim () {151 + let id = SecretId::from_string("12" .to_owned ());152 + assert_eq! (id.as_str (), "12" );153 + assert_eq! (format! ("smith:sec:{id}" ), "smith:sec:12" );157 + fn ids_serialize_as_bare_strings () {161 + let id = SessionId::from_string("abc" .to_owned ());162 + let json = serde_json::to_string(& id).unwrap ();163 + assert_eq! (json, "\"abc\"" );+40 / −0 text lines
3 24 #![forbid(unsafe_code)]#![forbid(unsafe_code)]4 25 #![warn(missing_docs)]#![warn(missing_docs)]
34 + pub use config::Config;35 + pub use ids::{EntryId, SecretId, SessionId, VcsOpId};36 + pub use message::{ContentBlock, Message, Role};38 + ProviderError, ProviderEvent, ProviderRequest, ProviderUsage, StopReason, ThinkingLevel,40 + pub use stream::StreamFn;42 + AgentTool, AgentToolResult, AgentToolUpdate, ToolDefinition, ToolDefinitionSpec,smith/src/message.rs Added new file mode 100644+202 / −0 text lines
9 + use serde::{Deserialize, Serialize};12 + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]13 + #[serde(rename_all = "snake_case" )]32 + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]33 + #[serde(rename_all = "snake_case" )]34 + pub enum ContentBlock {51 + arguments: serde_json::Value,72 + #[serde(default, skip_serializing_if = "Option::is_none" )]73 + provider_metadata: Option <serde_json::Value>,78 + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]83 + pub content: Vec <ContentBlock>,89 + pub const fn new (role: Role, content: Vec <ContentBlock>) -> Self {90 + Self { role, content }95 + pub fn text (role: Role, text: impl Into <String >) -> Self {96 + Self ::new(role, vec! [ContentBlock::Text(text.into ())])102 + clippy::unwrap_used,104 + reason = "Test fixtures use unwrap and panic so failures report their exact operation." 107 + use super ::{ContentBlock, Message, Role};111 + fn cbor_round_trip (message: & Message) -> Message {112 + let mut bytes = Vec ::new();113 + ciborium::into_writer(message, &mut bytes).unwrap ();114 + ciborium::from_reader(bytes.as_slice ()).unwrap ()118 + fn every_block_kind_survives_cbor () {119 + let message = Message::new(122 + ContentBlock::Text("hello" .to_owned ()),123 + ContentBlock::Image {124 + data: "AAAA" .to_owned (),125 + media_type: "image/png" .to_owned (),127 + ContentBlock::ToolCall {128 + id: "call-1" .to_owned (),129 + name: "read" .to_owned (),130 + arguments: serde_json::json! ({ "path" : "src/lib.rs" }),132 + ContentBlock::ToolResult {133 + id: "call-1" .to_owned (),134 + result: "contents" .to_owned (),137 + ContentBlock::Thinking {138 + content: "considering" .to_owned (),139 + provider_metadata: None ,144 + assert_eq! (cbor_round_trip (& message), message);148 + fn thinking_provider_metadata_is_preserved_opaquely () {152 + let metadata = serde_json::json! ({153 + "signature" : "sig-abc" ,154 + "nested" : { "unknown_to_smith" : [1 , 2 , 3 ] },156 + let message = Message::new(158 + vec! [ContentBlock::Thinking {159 + content: "considering" .to_owned (),160 + provider_metadata: Some (metadata.clone ()),164 + let restored = cbor_round_trip (& message);165 + let ContentBlock::Thinking {166 + provider_metadata, .. 167 + } = & restored.content[0 ]169 + panic! ("the thinking block must decode as a thinking block" );171 + assert_eq! (provider_metadata.as_ref (), Some (& metadata));175 + fn absent_provider_metadata_stays_absent () {178 + let message = Message::new(180 + vec! [ContentBlock::Thinking {181 + content: "considering" .to_owned (),182 + provider_metadata: None ,185 + let json = serde_json::to_string(& message).unwrap ();187 + ! json.contains ("provider_metadata" ),188 + "empty metadata must not be written: {json}" 190 + assert_eq! (cbor_round_trip (& message), message);194 + fn messages_survive_json_as_well () {197 + let message = Message::text(Role::User, "hello" );198 + let json = serde_json::to_string(& message).unwrap ();199 + let restored: Message = serde_json::from_str(& json).unwrap ();200 + assert_eq! (restored, message);smith/src/provider.rs Added new file mode 100644+259 / −0 text lines
10 + use serde::{Deserialize, Serialize};13 + use crate ::message::Message;14 + use crate ::tool::ToolDefinition;20 + #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]21 + pub struct ProviderUsage {29 + pub cache_write: u64 ,35 + #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]36 + #[serde(rename_all = "snake_case" )]53 + #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]54 + #[serde(rename_all = "snake_case" )]55 + pub enum ThinkingLevel {72 + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]73 + #[serde(rename_all = "snake_case" )]74 + pub enum ProviderEvent {96 + arguments: serde_json::Value,101 + usage: ProviderUsage,103 + stop_reason: StopReason,106 + Error(ProviderError),114 + #[derive(Debug, Clone, PartialEq, Eq, Error, Serialize, Deserialize)]115 + #[serde(rename_all = "snake_case" , tag = "kind" , content = "message" )]116 + pub enum ProviderError {118 + #[error("rate limited: {0}" )]121 + #[error("authentication failed: {0}" )]124 + #[error("network error: {0}" )]127 + #[error("server error: {0}" )]128 + ServerError(String ),130 + #[error("invalid request: {0}" )]131 + InvalidRequest(String ),133 + #[error("model not found: {0}" )]134 + ModelNotFound(String ),136 + #[error("timed out: {0}" )]141 + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]142 + pub struct ProviderRequest {144 + pub messages: Vec <Message>,146 + #[serde(default, skip_serializing_if = "Option::is_none" )]147 + pub system: Option <String >,149 + pub provider_id: String,151 + pub model_id: String,153 + #[serde(default, skip_serializing_if = "Vec::is_empty" )]154 + pub tools: Vec <ToolDefinition>,157 + pub thinking: ThinkingLevel,159 + #[serde(default, skip_serializing_if = "Option::is_none" )]160 + pub max_tokens: Option <u32 >,162 + #[serde(default, skip_serializing_if = "Vec::is_empty" )]163 + pub stop_sequences: Vec <String >,166 + impl ProviderRequest {173 + provider_id: impl Into <String >,174 + model_id: impl Into <String >,175 + messages: Vec <Message>,180 + provider_id: provider_id.into (),181 + model_id: model_id.into (),183 + thinking: ThinkingLevel::Off,185 + stop_sequences: Vec ::new(),192 + clippy::unwrap_used,193 + reason = "Test fixtures use unwrap so setup failures report their exact operation." 196 + use super ::{ProviderError, ProviderEvent, ProviderRequest, ProviderUsage, StopReason};197 + use crate ::message::{Message, Role};200 + fn events_survive_cbor () {202 + ProviderEvent::TextDelta {203 + text: "hel" .to_owned (),205 + ProviderEvent::ThinkingDelta {206 + text: "hmm" .to_owned (),208 + ProviderEvent::ToolCall {209 + id: "call-1" .to_owned (),210 + name: "read" .to_owned (),211 + arguments: serde_json::json! ({ "path" : "x" }),213 + ProviderEvent::Done {214 + usage: ProviderUsage {221 + stop_reason: StopReason::EndTurn,223 + ProviderEvent::Error(ProviderError::RateLimit("slow down" .to_owned ())),226 + let mut bytes = Vec ::new();227 + ciborium::into_writer(& events, &mut bytes).unwrap ();228 + let restored: Vec <ProviderEvent> = ciborium::from_reader(bytes.as_slice ()).unwrap ();229 + assert_eq! (restored, events);233 + fn provider_errors_carry_their_message () {236 + let error = ProviderError::AuthFailed("no ANTHROPIC_API_KEY" .to_owned ());239 + "authentication failed: no ANTHROPIC_API_KEY" 244 + fn a_request_omits_what_it_does_not_set () {245 + let request = ProviderRequest::new(248 + vec! [Message::text(Role::User, "hi" )],250 + let json = serde_json::to_string(& request).unwrap ();251 + assert! (! json.contains ("system" ), "unset system must not appear" );253 + ! json.contains ("stop_sequences" ),254 + "empty lists must not appear" 256 + let restored: ProviderRequest = serde_json::from_str(& json).unwrap ();257 + assert_eq! (restored, request);new file mode 100644+59 / −0 text lines
5 + use futures::stream::BoxStream;7 + use crate ::provider::{ProviderEvent, ProviderRequest};20 + pub type StreamFn = Arc<dyn Fn (ProviderRequest) -> BoxStream<'static , ProviderEvent> + Send + Sync >;26 + use futures::StreamExt as _ ;29 + use crate ::message::{Message, Role};30 + use crate ::provider::{ProviderEvent, ProviderRequest, ProviderUsage, StopReason};33 + fn a_scripted_stream_satisfies_the_alias () {39 + ProviderEvent::TextDelta {40 + text: "hello" .to_owned (),42 + ProviderEvent::Done {43 + usage: ProviderUsage::default(),44 + stop_reason: StopReason::EndTurn,48 + let stream_fn: StreamFn = Arc::new(move | _request: ProviderRequest| {49 + futures::stream::iter(script.clone ()).boxed ()53 + ProviderRequest::new("scripted" , "fixture" , vec! [Message::text(Role::User, "hi" )]);54 + let events: Vec <ProviderEvent> = futures::executor::block_on(stream_fn (request).collect ());56 + assert_eq! (events.len (), 2 );57 + assert! (matches! (events[1 ], ProviderEvent::Done { .. }));new file mode 100644+226 / −0 text lines
8 + use futures::future::BoxFuture;9 + use serde::{Deserialize, Serialize};11 + use crate ::message::ContentBlock;14 + #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]15 + #[serde(rename_all = "snake_case" )]16 + pub enum ToolExecutionMode {31 + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]32 + pub struct ToolDefinitionSpec {36 + pub description: String,38 + pub parameters: serde_json::Value,41 + pub execution_mode: ToolExecutionMode,49 + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]50 + pub struct ToolDefinition {53 + pub spec: ToolDefinitionSpec,59 + pub const fn new (spec: ToolDefinitionSpec) -> Self {65 + pub fn name (& self) -> &str {76 + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]77 + pub struct AgentToolUpdate {83 + #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]84 + pub struct AgentToolResult {86 + pub content: Vec <ContentBlock>,94 + #[serde(default, skip_serializing_if = "Vec::is_empty" )]95 + pub updates: Vec <AgentToolUpdate>,98 + impl AgentToolResult {101 + pub fn text (text: impl Into <String >) -> Self {103 + content: vec! [ContentBlock::Text(text.into ())],105 + updates: Vec ::new(),111 + pub fn error (text: impl Into <String >) -> Self {113 + content: vec! [ContentBlock::Text(text.into ())],115 + updates: Vec ::new(),126 + pub trait AgentTool : Send + Sync {128 + fn definition (& self) -> ToolDefinition;134 + fn execute (& self, arguments: serde_json::Value) -> BoxFuture<'_ , AgentToolResult>;139 + clippy::unwrap_used,140 + reason = "Test fixtures use unwrap so setup failures report their exact operation." 145 + use futures::FutureExt as _ ;146 + use futures::future::BoxFuture;149 + AgentTool, AgentToolResult, ToolDefinition, ToolDefinitionSpec, ToolExecutionMode,151 + use crate ::message::ContentBlock;155 + impl AgentTool for Echo {156 + fn definition (& self) -> ToolDefinition {157 + ToolDefinition::new(ToolDefinitionSpec {158 + name: "echo" .to_owned (),159 + description: "Returns its argument." .to_owned (),160 + parameters: serde_json::json! ({162 + "properties" : { "text" : { "type" : "string" } },163 + "required" : ["text" ],165 + execution_mode: ToolExecutionMode::Parallel,169 + fn execute (& self, arguments: serde_json::Value) -> BoxFuture<'_ , AgentToolResult> {171 + let text = arguments173 + .and_then (serde_json::Value::as_str)174 + .unwrap_or_default ()176 + AgentToolResult::text(text)183 + fn a_tool_is_usable_behind_dyn () {187 + let tool: Arc<dyn AgentTool> = Arc::new(Echo);188 + assert_eq! (tool.definition ().name (), "echo" );191 + futures::executor::block_on(tool.execute (serde_json::json! ({ "text" : "hello" })));192 + assert! (! result.is_error);193 + assert_eq! (result.content, vec! [ContentBlock::Text("hello" .to_owned ())]);197 + fn a_definition_serializes_flat () {200 + let definition = Echo.definition ();201 + let json = serde_json::to_value(& definition).unwrap ();203 + json.get ("name" ).and_then (serde_json::Value::as_str),206 + assert! (json.get ("spec" ).is_none (), "the spec must flatten: {json}" );208 + let restored: ToolDefinition = serde_json::from_value(json).unwrap ();209 + assert_eq! (restored, definition);213 + fn tools_are_sequential_unless_they_say_otherwise () {214 + assert_eq! (ToolExecutionMode::default(), ToolExecutionMode::Sequential);218 + fn an_error_result_is_still_content () {219 + let result = AgentToolResult::error("no such file" );220 + assert! (result.is_error);223 + vec! [ContentBlock::Text("no such file" .to_owned ())]