-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathllm_client.ts
More file actions
259 lines (242 loc) · 9.63 KB
/
llm_client.ts
File metadata and controls
259 lines (242 loc) · 9.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import type { AgentChatRepo } from "@App/app/repo/agent_chat";
import type {
AgentModelConfig,
ChatRequest,
ChatStreamEvent,
ContentBlock,
ToolCall,
ToolDefinition,
} from "@App/app/service/agent/core/types";
import { providerRegistry } from "@App/app/service/agent/core/providers";
import { resolveAttachments } from "@App/app/service/agent/core/attachment_resolver";
import { generateAttachmentId } from "@App/app/service/agent/core/providers/content_utils";
export interface LLMCallResult {
content: string;
thinking?: string;
toolCalls?: ToolCall[];
usage?: {
inputTokens: number;
outputTokens: number;
cacheCreationInputTokens?: number;
cacheReadInputTokens?: number;
};
contentBlocks?: ContentBlock[];
}
export class LLMClient {
constructor(private chatRepo: AgentChatRepo) {}
/**
* 调用 LLM 并收集完整响应(内部处理流式、重试与图片保存)
*/
async callLLM(
model: AgentModelConfig,
params: { messages: ChatRequest["messages"]; tools?: ToolDefinition[]; cache?: boolean },
sendEvent: (event: ChatStreamEvent) => void,
signal: AbortSignal
): Promise<LLMCallResult> {
const chatRequest: ChatRequest = {
conversationId: "",
modelId: model.id,
messages: params.messages,
tools: params.tools,
cache: params.cache,
};
// 预解析消息中 ContentBlock 引用的 attachmentId → base64
const attachmentResolver = await resolveAttachments(params.messages, model, (id) =>
this.chatRepo.getAttachment(id)
);
const provider = providerRegistry.get(model.provider);
if (!provider) {
throw new Error(`Unsupported LLM provider: ${model.provider}`);
}
const { url, init } = await provider.buildRequest({
model,
request: chatRequest,
resolver: attachmentResolver,
});
// 带重试的 LLM 调用,最多重试 5 次,间隔递增:10s, 10s, 20s, 20s, 30s
const RETRY_DELAYS = [10_000, 10_000, 20_000, 20_000, 30_000];
const MAX_RETRIES = RETRY_DELAYS.length;
let response!: Response;
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
try {
response = await fetch(url, { ...init, signal });
if (!response.ok) {
const errorText = await response.text().catch(() => "");
let errorMessage = `API error: ${response.status}`;
try {
const errorJson = JSON.parse(errorText);
errorMessage = errorJson.error?.message || errorJson.message || errorMessage;
} catch {
if (errorText) errorMessage += ` - ${errorText.slice(0, 200)}`;
}
throw new Error(errorMessage);
}
if (!response.body) {
throw new Error("No response body");
}
// 请求成功,跳出重试循环
break;
} catch (e: any) {
// 用户取消时直接抛出,不重试
if (signal.aborted) throw e;
// 4xx 客户端错误(除 408/425/429 外)不重试,立即抛出
const m = (e.message || "").match(/API error:\s*(\d{3})/);
if (m) {
const code = Number(m[1]);
if (code >= 400 && code < 500 && code !== 408 && code !== 425 && code !== 429) {
throw e;
}
}
// 已用完所有重试次数
if (attempt >= MAX_RETRIES) throw e;
// 向 UI 发送重试通知(含延迟时间,用于倒计时显示)
const delayMs = RETRY_DELAYS[attempt];
sendEvent({
type: "retry",
attempt: attempt + 1,
maxRetries: MAX_RETRIES,
error: e.message || "Unknown error",
delayMs,
});
// 等待后重试,等待期间可被 abort 取消;resolve 时移除监听器避免泄漏
await new Promise<void>((resolve, reject) => {
const onAbort = () => {
clearTimeout(timer);
reject(new Error("Aborted during retry wait"));
};
const timer = setTimeout(() => {
signal.removeEventListener("abort", onAbort);
resolve();
}, delayMs);
signal.addEventListener("abort", onAbort, { once: true });
});
}
}
const reader = response.body!.getReader();
const parseStream = provider.parseStream.bind(provider);
// 收集响应
let content = "";
let thinking = "";
const toolCalls: ToolCall[] = [];
let usage:
| { inputTokens: number; outputTokens: number; cacheCreationInputTokens?: number; cacheReadInputTokens?: number }
| undefined;
// 收集带 data 的图片 block(模型生成的图片),stream 结束后统一保存到 OPFS
const pendingImageSaves: Array<{ block: ContentBlock & { type: "image" }; data: string }> = [];
return new Promise((resolve, reject) => {
const onEvent = (event: ChatStreamEvent) => {
// 只转发流式内容事件,done 和 error 由 callLLMWithToolLoop 统一管理
// 避免在 tool calling 循环中提前发送 done 导致客户端过早 resolve
// 带 data 的 content_block_complete 暂不转发,等 OPFS 保存后再发
if (event.type !== "done" && event.type !== "error") {
if (event.type === "content_block_complete" && event.data) {
// 暂存,稍后保存到 OPFS 后再转发
pendingImageSaves.push({ block: event.block as ContentBlock & { type: "image" }, data: event.data });
} else {
sendEvent(event);
}
}
switch (event.type) {
case "content_delta":
content += event.delta;
break;
case "thinking_delta":
thinking += event.delta;
break;
case "tool_call_start":
// 并发 tool_call 时 parser 会交错发 delta,这里立即 push 到数组,
// 由 tool_call_delta 通过 id/index 定位目标 tool,避免串扰。
toolCalls.push({ ...event.toolCall, arguments: event.toolCall.arguments || "", status: "running" });
break;
case "tool_call_delta": {
if (!toolCalls.length) break;
let target: ToolCall | undefined = undefined;
// 1a. 按 id 匹配
if (event.id) {
target = toolCalls.find((t) => t.id === event.id);
}
// 1b. 按 index 匹配(OpenAI 后续 chunk 无 id 只有 index)
if (!target && event.index !== undefined) {
target = toolCalls[event.index];
}
// 2. fallback:最新一个状态为 running 的 tool call
if (!target) {
for (let i = toolCalls.length - 1; i >= 0; i--) {
if (toolCalls[i].status === "running") {
target = toolCalls[i];
break;
}
}
}
if (target) target.arguments += event.delta;
break;
}
case "done": {
if (event.usage) {
usage = event.usage;
}
// 保存模型生成的图片到 OPFS,然后转发事件
const finalize = async () => {
const savedBlocks: ContentBlock[] = [];
for (const pending of pendingImageSaves) {
try {
await this.chatRepo.saveAttachment(pending.block.attachmentId, pending.data);
savedBlocks.push(pending.block);
// 转发不含 data 的 content_block_complete 事件给 UI
sendEvent({ type: "content_block_complete", block: pending.block });
} catch {
// 保存失败忽略
}
}
// 提取文本中的 markdown 内联 base64 图片(某些 API 以  形式返回图片)
const imgRegex = /!\[([^\]]*)\]\((data:image\/([^;]+);base64,[A-Za-z0-9+/=\s]+)\)/g;
let match;
let cleanedContent = content;
while ((match = imgRegex.exec(content)) !== null) {
const [fullMatch, alt, dataUrl, subtype] = match;
const mimeType = `image/${subtype}`;
const ext = subtype || "png";
const blockId = generateAttachmentId(ext);
try {
await this.chatRepo.saveAttachment(blockId, dataUrl);
const block: ContentBlock = {
type: "image",
attachmentId: blockId,
mimeType,
name: alt || "generated-image",
};
savedBlocks.push(block);
sendEvent({ type: "content_block_complete", block });
cleanedContent = cleanedContent.replace(fullMatch, "");
} catch {
// 保存失败保留原始 markdown
}
}
// 清理提取图片后的多余空行
if (cleanedContent !== content) {
content = cleanedContent.replace(/\n{3,}/g, "\n\n").trim();
}
return savedBlocks.length > 0 ? savedBlocks : undefined;
};
finalize()
.then((contentBlocks) => {
resolve({
content,
thinking: thinking || undefined,
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
usage,
contentBlocks,
});
})
.catch(reject);
break;
}
case "error":
reject(new Error(event.message));
break;
}
};
parseStream(reader, onEvent, signal).catch(reject);
});
}
}