repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by admin
smith-harness/tests/fixtures/coherent-provider.rs
Raw//! Registered provider fixture (`SMH-SPEC-SPEC0001`).
wit_bindgen::generate!({ path: "wit", world: "plugin-world" });
use exports::smith::plugin::{plugin, provider::*, registration::*};
struct Plugin;
const VERSION: &str = "v1";
impl plugin::Guest for Plugin {
fn declare_tools() -> Vec<plugin::ToolDeclaration> {
vec![plugin::ToolDeclaration {
name: "identity".into(),
description: format!("{VERSION}-definition"),
input_schema_json: r#"{"type":"object"}"#.into(),
}]
}
fn invoke(_: String, _: String) -> Result<String, String> {
Ok(format!("{VERSION}-handler"))
}
}
impl exports::smith::plugin::registration::Guest for Plugin {
fn declare() -> Registrations {
Registrations {
credential_sources: vec![CredentialSource {
id: "fixture-key".into(),
}],
providers: vec![ProviderContribution {
id: "custom".into(),
protocol: ProtocolBinding::OpenaiCompatible,
credential_source_id: "fixture-key".into(),
}],
models: vec![ModelContribution {
id: "fixture-model".into(),
provider_id: "custom".into(),
}],
}
}
}
impl exports::smith::plugin::provider::Guest for Plugin {
fn declare_settings() -> ProviderSettings {
ProviderSettings {
include_usage: true,
subscription: false,
provenance: SettingsProvenance::Explicit,
}
}
fn validate_settings(settings: ProviderSettings) -> Result<ProviderSettings, String> {
Ok(settings)
}
fn build_request(
model: String,
request: ProviderRequest,
_: ProviderSettings,
_: CatalogSnapshot,
) -> Result<RequestTemplate, String> {
if model != "fixture-model" {
return Err("wrong model".into());
}
Ok(RequestTemplate {
url: "https://fixture.invalid/infer".into(),
authorization: "Bearer {{secret:connection-api-key}}".into(),
body: format!("{VERSION}|{}|{:?}",
request.tools.iter().map(|tool| tool.description.as_str()).collect::<Vec<_>>().join(","),
request.messages),
})
}
fn decode_response(
mut state: Vec<u8>,
chunk: Vec<u8>,
finished: bool,
) -> Result<DecodeStep, String> {
state.extend(chunk);
if !finished {
return Ok(DecodeStep {
state,
events: vec![],
});
}
let text = String::from_utf8(state).map_err(|_| "invalid text".to_string())?;
if text == "tool" {
return Ok(DecodeStep {
state: vec![],
events: vec![
StreamEvent::Text(format!("{VERSION}:tool")),
StreamEvent::ToolUse(ToolUse { name: "example:coherent/identity".into(), input_json: "{}".into(), call_id: "call-1".into() }),
StreamEvent::ToolUse(ToolUse { name: "example:coherent/identity".into(), input_json: "{}".into(), call_id: "call-2".into() }),
StreamEvent::Stop((StopReason::ToolUse, None)),
],
});
}
Ok(DecodeStep {
state: vec![],
events: vec![
StreamEvent::Text(format!("{VERSION}:{text}")),
StreamEvent::Stop((StopReason::EndTurn, None)),
],
})
}
}
export!(Plugin);