feat: 支持传图

This commit is contained in:
ZyGuo committed 2026-06-03 10:51:42 +08:00
1 parent 34a9f15cb4
commit 5d39aa55bd
3 files changed
+331 -86

No files matched your search

@@ -0,0 +1,71 @@
import assert from "node:assert/strict";
import test from "node:test";
import { parseBrainyChatSubmitMessage } from "./iframe-chat-submit";
test("parses iframe text, page context, and screenshot into a prompt message", () => {
const result = parseBrainyChatSubmitMessage({
source: "brainy-extension",
type: "brainy:chat-submit",
requestId: "iframe-123",
payload: {
text: "提取并翻译",
prompt: "提取图片中的文字并翻译成简体中文",
pageContext: {
title: "UN News",
content: "Banning children from social media is not the answer.",
url: "https://news.un.org/example",
},
screenshot: {
dataUrl: "data:image/png;base64,aGVsbG8=",
fileName: "capture.png",
images: [
{
filename: "capture.png",
url: "C:\\Users\\Rocky\\AppData\\Local\\Temp\\capture.png",
size: 0,
},
],
},
},
});
assert.equal(result.submitText, [
"提取并翻译",
"<extension_prompt>\n提取图片中的文字并翻译成简体中文\n</extension_prompt>",
"<page_context>\n<title>\nUN News\n</title>\n\n<url>\nhttps://news.un.org/example\n</url>\n\n<content>\nBanning children from social media is not the answer.\n</content>\n</page_context>",
].join("\n\n"));
assert.deepEqual(result.iframeContext, { brainy_request_id: "iframe-123" });
assert.equal(result.files.length, 1);
assert.equal(result.files[0]?.type, "file");
assert.equal(result.files[0]?.filename, "capture.png");
assert.equal(result.files[0]?.mediaType, "image/png");
assert.ok(result.files[0]?.file instanceof File);
assert.equal(result.files[0]?.file?.name, "capture.png");
assert.equal(result.files[0]?.file?.type, "image/png");
assert.equal(result.files[0]?.url, "data:image/png;base64,aGVsbG8=");
});
test("keeps screenshot-only iframe submissions in the conversation flow", () => {
const result = parseBrainyChatSubmitMessage({
source: "brainy-extension",
type: "brainy:chat-submit",
payload: {
screenshot: {
dataUrl: "data:image/jpeg;base64,aGVsbG8=",
images: [
{
filename: "shot.jpg",
url: "C:\\Users\\Rocky\\AppData\\Local\\Temp\\shot.jpg",
size: 0,
},
],
},
},
});
assert.equal(result.submitText, "");
assert.equal(result.files.length, 1);
assert.equal(result.files[0]?.filename, "shot.jpg");
assert.equal(result.files[0]?.mediaType, "image/jpeg");
});