repositories / smith
smith
There are many coding harnesses - but this one is fast
owned by admin
plugins/protocol-openai-compatible/src/lib.rs
Raw//! OpenAI-compatible protocol component over the smith:plugin world.
#![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,
ProviderMessage, 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 OpenAiCompatibleProtocol;
impl RegistrationGuest for OpenAiCompatibleProtocol {
fn declare() -> Registrations {
Registrations {
credential_sources: vec![CredentialSource {
id: "connection-api-key".to_string(),
}],
providers: vec![ProviderContribution {
id: "openai-compatible".to_string(),
protocol: ProtocolBinding::OpenaiCompatible,
credential_source_id: "connection-api-key".to_string(),
}],
models: Vec::new(),
}
}
}
impl PluginGuest for OpenAiCompatibleProtocol {
fn declare_tools() -> Vec<ToolDeclaration> {
Vec::new()
}
fn invoke(_name: String, _input_json: String) -> Result<String, String> {
Err("protocol declares no tools".to_string())
}
}
impl ProviderGuest for OpenAiCompatibleProtocol {
fn declare_settings() -> ProviderSettings {
ProviderSettings {
include_usage: true,
subscription: false,
provenance: SettingsProvenance::BuiltIn,
}
}
fn validate_settings(settings: ProviderSettings) -> Result<ProviderSettings, String> {
Ok(settings)
}
fn build_request(
model: String,
request: ProviderRequest,
settings: ProviderSettings,
catalog: CatalogSnapshot,
) -> Result<RequestTemplate, String> {
valid_model(&model)?;
Self::validate_settings(settings)?;
if !catalog.models.iter().any(|entry| entry.id == model) {
return Err("provider catalog snapshot does not admit model".to_string());
}
let mut wire_messages = Vec::with_capacity(request.messages.len());
for message in &request.messages {
encode_message(message, &mut wire_messages)?;
}
let mut body = json!({"model": model, "messages": wire_messages, "stream": true, "stream_options": {"include_usage": settings.include_usage}});
if !request.tools.is_empty() {
let tools = request.tools.iter().map(|tool| {
let parameters = serde_json::from_str::<Value>(&tool.input_schema_json)
.map_err(|error| format!("tool schema is not JSON: {error}"))?;
Ok(json!({"type":"function", "function": {"name": tool.name, "description": tool.description, "parameters": parameters}}))
}).collect::<Result<Vec<_>, String>>()?;
body["tools"] = Value::Array(tools);
}
if let Some(max) = request.output_tokens_max {
body["max_tokens"] = json!(max);
}
Ok(RequestTemplate {
url: "https://protocol.invalid/chat/completions".to_string(),
authorization: "Bearer {{secret:connection-api-key}}".to_string(),
body: body.to_string(),
})
}
fn decode_response(
state: Vec<u8>,
chunk: Vec<u8>,
finished: bool,
) -> Result<DecodeStep, String> {
let mut state = decode_state(&state)?;
let mut pending = state["pending"]
.as_array()
.ok_or("invalid codec pending bytes")?
.iter()
.map(|byte| {
byte.as_u64()
.and_then(|byte| u8::try_from(byte).ok())
.ok_or("invalid codec pending byte")
})
.collect::<Result<Vec<_>, _>>()?;
pending.extend_from_slice(&chunk);
let mut events = Vec::new();
while let Some((data, rest)) = take_sse_data(&pending) {
pending = rest;
let data = std::str::from_utf8(&data)
.map_err(|error| format!("provider response is not UTF-8: {error}"))?;
process_event(&mut state, data, &mut events)?;
}
if finished {
let tail = std::str::from_utf8(&pending)
.map_err(|error| format!("provider response is not UTF-8: {error}"))?
.trim();
if !tail.is_empty() {
process_event(&mut state, tail, &mut events)?;
}
pending.clear();
if !state["stopped"].as_bool().unwrap_or(false) {
return Err("provider stream ended before terminal marker".to_string());
}
}
state["pending"] = json!(pending);
Ok(DecodeStep {
state: serde_json::to_vec(&state).map_err(|error| error.to_string())?,
events,
})
}
}
fn valid_model(model: &str) -> Result<(), String> {
if model.is_empty()
|| !model
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-'))
{
Err("provider request has an invalid model".to_string())
} else {
Ok(())
}
}
fn encode_message(message: &ProviderMessage, out: &mut Vec<Value>) -> Result<(), String> {
if matches!(message.role, MessageRole::Tool) {
for block in &message.blocks {
if let MessageBlock::ToolResult(result) = block {
out.push(json!({"role":"tool", "tool_call_id": result.call_id, "content": result.output}));
}
}
return Ok(());
}
let role = match message.role {
MessageRole::System => "system",
MessageRole::User => "user",
MessageRole::Assistant => "assistant",
MessageRole::Tool => return Err("tool message has no tool result".to_string()),
};
let content = message
.blocks
.iter()
.filter_map(|block| match block {
MessageBlock::Text(text) => Some(text.as_str()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n");
let mut encoded = json!({"role": role, "content": content});
if matches!(message.role, MessageRole::Assistant) {
let calls = message
.blocks
.iter()
.filter_map(|block| match block {
MessageBlock::ToolUse(call) => Some(call),
_ => None,
})
.map(|call| {
serde_json::from_str::<Value>(&call.input_json)
.map_err(|error| format!("tool arguments are not JSON: {error}"))?;
Ok(json!({"id": call.call_id, "type":"function", "function":{"name": call.name, "arguments": call.input_json}}))
})
.collect::<Result<Vec<_>, String>>()?;
if !calls.is_empty() {
encoded["tool_calls"] = Value::Array(calls);
}
}
out.push(encoded);
Ok(())
}
fn decode_state(bytes: &[u8]) -> Result<Value, String> {
if bytes.is_empty() {
return Ok(
json!({"pending":[], "tools":{}, "finish_reason":null, "usage":null, "terminal":false, "stopped":false}),
);
}
serde_json::from_slice(bytes).map_err(|error| format!("invalid codec state: {error}"))
}
fn process_event(
state: &mut Value,
data: &str,
events: &mut Vec<StreamEvent>,
) -> Result<(), String> {
if state["terminal"].as_bool().unwrap_or(false) {
return Err("event after terminal".to_string());
}
if data.trim() == "[DONE]" {
return finish_done(state, events);
}
let chunk: Value =
serde_json::from_str(data.trim()).map_err(|error| format!("chunk is not JSON: {error}"))?;
if let Some(error) = chunk.get("error") {
return Err(error
.get("message")
.and_then(Value::as_str)
.unwrap_or("provider error")
.to_string());
}
if let Some(usage) = chunk.get("usage").filter(|usage| usage.is_object()) {
state["usage"] = json!({"input_tokens":usage.get("prompt_tokens").and_then(Value::as_u64), "output_tokens":usage.get("completion_tokens").and_then(Value::as_u64), "input_cache_hit_tokens":usage.get("prompt_tokens_details").and_then(|details| details.get("cached_tokens")).and_then(Value::as_u64).unwrap_or(0), "input_cache_write_tokens":0});
}
let Some(choice) = chunk
.get("choices")
.and_then(Value::as_array)
.and_then(|choices| choices.first())
else {
return Ok(());
};
let delta = choice.get("delta").unwrap_or(&Value::Null);
if let Some(text) = delta
.get("content")
.and_then(Value::as_str)
.filter(|text| !text.is_empty())
{
events.push(StreamEvent::Text(text.to_string()));
}
if let Some(text) = delta
.get("reasoning_content")
.and_then(Value::as_str)
.filter(|text| !text.is_empty())
{
events.push(StreamEvent::Thinking(text.to_string()));
}
if let Some(calls) = delta.get("tool_calls").and_then(Value::as_array) {
for call in calls {
accumulate_tool_call(state, call)?;
}
}
if let Some(reason) = choice.get("finish_reason").and_then(Value::as_str) {
if !state["finish_reason"].is_null() {
return Err("finish_reason sent twice".to_string());
}
state["finish_reason"] = json!(reason);
flush_tools(state, events)?;
}
Ok(())
}
fn accumulate_tool_call(state: &mut Value, call: &Value) -> Result<(), String> {
let index = call
.get("index")
.and_then(Value::as_u64)
.ok_or("tool call fragment without index")?
.to_string();
let tools = state["tools"]
.as_object_mut()
.ok_or("invalid codec tool state")?;
let entry = tools
.entry(index)
.or_insert_with(|| json!({"call_id":"", "name":"", "arguments":""}));
if let Some(call_id) = call.get("id").and_then(Value::as_str) {
entry["call_id"] = json!(call_id);
}
if let Some(name) = call
.get("function")
.and_then(|function| function.get("name"))
.and_then(Value::as_str)
{
entry["name"] = json!(name);
}
if let Some(fragment) = call
.get("function")
.and_then(|function| function.get("arguments"))
.and_then(Value::as_str)
{
entry["arguments"] = json!(format!(
"{}{}",
entry["arguments"].as_str().unwrap_or_default(),
fragment
));
}
Ok(())
}
fn flush_tools(state: &mut Value, events: &mut Vec<StreamEvent>) -> Result<(), String> {
let tools = state["tools"]
.as_object()
.ok_or("invalid codec tool state")?;
let mut indexes = tools
.keys()
.map(|index| index.parse::<u64>().map_err(|_| "invalid codec tool index"))
.collect::<Result<Vec<_>, _>>()?;
indexes.sort_unstable();
for index in indexes {
let entry = tools
.get(&index.to_string())
.ok_or("invalid codec tool state")?;
let name = entry["name"]
.as_str()
.filter(|name| !name.is_empty())
.ok_or("tool call without a name")?;
let arguments = entry["arguments"].as_str().unwrap_or_default();
let input = if arguments.trim().is_empty() {
"{}".to_string()
} else {
serde_json::from_str::<Value>(arguments)
.map_err(|error| format!("fragmented tool arguments are not JSON: {error}"))?
.to_string()
};
let call_id = entry["call_id"]
.as_str()
.filter(|call_id| !call_id.is_empty())
.ok_or("tool call without an id")?;
events.push(StreamEvent::ToolUse(ToolUse {
name: name.to_string(),
input_json: input,
call_id: call_id.to_string(),
}));
}
state["tools"] = json!({});
Ok(())
}
fn finish_done(state: &mut Value, events: &mut Vec<StreamEvent>) -> Result<(), String> {
let reason = state["finish_reason"]
.as_str()
.ok_or("terminal marker without finish_reason")?;
if !state["tools"]
.as_object()
.is_none_or(|tools| tools.is_empty())
{
return Err("terminal marker with unfinished tool calls".to_string());
}
let reason = match reason {
"stop" => StopReason::EndTurn,
"length" => StopReason::Limit,
"tool_calls" | "function_call" => StopReason::ToolUse,
"content_filter" => StopReason::ContentFilter,
"stop_sequence" => StopReason::StopSequence,
"refusal" => StopReason::Refusal,
"abort" => StopReason::Abort,
other => return Err(format!("unmapped finish_reason {other}")),
};
let usage = state["usage"].as_object().and_then(|usage| {
Some(Usage {
input_tokens: usage.get("input_tokens")?.as_u64()?,
output_tokens: usage.get("output_tokens")?.as_u64()?,
input_cache_hit_tokens: usage.get("input_cache_hit_tokens")?.as_u64()?,
input_cache_write_tokens: usage.get("input_cache_write_tokens")?.as_u64()?,
})
});
state["terminal"] = json!(true);
state["stopped"] = json!(true);
events.push(StreamEvent::Stop((reason, usage)));
Ok(())
}
fn take_sse_data(buffer: &[u8]) -> Option<(Vec<u8>, Vec<u8>)> {
let mut data = Vec::new();
let mut consumed = 0;
for line in buffer.split_inclusive(|byte| *byte == b'\n') {
let trimmed = line.strip_suffix(b"\n").unwrap_or(line);
let trimmed = trimmed.strip_suffix(b"\r").unwrap_or(trimmed);
if trimmed.is_empty() {
if data.is_empty() {
consumed += line.len();
continue;
}
return Some((data, buffer[consumed + line.len()..].to_vec()));
}
if let Some(payload) = trimmed.strip_prefix(b"data:") {
data.extend_from_slice(payload.strip_prefix(b" ").unwrap_or(payload));
}
consumed += line.len();
}
None
}
export!(OpenAiCompatibleProtocol);