Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/desktop/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@ai-sdk/azure": "^3.0.26",
"@ai-sdk/google": "^3.0.20",
"@ai-sdk/google-vertex": "^4.0.41",
"@ai-sdk/mcp": "^1.0.19",
"@ai-sdk/mistral": "^3.0.18",
"@ai-sdk/openai": "^3.0.25",
"@ai-sdk/openai-compatible": "^2.0.26",
Expand Down
6 changes: 5 additions & 1 deletion apps/desktop/src/chat/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ export class CustomChatTransport implements ChatTransport<HyprUIMessage> {
private registry: ToolRegistry,
private model: LanguageModel,
private systemPrompt?: string,
private extraTools?: Record<string, any>,
) {}

sendMessages: ChatTransport<HyprUIMessage>["sendMessages"] = async (
options,
) => {
const tools = this.registry.getTools("chat");
const tools = {
...this.registry.getTools("chat"),
...this.extraTools,
};

const agent = new ToolLoopAgent({
model: this.model,
Expand Down
9 changes: 6 additions & 3 deletions apps/desktop/src/components/chat/session.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface ChatSessionProps {
chatGroupId?: string;
attachedSessionId?: string;
modelOverride?: LanguageModel;
extraTools?: Record<string, any>;
children: (props: {
messages: HyprUIMessage[];
sendMessage: (message: HyprUIMessage) => void;
Expand All @@ -42,9 +43,10 @@ export function ChatSession({
chatGroupId,
attachedSessionId,
modelOverride,
extraTools,
children,
}: ChatSessionProps) {
const transport = useTransport(attachedSessionId, modelOverride);
const transport = useTransport(attachedSessionId, modelOverride, extraTools);
const store = main.UI.useStore(main.STORE_ID);

const { user_id } = main.UI.useValues(main.STORE_ID);
Expand Down Expand Up @@ -185,6 +187,7 @@ export function ChatSession({
function useTransport(
attachedSessionId?: string,
modelOverride?: LanguageModel,
extraTools?: Record<string, any>,
) {
const registry = useToolRegistry();
const configuredModel = useLanguageModel();
Expand Down Expand Up @@ -302,8 +305,8 @@ function useTransport(
return null;
}

return new CustomChatTransport(registry, model, systemPrompt);
}, [registry, model, systemPrompt]);
return new CustomChatTransport(registry, model, systemPrompt, extraTools);
}, [registry, model, systemPrompt, extraTools]);

return transport;
}
3 changes: 2 additions & 1 deletion apps/desktop/src/components/chat/view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ export function ChatView() {
const existingChatTab = tabs.find((t) => t.type === "chat");
openNew({
type: "chat",
state: { groupId: groupId ?? null, initialMessage: null },
state: { groupId: groupId ?? null, initialMessage: null, chatType: null },
});
if (existingChatTab) {
updateChatTabState(existingChatTab, {
groupId: groupId ?? null,
initialMessage: null,
chatType: null,
});
}
chat.sendEvent({ type: "OPEN_TAB" });
Expand Down
31 changes: 26 additions & 5 deletions apps/desktop/src/components/main/body/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useFeedbackLanguageModel,
useLanguageModel,
} from "../../../hooks/useLLMConnection";
import { useSupportMCPTools } from "../../../hooks/useSupportMCPTools";
import * as main from "../../../store/tinybase/store/main";
import type { Tab } from "../../../store/zustand/tabs";
import { useTabs } from "../../../store/zustand/tabs";
Expand Down Expand Up @@ -75,17 +76,19 @@ export function TabContentChat({

function ChatTabView({ tab }: { tab: Extract<Tab, { type: "chat" }> }) {
const groupId = tab.state.groupId ?? undefined;
const isFeedback = !!tab.state.initialMessage;
const isSupport = tab.state.chatType === "support";
const updateChatTabState = useTabs((state) => state.updateChatTabState);

const stableSessionId = useStableSessionId(groupId);
const userModel = useLanguageModel();
const feedbackModel = useFeedbackLanguageModel();
const model = isFeedback ? feedbackModel : userModel;
const model = isSupport ? feedbackModel : userModel;
const { tools: mcpTools, isReady: mcpReady } = useSupportMCPTools(isSupport);

const onGroupCreated = useCallback(
(newGroupId: string) =>
updateChatTabState(tab, {
...tab.state,
groupId: newGroupId,
initialMessage: null,
}),
Expand All @@ -103,7 +106,8 @@ function ChatTabView({ tab }: { tab: Extract<Tab, { type: "chat" }> }) {
key={stableSessionId}
sessionId={stableSessionId}
chatGroupId={groupId}
modelOverride={isFeedback ? feedbackModel : undefined}
modelOverride={isSupport ? feedbackModel : undefined}
extraTools={isSupport ? mcpTools : undefined}
>
{({ messages, sendMessage, regenerate, stop, status, error }) => (
<ChatTabContent
Expand All @@ -117,6 +121,7 @@ function ChatTabView({ tab }: { tab: Extract<Tab, { type: "chat" }> }) {
model={model}
handleSendMessage={handleSendMessage}
updateChatTabState={updateChatTabState}
mcpReady={mcpReady}
/>
)}
</ChatSession>
Expand All @@ -135,6 +140,7 @@ function ChatTabContent({
model,
handleSendMessage,
updateChatTabState,
mcpReady,
}: {
tab: Extract<Tab, { type: "chat" }>;
messages: HyprUIMessage[];
Expand All @@ -153,12 +159,19 @@ function ChatTabContent({
tab: Extract<Tab, { type: "chat" }>,
state: Extract<Tab, { type: "chat" }>["state"],
) => void;
mcpReady: boolean;
}) {
const sentRef = useRef(false);

useEffect(() => {
const initialMessage = tab.state.initialMessage;
if (!initialMessage || sentRef.current || !model || status !== "ready") {
if (
!initialMessage ||
sentRef.current ||
!model ||
status !== "ready" ||
!mcpReady
) {
return;
}

Expand All @@ -172,7 +185,15 @@ function ChatTabContent({
...tab.state,
initialMessage: null,
});
}, [tab, model, status, handleSendMessage, sendMessage, updateChatTabState]);
}, [
tab,
model,
status,
mcpReady,
handleSendMessage,
sendMessage,
updateChatTabState,
]);

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion apps/desktop/src/components/main/sidebar/profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export function ProfileSection({ onExpandChange }: ProfileSectionProps = {}) {
const handleClickHelp = useCallback(() => {
openNew({
type: "chat",
state: { groupId: null, initialMessage: "I need help." },
state: { groupId: null, initialMessage: "I need help.", chatType: null },
});
transitionChatMode({ type: "OPEN_TAB" });
closeMenu();
Expand Down
62 changes: 62 additions & 0 deletions apps/desktop/src/hooks/useSupportMCPTools.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { createMCPClient, type MCPClient } from "@ai-sdk/mcp";
import { useEffect, useRef, useState } from "react";

import { env } from "../env";

export function useSupportMCPTools(enabled: boolean) {
const [tools, setTools] = useState<Record<string, any>>({});
const [isReady, setIsReady] = useState(false);
const clientRef = useRef<MCPClient | null>(null);

useEffect(() => {
if (!enabled) {
setTools({});
setIsReady(false);
return;
}

let cancelled = false;

const init = async () => {
try {
const mcpUrl = new URL("/support/mcp", env.VITE_AI_URL).toString();

const client = await createMCPClient({
transport: {
type: "http",
url: mcpUrl,
},
name: "hyprnote-support-client",
});

if (cancelled) {
await client.close();
return;
}

clientRef.current = client;
const mcpTools = await client.tools();

if (!cancelled) {
setTools(mcpTools);
setIsReady(true);
}
} catch (error) {
console.error("Failed to initialize MCP client:", error);
if (!cancelled) {
setIsReady(true);
}
}
};

init();

return () => {
cancelled = true;
clientRef.current?.close().catch(console.error);
clientRef.current = null;
};
}, [enabled]);

return { tools, isReady: enabled ? isReady : true };
}
6 changes: 5 additions & 1 deletion apps/desktop/src/store/zustand/tabs/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,11 @@ export const getDefaultState = (tab: TabInput): Tab => {
return {
...base,
type: "chat",
state: tab.state ?? { groupId: null, initialMessage: null },
state: tab.state ?? {
groupId: null,
initialMessage: null,
chatType: null,
},
};
default:
const _exhaustive: never = tab;
Expand Down
5 changes: 4 additions & 1 deletion plugins/tray/src/menu_items/help_report_bug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ impl MenuItemHandler for HelpReportBug {
}

fn handle(app: &AppHandle<tauri::Wry>) {
use tauri_plugin_windows::{AppWindow, ChatState, OpenTab, TabInput, WindowsPluginExt};
use tauri_plugin_windows::{
AppWindow, ChatState, ChatType, OpenTab, TabInput, WindowsPluginExt,
};
use tauri_specta::Event;

if app.windows().show(AppWindow::Main).is_ok() {
let event = OpenTab {
tab: TabInput::Chat {
state: Some(ChatState {
initial_message: Some("I'd like to report a bug.".to_string()),
chat_type: Some(ChatType::Support),
..Default::default()
}),
},
Expand Down
5 changes: 4 additions & 1 deletion plugins/tray/src/menu_items/help_suggest_feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ impl MenuItemHandler for HelpSuggestFeature {
}

fn handle(app: &AppHandle<tauri::Wry>) {
use tauri_plugin_windows::{AppWindow, ChatState, OpenTab, TabInput, WindowsPluginExt};
use tauri_plugin_windows::{
AppWindow, ChatState, ChatType, OpenTab, TabInput, WindowsPluginExt,
};
use tauri_specta::Event;

if app.windows().show(AppWindow::Main).is_ok() {
let event = OpenTab {
tab: TabInput::Chat {
state: Some(ChatState {
initial_message: Some("I'd like to suggest a feature.".to_string()),
chat_type: Some(ChatType::Support),
..Default::default()
}),
},
Expand Down
3 changes: 2 additions & 1 deletion plugins/windows/js/bindings.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export type AiTab = "transcription" | "intelligence" | "templates" | "shortcuts"
export type AppWindow = { type: "onboarding" } | { type: "main" } | { type: "control" }
export type ChangelogState = { previous: string | null; current: string }
export type ChatShortcutsState = { isWebMode: boolean | null; selectedMineId: string | null; selectedWebIndex: number | null }
export type ChatState = { groupId: string | null; initialMessage: string | null }
export type ChatState = { groupId: string | null; initialMessage: string | null; chatType: ChatType | null }
export type ChatType = "regular" | "support"
export type ContactsState = { selectedOrganization: string | null; selectedPerson: string | null }
export type EditorView = { type: "raw" } | { type: "transcript" } | { type: "enhanced"; id: string } | { type: "attachments" }
export type ExtensionsState = { selectedExtension: string | null }
Expand Down
12 changes: 12 additions & 0 deletions plugins/windows/src/tab/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,22 @@ crate::common_derives! {
}
}

crate::common_derives! {
#[derive(Default)]
pub enum ChatType {
#[default]
#[serde(rename = "regular")]
Regular,
#[serde(rename = "support")]
Support,
}
}

crate::common_derives! {
#[derive(Default)]
pub struct ChatState {
pub group_id: Option<String>,
pub initial_message: Option<String>,
pub chat_type: Option<ChatType>,
}
}
Loading
Loading