release: complete ImageStudio 1.0.32 advanced material workflows
这个提交存在于:
父节点
176ee240b6
当前提交
937ee730ce
共有 101 个文件被更改,包括 17775 次插入 和 278 次删除
1
.gitignore
Vendored
1
.gitignore
Vendored
|
|
@ -3,6 +3,7 @@ __pycache__/
|
|||
*.py[cod]
|
||||
.pytest_cache/
|
||||
.pytest_tmp*/
|
||||
.tmp_pytest*/
|
||||
.ruff_cache/
|
||||
.mypy_cache/
|
||||
.venv/
|
||||
|
|
|
|||
95
CHANGELOG.md
95
CHANGELOG.md
|
|
@ -4,9 +4,104 @@
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.0.32] - 2026-07-18
|
||||
|
||||
- 画风验证审核升级为固定维度协议;服务端按头像、全身、跨场景分别加权脸部、眼睛、线稿、色彩、阴影、比例与背景,不再直接采用视觉模型的自由总分,并记录维度覆盖率与缺失项。
|
||||
- 复验共识自动聚合上一轮三张验证图,将至少两个场景重复出现的问题归类为公共画风缺陷,将单场景问题保留为尺度胶囊修正,避免头像问题污染全身或场景提示词。
|
||||
- 共识协议新增全局色彩与局部色差分离、结构化差异、提示词补丁、语义风险审计和尺度胶囊;可能被字面化为物体、光效或构图的比喻必须改写为表面、几何与位置关系。
|
||||
- detailed 画风胶囊改由服务端编译:按核心、结构、渲染优先级合并 `must_preserve`,近义去重并限制为 14 条公共核心特征,再叠加最多 5 条场景尺度特征,减少长提示词的注意力稀释。
|
||||
- 每轮三图完成后自动与上一轮计算总分和分场景差值,标记采用候选、保留上一版或需要人工判断;视觉审核失败仍不影响生成图片保留。
|
||||
|
||||
## [1.0.31] - 2026-07-18
|
||||
|
||||
- 画风逐图识别与交叉共识新增脸型、眼睛/虹膜、鼻口简化、头身比、发束组织、线条拓扑、阴影色相、高光形状和色彩关系等结构画风维度,不再只输出通用动漫渲染标签。
|
||||
- 归一化层保留视觉模型返回的 `normalized_asset_profile.attributes`,并为画风自动生成 `must_preserve`;修复结构化属性退化成单一 `legacy_description` 的问题。
|
||||
- 内容污染清单仅用于审计,不再原样注入生图负向提示词;改为只排除具体源角色身份、具体服装设计、姿势与场景,避免误伤 Moe 比例、动物耳等风格耦合特征。
|
||||
- 修复 detailed 胶囊含逗号语义短语被拆碎、短共识与详细胶囊重复拼接、正负提示词多次重复的问题,并对缺少结构维度的模型自报置信度进行服务端降权。
|
||||
- 每张验证图生成后自动使用代表源图进行视觉还原度审核,持久化维度评分、主要匹配/偏差及下一轮修正提示;审核失败不会丢弃已经生成的图片。
|
||||
- 进阶画风验证卡片显示自动还原度和主要偏差;视觉模型格式错误新增脱敏后的具体响应问题和解析原因。
|
||||
|
||||
## [1.0.30] - 2026-07-18
|
||||
|
||||
- 进阶画风复验会同时提交代表源图、上一轮三张失败验证图、全部源图识别报告、上一版提示词与用户修正,视觉模型可直接进行前后对照。
|
||||
- 将“太强、降低、太弱、加强、移除、保持”等反馈解析为 increase/decrease/remove/preserve 结构化方向,并持久化对照清单与实际调整计划。
|
||||
- 增加服务端语义防反转;例如“颜色对比太过强烈”不会再被转换为 intense/extreme/high contrast,而会落实为适中克制的对比并排除过强色差。
|
||||
- 三张验证生图统一使用 detailed 画风胶囊及其负向约束,不再只使用短共识提示词;仍保持纯文本迁移验证,不向生图模型传递源图。
|
||||
|
||||
- 已完成全仓角色工作流二次审计;确认在现有 `character_gradual` 主线上增量修复,不创建平行工作流。
|
||||
- 修复队列已按 1.0.9–1.0.20 小版本写入 `docs/workflow-secondary-audit-development-queue.md`。
|
||||
|
||||
## [1.0.29] - 2026-07-17
|
||||
|
||||
- 进阶素材的画风样本、三张当前验证图、单图源图/验证图以及历史轮次缩略图全部接入统一大图预览卡片。
|
||||
- 点击或使用键盘 Enter/空格可打开原图优先的大图预览,支持滚轮缩放、按钮缩放、双击缩放、拖动画布、重置和 Esc 关闭。
|
||||
- 大图遮罩继续使用安全单击关闭逻辑,文本拖选、图片拖动或指针移出卡片不会误触关闭。
|
||||
- 缩略图增加放大光标、键盘焦点和明确的“点击查看大图”提示。
|
||||
|
||||
## [1.0.28] - 2026-07-17
|
||||
|
||||
- 修复进阶画风导入后端使用 `advanced_style_set`、前端只识别 `style_set`,导致三图验证被误判为单图流程且主界面只显示一张验证图的问题。
|
||||
- 多图画风当前轮次恢复为头像、全身、跨场景三张验证卡片;三张图分别显示状态、提示词、文字调整和随机种子。
|
||||
- 验证轮次列表与详情接口为每一轮附带完整 `validation_cases`,历史轮次也可同时查看三张缩略图,不再只展示聚合轮次的第一张投影图。
|
||||
- 兼容旧 `style_set`、正式 `advanced_style_set` 以及服务端 `metadata.style_set` 标记,避免升级前后的记录再次走错前端分支。
|
||||
|
||||
## [1.0.27] - 2026-07-17
|
||||
|
||||
- 进阶画风复验拆分为“画风修正”和“下一轮验证内容”两条独立输入:前者只重建画风共识与胶囊,后者直接写入三个验证生图提示词。
|
||||
- 修复第二轮强制复用第一轮老年男性、机器人和宇航员场景的问题;用户指定角色、服装、构图或场景后会重建验证案例,且最终 `expanded_prompt` 必定包含用户原文。
|
||||
- 只修改画风时继续沿用相同验证场景与种子,便于 A/B 比较;修改验证内容时使用新种子,避免旧构图被随机种子持续锁定。
|
||||
- 默认验证改为同一新角色的近域头像、全身和跨场景三级对照,减少角色年龄、物种与写实先验对画风判断的干扰。
|
||||
- 验证卡片显示已应用的文字调整和实际种子;历史轮次持久化画风修正、验证内容及其作用范围。
|
||||
- 旧客户端继续支持单一 `adjustment_text`,服务端会同时应用到画风与验证内容链路,避免反馈静默丢失。
|
||||
|
||||
## [1.0.26] - 2026-07-17
|
||||
|
||||
- 修复任务较多时队列卡片被滚动区域压缩的问题;任务卡改为纵向自然高度排列,状态、标题、进度条与耗时不再相互覆盖。
|
||||
- 进阶素材验证轮询改为只更新当前活动导入或导入摘要,不再每 4.5 秒全量刷新导入库与素材库。
|
||||
- 增加轮询互斥,防止慢请求发生重叠;轮询失败保持当前页面,不再触发错误态或视觉重置。
|
||||
- 图片展示优先使用稳定的 `storage://` URI 缓存,忽略同一媒体每轮变化的临时签名 URL,避免验证生成期间图片反复重载和页面闪烁。
|
||||
|
||||
## [1.0.25] - 2026-07-17
|
||||
|
||||
- 修复进阶素材队列记录被手动删除后,导入或样本仍保持 queued/running 并永久卡住的问题;详情读取、列表刷新和重试前会将缺失任务对账为可重试失败。
|
||||
- 新增未封存进阶导入删除接口与前端双击确认按钮;删除会先取消残留任务,再级联清理样本、交叉共识、验证轮次和进阶素材独立文件。
|
||||
- 已封存为高优先级素材的导入禁止删除,仍需通过素材归档流程处理,避免破坏素材库、衣橱和工作流引用。
|
||||
- 缺失队列任务会显示明确提示,可选择单独重试、排除样本或删除整个导入,不再只显示模糊的“待处理样本”。
|
||||
|
||||
## [1.0.24] - 2026-07-17
|
||||
|
||||
- 模型分工统一为双源路由:每个文本/视觉节点分别选择 ImageStudio 插件适配器与 AstrBot 模型提供商,执行顺序固定为插件优先、AstrBot 备用。
|
||||
- 素材识别、进阶素材识别、多图画风共识、结构化文本调用和生成后视觉审核加入故障转移;前置提供商失败时自动尝试备用,并保留失败摘要。
|
||||
- 生图节点明确只选择 ImageStudio 生图适配器;AstrBot 4.25 没有公开生图桥接接口,因此界面禁用不可执行的宿主生图选择,避免保存后运行失败。
|
||||
- 进阶画风失败样本重试时重新读取当前双源配置,并同步刷新父导入路由,修复旧 `material_vlm` 被任务永久冻结的问题。
|
||||
- 提示词转译、生成前内容审核与生成后图片审核同步使用双选择栏;旧单路由配置保持兼容,双路由继续存为单个 AstrBot 配置字符串。
|
||||
- 修正运行时版本常量与发布元数据不同步的问题,状态页和插件元数据统一显示 1.0.24。
|
||||
|
||||
## [1.0.23] - 2026-07-17
|
||||
|
||||
- 将内置素材分类的英文名称统一显示为中文,并覆盖进阶素材导入、素材操作和识别提示词设置下拉框。
|
||||
- 保留后端分类 ID 与原始名称,兼容现有素材、衣橱和工作流数据。
|
||||
|
||||
## [1.0.22] - 2026-07-17
|
||||
|
||||
- 进阶素材的“风格”分类新增 2~8 张样本组导入(推荐 3~6 张);每张图拥有独立任务、计时、错误与重试,并由 8 Worker 同时处理,不再串行共享超时。
|
||||
- 新增逐图识别屏障和多图交叉归纳:模型同时读取全部有效原图与逐图报告,区分核心共同画风、可变项、源内容污染、疑似离群样本和 1~3 张代表样本,并生成 compact / standard / detailed 三档画风胶囊。
|
||||
- 新增三路纯提示词迁移验证,使用完全不同的角色、场景和构图且不传源图;三项任务独立并发,单项失败可单独重试,调整后创建新共识版本并复用相同验证场景。
|
||||
- 新增 `advanced_style_sources`、`advanced_style_consensus_runs`、`advanced_style_validation_cases` 三表;父导入与 2~8 张源记录原子写入,验证图片继续隔离在 `storage://advanced_materials/style-validations`。
|
||||
- 前端仅在“风格”分类启用多图上传,支持粘贴、拖入、追加、移除、排序/主参考、失败重试、样本排除与恢复,并展示交叉归纳、代表/离群样本和三场景状态。
|
||||
- 封存后仍只产生一个高优先级 `advmat_`;快速角色使用统一文字胶囊,详细工作流冻结主参考与最多两张可信代表图,普通素材伪造 metadata 无法注入额外引用。
|
||||
- 备份脱敏、恢复、孤儿媒体、逐场景成本、素材统计、Worker 重启恢复、路由契约和深色主题同步覆盖新链路;付费验证任务重启后不会自动重复提交。
|
||||
|
||||
## [1.0.21] - 2026-07-16
|
||||
|
||||
- 新增独立“进阶素材”入库线:源图经 Vision 识别后,只用提取出的文字提示词生成验证图;用户可并排判断还原度,不满意时追加描述进入下一轮,满意后才封存。
|
||||
- 新增 `advanced_material_imports`、`advanced_material_validation_attempts`、`advanced_materials` 三表与 `storage://advanced_materials` 独立文件根;验证图不进入普通图库、单图池或工作流候选池。
|
||||
- 进阶素材以服务端可信标记进入统一素材目录,快速角色和详细工作流优先使用;普通素材伪造 metadata 不会获得进阶优先级。
|
||||
- 验证生图接入恐暴/极端暴力前置禁止、成人内容白名单与 NSFW 路由、普通模型明确 NSFW 拒绝后的一次性回退,以及可保存的独立预算守卫。
|
||||
- 修复运行中取消后的迟到结果复活、快速重启任务永久卡住、失败/取消但已计费轮次漏算、脱敏备份写入 NOT NULL 失败和空 MIME 图片误报 PNG。
|
||||
- 新增进阶导入、三库切换、任务轮询、原图/验证图并排审核、调整重试、显式封存、深色主题和高优先级标记;模型分工新增进阶识别与验证生图两张卡。
|
||||
- 专项覆盖迁移幂等、仓储事务、普通/进阶隔离、无图生图输入、取消 CAS、安全路由、预算、重启恢复、备份、成本与前端上传契约。
|
||||
|
||||
## [1.0.20] - 2026-07-15
|
||||
|
||||
- 发布总验收补齐两处跨版本接线:工作台唯一挂载生成式交叉审查对话框并真实调用 decide/regenerate/approve;档案卡入库移除客户端三段式写入,统一调用后端 `finalize-card` 幂等事务。
|
||||
|
|
|
|||
|
|
@ -80,6 +80,17 @@
|
|||
"default": true
|
||||
},
|
||||
"model_material_recognition": {"description":"素材导入解析使用的 Vision 路由。","type":"string","default":"material_vlm"},
|
||||
"model_advanced_material_recognition": {"description":"进阶素材导入与高保真特征提取使用的 Vision 路由。","type":"string","default":"material_vlm"},
|
||||
"model_advanced_material_validation_generation": {"description":"进阶素材还原度验证图使用的生图路由。留空时回退到默认生图路由。","type":"string","default":""},
|
||||
"advanced_material_budget_enabled": {"description":"启用进阶素材验证图预算守卫。","type":"bool","default":false},
|
||||
"advanced_material_budget_days": {"description":"进阶素材预算统计窗口天数。","type":"int","default":30},
|
||||
"advanced_material_budget_max_task_count": {"description":"窗口内最大验证任务数;留空表示不限。","type":"string","default":""},
|
||||
"advanced_material_budget_max_image_count": {"description":"窗口内最大验证图片数;留空表示不限。","type":"string","default":""},
|
||||
"advanced_material_budget_max_estimated_cost": {"description":"窗口内最大估算费用;留空表示不限。","type":"string","default":""},
|
||||
"advanced_material_budget_price_key": {"description":"成本计价键,可填写 Provider ID 或模型 ID。","type":"string","default":""},
|
||||
"advanced_material_budget_per_request": {"description":"每次验证请求的估算单价。","type":"string","default":"0"},
|
||||
"advanced_material_budget_per_image": {"description":"每张验证图片的估算单价。","type":"string","default":"0"},
|
||||
"advanced_material_budget_per_1k_tokens": {"description":"每千 Token 的估算单价。","type":"string","default":"0"},
|
||||
"model_profile_parse": {"description":"外部角色导入解析使用的文本模型路由。","type":"string","default":""},
|
||||
"model_persona_split": {"description":"角色设定拆解使用的文本模型路由。","type":"string","default":""},
|
||||
"model_prompt_planning": {"description":"提示词规划与扩写使用的文本模型路由。","type":"string","default":""},
|
||||
|
|
@ -119,6 +130,11 @@
|
|||
"type": "string",
|
||||
"default": "data/db/imagestudio.db"
|
||||
},
|
||||
"advanced_materials_path": {
|
||||
"description": "进阶素材原图、验证图与封存文件的独立逻辑目录。",
|
||||
"type": "string",
|
||||
"default": "data/advanced_materials"
|
||||
},
|
||||
"postgresql_enabled": {
|
||||
"description": "启用 PostgreSQL。填写 DSN 或主机/库名/用户后也会自动按 PG 配置运行。",
|
||||
"type": "bool",
|
||||
|
|
|
|||
316
backend/dto/advanced_material_dto.py
普通文件
316
backend/dto/advanced_material_dto.py
普通文件
|
|
@ -0,0 +1,316 @@
|
|||
"""DTOs for the isolated advanced-material verification library."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AdvancedMaterialImport:
|
||||
import_id: str
|
||||
category_id: str | None
|
||||
display_name: str
|
||||
source_image_uri: str
|
||||
source_thumbnail_uri: str | None
|
||||
status: str
|
||||
recognition_prompt: str | None
|
||||
recognition_negative_prompt: str | None
|
||||
recognition_tags: list[str]
|
||||
recognition_result: dict[str, Any]
|
||||
recognition_model_route: str | None
|
||||
prompt_revision: int
|
||||
current_attempt_id: str | None
|
||||
accepted_material_id: str | None
|
||||
idempotency_key: str | None
|
||||
created_by: str | None
|
||||
error: dict[str, Any]
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"import_id": self.import_id,
|
||||
"category_id": self.category_id,
|
||||
"display_name": self.display_name,
|
||||
"source_image_uri": self.source_image_uri,
|
||||
"source_thumbnail_uri": self.source_thumbnail_uri,
|
||||
"status": self.status,
|
||||
"recognition_prompt": self.recognition_prompt,
|
||||
"recognition_negative_prompt": self.recognition_negative_prompt,
|
||||
"recognition_tags": list(self.recognition_tags),
|
||||
"recognition_result": dict(self.recognition_result),
|
||||
"recognition_model_route": self.recognition_model_route,
|
||||
"prompt_revision": self.prompt_revision,
|
||||
"current_attempt_id": self.current_attempt_id,
|
||||
"accepted_material_id": self.accepted_material_id,
|
||||
"idempotency_key": self.idempotency_key,
|
||||
"created_by": self.created_by,
|
||||
"error": dict(self.error),
|
||||
"metadata": dict(self.metadata),
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AdvancedMaterialValidationAttempt:
|
||||
attempt_id: str
|
||||
import_id: str
|
||||
attempt_number: int
|
||||
status: str
|
||||
adjustment_text: str | None
|
||||
prompt: str
|
||||
negative_prompt: str | None
|
||||
model_route: str | None
|
||||
provider_id: str | None
|
||||
provider_model: str | None
|
||||
task_id: str | None
|
||||
validation_image_uri: str | None
|
||||
validation_thumbnail_uri: str | None
|
||||
seed: int | None
|
||||
width: int | None
|
||||
height: int | None
|
||||
usage: dict[str, Any]
|
||||
fidelity_score: float | None
|
||||
decision: str | None
|
||||
decision_notes: str | None
|
||||
error: dict[str, Any]
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
completed_at: str | None
|
||||
decided_at: str | None
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"attempt_id": self.attempt_id,
|
||||
"import_id": self.import_id,
|
||||
"attempt_number": self.attempt_number,
|
||||
"status": self.status,
|
||||
"adjustment_text": self.adjustment_text,
|
||||
"prompt": self.prompt,
|
||||
"negative_prompt": self.negative_prompt,
|
||||
"model_route": self.model_route,
|
||||
"provider_id": self.provider_id,
|
||||
"provider_model": self.provider_model,
|
||||
"task_id": self.task_id,
|
||||
"validation_image_uri": self.validation_image_uri,
|
||||
"validation_thumbnail_uri": self.validation_thumbnail_uri,
|
||||
"seed": self.seed,
|
||||
"width": self.width,
|
||||
"height": self.height,
|
||||
"usage": dict(self.usage),
|
||||
"fidelity_score": self.fidelity_score,
|
||||
"decision": self.decision,
|
||||
"decision_notes": self.decision_notes,
|
||||
"error": dict(self.error),
|
||||
"metadata": dict(self.metadata),
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
"completed_at": self.completed_at,
|
||||
"decided_at": self.decided_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AdvancedStyleSource:
|
||||
source_id: str
|
||||
import_id: str
|
||||
sort_order: int
|
||||
is_primary: bool
|
||||
source_image_uri: str
|
||||
source_thumbnail_uri: str | None
|
||||
status: str
|
||||
recognition_model_route: str | None
|
||||
provider_id: str | None
|
||||
provider_model: str | None
|
||||
task_id: str | None
|
||||
recognition_prompt: str | None
|
||||
recognition_negative_prompt: str | None
|
||||
recognition_tags: list[str]
|
||||
recognition_result: dict[str, Any]
|
||||
error: dict[str, Any]
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
completed_at: str | None
|
||||
excluded_at: str | None
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"source_id": self.source_id,
|
||||
"import_id": self.import_id,
|
||||
"sort_order": self.sort_order,
|
||||
"is_primary": self.is_primary,
|
||||
"source_image_uri": self.source_image_uri,
|
||||
"source_thumbnail_uri": self.source_thumbnail_uri,
|
||||
"status": self.status,
|
||||
"recognition_model_route": self.recognition_model_route,
|
||||
"provider_id": self.provider_id,
|
||||
"provider_model": self.provider_model,
|
||||
"task_id": self.task_id,
|
||||
"recognition_prompt": self.recognition_prompt,
|
||||
"recognition_negative_prompt": self.recognition_negative_prompt,
|
||||
"recognition_tags": list(self.recognition_tags),
|
||||
"recognition_result": dict(self.recognition_result),
|
||||
"error": dict(self.error),
|
||||
"metadata": dict(self.metadata),
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
"completed_at": self.completed_at,
|
||||
"excluded_at": self.excluded_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AdvancedStyleConsensusRun:
|
||||
consensus_id: str
|
||||
import_id: str
|
||||
revision: int
|
||||
status: str
|
||||
task_id: str | None
|
||||
model_route: str | None
|
||||
provider_id: str | None
|
||||
provider_model: str | None
|
||||
source_ids: list[str]
|
||||
prompt: str | None
|
||||
negative_prompt: str | None
|
||||
tags: list[str]
|
||||
result: dict[str, Any]
|
||||
error: dict[str, Any]
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
completed_at: str | None
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"consensus_id": self.consensus_id,
|
||||
"import_id": self.import_id,
|
||||
"revision": self.revision,
|
||||
"status": self.status,
|
||||
"task_id": self.task_id,
|
||||
"model_route": self.model_route,
|
||||
"provider_id": self.provider_id,
|
||||
"provider_model": self.provider_model,
|
||||
"source_ids": list(self.source_ids),
|
||||
"prompt": self.prompt,
|
||||
"negative_prompt": self.negative_prompt,
|
||||
"tags": list(self.tags),
|
||||
"result": dict(self.result),
|
||||
"error": dict(self.error),
|
||||
"metadata": dict(self.metadata),
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
"completed_at": self.completed_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AdvancedStyleValidationCase:
|
||||
case_id: str
|
||||
attempt_id: str
|
||||
import_id: str
|
||||
case_key: str
|
||||
sort_order: int
|
||||
status: str
|
||||
task_id: str | None
|
||||
content_prompt: str
|
||||
final_prompt: str
|
||||
negative_prompt: str | None
|
||||
model_route: str | None
|
||||
provider_id: str | None
|
||||
provider_model: str | None
|
||||
seed: int | None
|
||||
width: int | None
|
||||
height: int | None
|
||||
validation_image_uri: str | None
|
||||
validation_thumbnail_uri: str | None
|
||||
usage: dict[str, Any]
|
||||
style_review: dict[str, Any]
|
||||
fidelity_score: float | None
|
||||
error: dict[str, Any]
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
completed_at: str | None
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"case_id": self.case_id,
|
||||
"attempt_id": self.attempt_id,
|
||||
"import_id": self.import_id,
|
||||
"case_key": self.case_key,
|
||||
"sort_order": self.sort_order,
|
||||
"status": self.status,
|
||||
"task_id": self.task_id,
|
||||
"content_prompt": self.content_prompt,
|
||||
"final_prompt": self.final_prompt,
|
||||
"negative_prompt": self.negative_prompt,
|
||||
"model_route": self.model_route,
|
||||
"provider_id": self.provider_id,
|
||||
"provider_model": self.provider_model,
|
||||
"seed": self.seed,
|
||||
"width": self.width,
|
||||
"height": self.height,
|
||||
"validation_image_uri": self.validation_image_uri,
|
||||
"validation_thumbnail_uri": self.validation_thumbnail_uri,
|
||||
"usage": dict(self.usage),
|
||||
"style_review": dict(self.style_review),
|
||||
"fidelity_score": self.fidelity_score,
|
||||
"error": dict(self.error),
|
||||
"metadata": dict(self.metadata),
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
"completed_at": self.completed_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class AdvancedMaterial:
|
||||
material_id: str
|
||||
import_id: str
|
||||
accepted_attempt_id: str
|
||||
category_id: str | None
|
||||
display_name: str
|
||||
source_image_uri: str
|
||||
source_thumbnail_uri: str | None
|
||||
validation_image_uri: str
|
||||
validation_thumbnail_uri: str | None
|
||||
prompt: str
|
||||
negative_prompt: str | None
|
||||
tags: list[str]
|
||||
priority: int
|
||||
status: str
|
||||
capsule: dict[str, Any]
|
||||
metadata: dict[str, Any]
|
||||
accepted_at: str
|
||||
archived_at: str | None
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"material_id": self.material_id,
|
||||
"import_id": self.import_id,
|
||||
"accepted_attempt_id": self.accepted_attempt_id,
|
||||
"category_id": self.category_id,
|
||||
"display_name": self.display_name,
|
||||
"source_image_uri": self.source_image_uri,
|
||||
"source_thumbnail_uri": self.source_thumbnail_uri,
|
||||
"validation_image_uri": self.validation_image_uri,
|
||||
"validation_thumbnail_uri": self.validation_thumbnail_uri,
|
||||
"prompt": self.prompt,
|
||||
"negative_prompt": self.negative_prompt,
|
||||
"tags": list(self.tags),
|
||||
"priority": self.priority,
|
||||
"status": self.status,
|
||||
"capsule": dict(self.capsule),
|
||||
"metadata": dict(self.metadata),
|
||||
"accepted_at": self.accepted_at,
|
||||
"archived_at": self.archived_at,
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
}
|
||||
|
|
@ -20,6 +20,7 @@ class WardrobeItem:
|
|||
last_used_at: str | None
|
||||
created_at: str
|
||||
updated_at: str
|
||||
library_kind: str = "standard"
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
|
|
@ -35,4 +36,5 @@ class WardrobeItem:
|
|||
"last_used_at": self.last_used_at,
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
"library_kind": self.library_kind,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -168,6 +168,110 @@ ROUTES: tuple[RouteSpec, ...] = (
|
|||
RouteSpec("GET", "media/orphans", "backend.routes.media_routes.scan_orphan_media", "read"),
|
||||
RouteSpec("GET", "media/payload", "backend.routes.media_routes.fetch_media_payload", "read"),
|
||||
RouteSpec("GET", "media/fetch", "backend.routes.media_routes.fetch_media", "public_token"),
|
||||
RouteSpec(
|
||||
"GET",
|
||||
"advanced-material-imports",
|
||||
"backend.routes.advanced_material_routes.list_advanced_material_imports",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/upload",
|
||||
"backend.routes.advanced_material_routes.upload_advanced_material",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/style-set/upload",
|
||||
"backend.routes.advanced_material_routes.upload_advanced_style_set",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"GET",
|
||||
"advanced-material-imports/{import_id}",
|
||||
"backend.routes.advanced_material_routes.get_advanced_material_import",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"DELETE",
|
||||
"advanced-material-imports/{import_id}",
|
||||
"backend.routes.advanced_material_routes.delete_advanced_material_import",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/{import_id}/retry",
|
||||
"backend.routes.advanced_material_routes.retry_advanced_material_import",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/{import_id}/style-sources/{source_id}/retry",
|
||||
"backend.routes.advanced_material_routes.retry_advanced_style_source",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/{import_id}/style-sources/{source_id}/exclude",
|
||||
"backend.routes.advanced_material_routes.exclude_advanced_style_source",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/{import_id}/style-consensus",
|
||||
"backend.routes.advanced_material_routes.create_advanced_style_consensus",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/{import_id}/style-validation-cases/{case_id}/retry",
|
||||
"backend.routes.advanced_material_routes.retry_advanced_style_validation_case",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/{import_id}/cancel",
|
||||
"backend.routes.advanced_material_routes.cancel_advanced_material_import",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"GET",
|
||||
"advanced-material-imports/{import_id}/validation-attempts",
|
||||
"backend.routes.advanced_material_routes.list_advanced_material_attempts",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/{import_id}/validation-attempts",
|
||||
"backend.routes.advanced_material_routes.create_advanced_material_attempt",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/{import_id}/attempts/{attempt_id}/reject",
|
||||
"backend.routes.advanced_material_routes.reject_advanced_material_attempt",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"advanced-material-imports/{import_id}/attempts/{attempt_id}/accept",
|
||||
"backend.routes.advanced_material_routes.accept_advanced_material_attempt",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec("GET", "advanced-materials", "backend.routes.advanced_material_routes.list_advanced_materials", "read"),
|
||||
RouteSpec(
|
||||
"GET",
|
||||
"advanced-materials/{material_id}",
|
||||
"backend.routes.advanced_material_routes.get_advanced_material",
|
||||
"read",
|
||||
),
|
||||
RouteSpec(
|
||||
"DELETE",
|
||||
"advanced-materials/{material_id}",
|
||||
"backend.routes.advanced_material_routes.archive_advanced_material",
|
||||
"materials",
|
||||
),
|
||||
RouteSpec("GET", "material-catalog", "backend.routes.advanced_material_routes.list_material_catalog", "read"),
|
||||
RouteSpec("GET", "material-categories", "backend.routes.material_routes.list_material_categories", "read"),
|
||||
RouteSpec("POST", "material-categories", "backend.routes.material_routes.create_material_category", "materials"),
|
||||
RouteSpec(
|
||||
|
|
|
|||
|
|
@ -0,0 +1,435 @@
|
|||
"""HTTP handlers for the isolated advanced-material workflow."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.routes.common import handle_request
|
||||
from backend.routes.params import int_or_default, optional_str
|
||||
from backend.services.advanced_material_service import (
|
||||
AdvancedMaterialService,
|
||||
create_advanced_material_service,
|
||||
)
|
||||
from backend.services.media_service import MediaService
|
||||
from core.identity import RequestContext
|
||||
from core.security import require_authenticated, require_permission
|
||||
|
||||
|
||||
def list_advanced_material_imports(
|
||||
ctx: RequestContext,
|
||||
query: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
params = query or {}
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
payload = resolved.list_imports(
|
||||
status=optional_str(params.get("status")),
|
||||
page=int_or_default(params.get("page"), 1),
|
||||
page_size=int_or_default(params.get("page_size"), 30),
|
||||
)
|
||||
return _serialize_import_list(payload)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def upload_advanced_material(
|
||||
ctx: RequestContext,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.upload(payload or {}, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def upload_advanced_style_set(
|
||||
ctx: RequestContext,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
"""Create one style-only multi-image import and enqueue every source."""
|
||||
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.upload_style_set(payload or {}, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def get_advanced_material_import(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.get_import(import_id))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def delete_advanced_material_import(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return resolved.delete_import(import_id, ctx)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def retry_advanced_material_import(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
del payload
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.retry_import(import_id, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def retry_advanced_style_source(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
source_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
del payload
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.retry_style_source(import_id, source_id, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def exclude_advanced_style_source(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
source_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
excluded = bool((payload or {}).get("excluded", True))
|
||||
return _serialize_payload(resolved.exclude_style_source(import_id, source_id, excluded, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def create_advanced_style_consensus(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.create_style_consensus(import_id, payload or {}, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def retry_advanced_style_validation_case(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
case_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
del payload
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.retry_style_validation_case(import_id, case_id, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def cancel_advanced_material_import(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
del payload
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.cancel_import(import_id, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def list_advanced_material_attempts(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
query: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
del query
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> list[dict[str, Any]]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return [_serialize_attempt(item) for item in resolved.list_attempts(import_id)]
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def create_advanced_material_attempt(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.create_validation_attempt(import_id, payload or {}, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def reject_advanced_material_attempt(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
attempt_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.reject_attempt(import_id, attempt_id, payload or {}, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def accept_advanced_material_attempt(
|
||||
ctx: RequestContext,
|
||||
import_id: str,
|
||||
attempt_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_payload(resolved.accept_attempt(import_id, attempt_id, payload or {}, ctx))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def list_advanced_materials(
|
||||
ctx: RequestContext,
|
||||
query: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
params = query or {}
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
payload = resolved.list_materials(
|
||||
page=int_or_default(params.get("page"), 1),
|
||||
page_size=int_or_default(params.get("page_size"), 30),
|
||||
)
|
||||
return _serialize_material_list(payload)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def get_advanced_material(
|
||||
ctx: RequestContext,
|
||||
material_id: str,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
return _serialize_material(resolved.get_material(material_id).to_dict())
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def archive_advanced_material(
|
||||
ctx: RequestContext,
|
||||
material_id: str,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return _serialize_material(resolved.archive_material(material_id, ctx).to_dict())
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def list_material_catalog(
|
||||
ctx: RequestContext,
|
||||
query: dict[str, Any] | None = None,
|
||||
service: AdvancedMaterialService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or create_advanced_material_service()
|
||||
params = query or {}
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
payload = resolved.list_catalog(
|
||||
page=int_or_default(params.get("page"), 1),
|
||||
page_size=int_or_default(params.get("page_size"), 100),
|
||||
)
|
||||
payload["items"] = [
|
||||
_serialize_material(item) if item.get("library_kind") == "advanced" else _serialize_standard_material(item)
|
||||
for item in payload.get("items", [])
|
||||
]
|
||||
return payload
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def _serialize_import_list(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
**payload,
|
||||
"items": [_serialize_import(item) for item in payload.get("items", [])],
|
||||
}
|
||||
|
||||
|
||||
def _serialize_material_list(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
**payload,
|
||||
"items": [_serialize_material(item) for item in payload.get("items", [])],
|
||||
}
|
||||
|
||||
|
||||
def _serialize_payload(payload: Any) -> Any:
|
||||
if isinstance(payload, list):
|
||||
return [_serialize_payload(item) for item in payload]
|
||||
if not isinstance(payload, dict):
|
||||
return payload
|
||||
result = {key: _serialize_payload(value) for key, value in payload.items()}
|
||||
if "source_id" in result and "source_image_uri" in result:
|
||||
return _serialize_style_source(result)
|
||||
if "case_id" in result and "validation_image_uri" in result:
|
||||
return _serialize_style_validation_case(result)
|
||||
if "import_id" in result and "source_image_uri" in result:
|
||||
return _serialize_import(result)
|
||||
if "attempt_id" in result and "validation_image_uri" in result:
|
||||
return _serialize_attempt(result)
|
||||
if str(result.get("material_id") or "").startswith("advmat_"):
|
||||
return _serialize_material(result)
|
||||
return result
|
||||
|
||||
|
||||
def _serialize_import(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
**payload,
|
||||
"source_image_url": _signed_url(payload.get("source_image_uri")),
|
||||
"source_thumbnail_url": _signed_url(payload.get("source_thumbnail_uri")),
|
||||
}
|
||||
|
||||
|
||||
def _serialize_attempt(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
**payload,
|
||||
**(
|
||||
{
|
||||
"validation_cases": [
|
||||
_serialize_style_validation_case(item)
|
||||
for item in payload.get("validation_cases", [])
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
}
|
||||
if isinstance(payload.get("validation_cases"), list)
|
||||
else {}
|
||||
),
|
||||
"validation_image_url": _signed_url(payload.get("validation_image_uri")),
|
||||
"validation_thumbnail_url": _signed_url(payload.get("validation_thumbnail_uri")),
|
||||
}
|
||||
|
||||
|
||||
def _serialize_style_source(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
**payload,
|
||||
"source_image_url": _signed_url(payload.get("source_image_uri")),
|
||||
"source_thumbnail_url": _signed_url(payload.get("source_thumbnail_uri")),
|
||||
}
|
||||
|
||||
|
||||
def _serialize_style_validation_case(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
**payload,
|
||||
"validation_image_url": _signed_url(payload.get("validation_image_uri")),
|
||||
"validation_thumbnail_url": _signed_url(payload.get("validation_thumbnail_uri")),
|
||||
}
|
||||
|
||||
|
||||
def _serialize_material(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
**payload,
|
||||
"library_kind": "advanced",
|
||||
"source_image_url": _signed_url(payload.get("source_image_uri")),
|
||||
"source_thumbnail_url": _signed_url(payload.get("source_thumbnail_uri")),
|
||||
"validation_image_url": _signed_url(payload.get("validation_image_uri")),
|
||||
"validation_thumbnail_url": _signed_url(payload.get("validation_thumbnail_uri")),
|
||||
"image_uri": payload.get("source_image_uri"),
|
||||
"thumbnail_uri": payload.get("source_thumbnail_uri"),
|
||||
"image_url": _signed_url(payload.get("source_image_uri")),
|
||||
"thumbnail_url": _signed_url(payload.get("source_thumbnail_uri")),
|
||||
"recognition_status": "confirmed",
|
||||
}
|
||||
|
||||
|
||||
def _serialize_standard_material(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
return {
|
||||
**payload,
|
||||
"library_kind": "standard",
|
||||
"image_url": _signed_url(payload.get("image_uri")),
|
||||
"thumbnail_url": _signed_url(payload.get("thumbnail_uri")),
|
||||
}
|
||||
|
||||
|
||||
def _signed_url(uri: Any) -> str | None:
|
||||
text = str(uri or "").strip()
|
||||
if not text:
|
||||
return None
|
||||
try:
|
||||
return MediaService().sign_media(text)["url"]
|
||||
except Exception:
|
||||
return None
|
||||
|
|
@ -7,16 +7,18 @@ from typing import Any
|
|||
from backend.routes.common import handle_request
|
||||
from backend.services.integration_service import IntegrationService
|
||||
from backend.services.integration_token_service import IntegrationTokenService
|
||||
from backend.services.material_catalog_resolver import MaterialCatalogResolver
|
||||
from backend.services.wardrobe_service import WardrobeService
|
||||
from core.audit_service import AuditService
|
||||
from core.config_service import ConfigService
|
||||
from core.identity import RequestContext
|
||||
from core.security import require_authenticated, require_config_manager
|
||||
from database.db_helper import Database, load_database_settings
|
||||
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||||
from database.repositories.integration_repository import IntegrationRepository
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
from database.repositories.wardrobe_repository import WardrobeRepository
|
||||
from database.repositories.wardrobe_repository import AdvancedWardrobeRepository, WardrobeRepository
|
||||
|
||||
|
||||
def private_companion_daily_outfit(
|
||||
|
|
@ -82,11 +84,15 @@ def _create_integration_service() -> IntegrationService:
|
|||
database = Database(load_database_settings())
|
||||
audit_service = AuditService(database)
|
||||
config_service = ConfigService()
|
||||
materials = MaterialRepository(database)
|
||||
advanced_materials = AdvancedMaterialRepository(database)
|
||||
wardrobe_service = WardrobeService(
|
||||
wardrobe_repository=WardrobeRepository(database),
|
||||
profile_repository=ProfileRepository(database),
|
||||
material_repository=MaterialRepository(database),
|
||||
material_repository=materials,
|
||||
audit_service=audit_service,
|
||||
advanced_wardrobe_repository=AdvancedWardrobeRepository(database),
|
||||
material_catalog=MaterialCatalogResolver(materials, advanced_materials),
|
||||
)
|
||||
return IntegrationService(
|
||||
wardrobe_service=wardrobe_service,
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ from backend.services.generation_job_retry_service import GenerationJobRetryServ
|
|||
from backend.services.queue_service import QueueService
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from core.security import require_authenticated, require_task_retry, require_write
|
||||
from core.security import require_authenticated, require_permission, require_task_retry, require_write
|
||||
from database.db_helper import Database, load_database_settings
|
||||
from database.repositories.job_repository import JobRepository
|
||||
|
||||
|
|
@ -26,13 +26,17 @@ def list_jobs(
|
|||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
return queue_service.list_jobs(
|
||||
result = queue_service.list_jobs(
|
||||
status=optional_str(params.get("status")),
|
||||
workflow_id=optional_str(params.get("workflow_id")),
|
||||
profile_id=optional_str(params.get("profile_id")),
|
||||
page=int_or_default(params.get("page"), 1),
|
||||
page_size=int_or_default(params.get("page_size"), 30),
|
||||
).to_dict()
|
||||
if not ctx.has_permission("can_manage_materials"):
|
||||
visible = [item for item in result["items"] if item.get("metadata", {}).get("source") != "advanced_material"]
|
||||
result = {**result, "items": visible, "total": max(int(result.get("total") or 0) - (len(result["items"]) - len(visible)), 0)}
|
||||
return result
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
@ -46,7 +50,10 @@ def get_job(
|
|||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
return queue_service.get_job(task_id).to_dict()
|
||||
task = queue_service.get_job(task_id)
|
||||
if task.metadata.get("source") == "advanced_material":
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
return task.to_dict()
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
@ -74,6 +81,13 @@ def cancel_job(
|
|||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
task = queue_service.get_job(task_id)
|
||||
if task.metadata.get("source") == "advanced_material":
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
from backend.services.advanced_material_service import create_advanced_material_service
|
||||
|
||||
import_id = str(task.metadata.get("advanced_material_import_id") or "").strip()
|
||||
return create_advanced_material_service().cancel_import(import_id, ctx)
|
||||
return queue_service.cancel_job(task_id, ctx).to_dict()
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
|
@ -85,11 +99,20 @@ def retry_job(
|
|||
payload: dict[str, Any] | None = None,
|
||||
service: GenerationJobRetryService | None = None,
|
||||
) -> dict:
|
||||
retry_service = service or _create_generation_job_retry_service()
|
||||
retry_service = service
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_task_retry(ctx)
|
||||
return retry_service.retry_job(task_id, ctx, overrides=payload or {}).to_dict()
|
||||
queue_service = _create_queue_service()
|
||||
task = queue_service.get_job(task_id)
|
||||
if task.metadata.get("source") == "advanced_material":
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
from backend.services.advanced_material_service import create_advanced_material_service
|
||||
|
||||
import_id = str(task.metadata.get("advanced_material_import_id") or "").strip()
|
||||
return create_advanced_material_service().retry_import(import_id, ctx)
|
||||
resolved_retry_service = retry_service or _create_generation_job_retry_service()
|
||||
return resolved_retry_service.retry_job(task_id, ctx, overrides=payload or {}).to_dict()
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
|
|||
|
|
@ -3,14 +3,34 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from backend.routes.common import handle_request
|
||||
from backend.services.model_provider_service import ModelProviderService
|
||||
from core.astrbot_provider_bridge import list_astrbot_text_models
|
||||
from core.identity import RequestContext
|
||||
from core.security import require_authenticated
|
||||
from database.db_helper import Database, load_database_settings
|
||||
from database.repositories.model_provider_repository import ModelProviderRepository
|
||||
|
||||
|
||||
def list_runtime_models(ctx: RequestContext) -> dict:
|
||||
def list_runtime_models(ctx: RequestContext, service: ModelProviderService | None = None) -> dict:
|
||||
model_service = service or ModelProviderService(ModelProviderRepository(Database(load_database_settings())))
|
||||
|
||||
def action() -> dict:
|
||||
require_authenticated(ctx)
|
||||
return {"items": list_astrbot_text_models()}
|
||||
plugin_items: list[dict[str, str]] = []
|
||||
providers = {item["provider_id"]: item for item in model_service.list_providers(enabled=True)}
|
||||
for route in model_service.list_routes(enabled=True):
|
||||
provider_id = str(route.get("provider_id") or "")
|
||||
provider = providers.get(provider_id) or {}
|
||||
plugin_items.append(
|
||||
{
|
||||
"route": str(route.get("route_name") or ""),
|
||||
"provider_id": provider_id,
|
||||
"name": str(provider.get("display_name") or provider_id),
|
||||
"model": str(route.get("model_id") or ""),
|
||||
"modality": str(route.get("modality") or ""),
|
||||
"source": "plugin",
|
||||
}
|
||||
)
|
||||
return {"items": [*plugin_items, *list_astrbot_text_models()]}
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from typing import Any
|
|||
|
||||
from backend.routes.common import handle_request
|
||||
from backend.services.resource_access_service import ResourceAccessService
|
||||
from backend.services.material_catalog_resolver import MaterialCatalogResolver
|
||||
from backend.services.workflow_node_material_service import WorkflowNodeMaterialService
|
||||
from backend.services.workflow_service import WorkflowService
|
||||
from core.audit_service import AuditService
|
||||
|
|
@ -13,6 +14,7 @@ from core.identity import RequestContext
|
|||
from core.security import require_authenticated, require_write
|
||||
from database.db_helper import Database, load_database_settings
|
||||
from database.repositories.image_repository import ImageRepository
|
||||
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||||
from database.repositories.job_repository import JobRepository
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
|
|
@ -71,7 +73,10 @@ def _create_service() -> WorkflowNodeMaterialService:
|
|||
)
|
||||
return WorkflowNodeMaterialService(
|
||||
workflow_service,
|
||||
MaterialRepository(database),
|
||||
MaterialCatalogResolver(
|
||||
MaterialRepository(database),
|
||||
AdvancedMaterialRepository(database),
|
||||
),
|
||||
round_repository=WorkflowRoundRepository(database),
|
||||
access_service=ResourceAccessService(
|
||||
profiles,
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ from backend.services.cost_service import CostService
|
|||
from backend.services.generation_service import GenerationService
|
||||
from backend.services.image_service import ImageService
|
||||
from backend.services.model_provider_runtime import ModelProviderRuntime
|
||||
from backend.services.material_catalog_resolver import MaterialCatalogResolver
|
||||
from core.astrbot_provider_bridge import astrbot_structured_text_callable
|
||||
from backend.services.model_provider_bootstrap import sync_generation_provider_from_config
|
||||
from backend.services.model_provider_service import ModelProviderService
|
||||
|
|
@ -39,6 +40,7 @@ from engines.character_flow import canonical_gradual_node_key
|
|||
from core.security import require_authenticated, require_task_retry, require_write
|
||||
from database.db_helper import Database, load_config_payload, load_database_settings
|
||||
from database.repositories.image_repository import ImageRepository
|
||||
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||||
from database.repositories.job_repository import JobRepository
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from database.repositories.model_provider_repository import ModelProviderRepository
|
||||
|
|
@ -315,6 +317,10 @@ def _create_generation_service() -> GenerationService:
|
|||
audit_service = AuditService(database)
|
||||
workflow_repository = WorkflowRepository(database)
|
||||
material_repository = MaterialRepository(database)
|
||||
material_catalog = MaterialCatalogResolver(
|
||||
material_repository,
|
||||
AdvancedMaterialRepository(database),
|
||||
)
|
||||
event_bus = get_default_event_bus()
|
||||
model_provider_service = ModelProviderService(ModelProviderRepository(database))
|
||||
model_provider_runtime = ModelProviderRuntime(model_provider_service)
|
||||
|
|
@ -370,10 +376,10 @@ def _create_generation_service() -> GenerationService:
|
|||
event_bus=event_bus,
|
||||
default_model_route=str(generation_config.get("default_model") or "").strip() or None,
|
||||
require_resolved_model_route=True,
|
||||
material_resolver=material_repository,
|
||||
material_resolver=material_catalog,
|
||||
material_snapshot_service=WorkflowMaterialSnapshotService(
|
||||
workflow_repository,
|
||||
material_repository,
|
||||
material_catalog,
|
||||
),
|
||||
model_assignments=model_assignments,
|
||||
prompt_translation_service=PromptTranslationService(translation_config, translation_callable),
|
||||
|
|
|
|||
|
|
@ -7,13 +7,15 @@ from typing import Any
|
|||
from backend.routes.common import handle_request
|
||||
from backend.routes.params import optional_str
|
||||
from backend.services.wardrobe_service import WardrobeService
|
||||
from backend.services.material_catalog_resolver import MaterialCatalogResolver
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from core.security import require_authenticated, require_permission
|
||||
from database.db_helper import Database, load_database_settings
|
||||
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
from database.repositories.wardrobe_repository import WardrobeRepository
|
||||
from database.repositories.wardrobe_repository import AdvancedWardrobeRepository, WardrobeRepository
|
||||
|
||||
|
||||
def list_wardrobe_items(
|
||||
|
|
@ -106,9 +108,13 @@ def apply_wardrobe_template(ctx: RequestContext, profile_id: str, template_id: s
|
|||
|
||||
def _create_wardrobe_service() -> WardrobeService:
|
||||
database = Database(load_database_settings())
|
||||
materials = MaterialRepository(database)
|
||||
advanced_materials = AdvancedMaterialRepository(database)
|
||||
return WardrobeService(
|
||||
wardrobe_repository=WardrobeRepository(database),
|
||||
profile_repository=ProfileRepository(database),
|
||||
material_repository=MaterialRepository(database),
|
||||
material_repository=materials,
|
||||
audit_service=AuditService(database),
|
||||
advanced_wardrobe_repository=AdvancedWardrobeRepository(database),
|
||||
material_catalog=MaterialCatalogResolver(materials, advanced_materials),
|
||||
)
|
||||
|
|
|
|||
文件差异内容过多而无法显示
加载差异
|
|
@ -0,0 +1,155 @@
|
|||
"""Persistent worker pool for advanced-material recognition and validation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.job_dto import GenerationTask
|
||||
from backend.services.advanced_material_service import (
|
||||
AdvancedMaterialService,
|
||||
create_advanced_material_service,
|
||||
)
|
||||
from backend.services.worker_service import QueueWorkerPool, RetryPolicy, WorkerTaskUpdate
|
||||
from core.identity import RequestContext
|
||||
|
||||
|
||||
class AdvancedMaterialWorkerRuntime:
|
||||
def __init__(
|
||||
self,
|
||||
service: AdvancedMaterialService,
|
||||
*,
|
||||
worker_count: int = 8,
|
||||
ctx: RequestContext | None = None,
|
||||
):
|
||||
self.service = service
|
||||
self.ctx = ctx or RequestContext.system("advanced-material-worker")
|
||||
self.pool = QueueWorkerPool(
|
||||
service.queue_service,
|
||||
self._execute,
|
||||
worker_count=worker_count,
|
||||
worker_id_prefix="imagestudio-advanced-material",
|
||||
interval_seconds=0.35,
|
||||
claim_batch_size=25,
|
||||
retry_policy=RetryPolicy(max_attempts=1),
|
||||
task_filter=_is_advanced_material_task,
|
||||
terminal_callback=self._on_terminal,
|
||||
# A style set intentionally fans out one task per source image and
|
||||
# one task per validation scene. They must not share a route-wide
|
||||
# single-flight lock: every task has its own persisted timeout,
|
||||
# retry and terminal state.
|
||||
per_route_limit=worker_count,
|
||||
)
|
||||
|
||||
def start(self) -> dict[str, Any]:
|
||||
recovery = self._recover_interrupted_tasks()
|
||||
result = self.pool.start(self.ctx)
|
||||
return {**result, "recovery": recovery}
|
||||
|
||||
def stop(self, *, timeout_seconds: float = 5.0) -> dict[str, Any]:
|
||||
return self.pool.stop(timeout_seconds=timeout_seconds)
|
||||
|
||||
def _execute(self, task: GenerationTask) -> WorkerTaskUpdate:
|
||||
return self.service.execute_task(task, self.ctx)
|
||||
|
||||
def _on_terminal(
|
||||
self,
|
||||
claimed_task: GenerationTask,
|
||||
current_task: GenerationTask,
|
||||
exc: Exception | None,
|
||||
) -> None:
|
||||
if current_task.status in {"failed", "dead_letter", "cancelled"}:
|
||||
self.service.handle_task_failure(
|
||||
claimed_task,
|
||||
exc or RuntimeError(current_task.error_message or "advanced material task failed"),
|
||||
self.ctx,
|
||||
)
|
||||
|
||||
def _recover_interrupted_tasks(self) -> dict[str, Any]:
|
||||
"""Safely recover recognition, but never replay a paid image request.
|
||||
|
||||
Recognition is read-only and idempotent. A validation request may have
|
||||
reached the remote provider before the plugin process stopped, so it is
|
||||
marked failed and left for an explicit human retry.
|
||||
"""
|
||||
|
||||
# The pool has not started yet, so every advanced-material task still
|
||||
# in running/retrying belongs to the previous process. Do not apply a
|
||||
# stale-age grace period: a fast container restart must recover tasks
|
||||
# written only milliseconds earlier. Do not cap the snapshot either,
|
||||
# otherwise unrelated queue rows can hide advanced-material work.
|
||||
candidates = self.service.queue_service.repository.list_stale(
|
||||
statuses=("running", "retrying"),
|
||||
updated_before=None,
|
||||
limit=None,
|
||||
)
|
||||
requeued: list[str] = []
|
||||
failed: list[str] = []
|
||||
skipped: list[str] = []
|
||||
for task in candidates:
|
||||
if not _is_advanced_material_task(task):
|
||||
skipped.append(task.task_id)
|
||||
continue
|
||||
interrupted = self.service.queue_service.update_task_if_status(
|
||||
task.task_id,
|
||||
{task.status},
|
||||
{
|
||||
"status": "failed",
|
||||
"error_message": "task was interrupted by a previous plugin process",
|
||||
"metadata": {
|
||||
**task.metadata,
|
||||
"recovery": {
|
||||
"source": "advanced_material_startup",
|
||||
"previous_status": task.status,
|
||||
"observed_at": datetime.now(timezone.utc).isoformat(),
|
||||
},
|
||||
},
|
||||
},
|
||||
self.ctx,
|
||||
)
|
||||
if interrupted is None:
|
||||
skipped.append(task.task_id)
|
||||
continue
|
||||
if task.task_type in {
|
||||
"advanced_material_recognition",
|
||||
"advanced_style_source_recognition",
|
||||
"advanced_style_consensus",
|
||||
}:
|
||||
queued = self.service.queue_service.update_task_if_status(
|
||||
task.task_id,
|
||||
{"failed"},
|
||||
{
|
||||
"status": "queued",
|
||||
"progress": 0,
|
||||
"error_message": None,
|
||||
"metadata": {
|
||||
**interrupted.metadata,
|
||||
"recovery": {**interrupted.metadata.get("recovery", {}), "status": "requeued"},
|
||||
},
|
||||
},
|
||||
self.ctx,
|
||||
)
|
||||
if queued is not None:
|
||||
requeued.append(task.task_id)
|
||||
continue
|
||||
self.service.handle_task_failure(
|
||||
task,
|
||||
RuntimeError("task was interrupted by a previous plugin process"),
|
||||
self.ctx,
|
||||
)
|
||||
failed.append(task.task_id)
|
||||
return {"requeued_task_ids": requeued, "failed_task_ids": failed, "skipped_task_ids": skipped}
|
||||
|
||||
|
||||
def create_advanced_material_worker_runtime() -> AdvancedMaterialWorkerRuntime:
|
||||
return AdvancedMaterialWorkerRuntime(create_advanced_material_service())
|
||||
|
||||
|
||||
def _is_advanced_material_task(task: GenerationTask) -> bool:
|
||||
return task.metadata.get("source") == "advanced_material" and task.task_type in {
|
||||
"advanced_material_recognition",
|
||||
"advanced_material_validation_generation",
|
||||
"advanced_style_source_recognition",
|
||||
"advanced_style_consensus",
|
||||
"advanced_style_validation_generation",
|
||||
}
|
||||
文件差异内容过多而无法显示
加载差异
|
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
|||
from dataclasses import dataclass, field
|
||||
from typing import Any, Mapping
|
||||
|
||||
from backend.services.material_catalog_resolver import is_trusted_advanced_metadata
|
||||
from backend.services.material_normalization_service import CAPSULE_TIERS, WEAR_LAYERS
|
||||
|
||||
|
||||
|
|
@ -31,6 +32,7 @@ CONTRADICTIONS = (
|
|||
("fitted", "oversized"), ("matte", "glossy"), ("short sleeve", "long sleeve"),
|
||||
("transparent", "opaque"), ("minimal", "ornate"),
|
||||
)
|
||||
ADVANCED_VERIFIED_PRIORITY_BONUS = 20.0
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -130,6 +132,11 @@ class CompositionBuilder:
|
|||
user_adjustment = _float(item.get("user_adjustment"), 0.0)
|
||||
role_bonus = 20.0 if role == "identity" else 8.0 if category in {"dress", "suit", "top", "bottom", "outerwear"} else 0.0
|
||||
lock_bonus = 100.0 if locked else 0.0
|
||||
advanced_verified_bonus = (
|
||||
ADVANCED_VERIFIED_PRIORITY_BONUS
|
||||
if is_trusted_advanced_metadata(metadata)
|
||||
else 0.0
|
||||
)
|
||||
slots = _string_list(profile.get("body_slots"))
|
||||
automatic_visibility = 10.0 if plan.visible_slots and set(slots) & set(plan.visible_slots) else 0.0
|
||||
visibility_bonus = _float(item.get("visibility_bonus"), 0.0) + automatic_visibility
|
||||
|
|
@ -137,7 +144,7 @@ class CompositionBuilder:
|
|||
season_penalty = 12.0 if plan.season and _text(item.get("season")) and plan.season.casefold() != _text(item.get("season")).casefold() else 0.0
|
||||
low_confidence_penalty = max(0.0, 0.80 - confidence) * 40.0
|
||||
base_weight = BASE_WEIGHTS.get(category, BASE_WEIGHTS["other"])
|
||||
effective_weight = base_weight + user_adjustment + role_bonus + lock_bonus + visibility_bonus + restoration_feedback_bonus - low_confidence_penalty - season_penalty
|
||||
effective_weight = base_weight + user_adjustment + role_bonus + lock_bonus + advanced_verified_bonus + visibility_bonus + restoration_feedback_bonus - low_confidence_penalty - season_penalty
|
||||
tier = _text(item.get("capsule_tier") or "detailed").lower()
|
||||
if tier not in CAPSULE_TIERS:
|
||||
tier = "standard"
|
||||
|
|
@ -164,6 +171,7 @@ class CompositionBuilder:
|
|||
"user_adjustment": user_adjustment,
|
||||
"lock_bonus": lock_bonus,
|
||||
"role_bonus": role_bonus,
|
||||
"advanced_verified_bonus": advanced_verified_bonus,
|
||||
"visibility_bonus": visibility_bonus,
|
||||
"restoration_feedback_bonus": round(restoration_feedback_bonus, 3),
|
||||
"season_penalty": season_penalty,
|
||||
|
|
|
|||
|
|
@ -73,6 +73,47 @@ class CostService:
|
|||
task_usage["output_tokens"],
|
||||
_usage_number(usage, "output_tokens", "completion_tokens"),
|
||||
)
|
||||
if not any((profile_id, workflow_id, node_id)):
|
||||
# A style-set attempt projects its first validation case onto the
|
||||
# legacy attempt row for compatibility. Bill the child cases and
|
||||
# exclude that projection so the representative image is not
|
||||
# counted twice.
|
||||
advanced_rows = (
|
||||
self._advanced_material_attempt_rows()
|
||||
+ self._advanced_style_validation_case_rows()
|
||||
)
|
||||
for row in advanced_rows:
|
||||
created_at = _parse_datetime(row["created_at"])
|
||||
if since and created_at and created_at < since:
|
||||
continue
|
||||
provider = str(row["provider_id"] or "unknown")
|
||||
if provider_id and provider != provider_id:
|
||||
continue
|
||||
provider_model = str(row["provider_model"] or "unknown")
|
||||
model_route = str(row["model_route"] or "unknown")
|
||||
usage = _json_loads(row["usage_json"])
|
||||
key = (provider, provider_model, model_route)
|
||||
group = groups.setdefault(key, _new_group(provider, provider_model, model_route))
|
||||
if row["validation_image_uri"]:
|
||||
group["image_count"] += 1
|
||||
task_key = str(row["task_id"] or row["billing_id"])
|
||||
group["_task_ids"].add(task_key)
|
||||
task_usage = group["_usage_by_task"].setdefault(
|
||||
task_key,
|
||||
{"total_tokens": 0, "input_tokens": 0, "output_tokens": 0},
|
||||
)
|
||||
task_usage["total_tokens"] = max(
|
||||
task_usage["total_tokens"],
|
||||
_usage_number(usage, "total_tokens", "total"),
|
||||
)
|
||||
task_usage["input_tokens"] = max(
|
||||
task_usage["input_tokens"],
|
||||
_usage_number(usage, "input_tokens", "prompt_tokens"),
|
||||
)
|
||||
task_usage["output_tokens"] = max(
|
||||
task_usage["output_tokens"],
|
||||
_usage_number(usage, "output_tokens", "completion_tokens"),
|
||||
)
|
||||
finalized = [_finalize_group(group, prices) for group in groups.values()]
|
||||
totals = {
|
||||
"image_count": sum(group["image_count"] for group in finalized),
|
||||
|
|
@ -158,6 +199,40 @@ class CostService:
|
|||
finally:
|
||||
conn.close()
|
||||
|
||||
def _advanced_material_attempt_rows(self) -> list[Any]:
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
return conn.execute(
|
||||
"""
|
||||
SELECT attempt_id AS billing_id, task_id, model_route, provider_id,
|
||||
provider_model, usage_json, validation_image_uri, created_at
|
||||
FROM advanced_material_validation_attempts AS attempt
|
||||
WHERE (usage_json IS NOT NULL OR validation_image_uri IS NOT NULL)
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM advanced_style_validation_cases AS style_case
|
||||
WHERE style_case.attempt_id = attempt.attempt_id
|
||||
)
|
||||
ORDER BY created_at DESC
|
||||
"""
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def _advanced_style_validation_case_rows(self) -> list[Any]:
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
return conn.execute(
|
||||
"""
|
||||
SELECT case_id AS billing_id, task_id, model_route, provider_id,
|
||||
provider_model, usage_json, validation_image_uri, created_at
|
||||
FROM advanced_style_validation_cases
|
||||
WHERE usage_json IS NOT NULL OR validation_image_uri IS NOT NULL
|
||||
ORDER BY created_at DESC
|
||||
"""
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def _new_group(provider_id: str, provider_model: str, model_route: str) -> dict[str, Any]:
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ from database.db_helper import load_config_payload, load_database_settings
|
|||
|
||||
MAX_BACKUP_FILES = 50_000
|
||||
MAX_RESTORE_BYTES = 10 * 1024 * 1024 * 1024
|
||||
PROMPT_BACKUP_PLACEHOLDER = "[EXCLUDED_FROM_BACKUP]"
|
||||
|
||||
|
||||
class DataBackupService:
|
||||
|
|
@ -75,7 +76,7 @@ class DataBackupService:
|
|||
storage = config.get("storage") if isinstance(config.get("storage"), dict) else {}
|
||||
media_roots = {
|
||||
key: resolve_data_path(storage.get(f"{key}_path") or f"data/{key}")
|
||||
for key in ("images", "thumbnails", "materials", "styles")
|
||||
for key in ("images", "thumbnails", "materials", "advanced_materials", "styles")
|
||||
}
|
||||
if include_media:
|
||||
for key, media_root in media_roots.items():
|
||||
|
|
@ -89,7 +90,7 @@ class DataBackupService:
|
|||
entries.append(_entry(path, f"media/{key}/{path.relative_to(media_root).as_posix()}"))
|
||||
manifest = {
|
||||
"format": "imagestudio-full-backup",
|
||||
"version": 2,
|
||||
"version": 3,
|
||||
"material_schema_version": "1.0",
|
||||
"backup_id": backup_id,
|
||||
"created_at": datetime.now(timezone.utc).isoformat(),
|
||||
|
|
@ -137,7 +138,7 @@ class DataBackupService:
|
|||
shutil.copy2(db_source, db_target)
|
||||
config = load_config_payload(self.config_service.config_path)
|
||||
storage = config.get("storage") if isinstance(config.get("storage"), dict) else {}
|
||||
for key in ("images", "thumbnails", "materials", "styles"):
|
||||
for key in ("images", "thumbnails", "materials", "advanced_materials", "styles"):
|
||||
source_root = temp / "media" / key
|
||||
if not source_root.exists():
|
||||
continue
|
||||
|
|
@ -230,10 +231,56 @@ def _sanitize_sqlite_backup(
|
|||
if not include_prompt_records:
|
||||
if "materials" in tables:
|
||||
conn.execute("UPDATE materials SET prompt = NULL, negative_prompt = NULL")
|
||||
if "advanced_material_imports" in tables:
|
||||
conn.execute(
|
||||
"UPDATE advanced_material_imports "
|
||||
"SET recognition_prompt = NULL, recognition_negative_prompt = NULL"
|
||||
)
|
||||
if "advanced_material_validation_attempts" in tables:
|
||||
conn.execute(
|
||||
"UPDATE advanced_material_validation_attempts "
|
||||
"SET prompt = ?, negative_prompt = NULL, adjustment_text = NULL, decision_notes = NULL",
|
||||
(PROMPT_BACKUP_PLACEHOLDER,),
|
||||
)
|
||||
if "advanced_materials" in tables:
|
||||
conn.execute(
|
||||
"UPDATE advanced_materials SET prompt = ?, negative_prompt = NULL",
|
||||
(PROMPT_BACKUP_PLACEHOLDER,),
|
||||
)
|
||||
if "advanced_style_sources" in tables:
|
||||
conn.execute(
|
||||
"UPDATE advanced_style_sources "
|
||||
"SET recognition_prompt = NULL, recognition_negative_prompt = NULL"
|
||||
)
|
||||
if "advanced_style_consensus_runs" in tables:
|
||||
conn.execute(
|
||||
"UPDATE advanced_style_consensus_runs SET prompt = NULL, negative_prompt = NULL"
|
||||
)
|
||||
if "advanced_style_validation_cases" in tables:
|
||||
conn.execute(
|
||||
"UPDATE advanced_style_validation_cases "
|
||||
"SET content_prompt = ?, final_prompt = ?, negative_prompt = NULL",
|
||||
(PROMPT_BACKUP_PLACEHOLDER, PROMPT_BACKUP_PLACEHOLDER),
|
||||
)
|
||||
if "generation_images" in tables:
|
||||
conn.execute("UPDATE generation_images SET positive_prompt = NULL, negative_prompt = NULL")
|
||||
json_columns = {
|
||||
"materials": ("material_id", ("metadata_json",)),
|
||||
"advanced_material_imports": ("import_id", ("recognition_result_json", "error_json", "metadata_json")),
|
||||
"advanced_material_validation_attempts": ("attempt_id", ("usage_json", "error_json", "metadata_json")),
|
||||
"advanced_materials": ("material_id", ("capsule_json", "metadata_json")),
|
||||
"advanced_style_sources": (
|
||||
"source_id",
|
||||
("recognition_tags_json", "recognition_result_json", "error_json", "metadata_json"),
|
||||
),
|
||||
"advanced_style_consensus_runs": (
|
||||
"consensus_id",
|
||||
("source_ids_json", "tags_json", "result_json", "error_json", "metadata_json"),
|
||||
),
|
||||
"advanced_style_validation_cases": (
|
||||
"case_id",
|
||||
("usage_json", "style_review_json", "error_json", "metadata_json"),
|
||||
),
|
||||
"generation_images": ("image_id", ("metadata_json",)),
|
||||
"generation_tasks": ("task_id", ("request_json", "result_json", "metadata_json")),
|
||||
"audit_logs": ("audit_id", ("before_snapshot", "after_snapshot", "metadata_json")),
|
||||
|
|
@ -282,7 +329,7 @@ def _strip_sensitive_records(
|
|||
for key, item in value.items():
|
||||
lowered = str(key).lower()
|
||||
if not include_prompt_records and lowered in prompt_keys:
|
||||
result[key] = "[EXCLUDED_FROM_BACKUP]"
|
||||
result[key] = PROMPT_BACKUP_PLACEHOLDER
|
||||
continue
|
||||
if not include_moderation_records and lowered in moderation_keys:
|
||||
continue
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ from urllib.parse import urlsplit
|
|||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.services.image_service import ImageService
|
||||
from backend.services.material_catalog_resolver import is_trusted_advanced_metadata
|
||||
from core.storage import VfsStorage
|
||||
|
||||
|
||||
|
|
@ -61,29 +62,29 @@ class GenerationReferenceService:
|
|||
)
|
||||
if self.material_resolver is not None:
|
||||
for material_id, source in _material_reference_candidates(payload):
|
||||
prepared, canonical_key, byte_count = self._resolve_material(
|
||||
for prepared, canonical_key, byte_count in self._resolve_material_references(
|
||||
material_id,
|
||||
source=source,
|
||||
storage_cache=storage_cache,
|
||||
)
|
||||
total_bytes = _append_reference(
|
||||
resolved,
|
||||
seen,
|
||||
prepared,
|
||||
canonical_key,
|
||||
byte_count,
|
||||
total_bytes,
|
||||
source,
|
||||
)
|
||||
):
|
||||
total_bytes = _append_reference(
|
||||
resolved,
|
||||
seen,
|
||||
prepared,
|
||||
canonical_key,
|
||||
byte_count,
|
||||
total_bytes,
|
||||
source,
|
||||
)
|
||||
return tuple(resolved)
|
||||
|
||||
def _resolve_material(
|
||||
def _resolve_material_references(
|
||||
self,
|
||||
material_id: str,
|
||||
*,
|
||||
source: str,
|
||||
storage_cache: dict[str, tuple[str, int]],
|
||||
) -> tuple[str, str, int]:
|
||||
) -> list[tuple[str, str, int]]:
|
||||
material = self.material_resolver.get_material(material_id) if self.material_resolver else None
|
||||
if material is None:
|
||||
raise ApiError(
|
||||
|
|
@ -101,6 +102,12 @@ class GenerationReferenceService:
|
|||
"source": source,
|
||||
},
|
||||
)
|
||||
metadata = material.metadata if isinstance(getattr(material, "metadata", None), dict) else {}
|
||||
if is_trusted_advanced_metadata(metadata) and isinstance(metadata.get("style_set"), dict):
|
||||
# Quick/direct generation consumes the verified style capsule as
|
||||
# text. Source images are deliberately withheld to avoid leaking
|
||||
# sample characters, clothing, composition or scenery.
|
||||
return []
|
||||
image_uri = _optional_text(material.image_uri)
|
||||
if not image_uri:
|
||||
raise ApiError(
|
||||
|
|
@ -108,7 +115,9 @@ class GenerationReferenceService:
|
|||
"confirmed reference material has no image",
|
||||
{"material_id": material_id, "source": source},
|
||||
)
|
||||
return self._prepare_uri(image_uri, source=source, storage_cache=storage_cache)
|
||||
# Detailed persisted rounds expand trusted style representatives only
|
||||
# after the concrete route capabilities are known.
|
||||
return [self._prepare_uri(image_uri, source=source, storage_cache=storage_cache)]
|
||||
|
||||
def _resolve_candidate(
|
||||
self,
|
||||
|
|
|
|||
|
|
@ -222,7 +222,8 @@ class GenerationService:
|
|||
node=node,
|
||||
payload=enriched_payload,
|
||||
)
|
||||
execution_payload = _apply_round_reference_snapshot(enriched_payload, material_snapshot)
|
||||
routed_material_snapshot = _route_aware_reference_snapshot(material_snapshot, route_resolution)
|
||||
execution_payload = _apply_round_reference_snapshot(enriched_payload, routed_material_snapshot)
|
||||
execution_payload, ephemeral_reference_count = _strip_ephemeral_image_data_urls(execution_payload)
|
||||
if ephemeral_reference_count:
|
||||
raise ApiError(
|
||||
|
|
@ -250,7 +251,7 @@ class GenerationService:
|
|||
)
|
||||
reference_snapshot = {
|
||||
**provided_reference_snapshot,
|
||||
**material_snapshot,
|
||||
**routed_material_snapshot,
|
||||
"reference_binding_mapping": reference_mapping,
|
||||
}
|
||||
reference_snapshot, snapshot_ephemeral_count = _strip_ephemeral_image_data_urls(reference_snapshot)
|
||||
|
|
@ -2195,6 +2196,112 @@ def _apply_round_reference_snapshot(
|
|||
return result
|
||||
|
||||
|
||||
def _route_aware_reference_snapshot(
|
||||
snapshot: dict[str, Any],
|
||||
route_resolution: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
"""Select frozen style references according to the resolved adapter.
|
||||
|
||||
Ordinary materials and temporary references retain their legacy primary-
|
||||
image behavior. A verified style set may add up to two representative
|
||||
images only when the route explicitly supports semantic reference roles;
|
||||
the adapter's global ``max_reference_images`` cap is then applied to the
|
||||
final immutable list.
|
||||
"""
|
||||
|
||||
result = copy.deepcopy(snapshot if isinstance(snapshot, dict) else {})
|
||||
resolution = route_resolution if isinstance(route_resolution, dict) else {}
|
||||
settings = resolution.get("settings") if isinstance(resolution.get("settings"), dict) else {}
|
||||
capabilities = (
|
||||
resolution.get("capabilities")
|
||||
if isinstance(resolution.get("capabilities"), dict)
|
||||
else {}
|
||||
)
|
||||
supports_roles = _route_capability_bool(
|
||||
settings,
|
||||
capabilities,
|
||||
"supports_reference_roles",
|
||||
) is True
|
||||
supports_reference_images = _route_capability_bool(
|
||||
settings,
|
||||
capabilities,
|
||||
"reference_images",
|
||||
"supports_reference_images",
|
||||
)
|
||||
supports_style_multi_reference = supports_roles and supports_reference_images is not False
|
||||
|
||||
raw_limit = settings.get("max_reference_images")
|
||||
if raw_limit is None:
|
||||
raw_limit = capabilities.get("max_reference_images")
|
||||
try:
|
||||
max_references = min(max(int(raw_limit), 0), 16) if raw_limit is not None else 16
|
||||
except (TypeError, ValueError):
|
||||
max_references = 16
|
||||
|
||||
references: list[str] = []
|
||||
materials = result.get("materials") if isinstance(result.get("materials"), list) else []
|
||||
for material in materials:
|
||||
if not isinstance(material, dict):
|
||||
continue
|
||||
primary = _optional_text(material.get("image_uri"))
|
||||
metadata = material.get("metadata") if isinstance(material.get("metadata"), dict) else {}
|
||||
is_style_set = isinstance(metadata.get("style_set"), dict)
|
||||
if is_style_set and supports_style_multi_reference:
|
||||
candidates = _string_list(material.get("reference_image_uris"))
|
||||
if primary and primary not in candidates:
|
||||
candidates.insert(0, primary)
|
||||
selected = candidates[:3]
|
||||
else:
|
||||
selected = [primary] if primary else []
|
||||
for uri in selected:
|
||||
if uri and uri not in references:
|
||||
references.append(uri)
|
||||
|
||||
temporary = (
|
||||
result.get("temporary_references")
|
||||
if isinstance(result.get("temporary_references"), list)
|
||||
else []
|
||||
)
|
||||
for item in temporary:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
uri = _optional_text(item.get("image_uri"))
|
||||
if uri and uri not in references:
|
||||
references.append(uri)
|
||||
|
||||
result["reference_uris"] = references[:max_references]
|
||||
result["reference_route_policy"] = {
|
||||
"supports_reference_roles": supports_roles,
|
||||
"supports_reference_images": supports_reference_images,
|
||||
"style_multi_reference_enabled": supports_style_multi_reference,
|
||||
"max_reference_images": max_references,
|
||||
"selected_reference_count": len(result["reference_uris"]),
|
||||
}
|
||||
return result
|
||||
|
||||
|
||||
def _route_capability_bool(
|
||||
settings: dict[str, Any],
|
||||
capabilities: dict[str, Any],
|
||||
*keys: str,
|
||||
) -> bool | None:
|
||||
for source in (settings, capabilities):
|
||||
for key in keys:
|
||||
if key not in source:
|
||||
continue
|
||||
value = source.get(key)
|
||||
if isinstance(value, bool):
|
||||
return value
|
||||
if isinstance(value, (int, float)):
|
||||
return bool(value)
|
||||
text = str(value or "").strip().casefold()
|
||||
if text in {"1", "true", "yes", "on", "enabled"}:
|
||||
return True
|
||||
if text in {"0", "false", "no", "off", "disabled", ""}:
|
||||
return False
|
||||
return None
|
||||
|
||||
|
||||
def _round_timeout_seconds(request: dict[str, Any], route_resolution: Any) -> int:
|
||||
resolution = route_resolution if isinstance(route_resolution, dict) else {}
|
||||
settings = resolution.get("settings") if isinstance(resolution.get("settings"), dict) else {}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,30 @@ class MaterialAnalyticsService:
|
|||
conn = self.database.connect()
|
||||
try:
|
||||
materials = conn.execute("SELECT recognition_status, metadata_json FROM materials WHERE recognition_status != 'archived'").fetchall()
|
||||
advanced_materials = conn.execute(
|
||||
"""
|
||||
SELECT material.status, material.metadata_json, attempt.fidelity_score
|
||||
FROM advanced_materials AS material
|
||||
LEFT JOIN advanced_material_validation_attempts AS attempt
|
||||
ON attempt.attempt_id = material.accepted_attempt_id
|
||||
WHERE material.status != 'archived'
|
||||
"""
|
||||
).fetchall()
|
||||
advanced_attempts = conn.execute(
|
||||
"SELECT status, fidelity_score FROM advanced_material_validation_attempts"
|
||||
).fetchall()
|
||||
style_imports = conn.execute(
|
||||
"SELECT status, metadata_json FROM advanced_material_imports"
|
||||
).fetchall()
|
||||
style_sources = conn.execute(
|
||||
"SELECT status FROM advanced_style_sources"
|
||||
).fetchall()
|
||||
style_consensus_runs = conn.execute(
|
||||
"SELECT status FROM advanced_style_consensus_runs"
|
||||
).fetchall()
|
||||
style_validation_cases = conn.execute(
|
||||
"SELECT status, fidelity_score FROM advanced_style_validation_cases"
|
||||
).fetchall()
|
||||
audits = conn.execute("SELECT action, after_snapshot, metadata_json FROM audit_logs WHERE action IN ('wardrobe_match','apply_wardrobe_template','recognize_material','renormalize_material')").fetchall()
|
||||
tasks = conn.execute("SELECT model_route, request_json, metadata_json FROM generation_tasks").fetchall()
|
||||
finally:
|
||||
|
|
@ -62,6 +86,26 @@ class MaterialAnalyticsService:
|
|||
route_reasons[str(route.get("reason") or "unknown")] += 1
|
||||
route_counts[str(row["model_route"] or route.get("selected_route") or "unresolved")] += 1
|
||||
total = len(materials)
|
||||
accepted_advanced = sum(1 for row in advanced_materials if str(row["status"]) == "active")
|
||||
active_style_sets = sum(
|
||||
1
|
||||
for row in advanced_materials
|
||||
if str(row["status"]) == "active" and _is_style_set_metadata(_json(row["metadata_json"]))
|
||||
)
|
||||
style_set_import_rows = [
|
||||
row for row in style_imports if _is_style_set_metadata(_json(row["metadata_json"]))
|
||||
]
|
||||
style_source_statuses = Counter(str(row["status"] or "unknown") for row in style_sources)
|
||||
style_consensus_statuses = Counter(str(row["status"] or "unknown") for row in style_consensus_runs)
|
||||
style_case_statuses = Counter(str(row["status"] or "unknown") for row in style_validation_cases)
|
||||
style_case_scores = [
|
||||
float(row["fidelity_score"])
|
||||
for row in style_validation_cases
|
||||
if row["fidelity_score"] is not None
|
||||
]
|
||||
reviewed_attempts = [row for row in advanced_attempts if str(row["status"]) in {"accepted", "rejected"}]
|
||||
accepted_attempts = sum(1 for row in reviewed_attempts if str(row["status"]) == "accepted")
|
||||
scored_attempts = [float(row["fidelity_score"]) for row in reviewed_attempts if row["fidelity_score"] is not None]
|
||||
return {
|
||||
"materials": {
|
||||
"total": total,
|
||||
|
|
@ -71,6 +115,30 @@ class MaterialAnalyticsService:
|
|||
"needs_confirmation": needs_confirmation,
|
||||
"categories": dict(category_counts),
|
||||
},
|
||||
"advanced_materials": {
|
||||
"active": accepted_advanced,
|
||||
"active_style_sets": active_style_sets,
|
||||
"validation_attempts": len(advanced_attempts),
|
||||
"reviewed_attempts": len(reviewed_attempts),
|
||||
"acceptance_rate": round(accepted_attempts / len(reviewed_attempts), 4) if reviewed_attempts else 0.0,
|
||||
"average_fidelity_score": round(sum(scored_attempts) / len(scored_attempts), 4) if scored_attempts else None,
|
||||
},
|
||||
"advanced_style_sets": {
|
||||
"imports": len(style_set_import_rows),
|
||||
"accepted_imports": sum(1 for row in style_set_import_rows if str(row["status"]) == "accepted"),
|
||||
"active_materials": active_style_sets,
|
||||
"sources": len(style_sources),
|
||||
"source_statuses": dict(style_source_statuses),
|
||||
"consensus_runs": len(style_consensus_runs),
|
||||
"consensus_statuses": dict(style_consensus_statuses),
|
||||
"validation_cases": len(style_validation_cases),
|
||||
"validation_case_statuses": dict(style_case_statuses),
|
||||
"average_case_fidelity_score": (
|
||||
round(sum(style_case_scores) / len(style_case_scores), 4)
|
||||
if style_case_scores
|
||||
else None
|
||||
),
|
||||
},
|
||||
"composition": {
|
||||
"runs": composition_count,
|
||||
"conflicts": conflicts,
|
||||
|
|
@ -95,3 +163,11 @@ def _json(value: Any) -> dict[str, Any]:
|
|||
except (TypeError, json.JSONDecodeError):
|
||||
return {}
|
||||
return parsed if isinstance(parsed, dict) else {}
|
||||
|
||||
|
||||
def _is_style_set_metadata(metadata: dict[str, Any]) -> bool:
|
||||
return (
|
||||
str(metadata.get("workflow_kind") or "").strip().casefold() == "style_set"
|
||||
or isinstance(metadata.get("style_set"), dict)
|
||||
or metadata.get("style_set") is True
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,210 @@
|
|||
"""Resolve normal and verified advanced materials through one trusted catalog.
|
||||
|
||||
The two libraries deliberately keep separate persistence models. Generation
|
||||
and workflow code, however, only need the stable :class:`Material` contract.
|
||||
This adapter is the trust boundary between persisted user-editable metadata and
|
||||
the composition priority rules: reserved catalog fields are always replaced by
|
||||
server-derived values and cannot be promoted by writing ordinary metadata.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.advanced_material_dto import AdvancedMaterial
|
||||
from backend.dto.material_dto import Material, MaterialCategory
|
||||
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
|
||||
|
||||
ADVANCED_MATERIAL_ID_PREFIX = "advmat_"
|
||||
ADVANCED_VERIFIED_PRIORITY = 100
|
||||
_CATALOG_RESERVED_METADATA_KEYS = frozenset(
|
||||
{"library_kind", "priority_class", "material_priority", "verification"}
|
||||
)
|
||||
|
||||
|
||||
class CatalogMaterialMetadata(dict[str, Any]):
|
||||
"""Dict-compatible metadata carrying an in-process catalog trust marker.
|
||||
|
||||
The marker is intentionally an attribute rather than a serializable key.
|
||||
Data loaded from JSON can therefore never manufacture the marker. The
|
||||
object remains JSON-serializable anywhere the existing ``Material`` DTO is
|
||||
serialized.
|
||||
"""
|
||||
|
||||
__slots__ = ("_advanced_verified",)
|
||||
|
||||
def __init__(self, value: dict[str, Any], *, advanced_verified: bool):
|
||||
super().__init__(value)
|
||||
self._advanced_verified = bool(advanced_verified)
|
||||
|
||||
@property
|
||||
def advanced_verified(self) -> bool:
|
||||
return self._advanced_verified
|
||||
|
||||
|
||||
def is_trusted_advanced_metadata(value: Any) -> bool:
|
||||
"""Return whether metadata was resolved from an active advanced record."""
|
||||
|
||||
return (
|
||||
isinstance(value, CatalogMaterialMetadata)
|
||||
and value.advanced_verified
|
||||
and value.get("library_kind") == "advanced"
|
||||
and value.get("priority_class") == "advanced_verified"
|
||||
and value.get("material_priority") == ADVANCED_VERIFIED_PRIORITY
|
||||
)
|
||||
|
||||
|
||||
class MaterialCatalogResolver:
|
||||
"""Expose normal and accepted advanced materials via the legacy DTO."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
materials: MaterialRepository,
|
||||
advanced_materials: AdvancedMaterialRepository,
|
||||
):
|
||||
self.materials = materials
|
||||
self.advanced_materials = advanced_materials
|
||||
|
||||
def get_material(self, material_id: str) -> Material | None:
|
||||
normalized_id = str(material_id or "").strip()
|
||||
if not normalized_id:
|
||||
return None
|
||||
if normalized_id.startswith(ADVANCED_MATERIAL_ID_PREFIX):
|
||||
advanced = self.advanced_materials.get_material(normalized_id)
|
||||
if advanced is None or advanced.status != "active":
|
||||
return None
|
||||
return self._advanced_to_material(advanced)
|
||||
material = self.materials.get_material(normalized_id)
|
||||
return self._normal_material(material) if material is not None else None
|
||||
|
||||
def get_category(self, category_id: str) -> MaterialCategory | None:
|
||||
return self.materials.get_category(category_id)
|
||||
|
||||
def update_material(self, material_id: str, payload: dict[str, Any]) -> Material | None:
|
||||
"""Update restoration feedback without crossing the library boundary.
|
||||
|
||||
Catalog identity and priority fields remain server-owned even when the
|
||||
caller supplies a complete metadata replacement.
|
||||
"""
|
||||
|
||||
normalized_id = str(material_id or "").strip()
|
||||
if normalized_id.startswith(ADVANCED_MATERIAL_ID_PREFIX):
|
||||
current = self.advanced_materials.get_material(normalized_id)
|
||||
if current is None or current.status != "active":
|
||||
return None
|
||||
update = dict(payload or {})
|
||||
if "metadata" in update:
|
||||
update["metadata"] = _without_catalog_fields(update.get("metadata"))
|
||||
updated = self.advanced_materials.update_material(normalized_id, update)
|
||||
return (
|
||||
self._advanced_to_material(updated)
|
||||
if updated is not None and updated.status == "active"
|
||||
else None
|
||||
)
|
||||
update = dict(payload or {})
|
||||
if "metadata" in update:
|
||||
update["metadata"] = _without_catalog_fields(update.get("metadata"))
|
||||
updated = self.materials.update_material(normalized_id, update)
|
||||
return self._normal_material(updated) if updated is not None else None
|
||||
|
||||
def _normal_material(self, material: Material) -> Material:
|
||||
metadata = CatalogMaterialMetadata(
|
||||
{
|
||||
**_without_catalog_fields(material.metadata),
|
||||
"library_kind": "standard",
|
||||
"priority_class": "standard",
|
||||
"material_priority": 0,
|
||||
"verification": {},
|
||||
},
|
||||
advanced_verified=False,
|
||||
)
|
||||
return Material(
|
||||
material_id=material.material_id,
|
||||
category_id=material.category_id,
|
||||
display_name=material.display_name,
|
||||
image_uri=material.image_uri,
|
||||
thumbnail_uri=material.thumbnail_uri,
|
||||
prompt=material.prompt,
|
||||
negative_prompt=material.negative_prompt,
|
||||
tags=list(material.tags),
|
||||
recognition_status=material.recognition_status,
|
||||
metadata=metadata,
|
||||
created_at=material.created_at,
|
||||
updated_at=material.updated_at,
|
||||
)
|
||||
|
||||
def _advanced_to_material(self, material: AdvancedMaterial) -> Material:
|
||||
attempt = self.advanced_materials.get_attempt(material.accepted_attempt_id)
|
||||
verification: dict[str, Any] = {
|
||||
"status": "accepted",
|
||||
"import_id": material.import_id,
|
||||
"accepted_attempt_id": material.accepted_attempt_id,
|
||||
"source_image_uri": material.source_image_uri,
|
||||
"validation_image_uri": material.validation_image_uri,
|
||||
"validation_thumbnail_uri": material.validation_thumbnail_uri,
|
||||
"accepted_at": material.accepted_at,
|
||||
}
|
||||
if attempt is not None:
|
||||
verification.update(
|
||||
{
|
||||
"attempt_number": attempt.attempt_number,
|
||||
"fidelity_score": attempt.fidelity_score,
|
||||
"decision": attempt.decision,
|
||||
"decision_notes": attempt.decision_notes,
|
||||
"model_route": attempt.model_route,
|
||||
"decided_at": attempt.decided_at,
|
||||
}
|
||||
)
|
||||
metadata = _without_catalog_fields(material.metadata)
|
||||
capsules = _generation_capsules(material.capsule, metadata)
|
||||
if capsules:
|
||||
metadata["generation_capsules"] = capsules
|
||||
metadata = CatalogMaterialMetadata(
|
||||
{
|
||||
**metadata,
|
||||
"library_kind": "advanced",
|
||||
"priority_class": "advanced_verified",
|
||||
"material_priority": ADVANCED_VERIFIED_PRIORITY,
|
||||
"verification": verification,
|
||||
},
|
||||
advanced_verified=True,
|
||||
)
|
||||
return Material(
|
||||
material_id=material.material_id,
|
||||
category_id=material.category_id,
|
||||
display_name=material.display_name,
|
||||
image_uri=material.source_image_uri,
|
||||
thumbnail_uri=material.source_thumbnail_uri,
|
||||
prompt=material.prompt,
|
||||
negative_prompt=material.negative_prompt,
|
||||
tags=list(material.tags),
|
||||
recognition_status="confirmed",
|
||||
metadata=metadata,
|
||||
created_at=material.created_at,
|
||||
updated_at=material.updated_at,
|
||||
)
|
||||
|
||||
|
||||
def _without_catalog_fields(value: Any) -> dict[str, Any]:
|
||||
source = value if isinstance(value, dict) else {}
|
||||
return {
|
||||
str(key): deepcopy(child)
|
||||
for key, child in source.items()
|
||||
if str(key) not in _CATALOG_RESERVED_METADATA_KEYS
|
||||
}
|
||||
|
||||
|
||||
def _generation_capsules(capsule: Any, metadata: dict[str, Any]) -> dict[str, Any]:
|
||||
source = capsule if isinstance(capsule, dict) else {}
|
||||
nested = source.get("generation_capsules")
|
||||
if isinstance(nested, dict):
|
||||
return deepcopy(nested)
|
||||
if any(isinstance(source.get(tier), dict) for tier in ("compact", "standard", "detailed")):
|
||||
return deepcopy(source)
|
||||
if source:
|
||||
return {"detailed": deepcopy(source)}
|
||||
existing = metadata.get("generation_capsules")
|
||||
return deepcopy(existing) if isinstance(existing, dict) else {}
|
||||
|
|
@ -114,7 +114,14 @@ class MaterialNormalizationService:
|
|||
now = _utc_now()
|
||||
payload = deepcopy(dict(structured_data))
|
||||
raw_section = payload.get("raw_analysis") if isinstance(payload.get("raw_analysis"), dict) else payload
|
||||
raw_attributes = self._normalize_attributes(raw_section.get("attributes"), confidence)
|
||||
profile_input = payload.get("normalized_asset_profile")
|
||||
if not isinstance(profile_input, dict):
|
||||
profile_input = payload.get("asset_profile") if isinstance(payload.get("asset_profile"), dict) else {}
|
||||
profile_attributes = self._normalize_attributes(profile_input.get("attributes"), confidence)
|
||||
raw_attributes = {
|
||||
**profile_attributes,
|
||||
**self._normalize_attributes(raw_section.get("attributes"), confidence),
|
||||
}
|
||||
if not raw_attributes:
|
||||
raw_attributes = self._attributes_from_legacy(prompt, tags, confidence)
|
||||
raw_analysis = {
|
||||
|
|
@ -127,9 +134,6 @@ class MaterialNormalizationService:
|
|||
"source_payload": payload,
|
||||
}
|
||||
|
||||
profile_input = payload.get("normalized_asset_profile")
|
||||
if not isinstance(profile_input, dict):
|
||||
profile_input = payload.get("asset_profile") if isinstance(payload.get("asset_profile"), dict) else {}
|
||||
normalized = self._build_profile(
|
||||
profile_input,
|
||||
raw_attributes=raw_attributes,
|
||||
|
|
@ -195,6 +199,9 @@ class MaterialNormalizationService:
|
|||
item.get("source_state") != "observed" or float(item.get("confidence") or 0.0) < 0.80
|
||||
for item in raw_attributes.values()
|
||||
)
|
||||
must_preserve = _string_list(value.get("must_preserve"))
|
||||
if category == "style" and not must_preserve:
|
||||
must_preserve = _attribute_prompt_parts(attributes)[:16]
|
||||
return {
|
||||
"schema_version": MATERIAL_SCHEMA_VERSION,
|
||||
"prompt_version": MATERIAL_PROMPT_VERSION,
|
||||
|
|
@ -204,7 +211,7 @@ class MaterialNormalizationService:
|
|||
"body_slots": body_slots,
|
||||
"wear_layer": wear_layer,
|
||||
"attributes": attributes,
|
||||
"must_preserve": _string_list(value.get("must_preserve")),
|
||||
"must_preserve": must_preserve,
|
||||
"forbidden_changes": _string_list(value.get("forbidden_changes")) or _split_prompt(negative_prompt),
|
||||
"tags": sorted(set(tags)),
|
||||
"confidence": _confidence(value.get("confidence"), confidence),
|
||||
|
|
|
|||
|
|
@ -860,9 +860,16 @@ def _vision_provider_error(exc: VisionProviderError) -> dict[str, Any]:
|
|||
"provider_id": _optional_text(exc.provider_id) or "unknown",
|
||||
"status_code": exc.status_code,
|
||||
"error_type": _optional_text(exc.error_type) or "provider_error",
|
||||
"response_issue": str(exc)[:300],
|
||||
}
|
||||
if isinstance(exc.details.get("retryable"), bool):
|
||||
failure["retryable"] = exc.details["retryable"]
|
||||
reason = _optional_text(exc.details.get("reason") or exc.details.get("parse_error"))
|
||||
if reason:
|
||||
failure["reason"] = reason[:500]
|
||||
structured_keys = exc.details.get("structured_keys")
|
||||
if isinstance(structured_keys, list):
|
||||
failure["structured_keys"] = [str(item) for item in structured_keys[:50]]
|
||||
return failure
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -9,13 +9,14 @@ from typing import Any
|
|||
from urllib.parse import urlsplit
|
||||
|
||||
from backend.services.model_provider_service import ModelProviderService
|
||||
from core.model_route_chain import model_route_candidates
|
||||
from core.secret_service import SecretStore, resolve_secret_value
|
||||
from core.remote_fetch import read_http_response_limited, validate_http_url
|
||||
from core.image_formats import detect_image_format
|
||||
from core.storage import VfsStorage
|
||||
from database.db_helper import load_config_payload
|
||||
from engines.image_provider import FallbackImageProvider, ImageProvider, ImageProviderConfig, OpenAIImageProvider
|
||||
from engines.vision_provider import OpenAIVisionProvider, VisionProvider, VisionProviderConfig
|
||||
from engines.vision_provider import FallbackVisionProvider, OpenAIVisionProvider, VisionProvider, VisionProviderConfig
|
||||
|
||||
|
||||
class ModelProviderRuntime:
|
||||
|
|
@ -32,22 +33,29 @@ class ModelProviderRuntime:
|
|||
route_name = str(route_context.get("route_name") or "").strip()
|
||||
if not route_name:
|
||||
return None
|
||||
resolved = self.model_provider_service.resolve_route(route_name, modality="image_generation")
|
||||
if not resolved:
|
||||
candidates: list[dict[str, Any]] = []
|
||||
seen: set[str] = set()
|
||||
for candidate_name in model_route_candidates(route_name):
|
||||
if candidate_name.startswith("astrbot:"):
|
||||
continue
|
||||
candidate = self.model_provider_service.resolve_route(candidate_name, modality="image_generation")
|
||||
if candidate and str(candidate.get("route_id")) not in seen:
|
||||
candidates.append(candidate)
|
||||
seen.add(str(candidate.get("route_id")))
|
||||
if not candidates:
|
||||
return None
|
||||
candidates = [resolved]
|
||||
resolved = candidates[0]
|
||||
route_settings = resolved.get("settings") if isinstance(resolved.get("settings"), dict) else {}
|
||||
# A model selected in the UI must only call its own provider. Generic
|
||||
# cross-provider fallback can silently switch models and used to hide
|
||||
# the selected provider's real error. Safety routing is handled by the
|
||||
# generation governance layer instead.
|
||||
fallback_enabled = _configured_bool(
|
||||
fallback_enabled = len(candidates) > 1 or _configured_bool(
|
||||
route_context.get("allow_provider_fallback"),
|
||||
route_settings.get("allow_provider_fallback"),
|
||||
default=False,
|
||||
)
|
||||
if fallback_enabled:
|
||||
seen = {str(resolved.get("route_id"))}
|
||||
excluded_routes = {str(item).strip() for item in route_context.get("excluded_fallback_routes", []) if str(item).strip()} if isinstance(route_context.get("excluded_fallback_routes"), list) else set()
|
||||
fallback_routes = sorted(
|
||||
self.model_provider_service.list_routes(enabled=True),
|
||||
|
|
@ -104,6 +112,16 @@ class ModelProviderRuntime:
|
|||
route_name = str(route_context.get("route_name") or "").strip()
|
||||
if not route_name:
|
||||
return None
|
||||
providers = [
|
||||
provider
|
||||
for candidate in model_route_candidates(route_name)
|
||||
if (provider := self._vision_provider_for_single_route(candidate)) is not None
|
||||
]
|
||||
if not providers:
|
||||
return None
|
||||
return providers[0] if len(providers) == 1 else FallbackVisionProvider(providers)
|
||||
|
||||
def _vision_provider_for_single_route(self, route_name: str) -> VisionProvider | None:
|
||||
if route_name.startswith("astrbot:"):
|
||||
# AstrBot owns this provider's credentials and runtime client. Do
|
||||
# not mirror those secrets into ImageStudio's provider tables.
|
||||
|
|
@ -149,6 +167,32 @@ class ModelProviderRuntime:
|
|||
|
||||
def structured_text_callable(self, route_name: str):
|
||||
"""Return an OpenAI-compatible JSON completion callable for a configured route."""
|
||||
callables = [
|
||||
completion
|
||||
for candidate in model_route_candidates(route_name)
|
||||
if (completion := self._structured_text_callable_for_single_route(candidate)) is not None
|
||||
]
|
||||
if not callables:
|
||||
return None
|
||||
if len(callables) == 1:
|
||||
return callables[0]
|
||||
|
||||
def complete_with_fallback(request_payload: dict[str, Any]) -> dict[str, Any] | str:
|
||||
failures: list[str] = []
|
||||
for completion in callables:
|
||||
try:
|
||||
return completion(request_payload)
|
||||
except Exception as exc:
|
||||
failures.append(str(exc)[:500])
|
||||
raise RuntimeError(f"all configured text routes failed: {' | '.join(failures)}")
|
||||
|
||||
return complete_with_fallback
|
||||
|
||||
def _structured_text_callable_for_single_route(self, route_name: str):
|
||||
if route_name.startswith("astrbot:"):
|
||||
from core.astrbot_provider_bridge import astrbot_structured_text_callable
|
||||
|
||||
return astrbot_structured_text_callable(route_name)
|
||||
resolved = self.model_provider_service.resolve_route(route_name)
|
||||
if not resolved:
|
||||
return None
|
||||
|
|
@ -207,6 +251,37 @@ class ModelProviderRuntime:
|
|||
return complete
|
||||
|
||||
def structured_vision_callable(self, route_name: str, *, storage: VfsStorage | None = None):
|
||||
callables = [
|
||||
review
|
||||
for candidate in model_route_candidates(route_name)
|
||||
if (review := self._structured_vision_callable_for_single_route(candidate, storage=storage)) is not None
|
||||
]
|
||||
if not callables:
|
||||
return None
|
||||
if len(callables) == 1:
|
||||
return callables[0]
|
||||
|
||||
def review_with_fallback(request_payload: dict[str, Any]) -> dict[str, Any]:
|
||||
failures: list[str] = []
|
||||
for review in callables:
|
||||
try:
|
||||
return review(request_payload)
|
||||
except Exception as exc:
|
||||
failures.append(str(exc)[:500])
|
||||
raise RuntimeError(f"all configured vision review routes failed: {' | '.join(failures)}")
|
||||
|
||||
return review_with_fallback
|
||||
|
||||
def _structured_vision_callable_for_single_route(
|
||||
self,
|
||||
route_name: str,
|
||||
*,
|
||||
storage: VfsStorage | None = None,
|
||||
):
|
||||
if route_name.startswith("astrbot:"):
|
||||
from core.astrbot_provider_bridge import astrbot_structured_vision_callable
|
||||
|
||||
return astrbot_structured_vision_callable(route_name, storage=storage)
|
||||
resolved = self.model_provider_service.resolve_route(route_name)
|
||||
if not resolved:
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ from __future__ import annotations
|
|||
import re
|
||||
from typing import Any
|
||||
|
||||
from core.model_route_chain import model_route_candidates
|
||||
from core.secret_service import redact_provider_secret_state
|
||||
from database.repositories.model_provider_repository import ModelProviderRepository
|
||||
|
||||
|
|
@ -49,14 +50,21 @@ class ModelProviderService:
|
|||
return self.repository.list_routes(enabled=enabled)
|
||||
|
||||
def resolve_route(self, route_name: str, *, modality: str | None = None) -> dict[str, Any] | None:
|
||||
resolved = self.repository.resolve_route(route_name, modality=modality)
|
||||
if resolved or "/" not in route_name:
|
||||
return resolved
|
||||
|
||||
provider_id, model_id = route_name.split("/", 1)
|
||||
if not provider_id or not model_id:
|
||||
return None
|
||||
return self.repository.resolve_provider_model(provider_id, model_id, modality=modality)
|
||||
for candidate in model_route_candidates(route_name):
|
||||
if candidate.startswith("astrbot:"):
|
||||
continue
|
||||
resolved = self.repository.resolve_route(candidate, modality=modality)
|
||||
if resolved or "/" not in candidate:
|
||||
if resolved:
|
||||
return resolved
|
||||
continue
|
||||
provider_id, model_id = candidate.split("/", 1)
|
||||
if not provider_id or not model_id:
|
||||
continue
|
||||
resolved = self.repository.resolve_provider_model(provider_id, model_id, modality=modality)
|
||||
if resolved:
|
||||
return resolved
|
||||
return None
|
||||
|
||||
|
||||
def _required_text(value: Any, field: str) -> str:
|
||||
|
|
|
|||
|
|
@ -34,6 +34,31 @@ class OrphanMediaService:
|
|||
for table, columns in (
|
||||
("generation_images", ("image_uri", "thumbnail_uri")),
|
||||
("materials", ("image_uri", "thumbnail_uri")),
|
||||
(
|
||||
"advanced_material_imports",
|
||||
("source_image_uri", "source_thumbnail_uri"),
|
||||
),
|
||||
(
|
||||
"advanced_material_validation_attempts",
|
||||
("validation_image_uri", "validation_thumbnail_uri"),
|
||||
),
|
||||
(
|
||||
"advanced_materials",
|
||||
(
|
||||
"source_image_uri",
|
||||
"source_thumbnail_uri",
|
||||
"validation_image_uri",
|
||||
"validation_thumbnail_uri",
|
||||
),
|
||||
),
|
||||
(
|
||||
"advanced_style_sources",
|
||||
("source_image_uri", "source_thumbnail_uri"),
|
||||
),
|
||||
(
|
||||
"advanced_style_validation_cases",
|
||||
("validation_image_uri", "validation_thumbnail_uri"),
|
||||
),
|
||||
("character_profiles", ("avatar_image_uri",)),
|
||||
):
|
||||
for row in conn.execute(f"SELECT {', '.join(columns)} FROM {table}").fetchall():
|
||||
|
|
|
|||
|
|
@ -159,9 +159,19 @@ class PromptModerationService:
|
|||
self.reviewer = reviewer
|
||||
|
||||
def moderate(self, *, original_prompt: str, translated_prompt: str = "", final_prompt: str = "", negative_prompt: str = "", reference_bindings: list[dict[str, Any]] | None = None) -> PromptModerationDecision:
|
||||
text = "\n".join(_text(value) for value in (original_prompt, translated_prompt, final_prompt, negative_prompt) if _text(value))
|
||||
rule = self._rule_decision(text)
|
||||
model = self._model_decision(text, reference_bindings or [])
|
||||
positive_text = "\n".join(
|
||||
_text(value)
|
||||
for value in (original_prompt, translated_prompt, final_prompt)
|
||||
if _text(value)
|
||||
)
|
||||
# Negative prompts describe content that should be excluded. Treating
|
||||
# words such as "nudity", "gore" or "child" there as requested
|
||||
# content creates false adult/prohibited decisions.
|
||||
rule = self._rule_decision(positive_text)
|
||||
review_text = positive_text
|
||||
if negative := _text(negative_prompt):
|
||||
review_text = f"{positive_text}\n\nNegative constraints (must be excluded):\n{negative}"
|
||||
model = self._model_decision(review_text, reference_bindings or [])
|
||||
return _stricter_decision(rule, model)
|
||||
|
||||
def _rule_decision(self, text: str) -> PromptModerationDecision:
|
||||
|
|
@ -185,8 +195,18 @@ class PromptModerationService:
|
|||
return _decision("safe", 0.85, [], ["no_risk_signal"], "allow", None)
|
||||
|
||||
def _model_decision(self, text: str, references: list[dict[str, Any]]) -> PromptModerationDecision | None:
|
||||
if self.reviewer is None or not bool(self.config.get("llm_review_enabled", False)):
|
||||
if not bool(self.config.get("llm_review_enabled", False)):
|
||||
return None
|
||||
if self.reviewer is None:
|
||||
fallback = _text(self.config.get("failure_policy") or "fail_closed")
|
||||
return _decision(
|
||||
"uncertain" if fallback == "confirm" else "prohibited",
|
||||
0.5,
|
||||
["review_unavailable"],
|
||||
["moderation_service_unavailable"],
|
||||
"confirm_or_reject" if fallback == "confirm" else "reject",
|
||||
"内容审核模型不可用,已采用保守策略。",
|
||||
)
|
||||
try:
|
||||
response = self.reviewer({"text": text, "reference_bindings": references, "levels": list(RISK_LEVELS), "rules_version": self.RULES_VERSION})
|
||||
level = _text(response.get("level"))
|
||||
|
|
@ -254,7 +274,27 @@ class GenerationRouteService:
|
|||
|
||||
|
||||
class GenerationFailureClassifier:
|
||||
CONTENT_SIGNALS = ("nsfw", "safety", "content policy", "moderation", "sexual content", "adult content", "blocked prompt", "内容审核", "敏感内容")
|
||||
CONTENT_SIGNALS = (
|
||||
"nsfw",
|
||||
"safety",
|
||||
"content policy",
|
||||
"moderation",
|
||||
"sexual content",
|
||||
"adult content",
|
||||
"blocked prompt",
|
||||
"underage",
|
||||
"child sexual",
|
||||
"violence",
|
||||
"gore",
|
||||
"terror",
|
||||
"extremism",
|
||||
"内容审核",
|
||||
"敏感内容",
|
||||
)
|
||||
ADULT_SAFETY_SIGNALS = ("nsfw", "sexual content", "adult content", "nudity", "erotic", "porn")
|
||||
MINOR_SAFETY_SIGNALS = ("minor", "underage", "child sexual", "csam")
|
||||
VIOLENCE_SAFETY_SIGNALS = ("violence", "violent", "gore", "dismember", "beheading", "torture")
|
||||
TERROR_SAFETY_SIGNALS = ("terror", "extremism", "extremist")
|
||||
|
||||
def classify(self, failure: Mapping[str, Any] | BaseException) -> dict[str, Any]:
|
||||
if isinstance(failure, Mapping):
|
||||
|
|
@ -269,8 +309,19 @@ class GenerationFailureClassifier:
|
|||
detail = f"{failure} {details}"
|
||||
provider_id = _optional_text(getattr(failure, "provider_id", None))
|
||||
lower = f"{error_type} {detail}".casefold()
|
||||
safety_reason = None
|
||||
if any(signal in lower for signal in self.CONTENT_SIGNALS):
|
||||
category = "content_safety_rejection"
|
||||
if any(signal in lower for signal in self.MINOR_SAFETY_SIGNALS):
|
||||
safety_reason = "minor"
|
||||
elif any(signal in lower for signal in self.TERROR_SAFETY_SIGNALS):
|
||||
safety_reason = "terror"
|
||||
elif any(signal in lower for signal in self.VIOLENCE_SAFETY_SIGNALS):
|
||||
safety_reason = "violence"
|
||||
elif any(signal in lower for signal in self.ADULT_SAFETY_SIGNALS):
|
||||
safety_reason = "adult"
|
||||
else:
|
||||
safety_reason = "unknown"
|
||||
elif "quota" in lower or "credit" in lower or "balance" in lower:
|
||||
category = "quota_exhausted"
|
||||
elif status == 429 or "rate" in lower:
|
||||
|
|
@ -287,7 +338,14 @@ class GenerationFailureClassifier:
|
|||
category = "upstream_error"
|
||||
else:
|
||||
category = "unknown"
|
||||
return {"failure_class": category, "provider_id": provider_id, "status_code": status, "error_type": error_type, "summary": _redact(detail)[:500]}
|
||||
return {
|
||||
"failure_class": category,
|
||||
"safety_reason": safety_reason,
|
||||
"provider_id": provider_id,
|
||||
"status_code": status,
|
||||
"error_type": error_type,
|
||||
"summary": _redact(detail)[:500],
|
||||
}
|
||||
|
||||
|
||||
class ImageModerationService:
|
||||
|
|
|
|||
|
|
@ -102,6 +102,8 @@ class SettingsService:
|
|||
"title": "模型分工",
|
||||
"fields": [
|
||||
_field("material_recognition", "string", label="素材解析路由"),
|
||||
_field("advanced_material_recognition", "string", label="进阶素材识别路由"),
|
||||
_field("advanced_material_validation_generation", "string", label="进阶素材验证生图路由"),
|
||||
_field("profile_parse", "string", label="角色导入解析路由"),
|
||||
_field("persona_split", "string", label="人设拆解路由"),
|
||||
_field("prompt_planning", "string", label="提示词规划路由"),
|
||||
|
|
@ -128,6 +130,21 @@ class SettingsService:
|
|||
_field("category_requirements", "list", label="分类附加要求"),
|
||||
],
|
||||
},
|
||||
{
|
||||
"key": "advanced_materials",
|
||||
"title": "进阶素材",
|
||||
"fields": [
|
||||
_field("cost_budget.enabled", "bool", label="启用验证预算守卫"),
|
||||
_field("cost_budget.days", "number", label="用量统计窗口(天)"),
|
||||
_field("cost_budget.max_task_count", "number", label="窗口内最大验证任务数"),
|
||||
_field("cost_budget.max_image_count", "number", label="窗口内最大验证图片数"),
|
||||
_field("cost_budget.max_estimated_cost", "number", label="窗口内最大估算费用"),
|
||||
_field("cost_budget.price_key", "string", label="计价键(Provider 或模型 ID)"),
|
||||
_field("cost_budget.per_request", "number", label="每次请求单价"),
|
||||
_field("cost_budget.per_image", "number", label="每张图片单价"),
|
||||
_field("cost_budget.per_1k_tokens", "number", label="每千 Token 单价"),
|
||||
],
|
||||
},
|
||||
{
|
||||
"key": "prompt_governance",
|
||||
"title": "提示词与内容分流",
|
||||
|
|
@ -180,6 +197,7 @@ class SettingsService:
|
|||
_field("images_path", "path", label="图片目录"),
|
||||
_field("thumbnails_path", "path", label="缩略图目录"),
|
||||
_field("materials_path", "path", label="素材目录"),
|
||||
_field("advanced_materials_path", "path", label="进阶素材独立目录"),
|
||||
_field("styles_path", "path", label="风格目录"),
|
||||
_field("archives_path", "path", label="归档目录"),
|
||||
_field("temp_path", "path", label="临时目录"),
|
||||
|
|
@ -324,6 +342,30 @@ class SettingsService:
|
|||
days = -1
|
||||
if days < 0:
|
||||
errors.append({"field": f"prompt_governance.retention.{key}", "message": "retention days must be zero or greater"})
|
||||
advanced_materials = config.get("advanced_materials", {})
|
||||
if isinstance(advanced_materials, Mapping):
|
||||
budget = advanced_materials.get("cost_budget", {})
|
||||
if isinstance(budget, Mapping):
|
||||
for key in ("days", "max_task_count", "max_image_count"):
|
||||
value = budget.get(key)
|
||||
if value in (None, ""):
|
||||
continue
|
||||
try:
|
||||
normalized = int(value)
|
||||
except (TypeError, ValueError):
|
||||
normalized = -1
|
||||
if normalized < 0:
|
||||
errors.append({"field": f"advanced_materials.cost_budget.{key}", "message": "value must be zero or greater"})
|
||||
for key in ("max_estimated_cost", "per_request", "per_image", "per_1k_tokens"):
|
||||
value = budget.get(key)
|
||||
if value in (None, ""):
|
||||
continue
|
||||
try:
|
||||
normalized_cost = float(value)
|
||||
except (TypeError, ValueError):
|
||||
normalized_cost = -1
|
||||
if normalized_cost < 0:
|
||||
errors.append({"field": f"advanced_materials.cost_budget.{key}", "message": "value must be zero or greater"})
|
||||
return {"valid": not errors, "errors": errors, "warnings": warnings}
|
||||
|
||||
def diff(self, candidate: Mapping[str, Any]) -> dict[str, Any]:
|
||||
|
|
|
|||
|
|
@ -8,13 +8,24 @@ from uuid import uuid4
|
|||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.dto.wardrobe_dto import WardrobeItem
|
||||
from backend.services.composition_builder import CompositionBuilder, CompositionPlan
|
||||
from backend.services.composition_builder import (
|
||||
ADVANCED_VERIFIED_PRIORITY_BONUS,
|
||||
CompositionBuilder,
|
||||
CompositionPlan,
|
||||
)
|
||||
from backend.services.material_catalog_resolver import (
|
||||
MaterialCatalogResolver,
|
||||
is_trusted_advanced_metadata,
|
||||
)
|
||||
from backend.services.material_normalization_service import MaterialNormalizationService
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
from database.repositories.wardrobe_repository import WardrobeRepository
|
||||
from database.repositories.wardrobe_repository import (
|
||||
AdvancedWardrobeRepository,
|
||||
WardrobeRepository,
|
||||
)
|
||||
|
||||
|
||||
DEFAULT_WARDROBE_CATEGORIES = ("top", "bottom", "dress", "shoes", "accessory")
|
||||
|
|
@ -27,6 +38,8 @@ class WardrobeService:
|
|||
profile_repository: ProfileRepository,
|
||||
material_repository: MaterialRepository,
|
||||
audit_service: AuditService,
|
||||
advanced_wardrobe_repository: AdvancedWardrobeRepository | None = None,
|
||||
material_catalog: MaterialCatalogResolver | None = None,
|
||||
composition_builder: CompositionBuilder | None = None,
|
||||
normalization_service: MaterialNormalizationService | None = None,
|
||||
):
|
||||
|
|
@ -34,6 +47,8 @@ class WardrobeService:
|
|||
self.profile_repository = profile_repository
|
||||
self.material_repository = material_repository
|
||||
self.audit_service = audit_service
|
||||
self.advanced_wardrobe_repository = advanced_wardrobe_repository
|
||||
self.material_catalog = material_catalog
|
||||
self.composition_builder = composition_builder or CompositionBuilder()
|
||||
self.normalization_service = normalization_service or MaterialNormalizationService()
|
||||
|
||||
|
|
@ -49,13 +64,14 @@ class WardrobeService:
|
|||
"wardrobe_category is required",
|
||||
{"field": "wardrobe_category"},
|
||||
)
|
||||
material = self.material_repository.get_material(material_id)
|
||||
material = self._get_catalog_material(material_id)
|
||||
if material is None or material.recognition_status != "confirmed":
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "confirmed material not found", {"material_id": material_id})
|
||||
repository = self._repository_for_material_id(material_id)
|
||||
duplicate = next(
|
||||
(
|
||||
existing
|
||||
for existing in self.wardrobe_repository.list_by_profile(profile_id, wardrobe_category)
|
||||
for existing in repository.list_by_profile(profile_id, wardrobe_category)
|
||||
if existing.material_id == material_id
|
||||
),
|
||||
None,
|
||||
|
|
@ -63,7 +79,7 @@ class WardrobeService:
|
|||
if duplicate is not None:
|
||||
raise _duplicate_wardrobe_item_error(profile_id, material_id, wardrobe_category, duplicate.wardrobe_item_id)
|
||||
try:
|
||||
item = self.wardrobe_repository.create(
|
||||
item = repository.create(
|
||||
{
|
||||
"wardrobe_item_id": payload.get("wardrobe_item_id") or new_wardrobe_item_id(),
|
||||
"profile_id": profile_id,
|
||||
|
|
@ -95,18 +111,21 @@ class WardrobeService:
|
|||
ctx: RequestContext,
|
||||
) -> list[WardrobeItem]:
|
||||
self._ensure_profile(profile_id, ctx)
|
||||
return self.wardrobe_repository.list_by_profile(profile_id, wardrobe_category)
|
||||
items = self.wardrobe_repository.list_by_profile(profile_id, wardrobe_category)
|
||||
if self.advanced_wardrobe_repository is not None:
|
||||
items.extend(self.advanced_wardrobe_repository.list_by_profile(profile_id, wardrobe_category))
|
||||
return sorted(items, key=lambda item: (item.is_favorite, item.created_at), reverse=True)
|
||||
|
||||
def delete_item(self, profile_id: str, wardrobe_item_id: str, ctx: RequestContext) -> WardrobeItem:
|
||||
self._ensure_profile(profile_id, ctx)
|
||||
before = self.wardrobe_repository.get(wardrobe_item_id)
|
||||
repository, before = self._find_wardrobe_item(wardrobe_item_id)
|
||||
if before is None or before.profile_id != profile_id:
|
||||
raise ApiError(
|
||||
ErrorCode.RESOURCE_NOT_FOUND,
|
||||
"wardrobe item not found",
|
||||
{"wardrobe_item_id": wardrobe_item_id},
|
||||
)
|
||||
deleted = self.wardrobe_repository.delete(wardrobe_item_id)
|
||||
deleted = repository.delete(wardrobe_item_id)
|
||||
assert deleted is not None
|
||||
self.audit_service.record(
|
||||
ctx=ctx,
|
||||
|
|
@ -128,7 +147,7 @@ class WardrobeService:
|
|||
material_ids = [str(item).strip() for item in payload.get("material_ids", []) if str(item).strip()] if isinstance(payload.get("material_ids"), list) else []
|
||||
if not name or not material_ids:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "template name and material_ids are required", {"fields": ["name", "material_ids"]})
|
||||
owned_material_ids = {item.material_id for item in self.wardrobe_repository.list_by_profile(profile_id)}
|
||||
owned_material_ids = {item.material_id for item in self.list_items(profile_id, None, ctx)}
|
||||
missing = [material_id for material_id in material_ids if material_id not in owned_material_ids]
|
||||
if missing:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "template contains materials outside this wardrobe", {"material_ids": missing})
|
||||
|
|
@ -165,13 +184,13 @@ class WardrobeService:
|
|||
template = next((item for item in self.list_templates(profile_id, ctx) if item.get("template_id") == template_id), None)
|
||||
if template is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "wardrobe template not found", {"template_id": template_id})
|
||||
wardrobe = self.wardrobe_repository.list_by_profile(profile_id)
|
||||
wardrobe = self.list_items(profile_id, None, ctx)
|
||||
item_by_material = {item.material_id: item for item in wardrobe}
|
||||
selected: list[dict[str, Any]] = []
|
||||
options = template.get("material_options") if isinstance(template.get("material_options"), dict) else {}
|
||||
for material_id in template.get("material_ids") or []:
|
||||
item = item_by_material.get(str(material_id))
|
||||
material = self.material_repository.get_material(str(material_id))
|
||||
material = self._get_catalog_material(str(material_id))
|
||||
if item is None or material is None or material.recognition_status != "confirmed":
|
||||
continue
|
||||
selected.append({
|
||||
|
|
@ -204,13 +223,13 @@ class WardrobeService:
|
|||
missing_categories: list[str] = []
|
||||
material_options = payload.get("material_options") if isinstance(payload.get("material_options"), dict) else {}
|
||||
for category in categories:
|
||||
candidates = self.wardrobe_repository.list_by_profile(profile_id, str(category))
|
||||
candidates = self._list_category_candidates(profile_id, str(category))
|
||||
if not candidates:
|
||||
missing_categories.append(str(category))
|
||||
continue
|
||||
scored_candidates = []
|
||||
for index, item in enumerate(candidates):
|
||||
material = self.material_repository.get_material(item.material_id)
|
||||
material = self._get_catalog_material(item.material_id)
|
||||
if material is None or material.recognition_status != "confirmed":
|
||||
continue
|
||||
score, breakdown = _score_candidate(item, material, context)
|
||||
|
|
@ -219,7 +238,7 @@ class WardrobeService:
|
|||
missing_categories.append(str(category))
|
||||
continue
|
||||
score, _order, item, material, breakdown = max(scored_candidates, key=lambda candidate: (candidate[0], candidate[1]))
|
||||
self.wardrobe_repository.mark_used(item.wardrobe_item_id)
|
||||
self._repository_for_item(item).mark_used(item.wardrobe_item_id)
|
||||
selected.append(
|
||||
{
|
||||
"wardrobe_item_id": item.wardrobe_item_id,
|
||||
|
|
@ -243,6 +262,7 @@ class WardrobeService:
|
|||
"usage_count": item.usage_count,
|
||||
"score": score,
|
||||
"score_breakdown": breakdown,
|
||||
"library_kind": item.library_kind,
|
||||
**(
|
||||
material_options.get(material.material_id)
|
||||
if isinstance(material_options.get(material.material_id), dict)
|
||||
|
|
@ -300,6 +320,41 @@ class WardrobeService:
|
|||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "profile not found", {"profile_id": profile_id})
|
||||
return profile
|
||||
|
||||
def _get_catalog_material(self, material_id: str):
|
||||
if self.material_catalog is not None:
|
||||
return self.material_catalog.get_material(material_id)
|
||||
return self.material_repository.get_material(material_id)
|
||||
|
||||
def _repository_for_material_id(self, material_id: str) -> WardrobeRepository:
|
||||
if (
|
||||
self.advanced_wardrobe_repository is not None
|
||||
and self.material_catalog is not None
|
||||
and str(material_id or "").startswith("advmat_")
|
||||
):
|
||||
return self.advanced_wardrobe_repository
|
||||
return self.wardrobe_repository
|
||||
|
||||
def _repository_for_item(self, item: WardrobeItem) -> WardrobeRepository:
|
||||
if item.library_kind == "advanced" and self.advanced_wardrobe_repository is not None:
|
||||
return self.advanced_wardrobe_repository
|
||||
return self.wardrobe_repository
|
||||
|
||||
def _find_wardrobe_item(self, wardrobe_item_id: str) -> tuple[WardrobeRepository, WardrobeItem | None]:
|
||||
standard = self.wardrobe_repository.get(wardrobe_item_id)
|
||||
if standard is not None:
|
||||
return self.wardrobe_repository, standard
|
||||
if self.advanced_wardrobe_repository is not None:
|
||||
advanced = self.advanced_wardrobe_repository.get(wardrobe_item_id)
|
||||
if advanced is not None:
|
||||
return self.advanced_wardrobe_repository, advanced
|
||||
return self.wardrobe_repository, None
|
||||
|
||||
def _list_category_candidates(self, profile_id: str, category: str) -> list[WardrobeItem]:
|
||||
candidates = self.wardrobe_repository.list_by_profile(profile_id, category)
|
||||
if self.advanced_wardrobe_repository is not None:
|
||||
candidates.extend(self.advanced_wardrobe_repository.list_by_profile(profile_id, category))
|
||||
return candidates
|
||||
|
||||
|
||||
def new_wardrobe_item_id() -> str:
|
||||
return f"wardrobe_{uuid4().hex}"
|
||||
|
|
@ -358,7 +413,7 @@ def _match_context(payload: dict[str, Any]) -> dict[str, Any]:
|
|||
}
|
||||
|
||||
|
||||
def _score_candidate(item: WardrobeItem, material, context: dict[str, Any]) -> tuple[int, dict[str, int]]:
|
||||
def _score_candidate(item: WardrobeItem, material, context: dict[str, Any]) -> tuple[float, dict[str, float]]:
|
||||
item_terms = _text_set(item.tags) | _text_set(material.tags) | _words(material.display_name) | _words(material.prompt)
|
||||
requested_tags = context["requested_tags"]
|
||||
tag_overlap = len(item_terms & requested_tags)
|
||||
|
|
@ -371,6 +426,9 @@ def _score_candidate(item: WardrobeItem, material, context: dict[str, Any]) -> t
|
|||
"prompt_hits": prompt_hits * 3,
|
||||
"season": 12 if context["season"] and _optional_lower(item.season) == context["season"] else 0,
|
||||
"style": 12 if context["style"] and _optional_lower(item.style) == context["style"] else 0,
|
||||
"advanced_verified": ADVANCED_VERIFIED_PRIORITY_BONUS
|
||||
if is_trusted_advanced_metadata(getattr(material, "metadata", None))
|
||||
else 0,
|
||||
}
|
||||
return sum(breakdown.values()), breakdown
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ from typing import Any
|
|||
from urllib.parse import urlsplit
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.services.material_catalog_resolver import MaterialCatalogResolver, is_trusted_advanced_metadata
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
|
||||
|
|
@ -19,6 +20,7 @@ _SAFE_MATERIAL_METADATA_KEYS = frozenset(
|
|||
{
|
||||
"normalized_asset_profile",
|
||||
"generation_capsule",
|
||||
"generation_capsules",
|
||||
"body_slot",
|
||||
"wear_layer",
|
||||
"material_type",
|
||||
|
|
@ -27,6 +29,10 @@ _SAFE_MATERIAL_METADATA_KEYS = frozenset(
|
|||
"user_locked",
|
||||
"stackable",
|
||||
"note",
|
||||
"library_kind",
|
||||
"priority_class",
|
||||
"material_priority",
|
||||
"verification",
|
||||
}
|
||||
)
|
||||
|
||||
|
|
@ -35,7 +41,7 @@ class WorkflowMaterialSnapshotService:
|
|||
def __init__(
|
||||
self,
|
||||
workflows: WorkflowRepository,
|
||||
materials: MaterialRepository,
|
||||
materials: MaterialRepository | MaterialCatalogResolver,
|
||||
):
|
||||
self.workflows = workflows
|
||||
self.materials = materials
|
||||
|
|
@ -94,6 +100,20 @@ class WorkflowMaterialSnapshotService:
|
|||
},
|
||||
)
|
||||
image_uri = normalize_persistent_reference_uri(material.image_uri, field="material.image_uri")
|
||||
raw_metadata = material.metadata if isinstance(material.metadata, dict) else {}
|
||||
metadata = _safe_material_metadata(raw_metadata)
|
||||
style_set = (
|
||||
raw_metadata.get("style_set")
|
||||
if is_trusted_advanced_metadata(raw_metadata) and isinstance(raw_metadata.get("style_set"), dict)
|
||||
else {}
|
||||
)
|
||||
if style_set:
|
||||
metadata["style_set"] = _plain_value(style_set)
|
||||
reference_image_uris = [
|
||||
normalize_persistent_reference_uri(uri, field="material.metadata.style_set.reference_source_uris")
|
||||
for uri in _string_list(style_set.get("reference_source_uris"))[:3]
|
||||
]
|
||||
reference_image_uris = list(dict.fromkeys([image_uri, *reference_image_uris]))[:3]
|
||||
category = self.materials.get_category(material.category_id) if material.category_id else None
|
||||
binding = binding_by_material.get(material_id, {})
|
||||
frozen_materials.append(
|
||||
|
|
@ -103,6 +123,7 @@ class WorkflowMaterialSnapshotService:
|
|||
"category_id": material.category_id,
|
||||
"category_name": category.name if category else None,
|
||||
"image_uri": image_uri,
|
||||
"reference_image_uris": reference_image_uris,
|
||||
"thumbnail_uri": _optional_persistent_uri(material.thumbnail_uri),
|
||||
"prompt": material.prompt,
|
||||
"negative_prompt": material.negative_prompt,
|
||||
|
|
@ -110,7 +131,7 @@ class WorkflowMaterialSnapshotService:
|
|||
"recognition_status": material.recognition_status,
|
||||
"reference_strength": _plain_value(binding.get("reference_strength")),
|
||||
"target_node_keys": _string_list(binding.get("target_node_keys")),
|
||||
"metadata": _safe_material_metadata(material.metadata),
|
||||
"metadata": metadata,
|
||||
"source_updated_at": material.updated_at,
|
||||
}
|
||||
)
|
||||
|
|
@ -140,7 +161,11 @@ class WorkflowMaterialSnapshotService:
|
|||
"created_at": datetime.now(UTC).isoformat(),
|
||||
"material_ids": [item["material_id"] for item in frozen_materials],
|
||||
"reference_uris": [
|
||||
*[item["image_uri"] for item in frozen_materials],
|
||||
*[
|
||||
uri
|
||||
for item in frozen_materials
|
||||
for uri in item.get("reference_image_uris", [item["image_uri"]])
|
||||
],
|
||||
*[item["image_uri"] for item in frozen_temporary],
|
||||
],
|
||||
"prompt_context": [
|
||||
|
|
|
|||
|
|
@ -20,6 +20,7 @@ from backend.services.material_binding_schema_service import (
|
|||
NODE_MATERIAL_BINDING_SCHEMA_VERSION,
|
||||
material_bindings_for_node,
|
||||
)
|
||||
from backend.services.material_catalog_resolver import MaterialCatalogResolver
|
||||
from backend.services.resource_access_service import ResourceAccessService
|
||||
from backend.services.workflow_material_snapshot_service import (
|
||||
normalize_persistent_reference_uri,
|
||||
|
|
@ -58,7 +59,7 @@ class WorkflowNodeMaterialService:
|
|||
def __init__(
|
||||
self,
|
||||
workflow_service: WorkflowService,
|
||||
material_repository: MaterialRepository,
|
||||
material_repository: MaterialRepository | MaterialCatalogResolver,
|
||||
*,
|
||||
round_repository: WorkflowRoundRepository | None = None,
|
||||
access_service: ResourceAccessService | None = None,
|
||||
|
|
@ -304,6 +305,7 @@ class WorkflowNodeMaterialService:
|
|||
material_source = source.get("material") if isinstance(source.get("material"), dict) else {}
|
||||
strength = _reference_strength(source.get("reference_strength"))
|
||||
colors = _string_list(material.metadata.get("colors")) if isinstance(material.metadata, dict) else []
|
||||
catalog_metadata = material.metadata if isinstance(material.metadata, dict) else {}
|
||||
return {
|
||||
"id": str(source.get("id") or f"node_material_{material_id}"),
|
||||
"category": str(
|
||||
|
|
@ -319,6 +321,10 @@ class WorkflowNodeMaterialService:
|
|||
"group": category.name if category is not None else None,
|
||||
"prompt": material.prompt or material_source.get("prompt"),
|
||||
"colors": colors,
|
||||
"library_kind": catalog_metadata.get("library_kind", "standard"),
|
||||
"priority_class": catalog_metadata.get("priority_class", "standard"),
|
||||
"material_priority": catalog_metadata.get("material_priority", 0),
|
||||
"verification": deepcopy(catalog_metadata.get("verification") or {}),
|
||||
},
|
||||
"target_stages": [],
|
||||
"target_node_keys": [canonical_gradual_node_key(node.node_key)],
|
||||
|
|
|
|||
19
config.yaml
19
config.yaml
|
|
@ -1,7 +1,7 @@
|
|||
plugin:
|
||||
name: astrbot_plugin_imagestudio
|
||||
display_name: ImageStudio
|
||||
version: 1.0.20
|
||||
version: 1.0.32
|
||||
|
||||
features:
|
||||
quick_profile: true
|
||||
|
|
@ -46,6 +46,8 @@ vision:
|
|||
# 按功能分配模型路由。留空时回退到 generation.default_model 或 vision.default_route。
|
||||
model_assignments:
|
||||
material_recognition: material_vlm
|
||||
advanced_material_recognition: material_vlm
|
||||
advanced_material_validation_generation: ""
|
||||
profile_parse: ""
|
||||
persona_split: ""
|
||||
prompt_planning: ""
|
||||
|
|
@ -71,6 +73,20 @@ recognition_prompts:
|
|||
请先判断图片中素材的准确分类,再尽可能完整地提取所有可见事实,包括物品类型、主色与辅色、材质、纹理、结构、轮廓、图案、裁剪、缝线、褶皱、装饰、配件、状态、视角、光照及可复用的设计特征。不要把无法确认的推测写成事实,并明确区分确定信息与低置信度判断。根据识别结果生成一段细节充分、可直接用于生图且能够保留原素材设计的正向提示词,同时生成用于排除错误类别、错误材质、结构畸变、文字、水印和无关背景的负向提示词。仅返回 JSON,字段必须包含 prompt、negative_prompt、tags、confidence、description;tags 应简短、可检索,并包含分类、颜色、材质和关键设计元素。
|
||||
category_requirements: []
|
||||
|
||||
# 进阶素材验证图的独立预算守卫。开启后,每次验证(含自动 NSFW
|
||||
# 回退)都会在调用提供方前检查时间窗口内的任务、图片和估算费用。
|
||||
advanced_materials:
|
||||
cost_budget:
|
||||
enabled: false
|
||||
days: 30
|
||||
max_task_count: null
|
||||
max_image_count: null
|
||||
max_estimated_cost: null
|
||||
price_key: ""
|
||||
per_request: 0
|
||||
per_image: 0
|
||||
per_1k_tokens: 0
|
||||
|
||||
# 提示词转译、内容审核和普通/NSFW 模型动态分流。
|
||||
# 默认只启用本地规则审核;LLM 转译与成人自动分流必须由管理员明确开启。
|
||||
prompt_governance:
|
||||
|
|
@ -126,6 +142,7 @@ storage:
|
|||
images_path: data/images
|
||||
thumbnails_path: data/thumbnails
|
||||
materials_path: data/materials
|
||||
advanced_materials_path: data/advanced_materials
|
||||
styles_path: data/styles
|
||||
archives_path: data/archives
|
||||
temp_path: data/temp
|
||||
|
|
|
|||
|
|
@ -3,15 +3,19 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import base64
|
||||
import concurrent.futures
|
||||
import json
|
||||
import re
|
||||
from typing import Any, Mapping
|
||||
|
||||
from core.image_formats import detect_image_format
|
||||
from core.storage import VfsStorage
|
||||
from engines.vision_provider import (
|
||||
VisionProviderError,
|
||||
VisionRecognitionRequest,
|
||||
VisionRecognitionResult,
|
||||
vision_image_urls,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -93,6 +97,75 @@ def astrbot_structured_text_callable(route_name: str):
|
|||
return complete
|
||||
|
||||
|
||||
def astrbot_structured_vision_callable(
|
||||
route_name: str,
|
||||
*,
|
||||
storage: VfsStorage | None = None,
|
||||
):
|
||||
"""Return a generic structured-image reviewer backed by AstrBot.
|
||||
|
||||
Unlike :func:`astrbot_vision_provider`, this adapter does not require the
|
||||
response to contain a material ``prompt`` field. It is used by generated
|
||||
image moderation and other JSON-only vision checks.
|
||||
"""
|
||||
|
||||
route = str(route_name or "").strip()
|
||||
if not route.startswith("astrbot:"):
|
||||
return None
|
||||
provider_id = route.split(":", 1)[1].strip()
|
||||
provider = _provider_by_id(provider_id)
|
||||
loop = _runtime_loop
|
||||
if provider is None or loop is None or loop.is_closed():
|
||||
return None
|
||||
vfs = storage or VfsStorage()
|
||||
model = str(
|
||||
_call_text(provider, "get_model")
|
||||
or _provider_config(provider).get("model")
|
||||
or _meta_value(provider, "model")
|
||||
or ""
|
||||
).strip()
|
||||
|
||||
def review(request_payload: dict[str, Any]) -> dict[str, Any]:
|
||||
image = request_payload.get("image") if isinstance(request_payload.get("image"), dict) else {}
|
||||
uri = str(image.get("image_uri") or image.get("thumbnail_uri") or "").strip()
|
||||
if uri.startswith("storage://"):
|
||||
path = vfs.ensure_file(uri)
|
||||
body = path.read_bytes()
|
||||
if len(body) > 25 * 1024 * 1024:
|
||||
raise ValueError("image moderation input exceeds size limit")
|
||||
image_format = detect_image_format(body)
|
||||
if image_format is None:
|
||||
raise ValueError("image moderation input is not a supported image")
|
||||
uri = f"data:{image_format.mime_type};base64,{base64.b64encode(body).decode('ascii')}"
|
||||
if not uri.startswith(("data:image/", "http://", "https://")):
|
||||
raise ValueError("image moderation requires a storage, data, or HTTP image URI")
|
||||
|
||||
async def invoke() -> str:
|
||||
response = await provider.text_chat(
|
||||
prompt=json.dumps(request_payload, ensure_ascii=False),
|
||||
image_urls=[uri],
|
||||
system_prompt=(
|
||||
"Review the attached image and return one JSON object only. Follow the requested actions and "
|
||||
"requirements exactly. Detect age ambiguity, unexpected nudity, terrorism, high-risk violence, "
|
||||
"extreme gore, and content beyond the allowed request. Never weaken a safety classification."
|
||||
),
|
||||
model=model or None,
|
||||
)
|
||||
return str(getattr(response, "completion_text", "") or "").strip()
|
||||
|
||||
future = asyncio.run_coroutine_threadsafe(invoke(), loop)
|
||||
try:
|
||||
text = future.result(timeout=ASTRBOT_VISION_TIMEOUT_SECONDS)
|
||||
except concurrent.futures.TimeoutError as exc:
|
||||
future.cancel()
|
||||
raise RuntimeError("AstrBot structured vision provider timed out") from exc
|
||||
if not text:
|
||||
raise ValueError("AstrBot structured vision provider returned an empty response")
|
||||
return _json_object_from_text(text)
|
||||
|
||||
return review
|
||||
|
||||
|
||||
def astrbot_vision_provider(route_name: str):
|
||||
"""Return a synchronous material-vision adapter backed by AstrBot.
|
||||
|
||||
|
|
@ -140,7 +213,8 @@ class _AstrBotVisionProvider:
|
|||
self.loop = loop
|
||||
|
||||
def recognize_material(self, request: VisionRecognitionRequest) -> VisionRecognitionResult:
|
||||
if not str(request.image_uri or "").startswith(("data:image/", "http://", "https://")):
|
||||
image_urls = list(vision_image_urls(request))
|
||||
if not image_urls:
|
||||
raise VisionProviderError(
|
||||
"AstrBot vision input must be a data or HTTP image URL",
|
||||
provider_id=self.provider_id,
|
||||
|
|
@ -150,7 +224,7 @@ class _AstrBotVisionProvider:
|
|||
async def invoke() -> str:
|
||||
response = await self.provider.text_chat(
|
||||
prompt=_material_user_prompt(request),
|
||||
image_urls=[request.image_uri],
|
||||
image_urls=image_urls,
|
||||
system_prompt=_material_system_prompt(request),
|
||||
model=request.model or self.model or None,
|
||||
)
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ from core.runtime_paths import resolve_config_path
|
|||
from database.db_helper import load_config_payload
|
||||
|
||||
|
||||
PLUGIN_VERSION = "1.0.20"
|
||||
PLUGIN_VERSION = "1.0.32"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -25,6 +25,7 @@ class StorageConfig:
|
|||
images_path: str = "data/images"
|
||||
thumbnails_path: str = "data/thumbnails"
|
||||
materials_path: str = "data/materials"
|
||||
advanced_materials_path: str = "data/advanced_materials"
|
||||
styles_path: str = "data/styles"
|
||||
archives_path: str = "data/archives"
|
||||
temp_path: str = "data/temp"
|
||||
|
|
@ -74,6 +75,9 @@ class ConfigService:
|
|||
images_path=str(storage.get("images_path", "data/images")),
|
||||
thumbnails_path=str(storage.get("thumbnails_path", "data/thumbnails")),
|
||||
materials_path=str(storage.get("materials_path", "data/materials")),
|
||||
advanced_materials_path=str(
|
||||
storage.get("advanced_materials_path", "data/advanced_materials")
|
||||
),
|
||||
styles_path=str(storage.get("styles_path", "data/styles")),
|
||||
archives_path=str(storage.get("archives_path", "data/archives")),
|
||||
temp_path=str(storage.get("temp_path", "data/temp")),
|
||||
|
|
|
|||
79
core/model_route_chain.py
普通文件
79
core/model_route_chain.py
普通文件
|
|
@ -0,0 +1,79 @@
|
|||
"""Backward-compatible two-source model route chains.
|
||||
|
||||
The AstrBot configuration schema exposes model assignments as strings. Keep
|
||||
that stable while allowing each assignment to carry one ImageStudio route and
|
||||
one AstrBot-hosted fallback route.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
ROUTE_CHAIN_PREFIX = "route-chain:"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ModelRouteChain:
|
||||
plugin: str = ""
|
||||
astrbot: str = ""
|
||||
|
||||
@property
|
||||
def candidates(self) -> tuple[str, ...]:
|
||||
values: list[str] = []
|
||||
for value in (self.plugin, self.astrbot):
|
||||
route = str(value or "").strip()
|
||||
if route and route not in values:
|
||||
values.append(route)
|
||||
return tuple(values)
|
||||
|
||||
|
||||
def parse_model_route_chain(value: Any) -> ModelRouteChain:
|
||||
route = str(value or "").strip()
|
||||
if not route:
|
||||
return ModelRouteChain()
|
||||
if route.startswith(ROUTE_CHAIN_PREFIX):
|
||||
try:
|
||||
payload = json.loads(route[len(ROUTE_CHAIN_PREFIX) :])
|
||||
except (TypeError, ValueError, json.JSONDecodeError):
|
||||
payload = None
|
||||
if isinstance(payload, dict):
|
||||
return ModelRouteChain(
|
||||
plugin=_text(payload.get("plugin")),
|
||||
astrbot=_astrbot_text(payload.get("astrbot")),
|
||||
)
|
||||
if route.startswith("astrbot:"):
|
||||
return ModelRouteChain(astrbot=route)
|
||||
return ModelRouteChain(plugin=route)
|
||||
|
||||
|
||||
def encode_model_route_chain(plugin: Any = "", astrbot: Any = "") -> str:
|
||||
plugin_route = _text(plugin)
|
||||
astrbot_route = _astrbot_text(astrbot)
|
||||
if plugin_route and not astrbot_route:
|
||||
return plugin_route
|
||||
if astrbot_route and not plugin_route:
|
||||
return astrbot_route
|
||||
if not plugin_route and not astrbot_route:
|
||||
return ""
|
||||
return ROUTE_CHAIN_PREFIX + json.dumps(
|
||||
{"plugin": plugin_route, "astrbot": astrbot_route},
|
||||
ensure_ascii=False,
|
||||
separators=(",", ":"),
|
||||
sort_keys=True,
|
||||
)
|
||||
|
||||
|
||||
def model_route_candidates(value: Any) -> tuple[str, ...]:
|
||||
return parse_model_route_chain(value).candidates
|
||||
|
||||
|
||||
def _text(value: Any) -> str:
|
||||
return str(value or "").strip()
|
||||
|
||||
|
||||
def _astrbot_text(value: Any) -> str:
|
||||
route = _text(value)
|
||||
return route if not route or route.startswith("astrbot:") else ""
|
||||
|
|
@ -148,6 +148,7 @@ def _storage_roots(config: ImageStudioConfig) -> dict[str, str]:
|
|||
"images": config.storage.images_path,
|
||||
"thumbnails": config.storage.thumbnails_path,
|
||||
"materials": config.storage.materials_path,
|
||||
"advanced_materials": config.storage.advanced_materials_path,
|
||||
"styles": config.storage.styles_path,
|
||||
"archives": config.storage.archives_path,
|
||||
"temp": config.storage.temp_path,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,109 @@
|
|||
CREATE TABLE IF NOT EXISTS advanced_material_imports (
|
||||
import_id TEXT PRIMARY KEY,
|
||||
category_id TEXT,
|
||||
display_name TEXT NOT NULL,
|
||||
source_image_uri TEXT NOT NULL,
|
||||
source_thumbnail_uri TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'pending_recognition',
|
||||
recognition_prompt TEXT,
|
||||
recognition_negative_prompt TEXT,
|
||||
recognition_tags_json TEXT,
|
||||
recognition_result_json TEXT,
|
||||
recognition_model_route TEXT,
|
||||
prompt_revision INTEGER NOT NULL DEFAULT 0,
|
||||
current_attempt_id TEXT,
|
||||
accepted_material_id TEXT,
|
||||
idempotency_key TEXT,
|
||||
created_by TEXT,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(category_id) REFERENCES material_categories(category_id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_material_imports_idempotency
|
||||
ON advanced_material_imports(idempotency_key)
|
||||
WHERE idempotency_key IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_material_imports_status
|
||||
ON advanced_material_imports(status, updated_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_material_imports_category
|
||||
ON advanced_material_imports(category_id, created_at);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS advanced_material_validation_attempts (
|
||||
attempt_id TEXT PRIMARY KEY,
|
||||
import_id TEXT NOT NULL,
|
||||
attempt_number INTEGER NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'queued',
|
||||
adjustment_text TEXT,
|
||||
prompt TEXT NOT NULL,
|
||||
negative_prompt TEXT,
|
||||
model_route TEXT,
|
||||
provider_id TEXT,
|
||||
provider_model TEXT,
|
||||
task_id TEXT,
|
||||
validation_image_uri TEXT,
|
||||
validation_thumbnail_uri TEXT,
|
||||
seed INTEGER,
|
||||
width INTEGER,
|
||||
height INTEGER,
|
||||
usage_json TEXT,
|
||||
fidelity_score REAL,
|
||||
decision TEXT,
|
||||
decision_notes TEXT,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
completed_at TIMESTAMP,
|
||||
decided_at TIMESTAMP,
|
||||
FOREIGN KEY(import_id) REFERENCES advanced_material_imports(import_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(task_id) REFERENCES generation_tasks(task_id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_material_attempts_sequence
|
||||
ON advanced_material_validation_attempts(import_id, attempt_number);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_material_attempts_status
|
||||
ON advanced_material_validation_attempts(status, updated_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_material_attempts_task
|
||||
ON advanced_material_validation_attempts(task_id);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_material_attempts_one_active
|
||||
ON advanced_material_validation_attempts(import_id)
|
||||
WHERE status IN ('queued', 'running');
|
||||
|
||||
CREATE TABLE IF NOT EXISTS advanced_materials (
|
||||
material_id TEXT PRIMARY KEY,
|
||||
import_id TEXT NOT NULL UNIQUE,
|
||||
accepted_attempt_id TEXT NOT NULL UNIQUE,
|
||||
category_id TEXT,
|
||||
display_name TEXT NOT NULL,
|
||||
source_image_uri TEXT NOT NULL,
|
||||
source_thumbnail_uri TEXT,
|
||||
validation_image_uri TEXT NOT NULL,
|
||||
validation_thumbnail_uri TEXT,
|
||||
prompt TEXT NOT NULL,
|
||||
negative_prompt TEXT,
|
||||
tags_json TEXT,
|
||||
priority INTEGER NOT NULL DEFAULT 100,
|
||||
status TEXT NOT NULL DEFAULT 'active',
|
||||
capsule_json TEXT,
|
||||
metadata_json TEXT,
|
||||
accepted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
archived_at TIMESTAMP,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(import_id) REFERENCES advanced_material_imports(import_id) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(accepted_attempt_id) REFERENCES advanced_material_validation_attempts(attempt_id) ON DELETE RESTRICT,
|
||||
FOREIGN KEY(category_id) REFERENCES material_categories(category_id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_materials_catalog
|
||||
ON advanced_materials(status, priority, accepted_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_materials_category
|
||||
ON advanced_materials(category_id, status, priority, accepted_at);
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
CREATE TABLE IF NOT EXISTS advanced_wardrobe_items (
|
||||
wardrobe_item_id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL,
|
||||
material_id TEXT NOT NULL,
|
||||
wardrobe_category TEXT NOT NULL,
|
||||
tags_json TEXT,
|
||||
season TEXT,
|
||||
style TEXT,
|
||||
is_favorite INTEGER NOT NULL DEFAULT 0,
|
||||
usage_count INTEGER NOT NULL DEFAULT 0,
|
||||
last_used_at TIMESTAMP,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(profile_id) REFERENCES character_profiles(profile_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(material_id) REFERENCES advanced_materials(material_id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_wardrobe_profile
|
||||
ON advanced_wardrobe_items(profile_id, wardrobe_category);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_wardrobe_profile_material
|
||||
ON advanced_wardrobe_items(profile_id, material_id, wardrobe_category);
|
||||
|
|
@ -0,0 +1,115 @@
|
|||
CREATE TABLE IF NOT EXISTS advanced_style_sources (
|
||||
source_id TEXT PRIMARY KEY,
|
||||
import_id TEXT NOT NULL,
|
||||
sort_order INTEGER NOT NULL,
|
||||
is_primary INTEGER NOT NULL DEFAULT 0,
|
||||
source_image_uri TEXT NOT NULL,
|
||||
source_thumbnail_uri TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'queued',
|
||||
recognition_model_route TEXT,
|
||||
provider_id TEXT,
|
||||
provider_model TEXT,
|
||||
task_id TEXT,
|
||||
recognition_prompt TEXT,
|
||||
recognition_negative_prompt TEXT,
|
||||
recognition_tags_json TEXT,
|
||||
recognition_result_json TEXT,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
completed_at TIMESTAMP,
|
||||
excluded_at TIMESTAMP,
|
||||
FOREIGN KEY(import_id) REFERENCES advanced_material_imports(import_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(task_id) REFERENCES generation_tasks(task_id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_style_sources_order
|
||||
ON advanced_style_sources(import_id, sort_order);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_style_sources_one_primary
|
||||
ON advanced_style_sources(import_id)
|
||||
WHERE is_primary = 1;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_style_sources_status
|
||||
ON advanced_style_sources(import_id, status, updated_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_style_sources_task
|
||||
ON advanced_style_sources(task_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS advanced_style_consensus_runs (
|
||||
consensus_id TEXT PRIMARY KEY,
|
||||
import_id TEXT NOT NULL,
|
||||
revision INTEGER NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'queued',
|
||||
task_id TEXT,
|
||||
model_route TEXT,
|
||||
provider_id TEXT,
|
||||
provider_model TEXT,
|
||||
source_ids_json TEXT,
|
||||
prompt TEXT,
|
||||
negative_prompt TEXT,
|
||||
tags_json TEXT,
|
||||
result_json TEXT,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
completed_at TIMESTAMP,
|
||||
FOREIGN KEY(import_id) REFERENCES advanced_material_imports(import_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(task_id) REFERENCES generation_tasks(task_id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_style_consensus_revision
|
||||
ON advanced_style_consensus_runs(import_id, revision);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_style_consensus_one_active
|
||||
ON advanced_style_consensus_runs(import_id)
|
||||
WHERE status IN ('queued', 'running');
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_style_consensus_task
|
||||
ON advanced_style_consensus_runs(task_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS advanced_style_validation_cases (
|
||||
case_id TEXT PRIMARY KEY,
|
||||
attempt_id TEXT NOT NULL,
|
||||
import_id TEXT NOT NULL,
|
||||
case_key TEXT NOT NULL,
|
||||
sort_order INTEGER NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'queued',
|
||||
task_id TEXT,
|
||||
content_prompt TEXT NOT NULL,
|
||||
final_prompt TEXT NOT NULL,
|
||||
negative_prompt TEXT,
|
||||
model_route TEXT,
|
||||
provider_id TEXT,
|
||||
provider_model TEXT,
|
||||
seed INTEGER,
|
||||
width INTEGER,
|
||||
height INTEGER,
|
||||
validation_image_uri TEXT,
|
||||
validation_thumbnail_uri TEXT,
|
||||
usage_json TEXT,
|
||||
style_review_json TEXT,
|
||||
fidelity_score REAL,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
completed_at TIMESTAMP,
|
||||
FOREIGN KEY(attempt_id) REFERENCES advanced_material_validation_attempts(attempt_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(import_id) REFERENCES advanced_material_imports(import_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(task_id) REFERENCES generation_tasks(task_id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_style_validation_case_key
|
||||
ON advanced_style_validation_cases(attempt_id, case_key);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_advanced_style_validation_case_order
|
||||
ON advanced_style_validation_cases(attempt_id, sort_order);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_style_validation_case_status
|
||||
ON advanced_style_validation_cases(import_id, attempt_id, status, updated_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_advanced_style_validation_case_task
|
||||
ON advanced_style_validation_cases(task_id);
|
||||
|
|
@ -232,6 +232,161 @@ TABLES: tuple[TableSpec, ...] = (
|
|||
ColumnSpec("recognition_status", nullable=False),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"advanced_material_imports",
|
||||
"import_id",
|
||||
(
|
||||
ColumnSpec("import_id", nullable=False),
|
||||
ColumnSpec("category_id"),
|
||||
ColumnSpec("display_name", nullable=False),
|
||||
ColumnSpec("source_image_uri", nullable=False),
|
||||
ColumnSpec("source_thumbnail_uri"),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("recognition_prompt"),
|
||||
ColumnSpec("recognition_negative_prompt"),
|
||||
ColumnSpec("recognition_tags_json"),
|
||||
ColumnSpec("recognition_result_json"),
|
||||
ColumnSpec("recognition_model_route"),
|
||||
ColumnSpec("prompt_revision", nullable=False),
|
||||
ColumnSpec("current_attempt_id"),
|
||||
ColumnSpec("accepted_material_id"),
|
||||
ColumnSpec("idempotency_key"),
|
||||
ColumnSpec("created_by"),
|
||||
ColumnSpec("error_json"),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"advanced_material_validation_attempts",
|
||||
"attempt_id",
|
||||
(
|
||||
ColumnSpec("attempt_id", nullable=False),
|
||||
ColumnSpec("import_id", nullable=False),
|
||||
ColumnSpec("attempt_number", nullable=False),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("adjustment_text"),
|
||||
ColumnSpec("prompt", nullable=False),
|
||||
ColumnSpec("negative_prompt"),
|
||||
ColumnSpec("model_route"),
|
||||
ColumnSpec("provider_id"),
|
||||
ColumnSpec("provider_model"),
|
||||
ColumnSpec("task_id"),
|
||||
ColumnSpec("validation_image_uri"),
|
||||
ColumnSpec("validation_thumbnail_uri"),
|
||||
ColumnSpec("seed"),
|
||||
ColumnSpec("width"),
|
||||
ColumnSpec("height"),
|
||||
ColumnSpec("usage_json"),
|
||||
ColumnSpec("fidelity_score"),
|
||||
ColumnSpec("decision"),
|
||||
ColumnSpec("decision_notes"),
|
||||
ColumnSpec("error_json"),
|
||||
ColumnSpec("metadata_json"),
|
||||
ColumnSpec("completed_at"),
|
||||
ColumnSpec("decided_at"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"advanced_materials",
|
||||
"material_id",
|
||||
(
|
||||
ColumnSpec("material_id", nullable=False),
|
||||
ColumnSpec("import_id", nullable=False),
|
||||
ColumnSpec("accepted_attempt_id", nullable=False),
|
||||
ColumnSpec("category_id"),
|
||||
ColumnSpec("display_name", nullable=False),
|
||||
ColumnSpec("source_image_uri", nullable=False),
|
||||
ColumnSpec("source_thumbnail_uri"),
|
||||
ColumnSpec("validation_image_uri", nullable=False),
|
||||
ColumnSpec("validation_thumbnail_uri"),
|
||||
ColumnSpec("prompt", nullable=False),
|
||||
ColumnSpec("negative_prompt"),
|
||||
ColumnSpec("tags_json"),
|
||||
ColumnSpec("priority", nullable=False),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("capsule_json"),
|
||||
ColumnSpec("metadata_json"),
|
||||
ColumnSpec("accepted_at", nullable=False),
|
||||
ColumnSpec("archived_at"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"advanced_style_sources",
|
||||
"source_id",
|
||||
(
|
||||
ColumnSpec("source_id", nullable=False),
|
||||
ColumnSpec("import_id", nullable=False),
|
||||
ColumnSpec("sort_order", nullable=False),
|
||||
ColumnSpec("is_primary", nullable=False),
|
||||
ColumnSpec("source_image_uri", nullable=False),
|
||||
ColumnSpec("source_thumbnail_uri"),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("recognition_model_route"),
|
||||
ColumnSpec("provider_id"),
|
||||
ColumnSpec("provider_model"),
|
||||
ColumnSpec("task_id"),
|
||||
ColumnSpec("recognition_prompt"),
|
||||
ColumnSpec("recognition_negative_prompt"),
|
||||
ColumnSpec("recognition_tags_json"),
|
||||
ColumnSpec("recognition_result_json"),
|
||||
ColumnSpec("error_json"),
|
||||
ColumnSpec("metadata_json"),
|
||||
ColumnSpec("completed_at"),
|
||||
ColumnSpec("excluded_at"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"advanced_style_consensus_runs",
|
||||
"consensus_id",
|
||||
(
|
||||
ColumnSpec("consensus_id", nullable=False),
|
||||
ColumnSpec("import_id", nullable=False),
|
||||
ColumnSpec("revision", nullable=False),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("task_id"),
|
||||
ColumnSpec("model_route"),
|
||||
ColumnSpec("provider_id"),
|
||||
ColumnSpec("provider_model"),
|
||||
ColumnSpec("source_ids_json"),
|
||||
ColumnSpec("prompt"),
|
||||
ColumnSpec("negative_prompt"),
|
||||
ColumnSpec("tags_json"),
|
||||
ColumnSpec("result_json"),
|
||||
ColumnSpec("error_json"),
|
||||
ColumnSpec("metadata_json"),
|
||||
ColumnSpec("completed_at"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"advanced_style_validation_cases",
|
||||
"case_id",
|
||||
(
|
||||
ColumnSpec("case_id", nullable=False),
|
||||
ColumnSpec("attempt_id", nullable=False),
|
||||
ColumnSpec("import_id", nullable=False),
|
||||
ColumnSpec("case_key", nullable=False),
|
||||
ColumnSpec("sort_order", nullable=False),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("task_id"),
|
||||
ColumnSpec("content_prompt", nullable=False),
|
||||
ColumnSpec("final_prompt", nullable=False),
|
||||
ColumnSpec("negative_prompt"),
|
||||
ColumnSpec("model_route"),
|
||||
ColumnSpec("provider_id"),
|
||||
ColumnSpec("provider_model"),
|
||||
ColumnSpec("seed"),
|
||||
ColumnSpec("width"),
|
||||
ColumnSpec("height"),
|
||||
ColumnSpec("validation_image_uri"),
|
||||
ColumnSpec("validation_thumbnail_uri"),
|
||||
ColumnSpec("usage_json"),
|
||||
ColumnSpec("style_review_json"),
|
||||
ColumnSpec("fidelity_score"),
|
||||
ColumnSpec("error_json"),
|
||||
ColumnSpec("metadata_json"),
|
||||
ColumnSpec("completed_at"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"audit_logs",
|
||||
"audit_id",
|
||||
|
|
@ -269,6 +424,22 @@ TABLES: tuple[TableSpec, ...] = (
|
|||
ColumnSpec("last_used_at"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"advanced_wardrobe_items",
|
||||
"wardrobe_item_id",
|
||||
(
|
||||
ColumnSpec("wardrobe_item_id", nullable=False),
|
||||
ColumnSpec("profile_id", nullable=False),
|
||||
ColumnSpec("material_id", nullable=False),
|
||||
ColumnSpec("wardrobe_category", nullable=False),
|
||||
ColumnSpec("tags_json"),
|
||||
ColumnSpec("season"),
|
||||
ColumnSpec("style"),
|
||||
ColumnSpec("is_favorite", nullable=False),
|
||||
ColumnSpec("usage_count", nullable=False),
|
||||
ColumnSpec("last_used_at"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"integration_logs",
|
||||
"integration_log_id",
|
||||
|
|
|
|||
文件差异内容过多而无法显示
加载差异
|
|
@ -160,14 +160,16 @@ class JobRepository:
|
|||
self,
|
||||
*,
|
||||
statuses: set[str] | tuple[str, ...] | list[str],
|
||||
updated_before: str,
|
||||
limit: int = 100,
|
||||
updated_before: str | None,
|
||||
limit: int | None = 100,
|
||||
) -> list[GenerationTask]:
|
||||
"""Return oldest tasks that were left in an executable state.
|
||||
|
||||
Recovery decides which task sources are safe to requeue. Keeping the
|
||||
JSON-source filter in the service makes this query portable across the
|
||||
SQLite and PostgreSQL backends.
|
||||
SQLite and PostgreSQL backends. Startup recovery may omit both the
|
||||
age boundary and limit because it runs before the new worker pool is
|
||||
started and must inspect every task owned by the previous process.
|
||||
"""
|
||||
|
||||
normalized_statuses = tuple(
|
||||
|
|
@ -177,6 +179,13 @@ class JobRepository:
|
|||
return []
|
||||
placeholder = self.placeholder
|
||||
status_placeholders = ", ".join(placeholder for _ in normalized_statuses)
|
||||
age_clause = f"AND updated_at <= {placeholder}" if updated_before else ""
|
||||
limit_clause = f"LIMIT {placeholder}" if limit is not None else ""
|
||||
params: list[Any] = list(normalized_statuses)
|
||||
if updated_before:
|
||||
params.append(updated_before)
|
||||
if limit is not None:
|
||||
params.append(_limit(limit))
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
rows = conn.execute(
|
||||
|
|
@ -184,11 +193,11 @@ class JobRepository:
|
|||
SELECT {TASK_COLUMNS}
|
||||
FROM generation_tasks
|
||||
WHERE status IN ({status_placeholders})
|
||||
AND updated_at <= {placeholder}
|
||||
{age_clause}
|
||||
ORDER BY updated_at ASC, created_at ASC, task_id ASC
|
||||
LIMIT {placeholder}
|
||||
{limit_clause}
|
||||
""",
|
||||
(*normalized_statuses, updated_before, _limit(limit)),
|
||||
tuple(params),
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
|
|
|
|||
|
|
@ -25,9 +25,22 @@ WARDROBE_COLUMNS = """
|
|||
"""
|
||||
|
||||
|
||||
WARDROBE_TABLES = frozenset({"wardrobe_items", "advanced_wardrobe_items"})
|
||||
|
||||
|
||||
class WardrobeRepository:
|
||||
def __init__(self, database: Database):
|
||||
def __init__(
|
||||
self,
|
||||
database: Database,
|
||||
*,
|
||||
table_name: str = "wardrobe_items",
|
||||
library_kind: str = "standard",
|
||||
):
|
||||
if table_name not in WARDROBE_TABLES:
|
||||
raise ValueError(f"unsupported wardrobe table: {table_name}")
|
||||
self.database = database
|
||||
self.table_name = table_name
|
||||
self.library_kind = library_kind
|
||||
|
||||
@property
|
||||
def placeholder(self) -> str:
|
||||
|
|
@ -39,7 +52,7 @@ class WardrobeRepository:
|
|||
try:
|
||||
conn.execute(
|
||||
f"""
|
||||
INSERT INTO wardrobe_items (
|
||||
INSERT INTO {self.table_name} (
|
||||
wardrobe_item_id,
|
||||
profile_id,
|
||||
material_id,
|
||||
|
|
@ -77,12 +90,12 @@ class WardrobeRepository:
|
|||
conn = self.database.connect()
|
||||
try:
|
||||
row = conn.execute(
|
||||
f"SELECT {WARDROBE_COLUMNS} FROM wardrobe_items WHERE wardrobe_item_id = {placeholder}",
|
||||
f"SELECT {WARDROBE_COLUMNS} FROM {self.table_name} WHERE wardrobe_item_id = {placeholder}",
|
||||
(wardrobe_item_id,),
|
||||
).fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
return _row_to_item(row) if row else None
|
||||
return _row_to_item(row, library_kind=self.library_kind) if row else None
|
||||
|
||||
def list_by_profile(self, profile_id: str, wardrobe_category: str | None = None) -> list[WardrobeItem]:
|
||||
placeholder = self.placeholder
|
||||
|
|
@ -96,7 +109,7 @@ class WardrobeRepository:
|
|||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT {WARDROBE_COLUMNS}
|
||||
FROM wardrobe_items
|
||||
FROM {self.table_name}
|
||||
WHERE {" AND ".join(where)}
|
||||
ORDER BY is_favorite DESC, usage_count ASC, created_at ASC
|
||||
""",
|
||||
|
|
@ -104,7 +117,7 @@ class WardrobeRepository:
|
|||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
return [_row_to_item(row) for row in rows]
|
||||
return [_row_to_item(row, library_kind=self.library_kind) for row in rows]
|
||||
|
||||
def delete(self, wardrobe_item_id: str) -> WardrobeItem | None:
|
||||
before = self.get(wardrobe_item_id)
|
||||
|
|
@ -113,7 +126,7 @@ class WardrobeRepository:
|
|||
placeholder = self.placeholder
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
conn.execute(f"DELETE FROM wardrobe_items WHERE wardrobe_item_id = {placeholder}", (wardrobe_item_id,))
|
||||
conn.execute(f"DELETE FROM {self.table_name} WHERE wardrobe_item_id = {placeholder}", (wardrobe_item_id,))
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
|
@ -125,7 +138,7 @@ class WardrobeRepository:
|
|||
try:
|
||||
conn.execute(
|
||||
f"""
|
||||
UPDATE wardrobe_items
|
||||
UPDATE {self.table_name}
|
||||
SET usage_count = usage_count + 1,
|
||||
last_used_at = CURRENT_TIMESTAMP,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
|
|
@ -139,7 +152,18 @@ class WardrobeRepository:
|
|||
return self.get(wardrobe_item_id)
|
||||
|
||||
|
||||
def _row_to_item(row) -> WardrobeItem:
|
||||
class AdvancedWardrobeRepository(WardrobeRepository):
|
||||
"""Wardrobe storage whose material FK targets verified advanced assets."""
|
||||
|
||||
def __init__(self, database: Database):
|
||||
super().__init__(
|
||||
database,
|
||||
table_name="advanced_wardrobe_items",
|
||||
library_kind="advanced",
|
||||
)
|
||||
|
||||
|
||||
def _row_to_item(row, *, library_kind: str = "standard") -> WardrobeItem:
|
||||
return WardrobeItem(
|
||||
wardrobe_item_id=row["wardrobe_item_id"],
|
||||
profile_id=row["profile_id"],
|
||||
|
|
@ -153,6 +177,7 @@ def _row_to_item(row) -> WardrobeItem:
|
|||
last_used_at=str(row["last_used_at"]) if row["last_used_at"] else None,
|
||||
created_at=str(row["created_at"]),
|
||||
updated_at=str(row["updated_at"]),
|
||||
library_kind=library_kind,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,70 @@
|
|||
# 进阶素材导入与还原验证工作流
|
||||
|
||||
## 目标
|
||||
|
||||
进阶素材用于保存经过“文字提示词可还原性”验证的高可信素材。普通素材流程保持不变;进阶素材拥有独立数据库表、独立文件目录和独立后台任务,不会把验证图写入普通图库或角色工作流候选池。
|
||||
|
||||
## 状态机
|
||||
|
||||
```text
|
||||
上传源图
|
||||
-> recognition_queued
|
||||
-> recognizing
|
||||
-> ready_for_validation
|
||||
-> validation_queued
|
||||
-> validating
|
||||
-> awaiting_review
|
||||
|-- 接受 -> accepted -> advanced_materials(active)
|
||||
`-- 不接受 -> adjustment_required
|
||||
-> validation_queued(下一轮)
|
||||
```
|
||||
|
||||
失败状态为 `recognition_failed` 和 `validation_failed`,均保留源图、识别结果和已完成轮次,并允许人工重试。验证生图任务不会在插件重启后自动重放,避免重复付费;识别任务可以安全恢复。
|
||||
|
||||
运行中取消使用导入与轮次双重 compare-and-set。提供方迟到返回时只保留脱敏后的计费归因,生成文件会被删除,导入和轮次不会被重新改回 `awaiting_review`。插件启动会处理旧进程留下的全部 `running/retrying` 进阶任务,不等待五分钟陈旧窗口。
|
||||
|
||||
## 隔离边界
|
||||
|
||||
- 普通素材表:`materials`
|
||||
- 进阶导入会话:`advanced_material_imports`
|
||||
- 进阶验证轮次:`advanced_material_validation_attempts`
|
||||
- 已封存进阶素材:`advanced_materials`
|
||||
- 普通素材目录:`storage://materials/...`
|
||||
- 进阶素材目录:`storage://advanced_materials/...`
|
||||
- 验证图不会创建 `generation_images` 记录,也不会进入普通图库。
|
||||
- 验证生图请求只携带识别提示词,不携带源图作为 `input_images`。
|
||||
|
||||
## 人工审核规则
|
||||
|
||||
- 只有当前 `awaiting_review` 且验证图存在的轮次可以封存。
|
||||
- 模型或主观评分只作为记录,不能自动封存。
|
||||
- “不还原”必须填写调整描述;描述会形成新的提示词修订和验证轮次,旧轮次永久保留。
|
||||
- 封存时保存原始素材作为角色引用图,验证图仅作为还原证据。
|
||||
|
||||
## 高优先级规则
|
||||
|
||||
统一素材目录通过服务端解析 `advmat_` 记录。只有 `advanced_materials.status=active` 的记录会获得可信的 `advanced_verified` 标记和优先级加成;普通素材即使伪造同名 metadata 也不会获得加成。用户明确锁定或设为必需的素材仍高于自动进阶加成。
|
||||
|
||||
## 内容安全、路由与预算
|
||||
|
||||
- 规则审核只检查正向需求;负向提示词中的 `nudity / gore / child` 等排除词不会被当成用户想生成的内容。
|
||||
- 恐怖主义、极端主义、现实高危暴力和极端血腥在调用生图提供方前直接禁止并记录高优先级审计。
|
||||
- 合法成人内容只有在入口开关、用户/群组白名单和 NSFW 路由同时满足时才会分流。
|
||||
- 普通模型只有返回明确的成人/NSFW 内容安全拒绝时才允许向 NSFW 路由回退一次;通用安全错误、未成年人、恐暴或未知原因不会盲目回退。
|
||||
- safe/sensitive 内容始终使用 `advanced_material_validation_generation` 专用模型,不被全局普通生图路由覆盖。
|
||||
- `advanced_materials.cost_budget` 可配置统计窗口、任务/图片/费用上限和请求/图片/Token 单价。自动回退按最坏两次提供方请求预检。
|
||||
|
||||
## 主要接口
|
||||
|
||||
- `POST /advanced-material-imports/upload`
|
||||
- `GET /advanced-material-imports`
|
||||
- `GET /advanced-material-imports/{import_id}`
|
||||
- `POST /advanced-material-imports/{import_id}/retry`
|
||||
- `POST /advanced-material-imports/{import_id}/validation-attempts`
|
||||
- `GET /advanced-material-imports/{import_id}/validation-attempts`
|
||||
- `POST /advanced-material-imports/{import_id}/attempts/{attempt_id}/reject`
|
||||
- `POST /advanced-material-imports/{import_id}/attempts/{attempt_id}/accept`
|
||||
- `GET /advanced-materials`
|
||||
- `GET /material-catalog`
|
||||
|
||||
上传、重试和创建验证轮次均立即返回任务信息;耗时识别和生图由持久 Worker 执行。
|
||||
|
|
@ -0,0 +1,162 @@
|
|||
# 进阶画风样本组:多图交叉提取与纯文本迁移验证
|
||||
|
||||
## 目标与边界
|
||||
|
||||
本功能只对素材根分类“风格”开放。服装、鞋袜、配饰、构图等普通与进阶素材继续使用单图导入,不改变现有接口语义。
|
||||
|
||||
一个进阶画风样本组接收 2~8 张同画风图片(推荐 3~6 张),将每张图片拆成完全独立、同时执行的视觉识别任务;全部可用结果完成后,再由交叉分析任务同时读取原图与逐图报告,分离共同画风、可变特征和内容污染。验证阶段不向生图模型传递任何源图,只把统一画风胶囊应用到与源样本完全不同的角色、场景和构图,以验证画风的迁移能力,而不是源图复刻能力。
|
||||
|
||||
最终仍封存为一个 `advmat_...` 进阶素材目录项。现有素材目录、衣橱、详细角色工作流继续通过统一 Material 合约使用它;旧的单图进阶素材不需要迁移。
|
||||
|
||||
## 用户流程
|
||||
|
||||
```text
|
||||
选择“风格”
|
||||
-> 上传 2~8 张样本
|
||||
-> 每张样本创建独立识别任务并同时派发
|
||||
-> 成功样本达到至少 2 张
|
||||
-> 交叉模型同时读取全部有效原图与逐图报告
|
||||
-> 生成共同画风、可变项、内容污染与异常样本报告
|
||||
-> 自动规划三个与源样本语义显著不同的验证场景
|
||||
-> 三个纯文本生图任务同时派发(无 input_images / image)
|
||||
-> 人工对照三项迁移结果
|
||||
|-- 接受 -> 封存一个高优先级进阶画风
|
||||
`-- 调整 -> 重建统一胶囊 -> 使用同一组场景重新并行验证
|
||||
```
|
||||
|
||||
## 并行任务与屏障
|
||||
|
||||
### 单图识别
|
||||
|
||||
- 每张源图拥有独立 `source_id`、`task_id`、状态、超时、重试次数、错误和识别结果。
|
||||
- 同一组内的 2~8 个识别任务一次性入队;Worker 不串行等待,调度并发上限默认等于组内图片数,硬上限为 8。
|
||||
- 任何一张失败都不得取消或超时其他任务,也不得共享计时器。
|
||||
- 失败样本可以单独重试或排除;成功样本不少于 2 张时允许用户继续。
|
||||
- 交叉分析通过数据库屏障触发:所有未排除任务进入终态,且成功数不少于 2;重复回调必须幂等,最多创建一个交叉分析任务。
|
||||
|
||||
### 验证生成
|
||||
|
||||
- 默认生成 `portrait`、`fullbody`、`novel_scene` 三个验证案例。
|
||||
- 三个案例分别创建任务并同时派发,每个案例拥有独立超时、重试、取消和错误。
|
||||
- 验证批次只在全部未排除案例进入终态后进入人工审核;单例失败可以单独重试。
|
||||
- 所有耗时接口立即返回“已入队”,前端通过导入详情轮询状态。
|
||||
|
||||
## 画风交叉分析
|
||||
|
||||
交叉模型输入:
|
||||
|
||||
- 全部有效原图;
|
||||
- 每张图片的结构化识别报告;
|
||||
- 每张图片的内容摘要;
|
||||
- 用户补充的画风要求;
|
||||
- 明确的“只抽取可观察画风、不继承内容”约束。
|
||||
|
||||
交叉结果至少包含:
|
||||
|
||||
- `consensus_style`:多图稳定共同特征;
|
||||
- `variable_style_features`:部分样本存在的弱特征;
|
||||
- `source_content_features`:人物、服装、姿势、道具、场景与构图内容;
|
||||
- `content_leakage_exclusions`:正式提示词必须排除的内容污染;
|
||||
- `outlier_source_ids`:疑似异画风样本;
|
||||
- `representative_source_ids`:按风格强度、内容差异和污染风险选择的 1~3 张代表图;
|
||||
- `consensus_confidence` 与逐特征证据;
|
||||
- `compact / standard / detailed` 三档统一画风胶囊。
|
||||
|
||||
共同特征的默认分级:
|
||||
|
||||
- 支持率不低于 60% 且置信度达标:核心画风;
|
||||
- 支持率 40%~60%:可变或弱画风;
|
||||
- 支持率低于 40%:不进入核心胶囊;
|
||||
- 人物身份、服装、姿势、道具、文字、背景主体:进入内容污染或禁止继承项;
|
||||
- 近重复图片降低证据权重,避免重复样本放大偶然特征。
|
||||
|
||||
## 纯文本异角色异场景验证
|
||||
|
||||
验证规划器先根据 `source_content_features` 生成三个语义距离显著的内容案例。案例必须改变尽可能多的内容维度:年龄、性别或物种、服装、姿势、镜头距离、室内外、背景、道具和构图。
|
||||
|
||||
每个案例分离保存:
|
||||
|
||||
- `content_prompt`:只描述新角色与新场景;
|
||||
- `style_prompt`:统一画风胶囊;
|
||||
- `final_prompt`:由后端可追踪地合并两者;
|
||||
- `negative_prompt`:画风禁止项与内容污染排除项;
|
||||
- 路由、模型、seed、输出图、用量与错误。
|
||||
|
||||
生图请求必须满足:
|
||||
|
||||
```json
|
||||
{
|
||||
"input_images": [],
|
||||
"image": []
|
||||
}
|
||||
```
|
||||
|
||||
验证输出可以由 Vision 模型依据统一画风指纹进行辅助审核,但不得以人物是否像源图作为指标。审核维度包括线条、着色、阴影、色彩规律、边缘、高光、纹理、媒介感、细节密度和内容污染。自动评分只作参考,不能自动封存。
|
||||
|
||||
## 人工调整与版本一致性
|
||||
|
||||
- 调整说明不得只追加到顶层 prompt。
|
||||
- 每次调整必须生成新的共识版本,并同步重建三档胶囊、负向词与内容污染排除项。
|
||||
- 使用同一组三个验证案例重新生成,便于前后对比;旧版本和旧验证结果永久保留。
|
||||
- 封存时必须引用当前已审核的共识版本,杜绝“验证用新提示词、正式生图仍用旧胶囊”。
|
||||
|
||||
## 数据模型
|
||||
|
||||
在现有 `advanced_material_imports / advanced_material_validation_attempts / advanced_materials` 之外增加风格专用子表:
|
||||
|
||||
- `advanced_style_sources`:源图、缩略图、排序、主参考、排除状态、独立任务与识别结果;
|
||||
- `advanced_style_consensus_runs`:交叉分析输入摘要、共同画风、可变项、内容污染、代表图、统一胶囊和版本号;
|
||||
- `advanced_style_validation_cases`:一个共识版本下的三个异场景案例、独立任务、输出图与审核结果。
|
||||
|
||||
导入主记录通过 metadata 保存 `workflow_kind=style_set`、当前共识版本和批次状态。`advanced_materials.source_image_uri` 继续保存主参考图以兼容旧消费者,完整源图与最多三张代表图通过关联数据和可信 metadata 暴露。
|
||||
|
||||
## 接口与兼容策略
|
||||
|
||||
保留所有现有单图接口,增加风格组接口:
|
||||
|
||||
- `POST /advanced-material-imports/style-set/upload`
|
||||
- `POST /advanced-material-imports/{import_id}/style-sources/{source_id}/retry`
|
||||
- `POST /advanced-material-imports/{import_id}/style-sources/{source_id}/exclude`
|
||||
- `POST /advanced-material-imports/{import_id}/style-consensus`
|
||||
- `POST /advanced-material-imports/{import_id}/style-validation-cases/{case_id}/retry`
|
||||
- `POST /advanced-material-imports/{import_id}/validation-attempts`(兼容入口;确保三场景批次存在)
|
||||
- `POST /advanced-material-imports/{import_id}/attempts/{attempt_id}/reject`(携带调整说明并创建新共识版本)
|
||||
- `POST /advanced-material-imports/{import_id}/attempts/{attempt_id}/accept`(封存当前已审核版本)
|
||||
|
||||
服务端按 `material_categories` 的规范名称确认“风格”,不信任前端标签。非风格分类上传多图必须返回明确校验错误。旧单图进阶素材仍走原接口和原状态机。
|
||||
|
||||
## 下游应用
|
||||
|
||||
- 快速角色:使用统一画风胶囊,不自动发送源图,维持当前轻量路径。
|
||||
- 详细角色工作流:使用 detailed 胶囊;只有提供方明确支持参考图时才选择主参考及最多两张代表图。
|
||||
- 适配器不支持原生参考角色/权重时,优先只发送主参考图,避免人物、服装和构图污染。
|
||||
- 验证图永不进入普通图库、角色候选池或正式参考图集合。
|
||||
|
||||
## 开发队列与验收状态
|
||||
|
||||
- [x] P0:新增风格样本、共识版本和验证案例迁移、DTO 与仓储。
|
||||
- [x] P0:后端只允许“风格”使用 2~8 张,多图上传原子落盘,失败时清理新文件。
|
||||
- [x] P0:逐图任务独立入队、独立超时/重试/错误,批次同时派发并提供幂等屏障。
|
||||
- [x] P0:交叉分析同时读取全部有效原图和逐图报告,生成带证据的统一画风结构与三档胶囊。
|
||||
- [x] P0:验证规划生成三个与源内容显著不同的案例;生图请求不得携带任何源图。
|
||||
- [x] P0:三个验证案例独立并行、可单独重试,失败不影响其他案例。
|
||||
- [x] P1:人工调整创建新共识版本并同步重建胶囊;封存只能引用当前已审核版本。
|
||||
- [x] P1:统一目录继续输出一个 `advmat_`,可信 metadata 携带代表源图但不暴露伪造优先级。
|
||||
- [x] P1:前端仅在选择“风格”时启用多图上传、排序、主参考、排除和逐图状态。
|
||||
- [x] P1:前端展示当前共识版本、共同画风、可变项、内容污染、异常样本和三案例验证。
|
||||
- [x] P2:备份恢复、孤儿媒体、成本预算、统计与日志覆盖新增表和文件。
|
||||
- [x] P2:迁移、仓储、服务、Worker 恢复、权限、路由、契约、类型检查和深夜模式测试。
|
||||
- [x] P2:完整后端测试、前端契约、类型检查、生产构建和 `git diff --check` 全部通过。
|
||||
|
||||
## 验收标准
|
||||
|
||||
1. 非风格分类无法创建多图组;旧单图进阶流程无回归。
|
||||
2. 风格组支持 2~8 张,所有单图识别任务同时进入队列且无共享超时。
|
||||
3. 单图失败可单独重试、排除;至少两张成功后可以交叉分析。
|
||||
4. 交叉结果能追溯每个核心特征来自哪些图片,并分离源内容污染。
|
||||
5. 三个验证案例的角色、场景和构图与源样本显著不同,且 provider 请求没有源图。
|
||||
6. 三个案例独立并行,任一失败不会覆盖其他任务状态。
|
||||
7. 人工调整后统一胶囊版本发生变化,封存素材使用新胶囊。
|
||||
8. 最终只产生一个进阶画风素材,验证图保持隔离。
|
||||
9. 旧数据库可平滑迁移,插件重启可恢复识别任务但不会自动重复付费验证。
|
||||
10. 全量自动化测试与静态构建通过,测试过程不调用真实付费模型。
|
||||
文件差异内容过多而无法显示
加载差异
|
|
@ -270,6 +270,34 @@ def _output_uris(
|
|||
normalized_extension = extension if str(extension).startswith(".") else f".{extension}"
|
||||
scope = request.get("output_scope") if isinstance(request.get("output_scope"), dict) else None
|
||||
task_stem = _safe_file_stem(task.task_id)
|
||||
if scope and scope.get("kind") == "advanced_material_validation":
|
||||
import_id = _safe_file_stem(str(scope.get("import_id") or "import"))
|
||||
attempt_id = _safe_file_stem(str(scope.get("attempt_id") or "attempt"))
|
||||
attempt_number = _safe_file_stem(str(scope.get("attempt_number") or "1"))
|
||||
filename = (
|
||||
f"validation_{attempt_number}__{attempt_id}__{task_stem}_{index + 1:02d}"
|
||||
f"{normalized_extension}"
|
||||
)
|
||||
relative = f"attempts/{import_id}/{attempt_id}/{filename}"
|
||||
thumbnail_relative = f"attempts/{import_id}/{attempt_id}/thumbnail__{filename}"
|
||||
return (
|
||||
f"storage://advanced_materials/{relative}",
|
||||
f"storage://advanced_materials/{thumbnail_relative}",
|
||||
)
|
||||
|
||||
if scope and scope.get("kind") == "advanced_style_validation":
|
||||
import_id = _safe_file_stem(str(scope.get("import_id") or "import"))
|
||||
attempt_id = _safe_file_stem(str(scope.get("attempt_id") or "attempt"))
|
||||
case_id = _safe_file_stem(str(scope.get("case_id") or "case"))
|
||||
case_key = _safe_file_stem(str(scope.get("case_key") or "validation"))
|
||||
filename = f"{case_key}__{case_id}__{task_stem}_{index + 1:02d}{normalized_extension}"
|
||||
relative = f"style-validations/{import_id}/{attempt_id}/{case_id}/{filename}"
|
||||
thumbnail_relative = f"style-validations/{import_id}/{attempt_id}/{case_id}/thumbnail__{filename}"
|
||||
return (
|
||||
f"storage://advanced_materials/{relative}",
|
||||
f"storage://advanced_materials/{thumbnail_relative}",
|
||||
)
|
||||
|
||||
if not scope or not scope.get("round_id") or not scope.get("candidate_id"):
|
||||
filename = f"{task_stem}_{index + 1}{normalized_extension}"
|
||||
return (
|
||||
|
|
@ -295,6 +323,26 @@ def _output_uris(
|
|||
|
||||
def _output_scope_metadata(request: dict[str, Any]) -> dict[str, Any]:
|
||||
scope = request.get("output_scope") if isinstance(request.get("output_scope"), dict) else None
|
||||
if scope and scope.get("kind") == "advanced_material_validation":
|
||||
allowed = ("kind", "import_id", "attempt_id", "attempt_number")
|
||||
return {
|
||||
"scoped_output": True,
|
||||
"output_scope": {
|
||||
key: str(scope[key])
|
||||
for key in allowed
|
||||
if scope.get(key) is not None and str(scope.get(key)).strip()
|
||||
},
|
||||
}
|
||||
if scope and scope.get("kind") == "advanced_style_validation":
|
||||
allowed = ("kind", "import_id", "attempt_id", "case_id", "case_key")
|
||||
return {
|
||||
"scoped_output": True,
|
||||
"output_scope": {
|
||||
key: str(scope[key])
|
||||
for key in allowed
|
||||
if scope.get(key) is not None and str(scope.get(key)).strip()
|
||||
},
|
||||
}
|
||||
if not scope or not scope.get("round_id") or not scope.get("candidate_id"):
|
||||
return {}
|
||||
allowed = (
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ JsonDict = dict[str, Any]
|
|||
HttpTransport = Callable[[str, JsonDict, dict[str, str], int], JsonDict]
|
||||
MAX_PROVIDER_JSON_BYTES = 4 * 1024 * 1024
|
||||
MAX_PROVIDER_ERROR_BYTES = 64 * 1024
|
||||
MAX_VISION_IMAGES = 8
|
||||
|
||||
|
||||
class VisionProvider(Protocol):
|
||||
|
|
@ -52,6 +53,7 @@ class VisionRecognitionRequest:
|
|||
tags: tuple[str, ...] = ()
|
||||
model: str | None = None
|
||||
metadata: dict[str, Any] = field(default_factory=dict)
|
||||
image_uris: tuple[str, ...] = ()
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -98,7 +100,7 @@ class OpenAIVisionProvider:
|
|||
provider_id=self.provider_id,
|
||||
error_type="configuration_error",
|
||||
)
|
||||
if not (_remote_image_url(request.image_uri) or _remote_image_url(request.thumbnail_uri)):
|
||||
if not vision_image_urls(request):
|
||||
raise VisionProviderError(
|
||||
"vision image must be supplied as an HTTP(S) URL or data:image URL",
|
||||
provider_id=self.provider_id,
|
||||
|
|
@ -136,6 +138,52 @@ class OpenAIVisionProvider:
|
|||
return _parse_vision_response(self.provider_id, model, response)
|
||||
|
||||
|
||||
class FallbackVisionProvider:
|
||||
"""Try vision providers in priority order and preserve failure context."""
|
||||
|
||||
def __init__(self, providers: list[VisionProvider]):
|
||||
if not providers:
|
||||
raise ValueError("at least one vision provider is required")
|
||||
self.providers = providers
|
||||
self.provider_id = providers[0].provider_id
|
||||
|
||||
def recognize_material(self, request: VisionRecognitionRequest) -> VisionRecognitionResult:
|
||||
failures: list[dict[str, Any]] = []
|
||||
for provider in self.providers:
|
||||
try:
|
||||
result = provider.recognize_material(request)
|
||||
if failures:
|
||||
return VisionRecognitionResult(
|
||||
provider_id=result.provider_id,
|
||||
model=result.model,
|
||||
prompt=result.prompt,
|
||||
negative_prompt=result.negative_prompt,
|
||||
tags=result.tags,
|
||||
confidence=result.confidence,
|
||||
description=result.description,
|
||||
structured_data=result.structured_data,
|
||||
raw_metadata={**result.raw_metadata, "route_fallback_failures": failures},
|
||||
)
|
||||
return result
|
||||
except VisionProviderError as exc:
|
||||
failures.append(
|
||||
{
|
||||
"provider_id": exc.provider_id,
|
||||
"status_code": exc.status_code,
|
||||
"error_type": exc.error_type,
|
||||
"message": str(exc),
|
||||
}
|
||||
)
|
||||
last = failures[-1]
|
||||
raise VisionProviderError(
|
||||
"all configured vision routes failed",
|
||||
provider_id=str(last["provider_id"]),
|
||||
status_code=last["status_code"],
|
||||
error_type="fallback_exhausted",
|
||||
details={"failures": failures},
|
||||
)
|
||||
|
||||
|
||||
class MockVisionProvider:
|
||||
provider_id = "mock"
|
||||
|
||||
|
|
@ -180,12 +228,36 @@ def _user_content(request: VisionRecognitionRequest) -> list[dict[str, Any]]:
|
|||
),
|
||||
}
|
||||
]
|
||||
image_url = _remote_image_url(request.image_uri) or _remote_image_url(request.thumbnail_uri)
|
||||
if image_url:
|
||||
for image_url in vision_image_urls(request):
|
||||
content.append({"type": "image_url", "image_url": {"url": image_url}})
|
||||
return content
|
||||
|
||||
|
||||
def vision_image_urls(request: VisionRecognitionRequest) -> tuple[str, ...]:
|
||||
"""Return ordered, deduplicated multimodal inputs for one vision request.
|
||||
|
||||
``image_uri`` remains the primary-image compatibility field. New callers
|
||||
may add a bounded group through ``image_uris``. The thumbnail preserves
|
||||
the legacy fallback behavior and is used only when no source image is
|
||||
valid, so it never becomes an accidental extra style sample.
|
||||
"""
|
||||
|
||||
resolved: list[str] = []
|
||||
seen: set[str] = set()
|
||||
for value in (request.image_uri, *request.image_uris):
|
||||
image_url = _remote_image_url(value)
|
||||
if image_url is None or image_url in seen:
|
||||
continue
|
||||
seen.add(image_url)
|
||||
resolved.append(image_url)
|
||||
if len(resolved) >= MAX_VISION_IMAGES:
|
||||
return tuple(resolved)
|
||||
if resolved:
|
||||
return tuple(resolved)
|
||||
thumbnail_url = _remote_image_url(request.thumbnail_uri)
|
||||
return (thumbnail_url,) if thumbnail_url else ()
|
||||
|
||||
|
||||
def _system_instruction(request: VisionRecognitionRequest) -> str:
|
||||
configured = str(request.metadata.get("recognition_instruction") or "").strip()
|
||||
fallback = (
|
||||
|
|
|
|||
4
frontend/package-lock.json
自动生成
4
frontend/package-lock.json
自动生成
|
|
@ -1,12 +1,12 @@
|
|||
{
|
||||
"name": "astrbot-plugin-imagestudio-frontend",
|
||||
"version": "1.0.20",
|
||||
"version": "1.0.32",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "astrbot-plugin-imagestudio-frontend",
|
||||
"version": "1.0.20",
|
||||
"version": "1.0.32",
|
||||
"dependencies": {
|
||||
"@vitejs/plugin-vue": "^5.2.0",
|
||||
"pinia": "^2.3.0",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "astrbot-plugin-imagestudio-frontend",
|
||||
"version": "1.0.20",
|
||||
"version": "1.0.32",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
|
|
|
|||
|
|
@ -98,6 +98,29 @@ export const endpoints = {
|
|||
materialRecognize: (materialId: string) => `materials/${materialId}/recognize`,
|
||||
materialReRecognize: (materialId: string) => `materials/${materialId}/re-recognize`,
|
||||
materialConfirm: (materialId: string) => `materials/${materialId}/confirm`,
|
||||
advancedMaterialImports: "advanced-material-imports",
|
||||
advancedMaterialImportUpload: "advanced-material-imports/upload",
|
||||
advancedMaterialStyleSetUpload: "advanced-material-imports/style-set/upload",
|
||||
advancedMaterialImport: (importId: string) => `advanced-material-imports/${importId}`,
|
||||
advancedMaterialImportDelete: (importId: string) => `advanced-material-imports/${importId}`,
|
||||
advancedMaterialImportRetry: (importId: string) => `advanced-material-imports/${importId}/retry`,
|
||||
advancedMaterialStyleSourceRetry: (importId: string, sourceId: string) =>
|
||||
`advanced-material-imports/${importId}/style-sources/${sourceId}/retry`,
|
||||
advancedMaterialStyleSourceExclude: (importId: string, sourceId: string) =>
|
||||
`advanced-material-imports/${importId}/style-sources/${sourceId}/exclude`,
|
||||
advancedMaterialStyleConsensus: (importId: string) =>
|
||||
`advanced-material-imports/${importId}/style-consensus`,
|
||||
advancedMaterialStyleValidationCaseRetry: (importId: string, caseId: string) =>
|
||||
`advanced-material-imports/${importId}/style-validation-cases/${caseId}/retry`,
|
||||
advancedMaterialValidationAttempts: (importId: string) =>
|
||||
`advanced-material-imports/${importId}/validation-attempts`,
|
||||
advancedMaterialAttemptAccept: (importId: string, attemptId: string) =>
|
||||
`advanced-material-imports/${importId}/attempts/${attemptId}/accept`,
|
||||
advancedMaterialAttemptReject: (importId: string, attemptId: string) =>
|
||||
`advanced-material-imports/${importId}/attempts/${attemptId}/reject`,
|
||||
advancedMaterials: "advanced-materials",
|
||||
advancedMaterial: (materialId: string) => `advanced-materials/${materialId}`,
|
||||
materialCatalog: "material-catalog",
|
||||
assetImportPack: "assets/import-pack",
|
||||
wardrobe: (profileId: string) => `profiles/${profileId}/wardrobe`,
|
||||
wardrobeImportMaterial: (profileId: string) => `profiles/${profileId}/wardrobe/import-material`,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,342 @@
|
|||
export type AdvancedMaterialImportStatus =
|
||||
| "recognition_queued"
|
||||
| "pending_recognition"
|
||||
| "recognizing"
|
||||
| "pending_validation"
|
||||
| "ready_for_validation"
|
||||
| "validation_queued"
|
||||
| "validating"
|
||||
| "validation_failed"
|
||||
| "recognition_failed"
|
||||
| "pending_review"
|
||||
| "awaiting_review"
|
||||
| "adjustment_required"
|
||||
| "accepted"
|
||||
| "rejected"
|
||||
| "failed"
|
||||
| "cancelled"
|
||||
| "archived"
|
||||
|
||||
export type AdvancedMaterialValidationAttemptStatus =
|
||||
| "queued"
|
||||
| "running"
|
||||
| "succeeded"
|
||||
| "completed"
|
||||
| "awaiting_review"
|
||||
| "failed"
|
||||
| "accepted"
|
||||
| "rejected"
|
||||
| "cancelled"
|
||||
|
||||
export type AdvancedMaterialStatus = "active" | "archived"
|
||||
|
||||
export type AdvancedStyleSourceStatus =
|
||||
| "uploaded"
|
||||
| "recognition_queued"
|
||||
| "recognizing"
|
||||
| "recognized"
|
||||
| "completed"
|
||||
| "failed"
|
||||
| "excluded"
|
||||
| "cancelled"
|
||||
|
||||
export type AdvancedStyleConsensusStatus =
|
||||
| "queued"
|
||||
| "running"
|
||||
| "completed"
|
||||
| "failed"
|
||||
|
||||
export type AdvancedStyleValidationScenario = "portrait" | "fullbody" | "novel_scene" | string
|
||||
|
||||
export interface AdvancedMaterialError {
|
||||
code?: string
|
||||
message?: string
|
||||
details?: Record<string, unknown>
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export interface AdvancedStyleSource {
|
||||
source_id: string
|
||||
import_id?: string
|
||||
sort_order: number
|
||||
is_primary: boolean
|
||||
excluded: boolean
|
||||
status: AdvancedStyleSourceStatus | string
|
||||
filename?: string | null
|
||||
source_image_uri?: string | null
|
||||
source_thumbnail_uri?: string | null
|
||||
source_image_url?: string | null
|
||||
source_thumbnail_url?: string | null
|
||||
image_uri?: string | null
|
||||
thumbnail_uri?: string | null
|
||||
image_url?: string | null
|
||||
thumbnail_url?: string | null
|
||||
task_id?: string | null
|
||||
recognition_prompt?: string | null
|
||||
recognition_negative_prompt?: string | null
|
||||
recognition_tags: string[]
|
||||
recognition_result: Record<string, unknown>
|
||||
retry_count?: number
|
||||
error: AdvancedMaterialError
|
||||
metadata: Record<string, unknown>
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
completed_at?: string | null
|
||||
excluded_at?: string | null
|
||||
}
|
||||
|
||||
export interface AdvancedStyleConsensusFeature {
|
||||
trait?: string
|
||||
label?: string
|
||||
description?: string
|
||||
supporting_source_ids?: string[]
|
||||
supporting_images?: string[]
|
||||
support_ratio?: number
|
||||
confidence?: number
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export interface AdvancedStyleConsensus {
|
||||
consensus_id?: string | null
|
||||
version_id?: string | null
|
||||
revision?: number | null
|
||||
version_number?: number | null
|
||||
status: AdvancedStyleConsensusStatus | string
|
||||
consensus_style: AdvancedStyleConsensusFeature[] | Record<string, unknown>
|
||||
variable_style_features: AdvancedStyleConsensusFeature[]
|
||||
source_content_features: AdvancedStyleConsensusFeature[] | string[]
|
||||
content_leakage_exclusions: string[]
|
||||
outlier_source_ids: string[]
|
||||
representative_source_ids: string[]
|
||||
consensus_confidence?: number | null
|
||||
generation_capsules: Record<string, unknown>
|
||||
source_ids?: string[]
|
||||
tags?: string[]
|
||||
result?: Record<string, unknown>
|
||||
prompt?: string | null
|
||||
negative_prompt?: string | null
|
||||
task_id?: string | null
|
||||
error: AdvancedMaterialError
|
||||
metadata: Record<string, unknown>
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
}
|
||||
|
||||
export interface AdvancedStyleValidationCase {
|
||||
case_id: string
|
||||
import_id?: string
|
||||
attempt_id?: string | null
|
||||
scenario: AdvancedStyleValidationScenario
|
||||
case_key?: string
|
||||
sort_order?: number
|
||||
title?: string | null
|
||||
status: AdvancedMaterialValidationAttemptStatus | string
|
||||
content_prompt?: string | null
|
||||
style_prompt?: string | null
|
||||
final_prompt?: string | null
|
||||
negative_prompt?: string | null
|
||||
validation_image_uri?: string | null
|
||||
validation_thumbnail_uri?: string | null
|
||||
validation_image_url?: string | null
|
||||
validation_thumbnail_url?: string | null
|
||||
task_id?: string | null
|
||||
provider_id?: string | null
|
||||
provider_model?: string | null
|
||||
model_route?: string | null
|
||||
seed?: number | null
|
||||
input_image_count?: number
|
||||
style_scores: Record<string, unknown>
|
||||
style_review?: Record<string, unknown>
|
||||
content_novelty: Record<string, unknown>
|
||||
usage?: Record<string, unknown>
|
||||
fidelity_score?: number | null
|
||||
error: AdvancedMaterialError
|
||||
metadata: Record<string, unknown>
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
}
|
||||
|
||||
export interface AdvancedMaterialImport {
|
||||
import_id: string
|
||||
category_id?: string | null
|
||||
display_name: string
|
||||
source_image_uri?: string | null
|
||||
source_thumbnail_uri?: string | null
|
||||
source_image_url?: string | null
|
||||
source_thumbnail_url?: string | null
|
||||
status: AdvancedMaterialImportStatus | string
|
||||
recognition_prompt?: string | null
|
||||
recognition_negative_prompt?: string | null
|
||||
recognition_tags: string[]
|
||||
recognition_result: Record<string, unknown>
|
||||
recognition_model_route?: string | null
|
||||
prompt_revision?: number | null
|
||||
current_attempt_id?: string | null
|
||||
accepted_material_id?: string | null
|
||||
task_id?: string | null
|
||||
workflow_kind?: "single" | "style_set" | string
|
||||
sources?: AdvancedStyleSource[]
|
||||
consensus?: AdvancedStyleConsensus | null
|
||||
validation_cases?: AdvancedStyleValidationCase[]
|
||||
error: AdvancedMaterialError
|
||||
metadata: Record<string, unknown>
|
||||
created_by?: string | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface AdvancedMaterialValidationAttempt {
|
||||
attempt_id: string
|
||||
import_id: string
|
||||
attempt_number: number
|
||||
status: AdvancedMaterialValidationAttemptStatus | string
|
||||
adjustment_text?: string | null
|
||||
prompt?: string | null
|
||||
negative_prompt?: string | null
|
||||
model_route?: string | null
|
||||
provider_id?: string | null
|
||||
provider_model?: string | null
|
||||
task_id?: string | null
|
||||
validation_image_uri?: string | null
|
||||
validation_thumbnail_uri?: string | null
|
||||
validation_image_url?: string | null
|
||||
validation_thumbnail_url?: string | null
|
||||
seed?: number | null
|
||||
width?: number | null
|
||||
height?: number | null
|
||||
usage: Record<string, unknown>
|
||||
fidelity_score?: number | null
|
||||
decision?: string | null
|
||||
decision_notes?: string | null
|
||||
error: AdvancedMaterialError
|
||||
metadata: Record<string, unknown>
|
||||
validation_mode?: "single" | "style_cross_scene" | string
|
||||
validation_cases?: AdvancedStyleValidationCase[]
|
||||
created_at: string
|
||||
updated_at: string
|
||||
completed_at?: string | null
|
||||
decided_at?: string | null
|
||||
}
|
||||
|
||||
export interface AdvancedMaterial {
|
||||
material_id: string
|
||||
import_id: string
|
||||
accepted_attempt_id: string
|
||||
category_id?: string | null
|
||||
display_name: string
|
||||
source_image_uri?: string | null
|
||||
source_thumbnail_uri?: string | null
|
||||
source_image_url?: string | null
|
||||
source_thumbnail_url?: string | null
|
||||
validation_image_uri?: string | null
|
||||
validation_thumbnail_uri?: string | null
|
||||
validation_image_url?: string | null
|
||||
validation_thumbnail_url?: string | null
|
||||
prompt?: string | null
|
||||
negative_prompt?: string | null
|
||||
tags: string[]
|
||||
priority: number
|
||||
status: AdvancedMaterialStatus | string
|
||||
capsule: Record<string, unknown>
|
||||
metadata: Record<string, unknown>
|
||||
accepted_at?: string | null
|
||||
archived_at?: string | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface AdvancedMaterialImportListResult {
|
||||
items: AdvancedMaterialImport[]
|
||||
total: number
|
||||
page: number
|
||||
page_size: number
|
||||
}
|
||||
|
||||
export interface AdvancedMaterialValidationAttemptListResult {
|
||||
items: AdvancedMaterialValidationAttempt[]
|
||||
total: number
|
||||
}
|
||||
|
||||
export interface AdvancedMaterialListResult {
|
||||
items: AdvancedMaterial[]
|
||||
total: number
|
||||
page: number
|
||||
page_size: number
|
||||
}
|
||||
|
||||
export interface MaterialCatalogEntry {
|
||||
material_id: string
|
||||
material_type?: "advanced" | "standard" | string
|
||||
library_kind?: "advanced" | "standard" | string
|
||||
display_name: string
|
||||
category_id?: string | null
|
||||
image_uri?: string | null
|
||||
image_url?: string | null
|
||||
thumbnail_uri?: string | null
|
||||
thumbnail_url?: string | null
|
||||
prompt?: string | null
|
||||
negative_prompt?: string | null
|
||||
tags: string[]
|
||||
priority?: number
|
||||
status: string
|
||||
recognition_status?: string
|
||||
metadata: Record<string, unknown>
|
||||
created_at?: string
|
||||
updated_at?: string
|
||||
}
|
||||
|
||||
export interface MaterialCatalogResult {
|
||||
items: MaterialCatalogEntry[]
|
||||
total: number
|
||||
page: number
|
||||
page_size: number
|
||||
}
|
||||
|
||||
export interface AdvancedMaterialUploadRequest {
|
||||
image_data_url: string
|
||||
display_name: string
|
||||
category_id: string
|
||||
filename: string
|
||||
content_type?: string
|
||||
size_bytes: number
|
||||
upload_source: "file" | "paste" | "drop"
|
||||
metadata: Record<string, unknown>
|
||||
}
|
||||
|
||||
export interface AdvancedMaterialUploadResult {
|
||||
import: AdvancedMaterialImport
|
||||
task?: Record<string, unknown> | null
|
||||
task_id?: string | null
|
||||
queued?: boolean
|
||||
}
|
||||
|
||||
export interface AdvancedStyleSetUploadImage {
|
||||
client_source_id?: string
|
||||
image_data_url: string
|
||||
filename: string
|
||||
content_type?: string
|
||||
size_bytes: number
|
||||
upload_source: "file" | "paste" | "drop"
|
||||
sort_order: number
|
||||
is_primary: boolean
|
||||
}
|
||||
|
||||
export interface AdvancedStyleSetUploadRequest {
|
||||
display_name: string
|
||||
category_id: string
|
||||
images: AdvancedStyleSetUploadImage[]
|
||||
metadata: Record<string, unknown>
|
||||
}
|
||||
|
||||
export interface AdvancedMaterialValidationAttemptRequest {
|
||||
adjustment_text?: string
|
||||
}
|
||||
|
||||
export interface AdvancedMaterialDecisionRequest {
|
||||
adjustment_text?: string
|
||||
style_adjustment_text?: string
|
||||
validation_instruction?: string
|
||||
reuse_validation_scenarios?: boolean
|
||||
decision_notes?: string
|
||||
fidelity_score?: number
|
||||
}
|
||||
|
|
@ -11,6 +11,7 @@ export interface WardrobeItem {
|
|||
last_used_at?: string | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
library_kind?: "standard" | "advanced" | string
|
||||
}
|
||||
|
||||
export interface WardrobeMatchResult {
|
||||
|
|
@ -26,6 +27,7 @@ export interface WardrobeMatchResult {
|
|||
style?: string | null
|
||||
is_favorite: boolean
|
||||
usage_count: number
|
||||
library_kind?: "standard" | "advanced" | string
|
||||
score: number
|
||||
score_breakdown: Record<string, number>
|
||||
}>
|
||||
|
|
|
|||
|
|
@ -10,6 +10,11 @@ import MaterialOperationDialog from "./components/MaterialOperationDialog.vue"
|
|||
import { MATERIAL_BODY_SLOT_OPTIONS } from "./material-labels"
|
||||
import MetricCardGrid from "./components/MetricCardGrid.vue"
|
||||
import StateCard from "./components/StateCard.vue"
|
||||
import AdvancedMaterialImportPage from "./features/materials/AdvancedMaterialImportPage.vue"
|
||||
import {
|
||||
parseMaterialLibraryView,
|
||||
type MaterialLibraryView,
|
||||
} from "./features/materials/advanced-material-state"
|
||||
import GalleryPage from "./features/gallery/GalleryPage.vue"
|
||||
import type { GalleryImage } from "./features/gallery/types"
|
||||
import LogsPage from "./features/logs/LogsPage.vue"
|
||||
|
|
@ -125,6 +130,7 @@ import type {
|
|||
WorkflowNodeMaterialBindingsSaveRequest,
|
||||
} from "../../shared/types/workflow-material"
|
||||
import type { WardrobeItem } from "../../shared/types/wardrobe"
|
||||
import type { MaterialCatalogEntry } from "../../shared/types/advanced-material"
|
||||
import type { UiActionCard, UiDetailRow, UiMetricCard } from "../../shared/types/ui"
|
||||
import { uniqueBy, uniqueStrings } from "../../shared/utils/collections"
|
||||
import {
|
||||
|
|
@ -377,6 +383,8 @@ interface QuickProfileMaterialOption {
|
|||
colors: [string, string, string]
|
||||
imageUrl?: string
|
||||
source?: "backend"
|
||||
advanced?: boolean
|
||||
priority?: number
|
||||
}
|
||||
|
||||
interface QuickProfileMaterialCategorySpec {
|
||||
|
|
@ -650,6 +658,7 @@ const capabilityProfiles = ref<CapabilityProfile[]>([])
|
|||
const runtimeModelOptions = ref<RuntimeModelOption[]>([])
|
||||
const materialCategories = ref<MaterialCategory[]>([])
|
||||
const materials = ref<MaterialListResult | null>(null)
|
||||
const materialCatalog = ref<MaterialCatalogEntry[]>([])
|
||||
const profileWardrobeItemsByProfileId = ref<Record<string, WardrobeItem[]>>({})
|
||||
const profileWardrobeErrorsByProfileId = ref<Record<string, string>>({})
|
||||
const profileWardrobeLoadingByProfileId = ref<Record<string, boolean>>({})
|
||||
|
|
@ -659,6 +668,7 @@ const profileGenerationBusyByProfileId = ref<Record<string, ProfileGenerationMod
|
|||
const profileGenerationErrorsByProfileId = ref<Record<string, string>>({})
|
||||
|
||||
const activeNav = ref<NavKey>(initialNav())
|
||||
const materialLibraryView = ref<MaterialLibraryView>(initialMaterialLibraryView())
|
||||
const activeGenerateMode = ref<GenerateMode>("image")
|
||||
const workflowEntryConfirmed = ref(false)
|
||||
const workflowEntryFromProfile = ref(false)
|
||||
|
|
@ -1100,14 +1110,46 @@ const materialCategoryNameById = computed(() => {
|
|||
const entries = materialCategories.value.map((category) => [category.category_id, category.name] as const)
|
||||
return new Map(entries)
|
||||
})
|
||||
const materialHeroTitle = computed(() =>
|
||||
materialLibraryView.value === "standard"
|
||||
? "角色参考素材工作台"
|
||||
: materialLibraryView.value === "advanced"
|
||||
? "高还原度进阶素材库"
|
||||
: "进阶素材识别与验证",
|
||||
)
|
||||
const materialHeroDetail = computed(() =>
|
||||
materialLibraryView.value === "standard"
|
||||
? "集中管理参考图、识别结果、分类、素材包导入和角色衣橱绑定,后续角色工作流会从这里读取可确认素材。"
|
||||
: materialLibraryView.value === "advanced"
|
||||
? "只展示经过源图与验证图人工对照、明确封存的高优先级素材;数据和图片与普通素材严格隔离。"
|
||||
: "导入源图后自动识别并生成验证图,可根据还原度多轮调整描述,确认满意后再封存入库。",
|
||||
)
|
||||
const advancedCatalogMaterials = computed<Material[]>(() =>
|
||||
materialCatalog.value
|
||||
.filter((entry) => catalogEntryKind(entry) === "advanced" && normalizeStatus(entry.status) !== "archived")
|
||||
.map(materialFromCatalogEntry),
|
||||
)
|
||||
const combinedConfirmedMaterials = computed<Material[]>(() =>
|
||||
uniqueBy(
|
||||
[
|
||||
...advancedCatalogMaterials.value,
|
||||
...(materials.value?.items ?? []).filter(
|
||||
(material) => normalizeStatus(material.recognition_status) === "confirmed",
|
||||
),
|
||||
],
|
||||
(material) => material.material_id,
|
||||
),
|
||||
)
|
||||
const backendQuickProfileMaterialOptions = computed<QuickProfileMaterialOption[]>(() =>
|
||||
(materials.value?.items ?? [])
|
||||
.filter((material) => normalizeStatus(material.recognition_status) === "confirmed")
|
||||
combinedConfirmedMaterials.value
|
||||
.map(quickProfileMaterialOptionFromBackend)
|
||||
.filter((material): material is QuickProfileMaterialOption => Boolean(material)),
|
||||
)
|
||||
const quickProfileSelectableMaterials = computed<QuickProfileMaterialOption[]>(() => {
|
||||
return backendQuickProfileMaterialOptions.value
|
||||
return [...backendQuickProfileMaterialOptions.value].sort((left, right) => {
|
||||
const advancedDelta = Number(Boolean(right.advanced)) - Number(Boolean(left.advanced))
|
||||
return advancedDelta || Number(right.priority || 0) - Number(left.priority || 0)
|
||||
})
|
||||
})
|
||||
const rawMaterialLibraryCards = computed<MaterialLibraryCard[]>(() => {
|
||||
const backendItems = materials.value?.items ?? []
|
||||
|
|
@ -1560,7 +1602,11 @@ const selectedProfileWardrobeLoading = computed(
|
|||
)
|
||||
const profileWardrobeMaterials = computed<Material[]>(() =>
|
||||
uniqueBy(
|
||||
[...(materials.value?.items ?? []), ...Object.values(wardrobeMaterialRecords.value)],
|
||||
[
|
||||
...advancedCatalogMaterials.value,
|
||||
...(materials.value?.items ?? []),
|
||||
...Object.values(wardrobeMaterialRecords.value),
|
||||
],
|
||||
(material) => material.material_id,
|
||||
),
|
||||
)
|
||||
|
|
@ -2387,11 +2433,7 @@ const selectedWorkflowRoundCreateHint = computed(() => {
|
|||
? `将以 ${likedCount} 张成功且喜欢的候选创建下一轮。`
|
||||
: "请先把 1–3 张生成成功的候选标记为“喜欢”,再开始下一轮。"
|
||||
})
|
||||
const confirmedWorkflowMaterials = computed(() =>
|
||||
(materials.value?.items ?? []).filter(
|
||||
(material) => normalizeStatus(material.recognition_status) === "confirmed",
|
||||
),
|
||||
)
|
||||
const confirmedWorkflowMaterials = computed(() => combinedConfirmedMaterials.value)
|
||||
const selectedWorkflowNodeMaterialIds = computed(
|
||||
() => workflowNodeMaterialDetail.value?.material_bindings.selected_material_ids ?? [],
|
||||
)
|
||||
|
|
@ -4305,6 +4347,10 @@ async function loadAll() {
|
|||
lastUpdated.value = new Date().toISOString()
|
||||
}
|
||||
|
||||
function initialMaterialLibraryView(): MaterialLibraryView {
|
||||
return parseMaterialLibraryView(new URLSearchParams(window.location.search).get("materialView"))
|
||||
}
|
||||
|
||||
function initialProfilePageId() {
|
||||
return new URLSearchParams(window.location.search).get("profile")?.trim() ?? ""
|
||||
}
|
||||
|
|
@ -4691,16 +4737,18 @@ async function loadMaterials() {
|
|||
materialActionMessage.value = ""
|
||||
}
|
||||
try {
|
||||
const [categories, firstMaterialPage] = await Promise.all([
|
||||
const [categories, firstMaterialPage, catalogItems] = await Promise.all([
|
||||
getJson<MaterialCategory[]>(endpoints.materialCategories),
|
||||
getJson<MaterialListResult>(endpoints.materials, {
|
||||
page: 1,
|
||||
page_size: 100,
|
||||
}),
|
||||
loadMaterialCatalog(),
|
||||
])
|
||||
const materialList = await loadRemainingMaterialPages(firstMaterialPage)
|
||||
materialCategories.value = categories
|
||||
materials.value = materialList
|
||||
materialCatalog.value = catalogItems
|
||||
if (!materialCategorySyncAttempted.value && bootstrap.value?.permissions.can_manage_materials) {
|
||||
materialCategorySyncAttempted.value = true
|
||||
void ensureDefaultMaterialCategories(categories)
|
||||
|
|
@ -4708,12 +4756,33 @@ async function loadMaterials() {
|
|||
} catch (err) {
|
||||
materialCategories.value = []
|
||||
materials.value = null
|
||||
materialCatalog.value = []
|
||||
materialsError.value = errorMessage(err)
|
||||
} finally {
|
||||
loadingMaterials.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadMaterialCatalog(): Promise<MaterialCatalogEntry[]> {
|
||||
try {
|
||||
const payload = await getJson<{ items?: MaterialCatalogEntry[] } | MaterialCatalogEntry[]>(endpoints.materialCatalog, {
|
||||
page: 1,
|
||||
page_size: 200,
|
||||
})
|
||||
const items = Array.isArray(payload) ? payload : payload.items ?? []
|
||||
return items
|
||||
.filter((item) => normalizeStatus(item.status) !== "archived")
|
||||
.sort((left, right) => {
|
||||
const advancedDelta = Number(catalogEntryKind(right) === "advanced") - Number(catalogEntryKind(left) === "advanced")
|
||||
return advancedDelta || Number(right.priority || 0) - Number(left.priority || 0)
|
||||
})
|
||||
} catch {
|
||||
// Older backends do not expose the unified catalog. Ordinary materials
|
||||
// remain usable while the advanced-material migration is unavailable.
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
async function loadRemainingMaterialPages(firstPage: MaterialListResult): Promise<MaterialListResult> {
|
||||
const pageSize = Math.max(1, firstPage.page_size)
|
||||
const pageCount = Math.ceil(firstPage.total / pageSize)
|
||||
|
|
@ -4951,13 +5020,20 @@ function setProfileWardrobeLoading(profileId: string, loading: boolean) {
|
|||
|
||||
async function hydrateWardrobeMaterials(items: WardrobeItem[]) {
|
||||
const knownIds = new Set([
|
||||
...advancedCatalogMaterials.value.map((material) => material.material_id),
|
||||
...(materials.value?.items ?? []).map((material) => material.material_id),
|
||||
...Object.keys(wardrobeMaterialRecords.value),
|
||||
])
|
||||
const missingIds = uniqueStrings(items.map((item) => item.material_id).filter((materialId) => !knownIds.has(materialId)))
|
||||
if (!missingIds.length) return
|
||||
const loaded = await Promise.allSettled(
|
||||
missingIds.map((materialId) => getJson<Material>(endpoints.material(materialId))),
|
||||
missingIds.map((materialId) =>
|
||||
getJson<Material>(
|
||||
materialId.startsWith("advmat_")
|
||||
? endpoints.advancedMaterial(materialId)
|
||||
: endpoints.material(materialId),
|
||||
),
|
||||
),
|
||||
)
|
||||
const records = loaded
|
||||
.filter((result): result is PromiseFulfilledResult<Material> => result.status === "fulfilled")
|
||||
|
|
@ -4981,6 +5057,19 @@ function selectRailItem(item: NavItem) {
|
|||
setActiveNav(item.key, item.module)
|
||||
}
|
||||
|
||||
function setMaterialLibraryView(view: MaterialLibraryView) {
|
||||
materialLibraryView.value = view
|
||||
const url = new URL(window.location.href)
|
||||
url.searchParams.set("view", "materials")
|
||||
url.searchParams.set("materialView", view)
|
||||
window.history.replaceState({}, "", `${url.pathname}${url.search}`)
|
||||
}
|
||||
|
||||
async function handleAdvancedMaterialChanged() {
|
||||
lastUpdated.value = new Date().toISOString()
|
||||
materialCatalog.value = await loadMaterialCatalog()
|
||||
}
|
||||
|
||||
function reuseGalleryImage(image: GalleryImage) {
|
||||
selectedGalleryImageId.value = image.image_id
|
||||
selectedGenerateImageId.value = image.image_id
|
||||
|
|
@ -5171,6 +5260,38 @@ function materialCategoryCanonicalName(name: string) {
|
|||
return labels[name] ?? name
|
||||
}
|
||||
|
||||
function materialFromCatalogEntry(entry: MaterialCatalogEntry): Material {
|
||||
const now = new Date().toISOString()
|
||||
const kind = catalogEntryKind(entry)
|
||||
const priority = Number(entry.priority || entry.metadata?.material_priority || 0)
|
||||
return {
|
||||
material_id: entry.material_id,
|
||||
category_id: entry.category_id ?? null,
|
||||
display_name: entry.display_name,
|
||||
image_uri: entry.image_uri ?? null,
|
||||
thumbnail_uri: entry.thumbnail_uri ?? null,
|
||||
image_url: entry.image_url || entry.image_uri || null,
|
||||
thumbnail_url: entry.thumbnail_url || entry.thumbnail_uri || null,
|
||||
prompt: entry.prompt ?? null,
|
||||
negative_prompt: entry.negative_prompt ?? null,
|
||||
tags: Array.isArray(entry.tags) ? entry.tags : [],
|
||||
recognition_status: "confirmed",
|
||||
metadata: {
|
||||
...entry.metadata,
|
||||
material_type: kind,
|
||||
advanced_material: kind === "advanced",
|
||||
catalog_priority: priority,
|
||||
source_image_uri: entry.image_uri ?? null,
|
||||
},
|
||||
created_at: String(entry.created_at || now),
|
||||
updated_at: String(entry.updated_at || entry.created_at || now),
|
||||
}
|
||||
}
|
||||
|
||||
function catalogEntryKind(entry: MaterialCatalogEntry) {
|
||||
return String(entry.library_kind || entry.material_type || entry.metadata?.library_kind || "standard")
|
||||
}
|
||||
|
||||
function quickProfileMaterialOptionFromBackend(material: Material, index: number): QuickProfileMaterialOption | null {
|
||||
const categoryRecord = materialCategories.value.find((category) => category.category_id === material.category_id)
|
||||
const parentCategory = categoryRecord?.parent_id
|
||||
|
|
@ -5196,6 +5317,8 @@ function quickProfileMaterialOptionFromBackend(material: Material, index: number
|
|||
displayMediaUrl(material.thumbnail_url) ||
|
||||
displayMediaUrl(material.image_url),
|
||||
source: "backend",
|
||||
advanced: material.metadata?.advanced_material === true || material.metadata?.material_type === "advanced",
|
||||
priority: Number(material.metadata?.catalog_priority || 0),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -12247,7 +12370,8 @@ function errorMessage(err: unknown) {
|
|||
<span v-else class="quick-profile-empty-media-mark">无缩略图</span>
|
||||
</span>
|
||||
<span class="quick-profile-material-picker-copy">
|
||||
<small v-if="activeQuickProfileMaterialPickerCategory === QUICK_PROFILE_ACCESSORY_CATEGORY">
|
||||
<small v-if="reference.advanced">进阶素材 · 高优先级</small>
|
||||
<small v-else-if="activeQuickProfileMaterialPickerCategory === QUICK_PROFILE_ACCESSORY_CATEGORY">
|
||||
{{ quickProfileMaterialGroupLabel(reference) }}
|
||||
</small>
|
||||
<strong>{{ reference.label }}</strong>
|
||||
|
|
@ -12916,18 +13040,53 @@ function errorMessage(err: unknown) {
|
|||
<header class="materials-hero">
|
||||
<div>
|
||||
<span>素材库</span>
|
||||
<h2>角色参考素材工作台</h2>
|
||||
<p>集中管理参考图、识别结果、分类、素材包导入和角色衣橱绑定,后续角色工作流会从这里读取可确认素材。</p>
|
||||
<h2>{{ materialHeroTitle }}</h2>
|
||||
<p>{{ materialHeroDetail }}</p>
|
||||
</div>
|
||||
<div class="materials-hero-actions">
|
||||
<div v-if="materialLibraryView === 'standard'" class="materials-hero-actions">
|
||||
<button type="button" :disabled="loadingMaterials" @click="handleMaterialAction(materialActionCards[0])">
|
||||
{{ loadingMaterials ? "同步中" : "刷新素材" }}
|
||||
</button>
|
||||
<button type="button" @click="openMaterialWorkflowBinding">进入素材绑定</button>
|
||||
</div>
|
||||
<div v-else class="materials-hero-actions">
|
||||
<button type="button" @click="setMaterialLibraryView('advanced')">查看进阶素材</button>
|
||||
<button type="button" @click="setMaterialLibraryView('advanced-import')">新建进阶导入</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<MetricCardGrid :cards="materialStatusCards" section-class="materials-status-grid" />
|
||||
<nav class="materials-view-tabs" aria-label="素材库视图">
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: materialLibraryView === 'standard' }"
|
||||
:aria-pressed="materialLibraryView === 'standard'"
|
||||
@click="setMaterialLibraryView('standard')"
|
||||
>
|
||||
<strong>普通素材库</strong>
|
||||
<small>快速识别与直接确认</small>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: materialLibraryView === 'advanced' }"
|
||||
:aria-pressed="materialLibraryView === 'advanced'"
|
||||
@click="setMaterialLibraryView('advanced')"
|
||||
>
|
||||
<strong>进阶素材库</strong>
|
||||
<small>验证通过的高优先级素材</small>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="{ active: materialLibraryView === 'advanced-import' }"
|
||||
:aria-pressed="materialLibraryView === 'advanced-import'"
|
||||
@click="setMaterialLibraryView('advanced-import')"
|
||||
>
|
||||
<strong>进阶导入</strong>
|
||||
<small>识别、验证生图与多轮调整</small>
|
||||
</button>
|
||||
</nav>
|
||||
|
||||
<template v-if="materialLibraryView === 'standard'">
|
||||
<MetricCardGrid :cards="materialStatusCards" section-class="materials-status-grid" />
|
||||
|
||||
<StateCard
|
||||
v-if="materialsError || materialActionMessage"
|
||||
|
|
@ -13117,6 +13276,16 @@ function errorMessage(err: unknown) {
|
|||
@save-profile="saveSelectedMaterialProfile"
|
||||
@close="closeOverlay('materialDetail')"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<AdvancedMaterialImportPage
|
||||
v-else
|
||||
:mode="materialLibraryView"
|
||||
:categories="materialCategories"
|
||||
:can-manage="Boolean(bootstrap?.permissions.can_manage_materials)"
|
||||
@navigate="setMaterialLibraryView"
|
||||
@changed="handleAdvancedMaterialChanged"
|
||||
/>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
|
|
@ -15006,7 +15175,8 @@ body {
|
|||
}
|
||||
|
||||
.queue-task-list {
|
||||
display: grid;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
max-height: min(70vh, 760px);
|
||||
overflow-y: auto;
|
||||
|
|
@ -15019,8 +15189,12 @@ body {
|
|||
grid-template-columns: 88px minmax(0, 1fr) 70px;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
flex: 0 0 auto;
|
||||
width: 100%;
|
||||
height: auto;
|
||||
min-height: 88px;
|
||||
box-sizing: border-box;
|
||||
overflow: hidden;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
padding: 12px;
|
||||
|
|
@ -15038,6 +15212,7 @@ body {
|
|||
.queue-task-status {
|
||||
display: inline-grid;
|
||||
place-items: center;
|
||||
align-self: center;
|
||||
min-height: 32px;
|
||||
border-radius: 8px;
|
||||
padding: 0 10px;
|
||||
|
|
@ -15117,6 +15292,7 @@ body {
|
|||
.queue-task-meta {
|
||||
display: grid;
|
||||
gap: 4px;
|
||||
align-self: center;
|
||||
justify-items: end;
|
||||
min-width: 0;
|
||||
}
|
||||
|
|
@ -15230,6 +15406,48 @@ body {
|
|||
gap: 10px;
|
||||
}
|
||||
|
||||
.materials-view-tabs {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
gap: 8px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 10px;
|
||||
padding: 7px;
|
||||
background: var(--panel);
|
||||
}
|
||||
|
||||
.materials-view-tabs button {
|
||||
display: grid;
|
||||
gap: 3px;
|
||||
min-width: 0;
|
||||
min-height: 54px;
|
||||
border: 1px solid transparent;
|
||||
border-radius: 8px;
|
||||
padding: 8px 11px;
|
||||
color: var(--text);
|
||||
text-align: left;
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.materials-view-tabs button:hover,
|
||||
.materials-view-tabs button.active {
|
||||
border-color: var(--primary);
|
||||
background: var(--primary-soft);
|
||||
}
|
||||
|
||||
.materials-view-tabs strong,
|
||||
.materials-view-tabs small {
|
||||
min-width: 0;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.materials-view-tabs small {
|
||||
color: var(--text-soft);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.materials-hero {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
|
@ -21196,6 +21414,10 @@ body {
|
|||
}
|
||||
|
||||
@media (max-width: 900px) {
|
||||
.materials-view-tabs {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
.navigation-shell {
|
||||
grid-template-columns: 1fr;
|
||||
padding-bottom: 88px;
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ import type {
|
|||
} from "../../../shared/types/material-operations"
|
||||
import { formatBytes } from "../../../shared/utils/presentation"
|
||||
import { isStorageUri, isSupportedImageStorageUri, storageFilename } from "../../../shared/utils/storage-uri"
|
||||
import { MATERIAL_BODY_SLOT_OPTIONS } from "../material-labels"
|
||||
import { MATERIAL_BODY_SLOT_OPTIONS, materialCategoryLabel } from "../material-labels"
|
||||
import { createSafeBackdropClose } from "../safe-backdrop-close"
|
||||
|
||||
const props = defineProps<{
|
||||
|
|
@ -60,8 +60,8 @@ const categoryOptions = computed(() => {
|
|||
.map((category) => ({
|
||||
value: category.category_id,
|
||||
label: category.parent_id
|
||||
? `${categoriesById.get(category.parent_id)?.name ?? "未分类"} / ${category.name}`
|
||||
: category.name,
|
||||
? `${materialCategoryLabel(categoriesById.get(category.parent_id)?.name ?? "未分类")} / ${materialCategoryLabel(category.name)}`
|
||||
: materialCategoryLabel(category.name),
|
||||
}))
|
||||
})
|
||||
|
||||
|
|
|
|||
文件差异内容过多而无法显示
加载差异
|
|
@ -0,0 +1,148 @@
|
|||
import type {
|
||||
AdvancedMaterial,
|
||||
AdvancedMaterialImport,
|
||||
AdvancedMaterialValidationAttempt,
|
||||
} from "../../../../shared/types/advanced-material"
|
||||
|
||||
export type MaterialLibraryView = "standard" | "advanced" | "advanced-import"
|
||||
|
||||
export interface AdvancedMaterialStatusMeta {
|
||||
label: string
|
||||
detail: string
|
||||
tone: "idle" | "good" | "warn" | "bad" | "active"
|
||||
busy: boolean
|
||||
}
|
||||
|
||||
const MATERIAL_VIEWS = new Set<MaterialLibraryView>(["standard", "advanced", "advanced-import"])
|
||||
|
||||
export function parseMaterialLibraryView(value: string | null | undefined): MaterialLibraryView {
|
||||
return value && MATERIAL_VIEWS.has(value as MaterialLibraryView)
|
||||
? (value as MaterialLibraryView)
|
||||
: "standard"
|
||||
}
|
||||
|
||||
export function advancedImportStatusMeta(status: string): AdvancedMaterialStatusMeta {
|
||||
switch (normalize(status)) {
|
||||
case "pending_recognition":
|
||||
case "recognition_queued":
|
||||
case "style_source_recognition_queued":
|
||||
return { label: "等待识别", detail: "源图已保存,等待视觉模型提取特征。", tone: "idle", busy: true }
|
||||
case "recognizing":
|
||||
case "recognizing_style_sources":
|
||||
return { label: "正在识别", detail: "正在提取关键词、提示词和视觉特征。", tone: "active", busy: true }
|
||||
case "style_source_review_required":
|
||||
return { label: "待处理样本", detail: "部分样本识别未完成;可单独重试失败项或排除后继续。", tone: "warn", busy: false }
|
||||
case "style_consensus_queued":
|
||||
case "style_consensus_running":
|
||||
return { label: "正在交叉归纳", detail: "正在对全部有效样本和逐图报告进行共性与差异分析。", tone: "active", busy: true }
|
||||
case "style_consensus_failed":
|
||||
return { label: "交叉归纳失败", detail: "逐图结果仍已保留,可重新发起交叉归纳。", tone: "bad", busy: false }
|
||||
case "pending_validation":
|
||||
case "ready_for_validation":
|
||||
return { label: "等待验证", detail: "识别完成,可以生成第一张还原度验证图。", tone: "warn", busy: false }
|
||||
case "validating":
|
||||
case "validation_queued":
|
||||
return { label: "正在验证", detail: "验证图已入队,完成后可对照源图审核。", tone: "active", busy: true }
|
||||
case "validation_partial_failed":
|
||||
return { label: "部分验证失败", detail: "已完成的验证图会保留,只需单独重试失败场景。", tone: "warn", busy: false }
|
||||
case "pending_review":
|
||||
case "awaiting_review":
|
||||
return { label: "等待审核", detail: "请比较源图与验证图,决定封存或继续调整。", tone: "warn", busy: false }
|
||||
case "accepted":
|
||||
return { label: "已封存", detail: "已作为高优先级进阶素材入库。", tone: "good", busy: false }
|
||||
case "rejected":
|
||||
case "adjustment_required":
|
||||
return { label: "待调整", detail: "上一轮未通过,可补充描述重新验证。", tone: "warn", busy: false }
|
||||
case "failed":
|
||||
case "validation_failed":
|
||||
case "recognition_failed":
|
||||
return { label: "处理失败", detail: "可以查看失败原因并重试当前阶段。", tone: "bad", busy: false }
|
||||
case "cancelled":
|
||||
return { label: "已取消", detail: "当前导入任务已取消。", tone: "idle", busy: false }
|
||||
case "archived":
|
||||
return { label: "已归档", detail: "当前导入记录已归档。", tone: "idle", busy: false }
|
||||
default:
|
||||
return { label: status || "未知状态", detail: "后端返回了尚未识别的状态。", tone: "idle", busy: false }
|
||||
}
|
||||
}
|
||||
|
||||
export function advancedAttemptStatusMeta(status: string): AdvancedMaterialStatusMeta {
|
||||
switch (normalize(status)) {
|
||||
case "queued":
|
||||
return { label: "已入队", detail: "验证任务正在等待执行。", tone: "idle", busy: true }
|
||||
case "running":
|
||||
return { label: "生成中", detail: "正在生成本轮还原度验证图。", tone: "active", busy: true }
|
||||
case "succeeded":
|
||||
case "completed":
|
||||
case "awaiting_review":
|
||||
return { label: "待审核", detail: "验证图已生成,请判断还原度。", tone: "warn", busy: false }
|
||||
case "accepted":
|
||||
return { label: "已采纳", detail: "本轮验证图已经封存入库。", tone: "good", busy: false }
|
||||
case "rejected":
|
||||
return { label: "未采纳", detail: "本轮已保留,可基于反馈继续调整。", tone: "warn", busy: false }
|
||||
case "failed":
|
||||
return { label: "生成失败", detail: "检查错误后可再次发起验证。", tone: "bad", busy: false }
|
||||
case "cancelled":
|
||||
return { label: "已取消", detail: "本轮验证任务已取消。", tone: "idle", busy: false }
|
||||
default:
|
||||
return { label: status || "未知状态", detail: "后端返回了尚未识别的状态。", tone: "idle", busy: false }
|
||||
}
|
||||
}
|
||||
|
||||
export function sortAdvancedMaterials(items: AdvancedMaterial[]): AdvancedMaterial[] {
|
||||
return [...items].sort((left, right) => {
|
||||
const activeDelta = Number(normalize(right.status) === "active") - Number(normalize(left.status) === "active")
|
||||
if (activeDelta) return activeDelta
|
||||
const priorityDelta = Number(right.priority || 0) - Number(left.priority || 0)
|
||||
if (priorityDelta) return priorityDelta
|
||||
return timestamp(right.accepted_at || right.updated_at) - timestamp(left.accepted_at || left.updated_at)
|
||||
})
|
||||
}
|
||||
|
||||
export function sortAdvancedImports(items: AdvancedMaterialImport[]): AdvancedMaterialImport[] {
|
||||
return [...items].sort((left, right) => timestamp(right.updated_at) - timestamp(left.updated_at))
|
||||
}
|
||||
|
||||
export function sortValidationAttempts(items: AdvancedMaterialValidationAttempt[]): AdvancedMaterialValidationAttempt[] {
|
||||
return [...items].sort((left, right) => {
|
||||
const attemptDelta = Number(right.attempt_number || 0) - Number(left.attempt_number || 0)
|
||||
return attemptDelta || timestamp(right.created_at) - timestamp(left.created_at)
|
||||
})
|
||||
}
|
||||
|
||||
export function advancedMaterialErrorMessage(value: unknown): string {
|
||||
if (!value || typeof value !== "object") return ""
|
||||
const record = value as Record<string, unknown>
|
||||
const message = String(record.message || record.detail || record.reason || "").trim()
|
||||
if (message) return message
|
||||
const code = String(record.code || record.error_code || "").trim()
|
||||
return code ? `错误代码:${code}` : ""
|
||||
}
|
||||
|
||||
export function firstRecord<T>(payload: unknown, keys: string[]): T | null {
|
||||
if (!payload || typeof payload !== "object") return null
|
||||
const record = payload as Record<string, unknown>
|
||||
for (const key of keys) {
|
||||
const value = record[key]
|
||||
if (value && typeof value === "object" && !Array.isArray(value)) return value as T
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function listItems<T>(payload: unknown): T[] {
|
||||
if (Array.isArray(payload)) return payload as T[]
|
||||
if (!payload || typeof payload !== "object") return []
|
||||
const record = payload as Record<string, unknown>
|
||||
if (Array.isArray(record.items)) return record.items as T[]
|
||||
if (record.data && typeof record.data === "object") return listItems<T>(record.data)
|
||||
return []
|
||||
}
|
||||
|
||||
function normalize(status: string) {
|
||||
return String(status || "").trim().toLowerCase()
|
||||
}
|
||||
|
||||
function timestamp(value: string | null | undefined) {
|
||||
const parsed = Date.parse(String(value || ""))
|
||||
return Number.isFinite(parsed) ? parsed : 0
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
import { computed, onMounted, ref } from "vue"
|
||||
import { getJson } from "../../../../shared/api/client"
|
||||
import { endpoints } from "../../../../shared/api/endpoints"
|
||||
import { encodeModelRouteChain, parseModelRouteChain, type ModelRouteChain } from "./model-route-chain"
|
||||
|
||||
type Assignment = {
|
||||
key: string
|
||||
|
|
@ -12,7 +13,7 @@ type Assignment = {
|
|||
runtime: "active" | "rules"
|
||||
}
|
||||
type AdapterOption = { id: string; name: string; route: string; model: string; enabled: boolean }
|
||||
type RuntimeModel = { route: string; provider_id: string; name: string; model: string; modality: string; source: string }
|
||||
type RuntimeModel = { route: string; provider_id: string; name: string; model: string; modality: string; source: "plugin" | "astrbot" | string }
|
||||
type ReviewGroup = {
|
||||
key: string
|
||||
primaryKey: string
|
||||
|
|
@ -34,6 +35,8 @@ const runtimeModels = ref<RuntimeModel[]>([])
|
|||
|
||||
const cards: Assignment[] = [
|
||||
{ key: "material_recognition", title: "素材导入解析", description: "识别素材类别、描述、标签和生成提示词。", recommendation: "建议选择支持图片理解与结构化 JSON 输出的视觉模型;优先稳定和低成本,通常不需要最强推理模型。", kind: "vision", runtime: "active" },
|
||||
{ key: "advanced_material_recognition", title: "进阶素材高保真识别", description: "尽可能完整提取颜色、材质、纹理、轮廓、结构和装饰细节,作为还原验证的文字基线。", recommendation: "建议选择视觉细节提取稳定、结构化 JSON 输出可靠的模型;这里应优先识别完整度,而不是创意改写能力。", kind: "vision", runtime: "active" },
|
||||
{ key: "advanced_material_validation_generation", title: "进阶素材验证生图", description: "仅根据识别提示词生成一张验证图,用于判断文字描述能否还原原素材。", recommendation: "建议选择提示词遵循和材质、结构还原能力稳定的生图模型;验证阶段不会把原图作为参考图传入。", kind: "image", runtime: "active" },
|
||||
{ key: "profile_parse", title: "外部角色导入解析", description: "将外部角色文本拆成结构化角色字段。", recommendation: "建议选择指令遵循稳定、长文本处理好且支持 JSON 输出的文本模型;角色资料很长时关注上下文长度。", kind: "text", runtime: "rules" },
|
||||
{ key: "persona_split", title: "角色设定拆解", description: "拆解身份、外观、服装、关系和世界观。", recommendation: "建议使用语义理解和信息抽取能力较强的文本模型,重点避免遗漏设定或把推测内容当成事实。", kind: "text", runtime: "rules" },
|
||||
{ key: "prompt_planning", title: "提示词规划与扩写", description: "生成阶段提示词矩阵和负面提示词。", recommendation: "建议选择熟悉生图提示词、风格词和负面词的文本模型;创意能力优先于纯推理速度。", kind: "text", runtime: "rules" },
|
||||
|
|
@ -45,9 +48,8 @@ const cards: Assignment[] = [
|
|||
{ key: "hires_fix", title: "高清修复", description: "最终放大、细节修复和风格统一。", recommendation: "建议选择支持高分辨率、图生图或重绘的模型,重点保留原角色身份与构图,避免放大时改变面部和服装。", kind: "image", runtime: "active" },
|
||||
]
|
||||
|
||||
const textRoutes = computed(() => runtimeModels.value.filter(item => item.modality === "text_generation"))
|
||||
const visionRoutes = computed(() => runtimeModels.value.filter(item => item.modality === "vision_recognition"))
|
||||
const imageRoutes = computed(() => runtimeModels.value.filter(item => item.modality === "image_generation"))
|
||||
const pluginModels = computed(() => runtimeModels.value.filter(item => item.source === "plugin" && item.route))
|
||||
const astrbotModels = computed(() => runtimeModels.value.filter(item => item.source === "astrbot" && item.route))
|
||||
const reviewGroups: ReviewGroup[] = [
|
||||
{
|
||||
key: "visual_review_routes",
|
||||
|
|
@ -68,13 +70,20 @@ const reviewGroups: ReviewGroup[] = [
|
|||
kind: "image",
|
||||
},
|
||||
]
|
||||
function value(key: string) { return String(props.modelValue[key] || "") }
|
||||
function options(card: Assignment) {
|
||||
const adapterRoutes = props.adapters.filter(item => item.enabled && item.route).map(item => item.route)
|
||||
if (card.kind === "image") return Array.from(new Set([...adapterRoutes, ...imageRoutes.value.map(item => item.route)].filter(Boolean)))
|
||||
const astrbotRoutes = textRoutes.value.map(item => item.route)
|
||||
if (card.kind === "vision") return Array.from(new Set([props.visionRoute, ...visionRoutes.value.map(item => item.route)].filter(Boolean)))
|
||||
return Array.from(new Set(astrbotRoutes.filter(Boolean)))
|
||||
function chainValue(key: string) { return parseModelRouteChain(props.modelValue[key]) }
|
||||
function sourceValue(key: string, source: keyof ModelRouteChain) { return chainValue(key)[source] }
|
||||
function pluginOptions(card: Pick<Assignment, "kind">) {
|
||||
const routes = pluginModels.value
|
||||
.filter(item => card.kind === "text" || item.modality === `${card.kind}_generation` || (card.kind === "vision" && item.modality === "vision_recognition"))
|
||||
.map(item => item.route)
|
||||
if (card.kind === "image") {
|
||||
routes.push(...props.adapters.filter(item => item.enabled && item.route).map(item => item.route))
|
||||
}
|
||||
return Array.from(new Set(routes.filter(Boolean)))
|
||||
}
|
||||
function astrbotOptions(card: Pick<Assignment, "kind">) {
|
||||
if (card.kind === "image") return []
|
||||
return Array.from(new Set(astrbotModels.value.map(item => item.route).filter(Boolean)))
|
||||
}
|
||||
function optionLabel(route: string) {
|
||||
const adapter = props.adapters.find(item => item.route === route)
|
||||
|
|
@ -83,7 +92,21 @@ function optionLabel(route: string) {
|
|||
if (!adapter) return route
|
||||
return `${adapter.name}${adapter.model ? ` · ${adapter.model}` : ""} (${route})`
|
||||
}
|
||||
function update(key: string, next: string) { emit("update:modelValue", { ...props.modelValue, [key]: next }) }
|
||||
function updateSource(key: string, source: keyof ModelRouteChain, next: string) {
|
||||
const chain = chainValue(key)
|
||||
emit("update:modelValue", {
|
||||
...props.modelValue,
|
||||
[key]: encodeModelRouteChain({ ...chain, [source]: next }),
|
||||
})
|
||||
}
|
||||
function unavailablePluginRoute(card: Pick<Assignment, "key" | "kind">) {
|
||||
const current = sourceValue(card.key, "plugin")
|
||||
return current && !pluginOptions(card).includes(current) ? current : ""
|
||||
}
|
||||
function unavailableAstrbotRoute(card: Pick<Assignment, "key" | "kind">) {
|
||||
const current = sourceValue(card.key, "astrbot")
|
||||
return current && !astrbotOptions(card).includes(current) ? current : ""
|
||||
}
|
||||
function reviewRoutes(group: ReviewGroup) {
|
||||
const configured = props.modelValue[group.key]
|
||||
const routes = Array.isArray(configured) && configured.length
|
||||
|
|
@ -97,6 +120,11 @@ function updateReview(group: ReviewGroup, index: number, route: string) {
|
|||
next[index] = route
|
||||
emit("update:modelValue", { ...props.modelValue, [group.key]: next, [group.primaryKey]: next[0] || "", [group.secondaryKey]: next[1] || "" })
|
||||
}
|
||||
function updateReviewSource(group: ReviewGroup, index: number, source: keyof ModelRouteChain, route: string) {
|
||||
const current = reviewRoutes(group)
|
||||
const chain = parseModelRouteChain(current[index])
|
||||
updateReview(group, index, encodeModelRouteChain({ ...chain, [source]: route }))
|
||||
}
|
||||
function addReviewRoute(group: ReviewGroup) {
|
||||
emit("update:modelValue", { ...props.modelValue, [group.key]: [...reviewRoutes(group), ""] })
|
||||
}
|
||||
|
|
@ -106,11 +134,18 @@ function removeReviewRoute(group: ReviewGroup, index: number) {
|
|||
const next = current.filter((_, itemIndex) => itemIndex !== index)
|
||||
emit("update:modelValue", { ...props.modelValue, [group.key]: next, [group.primaryKey]: next[0] || "", [group.secondaryKey]: next[1] || "" })
|
||||
}
|
||||
function reviewOptions(group: ReviewGroup, index: number) {
|
||||
function reviewOptions(group: ReviewGroup, index: number, source: keyof ModelRouteChain) {
|
||||
const current = reviewRoutes(group)
|
||||
const selected = new Set(current.filter((route, itemIndex) => itemIndex !== index && route))
|
||||
const selected = new Set(
|
||||
current
|
||||
.filter((_, itemIndex) => itemIndex !== index)
|
||||
.map(route => parseModelRouteChain(route)[source])
|
||||
.filter(Boolean),
|
||||
)
|
||||
const reviewCard: Assignment = { key: "review", title: "", description: "", recommendation: "", kind: group.kind, runtime: "active" }
|
||||
return options(reviewCard).filter(route => !selected.has(route) || route === current[index])
|
||||
const value = parseModelRouteChain(current[index])[source]
|
||||
const options = source === "plugin" ? pluginOptions(reviewCard) : astrbotOptions(reviewCard)
|
||||
return options.filter(route => !selected.has(route) || route === value)
|
||||
}
|
||||
onMounted(async () => {
|
||||
try { runtimeModels.value = (await getJson<{items:RuntimeModel[]}>(endpoints.runtimeModels)).items }
|
||||
|
|
@ -120,14 +155,32 @@ onMounted(async () => {
|
|||
|
||||
<template>
|
||||
<section class="assignments">
|
||||
<header><div><span>模型分工</span><strong>按功能节点选择模型路由</strong><p>生图与素材识别配置会立即进入运行链;标记为“规则引擎”的节点当前保留模型路由,启用远程 LLM 后无需重新调整界面。</p></div></header>
|
||||
<header><div><span>模型分工</span><strong>插件优先,AstrBot 备用</strong><p>每个节点分别保存插件适配器和 AstrBot 提供商。运行时优先调用插件路由;插件未配置、不可用或调用失败时,自动尝试 AstrBot 备用路由。</p></div></header>
|
||||
<div class="grid">
|
||||
<article v-for="card in cards" :key="card.key">
|
||||
<div class="card-head"><span :class="card.kind">{{ card.kind === 'image' ? '生图' : card.kind === 'vision' ? '视觉' : '文本' }}</span><em :class="card.runtime">{{ card.runtime === 'active' ? '已接入运行时' : '当前规则引擎' }}</em></div>
|
||||
<strong>{{ card.title }}</strong>
|
||||
<p>{{ card.description }}</p>
|
||||
<div class="recommendation"><b>选型建议</b><span>{{ card.recommendation }}</span></div>
|
||||
<label><span>模型路由</span><select :value="value(card.key)" :disabled="disabled" @change="update(card.key, ($event.target as HTMLSelectElement).value)"><option value="">自动选择默认路由</option><option v-for="route in options(card)" :key="route" :value="route">{{ optionLabel(route) }}</option></select></label>
|
||||
<div class="route-pair">
|
||||
<label>
|
||||
<span><b>1</b> 插件适配器(优先)</span>
|
||||
<select :value="sourceValue(card.key, 'plugin')" :disabled="disabled" @change="updateSource(card.key, 'plugin', ($event.target as HTMLSelectElement).value)">
|
||||
<option value="">不使用插件路由</option>
|
||||
<option v-if="unavailablePluginRoute(card)" :value="unavailablePluginRoute(card)" disabled>当前不可用:{{ unavailablePluginRoute(card) }}</option>
|
||||
<option v-for="route in pluginOptions(card)" :key="route" :value="route">{{ optionLabel(route) }}</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
<span><b>2</b> AstrBot 适配器(备用)</span>
|
||||
<select :value="sourceValue(card.key, 'astrbot')" :disabled="disabled || card.kind === 'image'" @change="updateSource(card.key, 'astrbot', ($event.target as HTMLSelectElement).value)">
|
||||
<option value="">{{ card.kind === 'image' ? 'AstrBot 4.25 暂无生图桥接' : '不使用 AstrBot 备用路由' }}</option>
|
||||
<option v-if="unavailableAstrbotRoute(card)" :value="unavailableAstrbotRoute(card)" disabled>当前不可用:{{ unavailableAstrbotRoute(card) }}</option>
|
||||
<option v-for="route in astrbotOptions(card)" :key="route" :value="route">{{ optionLabel(route) }}</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
<p v-if="card.kind === 'vision'" class="route-note">AstrBot 视觉路由仍需所选模型真实支持图片输入;运行时会通过 image_urls 调用。</p>
|
||||
<small>{{ card.key }}</small>
|
||||
</article>
|
||||
<article v-for="group in reviewGroups" :key="group.key" class="review-group">
|
||||
|
|
@ -135,9 +188,13 @@ onMounted(async () => {
|
|||
<div><strong>{{ group.title }}</strong><p>{{ group.description }}</p></div>
|
||||
<div class="recommendation"><b>选型建议</b><span>{{ group.recommendation }}</span></div>
|
||||
<div class="review-routes">
|
||||
<label v-for="(route, index) in reviewRoutes(group)" :key="index">
|
||||
<label v-for="(route, index) in reviewRoutes(group)" :key="index" class="review-route-row">
|
||||
<span>{{ index === 0 ? '主审核模型' : index === 1 ? '复核模型' : `附加审核模型 ${index}` }}</span>
|
||||
<div class="review-select"><select :value="route" :disabled="disabled" @change="updateReview(group, index, ($event.target as HTMLSelectElement).value)"><option value="">请选择不同的模型路由</option><option v-for="option in reviewOptions(group, index)" :key="option" :value="option">{{ optionLabel(option) }}</option></select><button v-if="index > 1" type="button" :disabled="disabled" title="移除此审核路由" @click="removeReviewRoute(group, index)">×</button></div>
|
||||
<div class="review-select">
|
||||
<select :value="parseModelRouteChain(route).plugin" :disabled="disabled" @change="updateReviewSource(group, index, 'plugin', ($event.target as HTMLSelectElement).value)"><option value="">插件路由(优先)</option><option v-for="option in reviewOptions(group, index, 'plugin')" :key="option" :value="option">{{ optionLabel(option) }}</option></select>
|
||||
<select :value="parseModelRouteChain(route).astrbot" :disabled="disabled || group.kind === 'image'" @change="updateReviewSource(group, index, 'astrbot', ($event.target as HTMLSelectElement).value)"><option value="">{{ group.kind === 'image' ? 'AstrBot 暂无生图桥接' : 'AstrBot 路由(备用)' }}</option><option v-for="option in reviewOptions(group, index, 'astrbot')" :key="option" :value="option">{{ optionLabel(option) }}</option></select>
|
||||
<button v-if="index > 1" type="button" :disabled="disabled" title="移除此审核路由" @click="removeReviewRoute(group, index)">×</button>
|
||||
</div>
|
||||
</label>
|
||||
</div>
|
||||
<button class="add-review" type="button" :disabled="disabled" @click="addReviewRoute(group)">+ 添加审核路由</button>
|
||||
|
|
@ -148,5 +205,5 @@ onMounted(async () => {
|
|||
</template>
|
||||
|
||||
<style scoped>
|
||||
.assignments{display:grid;gap:14px}.assignments>header{border:1px solid var(--border);border-radius:10px;padding:15px;background:linear-gradient(105deg,var(--primary-soft),var(--panel-muted))}.assignments header>div{display:grid;gap:4px}.assignments header span{color:var(--text-soft);font-size:11px;font-weight:800}.assignments header p,.grid p{margin:0;color:var(--text-soft);line-height:1.5}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:11px}.grid article{display:grid;gap:9px;border:1px solid var(--border);border-radius:10px;padding:14px;background:var(--panel-muted)}.card-head{display:flex;justify-content:space-between}.card-head span,.card-head em{border-radius:5px;padding:3px 7px;font-size:10px;font-style:normal}.card-head span.image{color:#7757e8;background:var(--primary-soft)}.card-head span.vision{color:#25845a;background:color-mix(in srgb,#39aa72 13%,var(--panel))}.card-head span.text{color:#a06a12;background:color-mix(in srgb,#e1a53a 15%,var(--panel))}.card-head em{color:var(--text-soft);background:var(--panel)}.card-head em.active{color:#25845a}.recommendation{display:grid;grid-template-columns:auto 1fr;align-items:start;gap:8px;border-left:3px solid var(--primary);border-radius:4px;padding:8px 10px;background:color-mix(in srgb,var(--primary-soft) 55%,var(--panel))}.recommendation b{white-space:nowrap;color:var(--primary);font-size:11px}.recommendation span{color:var(--text-soft);font-size:12px;line-height:1.55}.grid label{display:grid;gap:5px}.grid label>span,.grid article>small{color:var(--text-soft);font-size:11px}.grid select{width:100%;min-height:39px;border:1px solid var(--border);border-radius:7px;padding:0 34px 0 10px;color:var(--text);background:var(--panel);font:inherit;cursor:pointer}.grid select:disabled{cursor:not-allowed;opacity:.65}.review-group{grid-column:1/-1}.review-routes{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:10px}.review-select{display:flex;gap:7px}.review-select button{flex:0 0 39px;border:1px solid var(--border);border-radius:7px;color:#c45151;background:var(--panel);font-size:20px}.add-review{justify-self:start;border:1px dashed var(--primary);border-radius:7px;padding:8px 13px;color:var(--primary);background:var(--panel);cursor:pointer}@media(max-width:760px){.grid,.review-routes{grid-template-columns:1fr}.recommendation{grid-template-columns:1fr}}
|
||||
.assignments{display:grid;gap:14px}.assignments>header{border:1px solid var(--border);border-radius:10px;padding:15px;background:linear-gradient(105deg,var(--primary-soft),var(--panel-muted))}.assignments header>div{display:grid;gap:4px}.assignments header span{color:var(--text-soft);font-size:11px;font-weight:800}.assignments header p,.grid p{margin:0;color:var(--text-soft);line-height:1.5}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:11px}.grid article{display:grid;gap:9px;border:1px solid var(--border);border-radius:10px;padding:14px;background:var(--panel-muted)}.card-head{display:flex;justify-content:space-between}.card-head span,.card-head em{border-radius:5px;padding:3px 7px;font-size:10px;font-style:normal}.card-head span.image{color:#7757e8;background:var(--primary-soft)}.card-head span.vision{color:#25845a;background:color-mix(in srgb,#39aa72 13%,var(--panel))}.card-head span.text{color:#a06a12;background:color-mix(in srgb,#e1a53a 15%,var(--panel))}.card-head em{color:var(--text-soft);background:var(--panel)}.card-head em.active{color:#25845a}.recommendation{display:grid;grid-template-columns:auto 1fr;align-items:start;gap:8px;border-left:3px solid var(--primary);border-radius:4px;padding:8px 10px;background:color-mix(in srgb,var(--primary-soft) 55%,var(--panel))}.recommendation b{white-space:nowrap;color:var(--primary);font-size:11px}.recommendation span{color:var(--text-soft);font-size:12px;line-height:1.55}.route-pair{display:grid;grid-template-columns:1fr 1fr;gap:8px}.grid label{display:grid;gap:5px;min-width:0}.grid label>span,.grid article>small{color:var(--text-soft);font-size:11px}.grid label>span b{display:inline-grid;place-items:center;width:17px;height:17px;margin-right:4px;border-radius:50%;color:#fff;background:var(--primary);font-size:9px}.grid select{width:100%;min-height:39px;border:1px solid var(--border);border-radius:7px;padding:0 34px 0 10px;color:var(--text);background:var(--panel);font:inherit;cursor:pointer}.grid select:disabled{cursor:not-allowed;opacity:.65}.route-note{font-size:11px!important}.review-group{grid-column:1/-1}.review-routes{display:grid;gap:10px}.review-route-row{border:1px solid var(--border);border-radius:8px;padding:10px;background:var(--panel)}.review-select{display:grid;grid-template-columns:minmax(0,1fr) minmax(0,1fr) auto;gap:7px}.review-select button{width:39px;border:1px solid var(--border);border-radius:7px;color:#c45151;background:var(--panel);font-size:20px}.add-review{justify-self:start;border:1px dashed var(--primary);border-radius:7px;padding:8px 13px;color:var(--primary);background:var(--panel);cursor:pointer}@media(max-width:900px){.route-pair,.review-select{grid-template-columns:1fr}.review-select button{width:100%}}@media(max-width:760px){.grid{grid-template-columns:1fr}.recommendation{grid-template-columns:1fr}}
|
||||
</style>
|
||||
|
|
|
|||
|
|
@ -2,22 +2,35 @@
|
|||
import { computed, onMounted, ref } from "vue"
|
||||
import { getJson, postJson } from "../../../../shared/api/client"
|
||||
import { endpoints } from "../../../../shared/api/endpoints"
|
||||
import { encodeModelRouteChain, parseModelRouteChain, type ModelRouteChain } from "./model-route-chain"
|
||||
|
||||
type AdapterOption = { id:string; name:string; route:string; model:string; enabled:boolean }
|
||||
type RuntimeModel = { route:string;provider_id:string;name:string;model:string;modality:string;source:string }
|
||||
type RouteKind = "text" | "vision"
|
||||
type RouteOption = { label:string;value:string }
|
||||
const props = defineProps<{modelValue:Record<string,unknown>;adapters:AdapterOption[];disabled:boolean}>()
|
||||
const emit = defineEmits<{"update:modelValue":[value:Record<string,unknown>]}>()
|
||||
|
||||
const value = computed(()=>clone(props.modelValue))
|
||||
const runtimeModels=ref<RuntimeModel[]>([])
|
||||
const imageRoutes = computed(()=>props.adapters.filter(item=>item.enabled&&item.route).map(item=>({label:`${item.name}${item.model?` / ${item.model}`:""}`,value:item.route})))
|
||||
const textRoutes = computed(()=>runtimeModels.value.filter(item=>item.modality==="text_generation").map(item=>({label:`${item.name} / ${item.model||item.provider_id}`,value:item.route})))
|
||||
const routes = imageRoutes
|
||||
const previewPrompt=ref(""),previewSelection=ref("translated"),manualPrompt=ref(""),previewBusy=ref(false),previewError=ref(""),previewResult=ref<Record<string,unknown>|null>(null)
|
||||
function clone<T>(input:T):T{return JSON.parse(JSON.stringify(input??{})) as T}
|
||||
function get(path:string,fallback:unknown=""){let current:unknown=value.value;for(const key of path.split(".")){if(!current||typeof current!=="object"||Array.isArray(current))return fallback;current=(current as Record<string,unknown>)[key]}return current??fallback}
|
||||
function set(path:string,nextValue:unknown){const next=clone(value.value);const keys=path.split(".");let current=next;for(const key of keys.slice(0,-1)){if(!current[key]||typeof current[key]!=="object"||Array.isArray(current[key]))current[key]={};current=current[key] as Record<string,unknown>}current[keys.at(-1)!]=nextValue;emit("update:modelValue",next)}
|
||||
function checked(path:string){return get(path,false)===true}
|
||||
function routeChain(path:string){return parseModelRouteChain(get(path,""))}
|
||||
function routeSource(path:string,source:keyof ModelRouteChain){return routeChain(path)[source]}
|
||||
function setRouteSource(path:string,source:keyof ModelRouteChain,route:string){set(path,encodeModelRouteChain({...routeChain(path),[source]:route}))}
|
||||
function routeOptions(kind:RouteKind,source:keyof ModelRouteChain):RouteOption[]{
|
||||
const models=runtimeModels.value.filter(item=>item.route&&item.source===(source==="plugin"?"plugin":"astrbot"))
|
||||
const filtered=source==="astrbot"||kind==="text"?models:models.filter(item=>item.modality==="vision_recognition")
|
||||
const result=new Map<string,string>()
|
||||
for(const item of filtered)result.set(item.route,`${item.name} / ${item.model||item.provider_id}`)
|
||||
return Array.from(result,([value,label])=>({value,label}))
|
||||
}
|
||||
function unavailableRoute(path:string,kind:RouteKind,source:keyof ModelRouteChain){const current=routeSource(path,source);return current&&!routeOptions(kind,source).some(item=>item.value===current)?current:""}
|
||||
const entrypoints=[{key:"web",label:"Web"},{key:"qq",label:"QQ"},{key:"telegram",label:"Telegram"},{key:"discord",label:"Discord"},{key:"api",label:"API"}]
|
||||
async function runPreview(){if(!previewPrompt.value.trim())return;previewBusy.value=true;previewError.value="";try{previewResult.value=await postJson(endpoints.promptGovernancePreview,{prompt:previewPrompt.value,entrypoint:"web",translation_consent:true,selection:previewSelection.value,manual_prompt:manualPrompt.value,model_route:String(get('routing.safe_route')||'image_low'),content_permissions:{adult_user_allowed:true,adult_group_allowed:true,quota_allowed:true}})}catch(e){previewError.value=e instanceof Error?e.message:String(e)}finally{previewBusy.value=false}}
|
||||
onMounted(async()=>{try{runtimeModels.value=(await getJson<{items:RuntimeModel[]}>(endpoints.runtimeModels)).items}catch{runtimeModels.value=[]}})
|
||||
|
|
@ -29,17 +42,17 @@ onMounted(async()=>{try{runtimeModels.value=(await getJson<{items:RuntimeModel[]
|
|||
|
||||
<article class="preview-card"><header><div><span>链路预览</span><strong>发送前检查最终提示词与路由</strong></div><button :disabled="previewBusy||!previewPrompt.trim()" @click="runPreview">{{previewBusy?'处理中…':'运行预览'}}</button></header><textarea v-model="previewPrompt" rows="3" placeholder="输入中文或英文生图需求"></textarea><div class="preview-choice"><label><input v-model="previewSelection" type="radio" value="original">直接使用原文</label><label><input v-model="previewSelection" type="radio" value="translated">使用转译</label><label><input v-model="previewSelection" type="radio" value="manual">手动编辑后使用</label></div><textarea v-if="previewSelection==='manual'" v-model="manualPrompt" rows="2" placeholder="输入手动调整后的提示词"></textarea><p v-if="previewError" class="preview-error">{{previewError}}</p><div v-if="previewResult" class="preview-result"><span><b>转译</b>{{String((previewResult.translation as any)?.status||'—')}}</span><span><b>审核</b>{{String((previewResult.moderation as any)?.level||'—')}}</span><span><b>路由</b>{{String((previewResult.route_decision as any)?.selected_route||(previewResult.route_decision as any)?.reason||'—')}}</span><p>{{String((previewResult.normalization as any)?.normalized_text||previewResult.selected_prompt||'')}}</p><details><summary>查看完整决策摘要</summary><pre>{{JSON.stringify(previewResult,null,2)}}</pre></details></div></article>
|
||||
|
||||
<article class="card"><header><div><span>01 · Prompt Translation</span><strong>LLM 提示词转译</strong><p>用户明确允许后,将中文需求结构化为英文生图提示词;失败时安全回退原文。</p></div><label class="switch"><input type="checkbox" :checked="checked('translation.enabled')" :disabled="disabled" @change="set('translation.enabled',($event.target as HTMLInputElement).checked)"><span></span></label></header><div class="grid"><label><span>转译模型路由</span><select :value="String(get('translation.model_route'))" :disabled="disabled" @change="set('translation.model_route',($event.target as HTMLSelectElement).value)"><option value="">未配置(使用原文)</option><option v-for="route in textRoutes" :key="route.value" :value="route.value">{{route.label}}</option></select><small>直接读取 AstrBot 已添加的文本 LLM;建议选择指令遵循稳定、支持结构化 JSON 的模型。</small></label><label><span>模板版本</span><input :value="String(get('translation.template_version','1.0'))" :disabled="disabled" @input="set('translation.template_version',($event.target as HTMLInputElement).value)"><small>便于追踪提示词差异和回滚。</small></label></div><div class="entries"><span>允许自动转译的入口</span><label v-for="item in entrypoints" :key="item.key"><input type="checkbox" :checked="checked(`translation.entrypoints.${item.key}`)" :disabled="disabled" @change="set(`translation.entrypoints.${item.key}`,($event.target as HTMLInputElement).checked)">{{item.label}}</label></div></article>
|
||||
<article class="card"><header><div><span>01 · Prompt Translation</span><strong>LLM 提示词转译</strong><p>用户明确允许后,将中文需求结构化为英文生图提示词;插件路由失败时自动尝试 AstrBot 备用路由,全部失败才安全回退原文。</p></div><label class="switch"><input type="checkbox" :checked="checked('translation.enabled')" :disabled="disabled" @change="set('translation.enabled',($event.target as HTMLInputElement).checked)"><span></span></label></header><div class="route-pair source-pair"><label><span><b>1</b> ImageStudio 插件适配器(优先)</span><select :value="routeSource('translation.model_route','plugin')" :disabled="disabled" @change="setRouteSource('translation.model_route','plugin',($event.target as HTMLSelectElement).value)"><option value="">不使用插件路由</option><option v-if="unavailableRoute('translation.model_route','text','plugin')" :value="unavailableRoute('translation.model_route','text','plugin')" disabled>当前不可用:{{unavailableRoute('translation.model_route','text','plugin')}}</option><option v-for="route in routeOptions('text','plugin')" :key="route.value" :value="route.value">{{route.label}}</option></select></label><label><span><b>2</b> AstrBot 模型提供商(备用)</span><select :value="routeSource('translation.model_route','astrbot')" :disabled="disabled" @change="setRouteSource('translation.model_route','astrbot',($event.target as HTMLSelectElement).value)"><option value="">不使用 AstrBot 备用</option><option v-if="unavailableRoute('translation.model_route','text','astrbot')" :value="unavailableRoute('translation.model_route','text','astrbot')" disabled>当前不可用:{{unavailableRoute('translation.model_route','text','astrbot')}}</option><option v-for="route in routeOptions('text','astrbot')" :key="route.value" :value="route.value">{{route.label}}</option></select></label></div><div class="grid"><label><span>模板版本</span><input :value="String(get('translation.template_version','1.0'))" :disabled="disabled" @input="set('translation.template_version',($event.target as HTMLInputElement).value)"><small>便于追踪提示词差异和回滚。</small></label></div><div class="entries"><span>允许自动转译的入口</span><label v-for="item in entrypoints" :key="item.key"><input type="checkbox" :checked="checked(`translation.entrypoints.${item.key}`)" :disabled="disabled" @change="set(`translation.entrypoints.${item.key}`,($event.target as HTMLInputElement).checked)">{{item.label}}</label></div></article>
|
||||
|
||||
<article class="card"><header><div><span>02 · Moderation</span><strong>生成前内容审核</strong><p>规则审核始终执行;可增加一个 LLM 复核模型,冲突时采用更严格结果。</p></div><label class="switch"><input type="checkbox" :checked="checked('moderation.llm_review_enabled')" :disabled="disabled" @change="set('moderation.llm_review_enabled',($event.target as HTMLInputElement).checked)"><span></span></label></header><div class="grid"><label><span>审核模型路由</span><select :value="String(get('moderation.model_route'))" :disabled="disabled" @change="set('moderation.model_route',($event.target as HTMLSelectElement).value)"><option value="">仅使用本地规则</option><option v-for="route in textRoutes" :key="route.value" :value="route.value">{{route.label}}</option></select><small>建议使用对中文语义、年龄和风险分类理解稳定的模型。</small></label><label><span>审核服务失败策略</span><select :value="String(get('moderation.failure_policy','fail_closed'))" :disabled="disabled" @change="set('moderation.failure_policy',($event.target as HTMLSelectElement).value)"><option value="fail_closed">保守拒绝</option><option value="confirm">要求人工确认</option></select><small>审核不可用时不会自动进入宽松模型。</small></label></div></article>
|
||||
<article class="card"><header><div><span>02 · Moderation</span><strong>生成前内容审核</strong><p>规则审核始终执行;LLM 复核按插件优先、AstrBot 备用执行,冲突时采用更严格结果。</p></div><label class="switch"><input type="checkbox" :checked="checked('moderation.llm_review_enabled')" :disabled="disabled" @change="set('moderation.llm_review_enabled',($event.target as HTMLInputElement).checked)"><span></span></label></header><div class="route-pair source-pair"><label><span><b>1</b> ImageStudio 插件适配器(优先)</span><select :value="routeSource('moderation.model_route','plugin')" :disabled="disabled" @change="setRouteSource('moderation.model_route','plugin',($event.target as HTMLSelectElement).value)"><option value="">不使用插件路由</option><option v-if="unavailableRoute('moderation.model_route','text','plugin')" :value="unavailableRoute('moderation.model_route','text','plugin')" disabled>当前不可用:{{unavailableRoute('moderation.model_route','text','plugin')}}</option><option v-for="route in routeOptions('text','plugin')" :key="route.value" :value="route.value">{{route.label}}</option></select></label><label><span><b>2</b> AstrBot 模型提供商(备用)</span><select :value="routeSource('moderation.model_route','astrbot')" :disabled="disabled" @change="setRouteSource('moderation.model_route','astrbot',($event.target as HTMLSelectElement).value)"><option value="">不使用 AstrBot 备用</option><option v-if="unavailableRoute('moderation.model_route','text','astrbot')" :value="unavailableRoute('moderation.model_route','text','astrbot')" disabled>当前不可用:{{unavailableRoute('moderation.model_route','text','astrbot')}}</option><option v-for="route in routeOptions('text','astrbot')" :key="route.value" :value="route.value">{{route.label}}</option></select></label></div><div class="grid"><label><span>审核服务失败策略</span><select :value="String(get('moderation.failure_policy','fail_closed'))" :disabled="disabled" @change="set('moderation.failure_policy',($event.target as HTMLSelectElement).value)"><option value="fail_closed">保守拒绝</option><option value="confirm">要求人工确认</option></select><small>两条审核路由都不可用时不会自动进入宽松模型。</small></label></div></article>
|
||||
|
||||
<article class="card routing"><header><div><span>03 · Dynamic Routing</span><strong>普通 / NSFW 动态模型路由</strong><p>普通内容走安全模型;仅通过审核且权限满足的成人内容可自动进入指定 NSFW 模型。</p></div><label class="switch"><input type="checkbox" :checked="checked('routing.adult_auto_route_enabled')" :disabled="disabled" @change="set('routing.adult_auto_route_enabled',($event.target as HTMLInputElement).checked)"><span></span></label></header><div class="route-pair"><label><span>普通生图路由</span><select :value="String(get('routing.safe_route'))" :disabled="disabled" @change="set('routing.safe_route',($event.target as HTMLSelectElement).value)"><option value="">跟随当前默认路由</option><option v-for="route in routes" :key="route.value" :value="route.value">{{route.label}}</option></select></label><div class="arrow">→</div><label><span>NSFW 生图路由</span><select :value="String(get('routing.nsfw_route'))" :disabled="disabled" @change="set('routing.nsfw_route',($event.target as HTMLSelectElement).value)"><option value="">未配置</option><option v-for="route in routes" :key="route.value" :value="route.value">{{route.label}}</option></select></label></div><div class="entries"><span>允许成人内容路由的入口</span><label v-for="item in entrypoints" :key="item.key"><input type="checkbox" :checked="checked(`routing.entrypoints.${item.key}`)" :disabled="disabled" @change="set(`routing.entrypoints.${item.key}`,($event.target as HTMLInputElement).checked)">{{item.label}}</label></div><div class="policy"><b>固定安全策略</b><span>允许的成人内容:审核 + 管理员开关 + 入口权限 + 用户/群聊权限全部通过后自动分流。</span><span>恐怖主义、高危暴力、现实伤害威胁和极端血腥:始终禁止并警告,不进入任何生图模型。</span><span>普通模型仅在明确内容安全拒绝、二次审核为 adult_allowed 时单次回退 NSFW;网络、额度、鉴权和参数错误不会触发。</span></div></article>
|
||||
|
||||
<article class="card"><header><div><span>04 · Output Review</span><strong>生成后图片审核</strong><p>在聊天返回和普通图库展示前检查结果;异常结果进入受限图库并等待复核。</p></div><label class="switch"><input type="checkbox" :checked="checked('image_moderation.enabled')" :disabled="disabled" @change="set('image_moderation.enabled',($event.target as HTMLInputElement).checked)"><span></span></label></header><div class="grid"><label><span>图片审核模型路由</span><select :value="String(get('image_moderation.model_route'))" :disabled="disabled" @change="set('image_moderation.model_route',($event.target as HTMLSelectElement).value)"><option value="">未配置</option><option v-for="route in routes" :key="route.value" :value="route.value">{{route.label}}</option></select><small>需要支持图片理解和结构化风险输出。</small></label><label><span>敏感日志脱敏</span><select :value="String(get('retention.redact_sensitive_logs',true))" :disabled="disabled" @change="set('retention.redact_sensitive_logs',($event.target as HTMLSelectElement).value==='true')"><option value="true">开启</option><option value="false">关闭</option></select></label></div><div class="retention"><label><span>原始提示词</span><input type="number" min="0" :value="Number(get('retention.raw_prompt_days',30))" :disabled="disabled" @input="set('retention.raw_prompt_days',Number(($event.target as HTMLInputElement).value))"><small>天</small></label><label><span>转译提示词</span><input type="number" min="0" :value="Number(get('retention.translated_prompt_days',90))" :disabled="disabled" @input="set('retention.translated_prompt_days',Number(($event.target as HTMLInputElement).value))"><small>天</small></label><label><span>审核记录</span><input type="number" min="0" :value="Number(get('retention.moderation_record_days',180))" :disabled="disabled" @input="set('retention.moderation_record_days',Number(($event.target as HTMLInputElement).value))"><small>天</small></label></div></article>
|
||||
<article class="card"><header><div><span>04 · Output Review</span><strong>生成后图片审核</strong><p>在聊天返回和普通图库展示前检查结果;插件视觉模型失败时自动尝试 AstrBot 备用视觉模型。</p></div><label class="switch"><input type="checkbox" :checked="checked('image_moderation.enabled')" :disabled="disabled" @change="set('image_moderation.enabled',($event.target as HTMLInputElement).checked)"><span></span></label></header><div class="route-pair source-pair"><label><span><b>1</b> ImageStudio 视觉适配器(优先)</span><select :value="routeSource('image_moderation.model_route','plugin')" :disabled="disabled" @change="setRouteSource('image_moderation.model_route','plugin',($event.target as HTMLSelectElement).value)"><option value="">不使用插件视觉路由</option><option v-if="unavailableRoute('image_moderation.model_route','vision','plugin')" :value="unavailableRoute('image_moderation.model_route','vision','plugin')" disabled>当前不可用:{{unavailableRoute('image_moderation.model_route','vision','plugin')}}</option><option v-for="route in routeOptions('vision','plugin')" :key="route.value" :value="route.value">{{route.label}}</option></select></label><label><span><b>2</b> AstrBot 视觉提供商(备用)</span><select :value="routeSource('image_moderation.model_route','astrbot')" :disabled="disabled" @change="setRouteSource('image_moderation.model_route','astrbot',($event.target as HTMLSelectElement).value)"><option value="">不使用 AstrBot 备用</option><option v-if="unavailableRoute('image_moderation.model_route','vision','astrbot')" :value="unavailableRoute('image_moderation.model_route','vision','astrbot')" disabled>当前不可用:{{unavailableRoute('image_moderation.model_route','vision','astrbot')}}</option><option v-for="route in routeOptions('vision','astrbot')" :key="route.value" :value="route.value">{{route.label}}</option></select></label></div><small class="vision-note">AstrBot 备用模型必须真实支持图片输入;运行时通过 image_urls 调用。</small><div class="grid"><label><span>敏感日志脱敏</span><select :value="String(get('retention.redact_sensitive_logs',true))" :disabled="disabled" @change="set('retention.redact_sensitive_logs',($event.target as HTMLSelectElement).value==='true')"><option value="true">开启</option><option value="false">关闭</option></select></label></div><div class="retention"><label><span>原始提示词</span><input type="number" min="0" :value="Number(get('retention.raw_prompt_days',30))" :disabled="disabled" @input="set('retention.raw_prompt_days',Number(($event.target as HTMLInputElement).value))"><small>天</small></label><label><span>转译提示词</span><input type="number" min="0" :value="Number(get('retention.translated_prompt_days',90))" :disabled="disabled" @input="set('retention.translated_prompt_days',Number(($event.target as HTMLInputElement).value))"><small>天</small></label><label><span>审核记录</span><input type="number" min="0" :value="Number(get('retention.moderation_record_days',180))" :disabled="disabled" @input="set('retention.moderation_record_days',Number(($event.target as HTMLInputElement).value))"><small>天</small></label></div></article>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.governance{display:grid;gap:13px}.intro,.card{border:1px solid var(--border);border-radius:10px;background:var(--panel-muted)}.intro{display:flex;align-items:center;justify-content:space-between;gap:18px;padding:16px;background:linear-gradient(105deg,var(--primary-soft),var(--panel-muted))}.intro>div:first-child,.card header>div{display:grid;gap:4px}.intro span,.card header span,.entries>span{color:var(--text-soft);font-size:11px;font-weight:800}.intro p,.card p,.grid small{margin:0;color:var(--text-soft);font-size:12px;line-height:1.5}.flow{display:flex;align-items:center;gap:6px;white-space:nowrap}.flow b{border-radius:6px;padding:6px 8px;color:var(--primary);background:var(--panel)}.flow i{color:var(--text-soft);font-style:normal}.card{display:grid;gap:14px;padding:15px}.card>header{display:flex;align-items:flex-start;justify-content:space-between;gap:16px}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px}.grid label,.route-pair label{display:grid;gap:6px}.grid label>span,.route-pair label>span,.retention span{font-size:12px;font-weight:750}select,input{width:100%;min-height:40px;box-sizing:border-box;border:1px solid var(--border);border-radius:7px;padding:0 10px;color:var(--text);background:var(--panel)}.switch input{position:absolute;opacity:0;pointer-events:none}.switch span{position:relative;display:block;width:44px;height:25px;border-radius:99px;background:var(--border);cursor:pointer}.switch span:after{position:absolute;top:3px;left:3px;width:19px;height:19px;border-radius:50%;background:#fff;content:"";transition:.18s}.switch input:checked+span{background:var(--primary)}.switch input:checked+span:after{transform:translateX(19px)}.entries{display:flex;align-items:center;flex-wrap:wrap;gap:10px;border-top:1px solid var(--border);padding-top:12px}.entries label{display:flex;align-items:center;gap:5px;font-size:12px}.entries input{width:auto;min-height:auto}.route-pair{display:grid;grid-template-columns:1fr 36px 1fr;align-items:end;gap:8px}.arrow{display:grid;place-items:center;min-height:40px;color:var(--primary);font-size:20px}.routing{border-color:color-mix(in srgb,var(--primary) 35%,var(--border))}.policy{display:grid;gap:6px;border-left:3px solid #d39b35;border-radius:6px;padding:11px 12px;background:color-mix(in srgb,#e1a53a 10%,var(--panel))}.policy b{color:#9a6818}.policy span{color:var(--text-soft);font-size:12px;line-height:1.45}.retention{display:grid;grid-template-columns:repeat(3,1fr);gap:10px}.retention label{display:grid;grid-template-columns:1fr 72px auto;align-items:center;gap:7px}.retention small{color:var(--text-soft)}@media(max-width:800px){.intro{align-items:flex-start;flex-direction:column}.flow{flex-wrap:wrap}.grid,.route-pair,.retention{grid-template-columns:1fr}.arrow{transform:rotate(90deg)}}
|
||||
.governance{display:grid;gap:13px}.intro,.card{border:1px solid var(--border);border-radius:10px;background:var(--panel-muted)}.intro{display:flex;align-items:center;justify-content:space-between;gap:18px;padding:16px;background:linear-gradient(105deg,var(--primary-soft),var(--panel-muted))}.intro>div:first-child,.card header>div{display:grid;gap:4px}.intro span,.card header span,.entries>span{color:var(--text-soft);font-size:11px;font-weight:800}.intro p,.card p,.grid small{margin:0;color:var(--text-soft);font-size:12px;line-height:1.5}.flow{display:flex;align-items:center;gap:6px;white-space:nowrap}.flow b{border-radius:6px;padding:6px 8px;color:var(--primary);background:var(--panel)}.flow i{color:var(--text-soft);font-style:normal}.card{display:grid;gap:14px;padding:15px}.card>header{display:flex;align-items:flex-start;justify-content:space-between;gap:16px}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:12px}.grid label,.route-pair label{display:grid;gap:6px}.grid label>span,.route-pair label>span,.retention span{font-size:12px;font-weight:750}.source-pair{grid-template-columns:repeat(2,minmax(0,1fr));align-items:start}.source-pair label>span b{display:inline-grid;place-items:center;width:18px;height:18px;margin-right:4px;border-radius:50%;color:#fff;background:var(--primary);font-size:10px}.vision-note{color:var(--text-soft);font-size:11px}select,input{width:100%;min-height:40px;box-sizing:border-box;border:1px solid var(--border);border-radius:7px;padding:0 10px;color:var(--text);background:var(--panel)}.switch input{position:absolute;opacity:0;pointer-events:none}.switch span{position:relative;display:block;width:44px;height:25px;border-radius:99px;background:var(--border);cursor:pointer}.switch span:after{position:absolute;top:3px;left:3px;width:19px;height:19px;border-radius:50%;background:#fff;content:"";transition:.18s}.switch input:checked+span{background:var(--primary)}.switch input:checked+span:after{transform:translateX(19px)}.entries{display:flex;align-items:center;flex-wrap:wrap;gap:10px;border-top:1px solid var(--border);padding-top:12px}.entries label{display:flex;align-items:center;gap:5px;font-size:12px}.entries input{width:auto;min-height:auto}.route-pair{display:grid;grid-template-columns:1fr 36px 1fr;align-items:end;gap:8px}.arrow{display:grid;place-items:center;min-height:40px;color:var(--primary);font-size:20px}.routing{border-color:color-mix(in srgb,var(--primary) 35%,var(--border))}.policy{display:grid;gap:6px;border-left:3px solid #d39b35;border-radius:6px;padding:11px 12px;background:color-mix(in srgb,#e1a53a 10%,var(--panel))}.policy b{color:#9a6818}.policy span{color:var(--text-soft);font-size:12px;line-height:1.45}.retention{display:grid;grid-template-columns:repeat(3,1fr);gap:10px}.retention label{display:grid;grid-template-columns:1fr 72px auto;align-items:center;gap:7px}.retention small{color:var(--text-soft)}@media(max-width:800px){.intro{align-items:flex-start;flex-direction:column}.flow{flex-wrap:wrap}.grid,.route-pair,.retention{grid-template-columns:1fr}.arrow{transform:rotate(90deg)}}
|
||||
.preview-card{display:grid;gap:10px;border:1px solid var(--border);border-radius:10px;padding:15px;background:var(--panel-muted)}.preview-card>header{display:flex;align-items:center;justify-content:space-between;gap:12px}.preview-card>header>div{display:grid;gap:3px}.preview-card>header span{color:var(--text-soft);font-size:11px;font-weight:800}.preview-card button{min-height:36px;border:1px solid var(--primary);border-radius:7px;padding:0 11px;color:#fff;background:var(--primary);cursor:pointer}.preview-card button:disabled{opacity:.5}.preview-card textarea{width:100%;box-sizing:border-box;border:1px solid var(--border);border-radius:7px;padding:9px;color:var(--text);background:var(--panel);font:inherit;resize:vertical}.preview-choice{display:flex;flex-wrap:wrap;gap:10px}.preview-choice label{display:flex;align-items:center;gap:5px;font-size:12px}.preview-result{display:grid;grid-template-columns:repeat(3,1fr);gap:7px;border-top:1px solid var(--border);padding-top:10px}.preview-result>span{display:grid;gap:3px;border-radius:6px;padding:8px;background:var(--panel)}.preview-result>span b{color:var(--text-soft);font-size:10px}.preview-result>p,.preview-result>details{grid-column:1/-1}.preview-result>p{margin:0;border-radius:6px;padding:9px;background:var(--panel);font-size:12px;line-height:1.5}.preview-result pre{max-height:260px;overflow:auto;border-radius:6px;padding:8px;background:var(--panel);font-size:10px;white-space:pre-wrap}.preview-error{color:var(--danger)!important}
|
||||
</style>
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import { computed, onMounted, ref } from "vue"
|
|||
import { getJson } from "../../../../shared/api/client"
|
||||
import { endpoints } from "../../../../shared/api/endpoints"
|
||||
import type { MaterialCategory } from "../../../../shared/types/material"
|
||||
import { materialCategoryLabel } from "../../material-labels"
|
||||
|
||||
type Requirement = { category: string; requirement: string; enabled: boolean }
|
||||
const props = defineProps<{ modelValue: Record<string, unknown>; disabled: boolean }>()
|
||||
|
|
@ -32,7 +33,9 @@ function addRequirement() { emit("update:modelValue", { ...props.modelValue, cat
|
|||
function removeRequirement(index: number) { emit("update:modelValue", { ...props.modelValue, category_requirements: requirements.value.filter((_, itemIndex) => itemIndex !== index) }) }
|
||||
function categoryLabel(category: MaterialCategory) {
|
||||
const parent = categories.value.find(item => item.category_id === category.parent_id)
|
||||
return parent ? `${parent.name} / ${category.name}` : category.name
|
||||
return parent
|
||||
? `${materialCategoryLabel(parent.name)} / ${materialCategoryLabel(category.name)}`
|
||||
: materialCategoryLabel(category.name)
|
||||
}
|
||||
onMounted(async () => {
|
||||
try { categories.value = await getJson<MaterialCategory[]>(endpoints.materialCategories) }
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ const previewSchema: SettingsSchema = {schema_version:"3.0-preview",sections:[
|
|||
{key:"generation",title:"生成服务",fields:[]},
|
||||
{key:"model_assignments",title:"模型分工",fields:[]},
|
||||
{key:"recognition_prompts",title:"识别提示词",fields:[]},
|
||||
{key:"advanced_materials",title:"进阶素材",fields:[]},
|
||||
{key:"chat_access",title:"聊天权限",fields:[]},
|
||||
{key:"features",title:"功能开关",fields:[
|
||||
{key:"quick_profile",label:"快速角色",type:"bool",options:[],sensitive:false,readonly:false},{key:"workspace",label:"角色工作流",type:"bool",options:[],sensitive:false,readonly:false},{key:"material_library",label:"素材库",type:"bool",options:[],sensitive:false,readonly:false},{key:"wardrobe",label:"角色衣橱",type:"bool",options:[],sensitive:false,readonly:false},{key:"api_discovery",label:"能力探测",type:"bool",options:[],sensitive:false,readonly:false},{key:"private_companion_integration",label:"私伴插件集成",type:"bool",options:[],sensitive:false,readonly:false},
|
||||
|
|
@ -37,8 +38,8 @@ const previewSchema: SettingsSchema = {schema_version:"3.0-preview",sections:[
|
|||
]},
|
||||
{key:"backup",title:"数据备份",fields:[]},
|
||||
]}
|
||||
const previewConfig = computed<Record<string,unknown>>(()=>({generation:{default_model:"image_default",provider:{provider_id:"openai-compatible",base_url:"",image_endpoint:"/v1/images/generations",model_id:"",route_name:"image_default",api_key_env_var:"IMAGE_API_KEY",timeout_seconds:180}},model_assignments:{material_recognition:"material_vlm",quick_generation:"image_default",portrait_generation:"image_default",halfbody_generation:"image_default",fullbody_generation:"image_default",sheet_generation:"image_default",hires_fix:"image_default"},recognition_prompts:{default_instruction:"请先判断图片中素材的准确分类,再尽可能完整提取所有可见事实,包括颜色、材质、纹理、结构、轮廓、图案、裁剪、装饰、配件、视角和光照。不要把推测写成事实。根据结果生成可直接用于生图的详细正向提示词和负向提示词。仅返回 JSON:prompt、negative_prompt、tags、confidence、description。",category_requirements:[]},chat_access:{group_allowlist:[],user_allowlist:[]},features:{quick_profile:true,workspace:true,material_library:true,wardrobe:true,api_discovery:true,private_companion_integration:false},storage:{images_path:"data/images",thumbnails_path:"data/thumbnails",materials_path:"data/materials",styles_path:"data/styles",archives_path:"data/archives",temp_path:"data/temp"},database:{backend:"auto",sqlite:{path:"data/imagestudio.db"},postgresql:{enabled:false,host:"127.0.0.1",port:5432,database:"imagestudio",user:"",password:""}}}))
|
||||
const effectiveSchema=computed(()=>{const schema=clone(props.schema??previewSchema);for(const section of [{key:"prompt_governance",title:"提示词与内容分流",fields:[]},{key:"review",title:"审查策略",fields:[]},{key:"capabilities",title:"能力档案",fields:[]},{key:"integration",title:"私伴集成",fields:[]},{key:"cost_guard",title:"成本保护",fields:[]},{key:"diagnostics",title:"系统诊断",fields:[]},{key:"backup",title:"数据备份",fields:[]}])if(!schema.sections.some(item=>item.key===section.key))schema.sections.push(section);return schema})
|
||||
const previewConfig = computed<Record<string,unknown>>(()=>({generation:{default_model:"image_default",provider:{provider_id:"openai-compatible",base_url:"",image_endpoint:"/v1/images/generations",model_id:"",route_name:"image_default",api_key_env_var:"IMAGE_API_KEY",timeout_seconds:180}},model_assignments:{material_recognition:"material_vlm",advanced_material_recognition:"material_vlm",advanced_material_validation_generation:"image_default",quick_generation:"image_default",portrait_generation:"image_default",halfbody_generation:"image_default",fullbody_generation:"image_default",sheet_generation:"image_default",hires_fix:"image_default"},recognition_prompts:{default_instruction:"请先判断图片中素材的准确分类,再尽可能完整提取所有可见事实,包括颜色、材质、纹理、结构、轮廓、图案、裁剪、装饰、配件、视角和光照。不要把推测写成事实。根据结果生成可直接用于生图的详细正向提示词和负向提示词。仅返回 JSON:prompt、negative_prompt、tags、confidence、description。",category_requirements:[]},advanced_materials:{cost_budget:{enabled:false,days:30,max_task_count:null,max_image_count:null,max_estimated_cost:null,price_key:"",per_request:0,per_image:0,per_1k_tokens:0}},chat_access:{group_allowlist:[],user_allowlist:[]},features:{quick_profile:true,workspace:true,material_library:true,wardrobe:true,api_discovery:true,private_companion_integration:false},storage:{images_path:"data/images",thumbnails_path:"data/thumbnails",materials_path:"data/materials",advanced_materials_path:"data/advanced_materials",styles_path:"data/styles",archives_path:"data/archives",temp_path:"data/temp"},database:{backend:"auto",sqlite:{path:"data/imagestudio.db"},postgresql:{enabled:false,host:"127.0.0.1",port:5432,database:"imagestudio",user:"",password:""}}}))
|
||||
const effectiveSchema=computed(()=>{const schema=clone(props.schema??previewSchema);for(const section of [{key:"advanced_materials",title:"进阶素材",fields:[]},{key:"prompt_governance",title:"提示词与内容分流",fields:[]},{key:"review",title:"审查策略",fields:[]},{key:"capabilities",title:"能力档案",fields:[]},{key:"integration",title:"私伴集成",fields:[]},{key:"cost_guard",title:"成本保护",fields:[]},{key:"diagnostics",title:"系统诊断",fields:[]},{key:"backup",title:"数据备份",fields:[]}])if(!schema.sections.some(item=>item.key===section.key))schema.sections.push(section);return schema})
|
||||
const baseline=computed(()=>props.current?.config??previewConfig.value)
|
||||
const draft=ref<Record<string,unknown>>({})
|
||||
const activeSectionKey=ref("generation")
|
||||
|
|
@ -100,7 +101,10 @@ function probeConnected(id:string){return probeResults.value[id]?.probe_result.c
|
|||
function probeModels(id:string):string[]{const value=probeResults.value[id]?.probe_result.models;return Array.isArray(value)?value.map(String):[]}
|
||||
function probeMaxSize(id:string){const value=probeResults.value[id]?.capabilities.max_tested_size;return typeof value==="string"?value:"未测试"}
|
||||
function probeSizeLabel(id:string){const size=probeMaxSize(id);return size==="1920x1080"?"1K · 1920×1080":size==="2560x1440"?"2K · 2560×1440":size==="3840x2160"?"4K · 3840×2160":size}
|
||||
function sectionDescription(key:string){return key==="features"?"控制角色、素材和集成功能是否在工作台中启用。":key==="storage"?"规划图片、素材、缩略图、归档和临时文件的保存位置。":key==="database"?"选择本地 SQLite 或 PostgreSQL,并管理数据库连接参数。":key==="backup"?"导出脱敏配置备份,或从本地备份文件恢复设置。":"管理当前分区的运行配置。"}
|
||||
function sectionDescription(key:string){return key==="features"?"控制角色、素材和集成功能是否在工作台中启用。":key==="storage"?"规划图片、素材、缩略图、归档和临时文件的保存位置。":key==="database"?"选择本地 SQLite 或 PostgreSQL,并管理数据库连接参数。":key==="advanced_materials"?"限制进阶素材验证及自动回退产生的调用数量与估算费用。":key==="backup"?"导出脱敏配置备份,或从本地备份文件恢复设置。":"管理当前分区的运行配置。"}
|
||||
function configSymbol(key:string){return key==="storage"?"▣":key==="advanced_materials"?"进":"DB"}
|
||||
function configTitle(key:string){return key==="storage"?"存储目录规划":key==="advanced_materials"?"验证预算与计价":"数据库连接"}
|
||||
function configDescription(key:string){return key==="storage"?"支持相对插件数据目录或绝对路径,保存前请确保 AstrBot 进程具备写入权限。":key==="advanced_materials"?"关闭时不限制;开启后会在每次验证图请求及 NSFW 自动回退前进行预算预检。":"auto 会优先使用已配置的 PostgreSQL,否则回退到本地 SQLite。"}
|
||||
function featureDescription(key:string){return ({quick_profile:"从文本快速建立角色档案",workspace:"启用详细角色工作流与节点生成",material_library:"管理生成所需的参考素材",wardrobe:"为角色维护分类衣橱",api_discovery:"检测生成服务模型与分辨率能力",private_companion_integration:"与 AstrBot 私伴插件共享角色能力"} as Record<string,string>)[key]||"控制此功能模块是否可用"}
|
||||
async function exportBackup(){backupBusy.value=true;backupError.value="";backupMessage.value="";try{const data=await getJson<ExportSettingsResult>(endpoints.settingsExport);const blob=new Blob([JSON.stringify({format:"imagestudio-settings-backup",version:1,exported_at:new Date().toISOString(),...data},null,2)],{type:"application/json"});const url=URL.createObjectURL(blob);const link=document.createElement("a");link.href=url;link.download=`imagestudio-backup-${new Date().toISOString().slice(0,10)}.json`;link.click();URL.revokeObjectURL(url);backupMessage.value="脱敏配置备份已导出。"}catch(e){backupError.value=e instanceof Error?e.message:String(e)}finally{backupBusy.value=false}}
|
||||
async function selectImportFile(event:Event){backupError.value="";backupMessage.value="";const file=(event.target as HTMLInputElement).files?.[0];if(!file)return;try{const parsed=JSON.parse(await file.text()) as Record<string,unknown>;const config=obj(parsed.config??parsed);if(!Object.keys(config).length)throw new Error("备份文件中没有有效配置");importPreview.value=config;importFileName.value=file.name;backupMessage.value="文件读取成功,请确认后执行导入。"}catch(e){importPreview.value=null;importFileName.value="";backupError.value=e instanceof Error?e.message:"无法读取备份文件"}}
|
||||
|
|
@ -119,7 +123,7 @@ function inputValue(e:Event,type:string):ConfigValue{const t=e.target as HTMLInp
|
|||
<template><section class="settings-page">
|
||||
<header class="hero"><div><span>系统设置</span><h2>插件与生成服务</h2><p>集中管理 ImageStudio 的生成适配器、运行参数与功能配置。</p></div><div class="actions"><button @click="emit('refresh')">刷新配置</button><button :disabled="busy||!backendConnected||!canManage" @click="emit('reload')">重载配置</button></div></header>
|
||||
<section v-if="error||message" :class="['notice',{error:!!error}]">{{error||message}}</section>
|
||||
<section class="workspace"><aside class="nav"><header><span>设置导航</span><strong>{{effectiveSchema.sections.length}}</strong></header><button v-for="s in effectiveSchema.sections" :key="s.key" :class="{active:activeSection?.key===s.key}" @click="activeSectionKey=s.key"><span>{{s.title}}</span><small>{{s.key==='generation'?`${adapters.length} 个适配器`:s.key==='model_assignments'?'12 个功能节点':s.key==='recognition_prompts'?'分类识别要求':s.key==='prompt_governance'?'转译 / 审核 / 路由':s.key==='chat_access'?'群聊 / 用户':s.key==='backup'?'导入 / 导出':s.key==='review'?'规则管理':s.key==='capabilities'?'模型档案':s.key==='integration'?'凭据管理':s.key==='cost_guard'?'预算预检':s.key==='diagnostics'?'运行检查':`${s.fields.length} 项`}}</small></button></aside>
|
||||
<section class="workspace"><aside class="nav"><header><span>设置导航</span><strong>{{effectiveSchema.sections.length}}</strong></header><button v-for="s in effectiveSchema.sections" :key="s.key" :class="{active:activeSection?.key===s.key}" @click="activeSectionKey=s.key"><span>{{s.title}}</span><small>{{s.key==='generation'?`${adapters.length} 个适配器`:s.key==='model_assignments'?'按功能节点配置':s.key==='recognition_prompts'?'分类识别要求':s.key==='advanced_materials'?'验证预算 / 计价':s.key==='prompt_governance'?'转译 / 审核 / 路由':s.key==='chat_access'?'群聊 / 用户':s.key==='backup'?'导入 / 导出':s.key==='review'?'规则管理':s.key==='capabilities'?'模型档案':s.key==='integration'?'凭据管理':s.key==='cost_guard'?'预算预检':s.key==='diagnostics'?'运行检查':`${s.fields.length} 项`}}</small></button></aside>
|
||||
<main class="editor">
|
||||
<template v-if="activeSection?.key==='generation'"><header class="section-head"><div><span>生成服务</span><h3>API 适配器</h3><p>每个服务独立配置,展开卡片后填写 API 信息。首张卡片作为当前默认生成路由。</p></div></header>
|
||||
<div v-if="adapters.length" class="adapter-list"><article v-for="(a,index) in adapters" :key="a.id" class="adapter-card" :class="{connected:a.enabled&&!!a.base_url,disabled:!a.enabled}"><button class="adapter-summary" @click="expandedAdapter=a.id"><span class="adapter-icon">{{(a.name||'?').slice(0,1).toUpperCase()}}</span><span class="adapter-title"><strong>{{a.name||'未命名适配器'}}</strong><small>{{a.enabled?(a.base_url?'链接已配置':'等待配置'):'已停用'}}</small></span><em v-if="index===0">默认</em><span class="corner" aria-hidden="true"></span></button></article></div><div v-else class="adapter-empty"><strong>暂无生成适配器</strong><p>可以保持为空,或点击下方“添加适配器”重新配置。删除后保存配置即可永久生效。</p></div>
|
||||
|
|
@ -144,7 +148,7 @@ function inputValue(e:Event,type:string):ConfigValue{const t=e.target as HTMLInp
|
|||
<PromptGovernancePanel v-else-if="activeSection?.key==='prompt_governance'" :model-value="obj(draft.prompt_governance)" :adapters="modelAdapterOptions" :disabled="backendConnected&&!canManage" @update:model-value="updatePromptGovernance" />
|
||||
<ChatAccessPanel v-else-if="activeSection?.key==='chat_access'" :model-value="obj(draft.chat_access)" :disabled="backendConnected&&!canManage" @update:model-value="updateChatAccess" />
|
||||
<div v-else-if="activeSection?.key==='features'" class="feature-grid"><article v-for="f in activeSection.fields" :key="f.key" class="feature-card" :class="{enabled:fieldValue(fieldPath(f.key))===true}"><div class="feature-mark">{{f.label.slice(0,1)}}</div><div><strong>{{f.label}}</strong><p>{{featureDescription(f.key)}}</p></div><label class="toggle"><input type="checkbox" :checked="fieldValue(fieldPath(f.key))===true" @change="updateField(fieldPath(f.key),($event.target as HTMLInputElement).checked)"><span></span></label></article></div>
|
||||
<div v-else class="config-panel"><div class="config-banner"><div class="config-symbol">{{activeSection?.key==='storage'?'▣':'DB'}}</div><div><strong>{{activeSection?.key==='storage'?'存储目录规划':'数据库连接'}}</strong><p>{{activeSection?.key==='storage'?'支持相对插件数据目录或绝对路径,保存前请确保 AstrBot 进程具备写入权限。':'auto 会优先使用已配置的 PostgreSQL,否则回退到本地 SQLite。'}}</p></div></div><div class="fields"><label v-for="f in activeSection?.fields||[]" :key="f.key"><span>{{f.label}}</span><select v-if="f.options.length||f.type==='bool'||f.type==='boolean'" :value="String(fieldValue(fieldPath(f.key)))" @change="updateField(fieldPath(f.key),inputValue($event,f.type))"><option v-if="f.type==='bool'||f.type==='boolean'" value="true">开启</option><option v-if="f.type==='bool'||f.type==='boolean'" value="false">关闭</option><option v-for="o in f.options" :key="o" :value="o">{{o}}</option></select><input v-else :type="f.type==='number'?'number':f.sensitive?'password':'text'" :placeholder="f.type==='path'?'输入目录或文件路径':''" :value="fieldValue(fieldPath(f.key)) as any" @input="updateField(fieldPath(f.key),inputValue($event,f.type))"><small>{{fieldPath(f.key)}}</small></label></div><StorageMaintenance v-if="activeSection?.key==='storage'" :can-manage="canManage" :backend-connected="backendConnected" /></div>
|
||||
<div v-else class="config-panel"><div class="config-banner"><div class="config-symbol">{{configSymbol(activeSection?.key||'')}}</div><div><strong>{{configTitle(activeSection?.key||'')}}</strong><p>{{configDescription(activeSection?.key||'')}}</p></div></div><div class="fields"><label v-for="f in activeSection?.fields||[]" :key="f.key"><span>{{f.label}}</span><select v-if="f.options.length||f.type==='bool'||f.type==='boolean'" :value="String(fieldValue(fieldPath(f.key)))" @change="updateField(fieldPath(f.key),inputValue($event,f.type))"><option v-if="f.type==='bool'||f.type==='boolean'" value="true">开启</option><option v-if="f.type==='bool'||f.type==='boolean'" value="false">关闭</option><option v-for="o in f.options" :key="o" :value="o">{{o}}</option></select><input v-else :type="f.type==='number'?'number':f.sensitive?'password':'text'" :placeholder="f.type==='path'?'输入目录或文件路径':''" :value="fieldValue(fieldPath(f.key)) as any" @input="updateField(fieldPath(f.key),inputValue($event,f.type))"><small>{{fieldPath(f.key)}}</small></label></div><StorageMaintenance v-if="activeSection?.key==='storage'" :can-manage="canManage" :backend-connected="backendConnected" /></div>
|
||||
</template>
|
||||
<footer class="savebar"><span>{{dirty?'有未保存修改':'配置已同步'}}</span><div class="actions"><button v-if="activeSection?.key==='generation'" class="add-adapter-bottom" @click="addAdapter">+ 添加适配器</button><button :disabled="!dirty||busy" @click="resetDraft">撤销</button><button :disabled="busy||!backendConnected" @click="emit('validate',draft)">校验</button><button class="primary" :disabled="busy||!dirty||!backendConnected||!canManage" @click="emit('save',draft)">{{busy?'处理中…':'保存配置'}}</button></div></footer>
|
||||
</main>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
export const MODEL_ROUTE_CHAIN_PREFIX = "route-chain:"
|
||||
|
||||
export type ModelRouteChain = {
|
||||
plugin: string
|
||||
astrbot: string
|
||||
}
|
||||
|
||||
export function parseModelRouteChain(value: unknown): ModelRouteChain {
|
||||
const route = String(value ?? "").trim()
|
||||
if (!route) return { plugin: "", astrbot: "" }
|
||||
if (route.startsWith(MODEL_ROUTE_CHAIN_PREFIX)) {
|
||||
try {
|
||||
const parsed = JSON.parse(route.slice(MODEL_ROUTE_CHAIN_PREFIX.length)) as Record<string, unknown>
|
||||
return {
|
||||
plugin: String(parsed.plugin ?? "").trim(),
|
||||
astrbot: astrbotRoute(parsed.astrbot),
|
||||
}
|
||||
} catch {
|
||||
// Preserve malformed legacy input visibly in the plugin slot.
|
||||
}
|
||||
}
|
||||
return route.startsWith("astrbot:")
|
||||
? { plugin: "", astrbot: route }
|
||||
: { plugin: route, astrbot: "" }
|
||||
}
|
||||
|
||||
export function encodeModelRouteChain(chain: ModelRouteChain): string {
|
||||
const plugin = String(chain.plugin ?? "").trim()
|
||||
const astrbot = astrbotRoute(chain.astrbot)
|
||||
if (plugin && !astrbot) return plugin
|
||||
if (astrbot && !plugin) return astrbot
|
||||
if (!plugin && !astrbot) return ""
|
||||
return `${MODEL_ROUTE_CHAIN_PREFIX}${JSON.stringify({ astrbot, plugin })}`
|
||||
}
|
||||
|
||||
function astrbotRoute(value: unknown): string {
|
||||
const route = String(value ?? "").trim()
|
||||
return !route || route.startsWith("astrbot:") ? route : ""
|
||||
}
|
||||
|
|
@ -78,7 +78,9 @@ const visibleMaterials = computed(() => {
|
|||
})
|
||||
.sort((left, right) => {
|
||||
const selectedDifference = Number(selectedSet.value.has(right.material_id)) - Number(selectedSet.value.has(left.material_id))
|
||||
return selectedDifference || right.updated_at.localeCompare(left.updated_at)
|
||||
const advancedDifference = Number(isAdvancedMaterial(right)) - Number(isAdvancedMaterial(left))
|
||||
const priorityDifference = materialPriority(right) - materialPriority(left)
|
||||
return selectedDifference || advancedDifference || priorityDifference || right.updated_at.localeCompare(left.updated_at)
|
||||
})
|
||||
})
|
||||
const dirty = computed(() => normalizedIds(draftSelectedIds.value).join("|") !== normalizedIds(props.selectedMaterialIds).join("|"))
|
||||
|
|
@ -123,6 +125,14 @@ function materialImage(material: Material) {
|
|||
function categoryLabel(material: Material) {
|
||||
return material.category_id ? categoryNameById.value.get(material.category_id) || "未分类" : "未分类"
|
||||
}
|
||||
|
||||
function isAdvancedMaterial(material: Material) {
|
||||
return material.metadata?.advanced_material === true || material.metadata?.material_type === "advanced"
|
||||
}
|
||||
|
||||
function materialPriority(material: Material) {
|
||||
return Number(material.metadata?.catalog_priority || 0)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
@ -210,7 +220,7 @@ function categoryLabel(material: Material) {
|
|||
<b v-if="selectedSet.has(material.material_id)">已选</b>
|
||||
</span>
|
||||
<span class="workflow-material-drawer-copy">
|
||||
<small>{{ categoryLabel(material) }}</small>
|
||||
<small>{{ isAdvancedMaterial(material) ? "进阶优先 · " : "" }}{{ categoryLabel(material) }}</small>
|
||||
<strong>{{ material.display_name }}</strong>
|
||||
<em>{{ material.prompt || "已确认,可作为图片参考" }}</em>
|
||||
</span>
|
||||
|
|
|
|||
|
|
@ -26,3 +26,20 @@ export function materialBodySlotLabel(value: unknown) {
|
|||
const key = String(value ?? "").trim()
|
||||
return MATERIAL_BODY_SLOT_LABELS.get(key) ?? key
|
||||
}
|
||||
|
||||
const MATERIAL_CATEGORY_LABELS = new Map<string, string>([
|
||||
["top", "上装"],
|
||||
["bottom", "下装"],
|
||||
["dress", "连衣裙"],
|
||||
["shoes", "鞋履"],
|
||||
["accessory", "配饰"],
|
||||
["style", "风格"],
|
||||
["composition", "构图"],
|
||||
["outfit", "服装"],
|
||||
])
|
||||
|
||||
/** Keep backend category names stable while presenting built-in labels in Chinese. */
|
||||
export function materialCategoryLabel(value: unknown) {
|
||||
const key = String(value ?? "").trim()
|
||||
return MATERIAL_CATEGORY_LABELS.get(key.toLowerCase()) ?? key
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,122 @@
|
|||
import assert from "node:assert/strict"
|
||||
import { readFile } from "node:fs/promises"
|
||||
import test from "node:test"
|
||||
|
||||
const pagePath = new URL(
|
||||
"../standalone-web/src/features/materials/AdvancedMaterialImportPage.vue",
|
||||
import.meta.url,
|
||||
)
|
||||
const typesPath = new URL("../shared/types/advanced-material.ts", import.meta.url)
|
||||
const endpointsPath = new URL("../shared/api/endpoints.ts", import.meta.url)
|
||||
const appPath = new URL("../standalone-web/src/App.vue", import.meta.url)
|
||||
|
||||
test("omits an unknown upload MIME type instead of claiming the image is PNG", async () => {
|
||||
const [page, types] = await Promise.all([
|
||||
readFile(pagePath, "utf8"),
|
||||
readFile(typesPath, "utf8"),
|
||||
])
|
||||
|
||||
assert.match(types, /content_type\?: string/)
|
||||
assert.match(types, /fidelity_score\?: number \| null/)
|
||||
assert.match(page, /const declaredContentType = uploadFile\.value\.type\.trim\(\)/)
|
||||
assert.match(
|
||||
page,
|
||||
/\.\.\.\(declaredContentType \? \{ content_type: declaredContentType \} : \{\}\)/,
|
||||
)
|
||||
assert.doesNotMatch(
|
||||
page,
|
||||
/content_type:\s*uploadFile\.value\.type\s*\|\|\s*["']image\/png["']/,
|
||||
)
|
||||
})
|
||||
|
||||
test("enables 2-8 image style sets without replacing the legacy single-image upload", async () => {
|
||||
const [page, types, endpoints] = await Promise.all([
|
||||
readFile(pagePath, "utf8"),
|
||||
readFile(typesPath, "utf8"),
|
||||
readFile(endpointsPath, "utf8"),
|
||||
])
|
||||
|
||||
assert.match(page, /const MIN_STYLE_IMAGES = 2/)
|
||||
assert.match(page, /const MAX_STYLE_IMAGES = 8/)
|
||||
assert.match(page, /styleSetUploadEnabled/)
|
||||
assert.match(page, /endpoints\.advancedMaterialStyleSetUpload/)
|
||||
assert.match(page, /endpoints\.advancedMaterialImportUpload/)
|
||||
assert.match(page, /Promise\.all\(accepted\.map/)
|
||||
assert.match(page, /0 张参考图/)
|
||||
assert.match(page, /styleValidationCases\.value\.length === 3/)
|
||||
assert.match(types, /interface AdvancedStyleSource/)
|
||||
assert.match(types, /interface AdvancedStyleConsensus/)
|
||||
assert.match(types, /interface AdvancedStyleValidationCase/)
|
||||
assert.match(types, /images: AdvancedStyleSetUploadImage\[\]/)
|
||||
assert.match(endpoints, /advancedMaterialStyleSetUpload: "advanced-material-imports\/style-set\/upload"/)
|
||||
assert.match(endpoints, /style-validation-cases\/\$\{caseId\}\/retry/)
|
||||
})
|
||||
|
||||
test("allows an unsealed import to be deleted after its queue row is removed", async () => {
|
||||
const [page, endpoints] = await Promise.all([
|
||||
readFile(pagePath, "utf8"),
|
||||
readFile(endpointsPath, "utf8"),
|
||||
])
|
||||
|
||||
assert.match(endpoints, /advancedMaterialImportDelete/)
|
||||
assert.match(page, /deleteJson<Record<string, unknown>>/)
|
||||
assert.match(page, /确认删除导入/)
|
||||
assert.match(page, /!selectedImport\.accepted_material_id/)
|
||||
})
|
||||
|
||||
test("keeps queue cards at their natural height instead of compressing grid rows", async () => {
|
||||
const app = await readFile(appPath, "utf8")
|
||||
|
||||
assert.match(app, /\.queue-task-list\s*\{[^}]*display:\s*flex;[^}]*flex-direction:\s*column;/s)
|
||||
assert.match(app, /\.queue-task-card\s*\{[^}]*flex:\s*0 0 auto;[^}]*min-height:\s*88px;/s)
|
||||
})
|
||||
|
||||
test("polls only active import state and keeps storage-backed media URLs stable", async () => {
|
||||
const page = await readFile(pagePath, "utf8")
|
||||
|
||||
assert.match(page, /const pollBusy = ref\(false\)/)
|
||||
assert.match(page, /void pollActiveState\(\)/)
|
||||
assert.doesNotMatch(page, /setInterval\([\s\S]{0,500}void refreshAll\(true\)/)
|
||||
assert.match(page, /if \(importId && selectedWasBusy\)/)
|
||||
assert.match(page, /if \(!pairedUri \|\| !pairedUri\.startsWith\("storage:\/\/"\)\) signedUrls\.add\(value\)/)
|
||||
assert.match(page, /mediaUrl\(item\.source_thumbnail_uri, item\.source_image_uri/)
|
||||
})
|
||||
|
||||
test("routes style corrections and validation-content instructions separately", async () => {
|
||||
const [page, types] = await Promise.all([
|
||||
readFile(pagePath, "utf8"),
|
||||
readFile(typesPath, "utf8"),
|
||||
])
|
||||
|
||||
assert.match(types, /style_adjustment_text\?: string/)
|
||||
assert.match(types, /validation_instruction\?: string/)
|
||||
assert.match(page, /const validationInstructionText = ref\(""\)/)
|
||||
assert.match(page, /style_adjustment_text: selectedStyleSet\.value \? adjustment/)
|
||||
assert.match(page, /validation_instruction: selectedStyleSet\.value \? validationInstruction/)
|
||||
assert.match(page, /下一轮验证内容(选填)/)
|
||||
assert.match(page, /已应用文字调整/)
|
||||
assert.match(page, /应用文字调整并生成下一轮/)
|
||||
})
|
||||
|
||||
test("recognizes canonical advanced style sets and renders all three validation images", async () => {
|
||||
const page = await readFile(pagePath, "utf8")
|
||||
|
||||
assert.match(page, /workflowKind === "style_set" \|\| workflowKind === "advanced_style_set"/)
|
||||
assert.match(page, /attempt\.validation_cases\?\.length/)
|
||||
assert.match(page, /v-for="validationCase in attempt\.validation_cases"/)
|
||||
assert.match(page, /class="advanced-attempt-history-images"/)
|
||||
})
|
||||
|
||||
test("opens advanced-material thumbnails in the shared safe image preview dialog", async () => {
|
||||
const page = await readFile(pagePath, "utf8")
|
||||
|
||||
assert.match(page, /import ImagePreviewDialog from "\.\.\/\.\.\/components\/ImagePreviewDialog\.vue"/)
|
||||
assert.match(page, /const previewImageSrc = ref\(""\)/)
|
||||
assert.match(page, /function validationCasePreviewUrl/)
|
||||
assert.match(page, /@click="openImagePreview\(validationCasePreviewUrl\(validationCase\)/)
|
||||
assert.match(page, /<ImagePreviewDialog/)
|
||||
assert.match(page, /@close="closeImagePreview"/)
|
||||
assert.match(page, /function styleValidationFidelityPercent/)
|
||||
assert.match(page, /function styleValidationReviewSummary/)
|
||||
assert.match(page, /title="点击查看大图"/)
|
||||
})
|
||||
111
main.py
111
main.py
|
|
@ -125,6 +125,17 @@ _RUNTIME_PATH_TO_ASTRBOT_KEY = {
|
|||
"vision.provider.timeout_seconds": "vision_provider_timeout_seconds",
|
||||
"vision.provider.enabled": "vision_provider_enabled",
|
||||
"model_assignments.material_recognition": "model_material_recognition",
|
||||
"model_assignments.advanced_material_recognition": "model_advanced_material_recognition",
|
||||
"model_assignments.advanced_material_validation_generation": "model_advanced_material_validation_generation",
|
||||
"advanced_materials.cost_budget.enabled": "advanced_material_budget_enabled",
|
||||
"advanced_materials.cost_budget.days": "advanced_material_budget_days",
|
||||
"advanced_materials.cost_budget.max_task_count": "advanced_material_budget_max_task_count",
|
||||
"advanced_materials.cost_budget.max_image_count": "advanced_material_budget_max_image_count",
|
||||
"advanced_materials.cost_budget.max_estimated_cost": "advanced_material_budget_max_estimated_cost",
|
||||
"advanced_materials.cost_budget.price_key": "advanced_material_budget_price_key",
|
||||
"advanced_materials.cost_budget.per_request": "advanced_material_budget_per_request",
|
||||
"advanced_materials.cost_budget.per_image": "advanced_material_budget_per_image",
|
||||
"advanced_materials.cost_budget.per_1k_tokens": "advanced_material_budget_per_1k_tokens",
|
||||
"model_assignments.profile_parse": "model_profile_parse",
|
||||
"model_assignments.persona_split": "model_persona_split",
|
||||
"model_assignments.prompt_planning": "model_prompt_planning",
|
||||
|
|
@ -150,6 +161,7 @@ _RUNTIME_PATH_TO_ASTRBOT_KEY = {
|
|||
"chat_access.allow_2k": "chat_allow_2k",
|
||||
"chat_access.allow_4k": "chat_allow_4k",
|
||||
"features.private_companion_integration": "enable_private_companion_integration",
|
||||
"storage.advanced_materials_path": "advanced_materials_path",
|
||||
"database.backend": "database_backend",
|
||||
"database.sqlite.path": "sqlite_path",
|
||||
"database.postgresql.enabled": "postgresql_enabled",
|
||||
|
|
@ -361,6 +373,8 @@ class Main(AstrBotStar):
|
|||
self.chat_background_tasks: set[asyncio.Task[Any]] = set()
|
||||
self.round_worker_runtime: Any | None = None
|
||||
self.round_worker_result: dict[str, Any] | None = None
|
||||
self.advanced_material_worker_runtime: Any | None = None
|
||||
self.advanced_material_worker_result: dict[str, Any] | None = None
|
||||
self.runtime_loop: asyncio.AbstractEventLoop | None = None
|
||||
|
||||
async def initialize(self) -> None:
|
||||
|
|
@ -393,11 +407,32 @@ class Main(AstrBotStar):
|
|||
"error_type": type(exc).__name__,
|
||||
"message": " ".join(str(exc).split())[:500],
|
||||
}
|
||||
try:
|
||||
from backend.services.advanced_material_worker_runtime import (
|
||||
create_advanced_material_worker_runtime,
|
||||
)
|
||||
|
||||
self.advanced_material_worker_runtime = create_advanced_material_worker_runtime()
|
||||
self.advanced_material_worker_result = self.advanced_material_worker_runtime.start()
|
||||
except Exception as exc:
|
||||
# Keep the normal material library and recovery UI usable when the
|
||||
# optional advanced-material worker cannot start. Its persisted
|
||||
# imports/tasks remain available for retry after configuration is
|
||||
# corrected or the plugin is reloaded.
|
||||
self.advanced_material_worker_runtime = None
|
||||
self.advanced_material_worker_result = {
|
||||
"running": False,
|
||||
"error_type": type(exc).__name__,
|
||||
"message": " ".join(str(exc).split())[:500],
|
||||
}
|
||||
if self.context is not None:
|
||||
self.registration_result = register_plugin(self.context)
|
||||
return None
|
||||
|
||||
async def terminate(self) -> None:
|
||||
if self.advanced_material_worker_runtime is not None:
|
||||
self.advanced_material_worker_result = self.advanced_material_worker_runtime.stop()
|
||||
self.advanced_material_worker_runtime = None
|
||||
if self.round_worker_runtime is not None:
|
||||
self.round_worker_result = self.round_worker_runtime.stop()
|
||||
self.round_worker_runtime = None
|
||||
|
|
@ -1761,6 +1796,17 @@ def _mapping_from_runtime_config(config: Any | None) -> dict[str, Any]:
|
|||
"vision_provider_timeout_seconds",
|
||||
"vision_provider_enabled",
|
||||
"model_material_recognition",
|
||||
"model_advanced_material_recognition",
|
||||
"model_advanced_material_validation_generation",
|
||||
"advanced_material_budget_enabled",
|
||||
"advanced_material_budget_days",
|
||||
"advanced_material_budget_max_task_count",
|
||||
"advanced_material_budget_max_image_count",
|
||||
"advanced_material_budget_max_estimated_cost",
|
||||
"advanced_material_budget_price_key",
|
||||
"advanced_material_budget_per_request",
|
||||
"advanced_material_budget_per_image",
|
||||
"advanced_material_budget_per_1k_tokens",
|
||||
"model_profile_parse",
|
||||
"model_persona_split",
|
||||
"model_prompt_planning",
|
||||
|
|
@ -1786,6 +1832,7 @@ def _mapping_from_runtime_config(config: Any | None) -> dict[str, Any]:
|
|||
"chat_allow_2k",
|
||||
"chat_allow_4k",
|
||||
"enable_private_companion_integration",
|
||||
"advanced_materials_path",
|
||||
"database_backend",
|
||||
"sqlite_path",
|
||||
"postgresql_enabled",
|
||||
|
|
@ -1885,6 +1932,7 @@ def _runtime_config_patch_from_mapping(mapping: Mapping[str, Any]) -> dict[str,
|
|||
database = _mapping_from_any(mapping.get("database"))
|
||||
sqlite = _mapping_from_any(database.get("sqlite")) if database else {}
|
||||
postgresql = _mapping_from_any(database.get("postgresql")) if database else {}
|
||||
storage = _mapping_from_any(mapping.get("storage"))
|
||||
features = _mapping_from_any(mapping.get("features"))
|
||||
plugin = _mapping_from_any(mapping.get("plugin"))
|
||||
vision = _mapping_from_any(mapping.get("vision"))
|
||||
|
|
@ -1986,6 +2034,69 @@ def _runtime_config_patch_from_mapping(mapping: Mapping[str, Any]) -> dict[str,
|
|||
if value:
|
||||
_set_nested(patch, ("model_assignments", target_key), value)
|
||||
|
||||
# Advanced-material assignments deliberately preserve an empty value so
|
||||
# administrators can clear a dedicated route and return to its documented
|
||||
# fallback without a stale AstrBot runtime override restoring it.
|
||||
for target_key, source_key in {
|
||||
"advanced_material_recognition": "model_advanced_material_recognition",
|
||||
"advanced_material_validation_generation": "model_advanced_material_validation_generation",
|
||||
}.items():
|
||||
value = _runtime_value(mapping, source_key)
|
||||
if value is not _MISSING:
|
||||
_set_nested(patch, ("model_assignments", target_key), _runtime_text(value))
|
||||
|
||||
advanced_materials_path = _runtime_text(
|
||||
_runtime_value(
|
||||
mapping,
|
||||
"advanced_materials_path",
|
||||
fallback=storage.get("advanced_materials_path", _MISSING),
|
||||
)
|
||||
)
|
||||
if advanced_materials_path:
|
||||
_set_nested(patch, ("storage", "advanced_materials_path"), advanced_materials_path)
|
||||
|
||||
advanced_materials = mapping.get("advanced_materials") if isinstance(mapping.get("advanced_materials"), Mapping) else {}
|
||||
current_budget = advanced_materials.get("cost_budget") if isinstance(advanced_materials.get("cost_budget"), Mapping) else {}
|
||||
budget_enabled = _runtime_value(
|
||||
mapping,
|
||||
"advanced_material_budget_enabled",
|
||||
fallback=current_budget.get("enabled", _MISSING),
|
||||
)
|
||||
if budget_enabled is not _MISSING:
|
||||
_set_nested(patch, ("advanced_materials", "cost_budget", "enabled"), _runtime_bool(budget_enabled))
|
||||
for target_key, source_key, converter in (
|
||||
("days", "advanced_material_budget_days", int),
|
||||
("max_task_count", "advanced_material_budget_max_task_count", int),
|
||||
("max_image_count", "advanced_material_budget_max_image_count", int),
|
||||
("max_estimated_cost", "advanced_material_budget_max_estimated_cost", float),
|
||||
("per_request", "advanced_material_budget_per_request", float),
|
||||
("per_image", "advanced_material_budget_per_image", float),
|
||||
("per_1k_tokens", "advanced_material_budget_per_1k_tokens", float),
|
||||
):
|
||||
raw_value = _runtime_value(mapping, source_key, fallback=current_budget.get(target_key, _MISSING))
|
||||
if raw_value is _MISSING:
|
||||
continue
|
||||
text_value = _runtime_text(raw_value)
|
||||
if text_value == "" and target_key in {"max_task_count", "max_image_count", "max_estimated_cost"}:
|
||||
_set_nested(patch, ("advanced_materials", "cost_budget", target_key), None)
|
||||
continue
|
||||
try:
|
||||
normalized_value = converter(text_value or 0)
|
||||
except (TypeError, ValueError):
|
||||
continue
|
||||
_set_nested(patch, ("advanced_materials", "cost_budget", target_key), normalized_value)
|
||||
budget_price_key = _runtime_value(
|
||||
mapping,
|
||||
"advanced_material_budget_price_key",
|
||||
fallback=current_budget.get("price_key", _MISSING),
|
||||
)
|
||||
if budget_price_key is not _MISSING:
|
||||
_set_nested(
|
||||
patch,
|
||||
("advanced_materials", "cost_budget", "price_key"),
|
||||
_runtime_text(budget_price_key),
|
||||
)
|
||||
|
||||
# Multi-route assignments are first-class AstrBot plugin settings. Keep
|
||||
# empty lists as explicit values so clearing a selection in the Web UI is
|
||||
# persisted back to AstrBot instead of being restored from a stale runtime
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ name: astrbot_plugin_imagestudio
|
|||
display_name: ImageStudio
|
||||
desc: AstrBot >=4.25 ImageStudio role asset and image-generation workspace for Windows, Linux, macOS and Docker.
|
||||
short_desc: AstrBot 内的角色资产、工作流与图片生成管理工作台。
|
||||
version: 1.0.20
|
||||
version: 1.0.32
|
||||
author: YokiiroBW
|
||||
repo: "http://192.168.31.210:18085/YokiiroBW/astrbot_plugin_imagestudio"
|
||||
astrbot_version: ">=4.25.0,<5"
|
||||
|
|
|
|||
文件差异因一行或多行过长而隐藏
文件差异因一行或多行过长而隐藏
文件差异因一行或多行过长而隐藏
文件差异因一行或多行过长而隐藏
|
|
@ -4,8 +4,8 @@
|
|||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>AstrBot ImageStudio</title>
|
||||
<script type="module" crossorigin src="./assets/index-DLwA6M_k.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="./assets/index-BANum4Rx.css">
|
||||
<script type="module" crossorigin src="./assets/index-Dmf_ucVG.js"></script>
|
||||
<link rel="stylesheet" crossorigin href="./assets/index-1rjaQ0Bx.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
|
|
|
|||
二进制文件未显示。
二进制文件未显示。
二进制文件未显示。
二进制文件未显示。
二进制文件未显示。
二进制文件未显示。
二进制文件未显示。
二进制文件未显示。
二进制文件未显示。
二进制文件未显示。
二进制文件未显示。
|
|
@ -0,0 +1,548 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
from backend.services.cost_service import CostService
|
||||
from backend.services.data_backup_service import (
|
||||
PROMPT_BACKUP_PLACEHOLDER,
|
||||
_sanitize_sqlite_backup,
|
||||
)
|
||||
from backend.services.material_analytics_service import MaterialAnalyticsService
|
||||
from backend.services.orphan_media_service import OrphanMediaService
|
||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||
|
||||
|
||||
def _database(path: Path) -> Database:
|
||||
return Database(
|
||||
DatabaseSettings(
|
||||
backend="sqlite",
|
||||
sqlite=SqliteSettings(path=str(path)),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def test_prompt_redaction_preserves_not_null_advanced_material_columns(tmp_path: Path) -> None:
|
||||
path = tmp_path / "backup-redaction.sqlite3"
|
||||
with sqlite3.connect(path) as connection:
|
||||
connection.executescript(
|
||||
"""
|
||||
CREATE TABLE advanced_material_imports (
|
||||
import_id TEXT PRIMARY KEY,
|
||||
recognition_prompt TEXT,
|
||||
recognition_negative_prompt TEXT,
|
||||
recognition_result_json TEXT,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT
|
||||
);
|
||||
CREATE TABLE advanced_material_validation_attempts (
|
||||
attempt_id TEXT PRIMARY KEY,
|
||||
prompt TEXT NOT NULL,
|
||||
negative_prompt TEXT,
|
||||
adjustment_text TEXT,
|
||||
decision_notes TEXT,
|
||||
usage_json TEXT,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT
|
||||
);
|
||||
CREATE TABLE advanced_materials (
|
||||
material_id TEXT PRIMARY KEY,
|
||||
prompt TEXT NOT NULL,
|
||||
negative_prompt TEXT,
|
||||
capsule_json TEXT,
|
||||
metadata_json TEXT
|
||||
);
|
||||
CREATE TABLE advanced_style_sources (
|
||||
source_id TEXT PRIMARY KEY,
|
||||
recognition_prompt TEXT,
|
||||
recognition_negative_prompt TEXT,
|
||||
recognition_tags_json TEXT,
|
||||
recognition_result_json TEXT,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT
|
||||
);
|
||||
CREATE TABLE advanced_style_consensus_runs (
|
||||
consensus_id TEXT PRIMARY KEY,
|
||||
prompt TEXT,
|
||||
negative_prompt TEXT,
|
||||
source_ids_json TEXT,
|
||||
tags_json TEXT,
|
||||
result_json TEXT,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT
|
||||
);
|
||||
CREATE TABLE advanced_style_validation_cases (
|
||||
case_id TEXT PRIMARY KEY,
|
||||
content_prompt TEXT NOT NULL,
|
||||
final_prompt TEXT NOT NULL,
|
||||
negative_prompt TEXT,
|
||||
usage_json TEXT,
|
||||
style_review_json TEXT,
|
||||
error_json TEXT,
|
||||
metadata_json TEXT
|
||||
);
|
||||
"""
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_material_imports VALUES (?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"import-1",
|
||||
"recognition prompt",
|
||||
"recognition negative",
|
||||
json.dumps({"prompt": "nested recognition prompt"}),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_material_validation_attempts VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"attempt-1",
|
||||
"validation prompt",
|
||||
"validation negative",
|
||||
"make it redder",
|
||||
"review notes",
|
||||
None,
|
||||
None,
|
||||
json.dumps({"expanded_prompt": "nested validation prompt"}),
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_materials VALUES (?, ?, ?, ?, ?)",
|
||||
(
|
||||
"advmat-1",
|
||||
"sealed prompt",
|
||||
"sealed negative",
|
||||
json.dumps({"prompt": "nested sealed prompt"}),
|
||||
None,
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_style_sources VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"source-1",
|
||||
"source recognition",
|
||||
"source negative",
|
||||
json.dumps(["watercolor"]),
|
||||
json.dumps({"prompt": "nested source prompt"}),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_style_consensus_runs VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"consensus-1",
|
||||
"consensus prompt",
|
||||
"consensus negative",
|
||||
json.dumps(["source-1"]),
|
||||
json.dumps(["watercolor"]),
|
||||
json.dumps({"prompt": "nested consensus prompt"}),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_style_validation_cases VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"case-1",
|
||||
"new character",
|
||||
"watercolor, new character",
|
||||
"photorealistic",
|
||||
None,
|
||||
json.dumps({"expanded_prompt": "reviewed prompt"}),
|
||||
None,
|
||||
None,
|
||||
),
|
||||
)
|
||||
connection.commit()
|
||||
|
||||
_sanitize_sqlite_backup(
|
||||
path,
|
||||
include_prompt_records=False,
|
||||
include_moderation_records=True,
|
||||
)
|
||||
|
||||
with sqlite3.connect(path) as connection:
|
||||
import_row = connection.execute(
|
||||
"SELECT recognition_prompt, recognition_negative_prompt, recognition_result_json "
|
||||
"FROM advanced_material_imports"
|
||||
).fetchone()
|
||||
attempt_row = connection.execute(
|
||||
"SELECT prompt, negative_prompt, adjustment_text, decision_notes, metadata_json "
|
||||
"FROM advanced_material_validation_attempts"
|
||||
).fetchone()
|
||||
material_row = connection.execute(
|
||||
"SELECT prompt, negative_prompt, capsule_json FROM advanced_materials"
|
||||
).fetchone()
|
||||
source_row = connection.execute(
|
||||
"SELECT recognition_prompt, recognition_negative_prompt, recognition_result_json "
|
||||
"FROM advanced_style_sources"
|
||||
).fetchone()
|
||||
consensus_row = connection.execute(
|
||||
"SELECT prompt, negative_prompt, result_json FROM advanced_style_consensus_runs"
|
||||
).fetchone()
|
||||
case_row = connection.execute(
|
||||
"SELECT content_prompt, final_prompt, negative_prompt, style_review_json "
|
||||
"FROM advanced_style_validation_cases"
|
||||
).fetchone()
|
||||
|
||||
assert import_row[:2] == (None, None)
|
||||
assert json.loads(import_row[2])["prompt"] == PROMPT_BACKUP_PLACEHOLDER
|
||||
assert attempt_row[:4] == (PROMPT_BACKUP_PLACEHOLDER, None, None, None)
|
||||
assert json.loads(attempt_row[4])["expanded_prompt"] == PROMPT_BACKUP_PLACEHOLDER
|
||||
assert material_row[:2] == (PROMPT_BACKUP_PLACEHOLDER, None)
|
||||
assert json.loads(material_row[2])["prompt"] == PROMPT_BACKUP_PLACEHOLDER
|
||||
assert source_row[:2] == (None, None)
|
||||
assert json.loads(source_row[2])["prompt"] == PROMPT_BACKUP_PLACEHOLDER
|
||||
assert consensus_row[:2] == (None, None)
|
||||
assert json.loads(consensus_row[2])["prompt"] == PROMPT_BACKUP_PLACEHOLDER
|
||||
assert case_row[:3] == (
|
||||
PROMPT_BACKUP_PLACEHOLDER,
|
||||
PROMPT_BACKUP_PLACEHOLDER,
|
||||
None,
|
||||
)
|
||||
assert json.loads(case_row[3])["expanded_prompt"] == PROMPT_BACKUP_PLACEHOLDER
|
||||
|
||||
|
||||
def test_cost_summary_counts_failed_or_cancelled_attempts_with_billable_evidence(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
path = tmp_path / "advanced-material-cost.sqlite3"
|
||||
database = _database(path)
|
||||
database.initialize()
|
||||
|
||||
with database.connect() as connection:
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO advanced_material_imports (
|
||||
import_id, display_name, source_image_uri, status
|
||||
) VALUES (?, ?, ?, ?)
|
||||
""",
|
||||
("import-cost", "Cost material", "storage://advanced_materials/source.png", "failed"),
|
||||
)
|
||||
attempts = (
|
||||
(
|
||||
"attempt-failed-usage",
|
||||
1,
|
||||
"failed",
|
||||
json.dumps({"total_tokens": 125, "input_tokens": 75, "output_tokens": 50}),
|
||||
None,
|
||||
),
|
||||
(
|
||||
"attempt-cancelled-image",
|
||||
2,
|
||||
"cancelled",
|
||||
None,
|
||||
"storage://advanced_materials/attempts/cancelled.png",
|
||||
),
|
||||
("attempt-failed-empty", 3, "failed", None, None),
|
||||
)
|
||||
for attempt_id, number, status, usage_json, image_uri in attempts:
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO advanced_material_validation_attempts (
|
||||
attempt_id, import_id, attempt_number, status, prompt,
|
||||
model_route, provider_id, provider_model, usage_json,
|
||||
validation_image_uri
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
attempt_id,
|
||||
"import-cost",
|
||||
number,
|
||||
status,
|
||||
"validation prompt",
|
||||
"advanced-route",
|
||||
"provider-a",
|
||||
"model-a",
|
||||
usage_json,
|
||||
image_uri,
|
||||
),
|
||||
)
|
||||
connection.commit()
|
||||
|
||||
summary = CostService(
|
||||
database,
|
||||
unit_prices={
|
||||
"provider-a:model-a": {
|
||||
"per_request": 1,
|
||||
"per_image": 0.5,
|
||||
"per_1k_tokens": 2,
|
||||
}
|
||||
},
|
||||
).usage_summary()
|
||||
|
||||
assert summary["totals"] == {
|
||||
"image_count": 1,
|
||||
"task_count": 2,
|
||||
"total_tokens": 125,
|
||||
"input_tokens": 75,
|
||||
"output_tokens": 50,
|
||||
"estimated_cost": 2.75,
|
||||
}
|
||||
assert len(summary["groups"]) == 1
|
||||
|
||||
|
||||
def test_style_validation_cost_uses_cases_without_charging_attempt_projection_twice(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
path = tmp_path / "advanced-style-cost.sqlite3"
|
||||
database = _database(path)
|
||||
database.initialize()
|
||||
|
||||
with database.connect() as connection:
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO advanced_material_imports (
|
||||
import_id, display_name, source_image_uri, status, metadata_json
|
||||
) VALUES (?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
"style-cost-import",
|
||||
"Style cost",
|
||||
"storage://advanced_materials/style-cost/source.png",
|
||||
"awaiting_review",
|
||||
json.dumps({"workflow_kind": "style_set"}),
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO advanced_material_validation_attempts (
|
||||
attempt_id, import_id, attempt_number, status, prompt,
|
||||
model_route, provider_id, provider_model, usage_json,
|
||||
validation_image_uri
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
"style-cost-attempt",
|
||||
"style-cost-import",
|
||||
1,
|
||||
"awaiting_review",
|
||||
"watercolor",
|
||||
"style-route",
|
||||
"provider-style",
|
||||
"model-style",
|
||||
json.dumps({"total_tokens": 999}),
|
||||
"storage://advanced_materials/style-cost/portrait.png",
|
||||
),
|
||||
)
|
||||
for index, case_key in enumerate(("portrait", "fullbody", "scene")):
|
||||
connection.execute(
|
||||
"""
|
||||
INSERT INTO advanced_style_validation_cases (
|
||||
case_id, attempt_id, import_id, case_key, sort_order,
|
||||
status, content_prompt, final_prompt, model_route,
|
||||
provider_id, provider_model, validation_image_uri, usage_json
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
f"style-cost-{case_key}",
|
||||
"style-cost-attempt",
|
||||
"style-cost-import",
|
||||
case_key,
|
||||
index,
|
||||
"succeeded",
|
||||
case_key,
|
||||
f"watercolor, {case_key}",
|
||||
"style-route",
|
||||
"provider-style",
|
||||
"model-style",
|
||||
f"storage://advanced_materials/style-cost/{case_key}.png",
|
||||
json.dumps({"total_tokens": 20, "input_tokens": 12, "output_tokens": 8}),
|
||||
),
|
||||
)
|
||||
connection.commit()
|
||||
|
||||
summary = CostService(
|
||||
database,
|
||||
unit_prices={
|
||||
"provider-style:model-style": {
|
||||
"per_request": 1,
|
||||
"per_image": 0.5,
|
||||
"per_1k_tokens": 2,
|
||||
}
|
||||
},
|
||||
).usage_summary()
|
||||
|
||||
assert summary["totals"] == {
|
||||
"image_count": 3,
|
||||
"task_count": 3,
|
||||
"total_tokens": 60,
|
||||
"input_tokens": 36,
|
||||
"output_tokens": 24,
|
||||
"estimated_cost": 4.62,
|
||||
}
|
||||
|
||||
|
||||
def test_orphan_scan_keeps_style_sources_and_validation_cases_referenced(tmp_path: Path) -> None:
|
||||
path = tmp_path / "advanced-style-orphans.sqlite3"
|
||||
database = _database(path)
|
||||
database.initialize()
|
||||
source_uri = "storage://advanced_materials/style-orphan/source.png"
|
||||
validation_uri = "storage://advanced_materials/style-orphan/validation.png"
|
||||
with database.connect() as connection:
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_material_imports "
|
||||
"(import_id, display_name, source_image_uri, status) VALUES (?, ?, ?, ?)",
|
||||
("style-orphan-import", "Style orphan", source_uri, "validating"),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_material_validation_attempts "
|
||||
"(attempt_id, import_id, attempt_number, status, prompt) VALUES (?, ?, ?, ?, ?)",
|
||||
("style-orphan-attempt", "style-orphan-import", 1, "running", "watercolor"),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_style_sources "
|
||||
"(source_id, import_id, sort_order, is_primary, source_image_uri, status) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?)",
|
||||
("style-orphan-source", "style-orphan-import", 0, 1, source_uri, "succeeded"),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_style_validation_cases "
|
||||
"(case_id, attempt_id, import_id, case_key, sort_order, status, "
|
||||
"content_prompt, final_prompt, validation_image_uri) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"style-orphan-case",
|
||||
"style-orphan-attempt",
|
||||
"style-orphan-import",
|
||||
"portrait",
|
||||
0,
|
||||
"succeeded",
|
||||
"portrait",
|
||||
"watercolor portrait",
|
||||
validation_uri,
|
||||
),
|
||||
)
|
||||
connection.commit()
|
||||
|
||||
class FakeMediaIndex:
|
||||
def index_files(self, *, root=None, limit=500):
|
||||
del root, limit
|
||||
return {
|
||||
"items": [
|
||||
{"uri": source_uri},
|
||||
{"uri": validation_uri},
|
||||
{"uri": "storage://advanced_materials/style-orphan/unreferenced.png"},
|
||||
],
|
||||
"total": 3,
|
||||
"truncated": False,
|
||||
}
|
||||
|
||||
result = OrphanMediaService(database, FakeMediaIndex()).scan()
|
||||
|
||||
assert result["orphan_count"] == 1
|
||||
assert result["orphans"][0]["uri"].endswith("unreferenced.png")
|
||||
|
||||
|
||||
def test_material_analytics_reports_style_set_pipeline(tmp_path: Path) -> None:
|
||||
path = tmp_path / "advanced-style-analytics.sqlite3"
|
||||
database = _database(path)
|
||||
database.initialize()
|
||||
with database.connect() as connection:
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_material_imports "
|
||||
"(import_id, display_name, source_image_uri, status, metadata_json) "
|
||||
"VALUES (?, ?, ?, ?, ?)",
|
||||
(
|
||||
"style-analytics-import",
|
||||
"Analytics style",
|
||||
"storage://advanced_materials/analytics/source.png",
|
||||
"accepted",
|
||||
json.dumps({"workflow_kind": "style_set"}),
|
||||
),
|
||||
)
|
||||
for index in range(2):
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_style_sources "
|
||||
"(source_id, import_id, sort_order, is_primary, source_image_uri, status) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
f"style-analytics-source-{index}",
|
||||
"style-analytics-import",
|
||||
index,
|
||||
int(index == 0),
|
||||
f"storage://advanced_materials/analytics/{index}.png",
|
||||
"succeeded",
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_style_consensus_runs "
|
||||
"(consensus_id, import_id, revision, status, source_ids_json) "
|
||||
"VALUES (?, ?, ?, ?, ?)",
|
||||
(
|
||||
"style-analytics-consensus",
|
||||
"style-analytics-import",
|
||||
1,
|
||||
"succeeded",
|
||||
json.dumps(["style-analytics-source-0", "style-analytics-source-1"]),
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_material_validation_attempts "
|
||||
"(attempt_id, import_id, attempt_number, status, prompt, validation_image_uri, fidelity_score) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"style-analytics-attempt",
|
||||
"style-analytics-import",
|
||||
1,
|
||||
"accepted",
|
||||
"watercolor",
|
||||
"storage://advanced_materials/analytics/portrait.png",
|
||||
0.9,
|
||||
),
|
||||
)
|
||||
for index, case_key in enumerate(("portrait", "fullbody", "scene")):
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_style_validation_cases "
|
||||
"(case_id, attempt_id, import_id, case_key, sort_order, status, "
|
||||
"content_prompt, final_prompt, fidelity_score) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
f"style-analytics-{case_key}",
|
||||
"style-analytics-attempt",
|
||||
"style-analytics-import",
|
||||
case_key,
|
||||
index,
|
||||
"succeeded",
|
||||
case_key,
|
||||
f"watercolor {case_key}",
|
||||
0.8 + index * 0.05,
|
||||
),
|
||||
)
|
||||
connection.execute(
|
||||
"INSERT INTO advanced_materials "
|
||||
"(material_id, import_id, accepted_attempt_id, display_name, source_image_uri, "
|
||||
"validation_image_uri, prompt, status, metadata_json) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
(
|
||||
"style-analytics-material",
|
||||
"style-analytics-import",
|
||||
"style-analytics-attempt",
|
||||
"Analytics style",
|
||||
"storage://advanced_materials/analytics/source.png",
|
||||
"storage://advanced_materials/analytics/portrait.png",
|
||||
"watercolor",
|
||||
"active",
|
||||
json.dumps({"workflow_kind": "style_set", "style_set": {"source_ids": ["a", "b"]}}),
|
||||
),
|
||||
)
|
||||
connection.commit()
|
||||
|
||||
summary = MaterialAnalyticsService(database).summarize()
|
||||
|
||||
assert summary["advanced_materials"]["active_style_sets"] == 1
|
||||
assert summary["advanced_style_sets"] == {
|
||||
"imports": 1,
|
||||
"accepted_imports": 1,
|
||||
"active_materials": 1,
|
||||
"sources": 2,
|
||||
"source_statuses": {"succeeded": 2},
|
||||
"consensus_runs": 1,
|
||||
"consensus_statuses": {"succeeded": 1},
|
||||
"validation_cases": 3,
|
||||
"validation_case_statuses": {"succeeded": 3},
|
||||
"average_case_fidelity_score": 0.85,
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||
|
||||
|
||||
ADVANCED_MATERIAL_TABLES = {
|
||||
"advanced_material_imports",
|
||||
"advanced_material_validation_attempts",
|
||||
"advanced_materials",
|
||||
}
|
||||
|
||||
ADVANCED_STYLE_SET_TABLES = {
|
||||
"advanced_style_sources",
|
||||
"advanced_style_consensus_runs",
|
||||
"advanced_style_validation_cases",
|
||||
}
|
||||
|
||||
|
||||
def _database(path: Path) -> Database:
|
||||
return Database(
|
||||
DatabaseSettings(
|
||||
backend="sqlite",
|
||||
sqlite=SqliteSettings(path=str(path)),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _table_names(path: Path) -> set[str]:
|
||||
with sqlite3.connect(path) as connection:
|
||||
rows = connection.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type = 'table'"
|
||||
).fetchall()
|
||||
return {str(row[0]) for row in rows}
|
||||
|
||||
|
||||
def test_0016_fresh_install_and_repeated_initialize_are_idempotent(tmp_path: Path) -> None:
|
||||
path = tmp_path / "advanced-material-fresh.sqlite3"
|
||||
database = _database(path)
|
||||
|
||||
database.initialize()
|
||||
database.initialize()
|
||||
|
||||
assert ADVANCED_MATERIAL_TABLES <= _table_names(path)
|
||||
with sqlite3.connect(path) as connection:
|
||||
applied = connection.execute(
|
||||
"SELECT COUNT(*) FROM schema_migrations WHERE version = ?",
|
||||
("0016_advanced_materials",),
|
||||
).fetchone()
|
||||
assert applied == (1,)
|
||||
|
||||
|
||||
def test_0016_repairs_a_partially_missing_table_without_duplicate_schema_rows(
|
||||
tmp_path: Path,
|
||||
) -> None:
|
||||
path = tmp_path / "advanced-material-repair.sqlite3"
|
||||
database = _database(path)
|
||||
database.initialize()
|
||||
|
||||
with sqlite3.connect(path) as connection:
|
||||
connection.execute("PRAGMA foreign_keys = OFF")
|
||||
connection.execute("DROP TABLE advanced_materials")
|
||||
connection.commit()
|
||||
|
||||
database.initialize()
|
||||
database.initialize()
|
||||
|
||||
assert ADVANCED_MATERIAL_TABLES <= _table_names(path)
|
||||
with sqlite3.connect(path) as connection:
|
||||
applied = connection.execute(
|
||||
"SELECT COUNT(*) FROM schema_migrations WHERE version = ?",
|
||||
("0016_advanced_materials",),
|
||||
).fetchone()
|
||||
assert applied == (1,)
|
||||
|
||||
|
||||
def test_0018_installs_advanced_style_set_tables_idempotently(tmp_path: Path) -> None:
|
||||
path = tmp_path / "advanced-style-set.sqlite3"
|
||||
database = _database(path)
|
||||
|
||||
database.initialize()
|
||||
database.initialize()
|
||||
|
||||
assert ADVANCED_STYLE_SET_TABLES <= _table_names(path)
|
||||
with sqlite3.connect(path) as connection:
|
||||
applied = connection.execute(
|
||||
"SELECT COUNT(*) FROM schema_migrations WHERE version = ?",
|
||||
("0018_advanced_style_sets",),
|
||||
).fetchone()
|
||||
assert applied == (1,)
|
||||
|
|
@ -0,0 +1,388 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||||
|
||||
|
||||
def _repository(tmp_path: Path) -> tuple[Database, AdvancedMaterialRepository]:
|
||||
database = Database(
|
||||
DatabaseSettings(
|
||||
backend="sqlite",
|
||||
sqlite=SqliteSettings(path=str(tmp_path / "advanced-material-repository.sqlite3")),
|
||||
)
|
||||
)
|
||||
database.initialize()
|
||||
return database, AdvancedMaterialRepository(database)
|
||||
|
||||
|
||||
def _completed_attempt(
|
||||
repository: AdvancedMaterialRepository,
|
||||
*,
|
||||
import_id: str,
|
||||
attempt_id: str,
|
||||
) -> None:
|
||||
repository.create_import(
|
||||
{
|
||||
"import_id": import_id,
|
||||
"display_name": "verified red dress",
|
||||
"source_image_uri": f"storage://advanced_materials/sources/{import_id}/source.png",
|
||||
"source_thumbnail_uri": f"storage://advanced_materials/sources/{import_id}/source.png",
|
||||
"status": "awaiting_review",
|
||||
"recognition_prompt": "red silk dress, gold embroidery",
|
||||
"recognition_negative_prompt": "watermark",
|
||||
"recognition_tags": ["dress", "red"],
|
||||
}
|
||||
)
|
||||
repository.create_attempt(
|
||||
{
|
||||
"attempt_id": attempt_id,
|
||||
"import_id": import_id,
|
||||
"status": "awaiting_review",
|
||||
"prompt": "red silk dress, gold embroidery",
|
||||
"negative_prompt": "watermark",
|
||||
"validation_image_uri": (
|
||||
f"storage://advanced_materials/attempts/{import_id}/{attempt_id}/validation.png"
|
||||
),
|
||||
"validation_thumbnail_uri": (
|
||||
f"storage://advanced_materials/attempts/{import_id}/{attempt_id}/thumbnail.png"
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_accept_attempt_is_idempotent_for_the_same_import_and_attempt(tmp_path: Path) -> None:
|
||||
_database, repository = _repository(tmp_path)
|
||||
_completed_attempt(repository, import_id="advimp_idempotent", attempt_id="attempt_1")
|
||||
|
||||
first = repository.accept_attempt(
|
||||
"advimp_idempotent",
|
||||
"attempt_1",
|
||||
{
|
||||
"material_id": "advmat_first",
|
||||
"priority": 100,
|
||||
"capsule": {"standard": {"prompt_parts": ["red silk dress"]}},
|
||||
},
|
||||
)
|
||||
replay = repository.accept_attempt(
|
||||
"advimp_idempotent",
|
||||
"attempt_1",
|
||||
{"material_id": "advmat_should_not_be_created", "priority": 1},
|
||||
)
|
||||
|
||||
assert replay.material_id == first.material_id == "advmat_first"
|
||||
assert replay.priority == 100
|
||||
materials, total = repository.list_materials(page=1, page_size=10)
|
||||
assert total == 1
|
||||
assert [item.material_id for item in materials] == ["advmat_first"]
|
||||
record = repository.get_import("advimp_idempotent")
|
||||
attempt = repository.get_attempt("attempt_1")
|
||||
assert record is not None and record.status == "accepted"
|
||||
assert record.accepted_material_id == "advmat_first"
|
||||
assert attempt is not None and attempt.status == "accepted"
|
||||
|
||||
|
||||
def test_accept_attempt_rolls_back_material_attempt_and_import_together(tmp_path: Path) -> None:
|
||||
database, repository = _repository(tmp_path)
|
||||
_completed_attempt(repository, import_id="advimp_atomic", attempt_id="attempt_atomic")
|
||||
connection = database.connect()
|
||||
try:
|
||||
connection.execute(
|
||||
"""
|
||||
CREATE TRIGGER fail_advanced_material_accept
|
||||
BEFORE UPDATE OF status ON advanced_material_validation_attempts
|
||||
WHEN NEW.attempt_id = 'attempt_atomic' AND NEW.status = 'accepted'
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'simulated advanced material accept failure');
|
||||
END
|
||||
"""
|
||||
)
|
||||
connection.commit()
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
with pytest.raises(sqlite3.IntegrityError):
|
||||
repository.accept_attempt(
|
||||
"advimp_atomic",
|
||||
"attempt_atomic",
|
||||
{"material_id": "advmat_atomic"},
|
||||
)
|
||||
|
||||
assert repository.get_material("advmat_atomic") is None
|
||||
record = repository.get_import("advimp_atomic")
|
||||
attempt = repository.get_attempt("attempt_atomic")
|
||||
assert record is not None and record.status == "awaiting_review"
|
||||
assert record.accepted_material_id is None
|
||||
assert attempt is not None and attempt.status == "awaiting_review"
|
||||
assert attempt.decision is None
|
||||
|
||||
|
||||
def test_advanced_style_sources_and_consensus_support_independent_completion(tmp_path: Path) -> None:
|
||||
_database, repository = _repository(tmp_path)
|
||||
repository.create_import(
|
||||
{
|
||||
"import_id": "advimp_style_set",
|
||||
"display_name": "shared painted style",
|
||||
"source_image_uri": "storage://advanced_materials/style-set/primary.png",
|
||||
"status": "source_recognition_queued",
|
||||
}
|
||||
)
|
||||
first = repository.create_style_source(
|
||||
{
|
||||
"source_id": "advsrc_1",
|
||||
"import_id": "advimp_style_set",
|
||||
"sort_order": 0,
|
||||
"is_primary": True,
|
||||
"source_image_uri": "storage://advanced_materials/style-set/1.png",
|
||||
}
|
||||
)
|
||||
second = repository.create_style_source(
|
||||
{
|
||||
"source_id": "advsrc_2",
|
||||
"import_id": "advimp_style_set",
|
||||
"sort_order": 1,
|
||||
"source_image_uri": "storage://advanced_materials/style-set/2.png",
|
||||
}
|
||||
)
|
||||
|
||||
assert first.is_primary is True
|
||||
assert second.status == "queued"
|
||||
running = repository.update_style_source_if_status(
|
||||
"advsrc_1", {"queued"}, {"status": "running"}
|
||||
)
|
||||
assert running is not None and running.status == "running"
|
||||
completed = repository.update_style_source_if_status(
|
||||
"advsrc_1",
|
||||
{"running"},
|
||||
{
|
||||
"status": "succeeded",
|
||||
"recognition_prompt": "soft charcoal outlines, layered watercolor",
|
||||
"recognition_tags": ["watercolor", "charcoal"],
|
||||
"recognition_result": {"confidence": 0.91},
|
||||
},
|
||||
)
|
||||
assert completed is not None and completed.completed_at is not None
|
||||
assert repository.update_style_source_if_status(
|
||||
"advsrc_1", {"queued"}, {"status": "running"}
|
||||
) is None
|
||||
assert [item.source_id for item in repository.list_style_sources("advimp_style_set")] == [
|
||||
"advsrc_1",
|
||||
"advsrc_2",
|
||||
]
|
||||
|
||||
consensus = repository.create_style_consensus_run(
|
||||
{
|
||||
"consensus_id": "advconsensus_1",
|
||||
"import_id": "advimp_style_set",
|
||||
"revision": 1,
|
||||
"source_ids": ["advsrc_1", "advsrc_2"],
|
||||
"model_route": "material_vlm",
|
||||
}
|
||||
)
|
||||
assert consensus.source_ids == ["advsrc_1", "advsrc_2"]
|
||||
consensus = repository.update_style_consensus_run(
|
||||
consensus.consensus_id,
|
||||
{
|
||||
"status": "succeeded",
|
||||
"prompt": "soft charcoal outlines, translucent watercolor layers",
|
||||
"tags": ["watercolor"],
|
||||
"result": {"generation_capsules": {"detailed": {"prompt_parts": ["watercolor"]}}},
|
||||
},
|
||||
expected_statuses={"queued"},
|
||||
)
|
||||
assert consensus is not None and consensus.completed_at is not None
|
||||
assert repository.get_latest_style_consensus_run("advimp_style_set") == consensus
|
||||
|
||||
|
||||
def test_style_validation_batch_aggregates_three_cases_and_seals_one_material(tmp_path: Path) -> None:
|
||||
_database, repository = _repository(tmp_path)
|
||||
repository.create_import(
|
||||
{
|
||||
"import_id": "advimp_style_validation",
|
||||
"display_name": "verified watercolor style",
|
||||
"source_image_uri": "storage://advanced_materials/style-validation/primary.png",
|
||||
"status": "ready_for_validation",
|
||||
"recognition_prompt": "watercolor line art",
|
||||
"recognition_negative_prompt": "photorealistic",
|
||||
}
|
||||
)
|
||||
attempt, cases = repository.create_style_validation_batch(
|
||||
{
|
||||
"attempt_id": "advattempt_style_1",
|
||||
"import_id": "advimp_style_validation",
|
||||
"status": "running",
|
||||
"prompt": "watercolor line art",
|
||||
"negative_prompt": "photorealistic",
|
||||
"model_route": "image_default",
|
||||
},
|
||||
[
|
||||
{
|
||||
"case_id": "advcase_portrait",
|
||||
"case_key": "portrait",
|
||||
"sort_order": 0,
|
||||
"content_prompt": "elderly mechanic portrait",
|
||||
"final_prompt": "watercolor line art, elderly mechanic portrait",
|
||||
},
|
||||
{
|
||||
"case_id": "advcase_fullbody",
|
||||
"case_key": "fullbody",
|
||||
"sort_order": 1,
|
||||
"content_prompt": "robot courier full body",
|
||||
"final_prompt": "watercolor line art, robot courier full body",
|
||||
},
|
||||
{
|
||||
"case_id": "advcase_scene",
|
||||
"case_key": "scene",
|
||||
"sort_order": 2,
|
||||
"content_prompt": "stormy harbor crane scene",
|
||||
"final_prompt": "watercolor line art, stormy harbor crane scene",
|
||||
},
|
||||
],
|
||||
)
|
||||
assert attempt.attempt_number == 1
|
||||
assert [item.case_key for item in cases] == ["portrait", "fullbody", "scene"]
|
||||
|
||||
for item in cases[:2]:
|
||||
repository.update_style_validation_case(
|
||||
item.case_id,
|
||||
{
|
||||
"status": "succeeded",
|
||||
"validation_image_uri": f"storage://advanced_materials/validation/{item.case_id}.png",
|
||||
"validation_thumbnail_uri": f"storage://advanced_materials/validation/{item.case_id}-thumb.png",
|
||||
"provider_id": "provider",
|
||||
"provider_model": "image-model",
|
||||
"width": 1024,
|
||||
"height": 1024,
|
||||
},
|
||||
expected_statuses={"queued"},
|
||||
)
|
||||
repository.update_style_validation_case(
|
||||
cases[2].case_id,
|
||||
{"status": "failed", "error": {"error_type": "timeout"}},
|
||||
expected_statuses={"queued"},
|
||||
)
|
||||
record, attempt, current_cases = repository.finalize_style_validation_batch_if_ready(
|
||||
"advimp_style_validation", "advattempt_style_1"
|
||||
)
|
||||
assert record.status == "validation_partial_failed"
|
||||
assert attempt.status == "validation_partial_failed"
|
||||
assert current_cases[2].status == "failed"
|
||||
with pytest.raises(ValueError, match="all three"):
|
||||
repository.accept_attempt(
|
||||
"advimp_style_validation",
|
||||
"advattempt_style_1",
|
||||
{"material_id": "advmat_too_early"},
|
||||
)
|
||||
|
||||
repository.update_style_validation_case(
|
||||
"advcase_scene", {"status": "running", "error": None}, expected_statuses={"failed"}
|
||||
)
|
||||
repository.update_style_validation_case(
|
||||
"advcase_scene",
|
||||
{
|
||||
"status": "succeeded",
|
||||
"validation_image_uri": "storage://advanced_materials/validation/scene.png",
|
||||
"validation_thumbnail_uri": "storage://advanced_materials/validation/scene-thumb.png",
|
||||
"width": 1024,
|
||||
"height": 1024,
|
||||
},
|
||||
expected_statuses={"running"},
|
||||
)
|
||||
record, attempt, current_cases = repository.finalize_style_validation_batch_if_ready(
|
||||
"advimp_style_validation", "advattempt_style_1"
|
||||
)
|
||||
assert record.status == "awaiting_review"
|
||||
assert attempt.status == "awaiting_review"
|
||||
assert attempt.validation_image_uri.endswith("advcase_portrait.png")
|
||||
assert all(item.status == "succeeded" for item in current_cases)
|
||||
|
||||
material = repository.accept_attempt(
|
||||
"advimp_style_validation",
|
||||
"advattempt_style_1",
|
||||
{
|
||||
"material_id": "advmat_style_set",
|
||||
"prompt": "watercolor line art",
|
||||
"capsule": {"detailed": {"prompt_parts": ["watercolor line art"]}},
|
||||
},
|
||||
)
|
||||
assert material.material_id == "advmat_style_set"
|
||||
assert material.accepted_attempt_id == "advattempt_style_1"
|
||||
|
||||
|
||||
def test_style_validation_batch_creation_is_atomic(tmp_path: Path) -> None:
|
||||
_database, repository = _repository(tmp_path)
|
||||
repository.create_import(
|
||||
{
|
||||
"import_id": "advimp_atomic_style_batch",
|
||||
"display_name": "atomic style batch",
|
||||
"source_image_uri": "storage://advanced_materials/atomic/primary.png",
|
||||
"status": "ready_for_validation",
|
||||
"recognition_prompt": "ink wash",
|
||||
}
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="unique"):
|
||||
repository.create_style_validation_batch(
|
||||
{
|
||||
"attempt_id": "advattempt_invalid_style_batch",
|
||||
"import_id": "advimp_atomic_style_batch",
|
||||
"prompt": "ink wash",
|
||||
},
|
||||
[
|
||||
{"case_key": "portrait", "content_prompt": "a", "final_prompt": "ink wash, a"},
|
||||
{"case_key": "portrait", "content_prompt": "b", "final_prompt": "ink wash, b"},
|
||||
{"case_key": "scene", "content_prompt": "c", "final_prompt": "ink wash, c"},
|
||||
],
|
||||
)
|
||||
|
||||
assert repository.list_attempts("advimp_atomic_style_batch") == []
|
||||
|
||||
|
||||
def test_style_import_and_all_sources_are_created_atomically(tmp_path: Path) -> None:
|
||||
database, repository = _repository(tmp_path)
|
||||
connection = database.connect()
|
||||
try:
|
||||
connection.execute(
|
||||
"""
|
||||
CREATE TRIGGER fail_second_style_source
|
||||
BEFORE INSERT ON advanced_style_sources
|
||||
WHEN NEW.import_id = 'advimp_atomic_sources' AND NEW.sort_order = 1
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'simulated style source failure');
|
||||
END
|
||||
"""
|
||||
)
|
||||
connection.commit()
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
with pytest.raises(sqlite3.IntegrityError):
|
||||
repository.create_style_import_with_sources(
|
||||
{
|
||||
"import_id": "advimp_atomic_sources",
|
||||
"display_name": "atomic sources",
|
||||
"source_image_uri": "storage://advanced_materials/atomic-sources/1.png",
|
||||
"metadata": {"workflow_kind": "style_set"},
|
||||
},
|
||||
[
|
||||
{
|
||||
"source_id": "advsrc_atomic_1",
|
||||
"sort_order": 0,
|
||||
"is_primary": True,
|
||||
"source_image_uri": "storage://advanced_materials/atomic-sources/1.png",
|
||||
},
|
||||
{
|
||||
"source_id": "advsrc_atomic_2",
|
||||
"sort_order": 1,
|
||||
"is_primary": False,
|
||||
"source_image_uri": "storage://advanced_materials/atomic-sources/2.png",
|
||||
},
|
||||
],
|
||||
)
|
||||
|
||||
assert repository.get_import("advimp_atomic_sources") is None
|
||||
assert repository.list_style_sources("advimp_atomic_sources") == []
|
||||
|
|
@ -0,0 +1,193 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from backend.dto.job_dto import GenerationTask
|
||||
from backend.services.advanced_material_worker_runtime import AdvancedMaterialWorkerRuntime
|
||||
from backend.services.queue_service import QueueService
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||
from database.repositories.job_repository import JobRepository
|
||||
|
||||
|
||||
class _RecoveryService:
|
||||
def __init__(self, queue_service: QueueService):
|
||||
self.queue_service = queue_service
|
||||
self.failures: list[tuple[GenerationTask, Exception]] = []
|
||||
|
||||
def execute_task(self, _task: GenerationTask, _ctx: RequestContext):
|
||||
raise AssertionError("startup recovery must run before worker execution")
|
||||
|
||||
def handle_task_failure(
|
||||
self,
|
||||
task: GenerationTask,
|
||||
exc: Exception,
|
||||
_ctx: RequestContext,
|
||||
) -> None:
|
||||
self.failures.append((task, exc))
|
||||
|
||||
|
||||
def _runtime(tmp_path) -> tuple[AdvancedMaterialWorkerRuntime, JobRepository, _RecoveryService]:
|
||||
database = Database(
|
||||
DatabaseSettings(
|
||||
backend="sqlite",
|
||||
sqlite=SqliteSettings(path=str(tmp_path / "advanced-worker-recovery.sqlite3")),
|
||||
)
|
||||
)
|
||||
database.initialize()
|
||||
repository = JobRepository(database)
|
||||
queue = QueueService(repository, AuditService(database))
|
||||
service = _RecoveryService(queue)
|
||||
runtime = AdvancedMaterialWorkerRuntime(service, worker_count=1) # type: ignore[arg-type]
|
||||
return runtime, repository, service
|
||||
|
||||
|
||||
def _create_task(
|
||||
repository: JobRepository,
|
||||
task_id: str,
|
||||
*,
|
||||
task_type: str,
|
||||
status: str,
|
||||
source: str = "advanced_material",
|
||||
progress: int = 60,
|
||||
) -> GenerationTask:
|
||||
return repository.create(
|
||||
{
|
||||
"task_id": task_id,
|
||||
"task_type": task_type,
|
||||
"status": status,
|
||||
"progress": progress,
|
||||
"request": {},
|
||||
"metadata": {
|
||||
"source": source,
|
||||
"advanced_material_import_id": f"import-{task_id}",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def test_fast_restart_recovers_every_fresh_advanced_running_state(tmp_path) -> None:
|
||||
runtime, repository, service = _runtime(tmp_path)
|
||||
recognition_running = _create_task(
|
||||
repository,
|
||||
"advanced-recognition-running",
|
||||
task_type="advanced_material_recognition",
|
||||
status="running",
|
||||
)
|
||||
recognition_retrying = _create_task(
|
||||
repository,
|
||||
"advanced-recognition-retrying",
|
||||
task_type="advanced_material_recognition",
|
||||
status="retrying",
|
||||
)
|
||||
validation_running = _create_task(
|
||||
repository,
|
||||
"advanced-validation-running",
|
||||
task_type="advanced_material_validation_generation",
|
||||
status="running",
|
||||
)
|
||||
validation_retrying = _create_task(
|
||||
repository,
|
||||
"advanced-validation-retrying",
|
||||
task_type="advanced_material_validation_generation",
|
||||
status="retrying",
|
||||
)
|
||||
unrelated = _create_task(
|
||||
repository,
|
||||
"workflow-round-running",
|
||||
task_type="workflow_round_generation",
|
||||
status="running",
|
||||
source="workflow_round",
|
||||
)
|
||||
|
||||
# No timestamp aging: this simulates a container/plugin restart in the
|
||||
# same second that the previous process last wrote the queue rows.
|
||||
recovery = runtime._recover_interrupted_tasks()
|
||||
|
||||
assert set(recovery["requeued_task_ids"]) == {
|
||||
recognition_running.task_id,
|
||||
recognition_retrying.task_id,
|
||||
}
|
||||
assert set(recovery["failed_task_ids"]) == {
|
||||
validation_running.task_id,
|
||||
validation_retrying.task_id,
|
||||
}
|
||||
assert recovery["skipped_task_ids"] == [unrelated.task_id]
|
||||
|
||||
for task in (recognition_running, recognition_retrying):
|
||||
current = repository.get(task.task_id)
|
||||
assert current is not None
|
||||
assert current.status == "queued"
|
||||
assert current.progress == 0
|
||||
assert current.error_message is None
|
||||
assert current.metadata["recovery"]["previous_status"] == task.status
|
||||
assert current.metadata["recovery"]["status"] == "requeued"
|
||||
|
||||
for task in (validation_running, validation_retrying):
|
||||
current = repository.get(task.task_id)
|
||||
assert current is not None
|
||||
assert current.status == "failed"
|
||||
assert current.progress == task.progress
|
||||
assert current.error_message == "task was interrupted by a previous plugin process"
|
||||
assert current.metadata["recovery"]["previous_status"] == task.status
|
||||
assert "status" not in current.metadata["recovery"]
|
||||
|
||||
current_unrelated = repository.get(unrelated.task_id)
|
||||
assert current_unrelated is not None
|
||||
assert current_unrelated.status == "running"
|
||||
assert {task.task_id for task, _exc in service.failures} == {
|
||||
validation_running.task_id,
|
||||
validation_retrying.task_id,
|
||||
}
|
||||
|
||||
|
||||
def test_startup_snapshot_is_not_truncated_by_unrelated_tasks(tmp_path) -> None:
|
||||
runtime, repository, _service = _runtime(tmp_path)
|
||||
database = repository.database
|
||||
conn = database.connect()
|
||||
try:
|
||||
for index in range(105):
|
||||
conn.execute(
|
||||
"""
|
||||
INSERT INTO generation_tasks (
|
||||
task_id, task_type, status, progress, request_json, metadata_json,
|
||||
created_at, updated_at
|
||||
) VALUES (?, ?, 'running', 0, '{}', ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
f"a-unrelated-{index:03d}",
|
||||
"workflow_round_generation",
|
||||
'{"source":"workflow_round"}',
|
||||
"2020-01-01 00:00:00",
|
||||
"2020-01-01 00:00:00",
|
||||
),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
advanced = _create_task(
|
||||
repository,
|
||||
"z-advanced-after-first-page",
|
||||
task_type="advanced_material_recognition",
|
||||
status="running",
|
||||
)
|
||||
conn = database.connect()
|
||||
try:
|
||||
conn.execute(
|
||||
"""
|
||||
UPDATE generation_tasks
|
||||
SET created_at = ?, updated_at = ?
|
||||
WHERE task_id = ?
|
||||
""",
|
||||
("2020-01-01 00:00:00", "2020-01-01 00:00:00", advanced.task_id),
|
||||
)
|
||||
conn.commit()
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
recovery = runtime._recover_interrupted_tasks()
|
||||
|
||||
assert recovery["requeued_task_ids"] == [advanced.task_id]
|
||||
assert len(recovery["skipped_task_ids"]) == 105
|
||||
current = repository.get(advanced.task_id)
|
||||
assert current is not None
|
||||
assert current.status == "queued"
|
||||
|
|
@ -0,0 +1,698 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from backend.dto.common import ApiError
|
||||
from backend.dto.job_dto import GenerationTask
|
||||
from backend.dto.workflow_dto import WorkflowNode
|
||||
from backend.routes.advanced_material_routes import (
|
||||
delete_advanced_material_import,
|
||||
list_advanced_material_imports,
|
||||
list_advanced_materials,
|
||||
upload_advanced_material,
|
||||
)
|
||||
from backend.services.advanced_material_service import (
|
||||
AdvancedMaterialService,
|
||||
AdvancedMaterialTaskCancelled,
|
||||
)
|
||||
from backend.services.prompt_governance_service import (
|
||||
GenerationRouteService,
|
||||
PromptModerationService,
|
||||
)
|
||||
from backend.services.queue_service import QueueService
|
||||
from core.audit_service import AuditService
|
||||
from core.config_service import ImageStudioConfig, StorageConfig
|
||||
from core.identity import RequestContext
|
||||
from core.storage import VfsStorage
|
||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||||
from database.repositories.job_repository import JobRepository
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from engines.image_provider import (
|
||||
GeneratedImagePayload,
|
||||
ImageGenerationRequest,
|
||||
ImageGenerationResult,
|
||||
ImageProviderError,
|
||||
)
|
||||
from engines.single_flow import ONE_PIXEL_PNG, SingleFlowEngine
|
||||
from engines.vision_provider import VisionRecognitionRequest, VisionRecognitionResult
|
||||
|
||||
|
||||
PNG_DATA_URL = f"data:image/png;base64,{base64.b64encode(ONE_PIXEL_PNG).decode('ascii')}"
|
||||
|
||||
|
||||
class _StaticConfigService:
|
||||
def __init__(self, root: Path):
|
||||
self.config = ImageStudioConfig(
|
||||
storage=StorageConfig(
|
||||
images_path=str(root / "images"),
|
||||
thumbnails_path=str(root / "thumbnails"),
|
||||
materials_path=str(root / "materials"),
|
||||
advanced_materials_path=str(root / "advanced_materials"),
|
||||
styles_path=str(root / "styles"),
|
||||
archives_path=str(root / "archives"),
|
||||
temp_path=str(root / "temp"),
|
||||
)
|
||||
)
|
||||
|
||||
def load(self) -> ImageStudioConfig:
|
||||
return self.config
|
||||
|
||||
|
||||
class _FakeVisionProvider:
|
||||
provider_id = "fake-vision"
|
||||
|
||||
def __init__(self, *, prompt: str = "red silk dress, fitted waist, gold embroidery") -> None:
|
||||
self.requests: list[VisionRecognitionRequest] = []
|
||||
self.prompt = prompt
|
||||
|
||||
def recognize_material(self, request: VisionRecognitionRequest) -> VisionRecognitionResult:
|
||||
self.requests.append(request)
|
||||
return VisionRecognitionResult(
|
||||
provider_id=self.provider_id,
|
||||
model="fake-vision-model",
|
||||
prompt=self.prompt,
|
||||
negative_prompt="watermark, changed color",
|
||||
tags=["dress", "red", "silk"],
|
||||
confidence=0.97,
|
||||
description="A red silk dress with gold embroidery.",
|
||||
structured_data={
|
||||
"raw_analysis": {
|
||||
"attributes": {
|
||||
"color": {
|
||||
"value": "red",
|
||||
"confidence": 0.99,
|
||||
"source_state": "observed",
|
||||
},
|
||||
"material": {
|
||||
"value": "silk",
|
||||
"confidence": 0.93,
|
||||
"source_state": "observed",
|
||||
},
|
||||
}
|
||||
},
|
||||
"normalized_asset_profile": {
|
||||
"category": "dress",
|
||||
"role": "garment",
|
||||
"body_slots": ["upper_body", "waist", "hips", "upper_legs"],
|
||||
"wear_layer": "base",
|
||||
"must_preserve": ["red color", "gold embroidery"],
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class _FakeImageProvider:
|
||||
provider_id = "fake-image"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
failures: int = 0,
|
||||
failure_message: str = "controlled fake image failure",
|
||||
failure_error_type: str = "network_error",
|
||||
failure_status_code: int | None = None,
|
||||
) -> None:
|
||||
self.failures = failures
|
||||
self.failure_message = failure_message
|
||||
self.failure_error_type = failure_error_type
|
||||
self.failure_status_code = failure_status_code
|
||||
self.requests: list[ImageGenerationRequest] = []
|
||||
self.on_generate = None
|
||||
|
||||
def generate_image(self, request: ImageGenerationRequest) -> ImageGenerationResult:
|
||||
self.requests.append(request)
|
||||
if self.on_generate is not None:
|
||||
callback, self.on_generate = self.on_generate, None
|
||||
callback()
|
||||
if self.failures > 0:
|
||||
self.failures -= 1
|
||||
raise ImageProviderError(
|
||||
self.failure_message,
|
||||
provider_id=self.provider_id,
|
||||
status_code=self.failure_status_code,
|
||||
error_type=self.failure_error_type,
|
||||
)
|
||||
return ImageGenerationResult(
|
||||
provider_id=self.provider_id,
|
||||
model=request.model or "fake-image-model",
|
||||
images=[
|
||||
GeneratedImagePayload(
|
||||
b64_json=base64.b64encode(ONE_PIXEL_PNG).decode("ascii"),
|
||||
revised_prompt=request.prompt,
|
||||
)
|
||||
],
|
||||
usage={"images": 1},
|
||||
raw_metadata={"provider_base_url": "https://fake.invalid"},
|
||||
)
|
||||
|
||||
|
||||
class _FakeRuntime:
|
||||
def __init__(self, vision: _FakeVisionProvider, image: _FakeImageProvider) -> None:
|
||||
self.vision = vision
|
||||
self.image = image
|
||||
|
||||
def vision_provider_for_route(self, _route: dict[str, Any]) -> _FakeVisionProvider:
|
||||
return self.vision
|
||||
|
||||
def image_provider_for_route(self, _route: dict[str, Any]) -> _FakeImageProvider:
|
||||
return self.image
|
||||
|
||||
|
||||
class _CapturingSingleFlowEngine(SingleFlowEngine):
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self.requests: list[dict[str, Any]] = []
|
||||
|
||||
def generate(
|
||||
self,
|
||||
node: WorkflowNode,
|
||||
task: GenerationTask,
|
||||
request: dict[str, Any],
|
||||
):
|
||||
self.requests.append(dict(request))
|
||||
return super().generate(node, task, request)
|
||||
|
||||
|
||||
class _RawRouteService:
|
||||
def resolve_route(self, route_name: str, *, modality: str | None = None) -> dict[str, Any]:
|
||||
assert modality == "image_generation"
|
||||
return {
|
||||
"route_name": route_name,
|
||||
"provider_id": "fake-image",
|
||||
"model_id": "fake-image-model",
|
||||
"provider": {
|
||||
"provider_id": "fake-image",
|
||||
"base_url": "https://fake.invalid",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class _Fixture:
|
||||
database: Database
|
||||
service: AdvancedMaterialService
|
||||
queue: QueueService
|
||||
material_repository: MaterialRepository
|
||||
advanced_repository: AdvancedMaterialRepository
|
||||
storage: VfsStorage
|
||||
storage_root: Path
|
||||
vision: _FakeVisionProvider
|
||||
image: _FakeImageProvider
|
||||
engine: _CapturingSingleFlowEngine
|
||||
|
||||
|
||||
def _fixture(
|
||||
tmp_path: Path,
|
||||
*,
|
||||
image_failures: int = 0,
|
||||
image_failure_message: str = "controlled fake image failure",
|
||||
image_failure_error_type: str = "network_error",
|
||||
image_failure_status_code: int | None = None,
|
||||
vision_prompt: str = "red silk dress, fitted waist, gold embroidery",
|
||||
config: dict[str, Any] | None = None,
|
||||
) -> _Fixture:
|
||||
database = Database(
|
||||
DatabaseSettings(
|
||||
backend="sqlite",
|
||||
sqlite=SqliteSettings(path=str(tmp_path / "advanced-material-workflow.sqlite3")),
|
||||
)
|
||||
)
|
||||
database.initialize()
|
||||
audit = AuditService(database)
|
||||
queue = QueueService(JobRepository(database), audit)
|
||||
material_repository = MaterialRepository(database)
|
||||
advanced_repository = AdvancedMaterialRepository(database)
|
||||
storage_root = tmp_path / "vfs"
|
||||
storage = VfsStorage(_StaticConfigService(storage_root)) # type: ignore[arg-type]
|
||||
vision = _FakeVisionProvider(prompt=vision_prompt)
|
||||
image = _FakeImageProvider(
|
||||
failures=image_failures,
|
||||
failure_message=image_failure_message,
|
||||
failure_error_type=image_failure_error_type,
|
||||
failure_status_code=image_failure_status_code,
|
||||
)
|
||||
runtime = _FakeRuntime(vision, image)
|
||||
engine = _CapturingSingleFlowEngine(
|
||||
storage=storage,
|
||||
image_provider_factory=runtime.image_provider_for_route,
|
||||
allow_mock_fallback=False,
|
||||
)
|
||||
service_config: dict[str, Any] = {
|
||||
"model_assignments": {
|
||||
"advanced_material_recognition": "fake-vision-route",
|
||||
"advanced_material_validation_generation": "fake-image-route",
|
||||
}
|
||||
}
|
||||
if config:
|
||||
service_config.update(config)
|
||||
governance_config = service_config.get("prompt_governance") if isinstance(service_config.get("prompt_governance"), dict) else {}
|
||||
moderation_config = governance_config.get("moderation") if isinstance(governance_config.get("moderation"), dict) else {}
|
||||
routing_config = governance_config.get("routing") if isinstance(governance_config.get("routing"), dict) else {}
|
||||
service = AdvancedMaterialService(
|
||||
advanced_repository,
|
||||
material_repository,
|
||||
queue,
|
||||
audit,
|
||||
_RawRouteService(), # type: ignore[arg-type]
|
||||
runtime, # type: ignore[arg-type]
|
||||
config=service_config,
|
||||
storage=storage,
|
||||
single_flow_engine=engine,
|
||||
prompt_moderation_service=PromptModerationService(moderation_config),
|
||||
generation_route_service=GenerationRouteService(routing_config),
|
||||
)
|
||||
return _Fixture(
|
||||
database=database,
|
||||
service=service,
|
||||
queue=queue,
|
||||
material_repository=material_repository,
|
||||
advanced_repository=advanced_repository,
|
||||
storage=storage,
|
||||
storage_root=storage_root,
|
||||
vision=vision,
|
||||
image=image,
|
||||
engine=engine,
|
||||
)
|
||||
|
||||
|
||||
def _upload_and_run_recognition(
|
||||
fixture: _Fixture,
|
||||
ctx: RequestContext | None = None,
|
||||
) -> tuple[str, Any]:
|
||||
ctx = ctx or RequestContext.system("advanced-material-test")
|
||||
uploaded = fixture.service.upload(
|
||||
{
|
||||
"display_name": "red dress",
|
||||
"filename": "red-dress.png",
|
||||
"content_type": "image/png",
|
||||
"image_data_url": PNG_DATA_URL,
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
import_id = uploaded["import"]["import_id"]
|
||||
recognition_task = fixture.queue.get_job(uploaded["task"]["task_id"])
|
||||
fixture.service.execute_task(recognition_task, ctx)
|
||||
detail = fixture.service.get_import(import_id)
|
||||
return import_id, detail["attempts"][0]
|
||||
|
||||
|
||||
def _run_validation(fixture: _Fixture, attempt: dict[str, Any]) -> dict[str, Any]:
|
||||
task = fixture.queue.get_job(attempt["task_id"])
|
||||
fixture.service.execute_task(task, RequestContext.system("advanced-material-test"))
|
||||
return fixture.service.get_import(attempt["import_id"])
|
||||
|
||||
|
||||
def test_single_flow_advanced_scope_never_writes_ordinary_image_roots(tmp_path: Path) -> None:
|
||||
storage_root = tmp_path / "single-flow-vfs"
|
||||
storage = VfsStorage(_StaticConfigService(storage_root)) # type: ignore[arg-type]
|
||||
provider = _FakeImageProvider()
|
||||
engine = SingleFlowEngine(
|
||||
storage=storage,
|
||||
image_provider_factory=lambda _route: provider,
|
||||
allow_mock_fallback=False,
|
||||
)
|
||||
now = "2026-07-16T00:00:00+00:00"
|
||||
task = GenerationTask(
|
||||
task_id="task-advanced-output",
|
||||
profile_id=None,
|
||||
workflow_id=None,
|
||||
node_id=None,
|
||||
task_type="advanced_material_validation_generation",
|
||||
status="queued",
|
||||
model_route="fake-image-route",
|
||||
progress=0,
|
||||
idempotency_key=None,
|
||||
request={},
|
||||
result={},
|
||||
error_message=None,
|
||||
metadata={"source": "advanced_material"},
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
node = WorkflowNode(
|
||||
node_id="advanced-material:attempt-1",
|
||||
workflow_id="advanced-material:import-1",
|
||||
node_key="advanced_material_validation",
|
||||
title="validation",
|
||||
description=None,
|
||||
step_name="validation",
|
||||
node_type="generation",
|
||||
status="running",
|
||||
branch_name="main",
|
||||
prompt="red silk dress",
|
||||
negative_prompt="watermark",
|
||||
model_route="fake-image-route",
|
||||
selected_image_id=None,
|
||||
sort_order=0,
|
||||
metadata={"advanced_material": True},
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
|
||||
assets = engine.generate(
|
||||
node,
|
||||
task,
|
||||
{
|
||||
"count": 1,
|
||||
"model_route": "fake-image-route",
|
||||
"resolved_model_route": {
|
||||
"status": "resolved",
|
||||
"provider_id": "fake-image",
|
||||
"model_id": "fake-image-model",
|
||||
},
|
||||
"output_scope": {
|
||||
"kind": "advanced_material_validation",
|
||||
"import_id": "import-1",
|
||||
"attempt_id": "attempt-1",
|
||||
"attempt_number": 1,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
assert len(assets) == 1
|
||||
assert assets[0].image_uri.startswith("storage://advanced_materials/attempts/import-1/attempt-1/")
|
||||
assert assets[0].thumbnail_uri.startswith("storage://advanced_materials/attempts/import-1/attempt-1/")
|
||||
assert storage.ensure_file(assets[0].image_uri).is_file()
|
||||
assert storage.ensure_file(assets[0].thumbnail_uri).is_file()
|
||||
assert not (storage_root / "images").exists()
|
||||
assert not (storage_root / "thumbnails").exists()
|
||||
|
||||
|
||||
def test_upload_recognize_validate_reject_revalidate_accept_is_isolated(tmp_path: Path) -> None:
|
||||
fixture = _fixture(tmp_path)
|
||||
ctx = RequestContext.system("advanced-material-lifecycle")
|
||||
|
||||
import_id, first_attempt = _upload_and_run_recognition(fixture)
|
||||
|
||||
assert fixture.vision.requests
|
||||
assert fixture.vision.requests[0].image_uri.startswith("data:image/")
|
||||
assert first_attempt["attempt_number"] == 1
|
||||
assert fixture.service.get_import(import_id)["import"]["status"] == "validation_queued"
|
||||
|
||||
first_detail = _run_validation(fixture, first_attempt)
|
||||
first_attempt = first_detail["attempts"][0]
|
||||
assert first_detail["import"]["status"] == "awaiting_review"
|
||||
assert first_attempt["status"] == "awaiting_review"
|
||||
assert first_attempt["validation_image_uri"].startswith("storage://advanced_materials/")
|
||||
assert fixture.image.requests[0].input_images == ()
|
||||
assert "input_images" not in fixture.engine.requests[0]
|
||||
assert "image" not in fixture.engine.requests[0]
|
||||
assert fixture.engine.requests[0]["resolved_model_route"]["status"] == "resolved"
|
||||
assert not (fixture.storage_root / "images").exists()
|
||||
assert not (fixture.storage_root / "thumbnails").exists()
|
||||
|
||||
rejected = fixture.service.reject_attempt(
|
||||
import_id,
|
||||
first_attempt["attempt_id"],
|
||||
{
|
||||
"adjustment_text": "keep the sleeves longer and preserve the gold embroidery",
|
||||
"decision_notes": "sleeves were too short",
|
||||
"fidelity_score": 0.55,
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
second_attempt = rejected["next_attempt"]
|
||||
assert second_attempt["attempt_number"] == 2
|
||||
assert "keep the sleeves longer" in second_attempt["prompt"]
|
||||
assert rejected["attempt"]["status"] == "rejected"
|
||||
|
||||
second_detail = _run_validation(fixture, second_attempt)
|
||||
second_attempt = second_detail["attempts"][0]
|
||||
assert second_attempt["status"] == "awaiting_review"
|
||||
assert fixture.image.requests[-1].input_images == ()
|
||||
|
||||
accepted = fixture.service.accept_attempt(
|
||||
import_id,
|
||||
second_attempt["attempt_id"],
|
||||
{"fidelity_score": 0.96, "decision_notes": "faithful enough"},
|
||||
ctx,
|
||||
)
|
||||
replay = fixture.service.accept_attempt(
|
||||
import_id,
|
||||
second_attempt["attempt_id"],
|
||||
{"fidelity_score": 0.99},
|
||||
ctx,
|
||||
)
|
||||
|
||||
assert accepted["material"]["material_id"] == replay["material"]["material_id"]
|
||||
assert accepted["material"]["priority"] == 100
|
||||
assert accepted["import"]["status"] == "accepted"
|
||||
standard_items, standard_total = fixture.material_repository.list_materials(page=1, page_size=10)
|
||||
assert standard_items == []
|
||||
assert standard_total == 0
|
||||
advanced_items, advanced_total = fixture.advanced_repository.list_materials(page=1, page_size=10)
|
||||
assert advanced_total == 1
|
||||
assert advanced_items[0].material_id == accepted["material"]["material_id"]
|
||||
catalog = fixture.service.list_catalog(page=1, page_size=10)
|
||||
assert catalog["items"][0]["library_kind"] == "advanced"
|
||||
assert catalog["items"][0]["priority"] == 100
|
||||
|
||||
|
||||
def test_validation_failure_records_error_and_can_be_retried(tmp_path: Path) -> None:
|
||||
fixture = _fixture(tmp_path, image_failures=1)
|
||||
ctx = RequestContext.system("advanced-material-retry")
|
||||
import_id, first_attempt = _upload_and_run_recognition(fixture)
|
||||
failed_task = fixture.queue.get_job(first_attempt["task_id"])
|
||||
|
||||
with pytest.raises(ImageProviderError) as captured:
|
||||
fixture.service.execute_task(failed_task, ctx)
|
||||
fixture.service.handle_task_failure(failed_task, captured.value, ctx)
|
||||
|
||||
failed = fixture.service.get_import(import_id)
|
||||
assert failed["import"]["status"] == "validation_failed"
|
||||
assert failed["import"]["error"]["exception_type"] == "ImageProviderError"
|
||||
assert failed["attempts"][0]["status"] == "failed"
|
||||
assert failed["attempts"][0]["error"]["message"] == "controlled fake image failure"
|
||||
|
||||
retried = fixture.service.retry_import(import_id, ctx)
|
||||
assert retried["attempt"]["attempt_number"] == 2
|
||||
assert retried["task"]["status"] == "queued"
|
||||
detail = _run_validation(fixture, retried["attempt"])
|
||||
assert detail["import"]["status"] == "awaiting_review"
|
||||
assert detail["attempts"][0]["status"] == "awaiting_review"
|
||||
|
||||
|
||||
def _adult_routing_config() -> dict[str, Any]:
|
||||
return {
|
||||
"prompt_governance": {
|
||||
"moderation": {"llm_review_enabled": False},
|
||||
"routing": {
|
||||
"safe_route": "global-safe-route",
|
||||
"nsfw_route": "fake-nsfw-route",
|
||||
"adult_auto_route_enabled": True,
|
||||
"entrypoints": {"web": True},
|
||||
},
|
||||
},
|
||||
"chat_access": {"adult_user_allowlist": ["material-manager"]},
|
||||
}
|
||||
|
||||
|
||||
def _material_manager_context() -> RequestContext:
|
||||
return RequestContext.from_astrbot(
|
||||
username="material-manager",
|
||||
user_id="material-manager",
|
||||
source="web",
|
||||
platform="webui",
|
||||
permissions={"can_manage_materials"},
|
||||
)
|
||||
|
||||
|
||||
def test_safe_validation_keeps_dedicated_route_instead_of_global_safe_route(tmp_path: Path) -> None:
|
||||
fixture = _fixture(tmp_path, config=_adult_routing_config())
|
||||
import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
||||
|
||||
detail = _run_validation(fixture, attempt)
|
||||
|
||||
assert detail["import"]["import_id"] == import_id
|
||||
assert fixture.engine.requests[-1]["model_route"] == "fake-image-route"
|
||||
assert detail["attempts"][0]["model_route"] == "fake-image-route"
|
||||
decision = detail["attempts"][0]["metadata"]["prompt_governance"]["route_decision"]
|
||||
assert decision["selected_route"] == "fake-image-route"
|
||||
assert decision["global_safe_route_ignored"] == "global-safe-route"
|
||||
|
||||
|
||||
def test_adult_validation_routes_directly_to_nsfw_for_allowlisted_manager(tmp_path: Path) -> None:
|
||||
fixture = _fixture(
|
||||
tmp_path,
|
||||
vision_prompt="adult woman over 18, nude, red silk dress with gold embroidery",
|
||||
config=_adult_routing_config(),
|
||||
)
|
||||
_import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
||||
|
||||
detail = _run_validation(fixture, attempt)
|
||||
|
||||
assert fixture.engine.requests[-1]["model_route"] == "fake-nsfw-route"
|
||||
assert detail["attempts"][0]["model_route"] == "fake-nsfw-route"
|
||||
assert detail["attempts"][0]["metadata"]["effective_model_route"] == "fake-nsfw-route"
|
||||
|
||||
|
||||
def test_terror_or_extreme_violence_is_blocked_before_provider_call(tmp_path: Path) -> None:
|
||||
fixture = _fixture(
|
||||
tmp_path,
|
||||
vision_prompt="how to kill a real person with torture gore",
|
||||
config=_adult_routing_config(),
|
||||
)
|
||||
_import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
||||
task = fixture.queue.get_job(attempt["task_id"])
|
||||
|
||||
with pytest.raises(ApiError) as captured:
|
||||
fixture.service.execute_task(task, RequestContext.system("advanced-material-worker"))
|
||||
|
||||
assert captured.value.details["moderation_level"] == "violence_prohibited"
|
||||
assert fixture.image.requests == []
|
||||
|
||||
|
||||
def test_generic_content_safety_failure_does_not_blindly_route_to_nsfw(tmp_path: Path) -> None:
|
||||
fixture = _fixture(
|
||||
tmp_path,
|
||||
image_failures=1,
|
||||
image_failure_message="provider content safety rejection",
|
||||
image_failure_error_type="http_error",
|
||||
image_failure_status_code=400,
|
||||
config=_adult_routing_config(),
|
||||
)
|
||||
_import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
||||
task = fixture.queue.get_job(attempt["task_id"])
|
||||
|
||||
with pytest.raises(ImageProviderError):
|
||||
fixture.service.execute_task(task, RequestContext.system("advanced-material-worker"))
|
||||
|
||||
assert len(fixture.image.requests) == 1
|
||||
assert len(fixture.engine.requests) == 1
|
||||
|
||||
|
||||
def test_explicit_nsfw_provider_rejection_falls_back_once_and_attributes_route(tmp_path: Path) -> None:
|
||||
fixture = _fixture(
|
||||
tmp_path,
|
||||
image_failures=1,
|
||||
image_failure_message="NSFW content policy blocked prompt",
|
||||
image_failure_error_type="http_error",
|
||||
image_failure_status_code=400,
|
||||
config=_adult_routing_config(),
|
||||
)
|
||||
_import_id, attempt = _upload_and_run_recognition(fixture, _material_manager_context())
|
||||
|
||||
detail = _run_validation(fixture, attempt)
|
||||
|
||||
assert len(fixture.image.requests) == 2
|
||||
assert [request["model_route"] for request in fixture.engine.requests] == [
|
||||
"fake-image-route",
|
||||
"fake-nsfw-route",
|
||||
]
|
||||
completed = detail["attempts"][0]
|
||||
assert completed["model_route"] == "fake-nsfw-route"
|
||||
assert completed["metadata"]["route_fallback"]["failure_class"]["safety_reason"] == "adult"
|
||||
|
||||
|
||||
def test_late_validation_result_cannot_revive_cancelled_import_and_files_are_removed(tmp_path: Path) -> None:
|
||||
fixture = _fixture(tmp_path)
|
||||
ctx = RequestContext.system("advanced-material-cancel")
|
||||
import_id, attempt = _upload_and_run_recognition(fixture, ctx)
|
||||
task = fixture.queue.get_job(attempt["task_id"])
|
||||
fixture.image.on_generate = lambda: fixture.service.cancel_import(import_id, ctx)
|
||||
|
||||
with pytest.raises(AdvancedMaterialTaskCancelled):
|
||||
fixture.service.execute_task(task, RequestContext.system("advanced-material-worker"))
|
||||
|
||||
detail = fixture.service.get_import(import_id)
|
||||
cancelled = detail["attempts"][0]
|
||||
assert detail["import"]["status"] == "cancelled"
|
||||
assert cancelled["status"] == "cancelled"
|
||||
assert cancelled["usage"] == {"images": 1}
|
||||
attempt_root = fixture.storage_root / "advanced_materials" / "attempts"
|
||||
assert not attempt_root.exists() or not any(path.is_file() for path in attempt_root.rglob("*"))
|
||||
|
||||
|
||||
def test_negative_constraints_do_not_trigger_positive_prompt_rules() -> None:
|
||||
moderation = PromptModerationService({}).moderate(
|
||||
original_prompt="adult fashion portrait in a red silk dress",
|
||||
final_prompt="adult fashion portrait in a red silk dress",
|
||||
negative_prompt="nudity, gore, child, watermark",
|
||||
)
|
||||
|
||||
assert moderation.level == "safe"
|
||||
|
||||
|
||||
def test_enabled_llm_review_without_reviewer_fails_closed() -> None:
|
||||
moderation = PromptModerationService(
|
||||
{"llm_review_enabled": True, "failure_policy": "fail_closed"}
|
||||
).moderate(
|
||||
original_prompt="safe fashion portrait",
|
||||
final_prompt="safe fashion portrait",
|
||||
)
|
||||
|
||||
assert moderation.level == "prohibited"
|
||||
assert "moderation_service_unavailable" in moderation.reason_codes
|
||||
|
||||
|
||||
class _DenyCostGuard:
|
||||
def check_generation_budget(self, **_payload: Any) -> dict[str, Any]:
|
||||
return {"allowed": False, "violations": ["task_budget_exceeded"]}
|
||||
|
||||
|
||||
def test_advanced_material_budget_guard_blocks_before_provider_call(tmp_path: Path) -> None:
|
||||
fixture = _fixture(
|
||||
tmp_path,
|
||||
config={
|
||||
"advanced_materials": {
|
||||
"cost_budget": {
|
||||
"enabled": True,
|
||||
"days": 30,
|
||||
"max_task_count": 1,
|
||||
}
|
||||
}
|
||||
},
|
||||
)
|
||||
fixture.service.cost_guard_service = _DenyCostGuard() # type: ignore[assignment]
|
||||
_import_id, attempt = _upload_and_run_recognition(fixture)
|
||||
task = fixture.queue.get_job(attempt["task_id"])
|
||||
|
||||
with pytest.raises(ApiError) as captured:
|
||||
fixture.service.execute_task(task, RequestContext.system("advanced-material-worker"))
|
||||
|
||||
assert "budget exceeded" in captured.value.message
|
||||
assert fixture.image.requests == []
|
||||
|
||||
|
||||
class _RouteServiceStub:
|
||||
def list_imports(self, **_query: Any) -> dict[str, Any]:
|
||||
return {"items": [], "total": 0, "page": 1, "page_size": 30}
|
||||
|
||||
def list_materials(self, **_query: Any) -> dict[str, Any]:
|
||||
return {"items": [], "total": 0, "page": 1, "page_size": 30}
|
||||
|
||||
def upload(self, _payload: dict[str, Any], _ctx: RequestContext) -> dict[str, Any]:
|
||||
raise AssertionError("upload must not run without can_manage_materials")
|
||||
|
||||
def delete_import(self, import_id: str, _ctx: RequestContext) -> dict[str, Any]:
|
||||
return {"import_id": import_id, "deleted": True}
|
||||
|
||||
|
||||
def test_advanced_material_routes_enforce_manager_and_reader_permissions() -> None:
|
||||
service = _RouteServiceStub()
|
||||
anonymous = RequestContext()
|
||||
authenticated_reader = RequestContext.from_astrbot(
|
||||
username="reader",
|
||||
user_id="reader",
|
||||
permissions=set(),
|
||||
)
|
||||
manager = RequestContext.from_astrbot(
|
||||
username="manager",
|
||||
user_id="manager",
|
||||
permissions={"can_manage_materials"},
|
||||
)
|
||||
|
||||
assert list_advanced_material_imports(anonymous, service=service)["error_code"] == "UNAUTHORIZED"
|
||||
assert list_advanced_material_imports(authenticated_reader, service=service)["error_code"] == "FORBIDDEN"
|
||||
assert upload_advanced_material(authenticated_reader, {}, service=service)["error_code"] == "FORBIDDEN"
|
||||
assert delete_advanced_material_import(authenticated_reader, "import-1", service=service)["error_code"] == "FORBIDDEN"
|
||||
assert list_advanced_materials(anonymous, service=service)["error_code"] == "UNAUTHORIZED"
|
||||
assert list_advanced_materials(authenticated_reader, service=service)["ok"] is True
|
||||
assert list_advanced_material_imports(manager, service=service)["ok"] is True
|
||||
assert delete_advanced_material_import(manager, "import-1", service=service)["data"]["deleted"] is True
|
||||
|
|
@ -0,0 +1,994 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError
|
||||
from backend.dto.job_dto import GenerationTask
|
||||
from backend.dto.workflow_dto import WorkflowNode
|
||||
from backend.services.advanced_material_service import AdvancedMaterialService
|
||||
from backend.services.advanced_style_set_service import (
|
||||
STYLE_REVIEW_DIMENSIONS,
|
||||
_aggregate_style_review_feedback,
|
||||
_detailed_style_negative_prompt,
|
||||
_detailed_style_prompt,
|
||||
_normalize_style_dimension_scores,
|
||||
_scale_specific_style_prompt,
|
||||
_weighted_style_fidelity_score,
|
||||
)
|
||||
from backend.services.material_normalization_service import MaterialNormalizationService
|
||||
from backend.services.prompt_governance_service import GenerationRouteService, PromptModerationService
|
||||
from backend.services.queue_service import QueueService
|
||||
from core.audit_service import AuditService
|
||||
from core.config_service import ImageStudioConfig, StorageConfig
|
||||
from core.identity import RequestContext
|
||||
from core.model_route_chain import encode_model_route_chain
|
||||
from core.storage import VfsStorage
|
||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||||
from database.repositories.job_repository import JobRepository
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from engines.image_provider import (
|
||||
GeneratedImagePayload,
|
||||
ImageGenerationRequest,
|
||||
ImageGenerationResult,
|
||||
ImageProviderError,
|
||||
)
|
||||
from engines.single_flow import ONE_PIXEL_PNG, SingleFlowEngine
|
||||
from engines.vision_provider import VisionProviderError, VisionRecognitionRequest, VisionRecognitionResult
|
||||
|
||||
|
||||
PNG_DATA_URL = f"data:image/png;base64,{base64.b64encode(ONE_PIXEL_PNG).decode('ascii')}"
|
||||
|
||||
|
||||
class _StaticConfigService:
|
||||
def __init__(self, root: Path):
|
||||
self.config = ImageStudioConfig(
|
||||
storage=StorageConfig(
|
||||
images_path=str(root / "images"),
|
||||
thumbnails_path=str(root / "thumbnails"),
|
||||
materials_path=str(root / "materials"),
|
||||
advanced_materials_path=str(root / "advanced_materials"),
|
||||
styles_path=str(root / "styles"),
|
||||
archives_path=str(root / "archives"),
|
||||
temp_path=str(root / "temp"),
|
||||
)
|
||||
)
|
||||
|
||||
def load(self) -> ImageStudioConfig:
|
||||
return self.config
|
||||
|
||||
|
||||
class _StyleVisionProvider:
|
||||
provider_id = "style-vision"
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.requests: list[VisionRecognitionRequest] = []
|
||||
self.fail_reviews = False
|
||||
|
||||
def recognize_material(self, request: VisionRecognitionRequest) -> VisionRecognitionResult:
|
||||
self.requests.append(request)
|
||||
if request.metadata.get("style_validation_review"):
|
||||
if self.fail_reviews:
|
||||
raise VisionProviderError(
|
||||
"controlled automatic review failure",
|
||||
provider_id=self.provider_id,
|
||||
error_type="invalid_response",
|
||||
)
|
||||
return VisionRecognitionResult(
|
||||
provider_id=self.provider_id,
|
||||
model="style-vlm",
|
||||
prompt="preserve fine ink linework, rounded stylized face proportions and two-band shadows",
|
||||
negative_prompt="semi-realistic facial anatomy, thick generic outlines",
|
||||
tags=["style-fidelity-review"],
|
||||
confidence=0.88,
|
||||
description="The transferred image preserves most structural and rendering traits.",
|
||||
structured_data={
|
||||
"prompt": "preserve fine ink linework, rounded stylized face proportions and two-band shadows",
|
||||
"negative_prompt": "semi-realistic facial anatomy, thick generic outlines",
|
||||
"fidelity_score": 0.88,
|
||||
"dimension_scores": {"face_proportions": 0.84, "linework": 0.92, "shading": 0.89},
|
||||
"matches": ["fine ink linework", "two-band shadows"],
|
||||
"mismatches": ["face is slightly too realistic"],
|
||||
"correction_prompt": "rounder stylized face, smaller simplified nose and mouth",
|
||||
"negative_correction_prompt": "semi-realistic facial anatomy",
|
||||
},
|
||||
)
|
||||
is_consensus = bool(request.image_uris) or bool(request.metadata.get("consensus_id"))
|
||||
if not is_consensus:
|
||||
sample_id = str(request.metadata.get("source_id") or len(self.requests))
|
||||
prompt = f"sample style traits from {sample_id}"
|
||||
return VisionRecognitionResult(
|
||||
provider_id=self.provider_id,
|
||||
model="style-vlm",
|
||||
prompt=prompt,
|
||||
negative_prompt="photorealism, watermark",
|
||||
tags=["style", "ink", "cel-shading"],
|
||||
confidence=0.94,
|
||||
description="Observable line and coloring traits.",
|
||||
structured_data=_style_structures(prompt),
|
||||
)
|
||||
|
||||
reports = json.loads(str(request.prompt or "{}"))["source_reports"]
|
||||
source_ids = [str(item["source_id"]) for item in reports]
|
||||
instruction = str(request.metadata.get("recognition_instruction") or "")
|
||||
adjustment = "cooler shadows" if "cooler shadows" in instruction else ""
|
||||
prompt = "shared fine ink linework, flat cel colors, two-band shadows"
|
||||
negative_prompt = "photorealism, source identity, watermark"
|
||||
if adjustment:
|
||||
prompt = f"{prompt}, {adjustment}"
|
||||
if "颜色对比太过强烈" in instruction:
|
||||
prompt = "intense color contrast, extreme color dynamics, luminous highly saturated palette"
|
||||
negative_prompt = "low contrast, washed out, source identity, watermark"
|
||||
structured = _style_structures(prompt)
|
||||
structured.update(
|
||||
{
|
||||
"consensus_style": {"linework": "fine ink", "shading": "two-band cel"},
|
||||
"variable_style_features": ["accent saturation"],
|
||||
"source_content_features": ["sample identities and clothing"],
|
||||
"content_leakage_exclusions": ["source identity", "source clothing", "source scene"],
|
||||
"outlier_source_ids": [],
|
||||
"representative_source_ids": source_ids[:3],
|
||||
"consensus_confidence": 0.93,
|
||||
"feature_evidence": [
|
||||
{
|
||||
"trait": "fine ink linework",
|
||||
"supporting_source_ids": source_ids,
|
||||
"support_ratio": 1.0,
|
||||
"confidence": 0.95,
|
||||
}
|
||||
],
|
||||
"validation_cases": [
|
||||
{
|
||||
"case_key": "portrait",
|
||||
"content_prompt": "an elderly male instrument maker in a cool workshop, head portrait",
|
||||
"aspect_ratio": "1:1",
|
||||
},
|
||||
{
|
||||
"case_key": "fullbody",
|
||||
"content_prompt": "a non-human courier robot in a rainy harbor, dynamic full body",
|
||||
"aspect_ratio": "2:3",
|
||||
},
|
||||
{
|
||||
"case_key": "novel_scene",
|
||||
"content_prompt": "a child botanist on an alien meadow, wide environmental scene",
|
||||
"aspect_ratio": "16:9",
|
||||
},
|
||||
],
|
||||
}
|
||||
)
|
||||
return VisionRecognitionResult(
|
||||
provider_id=self.provider_id,
|
||||
model="style-vlm",
|
||||
prompt=prompt,
|
||||
negative_prompt=negative_prompt,
|
||||
tags=["style", "ink", "cel-shading"],
|
||||
confidence=0.93,
|
||||
description="Cross-image transferable style consensus.",
|
||||
structured_data=structured,
|
||||
)
|
||||
|
||||
|
||||
def _style_structures(prompt: str) -> dict[str, Any]:
|
||||
capsules = {
|
||||
tier: {
|
||||
"prompt_parts": [f"{prompt} / {tier}"],
|
||||
"negative_parts": ["photorealism", "watermark"],
|
||||
}
|
||||
for tier in ("compact", "standard", "detailed")
|
||||
}
|
||||
return {
|
||||
"raw_analysis": {
|
||||
"attributes": {
|
||||
"linework": {
|
||||
"value": "fine ink linework",
|
||||
"confidence": 0.95,
|
||||
"source_state": "observed",
|
||||
}
|
||||
}
|
||||
},
|
||||
"normalized_asset_profile": {
|
||||
"category": "style",
|
||||
"role": "style",
|
||||
"body_slots": ["global_style"],
|
||||
"must_preserve": ["fine ink linework", "two-band shadows"],
|
||||
},
|
||||
"generation_capsules": capsules,
|
||||
}
|
||||
|
||||
|
||||
class _StyleImageProvider:
|
||||
provider_id = "style-image"
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.requests: list[ImageGenerationRequest] = []
|
||||
self.fail_prompt_once: str | None = None
|
||||
self._failed = False
|
||||
|
||||
def generate_image(self, request: ImageGenerationRequest) -> ImageGenerationResult:
|
||||
self.requests.append(request)
|
||||
if self.fail_prompt_once and self.fail_prompt_once in request.prompt and not self._failed:
|
||||
self._failed = True
|
||||
raise ImageProviderError(
|
||||
"controlled style validation failure",
|
||||
provider_id=self.provider_id,
|
||||
error_type="timeout",
|
||||
)
|
||||
return ImageGenerationResult(
|
||||
provider_id=self.provider_id,
|
||||
model=request.model or "style-image-model",
|
||||
images=[
|
||||
GeneratedImagePayload(
|
||||
b64_json=base64.b64encode(ONE_PIXEL_PNG).decode("ascii"),
|
||||
revised_prompt=request.prompt,
|
||||
)
|
||||
],
|
||||
usage={"images": 1},
|
||||
raw_metadata={"provider_base_url": "https://style.invalid"},
|
||||
)
|
||||
|
||||
|
||||
class _Runtime:
|
||||
def __init__(self, vision: _StyleVisionProvider, image: _StyleImageProvider) -> None:
|
||||
self.vision = vision
|
||||
self.image = image
|
||||
|
||||
def vision_provider_for_route(self, _route: dict[str, Any]) -> _StyleVisionProvider:
|
||||
return self.vision
|
||||
|
||||
def image_provider_for_route(self, _route: dict[str, Any]) -> _StyleImageProvider:
|
||||
return self.image
|
||||
|
||||
|
||||
class _CapturingSingleFlowEngine(SingleFlowEngine):
|
||||
def __init__(self, **kwargs: Any) -> None:
|
||||
super().__init__(**kwargs)
|
||||
self.requests: list[dict[str, Any]] = []
|
||||
|
||||
def generate(
|
||||
self,
|
||||
node: WorkflowNode,
|
||||
task: GenerationTask,
|
||||
request: dict[str, Any],
|
||||
):
|
||||
self.requests.append(dict(request))
|
||||
return super().generate(node, task, request)
|
||||
|
||||
|
||||
class _RouteService:
|
||||
def resolve_route(self, route_name: str, *, modality: str | None = None) -> dict[str, Any]:
|
||||
if modality != "image_generation":
|
||||
raise AssertionError(f"unexpected modality: {modality}")
|
||||
return {
|
||||
"route_name": route_name,
|
||||
"status": "resolved",
|
||||
"provider_id": "style-image",
|
||||
"model_id": "style-image-model",
|
||||
"provider": {
|
||||
"provider_id": "style-image",
|
||||
"base_url": "https://style.invalid",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@dataclass
|
||||
class _Fixture:
|
||||
database: Database
|
||||
service: AdvancedMaterialService
|
||||
queue: QueueService
|
||||
materials: MaterialRepository
|
||||
advanced: AdvancedMaterialRepository
|
||||
storage_root: Path
|
||||
vision: _StyleVisionProvider
|
||||
image: _StyleImageProvider
|
||||
engine: _CapturingSingleFlowEngine
|
||||
style_category_id: str
|
||||
other_category_id: str
|
||||
|
||||
|
||||
def _fixture(root: Path) -> _Fixture:
|
||||
database = Database(
|
||||
DatabaseSettings(
|
||||
backend="sqlite",
|
||||
sqlite=SqliteSettings(path=str(root / "advanced-style-set.sqlite3")),
|
||||
)
|
||||
)
|
||||
database.initialize()
|
||||
audit = AuditService(database)
|
||||
queue = QueueService(JobRepository(database), audit)
|
||||
materials = MaterialRepository(database)
|
||||
style = materials.create_category(
|
||||
{
|
||||
"category_id": "cat_test_style",
|
||||
"name": "style",
|
||||
"sort_order": 1,
|
||||
"metadata": {"frontend_group": "style"},
|
||||
}
|
||||
)
|
||||
other = materials.create_category(
|
||||
{
|
||||
"category_id": "cat_test_garment",
|
||||
"name": "garment",
|
||||
"sort_order": 2,
|
||||
"metadata": {"frontend_group": "clothing"},
|
||||
}
|
||||
)
|
||||
advanced = AdvancedMaterialRepository(database)
|
||||
storage_root = root / "vfs"
|
||||
storage = VfsStorage(_StaticConfigService(storage_root)) # type: ignore[arg-type]
|
||||
vision = _StyleVisionProvider()
|
||||
image = _StyleImageProvider()
|
||||
runtime = _Runtime(vision, image)
|
||||
engine = _CapturingSingleFlowEngine(
|
||||
storage=storage,
|
||||
image_provider_factory=runtime.image_provider_for_route,
|
||||
allow_mock_fallback=False,
|
||||
)
|
||||
config = {
|
||||
"model_assignments": {
|
||||
"advanced_material_recognition": "style-vision-route",
|
||||
"advanced_material_validation_generation": "style-image-route",
|
||||
}
|
||||
}
|
||||
service = AdvancedMaterialService(
|
||||
advanced,
|
||||
materials,
|
||||
queue,
|
||||
audit,
|
||||
_RouteService(), # type: ignore[arg-type]
|
||||
runtime, # type: ignore[arg-type]
|
||||
config=config,
|
||||
storage=storage,
|
||||
single_flow_engine=engine,
|
||||
prompt_moderation_service=PromptModerationService({}),
|
||||
generation_route_service=GenerationRouteService({}),
|
||||
)
|
||||
return _Fixture(
|
||||
database=database,
|
||||
service=service,
|
||||
queue=queue,
|
||||
materials=materials,
|
||||
advanced=advanced,
|
||||
storage_root=storage_root,
|
||||
vision=vision,
|
||||
image=image,
|
||||
engine=engine,
|
||||
style_category_id=style.category_id,
|
||||
other_category_id=other.category_id,
|
||||
)
|
||||
|
||||
|
||||
def _images(count: int) -> list[dict[str, Any]]:
|
||||
return [
|
||||
{
|
||||
"filename": f"style-{index + 1}.png",
|
||||
"content_type": "image/png",
|
||||
"image_data_url": PNG_DATA_URL,
|
||||
"upload_source": "file",
|
||||
}
|
||||
for index in range(count)
|
||||
]
|
||||
|
||||
|
||||
def _upload(fixture: _Fixture, count: int = 3) -> dict[str, Any]:
|
||||
return fixture.service.upload_style_set(
|
||||
{
|
||||
"display_name": "Parallel style set",
|
||||
"category_id": fixture.style_category_id,
|
||||
"images": _images(count),
|
||||
},
|
||||
RequestContext.system("advanced-style-test"),
|
||||
)
|
||||
|
||||
|
||||
def _execute_source_stage(fixture: _Fixture, count: int = 3) -> tuple[str, Any]:
|
||||
uploaded = _upload(fixture, count)
|
||||
for item in uploaded["tasks"]:
|
||||
task = fixture.queue.get_job(item["task_id"])
|
||||
fixture.service.execute_task(task, RequestContext.system("advanced-style-worker"))
|
||||
import_id = uploaded["import"]["import_id"]
|
||||
consensus = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||||
if consensus is None:
|
||||
raise AssertionError("source barrier did not create a style consensus")
|
||||
return import_id, consensus
|
||||
|
||||
|
||||
def _execute_consensus(fixture: _Fixture, consensus: Any) -> tuple[Any, list[Any]]:
|
||||
task = fixture.queue.get_job(consensus.task_id)
|
||||
fixture.service.execute_task(task, RequestContext.system("advanced-style-worker"))
|
||||
record = fixture.advanced.get_import(consensus.import_id)
|
||||
if record is None or not record.current_attempt_id:
|
||||
raise AssertionError("consensus did not create a validation batch")
|
||||
attempt = fixture.advanced.get_attempt(record.current_attempt_id)
|
||||
cases = fixture.advanced.list_style_validation_cases(record.current_attempt_id)
|
||||
if attempt is None:
|
||||
raise AssertionError("validation attempt is missing")
|
||||
return attempt, cases
|
||||
|
||||
|
||||
def _execute_validation_cases(fixture: _Fixture, cases: list[Any]) -> None:
|
||||
for case in cases:
|
||||
task = fixture.queue.get_job(case.task_id)
|
||||
fixture.service.execute_task(task, RequestContext.system("advanced-style-worker"))
|
||||
|
||||
|
||||
class AdvancedStyleSetWorkflowTests(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.temporary = tempfile.TemporaryDirectory(prefix="imagestudio-style-set-")
|
||||
self.root = Path(self.temporary.name)
|
||||
|
||||
def tearDown(self) -> None:
|
||||
self.temporary.cleanup()
|
||||
|
||||
def test_style_only_boundaries_and_upload_creates_independent_source_tasks(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
ctx = RequestContext.system("advanced-style-boundaries")
|
||||
|
||||
with self.assertRaises(ApiError):
|
||||
fixture.service.upload_style_set(
|
||||
{
|
||||
"display_name": "not style",
|
||||
"category_id": fixture.other_category_id,
|
||||
"images": _images(2),
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
with self.assertRaises(ApiError):
|
||||
fixture.service.upload_style_set(
|
||||
{"display_name": "too few", "category_id": fixture.style_category_id, "images": _images(1)},
|
||||
ctx,
|
||||
)
|
||||
with self.assertRaises(ApiError):
|
||||
fixture.service.upload_style_set(
|
||||
{"display_name": "too many", "category_id": fixture.style_category_id, "images": _images(9)},
|
||||
ctx,
|
||||
)
|
||||
|
||||
minimum = _upload(fixture, 2)
|
||||
maximum = _upload(fixture, 8)
|
||||
self.assertEqual(len(minimum["sources"]), 2)
|
||||
self.assertEqual(len(maximum["sources"]), 8)
|
||||
self.assertEqual(len(maximum["tasks"]), 8)
|
||||
self.assertEqual(len({item["source_id"] for item in maximum["sources"]}), 8)
|
||||
self.assertEqual(len({item["task_id"] for item in maximum["tasks"]}), 8)
|
||||
self.assertTrue(all(item["task_type"] == "advanced_style_source_recognition" for item in maximum["tasks"]))
|
||||
|
||||
def test_consensus_reads_all_sources_and_creates_exactly_three_prompt_only_cases(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
import_id, consensus = _execute_source_stage(fixture, 3)
|
||||
|
||||
source_requests = fixture.vision.requests[:3]
|
||||
self.assertEqual(len(source_requests), 3)
|
||||
self.assertTrue(all(len(item.image_uris) == 0 for item in source_requests))
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
|
||||
with self.assertRaises(ApiError):
|
||||
fixture.service.create_style_consensus(
|
||||
import_id,
|
||||
{},
|
||||
RequestContext.system("advanced-style-overlap-guard"),
|
||||
)
|
||||
|
||||
consensus_request = fixture.vision.requests[-1]
|
||||
self.assertEqual(1 + len(consensus_request.image_uris), 3)
|
||||
reports = json.loads(str(consensus_request.prompt))["source_reports"]
|
||||
self.assertEqual({item["source_id"] for item in reports}, set(consensus.source_ids))
|
||||
self.assertEqual(len(cases), 3)
|
||||
self.assertEqual([item.case_key for item in cases], ["portrait", "fullbody", "novel_scene"])
|
||||
self.assertEqual(len({item.task_id for item in cases}), 3)
|
||||
self.assertTrue(all("source clothing" not in str(item.negative_prompt or "") for item in cases))
|
||||
self.assertTrue(all(item.final_prompt.count("shared fine ink linework") == 1 for item in cases))
|
||||
|
||||
_execute_validation_cases(fixture, cases)
|
||||
|
||||
self.assertEqual(len(fixture.engine.requests), 3)
|
||||
for request in fixture.engine.requests:
|
||||
self.assertNotIn("image", request)
|
||||
self.assertNotIn("input_images", request)
|
||||
self.assertEqual(request["output_scope"]["kind"], "advanced_style_validation")
|
||||
self.assertEqual(len(fixture.image.requests), 3)
|
||||
self.assertTrue(all(item.input_images == () for item in fixture.image.requests))
|
||||
self.assertTrue(all("/ detailed" in item["expanded_prompt"] for item in fixture.engine.requests))
|
||||
completed_cases = fixture.advanced.list_style_validation_cases(attempt.attempt_id)
|
||||
self.assertTrue(
|
||||
all(
|
||||
str(item.validation_image_uri).startswith("storage://advanced_materials/style-validations/")
|
||||
for item in completed_cases
|
||||
)
|
||||
)
|
||||
self.assertTrue(all(0.0 <= float(item.fidelity_score or 0.0) <= 1.0 for item in completed_cases))
|
||||
self.assertTrue(
|
||||
all(item.style_review["score_calibration"]["source"] == "server_weighted_dimensions" for item in completed_cases)
|
||||
)
|
||||
self.assertTrue(all(item.style_review.get("status") == "succeeded" for item in completed_cases))
|
||||
review_requests = [item for item in fixture.vision.requests if item.metadata.get("style_validation_review")]
|
||||
self.assertEqual(len(review_requests), 3)
|
||||
self.assertTrue(all(1 + len(item.image_uris) == 4 for item in review_requests))
|
||||
reviewed_attempt = fixture.advanced.get_attempt(attempt.attempt_id)
|
||||
expected_average = sum(float(item.fidelity_score or 0.0) for item in completed_cases) / len(completed_cases)
|
||||
self.assertAlmostEqual(float(reviewed_attempt.fidelity_score or 0.0), expected_average)
|
||||
self.assertFalse((fixture.storage_root / "images").exists())
|
||||
self.assertFalse((fixture.storage_root / "thumbnails").exists())
|
||||
record = fixture.advanced.get_import(import_id)
|
||||
self.assertIsNotNone(record)
|
||||
self.assertEqual(record.status, "awaiting_review")
|
||||
|
||||
def test_style_normalization_preserves_profile_attributes_and_intact_capsule_phrases(self) -> None:
|
||||
service = MaterialNormalizationService()
|
||||
phrase = "highly glossy, sharp specular highlights on hair, skin, and eyes"
|
||||
payload = {
|
||||
"raw_analysis": {"style_extraction": {"medium": "clean digital illustration"}},
|
||||
"normalized_asset_profile": {
|
||||
"category": "style",
|
||||
"role": "style",
|
||||
"body_slots": ["global_style"],
|
||||
"attributes": {
|
||||
"face_proportions": {
|
||||
"value": "rounded short face, large eye-to-face ratio, tiny simplified nose and mouth",
|
||||
"confidence": 0.96,
|
||||
"source": "consensus",
|
||||
},
|
||||
"linework": {
|
||||
"value": "fine tapered locally colored lines",
|
||||
"confidence": 0.95,
|
||||
"source": "consensus",
|
||||
},
|
||||
},
|
||||
},
|
||||
"generation_capsules": {
|
||||
tier: {"prompt_parts": [phrase], "negative_parts": ["semi-realistic facial anatomy"]}
|
||||
for tier in ("compact", "standard", "detailed")
|
||||
},
|
||||
}
|
||||
|
||||
result = service.normalize_recognition(
|
||||
payload,
|
||||
prompt="specific stylized illustration grammar",
|
||||
negative_prompt="semi-realistic facial anatomy",
|
||||
tags=["style"],
|
||||
confidence=0.96,
|
||||
provider_id="test-vlm",
|
||||
model="test-style-model",
|
||||
route_name="test-style-route",
|
||||
category_hint="style",
|
||||
)
|
||||
|
||||
profile = result["normalized_asset_profile"]
|
||||
self.assertIn("face_proportions", profile["attributes"])
|
||||
self.assertIn("linework", profile["attributes"])
|
||||
self.assertTrue(any("face proportions" in item for item in profile["must_preserve"]))
|
||||
self.assertEqual(result["generation_capsules"]["detailed"]["prompt_parts"], [phrase])
|
||||
|
||||
def test_one_validation_case_failure_is_isolated_and_retryable(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
import_id, consensus = _execute_source_stage(fixture, 3)
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
fixture.image.fail_prompt_once = "non-human courier robot"
|
||||
|
||||
failed_task_id = ""
|
||||
for case in cases:
|
||||
task = fixture.queue.get_job(case.task_id)
|
||||
try:
|
||||
fixture.service.execute_task(task, RequestContext.system("advanced-style-worker"))
|
||||
except ImageProviderError as exc:
|
||||
failed_task_id = task.task_id
|
||||
fixture.service.handle_task_failure(task, exc, RequestContext.system("advanced-style-worker"))
|
||||
|
||||
self.assertTrue(failed_task_id)
|
||||
current = fixture.advanced.list_style_validation_cases(attempt.attempt_id)
|
||||
self.assertEqual([item.status for item in current].count("failed"), 1)
|
||||
self.assertEqual([item.status for item in current].count("succeeded"), 2)
|
||||
self.assertEqual(fixture.advanced.get_import(import_id).status, "validation_partial_failed")
|
||||
failed = next(item for item in current if item.status == "failed")
|
||||
|
||||
retried = fixture.service.retry_style_validation_case(
|
||||
import_id,
|
||||
failed.case_id,
|
||||
RequestContext.system("advanced-style-retry"),
|
||||
)
|
||||
self.assertNotEqual(retried["task"]["task_id"], failed_task_id)
|
||||
self.assertEqual(retried["case"]["metadata"]["retry_count"], 1)
|
||||
old_task = fixture.queue.get_job(failed_task_id)
|
||||
fixture.service.handle_task_failure(
|
||||
old_task,
|
||||
RuntimeError("late failure from superseded validation task"),
|
||||
RequestContext.system("advanced-style-stale-worker"),
|
||||
)
|
||||
still_retried = fixture.advanced.get_style_validation_case(failed.case_id)
|
||||
self.assertEqual(still_retried.status, "queued")
|
||||
self.assertEqual(still_retried.task_id, retried["task"]["task_id"])
|
||||
retry_task = fixture.queue.get_job(retried["task"]["task_id"])
|
||||
fixture.service.execute_task(retry_task, RequestContext.system("advanced-style-worker"))
|
||||
|
||||
self.assertEqual(fixture.advanced.get_import(import_id).status, "awaiting_review")
|
||||
self.assertTrue(
|
||||
all(item.status == "succeeded" for item in fixture.advanced.list_style_validation_cases(attempt.attempt_id))
|
||||
)
|
||||
|
||||
def test_automatic_style_review_failure_never_discards_generated_validation_images(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
fixture.vision.fail_reviews = True
|
||||
import_id, consensus = _execute_source_stage(fixture, 3)
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
|
||||
_execute_validation_cases(fixture, cases)
|
||||
|
||||
completed = fixture.advanced.list_style_validation_cases(attempt.attempt_id)
|
||||
self.assertTrue(all(item.status == "succeeded" and item.validation_image_uri for item in completed))
|
||||
self.assertTrue(all(item.style_review.get("status") == "review_failed" for item in completed))
|
||||
self.assertTrue(all(item.fidelity_score is None for item in completed))
|
||||
self.assertEqual(fixture.advanced.get_import(import_id).status, "awaiting_review")
|
||||
|
||||
def test_rejection_creates_new_consensus_revision_and_rebuilds_capsules(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
import_id, consensus = _execute_source_stage(fixture, 3)
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
_execute_validation_cases(fixture, cases)
|
||||
first_consensus = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||||
first_capsule = first_consensus.result["generation_capsules"]["detailed"]["prompt_parts"]
|
||||
first_prompts = [item.content_prompt for item in cases]
|
||||
|
||||
rebuilt = fixture.service.reject_attempt(
|
||||
import_id,
|
||||
attempt.attempt_id,
|
||||
{"style_adjustment_text": "use cooler shadows", "decision_notes": "shadows are too warm"},
|
||||
RequestContext.system("advanced-style-reviewer"),
|
||||
)
|
||||
consensus_task = fixture.queue.get_job(rebuilt["task"]["task_id"])
|
||||
fixture.service.execute_task(consensus_task, RequestContext.system("advanced-style-worker"))
|
||||
|
||||
latest = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||||
self.assertEqual(latest.revision, first_consensus.revision + 1)
|
||||
self.assertIn("cooler shadows", latest.prompt)
|
||||
second_capsule = latest.result["generation_capsules"]["detailed"]["prompt_parts"]
|
||||
self.assertNotEqual(first_capsule, second_capsule)
|
||||
self.assertTrue(any("cooler shadows" in item for item in second_capsule))
|
||||
record = fixture.advanced.get_import(import_id)
|
||||
next_cases = fixture.advanced.list_style_validation_cases(record.current_attempt_id)
|
||||
self.assertEqual([item.content_prompt for item in next_cases], first_prompts)
|
||||
self.assertEqual([item.seed for item in next_cases], [item.seed for item in cases])
|
||||
self.assertEqual(fixture.advanced.get_attempt(attempt.attempt_id).status, "rejected")
|
||||
|
||||
def test_style_feedback_compares_previous_images_and_cannot_invert_decrease_direction(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
import_id, consensus = _execute_source_stage(fixture, 3)
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
_execute_validation_cases(fixture, cases)
|
||||
|
||||
rebuilt = fixture.service.reject_attempt(
|
||||
import_id,
|
||||
attempt.attempt_id,
|
||||
{"style_adjustment_text": "颜色对比太过强烈"},
|
||||
RequestContext.system("advanced-style-direction-guard"),
|
||||
)
|
||||
consensus_task = fixture.queue.get_job(rebuilt["task"]["task_id"])
|
||||
fixture.service.execute_task(consensus_task, RequestContext.system("advanced-style-worker"))
|
||||
|
||||
request = fixture.vision.requests[-1]
|
||||
self.assertEqual(1 + len(request.image_uris), 6)
|
||||
manifest = request.metadata["image_manifest"]
|
||||
self.assertEqual([item["kind"] for item in manifest].count("source_reference"), 3)
|
||||
self.assertEqual([item["kind"] for item in manifest].count("previous_validation"), 3)
|
||||
request_payload = json.loads(str(request.prompt))
|
||||
self.assertEqual(len(request_payload["previous_validation"]["validation_cases"]), 3)
|
||||
self.assertEqual(request_payload["style_adjustment_plan"]["decrease"], ["color contrast"])
|
||||
|
||||
latest = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||||
self.assertIsNotNone(latest)
|
||||
positive = str(latest.prompt or "").casefold()
|
||||
negative = str(latest.negative_prompt or "").casefold()
|
||||
self.assertNotIn("intense color contrast", positive)
|
||||
self.assertNotIn("extreme color dynamics", positive)
|
||||
self.assertNotIn("high contrast", positive)
|
||||
self.assertIn("balanced moderate color contrast", positive)
|
||||
self.assertIn("restrained color separation", positive)
|
||||
self.assertNotIn("low contrast", negative)
|
||||
self.assertNotIn("washed out", negative)
|
||||
self.assertIn("excessive color contrast", negative)
|
||||
self.assertEqual(latest.result["human_adjustments"]["style_adjustment_plan"]["decrease"], ["color contrast"])
|
||||
self.assertTrue(latest.result["human_adjustments"]["guard"]["applied"])
|
||||
self.assertEqual(len(latest.result["comparison_context"]["validation_cases"]), 3)
|
||||
detailed = latest.result["generation_capsules"]["detailed"]
|
||||
self.assertIn("balanced moderate color contrast", detailed["prompt_parts"])
|
||||
self.assertIn("excessive color contrast", detailed["negative_parts"])
|
||||
|
||||
record = fixture.advanced.get_import(import_id)
|
||||
next_cases = fixture.advanced.list_style_validation_cases(record.current_attempt_id)
|
||||
self.assertTrue(all("balanced moderate color contrast" in item.final_prompt for item in next_cases))
|
||||
start = len(fixture.engine.requests)
|
||||
_execute_validation_cases(fixture, next_cases)
|
||||
generated = fixture.engine.requests[start:]
|
||||
self.assertEqual(len(generated), 3)
|
||||
self.assertTrue(all("balanced moderate color contrast" in item["expanded_prompt"] for item in generated))
|
||||
self.assertTrue(all("excessive color contrast" in item["expanded_negative_prompt"] for item in generated))
|
||||
|
||||
def test_validation_content_feedback_rebuilds_cases_and_changes_seeds(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
import_id, consensus = _execute_source_stage(fixture, 3)
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
_execute_validation_cases(fixture, cases)
|
||||
first_prompts = [item.content_prompt for item in cases]
|
||||
first_seeds = [item.seed for item in cases]
|
||||
instruction = "女性角色,全身,萌袖卫衣、泡泡袜和运动鞋,白发蓝瞳猫耳"
|
||||
|
||||
rebuilt = fixture.service.reject_attempt(
|
||||
import_id,
|
||||
attempt.attempt_id,
|
||||
{"validation_instruction": instruction, "decision_notes": "第一轮角色差异过大"},
|
||||
RequestContext.system("advanced-style-reviewer"),
|
||||
)
|
||||
consensus_task = fixture.queue.get_job(rebuilt["task"]["task_id"])
|
||||
fixture.service.execute_task(consensus_task, RequestContext.system("advanced-style-worker"))
|
||||
|
||||
latest = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||||
self.assertEqual(latest.metadata["validation_instruction"], instruction)
|
||||
record = fixture.advanced.get_import(import_id)
|
||||
next_attempt = fixture.advanced.get_attempt(record.current_attempt_id)
|
||||
next_cases = fixture.advanced.list_style_validation_cases(record.current_attempt_id)
|
||||
self.assertNotEqual([item.content_prompt for item in next_cases], first_prompts)
|
||||
self.assertNotEqual([item.seed for item in next_cases], first_seeds)
|
||||
self.assertTrue(all(instruction in item.content_prompt for item in next_cases))
|
||||
self.assertTrue(all(instruction in item.final_prompt for item in next_cases))
|
||||
self.assertEqual(next_attempt.metadata["validation_instruction"], instruction)
|
||||
self.assertIn("验证内容", next_attempt.adjustment_text)
|
||||
request_count = len(fixture.engine.requests)
|
||||
_execute_validation_cases(fixture, next_cases)
|
||||
next_requests = fixture.engine.requests[request_count:]
|
||||
self.assertEqual(len(next_requests), 3)
|
||||
self.assertTrue(all(instruction in item["expanded_prompt"] for item in next_requests))
|
||||
self.assertTrue(all(item["seed"] in {case.seed for case in next_cases} for item in next_requests))
|
||||
|
||||
def test_style_attempt_listing_includes_all_three_validation_images(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
import_id, consensus = _execute_source_stage(fixture, 3)
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
_execute_validation_cases(fixture, cases)
|
||||
|
||||
listed = fixture.service.list_attempts(import_id)
|
||||
current = next(item for item in listed if item["attempt_id"] == attempt.attempt_id)
|
||||
self.assertEqual(len(current["validation_cases"]), 3)
|
||||
self.assertTrue(all(item["validation_image_uri"] for item in current["validation_cases"]))
|
||||
detail = fixture.service.get_import(import_id)
|
||||
detail_attempt = next(item for item in detail["attempts"] if item["attempt_id"] == attempt.attempt_id)
|
||||
self.assertEqual(len(detail_attempt["validation_cases"]), 3)
|
||||
|
||||
def test_legacy_adjustment_text_is_never_silently_discarded(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
import_id, consensus = _execute_source_stage(fixture, 3)
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
_execute_validation_cases(fixture, cases)
|
||||
legacy_text = "use a white-haired blue-eyed cat-eared woman in the next validation"
|
||||
|
||||
rebuilt = fixture.service.reject_attempt(
|
||||
import_id,
|
||||
attempt.attempt_id,
|
||||
{"adjustment_text": legacy_text},
|
||||
RequestContext.system("legacy-client"),
|
||||
)
|
||||
consensus_task = fixture.queue.get_job(rebuilt["task"]["task_id"])
|
||||
fixture.service.execute_task(consensus_task, RequestContext.system("advanced-style-worker"))
|
||||
|
||||
latest = fixture.advanced.get_latest_style_consensus_run(import_id)
|
||||
next_cases = fixture.advanced.list_style_validation_cases(fixture.advanced.get_import(import_id).current_attempt_id)
|
||||
self.assertEqual(latest.metadata["style_adjustment_text"], legacy_text)
|
||||
self.assertEqual(latest.metadata["validation_instruction"], legacy_text)
|
||||
self.assertTrue(all(legacy_text in item.final_prompt for item in next_cases))
|
||||
|
||||
def test_three_successful_cases_seal_one_advanced_material_idempotently(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
import_id, consensus = _execute_source_stage(fixture, 3)
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
_execute_validation_cases(fixture, cases)
|
||||
ctx = RequestContext.system("advanced-style-accept")
|
||||
|
||||
accepted = fixture.service.accept_attempt(
|
||||
import_id,
|
||||
attempt.attempt_id,
|
||||
{"fidelity_score": 0.94, "decision_notes": "style transfers across all scenes"},
|
||||
ctx,
|
||||
)
|
||||
replay = fixture.service.accept_attempt(import_id, attempt.attempt_id, {}, ctx)
|
||||
|
||||
self.assertEqual(accepted["material"]["material_id"], replay["material"]["material_id"])
|
||||
self.assertTrue(accepted["material"]["material_id"].startswith("advmat_"))
|
||||
self.assertEqual(accepted["material"]["priority"], 100)
|
||||
self.assertEqual(len(accepted["material"]["metadata"]["style_set"]["source_ids"]), 3)
|
||||
self.assertEqual(len(accepted["material"]["metadata"]["style_set"]["validation_cases"]), 3)
|
||||
advanced_items, advanced_total = fixture.advanced.list_materials(page=1, page_size=10)
|
||||
standard_items, standard_total = fixture.materials.list_materials(page=1, page_size=10)
|
||||
self.assertEqual(advanced_total, 1)
|
||||
self.assertEqual([item.material_id for item in advanced_items], [accepted["material"]["material_id"]])
|
||||
self.assertEqual(standard_total, 0)
|
||||
self.assertEqual(standard_items, [])
|
||||
|
||||
def test_queue_write_failures_leave_sources_and_validation_cases_retryable(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
original_create_task = fixture.queue.create_task
|
||||
|
||||
def reject_source_tasks(payload: dict[str, Any], ctx: RequestContext):
|
||||
if payload.get("task_type") == "advanced_style_source_recognition":
|
||||
raise RuntimeError("controlled source queue failure")
|
||||
return original_create_task(payload, ctx)
|
||||
|
||||
fixture.queue.create_task = reject_source_tasks # type: ignore[method-assign]
|
||||
uploaded = _upload(fixture, 2)
|
||||
self.assertEqual(uploaded["tasks"], [])
|
||||
self.assertEqual(uploaded["import"]["status"], "style_source_review_required")
|
||||
self.assertTrue(all(item["status"] == "failed" for item in uploaded["sources"]))
|
||||
|
||||
validation_root = self.root / "validation-queue"
|
||||
validation_root.mkdir(parents=True, exist_ok=True)
|
||||
fixture = _fixture(validation_root)
|
||||
import_id, consensus = _execute_source_stage(fixture, 2)
|
||||
original_create_task = fixture.queue.create_task
|
||||
|
||||
def reject_validation_tasks(payload: dict[str, Any], ctx: RequestContext):
|
||||
if payload.get("task_type") == "advanced_style_validation_generation":
|
||||
raise RuntimeError("controlled validation queue failure")
|
||||
return original_create_task(payload, ctx)
|
||||
|
||||
fixture.queue.create_task = reject_validation_tasks # type: ignore[method-assign]
|
||||
attempt, cases = _execute_consensus(fixture, consensus)
|
||||
self.assertEqual(attempt.status, "validation_partial_failed")
|
||||
self.assertTrue(all(item.status == "failed" and item.task_id is None for item in cases))
|
||||
self.assertEqual(fixture.advanced.get_import(import_id).status, "validation_partial_failed")
|
||||
|
||||
def test_retry_source_refreshes_obsolete_route_for_source_and_consensus(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
uploaded = _upload(fixture, 2)
|
||||
import_id = uploaded["import"]["import_id"]
|
||||
source_id = uploaded["sources"][0]["source_id"]
|
||||
fixture.advanced.update_style_source(
|
||||
source_id,
|
||||
{"status": "failed", "task_id": None, "error": {"error_type": "route_unavailable"}},
|
||||
)
|
||||
repaired_route = encode_model_route_chain(
|
||||
"plugin-vision-route",
|
||||
"astrbot:vision-provider",
|
||||
)
|
||||
fixture.service.config["model_assignments"]["advanced_material_recognition"] = repaired_route
|
||||
|
||||
retried = fixture.service.retry_style_source(
|
||||
import_id,
|
||||
source_id,
|
||||
RequestContext.system("advanced-style-route-repair"),
|
||||
)
|
||||
|
||||
source = fixture.advanced.get_style_source(source_id)
|
||||
record = fixture.advanced.get_import(import_id)
|
||||
task = fixture.queue.get_job(retried["task"]["task_id"])
|
||||
self.assertEqual(source.recognition_model_route, repaired_route)
|
||||
self.assertEqual(record.recognition_model_route, repaired_route)
|
||||
self.assertEqual(task.model_route, repaired_route)
|
||||
|
||||
def test_deleted_queue_row_is_reconciled_and_import_can_be_removed(self) -> None:
|
||||
fixture = _fixture(self.root)
|
||||
uploaded = _upload(fixture, 2)
|
||||
import_id = uploaded["import"]["import_id"]
|
||||
source_id = uploaded["sources"][0]["source_id"]
|
||||
task_id = uploaded["tasks"][0]["task_id"]
|
||||
source_before = fixture.advanced.get_style_source(source_id)
|
||||
source_path = fixture.service.storage.ensure_file(source_before.source_image_uri)
|
||||
self.assertTrue(source_path.exists())
|
||||
|
||||
fixture.queue.update_task(
|
||||
task_id,
|
||||
{"status": "failed", "error_message": "controlled queue failure"},
|
||||
RequestContext.system("advanced-style-queue-failure"),
|
||||
)
|
||||
fixture.queue.delete_job(task_id, RequestContext.system("advanced-style-queue-delete"))
|
||||
|
||||
detail = fixture.service.get_import(import_id)
|
||||
source_after = fixture.advanced.get_style_source(source_id)
|
||||
self.assertEqual(source_after.status, "failed")
|
||||
self.assertIsNone(source_after.task_id)
|
||||
self.assertEqual(source_after.error["error_type"], "queue_task_missing")
|
||||
self.assertEqual(detail["import"]["status"], "style_source_review_required")
|
||||
|
||||
deleted = fixture.service.delete_import(
|
||||
import_id,
|
||||
RequestContext.system("advanced-style-delete-import"),
|
||||
)
|
||||
self.assertTrue(deleted["deleted"])
|
||||
self.assertIsNone(fixture.advanced.get_import(import_id))
|
||||
self.assertIsNone(fixture.advanced.get_style_source(source_id))
|
||||
self.assertFalse(source_path.exists())
|
||||
|
||||
def test_style_review_scores_are_server_weighted_with_fixed_dimensions(self) -> None:
|
||||
normalized = _normalize_style_dimension_scores(
|
||||
{
|
||||
"face_proportions": 0.8,
|
||||
"iris_construction": {"score": 0.8},
|
||||
"line_art": 0.8,
|
||||
"global_palette": 0.8,
|
||||
"local_contrast": 0.8,
|
||||
"shadow_rendering": 0.8,
|
||||
"highlight_shape": 0.8,
|
||||
"body_ratio": 0.8,
|
||||
"hair_strands": 0.8,
|
||||
"detail_density": 0.8,
|
||||
"environment": 0.8,
|
||||
}
|
||||
)
|
||||
self.assertEqual(set(normalized), set(STYLE_REVIEW_DIMENSIONS))
|
||||
score, calibration = _weighted_style_fidelity_score("portrait", normalized, 0.99)
|
||||
self.assertAlmostEqual(float(score or 0.0), 0.8)
|
||||
self.assertEqual(calibration["source"], "server_weighted_dimensions")
|
||||
self.assertEqual(calibration["dimension_coverage"], 1.0)
|
||||
|
||||
def test_style_prompt_compiler_rewrites_semantic_risks_and_keeps_scale_overrides(self) -> None:
|
||||
payload = {
|
||||
"normalized_asset_profile": {
|
||||
"must_preserve": ["fine colored linework", "segmented ring halo highlight across the hair"],
|
||||
},
|
||||
"generation_capsules": {
|
||||
"detailed": {
|
||||
"prompt_parts": [
|
||||
"fine colored linework with tapered ends",
|
||||
"fine colored linework",
|
||||
"soft two-band cel shading",
|
||||
],
|
||||
"negative_parts": ["photorealism"],
|
||||
}
|
||||
},
|
||||
"prompt_priority_groups": {
|
||||
"core": ["rounded stylized facial structure"],
|
||||
"structural": ["large layered irises"],
|
||||
"rendering": ["pastel global palette with crisp local separation"],
|
||||
},
|
||||
"semantic_risk_audit": [
|
||||
{
|
||||
"original_phrase": "segmented ring halo highlight across the hair",
|
||||
"safe_rewrite": "segmented satin sheen painted directly on the curved hair surface",
|
||||
"negative_guard": "floating luminous object detached from the styled surface",
|
||||
}
|
||||
],
|
||||
"scale_capsules": {
|
||||
"fullbody": {"prompt_parts": ["readable colored silhouette lines at full-body scale"]}
|
||||
},
|
||||
}
|
||||
prompt = _detailed_style_prompt(payload, "fallback")
|
||||
negative = _detailed_style_negative_prompt(payload, "")
|
||||
self.assertNotIn("ring halo", prompt)
|
||||
self.assertIn("painted directly on the curved hair surface", prompt)
|
||||
self.assertEqual(prompt.count("fine colored linework"), 1)
|
||||
self.assertIn("detached from the styled surface", negative)
|
||||
self.assertIn("full-body scale", _scale_specific_style_prompt(payload, "fullbody"))
|
||||
|
||||
def test_review_aggregation_separates_recurrent_and_scale_specific_failures(self) -> None:
|
||||
def case(case_key: str, case_id: str, scores: dict[str, float]) -> SimpleNamespace:
|
||||
return SimpleNamespace(
|
||||
case_key=case_key,
|
||||
case_id=case_id,
|
||||
fidelity_score=0.6,
|
||||
style_review={
|
||||
"status": "succeeded",
|
||||
"fidelity_score": 0.6,
|
||||
"dimension_scores": scores,
|
||||
"style_deltas": [],
|
||||
"prompt_patch": {},
|
||||
"semantic_risks": [],
|
||||
},
|
||||
)
|
||||
|
||||
feedback = _aggregate_style_review_feedback(
|
||||
[
|
||||
case("portrait", "p", {"linework": 0.5, "eye_design": 0.5}),
|
||||
case("fullbody", "f", {"linework": 0.6, "anatomy_scale": 0.5}),
|
||||
case("novel_scene", "n", {"linework": 0.55, "background_rendering": 0.5}),
|
||||
]
|
||||
)
|
||||
self.assertEqual(feedback["recurrent_weak_dimensions"], ["linework"])
|
||||
self.assertEqual(feedback["scale_specific_weak_dimensions"]["portrait"], ["eye_design"])
|
||||
self.assertEqual(feedback["scale_specific_weak_dimensions"]["fullbody"], ["anatomy_scale"])
|
||||
self.assertEqual(feedback["scale_specific_weak_dimensions"]["novel_scene"], ["background_rendering"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
119
tests/test_advanced_style_vision.py
普通文件
119
tests/test_advanced_style_vision.py
普通文件
|
|
@ -0,0 +1,119 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
|
||||
from engines.vision_provider import (
|
||||
OpenAIVisionProvider,
|
||||
VisionProviderConfig,
|
||||
VisionRecognitionRequest,
|
||||
)
|
||||
|
||||
|
||||
def _response() -> dict:
|
||||
return {
|
||||
"choices": [
|
||||
{
|
||||
"message": {
|
||||
"content": json.dumps(
|
||||
{
|
||||
"prompt": "shared linework and cel shading",
|
||||
"tags": ["style", "cel shading"],
|
||||
"confidence": 0.92,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
def test_openai_vision_sends_ordered_deduplicated_style_images_with_limit() -> None:
|
||||
captured: dict = {}
|
||||
|
||||
def transport(url, payload, headers, timeout_seconds):
|
||||
captured.update(
|
||||
{
|
||||
"url": url,
|
||||
"payload": payload,
|
||||
"headers": headers,
|
||||
"timeout_seconds": timeout_seconds,
|
||||
}
|
||||
)
|
||||
return _response()
|
||||
|
||||
provider = OpenAIVisionProvider(
|
||||
VisionProviderConfig(
|
||||
provider_id="style-vlm",
|
||||
base_url="https://vision.example",
|
||||
vision_model="vision-model",
|
||||
),
|
||||
transport=transport,
|
||||
)
|
||||
primary = "data:image/png;base64,PRIMARY"
|
||||
request = VisionRecognitionRequest(
|
||||
display_name="style reference set",
|
||||
image_uri=primary,
|
||||
image_uris=tuple(
|
||||
[primary]
|
||||
+ [f"https://images.example/{index}.png" for index in range(1, 10)]
|
||||
),
|
||||
)
|
||||
|
||||
result = provider.recognize_material(request)
|
||||
|
||||
user_content = captured["payload"]["messages"][1]["content"]
|
||||
image_urls = [
|
||||
item["image_url"]["url"]
|
||||
for item in user_content
|
||||
if item.get("type") == "image_url"
|
||||
]
|
||||
assert image_urls == [primary] + [f"https://images.example/{index}.png" for index in range(1, 8)]
|
||||
assert result.prompt == "shared linework and cel shading"
|
||||
|
||||
|
||||
def test_openai_vision_keeps_single_image_contract_unchanged() -> None:
|
||||
captured: dict = {}
|
||||
|
||||
def transport(_url, payload, _headers, _timeout_seconds):
|
||||
captured["payload"] = payload
|
||||
return _response()
|
||||
|
||||
provider = OpenAIVisionProvider(
|
||||
VisionProviderConfig(
|
||||
provider_id="single-vlm",
|
||||
base_url="https://vision.example",
|
||||
vision_model="vision-model",
|
||||
),
|
||||
transport=transport,
|
||||
)
|
||||
provider.recognize_material(
|
||||
VisionRecognitionRequest(
|
||||
display_name="single material",
|
||||
image_uri="data:image/png;base64,SINGLE",
|
||||
)
|
||||
)
|
||||
|
||||
user_content = captured["payload"]["messages"][1]["content"]
|
||||
assert [item for item in user_content if item.get("type") == "image_url"] == [
|
||||
{"type": "image_url", "image_url": {"url": "data:image/png;base64,SINGLE"}}
|
||||
]
|
||||
|
||||
|
||||
def test_multi_image_field_does_not_shift_legacy_positional_arguments() -> None:
|
||||
request = VisionRecognitionRequest(
|
||||
"legacy material",
|
||||
"data:image/png;base64,LEGACY",
|
||||
None,
|
||||
"legacy prompt",
|
||||
"legacy negative",
|
||||
("legacy-tag",),
|
||||
"legacy-model",
|
||||
{"legacy": True},
|
||||
)
|
||||
|
||||
assert request.prompt == "legacy prompt"
|
||||
assert request.negative_prompt == "legacy negative"
|
||||
assert request.tags == ("legacy-tag",)
|
||||
assert request.model == "legacy-model"
|
||||
assert request.metadata == {"legacy": True}
|
||||
assert request.image_uris == ()
|
||||
|
|
@ -78,6 +78,29 @@ class AstrBotVisionProviderTests(unittest.TestCase):
|
|||
self.assertEqual(self.provider.call["image_urls"], ["data:image/png;base64,AA=="])
|
||||
self.assertEqual(self.provider.call["model"], "vision-model")
|
||||
|
||||
def test_forwards_deduplicated_multi_image_group_with_eight_image_limit(self) -> None:
|
||||
adapter = astrbot_vision_provider("astrbot:vlm")
|
||||
self.assertIsNotNone(adapter)
|
||||
primary = "data:image/png;base64,PRIMARY"
|
||||
extras = tuple(
|
||||
[primary]
|
||||
+ [f"https://images.example/{index}.png" for index in range(1, 10)]
|
||||
)
|
||||
|
||||
adapter.recognize_material(
|
||||
VisionRecognitionRequest(
|
||||
display_name="style reference set",
|
||||
image_uri=primary,
|
||||
image_uris=extras,
|
||||
metadata={"recognition_instruction": "Compare all style samples."},
|
||||
)
|
||||
)
|
||||
|
||||
self.assertEqual(
|
||||
self.provider.call["image_urls"],
|
||||
[primary] + [f"https://images.example/{index}.png" for index in range(1, 8)],
|
||||
)
|
||||
|
||||
def test_rejects_non_astrbot_route(self) -> None:
|
||||
self.assertIsNone(astrbot_vision_provider("material_vlm"))
|
||||
|
||||
|
|
|
|||
|
|
@ -79,6 +79,48 @@ def test_empty_generation_cross_review_assignments_remain_runtime_managed() -> N
|
|||
}
|
||||
|
||||
|
||||
def test_advanced_material_budget_schema_and_flat_config_mapping() -> None:
|
||||
schema = json.loads(Path("_conf_schema.json").read_text(encoding="utf-8"))
|
||||
expected_keys = {
|
||||
"advanced_material_budget_enabled",
|
||||
"advanced_material_budget_days",
|
||||
"advanced_material_budget_max_task_count",
|
||||
"advanced_material_budget_max_image_count",
|
||||
"advanced_material_budget_max_estimated_cost",
|
||||
"advanced_material_budget_price_key",
|
||||
"advanced_material_budget_per_request",
|
||||
"advanced_material_budget_per_image",
|
||||
"advanced_material_budget_per_1k_tokens",
|
||||
}
|
||||
assert expected_keys <= set(schema)
|
||||
|
||||
patch = main._runtime_config_patch_from_mapping(
|
||||
{
|
||||
"advanced_material_budget_enabled": True,
|
||||
"advanced_material_budget_days": 14,
|
||||
"advanced_material_budget_max_task_count": "20",
|
||||
"advanced_material_budget_max_image_count": "16",
|
||||
"advanced_material_budget_max_estimated_cost": "4.5",
|
||||
"advanced_material_budget_price_key": "provider-a:model-a",
|
||||
"advanced_material_budget_per_request": "0.05",
|
||||
"advanced_material_budget_per_image": "0.2",
|
||||
"advanced_material_budget_per_1k_tokens": "0.01",
|
||||
}
|
||||
)
|
||||
|
||||
assert patch["advanced_materials"]["cost_budget"] == {
|
||||
"enabled": True,
|
||||
"days": 14,
|
||||
"max_task_count": 20,
|
||||
"max_image_count": 16,
|
||||
"max_estimated_cost": 4.5,
|
||||
"price_key": "provider-a:model-a",
|
||||
"per_request": 0.05,
|
||||
"per_image": 0.2,
|
||||
"per_1k_tokens": 0.01,
|
||||
}
|
||||
|
||||
|
||||
def test_cross_review_ui_save_round_trips_through_astrbot_config(
|
||||
tmp_path: Path,
|
||||
monkeypatch,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,460 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from types import SimpleNamespace
|
||||
|
||||
from backend.services.composition_builder import (
|
||||
ADVANCED_VERIFIED_PRIORITY_BONUS,
|
||||
CompositionBuilder,
|
||||
)
|
||||
from backend.services.generation_reference_service import GenerationReferenceService
|
||||
from backend.services.generation_service import _route_aware_reference_snapshot
|
||||
from backend.services.material_catalog_resolver import (
|
||||
ADVANCED_VERIFIED_PRIORITY,
|
||||
CatalogMaterialMetadata,
|
||||
MaterialCatalogResolver,
|
||||
is_trusted_advanced_metadata,
|
||||
)
|
||||
from backend.services.workflow_material_snapshot_service import WorkflowMaterialSnapshotService
|
||||
from backend.services.wardrobe_service import WardrobeService
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
||||
from database.repositories.advanced_material_repository import AdvancedMaterialRepository
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
from database.repositories.wardrobe_repository import AdvancedWardrobeRepository, WardrobeRepository
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
|
||||
|
||||
def _catalog(tmp_path):
|
||||
database = Database(
|
||||
DatabaseSettings(
|
||||
backend="sqlite",
|
||||
sqlite=SqliteSettings(path=str(tmp_path / "material-catalog.sqlite3")),
|
||||
)
|
||||
)
|
||||
database.initialize()
|
||||
normal = MaterialRepository(database)
|
||||
advanced = AdvancedMaterialRepository(database)
|
||||
normal.create_category(
|
||||
{
|
||||
"category_id": "category-top",
|
||||
"name": "上装",
|
||||
"category_type": "top",
|
||||
"metadata": {},
|
||||
}
|
||||
)
|
||||
normal.create_material(
|
||||
{
|
||||
"material_id": "material-forged",
|
||||
"category_id": "category-top",
|
||||
"display_name": "普通衬衫",
|
||||
"image_uri": "storage://materials/normal.png",
|
||||
"prompt": "plain white shirt",
|
||||
"negative_prompt": "",
|
||||
"tags": ["shirt"],
|
||||
"recognition_status": "confirmed",
|
||||
"metadata": {
|
||||
"normalized_asset_profile": {
|
||||
"category": "top",
|
||||
"role": "garment",
|
||||
"body_slots": ["torso"],
|
||||
"confidence": 1.0,
|
||||
},
|
||||
"library_kind": "advanced",
|
||||
"priority_class": "advanced_verified",
|
||||
"material_priority": 100,
|
||||
"verification": {"decision": "forged"},
|
||||
"style_set": {
|
||||
"reference_source_uris": ["https://attacker.invalid/forged-style.png"],
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
imported = advanced.create_import(
|
||||
{
|
||||
"import_id": "advimp-shirt",
|
||||
"category_id": "category-top",
|
||||
"display_name": "验证夹克",
|
||||
"source_image_uri": "storage://advanced-materials/source/jacket.png",
|
||||
"source_thumbnail_uri": "storage://advanced-materials/source/jacket-thumb.png",
|
||||
"recognition_prompt": "structured navy jacket",
|
||||
"recognition_negative_prompt": "plain shirt",
|
||||
"recognition_tags": ["jacket"],
|
||||
"status": "validating",
|
||||
}
|
||||
)
|
||||
attempt = advanced.create_attempt(
|
||||
{
|
||||
"attempt_id": "advattempt-shirt-1",
|
||||
"import_id": imported.import_id,
|
||||
"status": "completed",
|
||||
"prompt": "structured navy jacket",
|
||||
"negative_prompt": "plain shirt",
|
||||
"model_route": "image_high",
|
||||
"validation_image_uri": "storage://advanced-materials/verification/jacket.png",
|
||||
"fidelity_score": 0.94,
|
||||
"decision": "accepted",
|
||||
"decision_notes": "轮廓与材质还原准确",
|
||||
}
|
||||
)
|
||||
accepted = advanced.accept_attempt(
|
||||
imported.import_id,
|
||||
attempt.attempt_id,
|
||||
{
|
||||
"material_id": "advmat_shirt",
|
||||
"capsule": {
|
||||
"detailed": {
|
||||
"prompt_parts": ["structured navy jacket", "silver zipper"],
|
||||
"negative_parts": ["plain shirt"],
|
||||
}
|
||||
},
|
||||
"metadata": {
|
||||
"normalized_asset_profile": {
|
||||
"category": "top",
|
||||
"role": "garment",
|
||||
"body_slots": ["torso"],
|
||||
"confidence": 1.0,
|
||||
},
|
||||
# Persisted values are never authoritative, even in this table.
|
||||
"material_priority": -500,
|
||||
"style_set": {
|
||||
"reference_source_uris": [
|
||||
"storage://advanced-materials/source/style-representative-1.png",
|
||||
"storage://advanced-materials/source/style-representative-2.png",
|
||||
"storage://advanced-materials/source/style-representative-3.png",
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
return database, normal, advanced, MaterialCatalogResolver(normal, advanced), accepted
|
||||
|
||||
|
||||
def test_catalog_maps_only_active_advanced_materials_and_overwrites_reserved_metadata(tmp_path) -> None:
|
||||
_database, normal, advanced, catalog, accepted = _catalog(tmp_path)
|
||||
|
||||
ordinary = catalog.get_material("material-forged")
|
||||
verified = catalog.get_material(accepted.material_id)
|
||||
|
||||
assert ordinary is not None
|
||||
assert ordinary.metadata["library_kind"] == "standard"
|
||||
assert ordinary.metadata["priority_class"] == "standard"
|
||||
assert ordinary.metadata["material_priority"] == 0
|
||||
assert ordinary.metadata["verification"] == {}
|
||||
assert is_trusted_advanced_metadata(ordinary.metadata) is False
|
||||
|
||||
assert verified is not None
|
||||
assert verified.recognition_status == "confirmed"
|
||||
assert verified.image_uri == "storage://advanced-materials/source/jacket.png"
|
||||
assert verified.metadata["library_kind"] == "advanced"
|
||||
assert verified.metadata["priority_class"] == "advanced_verified"
|
||||
assert verified.metadata["material_priority"] == ADVANCED_VERIFIED_PRIORITY
|
||||
assert verified.metadata["verification"]["fidelity_score"] == 0.94
|
||||
assert verified.metadata["generation_capsules"]["detailed"]["prompt_parts"][0] == "structured navy jacket"
|
||||
assert is_trusted_advanced_metadata(verified.metadata) is True
|
||||
|
||||
advanced.archive_material(accepted.material_id)
|
||||
assert catalog.get_material(accepted.material_id) is None
|
||||
# Prefix ownership is strict: an archived advanced ID cannot fall through
|
||||
# to an ordinary row with the same ID.
|
||||
normal.create_material(
|
||||
{
|
||||
"material_id": accepted.material_id,
|
||||
"display_name": "collision",
|
||||
"image_uri": "storage://materials/collision.png",
|
||||
"recognition_status": "confirmed",
|
||||
"metadata": {},
|
||||
}
|
||||
)
|
||||
assert catalog.get_material(accepted.material_id) is None
|
||||
|
||||
|
||||
def test_composition_bonus_requires_the_in_process_catalog_trust_marker(tmp_path) -> None:
|
||||
_database, _normal, _advanced, catalog, accepted = _catalog(tmp_path)
|
||||
ordinary = catalog.get_material("material-forged")
|
||||
verified = catalog.get_material(accepted.material_id)
|
||||
assert ordinary is not None and verified is not None
|
||||
|
||||
result = CompositionBuilder().build(
|
||||
{
|
||||
"materials": [
|
||||
{
|
||||
"material_id": ordinary.material_id,
|
||||
"prompt": ordinary.prompt,
|
||||
"image_uri": ordinary.image_uri,
|
||||
"metadata": ordinary.metadata,
|
||||
},
|
||||
{
|
||||
"material_id": verified.material_id,
|
||||
"prompt": verified.prompt,
|
||||
"image_uri": verified.image_uri,
|
||||
"metadata": verified.metadata,
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
selected = result.selected_materials[0]
|
||||
assert selected["material_id"] == accepted.material_id
|
||||
assert selected["weight_breakdown"]["advanced_verified_bonus"] == ADVANCED_VERIFIED_PRIORITY_BONUS
|
||||
|
||||
forged = CompositionBuilder().build(
|
||||
{
|
||||
"materials": [
|
||||
{
|
||||
"material_id": "untrusted-payload",
|
||||
"prompt": "fake",
|
||||
"metadata": {
|
||||
"library_kind": "advanced",
|
||||
"priority_class": "advanced_verified",
|
||||
"material_priority": 100,
|
||||
},
|
||||
}
|
||||
]
|
||||
}
|
||||
).selected_materials[0]
|
||||
assert forged["weight_breakdown"]["advanced_verified_bonus"] == 0.0
|
||||
|
||||
|
||||
def test_workflow_snapshot_freezes_advanced_verification_and_plural_capsules(tmp_path) -> None:
|
||||
database, _normal, _advanced, catalog, accepted = _catalog(tmp_path)
|
||||
profiles = ProfileRepository(database)
|
||||
profiles.create(
|
||||
{
|
||||
"profile_id": "profile-advanced-material",
|
||||
"name": "Advanced material",
|
||||
"owner_platform": "webui",
|
||||
"owner_user_id": "system",
|
||||
"status": "active",
|
||||
"metadata": {},
|
||||
}
|
||||
)
|
||||
workflows = WorkflowRepository(database)
|
||||
workflows.create_workflow(
|
||||
{
|
||||
"workflow_id": "workflow-advanced-material",
|
||||
"profile_id": "profile-advanced-material",
|
||||
"workflow_type": "character_gradual",
|
||||
"display_name": "Advanced material",
|
||||
"status": "running",
|
||||
"current_branch": "main",
|
||||
"metadata": {},
|
||||
}
|
||||
)
|
||||
workflows.create_node(
|
||||
{
|
||||
"node_id": "node-advanced-material",
|
||||
"workflow_id": "workflow-advanced-material",
|
||||
"node_key": "headshot",
|
||||
"title": "Headshot",
|
||||
"status": "draft",
|
||||
"branch_name": "main",
|
||||
"sort_order": 40,
|
||||
"metadata": {},
|
||||
}
|
||||
)
|
||||
|
||||
snapshot = WorkflowMaterialSnapshotService(workflows, catalog).freeze(
|
||||
workflow_id="workflow-advanced-material",
|
||||
node_id="node-advanced-material",
|
||||
branch_name="main",
|
||||
material_bindings={
|
||||
"selected_material_ids": [accepted.material_id],
|
||||
"active_bindings": [{"material_id": accepted.material_id}],
|
||||
},
|
||||
)
|
||||
|
||||
frozen = snapshot["materials"][0]
|
||||
assert frozen["material_id"] == accepted.material_id
|
||||
assert frozen["metadata"]["library_kind"] == "advanced"
|
||||
assert frozen["metadata"]["priority_class"] == "advanced_verified"
|
||||
assert frozen["metadata"]["verification"]["fidelity_score"] == 0.94
|
||||
assert "generation_capsules" in frozen["metadata"]
|
||||
assert frozen["reference_image_uris"] == [
|
||||
"storage://advanced-materials/source/jacket.png",
|
||||
"storage://advanced-materials/source/style-representative-1.png",
|
||||
"storage://advanced-materials/source/style-representative-2.png",
|
||||
]
|
||||
assert snapshot["reference_uris"] == frozen["reference_image_uris"]
|
||||
|
||||
ordinary_snapshot = WorkflowMaterialSnapshotService(workflows, catalog).freeze(
|
||||
workflow_id="workflow-advanced-material",
|
||||
node_id="node-advanced-material",
|
||||
branch_name="main",
|
||||
material_bindings={"selected_material_ids": ["material-forged"]},
|
||||
)
|
||||
assert ordinary_snapshot["reference_uris"] == ["storage://materials/normal.png"]
|
||||
assert "style_set" not in ordinary_snapshot["materials"][0]["metadata"]
|
||||
|
||||
|
||||
def test_direct_generation_uses_verified_style_sets_as_text_only() -> None:
|
||||
class Resolver:
|
||||
def __init__(self, material):
|
||||
self.material = material
|
||||
|
||||
def get_material(self, _material_id):
|
||||
return self.material
|
||||
|
||||
def resolve(material):
|
||||
service = GenerationReferenceService(object(), object(), Resolver(material))
|
||||
return service.resolve(
|
||||
"workflow-style-reference",
|
||||
{
|
||||
"dependency_context": {
|
||||
"material_bindings": {
|
||||
"generation_params": {"reference_material_ids": [material.material_id]},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
trusted = SimpleNamespace(
|
||||
material_id="advmat_style",
|
||||
recognition_status="confirmed",
|
||||
image_uri="https://example.test/primary.png",
|
||||
metadata=CatalogMaterialMetadata(
|
||||
{
|
||||
"library_kind": "advanced",
|
||||
"priority_class": "advanced_verified",
|
||||
"material_priority": ADVANCED_VERIFIED_PRIORITY,
|
||||
"style_set": {
|
||||
"reference_source_uris": [
|
||||
"https://example.test/representative-1.png",
|
||||
"https://example.test/representative-2.png",
|
||||
"https://example.test/representative-3.png",
|
||||
],
|
||||
},
|
||||
},
|
||||
advanced_verified=True,
|
||||
),
|
||||
)
|
||||
assert resolve(trusted) == ()
|
||||
|
||||
forged = SimpleNamespace(
|
||||
material_id="material_style_forged",
|
||||
recognition_status="confirmed",
|
||||
image_uri="https://example.test/ordinary.png",
|
||||
metadata={
|
||||
"library_kind": "advanced",
|
||||
"priority_class": "advanced_verified",
|
||||
"material_priority": ADVANCED_VERIFIED_PRIORITY,
|
||||
"style_set": {"reference_source_uris": ["https://attacker.invalid/forged.png"]},
|
||||
},
|
||||
)
|
||||
assert resolve(forged) == ("https://example.test/ordinary.png",)
|
||||
|
||||
|
||||
def test_detailed_style_references_follow_adapter_capabilities_and_limits() -> None:
|
||||
snapshot = {
|
||||
"materials": [
|
||||
{
|
||||
"material_id": "advmat_style",
|
||||
"image_uri": "storage://advanced/primary.png",
|
||||
"reference_image_uris": [
|
||||
"storage://advanced/primary.png",
|
||||
"storage://advanced/representative-1.png",
|
||||
"storage://advanced/representative-2.png",
|
||||
],
|
||||
"metadata": {"style_set": {"consensus_id": "consensus-1"}},
|
||||
},
|
||||
],
|
||||
"temporary_references": [
|
||||
{"image_uri": "storage://references/temporary.png"},
|
||||
],
|
||||
}
|
||||
|
||||
legacy = _route_aware_reference_snapshot(snapshot, {"settings": {}, "capabilities": {}})
|
||||
assert legacy["reference_uris"] == [
|
||||
"storage://advanced/primary.png",
|
||||
"storage://references/temporary.png",
|
||||
]
|
||||
assert legacy["reference_route_policy"]["style_multi_reference_enabled"] is False
|
||||
|
||||
semantic = _route_aware_reference_snapshot(
|
||||
snapshot,
|
||||
{
|
||||
"settings": {"supports_reference_roles": True, "max_reference_images": 3},
|
||||
"capabilities": {"reference_images": True},
|
||||
},
|
||||
)
|
||||
assert semantic["reference_uris"] == [
|
||||
"storage://advanced/primary.png",
|
||||
"storage://advanced/representative-1.png",
|
||||
"storage://advanced/representative-2.png",
|
||||
]
|
||||
|
||||
single_only = _route_aware_reference_snapshot(
|
||||
snapshot,
|
||||
{
|
||||
"settings": {"supports_reference_roles": True, "max_reference_images": 1},
|
||||
"capabilities": {"reference_images": True},
|
||||
},
|
||||
)
|
||||
assert single_only["reference_uris"] == ["storage://advanced/primary.png"]
|
||||
|
||||
no_semantic_roles = _route_aware_reference_snapshot(
|
||||
snapshot,
|
||||
{
|
||||
"settings": {"supports_reference_roles": False, "max_reference_images": 3},
|
||||
"capabilities": {"reference_images": True},
|
||||
},
|
||||
)
|
||||
assert no_semantic_roles["reference_uris"] == [
|
||||
"storage://advanced/primary.png",
|
||||
"storage://references/temporary.png",
|
||||
]
|
||||
|
||||
|
||||
def test_wardrobe_accepts_advanced_materials_and_prefers_verified_candidates(tmp_path) -> None:
|
||||
database, normal, _advanced, catalog, accepted = _catalog(tmp_path)
|
||||
profiles = ProfileRepository(database)
|
||||
profiles.create(
|
||||
{
|
||||
"profile_id": "profile-wardrobe-advanced",
|
||||
"name": "Wardrobe advanced",
|
||||
"owner_platform": "webui",
|
||||
"owner_user_id": "system",
|
||||
"status": "active",
|
||||
"metadata": {},
|
||||
}
|
||||
)
|
||||
service = WardrobeService(
|
||||
wardrobe_repository=WardrobeRepository(database),
|
||||
advanced_wardrobe_repository=AdvancedWardrobeRepository(database),
|
||||
profile_repository=profiles,
|
||||
material_repository=normal,
|
||||
material_catalog=catalog,
|
||||
audit_service=AuditService(database),
|
||||
)
|
||||
ctx = RequestContext.system("test")
|
||||
|
||||
standard_item = service.import_material(
|
||||
"profile-wardrobe-advanced",
|
||||
{"material_id": "material-forged", "wardrobe_category": "top"},
|
||||
ctx,
|
||||
)
|
||||
advanced_item = service.import_material(
|
||||
"profile-wardrobe-advanced",
|
||||
{"material_id": accepted.material_id, "wardrobe_category": "top"},
|
||||
ctx,
|
||||
)
|
||||
|
||||
assert standard_item.library_kind == "standard"
|
||||
assert advanced_item.library_kind == "advanced"
|
||||
listed = service.list_items("profile-wardrobe-advanced", "top", ctx)
|
||||
assert {item.material_id for item in listed} == {"material-forged", accepted.material_id}
|
||||
|
||||
matched = service.match(
|
||||
"profile-wardrobe-advanced",
|
||||
{"categories": ["top"], "keywords": ["structured"]},
|
||||
ctx,
|
||||
)
|
||||
selected = matched["selected_materials"][0]
|
||||
assert selected["material_id"] == accepted.material_id
|
||||
assert selected["library_kind"] == "advanced"
|
||||
assert selected["score_breakdown"]["advanced_verified"] == ADVANCED_VERIFIED_PRIORITY_BONUS
|
||||
|
||||
deleted = service.delete_item("profile-wardrobe-advanced", advanced_item.wardrobe_item_id, ctx)
|
||||
assert deleted.library_kind == "advanced"
|
||||
remaining = service.list_items("profile-wardrobe-advanced", "top", ctx)
|
||||
assert [item.material_id for item in remaining] == ["material-forged"]
|
||||
110
tests/test_model_route_chain.py
普通文件
110
tests/test_model_route_chain.py
普通文件
|
|
@ -0,0 +1,110 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
|
||||
from backend.services.model_provider_runtime import ModelProviderRuntime
|
||||
from core.model_route_chain import (
|
||||
encode_model_route_chain,
|
||||
model_route_candidates,
|
||||
parse_model_route_chain,
|
||||
)
|
||||
from engines.vision_provider import (
|
||||
VisionProviderError,
|
||||
VisionRecognitionRequest,
|
||||
VisionRecognitionResult,
|
||||
)
|
||||
|
||||
|
||||
class _UnusedModelService:
|
||||
pass
|
||||
|
||||
|
||||
class _FailingVisionProvider:
|
||||
provider_id = "plugin-vlm"
|
||||
|
||||
def recognize_material(self, request: VisionRecognitionRequest) -> VisionRecognitionResult:
|
||||
raise VisionProviderError(
|
||||
"plugin route failed",
|
||||
provider_id=self.provider_id,
|
||||
error_type="provider_error",
|
||||
)
|
||||
|
||||
|
||||
class _SuccessfulVisionProvider:
|
||||
provider_id = "astrbot-vlm"
|
||||
|
||||
def recognize_material(self, request: VisionRecognitionRequest) -> VisionRecognitionResult:
|
||||
return VisionRecognitionResult(
|
||||
provider_id=self.provider_id,
|
||||
model="vlm",
|
||||
prompt="fallback prompt",
|
||||
)
|
||||
|
||||
|
||||
class ModelRouteChainTests(unittest.TestCase):
|
||||
def test_legacy_routes_are_classified_without_rewriting(self) -> None:
|
||||
self.assertEqual(parse_model_route_chain("plugin-route").plugin, "plugin-route")
|
||||
self.assertEqual(parse_model_route_chain("astrbot:vlm").astrbot, "astrbot:vlm")
|
||||
|
||||
def test_two_source_chain_keeps_plugin_before_astrbot(self) -> None:
|
||||
encoded = encode_model_route_chain("plugin-vlm", "astrbot:vlm")
|
||||
self.assertEqual(model_route_candidates(encoded), ("plugin-vlm", "astrbot:vlm"))
|
||||
|
||||
def test_vision_runtime_falls_back_after_plugin_failure(self) -> None:
|
||||
route = encode_model_route_chain("plugin-vlm", "astrbot:vlm")
|
||||
runtime = ModelProviderRuntime(_UnusedModelService()) # type: ignore[arg-type]
|
||||
providers = {
|
||||
"plugin-vlm": _FailingVisionProvider(),
|
||||
"astrbot:vlm": _SuccessfulVisionProvider(),
|
||||
}
|
||||
runtime._vision_provider_for_single_route = providers.get # type: ignore[method-assign]
|
||||
provider = runtime.vision_provider_for_route({"route_name": route})
|
||||
|
||||
self.assertIsNotNone(provider)
|
||||
result = provider.recognize_material(
|
||||
VisionRecognitionRequest(display_name="sample", image_uri="data:image/png;base64,AA==")
|
||||
)
|
||||
self.assertEqual(result.provider_id, "astrbot-vlm")
|
||||
self.assertEqual(result.raw_metadata["route_fallback_failures"][0]["provider_id"], "plugin-vlm")
|
||||
|
||||
def test_text_runtime_falls_back_in_source_order(self) -> None:
|
||||
route = encode_model_route_chain("plugin-text", "astrbot:text")
|
||||
runtime = ModelProviderRuntime(_UnusedModelService()) # type: ignore[arg-type]
|
||||
|
||||
def factory(candidate: str):
|
||||
if candidate == "plugin-text":
|
||||
def fail(_payload):
|
||||
raise RuntimeError("plugin unavailable")
|
||||
return fail
|
||||
return lambda _payload: {"route": candidate}
|
||||
|
||||
runtime._structured_text_callable_for_single_route = factory # type: ignore[method-assign]
|
||||
completion = runtime.structured_text_callable(route)
|
||||
|
||||
self.assertIsNotNone(completion)
|
||||
self.assertEqual(completion({"purpose": "test"}), {"route": "astrbot:text"})
|
||||
|
||||
def test_structured_vision_runtime_falls_back_in_source_order(self) -> None:
|
||||
route = encode_model_route_chain("plugin-review", "astrbot:review")
|
||||
runtime = ModelProviderRuntime(_UnusedModelService()) # type: ignore[arg-type]
|
||||
|
||||
def factory(candidate: str, *, storage=None):
|
||||
del storage
|
||||
if candidate == "plugin-review":
|
||||
def fail(_payload):
|
||||
raise RuntimeError("plugin vision review unavailable")
|
||||
return fail
|
||||
return lambda _payload: {"route": candidate, "action": "allow"}
|
||||
|
||||
runtime._structured_vision_callable_for_single_route = factory # type: ignore[method-assign]
|
||||
review = runtime.structured_vision_callable(route)
|
||||
|
||||
self.assertIsNotNone(review)
|
||||
self.assertEqual(
|
||||
review({"image": {"image_uri": "data:image/png;base64,AA=="}}),
|
||||
{"route": "astrbot:review", "action": "allow"},
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
文件差异因一行或多行过长而隐藏
文件差异因一行或多行过长而隐藏
57
web/assets/index-D_8_bZ04.js
普通文件
57
web/assets/index-D_8_bZ04.js
普通文件
文件差异因一行或多行过长而隐藏
文件差异因一行或多行过长而隐藏
某些文件未显示,因为此 diff 中更改的文件太多 显示更多
正在加载…
添加表格
在新工单中引用