Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

fix: chat animation, thinking tokens, history save, contentBlocks race

Louis Beaumontdc32c99c9f15
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
index 68f5891eb..705a0b059 100644
--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx
+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
@@ -137,7 +137,8 @@ interface ToolCall {
type ContentBlock =
| { type: "text"; text: string }
- | { type: "tool"; toolCall: ToolCall };
+ | { type: "tool"; toolCall: ToolCall }
+ | { type: "thinking"; text: string; isThinking: boolean; durationMs?: number };
interface Message {
id: string;
@@ -271,6 +272,42 @@ function ToolCallBlock({ toolCall }: { toolCall: ToolCall }) {
);
}
+function ThinkingBlock({ text, isThinking, durationMs }: { text: string; isThinking: boolean; durationMs?: number }) {
+ const [expanded, setExpanded] = useState(false);
+ const [elapsed, setElapsed] = useState(0);
+ const startRef = useRef(Date.now());
+
+ useEffect(() => {
+ if (!isThinking) return;
+ const id = window.setInterval(() => setElapsed(Math.floor((Date.now() - startRef.current) / 1000)), 1000);
+ return () => window.clearInterval(id);
+ }, [isThinking]);
+
+ const seconds = isThinking ? elapsed : durationMs ? Math.round(durationMs / 1000) : 0;
+
+ return (
+ <div className="rounded-lg border border-border/30 bg-muted/20 text-xs overflow-hidden">
+ <button
+ onClick={() => setExpanded(!expanded)}
+ className="w-full flex items-center gap-2 px-3 py-2 hover:bg-muted/40 transition-colors text-left"
+ >
+ <div className={cn("h-2 w-2 rounded-full", isThinking ? "bg-foreground/60 animate-pulse" : "bg-foreground/30")} />
+ <span className="font-mono text-muted-foreground">
+ {isThinking ? `thinking... (${seconds}s)` : `thought for ${seconds}s`}
+ </span>
+ <span className="ml-auto text-muted-foreground">{expanded ? "▾" : "▸"}</span>
+ </button>
+ {expanded && text.trim() && (
+ <div className="px-3 py-2 border-t border-border/30">
+ <div className="pl-3 border-l-2 border-border/40 text-muted-foreground font-mono whitespace-pre-wrap break-words max-h-[300px] overflow-y-auto text-[11px] leading-relaxed">
+ {text}
+ </div>
+ </div>
+ )}
+ </div>
+ );
+}
+
// Markdown renderer for text blocks
function MarkdownBlock({ text, isUser }: { text: string; isUser: boolean }) {
return (
@@ -394,6 +431,9 @@ function MessageContent({ message }: { message: Message }) {
if (block.type === "tool") {
return <ToolCallBlock key={block.toolCall.id} toolCall={block.toolCall} />;
}
+ if (block.type === "thinking") {
+ return <ThinkingBlock key={`thinking-${i}`} text={block.text} isThinking={block.isThinking} durationMs={block.durationMs} />;
+ }
return null;
})}
</>
@@ -444,6 +484,7 @@ export function StandaloneChat() {
const piStartInFlightRef = useRef(false);
const piRestartCountRef = useRef(0);
const piStoppedIntentionallyRef = useRef(false);
+ const piThinkingStartRef = useRef<number | null>(null);
// Chat history state
const [conversationId, setConversationId] = useState<string | null>(null);
@@ -1121,6 +1162,46 @@ export function StandaloneChat() {
prev.map((m) => m.id === msgId ? { ...m, content, contentBlocks } : m)
);
}
+ } else if (evt.type === "thinking_start") {
+ piThinkingStartRef.current = Date.now();
+ const blocks = piContentBlocksRef.current;
+ blocks.push({ type: "thinking", text: "", isThinking: true });
+ if (piMessageIdRef.current) {
+ const msgId = piMessageIdRef.current;
+ const contentBlocks = [...blocks];
+ setMessages((prev) =>
+ prev.map((m) => m.id === msgId ? { ...m, content: m.content === "Processing..." ? "" : m.content, contentBlocks } : m)
+ );
+ }
+ } else if (evt.type === "thinking_delta" && evt.delta) {
+ const blocks = piContentBlocksRef.current;
+ const thinkingBlock = blocks[blocks.length - 1];
+ if (thinkingBlock && thinkingBlock.type === "thinking") {
+ thinkingBlock.text += evt.delta;
+ }
+ if (piMessageIdRef.current) {
+ const msgId = piMessageIdRef.current;
+ const contentBlocks = [...blocks];
+ setMessages((prev) =>
+ prev.map((m) => m.id === msgId ? { ...m, content: m.content === "Processing..." ? "" : m.content, contentBlocks } : m)
+ );
+ }
+ } else if (evt.type === "thinking_end") {
+ const blocks = piContentBlocksRef.current;
+ const thinkingBlock = blocks[blocks.length - 1];
+ if (thinkingBlock && thinkingBlock.type === "thinking") {
+ thinkingBlock.isThinking = false;
+ thinkingBlock.durationMs = piThinkingStartRef.current ? Date.now() - piThinkingStartRef.current : undefined;
+ if (evt.content) thinkingBlock.text = evt.content;
+ }
+ piThinkingStartRef.current = null;
+ if (piMessageIdRef.current) {
+ const msgId = piMessageIdRef.current;
+ const contentBlocks = [...blocks];
+ setMessages((prev) =>
+ prev.map((m) => m.id === msgId ? { ...m, contentBlocks } : m)
+ );
+ }
}
} else if (data.type === "tool_execution_start") {
if (piMessageIdRef.current) {
@@ -1236,6 +1317,11 @@ export function StandaloneChat() {
.map((c: any) => c.text))
.join("\n\n");
}
+ // Snapshot refs BEFORE setMessages — React's batching may defer the
+ // functional updater until after the refs are cleared below.
+ const blocksSnapshot = [...piContentBlocksRef.current];
+ const streamedText = piStreamingTextRef.current;
+
// Check if content was already set by error handlers above
setMessages((prev) => {
const existing = prev.find((m) => m.id === msgId);
@@ -1250,15 +1336,17 @@ export function StandaloneChat() {
if (existing && existing.content !== "Processing..." && !content) {
return prev;
}
- const contentBlocks = [...piContentBlocksRef.current];
- // If no text content but we have tool calls, don't show "Done" — the tool blocks speak for themselves
- const hasToolBlocks = contentBlocks.some((b) => b.type === "tool");
- if (!content && hasToolBlocks) {
- content = ""; // empty — tool blocks will render
+ const contentBlocks = [...blocksSnapshot];
+ // If no text content but we have tool/thinking blocks, don't show "no response"
+ const hasNonTextBlocks = contentBlocks.some((b) => b.type === "tool" || b.type === "thinking");
+ if (!content && hasNonTextBlocks) {
+ content = ""; // empty — tool/thinking blocks will render
} else if (!content) {
content = "no response from model — try a different prompt or model";
}
- if (!piStreamingTextRef.current && content && contentBlocks.length === 0) {
+ // Add text as a content block if no text block exists yet
+ const hasTextBlock = contentBlocks.some((b) => b.type === "text");
+ if (!streamedText && content && !hasTextBlock) {
contentBlocks.push({ type: "text", text: content });
}
return prev.map((m) => m.id === msgId ? { ...m, content, contentBlocks } : m);
@@ -1267,6 +1355,7 @@ export function StandaloneChat() {
piStreamingTextRef.current = "";
piMessageIdRef.current = null;
piContentBlocksRef.current = [];
+ piThinkingStartRef.current = null;
setIsLoading(false);
setIsStreaming(false);
} else if (data.type === "response" && data.success === false) {