@r_masseater/masseater-utils 0.8.2 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. package/README.md +46 -40
  2. package/dist/browser-test/vite.mjs +1 -29
  3. package/dist/browser-test-vite-DS3fTcyM.mjs +29 -0
  4. package/dist/browser-test-vite-DS3fTcyM.mjs.map +1 -0
  5. package/dist/button-980Op0uF.mjs +30 -0
  6. package/dist/button-980Op0uF.mjs.map +1 -0
  7. package/dist/desktop-shell/media-notes/protocol.d.mts +2 -0
  8. package/dist/desktop-shell/media-notes/protocol.mjs +1 -0
  9. package/dist/desktop-shell/media-notes.d.mts +125 -0
  10. package/dist/desktop-shell/media-notes.mjs +865 -0
  11. package/dist/desktop-shell/media-notes.mjs.map +1 -0
  12. package/dist/desktop-shell/release-cli.d.mts +1 -0
  13. package/dist/desktop-shell/release-cli.mjs +290 -0
  14. package/dist/desktop-shell/release-cli.mjs.map +1 -0
  15. package/dist/desktop-shell/telemetry.d.mts +5 -0
  16. package/dist/desktop-shell/telemetry.mjs +55 -0
  17. package/dist/desktop-shell/telemetry.mjs.map +1 -0
  18. package/dist/desktop-shell/vite.d.mts +24 -0
  19. package/dist/desktop-shell/vite.mjs +58 -0
  20. package/dist/desktop-shell/vite.mjs.map +1 -0
  21. package/dist/index-BtcxUsGh.d.mts +4 -0
  22. package/dist/index-CjUS4pmA.d.mts +59 -0
  23. package/dist/lapse.mjs +1 -1
  24. package/dist/media-protocol-CMyatw7A.mjs +32 -0
  25. package/dist/media-protocol-CMyatw7A.mjs.map +1 -0
  26. package/dist/node-main-Bwril94o.mjs +19 -0
  27. package/dist/node-main-Bwril94o.mjs.map +1 -0
  28. package/dist/node-main.mjs +1 -18
  29. package/dist/plugin-kit.mjs +22 -22
  30. package/dist/plugin-kit.mjs.map +1 -1
  31. package/dist/security-headers.d.mts +2 -1
  32. package/dist/ui/button.d.mts +1 -1
  33. package/dist/ui/button.mjs +1 -30
  34. package/dist/ui/next-page-trigger.mjs +1 -1
  35. package/dist/ui/page-footer.d.mts +1 -1
  36. package/dist/ui/page-header.d.mts +1 -1
  37. package/dist/ui/table.d.mts +1 -1
  38. package/dist/ui/web-analytics.d.mts +1 -0
  39. package/dist/vitest-otlp/workers.mjs +1 -1
  40. package/dist/workers-tracing/alchemy.d.mts +1 -1
  41. package/native/desktop-shell/Cargo.toml +38 -0
  42. package/native/desktop-shell/src/app/builder.rs +39 -0
  43. package/native/desktop-shell/src/app/mod.rs +3 -0
  44. package/native/desktop-shell/src/entities/access_token.rs +77 -0
  45. package/native/desktop-shell/src/entities/mod.rs +1 -0
  46. package/native/desktop-shell/src/features/mod.rs +4 -0
  47. package/native/desktop-shell/src/features/record_telemetry/appender.rs +127 -0
  48. package/native/desktop-shell/src/features/record_telemetry/forwarder.rs +183 -0
  49. package/native/desktop-shell/src/features/record_telemetry/invoke.rs +80 -0
  50. package/native/desktop-shell/src/features/record_telemetry/mod.rs +231 -0
  51. package/native/desktop-shell/src/features/serve_screen/mod.rs +135 -0
  52. package/native/desktop-shell/src/features/serve_tools/mod.rs +37 -0
  53. package/native/desktop-shell/src/features/update_app/mod.rs +53 -0
  54. package/native/desktop-shell/src/lib.rs +17 -0
  55. package/native/desktop-shell/src/shared/lib/bearer.rs +49 -0
  56. package/native/desktop-shell/src/shared/lib/host.rs +1 -0
  57. package/native/desktop-shell/src/shared/lib/mod.rs +5 -0
  58. package/native/desktop-shell/src/shared/mod.rs +2 -0
  59. package/native/desktop-shell/src/shared/ui/log.rs +15 -0
  60. package/native/desktop-shell/src/shared/ui/menu.rs +37 -0
  61. package/native/desktop-shell/src/shared/ui/mod.rs +7 -0
  62. package/native/desktop-shell/src/shared/ui/window.rs +49 -0
  63. package/native/media-notes/Cargo.toml +36 -0
  64. package/native/media-notes/src/entities/media_file.rs +89 -0
  65. package/native/media-notes/src/entities/media_notes.rs +31 -0
  66. package/native/media-notes/src/entities/mod.rs +3 -0
  67. package/native/media-notes/src/entities/source/credit.rs +129 -0
  68. package/native/media-notes/src/entities/source/link.rs +194 -0
  69. package/native/media-notes/src/entities/source/mod.rs +5 -0
  70. package/native/media-notes/src/features/mod.rs +2 -0
  71. package/native/media-notes/src/features/open_media/mod.rs +88 -0
  72. package/native/media-notes/src/features/share_mcp_access.rs +15 -0
  73. package/native/media-notes/src/lib.rs +12 -0
  74. package/native/media-notes/src/shared/db/mod.rs +3 -0
  75. package/native/media-notes/src/shared/db/store.rs +276 -0
  76. package/native/media-notes/src/shared/lib/failure.rs +55 -0
  77. package/native/media-notes/src/shared/lib/mcp.rs +34 -0
  78. package/native/media-notes/src/shared/lib/mod.rs +5 -0
  79. package/native/media-notes/src/shared/mod.rs +3 -0
  80. package/native/media-notes/src/shared/protocol.rs +69 -0
  81. package/package.json +129 -56
  82. package/skills/build-tauri-app/SKILL.md +58 -0
  83. package/dist/browser-test/vite.mjs.map +0 -1
  84. package/dist/node-main.mjs.map +0 -1
  85. package/dist/ui/button.mjs.map +0 -1
@@ -0,0 +1,276 @@
1
+ use std::fs;
2
+ use std::path::Path;
3
+ use std::sync::{Arc, Mutex, PoisonError};
4
+ use std::time::{SystemTime, UNIX_EPOCH};
5
+
6
+ use rusqlite::{Connection, OptionalExtension, params};
7
+ use serde::Serialize;
8
+ use serde::de::DeserializeOwned;
9
+
10
+ use crate::shared::lib::Outcome;
11
+ use crate::shared::protocol::Source;
12
+
13
+ const UNSOURCED: &str = "";
14
+
15
+ #[derive(Clone)]
16
+ pub struct Store {
17
+ connection: Arc<Mutex<Connection>>,
18
+ table: &'static str,
19
+ }
20
+
21
+ #[must_use]
22
+ pub fn now() -> i64 {
23
+ SystemTime::now()
24
+ .duration_since(UNIX_EPOCH)
25
+ .map_or(0, |elapsed| {
26
+ i64::try_from(elapsed.as_millis()).unwrap_or(i64::MAX)
27
+ })
28
+ }
29
+
30
+ #[must_use]
31
+ pub fn fresh_id() -> String {
32
+ uuid::Uuid::now_v7().to_string()
33
+ }
34
+
35
+ #[must_use]
36
+ pub fn is_id(text: &str) -> bool {
37
+ uuid::Uuid::try_parse(text).is_ok_and(|id| id.to_string() == text)
38
+ }
39
+
40
+ pub struct Row<'a, T> {
41
+ pub id: &'a str,
42
+ pub source: &'a Source,
43
+ pub at: i64,
44
+ pub value: &'a T,
45
+ }
46
+
47
+ fn decoded<T: DeserializeOwned>(rows: &[String]) -> Outcome<Vec<T>> {
48
+ rows.iter()
49
+ .map(|body| Ok(serde_json::from_str(body)?))
50
+ .collect()
51
+ }
52
+
53
+ impl Store {
54
+ pub fn open(directory: &Path, file: &str, table: &'static str) -> Outcome<Self> {
55
+ fs::create_dir_all(directory)?;
56
+ Self::prepare(Connection::open(directory.join(file))?, table)
57
+ }
58
+
59
+ pub fn in_memory(table: &'static str) -> Outcome<Self> {
60
+ Self::prepare(Connection::open_in_memory()?, table)
61
+ }
62
+
63
+ fn prepare(connection: Connection, table: &'static str) -> Outcome<Self> {
64
+ connection.execute_batch(&format!(
65
+ "CREATE TABLE IF NOT EXISTS {table} (
66
+ id TEXT PRIMARY KEY NOT NULL,
67
+ site TEXT NOT NULL,
68
+ source_id TEXT NOT NULL,
69
+ created_at INTEGER NOT NULL,
70
+ body TEXT NOT NULL
71
+ );
72
+ CREATE INDEX IF NOT EXISTS {table}_source ON {table} (site, source_id, created_at);"
73
+ ))?;
74
+ Ok(Self {
75
+ connection: Arc::new(Mutex::new(connection)),
76
+ table,
77
+ })
78
+ }
79
+
80
+ fn with<T>(&self, work: impl FnOnce(&Connection) -> Outcome<T>) -> Outcome<T> {
81
+ let connection = self
82
+ .connection
83
+ .lock()
84
+ .unwrap_or_else(PoisonError::into_inner);
85
+ work(&connection)
86
+ }
87
+
88
+ pub fn list<T: DeserializeOwned>(&self, source: Option<&Source>) -> Outcome<Vec<T>> {
89
+ let table = self.table;
90
+ self.with(|connection| {
91
+ let rows = match source {
92
+ None => connection
93
+ .prepare(&format!(
94
+ "SELECT body FROM {table} ORDER BY created_at DESC, id DESC"
95
+ ))?
96
+ .query_map([], |row| row.get(0))?
97
+ .collect::<rusqlite::Result<Vec<String>>>()?,
98
+ Some(source) => connection
99
+ .prepare(&format!(
100
+ "SELECT body FROM {table} WHERE site = ?1 AND source_id = ?2 ORDER BY created_at DESC, id DESC"
101
+ ))?
102
+ .query_map(params![source.site.as_str(), source.id], |row| row.get(0))?
103
+ .collect::<rusqlite::Result<Vec<String>>>()?,
104
+ };
105
+ decoded(&rows)
106
+ })
107
+ }
108
+
109
+ pub fn get<T: DeserializeOwned>(&self, id: &str) -> Outcome<Option<T>> {
110
+ let table = self.table;
111
+ let body = self.with(|connection| {
112
+ Ok(connection
113
+ .query_row(
114
+ &format!("SELECT body FROM {table} WHERE id = ?1"),
115
+ params![id],
116
+ |row| row.get::<_, String>(0),
117
+ )
118
+ .optional()?)
119
+ })?;
120
+ body.map(|text| Ok(serde_json::from_str(&text)?))
121
+ .transpose()
122
+ }
123
+
124
+ fn insert<T: Serialize>(
125
+ &self,
126
+ id: &str,
127
+ source: (&str, &str),
128
+ at: i64,
129
+ value: &T,
130
+ ) -> Outcome<()> {
131
+ let table = self.table;
132
+ let body = serde_json::to_string(value)?;
133
+ self.with(|connection| {
134
+ connection.execute(
135
+ &format!(
136
+ "INSERT OR REPLACE INTO {table} (id, site, source_id, created_at, body) VALUES (?1, ?2, ?3, ?4, ?5)"
137
+ ),
138
+ params![id, source.0, source.1, at, body],
139
+ )?;
140
+ Ok(())
141
+ })
142
+ }
143
+
144
+ pub fn put<T: Serialize>(&self, row: &Row<'_, T>) -> Outcome<()> {
145
+ self.insert(
146
+ row.id,
147
+ (row.source.site.as_str(), &row.source.id),
148
+ row.at,
149
+ row.value,
150
+ )
151
+ }
152
+
153
+ pub fn put_unsourced<T: Serialize>(&self, id: &str, at: i64, value: &T) -> Outcome<()> {
154
+ self.insert(id, (UNSOURCED, UNSOURCED), at, value)
155
+ }
156
+
157
+ pub fn remove(&self, id: &str) -> Outcome<bool> {
158
+ let table = self.table;
159
+ self.with(|connection| {
160
+ let removed = connection
161
+ .query_row(
162
+ &format!("DELETE FROM {table} WHERE id = ?1 RETURNING id"),
163
+ params![id],
164
+ |row| row.get::<_, String>(0),
165
+ )
166
+ .optional()?;
167
+ Ok(removed.is_some())
168
+ })
169
+ }
170
+ }
171
+
172
+ #[cfg(test)]
173
+ mod tests {
174
+ use serde::{Deserialize, Serialize};
175
+
176
+ use super::{Row, Store, fresh_id, is_id};
177
+ use crate::shared::protocol::{Site, Source};
178
+
179
+ #[derive(Debug, PartialEq, Serialize, Deserialize)]
180
+ struct Note {
181
+ words: String,
182
+ }
183
+
184
+ #[test]
185
+ fn only_ids_in_the_form_the_app_makes_are_ids() {
186
+ assert!(is_id(&fresh_id()));
187
+ let id = fresh_id();
188
+ for other in [
189
+ id.to_uppercase(),
190
+ id.replace('-', ""),
191
+ format!("{{{id}}}"),
192
+ format!("urn:uuid:{id}"),
193
+ format!("../{id}"),
194
+ format!("/{id}"),
195
+ "..".to_owned(),
196
+ String::new(),
197
+ ] {
198
+ assert!(!is_id(&other), "{other}");
199
+ }
200
+ }
201
+
202
+ fn source(id: &str) -> Source {
203
+ Source {
204
+ site: Site::Youtube,
205
+ id: id.to_owned(),
206
+ }
207
+ }
208
+
209
+ fn note(words: &str) -> Note {
210
+ Note {
211
+ words: words.to_owned(),
212
+ }
213
+ }
214
+
215
+ #[test]
216
+ fn keeps_notes_newest_first_per_source() {
217
+ let store = Store::in_memory("notes").expect("store");
218
+ let here = source("aaaaaaaaaaa");
219
+ let there = source("bbbbbbbbbbb");
220
+ for (id, at, place, words) in [
221
+ ("1", 1, &here, "first"),
222
+ ("2", 2, &here, "second"),
223
+ ("3", 3, &there, "elsewhere"),
224
+ ] {
225
+ store
226
+ .put(&Row {
227
+ id,
228
+ source: place,
229
+ at,
230
+ value: &note(words),
231
+ })
232
+ .expect("saved");
233
+ }
234
+ let notes: Vec<Note> = store.list(Some(&here)).expect("notes here");
235
+ assert_eq!(notes, vec![note("second"), note("first")]);
236
+ assert_eq!(store.list::<Note>(None).expect("every note").len(), 3);
237
+ }
238
+
239
+ #[test]
240
+ fn replaces_and_removes_a_note() {
241
+ let store = Store::in_memory("notes").expect("store");
242
+ let here = source("aaaaaaaaaaa");
243
+ let row = |value| Row {
244
+ id: "1",
245
+ source: &here,
246
+ at: 1,
247
+ value,
248
+ };
249
+ let draft = note("draft");
250
+ let last = note("final");
251
+ store.put(&row(&draft)).expect("saved");
252
+ store.put(&row(&last)).expect("replaced");
253
+ assert_eq!(store.get::<Note>("1").expect("read"), Some(note("final")));
254
+ assert!(store.remove("1").expect("first removal"));
255
+ assert!(!store.remove("1").expect("second removal"));
256
+ assert_eq!(store.get::<Note>("1").expect("gone"), None);
257
+ }
258
+
259
+ #[test]
260
+ fn keeps_a_note_that_belongs_to_no_media() {
261
+ let store = Store::in_memory("notes").expect("store");
262
+ store
263
+ .put_unsourced("1", 1, &note("about every song"))
264
+ .expect("saved");
265
+ assert_eq!(
266
+ store.list::<Note>(None).expect("every note"),
267
+ vec![note("about every song")]
268
+ );
269
+ assert_eq!(
270
+ store
271
+ .list::<Note>(Some(&source("aaaaaaaaaaa")))
272
+ .expect("none here"),
273
+ Vec::<Note>::new()
274
+ );
275
+ }
276
+ }
@@ -0,0 +1,55 @@
1
+ use serde::Serialize;
2
+ use specta::Type;
3
+
4
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Type)]
5
+ pub struct Failure {
6
+ pub message: String,
7
+ }
8
+
9
+ impl Failure {
10
+ pub fn new(message: impl Into<String>) -> Self {
11
+ Self {
12
+ message: message.into(),
13
+ }
14
+ }
15
+ }
16
+
17
+ impl std::fmt::Display for Failure {
18
+ fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19
+ formatter.write_str(&self.message)
20
+ }
21
+ }
22
+
23
+ impl std::error::Error for Failure {}
24
+
25
+ impl From<rusqlite::Error> for Failure {
26
+ fn from(error: rusqlite::Error) -> Self {
27
+ Self::new(error.to_string())
28
+ }
29
+ }
30
+
31
+ impl From<serde_json::Error> for Failure {
32
+ fn from(error: serde_json::Error) -> Self {
33
+ Self::new(error.to_string())
34
+ }
35
+ }
36
+
37
+ impl From<std::io::Error> for Failure {
38
+ fn from(error: std::io::Error) -> Self {
39
+ Self::new(error.to_string())
40
+ }
41
+ }
42
+
43
+ impl From<reqwest::Error> for Failure {
44
+ fn from(error: reqwest::Error) -> Self {
45
+ Self::new(error.to_string())
46
+ }
47
+ }
48
+
49
+ impl From<tauri::Error> for Failure {
50
+ fn from(error: tauri::Error) -> Self {
51
+ Self::new(error.to_string())
52
+ }
53
+ }
54
+
55
+ pub type Outcome<T> = Result<T, Failure>;
@@ -0,0 +1,34 @@
1
+ use rmcp::model::{CallToolResult, ContentBlock};
2
+ use serde::Serialize;
3
+
4
+ use crate::shared::lib::Outcome;
5
+
6
+ pub fn answer<T: Serialize>(outcome: Outcome<T>) -> CallToolResult {
7
+ match outcome.and_then(|value| Ok(serde_json::to_value(&value)?)) {
8
+ Ok(value) if value.is_object() => CallToolResult::structured(value),
9
+ Ok(value) => CallToolResult::success(vec![ContentBlock::text(value.to_string())]),
10
+ Err(failure) => CallToolResult::error(vec![ContentBlock::text(failure.message)]),
11
+ }
12
+ }
13
+
14
+ #[cfg(test)]
15
+ mod tests {
16
+ use serde_json::json;
17
+
18
+ use super::answer;
19
+ use crate::shared::lib::Failure;
20
+
21
+ #[test]
22
+ fn an_object_comes_back_as_structured_content() {
23
+ let result = answer(Ok(json!({ "songs": [] })));
24
+ assert_eq!(result.structured_content, Some(json!({ "songs": [] })));
25
+ assert_eq!(result.is_error, Some(false));
26
+ }
27
+
28
+ #[test]
29
+ fn a_failure_comes_back_as_an_error_without_structure() {
30
+ let result = answer::<()>(Err(Failure::new("nope")));
31
+ assert_eq!(result.structured_content, None);
32
+ assert_eq!(result.is_error, Some(true));
33
+ }
34
+ }
@@ -0,0 +1,5 @@
1
+ mod failure;
2
+ mod mcp;
3
+
4
+ pub use failure::{Failure, Outcome};
5
+ pub use mcp::answer;
@@ -0,0 +1,3 @@
1
+ pub mod db;
2
+ pub mod lib;
3
+ pub mod protocol;
@@ -0,0 +1,69 @@
1
+ // @generated by libs/masseater-utils/bin/codegen.ts. Do not edit.
2
+
3
+ use schemars::JsonSchema;
4
+ use serde::{Deserialize, Serialize};
5
+ use specta::Type;
6
+
7
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type, JsonSchema)]
8
+ pub struct McpAccess {
9
+ pub url: String,
10
+ pub token: String,
11
+ }
12
+
13
+ #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Type, JsonSchema)]
14
+ #[serde(rename_all = "lowercase")]
15
+ pub enum Site {
16
+ Youtube,
17
+ Niconico,
18
+ Soundcloud,
19
+ File,
20
+ }
21
+
22
+ impl Site {
23
+ #[must_use]
24
+ pub const fn as_str(self) -> &'static str {
25
+ match self {
26
+ Self::Youtube => "youtube",
27
+ Self::Niconico => "niconico",
28
+ Self::Soundcloud => "soundcloud",
29
+ Self::File => "file",
30
+ }
31
+ }
32
+
33
+ #[must_use]
34
+ pub fn parse(value: &str) -> Option<Self> {
35
+ match value {
36
+ "youtube" => Some(Self::Youtube),
37
+ "niconico" => Some(Self::Niconico),
38
+ "soundcloud" => Some(Self::Soundcloud),
39
+ "file" => Some(Self::File),
40
+ _ => None,
41
+ }
42
+ }
43
+ }
44
+
45
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type, JsonSchema)]
46
+ pub struct Source {
47
+ pub site: Site,
48
+ pub id: String,
49
+ }
50
+
51
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type, JsonSchema)]
52
+ pub struct Media {
53
+ pub source: Source,
54
+ pub title: String,
55
+ pub artist: String,
56
+ }
57
+
58
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type, JsonSchema)]
59
+ pub struct Opened {
60
+ pub media: Media,
61
+ pub location: String,
62
+ }
63
+
64
+ #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Type, JsonSchema)]
65
+ pub struct Saved {
66
+ pub id: String,
67
+ #[specta(type = specta_typescript::Number)]
68
+ pub at: i64,
69
+ }