MCPcopy Create free account
hub / github.com/Seanium/ChatMap / generateStreamingResponseClient

Function generateStreamingResponseClient

lib/client-api.ts:112–187  ·  view source on GitHub ↗
(conversationHistory: Message[], abortSignal?: AbortSignal)

Source from the content-addressed store, hash-verified

110
111// 流式响应生成函数
112export async function generateStreamingResponseClient(conversationHistory: Message[], abortSignal?: AbortSignal) {
113 if (!conversationHistory || !Array.isArray(conversationHistory) || conversationHistory.length === 0) {
114 throw new Error("对话历史无效");
115 }
116
117 try {
118 // 从 localStorage 获取模型设置
119 const settingsStr = localStorage.getItem("model_settings")
120
121 if (!settingsStr) {
122 const legacyApiKey = localStorage.getItem("openai_api_key")
123 if (!legacyApiKey) {
124 throw new Error("缺少模型设置。请先配置您的 AI 模型。");
125 }
126
127 // 使用旧版 API 密钥(兼容性支持)
128 const response = await fetch("/api/stream", {
129 method: "POST",
130 headers: {
131 "Content-Type": "application/json",
132 "x-api-key": legacyApiKey,
133 },
134 body: JSON.stringify({ conversationHistory }),
135 signal: abortSignal,
136 })
137
138 if (!response.ok || !response.body) {
139 throw new Error(`流式响应失败: ${response.status} ${response.statusText}`);
140 }
141
142 return streamAsyncIterable(response.body, abortSignal);
143 }
144
145 // 解析模型设置
146 const settings: ModelSettings = JSON.parse(settingsStr)
147 const provider = settings.provider
148 const providerSettings = settings.providerSettings?.[provider] || {
149 baseURL: "",
150 model: "",
151 apiKey: ""
152 }
153
154 // 发送请求到流式 API 路由
155 const response = await fetch("/api/stream", {
156 method: "POST",
157 headers: {
158 "Content-Type": "application/json",
159 },
160 body: JSON.stringify({
161 conversationHistory,
162 settings: {
163 provider: provider,
164 baseURL: providerSettings.baseURL,
165 model: providerSettings.model,
166 apiKey: providerSettings.apiKey,
167 temperature: settings.temperature
168 }
169 }),

Callers 1

handleGenerateResponseFunction · 0.90

Calls 1

streamAsyncIterableFunction · 0.85

Tested by

no test coverage detected