Skip to content

Commit

Permalink
fix: weird settings state stuff + retry on backend fail + cloud badge
Browse files Browse the repository at this point in the history
  • Loading branch information
louis030195 committed Aug 20, 2024
1 parent b1b9d8e commit 2a6ea39
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Settings, useSettings } from "@/lib/hooks/use-settings";
import { useToast } from "@/components/ui/use-toast";
import { useHealthCheck } from "@/lib/hooks/use-health-check";
import { invoke } from "@tauri-apps/api/core";
import { Badge } from "./ui/badge";

interface AudioDevice {
name: string;
Expand Down Expand Up @@ -124,7 +125,15 @@ export function RecordingSettings({

try {
console.log("localSettings", localSettings);
await updateSettings(localSettings);
// Only update specific fields
const settingsToUpdate = {
audioTranscriptionEngine: localSettings.audioTranscriptionEngine,
ocrEngine: localSettings.ocrEngine,
monitorId: localSettings.monitorId,
audioDevices: localSettings.audioDevices,
};
console.log("Settings to update:", settingsToUpdate);
await updateSettings(settingsToUpdate);

await invoke("kill_all_sreenpipes");

Expand Down Expand Up @@ -200,7 +209,12 @@ export function RecordingSettings({
<SelectValue placeholder="select audio transcription engine" />
</SelectTrigger>
<SelectContent>
<SelectItem value="deepgram">deepgram</SelectItem>
<SelectItem value="deepgram">
<div className="flex items-center justify-between w-full space-x-2">
<span>deepgram</span>
<Badge variant="secondary">cloud</Badge>
</div>
</SelectItem>
<SelectItem value="whisper-tiny">whisper-tiny</SelectItem>
<SelectItem value="whisper-large">whisper-large</SelectItem>
</SelectContent>
Expand All @@ -220,7 +234,12 @@ export function RecordingSettings({
<SelectValue placeholder="select ocr engine" />
</SelectTrigger>
<SelectContent>
<SelectItem value="unstructured">unstructured</SelectItem>
<SelectItem value="unstructured">
<div className="flex items-center justify-between w-full space-x-2">
<span>unstructured</span>
<Badge variant="secondary">cloud</Badge>
</div>
</SelectItem>
{currentPlatform !== "macos" && (
<SelectItem value="tesseract">tesseract</SelectItem>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ const DevModeSettings = () => {
const { settings, updateSettings } = useSettings();
const [localSettings, setLocalSettings] = useState(settings);
const handleDevModeToggle = (checked: boolean) => {
console.log("checked", checked);
setLocalSettings({ ...localSettings, devMode: checked });
updateSettings({ ...localSettings, devMode: checked });

setLocalSettings((prev) => ({ ...prev, devMode: checked }));
updateSettings({ devMode: checked });
};
const [isLoading, setIsLoading] = useState(false);
const { toast } = useToast();
Expand Down
28 changes: 14 additions & 14 deletions examples/apps/screenpipe-app-tauri/components/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,34 @@ export function Settings({ className }: { className?: string }) {
const [showApiKey, setShowApiKey] = React.useState(false);

const handleApiKeyChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setLocalSettings({ ...localSettings, openaiApiKey: e.target.value });
updateSettings({ ...localSettings, openaiApiKey: e.target.value });
const newValue = e.target.value;
setLocalSettings((prev) => ({ ...prev, openaiApiKey: newValue }));
updateSettings({ openaiApiKey: newValue });
};

const handleOllamaToggle = (checked: boolean) => {
console.log("checked", checked);
setLocalSettings({ ...localSettings, useOllama: checked });
updateSettings({ ...localSettings, useOllama: checked });
setLocalSettings((prev) => ({ ...prev, useOllama: checked }));
updateSettings({ useOllama: checked });
};

const handleOllamaUrlChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setLocalSettings({ ...localSettings, ollamaUrl: e.target.value });
updateSettings({ ...localSettings, ollamaUrl: e.target.value });
const newValue = e.target.value;
setLocalSettings((prev) => ({ ...prev, ollamaUrl: newValue }));
updateSettings({ ollamaUrl: newValue });
};

const handleModelChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setLocalSettings({ ...localSettings, aiModel: e.target.value });
updateSettings({ ...localSettings, aiModel: e.target.value });
const newValue = e.target.value;
setLocalSettings((prev) => ({ ...prev, aiModel: newValue }));
updateSettings({ aiModel: newValue });
};

const handleCustomPromptChange = (
e: React.ChangeEvent<HTMLTextAreaElement>
) => {
setLocalSettings({ ...localSettings, customPrompt: e.target.value });
updateSettings({ ...localSettings, customPrompt: e.target.value });
const newValue = e.target.value;
setLocalSettings((prev) => ({ ...prev, customPrompt: newValue }));
updateSettings({ customPrompt: newValue });
};

const handleResetCustomPrompt = () => {
Expand Down Expand Up @@ -239,9 +242,6 @@ export function Settings({ className }: { className?: string }) {
</CardContent>
</Card>




<Separator />

<Card>
Expand Down
38 changes: 20 additions & 18 deletions examples/apps/screenpipe-app-tauri/lib/hooks/use-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ export function useSettings() {
const savedUserId = ((await store!.get("userId")) as string) || "";
const savedCustomPrompt =
((await store!.get("customPrompt")) as string) || "";
const savedDevMode =
((await store!.get("devMode")) as boolean) || false;
let savedDevMode = (await store!.get("devMode")) as boolean;

savedDevMode = savedDevMode === true;
const savedAudioTranscriptionEngine =
((await store!.get("audioTranscriptionEngine")) as string) ||
"whisper-tiny";
Expand Down Expand Up @@ -138,23 +139,24 @@ export function useSettings() {
}

try {
const updatedSettings = { ...settings, ...newSettings };
// Only update the fields that are explicitly provided in newSettings
const updatedSettings = { ...settings };
for (const key in newSettings) {
if (Object.prototype.hasOwnProperty.call(newSettings, key)) {
// @ts-ignore
updatedSettings[key as keyof Settings] =
newSettings[key as keyof Settings]!;
}
}

setSettings(updatedSettings);
await store!.set("openaiApiKey", updatedSettings.openaiApiKey);
await store!.set("useOllama", updatedSettings.useOllama);
await store!.set("ollamaUrl", updatedSettings.ollamaUrl);
await store!.set("aiModel", updatedSettings.aiModel);
await store!.set("installedPipes", updatedSettings.installedPipes);
await store!.set("userId", updatedSettings.userId);
await store!.set("customPrompt", updatedSettings.customPrompt);
await store!.set("devMode", updatedSettings.devMode);
await store!.set(
"audioTranscriptionEngine",
updatedSettings.audioTranscriptionEngine
);
await store!.set("ocrEngine", updatedSettings.ocrEngine);
await store!.set("monitorId", updatedSettings.monitorId);
await store!.set("audioDevices", updatedSettings.audioDevices);
// Only update the store for the fields that were changed
for (const key in newSettings) {
if (Object.prototype.hasOwnProperty.call(newSettings, key)) {
await store!.set(key, updatedSettings[key as keyof Settings]);
}
}

await store!.save();
} catch (error) {
console.error("Failed to update settings:", error);
Expand Down
2 changes: 1 addition & 1 deletion examples/apps/screenpipe-app-tauri/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
version = "0.1.53"
version = "0.1.54"
description = ""
authors = ["you"]
license = ""
Expand Down
95 changes: 57 additions & 38 deletions examples/apps/screenpipe-app-tauri/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ use tauri::Config;

use serde_json::Value;
use std::env;
use std::fs;
use std::fs::File;
use std::io::Write;
use tauri_plugin_shell::process::CommandEvent;

use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use tauri::Manager;
use tauri::State;
use tauri::Wry;
Expand All @@ -25,8 +24,10 @@ use tauri::{
use tauri_plugin_autostart::MacosLauncher;
use tauri_plugin_autostart::ManagerExt;
use tauri_plugin_shell::process::CommandChild;
use tauri_plugin_shell::process::CommandEvent;
use tauri_plugin_shell::ShellExt;
use tauri_plugin_store::{with_store, StoreCollection};
use tokio::time::sleep;
use uuid::Uuid;
mod analytics;

Expand All @@ -42,30 +43,54 @@ async fn kill_all_sreenpipes(
) -> Result<(), String> {
debug!("Killing screenpipe");

if let Some(child) = state.0.lock().unwrap().take() {
child.kill().map_err(|e| e.to_string())?;
}
const MAX_RETRIES: u32 = 3;
const RETRY_DELAY: Duration = Duration::from_secs(1);

// Hard kill the sidecar
#[cfg(not(target_os = "windows"))]
{
let _ = tokio::process::Command::new("pkill")
.arg("-f")
.arg("screenpipe")
.output()
.await
.map_err(|e| e.to_string())?;
}
#[cfg(target_os = "windows")]
{
let _ = tokio::process::Command::new("taskkill")
.args(&["/F", "/IM", "screenpipe.exe"])
.output()
.await
.map_err(|e| e.to_string())?;
for attempt in 1..=MAX_RETRIES {
if let Some(child) = state.0.lock().unwrap().take() {
if let Err(e) = child.kill() {
error!("Failed to kill child process (attempt {}): {}", attempt, e);
}
}

// Hard kill the sidecar
let kill_result = async {
#[cfg(not(target_os = "windows"))]
{
tokio::process::Command::new("pkill")
.arg("-f")
.arg("screenpipe")
.output()
.await
}
#[cfg(target_os = "windows")]
{
tokio::process::Command::new("taskkill")
.args(&["/F", "/IM", "screenpipe.exe"])
.output()
.await
}
}
.await;

match kill_result {
Ok(_) => {
debug!("Successfully killed screenpipe processes");
return Ok(());
}
Err(e) => {
error!(
"Failed to kill screenpipe processes (attempt {}): {}",
attempt, e
);
if attempt < MAX_RETRIES {
sleep(RETRY_DELAY).await;
}
}
}
}

Ok(())
Err("Failed to kill screenpipe processes after multiple attempts".to_string())
}

#[tauri::command]
Expand Down Expand Up @@ -187,7 +212,10 @@ fn spawn_sidecar(app: &tauri::AppHandle) -> Result<CommandChild, String> {

let (mut rx, child) = result.unwrap();

// only in production mode because it breaks the "bun tauri dev"
#[cfg(not(debug_assertions))]
tauri::async_runtime::spawn(async move {
#[allow(unused_variables)]
let mut i = 0;
while let Some(event) = rx.recv().await {
if let CommandEvent::Stdout(line) = event {
Expand Down Expand Up @@ -443,6 +471,8 @@ async fn main() {
debug!("Ready event");
}
tauri::RunEvent::ExitRequested { .. } => {
let app_handle_clone = app_handle.clone();
let app_handle_clone2 = app_handle.clone();
debug!("ExitRequested event");
// kill all screenpipe processes if the user is not using dev mode using pkill
// get dev mode from the store
Expand All @@ -462,20 +492,9 @@ async fn main() {
.unwrap_or(false);
if !use_dev_mode {
tauri::async_runtime::spawn(async move {
#[cfg(not(target_os = "windows"))]
{
let _ = tokio::process::Command::new("pkill")
.arg("-f")
.arg("screenpipe")
.output()
.await;
}
#[cfg(target_os = "windows")]
{
let _ = tokio::process::Command::new("taskkill")
.args(&["/F", "/IM", "screenpipe.exe"])
.output()
.await;
let state = app_handle_clone.state::<SidecarState>();
if let Err(e) = kill_all_sreenpipes(state, app_handle_clone2).await {
error!("Failed to kill screenpipe processes: {}", e);
}
});
}
Expand Down

0 comments on commit 2a6ea39

Please sign in to comment.