Skip to content

Conversation

cephalization
Copy link
Contributor

@cephalization cephalization commented Jan 31, 2025

Resolves: #6191

This PR contains two major changes, and should be viewed by-commit.

Normalize playground instance messages


There is now a split in instance types:

  • PlaygroundInstance
  • PlaygroundNormalizedInstance

The main difference is that chat templates within a normalized instance only contain messageIds instead of message objects.

The PlaygroundStore now only deals in PlaygroundNormalizedInstance, and handles conversion from PlaygroundInstance to PlaygroundNormalizedInstance during initialization.

Convert code mirror editors on playground to be uncontrolled


  • TemplateEditor
  • VariableEditor
  • PlaygroundTool (editor)

All of this is in service of fixing a react-codemirror bug that breaks rendering when updates take too long, which was occurring because messages were stored deeply within an instance causing a cascade of re-renders on every keystroke.

@cephalization cephalization marked this pull request as ready for review January 31, 2025 21:54
@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Jan 31, 2025
Comment on lines +67 to +71
useEffect(() => {
if (readOnly) {
setValue(defaultValue);
}
}, [readOnly, defaultValue]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confused why this useEffect is needed. Isn't value defaulting to defaultValue anyways?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is defaulting to it, but never changes from it. This gives codemirror a stable "value" which it was already treating as a default value. Confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The useEffect makes sure to update the value with the new defaultValue if we are in readOnly mode. This lets codemirror blast away when new changes come in like normal, so that the caller doesn't have to mess with keys and remounting

Copy link
Collaborator

@mikeldking mikeldking left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Def. much better behavior.

Comment on lines +48 to +54
useEffect(() => {
if (defaultValue == null) {
setInitialValue("");
setVersion((prev) => prev + 1);
}
valueRef.current = defaultValue;
}, [defaultValue]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this useEffect needed? Seems un-necessary?

valueRef.current = defaultValue;
}, [defaultValue]);
useEffect(() => {
setInitialValue(valueRef.current);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit confused why this is even entirely needed. Shouldn't we just not have initial values for this stuff?

if (instance.template.__type === "chat") {
const normalizedTemplate = normalizeChatTemplate(instance.template);
messageMap = {
...messageMap,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does messageMap need to be spread in here? It's largely an un-initialized object on line 190?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are accumulating messages for each initial instance. it is initialized as an empty object but accumulates every loop.

@cephalization cephalization force-pushed the cephalization/prompts/6191-playground-editors-focus branch from c454345 to fca3202 Compare February 3, 2025 15:35
@cephalization cephalization merged commit b971d08 into prompts Feb 3, 2025
6 checks passed
@cephalization cephalization deleted the cephalization/prompts/6191-playground-editors-focus branch February 3, 2025 18:39
mikeldking pushed a commit that referenced this pull request Feb 6, 2025
* refactor(prompts): Normalize playground instance messages

* Update use-zustand package to improve performance

* Convert TemplateEditor, VariableEditor, and PlaygroundTool (editor) to uncontrolled components

Workaround for uiwjs/react-codemirror#694

* Clean up variable names and semantics

* Mark instance as dirty when updating message
mikeldking pushed a commit that referenced this pull request Feb 19, 2025
* refactor(prompts): Normalize playground instance messages

* Update use-zustand package to improve performance

* Convert TemplateEditor, VariableEditor, and PlaygroundTool (editor) to uncontrolled components

Workaround for uiwjs/react-codemirror#694

* Clean up variable names and semantics

* Mark instance as dirty when updating message
s-yeddula pushed a commit that referenced this pull request Mar 5, 2025
* refactor(prompts): Normalize playground instance messages

* Update use-zustand package to improve performance

* Convert TemplateEditor, VariableEditor, and PlaygroundTool (editor) to uncontrolled components

Workaround for uiwjs/react-codemirror#694

* Clean up variable names and semantics

* Mark instance as dirty when updating message
jaywcjlove added a commit to uiwjs/react-codemirror that referenced this pull request Jun 27, 2025
github-actions bot pushed a commit to uiwjs/react-codemirror that referenced this pull request Jun 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:XL This PR changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants