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
Rust agentsagiaiai-memoryaudio-recordingcomputer-visionhermeshermes-agentllmlocal-ailocal-firstmachine-learningmcpmultimodalopenclawprivacyrewindscreen-recordingspeech-to-textycombinator
fix: deterministic Pi restart — no more useEffect/debounce race
Louis Beaumont8f8549154c98
diff --git a/apps/screenpipe-app-tauri/components/rewind/ai-presets-selector.tsx b/apps/screenpipe-app-tauri/components/rewind/ai-presets-selector.tsxindex 31d4c593f..82398c39b 100644--- a/apps/screenpipe-app-tauri/components/rewind/ai-presets-selector.tsx+++ b/apps/screenpipe-app-tauri/components/rewind/ai-presets-selector.tsx@@ -840,6 +840,8 @@ interface AIPresetsSelectorProps {recommendedPresets?: RecommendedPreset[];shortcutKey?: string;onPresetChange?: (preset: AIPreset) => void;+ /** Called when user saves a preset (explicit action) — use to restart Pi deterministically */+ onPresetSaved?: (preset: AIPreset) => void;showLoginCta?: boolean;/** Controlled mode: override which preset id is shown as selected */controlledPresetId?: string | null;@@ -924,6 +926,7 @@ export const AIPresetsSelector = ({recommendedPresets,shortcutKey = "/",onPresetChange,+ onPresetSaved,showLoginCta = true,controlledPresetId,onControlledSelect,@@ -1116,6 +1119,16 @@ export const AIPresetsSelector = ({});}+ // Notify parent to restart Pi if the saved preset is the active/default one+ if (onPresetSaved) {+ const savedFull = { ...preset } as AIPreset;+ const isDefault = selectedPresetToEdit?.defaultPreset ||+ (!selectedPresetToEdit && settings.aiPresets.length === 0);+ if (isDefault) {+ onPresetSaved(savedFull);+ }+ }+setDialogOpen(false);setSelectedPresetToEdit(undefined);};@@ -1147,6 +1160,11 @@ export const AIPresetsSelector = ({aiPresets: updatedPresets,});+ // Restart Pi with the new default preset+ if (onPresetSaved) {+ onPresetSaved(preset);+ }+toast.success("Default preset updated", {description: `${preset.id} is now the default preset`,});diff --git a/apps/screenpipe-app-tauri/components/standalone-chat.tsx b/apps/screenpipe-app-tauri/components/standalone-chat.tsxindex f74c09304..c8b64498e 100644--- a/apps/screenpipe-app-tauri/components/standalone-chat.tsx+++ b/apps/screenpipe-app-tauri/components/standalone-chat.tsx@@ -1575,19 +1575,20 @@ export function StandaloneChat({ className }: { className?: string } = {}) {// Pi project dir is managed Rust-side at boot// Build Pi provider config from active preset- const buildProviderConfig = useCallback(() => {- if (!activePreset) return null;+ const buildProviderConfig = useCallback((preset?: AIPreset | null) => {+ const p = preset || activePreset;+ if (!p) return null;// Combine the screenpipe search instructions with the user's preset prompt.// This is passed via --append-system-prompt to Pi, enabling Anthropic prompt// caching (90% input cost reduction on subsequent messages).- const presetPrompt = activePreset.prompt || "";+ const presetPrompt = p.prompt || "";const systemPrompt = `${buildSystemPrompt()}\n\n${presetPrompt}`.trim() || null;return {- provider: activePreset.provider,- url: activePreset.url || "",- model: activePreset.model || "",- apiKey: ("apiKey" in activePreset ? (activePreset.apiKey as string) : null) || null,- maxTokens: (activePreset as any).maxTokens ?? 4096,+ provider: p.provider,+ url: p.url || "",+ model: p.model || "",+ apiKey: ("apiKey" in p ? (p.apiKey as string) : null) || null,+ maxTokens: (p as any).maxTokens ?? 4096,systemPrompt,};// eslint-disable-next-line react-hooks/exhaustive-deps@@ -1618,54 +1619,17 @@ export function StandaloneChat({ className }: { className?: string } = {}) {return () => clearInterval(interval);}, []);- // Track what provider+model Pi is actually running with (survives remounts via ref)- const piRunningConfigRef = useRef<{ provider?: string; model?: string; token?: string | null }>({});- const piRestartTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);-- // Restart Pi when user switches preset so the new model takes effect immediately.- // Pi uses CLI args from startup, so config-only updates don't change the running model.- // Debounced to collapse rapid settings saves into a single restart.- useEffect(() => {- if (!activePreset || !isSettingsLoaded) return;- const running = piRunningConfigRef.current;- const currentToken = settings.user?.token ?? null;- const configChanged = running.provider !== undefined &&- (running.provider !== activePreset.provider || running.model !== activePreset.model);- // Only treat token change as meaningful if it's a real switch (not initial load from null)- const tokenChanged = running.token !== undefined && running.token !== currentToken- && running.token !== null; // null → real token = initial load, not a user action-- if (!configChanged && !tokenChanged) {- // First mount or same config — just record what we expect Pi to be running- if (running.provider === undefined) {- piRunningConfigRef.current = { provider: activePreset.provider, model: activePreset.model, token: currentToken };- } else if (running.token !== currentToken) {- // Token loaded but config same — update ref without restarting- piRunningConfigRef.current.token = currentToken;- }- return;- }-- // Debounce: cancel any pending restart, schedule new one (2s to collapse boot-time events)- if (piRestartTimerRef.current) clearTimeout(piRestartTimerRef.current);- piRestartTimerRef.current = setTimeout(() => {- piRestartTimerRef.current = null;- const providerConfig = buildProviderConfig();- console.log("[Pi] Preset changed, restarting:", providerConfig?.provider, providerConfig?.model);- piRunningConfigRef.current = { provider: activePreset.provider, model: activePreset.model, token: currentToken };- // Reset session sync flag — Pi restarts fresh without conversation context,- // so the next message will re-inject chat history.- piSessionSyncedRef.current = false;- commands.piUpdateConfig(settings.user?.token ?? null, providerConfig).catch((e) => {- console.error("[Pi] Preset switch failed:", e);- });- }, 2000);-- return () => {- if (piRestartTimerRef.current) clearTimeout(piRestartTimerRef.current);- };- // eslint-disable-next-line react-hooks/exhaustive-deps- }, [activePreset?.provider, activePreset?.model, settings.user?.token, isSettingsLoaded]);+ // Restart Pi explicitly when user saves a preset — no useEffect, no debounce.+ // Called directly from the AIPresetsSelector onPresetSaved callback.+ const handlePiRestart = useCallback((preset: AIPreset) => {+ const providerConfig = buildProviderConfig(preset);+ console.log("[Pi] User saved preset, restarting:", providerConfig?.provider, providerConfig?.model);+ piSessionSyncedRef.current = false;+ commands.piUpdateConfig(settings.user?.token ?? null, providerConfig).catch((e) => {+ console.error("[Pi] Preset switch failed:", e);+ });+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [settings.user?.token]);// Listen for Pi events (all providers route through Pi) and pipe eventsuseEffect(() => {@@ -3639,6 +3603,7 @@ export function StandaloneChat({ className }: { className?: string } = {}) {<div className="flex-1 min-w-0"><AIPresetsSelectoronPresetChange={setActivePreset}+ onPresetSaved={handlePiRestart}controlledPresetId={activePipeExecution ? activePreset?.id : undefined}onControlledSelect={activePipeExecution ? (id) => {const match = settings.aiPresets?.find((p) => p.id === id);