MCPcopy Create free account
hub / github.com/scriptscat/scriptcat / finalize

Method finalize

src/app/service/agent/service_worker/llm_client.ts:192–235  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

190
191 // 保存模型生成的图片到 OPFS,然后转发事件
192 const finalize = async () => {
193 const savedBlocks: ContentBlock[] = [];
194 for (const pending of pendingImageSaves) {
195 try {
196 await this.chatRepo.saveAttachment(pending.block.attachmentId, pending.data);
197 savedBlocks.push(pending.block);
198 // 转发不含 data 的 content_block_complete 事件给 UI
199 sendEvent({ type: "content_block_complete", block: pending.block });
200 } catch {
201 // 保存失败忽略
202 }
203 }
204
205 // 提取文本中的 markdown 内联 base64 图片(某些 API 以 ![alt](data:image/...;base64,...) 形式返回图片)
206 const imgRegex = /!\[([^\]]*)\]\((data:image\/([^;]+);base64,[A-Za-z0-9+/=\s]+)\)/g;
207 let match;
208 let cleanedContent = content;
209 while ((match = imgRegex.exec(content)) !== null) {
210 const [fullMatch, alt, dataUrl, subtype] = match;
211 const mimeType = `image/${subtype}`;
212 const ext = subtype || "png";
213 const blockId = generateAttachmentId(ext);
214 try {
215 await this.chatRepo.saveAttachment(blockId, dataUrl);
216 const block: ContentBlock = {
217 type: "image",
218 attachmentId: blockId,
219 mimeType,
220 name: alt || "generated-image",
221 };
222 savedBlocks.push(block);
223 sendEvent({ type: "content_block_complete", block });
224 cleanedContent = cleanedContent.replace(fullMatch, "");
225 } catch {
226 // 保存失败保留原始 markdown
227 }
228 }
229 // 清理提取图片后的多余空行
230 if (cleanedContent !== content) {
231 content = cleanedContent.replace(/\n{3,}/g, "\n\n").trim();
232 }
233
234 return savedBlocks.length > 0 ? savedBlocks : undefined;
235 };
236
237 finalize()
238 .then((contentBlocks) => {

Callers

nothing calls this directly

Calls 4

generateAttachmentIdFunction · 0.90
sendEventFunction · 0.85
saveAttachmentMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected