修复 setting 配置存储为 json 时问题

This commit is contained in:
ruotongyu
2024-06-28 00:02:24 +08:00
parent 3ded08ee8a
commit f53ff16413
10 changed files with 42 additions and 31 deletions

View File

@@ -22,7 +22,8 @@ export function createWindow(options: OptionsType): BrowserWindow { // Create t
...(process.platform === 'linux' ? { icon } : {}),
webPreferences: {
preload: join(__dirname, '../preload/index.js'),
sandbox: false
sandbox: false,
webSecurity: false // 禁用web安全性
}
}, options))
// 如果是在开发环境下并且选项是打开开发者工具

View File

@@ -38,7 +38,8 @@ initData()
function initData() {
const initData = findOne('select * from config')
if (initData) return
db().exec(`insert into config (id, content) values(1,'{"shortCut":"Alt+d","llm": {"model": "gpt-4-turbo", "apiKey": "sk-xxx", "baseURL": "https://api.openai.com/v1"}}')`)
const llm = {model: "gpt-4-turbo", apiKey: "sk-xxx", baseURL: "https://api.openai.com/v1"}
db().exec(`insert into config (id, content) values(1,'{"shortCut":"Alt+d","llm": ${JSON.stringify(llm)}}')`)
}

View File

@@ -41,3 +41,4 @@ app.on('window-all-closed', () => {
// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.

View File

@@ -1,7 +1,7 @@
import { IpcMainInvokeEvent, dialog } from "electron"
import { ipcMain } from "electron"
import { getWindowByName } from "./windows"
import { config, findOne } from "./db/query"
import { findOne } from "./db/query"
const { app, globalShortcut } = require('electron')
ipcMain.handle("shortCut", (_event: IpcMainInvokeEvent) => {

View File

@@ -52,6 +52,7 @@ export const config = {
// 根据名称获取窗口
export const getWindowByName = (name: WindowNameType)=>{
// 根据id取得窗口
let win = BrowserWindow.fromId(config[name].id)
// 避免重复点击重复创建窗口
@@ -70,9 +71,9 @@ export const getWindowByEvent = (event: IpcMainEvent | IpcMainInvokeEvent) => {
app.whenReady().then(() => {
// getWindowByName('search')
getWindowByName('search')
// getWindowByName('code')
// getWindowByName('config')
getWindowByName('config')
getWindowByName('chat')
})

View File

@@ -1,21 +0,0 @@
import { StreamingTextResponse, streamText } from "ai";
import { createOpenAI } from '@ai-sdk/openai';
export async function useChat(messages: any){
const configType = await window.api.getConfig()
const llm = JSON.parse(JSON.parse(configType.content).llm)
const openai = createOpenAI({
// custom settings, e.g.
apiKey: llm.apiKey, // your openai key
baseURL: llm.baseURL, // if u dont need change baseUrlyou can delete this line
compatibility: 'compatible',
});
const stream = await streamText({
model: openai(llm.model),
messages: messages,
});
console.log('stream', stream)
return new StreamingTextResponse(stream.textStream);
}

View File

@@ -0,0 +1,25 @@
import { createOpenAI } from "@ai-sdk/openai";
import { StreamingTextResponse, streamText } from "ai";
// import { createOpenAI } from '@ai-sdk/openai';
export async function useChat(chat_messages: Array<any>){
const configType = (await window.api.getConfig()) as ConfigType
const config = JSON.parse(configType.content) as ConfigDataType
const messages = chat_messages.map((m) => {
return {
role: m.role,
content: m.content
}
})
const openai = createOpenAI({
apiKey: config.llm.apiKey,
baseURL: config.llm.baseURL,
});
const stream = await streamText({
model: openai(config.llm.model),
messages: messages,
});
return new StreamingTextResponse(stream.textStream) // 支持流式和非流式
}

View File

@@ -10,5 +10,5 @@ ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<RouterProvider router={router} />
</React.StrictMode>
</React.StrictMode>
)

View File

@@ -1,5 +1,5 @@
import { ProChat } from '@ant-design/pro-chat';
import {useChat} from '@renderer/hooks/route';
import {useChat} from '@renderer/hooks/useChat';
import { useTheme } from 'antd-style';
import "./chat-page.scss"
import Code from './Code';
@@ -18,8 +18,7 @@ export const Chat = () => {
<div className='text-black'>Agent助手</div>
}
request={async (messages) => {
console.log('messages', JSON.stringify(messages))
const response = await useChat(JSON.stringify(messages))
const response = await useChat(messages)
// 使用 Message 作为参数发送请求
return response// 支持流式和非流式
}}

View File

@@ -44,7 +44,11 @@ export const Setting = () => {
name="llm"
defaultValue={JSON.stringify(config.llm)}
onChange={(e)=>{
submit({...config, llm: e.currentTarget.value}, {method: "POST"})
window.api.sql(`update config set content=@content where id = 1`,
'update',
{
content: JSON.stringify({...config, llm: JSON.parse(e.currentTarget.value)})
})
}}
/>
</section>