repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by admin
plugins/protocol-google/src/lib.rs
Raw//! Google Gemini protocol component.
#![expect(missing_docs, reason = "wit-bindgen generates the world surface")]
#![expect(clippy::pedantic, reason = "wit-bindgen generates the world surface")]
wit_bindgen::generate!({ path: "../../wit/smith-plugin.wit", world: "plugin-world" });
use exports::smith::plugin::plugin::{Guest as PluginGuest, ToolDeclaration};
use exports::smith::plugin::provider::{
CatalogSnapshot, DecodeStep, Guest as ProviderGuest, MessageBlock, MessageRole,
ProviderRequest, ProviderSettings, RequestTemplate, SettingsProvenance, StopReason,
StreamEvent, ToolUse, Usage,
};
use exports::smith::plugin::registration::{
CredentialSource, Guest as RegistrationGuest, ProtocolBinding, ProviderContribution,
Registrations,
};
use serde_json::{Value, json};
struct GoogleProtocol;
impl RegistrationGuest for GoogleProtocol {
fn declare() -> Registrations {
Registrations {
credential_sources: vec![CredentialSource {
id: "connection-api-key".to_string(),
}],
providers: vec![ProviderContribution {
id: "google".to_string(),
protocol: ProtocolBinding::GoogleGemini,
credential_source_id: "connection-api-key".to_string(),
}],
models: Vec::new(),
}
}
}
impl PluginGuest for GoogleProtocol {
fn declare_tools() -> Vec<ToolDeclaration> {
Vec::new()
}
fn invoke(_: String, _: String) -> Result<String, String> {
Err("protocol declares no tools".into())
}
}
impl ProviderGuest for GoogleProtocol {
fn declare_settings() -> ProviderSettings {
ProviderSettings {
include_usage: true,
subscription: false,
provenance: SettingsProvenance::BuiltIn,
}
}
fn validate_settings(settings: ProviderSettings) -> Result<ProviderSettings, String> {
settings
.include_usage
.then_some(settings)
.ok_or_else(|| "protocol setting include_usage must be true".into())
}
fn build_request(
model: String,
request: ProviderRequest,
settings: ProviderSettings,
catalog: CatalogSnapshot,
) -> Result<RequestTemplate, String> {
let settings = Self::validate_settings(settings)?;
let context = request.code_assist;
if settings.subscription != context.is_some() {
return Err("Google subscription requires host-bound project context".into());
}
if !catalog.models.iter().any(|entry| entry.id == model) {
return Err("provider catalog snapshot does not admit model".into());
};
let mut system = Vec::new();
let mut contents = Vec::new();
for message in request.messages {
if matches!(message.role, MessageRole::System) {
system.push(text(&message.blocks));
continue;
}
let role = if matches!(message.role, MessageRole::Assistant) {
"model"
} else {
"user"
};
let parts=message.blocks.iter().filter_map(|block|match block{MessageBlock::Text(text)=>Some(Ok(json!({"text":text}))),MessageBlock::ToolUse(call)=>Some(serde_json::from_str::<Value>(&call.input_json).map(|args|json!({"functionCall":{"name":call.name,"args":args}})).map_err(|e|format!("tool arguments are not JSON: {e}"))),MessageBlock::ToolResult(result)=>Some(Ok(json!({"functionResponse":{"name":result.name,"response":{"output":result.output}}}))),MessageBlock::Thinking(_)=>None}).collect::<Result<Vec<_>,String>>()?;
contents.push(json!({"role":role,"parts":parts}));
}
let mut body = json!({"contents":contents,"generationConfig":{"maxOutputTokens":request.output_tokens_max.unwrap_or(4096)}});
if !system.is_empty() {
body["systemInstruction"] = json!({"parts":[{"text":system.join("\n")} ]});
}
if !request.tools.is_empty() {
body["tools"] = json!([{"functionDeclarations":request.tools.into_iter().map(|tool|serde_json::from_str::<Value>(&tool.input_schema_json).map(|parameters|json!({"name":tool.name,"description":tool.description,"parameters":parameters})).map_err(|e|format!("tool schema is not JSON: {e}"))).collect::<Result<Vec<_>,_>>()?}]);
}
if settings.subscription {
let context = context.ok_or("missing Code Assist context")?;
if context.project.len() > 63
|| context.project.is_empty()
|| context.user_prompt_id.is_empty()
|| context.user_prompt_id.len() > 128
{
return Err("invalid Code Assist context".into());
}
return Ok(RequestTemplate {
url: "https://protocol.invalid/code-assist".into(),
authorization: "Bearer {{secret:connection-oauth}}".into(),
body: json!({"model":model,"project":context.project,"user_prompt_id":context.user_prompt_id,"request":body}).to_string(),
});
}
Ok(RequestTemplate {
url: "https://protocol.invalid/models/streamGenerateContent".into(),
authorization: "{{secret:connection-api-key}}".into(),
body: body.to_string(),
})
}
fn decode_response(
state: Vec<u8>,
chunk: Vec<u8>,
finished: bool,
) -> Result<DecodeStep, String> {
let mut state: Value = if state.is_empty() {
json!({"pending":[],"usage":{"input":0,"output":0,"hit":0},"reason":null,"stopped":false})
} else {
serde_json::from_slice(&state).map_err(|e| e.to_string())?
};
let mut pending = state["pending"]
.as_array()
.ok_or("invalid codec pending bytes")?
.iter()
.map(|byte| {
byte.as_u64()
.and_then(|n| u8::try_from(n).ok())
.ok_or("invalid codec pending byte")
})
.collect::<Result<Vec<_>, _>>()?;
pending.extend(chunk);
if finished && is_sse_tail(&pending) {
// EOF dispatches the final SSE event even without a blank separator.
pending.extend_from_slice(b"\n\n");
}
let mut events = Vec::new();
while let Some((data, rest)) = sse(&pending) {
pending = rest;
if let Some(data) = data {
process(
&mut state,
std::str::from_utf8(&data).map_err(|e| e.to_string())?,
&mut events,
)?;
}
}
if finished {
if !pending.is_empty() && !sse_ignorable(&pending) {
process(
&mut state,
std::str::from_utf8(&pending).map_err(|e| e.to_string())?,
&mut events,
)?;
}
let reason = state["reason"]
.as_str()
.ok_or("provider stream ended before finish reason")?;
let reason = match reason {
"TOOL_USE" => StopReason::ToolUse,
"STOP" => StopReason::EndTurn,
"MAX_TOKENS" => StopReason::Limit,
"SAFETY" | "PROHIBITED_CONTENT" | "RECITATION" => StopReason::ContentFilter,
other => return Err(format!("unmapped finish reason {other}")),
};
events.push(StreamEvent::Stop((
reason,
Some(Usage {
input_tokens: state["usage"]["input"].as_u64().unwrap_or(0),
output_tokens: state["usage"]["output"].as_u64().unwrap_or(0),
input_cache_hit_tokens: state["usage"]["hit"].as_u64().unwrap_or(0),
input_cache_write_tokens: 0,
}),
)));
state["stopped"] = json!(true);
pending.clear();
}
state["pending"] = json!(pending);
Ok(DecodeStep {
state: serde_json::to_vec(&state).map_err(|e| e.to_string())?,
events,
})
}
}
fn text(blocks: &[MessageBlock]) -> String {
blocks
.iter()
.filter_map(|block| {
if let MessageBlock::Text(text) = block {
Some(text.as_str())
} else {
None
}
})
.collect::<Vec<_>>()
.join("\n")
}
fn sse(bytes: &[u8]) -> Option<(Option<Vec<u8>>, Vec<u8>)> {
let mut data = Vec::new();
let mut consumed = 0;
for raw in bytes.split_inclusive(|byte| *byte == 10) {
let length = raw.len();
if !raw.ends_with(&[10]) {
break;
}
let line = raw.strip_suffix(&[10]).unwrap_or(raw);
let line = line.strip_suffix(&[13]).unwrap_or(line);
consumed += length;
if line.is_empty() {
return Some((
(!data.is_empty()).then_some(data),
bytes[consumed..].to_vec(),
));
}
if let Some(value) = line.strip_prefix(b"data:") {
data.extend_from_slice(value.strip_prefix(b" ").unwrap_or(value));
}
}
None
}
fn is_sse_tail(bytes: &[u8]) -> bool {
let bytes = bytes.trim_ascii_start();
[b"data:".as_slice(), b":", b"event:", b"id:", b"retry:"]
.iter()
.any(|prefix| bytes.starts_with(prefix))
}
fn sse_ignorable(bytes: &[u8]) -> bool {
bytes
.split(|byte| matches!(*byte, 10 | 13))
.all(|line| line.is_empty() || line.starts_with(b":"))
}
fn process(state: &mut Value, data: &str, events: &mut Vec<StreamEvent>) -> Result<(), String> {
let value: Value =
serde_json::from_str(data.trim()).map_err(|e| format!("chunk is not JSON: {e}"))?;
if let Some(error) = value.get("error") {
return Err(error
.get("message")
.and_then(Value::as_str)
.unwrap_or("provider error")
.into());
}
let value = value.get("response").unwrap_or(&value);
for (field, key) in [
("input", "promptTokenCount"),
("output", "candidatesTokenCount"),
("hit", "cachedContentTokenCount"),
] {
if let Some(n) = value
.pointer(&format!("/usageMetadata/{key}"))
.and_then(Value::as_u64)
{
state["usage"][field] = json!(n)
}
}
let Some(candidate) = value
.get("candidates")
.and_then(Value::as_array)
.and_then(|items| items.first())
else {
return Ok(());
};
let mut tool_use = false;
if let Some(parts) = candidate
.pointer("/content/parts")
.and_then(Value::as_array)
{
for part in parts {
if let Some(text) = part
.get("text")
.and_then(Value::as_str)
.filter(|x| !x.is_empty())
{
events.push(StreamEvent::Text(text.into()))
}
if let Some(call) = part.get("functionCall") {
let name = call
.get("name")
.and_then(Value::as_str)
.ok_or("functionCall without name")?;
let args = call.get("args").cloned().unwrap_or_else(|| json!({}));
tool_use = true;
events.push(StreamEvent::ToolUse(ToolUse {
name: name.into(),
input_json: args.to_string(),
call_id: call
.get("id")
.and_then(Value::as_str)
.unwrap_or(name)
.into(),
}));
}
}
}
if tool_use {
state["reason"] = json!("TOOL_USE");
} else if let Some(reason) = candidate.get("finishReason").and_then(Value::as_str) {
state["reason"] = json!(reason);
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn sse_consumes_comment_and_blank_events_without_data() {
let bytes = b": keepalive\n\n\n";
let (data, rest) = sse(bytes).unwrap();
assert!(data.is_none());
let (data, rest) = sse(&rest).unwrap();
assert!(data.is_none());
assert!(rest.is_empty());
assert!(sse_ignorable(b": trailing keepalive"));
}
#[test]
fn sse_buffers_fragmented_data_until_blank_frame() {
assert!(sse(b"data: {\"response\":").is_none());
let (data, rest) = sse(b"data: {\"response\":{}}\n\n").unwrap();
assert_eq!(data.unwrap(), b"{\"response\":{}}");
assert!(rest.is_empty());
}
}
export!(GoogleProtocol);