| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | | use futures::future::BoxFuture; |
| 13 | | use serde_json::{json, Value as Json}; |
| 14 | | use spectra_core::authn::Caller; |
| 15 | | use spectra_core::conversation::{self as conv, Author, Post, Posted, Signer}; |
| 16 | | use spectra_core::daemon::Daemon; |
| 17 | | use spectra_core::event::NewEvent; |
| 18 | | use spectra_core::ids::now_ms; |
| 19 | | use spectra_core::principals::Principal; |
| 20 | | use std::sync::Arc; |
| 21 | | use std::time::Duration; |
| 22 | |
|
| 23 | |
|
| 24 | | pub const IDLE_WAITS: u32 = 3; |
| 25 | | pub const IDLE_COOLDOWN_MS: u64 = 10 * 60_000; |
| 26 | |
|
| 27 | |
|
| 28 | | pub struct Ctx { |
| 29 | | pub daemon: Arc<Daemon>, |
| 30 | | pub caller: Caller, |
| 31 | | pub session: Option<String>, |
| 32 | | } |
| 33 | |
|
| 34 | | impl Ctx { |
| 35 | 16× | fn author(&self) -> Author<'static> { |
| 36 | 16× | Author { principal: self.caller.principal.clone(), signer: Signer::Request(self.caller.auth.clone()) } |
| 37 | 16× | } |
| 38 | 40× | fn actor(&self) -> String { |
| 39 | 40× | self.caller.principal.actor() |
| 40 | 40× | } |
| 41 | 28× | fn event(&self, kind: &str, payload: Json) -> NewEvent { |
| 42 | 28× | let mut e = NewEvent::system(kind, payload).by(self.actor(), self.caller.auth.clone()); |
| 43 | 28× | e.session = self.session.clone(); |
| 44 | 28× | e |
| 45 | 28× | } |
| 46 | | } |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | | #[derive(Clone, Debug, Default)] |
| 51 | | pub struct Output { |
| 52 | | pub text: String, |
| 53 | | pub structured: Option<Json>, |
| 54 | | pub is_error: bool, |
| 55 | | |
| 56 | | pub quiet: bool, |
| 57 | | } |
| 58 | |
|
| 59 | | impl Output { |
| 60 | 23× | pub fn ok(text: impl Into<String>, structured: Json) -> Output { |
| 61 | 23× | Output { text: text.into(), structured: Some(structured), ..Default::default() } |
| 62 | 23× | } |
| 63 | 3× | pub fn err(text: impl Into<String>) -> Output { |
| 64 | 3× | Output { text: text.into(), is_error: true, ..Default::default() } |
| 65 | 3× | } |
| 66 | | } |
| 67 | |
|
| 68 | 2× | fn fail(e: anyhow::Error) -> Output { |
| 69 | 2× | Output::err(format!("{e:#}")) |
| 70 | 2× | } |
| 71 | |
|
| 72 | | pub type Run = for<'a> fn(&'a Ctx, Json) -> BoxFuture<'a, Output>; |
| 73 | |
|
| 74 | |
|
| 75 | | pub struct Tool { |
| 76 | | pub name: &'static str, |
| 77 | | pub description: &'static str, |
| 78 | | pub schema: fn() -> Json, |
| 79 | | pub read_only: bool, |
| 80 | | pub destructive: bool, |
| 81 | | |
| 82 | | pub long: bool, |
| 83 | | pub run: Run, |
| 84 | | } |
| 85 | |
|
| 86 | | impl Tool { |
| 87 | 44× | pub fn describe(&self) -> Json { |
| 88 | 44× | json!({ |
| 89 | 44× | "name": self.name, |
| 90 | 44× | "description": self.description, |
| 91 | 44× | "inputSchema": (self.schema)(), |
| 92 | 44× | "annotations": { "readOnlyHint": self.read_only, "destructiveHint": self.destructive }, |
| 93 | | }) |
| 94 | 44× | } |
| 95 | | } |
| 96 | |
|
| 97 | |
|
| 98 | 4× | pub fn all() -> &'static [Tool] { |
| 99 | 4× | TOOLS |
| 100 | 4× | } |
| 101 | |
|
| 102 | 26× | pub fn find(name: &str) -> Option<&'static Tool> { |
| 103 | 156× | TOOLS.iter().find(|t| t.name == name) |
| 104 | 26× | } |
| 105 | |
|
| 106 | |
|
| 107 | 26× | pub async fn invoke(ctx: &Ctx, tool: &Tool, args: Json) -> Output { |
| 108 | 26× | let started = std::time::Instant::now(); |
| 109 | 26× | let mut out = (tool.run)(ctx, args.clone()).await; |
| 110 | 26× | let Principal::Agent { name, .. } = &ctx.caller.principal else { return out }; |
| 111 | 26× | if tool.name != "rules" && tool.name != "brief" { |
| 112 | | |
| 113 | 22× | if let Some(rules) = spectra_core::rules::compile(&ctx.daemon.data).ok().filter(|r| !r.text.is_empty()) { |
| 114 | 15× | let read = spectra_core::rules::last_read(ctx.daemon.ledger.pool(), name).await.ok().flatten(); |
| 115 | 15× | if read.as_deref() != Some(rules.version.as_str()) { |
| 116 | 2× | let from = read.unwrap_or_else(|| "none".into()); |
| 117 | 2× | out.text = format!("{}\n\n{}", spectra_core::rules::changed_line(&from, &rules.version), out.text); |
| 118 | 13× | } |
| 119 | 7× | } |
| 120 | 4× | } |
| 121 | 26× | if out.quiet { |
| 122 | 3× | return out; |
| 123 | 23× | } |
| 124 | 23× | let data = &ctx.daemon.data; |
| 125 | 23× | let args_hash = spectra_core::attachments::put(data, args.to_string().as_bytes()).unwrap_or_default(); |
| 126 | 23× | let result_hash = spectra_core::attachments::put(data, out.text.as_bytes()).unwrap_or_default(); |
| 127 | 23× | let e = ctx.event( |
| 128 | 23× | "tool.called", |
| 129 | 23× | json!({ "tool": tool.name, "args": args_hash, "result": result_hash, "ms": started.elapsed().as_millis() as u64, "is_error": out.is_error, "read": tool.read_only }), |
| 130 | | ); |
| 131 | 23× | let _ = ctx.daemon.ledger.append_one(e).await; |
| 132 | 23× | out |
| 133 | 26× | } |
| 134 | |
|
| 135 | 44× | fn obj(props: Json, required: &[&str]) -> Json { |
| 136 | 44× | json!({ "type": "object", "properties": props, "required": required, "additionalProperties": false }) |
| 137 | 44× | } |
| 138 | |
|
| 139 | | static TOOLS: &[Tool] = &[ |
| 140 | | Tool { |
| 141 | | name: "brief", |
| 142 | | description: "Call first: who you are, your project, the house rules, your unread inbox, and who else is here.", |
| 143 | 4× | schema: || obj(json!({ "new_session": { "type": "boolean" } }), &[]), |
| 144 | | read_only: true, |
| 145 | | destructive: false, |
| 146 | | long: false, |
| 147 | | run: brief, |
| 148 | | }, |
| 149 | | Tool { |
| 150 | | name: "rules", |
| 151 | | description: "The house rules and their version, in full.", |
| 152 | 4× | schema: || obj(json!({}), &[]), |
| 153 | | read_only: true, |
| 154 | | destructive: false, |
| 155 | | long: false, |
| 156 | | run: rules, |
| 157 | | }, |
| 158 | | Tool { |
| 159 | | name: "agents", |
| 160 | | description: "Every agent with its owner, kind, project, presence and status.", |
| 161 | 4× | schema: || obj(json!({ "project": { "type": "string" } }), &[]), |
| 162 | | read_only: true, |
| 163 | | destructive: false, |
| 164 | | long: false, |
| 165 | | run: agents, |
| 166 | | }, |
| 167 | | Tool { |
| 168 | | name: "status", |
| 169 | | description: "Say what you are doing, so others can see.", |
| 170 | 4× | schema: || obj(json!({ "state": { "enum": ["working", "idle", "blocked"] }, "text": { "type": "string" }, "task": { "type": "string" } }), &[]), |
| 171 | | read_only: false, |
| 172 | | destructive: false, |
| 173 | | long: false, |
| 174 | | run: status, |
| 175 | | }, |
| 176 | | Tool { |
| 177 | | name: "say", |
| 178 | | description: "Post a message (default: your project's channel). @name mentions; reply_to answers in a thread.", |
| 179 | 4× | schema: || { |
| 180 | 4× | obj( |
| 181 | 4× | json!({ "body": { "type": "string" }, "channel": { "type": "string" }, "reply_to": { "type": "string" }, |
| 182 | 4× | "kind": { "enum": ["chat", "request", "answer", "handoff", "status", "decision"] }, "to": { "type": "string" } }), |
| 183 | 4× | &["body"], |
| 184 | | ) |
| 185 | 4× | }, |
| 186 | | read_only: false, |
| 187 | | destructive: false, |
| 188 | | long: false, |
| 189 | | run: say, |
| 190 | | }, |
| 191 | | Tool { |
| 192 | | name: "read", |
| 193 | | description: "Messages of a channel (default: your project's) or a whole thread; marks nothing read.", |
| 194 | 4× | schema: || obj(json!({ "channel": { "type": "string" }, "thread": { "type": "string" }, "since": { "type": "string" }, "limit": { "type": "integer" } }), &[]), |
| 195 | | read_only: true, |
| 196 | | destructive: false, |
| 197 | | long: false, |
| 198 | | run: read, |
| 199 | | }, |
| 200 | | Tool { |
| 201 | | name: "inbox", |
| 202 | | description: "Your unread mentions and direct messages.", |
| 203 | 4× | schema: || obj(json!({}), &[]), |
| 204 | | read_only: true, |
| 205 | | destructive: false, |
| 206 | | long: false, |
| 207 | | run: inbox, |
| 208 | | }, |
| 209 | | Tool { |
| 210 | | name: "wait", |
| 211 | | description: "Only while waiting for an answer inside a piece of work: blocks up to timeout_s for something new. If idle, end your turn instead.", |
| 212 | 4× | schema: || obj(json!({ "timeout_s": { "type": "integer", "minimum": 1, "maximum": 50 } }), &["timeout_s"]), |
| 213 | | read_only: true, |
| 214 | | destructive: false, |
| 215 | | long: true, |
| 216 | | run: wait, |
| 217 | | }, |
| 218 | | Tool { |
| 219 | | name: "ack", |
| 220 | | description: "Mark inbox items read, with an optional reaction (ack, done, eyes, no).", |
| 221 | 4× | schema: || obj(json!({ "ids": { "type": "array", "items": { "type": "string" } }, "reaction": { "enum": ["ack", "done", "eyes", "no"] } }), &["ids"]), |
| 222 | | read_only: false, |
| 223 | | destructive: false, |
| 224 | | long: false, |
| 225 | | run: ack, |
| 226 | | }, |
| 227 | | Tool { |
| 228 | | name: "search", |
| 229 | | description: "Search history: words, \"phrases\", prefix*, AND/OR/NOT; filter by channel, author, since (7d, 3h).", |
| 230 | 4× | schema: || { |
| 231 | 4× | obj(json!({ "query": { "type": "string" }, "channel": { "type": "string" }, "author": { "type": "string" }, "since": { "type": "string" }, "limit": { "type": "integer" } }), &["query"]) |
| 232 | 4× | }, |
| 233 | | read_only: true, |
| 234 | | destructive: false, |
| 235 | | long: false, |
| 236 | | run: search, |
| 237 | | }, |
| 238 | | Tool { |
| 239 | | name: "redact", |
| 240 | | description: "Redact your own message, with the reason.", |
| 241 | 4× | schema: || obj(json!({ "id": { "type": "string" }, "reason": { "type": "string" } }), &["id", "reason"]), |
| 242 | | read_only: false, |
| 243 | | destructive: true, |
| 244 | | long: false, |
| 245 | | run: redact, |
| 246 | | }, |
| 247 | | ]; |
| 248 | |
|
| 249 | 45× | fn s<'a>(args: &'a Json, k: &str) -> Option<&'a str> { |
| 250 | 45× | args.get(k).and_then(Json::as_str).filter(|v| !v.is_empty()) |
| 251 | 45× | } |
| 252 | |
|
| 253 | |
|
| 254 | 4× | async fn label(d: &Daemon, actor: &str) -> String { |
| 255 | 4× | if actor == "human" { |
| 256 | 0× | let name: Option<String> = sqlx::query_scalar("SELECT name FROM identities WHERE role = 'owner' AND current = 1") |
| 257 | 0× | .fetch_optional(d.ledger.pool()) |
| 258 | 0× | .await |
| 259 | 0× | .ok() |
| 260 | 0× | .flatten(); |
| 261 | 0× | return format!("{} (the human)", name.unwrap_or_else(|| "the owner".into())); |
| 262 | 4× | } |
| 263 | 4× | if let Some(a) = actor.strip_prefix("agent:") { |
| 264 | 4× | let row: Option<(String, String)> = sqlx::query_as("SELECT owner_name, kind FROM agents WHERE name = ?") |
| 265 | 4× | .bind(a) |
| 266 | 4× | .fetch_optional(d.ledger.pool()) |
| 267 | 4× | .await |
| 268 | 4× | .ok() |
| 269 | 4× | .flatten(); |
| 270 | 4× | if let Some((owner, kind)) = row { |
| 271 | 4× | return format!("{a} ({owner}'s agent, {kind})"); |
| 272 | 0× | } |
| 273 | 0× | return a.to_string(); |
| 274 | 0× | } |
| 275 | 0× | "Spectra".into() |
| 276 | 4× | } |
| 277 | |
|
| 278 | |
|
| 279 | 1× | async fn delimited(d: &Daemon, m: &conv::Message, channel: &str) -> String { |
| 280 | 1× | let at = spectra_core::ids::format_time(m.at_ms as u64); |
| 281 | 1× | let thread = m.reply_to.as_deref().map(|r| format!(", reply in thread {r}")).unwrap_or_default(); |
| 282 | 1× | let replies = if m.replies > 0 { format!(" ({} replies)", m.replies) } else { String::new() }; |
| 283 | 1× | format!("[from {} at {at} in {channel}, id {}{thread}{replies}]\n{}\n[end]", label(d, &m.author).await, m.id, m.body) |
| 284 | 1× | } |
| 285 | |
|
| 286 | 3× | fn brief(ctx: &Ctx, args: Json) -> BoxFuture<'_, Output> { |
| 287 | 3× | let _ = args; |
| 288 | 3× | Box::pin(async move { |
| 289 | 3× | let d = &ctx.daemon; |
| 290 | 3× | let rules = match spectra_core::rules::compile(&d.data) { |
| 291 | 3× | Ok(r) => r, |
| 292 | 0× | Err(e) => return fail(e), |
| 293 | | }; |
| 294 | 3× | let me = ctx.caller.principal.display(); |
| 295 | 3× | let project = match &ctx.caller.principal { |
| 296 | 3× | Principal::Agent { project, .. } => Some(project.clone()), |
| 297 | 0× | _ => None, |
| 298 | | }; |
| 299 | 3× | let mut relations = (Vec::<String>::new(), Vec::<String>::new()); |
| 300 | 3× | if let Some(p) = &project { |
| 301 | 3× | relations.0 = sqlx::query_scalar("SELECT depends_on FROM project_relations WHERE project = ?") |
| 302 | 3× | .bind(p) |
| 303 | 3× | .fetch_all(d.ledger.pool()) |
| 304 | 3× | .await |
| 305 | 3× | .unwrap_or_default(); |
| 306 | 3× | relations.1 = sqlx::query_scalar("SELECT project FROM project_relations WHERE depends_on = ?") |
| 307 | 3× | .bind(p) |
| 308 | 3× | .fetch_all(d.ledger.pool()) |
| 309 | 3× | .await |
| 310 | 3× | .unwrap_or_default(); |
| 311 | 0× | } |
| 312 | 3× | let items = conv::inbox(&d.ledger, &ctx.actor(), Some(&ctx.author())).await.unwrap_or_default(); |
| 313 | 3× | let others = agents(ctx, json!({})).await; |
| 314 | 3× | let _ = d.ledger.append_one(ctx.event("rules.read", json!({ "version": rules.version }))).await; |
| 315 | 3× | let mut text = format!("You are {me}."); |
| 316 | 3× | if let Some(p) = &project { |
| 317 | 3× | text.push_str(&format!(" Your project is {p}; your channel is #{p}.")); |
| 318 | 3× | if !relations.0.is_empty() { |
| 319 | 0× | text.push_str(&format!(" It pins {}.", relations.0.join(", "))); |
| 320 | 3× | } |
| 321 | 3× | if !relations.1.is_empty() { |
| 322 | 0× | text.push_str(&format!(" {} pin it.", relations.1.join(", "))); |
| 323 | 3× | } |
| 324 | 0× | } |
| 325 | 3× | if let Some(sess) = &ctx.session { |
| 326 | 3× | text.push_str(&format!(" Session {sess}.")); |
| 327 | 3× | } |
| 328 | 3× | text.push_str(&format!("\n\nHouse rules (version {}):\n", rules.version)); |
| 329 | 3× | text.push_str(if rules.text.is_empty() { "(none yet)\n" } else { &rules.text }); |
| 330 | 3× | text.push_str(&format!("\nInbox: {} unread.\n", items.len())); |
| 331 | 3× | for i in &items { |
| 332 | 0× | text.push_str(&format!("- {} from {} in {} ({}): {}\n", i.kind, label(d, &i.from).await, i.channel, i.id, i.summary)); |
| 333 | | } |
| 334 | 3× | text.push_str(&format!("\nAgents:\n{}\n", others.text)); |
| 335 | 3× | text.push_str("\nWhen you have nothing to do, end your turn: Spectra starts you again when there is work."); |
| 336 | 3× | Output::ok( |
| 337 | 3× | text, |
| 338 | 3× | json!({ "you": me, "project": project, "session": ctx.session, "rules": { "version": rules.version, "text": rules.text }, |
| 339 | 3× | "inbox": items, "agents": others.structured.and_then(|s| s.get("agents").cloned()), "pins": relations.0, "pinned_by": relations.1 }), |
| 340 | | ) |
| 341 | 3× | }) |
| 342 | 3× | } |
| 343 | |
|
| 344 | 1× | fn rules(ctx: &Ctx, _args: Json) -> BoxFuture<'_, Output> { |
| 345 | 1× | Box::pin(async move { |
| 346 | 1× | match spectra_core::rules::compile(&ctx.daemon.data) { |
| 347 | 1× | Ok(r) => { |
| 348 | 1× | let _ = ctx.daemon.ledger.append_one(ctx.event("rules.read", json!({ "version": r.version }))).await; |
| 349 | 1× | let body = if r.text.is_empty() { "(no house rules yet)".to_string() } else { r.text.clone() }; |
| 350 | 1× | Output::ok(format!("House rules, version {}:\n\n{body}", r.version), json!({ "version": r.version, "text": r.text })) |
| 351 | | } |
| 352 | 0× | Err(e) => fail(e), |
| 353 | | } |
| 354 | 1× | }) |
| 355 | 1× | } |
| 356 | |
|
| 357 | 5× | fn agents(ctx: &Ctx, args: Json) -> BoxFuture<'_, Output> { |
| 358 | 5× | Box::pin(async move { |
| 359 | 5× | let project = s(&args, "project"); |
| 360 | 5× | let rows: Vec<AgentRow> = match sqlx::query_as( |
| 361 | 5× | "SELECT a.name, a.owner_name, a.kind, a.project, p.state, p.status_text FROM agents a LEFT JOIN presence p ON p.agent = a.name \ |
| 362 | 5× | WHERE a.disabled_ms IS NULL AND (? IS NULL OR a.project = ?) ORDER BY a.name", |
| 363 | | ) |
| 364 | 5× | .bind(project) |
| 365 | 5× | .bind(project) |
| 366 | 5× | .fetch_all(ctx.daemon.ledger.pool()) |
| 367 | 5× | .await |
| 368 | | { |
| 369 | 5× | Ok(r) => r, |
| 370 | 0× | Err(e) => return fail(e.into()), |
| 371 | | }; |
| 372 | | |
| 373 | 5× | let rows: Vec<_> = { |
| 374 | 5× | let p = ctx.daemon.presence.lock().await; |
| 375 | 5× | let now = now_ms(); |
| 376 | 5× | rows.into_iter() |
| 377 | 9× | .map(|(name, owner, kind, project, _, status)| { |
| 378 | 9× | let live = p.state(&name, now).to_string(); |
| 379 | 9× | (name, owner, kind, project, Some(live), status) |
| 380 | 9× | }) |
| 381 | 5× | .collect() |
| 382 | | }; |
| 383 | 5× | let list: Vec<Json> = rows |
| 384 | 5× | .iter() |
| 385 | 9× | .map(|(name, owner, kind, project, presence, status)| { |
| 386 | 9× | json!({ "name": name, "owner": owner, "kind": kind, "project": project, "presence": presence.clone().unwrap_or_else(|| "offline".into()), "status": status }) |
| 387 | 9× | }) |
| 388 | 5× | .collect(); |
| 389 | 5× | let text = if rows.is_empty() { |
| 390 | 0× | "No agents.".to_string() |
| 391 | | } else { |
| 392 | 5× | rows.iter() |
| 393 | 9× | .map(|(name, owner, kind, project, presence, status)| { |
| 394 | 9× | let st = status.as_deref().map(|s| format!(" — \"{s}\"")).unwrap_or_default(); |
| 395 | 9× | format!("- {name} · {owner}'s agent ({kind}, #{project}): {}{st}", presence.as_deref().unwrap_or("offline")) |
| 396 | 9× | }) |
| 397 | 5× | .collect::<Vec<_>>() |
| 398 | 5× | .join("\n") |
| 399 | | }; |
| 400 | 5× | Output::ok(text, json!({ "agents": list })) |
| 401 | 5× | }) |
| 402 | 5× | } |
| 403 | |
|
| 404 | |
|
| 405 | |
|
| 406 | | type AgentRow = (String, String, String, String, Option<String>, Option<String>); |
| 407 | |
|
| 408 | 1× | fn status(ctx: &Ctx, args: Json) -> BoxFuture<'_, Output> { |
| 409 | 1× | Box::pin(async move { |
| 410 | 1× | let Principal::Agent { name, .. } = &ctx.caller.principal else { return Output::err("status is for agents") }; |
| 411 | 1× | let state = s(&args, "state").unwrap_or("working"); |
| 412 | 1× | let text = s(&args, "text"); |
| 413 | 1× | if text.is_some_and(|t| t.len() > 500) { |
| 414 | 0× | return Output::err("a status is at most 500 characters"); |
| 415 | 1× | } |
| 416 | 1× | let e = ctx.event("agent.status", json!({ "state": state, "text": text, "task": s(&args, "task") })).subject(name.clone()); |
| 417 | 1× | if let Err(e) = ctx.daemon.ledger.append_one(e).await { |
| 418 | 0× | return fail(e); |
| 419 | 1× | } |
| 420 | | { |
| 421 | 1× | let mut p = ctx.daemon.presence.lock().await; |
| 422 | 1× | p.status(name, (state != "idle").then(now_ms)); |
| 423 | 1× | p.blocked(name, state == "blocked"); |
| 424 | | } |
| 425 | 1× | Output::ok( |
| 426 | 1× | format!("Status set: {state}{}", text.map(|t| format!(" — {t}")).unwrap_or_default()), |
| 427 | 1× | json!({ "state": state, "text": text }), |
| 428 | | ) |
| 429 | 1× | }) |
| 430 | 1× | } |
| 431 | |
|
| 432 | 5× | fn say(ctx: &Ctx, args: Json) -> BoxFuture<'_, Output> { |
| 433 | 5× | Box::pin(async move { |
| 434 | 5× | let Some(body) = s(&args, "body") else { return Output::err("say needs a body") }; |
| 435 | 5× | let mut channel = s(&args, "channel").map(String::from); |
| 436 | 5× | if let Some(to) = s(&args, "to") { |
| 437 | 0× | match conv::ensure_direct(&ctx.daemon.ledger, &ctx.caller.principal, to).await { |
| 438 | 0× | Ok(c) => channel = Some(c.name), |
| 439 | 0× | Err(e) => return fail(e), |
| 440 | | } |
| 441 | 5× | } |
| 442 | 5× | let post = Post { |
| 443 | 5× | channel, |
| 444 | 5× | body: body.into(), |
| 445 | 5× | reply_to: s(&args, "reply_to").map(String::from), |
| 446 | 5× | kind: s(&args, "kind").map(String::from), |
| 447 | 5× | ..Default::default() |
| 448 | 5× | }; |
| 449 | 5× | match conv::post(&ctx.daemon.ledger, &ctx.author(), post, now_ms()).await { |
| 450 | 4× | Ok(Posted::Message { id, channel }) => { |
| 451 | 4× | Output::ok(format!("Posted {id} in {channel}."), json!({ "id": id, "channel": channel })) |
| 452 | | } |
| 453 | 0× | Ok(Posted::Duplicate { id }) => { |
| 454 | 0× | Output::ok(format!("Not posted: you said exactly this in the last minute ({id})."), json!({ "duplicate_of": id })) |
| 455 | | } |
| 456 | 1× | Err(e) => fail(e), |
| 457 | | } |
| 458 | 5× | }) |
| 459 | 5× | } |
| 460 | |
|
| 461 | 1× | fn read(ctx: &Ctx, args: Json) -> BoxFuture<'_, Output> { |
| 462 | 1× | Box::pin(async move { |
| 463 | 1× | let limit = args.get("limit").and_then(Json::as_u64).unwrap_or(50) as u32; |
| 464 | 1× | match conv::read(&ctx.daemon.ledger, &ctx.caller.principal, s(&args, "channel"), s(&args, "thread"), s(&args, "since"), limit).await |
| 465 | | { |
| 466 | 1× | Ok((ch, msgs)) => { |
| 467 | 1× | let mut text = format!("{} message(s) in {}:\n\n", msgs.len(), ch.display()); |
| 468 | 1× | for m in &msgs { |
| 469 | 1× | text.push_str(&delimited(&ctx.daemon, m, &ch.display()).await); |
| 470 | 1× | text.push_str("\n\n"); |
| 471 | | } |
| 472 | 1× | Output::ok(text.trim_end().to_string(), json!({ "channel": ch.display(), "messages": msgs })) |
| 473 | | } |
| 474 | 0× | Err(e) => fail(e), |
| 475 | | } |
| 476 | 1× | }) |
| 477 | 1× | } |
| 478 | |
|
| 479 | 3× | fn inbox(ctx: &Ctx, _args: Json) -> BoxFuture<'_, Output> { |
| 480 | 3× | Box::pin(async move { |
| 481 | 3× | match conv::inbox(&ctx.daemon.ledger, &ctx.actor(), Some(&ctx.author())).await { |
| 482 | 3× | Ok(items) => inbox_output(ctx, items).await, |
| 483 | 0× | Err(e) => fail(e), |
| 484 | | } |
| 485 | 3× | }) |
| 486 | 3× | } |
| 487 | |
|
| 488 | 5× | async fn inbox_output(ctx: &Ctx, items: Vec<conv::Item>) -> Output { |
| 489 | 5× | if items.is_empty() { |
| 490 | 3× | return Output::ok("Nothing unread.", json!({ "items": [] })); |
| 491 | 2× | } |
| 492 | 2× | let mut text = format!("{} unread:\n\n", items.len()); |
| 493 | 2× | for i in &items { |
| 494 | 2× | text.push_str(&format!( |
| 495 | | "[{} from {} in {}, id {}]\n{}\n[end]\nAct on it: {}\n\n", |
| 496 | | i.kind, |
| 497 | 2× | label(&ctx.daemon, &i.from).await, |
| 498 | | i.channel, |
| 499 | | i.id, |
| 500 | | i.summary, |
| 501 | | i.action_hint |
| 502 | | )); |
| 503 | | } |
| 504 | 2× | Output::ok(text.trim_end().to_string(), json!({ "items": items })) |
| 505 | 5× | } |
| 506 | |
|
| 507 | 6× | fn wait(ctx: &Ctx, args: Json) -> BoxFuture<'_, Output> { |
| 508 | 6× | Box::pin(async move { |
| 509 | 6× | let actor = ctx.actor(); |
| 510 | 6× | let now = now_ms(); |
| 511 | | { |
| 512 | 6× | let guard = ctx.daemon.idle_waits.lock().await; |
| 513 | 6× | if let Some((_, until)) = guard.get(&actor) { |
| 514 | 3× | if *until > now { |
| 515 | 1× | return Output::err("Nothing is waiting for you. End your turn; Spectra starts you again when there is work."); |
| 516 | 2× | } |
| 517 | 3× | } |
| 518 | | } |
| 519 | 5× | let timeout = args.get("timeout_s").and_then(Json::as_u64).unwrap_or(30).clamp(1, 50); |
| 520 | 5× | let name = actor.strip_prefix("agent:").unwrap_or(&actor).to_string(); |
| 521 | | |
| 522 | 5× | let mut rx = ctx.daemon.ledger.subscribe(); |
| 523 | 5× | let ledger = &ctx.daemon.ledger; |
| 524 | 5× | ctx.daemon.presence.lock().await.wait_started(&name); |
| 525 | 5× | let found = async { |
| 526 | | loop { |
| 527 | 8× | if conv::unread(ledger, &actor).await.unwrap_or(0) > 0 { |
| 528 | 2× | return true; |
| 529 | 6× | } |
| 530 | 6× | match rx.recv().await { |
| 531 | 3× | Ok(_) | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue, |
| 532 | 0× | Err(_) => return false, |
| 533 | | } |
| 534 | | } |
| 535 | 2× | }; |
| 536 | 5× | let got = tokio::time::timeout(Duration::from_secs(timeout), found).await.unwrap_or(false); |
| 537 | 5× | ctx.daemon.presence.lock().await.wait_ended(&name); |
| 538 | 5× | let mut guard = ctx.daemon.idle_waits.lock().await; |
| 539 | 5× | if got { |
| 540 | 2× | guard.remove(&actor); |
| 541 | 2× | drop(guard); |
| 542 | 2× | return match conv::inbox(ledger, &actor, Some(&ctx.author())).await { |
| 543 | 2× | Ok(items) => inbox_output(ctx, items).await, |
| 544 | 0× | Err(e) => fail(e), |
| 545 | | }; |
| 546 | 3× | } |
| 547 | 3× | let entry = guard.entry(actor).or_insert((0, 0)); |
| 548 | 3× | entry.0 += 1; |
| 549 | 3× | if entry.0 >= IDLE_WAITS { |
| 550 | 1× | entry.1 = now_ms() + IDLE_COOLDOWN_MS; |
| 551 | 1× | entry.0 = 0; |
| 552 | 2× | } |
| 553 | 3× | Output { text: "Nothing new.".into(), structured: Some(json!({ "items": [] })), is_error: false, quiet: true } |
| 554 | 6× | }) |
| 555 | 6× | } |
| 556 | |
|
| 557 | 1× | fn ack(ctx: &Ctx, args: Json) -> BoxFuture<'_, Output> { |
| 558 | 1× | Box::pin(async move { |
| 559 | 1× | let ids: Vec<String> = args |
| 560 | 1× | .get("ids") |
| 561 | 1× | .and_then(Json::as_array) |
| 562 | 1× | .map(|a| a.iter().filter_map(|v| v.as_str().map(String::from)).collect()) |
| 563 | 1× | .unwrap_or_default(); |
| 564 | 1× | if ids.is_empty() { |
| 565 | 0× | return Output::err("ack needs ids"); |
| 566 | 1× | } |
| 567 | 1× | match conv::ack(&ctx.daemon.ledger, &ctx.author(), &ids, s(&args, "reaction")).await { |
| 568 | 1× | Ok(n) => Output::ok(format!("Marked {n} read."), json!({ "read": n })), |
| 569 | 0× | Err(e) => fail(e), |
| 570 | | } |
| 571 | 1× | }) |
| 572 | 1× | } |
| 573 | |
|
| 574 | 1× | fn search(ctx: &Ctx, args: Json) -> BoxFuture<'_, Output> { |
| 575 | 1× | Box::pin(async move { |
| 576 | 1× | let Some(q) = s(&args, "query") else { return Output::err("search needs a query") }; |
| 577 | 1× | let since = s(&args, "since").and_then(|v| parse_since(v, now_ms())); |
| 578 | 1× | let limit = args.get("limit").and_then(Json::as_u64).unwrap_or(20) as u32; |
| 579 | 1× | match conv::search(&ctx.daemon.ledger, &ctx.caller.principal, q, s(&args, "channel"), s(&args, "author"), since, limit).await { |
| 580 | 1× | Ok(hits) => { |
| 581 | 1× | let mut text = format!("{} hit(s):\n", hits.len()); |
| 582 | 1× | for h in &hits { |
| 583 | 1× | text.push_str(&format!( |
| 584 | | "- [{} in {} at {}, id {}] {}\n", |
| 585 | 1× | label(&ctx.daemon, &h.author).await, |
| 586 | | h.channel, |
| 587 | 1× | spectra_core::ids::format_time(h.at_ms as u64), |
| 588 | | h.id, |
| 589 | | h.snippet |
| 590 | | )); |
| 591 | | } |
| 592 | 1× | Output::ok(text.trim_end().to_string(), json!({ "hits": hits })) |
| 593 | | } |
| 594 | 0× | Err(e) => fail(e), |
| 595 | | } |
| 596 | 1× | }) |
| 597 | 1× | } |
| 598 | |
|
| 599 | |
|
| 600 | 1× | pub fn parse_since(v: &str, now: u64) -> Option<i64> { |
| 601 | 1× | let (n, unit) = v.split_at(v.len().saturating_sub(1)); |
| 602 | 1× | let ms = match unit { |
| 603 | 1× | "d" => 86_400_000, |
| 604 | 0× | "h" => 3_600_000, |
| 605 | 0× | "m" => 60_000, |
| 606 | 0× | _ => return v.parse().ok(), |
| 607 | | }; |
| 608 | 1× | n.parse::<u64>().ok().map(|n| now.saturating_sub(n * ms) as i64) |
| 609 | 1× | } |
| 610 | |
|
| 611 | 2× | fn redact(ctx: &Ctx, args: Json) -> BoxFuture<'_, Output> { |
| 612 | 2× | Box::pin(async move { |
| 613 | 2× | let (Some(id), Some(reason)) = (s(&args, "id"), s(&args, "reason")) else { return Output::err("redact needs id and reason") }; |
| 614 | 2× | match conv::redact(&ctx.daemon.ledger, &ctx.author(), id, reason).await { |
| 615 | 1× | Ok(()) => Output::ok(format!("Redacted {id}."), json!({ "id": id })), |
| 616 | 1× | Err(e) => fail(e), |
| 617 | | } |
| 618 | 2× | }) |
| 619 | 2× | } |