From 705ac89d1c77fff67daac82556200b980e11199f Mon Sep 17 00:00:00 2001 From: hp0912 <809211365@qq.com> Date: Sat, 4 Apr 2026 23:18:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=89=E8=A3=85=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E4=BE=9D=E8=B5=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- skills/text-to-image/SKILL.md | 31 ++++++++++++--- skills/text-to-image/scripts/bootstrap.py | 38 +++++++++++++++++++ skills/text-to-image/scripts/requirements.txt | 1 + skills/text-to-image/scripts/text_to_image.py | 19 ---------- 4 files changed, 65 insertions(+), 24 deletions(-) create mode 100644 skills/text-to-image/scripts/bootstrap.py create mode 100644 skills/text-to-image/scripts/requirements.txt diff --git a/skills/text-to-image/SKILL.md b/skills/text-to-image/SKILL.md index 535070c..430938e 100644 --- a/skills/text-to-image/SKILL.md +++ b/skills/text-to-image/SKILL.md @@ -36,7 +36,19 @@ argument-hint: "需要 prompt 参数(画图提示词),可选 model(模 "model": { "type": "string", "description": "画图模型选择(可选):即梦4.5(jimeng-4.5) / 即梦4.6(jimeng-4.6) / 即梦5.0(jimeng-5.0) / 豆包4.5(doubao-seedream-4.5) / 豆包4.0(doubao-seedream-4.0) / 豆包文生图(doubao-seedream-3.0-t2i) / 豆包图生图(doubao-seededit-3.0-i2i) / 造相基础版(Z-Image) / 造相蒸馏版(Z-Image-Turbo) / 造相图片编辑(Qwen-Image-Edit-2511),默认: 空(none)。", - "enum": ["none", "jimeng-4.5", "jimeng-4.6", "jimeng-5.0", "doubao-seedream-4.5", "doubao-seedream-4.0", "doubao-seedream-3.0-t2i", "doubao-seededit-3.0-i2i", "Z-Image", "Z-Image-Turbo", "Qwen-Image-Edit-2511"], + "enum": [ + "none", + "jimeng-4.5", + "jimeng-4.6", + "jimeng-5.0", + "doubao-seedream-4.5", + "doubao-seedream-4.0", + "doubao-seedream-3.0-t2i", + "doubao-seededit-3.0-i2i", + "Z-Image", + "Z-Image-Turbo", + "Qwen-Image-Edit-2511" + ], "default": "none" }, "negative_prompt": { @@ -68,12 +80,21 @@ argument-hint: "需要 prompt 参数(画图提示词),可选 model(模 - `MYSQL_USER`:MySQL 数据库用户名。 - `MYSQL_PASSWORD`:MySQL 数据库密码。 +## 依赖安装 + +- 在执行 `text-to-image/scripts/text_to_image.py` 之前,必须先安装依赖。 +- 建议优先执行安装脚本:`python3 skills/text-to-image/scripts/bootstrap.py` +- 该脚本会自动执行:`python3 -m pip install -r skills/text-to-image/scripts/requirements.txt` +- 如果不使用安装脚本,也可以直接执行:`python3 -m pip install -r skills/text-to-image/scripts/requirements.txt` +- 如果环境里已经安装过这些依赖,重复安装通常不会报错,只会做已满足检查。 + ## 执行步骤 1. 当用户输入绘图相关内容时触发该技能。 2. 从用户输入中提取 prompt(画图提示词),不对提示词做总结或修改。可选提取 model、negative_prompt、ratio、resolution 参数。 -3. 将参数组装为 JSON 字符串,在仓库根目录下执行本地脚本:`python3 text-to-image/scripts/text_to_image.py ''`。 -4. 脚本内部执行逻辑: +3. 在执行脚本前,先安装依赖:`python3 skills/text-to-image/scripts/bootstrap.py`,或直接执行 `python3 -m pip install -r skills/text-to-image/scripts/requirements.txt`。 +4. 将参数组装为 JSON 字符串,在仓库根目录下执行本地脚本:`python3 text-to-image/scripts/text_to_image.py ''`。 +5. 脚本内部执行逻辑: - 连接 MySQL 数据库(数据库名 = `ROBOT_CODE`)。 - 查询 `global_settings` 表获取全局绘图配置(`image_ai_enabled`、`image_ai_settings`)。 - 如果 `ROBOT_FROM_WX_ID` 以 `@chatroom` 结尾,查询 `chat_room_settings` 表(`WHERE chat_room_id = ?`)覆盖全局配置;否则查询 `friend_settings` 表(`WHERE wechat_id = ?`)覆盖全局配置。 @@ -81,9 +102,9 @@ argument-hint: "需要 prompt 参数(画图提示词),可选 model(模 - 解析 `image_ai_settings` JSON,根据选择的模型提取对应配置(JiMeng / DouBao / Z-Image)。 - 调用对应的绘图 API 生成图片。 - 输出图片 URL。 -5. 如果脚本执行失败,回复兜底文案:`AI 绘图暂时不可用,请稍后再试。` +6. 如果脚本执行失败,回复兜底文案:`AI 绘图暂时不可用,请稍后再试。` ## 回复要求 - 成功时,脚本输出 `图片URL` 格式,直接发送图片,不要额外追加解释文字。 -- 失败时,使用固定兜底文案回复。 \ No newline at end of file +- 失败时,使用固定兜底文案回复。 diff --git a/skills/text-to-image/scripts/bootstrap.py b/skills/text-to-image/scripts/bootstrap.py new file mode 100644 index 0000000..8c3a129 --- /dev/null +++ b/skills/text-to-image/scripts/bootstrap.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +from __future__ import annotations + +import subprocess +import sys +from pathlib import Path + + +def main() -> int: + script_dir = Path(__file__).resolve().parent + requirements_file = script_dir / "requirements.txt" + + if not requirements_file.is_file(): + sys.stderr.write(f"未找到依赖文件: {requirements_file}\n") + return 1 + + command = [ + sys.executable, + "-m", + "pip", + "install", + "-r", + str(requirements_file), + ] + + try: + subprocess.run(command, check=True) + except subprocess.CalledProcessError as exc: + sys.stderr.write(f"安装依赖失败,退出码: {exc.returncode}\n") + return exc.returncode or 1 + + sys.stdout.write("依赖安装完成\n") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) \ No newline at end of file diff --git a/skills/text-to-image/scripts/requirements.txt b/skills/text-to-image/scripts/requirements.txt new file mode 100644 index 0000000..2c34aed --- /dev/null +++ b/skills/text-to-image/scripts/requirements.txt @@ -0,0 +1 @@ +pymysql>=1.1,<2 \ No newline at end of file diff --git a/skills/text-to-image/scripts/text_to_image.py b/skills/text-to-image/scripts/text_to_image.py index 785e67a..8d64539 100644 --- a/skills/text-to-image/scripts/text_to_image.py +++ b/skills/text-to-image/scripts/text_to_image.py @@ -5,29 +5,10 @@ from __future__ import annotations import json import os import re -import subprocess import sys import time import urllib.request - -# --------------------------------------------------------------------------- -# Dependency bootstrap -# --------------------------------------------------------------------------- - -def _ensure_package(pip_name: str, import_name: str | None = None) -> None: - """Install *pip_name* if it cannot be imported.""" - try: - __import__(import_name or pip_name) - except ImportError: - subprocess.check_call( - [sys.executable, "-m", "pip", "install", pip_name, "-q"], - stdout=subprocess.DEVNULL, - ) - - -_ensure_package("pymysql") - import pymysql # type: ignore # noqa: E402