(query: string, conversationHistory?: Message[])
| 9 | } |
| 10 | |
| 11 | export async function generateMapResponseClient(query: string, conversationHistory?: Message[]) { |
| 12 | if (!query || typeof query !== "string") { |
| 13 | return { |
| 14 | text: "查询无效。请提供有效的查询字符串。", |
| 15 | locations: [], |
| 16 | error: true, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | try { |
| 21 | // 从 localStorage 获取模型设置 |
| 22 | const settingsStr = localStorage.getItem("model_settings") |
| 23 | |
| 24 | // 如果没有设置,检查旧版 API 密钥或返回错误 |
| 25 | if (!settingsStr) { |
| 26 | const legacyApiKey = localStorage.getItem("openai_api_key") |
| 27 | if (!legacyApiKey) { |
| 28 | return { |
| 29 | text: "缺少模型设置。请先配置您的 AI 模型。", |
| 30 | locations: [], |
| 31 | error: true, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // 使用旧版 API 密钥(兼容性支持) |
| 36 | const response = await fetch("/api/generate", { |
| 37 | method: "POST", |
| 38 | headers: { |
| 39 | "Content-Type": "application/json", |
| 40 | "x-api-key": legacyApiKey, |
| 41 | }, |
| 42 | body: JSON.stringify({ |
| 43 | query, |
| 44 | conversationHistory: conversationHistory || [] |
| 45 | }), |
| 46 | }) |
| 47 | |
| 48 | const data = await response.json() |
| 49 | return { |
| 50 | text: data.text || "无可用的响应文本", |
| 51 | task_type: data.task_type || "LOCATION_LIST", |
| 52 | locations: Array.isArray(data.locations) ? data.locations : [], |
| 53 | polylines: Array.isArray(data.polylines) ? data.polylines : [], |
| 54 | error: !!data.error, |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // 解析模型设置 |
| 59 | const settings: ModelSettings = JSON.parse(settingsStr) |
| 60 | const provider = settings.provider |
| 61 | const providerSettings = settings.providerSettings?.[provider] || { |
| 62 | baseURL: "", |
| 63 | model: "", |
| 64 | apiKey: "" |
| 65 | } |
| 66 | |
| 67 | // 发送请求到 API 路由,包含完整的模型设置和对话历史 |
| 68 | const response = await fetch("/api/generate", { |
no outgoing calls
no test coverage detected