Skip to content

armgit / Wake

Public Imported from GitHub
0

All your AI agent sessions in one place — browse, search, resume. Rust + GPUI.

Rust

feat: add inline transcript images

Corey Chiua7b8f5ff8bf5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a9add13..87d6f27 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,11 @@
## Unreleased
+- New: inline images from every readable agent transcript format now appear with compact thumbnails, full-window zoom, copy, and collision-safe save-to-Downloads actions; unsupported image formats can still be saved in their original bytes
+- Performance and privacy: image bytes are decoded only when a transcript is opened, each image is capped at 12 MiB with a 64 MiB transcript budget, remote URLs and transcript-supplied local paths are never dereferenced, and background indexing keeps lightweight `[image]` placeholders instead of base64 payloads
+- Fix: global search now closes reliably when clicking outside its panel while remaining open when switching to another application
+- Credits: inline-image support was inspired by [PR #14](https://github.com/iAmCorey/Wake/pull/14). Thanks to [@Tespera](https://github.com/Tespera) for investigating the Claude Code and Codex formats; the final implementation generalized that work across all agent sources that expose readable image blocks
+
## [0.3.3] — 2026-09-01
- New: Grok Build `execute-plan` and subagent sessions now nest under their root parent, with expandable child counts, root-level sidebar totals, source-project recovery for worktrees, flat Starred/search results, and cascading Trash deletion
diff --git a/Cargo.lock b/Cargo.lock
index 756dba9..d36d9fa 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7749,6 +7749,7 @@ dependencies = [
"gpui",
"gpui-component",
"gpui_platform",
+ "image",
"semver",
"serde",
"serde_json",
@@ -7765,6 +7766,7 @@ name = "wake-core"
version = "0.3.3"
dependencies = [
"anyhow",
+ "base64",
"chrono",
"dirs 5.0.1",
"notify",
diff --git a/Cargo.toml b/Cargo.toml
index b5006f1..de7bd7d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,6 +19,8 @@ chrono = { version = "0.4", features = ["serde"] }
zstd = "0.13"
semver = "1"
reqwest = { package = "zed-reqwest", version = "0.12.15-zed", default-features = false, features = ["blocking", "json", "rustls-tls-native-roots"] }
+base64 = "0.22"
+image = { version = "0.25", default-features = false, features = ["png", "jpeg", "gif", "webp", "bmp", "tiff"] }
[profile.dev]
# gpui 依赖树巨大,dev 依赖开优化让 UI 不卡
diff --git a/DESIGN.md b/DESIGN.md
index 224331a..2fd6771 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -184,6 +184,7 @@ macOS 不设置横跨三栏的自定义 header。主窗口透明标题栏高 44p
- 用户使用靠右的低饱和引用块。
- 助手正文平铺,不在每条回复前重复 Agent 名称。
+- 消息图片以 104px、10px 圆角的缩略图随正文排列;点击后使用全窗口暗色遮罩按原始宽高比预览,绝不放大超过源尺寸。预览胶囊集中显示格式、尺寸、文件大小以及复制/保存动作;无法直接预览的格式保留原始字节,并提供下载入口。
- 仅有 thinking、没有正文或工具的中间事件不进入阅读视图;正式回复所带的 thinking 显示为折叠面板,收起是一行摘要,展开后显示完整原文。Thinking 与工具调用分别保存展开状态,禁止互相连带。
- Codex review 的 `ExitedReviewMode.review_output` 转为可读 Markdown,展示结论、说明、finding、代码位置与置信度;注入式 `<user_action>` 只作缺少结构化事件时的文本兜底,不显示 XML 外壳,也不与结构化结果重复。
- 工具调用合并为低强调折叠卡:单条收起时显示工具名与参数摘要,多条显示数量与名称序列,失败数常驻。摘要格数从当前阅读区像素宽反算,再用 `unicode-width` 截断。展开后显示可用的完整 Input,以及成功和失败 Output;失败结果使用 danger 色。Input/Output 面板最多展示前 600 个字符,但始终提供复制完整原文的操作,不引入嵌套滚动区。
diff --git a/crates/wake-core/Cargo.toml b/crates/wake-core/Cargo.toml
index b7810a4..2558ae5 100644
--- a/crates/wake-core/Cargo.toml
+++ b/crates/wake-core/Cargo.toml
@@ -14,6 +14,7 @@ walkdir.workspace = true
notify.workspace = true
chrono.workspace = true
zstd.workspace = true
+base64.workspace = true
# Linux 废纸篓:freedesktop Trash spec 的纯库实现
# (macOS 走 Finder/osascript,行为已验证不动)
diff --git a/crates/wake-core/src/adapters/claude.rs b/crates/wake-core/src/adapters/claude.rs
index 5a721b8..9c9afb8 100644
--- a/crates/wake-core/src/adapters/claude.rs
+++ b/crates/wake-core/src/adapters/claude.rs
@@ -51,7 +51,7 @@ struct ParseResult {
#[derive(Default)]
struct PendingAssistant {
msg_id: Option<String>,
- text: Vec<String>,
+ content: ParsedContent,
thinking: Vec<String>,
tool_calls: Vec<ToolCallView>,
timestamp: Option<i64>,
@@ -64,8 +64,8 @@ fn flush_assistant(
tool_index: &mut HashMap<String, (usize, usize)>,
) {
let Some(p) = pending.take() else { return };
- let text = p.text.join("\n\n");
- if text.is_empty() && p.thinking.is_empty() && p.tool_calls.is_empty() {
+ let ParsedContent { text, images } = p.content;
+ if text.is_empty() && p.thinking.is_empty() && p.tool_calls.is_empty() && images.is_empty() {
return;
}
let (clipped, truncated) = clip(&text, MAX_MSG_TEXT);
@@ -91,10 +91,16 @@ fn flush_assistant(
thinking,
timestamp: p.timestamp,
model: p.model,
+ images,
});
}
-fn parse_claude_jsonl(path: &Path, include_sidechain: bool) -> Result<ParseResult> {
+fn parse_claude_jsonl(
+ path: &Path,
+ include_sidechain: bool,
+ decode_images: bool,
+) -> Result<ParseResult> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let file = fs::File::open(path)?;
let reader = BufReader::with_capacity(1 << 20, file);
@@ -196,6 +202,7 @@ fn parse_claude_jsonl(path: &Path, include_sidechain: bool) -> Result<ParseResul
thinking: None,
timestamp: ts_opt(ts),
model: None,
+ images: Vec::new(),
});
}
}
@@ -206,17 +213,17 @@ fn parse_claude_jsonl(path: &Path, include_sidechain: bool) -> Result<ParseResul
flush_assistant(&mut pending, &mut messages, &mut tool_index);
let Some(message) = message else { continue };
let content = message.get("content");
- let mut parts: Vec<String> = Vec::new();
+ let mut parsed_message = ParsedContent::default();
let mut had_tool_result = false;
match content {
- Some(Value::String(s)) => parts.push(s.clone()),
+ Some(Value::String(s)) => parsed_message.push_text(s),
Some(Value::Array(blocks)) => {
for b in blocks {
match b.get("type").and_then(|v| v.as_str()) {
Some("text") => {
if let Some(t) = b.get("text").and_then(|v| v.as_str()) {
- parts.push(t.to_string());
+ parsed_message.push_text(t);
}
}
Some("tool_result") => {
@@ -224,15 +231,23 @@ fn parse_claude_jsonl(path: &Path, include_sidechain: bool) -> Result<ParseResul
let id =
b.get("tool_use_id").and_then(|v| v.as_str()).unwrap_or("");
if let Some(&(mi, ti)) = tool_index.get(id) {
- let out = stringify_tool_result(b.get("content"));
- let target = &mut messages[mi].tool_calls[ti];
- target.output = Some(clip(&out, MAX_TOOL_IO).0);
+ let parsed = tool_result_parts(
+ b.get("content").unwrap_or(&Value::Null),
+ decode_images,
+ );
+ let message = &mut messages[mi];
+ let target = &mut message.tool_calls[ti];
+ target.output = Some(clip(&parsed.text, MAX_TOOL_IO).0);
if b.get("is_error").and_then(|v| v.as_bool()) == Some(true) {
target.is_error = true;
}
+ append_images_to_message_end(message, parsed.images);
}
}
- Some("image") => parts.push("[image]".to_string()),
+ _ if is_image_part(b) => {
+ let parsed = content_parts(b, decode_images);
+ parsed_message.append(parsed);
+ }
_ => {}
}
}
@@ -240,8 +255,8 @@ fn parse_claude_jsonl(path: &Path, include_sidechain: bool) -> Result<ParseResul
_ => {}
}
- let text = parts.join("\n\n").trim().to_string();
- if text.is_empty() {
+ let ParsedContent { text, images } = parsed_message;
+ if text.is_empty() && images.is_empty() {
let _ = had_tool_result;
continue;
}
@@ -269,6 +284,7 @@ fn parse_claude_jsonl(path: &Path, include_sidechain: bool) -> Result<ParseResul
thinking: None,
timestamp: ts_opt(ts),
model: None,
+ images,
});
continue;
}
@@ -321,7 +337,7 @@ fn parse_claude_jsonl(path: &Path, include_sidechain: bool) -> Result<ParseResul
Some("text") => {
if let Some(t) = b.get("text").and_then(|v| v.as_str()) {
if !t.trim().is_empty() {
- p.text.push(t.to_string());
+ p.content.push_text(t);
}
}
}
@@ -343,11 +359,15 @@ fn parse_claude_jsonl(path: &Path, include_sidechain: bool) -> Result<ParseResul
p.tool_calls
.push(tool_call_view(id, name, &input, None, false));
}
+ _ if is_image_part(b) => {
+ let parsed = content_parts(b, decode_images);
+ p.content.append(parsed);
+ }
_ => {}
}
}
}
- Some(Value::String(s)) if !s.trim().is_empty() => p.text.push(s.clone()),
+ Some(Value::String(s)) if !s.trim().is_empty() => p.content.push_text(s),
_ => {}
}
}
@@ -392,23 +412,7 @@ fn mk_msg(role: Role, kind: MessageKind, text: &str, ts: i64) -> TranscriptMessa
thinking: None,
timestamp: ts_opt(ts),
model: None,
- }
-}
-
-fn stringify_tool_result(content: Option<&Value>) -> String {
- match content {
- Some(Value::String(s)) => s.clone(),
- Some(Value::Array(arr)) => arr
- .iter()
- .filter_map(|c| match c.get("type").and_then(|v| v.as_str()) {
- Some("text") => c.get("text").and_then(|v| v.as_str()).map(String::from),
- Some("image") => Some("[image]".to_string()),
- _ => None,
- })
- .collect::<Vec<_>>()
- .join("\n"),
- Some(v @ Value::Object(_)) => serde_json::to_string(v).unwrap_or_default(),
- _ => String::new(),
+ images: Vec::new(),
}
}
@@ -536,7 +540,7 @@ impl AgentAdapter for ClaudeAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let parsed = parse_claude_jsonl(Path::new(&r.file_path), false)?;
+ let parsed = parse_claude_jsonl(Path::new(&r.file_path), false, false)?;
let meta = build_meta(r, &parsed);
let units = units_from_messages(&parsed.messages);
Ok(ParsedSession {
@@ -547,7 +551,7 @@ impl AgentAdapter for ClaudeAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let parsed = parse_claude_jsonl(Path::new(&r.file_path), false)?;
+ let parsed = parse_claude_jsonl(Path::new(&r.file_path), false, true)?;
let sidechains = list_sidechains(r);
let mut mainline = parsed.messages.clone();
// 把 sidechain 挂到主线对应 Task tool call
@@ -579,7 +583,7 @@ impl AgentAdapter for ClaudeAdapter {
if !file.is_file() {
return Ok(Vec::new());
}
- let parsed = parse_claude_jsonl(&file, true)?;
+ let parsed = parse_claude_jsonl(&file, true, true)?;
Ok(parsed.messages)
}
diff --git a/crates/wake-core/src/adapters/codex.rs b/crates/wake-core/src/adapters/codex.rs
index e20f75f..603942a 100644
--- a/crates/wake-core/src/adapters/codex.rs
+++ b/crates/wake-core/src/adapters/codex.rs
@@ -288,7 +288,8 @@ fn format_review_json(text: &str) -> Option<String> {
format_review_output(&review)
}
-fn parse_rollout(path: &Path) -> Result<CodexParse> {
+fn parse_rollout(path: &Path, decode_images: bool) -> Result<CodexParse> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let file = fs::File::open(path)?;
let reader = BufReader::with_capacity(1 << 20, file);
@@ -372,47 +373,48 @@ fn parse_rollout(path: &Path) -> Result<CodexParse> {
match pt {
"message" => {
let role = payload.get("role").and_then(|v| v.as_str()).unwrap_or("");
- let mut parts: Vec<String> = Vec::new();
- match payload.get("content") {
- Some(Value::Array(blocks)) => {
- for b in blocks {
- if matches!(
- b.get("type").and_then(|v| v.as_str()),
- Some("input_text") | Some("output_text") | Some("text")
- ) {
- if let Some(t) = b.get("text").and_then(|v| v.as_str()) {
- parts.push(t.to_string());
- }
- }
+ let content = payload.get("content").unwrap_or(&Value::Null);
+ let blocks = content
+ .as_array()
+ .map(Vec::as_slice)
+ .unwrap_or_else(|| std::slice::from_ref(content));
+ let mut parsed_message = ParsedContent::default();
+ for block in blocks {
+ let mut parsed = content_parts(block, decode_images);
+ let text = if role == "user" {
+ clean_user_text(&parsed.text)
+ } else {
+ parsed.text.trim().to_string()
+ };
+ if text != parsed.text {
+ for image in &mut parsed.images {
+ image.text_offset = image.text_offset.min(text.len());
}
}
- Some(Value::String(s)) => parts.push(s.clone()),
- _ => {}
+ parsed.text = text;
+ parsed_message.append(parsed);
}
- let text = parts.join("\n\n").trim().to_string();
- if text.is_empty() {
+ let ParsedContent { text, images } = parsed_message;
+ if text.is_empty() && images.is_empty() {
continue;
}
- match role {
+ let mut message = match role {
"user" => {
if let Some(results) = review_results_from_user_action(&text) {
- messages.push(mk_msg(
- Role::Assistant,
- MessageKind::Text,
- &results,
- ts,
- ));
- pending_review_message = Some(messages.len() - 1);
+ pending_review_message = Some(messages.len());
+ mk_msg(Role::Assistant, MessageKind::Text, &results, ts)
} else {
- messages.push(mk_msg(Role::User, user_kind(&text), &text, ts));
+ mk_msg(Role::User, user_kind(&text), &text, ts)
}
}
"assistant" => {
let text = format_review_json(&text).unwrap_or(text);
- messages.push(mk_msg(Role::Assistant, MessageKind::Text, &text, ts))
+ mk_msg(Role::Assistant, MessageKind::Text, &text, ts)
}
- _ => messages.push(mk_msg(Role::System, MessageKind::Meta, &text, ts)),
- }
+ _ => mk_msg(Role::System, MessageKind::Meta, &text, ts),
+ };
+ message.images = images;
+ messages.push(message);
}
"reasoning" => {
// encrypted_content 丢弃,只取明文 summary
@@ -500,19 +502,12 @@ fn parse_rollout(path: &Path) -> Result<CodexParse> {
.and_then(|v| v.as_str())
.unwrap_or("");
if let Some(&(mi, ti)) = tool_index.get(call_id) {
- let out_text = match payload.get("output") {
- Some(Value::String(s)) => s.clone(),
- Some(o @ Value::Object(_)) => o
- .get("content")
- .and_then(|v| v.as_str())
- .map(String::from)
- .unwrap_or_else(|| {
- serde_json::to_string(o).unwrap_or_default()
- }),
- _ => String::new(),
- };
- messages[mi].tool_calls[ti].output =
- Some(clip(&out_text, MAX_TOOL_IO).0);
+ let output = payload.get("output").unwrap_or(&Value::Null);
+ let content = output.get("content").unwrap_or(output);
+ let parsed = tool_result_parts(content, decode_images);
+ let message = &mut messages[mi];
+ message.tool_calls[ti].output = Some(clip(&parsed.text, MAX_TOOL_IO).0);
+ append_images_to_message_end(message, parsed.images);
}
}
_ => {}
@@ -533,8 +528,9 @@ fn parse_rollout(path: &Path) -> Result<CodexParse> {
}
"user_message" => {
if let Some(m) = payload.get("message").and_then(|v| v.as_str()) {
- if !m.trim().is_empty() {
- if let Some(results) = review_results_from_user_action(m) {
+ let cleaned = clean_user_text(m);
+ if !cleaned.trim().is_empty() {
+ if let Some(results) = review_results_from_user_action(&cleaned) {
event_fallback.push(mk_msg(
Role::Assistant,
MessageKind::Text,
@@ -544,8 +540,8 @@ fn parse_rollout(path: &Path) -> Result<CodexParse> {
} else {
event_fallback.push(mk_msg(
Role::User,
- user_kind(m),
- m.trim(),
+ user_kind(&cleaned),
+ cleaned.trim(),
ts,
));
}
@@ -610,7 +606,7 @@ fn parse_rollout(path: &Path) -> Result<CodexParse> {
// response_item 完全缺席的会话退回 event_msg 流
let has_real = messages
.iter()
- .any(|m| m.kind == MessageKind::Text && !m.text.is_empty());
+ .any(|m| m.kind == MessageKind::Text && (!m.text.is_empty() || !m.images.is_empty()));
let mut final_messages = if has_real {
messages
} else if !event_fallback.is_empty() {
@@ -633,6 +629,15 @@ fn parse_rollout(path: &Path) -> Result<CodexParse> {
})
}
+/// Codex Desktop 会把文件清单包在真实提问外面,并在正文写入指向临时文件
+/// 的 `<image ...>` 标签;图片本体由独立内容块承载。
+fn clean_user_text(text: &str) -> String {
+ let unwrapped = unwrap_file_preamble(text);
+ strip_image_tags(unwrapped.as_deref().unwrap_or(text))
+ .trim()
+ .to_string()
+}
+
fn mk_msg(role: Role, kind: MessageKind, text: &str, ts: i64) -> TranscriptMessage {
let (clipped, truncated) = clip(text, MAX_MSG_TEXT);
TranscriptMessage {
@@ -645,6 +650,7 @@ fn mk_msg(role: Role, kind: MessageKind, text: &str, ts: i64) -> TranscriptMessa
thinking: None,
timestamp: if ts > 0 { Some(ts) } else { None },
model: None,
+ images: Vec::new(),
}
}
@@ -806,7 +812,7 @@ impl AgentAdapter for CodexAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let parsed = parse_rollout(Path::new(&r.file_path))?;
+ let parsed = parse_rollout(Path::new(&r.file_path), false)?;
let meta = build_meta(r, &parsed, &self.archived_dir);
let units = units_from_messages(&parsed.messages);
Ok(ParsedSession {
@@ -817,7 +823,7 @@ impl AgentAdapter for CodexAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let parsed = parse_rollout(Path::new(&r.file_path))?;
+ let parsed = parse_rollout(Path::new(&r.file_path), true)?;
Ok(ParsedTranscript {
meta: build_meta(r, &parsed, &self.archived_dir),
mainline: parsed.messages,
diff --git a/crates/wake-core/src/adapters/cursor.rs b/crates/wake-core/src/adapters/cursor.rs
index 9c31d6a..183e7e1 100644
--- a/crates/wake-core/src/adapters/cursor.rs
+++ b/crates/wake-core/src/adapters/cursor.rs
@@ -93,15 +93,15 @@ struct CursorParse {
#[derive(Default)]
struct PendingAssistant {
- text: Vec<String>,
+ content: ParsedContent,
tool_calls: Vec<ToolCallView>,
timestamp: Option<i64>,
}
fn flush_assistant(pending: &mut Option<PendingAssistant>, messages: &mut Vec<TranscriptMessage>) {
let Some(p) = pending.take() else { return };
- let text = p.text.join("\n\n");
- if text.is_empty() && p.tool_calls.is_empty() {
+ let ParsedContent { text, images } = p.content;
+ if text.is_empty() && p.tool_calls.is_empty() && images.is_empty() {
return;
}
let (clipped, truncated) = clip(&text, MAX_MSG_TEXT);
@@ -115,10 +115,12 @@ fn flush_assistant(pending: &mut Option<PendingAssistant>, messages: &mut Vec<Tr
thinking: None,
timestamp: p.timestamp,
model: None,
+ images,
});
}
-fn parse_cursor_jsonl(path: &Path) -> Result<CursorParse> {
+fn parse_cursor_jsonl(path: &Path, decode_images: bool) -> Result<CursorParse> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let file = fs::File::open(path)?;
let reader = BufReader::with_capacity(1 << 20, file);
@@ -160,29 +162,36 @@ fn parse_cursor_jsonl(path: &Path) -> Result<CursorParse> {
match role {
"user" => {
flush_assistant(&mut pending, &mut messages);
- let mut parts: Vec<String> = Vec::new();
+ let mut parsed_message = ParsedContent::default();
let mut ts = 0i64;
for b in &blocks {
- if b.get("type").and_then(|v| v.as_str()) != Some("text") {
- continue;
- }
- let Some(raw) = b.get("text").and_then(|v| v.as_str()) else {
- continue;
- };
- if let Some(t) = extract_tag(raw, "timestamp") {
- let parsed = cursor_ts_ms(&t);
- if parsed > 0 {
- ts = parsed;
+ match b.get("type").and_then(|v| v.as_str()) {
+ Some("text") => {
+ let Some(raw) = b.get("text").and_then(|v| v.as_str()) else {
+ continue;
+ };
+ if let Some(t) = extract_tag(raw, "timestamp") {
+ let parsed = cursor_ts_ms(&t);
+ if parsed > 0 {
+ ts = parsed;
+ }
+ }
+ // 真实输入在 <user_query> 壳内;没有壳的行原样保留
+ let body =
+ extract_tag(raw, "user_query").unwrap_or_else(|| raw.to_string());
+ if !body.trim().is_empty() {
+ parsed_message.push_text(&body);
+ }
}
- }
- // 真实输入在 <user_query> 壳内;没有壳的行(注入上下文等)原样保留
- let body = extract_tag(raw, "user_query").unwrap_or_else(|| raw.to_string());
- if !body.trim().is_empty() {
- parts.push(body.trim().to_string());
+ _ if is_image_part(b) => {
+ let parsed = content_parts(b, decode_images);
+ parsed_message.append(parsed);
+ }
+ _ => {}
}
}
- let text = parts.join("\n\n");
- if text.trim().is_empty() {
+ let ParsedContent { text, images } = parsed_message;
+ if text.trim().is_empty() && images.is_empty() {
continue;
}
if ts > 0 {
@@ -191,7 +200,9 @@ fn parse_cursor_jsonl(path: &Path) -> Result<CursorParse> {
}
updated_at = updated_at.max(ts);
}
- messages.push(text_msg(Role::User, &text, ts));
+ let mut message = text_msg(Role::User, &text, ts);
+ message.images = images;
+ messages.push(message);
}
"assistant" => {
// Cursor 逐块落行且无消息 id,连续 assistant 行并成一条,turn_ended/user 处 flush
@@ -201,7 +212,7 @@ fn parse_cursor_jsonl(path: &Path) -> Result<CursorParse> {
Some("text") => {
if let Some(t) = b.get("text").and_then(|v| v.as_str()) {
if !t.trim().is_empty() {
- p.text.push(t.to_string());
+ p.content.push_text(t);
}
}
}
@@ -217,6 +228,10 @@ fn parse_cursor_jsonl(path: &Path) -> Result<CursorParse> {
false,
));
}
+ _ if is_image_part(b) => {
+ let parsed = content_parts(b, decode_images);
+ p.content.append(parsed);
+ }
_ => {}
}
}
@@ -343,7 +358,7 @@ impl AgentAdapter for CursorAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let parsed = parse_cursor_jsonl(Path::new(&r.file_path))?;
+ let parsed = parse_cursor_jsonl(Path::new(&r.file_path), false)?;
let meta = build_meta(r, &parsed);
let units = units_from_messages(&parsed.messages);
Ok(ParsedSession {
@@ -354,7 +369,7 @@ impl AgentAdapter for CursorAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let parsed = parse_cursor_jsonl(Path::new(&r.file_path))?;
+ let parsed = parse_cursor_jsonl(Path::new(&r.file_path), true)?;
// subagents/ 子目录与 Claude 同构,但无 meta 边车、主线 Task 调用无 id,
// 挂不上具体 tool_use——仅列出供导出携带
let mut sidechains = Vec::new();
@@ -389,7 +404,7 @@ impl AgentAdapter for CursorAdapter {
if !file.is_file() {
return Ok(Vec::new());
}
- Ok(parse_cursor_jsonl(&file)?.messages)
+ Ok(parse_cursor_jsonl(&file, true)?.messages)
}
fn with_custom_root(&self, dir: PathBuf) -> Box<dyn AgentAdapter> {
diff --git a/crates/wake-core/src/adapters/dsh.rs b/crates/wake-core/src/adapters/dsh.rs
index 83053cf..5029bf7 100644
--- a/crates/wake-core/src/adapters/dsh.rs
+++ b/crates/wake-core/src/adapters/dsh.rs
@@ -138,7 +138,8 @@ struct DshParse {
unknown_lines: u32,
}
-fn parse_dsh_log(path: &Path) -> Result<DshParse> {
+fn parse_dsh_log(path: &Path, decode_images: bool) -> Result<DshParse> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let reader = open_log(path, 1 << 20)?;
let mut p = DshParse {
@@ -191,12 +192,16 @@ fn parse_dsh_log(path: &Path) -> Result<DshParse> {
}
}
"user/message" => {
- let text = blocks_text(data.get("content").unwrap_or(&serde_json::Value::Null));
- if text.is_empty() {
+ let parsed = content_parts(
+ data.get("content").unwrap_or(&serde_json::Value::Null),
+ decode_images,
+ );
+ if parsed.text.is_empty() && parsed.images.is_empty() {
continue;
}
let kind = data.pointer("/source/kind").and_then(|k| k.as_str());
- let mut m = text_msg(Role::User, &text, ts);
+ let mut m = text_msg(Role::User, &parsed.text, ts);
+ m.images = parsed.images;
// dsh 的 source.kind 权威区分真人输入与注入上下文(实测一族:
// "agent-instructions"/"plugin"/"skill-catalog",后续还会长)。
// 白名单 "user":非真人一律归 Meta,缺失时当真人(宁可多显示)
@@ -210,7 +215,7 @@ fn parse_dsh_log(path: &Path) -> Result<DshParse> {
let content = msg.get("content").unwrap_or(&serde_json::Value::Null);
// 单遍分桶:text 进正文、reasoning 进 thinking、tool-call 进工具
// (blocks_text 类型盲,会把 reasoning 的 text 混进正文,不适用)
- let mut text_parts: Vec<&str> = Vec::new();
+ let mut parsed_message = ParsedContent::default();
let mut thinking_parts: Vec<&str> = Vec::new();
let mut tools: Vec<ToolCallView> = Vec::new();
for b in content.as_array().into_iter().flatten() {
@@ -221,7 +226,11 @@ fn parse_dsh_log(path: &Path) -> Result<DshParse> {
.filter(|t| !t.is_empty())
};
match b.get("type").and_then(|v| v.as_str()) {
- Some("text") => text_parts.extend(block_text()),
+ Some("text") => {
+ if let Some(text) = block_text() {
+ parsed_message.push_text(text);
+ }
+ }
Some("reasoning") => thinking_parts.extend(block_text()),
Some("tool-call") => {
let id = b.get("id").and_then(|v| v.as_str()).unwrap_or_default();
@@ -235,6 +244,10 @@ fn parse_dsh_log(path: &Path) -> Result<DshParse> {
.unwrap_or(serde_json::Value::String(raw.to_string()));
tools.push(tool_call_view(id.to_string(), name, &input, None, false));
}
+ _ if is_image_part(b) => {
+ let parsed = content_parts(b, decode_images);
+ parsed_message.append(parsed);
+ }
_ => {}
}
}
@@ -266,10 +279,13 @@ fn parse_dsh_log(path: &Path) -> Result<DshParse> {
p.tokens_used = Some(p.tokens_used.unwrap_or(0) + sum);
}
}
- if text_parts.is_empty() && tools.is_empty() && thinking_parts.is_empty() {
+ if parsed_message.text.is_empty()
+ && tools.is_empty()
+ && thinking_parts.is_empty()
+ && parsed_message.images.is_empty()
+ {
continue;
}
- let text = text_parts.join("\n\n");
// 一个 turn 多个 step,每 step 一条 assistant/message;连续 assistant
// 行(中间只隔 tool/result)合并成一条,详情页每回合一条助手消息
// (合并 + 整体 clip 机制与 pi.rs 同构,改动需两侧同步)
@@ -278,11 +294,12 @@ fn parse_dsh_log(path: &Path) -> Result<DshParse> {
}
let base = p.messages.len() - 1;
let last = &mut p.messages[base];
- if !text.is_empty() && last.text.len() < MAX_MSG_TEXT {
- if !last.text.is_empty() {
- last.text.push_str("\n\n");
+ if !parsed_message.text.is_empty() || !parsed_message.images.is_empty() {
+ if last.text.len() < MAX_MSG_TEXT {
+ append_content_to_message(last, parsed_message, "\n\n");
+ } else {
+ append_images_to_message_end(last, parsed_message.images);
}
- last.text.push_str(&text);
if last.text.len() > MAX_MSG_TEXT {
let (t, _) = clip(&last.text, MAX_MSG_TEXT);
last.text = t;
@@ -322,17 +339,21 @@ fn parse_dsh_log(path: &Path) -> Result<DshParse> {
continue;
};
if let Some(&(mi, ti)) = tool_index.get(call_id) {
- let tc = &mut p.messages[mi].tool_calls[ti];
- let text =
- blocks_text(block.get("content").unwrap_or(&serde_json::Value::Null));
- if !text.is_empty() {
- tc.output = Some(clip(&text, MAX_TOOL_IO).0);
+ let parsed = content_parts(
+ block.get("content").unwrap_or(&serde_json::Value::Null),
+ decode_images,
+ );
+ let message = &mut p.messages[mi];
+ let tc = &mut message.tool_calls[ti];
+ if !parsed.text.is_empty() {
+ tc.output = Some(clip(&parsed.text, MAX_TOOL_IO).0);
}
if block.get("isError").and_then(|v| v.as_bool()) == Some(true)
|| data.get("error").is_some_and(|e| !e.is_null())
{
tc.is_error = true;
}
+ append_images_to_message_end(message, parsed.images);
}
}
"session/title" => {
@@ -471,7 +492,7 @@ impl AgentAdapter for DshAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let parsed = parse_dsh_log(Path::new(&r.file_path))?;
+ let parsed = parse_dsh_log(Path::new(&r.file_path), false)?;
let meta = build_meta(r, &parsed);
let units = units_from_messages(&parsed.messages);
Ok(ParsedSession {
@@ -482,7 +503,7 @@ impl AgentAdapter for DshAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let parsed = parse_dsh_log(Path::new(&r.file_path))?;
+ let parsed = parse_dsh_log(Path::new(&r.file_path), true)?;
Ok(ParsedTranscript {
meta: build_meta(r, &parsed),
mainline: parsed.messages,
diff --git a/crates/wake-core/src/adapters/gemini.rs b/crates/wake-core/src/adapters/gemini.rs
index 8b56b5a..1c13fe0 100644
--- a/crates/wake-core/src/adapters/gemini.rs
+++ b/crates/wake-core/src/adapters/gemini.rs
@@ -67,7 +67,8 @@ struct GeminiParse {
unknown_lines: u32,
}
-fn parse_gemini_jsonl(path: &Path) -> Result<GeminiParse> {
+fn parse_gemini_jsonl(path: &Path, decode_images: bool) -> Result<GeminiParse> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let file = fs::File::open(path)?;
let reader = BufReader::with_capacity(1 << 20, file);
@@ -120,18 +121,8 @@ fn parse_gemini_jsonl(path: &Path) -> Result<GeminiParse> {
continue;
}
};
- let mut parts: Vec<String> = Vec::new();
- if let Some(Value::Array(blocks)) = m.get("content") {
- for b in blocks {
- if let Some(t) = b.get("text").and_then(|v| v.as_str()) {
- if !t.trim().is_empty() {
- parts.push(t.trim().to_string());
- }
- }
- }
- }
- let text = parts.join("\n\n");
- if text.is_empty() {
+ let parsed = content_parts(m.get("content").unwrap_or(&Value::Null), decode_images);
+ if parsed.text.is_empty() && parsed.images.is_empty() {
continue;
}
let ts = m
@@ -145,7 +136,9 @@ fn parse_gemini_jsonl(path: &Path) -> Result<GeminiParse> {
}
updated_at = updated_at.max(ts);
}
- messages.push(text_msg(role, &text, ts));
+ let mut message = text_msg(role, &parsed.text, ts);
+ message.images = parsed.images;
+ messages.push(message);
}
}
}
@@ -258,7 +251,7 @@ impl AgentAdapter for GeminiAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let parsed = parse_gemini_jsonl(Path::new(&r.file_path))?;
+ let parsed = parse_gemini_jsonl(Path::new(&r.file_path), false)?;
let meta = build_meta(r, &parsed, &self.cwd_for(r));
let units = units_from_messages(&parsed.messages);
Ok(ParsedSession {
@@ -269,7 +262,7 @@ impl AgentAdapter for GeminiAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let parsed = parse_gemini_jsonl(Path::new(&r.file_path))?;
+ let parsed = parse_gemini_jsonl(Path::new(&r.file_path), true)?;
Ok(ParsedTranscript {
meta: build_meta(r, &parsed, &self.cwd_for(r)),
mainline: parsed.messages,
diff --git a/crates/wake-core/src/adapters/grok.rs b/crates/wake-core/src/adapters/grok.rs
index 80ae1e2..2380da7 100644
--- a/crates/wake-core/src/adapters/grok.rs
+++ b/crates/wake-core/src/adapters/grok.rs
@@ -138,6 +138,7 @@ struct Replay {
cur_text: String,
cur_thinking: String,
cur_tools: Vec<ToolCallView>,
+ cur_images: Vec<ImageAttachment>,
cur_ts: i64,
}
@@ -150,6 +151,7 @@ impl Replay {
cur_text: String::new(),
cur_thinking: String::new(),
cur_tools: Vec::new(),
+ cur_images: Vec::new(),
cur_ts: 0,
}
}
@@ -161,13 +163,19 @@ impl Replay {
let text = std::mem::take(&mut self.cur_text);
let thinking = std::mem::take(&mut self.cur_thinking);
let tools = std::mem::take(&mut self.cur_tools);
- if text.trim().is_empty() && thinking.trim().is_empty() && tools.is_empty() {
+ let images = std::mem::take(&mut self.cur_images);
+ if text.trim().is_empty()
+ && thinking.trim().is_empty()
+ && tools.is_empty()
+ && images.is_empty()
+ {
return;
}
let mut m = text_msg(role, &text, self.cur_ts);
if !thinking.trim().is_empty() {
m.thinking = Some(clip(thinking.trim(), MAX_TOOL_IO).0);
}
+ m.images = images;
let base = self.messages.len();
for (ix, tc) in tools.into_iter().enumerate() {
self.tool_index.insert(tc.id.clone(), (base, ix));
@@ -194,29 +202,39 @@ fn chunk_text(update: &Value) -> &str {
.unwrap_or_default()
}
-fn tool_content_text(content: &Value) -> String {
- let mut parts: Vec<String> = Vec::new();
- for item in content.as_array().into_iter().flatten() {
- let inner = item.get("content").unwrap_or(item);
- let t = inner
- .get("text")
- .and_then(|x| x.as_str())
- .unwrap_or_default();
- if !t.trim().is_empty() {
- parts.push(t.trim().to_string());
- }
+/// Grok 的文本 chunk 是增量片段,不能经 `content_parts` 的 trim/join,
+/// 否则模型在 token 边界写入的空格会被吃掉。只有图片块走通用附件解析。
+fn append_message_chunk(rp: &mut Replay, update: &Value, role: Role, ts: i64, decode_images: bool) {
+ rp.ensure(role, ts);
+ let content = update.get("content").unwrap_or(&Value::Null);
+ if has_image_content(content) {
+ let parsed = content_parts(content, decode_images);
+ let mut combined = ParsedContent {
+ text: std::mem::take(&mut rp.cur_text),
+ images: std::mem::take(&mut rp.cur_images),
+ };
+ combined.append_with_separator(parsed, "");
+ rp.cur_text = combined.text;
+ rp.cur_images = combined.images;
+ } else {
+ rp.cur_text.push_str(chunk_text(update));
}
- parts.join("\n")
}
/// tool_call_update 回填:完成态带 content(文本输出,覆盖式取最终态),
/// status failed 置错;初始 tool_call 缺 rawInput 时由 update 补
-fn apply_tool_update(tc: &mut ToolCallView, update: &Value) {
+fn apply_tool_update(
+ tc: &mut ToolCallView,
+ update: &Value,
+ decode_images: bool,
+) -> Vec<ImageAttachment> {
+ let mut images = Vec::new();
if let Some(content) = update.get("content") {
- let t = tool_content_text(content);
- if !t.is_empty() {
- tc.output = Some(clip(&t, MAX_TOOL_IO).0);
+ let parsed = content_parts(content, decode_images);
+ if !parsed.text.is_empty() {
+ tc.output = Some(clip(&parsed.text, MAX_TOOL_IO).0);
}
+ images = parsed.images;
}
if update.get("status").and_then(|v| v.as_str()) == Some("failed") {
tc.is_error = true;
@@ -229,9 +247,11 @@ fn apply_tool_update(tc: &mut ToolCallView, update: &Value) {
}
}
}
+ images
}
-fn parse_grok_updates(path: &Path) -> Result<(Vec<TranscriptMessage>, u32)> {
+fn parse_grok_updates(path: &Path, decode_images: bool) -> Result<(Vec<TranscriptMessage>, u32)> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let file = fs::File::open(path)?;
let reader = BufReader::with_capacity(1 << 20, file);
let mut rp = Replay::new();
@@ -256,12 +276,10 @@ fn parse_grok_updates(path: &Path) -> Result<(Vec<TranscriptMessage>, u32)> {
};
match update.get("sessionUpdate").and_then(|v| v.as_str()) {
Some("user_message_chunk") => {
- rp.ensure(Role::User, ts);
- rp.cur_text.push_str(chunk_text(update));
+ append_message_chunk(&mut rp, update, Role::User, ts, decode_images);
}
Some("agent_message_chunk") => {
- rp.ensure(Role::Assistant, ts);
- rp.cur_text.push_str(chunk_text(update));
+ append_message_chunk(&mut rp, update, Role::Assistant, ts, decode_images);
}
Some("agent_thought_chunk") => {
rp.ensure(Role::Assistant, ts);
@@ -286,10 +304,19 @@ fn parse_grok_updates(path: &Path) -> Result<(Vec<TranscriptMessage>, u32)> {
continue;
};
// 目标 tool 可能还在当前未 flush 的段里,也可能已入 messages
- if let Some(tc) = rp.cur_tools.iter_mut().find(|tc| tc.id == id) {
- apply_tool_update(tc, update);
+ if let Some(tool_ix) = rp.cur_tools.iter().position(|tc| tc.id == id) {
+ let images =
+ apply_tool_update(&mut rp.cur_tools[tool_ix], update, decode_images);
+ let mut images = images;
+ for image in &mut images {
+ image.text_offset = rp.cur_text.len();
+ }
+ rp.cur_images.extend(images);
} else if let Some(&(mi, ti)) = rp.tool_index.get(id) {
- apply_tool_update(&mut rp.messages[mi].tool_calls[ti], update);
+ let message = &mut rp.messages[mi];
+ let images =
+ apply_tool_update(&mut message.tool_calls[ti], update, decode_images);
+ append_images_to_message_end(message, images);
}
}
// 已知的非内容更新:任务后台化/压缩等
@@ -428,7 +455,7 @@ impl AgentAdapter for GrokAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let (messages, unknown) = parse_grok_updates(Path::new(&r.file_path))?;
+ let (messages, unknown) = parse_grok_updates(Path::new(&r.file_path), false)?;
let side = read_summary(Path::new(&r.file_path));
let meta = build_meta(r, &side, &messages, &self.grok_home, &self.group_ctx());
let units = units_from_messages(&messages);
@@ -440,7 +467,7 @@ impl AgentAdapter for GrokAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let (messages, unknown) = parse_grok_updates(Path::new(&r.file_path))?;
+ let (messages, unknown) = parse_grok_updates(Path::new(&r.file_path), true)?;
let side = read_summary(Path::new(&r.file_path));
Ok(ParsedTranscript {
meta: build_meta(r, &side, &messages, &self.grok_home, &self.group_ctx()),
diff --git a/crates/wake-core/src/adapters/kimi.rs b/crates/wake-core/src/adapters/kimi.rs
index 6fa0acd..d86a65c 100644
--- a/crates/wake-core/src/adapters/kimi.rs
+++ b/crates/wake-core/src/adapters/kimi.rs
@@ -107,37 +107,8 @@ fn read_state(wire_path: &Path) -> KimiState {
s
}
-/// content parts(turn.prompt 的 input / message.content)→ 纯文本。
-/// part 可以是裸字符串或 {type:text,text};媒体 part(blobref)跳过。
-fn parts_text(v: &Value) -> String {
- let mut parts: Vec<String> = Vec::new();
- match v {
- Value::String(s) => {
- if !s.trim().is_empty() {
- parts.push(s.trim().to_string());
- }
- }
- Value::Array(arr) => {
- for p in arr {
- match p {
- Value::String(s) if !s.trim().is_empty() => parts.push(s.trim().to_string()),
- Value::Object(_) => {
- if let Some(t) = p.get("text").and_then(|x| x.as_str()) {
- if !t.trim().is_empty() {
- parts.push(t.trim().to_string());
- }
- }
- }
- _ => {}
- }
- }
- }
- _ => {}
- }
- parts.join("\n\n")
-}
-
-fn parse_kimi_wire(path: &Path) -> Result<(Vec<TranscriptMessage>, u32)> {
+fn parse_kimi_wire(path: &Path, decode_images: bool) -> Result<(Vec<TranscriptMessage>, u32)> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let file = fs::File::open(path)?;
let reader = BufReader::with_capacity(1 << 20, file);
let mut messages: Vec<TranscriptMessage> = Vec::new();
@@ -157,9 +128,11 @@ fn parse_kimi_wire(path: &Path) -> Result<(Vec<TranscriptMessage>, u32)> {
};
match row.get("type").and_then(|v| v.as_str()) {
Some("turn.prompt") | Some("turn.steer") => {
- let text = parts_text(row.get("input").unwrap_or(&Value::Null));
- if !text.is_empty() {
- messages.push(text_msg(Role::User, &text, 0));
+ let parsed = content_parts(row.get("input").unwrap_or(&Value::Null), decode_images);
+ if !parsed.text.is_empty() || !parsed.images.is_empty() {
+ let mut message = text_msg(Role::User, &parsed.text, 0);
+ message.images = parsed.images;
+ messages.push(message);
}
}
Some("context.append_message") => {
@@ -172,9 +145,12 @@ fn parse_kimi_wire(path: &Path) -> Result<(Vec<TranscriptMessage>, u32)> {
// user 输入已由 turn.prompt 覆盖;tool/system 等上下文行跳过
_ => continue,
};
- let text = parts_text(msg.get("content").unwrap_or(&Value::Null));
- if !text.is_empty() {
- messages.push(text_msg(role, &text, 0));
+ let parsed =
+ content_parts(msg.get("content").unwrap_or(&Value::Null), decode_images);
+ if !parsed.text.is_empty() || !parsed.images.is_empty() {
+ let mut message = text_msg(role, &parsed.text, 0);
+ message.images = parsed.images;
+ messages.push(message);
}
}
// 已知的配置/生命周期/工具事件行(工具明细在 loop_event 里,v1 不展开)
@@ -291,7 +267,7 @@ impl AgentAdapter for KimiAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let (messages, unknown) = parse_kimi_wire(Path::new(&r.file_path))?;
+ let (messages, unknown) = parse_kimi_wire(Path::new(&r.file_path), false)?;
let state = read_state(Path::new(&r.file_path));
let meta = build_meta(r, &state, &self.cwd_for(&r.native_id), &messages);
let units = units_from_messages(&messages);
@@ -303,7 +279,7 @@ impl AgentAdapter for KimiAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let (messages, unknown) = parse_kimi_wire(Path::new(&r.file_path))?;
+ let (messages, unknown) = parse_kimi_wire(Path::new(&r.file_path), true)?;
let state = read_state(Path::new(&r.file_path));
Ok(ParsedTranscript {
meta: build_meta(r, &state, &self.cwd_for(&r.native_id), &messages),
diff --git a/crates/wake-core/src/adapters/kiro.rs b/crates/wake-core/src/adapters/kiro.rs
index d015864..07d7780 100644
--- a/crates/wake-core/src/adapters/kiro.rs
+++ b/crates/wake-core/src/adapters/kiro.rs
@@ -68,7 +68,8 @@ fn read_sidecar(jsonl_path: &Path) -> Sidecar {
side
}
-fn parse_kiro_jsonl(path: &Path) -> Result<(Vec<TranscriptMessage>, u32)> {
+fn parse_kiro_jsonl(path: &Path, decode_images: bool) -> Result<(Vec<TranscriptMessage>, u32)> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let file = fs::File::open(path)?;
let reader = BufReader::with_capacity(1 << 20, file);
let mut messages: Vec<TranscriptMessage> = Vec::new();
@@ -98,20 +99,8 @@ fn parse_kiro_jsonl(path: &Path) -> Result<(Vec<TranscriptMessage>, u32)> {
unknown += 1;
continue;
};
- let mut parts: Vec<String> = Vec::new();
- if let Some(Value::Array(blocks)) = data.get("content") {
- for b in blocks {
- if b.get("kind").and_then(|v| v.as_str()) == Some("text") {
- if let Some(t) = b.get("data").and_then(|v| v.as_str()) {
- if !t.trim().is_empty() {
- parts.push(t.trim().to_string());
- }
- }
- }
- }
- }
- let text = parts.join("\n\n");
- if text.is_empty() {
+ let parsed = content_parts(data.get("content").unwrap_or(&Value::Null), decode_images);
+ if parsed.text.is_empty() && parsed.images.is_empty() {
continue;
}
let ts_sec = data
@@ -119,7 +108,9 @@ fn parse_kiro_jsonl(path: &Path) -> Result<(Vec<TranscriptMessage>, u32)> {
.and_then(|m| m.get("timestamp"))
.and_then(|v| v.as_i64())
.unwrap_or(0);
- messages.push(text_msg(role, &text, ts_sec * 1000));
+ let mut message = text_msg(role, &parsed.text, ts_sec * 1000);
+ message.images = parsed.images;
+ messages.push(message);
}
assign_seq(&mut messages);
Ok((messages, unknown))
@@ -177,7 +168,7 @@ impl AgentAdapter for KiroAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let (messages, unknown) = parse_kiro_jsonl(Path::new(&r.file_path))?;
+ let (messages, unknown) = parse_kiro_jsonl(Path::new(&r.file_path), false)?;
let side = read_sidecar(Path::new(&r.file_path));
let meta = build_meta(r, &side, &messages);
let units = units_from_messages(&messages);
@@ -189,7 +180,7 @@ impl AgentAdapter for KiroAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let (messages, unknown) = parse_kiro_jsonl(Path::new(&r.file_path))?;
+ let (messages, unknown) = parse_kiro_jsonl(Path::new(&r.file_path), true)?;
let side = read_sidecar(Path::new(&r.file_path));
Ok(ParsedTranscript {
meta: build_meta(r, &side, &messages),
diff --git a/crates/wake-core/src/adapters/opencode.rs b/crates/wake-core/src/adapters/opencode.rs
index 5fea83f..dd793d7 100644
--- a/crates/wake-core/src/adapters/opencode.rs
+++ b/crates/wake-core/src/adapters/opencode.rs
@@ -284,7 +284,12 @@ impl OpencodeAdapter {
/// 单会话解析:一次连接;v2 行命中走 session_message,否则回落 v1 的
/// message + part 两表路径
- fn parse(&self, r: &SessionFileRef) -> Result<(SessionMeta, Vec<TranscriptMessage>, u32)> {
+ fn parse(
+ &self,
+ r: &SessionFileRef,
+ decode_images: bool,
+ ) -> Result<(SessionMeta, Vec<TranscriptMessage>, u32)> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let db = self
.db_for_ref(r)
.ok_or_else(|| anyhow!("opencode database is outside adapter roots"))?;
@@ -294,8 +299,8 @@ impl OpencodeAdapter {
.next()
.ok_or_else(|| anyhow!("opencode session {} not in db", r.native_id))?;
let (messages, unknown) = match row.v2_messages {
- true => parse_v2_messages(&ro, &r.native_id)?,
- false => parse_v1_messages(&ro, &r.native_id)?,
+ true => parse_v2_messages(&ro, &r.native_id, decode_images)?,
+ false => parse_v1_messages(&ro, &r.native_id, decode_images)?,
};
let count = messages
.iter()
@@ -307,7 +312,11 @@ impl OpencodeAdapter {
}
/// v1 正文:message 表(角色/时间)+ part 表(内容块)按 message_id 分组
-fn parse_v1_messages(ro: &SqliteRo, sid: &str) -> Result<(Vec<TranscriptMessage>, u32)> {
+fn parse_v1_messages(
+ ro: &SqliteRo,
+ sid: &str,
+ decode_images: bool,
+) -> Result<(Vec<TranscriptMessage>, u32)> {
// part 按 (message_id, id) 排序分组;id 前缀时间有序
let mut parts_by_msg: HashMap<String, Vec<Value>> = HashMap::new();
{
@@ -358,13 +367,15 @@ fn parse_v1_messages(ro: &SqliteRo, sid: &str) -> Result<(Vec<TranscriptMessage>
if p.get("synthetic").and_then(|v| v.as_bool()) == Some(true) {
acc.synthetic.push(t.trim().to_string());
} else {
- acc.text.push(t.trim().to_string());
+ acc.content.push_text(t);
}
}
Some("reasoning") => acc.push_reasoning(&p),
- Some("tool") => acc.push_tool(&p),
- Some("step-start") | Some("step-finish") | Some("snapshot") | Some("patch")
- | Some("file") => {}
+ Some("tool") => acc.push_tool(&p, decode_images),
+ Some("image") => acc.push_image(&p, decode_images),
+ Some("step-start") | Some("step-finish") | Some("snapshot") | Some("patch") => {}
+ Some("file") if is_image_part(&p) => acc.push_image(&p, decode_images),
+ Some("file") => {}
_ => unknown += 1,
}
}
@@ -378,7 +389,11 @@ fn parse_v1_messages(ro: &SqliteRo, sid: &str) -> Result<(Vec<TranscriptMessage>
/// v2 正文:session_message 单表按 seq 有序,type 列分 user/synthetic/assistant,
/// data JSON——user 的 text 在顶层,assistant 的 content 是块数组
-fn parse_v2_messages(ro: &SqliteRo, sid: &str) -> Result<(Vec<TranscriptMessage>, u32)> {
+fn parse_v2_messages(
+ ro: &SqliteRo,
+ sid: &str,
+ decode_images: bool,
+) -> Result<(Vec<TranscriptMessage>, u32)> {
let mut messages: Vec<TranscriptMessage> = Vec::new();
let mut unknown = 0u32;
let mut stmt = ro
@@ -400,8 +415,24 @@ fn parse_v2_messages(ro: &SqliteRo, sid: &str) -> Result<(Vec<TranscriptMessage>
match mtype.as_str() {
"user" => {
let text = md.get("text").and_then(|v| v.as_str()).unwrap_or("").trim();
- if !text.is_empty() {
- messages.push(text_msg(Role::User, text, ts));
+ let mut parsed = content_parts(
+ md.get("content")
+ .or_else(|| md.get("attachments"))
+ .unwrap_or(&Value::Null),
+ decode_images,
+ );
+ if text == parsed.text.trim() {
+ parsed.text = text.to_string();
+ } else if !text.is_empty() {
+ let mut combined = ParsedContent::default();
+ combined.push_text(text);
+ combined.append(parsed);
+ parsed = combined;
+ }
+ if !parsed.text.is_empty() || !parsed.images.is_empty() {
+ let mut message = text_msg(Role::User, &parsed.text, ts);
+ message.images = parsed.images;
+ messages.push(message);
}
}
"synthetic" => {
@@ -460,14 +491,17 @@ fn parse_v2_messages(ro: &SqliteRo, sid: &str) -> Result<(Vec<TranscriptMessage>
Some("text") => {
if let Some(t) = b.get("text").and_then(|v| v.as_str()) {
if !t.trim().is_empty() {
- acc.text.push(t.trim().to_string());
+ acc.content.push_text(t);
}
}
}
Some("reasoning") => acc.push_reasoning(b),
- Some("tool") => acc.push_tool(b),
+ Some("tool") => acc.push_tool(b, decode_images),
+ Some("image") => acc.push_image(b, decode_images),
Some("step-start") | Some("step-finish") | Some("snapshot")
- | Some("patch") | Some("file") => {}
+ | Some("patch") => {}
+ Some("file") if is_image_part(b) => acc.push_image(b, decode_images),
+ Some("file") => {}
_ => unknown += 1,
}
}
@@ -499,7 +533,7 @@ fn parse_v2_messages(ro: &SqliteRo, sid: &str) -> Result<(Vec<TranscriptMessage>
/// 一条消息的内容块累加器(text/synthetic/reasoning/tool),两代路径共用
#[derive(Default)]
struct BlockAcc {
- text: Vec<String>,
+ content: ParsedContent,
synthetic: Vec<String>,
thinking: Vec<String>,
tools: Vec<ToolCallView>,
@@ -517,10 +551,21 @@ impl BlockAcc {
/// tool 块兼容两种形态:
/// preview 早期 `{callID,tool,state:{input,output}}` 与真实 next
/// `{id,name,state:{input,content,result,error}}`。
- fn push_tool(&mut self, b: &Value) {
+ fn push_tool(&mut self, b: &Value, decode_images: bool) {
let state = b.get("state").cloned().unwrap_or(Value::Null);
let input = state.get("input").cloned().unwrap_or(Value::Null);
- let output = opencode_tool_output(&state);
+ let mut output = opencode_tool_output(&state);
+ if let Some(content) = state.get("content") {
+ let parsed = content_parts(content, decode_images);
+ if !parsed.text.is_empty() {
+ output = Some(parsed.text);
+ }
+ let mut images = parsed.images;
+ for image in &mut images {
+ image.text_offset = self.content.text.len();
+ }
+ self.content.images.extend(images);
+ }
self.tools.push(tool_call_view(
b.get("callID")
.or_else(|| b.get("id"))
@@ -537,14 +582,21 @@ impl BlockAcc {
));
}
+ fn push_image(&mut self, block: &Value, decode_images: bool) {
+ let parsed = content_parts(block, decode_images);
+ self.content.append(parsed);
+ }
+
/// 组装为消息;全空返回 None。只有注入内容的消息归 Meta 折叠。
fn into_message(self, role: Role, ts: i64, model: Option<String>) -> Option<TranscriptMessage> {
- let (text, kind) = if self.text.is_empty() && !self.synthetic.is_empty() {
+ let ParsedContent { text, images } = self.content;
+ let (text, kind) = if text.is_empty() && !self.synthetic.is_empty() {
(self.synthetic.join("\n\n"), MessageKind::Meta)
} else {
- (self.text.join("\n\n"), MessageKind::Text)
+ (text, MessageKind::Text)
};
- if text.is_empty() && self.thinking.is_empty() && self.tools.is_empty() {
+ if text.is_empty() && self.thinking.is_empty() && self.tools.is_empty() && images.is_empty()
+ {
return None;
}
let (clipped, truncated) = clip(&text, MAX_MSG_TEXT);
@@ -562,6 +614,7 @@ impl BlockAcc {
},
timestamp: if ts > 0 { Some(ts) } else { None },
model,
+ images,
})
}
}
@@ -680,7 +733,7 @@ impl AgentAdapter for OpencodeAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let (meta, messages, unknown) = self.parse(r)?;
+ let (meta, messages, unknown) = self.parse(r, false)?;
let units = units_from_messages(&messages);
Ok(ParsedSession {
meta,
@@ -690,7 +743,7 @@ impl AgentAdapter for OpencodeAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let (meta, messages, unknown) = self.parse(r)?;
+ let (meta, messages, unknown) = self.parse(r, true)?;
Ok(ParsedTranscript {
meta,
mainline: messages,
diff --git a/crates/wake-core/src/adapters/parse_utils.rs b/crates/wake-core/src/adapters/parse_utils.rs
index b5d8799..1e7a6e5 100644
--- a/crates/wake-core/src/adapters/parse_utils.rs
+++ b/crates/wake-core/src/adapters/parse_utils.rs
@@ -1,5 +1,54 @@
use crate::models::*;
+use base64::engine::general_purpose::{STANDARD as BASE64, URL_SAFE as BASE64_URL_SAFE};
+use base64::Engine as _;
use serde_json::Value;
+use std::cell::Cell;
+
+const MAX_TRANSCRIPT_IMAGE_BYTES: usize = 64 * 1024 * 1024;
+
+thread_local! {
+ static IMAGE_DECODE_REMAINING: Cell<Option<usize>> = const { Cell::new(None) };
+}
+
+/// 一次会话解析的图片压缩字节总预算。嵌套调用复用外层预算,离开解析作用域后恢复。
+pub struct ImageDecodeBudgetGuard {
+ previous: Option<usize>,
+ changed: bool,
+}
+
+pub fn transcript_image_decode_budget(decode_images: bool) -> ImageDecodeBudgetGuard {
+ image_decode_budget(decode_images.then_some(MAX_TRANSCRIPT_IMAGE_BYTES))
+}
+
+fn image_decode_budget(limit: Option<usize>) -> ImageDecodeBudgetGuard {
+ IMAGE_DECODE_REMAINING.with(|remaining| {
+ let previous = remaining.get();
+ let changed = previous.is_none() && limit.is_some();
+ if changed {
+ remaining.set(limit);
+ }
+ ImageDecodeBudgetGuard { previous, changed }
+ })
+}
+
+impl Drop for ImageDecodeBudgetGuard {
+ fn drop(&mut self) {
+ if self.changed {
+ IMAGE_DECODE_REMAINING.with(|remaining| remaining.set(self.previous));
+ }
+ }
+}
+
+fn consume_image_decode_budget(bytes: usize) -> bool {
+ IMAGE_DECODE_REMAINING.with(|remaining| match remaining.get() {
+ Some(left) if bytes <= left => {
+ remaining.set(Some(left - bytes));
+ true
+ }
+ Some(_) => false,
+ None => true,
+ })
+}
/// 文件 mtime → epoch ms(各家 adapter 与 watcher 共用)
pub fn mtime_ms(meta: &std::fs::Metadata) -> i64 {
@@ -48,6 +97,7 @@ pub fn text_msg(role: Role, text: &str, ts: i64) -> TranscriptMessage {
thinking: None,
timestamp: if ts > 0 { Some(ts) } else { None },
model: None,
+ images: Vec::new(),
}
}
@@ -63,9 +113,9 @@ pub fn assign_seq(messages: &mut [TranscriptMessage]) {
pub fn title_from_messages(messages: &[TranscriptMessage]) -> Option<String> {
messages
.iter()
- .find(|m| m.role == Role::User && m.kind == MessageKind::Text)
+ .filter(|m| m.role == Role::User && m.kind == MessageKind::Text)
.map(|m| clean_title_candidate(&m.text))
- .filter(|t| !t.is_empty())
+ .find(|title| !title.is_empty())
}
/// `AgentAdapter::file_ref` 的默认实现:非空 .jsonl,stem 即 native_id。
@@ -123,16 +173,426 @@ pub fn list_jsonl_refs(
refs
}
-/// content blocks 数组 → 纯文本(取各块 "text" 字段,trim 后 join)
-pub fn blocks_text(v: &Value) -> String {
- v.as_array()
- .into_iter()
- .flatten()
- .filter_map(|b| b.get("text").and_then(|t| t.as_str()))
- .map(str::trim)
- .filter(|t| !t.is_empty())
- .collect::<Vec<_>>()
- .join("\n\n")
+/// content blocks → 文本和图片。支持各家目前出现的 Anthropic、OpenAI、
+/// Gemini、Pi/Kiro 与 OpenCode 形状;未知、远程、本地路径或过大的图片退回占位符。
+#[derive(Debug, Default)]
+pub struct ParsedContent {
+ pub text: String,
+ pub images: Vec<ImageAttachment>,
+}
+
+impl ParsedContent {
+ /// 追加一个正文块,并保持已经记录的图片偏移不变。
+ pub fn push_text(&mut self, value: &str) {
+ self.push_text_with_separator(value, "\n\n");
+ }
+
+ pub fn push_text_with_separator(&mut self, value: &str, separator: &str) {
+ let value = value.trim();
+ if value.is_empty() {
+ return;
+ }
+ if !self.text.is_empty() {
+ self.text.push_str(separator);
+ }
+ self.text.push_str(value);
+ }
+
+ /// 追加另一段已解析内容;图片偏移随正文基址一起平移。
+ pub fn append(&mut self, other: Self) {
+ self.append_with_separator(other, "\n\n");
+ }
+
+ pub fn append_with_separator(&mut self, mut other: Self, separator: &str) {
+ let had_text = !self.text.is_empty();
+ let has_other_text = !other.text.is_empty();
+ let base = self.text.len();
+ let separator_len = if had_text && has_other_text {
+ separator.len()
+ } else {
+ 0
+ };
+ for image in &mut other.images {
+ image.text_offset += base
+ + if image.text_offset > 0 {
+ separator_len
+ } else {
+ 0
+ };
+ }
+ if has_other_text {
+ if separator_len > 0 {
+ self.text.push_str(separator);
+ }
+ self.text.push_str(&other.text);
+ }
+ self.images.extend(other.images);
+ }
+
+ fn push_image(&mut self, mut image: ImageAttachment) {
+ image.text_offset = self.text.len();
+ self.images.push(image);
+ }
+}
+
+/// 把新内容并入已存在的消息,并同步平移图片的正文偏移。
+pub fn append_content_to_message(
+ message: &mut TranscriptMessage,
+ content: ParsedContent,
+ separator: &str,
+) {
+ let mut combined = ParsedContent {
+ text: std::mem::take(&mut message.text),
+ images: std::mem::take(&mut message.images),
+ };
+ combined.append_with_separator(content, separator);
+ message.text = combined.text;
+ message.images = combined.images;
+}
+
+/// 工具结果不属于消息正文流;其图片统一放到现有正文末尾。
+pub fn append_images_to_message_end(
+ message: &mut TranscriptMessage,
+ mut images: Vec<ImageAttachment>,
+) {
+ for image in &mut images {
+ image.text_offset = message.text.len();
+ }
+ message.images.extend(images);
+}
+
+pub fn content_parts(v: &Value, decode_images: bool) -> ParsedContent {
+ let mut parsed = ParsedContent::default();
+ collect_content(v, decode_images, 0, &mut parsed);
+ parsed
+}
+
+fn collect_content(v: &Value, decode_images: bool, depth: usize, parsed: &mut ParsedContent) {
+ if depth > 4 {
+ return;
+ }
+ match v {
+ Value::String(value) => {
+ parsed.push_text(value);
+ }
+ Value::Array(parts) => {
+ for part in parts {
+ collect_content(part, decode_images, depth + 1, parsed);
+ }
+ }
+ Value::Object(_) => {
+ if is_image_part(v) {
+ if decode_images {
+ if let Some(image) = decode_image_part(v, depth) {
+ parsed.push_image(image);
+ return;
+ }
+ }
+ parsed.push_text(IMAGE_PLACEHOLDER);
+ return;
+ }
+
+ let kind = part_kind(v);
+ if matches!(kind.as_deref(), Some("text" | "input_text" | "output_text")) {
+ if let Some(value) = v
+ .get("text")
+ .or_else(|| v.get("data"))
+ .and_then(Value::as_str)
+ {
+ parsed.push_text(value);
+ }
+ return;
+ }
+
+ // Gemini 的普通 part 没有 type;Grok 的 content wrapper 则需要
+ // 再下钻一层。reasoning/tool 块由各 adapter 自己分桶,不在这里混入正文。
+ if kind.is_none() {
+ if let Some(value) = v.get("text").and_then(Value::as_str) {
+ parsed.push_text(value);
+ return;
+ }
+ if let Some(inner) = v.get("content") {
+ collect_content(inner, decode_images, depth + 1, parsed);
+ return;
+ }
+ }
+ if matches!(kind.as_deref(), Some("content" | "part")) {
+ if let Some(inner) = v.get("content").or_else(|| v.get("data")) {
+ collect_content(inner, decode_images, depth + 1, parsed);
+ }
+ }
+ }
+ _ => {}
+ }
+}
+
+/// 工具结果与普通消息块的区别是:未知对象在旧解析器里会以 JSON 原文显示。
+/// 图片仍走统一解码,无法识别的非图片对象则保留这个兼容行为。
+pub fn tool_result_parts(v: &Value, decode_images: bool) -> ParsedContent {
+ let values = v
+ .as_array()
+ .map(Vec::as_slice)
+ .unwrap_or_else(|| std::slice::from_ref(v));
+ let mut result = ParsedContent::default();
+ for value in values {
+ let parsed = content_parts(value, decode_images);
+ if !parsed.text.is_empty() {
+ result.append_with_separator(parsed, "\n");
+ } else if parsed.images.is_empty() && !value.is_null() {
+ if let Ok(json) = serde_json::to_string(value) {
+ result.push_text_with_separator(&json, "\n");
+ }
+ } else {
+ result.append_with_separator(parsed, "\n");
+ }
+ }
+ result
+}
+
+fn part_kind(v: &Value) -> Option<String> {
+ v.get("type")
+ .or_else(|| v.get("kind"))
+ .and_then(Value::as_str)
+ .map(|value| value.to_ascii_lowercase().replace('-', "_"))
+}
+
+fn image_media_type(v: &Value) -> Option<String> {
+ ["media_type", "mediaType", "mime_type", "mimeType", "mime"]
+ .iter()
+ .find_map(|key| v.get(*key).and_then(Value::as_str))
+ .filter(|value| value.starts_with("image/"))
+ .map(|value| {
+ value
+ .split(';')
+ .next()
+ .unwrap_or(value)
+ .to_ascii_lowercase()
+ })
+}
+
+pub fn is_image_part(v: &Value) -> bool {
+ part_kind(v).is_some_and(|kind| kind.contains("image"))
+ || image_media_type(v).is_some()
+ || v.get("inlineData").is_some()
+ || v.get("inline_data").is_some()
+ || v.get("image_url").is_some()
+ || v.get("imageUrl").is_some()
+ || v.get("source")
+ .is_some_and(|source| image_media_type(source).is_some())
+}
+
+pub fn has_image_content(v: &Value) -> bool {
+ if is_image_part(v) {
+ return true;
+ }
+ match v {
+ Value::Array(values) => values.iter().any(has_image_content),
+ Value::Object(_) => v
+ .get("content")
+ .or_else(|| v.get("data"))
+ .is_some_and(has_image_content),
+ _ => false,
+ }
+}
+
+fn decode_image_part(v: &Value, depth: usize) -> Option<ImageAttachment> {
+ if depth > 5 {
+ return None;
+ }
+ for key in ["source", "inlineData", "inline_data"] {
+ if let Some(inner) = v.get(key) {
+ if let Some(image) = decode_image_part(inner, depth + 1) {
+ return Some(image);
+ }
+ }
+ }
+
+ let media_type = image_media_type(v);
+ for key in [
+ "image_url",
+ "imageUrl",
+ "url",
+ "uri",
+ "fileUri",
+ "file_uri",
+ "path",
+ ] {
+ if let Some(source) = v.get(key) {
+ if let Some(value) = source.as_str() {
+ if let Some(image) = decode_image_source(value, media_type.as_deref()) {
+ return Some(image);
+ }
+ } else if let Some(image) = decode_image_part(source, depth + 1) {
+ return Some(image);
+ }
+ }
+ }
+
+ if let Some(data) = v.get("data") {
+ match data {
+ Value::String(value) => {
+ if value.starts_with("data:") {
+ return decode_data_uri(value);
+ }
+ if media_type.as_deref() == Some("image/svg+xml") && value.contains("<svg") {
+ return attachment_from_bytes(value.as_bytes().to_vec(), media_type.as_deref());
+ }
+ return decode_base64_image(value, media_type.as_deref());
+ }
+ Value::Array(values) if values.len() <= MAX_IMAGE_BYTES as usize => {
+ let bytes = values
+ .iter()
+ .map(Value::as_u64)
+ .collect::<Option<Vec<_>>>()?
+ .into_iter()
+ .map(u8::try_from)
+ .collect::<Result<Vec<_>, _>>()
+ .ok()?;
+ return attachment_from_bytes(bytes, media_type.as_deref());
+ }
+ Value::Object(_) => return decode_image_part(data, depth + 1),
+ _ => {}
+ }
+ }
+ None
+}
+
+fn decode_image_source(value: &str, _media_type: Option<&str>) -> Option<ImageAttachment> {
+ if value.starts_with("data:") {
+ return decode_data_uri(value);
+ }
+ // 会话文件是不可信输入,不能允许其中的 path/file:// 指向任意本地文件。
+ // 各适配器只解析会话自身携带的 data URI/base64/byte-array 图片。
+ None
+}
+
+fn decode_base64_image(data: &str, media_type: Option<&str>) -> Option<ImageAttachment> {
+ if data.len() > MAX_IMAGE_B64 {
+ return None;
+ }
+ let compact;
+ let data = if data.bytes().any(|byte| byte.is_ascii_whitespace()) {
+ compact = data
+ .bytes()
+ .filter(|byte| !byte.is_ascii_whitespace())
+ .collect::<Vec<_>>();
+ compact.as_slice()
+ } else {
+ data.as_bytes()
+ };
+ let bytes = BASE64
+ .decode(data)
+ .or_else(|_| BASE64_URL_SAFE.decode(data))
+ .ok()?;
+ attachment_from_bytes(bytes, media_type)
+}
+
+/// `data:image/png;base64,...` → 图片附件。只接受 image MIME 与 base64,
+/// 不下载 http(s) URL。
+pub fn decode_data_uri(uri: &str) -> Option<ImageAttachment> {
+ let rest = uri.strip_prefix("data:")?;
+ let (metadata, data) = rest.split_once(',')?;
+ let mut fields = metadata.split(';');
+ let media_type = fields.next()?;
+ if !media_type.starts_with("image/") || !fields.any(|field| field == "base64") {
+ return None;
+ }
+ decode_base64_image(data, Some(media_type))
+}
+
+fn attachment_from_bytes(bytes: Vec<u8>, media_type: Option<&str>) -> Option<ImageAttachment> {
+ if bytes.is_empty() || bytes.len() as u64 > MAX_IMAGE_BYTES {
+ return None;
+ }
+ let media_type = media_type
+ .filter(|value| value.starts_with("image/"))
+ .map(str::to_string)
+ .or_else(|| sniff_image_media_type(&bytes).map(str::to_string))?;
+ if !consume_image_decode_budget(bytes.len()) {
+ return None;
+ }
+ Some(ImageAttachment {
+ media_type,
+ bytes,
+ text_offset: 0,
+ })
+}
+
+fn sniff_image_media_type(bytes: &[u8]) -> Option<&'static str> {
+ if bytes.starts_with(b"\x89PNG\r\n\x1a\n") {
+ Some("image/png")
+ } else if bytes.starts_with(&[0xff, 0xd8, 0xff]) {
+ Some("image/jpeg")
+ } else if bytes.starts_with(b"GIF87a") || bytes.starts_with(b"GIF89a") {
+ Some("image/gif")
+ } else if bytes.len() >= 12 && bytes.starts_with(b"RIFF") && &bytes[8..12] == b"WEBP" {
+ Some("image/webp")
+ } else if bytes.starts_with(b"BM") {
+ Some("image/bmp")
+ } else if bytes.starts_with(b"II*\0") || bytes.starts_with(b"MM\0*") {
+ Some("image/tiff")
+ } else if std::str::from_utf8(&bytes[..bytes.len().min(512)])
+ .ok()
+ .is_some_and(|head| head.contains("<svg"))
+ {
+ Some("image/svg+xml")
+ } else {
+ None
+ }
+}
+
+pub const IMAGE_PLACEHOLDER: &str = "[image]";
+
+/// 拆掉 Codex Desktop 文件清单包装,但保留 `My request` 后的真实提问。
+pub fn unwrap_file_preamble(text: &str) -> Option<String> {
+ const HEADS: &[&str] = &[
+ "# Files mentioned by the user",
+ "# Files pasted by the user",
+ ];
+ const MARKERS: &[&str] = &["## My request for Codex:", "## My request:"];
+ let text = text.trim_start();
+ if !HEADS.iter().any(|head| text.starts_with(head)) {
+ return None;
+ }
+ Some(
+ MARKERS
+ .iter()
+ .find_map(|marker| text.find(marker).map(|index| (marker, index)))
+ .map(|(marker, index)| text[index + marker.len()..].trim().to_string())
+ .unwrap_or_default(),
+ )
+}
+
+/// 剥掉 Codex Desktop 写入正文的 `<image ...>` 标签。真正的图片通常同时
+/// 存在于 input_image 内容块;临时文件路径失效时不再污染正文。
+pub fn strip_image_tags(text: &str) -> String {
+ const INJECTED_PREFIX: &str = "<image name=[Image #";
+ if !text.contains(INJECTED_PREFIX) {
+ return text.to_string();
+ }
+ let mut out = String::with_capacity(text.len());
+ let mut rest = text;
+ while let Some(start) = rest.find(INJECTED_PREFIX) {
+ out.push_str(&rest[..start]);
+ let after = &rest[start..];
+ let Some(open_end) = after.find('>').map(|index| index + 1) else {
+ out.push_str(after);
+ return out;
+ };
+ let open_tag = &after[..open_end];
+ if !open_tag.contains("] path=") {
+ out.push_str(&after[..INJECTED_PREFIX.len()]);
+ rest = &after[INJECTED_PREFIX.len()..];
+ continue;
+ }
+ let end = after[open_end..]
+ .find("</image>")
+ .map(|index| open_end + index + "</image>".len())
+ .unwrap_or(open_end);
+ rest = &after[end..];
+ }
+ out.push_str(rest);
+ out.trim().to_string()
}
/// 按 mtime 失效的单值缓存(边车映射/SQLite 全表在一轮扫描内的重复调用)。
@@ -281,7 +741,11 @@ pub fn clean_title_candidate(raw: &str) -> String {
out.push(c);
}
}
- let compact = out.split_whitespace().collect::<Vec<_>>().join(" ");
+ let compact = out
+ .replace(IMAGE_PLACEHOLDER, " ")
+ .split_whitespace()
+ .collect::<Vec<_>>()
+ .join(" ");
if compact.is_empty() {
return String::new();
}
@@ -404,3 +868,150 @@ pub fn make_preview(input: &Value) -> String {
one
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const IMAGE_SHAPES: &str = include_str!("../../tests/fixtures/image_content_shapes.json");
+
+ #[test]
+ fn readable_agent_image_shapes_decode_only_for_transcripts() {
+ let fixtures: Vec<Value> = serde_json::from_str(IMAGE_SHAPES).expect("image fixtures");
+ assert_eq!(fixtures.len(), 7);
+
+ for fixture in fixtures {
+ let name = fixture["name"].as_str().expect("fixture name");
+ let content = &fixture["content"];
+
+ let indexed = content_parts(content, false);
+ assert_eq!(indexed.text, IMAGE_PLACEHOLDER, "index shape {name}");
+ assert!(indexed.images.is_empty(), "index bytes {name}");
+
+ let transcript = content_parts(content, true);
+ assert!(transcript.text.is_empty(), "transcript text {name}");
+ assert_eq!(transcript.images.len(), 1, "transcript image {name}");
+ assert_eq!(transcript.images[0].media_type, "image/png", "mime {name}");
+ assert!(
+ transcript.images[0].bytes.starts_with(b"\x89PNG\r\n\x1a\n"),
+ "png bytes {name}"
+ );
+ }
+ }
+
+ #[test]
+ fn image_parser_is_bounded_and_never_fetches_remote_urls() {
+ let remote = serde_json::json!({
+ "type": "image",
+ "image_url": "https://example.com/private.png"
+ });
+ let parsed = content_parts(&remote, true);
+ assert_eq!(parsed.text, IMAGE_PLACEHOLDER);
+ assert!(parsed.images.is_empty());
+
+ let non_image = serde_json::json!({
+ "type": "document",
+ "source": {"type": "base64", "media_type": "application/pdf", "data": "AA=="}
+ });
+ assert!(!is_image_part(&non_image));
+ let parsed = content_parts(&non_image, true);
+ assert!(parsed.text.is_empty());
+ assert!(parsed.images.is_empty());
+
+ let oversized = format!("data:image/png;base64,{}", "A".repeat(MAX_IMAGE_B64 + 1));
+ assert!(decode_data_uri(&oversized).is_none());
+ }
+
+ #[test]
+ fn transcript_image_budget_is_aggregate_and_restores_after_scope() {
+ let png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";
+ let image = serde_json::json!({
+ "type": "image",
+ "media_type": "image/png",
+ "data": png
+ });
+ {
+ let _budget = image_decode_budget(Some(100));
+ assert_eq!(content_parts(&image, true).images.len(), 1);
+ let rejected = content_parts(&image, true);
+ assert!(rejected.images.is_empty());
+ assert_eq!(rejected.text, IMAGE_PLACEHOLDER);
+ }
+ assert_eq!(content_parts(&image, true).images.len(), 1);
+ }
+
+ #[test]
+ fn image_parser_handles_wrappers_mimeless_data_and_tool_fallbacks() {
+ let png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";
+ let wrapped = serde_json::json!({
+ "content": {"type": "image", "data": png}
+ });
+ let parsed = content_parts(&wrapped, true);
+ assert_eq!(parsed.images.len(), 1);
+ assert_eq!(parsed.images[0].media_type, "image/png");
+
+ let object = serde_json::json!({"exit_code": 0, "duration_ms": 12});
+ let parsed = tool_result_parts(&object, true);
+ assert!(parsed.images.is_empty());
+ assert_eq!(parsed.text, r#"{"duration_ms":12,"exit_code":0}"#);
+ }
+
+ #[test]
+ fn image_parser_preserves_text_image_order() {
+ let png = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=";
+ let content = serde_json::json!([
+ {"type": "text", "text": "前面的说明"},
+ {"type": "image", "media_type": "image/png", "data": png},
+ {"type": "text", "text": "后面的说明"}
+ ]);
+ let parsed = content_parts(&content, true);
+ assert_eq!(parsed.text, "前面的说明\n\n后面的说明");
+ assert_eq!(parsed.images.len(), 1);
+ assert_eq!(parsed.images[0].text_offset, "前面的说明".len());
+ }
+
+ #[test]
+ fn image_parser_never_dereferences_local_paths() {
+ let dir = tempfile::tempdir().expect("temp dir");
+ let path = dir.path().join("secret.png");
+ std::fs::write(&path, b"not really an image").expect("fixture");
+ let content = serde_json::json!({
+ "type": "image",
+ "media_type": "image/png",
+ "path": path
+ });
+ let parsed = content_parts(&content, true);
+ assert_eq!(parsed.text, IMAGE_PLACEHOLDER);
+ assert!(parsed.images.is_empty());
+ }
+
+ #[test]
+ fn codex_wrappers_keep_only_the_user_request() {
+ let wrapped = concat!(
+ "# Files mentioned by the user:\n\n",
+ "## Screenshot.png: /tmp/Screenshot.png\n\n",
+ "## My request:\n",
+ "看看这里 <image name=[Image #1] path=\"/tmp/missing.png\">ignored</image> 怎么了"
+ );
+ let request = unwrap_file_preamble(wrapped).expect("wrapped request");
+ assert_eq!(strip_image_tags(&request), "看看这里 怎么了");
+ let ordinary = "为什么 <image src=\"avatar.png\"></image> 没有显示?";
+ assert_eq!(strip_image_tags(ordinary), ordinary);
+ assert_eq!(clean_title_candidate("[image] 看看这里"), "看看这里");
+ }
+
+ #[test]
+ fn title_skips_pure_image_messages() {
+ let mut image = text_msg(Role::User, IMAGE_PLACEHOLDER, 1);
+ image.images.push(ImageAttachment {
+ media_type: "image/png".to_string(),
+ bytes: vec![1],
+ text_offset: 0,
+ });
+ let text = text_msg(Role::User, "后面的真实问题", 2);
+ assert_eq!(
+ title_from_messages(&[image, text]).as_deref(),
+ Some("后面的真实问题")
+ );
+ }
+}
diff --git a/crates/wake-core/src/adapters/pi.rs b/crates/wake-core/src/adapters/pi.rs
index e9c48d8..2c1ecdb 100644
--- a/crates/wake-core/src/adapters/pi.rs
+++ b/crates/wake-core/src/adapters/pi.rs
@@ -60,7 +60,8 @@ struct PiParse {
unknown_lines: u32,
}
-fn parse_pi_jsonl(path: &Path) -> Result<PiParse> {
+fn parse_pi_jsonl(path: &Path, decode_images: bool) -> Result<PiParse> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let file = fs::File::open(path)?;
let reader = BufReader::with_capacity(1 << 20, file);
@@ -115,13 +116,15 @@ fn parse_pi_jsonl(path: &Path) -> Result<PiParse> {
let content = msg.get("content").unwrap_or(&serde_json::Value::Null);
match msg.get("role").and_then(|v| v.as_str()) {
Some("user") => {
- let text = blocks_text(content);
- if !text.is_empty() {
- p.messages.push(text_msg(Role::User, &text, ts));
+ let parsed = content_parts(content, decode_images);
+ if !parsed.text.is_empty() || !parsed.images.is_empty() {
+ let mut message = text_msg(Role::User, &parsed.text, ts);
+ message.images = parsed.images;
+ p.messages.push(message);
}
}
Some("assistant") => {
- let text = blocks_text(content);
+ let parsed = content_parts(content, decode_images);
let mut tools: Vec<ToolCallView> = Vec::new();
for b in content.as_array().into_iter().flatten() {
if b.get("type").and_then(|v| v.as_str()) == Some("toolCall") {
@@ -141,7 +144,7 @@ fn parse_pi_jsonl(path: &Path) -> Result<PiParse> {
));
}
}
- if text.is_empty() && tools.is_empty() {
+ if parsed.text.is_empty() && tools.is_empty() && parsed.images.is_empty() {
continue;
}
let model = msg.get("model").and_then(|v| v.as_str()).map(String::from);
@@ -164,11 +167,12 @@ fn parse_pi_jsonl(path: &Path) -> Result<PiParse> {
let last = &mut p.messages[base];
// 合并后统一压 MAX_MSG_TEXT 上限(整个 agentic 回合并成
// 一条消息,不能靠单行的 text_msg clip)
- if !text.is_empty() && last.text.len() < MAX_MSG_TEXT {
- if !last.text.is_empty() {
- last.text.push_str("\n\n");
+ if !parsed.text.is_empty() || !parsed.images.is_empty() {
+ if last.text.len() < MAX_MSG_TEXT {
+ append_content_to_message(last, parsed, "\n\n");
+ } else {
+ append_images_to_message_end(last, parsed.images);
}
- last.text.push_str(&text);
if last.text.len() > MAX_MSG_TEXT {
let (t, _) = clip(&last.text, MAX_MSG_TEXT);
last.text = t;
@@ -188,14 +192,16 @@ fn parse_pi_jsonl(path: &Path) -> Result<PiParse> {
continue;
};
if let Some(&(mi, ti)) = tool_index.get(call_id) {
- let tc = &mut p.messages[mi].tool_calls[ti];
- let text = blocks_text(content);
- if !text.is_empty() {
- tc.output = Some(clip(&text, MAX_TOOL_IO).0);
+ let parsed = content_parts(content, decode_images);
+ let message = &mut p.messages[mi];
+ let tc = &mut message.tool_calls[ti];
+ if !parsed.text.is_empty() {
+ tc.output = Some(clip(&parsed.text, MAX_TOOL_IO).0);
}
if msg.get("isError").and_then(|v| v.as_bool()) == Some(true) {
tc.is_error = true;
}
+ append_images_to_message_end(message, parsed.images);
}
}
_ => {
@@ -263,7 +269,7 @@ impl AgentAdapter for PiAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let parsed = parse_pi_jsonl(Path::new(&r.file_path))?;
+ let parsed = parse_pi_jsonl(Path::new(&r.file_path), false)?;
let meta = build_meta(self.agent, r, &parsed);
let units = units_from_messages(&parsed.messages);
Ok(ParsedSession {
@@ -274,7 +280,7 @@ impl AgentAdapter for PiAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let parsed = parse_pi_jsonl(Path::new(&r.file_path))?;
+ let parsed = parse_pi_jsonl(Path::new(&r.file_path), true)?;
Ok(ParsedTranscript {
meta: build_meta(self.agent, r, &parsed),
mainline: parsed.messages,
diff --git a/crates/wake-core/src/adapters/qoder.rs b/crates/wake-core/src/adapters/qoder.rs
index bc8ac12..f0e41e8 100644
--- a/crates/wake-core/src/adapters/qoder.rs
+++ b/crates/wake-core/src/adapters/qoder.rs
@@ -85,7 +85,7 @@ enum ActiveLeaf {
#[derive(Default)]
struct PendingAssistant {
msg_id: Option<String>,
- text: Vec<String>,
+ content: ParsedContent,
thinking: Vec<String>,
tool_calls: Vec<ToolCallView>,
timestamp: Option<i64>,
@@ -156,36 +156,18 @@ fn usage_tokens(usage: &Value) -> i64 {
.sum()
}
-fn stringify_tool_result(v: Option<&Value>) -> String {
- match v {
- Some(Value::String(s)) => s.clone(),
- Some(Value::Array(items)) => items
- .iter()
- .filter_map(|item| {
- item.get("text")
- .and_then(Value::as_str)
- .map(str::to_string)
- .or_else(|| {
- item.as_str()
- .map(str::to_string)
- .or_else(|| serde_json::to_string(item).ok())
- })
- })
- .collect::<Vec<_>>()
- .join("\n"),
- Some(Value::Null) | None => String::new(),
- Some(other) => serde_json::to_string(other).unwrap_or_default(),
- }
-}
-
fn flush_assistant(
pending: &mut Option<PendingAssistant>,
messages: &mut Vec<TranscriptMessage>,
tool_index: &mut HashMap<String, (usize, usize)>,
) {
let Some(p) = pending.take() else { return };
- let text = p.text.join("\n\n");
- if text.trim().is_empty() && p.tool_calls.is_empty() && p.thinking.is_empty() {
+ let ParsedContent { text, images } = p.content;
+ if text.trim().is_empty()
+ && p.tool_calls.is_empty()
+ && p.thinking.is_empty()
+ && images.is_empty()
+ {
return;
}
let (text, truncated) = clip(text.trim(), MAX_MSG_TEXT);
@@ -210,6 +192,7 @@ fn flush_assistant(
thinking,
timestamp: p.timestamp,
model: p.model,
+ images,
});
}
@@ -335,7 +318,8 @@ fn active_chain(
out
}
-fn parse_qoder_jsonl(path: &Path) -> Result<QoderParse> {
+fn parse_qoder_jsonl(path: &Path, decode_images: bool) -> Result<QoderParse> {
+ let _image_budget = transcript_image_decode_budget(decode_images);
let file = fs::File::open(path)?;
let reader = BufReader::with_capacity(1 << 20, file);
let mut nodes: HashMap<String, MessageNode> = HashMap::new();
@@ -477,6 +461,7 @@ fn parse_qoder_jsonl(path: &Path) -> Result<QoderParse> {
thinking: None,
timestamp: (ts > 0).then_some(ts),
model: None,
+ images: Vec::new(),
});
}
continue;
@@ -492,36 +477,39 @@ fn parse_qoder_jsonl(path: &Path) -> Result<QoderParse> {
};
if typ == "user" {
flush_assistant(&mut pending, &mut messages, &mut tool_index);
- let mut parts = Vec::new();
+ let mut parsed_message = ParsedContent::default();
match message.get("content") {
- Some(Value::String(text)) => parts.push(text.clone()),
+ Some(Value::String(text)) => parsed_message.push_text(text),
Some(Value::Array(blocks)) => {
for block in blocks {
match block.get("type").and_then(Value::as_str) {
Some("text") => {
if let Some(text) = block.get("text").and_then(Value::as_str) {
- parts.push(text.to_string());
+ parsed_message.push_text(text);
}
}
- Some("image") => parts.push("[image]".to_string()),
+ _ if is_image_part(block) => {
+ let parsed = content_parts(block, decode_images);
+ parsed_message.append(parsed);
+ }
Some("tool_result") => {
let id = block
.get("tool_use_id")
.and_then(Value::as_str)
.unwrap_or("");
if let Some(&(msg_ix, tool_ix)) = tool_index.get(id) {
- let tool = &mut messages[msg_ix].tool_calls[tool_ix];
- tool.output = Some(
- clip(
- &stringify_tool_result(block.get("content")),
- MAX_TOOL_IO,
- )
- .0,
+ let parsed = tool_result_parts(
+ block.get("content").unwrap_or(&Value::Null),
+ decode_images,
);
+ let message = &mut messages[msg_ix];
+ let tool = &mut message.tool_calls[tool_ix];
+ tool.output = Some(clip(&parsed.text, MAX_TOOL_IO).0);
tool.is_error = block
.get("is_error")
.and_then(Value::as_bool)
.unwrap_or(false);
+ append_images_to_message_end(message, parsed.images);
}
}
_ => {}
@@ -530,8 +518,8 @@ fn parse_qoder_jsonl(path: &Path) -> Result<QoderParse> {
}
_ => {}
}
- let text = parts.join("\n\n").trim().to_string();
- if text.is_empty() {
+ let ParsedContent { text, images } = parsed_message;
+ if text.is_empty() && images.is_empty() {
continue;
}
let compact = row.get("isCompactSummary").and_then(Value::as_bool) == Some(true);
@@ -563,6 +551,7 @@ fn parse_qoder_jsonl(path: &Path) -> Result<QoderParse> {
thinking: None,
timestamp: (ts > 0).then_some(ts),
model: None,
+ images,
});
continue;
}
@@ -602,7 +591,7 @@ fn parse_qoder_jsonl(path: &Path) -> Result<QoderParse> {
}
match message.get("content") {
Some(Value::String(text)) if !text.trim().is_empty() => {
- current.text.push(text.clone());
+ current.content.push_text(text);
}
Some(Value::Array(blocks)) => {
for block in blocks {
@@ -610,7 +599,7 @@ fn parse_qoder_jsonl(path: &Path) -> Result<QoderParse> {
Some("text") => {
if let Some(text) = block.get("text").and_then(Value::as_str) {
if !text.trim().is_empty() {
- current.text.push(text.to_string());
+ current.content.push_text(text);
}
}
}
@@ -633,6 +622,10 @@ fn parse_qoder_jsonl(path: &Path) -> Result<QoderParse> {
.tool_calls
.push(tool_call_view(id, name, &input, None, false));
}
+ _ if is_image_part(block) => {
+ let parsed = content_parts(block, decode_images);
+ current.content.append(parsed);
+ }
_ => {}
}
}
@@ -727,7 +720,7 @@ impl AgentAdapter for QoderAdapter {
}
fn parse_session(&self, r: &SessionFileRef) -> Result<ParsedSession> {
- let parsed = parse_qoder_jsonl(Path::new(&r.file_path))?;
+ let parsed = parse_qoder_jsonl(Path::new(&r.file_path), false)?;
let meta = build_meta(r, &parsed);
let units = units_from_messages(&parsed.messages);
Ok(ParsedSession {
@@ -738,7 +731,7 @@ impl AgentAdapter for QoderAdapter {
}
fn parse_transcript(&self, r: &SessionFileRef) -> Result<ParsedTranscript> {
- let parsed = parse_qoder_jsonl(Path::new(&r.file_path))?;
+ let parsed = parse_qoder_jsonl(Path::new(&r.file_path), true)?;
Ok(ParsedTranscript {
meta: build_meta(r, &parsed),
mainline: parsed.messages,
diff --git a/crates/wake-core/src/models.rs b/crates/wake-core/src/models.rs
index 7f1491b..647c716 100644
--- a/crates/wake-core/src/models.rs
+++ b/crates/wake-core/src/models.rs
@@ -232,6 +232,17 @@ impl Role {
}
}
+/// 会话正文里携带的一张图片。这里保存 PNG/JPEG 等原始编码字节,
+/// 不是解码后的像素;adapter 只在详情解析路径填充,索引路径仅保留占位符。
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct ImageAttachment {
+ pub media_type: String,
+ pub bytes: Vec<u8>,
+ /// 图片在消息纯文本中的字节偏移。多个图片可共享同一偏移,表示连续图片块。
+ #[serde(default)]
+ pub text_offset: usize,
+}
+
/// 详情页统一消息模型
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranscriptMessage {
@@ -246,6 +257,9 @@ pub struct TranscriptMessage {
/// epoch ms
pub timestamp: Option<i64>,
pub model: Option<String>,
+ /// 随消息出现的图片,保持源内容块的顺序。
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub images: Vec<ImageAttachment>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -415,6 +429,10 @@ pub const HL_CLOSE: char = '\u{e001}';
/// 单条消息正文入库/传输上限
pub const MAX_MSG_TEXT: usize = 32 * 1024;
+/// 单张图片的 base64 文本上限(约 12 MiB 原始图片)。
+pub const MAX_IMAGE_B64: usize = 16 * 1024 * 1024;
+/// 本地引用图片的读取上限,与内联图片解码后的量级保持一致。
+pub const MAX_IMAGE_BYTES: u64 = 12 * 1024 * 1024;
/// tool 输入/输出、thinking 上限
pub const MAX_TOOL_IO: usize = 16 * 1024;
pub const MAX_TITLE: usize = 80;
diff --git a/crates/wake-core/src/services/exporter.rs b/crates/wake-core/src/services/exporter.rs
index 219fd95..0e2ba7e 100644
--- a/crates/wake-core/src/services/exporter.rs
+++ b/crates/wake-core/src/services/exporter.rs
@@ -47,8 +47,26 @@ fn render_message(m: &TranscriptMessage, out: &mut String) {
"<details><summary>🧠 Thinking</summary>\n\n{th}\n\n</details>\n\n"
));
}
- if !m.text.is_empty() {
- out.push_str(&m.text);
+ let mut cursor = 0usize;
+ for image in &m.images {
+ let mut offset = image.text_offset.min(m.text.len());
+ while offset > cursor && !m.text.is_char_boundary(offset) {
+ offset -= 1;
+ }
+ offset = offset.max(cursor);
+ if offset > cursor {
+ out.push_str(&m.text[cursor..offset]);
+ out.push_str("\n\n");
+ }
+ out.push_str(&format!(
+ "> 🖼 Image attachment · {} · {} bytes\n\n",
+ image.media_type,
+ image.bytes.len()
+ ));
+ cursor = offset;
+ }
+ if cursor < m.text.len() {
+ out.push_str(&m.text[cursor..]);
out.push_str("\n\n");
}
for tc in &m.tool_calls {
diff --git a/crates/wake-core/tests/adapter_contracts.rs b/crates/wake-core/tests/adapter_contracts.rs
index 1f86b1a..d200bca 100644
--- a/crates/wake-core/tests/adapter_contracts.rs
+++ b/crates/wake-core/tests/adapter_contracts.rs
@@ -351,6 +351,20 @@ fn claude_ref() -> SessionFileRef {
"11111111-aaaa-bbbb-cccc-000000000001",
)
}
+fn claude_images_ref() -> SessionFileRef {
+ fs_ref(
+ AgentId::ClaudeCode,
+ &fixture("claude/projects/-Users-tester-Github-wakefx/44444444-aaaa-bbbb-cccc-000000000004.jsonl"),
+ "44444444-aaaa-bbbb-cccc-000000000004",
+ )
+}
+fn codex_images_ref() -> SessionFileRef {
+ fs_ref(
+ AgentId::Codex,
+ &fixture("codex/sessions/2026/08/08/rollout-2026-08-08T09-00-00-55555555-aaaa-bbbb-cccc-000000000005.jsonl"),
+ "55555555-aaaa-bbbb-cccc-000000000005",
+ )
+}
fn codex_ref() -> SessionFileRef {
fs_ref(
AgentId::Codex,
@@ -358,6 +372,95 @@ fn codex_ref() -> SessionFileRef {
"22222222-aaaa-bbbb-cccc-000000000002",
)
}
+
+#[test]
+fn claude_inline_images_only_decode_for_transcripts() {
+ setup();
+ let adapter = ClaudeAdapter::new();
+ let r = claude_images_ref();
+ let transcript = adapter.parse_transcript(&r).expect("claude images");
+ let users: Vec<_> = transcript
+ .mainline
+ .iter()
+ .filter(|message| message.role == Role::User)
+ .collect();
+
+ assert_eq!(users.len(), 4);
+ assert_eq!(users[0].text, "这个按钮的颜色对不上");
+ assert_eq!(users[0].images.len(), 1);
+ assert_eq!(users[0].images[0].text_offset, 0);
+ assert_eq!(users[0].images[0].media_type, "image/png");
+ assert!(users[0].images[0].bytes.starts_with(b"\x89PNG"));
+ assert!(users[1].text.is_empty());
+ assert_eq!(users[1].images.len(), 1, "纯图片消息不能被丢掉");
+ assert_eq!(users[2].images[0].media_type, "image/heic");
+ assert!(users[3].images.is_empty());
+ assert_eq!(users[3].text, "[image]");
+ assert_eq!(transcript.meta.title, "这个按钮的颜色对不上");
+
+ let session = adapter.parse_session(&r).expect("claude image index");
+ let user_units: Vec<_> = session
+ .units
+ .iter()
+ .filter(|unit| unit.role == Role::User)
+ .map(|unit| unit.text.as_str())
+ .collect();
+ assert_eq!(
+ user_units,
+ vec![
+ "[image]\n\n这个按钮的颜色对不上",
+ "[image]",
+ "[image]",
+ "[image]"
+ ]
+ );
+}
+
+#[test]
+fn codex_inline_images_decode_and_strip_desktop_wrappers() {
+ setup();
+ let adapter = CodexAdapter::new();
+ let r = codex_images_ref();
+ let transcript = adapter.parse_transcript(&r).expect("codex images");
+ let users: Vec<_> = transcript
+ .mainline
+ .iter()
+ .filter(|message| message.role == Role::User)
+ .collect();
+
+ assert_eq!(users.len(), 5);
+ assert_eq!(users[0].text, "这个按钮颜色不对");
+ assert_eq!(users[0].images.len(), 1);
+ assert_eq!(users[0].images[0].text_offset, users[0].text.len());
+ assert!(users[0].images[0].bytes.starts_with(b"\x89PNG"));
+ assert!(users[1].text.is_empty());
+ assert_eq!(users[1].images.len(), 1, "纯图片消息不能被丢掉");
+ assert!(users[2].images.is_empty());
+ assert_eq!(users[2].text, "[image]");
+ assert_eq!(users[3].text, "参考这张图,人物面部不要有任何变化");
+ assert_eq!(users[3].kind, MessageKind::Text);
+ assert!(users[4].text.is_empty());
+ assert_eq!(users[4].images.len(), 1);
+
+ let session = adapter.parse_session(&r).expect("codex image index");
+ let user_units: Vec<_> = session
+ .units
+ .iter()
+ .filter(|unit| unit.role == Role::User)
+ .map(|unit| unit.text.as_str())
+ .collect();
+ assert_eq!(
+ user_units,
+ vec![
+ "这个按钮颜色不对\n\n[image]",
+ "[image]",
+ "[image]",
+ "参考这张图,人物面部不要有任何变化\n\n[image]",
+ "[image]"
+ ]
+ );
+ assert_eq!(session.meta.title, "这个按钮颜色不对");
+}
fn codex_subagent_ref() -> SessionFileRef {
fs_ref(
AgentId::Codex,
diff --git a/crates/wake-core/tests/fixtures/claude/projects/-Users-tester-Github-wakefx/44444444-aaaa-bbbb-cccc-000000000004.jsonl b/crates/wake-core/tests/fixtures/claude/projects/-Users-tester-Github-wakefx/44444444-aaaa-bbbb-cccc-000000000004.jsonl
new file mode 100644
index 0000000..436bf08
--- /dev/null
+++ b/crates/wake-core/tests/fixtures/claude/projects/-Users-tester-Github-wakefx/44444444-aaaa-bbbb-cccc-000000000004.jsonl
@@ -0,0 +1,5 @@
+{"parentUuid":null,"isSidechain":false,"cwd":"/Users/tester/Github/wakefx","gitBranch":"main","sessionId":"44444444-aaaa-bbbb-cccc-000000000004","uuid":"i-0","timestamp":"2026-08-07T10:00:00.000Z","type":"user","message":{"role":"user","content":[{"type":"image","source":{"type":"base64","media_type":"image/png","data":"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC"}},{"type":"text","text":"这个按钮的颜色对不上"}]}}
+{"parentUuid":"i-0","isSidechain":false,"cwd":"/Users/tester/Github/wakefx","gitBranch":"main","sessionId":"44444444-aaaa-bbbb-cccc-000000000004","uuid":"i-1","timestamp":"2026-08-07T10:00:10.000Z","type":"user","message":{"role":"user","content":[{"type":"image","source":{"type":"base64","media_type":"image/png","data":"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC"}}]}}
+{"parentUuid":"i-1","isSidechain":false,"cwd":"/Users/tester/Github/wakefx","gitBranch":"main","sessionId":"44444444-aaaa-bbbb-cccc-000000000004","uuid":"i-2","timestamp":"2026-08-07T10:00:20.000Z","type":"user","message":{"role":"user","content":[{"type":"image","source":{"type":"base64","media_type":"image/heic","data":"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGP4z8AAAAMBAQDJ/pLvAAAAAElFTkSuQmCC"}}]}}
+{"parentUuid":"i-2","isSidechain":false,"cwd":"/Users/tester/Github/wakefx","gitBranch":"main","sessionId":"44444444-aaaa-bbbb-cccc-000000000004","uuid":"i-3","timestamp":"2026-08-07T10:00:30.000Z","type":"user","message":{"role":"user","content":[{"type":"image","source":{"type":"url","url":"https://example.com/a.png"}}]}}
+{"parentUuid":"i-3","isSidechain":false,"cwd":"/Users/tester/Github/wakefx","gitBranch":"main","sessionId":"44444444-aaaa-bbbb-cccc-000000000004","uuid":"i-4","timestamp":"2026-08-07T10:00:40.000Z","type":"assistant","message":{"id":"msg_i","type":"message","role":"assistant","model":"claude-opus-5","content":[{"type":"text","text":"取的是 theme.accent,应该走 theme.primary。"}],"usage":{"input_tokens":100,"output_tokens":20}}}
diff --git a/crates/wake-core/tests/fixtures/codex/sessions/2026/08/08/rollout-2026-08-08T09-00-00-55555555-aaaa-bbbb-cccc-000000000005.jsonl b/crates/wake-core/tests/fixtures/codex/sessions/2026/08/08/rollout-2026-08-08T09-00-00-55555555-aaaa-bbbb-cccc-000000000005.jsonl
new file mode 100644
index 0000000..b4660d4
--- /dev/null
+++ b/crates/wake-core/tests/fixtures/codex/sessions/2026/08/08/rollout-2026-08-08T09-00-00-55555555-aaaa-bbbb-cccc-000000000005.jsonl
@@ -0,0 +1,7 @@
+{"timestamp":"2026-08-08T09:00:00.000Z","type":"session_meta","payload":{"id":"55555555-aaaa-bbbb-cccc-000000000005","cwd":"/Users/tester/Github/wakefx","originator":"codex_cli_rs"}}
+{"timestamp":"2026-08-08T09:00:10.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"这个按钮颜色不对\n<image name=[Image #1] path=\"/var/folders/zz/T/codex-clipboard-abc.png\">\n</image>"},{"type":"input_image","image_url":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNg+M8AAAICAQB7CYF4AAAAAElFTkSuQmCC","detail":"high"}]}}
+{"timestamp":"2026-08-08T09:00:20.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_image","image_url":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNg+M8AAAICAQB7CYF4AAAAAElFTkSuQmCC"}]}}
+{"timestamp":"2026-08-08T09:00:30.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_image","image_url":"https://example.com/a.png"}]}}
+{"timestamp":"2026-08-08T09:00:40.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"\n# Files mentioned by the user:\n\n## shot.png: /Users/tester/Downloads/shot.png\n\n## My request for Codex:\n参考这张图,人物面部不要有任何变化\n"},{"type":"input_image","image_url":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNg+M8AAAICAQB7CYF4AAAAAElFTkSuQmCC"}]}}
+{"timestamp":"2026-08-08T09:00:50.000Z","type":"response_item","payload":{"type":"message","role":"user","content":[{"type":"input_text","text":"\n# Files mentioned by the user:\n\n## a.png: /Users/tester/Downloads/a.png\n"},{"type":"input_image","image_url":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNg+M8AAAICAQB7CYF4AAAAAElFTkSuQmCC"}]}}
+{"timestamp":"2026-08-08T09:01:00.000Z","type":"response_item","payload":{"type":"message","role":"assistant","content":[{"type":"output_text","text":"取的是 accent,应该走 primary。"}]}}
diff --git a/crates/wake-core/tests/fixtures/image_content_shapes.json b/crates/wake-core/tests/fixtures/image_content_shapes.json
new file mode 100644
index 0000000..4b69cab
--- /dev/null
+++ b/crates/wake-core/tests/fixtures/image_content_shapes.json
@@ -0,0 +1,71 @@
+[
+ {
+ "name": "anthropic-source",
+ "agents": ["claude-code", "qoder", "cursor"],
+ "content": {
+ "type": "image",
+ "source": {
+ "type": "base64",
+ "media_type": "image/png",
+ "data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
+ }
+ }
+ },
+ {
+ "name": "openai-input-image",
+ "agents": ["codex"],
+ "content": {
+ "type": "input_image",
+ "image_url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
+ }
+ },
+ {
+ "name": "gemini-inline-data",
+ "agents": ["gemini"],
+ "content": {
+ "inlineData": {
+ "mimeType": "image/png",
+ "data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
+ }
+ }
+ },
+ {
+ "name": "direct-image-data",
+ "agents": ["pi", "omp", "dsh"],
+ "content": {
+ "type": "image",
+ "mimeType": "image/png",
+ "data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
+ }
+ },
+ {
+ "name": "nested-image-data",
+ "agents": ["kiro", "kimi"],
+ "content": {
+ "kind": "image",
+ "data": {
+ "mimeType": "image/png",
+ "data": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
+ }
+ }
+ },
+ {
+ "name": "opencode-file",
+ "agents": ["opencode"],
+ "content": {
+ "type": "file",
+ "mime": "image/png",
+ "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
+ }
+ },
+ {
+ "name": "camel-case-image-url",
+ "agents": ["grok"],
+ "content": {
+ "type": "image",
+ "imageUrl": {
+ "url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
+ }
+ }
+ }
+]
diff --git a/crates/wake/Cargo.toml b/crates/wake/Cargo.toml
index faf3ab8..80295f1 100644
--- a/crates/wake/Cargo.toml
+++ b/crates/wake/Cargo.toml
@@ -22,6 +22,7 @@ chrono.workspace = true
dirs.workspace = true
semver.workspace = true
reqwest.workspace = true
+image.workspace = true
unicode-width = "0.2"
unicode-segmentation = "1.13"
diff --git a/crates/wake/src/format.rs b/crates/wake/src/format.rs
index 5aa94c9..e16a0e4 100644
--- a/crates/wake/src/format.rs
+++ b/crates/wake/src/format.rs
@@ -162,6 +162,18 @@ pub fn expand_tilde(p: &str) -> String {
}
}
+/// 图片字节数的人读格式;详情预览只需要 KB/MB 两档。
+pub fn human_bytes(bytes: usize) -> String {
+ const KB: f64 = 1024.0;
+ const MB: f64 = KB * 1024.0;
+ let bytes = bytes as f64;
+ if bytes >= MB {
+ format!("{:.1} MB", bytes / MB)
+ } else {
+ format!("{:.0} KB", (bytes / KB).max(1.0))
+ }
+}
+
/// 首行截断预览
pub fn one_line(s: &str, max_chars: usize) -> String {
let joined = s.split_whitespace().collect::<Vec<_>>().join(" ");
@@ -242,4 +254,11 @@ mod tests {
let toned = "👍🏽";
assert_eq!(clip_display(&format!("{toned}xy"), 3), format!("{toned}…"));
}
+
+ #[test]
+ fn human_bytes_uses_readable_image_units() {
+ assert_eq!(human_bytes(1), "1 KB");
+ assert_eq!(human_bytes(1536), "2 KB");
+ assert_eq!(human_bytes(2 * 1024 * 1024 + 512 * 1024), "2.5 MB");
+ }
}
diff --git a/crates/wake/src/ui.rs b/crates/wake/src/ui.rs
index 44f1716..a853804 100644
--- a/crates/wake/src/ui.rs
+++ b/crates/wake/src/ui.rs
@@ -143,6 +143,10 @@ pub const RADIUS_BUTTON: Pixels = px(6.);
pub const RADIUS_KBD: Pixels = px(5.);
/// 小胶囊 badge(项目名 / model / source / 各处计数共用)
pub const RADIUS_BADGE: Pixels = px(4.);
+/// 对话正文内联图片缩略图。
+pub const IMAGE_THUMB: Pixels = px(104.);
+pub const RADIUS_IMAGE: Pixels = px(10.);
+pub const IMAGE_SCRIM: f32 = 0.62;
/// 数据可视化小色块(热力图、分布柱、图例),统一保持方格读数感。
pub const RADIUS_CELL: Pixels = px(2.);
diff --git a/crates/wake/src/workbench.rs b/crates/wake/src/workbench.rs
index 9ed7802..d927f40 100644
--- a/crates/wake/src/workbench.rs
+++ b/crates/wake/src/workbench.rs
@@ -1440,6 +1440,7 @@ fn load_visible_transcript(
&& (!message.text.trim().is_empty()
|| !message.tool_calls.is_empty()
|| message.thinking.is_some()
+ || !message.images.is_empty()
|| message.kind == MessageKind::CompactSummary)
})
.collect())
@@ -1466,12 +1467,109 @@ struct DetailState {
expanded_thinking: HashSet<usize>,
/// 搜索跳转目标(FTS seq,契约=消息 seq);解析完成后滚到该消息并保持高亮
jump_seq: Option<i64>,
+ /// 与 transcript 同下标;原始字节只保留在 Arc<Image> 内,避免双份内存。
+ images: Vec<Vec<ImageSlot>>,
+ /// 放大预览中的图片:(消息下标, 消息内图片下标)。
+ zoom: Option<(usize, usize)>,
+ /// 放大预览操作的短暂就地反馈。绑定具体图片,避免切换预览后把上一张的
+ /// 成功状态带过来;generation 让较早的复位计时器不能清掉较新的反馈。
+ image_action_feedback: [Option<ImageActionFeedback>; 2],
+}
+
+#[derive(Clone, Copy, PartialEq, Eq)]
+enum ImageAction {
+ Copy,
+ Save,
+}
+
+impl ImageAction {
+ const fn index(self) -> usize {
+ match self {
+ Self::Copy => 0,
+ Self::Save => 1,
+ }
+ }
+}
+
+#[derive(Clone, Copy, PartialEq, Eq)]
+struct ImageActionFeedback {
+ target: (usize, usize),
+ action: ImageAction,
+ generation: u64,
+}
+
+#[derive(Clone)]
+enum ImageSlot {
+ Ready {
+ image: Arc<gpui::Image>,
+ dims: Option<(u32, u32)>,
+ text_offset: usize,
+ },
+ Unsupported {
+ media_type: SharedString,
+ bytes: Arc<Vec<u8>>,
+ text_offset: usize,
+ },
+ Omitted {
+ text_offset: usize,
+ },
+}
+
+impl ImageSlot {
+ fn text_offset(&self) -> usize {
+ match self {
+ Self::Ready { text_offset, .. }
+ | Self::Unsupported { text_offset, .. }
+ | Self::Omitted { text_offset } => *text_offset,
+ }
+ }
+}
+
+fn take_image_slots(messages: &mut [TranscriptMessage]) -> Vec<Vec<ImageSlot>> {
+ const TRANSCRIPT_IMAGE_BYTES: usize = 64 * 1024 * 1024;
+ const TRANSCRIPT_DECODED_PIXELS: u64 = 96_000_000;
+ let mut remaining_bytes = TRANSCRIPT_IMAGE_BYTES;
+ let mut remaining_pixels = TRANSCRIPT_DECODED_PIXELS;
+
+ messages
+ .iter_mut()
+ .map(|message| {
+ std::mem::take(&mut message.images)
+ .into_iter()
+ .map(|attachment| {
+ let text_offset = attachment.text_offset.min(message.text.len());
+ if attachment.bytes.len() > remaining_bytes {
+ return ImageSlot::Omitted { text_offset };
+ }
+ remaining_bytes -= attachment.bytes.len();
+ match image_format_of(&attachment.media_type).and_then(|format| {
+ image_probe(&attachment.bytes, format).map(|probe| (format, probe))
+ }) {
+ Some((format, probe)) if probe.decoded_pixels <= remaining_pixels => {
+ remaining_pixels -= probe.decoded_pixels;
+ ImageSlot::Ready {
+ image: Arc::new(gpui::Image::from_bytes(format, attachment.bytes)),
+ dims: Some(probe.dims),
+ text_offset,
+ }
+ }
+ _ => ImageSlot::Unsupported {
+ media_type: attachment.media_type.into(),
+ bytes: Arc::new(attachment.bytes),
+ text_offset,
+ },
+ }
+ })
+ .collect()
+ })
+ .collect()
}
#[cfg(test)]
mod detail_selection_tests {
use std::time::Duration;
+ use super::image_probe;
use gpui::{
div, list, point, px, AppContext as _, Context, Element as _, InteractiveElement as _,
IntoElement, ListAlignment, ListState, Modifiers, MouseButton, MouseMoveEvent,
@@ -1676,6 +1774,20 @@ mod detail_selection_tests {
);
assert!(!view.read_with(cx, |view, _| view.auto_scroll.is_active()));
}
+
+ #[test]
+ fn image_probe_accepts_small_matching_images_and_rejects_mime_mismatch() {
+ let mut png = std::io::Cursor::new(Vec::new());
+ image::DynamicImage::new_rgba8(2, 3)
+ .write_to(&mut png, image::ImageFormat::Png)
+ .expect("png fixture");
+ let bytes = png.into_inner();
+
+ let probe = image_probe(&bytes, gpui::ImageFormat::Png).expect("safe png");
+ assert_eq!(probe.dims, (2, 3));
+ assert_eq!(probe.decoded_pixels, 6);
+ assert!(image_probe(&bytes, gpui::ImageFormat::Jpeg).is_none());
+ }
}
// ---------------- Workbench ----------------
@@ -1734,6 +1846,9 @@ pub struct Workbench {
/// 文本拖选靠近详情视口边缘时,持续推动外层消息列表。
detail_selection_auto_scroll: AutoScroll,
detail: Option<DetailState>,
+ /// 图片操作反馈的全局递增代次。即使关闭后重新打开同一会话和图片,旧计时
+ /// 器也不会误清新状态。
+ image_action_feedback_generation: u64,
/// Insights 页(侧栏底部入口):打开时替换中栏+右栏。与其他导航目的地
/// 互斥(侧栏单选模型);数据在 open/refresh 时后台重算,Rc 免深拷贝
@@ -2031,6 +2146,7 @@ impl Workbench {
_calendar_task: None,
detail_selection_auto_scroll: AutoScroll::default(),
detail: None,
+ image_action_feedback_generation: 0,
insights_open: false,
insights: None,
insights_loading: false,
@@ -3209,6 +3325,7 @@ impl Workbench {
// ↑↓ 在 Input 内不被消费,冒泡到这里走 main.rs 的
// PALETTE_CONTEXT 键位(Input 拆出 List 后原生 List 绑定够不着)
.key_context(PALETTE_CONTEXT)
+ .relative()
.on_action(window.listener_for(
&this,
|wb: &mut Self, _: &PaletteUp, window, cx| {
@@ -3225,6 +3342,40 @@ impl Workbench {
// 不用手工重算列表高度
.h(PALETTE_HEIGHT)
.gap(SPACE_MD)
+ .child(
+ // gpui-component 0.5.1 的 Dialog 把遮罩关闭挂在冒泡
+ // 阶段,而遮罩自身的 occlude 在当前 GPUI 版本会让该
+ // 路径失效。用面板实际布局边界做窗口级捕获,确保任何
+ // 面板外左键都能关闭;面板内输入、结果点击不受影响。
+ canvas(
+ |_, _, _| {},
+ |content_bounds, _, window, _| {
+ let dialog_bounds = Bounds {
+ origin: point(
+ content_bounds.origin.x - SPACE_XL,
+ content_bounds.origin.y - px(24.),
+ ),
+ size: size(
+ content_bounds.size.width + SPACE_XL * 2.,
+ content_bounds.size.height + px(48.),
+ ),
+ };
+ window.on_mouse_event(
+ move |event: &MouseDownEvent, phase, window, cx| {
+ if phase.capture()
+ && event.button == MouseButton::Left
+ && !dialog_bounds.contains(&event.position)
+ {
+ cx.stop_propagation();
+ window.close_dialog(cx);
+ }
+ },
+ );
+ },
+ )
+ .absolute()
+ .inset_0(),
+ )
.child(
div()
.flex_shrink_0()
@@ -3433,6 +3584,9 @@ impl Workbench {
expanded_tools: HashSet::new(),
expanded_thinking: HashSet::new(),
jump_seq,
+ images: Vec::new(),
+ zoom: None,
+ image_action_feedback: [None; 2],
});
// 搜索路径:中栏列表同步选中并滚到该会话。
// 列表点击路径(jump=None)不走——List 点击自带选中,再滚会跳视口
@@ -3443,7 +3597,10 @@ impl Workbench {
let adapters = self.adapters.clone();
let task = cx.background_spawn(async move {
- let result = load_visible_transcript(&adapters, &meta);
+ let result = load_visible_transcript(&adapters, &meta).map(|mut messages| {
+ let images = take_image_slots(&mut messages);
+ (messages, images)
+ });
(meta.key.clone(), result)
});
cx.spawn_in(window, async move |this, cx| {
@@ -3453,7 +3610,7 @@ impl Workbench {
if key == detail.meta.key {
detail.loading = false;
match result {
- Ok(messages) => {
+ Ok((messages, images)) => {
detail.msg_list = gpui::ListState::new(
messages.len(),
gpui::ListAlignment::Bottom,
@@ -3474,10 +3631,14 @@ impl Workbench {
}
}
detail.transcript = Rc::new(messages);
+ detail.images = images;
+ detail.zoom = None;
detail.error = None;
}
Err(error) => {
detail.transcript = Rc::new(Vec::new());
+ detail.images.clear();
+ detail.zoom = None;
detail.msg_list =
gpui::ListState::new(0, gpui::ListAlignment::Bottom, px(512.));
detail.error = Some(error.into());
@@ -4479,6 +4640,278 @@ impl Workbench {
})
}
+ fn show_image_action_success(
+ &mut self,
+ target: (usize, usize),
+ action: ImageAction,
+ cx: &mut Context<Self>,
+ ) {
+ self.image_action_feedback_generation =
+ self.image_action_feedback_generation.wrapping_add(1);
+ let feedback = ImageActionFeedback {
+ target,
+ action,
+ generation: self.image_action_feedback_generation,
+ };
+ let Some(detail) = &mut self.detail else {
+ return;
+ };
+ if detail.zoom != Some(target) {
+ return;
+ }
+ detail.image_action_feedback[action.index()] = Some(feedback);
+ cx.notify();
+
+ cx.spawn(async move |this, cx| {
+ cx.background_executor()
+ .timer(std::time::Duration::from_millis(1_600))
+ .await;
+ this.update(cx, |this, cx| {
+ if let Some(detail) = &mut this.detail {
+ if detail.image_action_feedback[action.index()] == Some(feedback) {
+ detail.image_action_feedback[action.index()] = None;
+ cx.notify();
+ }
+ }
+ })
+ .ok();
+ })
+ .detach();
+ }
+
+ fn render_image_zoom(&mut self, window: &Window, cx: &mut Context<Self>) -> AnyElement {
+ let Some((message_index, image_index)) =
+ self.detail.as_ref().and_then(|detail| detail.zoom)
+ else {
+ return div().into_any_element();
+ };
+ let Some(ImageSlot::Ready { image, dims, .. }) = self
+ .detail
+ .as_ref()
+ .and_then(|detail| detail.images.get(message_index))
+ .and_then(|images| images.get(image_index))
+ .cloned()
+ else {
+ return div().into_any_element();
+ };
+
+ let kind = image
+ .format
+ .mime_type()
+ .trim_start_matches("image/")
+ .to_ascii_uppercase();
+ let size = crate::format::human_bytes(image.bytes.len());
+ let metadata = match dims {
+ Some((width, height)) => format!("{kind} · {width} × {height} · {size}"),
+ None => format!("{kind} · {size}"),
+ };
+ let shown = zoom_fit(dims, window.viewport_size());
+ let feedback = self
+ .detail
+ .as_ref()
+ .map(|detail| detail.image_action_feedback)
+ .unwrap_or([None; 2]);
+ let target = (message_index, image_index);
+ let copy_succeeded =
+ feedback[ImageAction::Copy.index()].is_some_and(|feedback| feedback.target == target);
+ let save_succeeded =
+ feedback[ImageAction::Save.index()].is_some_and(|feedback| feedback.target == target);
+ let success = cx.theme().success;
+
+ let close_backdrop = cx.listener(|this, _, _, cx| {
+ if let Some(detail) = &mut this.detail {
+ detail.zoom = None;
+ detail.image_action_feedback = [None; 2];
+ cx.notify();
+ }
+ });
+ let close_button = cx.listener(|this, _, _, cx| {
+ if let Some(detail) = &mut this.detail {
+ detail.zoom = None;
+ detail.image_action_feedback = [None; 2];
+ cx.notify();
+ }
+ });
+ let copy_image = image.clone();
+ let save_image = image.clone();
+ let copy_action = cx.listener(move |this, _, window, cx| {
+ cx.stop_propagation();
+ cx.write_to_clipboard(ClipboardItem::new_image(&copy_image));
+ this.show_image_action_success(target, ImageAction::Copy, cx);
+ window.push_notification(Notification::success("Image copied"), cx);
+ });
+ let save_action = cx.listener(move |this, _, window, cx| {
+ cx.stop_propagation();
+ match save_image_to_downloads(&save_image.bytes, save_image.format.mime_type()) {
+ Some(path) => {
+ this.show_image_action_success(target, ImageAction::Save, cx);
+ window.push_notification(
+ Notification::success(format!("Saved to {}", path.display())),
+ cx,
+ );
+ }
+ None => {
+ window.push_notification(Notification::error("Couldn't save the image"), cx)
+ }
+ }
+ });
+
+ div()
+ .id("image-zoom")
+ .absolute()
+ .inset_0()
+ .occlude()
+ .bg(gpui::black().opacity(IMAGE_SCRIM))
+ .on_click(close_backdrop)
+ .child(
+ v_flex()
+ .size_full()
+ .items_center()
+ .justify_center()
+ .gap(SPACE_XL)
+ .px(px(56.))
+ .py(px(52.))
+ .child(
+ gpui::img(image)
+ .id("image-zoom-figure")
+ .w(shown.width)
+ .h(shown.height)
+ .rounded(SPACE_SM)
+ .shadow(vec![zoom_shadow(px(18.), px(48.), 0.45)])
+ .on_click(|_, _, cx| cx.stop_propagation()),
+ )
+ .child(
+ h_flex()
+ .id("image-zoom-actions")
+ .flex_shrink_0()
+ .h(px(40.))
+ .items_center()
+ .gap(SPACE_SM)
+ .pl(SPACE_LG)
+ .pr(SPACE_SM)
+ .rounded(px(20.))
+ .bg(gpui::black().opacity(0.78))
+ .border_1()
+ .border_color(gpui::white().opacity(0.13))
+ .shadow(vec![zoom_shadow(px(8.), px(26.), 0.35)])
+ .on_click(|_, _, cx| cx.stop_propagation())
+ .child(
+ div()
+ .text_size(FONT_LABEL)
+ .text_color(gpui::white().opacity(0.64))
+ .child(metadata),
+ )
+ .child(div().w(px(1.)).h(px(16.)).bg(gpui::white().opacity(0.16)))
+ .child(
+ h_flex()
+ .id("image-copy")
+ .size(px(30.))
+ .items_center()
+ .justify_center()
+ .rounded(RADIUS_BUTTON)
+ .cursor_pointer()
+ .when(copy_succeeded, |style| style.bg(success.opacity(0.2)))
+ .hover(move |style| {
+ style.bg(if copy_succeeded {
+ success.opacity(0.3)
+ } else {
+ gpui::white().opacity(0.16)
+ })
+ })
+ .tooltip(move |window, cx| {
+ gpui_component::tooltip::Tooltip::new(if copy_succeeded {
+ "Copied"
+ } else {
+ "Copy image"
+ })
+ .build(window, cx)
+ })
+ .on_click(copy_action)
+ .child(
+ icon(if copy_succeeded {
+ "icons/check.svg"
+ } else {
+ "icons/copy.svg"
+ })
+ .with_size(px(15.))
+ .text_color(
+ if copy_succeeded {
+ success
+ } else {
+ gpui::white().opacity(0.84)
+ },
+ ),
+ ),
+ )
+ .child(
+ h_flex()
+ .id("image-save")
+ .size(px(30.))
+ .items_center()
+ .justify_center()
+ .rounded(RADIUS_BUTTON)
+ .cursor_pointer()
+ .when(save_succeeded, |style| style.bg(success.opacity(0.2)))
+ .hover(move |style| {
+ style.bg(if save_succeeded {
+ success.opacity(0.3)
+ } else {
+ gpui::white().opacity(0.16)
+ })
+ })
+ .tooltip(move |window, cx| {
+ gpui_component::tooltip::Tooltip::new(if save_succeeded {
+ "Saved"
+ } else {
+ "Save to Downloads"
+ })
+ .build(window, cx)
+ })
+ .on_click(save_action)
+ .child(
+ icon(if save_succeeded {
+ "icons/check.svg"
+ } else {
+ "icons/download.svg"
+ })
+ .with_size(px(15.))
+ .text_color(
+ if save_succeeded {
+ success
+ } else {
+ gpui::white().opacity(0.84)
+ },
+ ),
+ ),
+ ),
+ ),
+ )
+ .child(
+ h_flex()
+ .id("image-zoom-close")
+ .absolute()
+ .top(SPACE_LG)
+ .right(SPACE_LG)
+ .size(px(32.))
+ .items_center()
+ .justify_center()
+ .rounded(px(9.))
+ .bg(gpui::white().opacity(0.11))
+ .hover(|style| style.bg(gpui::white().opacity(0.2)))
+ .cursor_pointer()
+ .tooltip(|window, cx| {
+ gpui_component::tooltip::Tooltip::new("Close").build(window, cx)
+ })
+ .on_click(close_button)
+ .child(
+ icon("icons/x.svg")
+ .with_size(px(15.))
+ .text_color(gpui::white().opacity(0.86)),
+ ),
+ )
+ .into_any_element()
+ }
+
// ---------- 对话区逐消息渲染(设计语言:用户右气泡 / 助手平铺 / 工具折叠簇) ----------
/// gpui::list 的行渲染。在布局阶段经 entity.update 调用(render 已返回,
@@ -4502,6 +4935,9 @@ impl Workbench {
// 尾部要用的 Copy 值提前取出,theme 借用不跨越 inner 构建期的 &mut cx
let jump_bg = theme.primary.opacity(0.09);
let jump_radius = theme.radius;
+ let image_border = theme.border;
+ let image_panel = theme.popover;
+ let image_muted = theme.muted_foreground;
let Some(detail) = &self.detail else {
return div().into_any_element();
};
@@ -4511,6 +4947,7 @@ impl Workbench {
let jump_seq = detail.jump_seq;
// Rc 克隆只加引用计数;逐行借用,避免每帧深拷贝整条消息(text 可达 32KB)
let transcript = detail.transcript.clone();
+ let shots = detail.images.get(ix).cloned().unwrap_or_default();
let Some(m) = transcript.get(ix) else {
return div().into_any_element();
};
@@ -4520,6 +4957,7 @@ impl Workbench {
&& m.text.is_empty()
&& m.tool_calls.is_empty()
&& m.thinking.is_some()
+ && shots.is_empty()
{
return div().into_any_element();
}
@@ -4530,30 +4968,44 @@ impl Workbench {
centered_pill("Context compacted", cx).into_any_element()
} else {
match m.role {
- MessageRole::User => h_flex()
- .w_full()
- .justify_end()
- .child(
- div()
- .max_w(px(540.))
- .min_w_0()
- .rounded(theme.radius_lg)
- .bg(theme.muted)
- .px(px(14.))
- .py(SPACE_SM)
- .text_size(FONT_MSG_USER)
- .line_height(relative(1.85))
- .child(markdown_body(
- format!("dmsg-{}", m.seq).into(),
- m.text.clone(),
- FONT_MSG_USER,
- gpui::rems(0.5),
- dark,
- window,
- cx,
- )),
- )
- .into_any_element(),
+ MessageRole::User => {
+ let has_text = !m.text.trim().is_empty();
+ let mut bubble = v_flex()
+ .max_w(px(540.))
+ .min_w_0()
+ .gap(SPACE_SM)
+ .rounded(theme.radius_lg)
+ .bg(theme.muted)
+ .text_size(FONT_MSG_USER)
+ .line_height(relative(1.85));
+ bubble = if !shots.is_empty() && !has_text {
+ bubble.p(px(7.))
+ } else {
+ bubble.px(px(14.)).py(SPACE_SM)
+ };
+ if has_text || !shots.is_empty() {
+ bubble = bubble.child(message_content(
+ ix,
+ m.seq,
+ &m.text,
+ &shots,
+ FONT_MSG_USER,
+ gpui::rems(0.5),
+ dark,
+ image_border,
+ image_panel,
+ image_muted,
+ cx.entity(),
+ window,
+ cx,
+ ));
+ }
+ h_flex()
+ .w_full()
+ .justify_end()
+ .child(bubble)
+ .into_any_element()
+ }
MessageRole::Assistant => {
let mut col = v_flex().w_full().min_w_0().gap(SPACE_SM);
if let Some(thinking) = &m.thinking {
@@ -4571,17 +5023,23 @@ impl Workbench {
cx,
));
}
- if !m.text.is_empty() {
+ if !m.text.is_empty() || !shots.is_empty() {
col = col.child(
div()
.text_size(FONT_MSG_BODY)
.line_height(relative(1.9))
- .child(markdown_body(
- format!("dmsg-{}", m.seq).into(),
- m.text.clone(),
+ .child(message_content(
+ ix,
+ m.seq,
+ &m.text,
+ &shots,
FONT_MSG_BODY,
gpui::rems(0.6),
dark,
+ image_border,
+ image_panel,
+ image_muted,
+ cx.entity(),
window,
cx,
)),
@@ -6107,6 +6565,369 @@ fn empty_state(
.child(div().text_size(FONT_CAPTION).child(caption.into()))
}
+fn image_format_of(media_type: &str) -> Option<gpui::ImageFormat> {
+ use gpui::ImageFormat as Format;
+ Some(match media_type.trim().to_ascii_lowercase().as_str() {
+ "image/png" => Format::Png,
+ "image/jpeg" | "image/jpg" => Format::Jpeg,
+ "image/webp" => Format::Webp,
+ "image/gif" => Format::Gif,
+ // SVG 的画布与滤镜开销无法在 GPUI 解码前可靠约束,保留原始下载即可。
+ "image/svg+xml" => return None,
+ "image/bmp" => Format::Bmp,
+ "image/tiff" | "image/tif" => Format::Tiff,
+ _ => return None,
+ })
+}
+
+#[derive(Clone, Copy)]
+struct ImageProbe {
+ dims: (u32, u32),
+ decoded_pixels: u64,
+}
+
+fn image_probe(bytes: &[u8], format: gpui::ImageFormat) -> Option<ImageProbe> {
+ use image::AnimationDecoder as _;
+
+ const MAX_DIMENSION: u32 = 8_192;
+ const MAX_STATIC_PIXELS: u64 = 16_000_000;
+ const MAX_ANIMATED_PIXELS: u64 = 32_000_000;
+ const MAX_ANIMATION_FRAMES: usize = 120;
+
+ let reader = image::ImageReader::new(std::io::Cursor::new(bytes))
+ .with_guessed_format()
+ .ok()?;
+ let guessed = reader.format()?;
+ let expected = match format {
+ gpui::ImageFormat::Png => image::ImageFormat::Png,
+ gpui::ImageFormat::Jpeg => image::ImageFormat::Jpeg,
+ gpui::ImageFormat::Gif => image::ImageFormat::Gif,
+ gpui::ImageFormat::Webp => image::ImageFormat::WebP,
+ gpui::ImageFormat::Bmp => image::ImageFormat::Bmp,
+ gpui::ImageFormat::Tiff => image::ImageFormat::Tiff,
+ gpui::ImageFormat::Svg | gpui::ImageFormat::Ico | gpui::ImageFormat::Pnm => return None,
+ };
+ if guessed != expected {
+ return None;
+ }
+ let dims = reader.into_dimensions().ok()?;
+ if dims.0 == 0 || dims.1 == 0 || dims.0 > MAX_DIMENSION || dims.1 > MAX_DIMENSION {
+ return None;
+ }
+ let pixels = u64::from(dims.0) * u64::from(dims.1);
+ if pixels > MAX_STATIC_PIXELS {
+ return None;
+ }
+
+ let frame_count = match format {
+ gpui::ImageFormat::Gif => {
+ let decoder = image::codecs::gif::GifDecoder::new(std::io::Cursor::new(bytes)).ok()?;
+ bounded_animation_frames(decoder.into_frames(), pixels, MAX_ANIMATED_PIXELS)?
+ }
+ gpui::ImageFormat::Webp => {
+ let decoder =
+ image::codecs::webp::WebPDecoder::new(std::io::Cursor::new(bytes)).ok()?;
+ if decoder.has_animation() {
+ bounded_animation_frames(decoder.into_frames(), pixels, MAX_ANIMATED_PIXELS)?
+ } else {
+ 1
+ }
+ }
+ _ => 1,
+ };
+ if frame_count > MAX_ANIMATION_FRAMES {
+ return None;
+ }
+ Some(ImageProbe {
+ dims,
+ decoded_pixels: pixels.checked_mul(frame_count as u64)?,
+ })
+}
+
+fn bounded_animation_frames<'a>(
+ frames: image::Frames<'a>,
+ pixels_per_frame: u64,
+ max_pixels: u64,
+) -> Option<usize> {
+ let limit = (max_pixels / pixels_per_frame).clamp(1, 120) as usize;
+ let mut count = 0usize;
+ for frame in frames.take(limit + 1) {
+ frame.ok()?;
+ count += 1;
+ if count > limit {
+ return None;
+ }
+ }
+ (count > 0).then_some(count)
+}
+
+fn image_extension(media_type: &str) -> &'static str {
+ match media_type.trim().to_ascii_lowercase().as_str() {
+ "image/jpeg" | "image/jpg" => "jpg",
+ "image/webp" => "webp",
+ "image/gif" => "gif",
+ "image/svg+xml" => "svg",
+ "image/bmp" => "bmp",
+ "image/tiff" | "image/tif" => "tiff",
+ "image/heic" | "image/heif" => "heic",
+ "image/avif" => "avif",
+ _ => "png",
+ }
+}
+
+fn save_image_to_downloads(bytes: &[u8], media_type: &str) -> Option<PathBuf> {
+ use std::hash::{Hash as _, Hasher as _};
+ use std::io::Write as _;
+
+ let mut hasher = std::collections::hash_map::DefaultHasher::new();
+ bytes.hash(&mut hasher);
+ let stem = format!("wake-image-{:016x}", hasher.finish());
+ let extension = image_extension(media_type);
+ let downloads = dirs::download_dir()?;
+ for suffix in 0..1000 {
+ let name = if suffix == 0 {
+ format!("{stem}.{extension}")
+ } else {
+ format!("{stem}-{suffix}.{extension}")
+ };
+ let path = downloads.join(name);
+ match std::fs::OpenOptions::new()
+ .write(true)
+ .create_new(true)
+ .open(&path)
+ {
+ Ok(mut file) => {
+ if file.write_all(bytes).is_ok() {
+ return Some(path);
+ }
+ let _ = std::fs::remove_file(&path);
+ return None;
+ }
+ Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
+ if std::fs::read(&path).ok().as_deref() == Some(bytes) {
+ return Some(path);
+ }
+ }
+ Err(_) => return None,
+ }
+ }
+ None
+}
+
+fn zoom_fit(dims: Option<(u32, u32)>, viewport: Size<Pixels>) -> Size<Pixels> {
+ let available_width = (f32::from(viewport.width) - 112.0).max(120.0);
+ let available_height = (f32::from(viewport.height) - 164.0).max(120.0);
+ let Some((width, height)) = dims.filter(|(width, height)| *width > 0 && *height > 0) else {
+ return gpui::size(
+ px(available_width.min(720.0)),
+ px(available_height.min(480.0)),
+ );
+ };
+ let (width, height) = (width as f32, height as f32);
+ let scale = (available_width / width)
+ .min(available_height / height)
+ .min(1.0);
+ gpui::size(px(width * scale), px(height * scale))
+}
+
+fn zoom_shadow(y: Pixels, blur: Pixels, alpha: f32) -> gpui::BoxShadow {
+ gpui::BoxShadow {
+ color: gpui::black().opacity(alpha),
+ offset: gpui::point(px(0.), y),
+ blur_radius: blur,
+ spread_radius: px(0.),
+ inset: false,
+ }
+}
+
+#[allow(clippy::too_many_arguments)]
+fn message_content(
+ message_index: usize,
+ seq: i64,
+ text: &str,
+ slots: &[ImageSlot],
+ base: Pixels,
+ paragraph_gap: gpui::Rems,
+ dark: bool,
+ border: Hsla,
+ panel: Hsla,
+ muted: Hsla,
+ workbench: Entity<Workbench>,
+ window: &mut Window,
+ cx: &mut App,
+) -> Div {
+ let mut content = v_flex().min_w_0().gap(SPACE_SM);
+ let mut cursor = 0usize;
+ let mut image_index = 0usize;
+ let mut text_part = 0usize;
+
+ while image_index < slots.len() {
+ let mut offset = slots[image_index].text_offset().min(text.len());
+ while offset > cursor && !text.is_char_boundary(offset) {
+ offset -= 1;
+ }
+ offset = offset.max(cursor);
+ let segment = text[cursor..offset].trim_matches('\n');
+ if !segment.trim().is_empty() {
+ content = content.child(markdown_body(
+ format!("dmsg-{seq}-part-{text_part}").into(),
+ segment.to_string(),
+ base,
+ paragraph_gap,
+ dark,
+ window,
+ cx,
+ ));
+ text_part += 1;
+ }
+
+ let group_start = image_index;
+ image_index += 1;
+ while image_index < slots.len()
+ && slots[image_index].text_offset().min(text.len())
+ == slots[group_start].text_offset().min(text.len())
+ {
+ image_index += 1;
+ }
+ content = content.child(image_strip(
+ message_index,
+ group_start,
+ &slots[group_start..image_index],
+ border,
+ panel,
+ muted,
+ workbench.clone(),
+ ));
+ cursor = offset;
+ }
+
+ let tail = text[cursor..].trim_matches('\n');
+ if !tail.trim().is_empty() {
+ content = content.child(markdown_body(
+ format!("dmsg-{seq}-part-{text_part}").into(),
+ tail.to_string(),
+ base,
+ paragraph_gap,
+ dark,
+ window,
+ cx,
+ ));
+ }
+ content
+}
+
+fn image_strip(
+ message_index: usize,
+ image_index_start: usize,
+ slots: &[ImageSlot],
+ border: Hsla,
+ panel: Hsla,
+ muted: Hsla,
+ workbench: Entity<Workbench>,
+) -> Div {
+ let mut row = h_flex().flex_wrap().gap(SPACE_SM);
+ for (local_index, slot) in slots.iter().enumerate() {
+ let image_index = image_index_start + local_index;
+ row = row.child(match slot {
+ ImageSlot::Ready { image, dims, .. } => {
+ let entity = workbench.clone();
+ let figure = gpui::img(image.clone());
+ let figure = if dims.is_some_and(|(width, height)| width < height) {
+ figure.w(IMAGE_THUMB)
+ } else {
+ figure.h(IMAGE_THUMB)
+ };
+ div()
+ .id(SharedString::from(format!(
+ "message-image:{message_index}:{image_index}"
+ )))
+ .size(IMAGE_THUMB)
+ .flex()
+ .items_center()
+ .justify_center()
+ .overflow_hidden()
+ .rounded(RADIUS_IMAGE)
+ .border_1()
+ .border_color(border)
+ .bg(panel)
+ .cursor_pointer()
+ .child(figure)
+ .on_click(move |_, _, cx| {
+ entity.update(cx, |this, cx| {
+ if let Some(detail) = &mut this.detail {
+ detail.zoom = Some((message_index, image_index));
+ detail.image_action_feedback = [None; 2];
+ cx.notify();
+ }
+ });
+ })
+ .into_any_element()
+ }
+ ImageSlot::Unsupported {
+ media_type, bytes, ..
+ } => {
+ let media_type_for_save = media_type.to_string();
+ let bytes_for_save = bytes.clone();
+ v_flex()
+ .id(SharedString::from(format!(
+ "unsupported-message-image:{message_index}:{image_index}"
+ )))
+ .size(IMAGE_THUMB)
+ .items_center()
+ .justify_center()
+ .gap(SPACE_XS)
+ .p(SPACE_SM)
+ .rounded(RADIUS_IMAGE)
+ .border_1()
+ .border_color(border)
+ .bg(panel)
+ .text_size(FONT_LABEL)
+ .text_color(muted)
+ .cursor_pointer()
+ .tooltip(|window, cx| {
+ gpui_component::tooltip::Tooltip::new("Save original image")
+ .build(window, cx)
+ })
+ .on_click(move |_, window, cx| {
+ match save_image_to_downloads(&bytes_for_save, &media_type_for_save) {
+ Some(path) => window.push_notification(
+ Notification::success(format!("Saved to {}", path.display())),
+ cx,
+ ),
+ None => window.push_notification(
+ Notification::error("Couldn't save the image"),
+ cx,
+ ),
+ }
+ })
+ .child("Preview unavailable")
+ .child(div().max_w_full().truncate().child(media_type.clone()))
+ .child(icon("icons/download.svg").with_size(px(14.)))
+ .into_any_element()
+ }
+ ImageSlot::Omitted { .. } => v_flex()
+ .id(SharedString::from(format!(
+ "omitted-message-image:{message_index}:{image_index}"
+ )))
+ .size(IMAGE_THUMB)
+ .items_center()
+ .justify_center()
+ .gap(SPACE_XS)
+ .p(SPACE_SM)
+ .rounded(RADIUS_IMAGE)
+ .border_1()
+ .border_color(border)
+ .bg(panel)
+ .text_size(FONT_LABEL)
+ .text_color(muted)
+ .child("Image omitted")
+ .child("Transcript limit reached")
+ .into_any_element(),
+ });
+ }
+ row
+}
+
/// 对话区居中小胶囊(System 消息 / Context compacted)
fn centered_pill(text: impl Into<SharedString>, cx: &App) -> Div {
let theme = cx.theme();
@@ -6808,6 +7629,7 @@ impl Render for Workbench {
}
}),
)
+ .child(self.render_image_zoom(window, cx))
.children(Root::render_dialog_layer(window, cx))
.children(Root::render_notification_layer(window, cx))
}