Skip to content

Commit

Permalink
closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
yuruo committed Jul 12, 2024
1 parent c7bbfe9 commit aeaa42a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 24 deletions.
2 changes: 1 addition & 1 deletion app/src/main/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
22 changes: 18 additions & 4 deletions app/src/renderer/src/components/Chat/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) {
Expand All @@ -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>
Expand All @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion app/src/renderer/src/components/CodeEditor/index.tsx
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
30 changes: 30 additions & 0 deletions app/src/renderer/src/hooks/useRunCode.ts
Original file line number Diff line number Diff line change
@@ -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 }
}
25 changes: 7 additions & 18 deletions app/src/renderer/src/pages/Content/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}>
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions server/route/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}


Expand Down

0 comments on commit aeaa42a

Please sign in to comment.