matt HOFFNER commited on
Commit
6bd9e2b
·
1 Parent(s): 5b465ac

fixing redundant message firing

Browse files
Files changed (1) hide show
  1. components/Playground/index.tsx +16 -7
components/Playground/index.tsx CHANGED
@@ -18,7 +18,14 @@ import { SendIcon } from "../../constants/icon";
18
  import ReactMarkdown from "react-markdown";
19
  import { ChatRequestOptions } from "ai";
20
 
21
- const DEFAULT_PROMPT = "You are a helpful AI assistant. Assist the user in writing React code, ensuring that all logic is contained within a single app component file and that the output is rendered to the DOM's root element. Always write React using inline styles and end the code snippet with rendering to root: ReactDOM.render(<App />, document.getElementById('root'));"
 
 
 
 
 
 
 
22
 
23
  const Playground = () => {
24
  const dispatch = useAppDispatch();
@@ -63,20 +70,22 @@ const Playground = () => {
63
  const modifiedHandleSubmit = async (e: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => {
64
  e.preventDefault();
65
 
66
- if (messages.length === 0 || messages[0].role !== "system") {
67
  const systemMessageToAdd: Message = {
68
  id: `${Date.now()}-system`,
69
  createdAt: new Date(),
70
  content: systemMessage,
71
  role: "system"
72
  };
73
-
74
  await append(systemMessageToAdd);
75
- } else {
76
- handleSubmit(e, chatRequestOptions);
77
  }
78
- };
79
-
 
 
 
 
 
80
  useEffect(() => {
81
  if (!esbuildStatus.isReady) {
82
  dispatch(initEsbuild());
 
18
  import ReactMarkdown from "react-markdown";
19
  import { ChatRequestOptions } from "ai";
20
 
21
+ const DEFAULT_PROMPT = `
22
+ You are assisting in a WASM-powered live editor, Codetree, where React code is instantly reflected in the browser. Generate React code with the following constraints:
23
+ 1. All logic should be encapsulated within a single app component file.
24
+ 2. External dependencies or imports outside of the React core are not permitted.
25
+ 3. Always use inline styles for styling.
26
+ 4. Code snippets should conclude with rendering to the DOM's root element: ReactDOM.render(<App />, document.getElementById('root'));
27
+ 5. Ensure the code is optimized for a live browser environment and adheres strictly to React best practices.
28
+ `;
29
 
30
  const Playground = () => {
31
  const dispatch = useAppDispatch();
 
70
  const modifiedHandleSubmit = async (e: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => {
71
  e.preventDefault();
72
 
73
+ if (messages.length === 0) {
74
  const systemMessageToAdd: Message = {
75
  id: `${Date.now()}-system`,
76
  createdAt: new Date(),
77
  content: systemMessage,
78
  role: "system"
79
  };
 
80
  await append(systemMessageToAdd);
 
 
81
  }
82
+ if (messages.length === 1 && messages[0].role === "system") {
83
+ await handleSubmit(e, chatRequestOptions);
84
+ }
85
+ if (messages.length > 1) {
86
+ await handleSubmit(e, chatRequestOptions);
87
+ }
88
+ }
89
  useEffect(() => {
90
  if (!esbuildStatus.isReady) {
91
  dispatch(initEsbuild());