feat: 对话功能开发

This commit is contained in:
2026-04-12 12:58:13 +08:00
parent 6ca081afa8
commit fad8c10e69
2 changed files with 24 additions and 17 deletions

View File

@@ -44,7 +44,7 @@
<script setup> <script setup>
import { ref, computed, onMounted, onUnmounted, nextTick, watch } from 'vue'; import { ref, computed, onMounted, onUnmounted, nextTick, watch } from 'vue';
import { useRoute } from 'vue-router'; import { useRoute, useRouter } from 'vue-router';
import { ElMessage } from 'element-plus'; import { ElMessage } from 'element-plus';
import { ChatDotRound, ArrowRight, ArrowDown } from '@element-plus/icons-vue'; import { ChatDotRound, ArrowRight, ArrowDown } from '@element-plus/icons-vue';
import { useAppStore } from '@/stores/app.js'; import { useAppStore } from '@/stores/app.js';
@@ -52,6 +52,7 @@ import { sseManager } from '@/http/sse.js';
import axios from 'axios'; import axios from 'axios';
const route = useRoute(); const route = useRoute();
const router = useRouter();
const appStore = useAppStore(); const appStore = useAppStore();
const isSending = ref(false); const isSending = ref(false);
const inputText = ref(''); const inputText = ref('');
@@ -144,10 +145,25 @@ async function loadHistoryMessages(sessionId) {
// 监听路由参数变化,加载对应会话 // 监听路由参数变化,加载对应会话
watch( watch(
routeSessionId, routeSessionId,
(newSessionId) => { async (newSessionId) => {
if (newSessionId) { if (newSessionId) {
currentSessionId.value = newSessionId; currentSessionId.value = newSessionId;
loadHistoryMessages(newSessionId); // 等待历史消息加载完毕,避免在 send 时 messages 被清空
await loadHistoryMessages(newSessionId);
// 处理从首页带过来的初始消息
const text = route.query.text;
if (text) {
inputText.value = text;
// 清除 query 中的 text防止刷新页面时重复发送
const query = { ...route.query };
delete query.text;
router.replace({ query });
// 触发发送逻辑
send();
}
// 确保 SSE 连接已建立 // 确保 SSE 连接已建立
if (!appStore.sseConnected) { if (!appStore.sseConnected) {
appStore.initSSE(); appStore.initSSE();

View File

@@ -93,25 +93,16 @@ async function handleSend() {
const session = await historyStore.createSession(text); const session = await historyStore.createSession(text);
console.log('创建会话成功:', session); console.log('创建会话成功:', session);
// 异步发送消息
try {
await window.opencode.promptAsync(session.id, text);
console.log('异步发送消息指令已发送');
} catch (sendErr) {
console.error('发送初始消息失败:', sendErr);
// 如果服务未启动,尝试启动
if (appStore.serviceStatus !== appStore.SERVICE_STATUS.RUNNING) {
appStore.triggerStartService();
}
}
// 清空输入框 // 清空输入框
inputText.value = ''; inputText.value = '';
// 跳转到对话页面 // 跳转到对话页面,并将消息文本带入 query
router.push({ router.push({
path: '/chat', path: '/chat',
query: { sessionId: session.id }, query: {
sessionId: session.id,
text: text,
},
}); });
} catch (err) { } catch (err) {
console.error('创建会话失败:', err); console.error('创建会话失败:', err);