Skip to main content

agentmux_srv_core/backend/transcript/
turns.rs

1// Copyright 2025-2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Where a turn starts in a Claude stream-json transcript.
5
6/// Whether `line` starts a turn in a Claude stream-json transcript: the same
7/// rule as the frontend's `ClaudeTranslator.handleUserMessage` producing a
8/// `user_message` — a `user` record whose content is a non-empty string, or
9/// an array with an image and some text and no `tool_result` (a text-only
10/// array is how the CLI records "[Request interrupted by user]" and other
11/// meta lines, never rendered as a turn).
12pub fn is_claude_turn_start(line: &str) -> bool {
13    use serde_json::Value;
14    if !line.contains("\"user\"") {
15        return false;
16    }
17    let Ok(v) = serde_json::from_str::<Value>(line) else {
18        return false;
19    };
20    if v.get("type").and_then(Value::as_str) != Some("user") {
21        return false;
22    }
23    match v.get("message").and_then(|m| m.get("content")) {
24        Some(Value::String(s)) => !s.is_empty(),
25        Some(Value::Array(blocks)) => {
26            let ty = |b: &Value| b.get("type").and_then(Value::as_str).map(str::to_owned);
27            let has = |t: &str| blocks.iter().any(|b| ty(b).as_deref() == Some(t));
28            !has("tool_result")
29                && has("image")
30                && blocks.iter().any(|b| {
31                    ty(b).as_deref() == Some("text")
32                        && b.get("text")
33                            .and_then(Value::as_str)
34                            .is_some_and(|t| !t.is_empty())
35                })
36        }
37        _ => false,
38    }
39}