Skip to content

Commit

Permalink
feat: retrieve logs (#529)
Browse files Browse the repository at this point in the history
* feat: retrieve logs

* feat: use tauri path resolver

* feat: display logs

---------

Co-authored-by: paulclindo <[email protected]>
  • Loading branch information
benolt and paulclindo authored Dec 18, 2024
1 parent b70973a commit e40778f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 7 deletions.
3 changes: 2 additions & 1 deletion apps/shinkai-desktop/src-tauri/capabilities/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"shinkai_node_get_ollama_api_url",
"shinkai_node_get_default_model",
"hardware_get_summary",
"galxe_generate_proof"
"galxe_generate_proof",
"retrieve_logs"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"shinkai_node_remove_storage",
"shinkai_node_set_default_options",
"shinkai_node_get_ollama_api_url",
"shinkai_node_get_default_model"
"shinkai_node_get_default_model",
"retrieve_logs"
]
}
22 changes: 22 additions & 0 deletions apps/shinkai-desktop/src-tauri/src/commands/logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use tauri::Manager;

#[tauri::command]
pub async fn retrieve_logs(app_handle: tauri::AppHandle) -> Result<String, String> {
let log_dir = app_handle.path().app_log_dir().map_err(|e| e.to_string())?;
let product_name = app_handle
.config()
.product_name
.clone()
.unwrap_or("Shinkai Desktop".to_string());
let log_file = format!("{}/{}.log", log_dir.display(), product_name);

let log_contents = match std::fs::read_to_string(log_file) {
Ok(contents) => contents,
Err(e) => {
log::error!("Failed to read log file: {}", e);
return Err("Failed to read log file".to_string());
}
};

Ok(log_contents)
}
3 changes: 2 additions & 1 deletion apps/shinkai-desktop/src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod fetch;
pub mod galxe;
pub mod hardware;
pub mod logs;
pub mod shinkai_node_manager_commands;
pub mod spotlight_commands;
pub mod fetch;
2 changes: 2 additions & 0 deletions apps/shinkai-desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::commands::shinkai_node_manager_commands::{
shinkai_node_set_options, shinkai_node_spawn, show_shinkai_node_manager_window,
};

use commands::logs::retrieve_logs;
use commands::spotlight_commands::{hide_spotlight_window_app, show_spotlight_window_app};
use global_shortcuts::global_shortcut_handler;
use globals::SHINKAI_NODE_MANAGER_INSTANCE;
Expand Down Expand Up @@ -89,6 +90,7 @@ fn main() {
get_request,
post_request,
shinkai_node_get_ollama_version,
retrieve_logs,
])
.setup(|app| {
log::info!("startin app version: {}", env!("CARGO_PKG_VERSION"));
Expand Down
21 changes: 21 additions & 0 deletions apps/shinkai-desktop/src/lib/shinkai-logs/logs-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
QueryObserverOptions,
useQuery,
UseQueryResult
} from '@tanstack/react-query';
import { invoke } from '@tauri-apps/api/core';

export const useRetrieveLogsQuery = (
options?: Omit<QueryObserverOptions, 'queryKey'>,
): UseQueryResult<string, Error> => {
const query = useQuery({
queryKey: ['retrieve_logs'],
queryFn: (): Promise<string> => invoke('retrieve_logs'),
...options,
});
return { ...query } as UseQueryResult<string, Error>;
};

export const retrieveLogs = async (): Promise<string> => {
return invoke('retrieve_logs');
};
33 changes: 29 additions & 4 deletions apps/shinkai-desktop/src/windows/shinkai-node-manager/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { z } from 'zod';

import logo from '../../../src-tauri/icons/[email protected]';
import { OllamaModels } from '../../components/shinkai-node-manager/ollama-models';
import { useRetrieveLogsQuery } from '../../lib/shinkai-logs/logs-client';
import { ALLOWED_OLLAMA_MODELS } from '../../lib/shinkai-node-manager/ollama-models';
import {
shinkaiNodeQueryClient,
Expand Down Expand Up @@ -84,6 +85,7 @@ const App = () => {
const setLogout = useAuth((auth) => auth.setLogout);
const { setShinkaiNodeOptions } = useShinkaiNodeManager();
const logsScrollRef = useRef<HTMLDivElement | null>(null);
const tauriLogsScrollRef = useRef<HTMLDivElement | null>(null);
const [isConfirmResetDialogOpened, setIsConfirmResetDialogOpened] =
useState<boolean>(false);
const { data: shinkaiNodeIsRunning } = useShinkaiNodeIsRunningQuery({
Expand All @@ -94,10 +96,11 @@ const App = () => {
});
const { data: lastNLogs } = useShinkaiNodeGetLastNLogsQuery(
{ length: 100 },
{
refetchInterval: 1000,
},
{ refetchInterval: 1000 },
);

const { data: tauriLogs } = useRetrieveLogsQuery();

const {
isPending: shinkaiNodeSpawnIsPending,
mutateAsync: shinkaiNodeSpawn,
Expand Down Expand Up @@ -314,7 +317,19 @@ const App = () => {
>
<TabsList className="w-full">
<TabsTrigger className="grow" value="logs">
Logs
Shinkai Node Logs
</TabsTrigger>
<TabsTrigger
className="grow"
onClick={() => {
tauriLogsScrollRef.current?.scrollIntoView({
behavior: 'smooth',
block: 'end',
});
}}
value="tauri-logs"
>
Tauri Logs
</TabsTrigger>
<TabsTrigger className="grow" value="options">
Options
Expand Down Expand Up @@ -342,6 +357,16 @@ const App = () => {
</div>
</ScrollArea>
</TabsContent>
<TabsContent className="h-full overflow-hidden" value="tauri-logs">
<ScrollArea className="flex h-full flex-1 flex-col overflow-auto [&>div>div]:!block">
<div
className="text-gray-80 whitespace-pre-wrap p-1 font-mono text-xs leading-relaxed"
ref={tauriLogsScrollRef}
>
{tauriLogs}
</div>
</ScrollArea>
</TabsContent>
<TabsContent className="h-full overflow-hidden" value="options">
<ScrollArea className="flex h-full flex-1 flex-col overflow-auto [&>div>div]:!block">
<div className="flex flex-row justify-end pr-4">
Expand Down

0 comments on commit e40778f

Please sign in to comment.