@@ -1,10 +1,14 @@
11 import { HStack, Text, TextInput, VStack } from "@cel-tui/core";
2-import { getModels, type ThinkingLevel } from "@earendil-works/pi-ai";
2+import {
3+ getModels,
4+ type Message,
5+ type ThinkingLevel,
6+} from "@earendil-works/pi-ai";
37 import { getOAuthProviders } from "@earendil-works/pi-ai/oauth";
48 import { saveSettings } from "./args";
59 import { getAvailableProviders } from "./oauth";
610 import { listSessionsForCwd } from "./session";
7-import { estimateTokens } from "./shared";
11+import { estimateTokens, formatTimestamp } from "./shared";
812 import { TextPill, theme } from "./tui-components";
913 import type { SelectOptions, SelectState, Session, TUIState } from "./types";
1014
@@ -302,6 +306,61 @@ export function mainMenu(state: TUIState) {
302306 state.overlay = false;
303307 };
304308
309+ const toTUIMessage = (msg: Message) => {
310+ const textFromContent = (
311+ content: string | { type: string; text?: string }[],
312+ ) =>
313+ typeof content === "string"
314+ ? content
315+ : content
316+ .filter((c) => c.type === "text")
317+ .map((c) => c.text ?? "")
318+ .join("")
319+ .trim();
320+
321+ if (msg.role === "user") {
322+ return {
323+ timestamp: formatTimestamp(msg.timestamp),
324+ role: "user" as const,
325+ text: textFromContent(msg.content)
326+ .replaceAll(/<system-reminder>[\s\S]*?<\/system-reminder>/g, "")
327+ .trim(),
328+ };
329+ } else {
330+ const text = msg.content
331+ .filter((c) => c.type === "text")
332+ .map((c) => c.text)
333+ .join("")
334+ .trim();
335+ const thinking = msg.content
336+ .filter((c) => c.type === "thinking")
337+ .map((c) => c.thinking)
338+ .join("")
339+ .trim();
340+ const toolCalls = msg.content
341+ .filter((c) => c.type === "toolCall")
342+ .map((c) => {
343+ const toolResult = state.messages.find(
344+ (m) => m.role === "toolResult" && m.toolCallId === c.id,
345+ );
346+ return {
347+ id: c.id,
348+ tool: c.name,
349+ args: c.arguments,
350+ output: toolResult ? textFromContent(toolResult.content) : "",
351+ };
352+ });
353+
354+ return {
355+ timestamp: formatTimestamp(msg.timestamp),
356+ role: "assistant" as const,
357+ text,
358+ thinking,
359+ toolCalls,
360+ };
361+ }
362+ };
363+
305364 const select = useSelectOverlay({
306365 ...mainPane,
307366 onSelect: (s) => {
@@ -331,6 +390,9 @@ export function mainMenu(state: TUIState) {
331390
332391 state.sessionId = session.id;
333392 state.messages = session.messages;
393+ state.tuiMessages = session.messages
394+ .filter((message) => message.role !== "toolResult")
395+ .map(toTUIMessage);
334396 state.prompt = "";
335397 state.contextSize = estimateTokens(JSON.stringify(state.messages));
336398 state.scrollOffset = 0;