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