forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmood.rs
More file actions
161 lines (150 loc) · 5.64 KB
/
Copy pathmood.rs
File metadata and controls
161 lines (150 loc) · 5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use echomirror_core::mood::{self, GetMoodHistoryOptions, LogMoodPayload};
use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyAny;
use crate::client::PyEchoMirrorClient;
use crate::errors::to_py_err;
use crate::types::{PyAiReflection, PyMoodEntry, PyMoodHistoryPage, PyMoodStreak, PyMoodSummary};
/// Mood logging, history, streaks, summaries, and AI reflections.
///
/// ```python
/// mood = MoodClient(client)
/// entry = await mood.log(score=8, note="Great day", tags=["work"])
/// ```
#[pyclass(name = "MoodClient")]
#[derive(Clone)]
pub struct PyMoodClient {
client: PyEchoMirrorClient,
}
#[pymethods]
impl PyMoodClient {
#[new]
pub fn new(client: PyEchoMirrorClient) -> Self {
Self { client }
}
/// Log a mood entry for the authenticated user. `score` must be 1-10.
#[pyo3(signature = (score, note=None, tags=None))]
fn log<'py>(
&self,
py: Python<'py>,
score: u8,
note: Option<String>,
tags: Option<Vec<String>>,
) -> PyResult<Bound<'py, PyAny>> {
if !(1..=10).contains(&score) {
return Err(PyValueError::new_err("score must be between 1 and 10"));
}
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let payload = LogMoodPayload {
score,
note,
tags: tags.unwrap_or_default(),
};
let entry = mood::log_mood(&inner, payload).await.map_err(to_py_err)?;
Ok(PyMoodEntry::from(entry))
})
}
/// Get paginated mood history for the authenticated user.
#[pyo3(signature = (limit=None, offset=None, from_date=None, to_date=None, tags=None, min_score=None, max_score=None))]
#[allow(clippy::too_many_arguments)]
fn get_history<'py>(
&self,
py: Python<'py>,
limit: Option<u32>,
offset: Option<u32>,
from_date: Option<String>,
to_date: Option<String>,
tags: Option<Vec<String>>,
min_score: Option<u8>,
max_score: Option<u8>,
) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let options = GetMoodHistoryOptions {
limit,
offset,
from: from_date,
to: to_date,
tags: tags.unwrap_or_default(),
min_score,
max_score,
};
let page = mood::get_mood_history(&inner, options)
.await
.map_err(to_py_err)?;
Ok(PyMoodHistoryPage {
entries: page.entries.into_iter().map(Into::into).collect(),
total: page.total,
})
})
}
/// Get a single mood entry by ID.
fn get_entry<'py>(&self, py: Python<'py>, entry_id: String) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let entry = mood::get_mood_entry(&inner, &entry_id)
.await
.map_err(to_py_err)?;
Ok(PyMoodEntry::from(entry))
})
}
/// Delete a mood entry.
fn delete_entry<'py>(&self, py: Python<'py>, entry_id: String) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
mood::delete_mood_entry(&inner, &entry_id)
.await
.map_err(to_py_err)?;
Ok(())
})
}
/// Get the user's current and longest streak.
fn get_streak<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let streak = mood::get_mood_streak(&inner).await.map_err(to_py_err)?;
Ok(PyMoodStreak::from(streak))
})
}
/// Get aggregated mood statistics for a time period ("week" | "month" | "year" | "all").
#[pyo3(signature = (period="week".to_string()))]
fn get_summary<'py>(&self, py: Python<'py>, period: String) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let summary = mood::get_mood_summary(&inner, &period)
.await
.map_err(to_py_err)?;
Ok(PyMoodSummary::from(summary))
})
}
/// Request an AI reflection for a mood entry. Reflections are generated
/// asynchronously — poll `get_reflection` until it returns non-`None`.
fn request_reflection<'py>(
&self,
py: Python<'py>,
entry_id: String,
) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let reflection = mood::request_ai_reflection(&inner, &entry_id)
.await
.map_err(to_py_err)?;
Ok(PyAiReflection::from(reflection))
})
}
/// Get the AI reflection for a mood entry, once generated (`None` if not ready yet).
fn get_reflection<'py>(
&self,
py: Python<'py>,
entry_id: String,
) -> PyResult<Bound<'py, PyAny>> {
let inner = self.client.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
let reflection = mood::get_ai_reflection(&inner, &entry_id)
.await
.map_err(to_py_err)?;
Ok(reflection.map(PyAiReflection::from))
})
}
}