matt HOFFNER commited on
Commit
85ef79d
·
1 Parent(s): a8f0877

append message fix

Browse files
Files changed (1) hide show
  1. components/Playground/index.tsx +20 -26
components/Playground/index.tsx CHANGED
@@ -1,5 +1,5 @@
1
  import React, { useMemo, useRef, useEffect, useState, ChangeEvent, FormEvent } from "react";
2
- import { useChat } from "ai/react";
3
  import { useAppDispatch, useAppSelector } from "../../store/hook";
4
  import {
5
  compiler_state,
@@ -16,6 +16,7 @@ import Footer from "./Footer";
16
  import Pane from "./Pane";
17
  import { SendIcon } from "../../constants/icon";
18
  import ReactMarkdown from "react-markdown";
 
19
 
20
  const Playground = () => {
21
  const dispatch = useAppDispatch();
@@ -51,34 +52,27 @@ const Playground = () => {
51
  };
52
  }, [markdownCode, prevMarkdownCode, dispatch]);
53
 
54
-
55
-
56
-
57
-
58
- const { setMessages, messages, input, setInput, handleSubmit, ...rest } = useChat({
59
  onError: (error) => {
60
  console.error(error);
61
  },
62
- });
63
-
64
- const modifiedHandleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
65
- // Stop the default form submission behavior
66
- e.preventDefault();
67
-
68
- // Prepend the system message if it's not already there
69
- if (messages.length === 0 || messages[0].role !== "system") {
70
- const newMessage = {
71
- role: "system",
72
- content: systemMessage
73
- };
74
- // @ts-ignore
75
- setMessages(prevMessages => [newMessage, ...prevMessages]);
76
- }
77
-
78
- // Call the original handleSubmit from useChat
79
- handleSubmit(e);
80
- };
81
-
82
 
83
  useEffect(() => {
84
  if (!esbuildStatus.isReady) {
 
1
  import React, { useMemo, useRef, useEffect, useState, ChangeEvent, FormEvent } from "react";
2
+ import { Message, useChat } from "ai/react";
3
  import { useAppDispatch, useAppSelector } from "../../store/hook";
4
  import {
5
  compiler_state,
 
16
  import Pane from "./Pane";
17
  import { SendIcon } from "../../constants/icon";
18
  import ReactMarkdown from "react-markdown";
19
+ import { ChatRequestOptions } from "ai";
20
 
21
  const Playground = () => {
22
  const dispatch = useAppDispatch();
 
52
  };
53
  }, [markdownCode, prevMarkdownCode, dispatch]);
54
 
55
+ const { append, messages, input, setInput, handleSubmit, ...rest } = useChat({
 
 
 
 
56
  onError: (error) => {
57
  console.error(error);
58
  },
59
+ });
60
+
61
+ const modifiedHandleSubmit = async (e: React.FormEvent<HTMLFormElement>, chatRequestOptions?: ChatRequestOptions) => {
62
+ e.preventDefault();
63
+
64
+ if (messages.length === 0 || messages[0].role !== "system") {
65
+ const systemMessageToAdd: Message = {
66
+ id: `${Date.now()}-system`,
67
+ createdAt: new Date(),
68
+ content: systemMessage,
69
+ role: "system"
70
+ };
71
+
72
+ await append(systemMessageToAdd);
73
+ }
74
+ await handleSubmit(e, chatRequestOptions);
75
+ };
 
 
 
76
 
77
  useEffect(() => {
78
  if (!esbuildStatus.isReady) {