copilot_sdk_supercharged/lib.rs
1// Copyright (c) Microsoft Corporation. All rights reserved.
2
3//! # Copilot SDK for Rust
4//!
5//! A Rust client library for programmatic control of GitHub Copilot CLI via JSON-RPC 2.0.
6//!
7//! This SDK communicates with the Copilot CLI server using JSON-RPC 2.0 over stdio
8//! or TCP, with Content-Length header framing (LSP protocol style).
9//!
10//! ## Quick Start
11//!
12//! ```rust,no_run
13//! use copilot_sdk::*;
14//!
15//! #[tokio::main]
16//! async fn main() -> Result<(), CopilotError> {
17//! // Create and start a client
18//! let client = CopilotClient::new(CopilotClientOptions {
19//! cli_path: Some("/path/to/copilot-cli".to_string()),
20//! ..Default::default()
21//! });
22//! client.start().await?;
23//!
24//! // Create a session
25//! let session = client.create_session(SessionConfig::default()).await?;
26//!
27//! // Send a message and wait for completion
28//! let response = session.send_and_wait(
29//! MessageOptions {
30//! prompt: "What is 2 + 2?".to_string(),
31//! attachments: None,
32//! mode: None,
33//! },
34//! None,
35//! ).await?;
36//!
37//! if let Some(event) = response {
38//! if let Some(content) = event.assistant_message_content() {
39//! println!("Assistant: {}", content);
40//! }
41//! }
42//!
43//! // Clean up
44//! session.destroy().await?;
45//! client.stop().await?;
46//! Ok(())
47//! }
48//! ```
49
50pub mod client;
51pub mod jsonrpc;
52pub mod sdk_protocol_version;
53pub mod session;
54pub mod types;
55
56// Re-export main types at crate root for convenience
57pub use client::CopilotClient;
58pub use session::{
59 CopilotSession, HooksHandlerFn, PermissionHandlerFn, Subscription, ToolHandler,
60 UserInputHandlerFn,
61};
62pub use types::*;
63
64/// Error types for the Copilot SDK.
65#[derive(Debug, thiserror::Error)]
66pub enum CopilotError {
67 /// JSON-RPC error response from the server.
68 #[error("JSON-RPC error {code}: {message}")]
69 JsonRpc {
70 code: i32,
71 message: String,
72 data: Option<serde_json::Value>,
73 },
74
75 /// Serialization/deserialization error.
76 #[error("Serialization error: {0}")]
77 Serialization(String),
78
79 /// Connection closed unexpectedly.
80 #[error("Connection closed")]
81 ConnectionClosed,
82
83 /// Request timed out.
84 #[error("Request timed out after {0}ms")]
85 Timeout(u64),
86
87 /// I/O error.
88 #[error("I/O error: {0}")]
89 Io(String),
90
91 /// Protocol error (malformed messages, etc.).
92 #[error("Protocol error: {0}")]
93 Protocol(String),
94
95 /// Client not connected.
96 #[error("Client not connected. Call start() first.")]
97 NotConnected,
98
99 /// Configuration error.
100 #[error("Configuration error: {0}")]
101 Configuration(String),
102
103 /// Failed to spawn CLI process.
104 #[error("Process spawn error: {0}")]
105 ProcessSpawn(String),
106
107 /// Connection failed.
108 #[error("Connection error: {0}")]
109 Connection(String),
110
111 /// Protocol version mismatch.
112 #[error("SDK protocol version mismatch: expected {expected}, server reports {actual:?}")]
113 ProtocolMismatch {
114 expected: u32,
115 actual: Option<u32>,
116 },
117
118 /// Session-related error.
119 #[error("Session error: {0}")]
120 SessionError(String),
121
122 /// No handler registered for a request.
123 #[error("No handler: {0}")]
124 NoHandler(String),
125}