新功能: - 创建AI聊天助手组件(AIChatWidget) - 支持文字+图片多模态对话 - 集成Gemini AI API - 支持图片编辑和内容生成 - 实时对话历史管理 - 优雅的UI设计,支持黑夜模式 技术实现: - 后端API代理Gemini API(/api/ai-chat) - 支持最多5张图片上传 - 流式响应处理 - 对话历史管理 - Base64图片处理 配置: - 在.env中添加Gemini API配置 - GEMINI_API_KEY - GEMINI_API_BASE_URL - GEMINI_MODEL 替换原弹幕功能,提供更强大的AI助手体验!
86 lines
2.3 KiB
JavaScript
Executable File
86 lines
2.3 KiB
JavaScript
Executable File
import { Service, imagex } from "../lib";
|
|
import {
|
|
previewImageUploadFileReponseSchemaValidate,
|
|
uploadImagesReponseSchemaValidate,
|
|
} from "./schema/imagex";
|
|
import path from "path";
|
|
import { readFile } from "fs/promises";
|
|
import axios from "axios";
|
|
|
|
function atob(content) {
|
|
const buff = Buffer.from(content, "base64");
|
|
return buff.toString();
|
|
}
|
|
|
|
const defaultConfig = {
|
|
serviceName: "imagex",
|
|
defaultVersion: "2018-08-01",
|
|
};
|
|
const imagexService = new Service(defaultConfig);
|
|
|
|
test("imagex:PreviewImageUploadFile", async () => {
|
|
const response = await imagexService.fetchOpenAPI({
|
|
Action: "PreviewImageUploadFile",
|
|
Version: "2018-08-01",
|
|
query: {
|
|
ServiceId: "7zcoejbqou",
|
|
StoreUri: "tos-cn-i-7zcoejbqou/732c6acc5637408a96bb67d59bf0de85",
|
|
},
|
|
});
|
|
const validateResult = previewImageUploadFileReponseSchemaValidate(response);
|
|
expect(validateResult).toBe(true);
|
|
});
|
|
|
|
test("imagex:UploadImages", async () => {
|
|
const imagexService = imagex.defaultService;
|
|
const response = await imagexService.UploadImages({
|
|
serviceId: "7zcoejbqou",
|
|
files: [path.resolve(__dirname, "imagex_test_pic.png")],
|
|
fileKeys: ["sdk_test_1"],
|
|
});
|
|
const validateResult = uploadImagesReponseSchemaValidate(response);
|
|
expect(validateResult).toBe(true);
|
|
});
|
|
|
|
test("imagex:GetUploadAuthToken", async () => {
|
|
const protocal = "https:";
|
|
const host = "open.volcengineapi.com";
|
|
|
|
const imagexService = imagex.defaultService;
|
|
|
|
imagexService.setAccessKeyId("testAk");
|
|
imagexService.setSecretKey("testSk");
|
|
|
|
const base64String = imagexService.GetUploadAuthToken({
|
|
ServiceId: "your imagex service id",
|
|
// 默认900 (15min)
|
|
"X-Expires": "60",
|
|
});
|
|
|
|
const imageBlob = await readFile(path.resolve(__dirname, "./imagex_test_pic.png"));
|
|
|
|
const tokens = JSON.parse(atob(base64String));
|
|
|
|
const { ApplyUploadToken, CommitUploadToken } = tokens;
|
|
|
|
const {
|
|
data: {
|
|
Result: {
|
|
UploadAddress: {
|
|
UploadHosts: [UploadHost],
|
|
StoreInfos,
|
|
SessionKey,
|
|
},
|
|
},
|
|
},
|
|
} = await axios.get(`${protocal}//${host}?${ApplyUploadToken}`);
|
|
|
|
await imagexService.DoUpload([imageBlob], UploadHost, StoreInfos);
|
|
|
|
const {
|
|
data: { Result: CommitUploadRes },
|
|
} = await axios.post(`${protocal}//${host}?${CommitUploadToken}`, { SessionKey });
|
|
|
|
return CommitUploadRes;
|
|
});
|