mirror of
https://github.com/yuruotong1/autoMate.git
synced 2025-12-26 05:16:21 +08:00
closes #9
This commit is contained in:
parent
c7bbfe98c5
commit
aeaa42afb6
@ -18,7 +18,7 @@ export const config = {
|
||||
initShow: true,
|
||||
width: 1300,
|
||||
height: 700,
|
||||
openDevTools: true,
|
||||
openDevTools: false,
|
||||
frame: true,
|
||||
transparent: false,
|
||||
hash: '/#config/category/contentList'
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { ProChat, ProChatInstance } from '@ant-design/pro-chat';
|
||||
import useChat from '@renderer/hooks/useChat';
|
||||
import useRunCode from '@renderer/hooks/useRunCode';
|
||||
import { useStore } from '@renderer/store/useStore';
|
||||
import { Button } from 'antd';
|
||||
import { useTheme } from 'antd-style';
|
||||
@ -13,6 +14,8 @@ export default function Chat(props: {id: number, revalidator: () => void, search
|
||||
const proChatRef = useRef<ProChatInstance>();
|
||||
// 确保 useeffect 只执行一次
|
||||
const effectRan = useRef(false);
|
||||
const {id, revalidator} = props;
|
||||
const { runCode } = useRunCode()
|
||||
|
||||
useEffect(()=>{
|
||||
if (effectRan.current === false) {
|
||||
@ -30,17 +33,28 @@ export default function Chat(props: {id: number, revalidator: () => void, search
|
||||
}}
|
||||
chatItemRenderConfig={{
|
||||
contentRender: (props, defaultDom) => {
|
||||
if (props.originData?.role === 'coder') {
|
||||
if (props.originData!.role === 'coder' && props.originData!.originData) {
|
||||
try {
|
||||
// const resJson = JSON.parse(item?.originData?.content);
|
||||
return (<div className='flex flex-row'>
|
||||
<Button onClick={()=>{
|
||||
console.log('运行')
|
||||
const code = props.originData!.originData.code;
|
||||
runCode(code)
|
||||
}}>
|
||||
运行
|
||||
</Button>
|
||||
<Button className='ml-2' onClick={()=>{
|
||||
console.log('应用')
|
||||
// 更新代码
|
||||
window.api.sql(
|
||||
`update contents set content=@content where id=@id`,
|
||||
"update",
|
||||
{
|
||||
content: props.originData!.originData.code,
|
||||
id
|
||||
}
|
||||
)
|
||||
// 让代码展示区域重新加载
|
||||
revalidator()
|
||||
}}>
|
||||
应用
|
||||
</Button>
|
||||
@ -63,7 +77,7 @@ export default function Chat(props: {id: number, revalidator: () => void, search
|
||||
request={async (messages) => {
|
||||
const response = await getResponse(messages)
|
||||
console.log(response)
|
||||
if (response.isExistCode === 0) {
|
||||
if (response.status === 0 && response.code != "") {
|
||||
setTimeout(() => {
|
||||
proChatRef.current?.pushChat({
|
||||
type: 'text',
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import CodeMirror from '@uiw/react-codemirror';
|
||||
import { python } from '@codemirror/lang-python';
|
||||
import { Drawer, FloatButton, Spin } from 'antd';
|
||||
import { Drawer, FloatButton } from 'antd';
|
||||
import { QuestionCircleOutlined } from '@ant-design/icons';
|
||||
import Chat from '@renderer/components/Chat';
|
||||
import "./codeEditor.scss"
|
||||
|
||||
30
app/src/renderer/src/hooks/useRunCode.ts
Normal file
30
app/src/renderer/src/hooks/useRunCode.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import { localServerBaseUrl } from "@renderer/config";
|
||||
import { useStore } from "@renderer/store/useStore";
|
||||
|
||||
export default()=>{
|
||||
const setChatMessages = useStore(state => state.setChatMessage)
|
||||
const chatMessages = useStore(state => state.chatMessages)
|
||||
const runCode = async function (code: string) {
|
||||
const res = await fetch(localServerBaseUrl + "/execute", {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
code: code
|
||||
})
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
setChatMessages([...chatMessages,
|
||||
{
|
||||
role: "coder",
|
||||
content: "代码运行结果如下:" + JSON.stringify(data),
|
||||
createAt: new Date().getTime(),
|
||||
updateAt: new Date().getTime(),
|
||||
id: new Date().getTime().toString()
|
||||
}])
|
||||
}
|
||||
|
||||
return { runCode }
|
||||
}
|
||||
@ -2,9 +2,9 @@ import { Form, useLoaderData, useRevalidator, useSubmit } from "react-router-dom
|
||||
import "./content.scss"
|
||||
import CodeEditor from "@renderer/components/CodeEditor"
|
||||
import { Button } from "antd"
|
||||
import { localServerBaseUrl } from "@renderer/config"
|
||||
import { useState } from "react"
|
||||
import { useStore } from "@renderer/store/useStore"
|
||||
import useRunCode from "@renderer/hooks/useRunCode"
|
||||
|
||||
|
||||
export const Content = () => {
|
||||
const { content, categories, search } = useLoaderData() as {
|
||||
@ -15,8 +15,7 @@ export const Content = () => {
|
||||
const revalidator = useRevalidator();
|
||||
const submit = useSubmit()
|
||||
const [open, setOpen] = useState(false);
|
||||
const setChatMessages = useStore(state => state.setChatMessage)
|
||||
const chatMessages = useStore(state => state.chatMessages)
|
||||
const { runCode } = useRunCode()
|
||||
return (
|
||||
<Form method="PUT">
|
||||
<main className="content-page" key={content.id}>
|
||||
@ -34,20 +33,10 @@ export const Content = () => {
|
||||
))}
|
||||
</select>
|
||||
<Button onClick={async () => {
|
||||
const code_content = (await window.api.sql(`select * from contents where id = ${content.id}`, "findOne")) as ContentType
|
||||
const res = await fetch(localServerBaseUrl + "/execute", {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
code: code_content.content
|
||||
})
|
||||
});
|
||||
const data = await res.json();
|
||||
setOpen(true)
|
||||
setChatMessages([...chatMessages,
|
||||
{ role: "assistant", content: "代码运行结果如下:" + JSON.stringify(data), createAt: new Date().getTime(), updateAt: new Date().getTime(), id: new Date().getTime().toString() }])
|
||||
setOpen(true)
|
||||
const code_content = (await window.api.sql(`select * from contents where id = ${content.id}`, "findOne")) as ContentType
|
||||
const code = code_content.content
|
||||
runCode(code)
|
||||
}}>运行</Button>
|
||||
</div>
|
||||
<CodeEditor
|
||||
|
||||
@ -19,9 +19,11 @@ def llm():
|
||||
config = json.loads(get_config())["llm"]
|
||||
messages = [{"role": "system", "content": code_prompt.substitute()}] + messages
|
||||
try:
|
||||
print(config)
|
||||
res = completion(messages=messages, **config).choices[0].message.content
|
||||
return {"content": res, "code": extract_code_blocks(res), "status": 0}
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return {"content": str(e), "status": 1}
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user