async_openai/types/chatkit/
session.rs

1use std::collections::HashMap;
2
3use derive_builder::Builder;
4use serde::{Deserialize, Serialize};
5
6use crate::error::OpenAIError;
7
8/// Represents a ChatKit session and its resolved configuration.
9#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
10pub struct ChatSessionResource {
11    /// Identifier for the ChatKit session.
12    pub id: String,
13    /// Type discriminator that is always `chatkit.session`.
14    #[serde(default = "default_session_object")]
15    pub object: String,
16    /// Unix timestamp (in seconds) for when the session expires.
17    pub expires_at: i64,
18    /// Ephemeral client secret that authenticates session requests.
19    pub client_secret: String,
20    /// Workflow metadata for the session.
21    pub workflow: ChatkitWorkflow,
22    /// User identifier associated with the session.
23    pub user: String,
24    /// Resolved rate limit values.
25    pub rate_limits: ChatSessionRateLimits,
26    /// Convenience copy of the per-minute request limit.
27    pub max_requests_per_1_minute: i32,
28    /// Current lifecycle state of the session.
29    pub status: ChatSessionStatus,
30    /// Resolved ChatKit feature configuration for the session.
31    pub chatkit_configuration: ChatSessionChatkitConfiguration,
32}
33
34fn default_session_object() -> String {
35    "chatkit.session".to_string()
36}
37
38/// Workflow metadata and state returned for the session.
39#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
40pub struct ChatkitWorkflow {
41    /// Identifier of the workflow backing the session.
42    pub id: String,
43    /// Specific workflow version used for the session. Defaults to null when using the latest deployment.
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub version: Option<String>,
46    /// State variable key-value pairs applied when invoking the workflow. Defaults to null when no overrides were provided.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub state_variables: Option<HashMap<String, serde_json::Value>>,
49    /// Tracing settings applied to the workflow.
50    pub tracing: ChatkitWorkflowTracing,
51}
52
53/// Controls diagnostic tracing during the session.
54#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
55pub struct ChatkitWorkflowTracing {
56    /// Indicates whether tracing is enabled.
57    pub enabled: bool,
58}
59
60/// Active per-minute request limit for the session.
61#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
62pub struct ChatSessionRateLimits {
63    /// Maximum allowed requests per one-minute window.
64    pub max_requests_per_1_minute: i32,
65}
66
67/// Current lifecycle state of the session.
68#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
69#[serde(rename_all = "snake_case")]
70pub enum ChatSessionStatus {
71    Active,
72    Expired,
73    Cancelled,
74}
75
76/// ChatKit configuration for the session.
77#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
78pub struct ChatSessionChatkitConfiguration {
79    /// Automatic thread titling preferences.
80    pub automatic_thread_titling: ChatSessionAutomaticThreadTitling,
81    /// Upload settings for the session.
82    pub file_upload: ChatSessionFileUpload,
83    /// History retention configuration.
84    pub history: ChatSessionHistory,
85}
86
87/// Automatic thread title preferences for the session.
88#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
89pub struct ChatSessionAutomaticThreadTitling {
90    /// Whether automatic thread titling is enabled.
91    pub enabled: bool,
92}
93
94/// Upload permissions and limits applied to the session.
95#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
96pub struct ChatSessionFileUpload {
97    /// Indicates if uploads are enabled for the session.
98    pub enabled: bool,
99    /// Maximum upload size in megabytes.
100    #[serde(skip_serializing_if = "Option::is_none")]
101    pub max_file_size: Option<i32>,
102    /// Maximum number of uploads allowed during the session.
103    #[serde(skip_serializing_if = "Option::is_none")]
104    pub max_files: Option<i32>,
105}
106
107/// History retention preferences returned for the session.
108#[derive(Clone, Serialize, Debug, Deserialize, PartialEq)]
109pub struct ChatSessionHistory {
110    /// Indicates if chat history is persisted for the session.
111    pub enabled: bool,
112    /// Number of prior threads surfaced in history views. Defaults to null when all history is retained.
113    #[serde(skip_serializing_if = "Option::is_none")]
114    pub recent_threads: Option<i32>,
115}
116
117/// Parameters for provisioning a new ChatKit session.
118#[derive(Clone, Serialize, Debug, Deserialize, Builder, PartialEq, Default)]
119#[builder(name = "CreateChatSessionRequestArgs")]
120#[builder(pattern = "mutable")]
121#[builder(setter(into, strip_option), default)]
122#[builder(derive(Debug))]
123#[builder(build_fn(error = "OpenAIError"))]
124pub struct CreateChatSessionBody {
125    /// Workflow that powers the session.
126    pub workflow: WorkflowParam,
127    /// A free-form string that identifies your end user; ensures this Session can access other objects that have the same `user` scope.
128    pub user: String,
129    /// Optional override for session expiration timing in seconds from creation. Defaults to 10 minutes.
130    #[serde(skip_serializing_if = "Option::is_none")]
131    pub expires_after: Option<ExpiresAfterParam>,
132    /// Optional override for per-minute request limits. When omitted, defaults to 10.
133    #[serde(skip_serializing_if = "Option::is_none")]
134    pub rate_limits: Option<RateLimitsParam>,
135    /// Optional overrides for ChatKit runtime configuration features
136    #[serde(skip_serializing_if = "Option::is_none")]
137    pub chatkit_configuration: Option<ChatkitConfigurationParam>,
138}
139
140/// Workflow reference and overrides applied to the chat session.
141#[derive(Clone, Serialize, Debug, Deserialize, Builder, PartialEq, Default)]
142#[builder(name = "WorkflowParamArgs")]
143#[builder(pattern = "mutable")]
144#[builder(setter(into, strip_option), default)]
145#[builder(derive(Debug))]
146#[builder(build_fn(error = "OpenAIError"))]
147pub struct WorkflowParam {
148    /// Identifier for the workflow invoked by the session.
149    pub id: String,
150    /// Specific workflow version to run. Defaults to the latest deployed version.
151    #[serde(skip_serializing_if = "Option::is_none")]
152    pub version: Option<String>,
153    /// State variables forwarded to the workflow. Keys may be up to 64 characters, values must be primitive types, and the map defaults to an empty object.
154    #[serde(skip_serializing_if = "Option::is_none")]
155    pub state_variables: Option<HashMap<String, serde_json::Value>>,
156    /// Optional tracing overrides for the workflow invocation. When omitted, tracing is enabled by default.
157    #[serde(skip_serializing_if = "Option::is_none")]
158    pub tracing: Option<WorkflowTracingParam>,
159}
160
161/// Controls diagnostic tracing during the session.
162#[derive(Clone, Serialize, Default, Debug, Deserialize, Builder, PartialEq)]
163#[builder(name = "WorkflowTracingParamArgs")]
164#[builder(pattern = "mutable")]
165#[builder(setter(into, strip_option), default)]
166#[builder(derive(Debug))]
167#[builder(build_fn(error = "OpenAIError"))]
168pub struct WorkflowTracingParam {
169    /// Whether tracing is enabled during the session. Defaults to true.
170    #[serde(skip_serializing_if = "Option::is_none")]
171    pub enabled: Option<bool>,
172}
173
174/// Controls when the session expires relative to an anchor timestamp.
175#[derive(Clone, Serialize, Debug, Deserialize, Builder, PartialEq, Default)]
176#[builder(name = "ExpiresAfterParamArgs")]
177#[builder(pattern = "mutable")]
178#[builder(setter(into, strip_option), default)]
179#[builder(derive(Debug))]
180#[builder(build_fn(error = "OpenAIError"))]
181pub struct ExpiresAfterParam {
182    /// Base timestamp used to calculate expiration. Currently fixed to `created_at`.
183    #[serde(default = "default_anchor")]
184    #[builder(default = "default_anchor()")]
185    pub anchor: String,
186    /// Number of seconds after the anchor when the session expires.
187    pub seconds: i32,
188}
189
190fn default_anchor() -> String {
191    "created_at".to_string()
192}
193
194/// Controls request rate limits for the session.
195#[derive(Clone, Serialize, Debug, Deserialize, Builder, PartialEq, Default)]
196#[builder(name = "RateLimitsParamArgs")]
197#[builder(pattern = "mutable")]
198#[builder(setter(into, strip_option), default)]
199#[builder(derive(Debug))]
200#[builder(build_fn(error = "OpenAIError"))]
201pub struct RateLimitsParam {
202    /// Maximum number of requests allowed per minute for the session. Defaults to 10.
203    #[serde(skip_serializing_if = "Option::is_none")]
204    pub max_requests_per_1_minute: Option<i32>,
205}
206
207/// Optional per-session configuration settings for ChatKit behavior.
208#[derive(Clone, Serialize, Debug, Deserialize, Builder, PartialEq, Default)]
209#[builder(name = "ChatkitConfigurationParamArgs")]
210#[builder(pattern = "mutable")]
211#[builder(setter(into, strip_option), default)]
212#[builder(derive(Debug))]
213#[builder(build_fn(error = "OpenAIError"))]
214pub struct ChatkitConfigurationParam {
215    /// Configuration for automatic thread titling. When omitted, automatic thread titling is enabled by default.
216    #[serde(skip_serializing_if = "Option::is_none")]
217    pub automatic_thread_titling: Option<AutomaticThreadTitlingParam>,
218    /// Configuration for upload enablement and limits. When omitted, uploads are disabled by default (max_files 10, max_file_size 512 MB).
219    #[serde(skip_serializing_if = "Option::is_none")]
220    pub file_upload: Option<FileUploadParam>,
221    /// Configuration for chat history retention. When omitted, history is enabled by default with no limit on recent_threads (null).
222    #[serde(skip_serializing_if = "Option::is_none")]
223    pub history: Option<HistoryParam>,
224}
225
226/// Controls whether ChatKit automatically generates thread titles.
227#[derive(Clone, Serialize, Debug, Deserialize, Builder, PartialEq, Default)]
228#[builder(name = "AutomaticThreadTitlingParamArgs")]
229#[builder(pattern = "mutable")]
230#[builder(setter(into, strip_option), default)]
231#[builder(derive(Debug))]
232#[builder(build_fn(error = "OpenAIError"))]
233pub struct AutomaticThreadTitlingParam {
234    /// Enable automatic thread title generation. Defaults to true.
235    #[serde(skip_serializing_if = "Option::is_none")]
236    pub enabled: Option<bool>,
237}
238
239/// Controls whether users can upload files.
240#[derive(Clone, Serialize, Debug, Deserialize, Builder, PartialEq, Default)]
241#[builder(name = "FileUploadParamArgs")]
242#[builder(pattern = "mutable")]
243#[builder(setter(into, strip_option), default)]
244#[builder(derive(Debug))]
245#[builder(build_fn(error = "OpenAIError"))]
246pub struct FileUploadParam {
247    /// Enable uploads for this session. Defaults to false.
248    #[serde(skip_serializing_if = "Option::is_none")]
249    pub enabled: Option<bool>,
250    /// Maximum size in megabytes for each uploaded file. Defaults to 512 MB, which is the maximum allowable size.
251    #[serde(skip_serializing_if = "Option::is_none")]
252    pub max_file_size: Option<i32>,
253    /// Maximum number of files that can be uploaded to the session. Defaults to 10.
254    #[serde(skip_serializing_if = "Option::is_none")]
255    pub max_files: Option<i32>,
256}
257
258/// Controls how much historical context is retained for the session.
259#[derive(Clone, Serialize, Debug, Deserialize, Builder, PartialEq, Default)]
260#[builder(name = "HistoryParamArgs")]
261#[builder(pattern = "mutable")]
262#[builder(setter(into, strip_option), default)]
263#[builder(derive(Debug))]
264#[builder(build_fn(error = "OpenAIError"))]
265pub struct HistoryParam {
266    /// Enables chat users to access previous ChatKit threads. Defaults to true.
267    #[serde(skip_serializing_if = "Option::is_none")]
268    pub enabled: Option<bool>,
269    /// Number of recent ChatKit threads users have access to. Defaults to unlimited when unset.
270    #[serde(skip_serializing_if = "Option::is_none")]
271    pub recent_threads: Option<i32>,
272}