first commit

This commit is contained in:
2026-04-09 21:35:06 +08:00
commit 22fe6e069c
38 changed files with 12631 additions and 0 deletions

20
src/renderer/http/api.js Normal file
View File

@@ -0,0 +1,20 @@
import { getAction, postAction, deleteAction } from './manage.js'
import url, { getBaseUrl } from './url.js'
// 健康检查
export const getHealthAction = () => getAction(url.health)
// 会话
export const createSessionAction = (data) => postAction(url.session.create, data)
export const getSessionAction = (id) => getAction(url.session.detail(id))
export const listSessionsAction = () => getAction(url.session.list)
export const deleteSessionAction = (id) => deleteAction(url.session.delete(id))
// 消息
export const sendMessageAction = (sessionId, data) => postAction(url.message.send(sessionId), data)
export const listMessagesAction = (sessionId) => getAction(url.message.list(sessionId))
// SSE 事件流(返回 EventSource 实例,由调用方管理生命周期)
export function createEventSource() {
return new EventSource(`${getBaseUrl()}${url.event}`)
}

View File

@@ -0,0 +1,38 @@
import { getBaseUrl } from './url.js'
import { ElMessage } from 'element-plus'
async function request(path, options = {}) {
const { headers = {}, silent = false, ...rest } = options
const url = `${getBaseUrl()}${path}`
const config = {
headers: {
'Content-Type': 'application/json',
...headers,
},
...rest,
}
try {
const res = await fetch(url, config)
if (!res.ok) {
const msg = await res.text().catch(() => '')
const errMsg = `请求失败: ${res.status}${msg ? ' - ' + msg : ''}`
if (!silent) ElMessage.error(errMsg)
return Promise.reject(new Error(errMsg))
}
// 空响应
const text = await res.text()
if (!text) return Promise.resolve(null)
const data = JSON.parse(text)
return Promise.resolve(data)
} catch (err) {
if (!silent) ElMessage.error(err.message || '网络连接失败')
return Promise.reject(err)
}
}
export default request

View File

@@ -0,0 +1,26 @@
import request from './index.js'
export function getAction(url, params) {
const query = params ? '?' + new URLSearchParams(params).toString() : ''
return request(`${url}${query}`, { method: 'GET' })
}
export function postAction(url, data, headers = {}) {
return request(url, {
method: 'POST',
body: JSON.stringify(data),
headers,
})
}
export function putAction(url, data) {
return request(url, {
method: 'PUT',
body: JSON.stringify(data),
})
}
export function deleteAction(url, params) {
const query = params ? '?' + new URLSearchParams(params).toString() : ''
return request(`${url}${query}`, { method: 'DELETE' })
}

28
src/renderer/http/url.js Normal file
View File

@@ -0,0 +1,28 @@
// OpenCode 服务地址由主进程动态分配端口,通过 getBaseUrl() 获取
export function getBaseUrl() {
return window.__opencodeBaseUrl || 'http://127.0.0.1:4096'
}
const url = {
// 健康检查
health: '/global/health',
// 会话
session: {
create: '/session',
detail: (id) => `/session/${id}`,
list: '/session',
delete: (id) => `/session/${id}`,
},
// 消息
message: {
send: (sessionId) => `/session/${sessionId}/message`,
list: (sessionId) => `/session/${sessionId}/message`,
},
// SSE 事件流
event: '/event',
}
export default url