zagens-cli 0.7.5

Zagens headless CLI + HTTP/SSE runtime sidecar (`zagens`, `zagens-runtime` binaries)
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
//! Session list/detail and resume-thread seeding (R-003 A4.5).

use std::collections::HashMap;
use std::sync::Arc;

use axum::Json;
use axum::extract::{Path as AxumPath, Query, State};
use axum::http::StatusCode;
use serde::{Deserialize, Serialize};
use serde_json::json;
use tokio::sync::{Mutex, Semaphore};

use crate::runtime_threads::CreateThreadRequest;
use crate::session_manager::SavedSession;

use zagens_runtime_api::{
    ApiError, ResumeSessionResponse, SessionDetailResponse, SessionsListResponse,
};

use super::RuntimeApiState;

#[derive(Debug, Clone, Serialize)]
pub(crate) struct ResumeTaskState {
    thread_id: String,
    session_id: String,
    state: String,
    items_written: usize,
    items_total: usize,
    #[serde(skip_serializing_if = "Option::is_none")]
    error: Option<String>,
}

#[derive(Clone)]
pub(crate) struct ResumeTaskTracker {
    tasks: Arc<Mutex<HashMap<String, ResumeTaskState>>>,
    seed_gate: Arc<Semaphore>,
}

impl ResumeTaskTracker {
    pub(crate) fn new() -> Self {
        Self {
            tasks: Arc::new(Mutex::new(HashMap::new())),
            seed_gate: Arc::new(Semaphore::new(1)),
        }
    }

    async fn register(&self, thread_id: &str, session_id: &str, items_total: usize) {
        let mut tasks = self.tasks.lock().await;
        tasks.insert(
            thread_id.to_string(),
            ResumeTaskState {
                thread_id: thread_id.to_string(),
                session_id: session_id.to_string(),
                state: "seeding".to_string(),
                items_written: 0,
                items_total,
                error: None,
            },
        );
    }

    async fn mark_ready(&self, thread_id: &str) {
        let mut tasks = self.tasks.lock().await;
        if let Some(task) = tasks.get_mut(thread_id) {
            task.state = "ready".to_string();
            task.items_written = task.items_total;
        }
    }

    async fn mark_error(&self, thread_id: &str, error: String) {
        let mut tasks = self.tasks.lock().await;
        if let Some(task) = tasks.get_mut(thread_id) {
            task.state = "error".to_string();
            task.error = Some(error);
        }
    }

    async fn get(&self, thread_id: &str) -> Option<ResumeTaskState> {
        let tasks = self.tasks.lock().await;
        tasks.get(thread_id).cloned()
    }

    async fn acquire_seed_permit(&self) -> tokio::sync::OwnedSemaphorePermit {
        self.seed_gate
            .clone()
            .acquire_owned()
            .await
            .expect("seed gate semaphore closed")
    }
}

#[derive(Debug, Deserialize)]
pub(crate) struct ResumeSessionRequest {
    model: Option<String>,
    mode: Option<String>,
    #[serde(default)]
    task_type: Option<String>,
}

#[derive(Debug, Deserialize)]
pub(crate) struct SessionsQuery {
    limit: Option<usize>,
    search: Option<String>,
}

pub(crate) async fn list_sessions(
    State(state): State<RuntimeApiState>,
    Query(query): Query<SessionsQuery>,
) -> Result<Json<SessionsListResponse>, ApiError> {
    let manager = state.shared_session_manager.clone();
    let search = query.search.clone();
    let limit = query.limit.unwrap_or(50).clamp(1, 500);
    let mut sessions = tokio::task::spawn_blocking(move || {
        if let Some(search) = search {
            manager
                .search_sessions(&search)
                .map_err(|e| format!("Failed to search sessions: {e}"))
        } else {
            manager
                .list_sessions()
                .map_err(|e| format!("Failed to list sessions: {e}"))
        }
    })
    .await
    .map_err(|e| ApiError::internal(format!("session list task panicked: {e}")))?
    .map_err(|e: String| ApiError::internal(e))?;
    sessions.truncate(limit);
    Ok(Json(SessionsListResponse { sessions }))
}

pub(crate) async fn get_session(
    State(state): State<RuntimeApiState>,
    AxumPath(id): AxumPath<String>,
) -> Result<Json<SessionDetailResponse>, ApiError> {
    let manager = state.shared_session_manager.clone();
    let detail = tokio::task::spawn_blocking({
        let id = id.clone();
        move || -> std::io::Result<SessionDetailResponse> {
            let session = manager.load_session(&id)?;
            Ok(session_to_detail(session))
        }
    })
    .await
    .map_err(|e| ApiError::internal(format!("get session task panicked: {e}")))?
    .map_err(|e| map_session_err(&id, e, "read"))?;
    Ok(Json(detail))
}

pub(crate) async fn resume_session_thread(
    State(state): State<RuntimeApiState>,
    AxumPath(id): AxumPath<String>,
    Json(req): Json<ResumeSessionRequest>,
) -> Result<(StatusCode, Json<ResumeSessionResponse>), ApiError> {
    let t0 = std::time::Instant::now();
    let manager = state.shared_session_manager.clone();
    let session = tokio::task::spawn_blocking({
        let id = id.clone();
        move || -> Result<SavedSession, std::io::Error> { manager.load_session(&id) }
    })
    .await
    .map_err(|e| ApiError::internal(format!("resume session task panicked: {e}")))?
    .map_err(|e| map_session_err(&id, e, "read"))?;
    let t1 = t0.elapsed();
    eprintln!(
        "[resume-session] load_session done in {:.1}s, {} messages",
        t1.as_secs_f64(),
        session.messages.len(),
    );

    let model = req.model.unwrap_or_else(|| session.metadata.model.clone());
    let mode = req.mode.unwrap_or_else(|| {
        session
            .metadata
            .mode
            .clone()
            .unwrap_or_else(|| "agent".to_string())
    });

    let workspace = session.metadata.workspace.clone();
    let task_type = crate::task_type::resolve_task_type(req.task_type.as_deref(), &workspace, None);

    // Reuse the persisted runtime thread when it still has events so Zagens can
    // replay tool cards and thinking after app restart (instead of seeding a blank thread).
    if let Some(ref stored_tid) = session.metadata.runtime_thread_id {
        let stored_tid = stored_tid.trim();
        if !stored_tid.is_empty() {
            match state.runtime_threads.load_thread_sync(stored_tid) {
                Ok(_) => {
                    let has_events = state
                        .runtime_threads
                        .events_since_async(stored_tid, Some(0))
                        .await
                        .map(|events| !events.is_empty())
                        .unwrap_or(false);
                    if has_events {
                        eprintln!(
                            "[resume-session] reusing runtime thread {stored_tid} (events present)"
                        );
                        return Ok((
                            StatusCode::OK,
                            Json(ResumeSessionResponse {
                                thread_id: stored_tid.to_string(),
                                session_id: id,
                                message_count: session.messages.len(),
                                state: "ready".to_string(),
                            }),
                        ));
                    }
                    eprintln!(
                        "[resume-session] linked thread {stored_tid} has no events — seeding new thread"
                    );
                }
                Err(e) => {
                    eprintln!(
                        "[resume-session] linked thread {stored_tid} not on disk ({e}) — seeding new thread"
                    );
                }
            }
        }
    }

    let thread = state
        .runtime_threads
        .create_thread(CreateThreadRequest {
            model: Some(model),
            workspace: Some(workspace),
            mode: Some(mode),
            allow_shell: None,
            trust_mode: None,
            auto_approve: None,
            archived: false,
            system_prompt: session.system_prompt.clone(),
            task_id: None,
            task_type: Some(task_type.as_str().to_string()),
        })
        .await
        .map_err(|e| ApiError::internal(format!("Failed to create thread: {e}")))?;

    let msgs = session.messages;
    let msg_count = msgs.len();
    let tid = thread.id.clone();
    let tid_log = tid.clone();

    // Link session file to this thread immediately so the next app restart can replay events.
    {
        let manager = state.shared_session_manager.clone();
        let session_id = id.clone();
        let link_tid = tid.clone();
        let link_result = tokio::task::spawn_blocking(move || -> std::io::Result<()> {
            let mut saved = manager.load_session(&session_id)?;
            saved.metadata.runtime_thread_id = Some(link_tid);
            manager.save_session(&saved).map(|_| ())
        })
        .await;
        match link_result {
            Ok(Ok(())) => {}
            Ok(Err(e)) => {
                eprintln!("[resume-session] link runtime_thread_id: {e}");
            }
            Err(e) => {
                eprintln!("[resume-session] link runtime_thread_id task: {e}");
            }
        }
    }

    state.resume_tracker.register(&tid, &id, msg_count).await;

    let tracker = state.resume_tracker.clone();
    let threads = state.runtime_threads.clone();
    tokio::spawn(async move {
        let _permit = tracker.acquire_seed_permit().await;

        let seed_result = tokio::task::spawn_blocking({
            let rt = tokio::runtime::Handle::current();
            let tid_clone = tid.clone();
            move || {
                rt.block_on(async { threads.seed_thread_from_messages(&tid_clone, &msgs).await })
            }
        })
        .await
        .map_err(|e| format!("seed panicked: {e}"))
        .and_then(|r| r.map_err(|e| format!("{e:#}")));

        match seed_result {
            Ok(()) => {
                let elapsed = t0.elapsed();
                eprintln!(
                    "[resume-session] seed done in {:.1}s total, thread={}",
                    elapsed.as_secs_f64(),
                    tid_log,
                );
                tracker.mark_ready(&tid).await;
            }
            Err(err) => {
                eprintln!("[resume-session] seed failed, thread={}: {}", tid_log, err,);
                tracker.mark_error(&tid, err).await;
            }
        }
    });

    Ok((
        StatusCode::ACCEPTED,
        Json(ResumeSessionResponse {
            thread_id: thread.id,
            session_id: id,
            message_count: msg_count,
            state: "seeding".to_string(),
        }),
    ))
}

pub(crate) async fn delete_session(
    State(state): State<RuntimeApiState>,
    AxumPath(id): AxumPath<String>,
) -> Result<StatusCode, ApiError> {
    let manager = state.shared_session_manager.clone();
    tokio::task::spawn_blocking({
        let id = id.clone();
        move || -> Result<(), std::io::Error> { manager.delete_session(&id) }
    })
    .await
    .map_err(|e| ApiError::internal(format!("delete session task panicked: {e}")))?
    .map_err(|e| map_session_err(&id, e, "delete"))?;
    Ok(StatusCode::NO_CONTENT)
}

pub(crate) async fn get_resume_task(
    State(state): State<RuntimeApiState>,
    AxumPath(thread_id): AxumPath<String>,
) -> Result<Json<ResumeTaskState>, ApiError> {
    state
        .resume_tracker
        .get(&thread_id)
        .await
        .ok_or_else(|| ApiError::not_found(format!("resume task not found for thread {thread_id}")))
        .map(Json)
}

fn session_to_detail(session: SavedSession) -> SessionDetailResponse {
    let messages: Vec<serde_json::Value> = session
        .messages
        .iter()
        .map(|msg| {
            let content_blocks: Vec<serde_json::Value> = msg
                .content
                .iter()
                .map(|block| match block {
                    crate::models::ContentBlock::Text { text, .. } => {
                        json!({ "type": "text", "text": text })
                    }
                    crate::models::ContentBlock::Thinking { thinking, .. } => {
                        json!({ "type": "thinking", "text": thinking })
                    }
                    crate::models::ContentBlock::ToolUse {
                        id, name, input, ..
                    } => {
                        json!({
                            "type": "tool_use",
                            "id": id,
                            "name": name,
                            "input": input,
                        })
                    }
                    crate::models::ContentBlock::ToolResult {
                        tool_use_id,
                        content,
                        is_error,
                        ..
                    } => {
                        json!({
                            "type": "tool_result",
                            "tool_use_id": tool_use_id,
                            "content": content,
                            "is_error": is_error,
                        })
                    }
                    _ => json!({ "type": "other" }),
                })
                .collect();
            json!({
                "role": msg.role,
                "content": content_blocks,
            })
        })
        .collect();
    SessionDetailResponse {
        metadata: session.metadata,
        messages,
        system_prompt: session.system_prompt,
    }
}

pub(crate) fn map_session_err(id: &str, err: std::io::Error, action: &str) -> ApiError {
    match err.kind() {
        std::io::ErrorKind::NotFound => ApiError::not_found(format!("Session '{id}' not found")),
        std::io::ErrorKind::InvalidData => {
            ApiError::bad_request(format!("Failed to parse session '{id}': {err}"))
        }
        std::io::ErrorKind::InvalidInput => {
            ApiError::bad_request(format!("Invalid session id '{id}'"))
        }
        _ => ApiError::internal(format!("Failed to {action} session '{id}': {err}")),
    }
}