Skip to main content

unofficial_opencode_sdk/
models.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use serde_json::Value;
5
6/// A session returned by the legacy/current `/session` API.
7///
8/// Stable, commonly used fields are typed. Additional fields are retained so
9/// new upstream fields do not make an older SDK fail to deserialize.
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
11#[serde(rename_all = "camelCase")]
12pub struct Session {
13    pub id: String,
14    #[serde(default)]
15    pub slug: Option<String>,
16    #[serde(default)]
17    #[serde(rename = "projectID")]
18    pub project_id: Option<String>,
19    #[serde(default)]
20    #[serde(rename = "workspaceID")]
21    pub workspace_id: Option<String>,
22    #[serde(default)]
23    pub directory: Option<String>,
24    #[serde(default)]
25    pub path: Option<String>,
26    #[serde(default)]
27    #[serde(rename = "parentID")]
28    pub parent_id: Option<String>,
29    pub title: String,
30    #[serde(default)]
31    pub agent: Option<String>,
32    #[serde(default)]
33    pub version: Option<String>,
34    #[serde(default)]
35    pub time: Option<SessionTime>,
36    #[serde(flatten)]
37    pub extra: BTreeMap<String, Value>,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
41#[serde(rename_all = "camelCase")]
42pub struct SessionTime {
43    pub created: u64,
44    pub updated: u64,
45    #[serde(default)]
46    pub compacting: Option<u64>,
47    #[serde(default)]
48    pub archived: Option<u64>,
49}
50
51/// Body accepted by `POST /session` at the pinned OpenCode snapshot.
52#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
53#[serde(rename_all = "camelCase")]
54pub struct CreateSessionRequest {
55    #[serde(skip_serializing_if = "Option::is_none", rename = "parentID")]
56    pub parent_id: Option<String>,
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub title: Option<String>,
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub agent: Option<String>,
61    /// Kept as JSON for the first vertical slice because this nested contract
62    /// is one of the schemas whose OpenAPI 3.1 unions are not handled correctly
63    /// by the evaluated whole-document Rust generators.
64    #[serde(skip_serializing_if = "Option::is_none")]
65    pub model: Option<Value>,
66    #[serde(skip_serializing_if = "Option::is_none")]
67    pub metadata: Option<BTreeMap<String, Value>>,
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub permission: Option<Value>,
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
73#[serde(rename_all = "camelCase")]
74pub struct ModelRef {
75    #[serde(rename = "providerID")]
76    pub provider_id: String,
77    #[serde(rename = "modelID")]
78    pub model_id: String,
79}
80
81#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
82#[serde(tag = "type", rename_all = "lowercase")]
83pub enum PromptPart {
84    Text {
85        #[serde(skip_serializing_if = "Option::is_none")]
86        id: Option<String>,
87        text: String,
88        #[serde(skip_serializing_if = "Option::is_none")]
89        synthetic: Option<bool>,
90        #[serde(skip_serializing_if = "Option::is_none")]
91        ignored: Option<bool>,
92        #[serde(skip_serializing_if = "Option::is_none")]
93        time: Option<Value>,
94        #[serde(skip_serializing_if = "Option::is_none")]
95        metadata: Option<BTreeMap<String, Value>>,
96    },
97    /// Escape hatch for current non-text prompt parts (file/agent/subtask).
98    /// The outer request remains typed while this vertical slice avoids
99    /// pretending unsupported OpenAPI unions are exhaustively generated.
100    #[serde(untagged)]
101    Other(Value),
102}
103
104impl PromptPart {
105    pub fn text(text: impl Into<String>) -> Self {
106        Self::Text {
107            id: None,
108            text: text.into(),
109            synthetic: None,
110            ignored: None,
111            time: None,
112            metadata: None,
113        }
114    }
115}
116
117/// Body accepted by `POST /session/{id}/message`.
118#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
119#[serde(rename_all = "camelCase")]
120pub struct PromptRequest {
121    #[serde(skip_serializing_if = "Option::is_none")]
122    #[serde(rename = "messageID")]
123    pub message_id: Option<String>,
124    #[serde(skip_serializing_if = "Option::is_none")]
125    pub model: Option<ModelRef>,
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub agent: Option<String>,
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub no_reply: Option<bool>,
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub tools: Option<BTreeMap<String, bool>>,
132    #[serde(skip_serializing_if = "Option::is_none")]
133    pub system: Option<String>,
134    #[serde(skip_serializing_if = "Option::is_none")]
135    pub variant: Option<String>,
136    #[serde(default)]
137    pub parts: Vec<PromptPart>,
138}
139
140/// Prompt/message response shape. Message info and parts are intentionally
141/// retained losslessly until the full upstream union generator is proven.
142#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
143pub struct MessageWithParts {
144    pub info: Value,
145    #[serde(default)]
146    pub parts: Vec<Value>,
147}