openai_rst/thread.rs
1//! This module defines the structures and methods for creating and modifying threads, as well as the message format used within threads.
2//! It includes:
3//! - `CreateThreadRequest`: Struct for creating a new thread with optional messages and metadata.
4//! - `ThreadObject`: Struct representing a thread object with various attributes.
5//! - `Message`: Struct for messages within a thread, including role, content, and optional metadata.
6//! - `ModifyThreadRequest`: Struct for modifying an existing thread's metadata.
7//! - `impl_builder_methods!`: Macro for generating builder methods for structs.
8
9use crate::common::MessageRole;
10use serde::{Deserialize, Serialize};
11use std::collections::HashMap;
12
13use crate::impl_builder_methods;
14
15/// Represents a request to create a new thread.
16#[derive(Debug, Serialize, Clone)]
17pub struct CreateThreadRequest {
18 /// Optional list of messages in the thread.
19 #[serde(skip_serializing_if = "Option::is_none")]
20 pub messages: Option<Vec<Message>>,
21 /// Optional metadata for the thread.
22 #[serde(skip_serializing_if = "Option::is_none")]
23 pub metadata: Option<HashMap<String, String>>,
24}
25
26impl CreateThreadRequest {
27 /// Creates a new `CreateThreadRequest`.
28 pub fn new() -> Self {
29 Self {
30 messages: None,
31 metadata: None,
32 }
33 }
34}
35
36impl Default for CreateThreadRequest {
37 /// Provides a default implementation for `CreateThreadRequest`.
38 fn default() -> Self {
39 Self::new()
40 }
41}
42
43impl_builder_methods!(
44 CreateThreadRequest,
45 messages: Vec<Message>,
46 metadata: HashMap<String, String>
47);
48
49/// Represents a thread object with various attributes.
50#[derive(Debug, Deserialize, Serialize)]
51pub struct ThreadObject {
52 /// Unique identifier for the thread.
53 pub id: String,
54 /// Object type, typically "thread".
55 pub object: String,
56 /// Timestamp of when the thread was created.
57 pub created_at: i64,
58 /// Metadata associated with the thread.
59 pub metadata: HashMap<String, String>,
60 /// Optional headers from the response.
61 pub headers: Option<HashMap<String, String>>,
62}
63
64/// Represents a message within a thread.
65#[derive(Debug, Deserialize, Serialize, Clone)]
66pub struct Message {
67 /// Role of the message sender.
68 pub role: MessageRole,
69 /// Content of the message.
70 pub content: String,
71 /// Optional file IDs associated with the message.
72 #[serde(skip_serializing_if = "Option::is_none")]
73 pub file_ids: Option<String>,
74 /// Optional metadata for the message.
75 #[serde(skip_serializing_if = "Option::is_none")]
76 pub metadata: Option<HashMap<String, String>>,
77}
78
79/// Represents a request to modify an existing thread's metadata.
80#[derive(Default, Debug, Serialize, Clone)]
81pub struct ModifyThreadRequest {
82 /// Optional metadata to update in the thread.
83 #[serde(skip_serializing_if = "Option::is_none")]
84 pub metadata: Option<HashMap<String, String>>,
85}
86
87impl ModifyThreadRequest {
88 /// Creates a new `ModifyThreadRequest`.
89 pub fn new() -> Self {
90 Self { metadata: None }
91 }
92}
93
94impl_builder_methods!(
95 ModifyThreadRequest,
96 metadata: HashMap<String, String>
97);