octomind 0.25.0

Session-based AI development assistant with conversational codebase interaction, multimodal vision support, built-in MCP tools, and multi-provider AI integration
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2026 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Core schedule tool: global store, MCP handler, and session-loop helpers.

use super::storage::{parse_when, ScheduleEntry, ScheduleStore};
use crate::mcp::{McpFunction, McpToolCall, McpToolResult};
use anyhow::Result;
use serde_json::{json, Value};
use std::sync::{Arc, Mutex, OnceLock};

// ---------------------------------------------------------------------------
// Global store
// ---------------------------------------------------------------------------

static SCHEDULE_STORE: OnceLock<Arc<Mutex<ScheduleStore>>> = OnceLock::new();

/// Get schedule storage for the current context.
/// Returns session-scoped storage if in a session, otherwise CLI global.
fn get_store() -> Arc<Mutex<ScheduleStore>> {
	if let Some(session_id) = crate::session::context::current_session_id() {
		crate::session::context::get_schedule_storage(&session_id)
	} else {
		SCHEDULE_STORE
			.get_or_init(|| Arc::new(Mutex::new(ScheduleStore::new())))
			.clone()
	}
}

// ---------------------------------------------------------------------------
// Session-loop helpers (called from main_loop.rs)
// ---------------------------------------------------------------------------

/// Flush all due entries into the session inbox.  Call once per loop iteration
/// so the inbox is the single source of truth for all injected messages.
pub fn flush_due_to_inbox() {
	let store = get_store();
	while let Some(entry) = store.lock().unwrap().pop_due() {
		crate::session::inbox::push_inbox_message(crate::session::inbox::InboxMessage {
			source: crate::session::inbox::InboxSource::Schedule {
				id: entry.id.clone(),
			},
			content: entry.message,
		});
	}
}

/// Returns true if there are any pending scheduled entries.
pub fn has_pending_schedules() -> bool {
	!get_store().lock().unwrap().is_empty()
}

/// Returns a future that resolves when the next scheduled entry is due.
/// Returns `futures::future::pending()` (never resolves) when the store is empty —
/// so the select! arm is a no-op when nothing is scheduled.
///
/// Also wakes up when schedules change (new/edited/removed) via schedule notify.
pub async fn next_schedule_sleep() {
	let session_id = crate::session::context::current_session_id();
	let duration = get_store().lock().unwrap().next_due_duration();

	match (duration, session_id) {
		(Some(d), Some(ref sid)) => {
			// Wait for timer OR schedule change notification
			let notify = crate::session::context::get_schedule_notify(sid);
			tokio::select! {
				_ = tokio::time::sleep(d) => {}
				_ = notify.notified() => {}
			}
		}
		(Some(d), None) => {
			tokio::time::sleep(d).await;
		}
		(None, Some(ref sid)) => {
			// No schedules - wait for notification that one was added
			let notify = crate::session::context::get_schedule_notify(sid);
			notify.notified().await;
		}
		(None, None) => {
			futures::future::pending::<()>().await;
		}
	}
}

// ---------------------------------------------------------------------------
// MCP tool definition
// ---------------------------------------------------------------------------

pub fn get_schedule_function() -> McpFunction {
	McpFunction {
		name: "schedule".to_string(),
		description: r#"Schedule a message to be automatically injected as a user message into the current session at a future time. The session keeps running until all scheduled messages have fired — nothing is blocked.

Each scheduled entry fires exactly once and is automatically removed after triggering. To repeat a task, schedule it again.

**commands:**
- `add`    — schedule a new message (requires `when` and `message`; `description` recommended)
- `list`   — show all pending scheduled entries with IDs, trigger times, and countdown
- `remove` — cancel a scheduled entry by `id`
- `edit`   — update an existing entry by `id` (any of `when`, `message`, `description`)

**`when` format** (local timezone):
- Relative: `"in 5m"`, `"in 2h"`, `"in 1h30m"`, `"in 90s"`, `"in 2h 30m"`
- Time today: `"15:30"`, `"3:30pm"`, `"9am"` (if already past, fires tomorrow)
- Exact datetime: `"2026-03-22 15:30"`

**`description`** — what this task is about (shown in list, helps you track intent).

**`message`** — the EXACT text that will be injected verbatim as a user message when the timer fires. Write it as if a human typed it: include all context the AI will need to act on it, because the AI will see only this message at trigger time with no other hint about why it arrived."#.to_string(),
		parameters: json!({
			"type": "object",
			"properties": {
				"command": {
					"type": "string",
					"enum": ["add", "list", "remove", "edit"],
					"description": "Action to perform."
				},
				"when": {
					"type": "string",
					"description": "When to fire. Relative: 'in 5m', 'in 2h', 'in 1h30m', 'in 90s'. Time today: '15:30', '3:30pm'. Exact: '2026-03-22 15:30'. Required for add; optional for edit."
				},
				"message": {
					"type": "string",
					"description": "The exact text injected verbatim as a user message when the timer fires. Write it with full context — the AI will see only this text at trigger time. Required for add; optional for edit."
				},
				"description": {
					"type": "string",
					"description": "Human-readable description of what this scheduled task is about. Shown in list output. Recommended for add; optional for edit."
				},
				"id": {
					"type": "string",
					"description": "Entry ID (from list output). Required for remove and edit."
				}
			},
			"required": ["command"]
		}),
	}
}

// ---------------------------------------------------------------------------
// MCP tool handler
// ---------------------------------------------------------------------------

pub async fn execute_schedule_tool(call: &McpToolCall) -> Result<McpToolResult> {
	let command = match call.parameters.get("command") {
		Some(Value::String(s)) if !s.trim().is_empty() => s.clone(),
		Some(_) => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"'command' must be a non-empty string".to_string(),
			))
		}
		None => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"missing required parameter 'command'".to_string(),
			))
		}
	};

	match command.as_str() {
		"add" => handle_add(call),
		"list" => handle_list(call),
		"remove" => handle_remove(call),
		"edit" => handle_edit(call),
		other => Ok(McpToolResult::error(
			call.tool_name.clone(),
			call.tool_id.clone(),
			format!("unknown command '{}' — use: add, list, remove, edit", other),
		)),
	}
}

// ---------------------------------------------------------------------------
// Command handlers
// ---------------------------------------------------------------------------

fn handle_add(call: &McpToolCall) -> Result<McpToolResult> {
	let when_str = match call.parameters.get("when") {
		Some(Value::String(s)) if !s.trim().is_empty() => s.clone(),
		Some(_) => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"'when' must be a non-empty string".to_string(),
			))
		}
		None => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"missing required parameter 'when' for add".to_string(),
			))
		}
	};

	let message = match call.parameters.get("message") {
		Some(Value::String(s)) if !s.trim().is_empty() => s.clone(),
		Some(_) => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"'message' must be a non-empty string".to_string(),
			))
		}
		None => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"missing required parameter 'message' for add".to_string(),
			))
		}
	};

	let description = match call.parameters.get("description") {
		Some(Value::String(s)) if !s.trim().is_empty() => s.clone(),
		_ => String::new(),
	};

	let trigger_at = match parse_when(&when_str) {
		Ok(t) => t,
		Err(e) => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				format!("invalid 'when' value: {}", e),
			))
		}
	};

	let entry = ScheduleEntry::new(description.clone(), message, trigger_at);
	let id = entry.id.clone();
	let countdown = entry.countdown();
	let trigger_fmt = entry.trigger_at.format("%Y-%m-%d %H:%M:%S").to_string();

	get_store().lock().unwrap().add(entry);

	// Wake up the schedule monitor so it recalculates the next due time
	if let Some(sid) = crate::session::context::current_session_id() {
		crate::session::context::notify_schedule_change(&sid);
	}
	let desc_line = if description.is_empty() {
		String::new()
	} else {
		format!("\nDescription: {}", description)
	};

	Ok(McpToolResult::success(
		call.tool_name.clone(),
		call.tool_id.clone(),
		format!(
			"✅ Scheduled [{}] at {} ({}){}\n\nThe message will be injected as a user message when the timer fires.",
			id, trigger_fmt, countdown, desc_line
		),
	))
}

fn handle_list(call: &McpToolCall) -> Result<McpToolResult> {
	let store = get_store();
	let guard = store.lock().unwrap();
	let entries = guard.entries();

	if entries.is_empty() {
		return Ok(McpToolResult::success(
			call.tool_name.clone(),
			call.tool_id.clone(),
			"No scheduled entries.".to_string(),
		));
	}

	let mut lines = vec![format!("{} scheduled entries:\n", entries.len())];
	for entry in entries {
		let trigger_fmt = entry.trigger_at.format("%Y-%m-%d %H:%M:%S").to_string();
		let desc = if entry.description.is_empty() {
			"(no description)".to_string()
		} else {
			entry.description.clone()
		};
		// Truncate message preview to 80 chars.
		let preview = if entry.message.len() > 80 {
			format!("{}", {
				let mut end = 80;
				while !entry.message.is_char_boundary(end) {
					end -= 1;
				}
				&entry.message[..end]
			})
		} else {
			entry.message.clone()
		};
		lines.push(format!(
			"[{}] {} ({}) — {}\n  Message: {}",
			entry.id,
			trigger_fmt,
			entry.countdown(),
			desc,
			preview
		));
	}

	Ok(McpToolResult::success(
		call.tool_name.clone(),
		call.tool_id.clone(),
		lines.join("\n"),
	))
}

fn handle_remove(call: &McpToolCall) -> Result<McpToolResult> {
	let id = match call.parameters.get("id") {
		Some(Value::String(s)) if !s.trim().is_empty() => s.clone(),
		Some(_) => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"'id' must be a non-empty string".to_string(),
			))
		}
		None => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"missing required parameter 'id' for remove".to_string(),
			))
		}
	};

	let removed = get_store().lock().unwrap().remove(&id);
	if removed {
		// Wake up the schedule monitor so it recalculates the next due time
		if let Some(sid) = crate::session::context::current_session_id() {
			crate::session::context::notify_schedule_change(&sid);
		}
		Ok(McpToolResult::success(
			call.tool_name.clone(),
			call.tool_id.clone(),
			format!("✅ Removed scheduled entry [{}].", id),
		))
	} else {
		Ok(McpToolResult::error(
			call.tool_name.clone(),
			call.tool_id.clone(),
			format!("No scheduled entry found with id '{}'.", id),
		))
	}
}

fn handle_edit(call: &McpToolCall) -> Result<McpToolResult> {
	let id = match call.parameters.get("id") {
		Some(Value::String(s)) if !s.trim().is_empty() => s.clone(),
		Some(_) => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"'id' must be a non-empty string".to_string(),
			))
		}
		None => {
			return Ok(McpToolResult::error(
				call.tool_name.clone(),
				call.tool_id.clone(),
				"missing required parameter 'id' for edit".to_string(),
			))
		}
	};

	let new_when = match call.parameters.get("when") {
		Some(Value::String(s)) if !s.trim().is_empty() => match parse_when(s) {
			Ok(t) => Some(t),
			Err(e) => {
				return Ok(McpToolResult::error(
					call.tool_name.clone(),
					call.tool_id.clone(),
					format!("invalid 'when' value: {}", e),
				))
			}
		},
		_ => None,
	};

	let new_message = match call.parameters.get("message") {
		Some(Value::String(s)) if !s.trim().is_empty() => Some(s.clone()),
		_ => None,
	};

	let new_description = match call.parameters.get("description") {
		Some(Value::String(s)) if !s.trim().is_empty() => Some(s.clone()),
		_ => None,
	};

	if new_when.is_none() && new_message.is_none() && new_description.is_none() {
		return Ok(McpToolResult::error(
			call.tool_name.clone(),
			call.tool_id.clone(),
			"edit requires at least one of: when, message, description".to_string(),
		));
	}
	let store = get_store();
	let updated = store
		.lock()
		.unwrap()
		.edit(&id, new_description, new_message, new_when);

	if updated {
		// Wake up the schedule monitor so it recalculates the next due time
		if let Some(sid) = crate::session::context::current_session_id() {
			crate::session::context::notify_schedule_change(&sid);
		}

		// Read back the updated entry for confirmation.
		let store = get_store();
		let guard = store.lock().unwrap();
		let entry = guard.entries().iter().find(|e| e.id == id);
		let summary = entry
			.map(|e| {
				format!(
					" → fires at {} ({})",
					e.trigger_at.format("%Y-%m-%d %H:%M:%S"),
					e.countdown()
				)
			})
			.unwrap_or_default();

		Ok(McpToolResult::success(
			call.tool_name.clone(),
			call.tool_id.clone(),
			format!("✅ Updated scheduled entry [{}]{}.", id, summary),
		))
	} else {
		Ok(McpToolResult::error(
			call.tool_name.clone(),
			call.tool_id.clone(),
			format!("No scheduled entry found with id '{}'.", id),
		))
	}
}