Skip to content

Commit

Permalink
Install tools directly from Shinkai Store.
Browse files Browse the repository at this point in the history
  • Loading branch information
guillevalin committed Dec 27, 2024
1 parent e946e68 commit 462cb63
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
35 changes: 35 additions & 0 deletions apps/shinkai-desktop/src-tauri/src/deep_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ pub struct OAuthDeepLinkPayload {
pub code: String,
}

#[derive(Debug, Clone, serde::Serialize)]
pub struct StoreDeepLinkPayload {
pub tool_type: String,
pub tool_url: String,
}

pub fn setup_deep_links(app: &tauri::AppHandle) -> tauri::Result<()> {
#[cfg(any(windows, target_os = "linux"))]
{
Expand All @@ -24,7 +30,36 @@ pub fn setup_deep_links(app: &tauri::AppHandle) -> tauri::Result<()> {
for url in urls {
log::debug!("handling deep link: {:?}", url);
if let Some(host) = url.host() {
if host.to_string() == "store" {
// shinkai://store?type=tool&url=https://download.shinkai.app/tool/email-fetcher.zip
let query_pairs = url.query_pairs().collect::<Vec<_>>();
let tool_type = query_pairs
.iter()
.find(|(key, _)| key == "type")
.map(|(_, value)| value.to_string())
.unwrap_or_default();
let tool_url = query_pairs
.iter()
.find(|(key, _)| key == "url")
.map(|(_, value)| value.to_string())
.unwrap_or_default();

let payload = StoreDeepLinkPayload { tool_type, tool_url };

log::debug!(
"emitting store-deep-link event to {}",
Window::Coordinator.as_str()
);
let _ = recreate_window(app_handle.clone(), Window::Main, true);
let _ = app_handle.emit_to(
Window::Coordinator.as_str(),
"store-deep-link",
payload,
);
}

if host.to_string() == "oauth" {
// shinkai://oauth?code=11&state=22
log::debug!("oauth deep link: {:?}", url);
let query_pairs = url.query_pairs().collect::<Vec<_>>();
let state = query_pairs
Expand Down
24 changes: 22 additions & 2 deletions apps/shinkai-desktop/src/pages/tools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import {
} from '@shinkai_network/shinkai-ui';
import { formatText } from '@shinkai_network/shinkai-ui/helpers';
import { cn } from '@shinkai_network/shinkai-ui/utils';
import { listen } from '@tauri-apps/api/event';
import {
BoltIcon,
CloudDownloadIcon,
PlusIcon,
SearchIcon,
XIcon,
} from 'lucide-react';
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { Link, useNavigate } from 'react-router-dom';
import { toast } from 'sonner';
Expand Down Expand Up @@ -282,7 +283,7 @@ type ImportToolFormSchema = z.infer<typeof importToolFormSchema>;

function ImportToolModal() {
const auth = useAuth((state) => state.auth);

const navigate = useNavigate();
const importToolForm = useForm<ImportToolFormSchema>({
resolver: zodResolver(importToolFormSchema),
Expand All @@ -309,6 +310,25 @@ function ImportToolModal() {
},
});

useEffect(() => {
const unlisten = listen('store-deep-link', (event) => {
if (!auth) return;

const payload = event.payload as { tool_type: string; tool_url: string };
if (payload.tool_type === 'tool') {
importTool({
nodeAddress: auth?.node_address ?? '',
token: auth?.api_v2_key ?? '',
url: payload.tool_url,
});
}
});

return () => {
unlisten.then((fn) => fn());
};
}, [importTool, auth]);

const onSubmit = async (data: ImportToolFormSchema) => {
await importTool({
nodeAddress: auth?.node_address ?? '',
Expand Down

0 comments on commit 462cb63

Please sign in to comment.