|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +import axios from 'axios' |
| 19 | + |
| 20 | +const BASE_URL = '/api/v1' |
| 21 | + |
| 22 | +// 定义接口类型 |
| 23 | +export interface Session { |
| 24 | + session_id: string |
| 25 | + created_at: string |
| 26 | + updated_at: string |
| 27 | + message_count: number |
| 28 | + status: string |
| 29 | +} |
| 30 | + |
| 31 | +export interface ChatMessage { |
| 32 | + id: string |
| 33 | + content: string |
| 34 | + role: 'user' | 'assistant' |
| 35 | + timestamp: number |
| 36 | + type?: 'normal' | 'error' | 'partial_error' |
| 37 | +} |
| 38 | + |
| 39 | +export interface ChatResponse { |
| 40 | + data: { |
| 41 | + session_id: string |
| 42 | + messages: ChatMessage[] |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// AI 服务接口 |
| 47 | +export const aiService = { |
| 48 | + // 创建新会话 |
| 49 | + async createSession(): Promise<string> { |
| 50 | + const response = await axios.post(`${BASE_URL}/ai/sessions`) |
| 51 | + return response.data.data.session_id |
| 52 | + }, |
| 53 | + |
| 54 | + // 获取会话列表 |
| 55 | + async getSessions(): Promise<Session[]> { |
| 56 | + const response = await axios.get(`${BASE_URL}/ai/sessions`) |
| 57 | + return response.data.data.sessions || [] |
| 58 | + }, |
| 59 | + |
| 60 | + // 获取特定会话信息 |
| 61 | + async getSessionInfo(sessionId: string): Promise<ChatResponse> { |
| 62 | + const response = await axios.get(`${BASE_URL}/ai/sessions/${sessionId}`) |
| 63 | + return response.data |
| 64 | + }, |
| 65 | + |
| 66 | + // 删除会话 |
| 67 | + async deleteSession(sessionId: string): Promise<void> { |
| 68 | + await axios.delete(`${BASE_URL}/ai/sessions/${sessionId}`) |
| 69 | + }, |
| 70 | + |
| 71 | + // 发送聊天消息(流式响应) |
| 72 | + async sendChatMessage(message: string, sessionId?: string): Promise<ReadableStream> { |
| 73 | + const headers: Record<string, string> = { |
| 74 | + 'Content-Type': 'application/json' |
| 75 | + } |
| 76 | + |
| 77 | + if (sessionId) { |
| 78 | + headers['X-Session-ID'] = sessionId |
| 79 | + } |
| 80 | + |
| 81 | + const response = await fetch(`${BASE_URL}/ai/chat/stream`, { |
| 82 | + method: 'POST', |
| 83 | + headers, |
| 84 | + body: JSON.stringify({ |
| 85 | + message, |
| 86 | + sessionID: sessionId |
| 87 | + }), |
| 88 | + mode: 'cors', // 允许跨域 |
| 89 | + credentials: 'include' // 允许携带 cookie |
| 90 | + }) |
| 91 | + |
| 92 | + if (!response.ok) { |
| 93 | + throw new Error(`HTTP error! status: ${response.status}`) |
| 94 | + } |
| 95 | + |
| 96 | + return response.body! |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +export default aiService |
0 commit comments