Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: show the whole conversation for imported Claude chats (#13182)
Nilay4ce8866c3109
diff --git a/studio/backend/core/external_import/claude.py b/studio/backend/core/external_import/claude.pyindex b01a5975d0a..a61aa3f9cbd 100644--- a/studio/backend/core/external_import/claude.py+++ b/studio/backend/core/external_import/claude.py@@ -80,27 +80,59 @@ def _parts(record: dict, message_id: str) -> tuple[list[dict], dict[str, str]]:elif kind == "tool_use" and not user:parts.append(tool_call(str(block.get("id") or f"{message_id}-{position}"), block))elif kind == "tool_result" and user and block.get("tool_use_id"):- # An empty output is still a finished call; Studio replays "" differently from none.+ # empty output still marks a finished call; Studio replays "" differently from noneresults[str(block["tool_use_id"])] = _result_text(block.get("content"))return parts, resultsdef read_transcript(path: Path, thread_id: str, session_id: str) -> Transcript:file_created, file_updated = file_times_ms(path)- # File order, not a tree walk: the import ledger relies on append-only order.- records = [- r- for r in read_jsonl(path)- if r.get("type") in ("user", "assistant")- and not r.get("isSidechain")- and not r.get("isMeta")- ]- by_uuid = {str(r["uuid"]): r for r in records if r.get("uuid")}- imported: dict[str, str] = {}+ # file order preserves the append-only import ledger+ # attachments and system lines link ancestry; compact_boundary inherits prior if logicalParentUuid is absent or forward.+ records: list[tuple[dict[str, Any], int | None, int | None]] = []+ parents: list[int | None] = []+ latest: dict[str, int] = {}+ previous_node = None+ for line in read_jsonl(path):+ parent_node = latest.get(str(line.get("parentUuid") or ""))+ node = None+ if line.get("uuid"):+ compacted = line.get("subtype") == "compact_boundary" and not line.get("parentUuid")+ if compacted:+ logical = str(line.get("logicalParentUuid") or "")+ parent_node = latest[logical] if logical in latest else previous_node+ node = len(parents)+ parents.append(parent_node)+ latest[str(line["uuid"])] = node+ previous_node = node+ if (+ line.get("type") in ("user", "assistant")+ and not line.get("isSidechain")+ and not line.get("isMeta")+ ):+ records.append((line, node, parent_node))+ imported: dict[int, str] = {}+ # parallel tool results hang off their own call; continue from the reply's last block, not a fork.+ tail_of: dict[int, int] = {}+ active_reply = None+ active_blocks: list[int] = []open_calls: dict[str, dict] = {}messages: list[dict] = []- for index, record in enumerate(records):+ for index, (record, node, parent_node) in enumerate(records):uuid = str(record.get("uuid") or "")+ reply = record.get("message", {}).get("id") if record["type"] == "assistant" else None+ continues_reply = record["type"] == "assistant" and reply and reply == active_reply+ if continues_reply and active_blocks:+ ancestor, seen = parent_node, set()+ while ancestor is not None and ancestor not in imported and ancestor not in seen:+ seen.add(ancestor)+ ancestor = parents[ancestor]+ continues_reply = ancestor == active_blocks[-1]+ if not continues_reply:+ if active_blocks:+ tail_of.update((block, active_blocks[-1]) for block in active_blocks)+ active_reply = reply+ active_blocks = []message_id = stable_id("claude", session_id, uuid or f"index:{index}", length = 16)parts, results = _parts(record, message_id)for call_id, result in results.items():@@ -109,25 +141,30 @@ def read_transcript(path: Path, thread_id: str, session_id: str) -> Transcript:if not parts:continueopen_calls.update((p["toolCallId"], p) for p in parts if p["type"] == "tool-call")- # Nearest ancestor that became a message; rewinds keep their branch this way.- parent, seen = record.get("parentUuid"), set()- while parent and parent not in imported and parent not in seen:+ # use the nearest imported ancestor so rewinds stay on their original branch.+ parent, seen = parent_node, set()+ while parent is not None and parent not in imported and parent not in seen:seen.add(parent)- parent = by_uuid.get(parent, {}).get("parentUuid")+ parent = parents[parent]+ parent = tail_of.get(parent, parent)timestamp = iso_ms(record.get("timestamp"))messages.append({"id": message_id,"threadId": thread_id,- "parentId": imported.get(parent) if parent else None,+ "parentId": imported.get(parent) if parent is not None else None,"role": record["type"],"content": parts,"createdAt": timestamp if timestamp is not None else file_created + index,"metadata": {"importedFrom": "claude", "claudeSessionId": session_id},})- if uuid:- imported[uuid] = message_id+ if node is not None:+ imported[node] = message_id+ if reply:+ active_blocks.append(node)+ if active_blocks:+ tail_of.update((block, active_blocks[-1]) for block in active_blocks)return Transcript(session_id = session_id,thread_id = thread_id,diff --git a/studio/backend/tests/test_external_import.py b/studio/backend/tests/test_external_import.pyindex c45fb0a265e..8955d35ab17 100644--- a/studio/backend/tests/test_external_import.py+++ b/studio/backend/tests/test_external_import.py@@ -1,7 +1,9 @@# SPDX-License-Identifier: AGPL-3.0-only# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0+import gcimport json+import weakreffrom pathlib import Pathimport pytest@@ -287,6 +289,199 @@ def test_claude_rewinds_keep_their_branch(claude_home):assert parents == [None, ids[0], ids[1], ids[1]]+def test_claude_harness_records_and_compaction_keep_one_conversation(claude_home):+ path = _session(+ claude_home,+ records = [+ c_user("u1", "one"),+ c_user("meta", "caveat", parent = "u1", isMeta = True),+ {"type": "attachment", "uuid": "at1", "parentUuid": "meta", "attachment": {}},+ c_asst("a1", [{"type": "text", "text": "A"}], parent = "at1"),+ {"type": "system", "subtype": "turn_duration", "uuid": "s1", "parentUuid": "a1"},+ c_user("u2", "two", parent = "s1"),+ c_asst("a2", [{"type": "text", "text": "B"}], parent = "u2"),+ {+ "type": "system",+ "subtype": "compact_boundary",+ "uuid": "cb",+ "parentUuid": None,+ "logicalParentUuid": "a2",+ },+ c_user("u3", "three", parent = "cb"),+ c_asst("a3", [{"type": "text", "text": "C"}], parent = "u3"),+ ],+ )+ messages = claude.read_transcript(path, "t", "s1").messages+ ids = [m["id"] for m in messages]+ assert [m["parentId"] for m in messages] == [None, *ids[:-1]]+++def test_claude_rewind_compaction_uses_its_logical_parent(claude_home):+ path = _session(+ claude_home,+ records = [+ c_user("u1", "one"),+ c_asst("a1", [{"type": "text", "text": "A"}], parent = "u1"),+ c_user("u2", "abandoned", parent = "a1"),+ c_asst("a2", [{"type": "text", "text": "B"}], parent = "u2"),+ {+ "type": "system",+ "subtype": "compact_boundary",+ "uuid": "cb",+ "parentUuid": None,+ "logicalParentUuid": "a1",+ },+ c_user("sum", "summary", parent = "cb", isCompactSummary = True),+ c_user("u3", "three", parent = "sum"),+ c_asst("a3", [{"type": "text", "text": "C"}], parent = "u3"),+ ],+ )+ messages = claude.read_transcript(path, "t", "s1").messages+ ids = [m["id"] for m in messages]+ assert [m["parentId"] for m in messages] == [None, ids[0], ids[1], ids[2], ids[1], ids[4], ids[5]]+++def test_claude_releases_filtered_records_while_streaming(tmp_path, monkeypatch):+ class Record(dict):+ pass++ released = False++ def records(_path):+ nonlocal released+ attachment = Record(type = "attachment", uuid = "at1", parentUuid = None, payload = "x" * 1_000_000)+ reference = weakref.ref(attachment, lambda _ref: mark_released())+ yield attachment+ attachment = None+ yield {"type": "progress", "uuid": "p1", "parentUuid": "at1"}+ gc.collect()+ assert reference() is None+ yield c_user("u1", "one", parent = "p1")++ def mark_released():+ nonlocal released+ released = True++ path = tmp_path / "session.jsonl"+ path.touch()+ monkeypatch.setattr(claude, "read_jsonl", records)+ messages = claude.read_transcript(path, "t", "s1").messages+ assert released+ assert len(messages) == 1+++def test_claude_duplicate_reply_ids_do_not_merge_sequential_responses(claude_home):+ def block(uuid, reply, tool_id, parent):+ record = c_asst(+ uuid,+ [{"type": "tool_use", "id": tool_id, "name": "Bash", "input": {"cmd": "ls"}}],+ parent = parent,+ )+ record["message"]["id"] = reply+ return record++ def result(uuid, tool_id, parent):+ return c_user(+ uuid, [{"type": "tool_result", "tool_use_id": tool_id, "content": "ok"}], parent = parent+ )++ path = _session(+ claude_home,+ records = [+ c_user("u1", "one"),+ block("a1", "duplicate", "t1", "u1"),+ result("r1", "t1", "a1"),+ block("a2", "duplicate", "t2", "r1"),+ result("r2", "t2", "a2"),+ c_asst("a3", [{"type": "text", "text": "done"}], parent = "r2"),+ c_user("u2", "rewind", parent = "a1"),+ ],+ )+ messages = claude.read_transcript(path, "t", "s1").messages+ ids = [m["id"] for m in messages]+ assert [m["parentId"] for m in messages] == [None, ids[0], ids[1], ids[2], ids[1]]+++def test_claude_duplicate_reply_ids_do_not_merge_sibling_responses(claude_home):+ first = c_asst("a1", [{"type": "text", "text": "first"}], parent = "u1")+ first["message"]["id"] = "duplicate"+ alternate = c_asst("a2", [{"type": "text", "text": "alternate"}], parent = "u1")+ alternate["message"]["id"] = "duplicate"+ path = _session(+ claude_home,+ records = [+ c_user("u1", "one"),+ first,+ alternate,+ c_user("u2", "rewind from first", parent = "a1"),+ ],+ )+ messages = claude.read_transcript(path, "t", "s1").messages+ ids = [m["id"] for m in messages]+ assert [m["parentId"] for m in messages] == [None, ids[0], ids[0], ids[1]]+++def test_claude_duplicate_hook_uuids_keep_per_occurrence_ancestry(claude_home):+ path = _session(+ claude_home,+ records = [+ c_user("u1", "one"),+ {"type": "saved_hook_context", "uuid": "hook", "parentUuid": "u1"},+ c_asst("a1", [{"type": "text", "text": "A"}], parent = "hook"),+ c_user("u2", "two", parent = "a1"),+ {"type": "saved_hook_context", "uuid": "hook", "parentUuid": "u2"},+ c_asst("a2", [{"type": "text", "text": "B"}], parent = "hook"),+ ],+ )+ messages = claude.read_transcript(path, "t", "s1").messages+ ids = [m["id"] for m in messages]+ assert [m["parentId"] for m in messages] == [None, ids[0], ids[1], ids[2]]++[email protected]("logical", ["never-written", "at2"])+def test_claude_parallel_tool_calls_and_compaction_keep_one_conversation(claude_home, logical):+ def block(uuid, reply, content, parent):+ record = c_asst(uuid, content, parent = parent)+ record["message"]["id"] = reply+ return record++ def call(tool_id):+ return [{"type": "tool_use", "id": tool_id, "name": "Bash", "input": {"cmd": "ls"}}]++ def result(uuid, tool_id, parent):+ return c_user(+ uuid, [{"type": "tool_result", "tool_use_id": tool_id, "content": "ok"}], parent = parent+ )++ path = _session(+ claude_home,+ records = [+ c_user("u1", "one"),+ block("a1", "m1", call("t1"), "u1"),+ block("a1b", "m1", call("t2"), "a1"),+ result("r2", "t2", "a1b"),+ result("r1", "t1", "a1"),+ block("a2", "m2", [{"type": "text", "text": "A"}], "r1"),+ c_user("u2", "/compact", parent = "a2"),+ {+ "type": "system",+ "subtype": "compact_boundary",+ "uuid": "cb",+ "parentUuid": None,+ "logicalParentUuid": logical,+ },+ c_user("sum", "summary", parent = "cb", isCompactSummary = True),+ {"type": "attachment", "uuid": "at2", "parentUuid": "sum", "attachment": {}},+ c_user("u3", "three", parent = "at2"),+ c_asst("a3", [{"type": "text", "text": "C"}], parent = "u3"),+ ],+ )+ messages = claude.read_transcript(path, "t", "s1").messages+ ids = [m["id"] for m in messages]+ assert len(messages) == 8+ assert [m["parentId"] for m in messages] == [None, *ids[:-1]]++def test_cursor_reads_the_query_and_strips_injected_context(cursor_home):path = _write(cursor_home / "projects" / "p" / "agent-transcripts" / "s1" / "s1.jsonl",