Skip to content

Commit

Permalink
Create a notion of preferredAIProvider in state.
Browse files Browse the repository at this point in the history
Action creators and reducers, not yet used for anything.

Part of #5.
  • Loading branch information
jkomoros committed Jan 7, 2024
1 parent db17ba8 commit 9a030d2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
12 changes: 10 additions & 2 deletions app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
promptSchema,
sproutNameSchema,
sproutStateSchema,
uncompiledPackagedSproutSchema
uncompiledPackagedSproutSchema,
modelProvider
} from '../src/types.js';

export const UPDATE_PAGE = 'UPDATE_PAGE';
Expand All @@ -40,6 +41,7 @@ export const FORCE_OPEN_API_KEYS_DIALOG = 'FORCE_OPEN_API_KEYS_DIALOG';
//This is actually poorly named, because it doesn't force the editor to close,
//it just lowers the forcedOpen flag.
export const FORCE_CLOSE_API_KEYS_DIALOG = 'FORCE_CLOSE_API_KEYS_DIALOG';
export const SET_PREFERRED_AI_PROVIDER = 'SET_PREFERRED_AI_PROVIDER';

const actionUpdatePage = z.object({
type: z.literal(UPDATE_PAGE),
Expand Down Expand Up @@ -139,6 +141,11 @@ const actionForceCloseAPIKeysDialog = z.object({
type: z.literal(FORCE_CLOSE_API_KEYS_DIALOG)
}).strict();

const actionSetPreferredAIProvider = z.object({
type: z.literal(SET_PREFERRED_AI_PROVIDER),
provider: modelProvider
}).strict();

const someAction = z.discriminatedUnion('type', [
actionUpdatePage,
actionUpdateOffline,
Expand All @@ -159,7 +166,8 @@ const someAction = z.discriminatedUnion('type', [
actionEditingModifySprout,
actionWriteSprout,
actionForceCloseAPIKeysDialog,
actionForceOpenAPIKeysDialog
actionForceOpenAPIKeysDialog,
actionSetPreferredAIProvider
]);

export type SomeAction = z.infer<typeof someAction>;
12 changes: 12 additions & 0 deletions app/actions/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
selectDraftMessage,
selectEditorOpen,
selectIsEditing,
selectPreferredAIProvider,
selectSproutData,
selectSproutSnapshot,
selectSproutStreaming
Expand All @@ -46,6 +47,7 @@ import {
APIKeys,
DirectoryInfo,
ImageURL,
ModelProvider,
Path,
Prompt,
SproutName,
Expand Down Expand Up @@ -369,4 +371,14 @@ export const forceClosedAPIKeysDialog = () : SomeAction => {
return {
type: FORCE_CLOSE_API_KEYS_DIALOG
};
};

export const setPreferredAIProvider = (provider : ModelProvider) : ThunkSomeAction => (dispatch, getState) => {
const state = getState();
const currentProvider = selectPreferredAIProvider(state);
if (currentProvider === provider) return;
dispatch({
type: 'SET_PREFERRED_AI_PROVIDER',
provider
});
};
7 changes: 7 additions & 0 deletions app/reducers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
OPEN_EDITOR,
SELECT_SPROUT,
SET_API_KEYS,
SET_PREFERRED_AI_PROVIDER,
SPROUT_PROVIDED_USER_RESPONSE,
SPROUT_STOPPED_STREAMING,
START_EDITING,
Expand All @@ -28,6 +29,7 @@ const INITIAL_STATE : DataState = {
'anthropic.com': ''
},
apiKeysEditorForcedOpen: false,
preferredAIProvider: 'openai.com',
sprouts: {},
currentSproutName: null,
sproutStreaming: false,
Expand Down Expand Up @@ -141,6 +143,11 @@ const data = (state : DataState = INITIAL_STATE, action : SomeAction) : DataStat
...state,
apiKeysEditorForcedOpen: false
};
case SET_PREFERRED_AI_PROVIDER:
return {
...state,
preferredAIProvider: action.provider
};
default:
return state;
}
Expand Down
1 change: 1 addition & 0 deletions app/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const selectIsEditing = (state : RootState) => state.data ? state.data.ed
export const selectChangesMade = (state : RootState) => state.data ? state.data.changesMade : false;
export const selectSproutSnapshot = (state : RootState) => state.data ? state.data.sproutSnapshot : null;
export const selectWrittenSprouts = (state : RootState) => state.data ? state.data.writtenSprouts : {};
export const selectPreferredAIProvider = (state : RootState) => state.data ? state.data.preferredAIProvider : 'openai.com';
const selectAPIKeysEditorForcedOpen = (state : RootState) => state.data ? state.data.apiKeysEditorForcedOpen : false;

export const selectHashForCurrentState = (_state : RootState) => '';
Expand Down
2 changes: 2 additions & 0 deletions app/types_store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
APIKeys,
ImageURL,
ModelProvider,
PackagedSprout,
SproutName,
UncompiledPackagedSprout
Expand All @@ -25,6 +26,7 @@ export type DataState = {
sprouts: SproutDataMap,
currentSproutName: SproutLocation | null,
sproutStreaming: boolean,
preferredAIProvider : ModelProvider,
//The conversation is managed by the currentSprout, but we need to tell the
//selector machinery once we have reason to believe its state has changed.
streamCounter: number,
Expand Down

0 comments on commit 9a030d2

Please sign in to comment.