feat: 开发对话功能

This commit is contained in:
2026-04-11 16:54:09 +08:00
parent 5151379726
commit 8c2ea4488b
8 changed files with 466 additions and 167 deletions

View File

@@ -199,6 +199,18 @@ function registerIpcHandlers() {
return res.json();
});
ipcMain.handle('opencode:session:prompt-async', async (_e, sessionId, text) => {
if (!opencodePort) throw new Error('OpenCode 服务未启动');
const res = await fetch(`http://127.0.0.1:${opencodePort}/session/${sessionId}/prompt_async`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ parts: [{ type: 'text', text }] }),
});
if (!res.ok) throw new Error(`发送消息失败: ${res.status}`);
// 204 No Content无需解析响应体
return true;
});
// Bonjour
ipcMain.handle('bonjour:get-services', () => getDiscoveredServices());

View File

@@ -15,6 +15,7 @@ contextBridge.exposeInMainWorld('opencode', {
health: () => ipcRenderer.invoke('opencode:health'),
createSession: (data) => ipcRenderer.invoke('opencode:session:create', data),
sendMessage: (sessionId, text) => ipcRenderer.invoke('opencode:session:send', sessionId, text),
promptAsync: (sessionId, text) => ipcRenderer.invoke('opencode:session:prompt-async', sessionId, text),
});
contextBridge.exposeInMainWorld('bonjour', {

View File

@@ -1,20 +1,21 @@
import { getAction, postAction, deleteAction } from './manage.js'
import url, { getBaseUrl } from './url.js'
import { getAction, postAction, deleteAction } from './manage.js';
import url, { getBaseUrl } from './url.js';
// 健康检查
export const getHealthAction = () => getAction(url.health)
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 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))
export const sendMessageAction = (sessionId, data) => postAction(url.message.send(sessionId), data);
export const promptAsyncAction = (sessionId, data) => postAction(url.message.promptAsync(sessionId), data);
export const listMessagesAction = (sessionId) => getAction(url.message.list(sessionId));
// SSE 事件流(返回 EventSource 实例,由调用方管理生命周期)
export function createEventSource() {
return new EventSource(`${getBaseUrl()}${url.event}`)
return new EventSource(`${getBaseUrl()}${url.event}`);
}

View File

@@ -1,6 +1,6 @@
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { getBaseUrl } from './url.js'
import axios from 'axios';
import { ElMessage } from 'element-plus';
import { getBaseUrl } from './url.js';
// baseURL 由主进程动态分配端口,通过 getBaseUrl() 运行时获取
const axiosInstance = axios.create({
@@ -10,131 +10,142 @@ const axiosInstance = axios.create({
'Content-Type': 'application/json;charset=utf-8',
},
responseType: 'json',
})
});
// 每次请求前动态更新 baseURL确保服务启动后端口变更能被感知
axiosInstance.interceptors.request.use((config) => {
config.baseURL = getBaseUrl();
return config;
});
// 请求拦截
axiosInstance.interceptors.request.use((config) => {
config.headers = config.headers || {}
let Authorization = localStorage.getItem('Authorization')
config.headers = config.headers || {};
let Authorization = localStorage.getItem('Authorization');
// 优先使用本地持久化的 Authorization 头(完整值)
config.headers.Authorization = Authorization || ''
config.headers.Authorization = Authorization || '';
if ('get' === config?.method?.toLowerCase()) {
if (config.params) {
config.params.timestamp = new Date().getTime()
config.params.timestamp = new Date().getTime();
}
}
// 移除敏感信息日志
// console.log(config, 'axios request.use config')
return config
})
return config;
});
axiosInstance.interceptors.response.use(
(response) => {
// 移除敏感信息日志
// console.log(response, 'response response')
// 若请求为二进制下载blob直接透传响应交由调用方自行处理
try {
const isBlob = response?.config?.responseType === 'blob'
const isBlob = response?.config?.responseType === 'blob';
if (isBlob) {
// 仍然尝试持久化可能返回的 Authorization
const respHeaders = response?.headers || {}
const newAuthorization = respHeaders['authorization'] || respHeaders['Authorization']
const respHeaders = response?.headers || {};
const newAuthorization = respHeaders['authorization'] || respHeaders['Authorization'];
if (newAuthorization && typeof newAuthorization === 'string') {
localStorage.setItem('Authorization', newAuthorization)
localStorage.setItem('Authorization', newAuthorization);
}
return response
return response;
}
} catch (e) {
console.log(e)
console.log(e);
// 忽略检查失败
}
// 如果响应头里带有 Authorization则使用 useStorage 持久化到 localStorage
// 以便后续请求自动携带该请求头
try {
const respHeaders = response?.headers || {}
const newAuthorization = respHeaders['authorization'] || respHeaders['Authorization']
const respHeaders = response?.headers || {};
const newAuthorization = respHeaders['authorization'] || respHeaders['Authorization'];
if (newAuthorization && typeof newAuthorization === 'string') {
localStorage.setItem('Authorization', newAuthorization)
localStorage.setItem('Authorization', newAuthorization);
}
} catch (e) {
// 忽略持久化失败,避免影响主流程
console.warn('持久化 Authorization 失败:', e)
console.warn('持久化 Authorization 失败:', e);
}
// 204 No Content如 prompt_async直接视为成功
if (response.status === 204) {
return Promise.resolve(null);
}
if (response.status === 200) {
const res = response.data || {}
const code = res.code
const msg = res.message || res.msg
const res = response.data || {};
const code = res.code;
const msg = res.message || res.msg;
// 明确的 200 成功,但需要按业务码再判断
if (code === 0) {
// 业务成功
return Promise.resolve(res)
return Promise.resolve(res);
}
// 特殊业务码处理
if (code === 401) {
// 清除持久化的 Authorization避免后续使用失效的头部
localStorage.removeItem('Authorization')
sessionStorage.removeItem('Token')
localStorage.removeItem('Authorization');
sessionStorage.removeItem('Token');
// 延迟跳转,确保消息显示
setTimeout(() => {
window.location.href = '/#/login'
}, 500)
return Promise.reject(new Error('认证失败,请重新登录'))
window.location.href = '/#/login';
}, 500);
return Promise.reject(new Error('认证失败,请重新登录'));
}
// 其余非 0 的业务码统一拦截提示,但不在这里显示 ElMessage
// 交由业务层使用 await-to-js 处理
return Promise.reject(new Error(msg || '请求失败'))
return Promise.reject(new Error(msg || '请求失败'));
}
// 非 2xx 按错误分支处理(通常会进入 error 拦截器)
return Promise.reject(new Error('请求失败'))
return Promise.reject(new Error('请求失败'));
},
(error) => {
console.error('请求错误:', error)
console.error('请求错误:', error);
if (error.response) {
// 服务器响应错误
const status = error.response.status
const message = error.response.data?.message || error.response.data?.msg || '请求失败'
const status = error.response.status;
const message = error.response.data?.message || error.response.data?.msg || '请求失败';
switch (status) {
case 400:
ElMessage.error(`无效的请求参数:${message}`)
break
ElMessage.error(`无效的请求参数:${message}`);
break;
case 401:
// 清除持久化的 Authorization避免后续使用失效的头部
localStorage.removeItem('Authorization')
ElMessage.error('未授权访问或登录已过期,请重新登录')
break
localStorage.removeItem('Authorization');
ElMessage.error('未授权访问或登录已过期,请重新登录');
break;
case 403:
ElMessage.error('访问被拒绝')
break
ElMessage.error('访问被拒绝');
break;
case 404:
ElMessage.error('资源未找到')
break
ElMessage.error('资源未找到');
break;
case 500:
ElMessage.error('服务器内部错误')
break
ElMessage.error('服务器内部错误');
break;
case 502:
case 503:
case 504:
ElMessage.error('服务暂时不可用,请稍后重试')
break
ElMessage.error('服务暂时不可用,请稍后重试');
break;
default:
ElMessage.error(`请求失败: ${message}`)
ElMessage.error(`请求失败: ${message}`);
}
} else if (error.request) {
// 网络错误
ElMessage.error('网络连接失败,请检查网络连接')
ElMessage.error('网络连接失败,请检查网络连接');
} else {
// 其他错误
ElMessage.error('请求发送失败')
ElMessage.error('请求发送失败');
}
return Promise.reject(error)
},
)
return Promise.reject(error);
}
);
export default axiosInstance
export default axiosInstance;

View File

@@ -1,6 +1,6 @@
// OpenCode 服务地址由主进程动态分配端口,通过 getBaseUrl() 获取
export function getBaseUrl() {
return window.__opencodeBaseUrl || 'http://127.0.0.1:4096'
return window.__opencodeBaseUrl || 'http://127.0.0.1:4096';
}
const url = {
@@ -18,11 +18,12 @@ const url = {
// 消息
message: {
send: (sessionId) => `/session/${sessionId}/message`,
promptAsync: (sessionId) => `/session/${sessionId}/prompt_async`,
list: (sessionId) => `/session/${sessionId}/message`,
},
// SSE 事件流
event: '/event',
}
};
export default url
export default url;

View File

@@ -7,12 +7,8 @@
<span class="status-text">{{ statusText }}</span>
</div>
<div class="status-actions">
<el-button v-if="!isRunning" size="small" type="primary" :loading="isStarting" @click="startService">
启动服务
</el-button>
<el-button v-else size="small" type="danger" plain @click="stopService">
停止服务
</el-button>
<el-button v-if="!isRunning" size="small" type="primary" :loading="isStarting" @click="startService"> 启动服务 </el-button>
<el-button v-else size="small" type="danger" plain @click="stopService"> 停止服务 </el-button>
</div>
</div>
@@ -22,12 +18,7 @@
<el-icon :size="40" color="#c0c4cc"><ChatDotRound /></el-icon>
<p>启动服务后开始对话</p>
</div>
<div
v-for="msg in messages"
:key="msg.id"
class="bubble-wrap"
:class="msg.role"
>
<div v-for="msg in messages" :key="msg.id" class="bubble-wrap" :class="msg.role">
<div class="bubble">
<pre class="bubble-text">{{ msg.text }}</pre>
</div>
@@ -45,153 +36,171 @@
resize="none"
@keydown.ctrl.enter.prevent="send"
/>
<el-button
type="primary"
:disabled="!isRunning || isSending || !inputText.trim()"
:loading="isSending"
@click="send"
>
发送
</el-button>
<el-button type="primary" :disabled="!isRunning || isSending || !inputText.trim()" :loading="isSending" @click="send"> 发送 </el-button>
</div>
</div>
</template>
<script setup>
import { ref, computed, onUnmounted, nextTick } from 'vue'
import { ElMessage } from 'element-plus'
import { ChatDotRound } from '@element-plus/icons-vue'
import { createEventSource } from '@/http/api.js'
import { ref, computed, onUnmounted, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import { ChatDotRound } from '@element-plus/icons-vue';
import { createEventSource } from '@/http/api.js';
const isRunning = ref(false)
const isStarting = ref(false)
const isSending = ref(false)
const inputText = ref('')
const messages = ref([])
const messagesRef = ref(null)
const currentSessionId = ref(null)
let eventSource = null
const isRunning = ref(false);
const isStarting = ref(false);
const isSending = ref(false);
const inputText = ref('');
const messages = ref([]);
const messagesRef = ref(null);
const currentSessionId = ref(null);
const assistantMessageIds = new Set();
let eventSource = null;
const serviceStatus = computed(() => {
if (isStarting.value) return 'starting'
return isRunning.value ? 'running' : 'stopped'
})
if (isStarting.value) return 'starting';
return isRunning.value ? 'running' : 'stopped';
});
const statusText = computed(() => {
if (isStarting.value) return '正在启动...'
return isRunning.value ? '服务运行中' : '服务未启动'
})
if (isStarting.value) return '正在启动...';
return isRunning.value ? '服务运行中' : '服务未启动';
});
function scrollToBottom() {
nextTick(() => {
if (messagesRef.value) {
messagesRef.value.scrollTop = messagesRef.value.scrollHeight
messagesRef.value.scrollTop = messagesRef.value.scrollHeight;
}
})
});
}
function upsertAssistantBubble(msgId, text) {
const existing = messages.value.find(m => m.id === msgId)
const existing = messages.value.find((m) => m.id === msgId);
if (existing) {
existing.text = text
existing.text = text;
} else {
messages.value.push({ id: msgId, role: 'assistant', text })
messages.value.push({ id: msgId, role: 'assistant', text });
}
scrollToBottom()
scrollToBottom();
}
function connectSSE() {
if (eventSource) eventSource.close()
eventSource = createEventSource()
if (eventSource) eventSource.close();
eventSource = createEventSource();
eventSource.onmessage = (e) => {
try {
const data = JSON.parse(e.data)
const props = data.properties || {}
const data = JSON.parse(e.data);
// 打印所有 SSE 事件,便于调试事件结构
console.log('[SSE]', data.type, JSON.stringify(data));
const props = data.properties || {};
if (data.type === 'message.part.updated') {
const part = props.part
if (!part || part.type !== 'text') return
if (part.sessionID !== currentSessionId.value) return
upsertAssistantBubble(part.messageID, part.text || '')
const part = props.part;
if (!part || part.type !== 'text') return;
if (part.sessionID !== currentSessionId.value) return;
// 通过 messageID 前缀区分用户/助手消息:只渲染 assistant 消息的 part
// assistant 消息的 messageID 会在 message.updated 事件中记录,用 assistantMessageIds 集合过滤
if (!assistantMessageIds.has(part.messageID)) return;
upsertAssistantBubble(part.messageID, part.text || '');
}
if (data.type === 'message.completed') {
isSending.value = false
if (data.type === 'message.updated') {
const info = props.info;
// 记录 assistant 消息的 ID供 message.part.updated 过滤使用
if (info && info.role === 'assistant' && info.sessionID === currentSessionId.value) {
assistantMessageIds.add(info.id);
}
}
if (data.type === 'session.idle') {
// session.idle 表示 AI 响应已全部完成,重置发送状态
if (props.sessionID === currentSessionId.value) {
isSending.value = false;
}
}
} catch (_) {}
}
};
eventSource.onerror = () => {
isSending.value = false
}
isSending.value = false;
};
}
async function startService() {
isStarting.value = true
isStarting.value = true;
try {
const info = await window.opencode.start()
isRunning.value = info.running
const info = await window.opencode.start();
isRunning.value = info.running;
// 更新 baseUrl 供 http 层使用
if (info.url) window.__opencodeBaseUrl = info.url
connectSSE()
ElMessage.success('服务已启动')
if (info.url) window.__opencodeBaseUrl = info.url;
connectSSE();
ElMessage.success('服务已启动');
} catch (err) {
ElMessage.error(`启动失败: ${err.message}`)
ElMessage.error(`启动失败: ${err.message}`);
} finally {
isStarting.value = false
isStarting.value = false;
}
}
async function stopService() {
await window.opencode.stop()
isRunning.value = false
currentSessionId.value = null
messages.value = []
if (eventSource) { eventSource.close(); eventSource = null }
ElMessage.info('服务已停止')
await window.opencode.stop();
isRunning.value = false;
currentSessionId.value = null;
messages.value = [];
assistantMessageIds.clear();
if (eventSource) {
eventSource.close();
eventSource = null;
}
ElMessage.info('服务已停止');
}
async function send() {
const text = inputText.value.trim()
if (!text || isSending.value) return
const text = inputText.value.trim();
if (!text || isSending.value) return;
// 首次发送时创建会话
if (!currentSessionId.value) {
try {
const session = await window.opencode.createSession()
currentSessionId.value = session.id
const session = await window.opencode.createSession();
currentSessionId.value = session.id;
} catch (err) {
ElMessage.error(`创建会话失败: ${err.message}`)
return
ElMessage.error(`创建会话失败: ${err.message}`);
return;
}
}
messages.value.push({ id: Date.now(), role: 'user', text })
inputText.value = ''
isSending.value = true
scrollToBottom()
messages.value.push({ id: Date.now(), role: 'user', text });
inputText.value = '';
isSending.value = true;
scrollToBottom();
try {
await window.opencode.sendMessage(currentSessionId.value, text)
await window.opencode.promptAsync(currentSessionId.value, text);
// 发送成功后等待 SSE 事件流推送 AI 响应isSending 由 message.completed 事件重置
} catch (err) {
ElMessage.error(`发送失败: ${err.message}`)
isSending.value = false
ElMessage.error(`发送失败: ${err.message}`);
isSending.value = false;
}
}
// 初始化时同步服务状态
window.opencode?.info().then((info) => {
isRunning.value = info.running
if (info.running) {
if (info.url) window.__opencodeBaseUrl = info.url
connectSSE()
}
}).catch(() => {})
window.opencode
?.info()
.then((info) => {
isRunning.value = info.running;
if (info.running) {
if (info.url) window.__opencodeBaseUrl = info.url;
connectSSE();
}
})
.catch(() => {});
onUnmounted(() => {
if (eventSource) eventSource.close()
})
if (eventSource) eventSource.close();
});
</script>
<style scoped>
@@ -228,13 +237,25 @@ onUnmounted(() => {
flex-shrink: 0;
}
.dot.stopped { background: #c0c4cc; }
.dot.starting { background: #e6a23c; animation: pulse 1s infinite; }
.dot.running { background: #67c23a; }
.dot.stopped {
background: #c0c4cc;
}
.dot.starting {
background: #e6a23c;
animation: pulse 1s infinite;
}
.dot.running {
background: #67c23a;
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.3; }
0%,
100% {
opacity: 1;
}
50% {
opacity: 0.3;
}
}
.messages {
@@ -263,8 +284,12 @@ onUnmounted(() => {
display: flex;
}
.bubble-wrap.user { justify-content: flex-end; }
.bubble-wrap.assistant { justify-content: flex-start; }
.bubble-wrap.user {
justify-content: flex-end;
}
.bubble-wrap.assistant {
justify-content: flex-start;
}
.bubble {
max-width: 75%;