feat: 对接生图功能

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-25 19:14:58 +08:00
parent 5cb72f8579
commit 223845b0ed
12 changed files with 268 additions and 30 deletions
+49 -1
View File
@@ -1,6 +1,22 @@
// server/utils/openai.ts
import OpenAI from "openai";
import type { BaseOptions } from "#shared/types/openai";
import type { BaseOptions, IImageGenerateData } from "#shared/types/openai";
const IMAGE_GENERATION_URL = "https://api.qflink.xyz/v1/images/generations";
const IMAGE_GENERATION_MODEL = "gpt-image-2";
const IMAGE_GENERATION_SIZE = "1024x1024";
interface IImageGenerationResponse {
/** 上游创建时间 */
created?: number;
/** 上游图片生成结果列表 */
data?: Array<{
/** 上游返回的修订提示词,可能为空 */
revised_prompt?: string;
/** 生成图片地址 */
url?: string;
}>;
}
/** 通用 AI 调用函数(支持文本 / 图文 / 多模态) */
export const askAI = async ({
@@ -84,6 +100,38 @@ export const askVision = async (
return res.output_text;
};
/** 调用图片生成接口,完整 API Key 只在服务端使用 */
export const askImg = async ({
apiKey,
prompt
}: {
apiKey: string;
prompt: string;
}): Promise<IImageGenerateData> => {
const result = await $fetch<IImageGenerationResponse>(IMAGE_GENERATION_URL, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
"Content-Type": "application/json"
},
body: {
model: IMAGE_GENERATION_MODEL,
prompt,
size: IMAGE_GENERATION_SIZE
}
});
const image = result.data?.[0];
if (!image?.url) {
throw new Error("图片生成失败");
}
return {
imageUrl: image.url,
revisedPrompt: image.revised_prompt
};
};
/** 调用流式接口 */
export const askStream = async (
options: BaseOptions & {