Skip to content

Commit

Permalink
Fix isIndexLoaded async issue in ChatControls
Browse files Browse the repository at this point in the history
  • Loading branch information
logancyang committed Nov 16, 2024
1 parent 7a8b39e commit c1bdc6a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ ${chatContent}`;
debug={debug}
addMessage={addMessage}
vault={app.vault}
isIndexLoaded={plugin.vectorStoreManager.getIsIndexLoaded()}
vectorStoreManager={plugin.vectorStoreManager}
/>
</div>
</div>
Expand Down
24 changes: 16 additions & 8 deletions src/components/ChatComponents/ChatControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import React, { useEffect, useState } from "react";
import { ChainType } from "@/chainFactory";
import { TooltipActionButton } from "@/components/ChatComponents/TooltipActionButton";
import { stringToChainType } from "@/utils";
import VectorStoreManager from "@/VectorStoreManager";
import * as DropdownMenu from "@radix-ui/react-dropdown-menu";
import { ChevronDown, Download, Puzzle, RefreshCw } from "lucide-react";

Expand All @@ -20,8 +21,8 @@ interface ChatControlsProps {
onRefreshVaultContext: () => void;
settings: CopilotSettings;
vault_qa_strategy: string;
vectorStoreManager: VectorStoreManager;
debug?: boolean;
isIndexLoaded: boolean;
}

const ChatControls: React.FC<ChatControlsProps> = ({
Expand All @@ -32,10 +33,11 @@ const ChatControls: React.FC<ChatControlsProps> = ({
onRefreshVaultContext,
settings,
vault_qa_strategy,
vectorStoreManager,
debug,
isIndexLoaded,
}) => {
const [selectedChain, setSelectedChain] = useState<ChainType>(currentChain);
const [localIndexLoaded, setLocalIndexLoaded] = useState<boolean>(false);

const handleChainChange = async ({ value }: { value: string }) => {
const newChain = stringToChainType(value);
Expand Down Expand Up @@ -85,6 +87,12 @@ const ChatControls: React.FC<ChatControlsProps> = ({
setSelectedChain(settings.defaultChainType);
}, [settings.defaultChainType]);

useEffect(() => {
vectorStoreManager.waitForInitialization().then(() => {
setLocalIndexLoaded(true);
});
}, [vectorStoreManager]);

// const handleFindSimilarNotes = async () => {
// const activeFile = app.workspace.getActiveFile();
// if (!activeFile) {
Expand Down Expand Up @@ -135,17 +143,17 @@ const ChatControls: React.FC<ChatControlsProps> = ({
</DropdownMenu.Item>
<DropdownMenu.Item
onSelect={() => handleChainChange({ value: "vault_qa" })}
disabled={!isIndexLoaded}
className={!isIndexLoaded ? "disabled-menu-item" : ""}
disabled={!localIndexLoaded}
className={!localIndexLoaded ? "disabled-menu-item" : ""}
>
vault QA (basic) {!isIndexLoaded && "(index not loaded)"}
vault QA (basic) {!localIndexLoaded && "(index not loaded)"}
</DropdownMenu.Item>
<DropdownMenu.Item
onSelect={() => handleChainChange({ value: "copilot_plus" })}
disabled={!isIndexLoaded}
className={!isIndexLoaded ? "disabled-menu-item" : ""}
disabled={!localIndexLoaded}
className={!localIndexLoaded ? "disabled-menu-item" : ""}
>
copilot plus (alpha) {!isIndexLoaded && "(index not loaded)"}
copilot plus (alpha) {!localIndexLoaded && "(index not loaded)"}
</DropdownMenu.Item>
</DropdownMenu.Content>
</DropdownMenu.Portal>
Expand Down
7 changes: 4 additions & 3 deletions src/components/ChatComponents/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { NoteTitleModal } from "@/components/NoteTitleModal";
import { CustomPromptProcessor } from "@/customPromptProcessor";
import { CopilotSettings } from "@/settings/SettingsPage";
import { ChatMessage } from "@/sharedState";
import VectorStoreManager from "@/VectorStoreManager";
import * as DropdownMenu from "@radix-ui/react-dropdown-menu";
import { ChevronUp, Command, CornerDownLeft, StopCircle } from "lucide-react";
import { App, Platform, TFile, Vault } from "obsidian";
Expand All @@ -31,7 +32,7 @@ interface ChatInputProps {
addMessage: (message: ChatMessage) => void;
vault: Vault;
vault_qa_strategy: string;
isIndexLoaded: boolean;
vectorStoreManager: VectorStoreManager;
debug?: boolean;
}

Expand All @@ -57,7 +58,7 @@ const ChatInput: React.FC<ChatInputProps> = ({
addMessage,
vault,
vault_qa_strategy,
isIndexLoaded,
vectorStoreManager,
debug,
}) => {
const [shouldFocus, setShouldFocus] = useState(false);
Expand Down Expand Up @@ -206,7 +207,7 @@ const ChatInput: React.FC<ChatInputProps> = ({
onRefreshVaultContext={onRefreshVaultContext}
settings={settings}
vault_qa_strategy={vault_qa_strategy}
isIndexLoaded={isIndexLoaded}
vectorStoreManager={vectorStoreManager}
debug={debug}
/>

Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ export const DEFAULT_SETTINGS: CopilotSettings = {
activeModels: BUILTIN_CHAT_MODELS,
activeEmbeddingModels: BUILTIN_EMBEDDING_MODELS,
embeddingRequestsPerSecond: 10,
disableIndexOnMobile: false,
disableIndexOnMobile: true,
enabledCommands: {
[COMMAND_IDS.FIX_GRAMMAR]: {
enabled: true,
Expand Down
2 changes: 1 addition & 1 deletion src/settings/components/QASettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ const QASettings: React.FC<QASettingsProps> = ({
/>
<ToggleComponent
name="Disable index loading on mobile"
description="When enabled, vector store index won't be loaded on mobile devices to save resources. Only chat mode will be available. Any existing index from desktop sync will be preserved."
description="When enabled, vector store index won't be loaded on mobile devices to save resources. Only chat mode will be available. Any existing index from desktop sync will be preserved. Uncheck to enable QA modes on mobile."
value={disableIndexOnMobile}
onChange={setDisableIndexOnMobile}
/>
Expand Down

0 comments on commit c1bdc6a

Please sign in to comment.