claude_code_sdk_rust/
sessions.rs1use crate::error::Result;
4use crate::internal::sessions_fs;
5use serde::{Deserialize, Serialize};
6
7pub mod store;
8pub use store::*;
9pub mod store_fork;
10pub use store_fork::*;
11pub mod import;
12pub use import::*;
13pub mod local_fork;
14pub use local_fork::*;
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct SessionInfo {
19 pub id: String,
20 pub title: String,
21 pub created_at: String,
22 pub updated_at: String,
23 pub message_count: usize,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct SessionMessage {
29 pub id: String,
30 pub role: String,
31 pub content: String,
32 pub timestamp: String,
33}
34
35#[derive(Debug, Clone, Default)]
37pub struct ListSessionsOptions {
38 pub directory: Option<String>,
39 pub limit: Option<usize>,
40 pub offset: Option<usize>,
41}
42
43#[derive(Debug, Clone)]
45pub struct SessionQueryOptions {
46 pub session_id: String,
47 pub include_messages: bool,
48 pub directory: Option<String>,
49 pub limit: Option<usize>,
50 pub offset: Option<usize>,
51}
52
53#[derive(Debug, Clone)]
55pub struct SessionMutationOptions {
56 pub session_id: String,
57 pub directory: Option<String>,
58}
59
60pub async fn list_sessions(opts: &ListSessionsOptions) -> Result<Vec<SessionInfo>> {
62 sessions_fs::list_sessions(opts).await
63}
64
65pub async fn get_session_info(session_id: &str, opts: &SessionQueryOptions) -> Result<SessionInfo> {
67 sessions_fs::get_session_info(session_id, opts.directory.as_deref()).await
68}
69
70pub async fn get_session_messages(
72 session_id: &str,
73 opts: &SessionQueryOptions,
74) -> Result<Vec<SessionMessage>> {
75 sessions_fs::get_session_messages(
76 session_id,
77 opts.directory.as_deref(),
78 opts.limit,
79 opts.offset.unwrap_or(0),
80 )
81 .await
82}
83
84pub async fn list_subagents(session_id: &str, opts: &SessionQueryOptions) -> Result<Vec<String>> {
86 sessions_fs::list_subagents(session_id, opts.directory.as_deref()).await
87}
88
89pub async fn get_subagent_messages(
91 session_id: &str,
92 agent_id: &str,
93 opts: &SessionQueryOptions,
94) -> Result<Vec<SessionMessage>> {
95 sessions_fs::get_subagent_messages(
96 session_id,
97 agent_id,
98 opts.directory.as_deref(),
99 opts.limit,
100 opts.offset.unwrap_or(0),
101 )
102 .await
103}
104
105pub async fn rename_session(
107 session_id: &str,
108 title: &str,
109 opts: &SessionMutationOptions,
110) -> Result<()> {
111 sessions_fs::rename_session(session_id, title, opts.directory.as_deref()).await
112}
113
114pub async fn tag_session(
116 session_id: &str,
117 tag: Option<&str>,
118 opts: &SessionMutationOptions,
119) -> Result<()> {
120 sessions_fs::tag_session(session_id, tag, opts.directory.as_deref()).await
121}
122
123pub async fn fork_session(
125 session_id: &str,
126 opts: &SessionMutationOptions,
127 up_to_message_id: Option<&str>,
128 title: Option<&str>,
129) -> Result<LocalForkSessionResult> {
130 local_fork::fork_session(
131 session_id,
132 opts.directory.as_deref(),
133 up_to_message_id,
134 title,
135 )
136 .await
137}
138
139pub async fn delete_session(session_id: &str, opts: &SessionMutationOptions) -> Result<()> {
141 sessions_fs::delete_session(session_id, opts.directory.as_deref()).await
142}