Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 59 additions & 5 deletions src/tool/tool_image.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down Expand Up @@ -51,24 +54,75 @@ 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) {
logger.error(`未找到AIDrawing依赖`);
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: [] };
Expand Down