Luigit
repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by admin

plugins/protocol-openai-responses/src/lib.rs

Raw
//! Nonpersistent Responses codec (`SMH-SPEC-PLUG0001`, `SMH-SPEC-SPEC0001`).
//!
//! No credential effect or provider registration is supplied here.
//! Activating this codec requires the separately validated OAuth module binding.
#![deny(unsafe_code)]

#[expect(clippy::pedantic, reason = "wit-bindgen generates component ABI glue")]
#[allow(
    unsafe_code,
    reason = "wit-bindgen owns component ABI glue; no authored unsafe code"
)]
mod bindings {
    wit_bindgen::generate!({
        path: "../../wit/smith-plugin.wit",
        world: "responses-world",
    });
    use super::ResponsesProtocol;
    export!(ResponsesProtocol with_types_in self);
}

use bindings::exports::smith::plugin::plugin::{Guest as PluginGuest, ToolDeclaration};
use bindings::exports::smith::plugin::protocol_openai_responses_v1::Guest as ResponsesBindingGuest;
use bindings::exports::smith::plugin::provider::{
    CatalogSnapshot, DecodeStep, Guest as ProviderGuest, MessageBlock, MessageRole,
    ProviderMessage, ProviderRequest, ProviderSettings, RequestTemplate, SettingsProvenance,
    StopReason, StreamEvent, ToolUse, Usage,
};
use bindings::exports::smith::plugin::registration::{Guest as RegistrationGuest, Registrations};
use serde_json::{Value, json};

const REQUEST_TEXT_BYTES_MAX: usize = 64 * 1024;
const REQUEST_ITEMS_MAX: usize = 128;
const FRAME_BYTES_MAX: usize = 64 * 1024;
const CHUNK_BYTES_MAX: usize = 256 * 1024;
const STATE_HEADER_BYTES: usize = 18;
const OUTPUT_ITEMS_MAX: u64 = 128;
const TOOL_NAMESPACE: &str = "smith";

struct ResponsesProtocol;

impl ResponsesBindingGuest for ResponsesProtocol {
    fn credential_source() -> String {
        "openai-chatgpt-plan@1".into()
    }
}

impl RegistrationGuest for ResponsesProtocol {
    fn declare() -> Registrations {
        Registrations {
            credential_sources: Vec::new(),
            providers: Vec::new(),
            models: Vec::new(),
        }
    }
}

impl PluginGuest for ResponsesProtocol {
    fn declare_tools() -> Vec<ToolDeclaration> {
        Vec::new()
    }

    fn invoke(_name: String, _input_json: String) -> Result<String, String> {
        Err("protocol declares no tools".into())
    }
}

impl ProviderGuest for ResponsesProtocol {
    fn declare_settings() -> ProviderSettings {
        ProviderSettings {
            include_usage: true,
            subscription: false,
            provenance: SettingsProvenance::BuiltIn,
        }
    }

    fn validate_settings(settings: ProviderSettings) -> Result<ProviderSettings, String> {
        if !settings.include_usage {
            return Err("Responses usage reporting cannot be disabled".into());
        }
        Ok(settings)
    }

    fn build_request(
        model: String,
        request: ProviderRequest,
        settings: ProviderSettings,
        catalog: CatalogSnapshot,
    ) -> Result<RequestTemplate, String> {
        Self::validate_settings(settings)?;
        validate_request(&model, &request, &catalog)?;
        let mut input = Vec::new();
        for message in &request.messages {
            encode_message(message, &mut input)?;
        }
        let mut body = json!({"model":model, "input":input, "store":false, "stream":true});
        if !request.tools.is_empty() {
            let mut tools = Vec::with_capacity(request.tools.len());
            for tool in &request.tools {
                if !valid_name(&tool.name) {
                    return Err("invalid function tool name".into());
                }
                let parameters: Value = serde_json::from_str(&tool.input_schema_json)
                    .map_err(|_| "function tool schema is not JSON")?;
                if parameters.get("type").and_then(Value::as_str) != Some("object") {
                    return Err("function tool schema must describe an object".into());
                }
                tools.push(json!({
                    "type":"function", "name":tool.name, "description":tool.description,
                    "parameters":parameters, "strict":false,
                }));
            }
            body["tools"] = json!([{
                "type":"namespace", "name":TOOL_NAMESPACE,
                "description":"Smith local coding tools", "tools":tools,
            }]);
        }
        Ok(RequestTemplate {
            url: "https://protocol.invalid/responses".into(),
            authorization: "Bearer {{secret:connection-oauth}}".into(),
            body: body.to_string(),
        })
    }

    fn decode_response(
        state: Vec<u8>,
        chunk: Vec<u8>,
        finished: bool,
    ) -> Result<DecodeStep, String> {
        if chunk.len() > CHUNK_BYTES_MAX {
            return Err("Responses chunk exceeds byte limit".into());
        }
        let mut decoder = Decoder::restore(&state)?;
        let mut events = Vec::new();
        for fragment in chunk.split_inclusive(|byte| *byte == b'\n') {
            if decoder.pending.len() + fragment.len() > FRAME_BYTES_MAX {
                return Err("Responses SSE frame exceeds byte limit".into());
            }
            decoder.pending.extend_from_slice(fragment);
            if fragment.last() != Some(&b'\n') {
                continue;
            }
            let line_end = decoder.pending.len() - 1;
            let line_start = decoder.pending[..line_end]
                .iter()
                .rposition(|byte| *byte == b'\n')
                .map_or(0, |index| index + 1);
            if !decoder.pending[line_start..line_end]
                .iter()
                .all(|byte| *byte == b'\r')
            {
                continue;
            }
            let data = sse_data(&decoder.pending)?;
            if !data.is_empty() {
                decoder.event(&data, &mut events)?;
                if events.len() > REQUEST_ITEMS_MAX {
                    return Err("Responses decoded event count exceeds limit".into());
                }
            }
            decoder.pending.clear();
        }
        if finished && (!decoder.completed || !decoder.pending.is_empty()) {
            return Err("Responses stream ended without complete response.completed frame".into());
        }
        Ok(DecodeStep {
            state: decoder.save(),
            events,
        })
    }
}

fn valid_name(name: &str) -> bool {
    !name.is_empty()
        && name.len() <= 64
        && name
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-'))
}

fn validate_request(
    model: &str,
    request: &ProviderRequest,
    catalog: &CatalogSnapshot,
) -> Result<(), String> {
    if model.is_empty()
        || model.len() > 256
        || !model
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'_' | b'-'))
    {
        return Err("invalid Responses model".into());
    }
    let selected = catalog
        .models
        .iter()
        .find(|entry| entry.id == model)
        .ok_or("account catalog does not admit Responses model")?;
    if selected.capabilities.thinking {
        return Err("this Responses protocol version does not support reasoning models".into());
    }
    if request.output_tokens_max.is_some() {
        return Err("ChatGPT plan does not support max_output_tokens".into());
    }
    if !request.tools.is_empty() && !selected.capabilities.tool_use {
        return Err("account catalog does not admit function tools".into());
    }
    if request.messages.len() > REQUEST_ITEMS_MAX || request.tools.len() > REQUEST_ITEMS_MAX {
        return Err("Responses request item count exceeds limit".into());
    }
    let mut bytes = 0_usize;
    let mut items = request.tools.len();
    for message in &request.messages {
        items = items.saturating_add(message.blocks.len());
        for block in &message.blocks {
            bytes = bytes.saturating_add(match block {
                MessageBlock::Text(text) | MessageBlock::Thinking(text) => text.len(),
                MessageBlock::ToolUse(call) => call
                    .name
                    .len()
                    .saturating_add(call.call_id.len())
                    .saturating_add(call.input_json.len()),
                MessageBlock::ToolResult(result) => result
                    .name
                    .len()
                    .saturating_add(result.call_id.len())
                    .saturating_add(result.output.len()),
            });
        }
    }
    for tool in &request.tools {
        bytes = bytes
            .saturating_add(tool.name.len())
            .saturating_add(tool.description.len())
            .saturating_add(tool.input_schema_json.len());
    }
    if bytes > REQUEST_TEXT_BYTES_MAX || items > REQUEST_ITEMS_MAX {
        return Err("Responses request exceeds text or item limit".into());
    }
    Ok(())
}

fn encode_message(message: &ProviderMessage, input: &mut Vec<Value>) -> Result<(), String> {
    let role = match message.role {
        MessageRole::System => "developer",
        MessageRole::User => "user",
        MessageRole::Assistant => "assistant",
        MessageRole::Tool => "tool",
    };
    for block in &message.blocks {
        match block {
            MessageBlock::Text(text) if role != "tool" => {
                input.push(json!({"role":role, "content":text}));
            }
            MessageBlock::ToolUse(call) if role == "assistant" => {
                let arguments: Value = serde_json::from_str(&call.input_json)
                    .map_err(|_| "function arguments are not JSON")?;
                if !arguments.is_object() || !valid_name(&call.name) || call.call_id.is_empty() {
                    return Err("invalid function call history".into());
                }
                input.push(json!({
                    "type":"function_call", "call_id":call.call_id,
                    "name":call.name, "namespace":TOOL_NAMESPACE, "arguments":call.input_json,
                }));
            }
            MessageBlock::ToolResult(result) if role == "tool" && !result.call_id.is_empty() => {
                input.push(json!({"type":"function_call_output", "call_id":result.call_id, "output":result.output}));
            }
            _ => return Err("unsupported Responses message block or role".into()),
        }
    }
    Ok(())
}

/// Invocation scratch only; host carries the bounded serialized state between chunks.
struct Decoder {
    pending: Vec<u8>,
    tool_indexes: u128,
    completed: bool,
    refusal: bool,
}

impl Decoder {
    fn restore(state: &[u8]) -> Result<Self, String> {
        if state.is_empty() {
            return Ok(Self {
                pending: Vec::new(),
                tool_indexes: 0,
                completed: false,
                refusal: false,
            });
        }
        if state.len() < STATE_HEADER_BYTES
            || state.len() > STATE_HEADER_BYTES + FRAME_BYTES_MAX
            || state[0] != 1
            || state[1] & !3 != 0
        {
            return Err("invalid Responses codec state".into());
        }
        let indexes: [u8; 16] = state[2..STATE_HEADER_BYTES]
            .try_into()
            .map_err(|_| "invalid Responses codec state")?;
        Ok(Self {
            pending: state[STATE_HEADER_BYTES..].to_vec(),
            tool_indexes: u128::from_le_bytes(indexes),
            completed: state[1] & 1 != 0,
            refusal: state[1] & 2 != 0,
        })
    }

    fn save(&self) -> Vec<u8> {
        let mut state = Vec::with_capacity(STATE_HEADER_BYTES + self.pending.len());
        state.push(1);
        state.push(u8::from(self.completed) | (u8::from(self.refusal) << 1));
        state.extend_from_slice(&self.tool_indexes.to_le_bytes());
        state.extend_from_slice(&self.pending);
        state
    }

    fn event(&mut self, data: &str, events: &mut Vec<StreamEvent>) -> Result<(), String> {
        if self.completed {
            return Err("Responses event after response.completed".into());
        }
        let event: Value = serde_json::from_str(data).map_err(|_| "Responses event is not JSON")?;
        let kind = event
            .get("type")
            .and_then(Value::as_str)
            .ok_or("Responses event has no type")?;
        match kind {
            "response.output_text.delta" | "response.refusal.delta" => {
                let refusal = kind == "response.refusal.delta";
                let text = event
                    .get("delta")
                    .and_then(Value::as_str)
                    .ok_or(if refusal {
                        "invalid Responses refusal delta"
                    } else {
                        "invalid Responses text delta"
                    })?;
                self.refusal |= refusal;
                if !text.is_empty() {
                    events.push(StreamEvent::Text(text.into()));
                }
            }
            "response.output_item.done" => self.item(&event, events)?,
            "response.completed" => {
                let response = event
                    .get("response")
                    .ok_or("Responses completion has no response")?;
                if response.get("status").and_then(Value::as_str) != Some("completed") {
                    return Err("Responses completion status is not completed".into());
                }
                let usage = response
                    .get("usage")
                    .ok_or("Responses completion has no usage")?;
                let input_tokens = usage
                    .get("input_tokens")
                    .and_then(Value::as_u64)
                    .ok_or("invalid Responses input usage")?;
                let output_tokens = usage
                    .get("output_tokens")
                    .and_then(Value::as_u64)
                    .ok_or("invalid Responses output usage")?;
                let cached = match usage
                    .get("input_tokens_details")
                    .and_then(|details| details.get("cached_tokens"))
                {
                    None => 0,
                    Some(value) => value.as_u64().ok_or("invalid Responses cache usage")?,
                };
                if cached > input_tokens {
                    return Err("Responses cached usage exceeds input usage".into());
                }
                let reason = if self.tool_indexes != 0 {
                    StopReason::ToolUse
                } else if self.refusal {
                    StopReason::Refusal
                } else {
                    StopReason::EndTurn
                };
                events.push(StreamEvent::Stop((
                    reason,
                    Some(Usage {
                        input_tokens,
                        output_tokens,
                        input_cache_hit_tokens: cached,
                        input_cache_write_tokens: 0,
                    }),
                )));
                self.completed = true;
            }
            "response.failed" | "response.incomplete" | "error" => {
                let error = event
                    .get("response")
                    .and_then(|response| response.get("error"))
                    .unwrap_or(&event);
                let code = error.get("code").and_then(Value::as_str).filter(|code| {
                    code.len() <= 128
                        && code
                            .bytes()
                            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
                });
                return Err(code.map_or_else(
                    || format!("Responses terminal failure: {kind}"),
                    |code| format!("Responses terminal failure: {kind} ({code})"),
                ));
            }
            "response.created"
            | "response.in_progress"
            | "response.output_item.added"
            | "response.content_part.added"
            | "response.content_part.done"
            | "response.output_text.done"
            | "response.refusal.done"
            | "response.function_call_arguments.delta"
            | "response.function_call_arguments.done" => {}
            _ => return Err("unsupported Responses stream event".into()),
        }
        Ok(())
    }

    fn item(&mut self, event: &Value, events: &mut Vec<StreamEvent>) -> Result<(), String> {
        let item = event.get("item").ok_or("Responses output item missing")?;
        match item.get("type").and_then(Value::as_str) {
            Some("message") => return Ok(()),
            Some("function_call") => {}
            _ => return Err("unsupported Responses output item".into()),
        }
        let index = event
            .get("output_index")
            .and_then(Value::as_u64)
            .filter(|index| *index < OUTPUT_ITEMS_MAX)
            .ok_or("invalid Responses output index")?;
        let bit = 1_u128 << index;
        if self.tool_indexes & bit != 0 {
            return Err("duplicate Responses function output item".into());
        }
        if item.get("status").and_then(Value::as_str) != Some("completed") {
            return Err("incomplete Responses function output item".into());
        }
        if item.get("namespace").and_then(Value::as_str) != Some(TOOL_NAMESPACE) {
            return Err("Responses function uses an unadvertised namespace".into());
        }
        let name = item
            .get("name")
            .and_then(Value::as_str)
            .filter(|name| valid_name(name))
            .ok_or("invalid Responses function name")?;
        let call_id = item
            .get("call_id")
            .and_then(Value::as_str)
            .filter(|id| !id.is_empty())
            .ok_or("invalid Responses function call id")?;
        let arguments = item
            .get("arguments")
            .and_then(Value::as_str)
            .ok_or("Responses function arguments missing")?;
        let parsed: Value = serde_json::from_str(arguments)
            .map_err(|_| "Responses function arguments are not JSON")?;
        if !parsed.is_object() {
            return Err("Responses function arguments must be an object".into());
        }
        events.push(StreamEvent::ToolUse(ToolUse {
            name: name.into(),
            call_id: call_id.into(),
            input_json: arguments.into(),
        }));
        self.tool_indexes |= bit;
        Ok(())
    }
}

fn sse_data(frame: &[u8]) -> Result<String, String> {
    let text = std::str::from_utf8(frame).map_err(|_| "Responses SSE frame is not UTF-8")?;
    let mut data = String::new();
    for line in text.split('\n') {
        let line = line.strip_suffix('\r').unwrap_or(line);
        if let Some(value) = line.strip_prefix("data:") {
            if !data.is_empty() {
                data.push('\n');
            }
            data.push_str(value.strip_prefix(' ').unwrap_or(value));
        }
    }
    Ok(data)
}

#[cfg(test)]
mod tests;