Skip to content

Commit

Permalink
refactor(playground): Add and use type guards for playground input type
Browse files Browse the repository at this point in the history
  • Loading branch information
cephalization committed Oct 15, 2024
1 parent 61addf3 commit e30cbd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/src/store/playground/playgroundStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { assertUnreachable } from "@phoenix/typeUtils";
import {
GenAIOperationType,
InitialPlaygroundState,
isManualInput,
PlaygroundChatTemplate,
PlaygroundInputMode,
PlaygroundInstance,
Expand Down Expand Up @@ -294,7 +295,7 @@ export const createPlaygroundStore = (
},
setVariableValue: (key: string, value: string) => {
const input = get().input;
if ("variablesValueCache" in input) {
if (isManualInput(input)) {
set({
input: {
...input,
Expand All @@ -316,7 +317,7 @@ export type PlaygroundStore = ReturnType<typeof createPlaygroundStore>;
* @returns the variable keys
*/
export const selectInputVariableKeys = (state: PlaygroundState) => {
if ("variableKeys" in state.input) {
if (isManualInput(state.input)) {
return state.input.variableKeys;
}
return [];
Expand All @@ -328,7 +329,7 @@ export const selectInputVariableKeys = (state: PlaygroundState) => {
* @returns the derived input variables
*/
export const selectDerivedInputVariables = (state: PlaygroundState) => {
if ("variableKeys" in state.input) {
if (isManualInput(state.input)) {
const input = state.input;
const variableKeys = input.variableKeys;
const variablesValueCache = input.variablesValueCache;
Expand Down
18 changes: 17 additions & 1 deletion app/src/store/playground/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type DatasetInput = {
};

type ManualInput = {
variablesValueCache: Record<string, string>;
variablesValueCache: Record<string, string | undefined>;
variableKeys: string[];
};

Expand Down Expand Up @@ -183,3 +183,19 @@ export interface PlaygroundState extends PlaygroundProps {

setVariableValue: (key: string, value: string) => void;
}

/**
* Check if the input is manual
*/
export const isManualInput = (input: PlaygroundInput): input is ManualInput => {
return "variablesValueCache" in input && "variableKeys" in input;
};

/**
* Check if the input is a dataset
*/
export const isDatasetInput = (
input: PlaygroundInput
): input is DatasetInput => {
return "datasetId" in input;
};

0 comments on commit e30cbd7

Please sign in to comment.