Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
[web] Stop persisting api keys inside conversations (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxijonson committed Jul 7, 2023
1 parent 9451077 commit 37c496e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
14 changes: 7 additions & 7 deletions packages/web/src/components/ConversationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ const ConversationFormProvided = ({
<Tabs.Panel value="functions" pt="md">
<ConversationFormFunctionsTab />
</Tabs.Panel>
<Tabs.Panel value="app" pt="md">
<AppSettings />
</Tabs.Panel>
{!hideAppSettings && (
<Tabs.Panel value="app" pt="md">
<AppSettings />
</Tabs.Panel>
)}
</Tabs>
{currentTab !== "app" && (
<Box>
Expand All @@ -65,10 +67,8 @@ const ConversationFormProvided = ({
{form.values.save && (
<Text size="xs" italic align="center">
This conversation will be saved to your browser's
local storage, along with your API key, if
specified. Make sure that you trust the device you
are using and that you are not using a shared
device.
local storage. Make sure the device you're using is
trusted and not shared with anyone else.
</Text>
)}
</Box>
Expand Down
15 changes: 11 additions & 4 deletions packages/web/src/contexts/providers/PersistenceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import useCallableFunctions from "../../hooks/useCallableFunctions";
import { PersistenceCallableFunction } from "../../entities/persistenceCallableFunction";
import { STORAGEKEY_PERSISTENCE } from "../../config/constants";
import { persistenceVersion } from "../../entities/migrations/persistence";
import useSettings from "../../hooks/useSettings";

interface PersistenceProviderProps {
children?: React.ReactNode;
}

const PersistenceProvider = ({ children }: PersistenceProviderProps) => {
const { settings } = useSettings();

const {
conversations,
addConversation,
setActiveConversation,
getConversationName,
setConversationName,
getConversationLastEdit,
Expand Down Expand Up @@ -234,9 +236,14 @@ const PersistenceProvider = ({ children }: PersistenceProviderProps) => {
const { name, lastEdited, ...conversationJson } =
persistence.conversations[i];
const newConversation = addConversation(
await Conversation.fromJSON(conversationJson)
await Conversation.fromJSON({
...conversationJson,
config: {
...conversationJson.config,
apiKey: settings.apiKey || undefined,
},
})
);
if (i === 0) setActiveConversation(newConversation.id, true);
addPersistedConversationId(newConversation.id);
setConversationName(newConversation.id, name);
setConversationLastEdit(newConversation.id, lastEdited);
Expand Down Expand Up @@ -284,9 +291,9 @@ const PersistenceProvider = ({ children }: PersistenceProviderProps) => {
persistence.functions,
persistence.functionsImportWarning,
persistence.functionsWarning,
setActiveConversation,
setConversationLastEdit,
setConversationName,
settings.apiKey,
]);

// Save conversations on change
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const migratePersistenceRemoveApiKey = (
value: Record<string, any>
): Record<string, any> => {
if (!value.conversations || !Array.isArray(value.conversations))
return value;

value.conversations = value.conversations.map((conversation) => {
if (conversation.config?.apiKey === undefined) return conversation;
const { apiKey, ...config } = conversation.config;

return {
...conversation,
config,
};
});

return value;
};
2 changes: 2 additions & 0 deletions packages/web/src/entities/migrations/persistence/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { migratePersistenceInitial } from "./1688489405401_initial";
import { migratePersistenceRemoveApiKey } from "./1688740831717_remove-api-key";

const migrations: ((value: Record<string, any>) => Record<string, any>)[] = [
migratePersistenceInitial,
migratePersistenceRemoveApiKey,
];

export const persistenceVersion = migrations.length;
Expand Down
4 changes: 4 additions & 0 deletions packages/web/src/entities/persistenceConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { conversationSchema } from "gpt-turbo";
export const persistenceConversationSchema = conversationSchema.extend({
name: z.string(),
lastEdited: z.number(),
config: conversationSchema.shape.config
.unwrap()
.omit({ apiKey: true })
.optional(),
});

export type PersistenceConversation = z.infer<
Expand Down

0 comments on commit 37c496e

Please sign in to comment.