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: Pi chat 'Done' stuck and 'Agent already processing' errors

Louis Beaumont8165235493f3
diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
index 3590ad9ca..b3e246f29 100644
--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx
+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx
@@ -499,7 +499,20 @@ export function StandaloneChat() {
};
// Load a specific conversation
- const loadConversation = (conv: ChatConversation) => {
+ const loadConversation = async (conv: ChatConversation) => {
+ // Abort any ongoing Pi processing before switching
+ if (isPi && (isLoading || isStreaming)) {
+ try {
+ await commands.piAbort();
+ } catch (e) {
+ console.warn("[Pi] Failed to abort:", e);
+ }
+ piStreamingTextRef.current = "";
+ piMessageIdRef.current = null;
+ piContentBlocksRef.current = [];
+ setIsLoading(false);
+ setIsStreaming(false);
+ }
setMessages(conv.messages.map(m => ({
id: m.id,
role: m.role,
@@ -520,7 +533,23 @@ export function StandaloneChat() {
};
// Start a new conversation
- const startNewConversation = () => {
+ const startNewConversation = async () => {
+ // Abort any ongoing Pi processing and start a fresh session
+ if (isPi && piInfo?.running) {
+ try {
+ if (isLoading || isStreaming) {
+ await commands.piAbort();
+ }
+ await commands.piNewSession();
+ } catch (e) {
+ console.warn("[Pi] Failed to reset session:", e);
+ }
+ piStreamingTextRef.current = "";
+ piMessageIdRef.current = null;
+ piContentBlocksRef.current = [];
+ setIsLoading(false);
+ setIsStreaming(false);
+ }
setMessages([]);
setConversationId(null);
setInput("");
@@ -1029,8 +1058,23 @@ export function StandaloneChat() {
piRestartCountRef.current = 0;
if (piMessageIdRef.current) {
const msgId = piMessageIdRef.current;
- const content = piStreamingTextRef.current || "Done";
+ // Use streamed text if available, otherwise extract from agent_end messages
+ let content = piStreamingTextRef.current;
+ if (!content && data.messages && Array.isArray(data.messages)) {
+ // Extract text from all assistant messages in the agent_end payload
+ content = data.messages
+ .filter((m: any) => m.role === "assistant")
+ .flatMap((m: any) => (m.content || [])
+ .filter((c: any) => c.type === "text")
+ .map((c: any) => c.text))
+ .join("\n\n");
+ }
+ content = content || "Done";
const contentBlocks = [...piContentBlocksRef.current];
+ // If we had no streamed text but got content from messages, add it as a text block
+ if (!piStreamingTextRef.current && content !== "Done" && contentBlocks.length === 0) {
+ contentBlocks.push({ type: "text", text: content });
+ }
setMessages((prev) =>
prev.map((m) => m.id === msgId ? { ...m, content, contentBlocks } : m)
);
@@ -1644,12 +1688,22 @@ export function StandaloneChat() {
sendMessage(input.trim());
};
- const handleStop = () => {
+ const handleStop = async () => {
+ if (isPi) {
+ try {
+ await commands.piAbort();
+ } catch (e) {
+ console.warn("[Pi] Failed to abort:", e);
+ }
+ piStreamingTextRef.current = "";
+ piMessageIdRef.current = null;
+ piContentBlocksRef.current = [];
+ }
if (abortControllerRef.current) {
abortControllerRef.current.abort();
- setIsLoading(false);
- setIsStreaming(false);
}
+ setIsLoading(false);
+ setIsStreaming(false);
};
return (
diff --git a/apps/screenpipe-app-tauri/lib/utils/tauri.ts b/apps/screenpipe-app-tauri/lib/utils/tauri.ts
index 52fa28a0f..36b1464c6 100644
--- a/apps/screenpipe-app-tauri/lib/utils/tauri.ts
+++ b/apps/screenpipe-app-tauri/lib/utils/tauri.ts
@@ -435,6 +435,14 @@ async piAbort() : Promise<Result<null, string>> {
else return { status: "error", error: e as any };
}
},
+async piNewSession() : Promise<Result<null, string>> {
+ try {
+ return { status: "ok", data: await TAURI_INVOKE("pi_new_session") };
+} catch (e) {
+ if(e instanceof Error) throw e;
+ else return { status: "error", error: e as any };
+}
+},
/**
* Save obsidian settings to persistent store (called when settings change)
*/
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/main.rs b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
index d37f9ff41..8223312f1 100755
--- a/apps/screenpipe-app-tauri/src-tauri/src/main.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/main.rs
@@ -1012,6 +1012,7 @@ async fn main() {
pi::pi_install,
pi::pi_prompt,
pi::pi_abort,
+ pi::pi_new_session,
// Obsidian Sync commands
obsidian_sync::obsidian_save_settings,
obsidian_sync::obsidian_validate_vault,
@@ -1160,6 +1161,7 @@ async fn main() {
pi::pi_install,
pi::pi_prompt,
pi::pi_abort,
+ pi::pi_new_session,
// Obsidian Sync commands
obsidian_sync::obsidian_save_settings,
obsidian_sync::obsidian_validate_vault,
diff --git a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
index c29f75ec6..4b7f5fe10 100644
--- a/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
+++ b/apps/screenpipe-app-tauri/src-tauri/src/pi.rs
@@ -569,6 +569,20 @@ pub async fn pi_abort(state: State<'_, PiState>) -> Result<(), String> {
m.send_command(json!({"type": "abort"}))
}
+/// Start a new Pi session (clears conversation history)
+#[tauri::command]
+#[specta::specta]
+pub async fn pi_new_session(state: State<'_, PiState>) -> Result<(), String> {
+ let mut manager = state.0.lock().await;
+ let m = manager.as_mut().ok_or("Pi not initialized")?;
+
+ if !m.is_running() {
+ return Err("Pi is not running".to_string());
+ }
+
+ m.send_command(json!({"type": "new_session"}))
+}
+
/// Check if pi is available
#[tauri::command]
#[specta::specta]