Skip to main content

kimi_wire/protocol/
method.rs

1use serde::{Deserialize, Serialize};
2
3use super::content::UserInput;
4
5// ============================================================================
6// Initialize
7// ============================================================================
8
9/// Initialize request parameters.
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
11pub struct InitializeParams {
12    /// Protocol version string (e.g. "1.10").
13    pub protocol_version: String,
14    /// Client identification.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub client: Option<ClientInfo>,
17    /// External tools the client wants to register.
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub external_tools: Option<Vec<ExternalTool>>,
20    /// Client capabilities.
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub capabilities: Option<ClientCapabilities>,
23    /// Hook subscriptions requested by the client.
24    #[serde(skip_serializing_if = "Option::is_none")]
25    pub hooks: Option<Vec<WireHookSubscription>>,
26}
27
28impl InitializeParams {
29    /// Create new initialize parameters with the given protocol version.
30    pub fn new(protocol_version: impl Into<String>) -> Self {
31        Self {
32            protocol_version: protocol_version.into(),
33            client: None,
34            external_tools: None,
35            capabilities: None,
36            hooks: None,
37        }
38    }
39
40    /// Set client info.
41    #[must_use]
42    pub fn with_client(mut self, client: ClientInfo) -> Self {
43        self.client = Some(client);
44        self
45    }
46
47    /// Set external tools.
48    #[must_use]
49    pub fn with_external_tools(mut self, tools: Vec<ExternalTool>) -> Self {
50        self.external_tools = Some(tools);
51        self
52    }
53
54    /// Set client capabilities.
55    #[must_use]
56    pub const fn with_capabilities(mut self, caps: ClientCapabilities) -> Self {
57        self.capabilities = Some(caps);
58        self
59    }
60
61    /// Set hook subscriptions.
62    #[must_use]
63    pub fn with_hooks(mut self, hooks: Vec<WireHookSubscription>) -> Self {
64        self.hooks = Some(hooks);
65        self
66    }
67}
68
69/// Client identification info.
70#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
71pub struct ClientInfo {
72    /// Client name.
73    pub name: String,
74    /// Client version.
75    #[serde(skip_serializing_if = "Option::is_none")]
76    pub version: Option<String>,
77}
78
79/// Capabilities advertised by the client.
80#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
81pub struct ClientCapabilities {
82    /// Whether the client supports interactive questions.
83    #[serde(skip_serializing_if = "Option::is_none")]
84    pub supports_question: Option<bool>,
85    /// Whether the client supports plan mode.
86    #[serde(skip_serializing_if = "Option::is_none")]
87    pub supports_plan_mode: Option<bool>,
88}
89
90/// A hook subscription.
91#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
92pub struct WireHookSubscription {
93    /// Subscription id.
94    pub id: String,
95    /// Event name to subscribe to.
96    pub event: String,
97    /// Optional regex matcher for event targets.
98    #[serde(skip_serializing_if = "Option::is_none")]
99    pub matcher: Option<String>,
100    /// Timeout for client response in seconds, default 30.
101    #[serde(skip_serializing_if = "Option::is_none")]
102    pub timeout: Option<u32>,
103}
104
105/// An external tool definition.
106#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
107pub struct ExternalTool {
108    /// Tool name.
109    pub name: String,
110    /// Tool description.
111    pub description: String,
112    /// Parameter definition in JSON Schema format.
113    pub parameters: serde_json::Value,
114}
115
116/// Initialize response result.
117#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
118pub struct InitializeResult {
119    /// Protocol version supported by the server.
120    pub protocol_version: String,
121    /// Server identification.
122    pub server: ServerInfo,
123    /// Available slash commands.
124    pub slash_commands: Vec<SlashCommandInfo>,
125    /// External tools accepted/rejected by the server.
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub external_tools: Option<ExternalToolsResult>,
128    /// Server capabilities.
129    #[serde(skip_serializing_if = "Option::is_none")]
130    pub capabilities: Option<ServerCapabilities>,
131    /// Hook info from the server.
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub hooks: Option<HooksInfo>,
134}
135
136/// Server identification info.
137#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
138pub struct ServerInfo {
139    /// Server name.
140    pub name: String,
141    /// Server version.
142    pub version: String,
143}
144
145/// Information about a slash command.
146#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
147pub struct SlashCommandInfo {
148    /// Command name.
149    pub name: String,
150    /// Command description.
151    pub description: String,
152    /// Command aliases.
153    pub aliases: Vec<String>,
154}
155
156/// Result of registering external tools.
157#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
158pub struct ExternalToolsResult {
159    /// Accepted tool names.
160    pub accepted: Vec<String>,
161    /// Rejected tools with reasons.
162    pub rejected: Vec<RejectedExternalTool>,
163}
164
165/// A rejected external tool.
166#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
167pub struct RejectedExternalTool {
168    /// Tool name.
169    pub name: String,
170    /// Rejection reason.
171    pub reason: String,
172}
173
174/// Capabilities advertised by the server.
175#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
176pub struct ServerCapabilities {
177    /// Whether the server supports interactive questions.
178    #[serde(skip_serializing_if = "Option::is_none")]
179    pub supports_question: Option<bool>,
180}
181
182/// Hook information returned by the server.
183#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
184pub struct HooksInfo {
185    /// Supported hook event names.
186    pub supported_events: Vec<String>,
187    /// Configured hooks: subscription id → timeout.
188    pub configured: std::collections::HashMap<String, u32>,
189}
190
191// ============================================================================
192// Prompt
193// ============================================================================
194
195/// Prompt request parameters.
196#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
197pub struct PromptParams {
198    /// User input for the prompt.
199    pub user_input: UserInput,
200}
201
202/// Prompt response result.
203#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
204pub struct PromptResult {
205    /// Turn completion status.
206    pub status: PromptStatus,
207    /// Number of steps taken, if known.
208    #[serde(skip_serializing_if = "Option::is_none")]
209    pub steps: Option<u64>,
210}
211
212/// Status of a completed turn.
213#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
214#[serde(rename_all = "snake_case")]
215#[non_exhaustive]
216pub enum PromptStatus {
217    /// Turn finished successfully.
218    Finished,
219    /// Turn was cancelled.
220    Cancelled,
221    /// Turn reached the step limit.
222    MaxStepsReached,
223    /// The turn is still pending.
224    ///
225    /// Non-standard: observed in some server implementations. Not part of the
226    /// official v1.10 spec, which only defines `finished`, `cancelled`, and
227    /// `max_steps_reached`.
228    ///
229    /// May be removed in a future major version. Prefer matching this under a
230    /// `_` arm rather than relying on it.
231    Pending,
232    /// An unexpected end-of-stream occurred.
233    ///
234    /// Non-standard: observed in some server implementations. Not part of the
235    /// official v1.10 spec.
236    ///
237    /// May be removed in a future major version. Prefer matching this under a
238    /// `_` arm rather than relying on it.
239    UnexpectedEof,
240}
241
242// ============================================================================
243// Replay
244// ============================================================================
245
246/// Replay request parameters (empty).
247#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
248pub struct ReplayParams {}
249
250/// Replay response result.
251#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
252pub struct ReplayResult {
253    /// Replay completion status.
254    pub status: ReplayStatus,
255    /// Number of events replayed.
256    pub events: u64,
257    /// Number of requests replayed.
258    pub requests: u64,
259}
260
261/// Replay completion status.
262#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
263#[serde(rename_all = "snake_case")]
264#[non_exhaustive]
265pub enum ReplayStatus {
266    /// Replay finished successfully.
267    Finished,
268    /// Replay was cancelled.
269    Cancelled,
270}
271
272// ============================================================================
273// Steer
274// ============================================================================
275
276/// Steer request parameters.
277#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
278pub struct SteerParams {
279    /// Additional user input to steer the turn.
280    pub user_input: UserInput,
281}
282
283/// Steer response result.
284#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
285pub struct SteerResult {
286    /// Steering status.
287    pub status: SteerStatus,
288}
289
290/// Steer operation status.
291#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
292#[serde(rename_all = "snake_case")]
293#[non_exhaustive]
294pub enum SteerStatus {
295    /// Input was successfully steered.
296    Steered,
297}
298
299// ============================================================================
300// SetPlanMode
301// ============================================================================
302
303/// `SetPlanMode` request parameters.
304#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
305pub struct SetPlanModeParams {
306    /// Whether to enable plan mode.
307    pub enabled: bool,
308}
309
310/// `SetPlanMode` response result.
311#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
312pub struct SetPlanModeResult {
313    /// Operation status.
314    pub status: SetPlanModeStatus,
315    /// Whether plan mode is now active.
316    pub plan_mode: bool,
317}
318
319/// `SetPlanMode` operation status.
320#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
321#[serde(rename_all = "snake_case")]
322#[non_exhaustive]
323pub enum SetPlanModeStatus {
324    /// Operation succeeded.
325    Ok,
326}
327
328// ============================================================================
329// Cancel
330// ============================================================================
331
332/// Cancel request parameters (empty).
333#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
334pub struct CancelParams {}
335
336/// Cancel response result (empty).
337#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
338pub struct CancelResult {}