Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339use std::io::{Read, Write};use std::path::PathBuf;
use anyhow::{Context, Result, bail};use serde::{Deserialize, Serialize};
use crate::service::{TerminalId, TerminalInfo};
pub const PROTOCOL_VERSION: u32 = 7;/// Additive capabilities of this daemon. Breaking changes bump `PROTOCOL_VERSION`.pub const FEATURES: &[&str] = &["rename"];
pub fn features() -> Vec<String> { FEATURES.iter().map(|feature| feature.to_string()).collect()}pub const MAX_FRAME_BYTES: usize = 8 * 1024 * 1024;const OUTPUT_FRAME_TAG: u8 = 0;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]#[serde(rename_all = "snake_case")]pub enum AttachmentRole { Controller, Observer,}
#[derive(Debug, Serialize, Deserialize)]pub struct Envelope { pub token: String, pub request: Request,}
#[derive(Debug, Serialize, Deserialize)]#[serde(rename_all = "snake_case", tag = "op")]pub enum Request { Ping, Own { instance_id: String, ticket: Option<String>, }, PrepareHandoff, Create { program: String, args: Vec<String>, cwd: PathBuf, title: String, group_id: String, env: std::collections::HashMap<String, String>, cols: u16, rows: u16, }, List, Write { id: TerminalId, attachment_id: Option<String>, data_base64: String, }, Resize { id: TerminalId, attachment_id: Option<String>, cols: u16, rows: u16, }, Control { id: TerminalId, attachment_id: String, cols: u16, rows: u16, }, Input { id: TerminalId, attachment_id: String, cols: u16, rows: u16, data_base64: String, }, Snapshot { id: TerminalId, }, ReadRows { id: TerminalId, rows: Option<u16>, }, Rename { id: TerminalId, title: Option<String>, }, Replay { id: TerminalId, offset: u64, }, Subscribe { id: TerminalId, offset: u64, attachment_id: String, role: AttachmentRole, takeover: bool, }, Terminate { id: TerminalId, }, Shutdown,}
#[derive(Debug, Serialize, Deserialize)]#[serde(rename_all = "snake_case", tag = "type")]pub enum Response { Owned, Handoff { ticket: String, expires_at: u64, }, Pong { instance_id: String, pid: u32, protocol: u32, #[serde(default)] features: Vec<String>, }, Created { terminal: TerminalInfo, }, Terminals { terminals: Vec<TerminalInfo>, }, Ok, Snapshot { terminal: TerminalInfo, text: String, checkpoint_base64: String, cursor_x: u16, cursor_y: u16, }, Rows { terminal: TerminalInfo, lines: Vec<String>, cursor_x: u16, cursor_y: u16, }, Replay { requested_offset: u64, available_offset: u64, end_offset: u64, truncated: bool, data_base64: String, }, Attached { terminal: TerminalInfo, role: AttachmentRole, generation: u64, requested_offset: u64, available_offset: u64, end_offset: u64, truncated: bool, replay_base64: String, }, Resized { cols: u16, rows: u16, generation: u64, checkpoint_base64: String, }, Exited { exit_code: Option<u32>, final_offset: u64, }, ControllerChanged { attachment_id: Option<String>, generation: u64, }, TitleChanged { title: String, }, ForegroundProcessChanged { process: Option<String>, }, Error { message: String, },}
pub fn write_frame(mut writer: impl Write, value: &impl Serialize) -> Result<()> { let payload = serde_json::to_vec(value)?; if payload.len() > MAX_FRAME_BYTES { bail!("frame exceeds {MAX_FRAME_BYTES} byte limit"); } let length = u32::try_from(payload.len()).context("frame length exceeds u32")?; writer.write_all(&length.to_be_bytes())?; writer.write_all(&payload)?; writer.flush()?; Ok(())}
pub fn write_output_frame( mut writer: impl Write, start: u64, end: u64, bytes: &[u8],) -> Result<()> { let payload_len = bytes .len() .checked_add(17) .context("output frame length overflow")?; if payload_len > MAX_FRAME_BYTES { bail!("frame exceeds {MAX_FRAME_BYTES} byte limit"); } let length = u32::try_from(payload_len).context("frame length exceeds u32")?; writer.write_all(&length.to_be_bytes())?; writer.write_all(&[OUTPUT_FRAME_TAG])?; writer.write_all(&start.to_be_bytes())?; writer.write_all(&end.to_be_bytes())?; writer.write_all(bytes)?; writer.flush()?; Ok(())}
#[derive(Debug)]pub enum SubscriptionEvent { Output { start: u64, end: u64, bytes: Vec<u8>, }, Response(Box<Response>),}
pub fn read_subscription_event(mut reader: impl Read) -> Result<SubscriptionEvent> { let payload = read_payload(&mut reader)?; if payload.first() != Some(&OUTPUT_FRAME_TAG) { return serde_json::from_slice(&payload) .map(Box::new) .map(SubscriptionEvent::Response) .context("invalid protocol JSON"); } if payload.len() < 17 { bail!("invalid output frame"); } Ok(SubscriptionEvent::Output { start: u64::from_be_bytes(payload[1..9].try_into().expect("fixed start offset")), end: u64::from_be_bytes(payload[9..17].try_into().expect("fixed end offset")), bytes: payload[17..].to_vec(), })}
pub fn read_frame<T: for<'de> Deserialize<'de>>(mut reader: impl Read) -> Result<T> { serde_json::from_slice(&read_payload(&mut reader)?).context("invalid protocol JSON")}
fn read_payload(mut reader: impl Read) -> Result<Vec<u8>> { let mut length = [0_u8; 4]; reader.read_exact(&mut length)?; let length = u32::from_be_bytes(length) as usize; if length > MAX_FRAME_BYTES { bail!("frame claims {length} bytes, limit is {MAX_FRAME_BYTES}"); } let mut payload = vec![0_u8; length]; reader.read_exact(&mut payload)?; Ok(payload)}
#[cfg(test)]mod tests { use super::*;
#[test] fn read_rows_accepts_optional_height_and_u16_counts() { for json in [ r#"{"op":"read_rows","id":1}"#, r#"{"op":"read_rows","id":1,"rows":null}"#, ] { assert!(matches!( serde_json::from_str::<Request>(json).unwrap(), Request::ReadRows { id: 1, rows: None } )); } for rows in [0, 1, u16::MAX] { let value = serde_json::to_value(Request::ReadRows { id: 1, rows: Some(rows), }) .unwrap(); assert_eq!( value, serde_json::json!({"op": "read_rows", "id": 1, "rows": rows}) ); assert!( matches!(serde_json::from_value::<Request>(value).unwrap(), Request::ReadRows { rows: Some(count), .. } if count == rows) ); } for rows in [-1, 65536] { assert!( serde_json::from_value::<Request>( serde_json::json!({"op": "read_rows", "id": 1, "rows": rows}) ) .is_err() ); } assert_eq!(PROTOCOL_VERSION, 7); }
#[test] fn additive_fields_default_when_absent() { assert!(matches!( serde_json::from_str::<Response>( r#"{"type":"pong","instance_id":"a","pid":1,"protocol":7}"# ) .unwrap(), Response::Pong { features, .. } if features.is_empty() )); assert!(matches!( serde_json::from_str::<Request>(r#"{"op":"rename","id":1,"title":null}"#).unwrap(), Request::Rename { id: 1, title: None } )); assert!(matches!( serde_json::from_str::<Request>(r#"{"op":"rename","id":1}"#).unwrap(), Request::Rename { id: 1, title: None } )); let terminal = serde_json::json!({ "id": 1, "pid": null, "title": "shell", "foreground_process": null, "group_id": "group", "command": [], "cwd": "/", "cols": 80, "rows": 24, "lifecycle": {"status": "running"}, "output_head": 0, "output_tail": 0, }); let info = serde_json::from_value::<TerminalInfo>(terminal).unwrap(); assert_eq!(info.user_title, None); assert_eq!( serde_json::to_value(&info).unwrap()["user_title"], serde_json::Value::Null ); }}