2023-06-15 12:52:46 +03:00
|
|
|
/* eslint-disable */
|
2023-06-02 18:01:49 +03:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
import { createContext, useEffect, useState } from "react";
|
2023-06-15 12:52:46 +03:00
|
|
|
|
|
|
|
import { setEmptyStringsUndefined } from "@/lib/helpers/setEmptyStringsUndefined";
|
|
|
|
|
2023-06-02 18:01:49 +03:00
|
|
|
import {
|
|
|
|
getBrainConfigFromLocalStorage,
|
|
|
|
saveBrainConfigInLocalStorage,
|
|
|
|
} from "./helpers/brainConfigLocalStorage";
|
2023-07-05 10:30:22 +03:00
|
|
|
import { BrainConfig, BrainConfigContextType } from "./types";
|
2023-06-02 18:01:49 +03:00
|
|
|
|
2023-07-05 10:30:22 +03:00
|
|
|
export const BrainConfigContext = createContext<
|
|
|
|
BrainConfigContextType | undefined
|
|
|
|
>(undefined);
|
2023-06-02 18:01:49 +03:00
|
|
|
|
|
|
|
const defaultBrainConfig: BrainConfig = {
|
2023-06-17 02:32:03 +03:00
|
|
|
model: "gpt-3.5-turbo-0613",
|
2023-06-02 18:01:49 +03:00
|
|
|
temperature: 0,
|
2023-07-01 18:22:28 +03:00
|
|
|
maxTokens: 256,
|
2023-06-02 18:01:49 +03:00
|
|
|
keepLocal: true,
|
|
|
|
anthropicKey: undefined,
|
|
|
|
backendUrl: undefined,
|
|
|
|
openAiKey: undefined,
|
|
|
|
supabaseKey: undefined,
|
|
|
|
supabaseUrl: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const BrainConfigProvider = ({
|
|
|
|
children,
|
|
|
|
}: {
|
|
|
|
children: React.ReactNode;
|
|
|
|
}) => {
|
|
|
|
const [brainConfig, setBrainConfig] =
|
|
|
|
useState<BrainConfig>(defaultBrainConfig);
|
|
|
|
|
|
|
|
const updateConfig = (newConfig: Partial<BrainConfig>) => {
|
|
|
|
setBrainConfig((config) => {
|
|
|
|
const updatedConfig: BrainConfig = {
|
|
|
|
...config,
|
|
|
|
...setEmptyStringsUndefined(newConfig),
|
|
|
|
};
|
|
|
|
saveBrainConfigInLocalStorage(updatedConfig);
|
|
|
|
|
|
|
|
return updatedConfig;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const resetConfig = () => {
|
|
|
|
updateConfig(defaultBrainConfig);
|
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setBrainConfig(getBrainConfigFromLocalStorage() ?? defaultBrainConfig);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<BrainConfigContext.Provider
|
|
|
|
value={{ config: brainConfig, updateConfig, resetConfig }}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</BrainConfigContext.Provider>
|
|
|
|
);
|
|
|
|
};
|