feat: Initial commit with full deployment architecture and scripts
This commit is contained in:
commit
abb3efe114
96 files changed
+16789
No files matched your search
@@ -0,0 +1,127 @@
|
||||
|
||||
/**
|
||||
* frontend/src/composables/useChat.ts
|
||||
* 文件用途:前端 SSE 聊天 Hook
|
||||
* 引用文件:vue
|
||||
* 逻辑关系:封装 Fetch API 处理流式响应,供 ChatView 组件使用
|
||||
*/
|
||||
import { ref } from 'vue';
|
||||
|
||||
export function useChat() {
|
||||
/**
|
||||
* messages: 响应式消息列表,用于 UI 渲染
|
||||
* 结构: [{ role: 'user'|'assistant', content: string }]
|
||||
*/
|
||||
const messages = ref<any[]>([]);
|
||||
|
||||
/**
|
||||
* isThinking: 标记 AI 是否正在生成回答,用于控制 Loading 状态和输入框禁用
|
||||
*/
|
||||
const isThinking = ref(false);
|
||||
|
||||
/**
|
||||
* conversationId: 当前会话 ID
|
||||
* 用于在多轮对话中关联上下文,并在反馈时找到对应 ChatLog
|
||||
*/
|
||||
const conversationId = ref<string | null>(null);
|
||||
|
||||
const getAuthHeaders = () => {
|
||||
const stored = localStorage.getItem('hxops_user');
|
||||
if (!stored) return {};
|
||||
const user = JSON.parse(stored) as { id?: number; username: string; role: string };
|
||||
const headers: Record<string, string> = {
|
||||
'X-Username': user.username,
|
||||
'X-Role': user.role,
|
||||
};
|
||||
if (user.id) {
|
||||
headers['X-User-Id'] = String(user.id);
|
||||
}
|
||||
return headers;
|
||||
};
|
||||
|
||||
/**
|
||||
* sendMessage
|
||||
* 用途:发送消息并处理 SSE 流式响应
|
||||
* 核心逻辑:
|
||||
* 1. 乐观更新 UI (立即显示用户消息)
|
||||
* 2. 创建空的 AI 消息占位
|
||||
* 3. 发起 Fetch POST 请求
|
||||
* 4. 获取 Reader 流式读取
|
||||
* 5. 逐块解码并追加到当前 AI 消息中
|
||||
* 参数:
|
||||
* query: 用户输入文本
|
||||
* agentId: 目标 Agent ID
|
||||
*/
|
||||
const sendMessage = async (query: string, agentId: number) => {
|
||||
// 1. 乐观 UI 更新:立即将用户消息推入列表
|
||||
messages.value.push({ role: 'user', content: query });
|
||||
// 创建一个空的 Assistant 消息用于流式追加,并记录其索引
|
||||
const botMsgIndex = messages.value.push({ role: 'assistant', content: '' }) - 1;
|
||||
|
||||
isThinking.value = true;
|
||||
|
||||
try {
|
||||
// 指向本地 Flask 端口 (需通过 Vite Proxy 转发到 5000)
|
||||
const response = await fetch('/api/chat/completions', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', ...getAuthHeaders() },
|
||||
body: JSON.stringify({
|
||||
query,
|
||||
agent_id: agentId,
|
||||
conversation_id: conversationId.value,
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const err = await response.json().catch(() => ({}));
|
||||
throw new Error(err.error || `Request failed with status ${response.status}`);
|
||||
}
|
||||
|
||||
// 获取 ReadableStream 读取器
|
||||
const reader = response.body?.getReader();
|
||||
const decoder = new TextDecoder();
|
||||
|
||||
if (!reader) throw new Error('No reader');
|
||||
|
||||
// 循环读取流数据
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
// 解码二进制块为文本
|
||||
const chunk = decoder.decode(value);
|
||||
// SSE 数据可能包含多行,按双换行符分割
|
||||
const lines = chunk.split('\n\n');
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('data: ')) {
|
||||
try {
|
||||
// 解析 JSON 数据
|
||||
const data = JSON.parse(line.slice(6));
|
||||
// 仅处理 message 事件,追加 answer 到当前消息
|
||||
if (data.event === 'message') {
|
||||
messages.value[botMsgIndex].content += data.answer;
|
||||
} else if (data.event === 'message_end') {
|
||||
// 记录会话 ID,便于后续发送反馈
|
||||
if (data.conversation_id) {
|
||||
conversationId.value = data.conversation_id;
|
||||
}
|
||||
} else if (data.event === 'error') {
|
||||
messages.value[botMsgIndex].content += `\n[Error: ${data.message}]`;
|
||||
}
|
||||
} catch (e) {
|
||||
// 忽略 JSON 解析错误 (可能是 keep-alive 或非完整 JSON)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
messages.value[botMsgIndex].content += `\n[Error: ${e.message || 'Network Issue'}]`;
|
||||
} finally {
|
||||
isThinking.value = false;
|
||||
}
|
||||
};
|
||||
|
||||
return { messages, isThinking, conversationId, sendMessage };
|
||||
}
|
||||
Reference in new issue
Block a user