Skip to content

Commit

Permalink
AIDialogType is a zod type.
Browse files Browse the repository at this point in the history
Part of #673.
  • Loading branch information
jkomoros committed Nov 12, 2023
1 parent a0bf64b commit 24c3056
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 31 deletions.
15 changes: 6 additions & 9 deletions src/actions/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ import {
} from '../types.js';

import {
AI_DIALOG_TYPE_CARD_SUMMARY,
AI_DIALOG_TYPE_MISSING_CONCEPTS,
AI_DIALOG_TYPE_SUGGEST_TITLE,
TEXT_FIELD_TITLE
} from '../type_constants.js';

Expand Down Expand Up @@ -323,7 +320,7 @@ export const titleForEditingCardWithAI = (count = 5) : ThunkSomeAction => async

const model = DEFAULT_MODEL;

dispatch(aiRequestStarted(AI_DIALOG_TYPE_SUGGEST_TITLE, model));
dispatch(aiRequestStarted('title', model));

let prompt = 'The following is a short essay: ' + CARD_SEPARATOR + body + CARD_SEPARATOR;
prompt += 'Here are examples of good titles of other essays:' + CARD_SEPARATOR + selectGoodTitles(state).join('\n') + CARD_SEPARATOR;
Expand All @@ -348,7 +345,7 @@ export const summarizeCardsWithAI = () : ThunkSomeAction => async (dispatch, get
const uid = selectUid(state);
const cards = selectActiveCollectionCards(state);
const model = DEFAULT_LONG_MODEL;
dispatch(aiRequestStarted(AI_DIALOG_TYPE_CARD_SUMMARY, model));
dispatch(aiRequestStarted('summary', model));

const [prompt, ids] = await cardsAISummaryPrompt(cards, model);
dispatch({
Expand Down Expand Up @@ -389,7 +386,7 @@ export const missingConceptsWithAI = () : ThunkSomeAction => async (dispatch, ge
const uid = selectUid(state);
const cards = selectActiveCollectionCards(state);
const model = DEFAULT_HIGH_FIDELITY_MODEL;
dispatch(aiRequestStarted(AI_DIALOG_TYPE_MISSING_CONCEPTS, model));
dispatch(aiRequestStarted('concepts', model));

const reversedConcepts = selectConcepts(state);
const conceptStrings = distilledConceptStrings(reversedConcepts);
Expand All @@ -412,17 +409,17 @@ export const missingConceptsWithAI = () : ThunkSomeAction => async (dispatch, ge
};

export const AI_DIALOG_TYPE_CONFIGURATION : {[key in AIDialogType] : AIDialogTypeConfiguration} = {
[AI_DIALOG_TYPE_CARD_SUMMARY]: {
'summary': {
title: 'Summarize Cards',
resultType: 'text-block',
},
[AI_DIALOG_TYPE_SUGGEST_TITLE]: {
'title': {
title: 'Suggest Title',
resultType: 'multi-line',
commitAction: commitTitleSuggestion,
rerunAction: titleForEditingCardWithAI
},
[AI_DIALOG_TYPE_MISSING_CONCEPTS]: {
'concepts': {
title: 'Missing Concepts',
resultType: 'tag-list'
}
Expand Down
6 changes: 1 addition & 5 deletions src/reducers/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ import {
AIState
} from '../types.js';

import {
AI_DIALOG_TYPE_CARD_SUMMARY
} from '../type_constants.js';

import {
DEFAULT_MODEL
} from '../actions/ai.js';

const INITIAL_STATE : AIState = {
open: false,
active: false,
kind: AI_DIALOG_TYPE_CARD_SUMMARY,
kind: 'summary',
selectedIndex: -1,
model: DEFAULT_MODEL,
result: [],
Expand Down
6 changes: 1 addition & 5 deletions src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ import {
MAX_SORT_ORDER_VALUE
} from './card_fields.js';

import {
AI_DIALOG_TYPE_CARD_SUMMARY
} from './type_constants.js';

import {
references,
unionReferences,
Expand Down Expand Up @@ -189,7 +185,7 @@ export const selectMultiEditReferencesDiff = (state : State) => state.multiedit

export const selectAIDialogOpen = (state : State) => state.ai ? state.ai.open : false;
export const selectAIActive = (state : State) => state.ai ? state.ai.active : false;
export const selectAIDialogKind = (state : State) : AIDialogType => state.ai ? state.ai.kind : AI_DIALOG_TYPE_CARD_SUMMARY;
export const selectAIDialogKind = (state : State) : AIDialogType => state.ai ? state.ai.kind : 'summary';
export const selectAIResult = (state : State) => state.ai ? state.ai.result : [];
export const selectAIResultIndex = (state : State) => state.ai ? state.ai.selectedIndex : -1;
export const selectAIError = (state : State) => state.ai ? state.ai.error : '';
Expand Down
10 changes: 0 additions & 10 deletions src/type_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,6 @@ export const TAB_CONFIG_TYPES = {
[TAB_CONFIG_RANDOM]: true
};

export const AI_DIALOG_TYPE_CARD_SUMMARY = 'summary';
export const AI_DIALOG_TYPE_SUGGEST_TITLE = 'title';
export const AI_DIALOG_TYPE_MISSING_CONCEPTS = 'concepts';

export const AI_DIALOG_TYPES = {
[AI_DIALOG_TYPE_CARD_SUMMARY]: true,
[AI_DIALOG_TYPE_SUGGEST_TITLE]: true,
[AI_DIALOG_TYPE_MISSING_CONCEPTS]: true
};

export const CARDS_COLLECTION = 'cards';
export const CARD_UPDATES_COLLECTION = 'updates';
export const SECTION_UPDATES_COLLECTION = 'updates';
Expand Down
9 changes: 7 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
TEXT_FIELD_TYPES_EDITABLE,
EDITOR_TAB_TYPES,
EDITOR_CONTENT_TAB_TYPES,
AI_DIALOG_TYPES,
FIND_CARD_TO_LINK,
FIND_CARD_TO_PERMISSION,
FIND_CARD_TO_REFERENCE,
Expand Down Expand Up @@ -1240,7 +1239,13 @@ export type MultiEditState = {

export type AIModelName = 'gpt-3.5-turbo' | 'gpt-3.5-turbo-16k' | 'gpt-4' | 'gpt-4-32k';

export type AIDialogType = keyof(typeof AI_DIALOG_TYPES);
const aiDialogType = z.enum([
'summary',
'title',
'concepts'
]);

export type AIDialogType = z.infer<typeof aiDialogType>;

export type AIState = {
open: boolean;
Expand Down

0 comments on commit 24c3056

Please sign in to comment.