diff --git a/src/tool/tool_image.ts b/src/tool/tool_image.ts index f0f938b..fc83e6c 100644 --- a/src/tool/tool_image.ts +++ b/src/tool/tool_image.ts @@ -1,6 +1,9 @@ +import { AIManager } from "../AI/AI"; +import { Image } from "../AI/image"; import { logger } from "../logger"; import { ConfigManager } from "../config/configManager"; import { Tool } from "./tool"; +import { generateId } from "../utils/utils"; export function registerImage() { const toolITT = new Tool({ @@ -51,14 +54,22 @@ export function registerImage() { negative_prompt: { type: 'string', description: '不希望图片中出现的内容描述' + }, + save: { + type: "boolean", + description: "是否保存图片" + }, + name: { + type: "string", + description: "如果保存图片,图片的名称" } }, - required: ['prompt'] + required: ['prompt' , 'save' , 'name'] } } }); - toolTTI.solve = async (ctx, msg, _, args) => { - const { prompt, negative_prompt } = args; + toolTTI.solve = async (ctx, msg, ai, args) => { + const { prompt, negative_prompt, save, name } = args; const ext = seal.ext.find('AIDrawing'); if (!ext) { @@ -66,9 +77,52 @@ export function registerImage() { return { content: `未找到AIDrawing依赖,请提示用户安装AIDrawing依赖`, images: [] }; } + // 切换到当前会话ai + if (!ctx.isPrivate) ai = AIManager.getAI(ctx.group.groupId); + + const kws = ["tti", name]; + try { - await globalThis.aiDrawing.generateImage(prompt, ctx, msg, negative_prompt); - return { content: `图像生成请求已发送`, images: [] }; + // 新版 AIDrawing + if (globalThis.aiDrawing && typeof globalThis.aiDrawing.sendImageRequest === 'function') { + const result = await globalThis.aiDrawing.sendImageRequest(prompt, negative_prompt); + const img = new Image(); + img.id = `${name}_${generateId()}`; + if (result.startsWith("http://") || result.startsWith("https://")) { + try { + await img.urlToBase64(); + } catch (e) { + logger.error(`将图片URL转换为base64失败: ${e}`); + img.file = result; + } + } else { + img.file = result; + } + + img.format = img.format || 'unknown'; + img.content = `AI绘图<|img:${img.id}|>\n${prompt ? `描述: ${prompt}` : '' }\n${negative_prompt ? `不希望出现: ${negative_prompt}` : '' }`; + + if (save) ai.memory.addMemory(ctx, ai, [], [], kws, [img], img.content); + + return { content: `生成成功,请使用<|img:${img.id}|>发送`, images: [img] }; + } + + // 兼容旧版 AIDrawing + if (globalThis.aiDrawing && typeof globalThis.aiDrawing.generateImage === 'function') { + try { + await globalThis.aiDrawing.generateImage(prompt, ctx, msg, negative_prompt); + if (save) { + logger.warning('旧版 AIDrawing,无法直接保存图片'); + return { content: `图像生成请求已发送`, images: [] }; + } + return { content: `图像生成请求已发送`, images: [] }; + } catch (e) { + logger.error(`图像生成失败::${e}`); + return { content: `图像生成失败:${e}`, images: [] }; + } + } + logger.error('未找到可用的 AIDrawing 接口,AIDrawing插件可能存在问题'); + return { content: `未找到可用的 AIDrawing 接口, AIDrawing插件可能存在问题`, images: [] }; } catch (e) { logger.error(`图像生成失败:${e}`); return { content: `图像生成失败:${e}`, images: [] };