-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…#5006) * feat(playground): Implement editable input variable textareas (#4987) * docs(playground): Improve commenting around playground store usage * refactor(playground): Switch to mostly unstyled codemirror for variable editing * fix(playground): Fix styling issues and flickering when rendering playground variable inputs * refactor(playground): Add and use type guards for playground input type * docs(playground): update comment on variable cache mechanics * fix(playground): fix types and tests post rebase
- Loading branch information
1 parent
767bd37
commit ab09109
Showing
7 changed files
with
205 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,69 @@ | ||
import React from "react"; | ||
|
||
import { Card } from "@arizeai/components"; | ||
import { Flex, Text, View } from "@arizeai/components"; | ||
|
||
import { usePlaygroundContext } from "@phoenix/contexts/PlaygroundContext"; | ||
import { | ||
selectDerivedInputVariables, | ||
selectInputVariableKeys, | ||
} from "@phoenix/store"; | ||
import { assertUnreachable } from "@phoenix/typeUtils"; | ||
|
||
import { VariableEditor } from "./VariableEditor"; | ||
|
||
export function PlaygroundInput() { | ||
const variables = usePlaygroundContext(selectDerivedInputVariables); | ||
const variableKeys = usePlaygroundContext(selectInputVariableKeys); | ||
const setVariableValue = usePlaygroundContext( | ||
(state) => state.setVariableValue | ||
); | ||
const templateLanguage = usePlaygroundContext( | ||
(state) => state.templateLanguage | ||
); | ||
|
||
if (variableKeys.length === 0) { | ||
let templateSyntax = ""; | ||
switch (templateLanguage) { | ||
case "f-string": { | ||
templateSyntax = "{input name}"; | ||
break; | ||
} | ||
case "mustache": { | ||
templateSyntax = "{{input name}}"; | ||
break; | ||
} | ||
default: | ||
assertUnreachable(templateLanguage); | ||
} | ||
return ( | ||
<View padding="size-100"> | ||
<Flex direction="column" justifyContent="center" alignItems="center"> | ||
<Text color="text-700"> | ||
Add variable inputs to your prompt using{" "} | ||
<Text color="text-900">{templateSyntax}</Text> within your prompt | ||
template. | ||
</Text> | ||
</Flex> | ||
</View> | ||
); | ||
} | ||
|
||
return ( | ||
<Card title="Input" collapsible variant="compact"> | ||
Input goes here | ||
</Card> | ||
<Flex direction="column" gap="size-200" width="100%"> | ||
{variableKeys.map((variableKey, i) => { | ||
return ( | ||
<VariableEditor | ||
// using the index as the key actually prevents the UI from | ||
// flickering; if we use the variable key directly, it will | ||
// re-mount the entire editor and cause a flicker because key may | ||
// change rapidly for a given variable | ||
key={i} | ||
label={variableKey} | ||
value={variables[variableKey]} | ||
onChange={(value) => setVariableValue(variableKey, value)} | ||
/> | ||
); | ||
})} | ||
</Flex> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from "react"; | ||
import { githubLight } from "@uiw/codemirror-theme-github"; | ||
import { nord } from "@uiw/codemirror-theme-nord"; | ||
import ReactCodeMirror, { | ||
BasicSetupOptions, | ||
EditorView, | ||
} from "@uiw/react-codemirror"; | ||
|
||
import { Field } from "@arizeai/components"; | ||
|
||
import { CodeWrap } from "@phoenix/components/code"; | ||
import { useTheme } from "@phoenix/contexts"; | ||
|
||
type VariableEditorProps = { | ||
label?: string; | ||
value?: string; | ||
onChange?: (value: string) => void; | ||
}; | ||
|
||
const basicSetupOptions: BasicSetupOptions = { | ||
lineNumbers: false, | ||
highlightActiveLine: false, | ||
foldGutter: false, | ||
highlightActiveLineGutter: false, | ||
bracketMatching: false, | ||
syntaxHighlighting: false, | ||
}; | ||
|
||
const extensions = [EditorView.lineWrapping]; | ||
|
||
export const VariableEditor = ({ | ||
label, | ||
value, | ||
onChange, | ||
}: VariableEditorProps) => { | ||
const { theme } = useTheme(); | ||
const codeMirrorTheme = theme === "light" ? githubLight : nord; | ||
return ( | ||
<Field label={label}> | ||
<CodeWrap width="100%"> | ||
<ReactCodeMirror | ||
theme={codeMirrorTheme} | ||
basicSetup={basicSetupOptions} | ||
value={value} | ||
extensions={extensions} | ||
onChange={onChange} | ||
/> | ||
</CodeWrap> | ||
</Field> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters