Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using jotai default store #911

Merged
merged 1 commit into from
Dec 9, 2024
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
24 changes: 14 additions & 10 deletions src/aiParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { ChainType } from "@/chainFactory";
import { BaseChatModel } from "@langchain/core/language_models/chat_models";
import { ChatPromptTemplate } from "@langchain/core/prompts";

import { atom, getDefaultStore, useAtom } from "jotai";
import { settingsAtom } from "@/settings/model";
import { atom, useAtom } from "jotai";
import { settingsAtom, settingsStore } from "@/settings/model";

const userModelKeyAtom = atom<string | null>(null);
const modelKeyAtom = atom(
Expand Down Expand Up @@ -76,33 +76,37 @@ export interface CustomModel {
}

export function setModelKey(modelKey: string) {
getDefaultStore().set(modelKeyAtom, modelKey);
settingsStore.set(modelKeyAtom, modelKey);
}

export function getModelKey(): string {
return getDefaultStore().get(modelKeyAtom);
return settingsStore.get(modelKeyAtom);
}

export function subscribeToModelKeyChange(callback: () => void): () => void {
return getDefaultStore().sub(modelKeyAtom, callback);
return settingsStore.sub(modelKeyAtom, callback);
}

export function useModelKey() {
return useAtom(modelKeyAtom);
return useAtom(modelKeyAtom, {
store: settingsStore,
});
}

export function getChainType(): ChainType {
return getDefaultStore().get(chainTypeAtom);
return settingsStore.get(chainTypeAtom);
}

export function setChainType(chainType: ChainType) {
getDefaultStore().set(chainTypeAtom, chainType);
settingsStore.set(chainTypeAtom, chainType);
}

export function subscribeToChainTypeChange(callback: () => void): () => void {
return getDefaultStore().sub(chainTypeAtom, callback);
return settingsStore.sub(chainTypeAtom, callback);
}

export function useChainType() {
return useAtom(chainTypeAtom);
return useAtom(chainTypeAtom, {
store: settingsStore,
});
}
13 changes: 8 additions & 5 deletions src/settings/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CustomModel } from "@/aiParams";
import { atom, getDefaultStore, useAtomValue } from "jotai";
import { atom, createStore, useAtomValue } from "jotai";

import { type ChainType } from "@/chainFactory";
import {
Expand Down Expand Up @@ -59,14 +59,15 @@ export interface CopilotSettings {
showSuggestedPrompts: boolean;
}

export const settingsStore = createStore();
export const settingsAtom = atom<CopilotSettings>(DEFAULT_SETTINGS);

/**
* Sets the settings in the atom.
*/
export function setSettings(settings: Partial<CopilotSettings>) {
const newSettings = mergeAllActiveModelsWithCoreModels({ ...getSettings(), ...settings });
getDefaultStore().set(settingsAtom, newSettings);
settingsStore.set(settingsAtom, newSettings);
}

/**
Expand All @@ -82,7 +83,7 @@ export function updateSetting<K extends keyof CopilotSettings>(key: K, value: Co
* changes.
*/
export function getSettings(): Readonly<CopilotSettings> {
return getDefaultStore().get(settingsAtom);
return settingsStore.get(settingsAtom);
}

/**
Expand All @@ -101,14 +102,16 @@ export function resetSettings(): void {
* Subscribes to changes in the settings atom.
*/
export function subscribeToSettingsChange(callback: () => void): () => void {
return getDefaultStore().sub(settingsAtom, callback);
return settingsStore.sub(settingsAtom, callback);
}

/**
* Hook to get the settings value from the atom.
*/
export function useSettingsValue(): Readonly<CopilotSettings> {
return useAtomValue(settingsAtom);
return useAtomValue(settingsAtom, {
store: settingsStore,
});
}

/**
Expand Down
Loading