比较提交
25 提交
| 作者 | SHA1 | 提交日期 | |
|---|---|---|---|
| ad6e21de88 | |||
| 70821748bc | |||
| 0abc656000 | |||
| 26960a98f5 | |||
| 8f9c543918 | |||
| b57ce466fe | |||
| 0feb76f214 | |||
| a5ff567272 | |||
| 7ad6f3e36a | |||
| 75286e1c54 | |||
| f2366c4361 | |||
| 91220b9059 | |||
| 3415232b4e | |||
| a828f0246b | |||
| 9ce3d3eefe | |||
| 044f2b7822 | |||
| 937ee730ce | |||
| 176ee240b6 | |||
| 7cfa01b0db | |||
| 2212800a46 | |||
| 11b4cda222 | |||
| b5dd6c5d26 | |||
| d4ad94eae9 | |||
| ecd3037b9b | |||
| 063a4b8a34 |
共有 243 个文件被更改,包括 57508 次插入 和 3370 次删除
10
.gitignore
Vendored
10
.gitignore
Vendored
|
|
@ -2,6 +2,8 @@
|
|||
__pycache__/
|
||||
*.py[cod]
|
||||
.pytest_cache/
|
||||
.pytest_tmp*/
|
||||
.tmp_pytest*/
|
||||
.ruff_cache/
|
||||
.mypy_cache/
|
||||
.venv/
|
||||
|
|
@ -34,9 +36,11 @@ data/exports/*
|
|||
|
||||
# Local machine/editor noise
|
||||
.tmp/
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
.env*
|
||||
.coverage
|
||||
htmlcov/
|
||||
coverage.xml
|
||||
*.log
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
.idea/
|
||||
|
|
|
|||
371
CHANGELOG.md
普通文件
371
CHANGELOG.md
普通文件
|
|
@ -0,0 +1,371 @@
|
|||
## 1.0.66
|
||||
|
||||
- Complete P0-P2 style-profile hardening: decouple age and anatomy evidence, enforce review consistency caps, gate core traits by cross-context evidence, re-audit semantic rewrites, deduplicate scale capsules, use neutral validation scenes, add layered rendering review, improve score trust display, and block automatic adoption of regressed candidates.
|
||||
|
||||
## 1.0.65
|
||||
|
||||
- Improve style transfer with adaptive face and eye proportions, opaque pearlescent hair-highlight constraints, high-frequency representative-image guidance, and representative reference validation enabled by default with safe pure-text fallback.
|
||||
|
||||
## 1.0.64
|
||||
|
||||
- Preserve high-specificity artist signatures with an evidence-gated style kernel, scale-specific prompt adapters, and background-effect isolation for portrait, full-body, and novel-scene validation.
|
||||
|
||||
## 1.0.63
|
||||
|
||||
- Compact the final style prompt into an evidence-backed style kernel, preserve palette/highlights/shading traits after paraphrase, remove literal background effects without evidence, and neutralize age/body template locks.
|
||||
|
||||
## 1.0.62
|
||||
|
||||
- Soften facial-age and head-to-body style cues so user-requested age, gender presentation, body maturity and proportions remain authoritative; reduce childlike or adult template locking during style transfer.
|
||||
|
||||
## 1.0.61
|
||||
|
||||
- Add a selectable advanced-style consensus strategy: pure-text structured consensus by default, with opt-in vision-assisted consensus for cases that benefit from direct image review.
|
||||
|
||||
## 1.0.60
|
||||
|
||||
- Use pure-text structured cross-image consensus from per-image recognition reports, avoiding multi-image Vision relay timeouts while preserving the existing normalization and validation workflow.
|
||||
|
||||
## 1.0.59
|
||||
|
||||
- Reduce grouped consensus vision calls to one representative image plus structured reports to avoid relay multi-image stalls.
|
||||
|
||||
## 1.0.58
|
||||
|
||||
- Split large style consensus requests into grouped multi-image analysis plus a compact final merge to reduce provider timeouts.
|
||||
|
||||
## 1.0.57
|
||||
|
||||
- Make the persisted plugin-data selection authoritative for the dedicated cross-analysis route.
|
||||
|
||||
## 1.0.56
|
||||
|
||||
- Ensure the dedicated consensus route overrides stale task snapshots and survives blank AstrBot runtime defaults.
|
||||
|
||||
## 1.0.55
|
||||
|
||||
- Fix AstrBot runtime-config bridging for the independent `advanced_style_consensus` route so the selected model is actually used.
|
||||
|
||||
## 1.0.54
|
||||
|
||||
- Add an independent `advanced_style_consensus` vision route for multi-image style cross-analysis; blank keeps the recognition-route fallback.
|
||||
- Isolate large cross-image vision requests from the single-image recognition route.
|
||||
|
||||
## 1.0.53
|
||||
|
||||
- Limit concurrent advanced-material vision requests per route to avoid GPT-5.6 relay timeouts when eight images fan out at once.
|
||||
- Add `advanced_materials.recognition_concurrency`, defaulting to `2`.
|
||||
|
||||
# Changelog
|
||||
|
||||
鏈」鐩粠 1.0.8 璧蜂娇鐢ㄨ鏂囦欢璁板綍鍙獙鏀剁殑灏忕増鏈€傚紑鍙戜腑鐨勭増鏈粎浠h〃浠g爜閲岀▼纰戯紱瀹屾垚鍏ㄩ儴浜屾瀹¤闃熷垪鍚庡啀缁熶竴閮ㄧ讲銆?
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.0.52] - 2026-07-20
|
||||
|
||||
- Remove source prompt, artist-name and tag anchoring from multi-image style consensus input.
|
||||
- Keep only cross-source-supported signature traits, reject low-support multi-dimension summaries, and reduce final style prompt duplication.
|
||||
|
||||
|
||||
## [1.0.51] - 2026-07-20
|
||||
|
||||
- Improve artist-style extraction with dimension-balanced signature traits across face structure, eyes, hair, linework, shading, palette, highlights, texture and anatomy.
|
||||
- Weight signatures by cross-source support and specificity, reuse structured source attributes, and mark signatures explicitly in the final generation prompt to reduce generic-anime style collapse.
|
||||
|
||||
|
||||
## [1.0.50] - 2026-07-20
|
||||
|
||||
- 增加画师风格签名特征聚合,优先保留跨源可验证的高辨识度线稿、眼睛、发丝高光、配色和阴影结构,减少多图共识被压平为通用动漫风格。
|
||||
- 过滤通用标签并保留签名特征进入最终 portrait/fullbody/novel_scene 生成提示词。
|
||||
|
||||
## [1.0.49] - 2026-07-20
|
||||
|
||||
- 修复已封存的 accepted 流仍显示在最近导入和验证工作台的问题。
|
||||
- 验证工作台默认折叠,支持点击展开和收起。
|
||||
|
||||
## [1.0.48] - 2026-07-20
|
||||
|
||||
- 封存验证流后,工作台自动隐藏已封存记录并切换到下一条未封存流,避免封存项目继续显示在最近导入区域。
|
||||
|
||||
## [1.0.47] - 2026-07-20
|
||||
|
||||
- 加强 portrait 验证的脸宽高比、颧部、下颌和下巴轮廓约束,减少头像生成时脸型偏移,同时不改变 fullbody/novel_scene 规则。
|
||||
|
||||
## [1.0.46] - 2026-07-20
|
||||
|
||||
- 参考图改为默认关闭,先进阶画风验证默认使用纯文本生成;仍可由用户显式开启参考图。
|
||||
|
||||
## [1.0.45] - 2026-07-20
|
||||
|
||||
- 参考图生成遇到中转站 400/422/5xx 兼容性错误时,自动回退为纯文本生成;429、鉴权失败和网络超时不盲目重试。
|
||||
- 保留参考图首选路径,兼容不支持 image 字段的 OpenAI-compatible 网关。
|
||||
|
||||
## [1.0.44] - 2026-07-20
|
||||
|
||||
- 加固 advanced-style 验证提示词,移除 juvenile、toddler 及明确二至四头身措辞,保留 neotenic 画风结构与全身比例约束,降低中转站风控误判概率。
|
||||
- 保存图像供应商失败的 HTTP 状态码、路由和安全响应摘要,便于区分 502、429、内容审核和超时。
|
||||
|
||||
## [1.0.43] - 2026-07-20
|
||||
|
||||
- 修复 advanced-style 重试后旧失败记录未标记为已替代的问题,补齐 superseded 时间戳写回。
|
||||
- 批量重试按 source/case lineage 去重,只重试每条逻辑任务的最新失败记录,并归并历史失败记录。
|
||||
|
||||
## [1.0.42] - 2026-07-20
|
||||
|
||||
- 直接重试失败的 advanced-material 子任务后,旧失败记录标记为“已被重试替代”,不再继续占用“需处理”列表。
|
||||
- 新增“jobs/failed/retry”批量接口与队列页“一键重试全部失败任务”,逐条失败也会继续处理并返回错误明细。
|
||||
|
||||
|
||||
## [1.0.41] - 2026-07-20
|
||||
|
||||
- Normalize advanced-material queue retry responses to return the replacement task shape expected by the queue page.
|
||||
|
||||
|
||||
## [1.0.40] - 2026-07-20
|
||||
|
||||
- Queue-page retry now dispatches advanced style source and validation tasks to their direct retry handlers instead of retrying the whole import.
|
||||
|
||||
|
||||
## [1.0.39] - 2026-07-20
|
||||
|
||||
- Fix style validation when the configured image gateway rejects the optional reference-image field with a generic 4xx HTTP error.
|
||||
- Retry the same validation request without `input_images`, preserving the new reference-image feature for compatible providers while keeping current NAS routes usable.
|
||||
|
||||
## [1.0.38] - 2026-07-20
|
||||
|
||||
- First-round style validation can attach one optional source image as a style-only reference (shared by portrait/fullbody/novel_scene).
|
||||
- Default uses representative/primary source; UI can pick any recognized sample or disable reference for pure-text transfer.
|
||||
- Prompt + negatives forbid identity/costume/pose/scene copy from the reference; providers that reject input images fall back to pure text with metadata flags.
|
||||
- Soft anatomy band from 1.0.37 and age-band ownership by content prompt remain unchanged.
|
||||
|
||||
## [1.0.37] - 2026-07-20
|
||||
|
||||
- Full-body style transfer adds a soft anatomy band (about 5-7 heads tall, prefer observed source ratio) to prevent chibi/super-deformed collapse after removing mature-adult locks.
|
||||
- Keep facial neoteny transferable; do not restore mature adult-woman identity locks. Negative prompt blocks 2-4 heads tall bodies on fullbody validation.
|
||||
|
||||
## [1.0.36] - 2026-07-20
|
||||
|
||||
- 鐢婚杩佺Щ鍙栨秷鈥滄垚鐔熸垚骞?/ 6.5鈥?.5 澶磋韩鈥濈‖閿侊細榛樿楠岃瘉涓讳綋涓嶅啀寮哄埗 mature adult body锛涘叏韬繚鐣欐簮鍥?head-to-body 涓庤偄浣撴娊璞°€?
|
||||
- facial_age_stylization銆佹簮鍥句綋鍨嬫瘮渚嬩綔涓哄彲杩佺Щ缁撴瀯椋庢牸淇濈暀锛涘疄闄?鏃跺簭骞撮緞浠呬綔鍙傝€冿紝涓嶅啓鍏ョ敓鍥?prompt銆?
|
||||
- 楠岃瘉 content prompt 鐨勫勾榫勫甫锛堝皯濂?鎴愬コ绛夛級鐢辩敤鎴锋寚浠ゆ垨鍐呭鎻愮ず鎺у埗锛涙湭鎸囧畾鏃朵繚鎸佷腑鎬у苟璁╂簮鍥鹃鏍煎崰涓汇€?
|
||||
|
||||
## [1.0.35] - 2026-07-20
|
||||
|
||||
- 鍔犲浐闃熷垪 QueueWorker 澶辫触璺緞锛氫笟鍔″け璐ユ垨浜屾鍐欏簱寮傚父涓嶅啀鏉€姝?worker 绾跨▼锛岄伩鍏?advanced-material 浠诲姟姘镐箙鍗″湪 queued銆?
|
||||
- worker 寰幆瀵?run_once 澧炲姞澶栧眰鍏滃簳锛泂tart 鍙湪绾跨▼鎰忓閫€鍑哄悗閲嶆柊鎷夎捣锛汚piError 閿欒娑堟伅浼樺厛灞曠ず message 涓?error_code銆?
|
||||
- 瑙嗚璇嗗埆 prohibited_content 绛変笟鍔″け璐ヤ粛璁颁负 failed锛屼絾闃熷垪涓庡悗缁换鍔″彲缁х画 claim 涓庢墽琛屻€?
|
||||
|
||||
## [1.0.34] - 2026-07-19
|
||||
|
||||
- 杩涢樁鐢婚 portrait銆乫ullbody銆乶ovel_scene 涓夎矾杩佺Щ楠岃瘉浣跨敤鐙珛 900 绉掔敓鍥捐秴鏃讹紝骞跺皢鍚屼竴鏁板€兼寔涔呭寲鍒伴槦鍒楄姹傘€佷换鍔″厓鏁版嵁鍜屽疄闄?Provider 璇锋眰锛涙櫘閫氬揩閫熺敓鍥剧户缁娇鐢ㄩ€傞厤鍣ㄨ嚜韬殑榛樿瓒呮椂銆?
|
||||
- 鐢熷浘 Provider 鏀寔鍗曟璇锋眰瑕嗙洊瓒呮椂锛岃寖鍥撮檺鍒朵负 10锝?600 绉掞紱閬垮厤涓轰簡鎱㈤€熼獙璇佹ā鍨嬭€岃杩彁楂樻墍鏈夌敓鍥句换鍔$殑鍏ㄥ眬绛夊緟鏃堕棿銆?
|
||||
- AstrBot 鎻掍欢妗ュ拰鍐呭祵椤甸潰鐨勯暱浠诲姟绛夊緟缁熶竴涓?900 绉掞紝浠诲姟鍗$洿鎺ユ樉绀哄悗绔褰曠殑鑰楁椂涓婇檺锛屼笉鍐嶅嚭鐜板墠绔厛鎶ヨ秴鏃躲€佸悗绔粛鍦ㄨ繍琛岀殑鍐茬獊鎻愮ず銆?
|
||||
- 淇 SQLite 鏃犳椂鍖?UTC 鏃堕棿琚祻瑙堝櫒褰撴垚鏈湴鏃堕棿瑙f瀽鐨勯棶棰橈紱澶辫触銆佸畬鎴愬拰鍙栨秷浠诲姟鐨勮€楁椂鍥哄畾浣跨敤 `updated_at - created_at`锛屼笉鍐嶆樉绀洪敊璇殑 8 灏忔椂鎴栭殢椤甸潰鍋滅暀鎸佺画澧為暱銆?
|
||||
|
||||
## [1.0.33] - 2026-07-19
|
||||
|
||||
- 灏嗚劯閮ㄥ勾榫勯鏍笺€佸疄闄呭勾榫勪笌韬綋鎴愮啛搴︽媶鍒嗗鐞嗭紱鐢婚姝e悜鎻愮ず璇嶄笉鍐嶇户鎵挎湭鎴愬勾銆侀潚灏戝勾鎴栧辜鎬佽韩浣撴帾杈烇紝涓夌粍楠岃瘉涓讳綋缁熶竴涓烘槑纭垚骞翠汉锛屽叏韬獙璇佸浐瀹氭垚鐔熻偄浣撻暱搴︿笌绾?6.5锝?.5 澶磋韩姣斾緥銆?
|
||||
- 鐢婚鏍稿績椤广€佸繀椤讳繚鐣欓」鍙婅儗鏅€佸叿浣撳厜鏁堛€佸叿浣撹壊鐩樼瓑鏄撴薄鏌撳唴瀹瑰鍔犺瘉鎹棬鎺э紱浠呭綋澶氬浘鏀寔鐜囦笉浣庝簬 60%銆佺疆淇″害涓嶄綆浜?70% 鏃惰繘鍏ユ渶缁堟彁绀鸿瘝銆?
|
||||
- 鑷姩杩樺師搴﹁瘎鍒嗗鍔犱竴鑷存€ф牎鍑嗭細鏈€缁堝垎涓嶉珮浜庢ā鍨嬭嚜璇勫垎锛屽亸宸€佷慨姝hˉ涓併€佽涔夐闄╁拰鏂囧瓧涓ラ噸搴︿細鎵e垎锛涒€滅淮搴﹀叏婊″垎浣嗗悓鏃惰姹備慨姝b€濅細鏍囪涓哄鏍哥粨鏋滀笉涓€鑷淬€?
|
||||
- 瀵规ā鍨嬬粰鍑虹殑瀹夊叏鏀瑰啓鍐嶅仛涓€娆¤涔夊舰鐘跺璁★紱鍙戜笣楂樺厜涓殑鍏夌幆銆佹í甯︺€佹潯鍧楃瓑鐗╀綋鍖栨帾杈炴敼鍐欎负璐村悎鍙戜笣鏇查潰鐨勫崐閫忔槑鏂画缂庨潰鍏夋辰锛屽苟鍔犲叆鐧借壊瀹炵嚎銆佽繛缁厜甯︺€佸嚑浣曟姌绾垮潡绛夎礋鍚戠害鏉熴€?
|
||||
- 鍏叡 detailed 鐢婚鑳跺泭涓?portrait/fullbody/novel_scene 灏哄害鑳跺泭鍦ㄦ湇鍔$鍘婚噸锛屽昂搴﹁兌鍥婂彧淇濈暀璇ユ瀯鍥惧昂搴︾嫭鏈夌殑绾︽潫锛屽噺灏戦暱鎻愮ず璇嶇殑閲嶅娉ㄦ剰鍔涘崰鐢ㄣ€?
|
||||
- 榛樿涓庢ā鍨嬭繑鍥炵殑楠岃瘉鍐呭缁熶竴缁忚繃涓€у寲锛氬ご鍍忎娇鐢ㄥ搼鍏夋鎷嶏紝鍏ㄨ韩浣跨敤涓€ф棩鍏夛紝璺ㄥ満鏅娇鐢ㄦ櫘閫氶槾澶╋紝绉婚櫎榛庢槑銆佹棩钀姐€侀湏铏广€佹暎鏅€侀暅澶村厜鏅曘€佷綋绉厜涓庢垙鍓ф€ч€嗗厜鍋忕疆銆?
|
||||
|
||||
## [1.0.32] - 2026-07-18
|
||||
|
||||
- 鐢婚楠岃瘉瀹℃牳鍗囩骇涓哄浐瀹氱淮搴﹀崗璁紱鏈嶅姟绔寜澶村儚銆佸叏韬€佽法鍦烘櫙鍒嗗埆鍔犳潈鑴搁儴銆佺溂鐫涖€佺嚎绋裤€佽壊褰┿€侀槾褰便€佹瘮渚嬩笌鑳屾櫙锛屼笉鍐嶇洿鎺ラ噰鐢ㄨ瑙夋ā鍨嬬殑鑷敱鎬诲垎锛屽苟璁板綍缁村害瑕嗙洊鐜囦笌缂哄け椤广€?
|
||||
- 澶嶉獙鍏辫瘑鑷姩鑱氬悎涓婁竴杞笁寮犻獙璇佸浘锛屽皢鑷冲皯涓や釜鍦烘櫙閲嶅鍑虹幇鐨勯棶棰樺綊绫讳负鍏叡鐢婚缂洪櫡锛屽皢鍗曞満鏅棶棰樹繚鐣欎负灏哄害鑳跺泭淇锛岄伩鍏嶅ご鍍忛棶棰樻薄鏌撳叏韬垨鍦烘櫙鎻愮ず璇嶃€?
|
||||
- 鍏辫瘑鍗忚鏂板鍏ㄥ眬鑹插僵涓庡眬閮ㄨ壊宸垎绂汇€佺粨鏋勫寲宸紓銆佹彁绀鸿瘝琛ヤ竵銆佽涔夐闄╁璁″拰灏哄害鑳跺泭锛涘彲鑳借瀛楅潰鍖栦负鐗╀綋銆佸厜鏁堟垨鏋勫浘鐨勬瘮鍠诲繀椤绘敼鍐欎负琛ㄩ潰銆佸嚑浣曚笌浣嶇疆鍏崇郴銆?
|
||||
- detailed 鐢婚鑳跺泭鏀圭敱鏈嶅姟绔紪璇戯細鎸夋牳蹇冦€佺粨鏋勩€佹覆鏌撲紭鍏堢骇鍚堝苟 `must_preserve`锛岃繎涔夊幓閲嶅苟闄愬埗涓?14 鏉″叕鍏辨牳蹇冪壒寰侊紝鍐嶅彔鍔犳渶澶?5 鏉″満鏅昂搴︾壒寰侊紝鍑忓皯闀挎彁绀鸿瘝鐨勬敞鎰忓姏绋€閲娿€?
|
||||
- 姣忚疆涓夊浘瀹屾垚鍚庤嚜鍔ㄤ笌涓婁竴杞绠楁€诲垎鍜屽垎鍦烘櫙宸€硷紝鏍囪閲囩敤鍊欓€夈€佷繚鐣欎笂涓€鐗堟垨闇€瑕佷汉宸ュ垽鏂紱瑙嗚瀹℃牳澶辫触浠嶄笉褰卞搷鐢熸垚鍥剧墖淇濈暀銆?
|
||||
|
||||
## [1.0.31] - 2026-07-18
|
||||
|
||||
- 鐢婚閫愬浘璇嗗埆涓庝氦鍙夊叡璇嗘柊澧炶劯鍨嬨€佺溂鐫?铏硅啘銆侀蓟鍙g畝鍖栥€佸ご韬瘮銆佸彂鏉熺粍缁囥€佺嚎鏉℃嫇鎵戙€侀槾褰辫壊鐩搞€侀珮鍏夊舰鐘跺拰鑹插僵鍏崇郴绛夌粨鏋勭敾椋庣淮搴︼紝涓嶅啀鍙緭鍑洪€氱敤鍔ㄦ极娓叉煋鏍囩銆?
|
||||
- 褰掍竴鍖栧眰淇濈暀瑙嗚妯″瀷杩斿洖鐨?`normalized_asset_profile.attributes`锛屽苟涓虹敾椋庤嚜鍔ㄧ敓鎴?`must_preserve`锛涗慨澶嶇粨鏋勫寲灞炴€ч€€鍖栨垚鍗曚竴 `legacy_description` 鐨勯棶棰樸€?
|
||||
- 鍐呭姹℃煋娓呭崟浠呯敤浜庡璁★紝涓嶅啀鍘熸牱娉ㄥ叆鐢熷浘璐熷悜鎻愮ず璇嶏紱鏀逛负鍙帓闄ゅ叿浣撴簮瑙掕壊韬唤銆佸叿浣撴湇瑁呰璁°€佸Э鍔夸笌鍦烘櫙锛岄伩鍏嶈浼?Moe 姣斾緥銆佸姩鐗╄€崇瓑椋庢牸鑰﹀悎鐗瑰緛銆?
|
||||
- 淇 detailed 鑳跺泭鍚€楀彿璇箟鐭琚媶纰庛€佺煭鍏辫瘑涓庤缁嗚兌鍥婇噸澶嶆嫾鎺ャ€佹璐熸彁绀鸿瘝澶氭閲嶅鐨勯棶棰橈紝骞跺缂哄皯缁撴瀯缁村害鐨勬ā鍨嬭嚜鎶ョ疆淇″害杩涜鏈嶅姟绔檷鏉冦€?
|
||||
- 姣忓紶楠岃瘉鍥剧敓鎴愬悗鑷姩浣跨敤浠h〃婧愬浘杩涜瑙嗚杩樺師搴﹀鏍革紝鎸佷箙鍖栫淮搴﹁瘎鍒嗐€佷富瑕佸尮閰?鍋忓樊鍙婁笅涓€杞慨姝f彁绀猴紱瀹℃牳澶辫触涓嶄細涓㈠純宸茬粡鐢熸垚鐨勫浘鐗囥€?
|
||||
- 杩涢樁鐢婚楠岃瘉鍗$墖鏄剧ず鑷姩杩樺師搴﹀拰涓昏鍋忓樊锛涜瑙夋ā鍨嬫牸寮忛敊璇柊澧炶劚鏁忓悗鐨勫叿浣撳搷搴旈棶棰樺拰瑙f瀽鍘熷洜銆?
|
||||
|
||||
## [1.0.30] - 2026-07-18
|
||||
|
||||
- 杩涢樁鐢婚澶嶉獙浼氬悓鏃舵彁浜や唬琛ㄦ簮鍥俱€佷笂涓€杞笁寮犲け璐ラ獙璇佸浘銆佸叏閮ㄦ簮鍥捐瘑鍒姤鍛娿€佷笂涓€鐗堟彁绀鸿瘝涓庣敤鎴蜂慨姝o紝瑙嗚妯″瀷鍙洿鎺ヨ繘琛屽墠鍚庡鐓с€?
|
||||
- 灏嗏€滃お寮恒€侀檷浣庛€佸お寮便€佸姞寮恒€佺Щ闄ゃ€佷繚鎸佲€濈瓑鍙嶉瑙f瀽涓?increase/decrease/remove/preserve 缁撴瀯鍖栨柟鍚戯紝骞舵寔涔呭寲瀵圭収娓呭崟涓庡疄闄呰皟鏁磋鍒掋€?
|
||||
- 澧炲姞鏈嶅姟绔涔夐槻鍙嶈浆锛涗緥濡傗€滈鑹插姣斿お杩囧己鐑堚€濅笉浼氬啀琚浆鎹负 intense/extreme/high contrast锛岃€屼細钀藉疄涓洪€備腑鍏嬪埗鐨勫姣斿苟鎺掗櫎杩囧己鑹插樊銆?
|
||||
- 涓夊紶楠岃瘉鐢熷浘缁熶竴浣跨敤 detailed 鐢婚鑳跺泭鍙婂叾璐熷悜绾︽潫锛屼笉鍐嶅彧浣跨敤鐭叡璇嗘彁绀鸿瘝锛涗粛淇濇寔绾枃鏈縼绉婚獙璇侊紝涓嶅悜鐢熷浘妯″瀷浼犻€掓簮鍥俱€?
|
||||
|
||||
- 宸插畬鎴愬叏浠撹鑹插伐浣滄祦浜屾瀹¤锛涚‘璁ゅ湪鐜版湁 `character_gradual` 涓荤嚎涓婂閲忎慨澶嶏紝涓嶅垱寤哄钩琛屽伐浣滄祦銆?
|
||||
- 淇闃熷垪宸叉寜 1.0.9鈥?.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`锛屽鑷翠笁鍥鹃獙璇佽璇垽涓哄崟鍥炬祦绋嬩笖涓荤晫闈㈠彧鏄剧ず涓€寮犻獙璇佸浘鐨勯棶棰樸€?
|
||||
- 澶氬浘鐢婚褰撳墠杞鎭㈠涓哄ご鍍忋€佸叏韬€佽法鍦烘櫙涓夊紶楠岃瘉鍗$墖锛涗笁寮犲浘鍒嗗埆鏄剧ず鐘舵€併€佹彁绀鸿瘝銆佹枃瀛楄皟鏁村拰闅忔満绉嶅瓙銆?
|
||||
- 楠岃瘉杞鍒楄〃涓庤鎯呮帴鍙d负姣忎竴杞檮甯﹀畬鏁?`validation_cases`锛屽巻鍙茶疆娆′篃鍙悓鏃舵煡鐪嬩笁寮犵缉鐣ュ浘锛屼笉鍐嶅彧灞曠ず鑱氬悎杞鐨勭涓€寮犳姇褰卞浘銆?
|
||||
- 鍏煎鏃?`style_set`銆佹寮?`advanced_style_set` 浠ュ強鏈嶅姟绔?`metadata.style_set` 鏍囪锛岄伩鍏嶅崌绾у墠鍚庣殑璁板綍鍐嶆璧伴敊鍓嶇鍒嗘敮銆?
|
||||
|
||||
## [1.0.27] - 2026-07-17
|
||||
|
||||
- 杩涢樁鐢婚澶嶉獙鎷嗗垎涓衡€滅敾椋庝慨姝b€濆拰鈥滀笅涓€杞獙璇佸唴瀹光€濅袱鏉$嫭绔嬭緭鍏ワ細鍓嶈€呭彧閲嶅缓鐢婚鍏辫瘑涓庤兌鍥婏紝鍚庤€呯洿鎺ュ啓鍏ヤ笁涓獙璇佺敓鍥炬彁绀鸿瘝銆?
|
||||
- 淇绗簩杞己鍒跺鐢ㄧ涓€杞€佸勾鐢锋€с€佹満鍣ㄤ汉鍜屽畤鑸憳鍦烘櫙鐨勯棶棰橈紱鐢ㄦ埛鎸囧畾瑙掕壊銆佹湇瑁呫€佹瀯鍥炬垨鍦烘櫙鍚庝細閲嶅缓楠岃瘉妗堜緥锛屼笖鏈€缁?`expanded_prompt` 蹇呭畾鍖呭惈鐢ㄦ埛鍘熸枃銆?
|
||||
- 鍙慨鏀圭敾椋庢椂缁х画娌跨敤鐩稿悓楠岃瘉鍦烘櫙涓庣瀛愶紝渚夸簬 A/B 姣旇緝锛涗慨鏀归獙璇佸唴瀹规椂浣跨敤鏂扮瀛愶紝閬垮厤鏃ф瀯鍥捐闅忔満绉嶅瓙鎸佺画閿佸畾銆?
|
||||
- 榛樿楠岃瘉鏀逛负鍚屼竴鏂拌鑹茬殑杩戝煙澶村儚銆佸叏韬拰璺ㄥ満鏅笁绾у鐓э紝鍑忓皯瑙掕壊骞撮緞銆佺墿绉嶄笌鍐欏疄鍏堥獙瀵圭敾椋庡垽鏂殑骞叉壈銆?
|
||||
- 楠岃瘉鍗$墖鏄剧ず宸插簲鐢ㄧ殑鏂囧瓧璋冩暣鍜屽疄闄呯瀛愶紱鍘嗗彶杞鎸佷箙鍖栫敾椋庝慨姝c€侀獙璇佸唴瀹瑰強鍏朵綔鐢ㄨ寖鍥淬€?
|
||||
- 鏃у鎴风缁х画鏀寔鍗曚竴 `adjustment_text`锛屾湇鍔$浼氬悓鏃跺簲鐢ㄥ埌鐢婚涓庨獙璇佸唴瀹归摼璺紝閬垮厤鍙嶉闈欓粯涓㈠け銆?
|
||||
|
||||
## [1.0.26] - 2026-07-17
|
||||
|
||||
- 淇浠诲姟杈冨鏃堕槦鍒楀崱鐗囪婊氬姩鍖哄煙鍘嬬缉鐨勯棶棰橈紱浠诲姟鍗℃敼涓虹旱鍚戣嚜鐒堕珮搴︽帓鍒楋紝鐘舵€併€佹爣棰樸€佽繘搴︽潯涓庤€楁椂涓嶅啀鐩镐簰瑕嗙洊銆?
|
||||
- 杩涢樁绱犳潗楠岃瘉杞鏀逛负鍙洿鏂板綋鍓嶆椿鍔ㄥ鍏ユ垨瀵煎叆鎽樿锛屼笉鍐嶆瘡 4.5 绉掑叏閲忓埛鏂板鍏ュ簱涓庣礌鏉愬簱銆?
|
||||
- 澧炲姞杞浜掓枼锛岄槻姝㈡參璇锋眰鍙戠敓閲嶅彔锛涜疆璇㈠け璐ヤ繚鎸佸綋鍓嶉〉闈紝涓嶅啀瑙﹀彂閿欒鎬佹垨瑙嗚閲嶇疆銆?
|
||||
- 鍥剧墖灞曠ず浼樺厛浣跨敤绋冲畾鐨?`storage://` URI 缂撳瓨锛屽拷鐣ュ悓涓€濯掍綋姣忚疆鍙樺寲鐨勪复鏃剁鍚?URL锛岄伩鍏嶉獙璇佺敓鎴愭湡闂村浘鐗囧弽澶嶉噸杞藉拰椤甸潰闂儊銆?
|
||||
|
||||
## [1.0.25] - 2026-07-17
|
||||
|
||||
- 淇杩涢樁绱犳潗闃熷垪璁板綍琚墜鍔ㄥ垹闄ゅ悗锛屽鍏ユ垨鏍锋湰浠嶄繚鎸?queued/running 骞舵案涔呭崱浣忕殑闂锛涜鎯呰鍙栥€佸垪琛ㄥ埛鏂板拰閲嶈瘯鍓嶄細灏嗙己澶变换鍔″璐︿负鍙噸璇曞け璐ャ€?
|
||||
- 鏂板鏈皝瀛樿繘闃跺鍏ュ垹闄ゆ帴鍙d笌鍓嶇鍙屽嚮纭鎸夐挳锛涘垹闄や細鍏堝彇娑堟畫鐣欎换鍔★紝鍐嶇骇鑱旀竻鐞嗘牱鏈€佷氦鍙夊叡璇嗐€侀獙璇佽疆娆″拰杩涢樁绱犳潗鐙珛鏂囦欢銆?
|
||||
- 宸插皝瀛樹负楂樹紭鍏堢骇绱犳潗鐨勫鍏ョ姝㈠垹闄わ紝浠嶉渶閫氳繃绱犳潗褰掓。娴佺▼澶勭悊锛岄伩鍏嶇牬鍧忕礌鏉愬簱銆佽。姗卞拰宸ヤ綔娴佸紩鐢ㄣ€?
|
||||
- 缂哄け闃熷垪浠诲姟浼氭樉绀烘槑纭彁绀猴紝鍙€夋嫨鍗曠嫭閲嶈瘯銆佹帓闄ゆ牱鏈垨鍒犻櫎鏁翠釜瀵煎叆锛屼笉鍐嶅彧鏄剧ず妯$硦鐨勨€滃緟澶勭悊鏍锋湰鈥濄€?
|
||||
|
||||
## [1.0.24] - 2026-07-17
|
||||
|
||||
- 妯″瀷鍒嗗伐缁熶竴涓哄弻婧愯矾鐢憋細姣忎釜鏂囨湰/瑙嗚鑺傜偣鍒嗗埆閫夋嫨 ImageStudio 鎻掍欢閫傞厤鍣ㄤ笌 AstrBot 妯″瀷鎻愪緵鍟嗭紝鎵ц椤哄簭鍥哄畾涓烘彃浠朵紭鍏堛€丄strBot 澶囩敤銆?
|
||||
- 绱犳潗璇嗗埆銆佽繘闃剁礌鏉愯瘑鍒€佸鍥剧敾椋庡叡璇嗐€佺粨鏋勫寲鏂囨湰璋冪敤鍜岀敓鎴愬悗瑙嗚瀹℃牳鍔犲叆鏁呴殰杞Щ锛涘墠缃彁渚涘晢澶辫触鏃惰嚜鍔ㄥ皾璇曞鐢紝骞朵繚鐣欏け璐ユ憳瑕併€?
|
||||
- 鐢熷浘鑺傜偣鏄庣‘鍙€夋嫨 ImageStudio 鐢熷浘閫傞厤鍣紱AstrBot 4.25 娌℃湁鍏紑鐢熷浘妗ユ帴鎺ュ彛锛屽洜姝ょ晫闈㈢鐢ㄤ笉鍙墽琛岀殑瀹夸富鐢熷浘閫夋嫨锛岄伩鍏嶄繚瀛樺悗杩愯澶辫触銆?
|
||||
- 杩涢樁鐢婚澶辫触鏍锋湰閲嶈瘯鏃堕噸鏂拌鍙栧綋鍓嶅弻婧愰厤缃紝骞跺悓姝ュ埛鏂扮埗瀵煎叆璺敱锛屼慨澶嶆棫 `material_vlm` 琚换鍔℃案涔呭喕缁撶殑闂銆?
|
||||
- 鎻愮ず璇嶈浆璇戙€佺敓鎴愬墠鍐呭瀹℃牳涓庣敓鎴愬悗鍥剧墖瀹℃牳鍚屾浣跨敤鍙岄€夋嫨鏍忥紱鏃у崟璺敱閰嶇疆淇濇寔鍏煎锛屽弻璺敱缁х画瀛樹负鍗曚釜 AstrBot 閰嶇疆瀛楃涓层€?
|
||||
- 淇杩愯鏃剁増鏈父閲忎笌鍙戝竷鍏冩暟鎹笉鍚屾鐨勯棶棰橈紝鐘舵€侀〉鍜屾彃浠跺厓鏁版嵁缁熶竴鏄剧ず 1.0.24銆?
|
||||
|
||||
## [1.0.23] - 2026-07-17
|
||||
|
||||
- 灏嗗唴缃礌鏉愬垎绫荤殑鑻辨枃鍚嶇О缁熶竴鏄剧ず涓轰腑鏂囷紝骞惰鐩栬繘闃剁礌鏉愬鍏ャ€佺礌鏉愭搷浣滃拰璇嗗埆鎻愮ず璇嶈缃笅鎷夋銆?
|
||||
- 淇濈暀鍚庣鍒嗙被 ID 涓庡師濮嬪悕绉帮紝鍏煎鐜版湁绱犳潗銆佽。姗卞拰宸ヤ綔娴佹暟鎹€?
|
||||
|
||||
## [1.0.22] - 2026-07-17
|
||||
|
||||
- 杩涢樁绱犳潗鐨勨€滈鏍尖€濆垎绫绘柊澧?2锝? 寮犳牱鏈粍瀵煎叆锛堟帹鑽?3锝? 寮狅級锛涙瘡寮犲浘鎷ユ湁鐙珛浠诲姟銆佽鏃躲€侀敊璇笌閲嶈瘯锛屽苟鐢?8 Worker 鍚屾椂澶勭悊锛屼笉鍐嶄覆琛屽叡浜秴鏃躲€?
|
||||
- 鏂板閫愬浘璇嗗埆灞忛殰鍜屽鍥句氦鍙夊綊绾筹細妯″瀷鍚屾椂璇诲彇鍏ㄩ儴鏈夋晥鍘熷浘涓庨€愬浘鎶ュ憡锛屽尯鍒嗘牳蹇冨叡鍚岀敾椋庛€佸彲鍙橀」銆佹簮鍐呭姹℃煋銆佺枒浼肩缇ゆ牱鏈拰 1锝? 寮犱唬琛ㄦ牱鏈紝骞剁敓鎴?compact / standard / detailed 涓夋。鐢婚鑳跺泭銆?
|
||||
- 鏂板涓夎矾绾彁绀鸿瘝杩佺Щ楠岃瘉锛屼娇鐢ㄥ畬鍏ㄤ笉鍚岀殑瑙掕壊銆佸満鏅拰鏋勫浘涓斾笉浼犳簮鍥撅紱涓夐」浠诲姟鐙珛骞跺彂锛屽崟椤瑰け璐ュ彲鍗曠嫭閲嶈瘯锛岃皟鏁村悗鍒涘缓鏂板叡璇嗙増鏈苟澶嶇敤鐩稿悓楠岃瘉鍦烘櫙銆?
|
||||
- 鏂板 `advanced_style_sources`銆乣advanced_style_consensus_runs`銆乣advanced_style_validation_cases` 涓夎〃锛涚埗瀵煎叆涓?2锝? 寮犳簮璁板綍鍘熷瓙鍐欏叆锛岄獙璇佸浘鐗囩户缁殧绂诲湪 `storage://advanced_materials/style-validations`銆?
|
||||
- 鍓嶇浠呭湪鈥滈鏍尖€濆垎绫诲惎鐢ㄥ鍥句笂浼狅紝鏀寔绮樿创銆佹嫋鍏ャ€佽拷鍔犮€佺Щ闄ゃ€佹帓搴?涓诲弬鑰冦€佸け璐ラ噸璇曘€佹牱鏈帓闄や笌鎭㈠锛屽苟灞曠ず浜ゅ弶褰掔撼銆佷唬琛?绂荤兢鏍锋湰鍜屼笁鍦烘櫙鐘舵€併€?
|
||||
- 灏佸瓨鍚庝粛鍙骇鐢熶竴涓珮浼樺厛绾?`advmat_`锛涘揩閫熻鑹蹭娇鐢ㄧ粺涓€鏂囧瓧鑳跺泭锛岃缁嗗伐浣滄祦鍐荤粨涓诲弬鑰冧笌鏈€澶氫袱寮犲彲淇′唬琛ㄥ浘锛屾櫘閫氱礌鏉愪吉閫?metadata 鏃犳硶娉ㄥ叆棰濆寮曠敤銆?
|
||||
- 澶囦唤鑴辨晱銆佹仮澶嶃€佸鍎垮獟浣撱€侀€愬満鏅垚鏈€佺礌鏉愮粺璁°€乄orker 閲嶅惎鎭㈠銆佽矾鐢卞绾﹀拰娣辫壊涓婚鍚屾瑕嗙洊鏂伴摼璺紱浠樿垂楠岃瘉浠诲姟閲嶅惎鍚庝笉浼氳嚜鍔ㄩ噸澶嶆彁浜ゃ€?
|
||||
|
||||
## [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` 骞傜瓑浜嬪姟銆?
|
||||
- AstrBot 鎵樼閰嶇疆妗ヨˉ榻愯瑙夎川妫€銆佸吋瀹瑰鏍镐笌鐢熸垚寮忎氦鍙夊鏌ョ殑涓?鍓?鍒楄〃瀛楁锛涗繚瀛樸€佹竻绌哄拰閲嶈浇鍧囦繚鐣欑湡瀹炲€硷紝涓嶅啀琚棫杩愯鏃惰鐩栨仮澶嶃€?
|
||||
- 鏂板鍏ㄦ柊瀹夎銆?.0.8 鍘熶綅鍗囩骇銆侀噸澶嶈縼绉汇€佺己琛?鍗婅縼绉绘仮澶嶃€乻tale Worker銆佺粓鎬侀€氱煡琛ュ彂銆佸涓诲凡鍔犺浇椤跺眰 `core` 鍖呭拰鎻掍欢鍒濆鍖栭『搴忓洖褰掋€?
|
||||
- 鍓嶇鏂板璺敱缁ф壙銆侀攣鐘舵€併€佷氦鍙夊鏌ユ帴绾夸笌鍘熷瓙妗f鍗℃帴绾垮绾︽祴璇曪紱鐢熶骇鏋勫缓缁х画杈撳嚭 AstrBot 鎻掍欢椤典笌鐙珛 Web 鍏煎浜х墿銆?
|
||||
- 娓呴櫎鐢熶骇鍖呬腑鏂繛鏃跺彲杈剧殑鍋囩敓鍥俱€佸亣瑙掕壊銆佺ず渚嬬礌鏉愬拰棰勮妯″瀷锛涙柇杩炰笌绌哄悗绔粺涓€鏄剧ず鏄庣‘绌虹姸鎬侊紝缂哄皯鍥剧墖鐨勭湡瀹炶褰曚粎鏄剧ず鈥滄湭鐢熸垚/鏃犵缉鐣ュ浘鈥濄€?
|
||||
- README 涓?INSTALL 鏇存柊 11 鑺傜偣宸ヤ綔娴併€佽亰澶╁懡浠ゃ€佹ā鍨嬪垎宸ャ€佹棤妯″瀷琛屼负銆丏ocker/NAS 瑕嗙洊鍜屾樉寮忚繍琛屾椂 ZIP 鐧藉悕鍗曪紱鍙戝竷鍖呯姝㈠寘鍚?`.env*`銆佺湡瀹炴暟鎹€佹祴璇曘€佸墠绔伐绋嬪拰缂撳瓨銆?
|
||||
- NAS 瀹炴満浠?1.0.8 鍘熶綅鍗囩骇骞堕噸鍚?`astrbot-ops-pro`锛欰strBot 鎴愬姛鍔犺浇 ImageStudio 1.0.20锛孲QLite 15 鏉¤縼绉讳笌鍏抽敭杞/妗f鍗?绱犳潗琛ㄩ€氳繃妫€鏌ワ紝鎻掍欢椤甸潰鑷姩杩炴帴涓旀湭瑙﹀彂鐪熷疄浠樿垂鐢熷浘銆?
|
||||
|
||||
## [1.0.9] - 2026-07-15
|
||||
|
||||
- 鍦ㄧ幇鏈夎妭鐐广€佷换鍔°€佸浘鐗囧拰鍘嗗彶閾捐矾涔嬩笂澧炲姞绋冲畾鐨勭敓鎴愯疆娆°€佸€欓€変笌鍊欓€夋睜鑱氬悎灞傘€?
|
||||
- 鏃у伐浣滄祦鍥剧墖閫氳繃鍙 legacy round 灞曠ず锛屼笉鎼枃浠躲€佷笉鏀瑰師鏈?`image_id`銆?
|
||||
- 鏂板 `TX-01`銆乣TX-01-03` 绛夋寔涔呭寲鏄剧ず鐮侊紝骞惰褰曟ā鍨嬫潵婧愩€佸け璐ユ憳瑕佷笌杞蹇収銆?
|
||||
- `SingleFlowEngine` 鏀寔鎸?workflow/branch/node/round/candidate 闅旂杈撳嚭鐩綍锛涙棤 round 涓婁笅鏂囨椂淇濇寔鏃ц矾寰勫吋瀹广€?
|
||||
- 琛ラ綈鏁版嵁搴撹〃鍏冩暟鎹笌 AstrBot/鍓嶇鍏变韩 round 鎺ュ彛锛涘垱寤鸿疆娆′粎瑙勫垝鍊欓€夛紝涓嶄細瑙﹀彂鐪熷疄鐢熷浘銆?
|
||||
|
||||
## [1.0.10] - 2026-07-15
|
||||
|
||||
- 璇︾粏宸ヤ綔娴佹瘡杞彧鎵ц涓€娆℃彁绀鸿瘝鎵╁啓銆佹不鐞嗐€佽矾鐢便€佺礌鏉愬揩鐓т笌鎴愭湰妫€鏌ワ紝鍐嶆媶鍒嗕负 4鈥? 鏉$嫭绔?`count=1` 鍊欓€変换鍔°€?
|
||||
- 鍚敤 5 Worker 鎸佷箙骞跺彂姹狅紱鍗曞€欓€夊け璐ユ垨瓒呮椂涓嶅奖鍝嶅悓杞叾浠栧€欓€夛紝杞鑱氬悎鏀寔瀹屾垚銆侀儴鍒嗘垚鍔熴€佸叏閮ㄥけ璐ュ拰鍙栨秷銆?
|
||||
- AstrBot 鍒濆鍖栬嚜鍔ㄥ惎鍔?Worker銆佺粓姝㈡椂鍋滄锛涘惎鍔ㄥ彲鎭㈠ stale `running/retrying` 浠诲姟锛屾棫鍚屾 `generate_node()` 淇濇寔鍏煎銆?
|
||||
- 寮傛浠诲姟鎷掔粷鎸佷箙鍖?data URL锛涙ā鍨嬭矾鐢卞彧淇濆瓨鏃犳晱鎰熷瓧娈电殑蹇収锛屾墽琛屾椂閲嶆柊瑙f瀽骞舵牎楠?provider/model 鏈紓绉汇€?
|
||||
|
||||
## [1.0.11] - 2026-07-15
|
||||
|
||||
- 璇︾粏瑙掕壊鍊欓€夌殑鑱氱劍銆佸钩閾烘瘮杈冦€佸枩娆?鎷掔粷鍙嶉涓庢渶缁堝畾绋挎敼涓哄洓濂楃嫭绔嬬姸鎬侊紱姣忚疆鏈€澶氫繚鐣?3 涓枩娆㈠€欓€夛紝鏈€缁堢‘璁や弗鏍煎彧鑳介€夋嫨 1 涓垚鍔熷€欓€夈€?
|
||||
- 鏂板 round feedback/confirm 鎺ュ彛锛涘弽棣堟寔涔呭寲鍒板€欓€夋睜涓庤疆娆″揩鐓э紝鍒锋柊鍚庡彲鎭㈠锛屼笖涓嶄細璇皟鐢ㄥ浘鐗囬噰鐢ㄦ帴鍙c€?
|
||||
- 鏈€缁堢‘璁ゅ湪鍗曚簨鍔″唴瀹屾垚鍊欓€夊畾绋裤€佸浘鐗囩嫭鍗犻€夋嫨銆佽妭鐐圭姸鎬併€佸巻鍙茶褰曘€佸垎鏀樁娈佃緭鍏ュ拰鍚庣户鑺傜偣鎺ㄨ繘锛涘け璐ュ畬鏁村洖婊氾紝鏈嶅姟绔嫆缁濆鎴风鎸囧畾涓嬩竴鑺傜偣銆?
|
||||
- 宸ヤ綔鍙版帴鍏?4/5 寮犲€欓€夋暟閲忋€佸綋鍓嶈疆/鍘嗗彶杞€佽妭鍒惰疆璇€侀儴鍒嗘垚鍔?鍏ㄥけ璐ャ€佹ā鍨嬫潵婧愩€佸崟鍊欓€夊け璐ュ拰鍏ラ槦鎻愮ず锛涙棫鍥剧墖缁х画浣跨敤鍙鍏煎杞銆?
|
||||
|
||||
## [1.0.12] - 2026-07-15
|
||||
|
||||
- 宸ヤ綔娴佸璇濇鏀寔鈥?銆? 鍠滄锛? 涓ユ牸鎷掔粷鈥濈瓑鑷劧璇█鍙嶉锛汱LM 鍙帴鏀舵寔涔呭睍绀虹爜鍜屽彈闄愬€欓€夋彁绀鸿瘝蹇収锛屾湇鍔$閲嶆柊鐧藉悕鍗曟槧灏勶紝妯″瀷澶辫触鏃跺畨鍏ㄥ洖閫€鍒版湰鍦扮‘瀹氭€цВ鏋愩€?
|
||||
- 鍙嶉鎸佷箙鍖栫敤鎴峰師鏂囥€佹憳瑕併€佸枩娆?鎷掔粷/涓ユ牸鎷掔粷銆佽崏绋垮畾绋裤€佺礌鏉愭剰鍥句笌姝h礋鎻愮ず璇嶈ˉ涓侊紱涓嬩竴杞褰曠埗杞骞朵弗鏍艰姹?1鈥? 涓敓鎴愭垚鍔熶笖鍠滄鐨勫€欓€夈€?
|
||||
- 涓嬩竴杞鍙栫埗杞銆佸€欓€夋睜鍜屽弽棣堢殑鍗曚竴涓€鑷存€у揩鐓э紝骞跺湪鍒涘缓浜嬪姟涓牎楠屽弽棣堢増鏈紱骞跺彂鍙嶉鍙樺寲鏃舵嫆缁濆啓鍏ワ紝涓嶄骇鐢熸贩鍚堟垨娈嬬暀瀛愯疆娆°€?
|
||||
- round 鐨勫垱寤恒€佽鍙栥€佸弽棣堛€佷笅涓€杞拰纭缁熶竴鎺ュ叆 profile 鈫?workflow 鈫?node 鎵€鏈夋潈閾撅紱瓒婃潈浣跨敤璧勬簮涓嶅瓨鍦ㄨ涔夛紝鍚屾椂淇濈暀 system 涓庡叏灞€绠$悊鍛樺吋瀹广€?
|
||||
- 鍓嶇鍙嶉鍙戦€佷笉鍐嶄緷璧栫敓鍥炬ā鍨嬶紱鐐瑰嚮鈥滃紑濮嬩笅涓€杞€濅細鍏堜繚瀛樻湭鎻愪氦鏂囨湰锛屼繚瀛樺け璐ヤ笉娓呯┖銆佷笉寤鸿疆娆★紝骞跺悓姝ュ睍绀哄€欓€夐棬妲涗笌鍙搷浣滃師鍥犮€?
|
||||
|
||||
## [1.0.13] - 2026-07-15
|
||||
|
||||
- 璇︾粏宸ヤ綔娴佺殑浠绘剰鏈皝瀛樺浘鍍忚妭鐐瑰彲鎵撳紑鐙珛绱犳潗鎶藉眽锛屽鐢ㄧ幇鏈変笂浼犮€佺矘璐淬€佽瑙夎瘑鍒€佺‘璁や笌绱犳潗搴撶瓫閫夎兘鍔涳紱鎶藉眽淇濈暀 workflow銆乥ranch銆乶ode銆乺ound 杩斿洖涓婁笅鏂囥€?
|
||||
- 鑺傜偣缁戝畾鎺ュ彛鍙寔涔呭寲宸茬‘璁ょ殑 `material_id` 鍜屽畨鍏ㄥ鑸笂涓嬫枃锛沝ata URL 浠呭湪涓婁紶璇锋眰鍐呭瓨闃舵浣跨敤锛屼笉杩涘叆鑺傜偣鍏冩暟鎹€佸紓姝ヤ换鍔℃垨鍘嗗彶璁板綍銆?
|
||||
- 姣忚疆鐢熸垚鍐荤粨涓嶅彲鍙?material/reference snapshot锛涘悗缁慨鏀圭礌鏉愩€佸垎绫绘垨褰撳墠鑺傜偣缁戝畾涓嶄細鏀瑰彉鍘嗗彶杞锛岄噸鏂版墦寮€瑙掕壊浼氫粠鍚庣鎭㈠瀹屾暣缁戝畾闆嗗悎銆?
|
||||
- 鍐欏叆缁熶竴鏍¢獙璧勬簮 owner銆佸綋鍓嶅垎鏀€佽妭鐐归樁娈典笌鍙鐘舵€侊紱閿佸畾/灏佸瓨鑺傜偣鏄庣‘鎻愮ず鍒涘缓鍒嗘敮銆傛娊灞変娇鐢ㄤ富棰?token銆佸畨鍏ㄨ儗鏅崟鍑诲叧闂拰绉诲姩绔竷灞€銆?
|
||||
|
||||
## [1.0.14] - 2026-07-15
|
||||
|
||||
- 淇濈暀鍘熸湁 VLM 瑙嗚璐ㄦ锛屽苟鏂板鐙珛鐢熸垚寮忎氦鍙夊鏍革紱瀹℃牳鍊欓€夊己鍒舵潵鑷笉鍚岀殑 image-generation provider/model锛岀己灏戦厤缃椂杩斿洖鍙搷浣滅殑 `required_action`銆?
|
||||
- 瀹℃牳姹犲缁堜繚鐣?01 涓诲€欓€夊苟鏍囨敞閫傞厤鍣?妯″瀷锛涘悗缁疆鍙噸鐢熺敤鎴峰嬀閫夐」锛屾湭鍕鹃€夊€欓€変笌妲戒綅淇濇寔涓嶅彉锛屾墍鏈?review/skip/approve 鎿嶄綔鍧囧啓鍏ュ巻鍙层€?
|
||||
- 瀹℃牳閫氳繃鎴栬烦杩囧悗锛屼互骞傜瓑鍘熷瓙浜嬪姟寤虹珛鍙 XF 楂樻竻淇浜ゆ帴杞銆佸€欓€夊拰绱犳潗蹇収锛屽啀鎺ㄨ繘宸ヤ綔娴侊紱娉ㄥ叆澶辫触浼氬畬鏁村洖婊氾紝涓嶇暀涓嬪崐杞鎴栭噸澶嶈褰曘€?
|
||||
- 瀹℃牳鍐崇瓥銆侀噸鐢熷拰閫氳繃鎺ュ彛缁熶竴鎺ュ叆 profile 鈫?workflow 鈫?node 鈫?round 鐨勬墍鏈夋潈閾撅紱鍓嶇瀹℃牳寮圭獥浣跨敤鏆楄壊 token銆佸畨鍏ㄨ儗鏅崟鍑诲叧闂拰绐勫睆甯冨眬銆?
|
||||
|
||||
## [1.0.15] - 2026-07-15
|
||||
|
||||
- 鍦ㄧ幇鏈?`character_gradual` 宸ヤ綔娴佷腑琛ラ綈纭浜鸿銆佹媶瑙h瀹氥€侀閫夌礌鏉愩€佸ご鍍忋€佷笂鍗婅韩銆佷笅鍗婅韩銆佸叏韬€佷笁瑙嗗浘銆佽瀹氬浘銆侀珮娓呬慨澶嶅拰妗f鍗″叡 11 涓鑼冭妭鐐广€?
|
||||
- 淇濈暀鏃ц妭鐐?ID銆佸浘鐗囦笌鍘嗗彶锛沗half_body / expression_sheet / pose_sheet / final_pack` 閫氳繃闆嗕腑鏄犲皠缁х画鍙锛屾棫宸ヤ綔娴佸彧鎯版€цˉ鍏呯己澶辩殑涓嬪崐韬妭鐐广€?
|
||||
- 鍓嶅悗绔彁绀鸿瘝瑙勫垝銆佺礌鏉愮粦瀹氥€佷緷璧栨敞鍏ャ€佹ā鍨嬪垎宸ュ拰鑺傜偣灞曠ず缁熶竴浣跨敤 canonical stage catalog銆?
|
||||
|
||||
## [1.0.16] - 2026-07-15
|
||||
|
||||
- 鍒嗘敮涓庣墿鐞嗚妭鐐瑰壇鏈敼涓哄崟浜嬪姟鍒涘缓锛屾牎楠岀埗鍒嗘敮銆佽嚜鐖躲€佺幆銆佺姸鎬併€佸垏鐐瑰拰鈥滀粎宸插畬鎴愯妭鐐瑰彲鍒嗘敮鈥濄€?
|
||||
- 澧炲姞澶氬眰绁栧厛瑙f瀽鍜?branch_state 闅旂锛涗緷璧栦笂涓嬫枃鏄庣‘鏍囨敞缁ф壙鏉ユ簮鍒嗘敮涓庢潵婧愯妭鐐广€?
|
||||
- 宸ヤ綔鍙板鍔犵揣鍑戝垎鏀潰鏉裤€佺埗閾?鍒囩偣/鐘舵€佸睍绀哄拰鎸夐渶渚濊禆涓婁笅鏂囷紝淇濇寔涓夋爮涓庣Щ鍔ㄧ妯悜甯冨眬銆?
|
||||
|
||||
## [1.0.17] - 2026-07-15
|
||||
|
||||
- 妗f鍗″皝瀛樻敼涓哄崟浜嬪姟锛氭牎楠屾椿鍔ㄥ垎鏀笌鍥涚被蹇呭~璧勪骇锛屽悓鏃跺啓鍏ュ崱銆佽祫浜с€佸ご鍍忋€佸巻鍙层€佹。妗堝崱鑺傜偣骞跺皝瀛樺伐浣滄祦銆?
|
||||
- 鏀寔骞傜瓑銆侀槻璺ㄥ垎鏀覆鍥俱€佸け璐ュ洖婊氾紝浠ュ強鏃?`final_archive_*` 鐨勫彧璇绘。妗堝崱瑙嗗浘銆?
|
||||
- 瑙掕壊鍒嗙被鍥哄畾涓烘湭鐢熸垚銆佸揩閫熻鑹层€佹。妗堝崱锛涜缁嗗伐浣滄祦杩涘害鐙珛灞曠ず锛屼笁绫诲垪琛ㄨ竟妗嗐€佺瓫閫夊拰灏佸瓨鍙鐘舵€佸凡鎺ュ叆銆?
|
||||
|
||||
## [1.0.18] - 2026-07-15
|
||||
|
||||
- `/褰辨` 涓?`/yp` 鐨勫府鍔┿€佺姸鎬併€佸揩閫熻鑹层€佽疆娆°€佸€欓€夈€佽嚜鐒惰瑷€鍙嶉銆佷笅涓€杞€佷弗鏍煎崟鍊欓€夊畾绋裤€佺敓鎴愬紡浜ゅ弶瀹℃煡銆佺礌鏉愩€佸垎鏀拰妗f鍗″懡浠ら€愰」濮旀墭鐩稿悓 canonical handler锛屼笌 Web 鍏辩敤 progression/round/feedback/confirm 鏈嶅姟銆?
|
||||
- 蹇€熻鑹茶亰澶╁叆鍙f敼涓虹幇鏈?`character_fast` 鍥涘浘骞惰娴侊紱璇︾粏瑙掕壊涓嶅啀璋冪敤鏃?select/adopt 鎹峰緞锛屾椿鍔ㄥ垎鏀拰鑺傜偣鎵€鏈夋潈鐢辨湇鍔$鏍¢獙锛実eneration 鑺傜偣鏃犲浘涓嶈兘绌哄畬鎴愩€?
|
||||
- 闀夸换鍔″厛杩斿洖鍏ラ槦鍥炴墽锛涜亰澶?reply target 鎸佷箙鍖栧埌杞锛學orker 缁堟€佷富鍔ㄥ洖浼犮€傞噸杞藉悗鍙仮澶嶅皻鏈‘璁ょ殑 scheduled 閫氱煡锛屽凡 delivered 鐨勯€氱煡鎸佷箙鍘婚噸銆?
|
||||
- 鍗曞浘浠绘剰鐧藉悕鍗曘€佸揩閫?璇︾粏瑙掕壊缇よ亰涓庣敤鎴峰弻鐧藉悕鍗曡鍒欎繚鎸佷笉鍙橈紱鐭ラ亾璧勬簮 ID 涔熶笉鑳界粫杩?profile 鈫?workflow 鈫?node 鈫?round 鎵€鏈夋潈閾俱€?
|
||||
- 甯姪涓庣姸鎬佷紭鍏堣繑鍥炶繍琛屾椂鐢熸垚鐨勫浘鐗囧崱锛屾覆鏌撴垨骞冲彴鑳藉姏涓嶈冻鏃惰嚜鍔ㄥ洖閫€涓烘枃鏈紝涓嶄細闃诲鍛戒护澶勭悊銆?
|
||||
|
||||
## [1.0.19] - 2026-07-15
|
||||
|
||||
- 淇濈暀鐜版湁涓夋爮宸ヤ綔鍙板苟鎶藉嚭 `WorkflowComposer`锛沜atalog銆佽疆娆°€佸€欓€夋睜銆佹瘮杈冦€佺‘璁ゃ€佺礌鏉愩€佸垎鏀笌渚濊禆闈㈡澘缁х画浣跨敤鍗曚竴缁勪欢瀹炵幇锛屽垹闄よ缁嗗伐浣滄祦瀵瑰叏灞€鍗曞浘妯″瀷閫夋嫨鐨勮€﹀悎銆?
|
||||
- 宸ヤ綔娴佺敓鍥捐矾鐢辨寜鈥滄樉寮忚鐩?鈫?鐖惰疆娆?鈫?鑺傜偣 鈫?妯″瀷鍒嗗伐 鈫?瑙掕壊榛樿 鈫?绯荤粺榛樿鈥濆睍绀猴紱棣栬疆鍜屼笅涓€杞粎鍦ㄧ敤鎴锋槑纭鐩栨椂鍙戦€?`model_route`锛岀户鎵挎椂璇锋眰浣撲笉鍖呭惈璇ュ瓧娈点€?
|
||||
- Composer 鍚屾椂鏄剧ず瀹為檯鐢熷浘妯″瀷銆佺户鎵挎潵婧愪笌鎻愮ず璇嶈鍒?LLM锛涙枃鏈?LLM 浼樺厛浣跨敤 translation 娌荤悊璺敱锛屽啀鍥為€€鍒?prompt_planning 鍒嗗伐锛屽苟鏄剧ず閫傞厤鍣?妯″瀷鍙嬪ソ鍚嶃€?
|
||||
- 淇鑺傜偣閿佽涔夛細鐢熸垚瀹屾垚浣嗘湭鏈€缁堥噰鐢ㄧ殑 `completed` 鑺傜偣浠嶅彲鍙嶉銆佸垱寤轰笅涓€杞拰淇敼鑺傜偣绱犳潗锛涗粎 `selected`銆佹湭鏉ヨ妭鐐瑰強灏佸瓨/褰掓。宸ヤ綔娴佸啓閿併€?
|
||||
- 鍘嗗彶杞榛樿鏄剧ず鏈€杩戜笁杞苟鍙睍寮€锛涚粓鎬佸巻鍙蹭笉閲嶅璇锋眰锛屼簨浠跺彧鍒锋柊褰撳墠椤甸潰鍜屽彲瑙佹椿鍔ㄨ疆娆°€傚ご鍍忎娇鐢?cover锛屽叏韬?涓夎鍥?璁惧畾鍥句娇鐢?contain銆?
|
||||
- 鎵€鏈夋柊澧炴诞灞備娇鐢ㄤ富棰?token 鍜屽畨鍏ㄨ儗鏅崟鍑诲叧闂紱绐勫睆 Composer銆佹寜閽竷灞€銆佺┖鐘舵€併€侀儴鍒嗘垚鍔熴€侀敊璇笌閲嶈瘯鎻愮ず瀹屾垚鍥炲綊銆?
|
||||
|
||||
## [1.0.8] - 2026-07-15
|
||||
|
||||
- 璇︾粏瑙掕壊宸ヤ綔娴佹敮鎸侀€愯妭鐐硅В閿侊細閿佸畾鑺傜偣鍙煡鐪嬶紝鍙湁褰撳墠鑺傜偣鍙搷浣溿€?
|
||||
- 鍓嶅悗绔鍔犻槻瓒婄骇鏍¢獙锛涚敓鍥捐妭鐐瑰繀椤婚噰鐢ㄥ浘鐗囧悗鎵嶈兘鎺ㄨ繘銆?
|
||||
- 鎻愮ず璇嶈鍒掋€佺礌鏉愮粦瀹氫繚瀛樹笌纭鍒嗙锛屽苟鍏煎鏃у伐浣滄祦鏁版嵁銆?
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
249
INSTALL.md
249
INSTALL.md
|
|
@ -1,52 +1,56 @@
|
|||
# 安装与打包
|
||||
# 安装、升级与发布包边界
|
||||
|
||||
ImageStudio 是 AstrBot 插件,不是独立 Web 服务。
|
||||
ImageStudio 是 AstrBot 插件,不是独立 Web 服务。本文件同时说明普通安装、旧版本升级、Docker/NAS 手工覆盖和发布包安全边界。
|
||||
|
||||
## 兼容性
|
||||
|
||||
- AstrBot `>= 4.25.0`。
|
||||
- Windows、Linux、macOS 与 Docker(以 AstrBot 本身支持的平台为准)。
|
||||
- Python 版本跟随对应 AstrBot 发行版。
|
||||
- SQLite 默认可用;PostgreSQL 需要额外安装驱动。
|
||||
- AstrBot:`>= 4.25.0,<5`。
|
||||
- Python:跟随对应 AstrBot 发行版。
|
||||
- 平台:Windows、Linux、macOS 和 Docker,以 AstrBot 本身支持范围为准。
|
||||
- 数据库:SQLite 默认可用;PostgreSQL 需要额外驱动。
|
||||
|
||||
## 安装到 AstrBot
|
||||
## 使用 ZIP 安装
|
||||
|
||||
推荐通过 AstrBot 插件管理器安装。手工安装时,将仓库完整复制到 AstrBot 配置的插件目录,目录名保持为 `astrbot_plugin_imagestudio`;常见安装位置为:
|
||||
AstrBot 插件安装器接受 ZIP 时,压缩包根目录必须直接包含 `main.py` 和 `metadata.yaml`,不能再套一层版本目录。解压后的目标目录名应为:
|
||||
|
||||
```text
|
||||
data/plugins/astrbot_plugin_imagestudio/
|
||||
```
|
||||
|
||||
至少保留以下运行文件和目录:
|
||||
最小运行时清单为:
|
||||
|
||||
```text
|
||||
main.py
|
||||
__init__.py
|
||||
metadata.yaml
|
||||
_conf_schema.json
|
||||
config.yaml
|
||||
pages/imagestudio/index.html
|
||||
_conf_schema.json
|
||||
requirements.txt
|
||||
adapters/
|
||||
assets_builtin/
|
||||
backend/
|
||||
core/
|
||||
database/
|
||||
engines/
|
||||
pages/imagestudio/
|
||||
web/
|
||||
```
|
||||
|
||||
重启或在 AstrBot 中重载插件。AstrBot 会发现 `Star` 插件类,调用其 `initialize()`;停用或重载时调用 `terminate()`。插件在 `initialize()` 中使用 `context.register_web_api` 注册 API。
|
||||
发布包还可以包含 `README.md`、`INSTALL.md`、`CHANGELOG.md` 和仅含 `.gitkeep` 的 `data/` 空目录骨架。`pages/imagestudio/` 是 AstrBot 正式插件页面;`web/` 是兼容构建产物,不能代替宿主注册的插件页面。
|
||||
|
||||
## 插件页面与 API
|
||||
安装后重启或重载插件。AstrBot 会发现 `main.py` 中的 `Star` 类,依次执行插件初始化、数据库迁移、API 注册和后台 Worker 启动。
|
||||
|
||||
AstrBot 自动发现 `pages/imagestudio/index.html`。请从 AstrBot 插件管理界面打开页面,不要把未注册的 `/web` 静态目录当作生产入口。
|
||||
## 首次打开与连接
|
||||
|
||||
页面 bridge 使用相对 endpoint:
|
||||
从 AstrBot 的插件管理页面打开 ImageStudio。插件页面使用相对 bridge endpoint,例如:
|
||||
|
||||
```text
|
||||
ui/bootstrap
|
||||
jobs
|
||||
settings/current
|
||||
workflows/<workflow_id>
|
||||
```
|
||||
|
||||
兼容路由如下:
|
||||
兼容路由前缀如下:
|
||||
|
||||
```text
|
||||
# AstrBot v4.25 legacy bridge
|
||||
|
|
@ -56,11 +60,22 @@ settings/current
|
|||
/api/v1/plugins/extensions/astrbot_plugin_imagestudio/<endpoint>
|
||||
```
|
||||
|
||||
`window.AstrBotPluginPage.apiGet("ui/bootstrap")` 这类 bridge 调用只传相对 endpoint;直接请求才拼接当前 `/api/v1/plugins/extensions/...` 前缀。
|
||||
`window.AstrBotPluginPage.apiGet("ui/bootstrap")` 只传相对 endpoint;只有直接 HTTP 请求才拼完整 `/api/v1/plugins/extensions/...` 前缀。页面会异步初始化并自动重试后端连接;如果持续停在连接中,应先查看 AstrBot 容器日志中的插件导入、迁移和 bridge 注册错误,而不是反复点击安装。
|
||||
|
||||
## 配置图片生成 Provider
|
||||
## 配置模型适配器
|
||||
|
||||
ImageStudio 不内置可直接用于生产的云端模型。首次生图前,请在 AstrBot 的插件配置页填写:
|
||||
推荐在 ImageStudio 设置页完成配置:
|
||||
|
||||
1. 添加一个适配器,填写名称、兼容 API 根地址和 API Key 来源。
|
||||
2. 点击“保存并测试”,让测试读取已保存配置,探测模型列表和图片生成 endpoint。
|
||||
3. 为图片生成、提示词规划/扩写、素材识别、视觉质检、生成式交叉审查、高清修复等用途分别选择路由。
|
||||
4. 生成式交叉审查至少需要一个与主候选不同的可用图片生成模型;视觉质检使用视觉模型,两者不能混作一个配置。
|
||||
|
||||
模型输入既可从已探测列表选择,也可手工填写 Provider 的真实模型 ID。工作流节点默认继承该阶段实际路由;只有显式选择覆盖项时,前端才发送 `model_route`。
|
||||
|
||||
### 配置文件引导 Provider
|
||||
|
||||
也可以通过 AstrBot 插件配置提供默认图片 Provider。不要把真实 Key 写进仓库;使用环境变量或宿主 secret store:
|
||||
|
||||
```text
|
||||
default_generation_model: image-main
|
||||
|
|
@ -73,13 +88,7 @@ generation_provider_api_key_env_var: IMAGESTUDIO_PROVIDER_API_KEY
|
|||
generation_provider_timeout_seconds: 180
|
||||
```
|
||||
|
||||
并在启动 AstrBot 的进程环境中提供密钥,例如:
|
||||
|
||||
```powershell
|
||||
$env:IMAGESTUDIO_PROVIDER_API_KEY = "replace-with-secret"
|
||||
```
|
||||
|
||||
对应的 `config.yaml` 结构为:
|
||||
等价的 `config.yaml` 结构:
|
||||
|
||||
```yaml
|
||||
generation:
|
||||
|
|
@ -94,29 +103,9 @@ generation:
|
|||
timeout_seconds: 180
|
||||
```
|
||||
|
||||
`route_name` 是 ImageStudio 使用的逻辑路由名,`model_id` 是 Provider 接收的真实模型名。无鉴权的本地 Provider 可以留空 `api_key_env_var`;除此之外不要把 API Key 直接写入仓库或 `config.yaml`。配置不完整时,生图接口会返回 `MODEL_CAPABILITY_UNCONFIRMED`;Provider 请求失败时任务和节点会进入失败状态,不会使用 mock 结果冒充生产成功。
|
||||
`route_name` 是 ImageStudio 内部逻辑路由,`model_id` 是 Provider 接收的真实模型名。无鉴权的本地 Provider 可以留空 `api_key_env_var`;其他情况必须在启动 AstrBot 的进程或容器环境中提供对应变量。
|
||||
|
||||
## 配置素材视觉识别 Provider
|
||||
|
||||
素材识别需要一个支持图片输入的 OpenAI 兼容多模态模型。在 AstrBot 插件配置页至少填写模型和路由;连接字段留空时会复用上面的生图 Provider。若 Vision 地址、密钥或超时不同而 ID 留空,插件会自动派生独立 Provider ID,避免覆盖生图配置:
|
||||
|
||||
```text
|
||||
vision_provider_model: vision-model-id
|
||||
vision_provider_route: material_vlm
|
||||
vision_provider_endpoint: /v1/chat/completions
|
||||
```
|
||||
|
||||
如需使用独立的视觉服务,可再填写:
|
||||
|
||||
```text
|
||||
vision_provider_id: vision_provider
|
||||
vision_provider_base_url: https://vision-provider.example.com
|
||||
vision_provider_api_key_env_var: IMAGESTUDIO_VISION_API_KEY
|
||||
vision_provider_timeout_seconds: 180
|
||||
vision_provider_enabled: true
|
||||
```
|
||||
|
||||
等价的 `config.yaml` 配置如下。`provider_id`、`base_url`、`api_key_env_var` 和超时留空时均复用 `generation.provider`,但 `model_id` 必须显式配置:
|
||||
素材识别和视觉质检需要支持图片输入的 OpenAI 兼容多模态模型。连接字段留空时可以复用生图 Provider,但 `model_id` 必须明确配置:
|
||||
|
||||
```yaml
|
||||
vision:
|
||||
|
|
@ -132,33 +121,78 @@ vision:
|
|||
enabled: true
|
||||
```
|
||||
|
||||
识别时 ImageStudio 会从插件 VFS 读取 `storage://` 素材文件,校验实际大小、扩展名和 PNG/JPEG/GIF/WebP 文件签名,再编码为 `data:image/...;base64` 发送给视觉 Provider。未配置有效路由或密钥时接口返回 `MODEL_CAPABILITY_UNCONFIRMED`;Provider 调用失败时素材保持 `pending_recognition`,只记录脱敏后的失败类型,不会生成或允许确认 mock 提示词与标签。
|
||||
若视觉服务使用不同的地址、密钥或超时,建议配置独立 Provider ID,避免覆盖图片 Provider。素材识别会读取并校验真实 `storage://` 图片,再临时编码成 data URL 发送给视觉 Provider;data URL 不会写入异步任务、数据库或轮次快照。
|
||||
|
||||
## 运行数据
|
||||
## 没有真实模型时
|
||||
|
||||
AstrBot 插件的持久化数据根目录为:
|
||||
未配置 Provider 不影响打开页面、迁移数据库、查看已有角色/图片/历史、编辑普通元数据或管理队列。需要模型的动作不会回退到假结果:
|
||||
|
||||
- 图片生成、高清修复无法确认能力时会返回缺少模型能力或 Provider 配置错误;
|
||||
- 素材识别失败时保持待识别状态,不会生成可确认的 mock 标签或提示词;
|
||||
- 视觉质检和生成式交叉审查缺少路由时会给出可操作错误;
|
||||
- 异步任务失败会保留脱敏后的失败类型和重试入口,不会冒充成功图片。
|
||||
|
||||
部署验收默认只检查插件加载、迁移、页面连接、只读接口和无成本交互。只有操作者明确允许并确认计费模型后,才应进行真实生成测试。
|
||||
|
||||
## 运行数据与备份
|
||||
|
||||
插件持久化数据位于 AstrBot 数据根目录,而不是插件源码目录:
|
||||
|
||||
```text
|
||||
data/plugin_data/astrbot_plugin_imagestudio/
|
||||
```
|
||||
|
||||
默认 SQLite 配置:
|
||||
其中包含 SQLite 数据库、图片、缩略图、素材、归档、导出和临时状态。配置中的 `data/...` 是映射到该插件数据根目录的逻辑路径,不要再次手工拼接 `data/plugin_data/astrbot_plugin_imagestudio`。
|
||||
|
||||
```yaml
|
||||
database:
|
||||
backend: auto
|
||||
sqlite:
|
||||
# 逻辑路径;AstrBot 运行时映射到插件数据目录
|
||||
path: data/db/imagestudio.db
|
||||
postgresql:
|
||||
enabled: false
|
||||
升级前至少备份:
|
||||
|
||||
```text
|
||||
data/plugin_data/astrbot_plugin_imagestudio/
|
||||
```
|
||||
|
||||
配置中的 `data/...` 是可移植的逻辑路径,AstrBot 运行时会把它映射到上述插件数据根目录;不要在配置里再次手工拼接 `data/plugin_data/astrbot_plugin_imagestudio`,否则可能产生重复目录。图片、素材、缩略图、归档和临时文件也位于同一个插件数据根目录。升级插件源码时不要删除该目录;备份时应单独备份它。
|
||||
使用 PostgreSQL 时还要单独备份对应数据库。不要把运行数据库或图片复制回源码仓库的 `data/` 骨架。
|
||||
|
||||
## 可选 PostgreSQL
|
||||
## 从旧版本升级
|
||||
|
||||
选择 `backend: postgresql`,或在 `backend: auto` 下启用并完整配置 PostgreSQL 后,ImageStudio 会使用 PG:
|
||||
1. 备份插件数据目录和当前插件源码目录。
|
||||
2. 停止插件或 AstrBot 容器,避免覆盖时仍有任务写文件。
|
||||
3. 把新包解压到临时目录,确认 `main.py` 位于临时目录根部。
|
||||
4. 用新包的运行时白名单覆盖插件源码;不要删除或覆盖 `data/plugin_data/astrbot_plugin_imagestudio/`。
|
||||
5. 启动 AstrBot。插件会在注册 API 前按顺序执行 `database/migrations/*.sql`,并在 `schema_migrations` 记录版本。迁移设计为幂等执行;已记录但缺失声明表时会重新校验并修复。
|
||||
6. 检查日志,再打开插件页面验证自动连接、角色列表、工作流、队列和设置。
|
||||
|
||||
升级会保留旧工作流、旧图片和旧节点 ID。详细角色旧 key 会惰性映射到新 11 节点目录,缺失的下半身节点只做增量补充,不重建整条工作流。旧 `final_archive_*` 档案仍可只读查看。
|
||||
|
||||
不要在未恢复数据库备份的情况下直接降级插件源码;新版本迁移后的数据库不保证被旧代码完整理解。如果出现 `no such table`,优先确认是否混用了新旧源码、数据库目录权限是否正确、迁移日志是否完整,不要先删除数据库。
|
||||
|
||||
## Docker / NAS 手工覆盖
|
||||
|
||||
NAS 上通常把宿主 `.../data` 挂载到容器 `/AstrBot/data`。对应路径关系示例:
|
||||
|
||||
```text
|
||||
# 宿主机
|
||||
<astrbot-data>/plugins/astrbot_plugin_imagestudio/
|
||||
<astrbot-data>/plugin_data/astrbot_plugin_imagestudio/
|
||||
|
||||
# 容器内
|
||||
/AstrBot/data/plugins/astrbot_plugin_imagestudio/
|
||||
/AstrBot/data/plugin_data/astrbot_plugin_imagestudio/
|
||||
```
|
||||
|
||||
建议流程:
|
||||
|
||||
1. 将 ZIP 上传到 NAS 临时目录并解压到独立 staging 目录。
|
||||
2. 核对 staging 根目录的 `main.py`、`metadata.yaml`、`pages/imagestudio/index.html`。
|
||||
3. 备份宿主的 `plugin_data/astrbot_plugin_imagestudio/`。
|
||||
4. 只把运行时白名单同步到 `plugins/astrbot_plugin_imagestudio/`。如使用 `rsync --delete`,必须把目标解析为准确的插件源码目录,并显式排除任何误放在源码目录内的 `.env*` 与运行数据;禁止对 `<astrbot-data>` 或 `plugin_data` 根目录执行 `--delete`。
|
||||
5. 通过 SSH 在 NAS 上重启对应 AstrBot Docker 容器,不使用浏览器安装器重复覆盖。
|
||||
6. 检查启动日志中是否存在 `ImportError`、`No module named`、`no such table`、迁移失败或 bridge 注册失败,再进行浏览器验收。
|
||||
|
||||
源码覆盖和容器重启是两个独立步骤;只替换文件不会让已运行的 Python 进程自动载入新版本。
|
||||
|
||||
## PostgreSQL(可选)
|
||||
|
||||
选择 `backend: postgresql`,或在 `backend: auto` 下启用并完整配置 PostgreSQL:
|
||||
|
||||
```yaml
|
||||
database:
|
||||
|
|
@ -172,43 +206,90 @@ database:
|
|||
password: ""
|
||||
```
|
||||
|
||||
默认 SQLite 不需要第三方数据库驱动。为避免给所有安装者强制安装 PG 驱动,`requirements.txt` 中不启用 PostgreSQL 依赖;需要 PG 时任选其一安装:
|
||||
SQLite 不需要第三方数据库驱动。使用 PostgreSQL 时任选一种兼容驱动安装到 AstrBot Python 环境:
|
||||
|
||||
```powershell
|
||||
python -m pip install "psycopg[binary]>=3.1"
|
||||
# 或兼容驱动
|
||||
# 或
|
||||
python -m pip install "psycopg2-binary>=2.9"
|
||||
```
|
||||
|
||||
## 密钥
|
||||
|
||||
不要提交 `.env*`、API Key、DSN、数据库密码或集成 token。私伴集成 token 的轮换 endpoint 为:
|
||||
|
||||
```text
|
||||
POST integrations/private-companion/token/rotate
|
||||
```
|
||||
|
||||
返回的新 token 只展示一次,配置仅保存 salt 与 hash。
|
||||
|
||||
## 验证
|
||||
## 本地无成本验证
|
||||
|
||||
```powershell
|
||||
python -m compileall adapters backend core database engines main.py
|
||||
python -m pytest -q --basetemp=.pytest_tmp_release
|
||||
npm.cmd --prefix frontend run typecheck
|
||||
npm.cmd --prefix frontend run build:plugin
|
||||
npm.cmd --prefix frontend run build:web
|
||||
```
|
||||
|
||||
测试完成后清理 `.pytest_tmp*`、`.pytest_cache`、`__pycache__` 和未使用构建缓存。不得删除真实 `data/plugin_data`、用户素材、生成图片或配置。
|
||||
|
||||
## 发布包白名单
|
||||
|
||||
不要使用无路径限制的 `git archive HEAD`,因为仓库还跟踪测试、审计文档、前端源码和历史 release 文件。正式运行时 ZIP 只归档下列路径:
|
||||
|
||||
```text
|
||||
main.py
|
||||
metadata.yaml
|
||||
config.yaml
|
||||
_conf_schema.json
|
||||
requirements.txt
|
||||
__init__.py
|
||||
README.md
|
||||
INSTALL.md
|
||||
CHANGELOG.md
|
||||
adapters/
|
||||
assets_builtin/
|
||||
backend/
|
||||
core/
|
||||
database/
|
||||
engines/
|
||||
pages/
|
||||
web/
|
||||
data/ # 仅跟踪的 .gitkeep 空目录骨架
|
||||
```
|
||||
|
||||
PowerShell 示例:
|
||||
|
||||
```powershell
|
||||
git archive --format=zip --output=release/astrbot_plugin_imagestudio.zip HEAD -- `
|
||||
main.py metadata.yaml config.yaml _conf_schema.json requirements.txt __init__.py `
|
||||
README.md INSTALL.md CHANGELOG.md `
|
||||
adapters assets_builtin backend core database engines pages web data
|
||||
```
|
||||
|
||||
## 打包
|
||||
以下内容必须排除:
|
||||
|
||||
使用 Git 归档可自动排除未跟踪的本地数据和多数开发产物:
|
||||
- `.git/`、`.agents/`、`.codex/`、`.env*` 和所有凭据文件;
|
||||
- `tests/`、`docs/`、`frontend/`、`node_modules/` 和开发说明/审计源文件;
|
||||
- `release/` 中的旧包、`.pytest_cache/`、`.pytest_tmp*/`、`__pycache__/`、日志和覆盖率缓存;
|
||||
- `data/plugin_data/`、数据库、真实图片、素材、缩略图、归档、导出和临时文件。
|
||||
|
||||
打包前只检查文件名和 Git 跟踪状态即可,不要把 `.env*` 内容输出到终端:
|
||||
|
||||
```powershell
|
||||
git archive --format=zip --output astrbot_plugin_imagestudio.zip HEAD
|
||||
git ls-files -- '.env' '.env.*' '**/.env' '**/.env.*'
|
||||
git ls-files -- 'data/**'
|
||||
git status --short --ignored -- '.env*'
|
||||
```
|
||||
|
||||
发布包应包含源码、迁移、`pages/imagestudio/` 构建产物、配置模板和文档;不应包含:
|
||||
预览白名单归档内容而不生成 ZIP:
|
||||
|
||||
- `.git/`、`.env*`、`node_modules/`、`__pycache__/`。
|
||||
- `data/plugin_data/astrbot_plugin_imagestudio/` 中的任何运行数据。
|
||||
- 本地测试数据库、生成图片、日志、缓存或密钥。
|
||||
```powershell
|
||||
git archive --format=tar HEAD -- `
|
||||
main.py metadata.yaml config.yaml _conf_schema.json requirements.txt __init__.py `
|
||||
README.md INSTALL.md CHANGELOG.md `
|
||||
adapters assets_builtin backend core database engines pages web data |
|
||||
tar -tf -
|
||||
```
|
||||
|
||||
最终应确认归档根目录存在 `main.py`,且清单中没有 `.env*`、真实 `data`、测试、前端工程、缓存或历史安装包。
|
||||
|
||||
## 密钥安全
|
||||
|
||||
- 不提交 `.env*`、API Key、DSN、数据库密码、SSH 私钥或集成 token。
|
||||
- 日志和错误响应只保留脱敏后的错误类型,不回显 Authorization Header 或密钥。
|
||||
- 如果凭据曾出现在本机交接文件、终端输出或历史包中,应在发布前轮换;仅仅把文件加入 `.gitignore` 不能让已泄露凭据恢复安全。
|
||||
- 私伴集成 token 的轮换 endpoint 是 `POST integrations/private-companion/token/rotate`;新 token 只展示一次,配置仅保存 salt 和 hash。
|
||||
|
|
|
|||
168
README.md
168
README.md
|
|
@ -1,114 +1,140 @@
|
|||
# AstrBot ImageStudio
|
||||
|
||||
ImageStudio 是运行在 AstrBot 内的角色资产与生图工作台插件,提供角色档案、工作流、素材库、衣橱、任务队列、日志和模型能力档案等功能。
|
||||
ImageStudio 是运行在 AstrBot 内的角色资产与生图工作台插件。它把角色档案、详细角色工作流、快速角色、素材库、衣橱、生成队列、模型路由、日志和聊天命令放在同一套持久化链路中。
|
||||
|
||||
## 兼容范围
|
||||
## 运行形态与兼容范围
|
||||
|
||||
- AstrBot:`4.25.0` 及以上。
|
||||
- 运行平台:AstrBot 支持的 Windows、Linux、macOS 和 Docker 环境。
|
||||
- 数据库:默认 SQLite;PostgreSQL 为可选后端。
|
||||
- 生产运行方式:由 AstrBot 加载插件,不启动独立 FastAPI、Flask 或 Vite 服务。
|
||||
- AstrBot:`>= 4.25.0,<5`。
|
||||
- 平台:以 AstrBot 支持的 Windows、Linux、macOS 和 Docker 环境为准。
|
||||
- 数据库:默认 SQLite;PostgreSQL 是可选后端。
|
||||
- 生产运行:由 AstrBot 加载 `main.py` 中的 `Star` 插件类,不启动独立 FastAPI、Flask 或 Vite 服务。
|
||||
- 插件页面:生产入口是 `pages/imagestudio/index.html`;`web/` 仅作为兼容构建产物保留。
|
||||
|
||||
## AstrBot 加载方式
|
||||
`initialize()` 会先应用 AstrBot 配置、执行数据库迁移和默认分类初始化,再注册 Web API 并启动持久化候选任务 Worker;`terminate()` 会停止 Worker 并释放运行资源。
|
||||
|
||||
AstrBot 通过 `main.py` 中的 `Star` 插件类自动发现 ImageStudio,并管理其生命周期:
|
||||
## 详细角色工作流
|
||||
|
||||
- `initialize()`:读取 AstrBot 配置、初始化持久化数据,并通过 `context.register_web_api` 注册插件 API。
|
||||
- `terminate()`:插件停用或重载时释放运行资源。
|
||||
- `pages/imagestudio/index.html`:由 AstrBot 自动发现为插件页面,无需手工注册静态 `/web` 入口。
|
||||
详细角色继续使用现有的 `character_gradual` 主线,没有另建平行流程。规范工作流包含 11 个顺序节点:
|
||||
|
||||
## 打开界面
|
||||
| 顺序 | 节点 | 用途 |
|
||||
|---:|---|---|
|
||||
| 1 | 人设导入与拆解 `profile_brief` | 接收粗略人设,拆解身份、性格、外观、服装和一致性锚点 |
|
||||
| 2 | 提示词规划 `prompt_plan` | 规划各图像阶段的正向、负向提示词和继承规则 |
|
||||
| 3 | 素材预选 `material_bind` | 绑定风格、构图、服装、鞋袜、配饰和临时参考 |
|
||||
| 4 | 头像 `headshot` | 确认五官、发型、眼睛、表情和身份锚点 |
|
||||
| 5 | 上半身像 `upper_body` | 确认上身服装、姿态和轮廓 |
|
||||
| 6 | 下半身像 `lower_body` | 确认腰胯、腿部比例、鞋袜和下身细节 |
|
||||
| 7 | 全身像 `full_body` | 确认完整服装、身体比例和整体剪影 |
|
||||
| 8 | 三视图 `turnaround` | 生成正面、侧面和背面的一致视图 |
|
||||
| 9 | 设定图 `design_sheet` | 汇总表情、姿势、手部、配饰和设计细节 |
|
||||
| 10 | 高清修复 `style_lock` | 完成高清修复、身份统一和风格锁定 |
|
||||
| 11 | 档案卡 `profile_card` | 校验必填资产并原子封存角色档案卡 |
|
||||
|
||||
安装并重载插件后,从 AstrBot 的插件管理界面打开 ImageStudio。生产页面来自:
|
||||
节点按顺序解锁。锁定节点仍可查看,但不能生成、改素材或写入结果;已完成节点只能从已完成切点创建分支后继续修改。旧工作流仍通过惰性映射读取:`half_body → upper_body`、`expression_sheet → turnaround`、`pose_sheet → design_sheet`、`final_pack → profile_card`,不会替换旧 `node_id` 或搬动旧图片。
|
||||
|
||||
```text
|
||||
pages/imagestudio/index.html
|
||||
```
|
||||
## 轮次、候选与反馈
|
||||
|
||||
仓库中的 `web/` 是独立前端构建/兼容产物。除非宿主显式注册对应静态路由,否则 `/api/v1/plugins/extensions/astrbot_plugin_imagestudio/web` 不是 AstrBot 保证提供的生产入口。
|
||||
- 每个详细生图轮次生成 4 或 5 个候选;每个候选是一条独立的持久任务、独立超时和隔离输出目录。
|
||||
- 一轮只规划一次提示词、治理、素材快照、模型路由和预算。单个候选失败不会抹掉其他成功结果,界面会显示部分成功和失败摘要。
|
||||
- UUID 是内部主键;`TX-15-03` 这类稳定显示码用于界面和聊天反馈。
|
||||
- “聚焦”“比较”“喜欢/拒绝/严格拒绝”和“最终定稿”是不同状态。最终采用必须明确选择恰好一个成功候选,不会自动采用第一张。
|
||||
- 自然语言反馈支持“1、2 喜欢,3 严格拒绝”等写法。服务端会重新校验编号;没有可用文本 LLM 时使用确定性的本地编号解析,不接受越界或幻觉编号。
|
||||
- 下一轮保存父轮次、用户原文、反馈摘要和正负提示词补丁;重启后可恢复轮次与未完成任务。
|
||||
|
||||
## API 兼容模式
|
||||
## 素材、审查、分支与档案卡
|
||||
|
||||
插件页面优先使用 `window.AstrBotPluginPage` bridge。传给 `apiGet`、`apiPost`、`apiPut` 或 `apiDelete` 的值必须是相对 endpoint,例如:
|
||||
### 素材闭环
|
||||
|
||||
任意尚未封存的图像节点都可以打开素材抽屉,复用素材库的上传、粘贴、识别、确认和绑定能力。异步任务只持久化 `material_id`,不会持久化 data URL。创建轮次时会冻结素材/参考快照,因此后续编辑素材不会改变历史轮次;重新进入角色或切换分支时会从后端恢复绑定和来源。
|
||||
|
||||
### 两类审查
|
||||
|
||||
- 视觉质检:使用视觉模型检查画面质量、身份一致性和结构问题。
|
||||
- 生成式交叉审查:保留主候选为 01,并让不同的图片生成模型产生复审候选;可只重做选中的复审项,也可在确认后跳过。视觉审核模型与交叉生图模型分别配置。
|
||||
|
||||
两条路径都会写入历史记录。交叉审查通过后才把采用结果交给高清修复阶段;没有配置不同的可用生图路由时,会返回可操作的配置错误,不会伪造复审结果。
|
||||
|
||||
### 分支
|
||||
|
||||
`main` 分支常驻。新分支必须从已完成节点创建,服务端校验父分支、切点、自引用和环。提示词、素材绑定、阶段输入、轮次和审查状态按分支隔离;界面同时展示父链、继承来源分支和来源节点。
|
||||
|
||||
### 档案卡
|
||||
|
||||
档案卡不是普通角色记录的别名。封存会在单个幂等事务中校验头像、人设图/文档、立绘、PV 图等必填资产,建立资产 manifest、写入历史并归档工作流。缺少资产或任一步失败时不会留下半张档案卡。封存后工作流只读;旧 `final_archive_*` 数据仍可只读查看。
|
||||
|
||||
## 模型配置与无模型行为
|
||||
|
||||
在 ImageStudio 设置页添加适配器并配置模型分工。图片生成、提示词规划/扩写、素材视觉识别、视觉质检、生成式交叉审查、高清修复等用途可以选择不同路由;详细工作流默认使用当前阶段的实际路由,只有用户显式覆盖时才发送新的 `model_route`。
|
||||
|
||||
ImageStudio 不内置可冒充生产结果的云端模型或 mock 图片。未配置真实 Provider 时:
|
||||
|
||||
- 页面、角色档案、素材元数据、分支、历史、队列查询和已有图片仍可使用;
|
||||
- 需要文本/视觉/生图模型的动作会返回明确的缺失能力或 Provider 错误;
|
||||
- 已创建的异步任务会进入可诊断的失败状态,不会显示为成功;
|
||||
- 可跳过的生成式交叉审查仍需用户明确确认,系统不会静默替用户通过。
|
||||
|
||||
Provider 和安装配置示例见 [INSTALL.md](INSTALL.md)。
|
||||
|
||||
## AstrBot 插件页面与 API
|
||||
|
||||
从 AstrBot 插件管理界面打开 ImageStudio。页面优先使用 `window.AstrBotPluginPage` bridge,并只向 `apiGet`、`apiPost`、`apiPut`、`apiDelete` 传相对 endpoint,例如:
|
||||
|
||||
```text
|
||||
ui/bootstrap
|
||||
dashboard/summary
|
||||
nodes/<node_id>/generate
|
||||
workflows/<workflow_id>/branches
|
||||
nodes/<node_id>/rounds
|
||||
rounds/<round_id>/feedback
|
||||
```
|
||||
|
||||
ImageStudio 同时兼容两套路由前缀:
|
||||
插件兼容两套路由前缀:
|
||||
|
||||
- AstrBot v4.25 legacy bridge:`/api/plug/astrbot_plugin_imagestudio/<endpoint>`。
|
||||
- 当前扩展 API:`/api/v1/plugins/extensions/astrbot_plugin_imagestudio/<endpoint>`。
|
||||
|
||||
bridge 会处理宿主前缀;前端不应把完整 `/api/plug/...` 或 `/api/v1/...` 地址传给 bridge。直接 `fetch` 时才使用当前 `/api/v1` 前缀。
|
||||
|
||||
## 运行与权限边界
|
||||
|
||||
ImageStudio 当前定位为 AstrBot WebUI 内的管理工作台。Bridge 只接受 AstrBot 已认证请求,并为插件页面授予显式的写入、配置、素材和任务重试能力;不会信任浏览器自行提交的用户名、用户 ID 或转发 IP 请求头。
|
||||
|
||||
角色档案列表和档案读写已按 AstrBot 登录身份隔离,但工作流、图片、任务、衣橱、Dashboard 和媒体索引尚未形成完整的跨资源多租户授权链。因此当前版本应仅暴露给可信 AstrBot 管理员,不应作为面向多个互不信任用户的公共工作台部署。
|
||||
|
||||
## 配置生图 Provider
|
||||
|
||||
生产生图需要一个 OpenAI 兼容的图片生成接口。请在 AstrBot 插件配置中填写至少以下字段:
|
||||
|
||||
```text
|
||||
default_generation_model = image-main
|
||||
generation_provider_base_url = https://provider.example.com
|
||||
generation_provider_model = provider-model-id
|
||||
generation_provider_route = image-main
|
||||
generation_provider_api_key_env_var = IMAGESTUDIO_PROVIDER_API_KEY
|
||||
```
|
||||
|
||||
然后在 AstrBot 进程环境中设置对应 API Key。无鉴权的本地 Provider 可以把 `generation_provider_api_key_env_var` 留空。Provider 未配置完整或调用失败时,生产模式会返回明确错误并把任务/节点标记为失败,不会把 mock 图片伪装成成功结果。完整示例见 [INSTALL.md](INSTALL.md)。
|
||||
|
||||
素材识别还需配置 `vision_provider_model`、`vision_provider_route` 和 `vision_provider_endpoint`。Vision 的 Provider ID、根地址、API Key 环境变量和超时可留空以复用生图 Provider;如果连接或超时不同但 ID 留空,插件会自动派生独立 Vision Provider ID,避免覆盖生图配置。识别模型必须显式填写。识别链会读取并校验真实 `storage://` 图片后发送 data URL,无有效路由或 Provider 失败时不会回退到 mock 结果。完整配置见 [INSTALL.md](INSTALL.md)。
|
||||
|
||||
## 主要功能
|
||||
|
||||
- 查看运行概览、队列、最近任务、最近图片和日志。
|
||||
- 创建角色档案及角色工作流,按节点生成、重试、审查、采纳和归档图片。
|
||||
- 管理素材分类、素材库、衣橱与穿搭匹配。
|
||||
- 登记、确认和校验 OpenAI 兼容模型能力档案;当前 discovery endpoint 是安全的元数据检查,不会主动向任意地址发起探测请求。
|
||||
- 使用 SQLite 零配置运行,或按需切换 PostgreSQL。
|
||||
直接 `fetch` 时才拼完整扩展 API;bridge 调用不能重复拼宿主前缀。当前产品定位是已认证 AstrBot 管理界面,不应把插件 API 直接暴露为面向互不信任用户的公共服务。工作流、轮次、图片、素材和任务的聊天/Web 操作会沿资源 owner 链校验,不能仅凭已知 ID 操作其他用户资源。
|
||||
|
||||
## 聊天命令
|
||||
|
||||
插件提供 `/影棚` 和短别名 `/yp`,两套命令行为一致。聊天权限在设置页“聊天权限”中配置。
|
||||
`/影棚` 与短别名 `/yp` 完全等价。常用命令如下;完整语法以 `/yp 帮助` 返回为准。
|
||||
|
||||
```text
|
||||
/yp 帮助
|
||||
/yp 状态
|
||||
/yp 单图 <提示词> [--角色 名称] [--模型 路由] [--尺寸 1K|2K|4K] [--数量 1-4]
|
||||
/yp 快速角色 <角色名和描述>
|
||||
/yp 确认
|
||||
/yp 工作流 <角色名或ID>
|
||||
/yp 节点 <workflow_id>
|
||||
/yp 生成 <node_id>
|
||||
/yp 下一步 <workflow_id>
|
||||
/yp 候选图 <node_id>
|
||||
/yp 选择 <序号>
|
||||
/yp 审核 <node_id|workflow_id>
|
||||
/yp 高清 <image_id> 2K|4K
|
||||
/yp 素材导入 <分类名称或ID> (命令消息需附带图片)
|
||||
/yp 加入衣橱 <material_id> <角色名或ID> <衣橱分类>
|
||||
/yp 快速角色 <角色名> <角色描述>
|
||||
/yp 工作流 <角色名或角色 ID>
|
||||
/yp 单图 <提示词>
|
||||
|
||||
/yp 轮次 <node_id> [4|5] [可选提示词]
|
||||
/yp 轮次状态 <round_id>
|
||||
/yp 候选 <round_id>
|
||||
/yp 反馈 <round_id> <自然语言反馈>
|
||||
/yp 下一轮 <round_id> [4|5]
|
||||
/yp 定稿 <round_id> <候选编号>
|
||||
|
||||
/yp 审查 <round_id> <候选编号> [审核|跳过]
|
||||
/yp 审查重做 <round_id> <候选编号,候选编号> [提示词修订]
|
||||
/yp 审查通过 <round_id> <候选编号>
|
||||
/yp 素材 <node_id> [material_id,material_id]
|
||||
/yp 分支 <workflow_id>
|
||||
/yp 新建分支 <workflow_id> <已完成切点node_id> <新分支名>
|
||||
/yp 切换分支 <workflow_id> <分支名>
|
||||
/yp 档案卡 <workflow_id> [可选人设文档]
|
||||
|
||||
/yp 任务 [task_id]
|
||||
/yp 取消 <task_id>
|
||||
/yp 重试 <task_id>
|
||||
```
|
||||
|
||||
快速角色和详细角色工作流要求群聊、用户白名单同时命中;单图允许任意一个白名单命中。聊天生成还受用户/群聊并发、每日额度和 2K/4K 开关约束。单图在后台线程执行,命令会立即返回请求 ID,完成后通过 AstrBot 统一消息来源主动发送图片。
|
||||
快速角色和详细角色工作流要求群聊白名单与用户白名单同时命中;单图允许任意一个白名单命中。长任务会立即返回已入队信息,完成或失败后通过原消息来源主动回传,重载后也可恢复尚未发送的终态通知。
|
||||
|
||||
## 持久化数据
|
||||
|
||||
运行数据统一放在 AstrBot 数据目录中:
|
||||
运行数据统一位于 AstrBot 数据目录:
|
||||
|
||||
```text
|
||||
data/plugin_data/astrbot_plugin_imagestudio/
|
||||
```
|
||||
|
||||
数据库、图片、缩略图、素材、归档和临时文件都应位于该目录下,不能依赖插件源码目录可写。安装、升级和打包细节见 [INSTALL.md](INSTALL.md),开发约定见 [DEVELOPMENT.md](DEVELOPMENT.md)。
|
||||
数据库、图片、缩略图、素材、归档和临时文件不能依赖插件源码目录可写。升级前应单独备份该目录,覆盖插件源码时不得删除它。安装、升级、迁移、Docker/NAS 手工覆盖和发布包边界见 [INSTALL.md](INSTALL.md)。
|
||||
|
||||
本轮 AstrBot 兼容、后端、前端、功能闭环与安全审计记录见 [docs/code-audit-2026-07-10.md](docs/code-audit-2026-07-10.md)。
|
||||
开发约定见 [DEVELOPMENT.md](DEVELOPMENT.md),本轮详细工作流审计见 [docs/workflow-secondary-audit-2026-07-15.md](docs/workflow-secondary-audit-2026-07-15.md)。
|
||||
|
|
|
|||
|
|
@ -80,11 +80,28 @@
|
|||
"default": true
|
||||
},
|
||||
"model_material_recognition": {"description":"素材导入解析使用的 Vision 路由。","type":"string","default":"material_vlm"},
|
||||
"model_advanced_style_consensus": {"description":"\u8fdb\u9636\u753b\u98ce\u591a\u56fe\u4ea4\u53c9\u5f52\u7eb3\u4f7f\u7528\u7684 Vision \u8def\u7531\uff1b\u7559\u7a7a\u65f6\u6cbf\u7528\u8fdb\u9636\u7d20\u6750\u8bc6\u522b\u8def\u7531\u3002","type":"string","default":""},
|
||||
"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":""},
|
||||
"model_visual_review_routes": {"description":"视觉质量审核使用的 Vision 路由列表。","type":"list","default":["material_vlm"]},
|
||||
"model_cross_review_primary": {"description":"候选图交叉审核主 Vision 路由。","type":"string","default":"material_vlm"},
|
||||
"model_cross_review_secondary": {"description":"候选图交叉审核复核 Vision 路由。","type":"string","default":"material_vlm"},
|
||||
"model_cross_review_routes": {"description":"兼容候选图视觉审核路由列表。","type":"list","default":["material_vlm","material_vlm"]},
|
||||
"model_generation_cross_review_primary": {"description":"生成式交叉审查主生图路由。","type":"string","default":""},
|
||||
"model_generation_cross_review_secondary": {"description":"生成式交叉审查复核生图路由。","type":"string","default":""},
|
||||
"model_generation_cross_review_routes": {"description":"生成式交叉审查使用的生图路由列表。","type":"list","default":[]},
|
||||
"model_quick_generation": {"description":"快速生图使用的模型路由。","type":"string","default":""},
|
||||
"model_portrait_generation": {"description":"头像草稿使用的生图路由。","type":"string","default":""},
|
||||
"model_halfbody_generation": {"description":"半身角色图使用的生图路由。","type":"string","default":""},
|
||||
|
|
@ -114,6 +131,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",
|
||||
|
|
|
|||
|
|
@ -6,28 +6,39 @@ import hashlib
|
|||
from pathlib import Path
|
||||
from typing import Iterable
|
||||
|
||||
from core.runtime_paths import resolve_data_path
|
||||
|
||||
CARD_DIR = Path(__file__).resolve().parent.parent / "data" / "chat_cards"
|
||||
|
||||
CARD_DIR = resolve_data_path("data/chat_cards")
|
||||
|
||||
|
||||
def render_help_card(lines: Iterable[str]) -> Path:
|
||||
resolved_lines = [str(line) for line in lines]
|
||||
return _render_card(
|
||||
key="help-v1",
|
||||
key="help-v2-" + "|".join(resolved_lines),
|
||||
eyebrow="IMAGESTUDIO COMMANDS",
|
||||
title="影棚命令帮助",
|
||||
subtitle="使用 /影棚 或 /yp 调用",
|
||||
lines=list(lines),
|
||||
lines=resolved_lines,
|
||||
accent=(40, 169, 151),
|
||||
)
|
||||
|
||||
|
||||
def render_status_card(*, state: str, version: str, database: str, api: str) -> Path:
|
||||
def render_status_card(
|
||||
*,
|
||||
state: str,
|
||||
version: str,
|
||||
database: str,
|
||||
api: str,
|
||||
details: Iterable[str] | None = None,
|
||||
) -> Path:
|
||||
detail_lines = [str(line) for line in (details or []) if str(line).strip()]
|
||||
return _render_card(
|
||||
key=f"status-{state}-{version}-{database}-{api}",
|
||||
key=f"status-{state}-{version}-{database}-{api}-{'|'.join(detail_lines)}",
|
||||
eyebrow="IMAGESTUDIO STATUS",
|
||||
title=f"运行状态 · {state}",
|
||||
subtitle=f"版本 {version}",
|
||||
lines=[f"数据库 {database}", f"接口 {api}", "聊天命令框架已加载"],
|
||||
lines=[f"数据库 {database}", f"接口 {api}", *detail_lines],
|
||||
accent=(40, 169, 151) if state == "正常" else (224, 157, 55),
|
||||
)
|
||||
|
||||
|
|
|
|||
|
|
@ -58,12 +58,21 @@ def help_text() -> str:
|
|||
/影棚 快速角色 <角色描述>
|
||||
/影棚 工作流 <角色名或角色 ID>
|
||||
/影棚 单图 <提示词>
|
||||
/影棚 轮次 <node_id> [4|5] [提示词]
|
||||
/影棚 候选 <round_id>
|
||||
/影棚 反馈 <round_id> <自然语言反馈>
|
||||
/影棚 下一轮 <round_id> [4|5]
|
||||
/影棚 定稿 <round_id> <候选编号>
|
||||
/影棚 审查 <round_id> <候选编号> [审核|跳过]
|
||||
/影棚 素材 <node_id> [material_id,...]
|
||||
/影棚 分支 <workflow_id>
|
||||
/影棚 档案卡 <workflow_id> [人设文档]
|
||||
|
||||
权限规则:
|
||||
• 快速角色、角色工作流:群聊和用户白名单必须同时命中
|
||||
• 单图:群聊或用户任一白名单命中即可
|
||||
|
||||
当前为命令框架版本,后续将逐步接入角色选择、节点生成、选图和任务回执。"""
|
||||
详细工作流使用稳定候选编号;只有“定稿/审查通过”会正式采用并推进节点。"""
|
||||
|
||||
|
||||
def access_denied_text(action: str, context: ChatCommandContext) -> str:
|
||||
|
|
|
|||
326
backend/dto/advanced_material_dto.py
普通文件
326
backend/dto/advanced_material_dto.py
普通文件
|
|
@ -0,0 +1,326 @@
|
|||
"""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]:
|
||||
metadata = dict(self.metadata)
|
||||
applied = bool(metadata.get("reference_image_applied"))
|
||||
if "input_image_count" in metadata:
|
||||
try:
|
||||
input_image_count = int(metadata.get("input_image_count") or 0)
|
||||
except (TypeError, ValueError):
|
||||
input_image_count = 1 if applied else 0
|
||||
else:
|
||||
input_image_count = 1 if applied else (1 if metadata.get("validation_reference_enabled") and metadata.get("validation_reference_source_uri") else 0)
|
||||
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": metadata,
|
||||
"input_image_count": input_image_count,
|
||||
"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,
|
||||
}
|
||||
65
backend/dto/profile_card_dto.py
普通文件
65
backend/dto/profile_card_dto.py
普通文件
|
|
@ -0,0 +1,65 @@
|
|||
"""Character profile-card DTOs."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CharacterProfileCardAsset:
|
||||
asset_id: str
|
||||
card_id: str
|
||||
asset_role: str
|
||||
image_id: str
|
||||
image_uri: str
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"asset_id": self.asset_id,
|
||||
"card_id": self.card_id,
|
||||
"asset_role": self.asset_role,
|
||||
"image_id": self.image_id,
|
||||
"image_uri": self.image_uri,
|
||||
"metadata": self.metadata,
|
||||
"created_at": self.created_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CharacterProfileCard:
|
||||
card_id: str
|
||||
profile_id: str
|
||||
workflow_id: str
|
||||
branch_name: str
|
||||
version: int
|
||||
status: str
|
||||
persona_document: str
|
||||
idempotency_key: str | None
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
assets: tuple[CharacterProfileCardAsset, ...] = ()
|
||||
|
||||
@property
|
||||
def read_only(self) -> bool:
|
||||
return self.status in {"sealed", "legacy_read_only"}
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"card_id": self.card_id,
|
||||
"profile_id": self.profile_id,
|
||||
"workflow_id": self.workflow_id,
|
||||
"branch_name": self.branch_name,
|
||||
"version": self.version,
|
||||
"status": self.status,
|
||||
"read_only": self.read_only,
|
||||
"persona_document": self.persona_document,
|
||||
"idempotency_key": self.idempotency_key,
|
||||
"metadata": self.metadata,
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
"assets": [asset.to_dict() for asset in self.assets],
|
||||
}
|
||||
|
|
@ -26,6 +26,8 @@ class CharacterProfile:
|
|||
created_at: str
|
||||
updated_at: str
|
||||
generation_kind: str = "ungenerated"
|
||||
profile_category: str = "ungenerated"
|
||||
detailed_workflow_state: str | None = None
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
|
|
@ -47,6 +49,8 @@ class CharacterProfile:
|
|||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
"generation_kind": self.generation_kind,
|
||||
"profile_category": self.profile_category,
|
||||
"detailed_workflow_state": self.detailed_workflow_state,
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
"""Transport-safe results for conversational workflow candidate feedback."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WorkflowFeedbackCandidateRef:
|
||||
"""A candidate resolved through a caller-owned display-code whitelist."""
|
||||
|
||||
candidate_id: str
|
||||
display_code: str
|
||||
|
||||
def to_dict(self) -> dict[str, str]:
|
||||
return {
|
||||
"candidate_id": self.candidate_id,
|
||||
"display_code": self.display_code,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WorkflowFeedbackInterpretation:
|
||||
"""Validated feedback actions extracted from one conversational message."""
|
||||
|
||||
liked: tuple[WorkflowFeedbackCandidateRef, ...] = ()
|
||||
rejected: tuple[WorkflowFeedbackCandidateRef, ...] = ()
|
||||
strict_rejected: tuple[WorkflowFeedbackCandidateRef, ...] = ()
|
||||
final: WorkflowFeedbackCandidateRef | None = None
|
||||
material_intent: str | None = None
|
||||
next_round_prompt_patch: str | None = None
|
||||
positive_patch: str | None = None
|
||||
negative_patch: str | None = None
|
||||
source: str = "deterministic"
|
||||
source_text: str = ""
|
||||
metadata: dict[str, Any] | None = None
|
||||
|
||||
@property
|
||||
def has_candidate_feedback(self) -> bool:
|
||||
return bool(self.liked or self.rejected or self.strict_rejected or self.final)
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"liked": [candidate.to_dict() for candidate in self.liked],
|
||||
"rejected": [candidate.to_dict() for candidate in self.rejected],
|
||||
"strict_rejected": [candidate.to_dict() for candidate in self.strict_rejected],
|
||||
"final": self.final.to_dict() if self.final else None,
|
||||
"material_intent": self.material_intent,
|
||||
"next_round_prompt_patch": self.next_round_prompt_patch,
|
||||
"positive_patch": self.positive_patch or self.next_round_prompt_patch,
|
||||
"negative_patch": self.negative_patch,
|
||||
"source": self.source,
|
||||
"source_text": self.source_text,
|
||||
"metadata": dict(self.metadata or {}),
|
||||
}
|
||||
148
backend/dto/workflow_round_dto.py
普通文件
148
backend/dto/workflow_round_dto.py
普通文件
|
|
@ -0,0 +1,148 @@
|
|||
"""DTOs for workflow generation rounds and their stable candidate pools."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WorkflowGenerationRound:
|
||||
round_id: str
|
||||
workflow_id: str
|
||||
node_id: str
|
||||
branch_name: str
|
||||
round_kind: str
|
||||
round_number: int
|
||||
display_code: str
|
||||
parent_round_id: str | None
|
||||
status: str
|
||||
requested_count: int
|
||||
idempotency_key: str | None
|
||||
prompt_snapshot: dict[str, Any]
|
||||
reference_snapshot: dict[str, Any]
|
||||
feedback_snapshot: dict[str, Any]
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"round_id": self.round_id,
|
||||
"workflow_id": self.workflow_id,
|
||||
"node_id": self.node_id,
|
||||
"branch_name": self.branch_name,
|
||||
"round_kind": self.round_kind,
|
||||
"round_number": self.round_number,
|
||||
"display_code": self.display_code,
|
||||
"parent_round_id": self.parent_round_id,
|
||||
"status": self.status,
|
||||
"requested_count": self.requested_count,
|
||||
"idempotency_key": self.idempotency_key,
|
||||
"prompt_snapshot": self.prompt_snapshot,
|
||||
"reference_snapshot": self.reference_snapshot,
|
||||
"feedback_snapshot": self.feedback_snapshot,
|
||||
"metadata": self.metadata,
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WorkflowGenerationCandidate:
|
||||
candidate_id: str
|
||||
origin_round_id: str
|
||||
candidate_index: int
|
||||
generation_status: str
|
||||
image_id: str | None
|
||||
current_task_id: str | None
|
||||
source_candidate_id: str | None
|
||||
model_route: str | None
|
||||
provider_id: str | None
|
||||
provider_model: str | None
|
||||
prompt_snapshot: dict[str, Any]
|
||||
failure: dict[str, Any]
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"candidate_id": self.candidate_id,
|
||||
"origin_round_id": self.origin_round_id,
|
||||
"candidate_index": self.candidate_index,
|
||||
"generation_status": self.generation_status,
|
||||
"image_id": self.image_id,
|
||||
"current_task_id": self.current_task_id,
|
||||
"source_candidate_id": self.source_candidate_id,
|
||||
"model_route": self.model_route,
|
||||
"provider_id": self.provider_id,
|
||||
"provider_model": self.provider_model,
|
||||
"prompt_snapshot": self.prompt_snapshot,
|
||||
"failure": self.failure,
|
||||
"metadata": self.metadata,
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WorkflowRoundCandidatePoolEntry:
|
||||
round_id: str
|
||||
candidate_id: str
|
||||
display_index: int
|
||||
display_code: str
|
||||
pool_state: str
|
||||
preference_status: str
|
||||
preference_reason: str | None
|
||||
is_final: bool
|
||||
metadata: dict[str, Any]
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"round_id": self.round_id,
|
||||
"candidate_id": self.candidate_id,
|
||||
"display_index": self.display_index,
|
||||
"display_code": self.display_code,
|
||||
"pool_state": self.pool_state,
|
||||
"preference_status": self.preference_status,
|
||||
"preference_reason": self.preference_reason,
|
||||
"is_final": self.is_final,
|
||||
"metadata": self.metadata,
|
||||
"created_at": self.created_at,
|
||||
"updated_at": self.updated_at,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WorkflowRoundCandidateView:
|
||||
pool: WorkflowRoundCandidatePoolEntry
|
||||
candidate: WorkflowGenerationCandidate
|
||||
image: dict[str, Any] | None = None
|
||||
task: dict[str, Any] | None = None
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
**self.pool.to_dict(),
|
||||
"candidate": self.candidate.to_dict(),
|
||||
"image": self.image,
|
||||
"task": self.task,
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WorkflowGenerationRoundDetail:
|
||||
round_record: WorkflowGenerationRound
|
||||
candidates: tuple[WorkflowRoundCandidateView, ...]
|
||||
read_only: bool = False
|
||||
legacy: bool = False
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
return {
|
||||
"round": self.round_record.to_dict(),
|
||||
"candidates": [candidate.to_dict() for candidate in self.candidates],
|
||||
"read_only": self.read_only,
|
||||
"legacy": self.legacy,
|
||||
}
|
||||
|
|
@ -67,12 +67,14 @@ ROUTES: tuple[RouteSpec, ...] = (
|
|||
RouteSpec("POST", "profiles/{profile_id}/archive", "backend.routes.profile_routes.archive_profile", "write"),
|
||||
RouteSpec("GET", "profiles/{profile_id}/workflows", "backend.routes.workflow_routes.list_profile_workflows", "read"),
|
||||
RouteSpec("POST", "profiles/{profile_id}/workflows", "backend.routes.workflow_routes.create_profile_workflow", "write"),
|
||||
RouteSpec("GET", "profiles/{profile_id}/profile-cards", "backend.routes.profile_card_routes.list_profile_cards", "read"),
|
||||
RouteSpec("GET", "workflows/{workflow_id}", "backend.routes.workflow_routes.get_workflow", "read"),
|
||||
RouteSpec("PUT", "workflows/{workflow_id}", "backend.routes.workflow_routes.update_workflow", "write"),
|
||||
RouteSpec("POST", "workflows/{workflow_id}/start", "backend.routes.workflow_routes.start_workflow", "write"),
|
||||
RouteSpec("POST", "workflows/{workflow_id}/cancel", "backend.routes.workflow_routes.cancel_workflow", "write"),
|
||||
RouteSpec("POST", "workflows/{workflow_id}/archive", "backend.routes.workflow_routes.archive_workflow", "write"),
|
||||
RouteSpec("POST", "workflows/{workflow_id}/execute-character", "backend.routes.workflow_routes.execute_character_workflow", "write"),
|
||||
RouteSpec("POST", "workflows/{workflow_id}/finalize-card", "backend.routes.profile_card_routes.finalize_profile_card", "write"),
|
||||
RouteSpec("GET", "workflows/{workflow_id}/nodes", "backend.routes.node_routes.list_workflow_nodes", "read"),
|
||||
RouteSpec("POST", "workflows/{workflow_id}/nodes", "backend.routes.node_routes.create_workflow_node", "write"),
|
||||
RouteSpec("GET", "workflows/{workflow_id}/branches", "backend.routes.branch_routes.list_workflow_branches", "read"),
|
||||
|
|
@ -82,6 +84,32 @@ ROUTES: tuple[RouteSpec, ...] = (
|
|||
RouteSpec("POST", "workflows/{workflow_id}/history", "backend.routes.history_routes.save_workflow_history", "write"),
|
||||
RouteSpec("GET", "nodes/{node_id}", "backend.routes.node_routes.get_node", "read"),
|
||||
RouteSpec("GET", "nodes/{node_id}/dependency-context", "backend.routes.node_routes.get_node_dependency_context", "read"),
|
||||
RouteSpec("GET", "nodes/{node_id}/material-bindings", "backend.routes.node_material_routes.get_node_material_bindings", "read"),
|
||||
RouteSpec("PUT", "nodes/{node_id}/material-bindings", "backend.routes.node_material_routes.save_node_material_bindings", "write"),
|
||||
RouteSpec("GET", "nodes/{node_id}/rounds", "backend.routes.round_routes.list_node_rounds", "read"),
|
||||
RouteSpec("POST", "nodes/{node_id}/rounds", "backend.routes.round_routes.create_node_round", "write"),
|
||||
RouteSpec("GET", "rounds/{round_id}", "backend.routes.round_routes.get_round", "read"),
|
||||
RouteSpec("POST", "rounds/{round_id}/feedback", "backend.routes.round_routes.update_round_feedback", "write"),
|
||||
RouteSpec("POST", "rounds/{round_id}/next", "backend.routes.round_routes.create_next_round", "write"),
|
||||
RouteSpec("POST", "rounds/{round_id}/confirm", "backend.routes.round_routes.confirm_round_candidate", "write"),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"rounds/{round_id}/generation-cross-review",
|
||||
"backend.routes.generation_cross_review_routes.decide_generation_cross_review",
|
||||
"write",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"rounds/{round_id}/generation-cross-review/regenerate",
|
||||
"backend.routes.generation_cross_review_routes.regenerate_generation_cross_review",
|
||||
"write",
|
||||
),
|
||||
RouteSpec(
|
||||
"POST",
|
||||
"rounds/{round_id}/generation-cross-review/approve",
|
||||
"backend.routes.generation_cross_review_routes.approve_generation_cross_review",
|
||||
"write",
|
||||
),
|
||||
RouteSpec("PUT", "nodes/{node_id}", "backend.routes.node_routes.update_node", "write"),
|
||||
RouteSpec("POST", "nodes/{node_id}/generate", "backend.routes.node_routes.generate_node", "write"),
|
||||
RouteSpec("POST", "nodes/{node_id}/retry", "backend.routes.node_routes.retry_node", "retry"),
|
||||
|
|
@ -105,14 +133,20 @@ ROUTES: tuple[RouteSpec, ...] = (
|
|||
RouteSpec("POST", "images/{image_id}/moderation-review", "backend.routes.image_routes.review_moderated_image", "materials"),
|
||||
RouteSpec("POST", "images/{image_id}/safe-delete", "backend.routes.image_routes.safe_delete_image", "write"),
|
||||
RouteSpec("DELETE", "images/{image_id}", "backend.routes.image_routes.delete_image", "write"),
|
||||
RouteSpec("GET", "profile-cards/{card_id}", "backend.routes.profile_card_routes.get_profile_card", "read"),
|
||||
RouteSpec("GET", "jobs", "backend.routes.job_routes.list_jobs", "read"),
|
||||
RouteSpec("GET", "jobs/{task_id}", "backend.routes.job_routes.get_job", "read"),
|
||||
RouteSpec("DELETE", "jobs/{task_id}", "backend.routes.job_routes.delete_job", "retry"),
|
||||
RouteSpec("POST", "jobs/{task_id}/cancel", "backend.routes.job_routes.cancel_job", "write"),
|
||||
RouteSpec("POST", "jobs/{task_id}/retry", "backend.routes.job_routes.retry_job", "retry"),
|
||||
RouteSpec("POST", "jobs/failed/retry", "backend.routes.job_routes.retry_failed_jobs", "retry"),
|
||||
RouteSpec("POST", "jobs/failed/clear", "backend.routes.job_routes.clear_failed_jobs", "retry"),
|
||||
RouteSpec("POST", "jobs/terminal/clear", "backend.routes.job_routes.clear_terminal_jobs", "retry"),
|
||||
RouteSpec("POST", "jobs/dead-letter/retry", "backend.routes.job_routes.retry_dead_letter_jobs", "retry"),
|
||||
RouteSpec("POST", "jobs/dead-letter/move-failed", "backend.routes.job_routes.move_failed_jobs_to_dead_letter", "retry"),
|
||||
RouteSpec("GET", "costs/usage", "backend.routes.cost_routes.get_cost_usage", "read"),
|
||||
RouteSpec("POST", "costs/guard", "backend.routes.cost_routes.check_cost_guard", "read"),
|
||||
RouteSpec("GET", "models/runtime", "backend.routes.model_runtime_routes.list_runtime_models", "read"),
|
||||
RouteSpec("GET", "settings/schema", "backend.routes.settings_routes.get_settings_schema", "read"),
|
||||
RouteSpec("GET", "settings/current", "backend.routes.settings_routes.get_current_settings", "read"),
|
||||
RouteSpec("POST", "settings/validate", "backend.routes.settings_routes.validate_settings", "config"),
|
||||
|
|
@ -133,7 +167,112 @@ ROUTES: tuple[RouteSpec, ...] = (
|
|||
RouteSpec("GET", "media/sign", "backend.routes.media_routes.sign_media", "read"),
|
||||
RouteSpec("GET", "media/index", "backend.routes.media_routes.index_media", "read"),
|
||||
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
|
||||
|
|
@ -0,0 +1,93 @@
|
|||
"""HTTP handlers for generation-based workflow cross review."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.routes.common import handle_request
|
||||
from backend.routes.round_routes import _create_round_service, _serialize_round_detail
|
||||
from backend.services.media_service import MediaService
|
||||
from backend.services.generation_cross_review_service import GenerationCrossReviewPlanner
|
||||
from backend.services.model_provider_service import ModelProviderService
|
||||
from backend.services.workflow_generation_cross_review_service import WorkflowGenerationCrossReviewService
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from core.security import require_write
|
||||
from database.db_helper import Database, load_config_payload, load_database_settings
|
||||
from database.repositories.model_provider_repository import ModelProviderRepository
|
||||
from database.repositories.workflow_cross_review_repository import WorkflowCrossReviewRepository
|
||||
|
||||
|
||||
def decide_generation_cross_review(
|
||||
ctx: RequestContext,
|
||||
round_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: WorkflowGenerationCrossReviewService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict[str, Any]:
|
||||
resolved = service or _create_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return _serialize_round_detail(
|
||||
resolved.decide(round_id, payload or {}, ctx),
|
||||
signer,
|
||||
)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def regenerate_generation_cross_review(
|
||||
ctx: RequestContext,
|
||||
round_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: WorkflowGenerationCrossReviewService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict[str, Any]:
|
||||
resolved = service or _create_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return _serialize_round_detail(
|
||||
resolved.regenerate(round_id, payload or {}, ctx),
|
||||
signer,
|
||||
)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def approve_generation_cross_review(
|
||||
ctx: RequestContext,
|
||||
round_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: WorkflowGenerationCrossReviewService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict[str, Any]:
|
||||
resolved = service or _create_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return _serialize_round_detail(
|
||||
resolved.approve(round_id, payload or {}, ctx),
|
||||
signer,
|
||||
)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def _create_service() -> WorkflowGenerationCrossReviewService:
|
||||
database = Database(load_database_settings())
|
||||
round_service = _create_round_service()
|
||||
provider_service = ModelProviderService(ModelProviderRepository(database))
|
||||
return WorkflowGenerationCrossReviewService(
|
||||
cross_review_repository=WorkflowCrossReviewRepository(database),
|
||||
round_repository=round_service.round_repository,
|
||||
workflow_repository=round_service.workflow_repository,
|
||||
round_service=round_service,
|
||||
planner=GenerationCrossReviewPlanner(provider_service),
|
||||
audit_service=AuditService(database),
|
||||
config=load_config_payload(),
|
||||
)
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
"""Job and queue route handlers."""
|
||||
"""Job and queue route handlers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
|
@ -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,24 @@ 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)
|
||||
|
||||
|
||||
def delete_job(
|
||||
ctx: RequestContext,
|
||||
task_id: str,
|
||||
service: QueueService | None = None,
|
||||
) -> dict:
|
||||
queue_service = service or _create_queue_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_task_retry(ctx)
|
||||
return queue_service.delete_job(task_id, ctx)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
|
@ -60,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)
|
||||
|
|
@ -71,15 +99,140 @@ 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)
|
||||
return _retry_task(task, ctx, payload=payload, retry_service=retry_service)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def retry_failed_jobs(
|
||||
ctx: RequestContext,
|
||||
payload: dict[str, Any] | None = None,
|
||||
) -> dict:
|
||||
params = payload or {}
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_task_retry(ctx)
|
||||
queue_service = _create_queue_service()
|
||||
limit = max(1, min(int_or_default(params.get("limit"), 100), 100))
|
||||
tasks = []
|
||||
seen_task_ids: set[str] = set()
|
||||
for status in ("failed", "dead_letter", "cancelled"):
|
||||
remaining = limit - len(tasks)
|
||||
if remaining <= 0:
|
||||
break
|
||||
for task in queue_service.list_jobs(status=status, page=1, page_size=remaining).items:
|
||||
if task.task_id not in seen_task_ids:
|
||||
seen_task_ids.add(task.task_id)
|
||||
tasks.append(task)
|
||||
|
||||
tasks = _latest_retryable_tasks(tasks)
|
||||
retried: list[dict[str, Any]] = []
|
||||
errors: list[dict[str, Any]] = []
|
||||
retry_service: GenerationJobRetryService | None = None
|
||||
for task in tasks:
|
||||
try:
|
||||
if task.metadata.get("source") != "advanced_material":
|
||||
retry_service = retry_service or _create_generation_job_retry_service()
|
||||
retried.append(_retry_task(task, ctx, retry_service=retry_service))
|
||||
except Exception as exc:
|
||||
errors.append(_retry_error_payload(task.task_id, exc))
|
||||
|
||||
return {
|
||||
"retried": retried,
|
||||
"errors": errors,
|
||||
"total": len(retried),
|
||||
"processed": len(retried) + len(errors),
|
||||
"limit": limit,
|
||||
"remaining_by_status": {
|
||||
status: queue_service.repository.count_by_status().get(status, 0)
|
||||
for status in ("failed", "dead_letter", "cancelled")
|
||||
},
|
||||
}
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def _latest_retryable_tasks(tasks: list[Any]) -> list[Any]:
|
||||
selected: list[Any] = []
|
||||
lineage_indexes: dict[tuple[str, str], int] = {}
|
||||
for task in tasks:
|
||||
lineage_key = _retry_lineage_key(task)
|
||||
if lineage_key is None:
|
||||
selected.append(task)
|
||||
continue
|
||||
previous_index = lineage_indexes.get(lineage_key)
|
||||
if previous_index is None:
|
||||
lineage_indexes[lineage_key] = len(selected)
|
||||
selected.append(task)
|
||||
continue
|
||||
previous_task = selected[previous_index]
|
||||
if _task_sort_key(task) > _task_sort_key(previous_task):
|
||||
selected[previous_index] = task
|
||||
return selected
|
||||
|
||||
|
||||
def _retry_lineage_key(task: Any) -> tuple[str, str] | None:
|
||||
metadata = task.metadata if isinstance(getattr(task, "metadata", None), dict) else {}
|
||||
source_id = str(metadata.get("advanced_style_source_id") or "").strip()
|
||||
case_id = str(metadata.get("advanced_style_validation_case_id") or "").strip()
|
||||
if task.task_type == "advanced_style_source_recognition" and source_id:
|
||||
return ("source", source_id)
|
||||
if task.task_type == "advanced_style_validation_generation" and case_id:
|
||||
return ("validation", case_id)
|
||||
return None
|
||||
|
||||
|
||||
def _task_sort_key(task: Any) -> str:
|
||||
return str(getattr(task, "updated_at", "") or getattr(task, "created_at", "") or "")
|
||||
|
||||
|
||||
def _retry_task(
|
||||
task: Any,
|
||||
ctx: RequestContext,
|
||||
*,
|
||||
payload: dict[str, Any] | None = None,
|
||||
retry_service: GenerationJobRetryService | None = None,
|
||||
) -> dict[str, Any]:
|
||||
if task.metadata.get("source") == "advanced_material":
|
||||
require_permission(ctx, "can_manage_materials")
|
||||
from backend.services.advanced_material_service import create_advanced_material_service
|
||||
|
||||
advanced_service = create_advanced_material_service()
|
||||
import_id = str(task.metadata.get("advanced_material_import_id") or "").strip()
|
||||
if task.task_type == "advanced_style_source_recognition":
|
||||
source_id = str(task.metadata.get("advanced_style_source_id") or "").strip()
|
||||
result = advanced_service.style_sets.retry_source(import_id, source_id, ctx)
|
||||
elif task.task_type == "advanced_style_validation_generation":
|
||||
case_id = str(task.metadata.get("advanced_style_validation_case_id") or "").strip()
|
||||
result = advanced_service.style_sets.retry_validation_case(import_id, case_id, ctx)
|
||||
else:
|
||||
result = advanced_service.retry_import(import_id, ctx)
|
||||
if isinstance(result, dict) and isinstance(result.get("task"), dict):
|
||||
return result["task"]
|
||||
return result
|
||||
resolved_retry_service = retry_service or _create_generation_job_retry_service()
|
||||
return resolved_retry_service.retry_job(task.task_id, ctx, overrides=payload or {}).to_dict()
|
||||
|
||||
|
||||
def _retry_error_payload(task_id: str, exc: Exception) -> dict[str, Any]:
|
||||
error_code = getattr(getattr(exc, "error_code", None), "value", None) or str(getattr(exc, "error_code", "retry_failed"))
|
||||
message = str(getattr(exc, "message", None) or str(exc))
|
||||
raw_details = getattr(exc, "details", None)
|
||||
details = dict(raw_details) if isinstance(raw_details, dict) else {}
|
||||
return {
|
||||
"task_id": task_id,
|
||||
"error_code": error_code,
|
||||
"message": message,
|
||||
**({"replacement_task_id": details["replacement_task_id"]} if details.get("replacement_task_id") else {}),
|
||||
}
|
||||
|
||||
|
||||
def retry_dead_letter_jobs(
|
||||
ctx: RequestContext,
|
||||
payload: dict[str, Any] | None = None,
|
||||
|
|
@ -110,6 +263,40 @@ def move_failed_jobs_to_dead_letter(
|
|||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def clear_failed_jobs(
|
||||
ctx: RequestContext,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: QueueService | None = None,
|
||||
) -> dict:
|
||||
queue_service = service or _create_queue_service()
|
||||
params = payload or {}
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_task_retry(ctx)
|
||||
return queue_service.clear_failed_jobs(ctx, limit=int_or_default(params.get("limit"), 100))
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def clear_terminal_jobs(
|
||||
ctx: RequestContext,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: QueueService | None = None,
|
||||
) -> dict:
|
||||
queue_service = service or _create_queue_service()
|
||||
params = payload or {}
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_task_retry(ctx)
|
||||
return queue_service.clear_terminal_jobs(
|
||||
ctx,
|
||||
statuses=params.get("statuses"),
|
||||
limit=int_or_default(params.get("limit"), 100),
|
||||
)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def _create_queue_service() -> QueueService:
|
||||
database = Database(load_database_settings())
|
||||
return QueueService(
|
||||
|
|
@ -128,3 +315,5 @@ def _create_generation_job_retry_service() -> GenerationJobRetryService:
|
|||
queue_service=generation_service.queue_service,
|
||||
generation_service=generation_service,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -48,6 +48,32 @@ def fetch_media(
|
|||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def fetch_media_payload(
|
||||
ctx: RequestContext,
|
||||
query: dict[str, Any],
|
||||
service: MediaService | None = None,
|
||||
) -> dict:
|
||||
"""Return signed media as JSON for authenticated plugin-page bridges.
|
||||
|
||||
AstrBot plugin pages run in a sandboxed iframe. Their API bridge carries
|
||||
the dashboard login state, while a plain ``<img>`` request to ``/api/plug``
|
||||
does not. Keeping this separate from ``fetch_media`` lets browser clients
|
||||
create a local Blob URL without making the signed streaming endpoint less
|
||||
strict at the AstrBot middleware layer.
|
||||
"""
|
||||
|
||||
media_service = service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
token = str(query.get("token") or "").strip()
|
||||
if not token:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "token is required", {"field": "token"})
|
||||
return media_service.fetch_media(token)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def index_media(
|
||||
ctx: RequestContext,
|
||||
query: dict[str, Any] | None = None,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,36 @@
|
|||
"""Runtime model catalog exposed by the AstrBot host."""
|
||||
|
||||
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, service: ModelProviderService | None = None) -> dict:
|
||||
model_service = service or ModelProviderService(ModelProviderRepository(Database(load_database_settings())))
|
||||
|
||||
def action() -> dict:
|
||||
require_authenticated(ctx)
|
||||
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)
|
||||
|
|
@ -0,0 +1,87 @@
|
|||
"""Material-binding routes for detailed-workflow image nodes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
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
|
||||
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
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
from database.repositories.workflow_round_repository import WorkflowRoundRepository
|
||||
|
||||
|
||||
def get_node_material_bindings(
|
||||
ctx: RequestContext,
|
||||
node_id: str,
|
||||
query: dict[str, Any] | None = None,
|
||||
service: WorkflowNodeMaterialService | None = None,
|
||||
) -> dict:
|
||||
material_service = service or _create_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
params = query or {}
|
||||
navigation = {
|
||||
key: params.get(key)
|
||||
for key in ("workflow_id", "branch_name", "node_id", "round_id", "return_view")
|
||||
if key in params
|
||||
}
|
||||
return material_service.get_node_bindings(
|
||||
node_id,
|
||||
ctx,
|
||||
navigation_context=navigation or None,
|
||||
)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def save_node_material_bindings(
|
||||
ctx: RequestContext,
|
||||
node_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: WorkflowNodeMaterialService | None = None,
|
||||
) -> dict:
|
||||
material_service = service or _create_service()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return material_service.save_node_bindings(node_id, payload or {}, ctx)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def _create_service() -> WorkflowNodeMaterialService:
|
||||
database = Database(load_database_settings())
|
||||
profiles = ProfileRepository(database)
|
||||
workflows = WorkflowRepository(database)
|
||||
workflow_service = WorkflowService(
|
||||
workflow_repository=workflows,
|
||||
profile_repository=profiles,
|
||||
audit_service=AuditService(database),
|
||||
)
|
||||
return WorkflowNodeMaterialService(
|
||||
workflow_service,
|
||||
MaterialCatalogResolver(
|
||||
MaterialRepository(database),
|
||||
AdvancedMaterialRepository(database),
|
||||
),
|
||||
round_repository=WorkflowRoundRepository(database),
|
||||
access_service=ResourceAccessService(
|
||||
profiles,
|
||||
workflows,
|
||||
ImageRepository(database),
|
||||
JobRepository(database),
|
||||
),
|
||||
)
|
||||
|
|
@ -13,6 +13,8 @@ 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
|
||||
from backend.services.node_dependency_context_service import NodeDependencyContextService
|
||||
|
|
@ -31,11 +33,14 @@ from backend.services.workflow_service import (
|
|||
is_repeatable_single_image_node,
|
||||
locked_node_edit_error,
|
||||
)
|
||||
from backend.services.workflow_material_snapshot_service import WorkflowMaterialSnapshotService
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
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
|
||||
|
|
@ -179,6 +184,7 @@ def retry_node(
|
|||
def action() -> dict[str, Any]:
|
||||
require_task_retry(ctx)
|
||||
node = workflow_service.get_node(node_id)
|
||||
workflow_service.ensure_node_action_allowed(node_id, "retry", node=node)
|
||||
if node.status in LOCKED_NODE_STATUSES and not is_repeatable_single_image_node(node):
|
||||
raise locked_node_edit_error(node)
|
||||
if node.status not in _RETRYABLE_NODE_STATUSES and not (
|
||||
|
|
@ -253,6 +259,21 @@ def mark_node_completed(
|
|||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
node = workflow_service.get_node(node_id)
|
||||
if (
|
||||
node.node_type in {"generation", "single_image"}
|
||||
and canonical_gradual_node_key(node.node_key) != "profile_card"
|
||||
):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"generation nodes must be completed by confirming an image candidate",
|
||||
{
|
||||
"reason": "generation_node_requires_candidate_adoption",
|
||||
"node_id": node.node_id,
|
||||
"node_key": node.node_key,
|
||||
"required_action": "adopt_image",
|
||||
},
|
||||
)
|
||||
return workflow_service.update_node(node_id, {"status": "completed"}, ctx).to_dict()
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
|
@ -295,6 +316,11 @@ def _create_generation_service() -> GenerationService:
|
|||
database = Database(load_database_settings())
|
||||
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)
|
||||
|
|
@ -309,8 +335,8 @@ def _create_generation_service() -> GenerationService:
|
|||
image_moderation_config = governance_config.get("image_moderation") if isinstance(governance_config.get("image_moderation"), dict) else {}
|
||||
translation_route = str(translation_config.get("model_route") or model_assignments.get("prompt_planning") or "").strip()
|
||||
moderation_route = str(moderation_config.get("model_route") or model_assignments.get("cross_review_primary") or "").strip()
|
||||
translation_callable = model_provider_runtime.structured_text_callable(translation_route) if translation_route else None
|
||||
moderation_callable = model_provider_runtime.structured_text_callable(moderation_route) if moderation_route else None
|
||||
translation_callable = (model_provider_runtime.structured_text_callable(translation_route) or astrbot_structured_text_callable(translation_route)) if translation_route else None
|
||||
moderation_callable = (model_provider_runtime.structured_text_callable(moderation_route) or astrbot_structured_text_callable(moderation_route)) if moderation_route else None
|
||||
single_flow_engine = SingleFlowEngine(
|
||||
image_provider_factory=model_provider_runtime.image_provider_for_route,
|
||||
allow_mock_fallback=False,
|
||||
|
|
@ -350,7 +376,11 @@ 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=MaterialRepository(database),
|
||||
material_resolver=material_catalog,
|
||||
material_snapshot_service=WorkflowMaterialSnapshotService(
|
||||
workflow_repository,
|
||||
material_catalog,
|
||||
),
|
||||
model_assignments=model_assignments,
|
||||
prompt_translation_service=PromptTranslationService(translation_config, translation_callable),
|
||||
prompt_semantic_normalizer=PromptSemanticNormalizer(),
|
||||
|
|
@ -360,6 +390,12 @@ def _create_generation_service() -> GenerationService:
|
|||
)
|
||||
|
||||
|
||||
def create_generation_service() -> GenerationService:
|
||||
"""Public runtime factory shared by HTTP routes and background workers."""
|
||||
|
||||
return _create_generation_service()
|
||||
|
||||
|
||||
def _bool(value: Any) -> bool:
|
||||
if isinstance(value, str):
|
||||
return value.strip().lower() in {"1", "true", "yes", "on"}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,96 @@
|
|||
"""Profile-card read and atomic-finalize routes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError
|
||||
from backend.routes.common import handle_request
|
||||
from backend.services.media_service import MediaService
|
||||
from backend.services.profile_card_service import ProfileCardService
|
||||
from core.audit_service import AuditService
|
||||
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.profile_card_repository import ProfileCardRepository
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
|
||||
|
||||
def finalize_profile_card(
|
||||
ctx: RequestContext,
|
||||
workflow_id: str,
|
||||
payload: dict[str, Any],
|
||||
service: ProfileCardService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or _create_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return _serialize_card(resolved.finalize(workflow_id, payload, ctx).to_dict(), signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def get_profile_card(
|
||||
ctx: RequestContext,
|
||||
card_id: str,
|
||||
service: ProfileCardService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or _create_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
return _serialize_card(resolved.get(card_id, ctx).to_dict(), signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def list_profile_cards(
|
||||
ctx: RequestContext,
|
||||
profile_id: str,
|
||||
service: ProfileCardService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
resolved = service or _create_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> list[dict[str, Any]]:
|
||||
require_authenticated(ctx)
|
||||
return [_serialize_card(card.to_dict(), signer) for card in resolved.list_for_profile(profile_id, ctx)]
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def _create_service() -> ProfileCardService:
|
||||
database = Database(load_database_settings())
|
||||
return ProfileCardService(
|
||||
cards=ProfileCardRepository(database),
|
||||
profiles=ProfileRepository(database),
|
||||
workflows=WorkflowRepository(database),
|
||||
images=ImageRepository(database),
|
||||
audit_service=AuditService(database),
|
||||
)
|
||||
|
||||
|
||||
def _serialize_card(payload: dict[str, Any], media_service: MediaService) -> dict[str, Any]:
|
||||
assets = []
|
||||
for item in payload.get("assets", []):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
assets.append({**item, "image_url": _signed_url(media_service, item.get("image_uri"))})
|
||||
return {**payload, "assets": assets}
|
||||
|
||||
|
||||
def _signed_url(media_service: MediaService, uri: Any) -> str | None:
|
||||
if not isinstance(uri, str) or not uri:
|
||||
return None
|
||||
try:
|
||||
return media_service.sign_media(uri)["url"]
|
||||
except ApiError:
|
||||
return None
|
||||
|
|
@ -13,6 +13,7 @@ from core.identity import RequestContext
|
|||
from core.security import require_authenticated
|
||||
from database.db_helper import Database, load_config_payload, load_database_settings
|
||||
from database.repositories.model_provider_repository import ModelProviderRepository
|
||||
from core.astrbot_provider_bridge import astrbot_structured_text_callable
|
||||
|
||||
|
||||
def expand_prompt(
|
||||
|
|
@ -45,8 +46,8 @@ def preview_prompt_governance(ctx: RequestContext, payload: dict[str, Any] | Non
|
|||
runtime = ModelProviderRuntime(model_service)
|
||||
translation_route = str(translation_config.get("model_route") or assignments.get("prompt_planning") or "").strip()
|
||||
moderation_route = str(moderation_config.get("model_route") or assignments.get("cross_review_primary") or "").strip()
|
||||
translator = runtime.structured_text_callable(translation_route) if translation_route else None
|
||||
reviewer = runtime.structured_text_callable(moderation_route) if moderation_route else None
|
||||
translator = (runtime.structured_text_callable(translation_route) or astrbot_structured_text_callable(translation_route)) if translation_route else None
|
||||
reviewer = (runtime.structured_text_callable(moderation_route) or astrbot_structured_text_callable(moderation_route)) if moderation_route else None
|
||||
original = str(request.get("prompt") or "").strip()
|
||||
translation = PromptTranslationService(translation_config, translator).translate(
|
||||
original,
|
||||
|
|
|
|||
198
backend/routes/round_routes.py
普通文件
198
backend/routes/round_routes.py
普通文件
|
|
@ -0,0 +1,198 @@
|
|||
"""Workflow generation-round route handlers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError
|
||||
from backend.routes.common import handle_request
|
||||
from backend.routes.params import as_bool
|
||||
from backend.routes.node_routes import create_generation_service
|
||||
from backend.services.media_service import MediaService
|
||||
from backend.services.model_provider_runtime import ModelProviderRuntime
|
||||
from backend.services.workflow_round_feedback_service import WorkflowRoundFeedbackService
|
||||
from backend.services.workflow_round_service import WorkflowRoundService
|
||||
from core.astrbot_provider_bridge import astrbot_structured_text_callable
|
||||
from core.identity import RequestContext
|
||||
from core.security import require_authenticated, require_write
|
||||
from database.repositories.workflow_round_repository import WorkflowRoundRepository
|
||||
|
||||
|
||||
def list_node_rounds(
|
||||
ctx: RequestContext,
|
||||
node_id: str,
|
||||
query: dict[str, Any] | None = None,
|
||||
service: WorkflowRoundService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
round_service = service or _create_round_service()
|
||||
signer = media_service or MediaService()
|
||||
params = query or {}
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
payload = round_service.list_node_rounds(
|
||||
node_id,
|
||||
include_legacy=as_bool(params.get("include_legacy"), default=True, non_string_truthiness=True),
|
||||
ctx=ctx,
|
||||
)
|
||||
return _serialize_round_collection(payload, signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def create_node_round(
|
||||
ctx: RequestContext,
|
||||
node_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: WorkflowRoundService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
round_service = service or _create_round_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return _serialize_round_detail(round_service.create_round(node_id, payload or {}, ctx), signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def get_round(
|
||||
ctx: RequestContext,
|
||||
round_id: str,
|
||||
service: WorkflowRoundService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
round_service = service or _create_round_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_authenticated(ctx)
|
||||
return _serialize_round_detail(round_service.get_round(round_id, ctx).to_dict(), signer)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def update_round_feedback(
|
||||
ctx: RequestContext,
|
||||
round_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: WorkflowRoundService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
round_service = service or _create_round_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return _serialize_round_detail(
|
||||
round_service.update_feedback(round_id, payload or {}, ctx),
|
||||
signer,
|
||||
)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def confirm_round_candidate(
|
||||
ctx: RequestContext,
|
||||
round_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: WorkflowRoundService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
round_service = service or _create_round_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return _serialize_round_detail(
|
||||
round_service.confirm_candidate(round_id, payload or {}, ctx),
|
||||
signer,
|
||||
)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def create_next_round(
|
||||
ctx: RequestContext,
|
||||
round_id: str,
|
||||
payload: dict[str, Any] | None = None,
|
||||
service: WorkflowRoundService | None = None,
|
||||
media_service: MediaService | None = None,
|
||||
) -> dict:
|
||||
round_service = service or _create_round_service()
|
||||
signer = media_service or MediaService()
|
||||
|
||||
def action() -> dict[str, Any]:
|
||||
require_write(ctx)
|
||||
return _serialize_round_detail(
|
||||
round_service.create_next_round(round_id, payload or {}, ctx),
|
||||
signer,
|
||||
)
|
||||
|
||||
return handle_request(ctx, action)
|
||||
|
||||
|
||||
def _create_round_service() -> WorkflowRoundService:
|
||||
generation_service = create_generation_service()
|
||||
database = generation_service.queue_service.repository.database
|
||||
feedback_route = str(
|
||||
generation_service.model_assignments.get("prompt_planning") or ""
|
||||
).strip()
|
||||
feedback_callable = None
|
||||
if feedback_route:
|
||||
provider_service = generation_service.model_provider_service
|
||||
if provider_service is not None:
|
||||
feedback_callable = ModelProviderRuntime(provider_service).structured_text_callable(
|
||||
feedback_route
|
||||
)
|
||||
feedback_callable = feedback_callable or astrbot_structured_text_callable(feedback_route)
|
||||
return WorkflowRoundService(
|
||||
round_repository=WorkflowRoundRepository(database),
|
||||
workflow_repository=generation_service.workflow_service.workflow_repository,
|
||||
image_repository=generation_service.image_service.image_repository,
|
||||
job_repository=generation_service.queue_service.repository,
|
||||
audit_service=generation_service.workflow_service.audit_service,
|
||||
generation_service=generation_service,
|
||||
feedback_service=WorkflowRoundFeedbackService(
|
||||
structured_text_callable=feedback_callable,
|
||||
model_route=feedback_route,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _serialize_round_collection(payload: dict[str, Any], media_service: MediaService) -> dict[str, Any]:
|
||||
return {
|
||||
**payload,
|
||||
"items": [
|
||||
_serialize_round_detail(item, media_service)
|
||||
for item in payload.get("items", [])
|
||||
if isinstance(item, dict)
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _serialize_round_detail(payload: dict[str, Any], media_service: MediaService) -> dict[str, Any]:
|
||||
candidates: list[dict[str, Any]] = []
|
||||
for item in payload.get("candidates", []):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
image = item.get("image") if isinstance(item.get("image"), dict) else None
|
||||
if image is not None:
|
||||
image = {
|
||||
**image,
|
||||
"image_url": _signed_url(media_service, image.get("image_uri")),
|
||||
"thumbnail_url": _signed_url(media_service, image.get("thumbnail_uri")),
|
||||
}
|
||||
candidates.append({**item, "image": image})
|
||||
return {**payload, "candidates": candidates}
|
||||
|
||||
|
||||
def _signed_url(media_service: MediaService, uri: Any) -> str | None:
|
||||
if not isinstance(uri, str) or not uri:
|
||||
return None
|
||||
try:
|
||||
return media_service.sign_media(uri)["url"]
|
||||
except ApiError:
|
||||
return None
|
||||
|
|
@ -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,167 @@
|
|||
"""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")
|
||||
recognition_concurrency = _recognition_concurrency(getattr(service, "config", {}))
|
||||
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,
|
||||
# Keep worker threads available for queue progress, but limit
|
||||
# simultaneous calls to the same vision route. Vision providers
|
||||
# and relay services commonly rate-limit image requests more
|
||||
# aggressively than ordinary text requests.
|
||||
per_route_limit=recognition_concurrency,
|
||||
)
|
||||
|
||||
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",
|
||||
}
|
||||
|
||||
|
||||
|
||||
def _recognition_concurrency(config: dict[str, Any] | None) -> int:
|
||||
advanced_materials = config.get("advanced_materials") if isinstance(config, dict) else None
|
||||
if not isinstance(advanced_materials, dict):
|
||||
return 2
|
||||
try:
|
||||
return min(max(int(advanced_materials.get("recognition_concurrency", 2)), 1), 8)
|
||||
except (TypeError, ValueError):
|
||||
return 2
|
||||
文件差异内容过多而无法显示
加载差异
|
|
@ -6,12 +6,14 @@ import json
|
|||
import os
|
||||
import re
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from core.model_router import SUPPORTED, UNSUPPORTED, route_from_capability_profile, select_model_route
|
||||
from database.db_helper import load_config_payload
|
||||
from database.repositories.capability_repository import CapabilityRepository
|
||||
|
||||
|
||||
|
|
@ -42,6 +44,8 @@ class ApiDiscoveryService:
|
|||
return DIALECTS
|
||||
|
||||
def probe_provider(self, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
if payload.get("use_saved_provider"):
|
||||
payload = _saved_provider_probe_payload(payload)
|
||||
provider_id = str(payload.get("provider_id") or "").strip()
|
||||
model_id = str(payload.get("model_id") or "").strip()
|
||||
if not provider_id:
|
||||
|
|
@ -177,18 +181,58 @@ def new_capability_profile_id() -> str:
|
|||
return f"cap_{uuid4().hex}"
|
||||
|
||||
|
||||
def _saved_provider_probe_payload(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
provider_id = str(payload.get("provider_id") or "").strip()
|
||||
config = load_config_payload()
|
||||
generation = config.get("generation") if isinstance(config.get("generation"), dict) else {}
|
||||
adapters = generation.get("adapters") if isinstance(generation, dict) else []
|
||||
saved: dict[str, Any] | None = None
|
||||
if isinstance(adapters, list):
|
||||
saved = next(
|
||||
(
|
||||
dict(item)
|
||||
for item in adapters
|
||||
if isinstance(item, dict)
|
||||
and str(item.get("id") or item.get("provider_id") or "").strip() == provider_id
|
||||
),
|
||||
None,
|
||||
)
|
||||
if saved is None and isinstance(generation, dict):
|
||||
provider = generation.get("provider")
|
||||
if isinstance(provider, dict):
|
||||
candidate_id = str(provider.get("id") or provider.get("provider_id") or "").strip()
|
||||
if not provider_id or candidate_id == provider_id:
|
||||
saved = dict(provider)
|
||||
if saved is None:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"saved generation adapter was not found",
|
||||
{"provider_id": provider_id},
|
||||
)
|
||||
return {
|
||||
**saved,
|
||||
"provider_id": provider_id or str(saved.get("id") or saved.get("provider_id") or "").strip(),
|
||||
"model_id": str(saved.get("model_id") or payload.get("model_id") or "auto-discovery"),
|
||||
"dialect": payload.get("dialect") or "openai_images",
|
||||
"live_probe": True,
|
||||
"allow_lightweight_test": bool(payload.get("allow_lightweight_test")),
|
||||
"use_saved_provider": True,
|
||||
}
|
||||
|
||||
|
||||
def _live_probe(payload: dict[str, Any]) -> dict[str, Any]:
|
||||
base_url = str(payload.get("base_url") or "").strip().rstrip("/")
|
||||
if not base_url.startswith(("http://", "https://")):
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "valid base_url is required", {"field": "base_url"})
|
||||
timeout = min(max(int(payload.get("timeout_seconds") or 20), 3), 60)
|
||||
env_name = str(payload.get("api_key_env_var") or "").strip()
|
||||
literal_api_key = str(payload.get("api_key") or "").strip()
|
||||
literal_api_key = str(payload.get("api_key_plaintext") or payload.get("api_key") or "").strip()
|
||||
api_key = literal_api_key or (os.getenv(env_name, "") if env_name else "")
|
||||
custom_headers = payload.get("custom_headers")
|
||||
custom_headers = custom_headers if isinstance(custom_headers, dict) else {}
|
||||
headers = {
|
||||
"Accept": "application/json",
|
||||
"User-Agent": "AstrBot-ImageStudio/1.0.10",
|
||||
**{str(key): str(value) for key, value in custom_headers.items()},
|
||||
**({"Authorization": f"Bearer {api_key}"} if api_key else {}),
|
||||
}
|
||||
|
|
@ -205,27 +249,37 @@ def _live_probe(payload: dict[str, Any]) -> dict[str, Any]:
|
|||
result["resolved_api_base"] = api_prefix
|
||||
result["image_endpoint"] = "/images/generations" if base_url.lower().endswith("/v1") else "/v1/images/generations"
|
||||
try:
|
||||
request = urllib.request.Request(models_url, headers=headers, method="GET")
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
body = response.read(2 * 1024 * 1024).decode("utf-8-sig")
|
||||
status, raw_body, response_headers = _http_request(
|
||||
"GET",
|
||||
models_url,
|
||||
headers=headers,
|
||||
timeout=timeout,
|
||||
max_bytes=2 * 1024 * 1024,
|
||||
)
|
||||
body = raw_body.decode("utf-8-sig", errors="replace")
|
||||
result["http_status"] = status
|
||||
if 200 <= status < 300:
|
||||
parsed = json.loads(body)
|
||||
items = parsed.get("data", []) if isinstance(parsed, dict) else []
|
||||
result["models"] = [str(item.get("id")) for item in items if isinstance(item, dict) and item.get("id")][:200]
|
||||
result["connected"] = True
|
||||
result["http_status"] = response.status
|
||||
except urllib.error.HTTPError as exc:
|
||||
try:
|
||||
error_body = exc.read(4096).decode("utf-8-sig", errors="replace")
|
||||
except Exception:
|
||||
error_body = ""
|
||||
result.update(
|
||||
else:
|
||||
result["error"] = f"models endpoint returned HTTP {status}"
|
||||
result["error_body"] = _redact_probe_error(body[:500])
|
||||
content_type = response_headers.get("content-type")
|
||||
if content_type:
|
||||
result["content_type"] = content_type
|
||||
location = response_headers.get("location")
|
||||
if location:
|
||||
redirect_target = urllib.parse.urljoin(models_url, location)
|
||||
result["redirect_target"] = redirect_target
|
||||
result["warnings"].append(
|
||||
{
|
||||
"http_status": exc.code,
|
||||
"error": f"models endpoint returned HTTP {exc.code}",
|
||||
"error_body": _redact_probe_error(error_body[:500]),
|
||||
"message": "models endpoint redirected; authorization was not forwarded across origins",
|
||||
"target": redirect_target,
|
||||
}
|
||||
)
|
||||
except (urllib.error.URLError, TimeoutError, json.JSONDecodeError, UnicodeDecodeError) as exc:
|
||||
except (urllib.error.URLError, TimeoutError, json.JSONDecodeError, UnicodeDecodeError, OSError) as exc:
|
||||
result["error"] = str(exc)
|
||||
|
||||
if payload.get("allow_lightweight_test") and result["connected"]:
|
||||
|
|
@ -236,16 +290,62 @@ def _live_probe(payload: dict[str, Any]) -> dict[str, Any]:
|
|||
model_id = str(payload.get("model_id") or "").strip()
|
||||
for size in ("1920x1080", "2560x1440", "3840x2160"):
|
||||
data = json.dumps({"model": model_id, "prompt": "A plain gray circle on white background, capability test", "size": size, "n": 1, "response_format": "b64_json"}).encode("utf-8")
|
||||
request = urllib.request.Request(endpoint_url, data=data, headers={**headers, "Content-Type": "application/json"}, method="POST")
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
response.read(1024)
|
||||
status, _, _ = _http_request(
|
||||
"POST",
|
||||
endpoint_url,
|
||||
headers={**headers, "Content-Type": "application/json"},
|
||||
timeout=timeout,
|
||||
data=data,
|
||||
max_bytes=1024,
|
||||
)
|
||||
if not 200 <= status < 300:
|
||||
raise RuntimeError(f"HTTP {status}")
|
||||
else:
|
||||
result["supported_sizes"].append(size)
|
||||
except Exception as exc: # Provider dialects expose inconsistent errors; retain a short diagnostic only.
|
||||
result["warnings"].append({"size": size, "message": str(exc)[:180]})
|
||||
return result
|
||||
|
||||
|
||||
def _http_request(
|
||||
method: str,
|
||||
url: str,
|
||||
*,
|
||||
headers: dict[str, str],
|
||||
timeout: int,
|
||||
data: bytes | None = None,
|
||||
max_bytes: int,
|
||||
) -> tuple[int, bytes, dict[str, str]]:
|
||||
try:
|
||||
import httpx
|
||||
except ImportError:
|
||||
request = urllib.request.Request(url, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(request, timeout=timeout) as response:
|
||||
return response.status, response.read(max_bytes), {key.lower(): value for key, value in response.headers.items()}
|
||||
except urllib.error.HTTPError as exc:
|
||||
return exc.code, exc.read(max_bytes), {key.lower(): value for key, value in exc.headers.items()}
|
||||
|
||||
with httpx.stream(
|
||||
method,
|
||||
url,
|
||||
headers=headers,
|
||||
content=data,
|
||||
timeout=timeout,
|
||||
follow_redirects=False,
|
||||
) as response:
|
||||
chunks: list[bytes] = []
|
||||
total = 0
|
||||
for chunk in response.iter_bytes():
|
||||
if total >= max_bytes:
|
||||
break
|
||||
remaining = max_bytes - total
|
||||
chunks.append(chunk[:remaining])
|
||||
total += min(len(chunk), remaining)
|
||||
return response.status_code, b"".join(chunks), {key.lower(): value for key, value in response.headers.items()}
|
||||
|
||||
|
||||
def _redact_probe_error(value: str) -> str:
|
||||
redacted = re.sub(r"(?i)bearer\s+[a-z0-9._-]+", "Bearer ***", value)
|
||||
return re.sub(r"(?i)sk[-_][a-z0-9._-]{8,}", "sk-***", redacted)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,356 @@
|
|||
"""Workflow branch lineage and branch-scoped metadata helpers.
|
||||
|
||||
The existing workflow model keeps a physical node copy per branch. This
|
||||
module adds a read-time lineage layer without replacing that storage model:
|
||||
locked copied ancestors resolve back to their immutable source node, while
|
||||
editable/regenerated branch nodes remain owned by their branch.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
from dataclasses import dataclass
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.dto.workflow_dto import WorkflowBranch, WorkflowNode
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
|
||||
|
||||
BRANCH_STATE_FIELDS = frozenset(
|
||||
{
|
||||
"character_execution",
|
||||
"material_binding_schema_version",
|
||||
"material_binding_source_node_id",
|
||||
"material_binding_updated_at",
|
||||
"material_bindings",
|
||||
"prompt_plan_schema_version",
|
||||
"prompt_plan_source_node_id",
|
||||
"prompt_plan_updated_at",
|
||||
"prompt_plan",
|
||||
"prompt_matrix",
|
||||
"stage_input_schema_version",
|
||||
"stage_inputs",
|
||||
"restoration_input_schema_version",
|
||||
"restoration_inputs",
|
||||
"stage_review_handoffs",
|
||||
"last_stage_review_confirmed_at",
|
||||
"last_stage_review_node_id",
|
||||
"last_stage_review_image_id",
|
||||
"current_stage_input_image_id",
|
||||
"final_archive_schema_version",
|
||||
"final_archive_history_id",
|
||||
"final_archive_image_ids",
|
||||
"final_archive_node_id",
|
||||
"final_archive_source_node_id",
|
||||
"final_archive_completed_at",
|
||||
}
|
||||
)
|
||||
|
||||
EFFECTIVE_CONTEXT_STATUSES = frozenset({"completed", "selected", "skipped"})
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ResolvedNodeSource:
|
||||
"""A logical branch node and the effective immutable source it reads."""
|
||||
|
||||
logical_node: WorkflowNode
|
||||
source_node: WorkflowNode
|
||||
inherited: bool
|
||||
source_branch: str
|
||||
source_node_id: str
|
||||
lineage: tuple[str, ...]
|
||||
|
||||
|
||||
class BranchLineageResolver:
|
||||
"""Resolve effective node sources through ``base_branch_name`` ancestry."""
|
||||
|
||||
def __init__(self, workflow_repository: WorkflowRepository):
|
||||
self.workflow_repository = workflow_repository
|
||||
self._workflow_id: str | None = None
|
||||
self._branches: dict[str, WorkflowBranch] = {}
|
||||
self._nodes_by_branch: dict[str, list[WorkflowNode]] = {}
|
||||
self._nodes_by_id: dict[str, WorkflowNode] = {}
|
||||
|
||||
def branch_chain(self, workflow_id: str, branch_name: str) -> tuple[str, ...]:
|
||||
self._load(workflow_id)
|
||||
current = str(branch_name or "main")
|
||||
chain: list[str] = []
|
||||
seen: set[str] = set()
|
||||
while True:
|
||||
if current in seen:
|
||||
raise _lineage_error(
|
||||
"workflow branch parent cycle detected",
|
||||
workflow_id,
|
||||
branch_name,
|
||||
reason="workflow_branch_parent_cycle",
|
||||
lineage=chain + [current],
|
||||
)
|
||||
seen.add(current)
|
||||
chain.append(current)
|
||||
if current == "main":
|
||||
return tuple(chain)
|
||||
branch = self._branches.get(current)
|
||||
if branch is None:
|
||||
raise _lineage_error(
|
||||
"workflow branch parent is missing",
|
||||
workflow_id,
|
||||
branch_name,
|
||||
reason="workflow_branch_parent_missing",
|
||||
missing_branch=current,
|
||||
lineage=chain,
|
||||
)
|
||||
current = str(branch.base_branch_name or "main")
|
||||
|
||||
def resolve_dependencies(
|
||||
self,
|
||||
target: WorkflowNode,
|
||||
*,
|
||||
include_draft: bool = False,
|
||||
) -> list[ResolvedNodeSource]:
|
||||
"""Resolve ordered logical dependencies for ``target``.
|
||||
|
||||
Branch-local completed nodes win. A reset/missing node falls back to
|
||||
the nearest parent branch. Locked copied ancestors are unwrapped to
|
||||
the original immutable node so callers can explain the true source.
|
||||
"""
|
||||
|
||||
self._load(target.workflow_id)
|
||||
local_nodes = self._nodes_by_branch.get(target.branch_name, [])
|
||||
dependencies: list[ResolvedNodeSource] = []
|
||||
for logical in local_nodes:
|
||||
if logical.sort_order >= target.sort_order:
|
||||
continue
|
||||
resolved = self._resolve_stage(
|
||||
target.workflow_id,
|
||||
target.branch_name,
|
||||
logical,
|
||||
include_draft=include_draft,
|
||||
seen=set(),
|
||||
)
|
||||
if resolved is not None:
|
||||
dependencies.append(resolved)
|
||||
return dependencies
|
||||
|
||||
def resolve_parent_seed(self, target: WorkflowNode) -> ResolvedNodeSource | None:
|
||||
"""Return the nearest confirmed ancestor value for an editable node."""
|
||||
|
||||
self._load(target.workflow_id)
|
||||
if target.branch_name == "main":
|
||||
return None
|
||||
branch = self._branches.get(target.branch_name)
|
||||
if branch is None:
|
||||
return None
|
||||
parent_name = str(branch.base_branch_name or "main")
|
||||
parent_logical = self._matching_node(parent_name, target)
|
||||
if parent_logical is None:
|
||||
return None
|
||||
return self._resolve_stage(
|
||||
target.workflow_id,
|
||||
parent_name,
|
||||
parent_logical,
|
||||
include_draft=False,
|
||||
seen=set(),
|
||||
logical_node=target,
|
||||
)
|
||||
|
||||
def _resolve_stage(
|
||||
self,
|
||||
workflow_id: str,
|
||||
branch_name: str,
|
||||
logical: WorkflowNode,
|
||||
*,
|
||||
include_draft: bool,
|
||||
seen: set[str],
|
||||
logical_node: WorkflowNode | None = None,
|
||||
) -> ResolvedNodeSource | None:
|
||||
if branch_name in seen:
|
||||
raise _lineage_error(
|
||||
"workflow branch parent cycle detected",
|
||||
workflow_id,
|
||||
branch_name,
|
||||
reason="workflow_branch_parent_cycle",
|
||||
lineage=sorted(seen | {branch_name}),
|
||||
)
|
||||
seen = {*seen, branch_name}
|
||||
local = self._matching_node(branch_name, logical)
|
||||
if local is not None and (include_draft or local.status in EFFECTIVE_CONTEXT_STATUSES):
|
||||
source, source_lineage = self._unwrap_locked_copy(local)
|
||||
display_logical = logical_node or logical
|
||||
lineage = _unique_lineage((display_logical.branch_name, *source_lineage, source.branch_name))
|
||||
return ResolvedNodeSource(
|
||||
logical_node=display_logical,
|
||||
source_node=source,
|
||||
inherited=(source.node_id != display_logical.node_id or source.branch_name != display_logical.branch_name),
|
||||
source_branch=source.branch_name,
|
||||
source_node_id=source.node_id,
|
||||
lineage=lineage,
|
||||
)
|
||||
if branch_name == "main":
|
||||
return None
|
||||
branch = self._branches.get(branch_name)
|
||||
if branch is None:
|
||||
raise _lineage_error(
|
||||
"workflow branch parent is missing",
|
||||
workflow_id,
|
||||
branch_name,
|
||||
reason="workflow_branch_parent_missing",
|
||||
missing_branch=branch_name,
|
||||
)
|
||||
parent_name = str(branch.base_branch_name or "main")
|
||||
parent_logical = self._matching_node(parent_name, logical)
|
||||
if parent_logical is None:
|
||||
return None
|
||||
return self._resolve_stage(
|
||||
workflow_id,
|
||||
parent_name,
|
||||
parent_logical,
|
||||
include_draft=include_draft,
|
||||
seen=seen,
|
||||
logical_node=logical_node or logical,
|
||||
)
|
||||
|
||||
def _unwrap_locked_copy(self, node: WorkflowNode) -> tuple[WorkflowNode, tuple[str, ...]]:
|
||||
current = node
|
||||
lineage: list[str] = [current.branch_name]
|
||||
seen: set[str] = set()
|
||||
while True:
|
||||
metadata = current.metadata if isinstance(current.metadata, dict) else {}
|
||||
copied_from = _optional_text(metadata.get("copied_from_node_id"))
|
||||
if not copied_from or metadata.get("branch_locked_ancestor") is not True:
|
||||
return current, tuple(lineage)
|
||||
if copied_from in seen:
|
||||
raise _lineage_error(
|
||||
"workflow copied-node lineage cycle detected",
|
||||
current.workflow_id,
|
||||
node.branch_name,
|
||||
reason="workflow_node_lineage_cycle",
|
||||
node_id=node.node_id,
|
||||
)
|
||||
seen.add(copied_from)
|
||||
source = self._nodes_by_id.get(copied_from) or self.workflow_repository.get_node(copied_from)
|
||||
if source is None:
|
||||
return current, tuple(lineage)
|
||||
current = source
|
||||
lineage.append(current.branch_name)
|
||||
|
||||
def _matching_node(self, branch_name: str, reference: WorkflowNode) -> WorkflowNode | None:
|
||||
candidates = self._nodes_by_branch.get(branch_name, [])
|
||||
by_key = next((node for node in candidates if node.node_key == reference.node_key), None)
|
||||
if by_key is not None:
|
||||
return by_key
|
||||
return next((node for node in candidates if node.sort_order == reference.sort_order), None)
|
||||
|
||||
def _load(self, workflow_id: str) -> None:
|
||||
if self._workflow_id == workflow_id:
|
||||
return
|
||||
self._workflow_id = workflow_id
|
||||
self._branches = {
|
||||
branch.branch_name: branch
|
||||
for branch in self.workflow_repository.list_branches(workflow_id)
|
||||
if branch.branch_name != "main"
|
||||
}
|
||||
nodes = self.workflow_repository.list_nodes(workflow_id)
|
||||
self._nodes_by_id = {node.node_id: node for node in nodes}
|
||||
self._nodes_by_branch = {}
|
||||
for node in nodes:
|
||||
self._nodes_by_branch.setdefault(node.branch_name, []).append(node)
|
||||
for branch_nodes in self._nodes_by_branch.values():
|
||||
branch_nodes.sort(key=lambda item: (item.sort_order, item.created_at, item.node_id))
|
||||
|
||||
|
||||
def branch_state_for(metadata: dict[str, Any] | None, branch_name: str) -> dict[str, Any]:
|
||||
"""Read branch state with a root-metadata fallback for pre-1.0.16 data."""
|
||||
|
||||
source = metadata if isinstance(metadata, dict) else {}
|
||||
branch_states = source.get("branch_state")
|
||||
if isinstance(branch_states, dict) and isinstance(branch_states.get(branch_name), dict):
|
||||
return deepcopy(branch_states[branch_name])
|
||||
return {
|
||||
key: deepcopy(source[key])
|
||||
for key in BRANCH_STATE_FIELDS
|
||||
if key in source
|
||||
}
|
||||
|
||||
|
||||
def workflow_metadata_for_branch(metadata: dict[str, Any] | None, branch_name: str) -> dict[str, Any]:
|
||||
"""Return the legacy root projection overlaid with one branch's state."""
|
||||
|
||||
result = deepcopy(metadata) if isinstance(metadata, dict) else {}
|
||||
state = branch_state_for(result, branch_name)
|
||||
for key in BRANCH_STATE_FIELDS:
|
||||
result.pop(key, None)
|
||||
result.update(state)
|
||||
return result
|
||||
|
||||
|
||||
def with_branch_state(
|
||||
metadata: dict[str, Any] | None,
|
||||
branch_name: str,
|
||||
patch: dict[str, Any],
|
||||
*,
|
||||
activate: bool,
|
||||
) -> dict[str, Any]:
|
||||
"""Merge one branch state while preserving every sibling branch."""
|
||||
|
||||
result = deepcopy(metadata) if isinstance(metadata, dict) else {}
|
||||
states = deepcopy(result.get("branch_state")) if isinstance(result.get("branch_state"), dict) else {}
|
||||
state = branch_state_for(result, branch_name)
|
||||
state.update({key: deepcopy(value) for key, value in patch.items() if key in BRANCH_STATE_FIELDS})
|
||||
states[branch_name] = state
|
||||
result["branch_state"] = states
|
||||
if activate:
|
||||
for key in BRANCH_STATE_FIELDS:
|
||||
result.pop(key, None)
|
||||
result.update(deepcopy(state))
|
||||
return result
|
||||
|
||||
|
||||
def activate_branch_state(metadata: dict[str, Any] | None, branch_name: str) -> dict[str, Any]:
|
||||
return with_branch_state(metadata, branch_name, {}, activate=True)
|
||||
|
||||
|
||||
def branch_state_patch(metadata: dict[str, Any] | None) -> dict[str, Any]:
|
||||
source = metadata if isinstance(metadata, dict) else {}
|
||||
return {key: deepcopy(source[key]) for key in BRANCH_STATE_FIELDS if key in source}
|
||||
|
||||
|
||||
def remap_branch_state_node_ids(value: Any, node_id_map: dict[str, str]) -> Any:
|
||||
"""Deep-copy state while remapping node-id dictionary keys and values."""
|
||||
|
||||
if isinstance(value, dict):
|
||||
return {
|
||||
node_id_map.get(str(key), str(key)): remap_branch_state_node_ids(item, node_id_map)
|
||||
for key, item in value.items()
|
||||
}
|
||||
if isinstance(value, list):
|
||||
return [remap_branch_state_node_ids(item, node_id_map) for item in value]
|
||||
if isinstance(value, tuple):
|
||||
return [remap_branch_state_node_ids(item, node_id_map) for item in value]
|
||||
if isinstance(value, str):
|
||||
return node_id_map.get(value, value)
|
||||
return deepcopy(value)
|
||||
|
||||
|
||||
def _lineage_error(message: str, workflow_id: str, branch_name: str, **details: Any) -> ApiError:
|
||||
return ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
message,
|
||||
{"workflow_id": workflow_id, "branch_name": branch_name, **details},
|
||||
)
|
||||
|
||||
|
||||
def _optional_text(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
text = str(value).strip()
|
||||
return text or None
|
||||
|
||||
|
||||
def _unique_lineage(values: tuple[str, ...]) -> tuple[str, ...]:
|
||||
result: list[str] = []
|
||||
for value in values:
|
||||
text = str(value or "").strip()
|
||||
if text and (not result or result[-1] != text):
|
||||
result.append(text)
|
||||
return tuple(result)
|
||||
|
|
@ -8,12 +8,16 @@ from backend.dto.common import ApiError, ErrorCode
|
|||
from backend.dto.profile_dto import CharacterProfile
|
||||
from backend.dto.workflow_dto import ProfileWorkflow, WorkflowNode
|
||||
from backend.services.workflow_service import WorkflowService
|
||||
from backend.services.workflow_progression_service import (
|
||||
CHARACTER_GRADUAL_PROGRESSION_VERSION,
|
||||
character_execution_metadata,
|
||||
character_gradual_progression,
|
||||
)
|
||||
from core.identity import RequestContext
|
||||
from engines.character_flow import canonical_gradual_node_key
|
||||
|
||||
|
||||
CHARACTER_WORKFLOW_TYPES = {"character_fast", "character_gradual", "quick_profile"}
|
||||
GENERATION_NODE_TYPES = {"generation", "single_image"}
|
||||
EDITABLE_PREPARATION_NODE_TYPES = {"planning", "reference_binding"}
|
||||
|
||||
|
||||
class CharacterWorkflowService:
|
||||
|
|
@ -50,70 +54,179 @@ class CharacterWorkflowService:
|
|||
"prepared_node_ids": [],
|
||||
"idempotent_replay": True,
|
||||
}
|
||||
if workflow.workflow_type != "character_gradual":
|
||||
return self._execute_unsequenced(workflow, active_nodes, profile, ctx)
|
||||
previous_execution = workflow.metadata.get("character_execution")
|
||||
if isinstance(previous_execution, dict):
|
||||
# execute-character is the confirmation of the synthetic import
|
||||
# entry. Replaying it must not prepare or unlock another node.
|
||||
progression = character_gradual_progression(
|
||||
workflow,
|
||||
active_nodes,
|
||||
branch_name=workflow.current_branch,
|
||||
)
|
||||
if progression is not None and progression.import_confirmed:
|
||||
return _execution_result(
|
||||
workflow,
|
||||
active_nodes,
|
||||
progression=progression,
|
||||
idempotent_replay=True,
|
||||
)
|
||||
|
||||
# Persist the import confirmation before touching profile_brief. The
|
||||
# central progression guard then makes that first backend node (and
|
||||
# only that node) writable.
|
||||
initial_execution = {
|
||||
**(previous_execution if isinstance(previous_execution, dict) else {}),
|
||||
"executor": "character_workflow",
|
||||
"workflow_type": workflow.workflow_type,
|
||||
"import_confirmed": True,
|
||||
"progression_version": CHARACTER_GRADUAL_PROGRESSION_VERSION,
|
||||
}
|
||||
running_workflow = self.workflow_service.update_workflow(
|
||||
workflow_id,
|
||||
{
|
||||
"status": "running" if workflow.status != "running" else workflow.status,
|
||||
"current_node_id": active_nodes[0].node_id,
|
||||
"metadata": {
|
||||
**workflow.metadata,
|
||||
"character_execution": initial_execution,
|
||||
},
|
||||
},
|
||||
ctx,
|
||||
allow_character_execution_update=True,
|
||||
)
|
||||
progression = character_gradual_progression(
|
||||
running_workflow,
|
||||
active_nodes,
|
||||
branch_name=running_workflow.current_branch,
|
||||
)
|
||||
if progression is None:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow does not support gradual progression",
|
||||
{"workflow_id": workflow_id, "workflow_type": workflow.workflow_type},
|
||||
)
|
||||
|
||||
prepared_nodes = list(active_nodes)
|
||||
current = progression.current_node
|
||||
if current is not None:
|
||||
prompt_context = [
|
||||
node.prompt or ""
|
||||
for node in active_nodes
|
||||
if node.node_id in progression.completed_node_ids and node.prompt
|
||||
]
|
||||
prepared = self.workflow_service.update_node(
|
||||
current.node_id,
|
||||
{
|
||||
"status": current.status,
|
||||
"prompt": _node_prompt(profile, current, running_workflow, prompt_context),
|
||||
"negative_prompt": _negative_prompt(profile, current),
|
||||
"metadata": {
|
||||
**current.metadata,
|
||||
"execution": {
|
||||
"executor": "character_workflow",
|
||||
"workflow_type": workflow.workflow_type,
|
||||
"context_prompts": len(prompt_context),
|
||||
"prepared_only": current.status == "draft",
|
||||
"progression_version": CHARACTER_GRADUAL_PROGRESSION_VERSION,
|
||||
},
|
||||
},
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
prepared_nodes = [prepared if node.node_id == prepared.node_id else node for node in active_nodes]
|
||||
progression = character_gradual_progression(
|
||||
running_workflow,
|
||||
prepared_nodes,
|
||||
branch_name=running_workflow.current_branch,
|
||||
) or progression
|
||||
|
||||
current_node_id = progression.current_node.node_id if progression.current_node else prepared_nodes[-1].node_id
|
||||
execution = character_execution_metadata(
|
||||
running_workflow,
|
||||
progression,
|
||||
existing=initial_execution,
|
||||
)
|
||||
execution["queued_node_ids"] = [node.node_id for node in prepared_nodes if node.status == "queued"]
|
||||
execution["prepared_node_ids"] = [current_node_id] if progression.current_node is not None else []
|
||||
updated_workflow = self.workflow_service.update_workflow(
|
||||
workflow_id,
|
||||
{
|
||||
"status": "completed" if progression.all_completed else "running",
|
||||
"current_node_id": current_node_id,
|
||||
"metadata": {
|
||||
**running_workflow.metadata,
|
||||
"character_execution": execution,
|
||||
},
|
||||
},
|
||||
ctx,
|
||||
allow_character_execution_update=True,
|
||||
)
|
||||
return _execution_result(updated_workflow, prepared_nodes, progression=progression)
|
||||
|
||||
def _execute_unsequenced(
|
||||
self,
|
||||
workflow: ProfileWorkflow,
|
||||
active_nodes: list[WorkflowNode],
|
||||
profile: CharacterProfile,
|
||||
ctx: RequestContext,
|
||||
) -> dict[str, Any]:
|
||||
previous_execution = workflow.metadata.get("character_execution")
|
||||
if workflow.status == "running" and isinstance(previous_execution, dict):
|
||||
completed_node_ids = [node.node_id for node in active_nodes if node.status == "completed"]
|
||||
queued_node_ids = [node.node_id for node in active_nodes if node.status == "queued"]
|
||||
prepared_node_ids = [
|
||||
node.node_id
|
||||
for node in active_nodes
|
||||
if node.node_type in EDITABLE_PREPARATION_NODE_TYPES and node.status == "draft"
|
||||
]
|
||||
return {
|
||||
"workflow": workflow.to_dict(),
|
||||
"nodes": [node.to_dict() for node in active_nodes],
|
||||
"completed_node_ids": completed_node_ids,
|
||||
"queued_node_ids": queued_node_ids,
|
||||
"prepared_node_ids": prepared_node_ids,
|
||||
"completed_node_ids": [
|
||||
node.node_id for node in active_nodes if node.status in {"completed", "selected"}
|
||||
],
|
||||
"queued_node_ids": [node.node_id for node in active_nodes if node.status == "queued"],
|
||||
"prepared_node_ids": [node.node_id for node in active_nodes if node.status == "draft"],
|
||||
"idempotent_replay": True,
|
||||
}
|
||||
running_workflow = workflow
|
||||
if workflow.status not in {"running", "completed"}:
|
||||
running_workflow = self.workflow_service.update_workflow(workflow_id, {"status": "running"}, ctx)
|
||||
if workflow.status != "running":
|
||||
running_workflow = self.workflow_service.update_workflow(
|
||||
workflow.workflow_id,
|
||||
{"status": "running"},
|
||||
ctx,
|
||||
)
|
||||
prepared_nodes: list[WorkflowNode] = []
|
||||
prompt_context: list[str] = []
|
||||
for node in active_nodes:
|
||||
prompt = _node_prompt(profile, node, running_workflow, prompt_context)
|
||||
negative_prompt = _negative_prompt(profile, node)
|
||||
if node.node_type in GENERATION_NODE_TYPES:
|
||||
target_status = "queued"
|
||||
elif node.node_type in EDITABLE_PREPARATION_NODE_TYPES:
|
||||
target_status = node.status
|
||||
else:
|
||||
target_status = "completed"
|
||||
prepared = self.workflow_service.update_node(
|
||||
node.node_id,
|
||||
{
|
||||
"status": target_status,
|
||||
"prompt": prompt,
|
||||
"negative_prompt": negative_prompt,
|
||||
"status": node.status,
|
||||
"prompt": _node_prompt(profile, node, running_workflow, prompt_context),
|
||||
"negative_prompt": _negative_prompt(profile, node),
|
||||
"metadata": {
|
||||
**node.metadata,
|
||||
"execution": {
|
||||
"executor": "character_workflow",
|
||||
"workflow_type": workflow.workflow_type,
|
||||
"context_prompts": len(prompt_context),
|
||||
"prepared_only": target_status in {"queued", "draft"},
|
||||
"prepared_only": node.status == "draft",
|
||||
},
|
||||
},
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
prepared_nodes.append(prepared)
|
||||
prompt_context.append(prompt)
|
||||
if prepared.prompt:
|
||||
prompt_context.append(prepared.prompt)
|
||||
queued_nodes = [node for node in prepared_nodes if node.status == "queued"]
|
||||
completed_nodes = [node for node in prepared_nodes if node.status == "completed"]
|
||||
editable_nodes = [
|
||||
node
|
||||
for node in prepared_nodes
|
||||
if node.node_type in EDITABLE_PREPARATION_NODE_TYPES and node.status == "draft"
|
||||
]
|
||||
workflow_status = "running" if queued_nodes or editable_nodes else "completed"
|
||||
completed_nodes = [node for node in prepared_nodes if node.status in {"completed", "selected"}]
|
||||
editable_nodes = [node for node in prepared_nodes if node.status == "draft"]
|
||||
next_node = next(
|
||||
(node for node in prepared_nodes if node.status not in {"completed", "selected", "skipped"}),
|
||||
prepared_nodes[-1],
|
||||
)
|
||||
updated_workflow = self.workflow_service.update_workflow(
|
||||
workflow_id,
|
||||
workflow.workflow_id,
|
||||
{
|
||||
"status": workflow_status,
|
||||
"current_node_id": (queued_nodes[0] if queued_nodes else prepared_nodes[-1]).node_id,
|
||||
"status": "running" if editable_nodes or queued_nodes else "completed",
|
||||
"current_node_id": next_node.node_id,
|
||||
"metadata": {
|
||||
**running_workflow.metadata,
|
||||
"character_execution": {
|
||||
|
|
@ -165,21 +278,23 @@ def _step_instruction(workflow_type: str, node_key: str) -> str:
|
|||
"preview": "saved quick profile prompts ready for structured setting split",
|
||||
}
|
||||
fast = {
|
||||
"fast_profile": "condensed character design brief",
|
||||
"fast_candidates": "candidate batch, varied composition, keep identity consistent",
|
||||
"fast_final": "final selected character illustration, production ready",
|
||||
"quick_avatar": "square avatar, face, hair, eyes, expression, keep identity consistent",
|
||||
"quick_bust": "half-body character reference, clothing, posture, keep identity consistent",
|
||||
"quick_stand": "full-body standing character illustration, complete outfit and proportions",
|
||||
"quick_sheet": "character design sheet, multiple views, outfit details and material callouts",
|
||||
}
|
||||
gradual = {
|
||||
"profile_brief": "core identity and visual anchors",
|
||||
"prompt_plan": "prompt matrix for portrait, half body, full body, and design sheet nodes",
|
||||
"prompt_plan": "prompt matrix and inheritance rules for every image stage",
|
||||
"material_bind": "reference material binding rules for style, composition, clothing, shoes, and accessories",
|
||||
"headshot": "headshot, face, hair, eyes, expression",
|
||||
"half_body": "half body, upper outfit, posture and silhouette",
|
||||
"upper_body": "upper body, outfit, posture, silhouette, preserve the confirmed face identity",
|
||||
"lower_body": "lower body, waist, hips, legs, footwear and consistent body proportions",
|
||||
"full_body": "full body, complete outfit and proportions",
|
||||
"expression_sheet": "expression sheet with reusable emotions",
|
||||
"pose_sheet": "pose sheet with action variants",
|
||||
"style_lock": "style consistency lock and final rendering rules",
|
||||
"final_pack": "final character pack, accepted images and reusable metadata",
|
||||
"turnaround": "front, side and back turnaround views with identical identity and costume structure",
|
||||
"design_sheet": "design sheet with expressions, poses, hands, accessories and reusable callouts",
|
||||
"style_lock": "high-resolution restoration, identity consistency and final rendering rules",
|
||||
"profile_card": "validate accepted assets and seal the immutable character profile card",
|
||||
}
|
||||
tables = {
|
||||
"character_fast": fast,
|
||||
|
|
@ -187,7 +302,8 @@ def _step_instruction(workflow_type: str, node_key: str) -> str:
|
|||
"quick_profile": quick_profile,
|
||||
}
|
||||
table = tables.get(workflow_type, {})
|
||||
return table.get(node_key, node_key.replace("_", " "))
|
||||
resolved_key = canonical_gradual_node_key(node_key) if workflow_type == "character_gradual" else node_key
|
||||
return table.get(resolved_key, resolved_key.replace("_", " "))
|
||||
|
||||
|
||||
def _unique_text(values: list[Any]) -> list[str]:
|
||||
|
|
@ -199,3 +315,29 @@ def _unique_text(values: list[Any]) -> list[str]:
|
|||
result.append(text)
|
||||
seen.add(text)
|
||||
return result
|
||||
|
||||
|
||||
def _execution_result(
|
||||
workflow: ProfileWorkflow,
|
||||
nodes: list[WorkflowNode],
|
||||
*,
|
||||
progression,
|
||||
idempotent_replay: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
current_node_id = progression.current_node.node_id if progression.current_node else None
|
||||
result = {
|
||||
"workflow": workflow.to_dict(),
|
||||
"nodes": [node.to_dict() for node in nodes],
|
||||
"completed_node_ids": list(progression.completed_node_ids),
|
||||
"queued_node_ids": [node.node_id for node in nodes if node.status == "queued"],
|
||||
"prepared_node_ids": [current_node_id] if current_node_id else [],
|
||||
"locked_node_ids": [
|
||||
node.node_id
|
||||
for node in nodes
|
||||
if not progression.is_accessible(node)
|
||||
],
|
||||
"progression": progression.to_dict(),
|
||||
}
|
||||
if idempotent_replay:
|
||||
result["idempotent_replay"] = True
|
||||
return result
|
||||
|
|
|
|||
文件差异内容过多而无法显示
加载差异
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
"""Multi-route visual review used by AstrBot chat commands."""
|
||||
"""Multi-route VLM visual-quality review used by AstrBot chat commands.
|
||||
|
||||
Generation-based cross review is planned separately by
|
||||
``GenerationCrossReviewPlanner`` and must use image-generation routes.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
|
|
@ -7,13 +11,16 @@ import mimetypes
|
|||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.services.model_provider_bootstrap import sync_vision_provider_from_config
|
||||
from backend.services.model_provider_runtime import ModelProviderRuntime
|
||||
from backend.services.model_provider_service import ModelProviderService
|
||||
from backend.services.workflow_progression_service import ensure_character_gradual_node_access
|
||||
from core.storage import VfsStorage
|
||||
from database.db_helper import Database, load_config_payload, load_database_settings
|
||||
from database.repositories.image_repository import ImageRepository
|
||||
from database.repositories.model_provider_repository import ModelProviderRepository
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
from engines.vision_provider import VisionRecognitionRequest
|
||||
|
||||
|
||||
|
|
@ -21,6 +28,7 @@ class CrossReviewService:
|
|||
def __init__(self):
|
||||
self.database = Database(load_database_settings())
|
||||
self.images = ImageRepository(self.database)
|
||||
self.workflows = WorkflowRepository(self.database)
|
||||
self.storage = VfsStorage()
|
||||
self.config = load_config_payload()
|
||||
provider_service = ModelProviderService(ModelProviderRepository(self.database))
|
||||
|
|
@ -28,6 +36,22 @@ class CrossReviewService:
|
|||
self.runtime = ModelProviderRuntime(provider_service)
|
||||
|
||||
def review_node(self, node_id: str) -> dict[str, Any]:
|
||||
node = self.workflows.get_node(node_id)
|
||||
if node is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "node not found", {"node_id": node_id})
|
||||
workflow = self.workflows.get_workflow(node.workflow_id)
|
||||
if workflow is None:
|
||||
raise ApiError(
|
||||
ErrorCode.RESOURCE_NOT_FOUND,
|
||||
"workflow not found",
|
||||
{"workflow_id": node.workflow_id},
|
||||
)
|
||||
ensure_character_gradual_node_access(
|
||||
workflow,
|
||||
self.workflows.list_nodes(node.workflow_id, branch_name=node.branch_name),
|
||||
node,
|
||||
operation="review",
|
||||
)
|
||||
images = self.images.list_by_node(node_id, include_archived=False)
|
||||
selected = next((item for item in images if item.is_selected), None) or (images[0] if images else None)
|
||||
if selected is None:
|
||||
|
|
@ -46,6 +70,7 @@ class CrossReviewService:
|
|||
successful = [item for item in reviews if item.get("status") == "succeeded"]
|
||||
defects = sorted({tag for item in successful for tag in item.get("defects", [])})
|
||||
return {
|
||||
"review_kind": "visual_quality",
|
||||
"image_id": selected.image_id,
|
||||
"routes": routes,
|
||||
"reviews": reviews,
|
||||
|
|
@ -76,6 +101,8 @@ class CrossReviewService:
|
|||
|
||||
def _routes(self) -> list[str]:
|
||||
assignments = self.config.get("model_assignments") if isinstance(self.config.get("model_assignments"), dict) else {}
|
||||
configured = assignments.get("visual_review_routes")
|
||||
if not isinstance(configured, list) or not configured:
|
||||
configured = assignments.get("cross_review_routes")
|
||||
routes = [str(item).strip() for item in configured] if isinstance(configured, list) else []
|
||||
routes.extend([str(assignments.get("cross_review_primary") or "").strip(), str(assignments.get("cross_review_secondary") or "").strip()])
|
||||
|
|
@ -93,3 +120,8 @@ def _disagreements(reviews: list[dict[str, Any]]) -> list[str]:
|
|||
tag_sets = [set(item.get("defects", [])) for item in reviews]
|
||||
union = set().union(*tag_sets)
|
||||
return sorted(tag for tag in union if not all(tag in tags for tags in tag_sets))
|
||||
|
||||
|
||||
# Clear name for new call sites; keep CrossReviewService as the compatibility
|
||||
# symbol used by existing chat commands and tests.
|
||||
VisualQualityReviewService = CrossReviewService
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,270 @@
|
|||
"""Plan generation-based cross review with distinct image-generation models."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.services.model_provider_service import ModelProviderService
|
||||
|
||||
|
||||
GENERATION_CROSS_REVIEW_VERSION = "2026-07-15.generation-cross-review.v2"
|
||||
|
||||
|
||||
class GenerationCrossReviewPlanner:
|
||||
"""Validate routes and produce a deterministic candidate-generation plan.
|
||||
|
||||
This is intentionally separate from ``CrossReviewService``, which performs
|
||||
VLM visual quality inspection. The planner never calls a provider; it only
|
||||
proves that every secondary route resolves to a distinct image model.
|
||||
"""
|
||||
|
||||
def __init__(self, model_providers: ModelProviderService):
|
||||
self.model_providers = model_providers
|
||||
|
||||
def plan(
|
||||
self,
|
||||
*,
|
||||
primary_route: str,
|
||||
configured_routes: list[str] | tuple[str, ...],
|
||||
source_candidate_id: str,
|
||||
source_image_id: str,
|
||||
selected_for_regeneration: list[str] | tuple[str, ...] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
primary_name = _required_text(primary_route, "primary_route")
|
||||
source_candidate = _required_text(source_candidate_id, "source_candidate_id")
|
||||
source_image = _required_text(source_image_id, "source_image_id")
|
||||
primary = self._resolve_image_route(primary_name)
|
||||
|
||||
ordered_names = _unique_routes(configured_routes)
|
||||
if primary_name not in ordered_names:
|
||||
ordered_names.insert(0, primary_name)
|
||||
resolved_routes = [self._resolve_image_route(route_name) for route_name in ordered_names]
|
||||
primary_identity = _route_identity(primary)
|
||||
distinct: list[dict[str, Any]] = []
|
||||
seen_identities = {primary_identity}
|
||||
duplicate_routes: list[str] = []
|
||||
for route in resolved_routes:
|
||||
identity = _route_identity(route)
|
||||
if route["route_name"] == primary_name:
|
||||
continue
|
||||
if identity in seen_identities:
|
||||
duplicate_routes.append(str(route["route_name"]))
|
||||
continue
|
||||
seen_identities.add(identity)
|
||||
distinct.append(route)
|
||||
if not distinct:
|
||||
raise ApiError(
|
||||
ErrorCode.MODEL_CAPABILITY_UNCONFIRMED,
|
||||
"cross review requires at least one different image-generation model",
|
||||
{
|
||||
"primary_route": primary_name,
|
||||
"configured_routes": ordered_names,
|
||||
"duplicate_routes": duplicate_routes,
|
||||
"required_modality": "image_generation",
|
||||
"required_action": "configure_distinct_generation_cross_review_routes",
|
||||
},
|
||||
)
|
||||
|
||||
regenerate_ids = {
|
||||
str(item).strip()
|
||||
for item in (selected_for_regeneration or [])
|
||||
if str(item).strip()
|
||||
}
|
||||
candidates: list[dict[str, Any]] = [
|
||||
{
|
||||
"candidate_index": 1,
|
||||
"role": "primary_preserved",
|
||||
"source_candidate_id": source_candidate,
|
||||
"source_image_id": source_image,
|
||||
"model_route": primary_name,
|
||||
"provider_id": primary["provider_id"],
|
||||
"provider_name": _route_summary(primary).get("provider_name"),
|
||||
"model_id": primary["model_id"],
|
||||
"generate": source_candidate in regenerate_ids,
|
||||
"preserve_when_not_selected": True,
|
||||
}
|
||||
]
|
||||
for index, route in enumerate(distinct, start=2):
|
||||
candidates.append(
|
||||
{
|
||||
"candidate_index": index,
|
||||
"role": "cross_review_model",
|
||||
"source_candidate_id": source_candidate,
|
||||
"source_image_id": source_image,
|
||||
"model_route": route["route_name"],
|
||||
"provider_id": route["provider_id"],
|
||||
"provider_name": _route_summary(route).get("provider_name"),
|
||||
"model_id": route["model_id"],
|
||||
"generate": True,
|
||||
"preserve_when_not_selected": True,
|
||||
}
|
||||
)
|
||||
return {
|
||||
"schema_version": GENERATION_CROSS_REVIEW_VERSION,
|
||||
"round_kind": "cross_review",
|
||||
"primary_route": _route_summary(primary),
|
||||
"secondary_routes": [_route_summary(item) for item in distinct],
|
||||
"candidates": candidates,
|
||||
"duplicate_routes_ignored": duplicate_routes,
|
||||
}
|
||||
|
||||
def plan_followup(
|
||||
self,
|
||||
*,
|
||||
candidates: list[dict[str, Any]] | tuple[dict[str, Any], ...],
|
||||
selected_for_regeneration: list[str] | tuple[str, ...],
|
||||
) -> dict[str, Any]:
|
||||
"""Plan a review child round without silently replacing candidates.
|
||||
|
||||
Every prior slot is copied into the child round. Only explicitly
|
||||
selected candidate IDs are regenerated, using the same image route as
|
||||
that slot. This keeps candidate 01 as the primary-model slot and makes
|
||||
later TXSC rounds directly comparable with their parent.
|
||||
"""
|
||||
|
||||
selected = {
|
||||
str(value).strip()
|
||||
for value in selected_for_regeneration
|
||||
if str(value).strip()
|
||||
}
|
||||
if not selected:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"select at least one cross-review candidate to regenerate",
|
||||
{
|
||||
"field": "candidate_ids",
|
||||
"minimum": 1,
|
||||
"required_action": "select_review_candidates",
|
||||
},
|
||||
)
|
||||
|
||||
normalized: list[dict[str, Any]] = []
|
||||
known_ids: set[str] = set()
|
||||
resolved_identities: dict[tuple[str, str], str] = {}
|
||||
for ordinal, raw in enumerate(candidates, start=1):
|
||||
candidate_id = _required_text(raw.get("candidate_id"), "candidate_id")
|
||||
if candidate_id in known_ids:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"cross-review candidate list contains duplicate IDs",
|
||||
{"candidate_id": candidate_id},
|
||||
)
|
||||
known_ids.add(candidate_id)
|
||||
image_id = _required_text(raw.get("image_id"), "image_id")
|
||||
route_name = _required_text(raw.get("model_route"), "model_route")
|
||||
route = self._resolve_image_route(route_name)
|
||||
identity = _route_identity(route)
|
||||
duplicate_candidate = resolved_identities.get(identity)
|
||||
if duplicate_candidate:
|
||||
raise ApiError(
|
||||
ErrorCode.MODEL_CAPABILITY_UNCONFIRMED,
|
||||
"cross-review candidate slots must use different image-generation models",
|
||||
{
|
||||
"candidate_id": candidate_id,
|
||||
"duplicate_candidate_id": duplicate_candidate,
|
||||
"model_route": route_name,
|
||||
"provider_id": route.get("provider_id"),
|
||||
"model_id": route.get("model_id"),
|
||||
"required_action": "configure_distinct_generation_cross_review_routes",
|
||||
},
|
||||
)
|
||||
resolved_identities[identity] = candidate_id
|
||||
try:
|
||||
candidate_index = int(raw.get("candidate_index") or ordinal)
|
||||
except (TypeError, ValueError) as exc:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"cross-review candidate index must be an integer",
|
||||
{"candidate_id": candidate_id},
|
||||
) from exc
|
||||
normalized.append(
|
||||
{
|
||||
"candidate_index": candidate_index,
|
||||
"role": "primary_preserved" if candidate_index == 1 else "cross_review_model",
|
||||
"source_candidate_id": candidate_id,
|
||||
"source_image_id": image_id,
|
||||
"model_route": route_name,
|
||||
"provider_id": route["provider_id"],
|
||||
"model_id": route["model_id"],
|
||||
"provider_name": _route_summary(route).get("provider_name"),
|
||||
"generate": candidate_id in selected,
|
||||
"preserve_when_not_selected": True,
|
||||
}
|
||||
)
|
||||
|
||||
unknown = sorted(selected - known_ids)
|
||||
if unknown:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"selected cross-review candidate does not belong to the parent round",
|
||||
{
|
||||
"unknown_candidate_ids": unknown,
|
||||
"allowed_candidate_ids": sorted(known_ids),
|
||||
},
|
||||
)
|
||||
if len(resolved_identities) < 2:
|
||||
raise ApiError(
|
||||
ErrorCode.MODEL_CAPABILITY_UNCONFIRMED,
|
||||
"cross review requires at least two different image-generation models",
|
||||
{
|
||||
"candidate_count": len(normalized),
|
||||
"required_action": "configure_distinct_generation_cross_review_routes",
|
||||
},
|
||||
)
|
||||
normalized.sort(key=lambda item: (int(item["candidate_index"]), str(item["source_candidate_id"])))
|
||||
return {
|
||||
"schema_version": GENERATION_CROSS_REVIEW_VERSION,
|
||||
"round_kind": "cross_review",
|
||||
"followup": True,
|
||||
"selected_for_regeneration": sorted(selected),
|
||||
"candidates": normalized,
|
||||
}
|
||||
|
||||
def _resolve_image_route(self, route_name: str) -> dict[str, Any]:
|
||||
resolved = self.model_providers.resolve_route(route_name, modality="image_generation")
|
||||
if not resolved:
|
||||
raise ApiError(
|
||||
ErrorCode.MODEL_CAPABILITY_UNCONFIRMED,
|
||||
"cross-review route is missing or is not an image-generation route",
|
||||
{
|
||||
"route_name": route_name,
|
||||
"required_modality": "image_generation",
|
||||
"required_action": "configure_image_generation_route",
|
||||
},
|
||||
)
|
||||
return resolved
|
||||
|
||||
|
||||
def _route_identity(route: dict[str, Any]) -> tuple[str, str]:
|
||||
return (str(route.get("provider_id") or ""), str(route.get("model_id") or ""))
|
||||
|
||||
|
||||
def _route_summary(route: dict[str, Any]) -> dict[str, Any]:
|
||||
provider = route.get("provider") if isinstance(route.get("provider"), dict) else {}
|
||||
return {
|
||||
"route_name": route.get("route_name"),
|
||||
"route_id": route.get("route_id"),
|
||||
"provider_id": route.get("provider_id"),
|
||||
"provider_name": provider.get("display_name"),
|
||||
"provider_type": provider.get("provider_type"),
|
||||
"model_id": route.get("model_id"),
|
||||
"modality": route.get("modality"),
|
||||
"status": "resolved",
|
||||
}
|
||||
|
||||
|
||||
def _unique_routes(values: list[str] | tuple[str, ...]) -> list[str]:
|
||||
result: list[str] = []
|
||||
for value in values:
|
||||
text = str(value or "").strip()
|
||||
if text and text not in result:
|
||||
result.append(text)
|
||||
return result
|
||||
|
||||
|
||||
def _required_text(value: Any, field: str) -> str:
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, f"{field} is required", {"field": field})
|
||||
return text
|
||||
|
|
@ -51,8 +51,10 @@ class GenerationJobRetryService:
|
|||
overrides=request_overrides,
|
||||
)
|
||||
prepared_node = None
|
||||
prepared_workflow = None
|
||||
|
||||
try:
|
||||
prepared_workflow = self._prepare_workflow(claimed, ctx)
|
||||
prepared_node = self._prepare_node(claimed, ctx)
|
||||
response = self.generation_service.generate_node(str(claimed.node_id), request, ctx)
|
||||
replacement = self.queue_service.get_job(str(response["task_id"]))
|
||||
|
|
@ -64,6 +66,10 @@ class GenerationJobRetryService:
|
|||
node_status_restored = self._restore_prepared_node(prepared_node, claimed.task_id, ctx)
|
||||
except Exception:
|
||||
node_status_restored = False
|
||||
try:
|
||||
self._restore_prepared_workflow(prepared_workflow, claimed.task_id, ctx)
|
||||
except Exception:
|
||||
pass
|
||||
self._restore_original(claimed, previous_status=previous_status, exc=exc, ctx=ctx)
|
||||
else:
|
||||
replacement = self._finalize_lineage(claimed, replacement, operation_id=operation_id, ctx=ctx)
|
||||
|
|
@ -78,6 +84,64 @@ class GenerationJobRetryService:
|
|||
|
||||
return self._finalize_lineage(claimed, replacement, operation_id=operation_id, ctx=ctx)
|
||||
|
||||
def _prepare_workflow(self, task: GenerationTask, ctx: RequestContext) -> Any | None:
|
||||
if not task.workflow_id:
|
||||
return None
|
||||
workflow_service = self.generation_service.workflow_service
|
||||
workflow = workflow_service.get_workflow(str(task.workflow_id)).workflow
|
||||
if workflow.workflow_type not in {"character_fast", "character_gradual"}:
|
||||
return None
|
||||
if workflow.status in {"active", "running"}:
|
||||
return None
|
||||
if workflow.status not in {"failed", "cancelled"}:
|
||||
raise ApiError(
|
||||
ErrorCode.TASK_NOT_RETRYABLE,
|
||||
"character workflow cannot be reopened for this retry",
|
||||
{
|
||||
"task_id": task.task_id,
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"workflow_status": workflow.status,
|
||||
},
|
||||
)
|
||||
prepared_metadata = dict(workflow.metadata or {})
|
||||
prepared_metadata.pop("quick_character_failure", None)
|
||||
prepared_metadata["retry_preparation"] = {
|
||||
"task_id": task.task_id,
|
||||
"prepared_at": _now(),
|
||||
"previous_status": workflow.status,
|
||||
}
|
||||
workflow_service.update_workflow(
|
||||
workflow.workflow_id,
|
||||
{
|
||||
"status": "running",
|
||||
"metadata": prepared_metadata,
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
return workflow
|
||||
|
||||
def _restore_prepared_workflow(self, original_workflow: Any | None, task_id: str, ctx: RequestContext) -> bool:
|
||||
if original_workflow is None:
|
||||
return True
|
||||
workflow_service = self.generation_service.workflow_service
|
||||
current = workflow_service.get_workflow(original_workflow.workflow_id).workflow
|
||||
preparation = current.metadata.get("retry_preparation") if isinstance(current.metadata, dict) else None
|
||||
if (
|
||||
current.status != "running"
|
||||
or not isinstance(preparation, dict)
|
||||
or _optional_text(preparation.get("task_id")) != task_id
|
||||
):
|
||||
return False
|
||||
workflow_service.update_workflow(
|
||||
original_workflow.workflow_id,
|
||||
{
|
||||
"status": original_workflow.status,
|
||||
"metadata": _metadata_without_owned_retry_preparation(current.metadata, task_id),
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
return True
|
||||
|
||||
def retry_dead_letters(self, ctx: RequestContext, *, limit: int = 5) -> dict[str, Any]:
|
||||
applied_limit = _synchronous_batch_limit(limit)
|
||||
jobs = self.queue_service.list_jobs(
|
||||
|
|
@ -186,7 +250,10 @@ class GenerationJobRetryService:
|
|||
return False
|
||||
after = workflow_service.workflow_repository.update_node(
|
||||
original_node.node_id,
|
||||
{"status": original_node.status, "metadata": original_node.metadata},
|
||||
{
|
||||
"status": original_node.status,
|
||||
"metadata": _metadata_without_owned_retry_preparation(current.metadata, task_id),
|
||||
},
|
||||
)
|
||||
if after is None:
|
||||
return False
|
||||
|
|
@ -271,8 +338,9 @@ class GenerationJobRetryService:
|
|||
)
|
||||
latest_original = self.queue_service.get_job(original.task_id)
|
||||
retry_execution = dict(latest_original.metadata.get("retry_execution") or {})
|
||||
self.queue_service.update_task(
|
||||
superseded = self.queue_service.update_task_if_status(
|
||||
original.task_id,
|
||||
{"retrying"},
|
||||
{
|
||||
"status": "superseded",
|
||||
"progress": 100,
|
||||
|
|
@ -293,8 +361,43 @@ class GenerationJobRetryService:
|
|||
},
|
||||
ctx,
|
||||
)
|
||||
if superseded is None:
|
||||
current = self.queue_service.get_job(original.task_id)
|
||||
raise ApiError(
|
||||
ErrorCode.TASK_NOT_RETRYABLE,
|
||||
"original task changed before retry lineage was finalized",
|
||||
{
|
||||
"task_id": original.task_id,
|
||||
"status": current.status,
|
||||
"replacement_task_id": updated_replacement.task_id,
|
||||
},
|
||||
)
|
||||
self._clear_retry_preparation(original, ctx)
|
||||
return updated_replacement
|
||||
|
||||
def _clear_retry_preparation(self, original: GenerationTask, ctx: RequestContext) -> None:
|
||||
workflow_service = self.generation_service.workflow_service
|
||||
if original.node_id:
|
||||
try:
|
||||
node = workflow_service.get_node(str(original.node_id))
|
||||
metadata = dict(node.metadata or {})
|
||||
preparation = metadata.get("retry_preparation")
|
||||
if isinstance(preparation, dict) and _optional_text(preparation.get("task_id")) == original.task_id:
|
||||
metadata.pop("retry_preparation", None)
|
||||
workflow_service.update_node(node.node_id, {"metadata": metadata}, ctx)
|
||||
except Exception:
|
||||
pass
|
||||
if original.workflow_id:
|
||||
try:
|
||||
workflow = workflow_service.get_workflow(str(original.workflow_id)).workflow
|
||||
metadata = dict(workflow.metadata or {})
|
||||
preparation = metadata.get("retry_preparation")
|
||||
if isinstance(preparation, dict) and _optional_text(preparation.get("task_id")) == original.task_id:
|
||||
metadata.pop("retry_preparation", None)
|
||||
workflow_service.update_workflow(workflow.workflow_id, {"metadata": metadata}, ctx)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def _restore_original(
|
||||
self,
|
||||
original: GenerationTask,
|
||||
|
|
@ -307,8 +410,9 @@ class GenerationJobRetryService:
|
|||
if latest.status != "retrying":
|
||||
return
|
||||
retry_execution = dict(latest.metadata.get("retry_execution") or {})
|
||||
self.queue_service.update_task(
|
||||
self.queue_service.update_task_if_status(
|
||||
original.task_id,
|
||||
{"retrying"},
|
||||
{
|
||||
"status": previous_status,
|
||||
"metadata": {
|
||||
|
|
@ -378,6 +482,14 @@ def _safe_error_message(exc: Exception) -> str:
|
|||
return text[:500] if text else type(exc).__name__
|
||||
|
||||
|
||||
def _metadata_without_owned_retry_preparation(metadata: Any, task_id: str) -> dict[str, Any]:
|
||||
current = dict(metadata or {}) if isinstance(metadata, dict) else {}
|
||||
preparation = current.get("retry_preparation")
|
||||
if isinstance(preparation, dict) and _optional_text(preparation.get("task_id")) == task_id:
|
||||
current.pop("retry_preparation", None)
|
||||
return current
|
||||
|
||||
|
||||
def _now() -> str:
|
||||
return datetime.now(timezone.utc).isoformat()
|
||||
|
||||
|
|
|
|||
|
|
@ -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,11 +62,11 @@ 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,
|
||||
|
|
@ -77,13 +78,13 @@ class GenerationReferenceService:
|
|||
)
|
||||
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,
|
||||
|
|
|
|||
文件差异内容过多而无法显示
加载差异
|
|
@ -136,6 +136,14 @@ class HistoryService:
|
|||
) -> None:
|
||||
if next_node is None:
|
||||
return
|
||||
# The source adoption advances the authoritative progression first.
|
||||
# Only the newly-derived current node may receive the handoff; a
|
||||
# client-supplied later node cannot skip intermediate stages.
|
||||
self.workflow_service.ensure_node_action_allowed(
|
||||
next_node.node_id,
|
||||
"handoff",
|
||||
node=next_node,
|
||||
)
|
||||
existing_metadata = dict(workflow.metadata or {})
|
||||
existing_stage_inputs = existing_metadata.get("stage_inputs")
|
||||
stage_inputs = dict(existing_stage_inputs) if isinstance(existing_stage_inputs, dict) else {}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ from uuid import uuid4
|
|||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.dto.image_dto import GenerationImage, ImageListResult
|
||||
from backend.services.workflow_service import LOCKED_NODE_STATUSES, locked_node_edit_error
|
||||
from backend.services.workflow_progression_service import ensure_character_gradual_node_access
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from database.repositories.image_repository import ImageRepository
|
||||
|
|
@ -31,6 +32,13 @@ class ImageService:
|
|||
if node_id and node is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "节点不存在", {"node_id": node_id})
|
||||
workflow = self.workflow_repository.get_workflow(node.workflow_id) if node else None
|
||||
if node and workflow:
|
||||
ensure_character_gradual_node_access(
|
||||
workflow,
|
||||
self.workflow_repository.list_nodes(node.workflow_id, branch_name=node.branch_name),
|
||||
node,
|
||||
operation="create_image",
|
||||
)
|
||||
image_uri = str(payload.get("image_uri") or "").strip()
|
||||
if not image_uri:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "图片 URI 不能为空", {"field": "image_uri"})
|
||||
|
|
@ -229,6 +237,15 @@ class ImageService:
|
|||
if not image.node_id:
|
||||
return
|
||||
node = self.workflow_repository.get_node(image.node_id)
|
||||
if node:
|
||||
workflow = self.workflow_repository.get_workflow(node.workflow_id)
|
||||
if workflow:
|
||||
ensure_character_gradual_node_access(
|
||||
workflow,
|
||||
self.workflow_repository.list_nodes(node.workflow_id, branch_name=node.branch_name),
|
||||
node,
|
||||
operation="image_mutation",
|
||||
)
|
||||
# ``completed`` means generation finished and its candidates are ready
|
||||
# for curation. Prompt/node edits stay locked in WorkflowService, but
|
||||
# users must still be able to change the tentative selection or
|
||||
|
|
|
|||
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
|
|
@ -6,9 +6,12 @@ from copy import deepcopy
|
|||
from typing import Any
|
||||
|
||||
from backend.dto.workflow_dto import WorkflowNode
|
||||
from engines.character_flow import canonical_gradual_node_key
|
||||
|
||||
|
||||
MATERIAL_BINDING_SCHEMA_VERSION = "2026-07-09.material-binding-schema.v1"
|
||||
NODE_MATERIAL_BINDING_SCHEMA_VERSION = "2026-07-15.node-material-bindings.v1"
|
||||
NODE_MATERIAL_BINDING_METADATA_KEY = "node_material_bindings"
|
||||
|
||||
REFERENCE_POLICY = {
|
||||
"mode": "explicit_only",
|
||||
|
|
@ -19,9 +22,9 @@ REFERENCE_POLICY = {
|
|||
STAGE_NODE_KEYS: dict[str, tuple[str, ...]] = {
|
||||
"profile-prep": ("profile_brief", "prompt_plan", "material_bind"),
|
||||
"portrait-draft": ("headshot", "fast_candidates"),
|
||||
"halfbody-draft": ("half_body",),
|
||||
"body-sheet": ("full_body", "expression_sheet", "pose_sheet"),
|
||||
"review-archive": ("style_lock", "final_pack", "fast_final"),
|
||||
"halfbody-draft": ("upper_body", "lower_body"),
|
||||
"body-sheet": ("full_body", "turnaround", "design_sheet"),
|
||||
"review-archive": ("style_lock", "profile_card", "fast_final"),
|
||||
}
|
||||
|
||||
STRENGTHS: dict[str, tuple[str, float]] = {
|
||||
|
|
@ -97,7 +100,15 @@ def material_bindings_for_node(
|
|||
workflow_nodes: list[WorkflowNode] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
normalized = normalize_material_bindings(workflow_metadata.get("material_bindings"), workflow_nodes)
|
||||
return node_material_binding_view(normalized, node)
|
||||
inherited = node_material_binding_view(normalized, node)
|
||||
state = (
|
||||
node.metadata.get(NODE_MATERIAL_BINDING_METADATA_KEY)
|
||||
if isinstance(node.metadata, dict)
|
||||
else None
|
||||
)
|
||||
if not isinstance(state, dict):
|
||||
return inherited
|
||||
return node_material_binding_override_view(state, node, inherited=inherited)
|
||||
|
||||
|
||||
def node_material_binding_view(normalized: dict[str, Any], node: WorkflowNode) -> dict[str, Any]:
|
||||
|
|
@ -106,12 +117,12 @@ def node_material_binding_view(normalized: dict[str, Any], node: WorkflowNode) -
|
|||
node_view = by_node_id.get(node.node_id)
|
||||
if not isinstance(node_view, dict):
|
||||
node_view = _node_view(
|
||||
active_rules=by_node_key.get(node.node_key, []),
|
||||
active_rules=by_node_key.get(canonical_gradual_node_key(node.node_key), []),
|
||||
node_id=node.node_id,
|
||||
node_key=node.node_key,
|
||||
title=node.title,
|
||||
)
|
||||
return {
|
||||
result = {
|
||||
"schema_version": normalized.get("schema_version", MATERIAL_BINDING_SCHEMA_VERSION),
|
||||
"reference_policy": normalized.get("reference_policy", deepcopy(REFERENCE_POLICY)),
|
||||
"summary": normalized.get("summary", {}),
|
||||
|
|
@ -120,6 +131,104 @@ def node_material_binding_view(normalized: dict[str, Any], node: WorkflowNode) -
|
|||
"target_node_title": node.title,
|
||||
**node_view,
|
||||
}
|
||||
generation_params = result.get("generation_params")
|
||||
selected_material_ids = (
|
||||
generation_params.get("reference_material_ids")
|
||||
if isinstance(generation_params, dict)
|
||||
and isinstance(generation_params.get("reference_material_ids"), list)
|
||||
else []
|
||||
)
|
||||
result["selected_material_ids"] = _unique_texts(selected_material_ids)
|
||||
result.setdefault("temporary_references", [])
|
||||
result.setdefault("navigation_context", {})
|
||||
result["binding_source"] = "workflow_material_bind"
|
||||
return result
|
||||
|
||||
|
||||
def node_material_binding_override_view(
|
||||
state: dict[str, Any],
|
||||
node: WorkflowNode,
|
||||
*,
|
||||
inherited: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Return one node-scoped binding view over the workflow-level defaults.
|
||||
|
||||
The node state stores the full effective selection deliberately. That
|
||||
makes refresh/restart recovery deterministic and lets a user explicitly
|
||||
remove a material that was selected during the earlier planning stage.
|
||||
The material records themselves remain in the material library; only IDs
|
||||
and lightweight binding options are persisted on the workflow node.
|
||||
"""
|
||||
|
||||
base = inherited if isinstance(inherited, dict) else {}
|
||||
active_bindings = [
|
||||
deepcopy(item)
|
||||
for item in state.get("active_bindings", [])
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
selected_material_ids = _unique_texts(
|
||||
state.get("selected_material_ids")
|
||||
if isinstance(state.get("selected_material_ids"), list)
|
||||
else [
|
||||
(
|
||||
item.get("material", {}).get("material_id")
|
||||
if isinstance(item.get("material"), dict)
|
||||
else item.get("material_id")
|
||||
)
|
||||
for item in active_bindings
|
||||
]
|
||||
)
|
||||
material_prompts = _unique_texts(
|
||||
(
|
||||
item.get("material", {}).get("prompt")
|
||||
if isinstance(item.get("material"), dict)
|
||||
else item.get("prompt")
|
||||
)
|
||||
for item in active_bindings
|
||||
)
|
||||
reference_strengths = [
|
||||
deepcopy(item.get("reference_strength"))
|
||||
for item in active_bindings
|
||||
if isinstance(item.get("reference_strength"), dict)
|
||||
]
|
||||
temporary_references = [
|
||||
deepcopy(item)
|
||||
for item in state.get("temporary_references", [])
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
return {
|
||||
**base,
|
||||
"schema_version": state.get("schema_version", NODE_MATERIAL_BINDING_SCHEMA_VERSION),
|
||||
"base_schema_version": base.get("schema_version", MATERIAL_BINDING_SCHEMA_VERSION),
|
||||
"reference_policy": deepcopy(REFERENCE_POLICY),
|
||||
"binding_source": "node_override",
|
||||
"target_node_id": node.node_id,
|
||||
"target_node_key": node.node_key,
|
||||
"target_node_title": node.title,
|
||||
"selected_material_ids": selected_material_ids,
|
||||
"has_references": bool(active_bindings or temporary_references),
|
||||
"active_bindings": active_bindings,
|
||||
"material_prompts": material_prompts,
|
||||
"prompt_context": ", ".join(material_prompts),
|
||||
"generation_params": {
|
||||
"reference_material_ids": selected_material_ids,
|
||||
"reference_prompts": material_prompts,
|
||||
"reference_strengths": reference_strengths,
|
||||
},
|
||||
"temporary_references": temporary_references,
|
||||
"navigation_context": deepcopy(state.get("navigation_context"))
|
||||
if isinstance(state.get("navigation_context"), dict)
|
||||
else {},
|
||||
"updated_at": state.get("updated_at"),
|
||||
"updated_by": state.get("updated_by"),
|
||||
"summary": {
|
||||
"rule_count": len(active_bindings),
|
||||
"active_rule_count": len(active_bindings),
|
||||
"has_references": bool(active_bindings or temporary_references),
|
||||
"temporary_reference_count": len(temporary_references),
|
||||
"target_node_keys": [canonical_gradual_node_key(node.node_key)],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def _extract_source_payload(payload: Any) -> dict[str, Any]:
|
||||
|
|
@ -218,7 +327,7 @@ def _index_by_node_key(rules: list[dict[str, Any]]) -> dict[str, list[dict[str,
|
|||
result: dict[str, list[dict[str, Any]]] = {}
|
||||
for rule in rules:
|
||||
for node_key in rule["target_node_keys"]:
|
||||
result.setdefault(node_key, []).append(rule)
|
||||
result.setdefault(canonical_gradual_node_key(node_key), []).append(rule)
|
||||
return result
|
||||
|
||||
|
||||
|
|
@ -227,11 +336,13 @@ def _index_by_node_id(
|
|||
workflow_nodes: list[WorkflowNode],
|
||||
) -> dict[str, dict[str, Any]]:
|
||||
by_node_key = _index_by_node_key(rules)
|
||||
return {
|
||||
node.node_id: _node_view(by_node_key.get(node.node_key, []), node.node_id, node.node_key, node.title)
|
||||
for node in workflow_nodes
|
||||
if by_node_key.get(node.node_key)
|
||||
}
|
||||
result: dict[str, dict[str, Any]] = {}
|
||||
for node in workflow_nodes:
|
||||
canonical_key = canonical_gradual_node_key(node.node_key)
|
||||
active_rules = by_node_key.get(canonical_key)
|
||||
if active_rules:
|
||||
result[node.node_id] = _node_view(active_rules, node.node_id, canonical_key, node.title)
|
||||
return result
|
||||
|
||||
|
||||
def _node_view(
|
||||
|
|
|
|||
|
|
@ -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),
|
||||
|
|
|
|||
|
|
@ -3,11 +3,18 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import base64
|
||||
import binascii
|
||||
from dataclasses import replace
|
||||
from io import BytesIO
|
||||
from pathlib import PurePosixPath
|
||||
from typing import Any, Callable
|
||||
from uuid import uuid4
|
||||
|
||||
try:
|
||||
from PIL import Image
|
||||
except ImportError: # pragma: no cover - AstrBot ships Pillow; standalone users may not.
|
||||
Image = None # type: ignore[assignment]
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.dto.material_dto import Material, MaterialCategory, MaterialListResult
|
||||
from backend.services.material_normalization_service import (
|
||||
|
|
@ -34,6 +41,9 @@ from engines.vision_provider import (
|
|||
|
||||
MATERIAL_STATUSES = {"pending_upload", "pending_recognition", "pending_confirm", "confirmed", "archived"}
|
||||
MAX_UPLOAD_BYTES = 20 * 1024 * 1024
|
||||
VISION_INPUT_MAX_EDGE = 1536
|
||||
VISION_INPUT_REENCODE_THRESHOLD = 1024 * 1024
|
||||
VISION_INPUT_JPEG_QUALITY = 88
|
||||
UPLOAD_SIZE_FIELDS = ("size_bytes", "content_length", "file_size", "size")
|
||||
UPLOAD_ALLOWED_EXTENSIONS = set(SUPPORTED_IMAGE_EXTENSIONS)
|
||||
UPLOAD_ALLOWED_CONTENT_TYPES = {
|
||||
|
|
@ -273,19 +283,75 @@ class MaterialService:
|
|||
return self.update_material(material_id, {"recognition_status": "archived"}, ctx)
|
||||
|
||||
def upload_material(self, payload: dict[str, Any], ctx: RequestContext) -> Material:
|
||||
image_uri = str(payload.get("image_uri") or "").strip()
|
||||
if not image_uri:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "image_uri is required", {"field": "image_uri"})
|
||||
filename = _normalize_upload_filename(payload.get("filename"))
|
||||
content_type = _normalize_upload_content_type(payload.get("content_type"))
|
||||
declared_size = _normalize_upload_size(payload)
|
||||
upload_metadata = _normalize_upload_metadata(payload.get("metadata"))
|
||||
image_data_url = str(payload.get("image_data_url") or "").strip()
|
||||
image_uri = str(payload.get("image_uri") or "").strip()
|
||||
persisted_path = None
|
||||
if image_data_url:
|
||||
content, detected_content_type, detected_extension = _decode_upload_data_url(image_data_url)
|
||||
if len(content) > MAX_UPLOAD_BYTES:
|
||||
raise ApiError(
|
||||
ErrorCode.UPLOAD_TOO_LARGE,
|
||||
"upload exceeds maximum allowed size",
|
||||
{"size_bytes": len(content), "max_size_bytes": MAX_UPLOAD_BYTES},
|
||||
)
|
||||
if declared_size is not None and declared_size != len(content):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"declared upload size does not match image bytes",
|
||||
{"declared_size_bytes": declared_size, "actual_size_bytes": len(content)},
|
||||
)
|
||||
if content_type and content_type != detected_content_type:
|
||||
raise ApiError(
|
||||
ErrorCode.UNSUPPORTED_FILE_TYPE,
|
||||
"upload content type does not match image bytes",
|
||||
{"content_type": content_type, "detected_content_type": detected_content_type},
|
||||
)
|
||||
filename_extension = _suffix(filename)
|
||||
if filename_extension and filename_extension not in UPLOAD_ALLOWED_CONTENT_TYPES[detected_content_type]:
|
||||
raise ApiError(
|
||||
ErrorCode.UNSUPPORTED_FILE_TYPE,
|
||||
"upload filename extension does not match image bytes",
|
||||
{"filename": filename, "detected_content_type": detected_content_type},
|
||||
)
|
||||
content_type = detected_content_type
|
||||
declared_size = len(content)
|
||||
safe_filename = filename or f"material-{uuid4().hex}{detected_extension}"
|
||||
stored_filename = f"{uuid4().hex}-{PurePosixPath(safe_filename).stem}{detected_extension}"
|
||||
image_uri = f"storage://materials/uploads/{stored_filename}"
|
||||
persisted_path = self.storage.resolve_uri(image_uri)
|
||||
try:
|
||||
persisted_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
temporary_path = persisted_path.with_suffix(f"{persisted_path.suffix}.part")
|
||||
temporary_path.write_bytes(content)
|
||||
temporary_path.replace(persisted_path)
|
||||
except OSError as exc:
|
||||
raise ApiError(
|
||||
ErrorCode.STORAGE_WRITE_FAILED,
|
||||
"material image could not be stored",
|
||||
{"uri": image_uri, "exception_type": exc.__class__.__name__},
|
||||
) from None
|
||||
upload_metadata["browser_upload"] = {
|
||||
"source": str(payload.get("upload_source") or "file"),
|
||||
"persisted": True,
|
||||
}
|
||||
filename = safe_filename
|
||||
if not image_uri:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"image_uri or image_data_url is required",
|
||||
{"fields": ["image_uri", "image_data_url"]},
|
||||
)
|
||||
recognition_route = _optional_text(payload.get("recognition_model_route"))
|
||||
if recognition_route:
|
||||
upload_metadata["recognition_model_route"] = recognition_route
|
||||
extension = _validate_upload_file_type(filename=filename, image_uri=image_uri, content_type=content_type)
|
||||
display_name = str(payload.get("display_name") or _display_name_from_filename(filename) or "Uploaded material").strip()
|
||||
thumbnail_uri = str(payload.get("thumbnail_uri") or image_uri).strip()
|
||||
try:
|
||||
return self.create_material(
|
||||
{
|
||||
"category_id": payload.get("category_id"),
|
||||
|
|
@ -301,12 +367,19 @@ class MaterialService:
|
|||
"extension": extension,
|
||||
"size_bytes": declared_size,
|
||||
"max_size_bytes": MAX_UPLOAD_BYTES,
|
||||
"storage_reference_only": True,
|
||||
"storage_reference_only": not bool(image_data_url),
|
||||
}
|
||||
},
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
except Exception:
|
||||
if persisted_path is not None:
|
||||
try:
|
||||
persisted_path.unlink(missing_ok=True)
|
||||
except OSError:
|
||||
pass
|
||||
raise
|
||||
|
||||
def recognize_material(self, material_id: str, ctx: RequestContext) -> Material:
|
||||
material = self.get_material(material_id)
|
||||
|
|
@ -457,13 +530,12 @@ class MaterialService:
|
|||
def _recognition_instruction(self, material: Material) -> str:
|
||||
base = str(self.recognition_prompt_config.get("default_instruction") or "").strip()
|
||||
requirements = self.recognition_prompt_config.get("category_requirements")
|
||||
if not isinstance(requirements, list):
|
||||
return base
|
||||
category_name = ""
|
||||
if material.category_id:
|
||||
category = self.repository.get_category(material.category_id)
|
||||
category_name = category.name if category else ""
|
||||
additions: list[str] = []
|
||||
if isinstance(requirements, list):
|
||||
for item in requirements:
|
||||
if not isinstance(item, dict) or item.get("enabled") is False:
|
||||
continue
|
||||
|
|
@ -471,6 +543,12 @@ class MaterialService:
|
|||
requirement = str(item.get("requirement") or "").strip()
|
||||
if requirement and (not category or category in {str(material.category_id or "").lower(), category_name.lower()}):
|
||||
additions.append(requirement)
|
||||
body_slot = str(material.metadata.get("body_slot") or "").strip()
|
||||
if body_slot:
|
||||
additions.append(f"The user marked this material for body slot: {body_slot}. Verify and preserve that slot in the structured result.")
|
||||
upload_note = str(material.metadata.get("note") or "").strip()
|
||||
if upload_note:
|
||||
additions.append(f"Additional user recognition requirement: {upload_note}")
|
||||
return "\n\n".join([part for part in [base, *additions] if part])
|
||||
|
||||
def _vision_provider(self, material: Material, route_name: str) -> VisionProvider | None:
|
||||
|
|
@ -690,8 +768,57 @@ def _storage_image_data_url(
|
|||
"detected_content_type": content_type,
|
||||
},
|
||||
)
|
||||
encoded = base64.b64encode(content).decode("ascii")
|
||||
return f"data:{content_type};base64,{encoded}", content_type, len(content)
|
||||
vision_content, vision_content_type = _optimized_vision_input(content, content_type)
|
||||
encoded = base64.b64encode(vision_content).decode("ascii")
|
||||
return f"data:{vision_content_type};base64,{encoded}", vision_content_type, len(vision_content)
|
||||
|
||||
|
||||
def _optimized_vision_input(content: bytes, content_type: str) -> tuple[bytes, str]:
|
||||
"""Bound multimodal payload size without modifying the stored original.
|
||||
|
||||
Chat-completion providers receive images inline as base64. Multi-megabyte
|
||||
PNG screenshots become substantially larger on the wire and can time out
|
||||
at reverse proxies even when their visual resolution is modest. AstrBot
|
||||
already depends on Pillow, so large or over-dimensioned inputs are reduced
|
||||
to a high-quality JPEG used only for recognition.
|
||||
"""
|
||||
|
||||
if Image is None:
|
||||
return content, content_type
|
||||
try:
|
||||
with Image.open(BytesIO(content)) as source:
|
||||
width, height = source.size
|
||||
needs_resize = max(width, height) > VISION_INPUT_MAX_EDGE
|
||||
needs_reencode = len(content) > VISION_INPUT_REENCODE_THRESHOLD
|
||||
if not needs_resize and not needs_reencode:
|
||||
return content, content_type
|
||||
|
||||
source.seek(0)
|
||||
frame = source.convert("RGBA")
|
||||
if needs_resize:
|
||||
frame.thumbnail(
|
||||
(VISION_INPUT_MAX_EDGE, VISION_INPUT_MAX_EDGE),
|
||||
Image.Resampling.LANCZOS,
|
||||
)
|
||||
background = Image.new("RGB", frame.size, (255, 255, 255))
|
||||
background.paste(frame, mask=frame.getchannel("A"))
|
||||
output = BytesIO()
|
||||
background.save(
|
||||
output,
|
||||
format="JPEG",
|
||||
quality=VISION_INPUT_JPEG_QUALITY,
|
||||
optimize=True,
|
||||
progressive=True,
|
||||
)
|
||||
optimized = output.getvalue()
|
||||
if not optimized or len(optimized) >= len(content):
|
||||
return content, content_type
|
||||
return optimized, "image/jpeg"
|
||||
except Exception:
|
||||
# Signature validation above already proved that the original is a
|
||||
# supported image. Optimization is best-effort and must never make a
|
||||
# previously valid material unusable.
|
||||
return content, content_type
|
||||
|
||||
|
||||
def _image_content_type(content: bytes) -> str | None:
|
||||
|
|
@ -733,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
|
||||
|
||||
|
||||
|
|
@ -781,6 +915,51 @@ def _normalize_upload_filename(value: Any) -> str | None:
|
|||
return filename
|
||||
|
||||
|
||||
def _decode_upload_data_url(value: str) -> tuple[bytes, str, str]:
|
||||
if not value.startswith("data:") or ";base64," not in value:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"image_data_url must be a base64 data URL",
|
||||
{"field": "image_data_url"},
|
||||
)
|
||||
header, encoded = value.split(",", 1)
|
||||
declared_content_type = header[5:].split(";", 1)[0].strip().lower()
|
||||
if declared_content_type not in UPLOAD_ALLOWED_CONTENT_TYPES:
|
||||
raise ApiError(
|
||||
ErrorCode.UNSUPPORTED_FILE_TYPE,
|
||||
"unsupported upload content type",
|
||||
{"content_type": declared_content_type, "allowed": sorted(UPLOAD_ALLOWED_CONTENT_TYPES)},
|
||||
)
|
||||
if len(encoded) > ((MAX_UPLOAD_BYTES + 2) // 3) * 4 + 8:
|
||||
raise ApiError(
|
||||
ErrorCode.UPLOAD_TOO_LARGE,
|
||||
"upload exceeds maximum allowed size",
|
||||
{"max_size_bytes": MAX_UPLOAD_BYTES},
|
||||
)
|
||||
try:
|
||||
content = base64.b64decode(encoded, validate=True)
|
||||
except (binascii.Error, ValueError):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"image_data_url contains invalid base64 data",
|
||||
{"field": "image_data_url"},
|
||||
) from None
|
||||
detected = detect_image_format(content)
|
||||
if detected is None:
|
||||
raise ApiError(
|
||||
ErrorCode.UNSUPPORTED_FILE_TYPE,
|
||||
"uploaded bytes do not match a supported image format",
|
||||
{"allowed": sorted(UPLOAD_ALLOWED_CONTENT_TYPES)},
|
||||
)
|
||||
if declared_content_type != detected.mime_type:
|
||||
raise ApiError(
|
||||
ErrorCode.UNSUPPORTED_FILE_TYPE,
|
||||
"data URL content type does not match image bytes",
|
||||
{"content_type": declared_content_type, "detected_content_type": detected.mime_type},
|
||||
)
|
||||
return content, detected.mime_type, detected.extension
|
||||
|
||||
|
||||
def _normalize_upload_content_type(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -9,12 +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:
|
||||
|
|
@ -31,13 +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]
|
||||
fallback_enabled = bool(route_context.get("allow_provider_fallback", True))
|
||||
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 = 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),
|
||||
|
|
@ -63,6 +81,7 @@ class ModelProviderRuntime:
|
|||
secret = resolve_secret_value(
|
||||
env_var=provider.get("api_key_env_var"),
|
||||
secret_ref=provider.get("api_key_secret_ref"),
|
||||
plaintext=_saved_provider_api_key(str(resolved.get("provider_id") or "")),
|
||||
secret_store=self.secret_store,
|
||||
)
|
||||
secret_configured = bool(provider.get("api_key_env_var") or provider.get("api_key_secret_ref"))
|
||||
|
|
@ -89,11 +108,26 @@ class ModelProviderRuntime:
|
|||
),
|
||||
)
|
||||
)
|
||||
|
||||
def vision_provider_for_route(self, route_context: dict[str, Any]) -> VisionProvider | None:
|
||||
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.
|
||||
from core.astrbot_provider_bridge import astrbot_vision_provider
|
||||
|
||||
return astrbot_vision_provider(route_name)
|
||||
resolved = self.model_provider_service.resolve_route(route_name, modality="vision_recognition")
|
||||
if not resolved:
|
||||
return None
|
||||
|
|
@ -103,6 +137,7 @@ class ModelProviderRuntime:
|
|||
secret = resolve_secret_value(
|
||||
env_var=provider.get("api_key_env_var"),
|
||||
secret_ref=provider.get("api_key_secret_ref"),
|
||||
plaintext=_saved_provider_api_key(str(resolved.get("provider_id") or "")),
|
||||
secret_store=self.secret_store,
|
||||
)
|
||||
secret_configured = bool(provider.get("api_key_env_var") or provider.get("api_key_secret_ref"))
|
||||
|
|
@ -132,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
|
||||
|
|
@ -141,6 +202,7 @@ class ModelProviderRuntime:
|
|||
secret = resolve_secret_value(
|
||||
env_var=provider.get("api_key_env_var"),
|
||||
secret_ref=provider.get("api_key_secret_ref"),
|
||||
plaintext=_saved_provider_api_key(str(resolved.get("provider_id") or "")),
|
||||
secret_store=self.secret_store,
|
||||
)
|
||||
if (provider.get("api_key_env_var") or provider.get("api_key_secret_ref")) and not secret.value:
|
||||
|
|
@ -189,11 +251,47 @@ 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
|
||||
provider = resolved.get("provider") or {}
|
||||
secret = resolve_secret_value(env_var=provider.get("api_key_env_var"), secret_ref=provider.get("api_key_secret_ref"), secret_store=self.secret_store)
|
||||
secret = resolve_secret_value(
|
||||
env_var=provider.get("api_key_env_var"),
|
||||
secret_ref=provider.get("api_key_secret_ref"),
|
||||
plaintext=_saved_provider_api_key(str(resolved.get("provider_id") or "")),
|
||||
secret_store=self.secret_store,
|
||||
)
|
||||
if (provider.get("api_key_env_var") or provider.get("api_key_secret_ref")) and not secret.value:
|
||||
return None
|
||||
base_url = str(provider.get("base_url") or "").rstrip("/")
|
||||
|
|
@ -248,6 +346,29 @@ class ModelProviderRuntime:
|
|||
return review
|
||||
|
||||
|
||||
def _saved_provider_api_key(provider_id: str) -> str | None:
|
||||
"""Read the UI-saved adapter credential without copying it into the DB."""
|
||||
|
||||
if not provider_id:
|
||||
return None
|
||||
config = load_config_payload()
|
||||
generation = config.get("generation") if isinstance(config.get("generation"), dict) else {}
|
||||
adapters = generation.get("adapters") if isinstance(generation.get("adapters"), list) else []
|
||||
candidates = adapters
|
||||
if not candidates and isinstance(generation.get("provider"), dict):
|
||||
candidates = [generation["provider"]]
|
||||
for candidate in candidates:
|
||||
if not isinstance(candidate, dict):
|
||||
continue
|
||||
candidate_id = str(candidate.get("id") or candidate.get("provider_id") or "").strip()
|
||||
if candidate_id != provider_id:
|
||||
continue
|
||||
value = str(candidate.get("api_key_plaintext") or "").strip()
|
||||
if value and value != "***MASKED***":
|
||||
return value
|
||||
return None
|
||||
|
||||
|
||||
def _configured_bool(*values: Any, default: bool) -> bool:
|
||||
for value in values:
|
||||
if value is 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,7 +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:
|
||||
return self.repository.resolve_route(route_name, 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:
|
||||
|
|
|
|||
|
|
@ -5,6 +5,11 @@ from __future__ import annotations
|
|||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.services.branch_lineage_service import (
|
||||
BranchLineageResolver,
|
||||
ResolvedNodeSource,
|
||||
workflow_metadata_for_branch,
|
||||
)
|
||||
from backend.services.material_binding_schema_service import material_bindings_for_node
|
||||
from backend.services.image_service import ImageService
|
||||
from backend.services.node_injection_template_service import build_node_injection_contract
|
||||
|
|
@ -12,9 +17,6 @@ from backend.services.prompt_plan_schema_service import prompt_plan_for_node
|
|||
from backend.services.workflow_service import WorkflowService
|
||||
|
||||
|
||||
CONTEXT_STATUSES = {"completed", "selected"}
|
||||
|
||||
|
||||
class NodeDependencyContextService:
|
||||
def __init__(
|
||||
self,
|
||||
|
|
@ -27,40 +29,54 @@ class NodeDependencyContextService:
|
|||
def get_context(self, node_id: str, *, include_draft: bool = False) -> dict[str, Any]:
|
||||
target = self.workflow_service.get_node(node_id)
|
||||
detail = self.workflow_service.get_workflow(target.workflow_id)
|
||||
dependencies = []
|
||||
for node in sorted(detail.nodes, key=lambda item: item.sort_order):
|
||||
if node.branch_name != target.branch_name or node.sort_order >= target.sort_order:
|
||||
continue
|
||||
if not include_draft and node.status not in CONTEXT_STATUSES:
|
||||
continue
|
||||
dependencies.append(self._node_context(node))
|
||||
resolver = BranchLineageResolver(self.workflow_service.workflow_repository)
|
||||
resolved_dependencies = resolver.resolve_dependencies(target, include_draft=include_draft)
|
||||
dependencies = [self._node_context(item) for item in resolved_dependencies]
|
||||
prompt_parts = [item["prompt"] for item in dependencies if item.get("prompt")]
|
||||
selected_image_ids = [
|
||||
item["selected_image"]["image_id"]
|
||||
for item in dependencies
|
||||
if isinstance(item.get("selected_image"), dict)
|
||||
]
|
||||
branch_nodes = [node for node in detail.nodes if node.branch_name == target.branch_name]
|
||||
branch_metadata = workflow_metadata_for_branch(detail.workflow.metadata, target.branch_name)
|
||||
inherited_seed = resolver.resolve_parent_seed(target)
|
||||
material_bindings = material_bindings_for_node(branch_metadata, target, branch_nodes)
|
||||
result = {
|
||||
"workflow_id": target.workflow_id,
|
||||
"node_id": target.node_id,
|
||||
"branch_name": target.branch_name,
|
||||
"branch_lineage": list(resolver.branch_chain(target.workflow_id, target.branch_name)),
|
||||
"dependency_count": len(dependencies),
|
||||
"dependencies": dependencies,
|
||||
"prompt_context": ", ".join(prompt_parts),
|
||||
"selected_image_ids": selected_image_ids,
|
||||
"material_bindings": material_bindings_for_node(detail.workflow.metadata, target, detail.nodes),
|
||||
"prompt_plan": prompt_plan_for_node(detail.workflow.metadata, target, detail.nodes),
|
||||
"material_bindings": material_bindings,
|
||||
"temporary_references": (
|
||||
material_bindings.get("temporary_references", [])
|
||||
if isinstance(material_bindings.get("temporary_references"), list)
|
||||
else []
|
||||
),
|
||||
"navigation_context": (
|
||||
material_bindings.get("navigation_context", {})
|
||||
if isinstance(material_bindings.get("navigation_context"), dict)
|
||||
else {}
|
||||
),
|
||||
"prompt_plan": prompt_plan_for_node(branch_metadata, target, branch_nodes),
|
||||
"inherited_seed": self._node_context(inherited_seed) if inherited_seed else None,
|
||||
}
|
||||
result["injection_template"] = build_node_injection_contract(
|
||||
target,
|
||||
dependency_context=result,
|
||||
material_bindings=result["material_bindings"],
|
||||
prompt_plan=result["prompt_plan"],
|
||||
stage_inputs=_stage_inputs_for_node(detail.workflow.metadata, target.node_id),
|
||||
stage_inputs=_stage_inputs_for_node(branch_metadata, target.node_id),
|
||||
)
|
||||
return result
|
||||
|
||||
def _node_context(self, node) -> dict[str, Any]:
|
||||
def _node_context(self, resolved: ResolvedNodeSource) -> dict[str, Any]:
|
||||
logical = resolved.logical_node
|
||||
node = resolved.source_node
|
||||
selected_image = None
|
||||
if node.selected_image_id and self.image_service:
|
||||
try:
|
||||
|
|
@ -76,16 +92,21 @@ class NodeDependencyContextService:
|
|||
if exc.error_code != ErrorCode.RESOURCE_NOT_FOUND:
|
||||
raise
|
||||
return {
|
||||
"node_id": node.node_id,
|
||||
"node_key": node.node_key,
|
||||
"title": node.title,
|
||||
"node_id": logical.node_id,
|
||||
"node_key": logical.node_key,
|
||||
"title": logical.title,
|
||||
"branch_name": logical.branch_name,
|
||||
"status": node.status,
|
||||
"sort_order": node.sort_order,
|
||||
"sort_order": logical.sort_order,
|
||||
"prompt": node.prompt,
|
||||
"negative_prompt": node.negative_prompt,
|
||||
"selected_image_id": node.selected_image_id,
|
||||
"selected_image": selected_image,
|
||||
"metadata": node.metadata,
|
||||
"inherited": resolved.inherited,
|
||||
"source_branch": resolved.source_branch,
|
||||
"source_node_id": resolved.source_node_id,
|
||||
"lineage": list(resolved.lineage),
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from copy import deepcopy
|
|||
from typing import Any
|
||||
|
||||
from backend.dto.workflow_dto import WorkflowNode
|
||||
from engines.character_flow import canonical_gradual_node_key
|
||||
|
||||
|
||||
NODE_INJECTION_TEMPLATE_VERSION = "2026-07-09.node-injection-schema.v1"
|
||||
|
|
@ -15,15 +16,20 @@ NODE_KEY_TEMPLATE_KINDS: dict[str, str] = {
|
|||
"prompt_plan": "prompt_plan",
|
||||
"material_bind": "material_bind",
|
||||
"headshot": "portrait_generation",
|
||||
"half_body": "halfbody_generation",
|
||||
"upper_body": "halfbody_generation",
|
||||
"lower_body": "fullbody_generation",
|
||||
"full_body": "fullbody_generation",
|
||||
"expression_sheet": "sheet_generation",
|
||||
"pose_sheet": "pose_generation",
|
||||
"turnaround": "sheet_generation",
|
||||
"design_sheet": "pose_generation",
|
||||
"style_lock": "hires_fix",
|
||||
"final_pack": "archive",
|
||||
"profile_card": "archive",
|
||||
"fast_profile": "persona_split",
|
||||
"fast_candidates": "portrait_generation",
|
||||
"fast_final": "archive",
|
||||
"quick_avatar": "portrait_generation",
|
||||
"quick_bust": "halfbody_generation",
|
||||
"quick_stand": "fullbody_generation",
|
||||
"quick_sheet": "sheet_generation",
|
||||
"questionnaire": "profile_import",
|
||||
"preview": "prompt_plan",
|
||||
"virtual_single_image": "single_image_generation",
|
||||
|
|
@ -84,7 +90,7 @@ EXPECTED_RESPONSE_SCHEMAS: dict[str, dict[str, Any]] = {
|
|||
"matrix": [
|
||||
{
|
||||
"target_id": "portrait | bust | fullbody | sheet | all",
|
||||
"target_node_keys": ["headshot | half_body | full_body | expression_sheet | pose_sheet"],
|
||||
"target_node_keys": ["headshot | upper_body | lower_body | full_body | turnaround | design_sheet"],
|
||||
"positive_patch": "string",
|
||||
"negative_patch": "string?",
|
||||
"weight_suggestions": [{"label": "string", "value": "number?", "reason": "string"}],
|
||||
|
|
@ -114,7 +120,7 @@ EXPECTED_RESPONSE_SCHEMAS: dict[str, dict[str, Any]] = {
|
|||
{
|
||||
"material_category": "style | composition | clothing | socks | shoes | accessory | other",
|
||||
"bind_to_stages": [{"stage_id": "string", "stage_title": "string"}],
|
||||
"target_node_keys": ["headshot | half_body | full_body | expression_sheet | pose_sheet | style_lock"],
|
||||
"target_node_keys": ["headshot | upper_body | lower_body | full_body | turnaround | design_sheet | style_lock"],
|
||||
"inheritance_rule": "string",
|
||||
"reference_strength": "string?",
|
||||
"use_reference": "boolean",
|
||||
|
|
@ -256,7 +262,10 @@ def template_kind_for_node(node: WorkflowNode, requested_kind: str | None = None
|
|||
normalized = str(requested_kind or "").strip()
|
||||
if normalized in EXPECTED_RESPONSE_SCHEMAS:
|
||||
return normalized
|
||||
return NODE_KEY_TEMPLATE_KINDS.get(node.node_key, "generic_workflow_node")
|
||||
return NODE_KEY_TEMPLATE_KINDS.get(
|
||||
canonical_gradual_node_key(node.node_key),
|
||||
"generic_workflow_node",
|
||||
)
|
||||
|
||||
|
||||
def build_node_injection_contract(
|
||||
|
|
|
|||
|
|
@ -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():
|
||||
|
|
|
|||
|
|
@ -0,0 +1,368 @@
|
|||
"""Finalize the existing detailed workflow into an immutable profile card."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.dto.profile_card_dto import CharacterProfileCard, CharacterProfileCardAsset
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from database.repositories.image_repository import ImageRepository
|
||||
from database.repositories.profile_card_repository import ProfileCardRepository
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
from engines.character_flow import canonical_gradual_node_key
|
||||
|
||||
|
||||
PROFILE_CARD_ASSET_ROLES = ("avatar", "persona_image", "standing", "pv")
|
||||
PROFILE_CARD_ROLE_STAGES: dict[str, frozenset[str]] = {
|
||||
"avatar": frozenset({"headshot"}),
|
||||
"persona_image": frozenset({"turnaround", "design_sheet"}),
|
||||
"standing": frozenset({"full_body"}),
|
||||
"pv": frozenset({"style_lock"}),
|
||||
}
|
||||
LEGACY_CARD_PREFIX = "legacy_card_"
|
||||
|
||||
|
||||
class ProfileCardService:
|
||||
def __init__(
|
||||
self,
|
||||
cards: ProfileCardRepository,
|
||||
profiles: ProfileRepository,
|
||||
workflows: WorkflowRepository,
|
||||
images: ImageRepository,
|
||||
audit_service: AuditService,
|
||||
):
|
||||
self.cards = cards
|
||||
self.profiles = profiles
|
||||
self.workflows = workflows
|
||||
self.images = images
|
||||
self.audit_service = audit_service
|
||||
|
||||
def finalize(self, workflow_id: str, payload: dict[str, Any], ctx: RequestContext) -> CharacterProfileCard:
|
||||
idempotency_key = _required_text(payload.get("idempotency_key"), "idempotency_key")
|
||||
existing = self.cards.get_by_idempotency_key(idempotency_key)
|
||||
if existing is not None:
|
||||
self._require_owner(existing.profile_id, ctx)
|
||||
if existing.workflow_id != workflow_id:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"profile-card idempotency key belongs to another workflow",
|
||||
{
|
||||
"idempotency_key": idempotency_key,
|
||||
"workflow_id": workflow_id,
|
||||
"existing_workflow_id": existing.workflow_id,
|
||||
},
|
||||
)
|
||||
return existing
|
||||
|
||||
workflow = self.workflows.get_workflow(workflow_id)
|
||||
if workflow is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "workflow not found", {"workflow_id": workflow_id})
|
||||
if workflow.workflow_type != "character_gradual":
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"only a detailed character workflow can create a profile card",
|
||||
{"workflow_id": workflow_id, "workflow_type": workflow.workflow_type},
|
||||
)
|
||||
if workflow.status in {"cancelled", "archived"}:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow cannot be finalized from its current status",
|
||||
{"workflow_id": workflow_id, "status": workflow.status},
|
||||
)
|
||||
profile = self._require_owner(workflow.profile_id, ctx)
|
||||
active_branch = workflow.current_branch or "main"
|
||||
branch_nodes = sorted(
|
||||
self.workflows.list_nodes(workflow_id, branch_name=active_branch),
|
||||
key=lambda node: (node.sort_order, node.node_id),
|
||||
)
|
||||
profile_card_node = next(
|
||||
(
|
||||
node
|
||||
for node in reversed(branch_nodes)
|
||||
if canonical_gradual_node_key(node.node_key) == "profile_card"
|
||||
),
|
||||
None,
|
||||
)
|
||||
if profile_card_node is None:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow does not contain a profile-card node",
|
||||
{"workflow_id": workflow_id, "branch_name": active_branch},
|
||||
)
|
||||
incomplete = [
|
||||
node.node_key
|
||||
for node in branch_nodes
|
||||
if node.sort_order < profile_card_node.sort_order
|
||||
and node.status not in {"selected", "skipped", "completed"}
|
||||
]
|
||||
if incomplete:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow stages must be completed before creating a profile card",
|
||||
{"workflow_id": workflow_id, "incomplete_node_keys": incomplete},
|
||||
)
|
||||
|
||||
persona_document = _required_text(payload.get("persona_document"), "persona_document")
|
||||
assets_by_role = _normalize_assets(payload.get("assets"))
|
||||
resolved_assets: list[dict[str, Any]] = []
|
||||
avatar_image_id = ""
|
||||
avatar_image_uri = ""
|
||||
for role in PROFILE_CARD_ASSET_ROLES:
|
||||
image_id = assets_by_role[role]
|
||||
image = self.images.get(image_id)
|
||||
if image is None or image.workflow_id != workflow_id:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"profile-card asset does not belong to this workflow",
|
||||
{"asset_role": role, "image_id": image_id, "workflow_id": workflow_id},
|
||||
)
|
||||
if image.is_archived or _image_is_restricted(image.metadata):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"profile-card asset is archived or restricted",
|
||||
{"asset_role": role, "image_id": image_id},
|
||||
)
|
||||
source_node = self.workflows.get_node(image.node_id) if image.node_id else None
|
||||
if (
|
||||
source_node is None
|
||||
or source_node.workflow_id != workflow_id
|
||||
or source_node.branch_name != active_branch
|
||||
):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"profile-card asset has no valid node in the active branch",
|
||||
{
|
||||
"asset_role": role,
|
||||
"image_id": image_id,
|
||||
"node_id": image.node_id,
|
||||
"active_branch": active_branch,
|
||||
},
|
||||
)
|
||||
if not image.is_selected and source_node.selected_image_id != image.image_id:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"profile-card assets must be confirmed workflow candidates",
|
||||
{"asset_role": role, "image_id": image_id, "node_key": source_node.node_key},
|
||||
)
|
||||
source_stage = _profile_card_stage_key(source_node.node_key)
|
||||
allowed_stages = PROFILE_CARD_ROLE_STAGES[role]
|
||||
if source_stage not in allowed_stages:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"profile-card asset comes from an incompatible workflow stage",
|
||||
{
|
||||
"asset_role": role,
|
||||
"image_id": image_id,
|
||||
"source_node_key": source_node.node_key,
|
||||
"source_stage": source_stage,
|
||||
"allowed_stages": sorted(allowed_stages),
|
||||
},
|
||||
)
|
||||
resolved_assets.append(
|
||||
{
|
||||
"asset_id": f"cardasset_{uuid4().hex}",
|
||||
"asset_role": role,
|
||||
"image_id": image.image_id,
|
||||
"image_uri": image.image_uri,
|
||||
"metadata": {
|
||||
"source_node_id": source_node.node_id,
|
||||
"source_node_key": source_node.node_key,
|
||||
"source_stage": source_stage,
|
||||
"source_branch": source_node.branch_name,
|
||||
"model_route": image.model_route,
|
||||
"model_used": image.model_used,
|
||||
},
|
||||
}
|
||||
)
|
||||
if role == "avatar":
|
||||
avatar_image_id = image.image_id
|
||||
avatar_image_uri = image.image_uri
|
||||
|
||||
card_id = str(payload.get("card_id") or f"profilecard_{uuid4().hex}")
|
||||
card = self.cards.finalize(
|
||||
{
|
||||
"card_id": card_id,
|
||||
"profile_id": profile.profile_id,
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"branch_name": active_branch,
|
||||
"status": "sealed",
|
||||
"persona_document": persona_document,
|
||||
"idempotency_key": idempotency_key,
|
||||
"metadata": {
|
||||
**(payload.get("metadata") if isinstance(payload.get("metadata"), dict) else {}),
|
||||
"schema_version": "character_profile_card_v1",
|
||||
"source_workflow_status": workflow.status,
|
||||
},
|
||||
"assets": resolved_assets,
|
||||
"avatar_image_id": avatar_image_id,
|
||||
"avatar_image_uri": avatar_image_uri,
|
||||
"profile_card_node_id": profile_card_node.node_id,
|
||||
"history_id": f"hist_{uuid4().hex}",
|
||||
"title_prefix": f"{profile.name} / 角色档案卡",
|
||||
}
|
||||
)
|
||||
self.audit_service.record(
|
||||
ctx=ctx,
|
||||
action="finalize_profile_card",
|
||||
target_type="character_profile_card",
|
||||
target_id=card.card_id,
|
||||
after_snapshot=card.to_dict(),
|
||||
metadata={"workflow_id": workflow_id, "profile_id": profile.profile_id},
|
||||
)
|
||||
return card
|
||||
|
||||
def get(self, card_id: str, ctx: RequestContext) -> CharacterProfileCard:
|
||||
card = self.cards.get(card_id)
|
||||
if card is None and card_id.startswith(LEGACY_CARD_PREFIX):
|
||||
workflow = self.workflows.get_workflow(card_id[len(LEGACY_CARD_PREFIX) :])
|
||||
if workflow is not None:
|
||||
self._require_owner(workflow.profile_id, ctx)
|
||||
card = self._legacy_card(workflow)
|
||||
if card is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "profile card not found", {"card_id": card_id})
|
||||
self._require_owner(card.profile_id, ctx)
|
||||
return card
|
||||
|
||||
def list_for_profile(self, profile_id: str, ctx: RequestContext) -> list[CharacterProfileCard]:
|
||||
self._require_owner(profile_id, ctx)
|
||||
cards = self.cards.list_by_profile(profile_id)
|
||||
card_workflow_ids = {card.workflow_id for card in cards}
|
||||
legacy_cards = [
|
||||
card
|
||||
for workflow in self.workflows.list_workflows(profile_id)
|
||||
if workflow.workflow_id not in card_workflow_ids
|
||||
if (card := self._legacy_card(workflow)) is not None
|
||||
]
|
||||
return [*cards, *legacy_cards]
|
||||
|
||||
def _legacy_card(self, workflow) -> CharacterProfileCard | None:
|
||||
metadata = workflow.metadata if isinstance(workflow.metadata, dict) else {}
|
||||
branch_states = metadata.get("branch_state") if isinstance(metadata.get("branch_state"), dict) else {}
|
||||
branch_state = branch_states.get(workflow.current_branch or "main")
|
||||
legacy_metadata = {
|
||||
**metadata,
|
||||
**(branch_state if isinstance(branch_state, dict) else {}),
|
||||
}
|
||||
image_ids = legacy_metadata.get("final_archive_image_ids")
|
||||
if not isinstance(image_ids, list) or not image_ids:
|
||||
return None
|
||||
card_id = f"{LEGACY_CARD_PREFIX}{workflow.workflow_id}"
|
||||
assets: list[CharacterProfileCardAsset] = []
|
||||
used_roles: set[str] = set()
|
||||
for index, image_id in enumerate(image_ids, start=1):
|
||||
image = self.images.get(str(image_id))
|
||||
if image is None or image.workflow_id != workflow.workflow_id:
|
||||
continue
|
||||
node = self.workflows.get_node(image.node_id) if image.node_id else None
|
||||
canonical_key = _profile_card_stage_key(node.node_key) if node is not None else ""
|
||||
preferred_role = {
|
||||
"headshot": "avatar",
|
||||
"full_body": "standing",
|
||||
"turnaround": "persona_image",
|
||||
"design_sheet": "persona_image",
|
||||
"style_lock": "pv",
|
||||
}.get(canonical_key)
|
||||
role = preferred_role if preferred_role and preferred_role not in used_roles else f"legacy_asset_{index:02d}"
|
||||
used_roles.add(role)
|
||||
assets.append(
|
||||
CharacterProfileCardAsset(
|
||||
asset_id=f"legacy_asset_{image.image_id}",
|
||||
card_id=card_id,
|
||||
asset_role=role,
|
||||
image_id=image.image_id,
|
||||
image_uri=image.image_uri,
|
||||
metadata={
|
||||
"legacy": True,
|
||||
"read_only": True,
|
||||
"source_node_id": image.node_id,
|
||||
"source_node_key": canonical_key or None,
|
||||
},
|
||||
created_at=image.created_at,
|
||||
)
|
||||
)
|
||||
if not assets:
|
||||
return None
|
||||
profile = self.profiles.get(workflow.profile_id)
|
||||
persona_document = "\n".join(
|
||||
text
|
||||
for text in (
|
||||
getattr(profile, "description", None),
|
||||
getattr(profile, "base_prompt", None),
|
||||
getattr(profile, "appearance_prompt", None),
|
||||
)
|
||||
if str(text or "").strip()
|
||||
)
|
||||
timestamp = str(legacy_metadata.get("final_archive_completed_at") or workflow.updated_at)
|
||||
return CharacterProfileCard(
|
||||
card_id=card_id,
|
||||
profile_id=workflow.profile_id,
|
||||
workflow_id=workflow.workflow_id,
|
||||
branch_name=workflow.current_branch or "main",
|
||||
version=0,
|
||||
status="legacy_read_only",
|
||||
persona_document=persona_document,
|
||||
idempotency_key=None,
|
||||
metadata={
|
||||
"legacy": True,
|
||||
"read_only": True,
|
||||
"schema_version": legacy_metadata.get("final_archive_schema_version"),
|
||||
"history_id": legacy_metadata.get("final_archive_history_id"),
|
||||
},
|
||||
created_at=timestamp,
|
||||
updated_at=timestamp,
|
||||
assets=tuple(assets),
|
||||
)
|
||||
|
||||
def _require_owner(self, profile_id: str, ctx: RequestContext):
|
||||
profile = self.profiles.get(profile_id)
|
||||
if profile is None or profile.status == "deleted":
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "profile not found", {"profile_id": profile_id})
|
||||
if not (
|
||||
ctx.has_permission("can_manage_all_profiles")
|
||||
or ctx.has_permission("*")
|
||||
or (ctx.actor_id and ctx.actor_id == profile.owner_user_id)
|
||||
):
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "profile not found", {"profile_id": profile_id})
|
||||
return profile
|
||||
|
||||
|
||||
def _required_text(value: Any, field: str) -> str:
|
||||
text = str(value or "").strip()
|
||||
if not text:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, f"{field} is required", {"field": field})
|
||||
return text
|
||||
|
||||
|
||||
def _normalize_assets(value: Any) -> dict[str, str]:
|
||||
if isinstance(value, dict):
|
||||
result = {str(role): str(image_id or "").strip() for role, image_id in value.items()}
|
||||
elif isinstance(value, list):
|
||||
result = {
|
||||
str(item.get("asset_role") or item.get("role") or "").strip(): str(item.get("image_id") or "").strip()
|
||||
for item in value
|
||||
if isinstance(item, dict)
|
||||
}
|
||||
else:
|
||||
result = {}
|
||||
missing = [role for role in PROFILE_CARD_ASSET_ROLES if not result.get(role)]
|
||||
if missing:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"profile-card assets are incomplete",
|
||||
{"required_roles": list(PROFILE_CARD_ASSET_ROLES), "missing_roles": missing},
|
||||
)
|
||||
return {role: result[role] for role in PROFILE_CARD_ASSET_ROLES}
|
||||
|
||||
|
||||
def _profile_card_stage_key(node_key: str | None) -> str:
|
||||
canonical = canonical_gradual_node_key(node_key)
|
||||
return "style_lock" if canonical in {"style_lock", "hires_fix"} else canonical
|
||||
|
||||
|
||||
def _image_is_restricted(metadata: Any) -> bool:
|
||||
source = metadata if isinstance(metadata, dict) else {}
|
||||
return bool(source.get("quarantined") or source.get("gallery_visibility") == "restricted")
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ from copy import deepcopy
|
|||
from typing import Any
|
||||
|
||||
from backend.dto.workflow_dto import WorkflowNode
|
||||
from engines.character_flow import canonical_gradual_node_key
|
||||
|
||||
|
||||
PROMPT_PLAN_SCHEMA_VERSION = "2026-07-09.prompt-plan-schema.v1"
|
||||
|
|
@ -14,17 +15,20 @@ TARGET_NODE_KEYS: dict[str, tuple[str, ...]] = {
|
|||
"portrait": ("headshot", "fast_candidates"),
|
||||
"headshot": ("headshot", "fast_candidates"),
|
||||
"face": ("headshot", "fast_candidates"),
|
||||
"bust": ("half_body",),
|
||||
"halfbody": ("half_body",),
|
||||
"half_body": ("half_body",),
|
||||
"bust": ("upper_body",),
|
||||
"halfbody": ("upper_body",),
|
||||
"half_body": ("upper_body",),
|
||||
"upper_body": ("upper_body",),
|
||||
"lower_body": ("lower_body",),
|
||||
"fullbody": ("full_body",),
|
||||
"full_body": ("full_body",),
|
||||
"body": ("full_body",),
|
||||
"sheet": ("expression_sheet", "pose_sheet"),
|
||||
"design_sheet": ("expression_sheet", "pose_sheet"),
|
||||
"expression_sheet": ("expression_sheet",),
|
||||
"pose_sheet": ("pose_sheet",),
|
||||
"all": ("headshot", "fast_candidates", "half_body", "full_body", "expression_sheet", "pose_sheet"),
|
||||
"sheet": ("turnaround", "design_sheet"),
|
||||
"design_sheet": ("design_sheet",),
|
||||
"turnaround": ("turnaround",),
|
||||
"expression_sheet": ("turnaround",),
|
||||
"pose_sheet": ("design_sheet",),
|
||||
"all": ("headshot", "fast_candidates", "upper_body", "lower_body", "full_body", "turnaround", "design_sheet"),
|
||||
}
|
||||
|
||||
TARGET_TITLES: dict[str, str] = {
|
||||
|
|
@ -97,7 +101,8 @@ def node_prompt_plan_view(normalized: dict[str, Any], node: WorkflowNode) -> dic
|
|||
by_node_key = normalized.get("by_node_key") if isinstance(normalized.get("by_node_key"), dict) else {}
|
||||
node_view = by_node_id.get(node.node_id)
|
||||
if not isinstance(node_view, dict):
|
||||
node_view = _node_view(by_node_key.get(node.node_key, []), node.node_id, node.node_key, node.title)
|
||||
canonical_key = canonical_gradual_node_key(node.node_key)
|
||||
node_view = _node_view(by_node_key.get(canonical_key, []), node.node_id, canonical_key, node.title)
|
||||
return {
|
||||
"schema_version": normalized.get("schema_version", PROMPT_PLAN_SCHEMA_VERSION),
|
||||
"target_prompt_language": normalized.get("target_prompt_language", "en"),
|
||||
|
|
@ -207,7 +212,7 @@ def _index_by_node_key(matrix: list[dict[str, Any]]) -> dict[str, list[dict[str,
|
|||
result: dict[str, list[dict[str, Any]]] = {}
|
||||
for item in matrix:
|
||||
for node_key in item["target_node_keys"]:
|
||||
result.setdefault(node_key, []).append(item)
|
||||
result.setdefault(canonical_gradual_node_key(node_key), []).append(item)
|
||||
return result
|
||||
|
||||
|
||||
|
|
@ -216,11 +221,13 @@ def _index_by_node_id(
|
|||
workflow_nodes: list[WorkflowNode],
|
||||
) -> dict[str, dict[str, Any]]:
|
||||
by_node_key = _index_by_node_key(matrix)
|
||||
return {
|
||||
node.node_id: _node_view(by_node_key.get(node.node_key, []), node.node_id, node.node_key, node.title)
|
||||
for node in workflow_nodes
|
||||
if by_node_key.get(node.node_key)
|
||||
}
|
||||
result: dict[str, dict[str, Any]] = {}
|
||||
for node in workflow_nodes:
|
||||
canonical_key = canonical_gradual_node_key(node.node_key)
|
||||
items = by_node_key.get(canonical_key)
|
||||
if items:
|
||||
result[node.node_id] = _node_view(items, node.node_id, canonical_key, node.title)
|
||||
return result
|
||||
|
||||
|
||||
def _node_view(
|
||||
|
|
@ -252,7 +259,7 @@ def _node_view(
|
|||
def _target_node_keys(item: dict[str, Any], target_id: str) -> list[str]:
|
||||
explicit = _string_list(item.get("target_node_keys") or item.get("node_keys"))
|
||||
if explicit:
|
||||
return explicit
|
||||
return [canonical_gradual_node_key(node_key) for node_key in explicit]
|
||||
return list(TARGET_NODE_KEYS.get(target_id, TARGET_NODE_KEYS.get("all", ())))
|
||||
|
||||
|
||||
|
|
@ -269,6 +276,10 @@ def _canonical_target(value: str) -> str:
|
|||
"bust": "bust",
|
||||
"halfbody": "bust",
|
||||
"half_body": "bust",
|
||||
"upper_body": "bust",
|
||||
"上半身": "bust",
|
||||
"下半身": "lower_body",
|
||||
"lower_body": "lower_body",
|
||||
"全身": "fullbody",
|
||||
"全身节点": "fullbody",
|
||||
"body": "fullbody",
|
||||
|
|
@ -278,6 +289,8 @@ def _canonical_target(value: str) -> str:
|
|||
"设定图节点": "sheet",
|
||||
"sheet": "sheet",
|
||||
"design_sheet": "sheet",
|
||||
"三视图": "turnaround",
|
||||
"turnaround": "turnaround",
|
||||
"all": "all",
|
||||
}
|
||||
return aliases.get(normalized, normalized)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
|
|
@ -29,6 +29,9 @@ TASK_STATUSES = {
|
|||
}
|
||||
|
||||
RETRYABLE_TASK_STATUSES = {"failed", "dead_letter", "cancelled"}
|
||||
FAILED_TASK_STATUSES = ("failed", "dead_letter", "cancelled")
|
||||
COMPLETED_TASK_STATUSES = ("completed", "succeeded", "superseded")
|
||||
DELETABLE_TASK_STATUSES = frozenset((*FAILED_TASK_STATUSES, *COMPLETED_TASK_STATUSES))
|
||||
|
||||
|
||||
class QueueService:
|
||||
|
|
@ -433,6 +436,106 @@ class QueueService:
|
|||
def recent_jobs(self, limit: int = 10) -> list[dict[str, Any]]:
|
||||
return [task.to_dict() for task in self.repository.recent(limit=limit)]
|
||||
|
||||
def recover_stale_tasks(
|
||||
self,
|
||||
ctx: RequestContext,
|
||||
*,
|
||||
stale_after_seconds: int = 300,
|
||||
limit: int = 100,
|
||||
source: str | None = "workflow_round",
|
||||
now: datetime | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Recover tasks abandoned by a previous plugin process.
|
||||
|
||||
The task FSM intentionally does not allow ``running -> queued``.
|
||||
Recovery therefore records a failed terminal observation first and
|
||||
only then requeues it. The two compare-and-set operations also avoid
|
||||
stealing work from a live worker that updated the row concurrently.
|
||||
"""
|
||||
|
||||
observed_at = now or datetime.now(timezone.utc)
|
||||
if observed_at.tzinfo is None:
|
||||
observed_at = observed_at.replace(tzinfo=timezone.utc)
|
||||
cutoff = observed_at.astimezone(timezone.utc) - timedelta(
|
||||
seconds=max(int(stale_after_seconds), 1)
|
||||
)
|
||||
cutoff_value = cutoff.strftime("%Y-%m-%d %H:%M:%S")
|
||||
candidates = self.repository.list_stale(
|
||||
statuses=("running", "retrying"),
|
||||
updated_before=cutoff_value,
|
||||
limit=min(max(int(limit), 1), 500),
|
||||
)
|
||||
recovered: list[str] = []
|
||||
skipped: list[dict[str, str]] = []
|
||||
for candidate in candidates:
|
||||
task_source = str(candidate.metadata.get("source") or "").strip()
|
||||
if source is not None and task_source != source:
|
||||
skipped.append(
|
||||
{
|
||||
"task_id": candidate.task_id,
|
||||
"status": candidate.status,
|
||||
"reason": "source_not_managed",
|
||||
}
|
||||
)
|
||||
continue
|
||||
recovery = {
|
||||
"source": "plugin_startup_stale_recovery",
|
||||
"observed_at": observed_at.astimezone(timezone.utc).isoformat(),
|
||||
"updated_before": cutoff_value,
|
||||
"previous_status": candidate.status,
|
||||
}
|
||||
failed = self.update_task_if_status(
|
||||
candidate.task_id,
|
||||
{candidate.status},
|
||||
{
|
||||
"status": "failed",
|
||||
"progress": candidate.progress,
|
||||
"error_message": "task was interrupted by a previous plugin process",
|
||||
"metadata": {**candidate.metadata, "recovery": recovery},
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
if failed is None:
|
||||
skipped.append(
|
||||
{
|
||||
"task_id": candidate.task_id,
|
||||
"status": self.get_job(candidate.task_id).status,
|
||||
"reason": "status_changed",
|
||||
}
|
||||
)
|
||||
continue
|
||||
queued = self.update_task_if_status(
|
||||
failed.task_id,
|
||||
{"failed"},
|
||||
{
|
||||
"status": "queued",
|
||||
"progress": 0,
|
||||
"error_message": None,
|
||||
"metadata": {
|
||||
**failed.metadata,
|
||||
"recovery": {**recovery, "status": "requeued"},
|
||||
},
|
||||
},
|
||||
ctx,
|
||||
require_workflow_not_cancelled=True,
|
||||
)
|
||||
if queued is None:
|
||||
skipped.append(
|
||||
{
|
||||
"task_id": candidate.task_id,
|
||||
"status": self.get_job(candidate.task_id).status,
|
||||
"reason": "workflow_cancelled_or_status_changed",
|
||||
}
|
||||
)
|
||||
continue
|
||||
recovered.append(queued.task_id)
|
||||
return {
|
||||
"recovered_task_ids": recovered,
|
||||
"recovered": len(recovered),
|
||||
"skipped": skipped,
|
||||
"cutoff": cutoff_value,
|
||||
}
|
||||
|
||||
def requeue_dead_letters(self, ctx: RequestContext, *, limit: int = 50) -> dict[str, Any]:
|
||||
jobs = self.list_jobs(status="dead_letter", page=1, page_size=_batch_limit(limit)).items
|
||||
requeued = []
|
||||
|
|
@ -460,6 +563,113 @@ class QueueService:
|
|||
)
|
||||
return {"moved": moved, "total": len(moved)}
|
||||
|
||||
def delete_job(self, task_id: str, ctx: RequestContext) -> dict[str, Any]:
|
||||
before = self.get_job(task_id)
|
||||
if before.status not in DELETABLE_TASK_STATUSES:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"only terminal task records can be deleted",
|
||||
{
|
||||
"task_id": task_id,
|
||||
"status": before.status,
|
||||
"allowed_statuses": sorted(DELETABLE_TASK_STATUSES),
|
||||
},
|
||||
)
|
||||
# Compare against the status that was authorized above, rather than
|
||||
# any deletable status. This keeps the audit snapshot truthful and
|
||||
# prevents a concurrent state change from being silently deleted.
|
||||
deleted = self.repository.delete_if_status(task_id, {before.status})
|
||||
if deleted is None:
|
||||
current = self.repository.get(task_id)
|
||||
if current is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "task not found", {"task_id": task_id})
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"task status changed before deletion",
|
||||
{"task_id": task_id, "status": current.status},
|
||||
)
|
||||
node_cleanup = dict(deleted.get("node_cleanup") or {})
|
||||
workflow_cleanup = dict(deleted.get("workflow_cleanup") or {})
|
||||
self.audit_service.record(
|
||||
ctx=ctx,
|
||||
action="delete_task_record",
|
||||
target_type="generation_task",
|
||||
target_id=task_id,
|
||||
before_snapshot=before.to_dict(),
|
||||
metadata={"node_cleanup": node_cleanup, "workflow_cleanup": workflow_cleanup},
|
||||
)
|
||||
self._publish(
|
||||
"generation.task_deleted",
|
||||
{
|
||||
"task_id": task_id,
|
||||
"status": before.status,
|
||||
"node_cleanup": node_cleanup,
|
||||
"workflow_cleanup": workflow_cleanup,
|
||||
},
|
||||
)
|
||||
return {
|
||||
"task_id": task_id,
|
||||
"deleted": True,
|
||||
"previous_status": before.status,
|
||||
"node_cleanup": node_cleanup,
|
||||
"workflow_cleanup": workflow_cleanup,
|
||||
}
|
||||
|
||||
def clear_failed_jobs(self, ctx: RequestContext, *, limit: int = 100) -> dict[str, Any]:
|
||||
return self._clear_job_records(ctx, statuses=FAILED_TASK_STATUSES, limit=limit, scope="failed")
|
||||
|
||||
def clear_terminal_jobs(
|
||||
self,
|
||||
ctx: RequestContext,
|
||||
*,
|
||||
statuses: list[str] | tuple[str, ...] | set[str] | None = None,
|
||||
limit: int = 100,
|
||||
) -> dict[str, Any]:
|
||||
return self._clear_job_records(
|
||||
ctx,
|
||||
statuses=_terminal_cleanup_statuses(statuses),
|
||||
limit=limit,
|
||||
scope="terminal",
|
||||
)
|
||||
|
||||
def _clear_job_records(
|
||||
self,
|
||||
ctx: RequestContext,
|
||||
*,
|
||||
statuses: tuple[str, ...],
|
||||
limit: int,
|
||||
scope: str,
|
||||
) -> dict[str, Any]:
|
||||
applied_limit = _batch_limit(limit)
|
||||
deleted: list[dict[str, Any]] = []
|
||||
errors: list[dict[str, str]] = []
|
||||
for status in statuses:
|
||||
# Failed compare-and-delete attempts are still processed records.
|
||||
# Counting only successful deletions could exceed the caller's
|
||||
# batch limit when statuses change concurrently.
|
||||
remaining = applied_limit - len(deleted) - len(errors)
|
||||
if remaining <= 0:
|
||||
break
|
||||
jobs = self.list_jobs(status=status, page=1, page_size=remaining).items
|
||||
for job in jobs:
|
||||
try:
|
||||
deleted.append(self.delete_job(job.task_id, ctx))
|
||||
except ApiError as exc:
|
||||
errors.append(
|
||||
{"task_id": job.task_id, "error_code": exc.error_code.value, "message": exc.message}
|
||||
)
|
||||
remaining_counts = self.repository.count_by_status()
|
||||
return {
|
||||
"scope": scope,
|
||||
"requested_statuses": list(statuses),
|
||||
"deleted": deleted,
|
||||
"errors": errors,
|
||||
"total": len(deleted),
|
||||
"processed": len(deleted) + len(errors),
|
||||
"limit": applied_limit,
|
||||
"remaining_by_status": {status: remaining_counts.get(status, 0) for status in statuses},
|
||||
}
|
||||
|
||||
def _validate_task_status_transition(self, current: str, target: str) -> None:
|
||||
if target not in TASK_STATUSES:
|
||||
raise ApiError(
|
||||
|
|
@ -515,6 +725,39 @@ def _batch_limit(value: int) -> int:
|
|||
return min(max(limit, 1), 100)
|
||||
|
||||
|
||||
def _terminal_cleanup_statuses(
|
||||
statuses: list[str] | tuple[str, ...] | set[str] | None,
|
||||
) -> tuple[str, ...]:
|
||||
if statuses is None:
|
||||
return (*FAILED_TASK_STATUSES, *COMPLETED_TASK_STATUSES)
|
||||
if not isinstance(statuses, (list, tuple, set)):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"terminal cleanup statuses must be an array",
|
||||
{"field": "statuses", "allowed_statuses": sorted(DELETABLE_TASK_STATUSES)},
|
||||
)
|
||||
requested = {str(status or "").strip().lower() for status in statuses if str(status or "").strip()}
|
||||
if not requested:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"terminal cleanup requires at least one status",
|
||||
{"field": "statuses", "allowed_statuses": sorted(DELETABLE_TASK_STATUSES)},
|
||||
)
|
||||
unsupported = sorted(requested - DELETABLE_TASK_STATUSES)
|
||||
if unsupported:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"terminal cleanup cannot include active task statuses",
|
||||
{
|
||||
"field": "statuses",
|
||||
"unsupported_statuses": unsupported,
|
||||
"allowed_statuses": sorted(DELETABLE_TASK_STATUSES),
|
||||
},
|
||||
)
|
||||
canonical_order = (*FAILED_TASK_STATUSES, *COMPLETED_TASK_STATUSES)
|
||||
return tuple(status for status in canonical_order if status in requested)
|
||||
|
||||
|
||||
def _int_or_default(value: Any, default: int) -> int:
|
||||
try:
|
||||
return int(value)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,317 @@
|
|||
"""Server-owned identity locks for quick character generation batches."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import hashlib
|
||||
import json
|
||||
import re
|
||||
import unicodedata
|
||||
from typing import Any
|
||||
|
||||
|
||||
IDENTITY_LOCK_SCHEMA_VERSION = "quick_character_identity_v1"
|
||||
QUICK_CHARACTER_TARGETS = ("quick_avatar", "quick_bust", "quick_stand", "quick_sheet")
|
||||
|
||||
_FIELD_ALIASES: dict[str, tuple[str, ...]] = {
|
||||
"gender_presentation": ("gender_presentation", "gender", "sex", "gender_identity"),
|
||||
"age_group": ("age_group", "age_stage", "age", "character_age", "apparent_age"),
|
||||
"species": ("species", "race", "character_species"),
|
||||
"ethnicity": ("ethnicity", "ancestry"),
|
||||
"skin_tone": ("skin_tone", "skin_color", "complexion"),
|
||||
"height_build": ("height_build", "build", "body_type", "physique", "height"),
|
||||
"body_features": ("body_features", "notable_features", "distinctive_features"),
|
||||
"facial_features": ("facial_features", "face_features", "face_identity"),
|
||||
"hair_color": ("hair_color", "hair_colour"),
|
||||
"hairstyle": ("hairstyle", "hair_style"),
|
||||
"eye_color": ("eye_color", "eye_colour"),
|
||||
"clothing": ("clothing", "outfit", "clothing_keywords"),
|
||||
}
|
||||
|
||||
_FIELD_LABELS = {
|
||||
"name": "character name",
|
||||
"gender_presentation": "gender presentation",
|
||||
"age_group": "age group",
|
||||
"species": "species",
|
||||
"ethnicity": "ethnicity/ancestry",
|
||||
"skin_tone": "skin tone",
|
||||
"height_build": "height and body build",
|
||||
"body_features": "distinctive body features",
|
||||
"facial_features": "facial identity features",
|
||||
"hair_color": "hair color",
|
||||
"hairstyle": "hairstyle",
|
||||
"eye_color": "eye color",
|
||||
"clothing": "core outfit",
|
||||
"identity_summary": "identity summary",
|
||||
"appearance_summary": "appearance summary",
|
||||
}
|
||||
|
||||
|
||||
def is_quick_character_node(workflow: Any, node: Any) -> bool:
|
||||
return (
|
||||
str(getattr(workflow, "workflow_type", "") or "").strip() == "character_fast"
|
||||
and str(getattr(node, "node_key", "") or "").strip() in QUICK_CHARACTER_TARGETS
|
||||
)
|
||||
|
||||
|
||||
def build_identity_lock(profile: Any, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Build a deterministic identity capsule from profile and explicit request fields."""
|
||||
|
||||
profile_metadata = _record(getattr(profile, "metadata", None))
|
||||
request_metadata = _record(payload.get("metadata"))
|
||||
sources = [
|
||||
_record(profile_metadata.get("questionnaire")),
|
||||
_record(profile_metadata.get("identity_fields")),
|
||||
profile_metadata,
|
||||
_record(request_metadata.get("identity_fields")),
|
||||
_record(payload.get("identity_fields")),
|
||||
_lock_fields(payload.get("identity_lock")),
|
||||
]
|
||||
|
||||
fields: dict[str, str] = {
|
||||
"name": _text(getattr(profile, "name", None)),
|
||||
}
|
||||
for field, aliases in _FIELD_ALIASES.items():
|
||||
value = _last_value(sources, aliases)
|
||||
if field == "gender_presentation":
|
||||
value = _normalize_gender(value)
|
||||
elif field == "age_group":
|
||||
value = _normalize_age_group(value)
|
||||
fields[field] = value
|
||||
|
||||
identity_summary = _first_text(
|
||||
_last_value(sources, ("identity", "role_core", "character_identity")),
|
||||
getattr(profile, "description", None),
|
||||
getattr(profile, "base_prompt", None),
|
||||
)
|
||||
appearance_summary = _first_text(
|
||||
_last_value(sources, ("appearance", "appearance_summary")),
|
||||
getattr(profile, "appearance_prompt", None),
|
||||
)
|
||||
if not fields["gender_presentation"]:
|
||||
fields["gender_presentation"] = _infer_gender(identity_summary, appearance_summary)
|
||||
if not fields["age_group"]:
|
||||
fields["age_group"] = _infer_age_group(identity_summary, appearance_summary)
|
||||
if identity_summary:
|
||||
fields["identity_summary"] = identity_summary
|
||||
if appearance_summary:
|
||||
fields["appearance_summary"] = appearance_summary
|
||||
|
||||
canonical_fields = {key: value for key, value in fields.items() if value}
|
||||
prompt = _identity_prompt(canonical_fields)
|
||||
return {
|
||||
"schema_version": IDENTITY_LOCK_SCHEMA_VERSION,
|
||||
"fields": canonical_fields,
|
||||
"prompt": prompt,
|
||||
}
|
||||
|
||||
|
||||
def identity_lock_hash(identity_lock: dict[str, Any]) -> str:
|
||||
canonical = json.dumps(identity_lock, ensure_ascii=False, sort_keys=True, separators=(",", ":"))
|
||||
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def explicit_identity_fields_present(payload: dict[str, Any]) -> bool:
|
||||
metadata = _record(payload.get("metadata"))
|
||||
return any(
|
||||
bool(value)
|
||||
for value in (
|
||||
payload.get("identity_fields"),
|
||||
payload.get("identity_lock"),
|
||||
payload.get("identity_lock_hash"),
|
||||
metadata.get("identity_fields"),
|
||||
metadata.get("identity_lock"),
|
||||
metadata.get("identity_lock_hash"),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def requested_identity_lock_hash(payload: dict[str, Any]) -> str:
|
||||
metadata = _record(payload.get("metadata"))
|
||||
return _text(payload.get("identity_lock_hash") or metadata.get("identity_lock_hash"))
|
||||
|
||||
|
||||
def append_identity_lock(prompt: Any, identity_lock: dict[str, Any]) -> str:
|
||||
base = _text(prompt)
|
||||
lock_prompt = _text(identity_lock.get("prompt"))
|
||||
if not lock_prompt:
|
||||
return base
|
||||
if lock_prompt.casefold() in base.casefold():
|
||||
return base
|
||||
return ", ".join(item for item in (base, lock_prompt) if item)
|
||||
|
||||
|
||||
def quick_character_batch_id(workflow_id: str, lock_hash: str) -> str:
|
||||
digest = hashlib.sha256(f"{workflow_id}:{lock_hash}".encode("utf-8")).hexdigest()[:24]
|
||||
return f"qcb_{digest}"
|
||||
|
||||
|
||||
def safe_route_snapshot(route_name: str, resolution: dict[str, Any] | None) -> dict[str, Any]:
|
||||
resolved = resolution if isinstance(resolution, dict) else {}
|
||||
return {
|
||||
"requested_route": _text(route_name),
|
||||
"route_name": _text(resolved.get("route_name") or route_name),
|
||||
"route_id": _text(resolved.get("route_id")),
|
||||
"status": _text(resolved.get("status") or "unresolved"),
|
||||
"provider_id": _text(resolved.get("provider_id")),
|
||||
"provider_type": _text(resolved.get("provider_type")),
|
||||
"model_id": _text(resolved.get("model_id")),
|
||||
"modality": _text(resolved.get("modality")),
|
||||
}
|
||||
|
||||
|
||||
def consistency_metadata(
|
||||
*,
|
||||
batch_id: str,
|
||||
lock_hash: str,
|
||||
identity_lock: dict[str, Any],
|
||||
target: str,
|
||||
route_snapshot: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
return {
|
||||
"quick_character_batch_id": batch_id,
|
||||
"identity_lock_hash": lock_hash,
|
||||
"quick_character_target": target,
|
||||
"identity_lock": identity_lock,
|
||||
"quick_character_route_snapshot": route_snapshot,
|
||||
}
|
||||
|
||||
|
||||
def _identity_prompt(fields: dict[str, str]) -> str:
|
||||
clauses = [
|
||||
"MANDATORY CHARACTER IDENTITY LOCK",
|
||||
"depict the same individual in every image in this quick-character batch",
|
||||
"do not change gender presentation, apparent age, species, ancestry, skin tone, facial identity, hair, eyes, body build, distinctive features, or core outfit",
|
||||
]
|
||||
clauses.extend(
|
||||
f"{_FIELD_LABELS[key]}: {value}"
|
||||
for key, value in fields.items()
|
||||
if key in _FIELD_LABELS and value
|
||||
)
|
||||
return "; ".join(clauses)
|
||||
|
||||
|
||||
def _normalize_gender(value: Any) -> str:
|
||||
text = _text(value).casefold()
|
||||
if not text:
|
||||
return ""
|
||||
if text in {"unspecified", "unknown", "auto", "未指定", "未知", "自动"}:
|
||||
return ""
|
||||
mapping = {
|
||||
"男": "male/masculine",
|
||||
"男性": "male/masculine",
|
||||
"男生": "male/masculine",
|
||||
"男人": "male/masculine",
|
||||
"male": "male/masculine",
|
||||
"man": "male/masculine",
|
||||
"masculine": "male/masculine",
|
||||
"女": "female/feminine",
|
||||
"女性": "female/feminine",
|
||||
"女生": "female/feminine",
|
||||
"女人": "female/feminine",
|
||||
"female": "female/feminine",
|
||||
"woman": "female/feminine",
|
||||
"feminine": "female/feminine",
|
||||
"非二元": "non-binary/androgynous",
|
||||
"nonbinary": "non-binary/androgynous",
|
||||
"non-binary": "non-binary/androgynous",
|
||||
"androgynous": "non-binary/androgynous",
|
||||
"中性": "non-binary/androgynous",
|
||||
}
|
||||
return mapping.get(text, _text(value))
|
||||
|
||||
|
||||
def _normalize_age_group(value: Any) -> str:
|
||||
text = _text(value).casefold()
|
||||
if not text:
|
||||
return ""
|
||||
if text in {"unspecified", "unknown", "auto", "未指定", "未知", "自动"}:
|
||||
return ""
|
||||
numeric = re.fullmatch(r"(?:age\s*)?(\d{1,3})(?:\s*(?:years? old|岁))?", text)
|
||||
if numeric:
|
||||
age = int(numeric.group(1))
|
||||
if age < 13:
|
||||
return "child"
|
||||
if age < 18:
|
||||
return "adolescent"
|
||||
if age < 40:
|
||||
return "adult"
|
||||
if age < 65:
|
||||
return "middle-aged adult"
|
||||
return "older adult"
|
||||
mapping = {
|
||||
"儿童": "child",
|
||||
"小孩": "child",
|
||||
"child": "child",
|
||||
"少年": "adolescent",
|
||||
"少女": "adolescent",
|
||||
"青少年": "adolescent",
|
||||
"teen": "adolescent",
|
||||
"teenager": "adolescent",
|
||||
"成年": "adult",
|
||||
"成年人": "adult",
|
||||
"adult": "adult",
|
||||
"青年": "young adult",
|
||||
"young adult": "young adult",
|
||||
"中年": "middle-aged adult",
|
||||
"middle aged": "middle-aged adult",
|
||||
"middle-aged": "middle-aged adult",
|
||||
"老年": "older adult",
|
||||
"elderly": "older adult",
|
||||
"senior": "older adult",
|
||||
}
|
||||
return mapping.get(text, _text(value))
|
||||
|
||||
|
||||
def _infer_gender(*values: Any) -> str:
|
||||
text = " ".join(_text(value) for value in values if _text(value)).casefold()
|
||||
female = bool(re.search(r"(?:女性|女生|女人|少女|女孩|\bfemale\b|\bwoman\b|\bgirl\b)", text))
|
||||
male = bool(re.search(r"(?:男性|男生|男人|少年|男孩|\bmale\b|\bman\b|\bboy\b)", text))
|
||||
if female == male:
|
||||
return ""
|
||||
return "female/feminine" if female else "male/masculine"
|
||||
|
||||
|
||||
def _infer_age_group(*values: Any) -> str:
|
||||
text = " ".join(_text(value) for value in values if _text(value)).casefold()
|
||||
matches = [
|
||||
("child", r"(?:儿童|小孩|幼童|\bchild\b)"),
|
||||
("adolescent", r"(?:青少年|少年|少女|男孩|女孩|\bteen(?:ager)?\b|\bboy\b|\bgirl\b)"),
|
||||
("adult", r"(?:成年|成年人|成人|\badult\b)"),
|
||||
("middle-aged adult", r"(?:中年|\bmiddle[- ]aged\b)"),
|
||||
("older adult", r"(?:老年|老人|\belderly\b|\bsenior\b)"),
|
||||
]
|
||||
found = [label for label, pattern in matches if re.search(pattern, text)]
|
||||
return found[0] if len(found) == 1 else ""
|
||||
|
||||
|
||||
def _last_value(sources: list[dict[str, Any]], aliases: tuple[str, ...]) -> str:
|
||||
result = ""
|
||||
for source in sources:
|
||||
for alias in aliases:
|
||||
value = _text(source.get(alias))
|
||||
if value:
|
||||
result = value
|
||||
break
|
||||
return result
|
||||
|
||||
|
||||
def _lock_fields(value: Any) -> dict[str, Any]:
|
||||
lock = _record(value)
|
||||
return _record(lock.get("fields")) if lock else {}
|
||||
|
||||
|
||||
def _record(value: Any) -> dict[str, Any]:
|
||||
return value if isinstance(value, dict) else {}
|
||||
|
||||
|
||||
def _first_text(*values: Any) -> str:
|
||||
return next((_text(value) for value in values if _text(value)), "")
|
||||
|
||||
|
||||
def _text(value: Any) -> str:
|
||||
if value is None:
|
||||
return ""
|
||||
if isinstance(value, (list, tuple, set)):
|
||||
value = ", ".join(_text(item) for item in value if _text(item))
|
||||
text = unicodedata.normalize("NFKC", str(value))
|
||||
return " ".join(text.split()).strip()
|
||||
|
|
@ -35,7 +35,12 @@ class QuickProfileService:
|
|||
)
|
||||
|
||||
identity = _text(questionnaire.get("identity"))
|
||||
gender_presentation = _text(questionnaire.get("gender_presentation"))
|
||||
age_stage = _first_text(questionnaire, "age_stage", "age_group")
|
||||
species = _text(questionnaire.get("species"))
|
||||
personality = _join(questionnaire.get("personality"))
|
||||
body_type = _first_text(questionnaire, "body_type", "height_build")
|
||||
distinctive_features = _first_text(questionnaire, "distinctive_features", "body_features")
|
||||
hair_color = _text(questionnaire.get("hair_color"))
|
||||
hairstyle = _text(questionnaire.get("hairstyle"))
|
||||
eye_color = _text(questionnaire.get("eye_color"))
|
||||
|
|
@ -45,10 +50,20 @@ class QuickProfileService:
|
|||
base_parts = [name]
|
||||
if identity:
|
||||
base_parts.append(identity)
|
||||
if gender_presentation:
|
||||
base_parts.append(f"gender presentation: {gender_presentation}")
|
||||
if age_stage:
|
||||
base_parts.append(f"age stage: {age_stage}")
|
||||
if species:
|
||||
base_parts.append(f"species: {species}")
|
||||
if personality:
|
||||
base_parts.append(f"personality: {personality}")
|
||||
|
||||
appearance_parts = []
|
||||
if body_type:
|
||||
appearance_parts.append(f"body type: {body_type}")
|
||||
if distinctive_features:
|
||||
appearance_parts.append(f"distinctive features: {distinctive_features}")
|
||||
if hair_color:
|
||||
appearance_parts.append(f"{hair_color} hair")
|
||||
if hairstyle:
|
||||
|
|
@ -89,6 +104,10 @@ class QuickProfileService:
|
|||
if not name:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "\u89d2\u8272\u540d\u79f0\u4e0d\u80fd\u4e3a\u7a7a", {"field": "name"})
|
||||
draft_id = _text(payload.get("draft_id") or draft.get("draft_id"))
|
||||
questionnaire = _normalize_questionnaire(
|
||||
payload.get("questionnaire") if isinstance(payload.get("questionnaire"), dict) else {}
|
||||
)
|
||||
metadata = _build_profile_metadata(payload.get("metadata"), questionnaire, draft_id)
|
||||
profile = profile_service.create_profile(
|
||||
{
|
||||
"name": name,
|
||||
|
|
@ -99,11 +118,7 @@ class QuickProfileService:
|
|||
"negative_prompt": preview.get("negative_prompt"),
|
||||
"default_model_route": payload.get("default_model_route") or preview.get("default_model_route"),
|
||||
"avatar_image_uri": payload.get("avatar_image_uri") or preview.get("avatar_image_uri"),
|
||||
"metadata": {
|
||||
**(payload.get("metadata") if isinstance(payload.get("metadata"), dict) else {}),
|
||||
"source": "quick_profile",
|
||||
**({"draft_id": draft_id} if draft_id else {}),
|
||||
},
|
||||
"metadata": metadata,
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
|
|
@ -131,11 +146,7 @@ class QuickProfileService:
|
|||
"profile_id": profile.profile_id,
|
||||
**({"draft_id": draft_id} if draft_id else {}),
|
||||
"profile_preview": preview,
|
||||
**(
|
||||
{"questionnaire": payload["questionnaire"]}
|
||||
if isinstance(payload.get("questionnaire"), dict)
|
||||
else {}
|
||||
),
|
||||
**({"questionnaire": questionnaire} if questionnaire else {}),
|
||||
},
|
||||
},
|
||||
ctx,
|
||||
|
|
@ -153,3 +164,58 @@ def _join(value: Any) -> str:
|
|||
if isinstance(value, list):
|
||||
return ", ".join(str(item).strip() for item in value if str(item).strip())
|
||||
return _text(value)
|
||||
|
||||
|
||||
_STRUCTURED_IDENTITY_ALIASES: dict[str, tuple[str, ...]] = {
|
||||
"gender_presentation": ("gender_presentation",),
|
||||
"age_stage": ("age_stage", "age_group"),
|
||||
"species": ("species",),
|
||||
"body_type": ("body_type", "height_build"),
|
||||
"distinctive_features": ("distinctive_features", "body_features"),
|
||||
}
|
||||
|
||||
|
||||
def _first_text(values: dict[str, Any], *keys: str) -> str:
|
||||
for key in keys:
|
||||
value = _text(values.get(key))
|
||||
if value:
|
||||
return value
|
||||
return ""
|
||||
|
||||
|
||||
def _normalize_questionnaire(questionnaire: dict[str, Any]) -> dict[str, Any]:
|
||||
normalized = dict(questionnaire)
|
||||
for canonical_key, aliases in _STRUCTURED_IDENTITY_ALIASES.items():
|
||||
if _text(normalized.get(canonical_key)):
|
||||
continue
|
||||
value = _first_text(questionnaire, *aliases)
|
||||
if value:
|
||||
normalized[canonical_key] = value
|
||||
return normalized
|
||||
|
||||
|
||||
def _build_profile_metadata(metadata: Any, questionnaire: dict[str, Any], draft_id: str) -> dict[str, Any]:
|
||||
result = dict(metadata) if isinstance(metadata, dict) else {}
|
||||
|
||||
existing_questionnaire = result.get("questionnaire")
|
||||
if isinstance(existing_questionnaire, dict):
|
||||
# User-provided nested metadata takes precedence while retaining all
|
||||
# normalized questionnaire fields and arbitrary custom keys.
|
||||
merged_questionnaire = {
|
||||
**questionnaire,
|
||||
**_normalize_questionnaire(existing_questionnaire),
|
||||
}
|
||||
else:
|
||||
merged_questionnaire = dict(questionnaire)
|
||||
if merged_questionnaire:
|
||||
result["questionnaire"] = merged_questionnaire
|
||||
|
||||
for canonical_key in _STRUCTURED_IDENTITY_ALIASES:
|
||||
value = _text(questionnaire.get(canonical_key))
|
||||
if value:
|
||||
result.setdefault(canonical_key, value)
|
||||
|
||||
result["source"] = "quick_profile"
|
||||
if draft_id:
|
||||
result["draft_id"] = draft_id
|
||||
return result
|
||||
|
|
|
|||
|
|
@ -0,0 +1,92 @@
|
|||
"""Owner-chain authorization for chat-facing ImageStudio resources.
|
||||
|
||||
Web administrators may be granted ``can_manage_all_profiles`` explicitly.
|
||||
Regular AstrBot chat callers must own the profile at the end of the resource
|
||||
chain; guessed workflow, node, image, or task IDs intentionally use not-found
|
||||
semantics instead of revealing another user's data.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from core.identity import RequestContext
|
||||
from database.repositories.image_repository import ImageRepository
|
||||
from database.repositories.job_repository import JobRepository
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
|
||||
|
||||
class ResourceAccessService:
|
||||
def __init__(
|
||||
self,
|
||||
profile_repository: ProfileRepository,
|
||||
workflow_repository: WorkflowRepository,
|
||||
image_repository: ImageRepository,
|
||||
job_repository: JobRepository,
|
||||
):
|
||||
self.profiles = profile_repository
|
||||
self.workflows = workflow_repository
|
||||
self.images = image_repository
|
||||
self.jobs = job_repository
|
||||
|
||||
def require_profile(self, profile_id: str, ctx: RequestContext):
|
||||
profile = self.profiles.get(profile_id)
|
||||
if profile is None or profile.status == "deleted" or not self._can_access(profile.owner_user_id, ctx):
|
||||
raise _not_found("character profile", profile_id)
|
||||
return profile
|
||||
|
||||
def require_workflow(self, workflow_id: str, ctx: RequestContext):
|
||||
workflow = self.workflows.get_workflow(workflow_id)
|
||||
if workflow is None:
|
||||
raise _not_found("workflow", workflow_id)
|
||||
self.require_profile(workflow.profile_id, ctx)
|
||||
return workflow
|
||||
|
||||
def require_node(self, node_id: str, ctx: RequestContext):
|
||||
node = self.workflows.get_node(node_id)
|
||||
if node is None:
|
||||
raise _not_found("workflow node", node_id)
|
||||
self.require_workflow(node.workflow_id, ctx)
|
||||
return node
|
||||
|
||||
def require_image(self, image_id: str, ctx: RequestContext):
|
||||
image = self.images.get(image_id)
|
||||
if image is None:
|
||||
raise _not_found("generation image", image_id)
|
||||
if image.profile_id:
|
||||
self.require_profile(image.profile_id, ctx)
|
||||
elif image.workflow_id:
|
||||
self.require_workflow(image.workflow_id, ctx)
|
||||
else:
|
||||
raise _not_found("generation image", image_id)
|
||||
return image
|
||||
|
||||
def require_task(self, task_id: str, ctx: RequestContext):
|
||||
task = self.jobs.get(task_id)
|
||||
if task is None:
|
||||
raise _not_found("generation task", task_id)
|
||||
if task.profile_id:
|
||||
self.require_profile(task.profile_id, ctx)
|
||||
elif task.workflow_id:
|
||||
self.require_workflow(task.workflow_id, ctx)
|
||||
elif task.node_id:
|
||||
self.require_node(task.node_id, ctx)
|
||||
else:
|
||||
raise _not_found("generation task", task_id)
|
||||
return task
|
||||
|
||||
@staticmethod
|
||||
def _can_access(owner_user_id: str | None, ctx: RequestContext) -> bool:
|
||||
if ctx.has_permission("can_manage_all_profiles") or ctx.has_permission("*"):
|
||||
return True
|
||||
return bool(ctx.actor_id and owner_user_id and ctx.actor_id == owner_user_id)
|
||||
|
||||
|
||||
def _not_found(resource: str, resource_id: str) -> ApiError:
|
||||
return ApiError(
|
||||
ErrorCode.RESOURCE_NOT_FOUND,
|
||||
f"{resource} not found",
|
||||
{"resource_id": str(resource_id)},
|
||||
)
|
||||
|
|
@ -45,6 +45,7 @@ class ReviewService:
|
|||
|
||||
def review_node(self, node_id: str) -> dict[str, Any]:
|
||||
node = self.workflow_service.get_node(node_id)
|
||||
self.workflow_service.ensure_node_action_allowed(node_id, "review", node=node)
|
||||
workflow = self.workflow_service.get_workflow(node.workflow_id).workflow
|
||||
images = self.image_repository.list_by_node(node_id, include_archived=True)
|
||||
return self.review_engine.review_node(node, images, workflow.metadata)
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ from database.db_helper import (
|
|||
get_runtime_config_overrides,
|
||||
load_config_file_payload,
|
||||
load_config_payload,
|
||||
persist_runtime_config_values,
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -101,12 +102,19 @@ class SettingsService:
|
|||
"title": "模型分工",
|
||||
"fields": [
|
||||
_field("material_recognition", "string", label="素材解析路由"),
|
||||
_field("advanced_material_recognition", "string", label="进阶素材识别路由"),
|
||||
_field("advanced_style_consensus", "string", label="进阶画风交叉归纳路由"),
|
||||
_field("advanced_material_validation_generation", "string", label="进阶素材验证生图路由"),
|
||||
_field("profile_parse", "string", label="角色导入解析路由"),
|
||||
_field("persona_split", "string", label="人设拆解路由"),
|
||||
_field("prompt_planning", "string", label="提示词规划路由"),
|
||||
_field("cross_review_primary", "string", label="交叉审核主模型路由"),
|
||||
_field("cross_review_secondary", "string", label="交叉审核复核模型路由"),
|
||||
_field("cross_review_routes", "list", label="交叉审核模型路由列表"),
|
||||
_field("visual_review_routes", "list", label="视觉质量审核路由列表"),
|
||||
_field("cross_review_primary", "string", label="兼容视觉主审核路由"),
|
||||
_field("cross_review_secondary", "string", label="兼容视觉复核路由"),
|
||||
_field("cross_review_routes", "list", label="兼容视觉审核路由列表"),
|
||||
_field("generation_cross_review_primary", "string", label="生成式交叉审查主生图路由"),
|
||||
_field("generation_cross_review_secondary", "string", label="生成式交叉审查复核生图路由"),
|
||||
_field("generation_cross_review_routes", "list", label="生成式交叉审查生图路由列表"),
|
||||
_field("quick_generation", "string", label="快速生图路由"),
|
||||
_field("portrait_generation", "string", label="头像生成路由"),
|
||||
_field("halfbody_generation", "string", label="半身生成路由"),
|
||||
|
|
@ -123,6 +131,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": "提示词与内容分流",
|
||||
|
|
@ -175,6 +198,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="临时目录"),
|
||||
|
|
@ -225,6 +249,20 @@ class SettingsService:
|
|||
provider = generation.get("provider", {})
|
||||
provider = provider if isinstance(provider, Mapping) else {}
|
||||
generation_provider = provider
|
||||
adapters = generation.get("adapters")
|
||||
if isinstance(adapters, list):
|
||||
for index, adapter in enumerate(adapters):
|
||||
if not isinstance(adapter, Mapping) or adapter.get("enabled") is False:
|
||||
continue
|
||||
base_url = str(adapter.get("base_url") or "").strip()
|
||||
if not base_url:
|
||||
warnings.append(
|
||||
{
|
||||
"field": f"generation.adapters.{index}.base_url",
|
||||
"message": "enabled adapter has no base URL and cannot be probed yet",
|
||||
}
|
||||
)
|
||||
else:
|
||||
route_name = str(provider.get("route_name") or generation.get("default_model") or "").strip()
|
||||
model_id = str(provider.get("model_id") or generation.get("default_model") or "").strip()
|
||||
base_url = str(provider.get("base_url") or "").strip()
|
||||
|
|
@ -305,6 +343,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]:
|
||||
|
|
@ -361,12 +423,17 @@ class SettingsService:
|
|||
before = self._load_current_mapping()
|
||||
before_file = load_config_file_payload(self.config_service.config_path)
|
||||
merged_effective = _merge_masked(before, incoming)
|
||||
conflicts = _runtime_override_conflicts(merged_effective, self.config_service.config_path)
|
||||
if conflicts:
|
||||
runtime_changes = _runtime_override_changes(
|
||||
before,
|
||||
merged_effective,
|
||||
self.config_service.config_path,
|
||||
)
|
||||
persistence = persist_runtime_config_values(runtime_changes) if runtime_changes else {}
|
||||
if runtime_changes and not persistence.get("available"):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"these settings are managed by AstrBot; update them in the AstrBot plugin settings",
|
||||
{"runtime_managed_fields": conflicts},
|
||||
"AstrBot-managed settings could not be persisted from this runtime",
|
||||
{"runtime_managed_fields": sorted(runtime_changes)},
|
||||
)
|
||||
merged = _restore_runtime_managed_fields(
|
||||
merged_effective,
|
||||
|
|
@ -639,11 +706,54 @@ def _merge_masked(current: Mapping[str, Any], incoming: Mapping[str, Any]) -> di
|
|||
continue
|
||||
if isinstance(value, Mapping) and isinstance(result.get(key), Mapping):
|
||||
result[str(key)] = _merge_masked(result[key], value)
|
||||
elif isinstance(value, list) and isinstance(result.get(key), list):
|
||||
result[str(key)] = _merge_masked_list(result[key], value)
|
||||
else:
|
||||
result[str(key)] = value
|
||||
return result
|
||||
|
||||
|
||||
def _merge_masked_list(current: list[Any], incoming: list[Any]) -> list[Any]:
|
||||
merged: list[Any] = []
|
||||
for index, item in enumerate(incoming):
|
||||
if isinstance(item, Mapping):
|
||||
clean_item = dict(item)
|
||||
copy_source_id = clean_item.pop("__copy_from_adapter_id", None)
|
||||
existing = _matching_list_mapping(current, clean_item, index)
|
||||
if not isinstance(existing, Mapping) and copy_source_id not in (None, ""):
|
||||
existing = _list_mapping_by_adapter_id(current, str(copy_source_id))
|
||||
merged.append(_merge_masked(existing if isinstance(existing, Mapping) else {}, clean_item))
|
||||
elif isinstance(item, str) and item in {"***MASKED***", "***PATH***"} and index < len(current):
|
||||
merged.append(current[index])
|
||||
else:
|
||||
merged.append(item)
|
||||
return merged
|
||||
|
||||
|
||||
def _list_mapping_by_adapter_id(current: list[Any], adapter_id: str) -> Mapping[str, Any] | None:
|
||||
for item in current:
|
||||
if not isinstance(item, Mapping):
|
||||
continue
|
||||
if str(item.get("id") or item.get("provider_id") or "") == adapter_id:
|
||||
return item
|
||||
return None
|
||||
|
||||
|
||||
def _matching_list_mapping(current: list[Any], incoming: Any, index: int) -> Mapping[str, Any] | None:
|
||||
if not isinstance(incoming, Mapping):
|
||||
return None
|
||||
for identity_key in ("id", "provider_id", "route_id", "category_id", "category", "key"):
|
||||
identity = incoming.get(identity_key)
|
||||
if identity in (None, ""):
|
||||
continue
|
||||
for item in current:
|
||||
if isinstance(item, Mapping) and item.get(identity_key) == identity:
|
||||
return item
|
||||
if index < len(current) and isinstance(current[index], Mapping):
|
||||
return current[index]
|
||||
return None
|
||||
|
||||
|
||||
def _runtime_override_conflicts(incoming: Mapping[str, Any], config_path: Path) -> list[str]:
|
||||
if not is_default_config_path(config_path):
|
||||
return []
|
||||
|
|
@ -651,7 +761,52 @@ def _runtime_override_conflicts(incoming: Mapping[str, Any], config_path: Path)
|
|||
if not overrides:
|
||||
return []
|
||||
candidate = _flatten(incoming)
|
||||
return sorted(path for path, value in overrides.items() if path in candidate and candidate[path] != value)
|
||||
generation = incoming.get("generation") if isinstance(incoming.get("generation"), Mapping) else {}
|
||||
uses_plugin_adapters = isinstance(generation.get("adapters"), list)
|
||||
|
||||
def is_plugin_adapter_compatibility_field(path: str) -> bool:
|
||||
if not uses_plugin_adapters:
|
||||
return False
|
||||
return path == "generation.default_model" or path.startswith("generation.provider.")
|
||||
|
||||
return sorted(
|
||||
path
|
||||
for path, value in overrides.items()
|
||||
if path in candidate
|
||||
and candidate[path] != value
|
||||
and not is_plugin_adapter_compatibility_field(path)
|
||||
)
|
||||
|
||||
|
||||
def _runtime_override_changes(
|
||||
before: Mapping[str, Any],
|
||||
incoming: Mapping[str, Any],
|
||||
config_path: Path,
|
||||
) -> dict[str, Any]:
|
||||
"""Return only user-changed values that are owned by AstrBot's plugin config."""
|
||||
|
||||
if not is_default_config_path(config_path):
|
||||
return {}
|
||||
managed = _flatten(get_runtime_config_overrides())
|
||||
if not managed:
|
||||
return {}
|
||||
previous = _flatten(before)
|
||||
candidate = _flatten(incoming)
|
||||
generation = incoming.get("generation") if isinstance(incoming.get("generation"), Mapping) else {}
|
||||
uses_plugin_adapters = isinstance(generation.get("adapters"), list)
|
||||
|
||||
def is_plugin_adapter_compatibility_field(path: str) -> bool:
|
||||
if not uses_plugin_adapters:
|
||||
return False
|
||||
return path == "generation.default_model" or path.startswith("generation.provider.")
|
||||
|
||||
return {
|
||||
path: candidate[path]
|
||||
for path in managed
|
||||
if path in candidate
|
||||
and candidate[path] != previous.get(path)
|
||||
and not is_plugin_adapter_compatibility_field(path)
|
||||
}
|
||||
|
||||
|
||||
def _restore_runtime_managed_fields(
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -2,18 +2,24 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import threading
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass, field
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError
|
||||
from backend.dto.job_dto import GenerationTask
|
||||
from backend.services.queue_service import QueueService
|
||||
from core.identity import RequestContext
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
WorkerHandler = Callable[[GenerationTask], "WorkerTaskUpdate | dict[str, Any] | None"]
|
||||
WorkerTaskFilter = Callable[[GenerationTask], bool]
|
||||
WorkerTerminalCallback = Callable[[GenerationTask, GenerationTask, Exception | None], None]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -76,6 +82,8 @@ class QueueWorker:
|
|||
interval_seconds: float = 1.0,
|
||||
claim_batch_size: int = 5,
|
||||
retry_policy: RetryPolicy | None = None,
|
||||
task_filter: WorkerTaskFilter | None = None,
|
||||
terminal_callback: WorkerTerminalCallback | None = None,
|
||||
):
|
||||
self.queue_service = queue_service
|
||||
self.handler = handler
|
||||
|
|
@ -83,6 +91,8 @@ class QueueWorker:
|
|||
self.interval_seconds = max(float(interval_seconds), 0.01)
|
||||
self.claim_batch_size = min(max(int(claim_batch_size), 1), 50)
|
||||
self.retry_policy = retry_policy or RetryPolicy()
|
||||
self.task_filter = task_filter
|
||||
self.terminal_callback = terminal_callback
|
||||
self._stop_event = threading.Event()
|
||||
self._lock = threading.Lock()
|
||||
self._thread: threading.Thread | None = None
|
||||
|
|
@ -95,6 +105,7 @@ class QueueWorker:
|
|||
update = _normalize_update(self.handler(task))
|
||||
latest = self.queue_service.get_job(task.task_id)
|
||||
if latest.status == "cancelled":
|
||||
self._notify_terminal(task, latest, None)
|
||||
return WorkerRunResult(
|
||||
worker_id=self.worker_id,
|
||||
status="cancelled",
|
||||
|
|
@ -121,6 +132,7 @@ class QueueWorker:
|
|||
},
|
||||
ctx,
|
||||
)
|
||||
self._notify_terminal(task, completed, None)
|
||||
return WorkerRunResult(
|
||||
worker_id=self.worker_id,
|
||||
status=completed.status,
|
||||
|
|
@ -153,7 +165,9 @@ class QueueWorker:
|
|||
with self._lock:
|
||||
if self.is_running():
|
||||
return {"worker_id": self.worker_id, "running": True, "already_running": True}
|
||||
self._stop_event.clear()
|
||||
# A previous thread may have died without clearing state. Always
|
||||
# rebuild a fresh stop event + thread so callers can revive workers.
|
||||
self._stop_event = threading.Event()
|
||||
self._thread = threading.Thread(
|
||||
target=self._run_loop,
|
||||
args=(ctx,),
|
||||
|
|
@ -176,7 +190,17 @@ class QueueWorker:
|
|||
|
||||
def _run_loop(self, ctx: RequestContext) -> None:
|
||||
while not self._stop_event.is_set():
|
||||
try:
|
||||
result = self.run_once(ctx)
|
||||
except Exception:
|
||||
# Provider/business failures must never kill the worker thread.
|
||||
# A dead thread permanently stalls claim of later queued tasks.
|
||||
logger.exception(
|
||||
"ImageStudio queue worker %s crashed while processing; continuing",
|
||||
self.worker_id,
|
||||
)
|
||||
self._stop_event.wait(self.interval_seconds)
|
||||
continue
|
||||
if result.status == "idle":
|
||||
self._stop_event.wait(self.interval_seconds)
|
||||
|
||||
|
|
@ -185,6 +209,8 @@ class QueueWorker:
|
|||
for candidate in queued:
|
||||
if _retry_deferred(candidate):
|
||||
continue
|
||||
if self.task_filter is not None and not self.task_filter(candidate):
|
||||
continue
|
||||
claimed = self.queue_service.claim_queued_task(
|
||||
candidate.task_id,
|
||||
ctx,
|
||||
|
|
@ -195,8 +221,27 @@ class QueueWorker:
|
|||
return None
|
||||
|
||||
def _handle_failure(self, task: GenerationTask, exc: Exception, ctx: RequestContext) -> WorkerRunResult:
|
||||
"""Mark the claimed task failed without ever re-raising.
|
||||
|
||||
Queue progress is more important than perfect failure bookkeeping. Any
|
||||
secondary error while recording failure must leave the worker thread
|
||||
alive so later queued work can still be claimed.
|
||||
"""
|
||||
message = _safe_error_message(exc)
|
||||
latest = task
|
||||
try:
|
||||
latest = self.queue_service.get_job(task.task_id)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"ImageStudio queue worker %s could not reload task %s during failure handling",
|
||||
self.worker_id,
|
||||
task.task_id,
|
||||
)
|
||||
latest = task
|
||||
|
||||
try:
|
||||
if latest.status == "cancelled":
|
||||
self._notify_terminal(task, latest, exc)
|
||||
return WorkerRunResult(
|
||||
worker_id=self.worker_id,
|
||||
status="cancelled",
|
||||
|
|
@ -204,15 +249,33 @@ class QueueWorker:
|
|||
processed=1,
|
||||
error_message=latest.error_message,
|
||||
)
|
||||
message = _safe_error_message(exc)
|
||||
attempt = _int(latest.metadata.get("worker_attempt"), _int(task.metadata.get("worker_attempt"), 1))
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"ImageStudio queue worker %s failed while handling cancelled task %s",
|
||||
self.worker_id,
|
||||
task.task_id,
|
||||
)
|
||||
|
||||
attempt = _int(
|
||||
(latest.metadata or {}).get("worker_attempt"),
|
||||
_int((task.metadata or {}).get("worker_attempt"), 1),
|
||||
)
|
||||
base_metadata = dict(task.metadata or {})
|
||||
try:
|
||||
if isinstance(latest.metadata, dict):
|
||||
base_metadata = {**base_metadata, **latest.metadata}
|
||||
except Exception:
|
||||
base_metadata = dict(task.metadata or {})
|
||||
|
||||
failed = latest
|
||||
try:
|
||||
failed = self.queue_service.update_task(
|
||||
task.task_id,
|
||||
{
|
||||
"status": "failed",
|
||||
"error_message": message,
|
||||
"metadata": {
|
||||
**task.metadata,
|
||||
**base_metadata,
|
||||
"worker": {
|
||||
"worker_id": self.worker_id,
|
||||
"status": "failed",
|
||||
|
|
@ -223,7 +286,25 @@ class QueueWorker:
|
|||
},
|
||||
ctx,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"ImageStudio queue worker %s could not mark task %s as failed",
|
||||
self.worker_id,
|
||||
task.task_id,
|
||||
)
|
||||
self._notify_terminal(task, latest, exc)
|
||||
return WorkerRunResult(
|
||||
worker_id=self.worker_id,
|
||||
status="failed",
|
||||
task_id=task.task_id,
|
||||
processed=1,
|
||||
failed=1,
|
||||
error_message=message,
|
||||
)
|
||||
|
||||
try:
|
||||
if self.retry_policy.should_retry(attempt, exc):
|
||||
try:
|
||||
queued = self.queue_service.update_task(
|
||||
failed.task_id,
|
||||
{
|
||||
|
|
@ -231,7 +312,7 @@ class QueueWorker:
|
|||
"progress": 0,
|
||||
"error_message": None,
|
||||
"metadata": {
|
||||
**failed.metadata,
|
||||
**(failed.metadata or {}),
|
||||
"retry": {
|
||||
"scheduled": True,
|
||||
"attempt": attempt,
|
||||
|
|
@ -244,6 +325,7 @@ class QueueWorker:
|
|||
},
|
||||
ctx,
|
||||
)
|
||||
self._notify_terminal(task, queued, exc)
|
||||
return WorkerRunResult(
|
||||
worker_id=self.worker_id,
|
||||
status="retry_scheduled",
|
||||
|
|
@ -251,14 +333,130 @@ class QueueWorker:
|
|||
processed=1,
|
||||
error_message=message,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"ImageStudio queue worker %s could not requeue task %s after failure",
|
||||
self.worker_id,
|
||||
task.task_id,
|
||||
)
|
||||
self._notify_terminal(task, failed, exc)
|
||||
return WorkerRunResult(
|
||||
worker_id=self.worker_id,
|
||||
status="failed",
|
||||
task_id=failed.task_id,
|
||||
processed=1,
|
||||
failed=1,
|
||||
error_message=failed.error_message,
|
||||
error_message=failed.error_message or message,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"ImageStudio queue worker %s hit a secondary error after failing task %s",
|
||||
self.worker_id,
|
||||
task.task_id,
|
||||
)
|
||||
return WorkerRunResult(
|
||||
worker_id=self.worker_id,
|
||||
status="failed",
|
||||
task_id=task.task_id,
|
||||
processed=1,
|
||||
failed=1,
|
||||
error_message=message,
|
||||
)
|
||||
|
||||
def _notify_terminal(
|
||||
self,
|
||||
claimed_task: GenerationTask,
|
||||
current_task: GenerationTask,
|
||||
exc: Exception | None,
|
||||
) -> None:
|
||||
if self.terminal_callback is None:
|
||||
return
|
||||
try:
|
||||
self.terminal_callback(claimed_task, current_task, exc)
|
||||
except Exception:
|
||||
# Queue state is authoritative. A coordinator callback may be
|
||||
# replayed from persisted task state after restart, so it must not
|
||||
# turn a successfully completed provider request into a failure.
|
||||
return
|
||||
|
||||
|
||||
class QueueWorkerPool:
|
||||
"""Bounded parallel workers sharing one persistent queue."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
queue_service: QueueService,
|
||||
handler: WorkerHandler,
|
||||
*,
|
||||
worker_count: int = 5,
|
||||
worker_id_prefix: str = "imagestudio-round",
|
||||
interval_seconds: float = 0.25,
|
||||
claim_batch_size: int = 25,
|
||||
retry_policy: RetryPolicy | None = None,
|
||||
task_filter: WorkerTaskFilter | None = None,
|
||||
terminal_callback: WorkerTerminalCallback | None = None,
|
||||
per_route_limit: int | None = None,
|
||||
):
|
||||
count = min(max(int(worker_count), 1), 16)
|
||||
bounded_handler = _BoundedWorkerHandler(
|
||||
handler,
|
||||
global_limit=count,
|
||||
per_route_limit=min(max(int(per_route_limit or count), 1), count),
|
||||
)
|
||||
self.workers = [
|
||||
QueueWorker(
|
||||
queue_service,
|
||||
bounded_handler,
|
||||
worker_id=f"{worker_id_prefix}-{index + 1}",
|
||||
interval_seconds=interval_seconds,
|
||||
claim_batch_size=claim_batch_size,
|
||||
retry_policy=retry_policy,
|
||||
task_filter=task_filter,
|
||||
terminal_callback=terminal_callback,
|
||||
)
|
||||
for index in range(count)
|
||||
]
|
||||
|
||||
def start(self, ctx: RequestContext) -> dict[str, Any]:
|
||||
results = [worker.start(ctx) for worker in self.workers]
|
||||
return {
|
||||
"running": any(item["running"] for item in results),
|
||||
"worker_count": len(self.workers),
|
||||
"workers": results,
|
||||
}
|
||||
|
||||
def stop(self, *, timeout_seconds: float = 5.0) -> dict[str, Any]:
|
||||
results = [
|
||||
worker.stop(timeout_seconds=timeout_seconds)
|
||||
for worker in reversed(self.workers)
|
||||
]
|
||||
return {
|
||||
"running": any(item["running"] for item in results),
|
||||
"worker_count": len(self.workers),
|
||||
"workers": list(reversed(results)),
|
||||
}
|
||||
|
||||
def is_running(self) -> bool:
|
||||
return any(worker.is_running() for worker in self.workers)
|
||||
|
||||
|
||||
class _BoundedWorkerHandler:
|
||||
def __init__(self, handler: WorkerHandler, *, global_limit: int, per_route_limit: int):
|
||||
self.handler = handler
|
||||
self.global_semaphore = threading.BoundedSemaphore(global_limit)
|
||||
self.per_route_limit = per_route_limit
|
||||
self._route_guard = threading.Lock()
|
||||
self._route_semaphores: dict[str, threading.BoundedSemaphore] = {}
|
||||
|
||||
def __call__(self, task: GenerationTask) -> WorkerTaskUpdate | dict[str, Any] | None:
|
||||
route_key = _task_route_key(task)
|
||||
with self._route_guard:
|
||||
route_semaphore = self._route_semaphores.setdefault(
|
||||
route_key,
|
||||
threading.BoundedSemaphore(self.per_route_limit),
|
||||
)
|
||||
with self.global_semaphore, route_semaphore:
|
||||
return self.handler(task)
|
||||
|
||||
|
||||
def _normalize_update(value: WorkerTaskUpdate | dict[str, Any] | None) -> WorkerTaskUpdate:
|
||||
|
|
@ -314,5 +512,34 @@ def _optional_text(value: Any) -> str | None:
|
|||
|
||||
|
||||
def _safe_error_message(exc: Exception) -> str:
|
||||
if isinstance(exc, ApiError):
|
||||
code = exc.error_code.value if hasattr(exc.error_code, "value") else str(exc.error_code)
|
||||
parts = [str(exc.message or "").strip(), f"[{code}]"]
|
||||
if isinstance(exc.details, dict) and exc.details:
|
||||
# Keep a short, single-line hint without dumping full provider payloads.
|
||||
hint_keys = ("reason", "finish_reason", "provider", "model")
|
||||
hints = []
|
||||
for key in hint_keys:
|
||||
value = exc.details.get(key)
|
||||
if value is None:
|
||||
continue
|
||||
text_value = " ".join(str(value).split())
|
||||
if text_value:
|
||||
hints.append(f"{key}={text_value[:80]}")
|
||||
if hints:
|
||||
parts.append("(" + ", ".join(hints) + ")")
|
||||
text = " ".join(part for part in parts if part)
|
||||
return text[:500] if text else f"ApiError [{code}]"
|
||||
text = " ".join(str(exc or type(exc).__name__).split())
|
||||
return text[:500] if text else type(exc).__name__
|
||||
|
||||
|
||||
def _task_route_key(task: GenerationTask) -> str:
|
||||
resolution = task.metadata.get("model_route_resolution")
|
||||
if not isinstance(resolution, dict):
|
||||
resolution = task.request.get("resolved_model_route")
|
||||
if not isinstance(resolution, dict):
|
||||
resolution = {}
|
||||
provider_id = _optional_text(resolution.get("provider_id")) or "unresolved"
|
||||
model_id = _optional_text(resolution.get("model_id")) or _optional_text(task.model_route) or "default"
|
||||
return f"{provider_id}:{model_id}"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,525 @@
|
|||
"""Deterministic interpretation of conversational workflow candidate feedback.
|
||||
|
||||
The interpreter deliberately does not query a model and never treats a
|
||||
candidate identifier as trustworthy by itself. Every user- or model-supplied
|
||||
display code is resolved through the candidate whitelist owned by the caller.
|
||||
This makes the module suitable for chat commands without allowing an LLM to
|
||||
invent candidates from another round or workflow.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Mapping, Sequence
|
||||
import re
|
||||
import unicodedata
|
||||
from typing import Any, Literal
|
||||
|
||||
from backend.dto.workflow_feedback_dto import (
|
||||
WorkflowFeedbackCandidateRef,
|
||||
WorkflowFeedbackInterpretation,
|
||||
)
|
||||
|
||||
|
||||
FeedbackAction = Literal["liked", "rejected", "strict_rejected", "final"]
|
||||
|
||||
|
||||
class WorkflowFeedbackInterpretationError(ValueError):
|
||||
"""Raised when feedback cannot be resolved safely and deterministically."""
|
||||
|
||||
def __init__(self, reason: str, message: str, details: Mapping[str, Any] | None = None) -> None:
|
||||
super().__init__(message)
|
||||
self.reason = reason
|
||||
self.details = dict(details or {})
|
||||
|
||||
|
||||
_ACTION_RE = re.compile(
|
||||
r"(?P<strict_rejected>严格拒绝|强烈拒绝|彻底拒绝|\b(?:strict|hard)\s+reject(?:ed)?\b)"
|
||||
r"|(?P<rejected>不喜欢|不要|拒绝|淘汰|排除|删除|"
|
||||
r"\b(?:reject(?:ed)?|discard(?:ed)?|drop(?:ped)?|remove(?:d)?|exclude(?:d)?)\b)"
|
||||
r"|(?P<final>最终采用|确认采用|采用|定稿|选定|就用|"
|
||||
r"\b(?:adopt(?:ed)?|final(?:ize|ise|ized|ised)?|select(?:ed)?|use)\b)"
|
||||
r"|(?P<liked>保留|喜欢|偏好|可用|"
|
||||
r"\b(?:keep|like(?:d)?|prefer(?:red)?|approve(?:d)?)\b)",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
_DISPLAY_CODE_OR_NUMBER_RE = re.compile(
|
||||
r"(?<![A-Za-z0-9])"
|
||||
r"(?:"
|
||||
r"(?=[A-Za-z0-9-]*\d)[A-Za-z][A-Za-z0-9]*(?:-[A-Za-z0-9]+)+"
|
||||
r"|\d{1,3}"
|
||||
r")"
|
||||
r"(?![A-Za-z0-9])",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
_CHINESE_ORDINAL_RE = re.compile(
|
||||
r"(?:候选|第)(?P<prefixed>[一二三四五六七八九十])(?:张|号)?"
|
||||
r"|(?P<suffixed>[一二三四五六七八九十])(?:张|号)",
|
||||
)
|
||||
|
||||
_NEXT_ROUND_MARKER_RE = re.compile(
|
||||
r"(?:下一轮|下轮|下一版|next\s+(?:round|batch))"
|
||||
r"(?:\s*(?:提示词|prompt(?:\s+patch)?))?",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
_MATERIAL_MARKER_RE = re.compile(
|
||||
r"(?:素材(?:意图|要求)?|参考素材|material(?:\s+intent)?|reference\s+material)",
|
||||
re.IGNORECASE,
|
||||
)
|
||||
|
||||
_CHINESE_NUMBERS = {
|
||||
"一": 1,
|
||||
"二": 2,
|
||||
"三": 3,
|
||||
"四": 4,
|
||||
"五": 5,
|
||||
"六": 6,
|
||||
"七": 7,
|
||||
"八": 8,
|
||||
"九": 9,
|
||||
"十": 10,
|
||||
}
|
||||
|
||||
|
||||
class _CandidateWhitelist:
|
||||
def __init__(self, candidates: Mapping[str, str]) -> None:
|
||||
self._exact: dict[str, WorkflowFeedbackCandidateRef] = {}
|
||||
self._numeric_aliases: dict[str, list[WorkflowFeedbackCandidateRef]] = {}
|
||||
for display_code, candidate_id in candidates.items():
|
||||
code = str(display_code or "").strip()
|
||||
identifier = str(candidate_id or "").strip()
|
||||
if not code or not identifier:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_candidate_whitelist",
|
||||
"candidate whitelist entries require display_code and candidate_id",
|
||||
{"display_code": code, "candidate_id": identifier},
|
||||
)
|
||||
normalized_code = _normalize_display_code(code)
|
||||
current = self._exact.get(normalized_code)
|
||||
if current and current.candidate_id != identifier:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_candidate_whitelist",
|
||||
"candidate whitelist contains duplicate display codes",
|
||||
{"display_code": code},
|
||||
)
|
||||
reference = current or WorkflowFeedbackCandidateRef(
|
||||
candidate_id=identifier,
|
||||
display_code=code,
|
||||
)
|
||||
self._exact[normalized_code] = reference
|
||||
terminal_index = _terminal_candidate_index(normalized_code)
|
||||
if terminal_index is not None:
|
||||
alias = str(terminal_index)
|
||||
aliases = self._numeric_aliases.setdefault(alias, [])
|
||||
if all(item.candidate_id != reference.candidate_id for item in aliases):
|
||||
aliases.append(reference)
|
||||
|
||||
@property
|
||||
def allowed_display_codes(self) -> tuple[str, ...]:
|
||||
return tuple(reference.display_code for reference in self._exact.values())
|
||||
|
||||
def resolve(self, raw_token: str) -> WorkflowFeedbackCandidateRef:
|
||||
token = _normalize_reference_token(raw_token)
|
||||
exact = self._exact.get(token)
|
||||
if exact:
|
||||
return exact
|
||||
if token.isdigit():
|
||||
aliases = self._numeric_aliases.get(str(int(token)), [])
|
||||
if len(aliases) == 1:
|
||||
return aliases[0]
|
||||
if len(aliases) > 1:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"ambiguous_candidate_reference",
|
||||
f"candidate reference {raw_token!r} is ambiguous",
|
||||
{
|
||||
"reference": raw_token,
|
||||
"matching_display_codes": [item.display_code for item in aliases],
|
||||
},
|
||||
)
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"unknown_candidate_reference",
|
||||
f"candidate reference {raw_token!r} is not in the current whitelist",
|
||||
{
|
||||
"reference": raw_token,
|
||||
"allowed_display_codes": list(self.allowed_display_codes),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class WorkflowFeedbackInterpreter:
|
||||
"""Interpret chat feedback and validate optional LLM-structured output."""
|
||||
|
||||
def interpret(
|
||||
self,
|
||||
text: str,
|
||||
*,
|
||||
candidate_whitelist: Mapping[str, str],
|
||||
) -> WorkflowFeedbackInterpretation:
|
||||
source_text = str(text or "").strip()
|
||||
normalized_text = _normalize_text(source_text)
|
||||
candidates = _CandidateWhitelist(candidate_whitelist)
|
||||
actions: dict[FeedbackAction, list[WorkflowFeedbackCandidateRef]] = {
|
||||
"liked": [],
|
||||
"rejected": [],
|
||||
"strict_rejected": [],
|
||||
"final": [],
|
||||
}
|
||||
|
||||
pending_tokens: list[str] = []
|
||||
active_prefix_action: FeedbackAction | None = None
|
||||
for fragment in _feedback_fragments(normalized_text):
|
||||
if _contains_directive_marker(fragment):
|
||||
pending_tokens = []
|
||||
active_prefix_action = None
|
||||
continue
|
||||
action_matches = list(_ACTION_RE.finditer(fragment))
|
||||
candidate_tokens = _candidate_tokens(fragment)
|
||||
if not action_matches:
|
||||
if candidate_tokens:
|
||||
if active_prefix_action:
|
||||
self._append_resolved(
|
||||
actions,
|
||||
active_prefix_action,
|
||||
candidate_tokens,
|
||||
candidates,
|
||||
)
|
||||
else:
|
||||
pending_tokens.extend(candidate_tokens)
|
||||
continue
|
||||
if len(action_matches) > 1:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"ambiguous_feedback_clause",
|
||||
"one feedback clause contains multiple actions; split it with punctuation",
|
||||
{"clause": fragment},
|
||||
)
|
||||
|
||||
action_match = action_matches[0]
|
||||
action = _action_from_match(action_match)
|
||||
first_candidate_start = _first_candidate_start(fragment)
|
||||
action_is_prefix = first_candidate_start is None or action_match.start() <= first_candidate_start
|
||||
tokens = [*pending_tokens, *candidate_tokens]
|
||||
pending_tokens = []
|
||||
if not tokens:
|
||||
active_prefix_action = None
|
||||
continue
|
||||
self._append_resolved(actions, action, tokens, candidates)
|
||||
active_prefix_action = action if action_is_prefix else None
|
||||
|
||||
material_intent, next_round_prompt_patch = _extract_directives(normalized_text)
|
||||
_validate_action_conflicts(actions)
|
||||
final_candidates = actions["final"]
|
||||
if len(final_candidates) > 1:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"multiple_final_candidates",
|
||||
"only one candidate can be marked final",
|
||||
{"display_codes": [candidate.display_code for candidate in final_candidates]},
|
||||
)
|
||||
return WorkflowFeedbackInterpretation(
|
||||
liked=tuple(actions["liked"]),
|
||||
rejected=tuple(actions["rejected"]),
|
||||
strict_rejected=tuple(actions["strict_rejected"]),
|
||||
final=final_candidates[0] if final_candidates else None,
|
||||
material_intent=material_intent,
|
||||
next_round_prompt_patch=next_round_prompt_patch,
|
||||
source="deterministic",
|
||||
source_text=source_text,
|
||||
)
|
||||
|
||||
def validate_llm_result(
|
||||
self,
|
||||
payload: Mapping[str, Any],
|
||||
*,
|
||||
candidate_whitelist: Mapping[str, str],
|
||||
source_text: str = "",
|
||||
) -> WorkflowFeedbackInterpretation:
|
||||
"""Validate offline LLM output without trusting its candidate IDs.
|
||||
|
||||
Candidate fields must contain display codes (or objects with a
|
||||
``display_code`` key). A raw ``candidate_id`` is intentionally not
|
||||
accepted because it bypasses the caller-owned round whitelist.
|
||||
"""
|
||||
|
||||
if not isinstance(payload, Mapping):
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_llm_result",
|
||||
"LLM feedback result must be an object",
|
||||
)
|
||||
_reject_llm_candidate_ids(payload)
|
||||
candidates = _CandidateWhitelist(candidate_whitelist)
|
||||
actions: dict[FeedbackAction, list[WorkflowFeedbackCandidateRef]] = {
|
||||
"liked": [],
|
||||
"rejected": [],
|
||||
"strict_rejected": [],
|
||||
"final": [],
|
||||
}
|
||||
for action in ("liked", "rejected", "strict_rejected", "final"):
|
||||
tokens = _llm_display_codes(payload.get(action), field=action)
|
||||
self._append_resolved(actions, action, tokens, candidates)
|
||||
|
||||
_validate_action_conflicts(actions)
|
||||
final_candidates = actions["final"]
|
||||
if len(final_candidates) > 1:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"multiple_final_candidates",
|
||||
"only one candidate can be marked final",
|
||||
{"display_codes": [candidate.display_code for candidate in final_candidates]},
|
||||
)
|
||||
positive_patch = _optional_text(
|
||||
payload.get("positive_patch") or payload.get("next_round_prompt_patch"),
|
||||
field="positive_patch",
|
||||
)
|
||||
return WorkflowFeedbackInterpretation(
|
||||
liked=tuple(actions["liked"]),
|
||||
rejected=tuple(actions["rejected"]),
|
||||
strict_rejected=tuple(actions["strict_rejected"]),
|
||||
final=final_candidates[0] if final_candidates else None,
|
||||
material_intent=_optional_text(payload.get("material_intent"), field="material_intent"),
|
||||
next_round_prompt_patch=_optional_text(
|
||||
payload.get("next_round_prompt_patch") or payload.get("positive_patch"),
|
||||
field="next_round_prompt_patch",
|
||||
),
|
||||
positive_patch=positive_patch,
|
||||
negative_patch=_optional_text(payload.get("negative_patch"), field="negative_patch"),
|
||||
source="llm_validated",
|
||||
source_text=str(source_text or "").strip(),
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _append_resolved(
|
||||
actions: dict[FeedbackAction, list[WorkflowFeedbackCandidateRef]],
|
||||
action: FeedbackAction,
|
||||
tokens: Sequence[str],
|
||||
candidates: _CandidateWhitelist,
|
||||
) -> None:
|
||||
target = actions[action]
|
||||
known_ids = {candidate.candidate_id for candidate in target}
|
||||
for token in tokens:
|
||||
candidate = candidates.resolve(token)
|
||||
if candidate.candidate_id not in known_ids:
|
||||
target.append(candidate)
|
||||
known_ids.add(candidate.candidate_id)
|
||||
|
||||
|
||||
def interpret_workflow_feedback(
|
||||
text: str,
|
||||
*,
|
||||
candidate_whitelist: Mapping[str, str],
|
||||
) -> WorkflowFeedbackInterpretation:
|
||||
"""Convenience entry point for deterministic interpretation."""
|
||||
|
||||
return WorkflowFeedbackInterpreter().interpret(
|
||||
text,
|
||||
candidate_whitelist=candidate_whitelist,
|
||||
)
|
||||
|
||||
|
||||
def _normalize_text(value: str) -> str:
|
||||
return unicodedata.normalize("NFKC", value).strip()
|
||||
|
||||
|
||||
def _normalize_display_code(value: str) -> str:
|
||||
return _normalize_text(value).upper()
|
||||
|
||||
|
||||
def _normalize_reference_token(value: str) -> str:
|
||||
token = _normalize_text(value).strip("#[](){}<>《》【】 ")
|
||||
token = re.sub(r"^(?:候选|第|candidate\s*#?|no\.?\s*)", "", token, flags=re.IGNORECASE)
|
||||
token = re.sub(r"(?:张|号)$", "", token)
|
||||
if token in _CHINESE_NUMBERS:
|
||||
token = str(_CHINESE_NUMBERS[token])
|
||||
return token.upper()
|
||||
|
||||
|
||||
def _terminal_candidate_index(display_code: str) -> int | None:
|
||||
match = re.search(r"(?:^|[-_])(?:[A-Z]*)(\d+)$", display_code, flags=re.IGNORECASE)
|
||||
if not match:
|
||||
return int(display_code) if display_code.isdigit() else None
|
||||
return int(match.group(1))
|
||||
|
||||
|
||||
def _feedback_fragments(text: str) -> tuple[str, ...]:
|
||||
return tuple(fragment.strip() for fragment in re.split(r"[,,;;。\n]+", text) if fragment.strip())
|
||||
|
||||
|
||||
def _candidate_token_matches(fragment: str) -> list[tuple[int, int, str]]:
|
||||
matches = [
|
||||
(match.start(), match.end(), match.group(0))
|
||||
for match in _DISPLAY_CODE_OR_NUMBER_RE.finditer(fragment)
|
||||
]
|
||||
for match in _CHINESE_ORDINAL_RE.finditer(fragment):
|
||||
value = match.group("prefixed") or match.group("suffixed")
|
||||
matches.append((match.start(), match.end(), str(_CHINESE_NUMBERS[value])))
|
||||
matches.sort(key=lambda item: (item[0], -(item[1] - item[0])))
|
||||
deduplicated: list[tuple[int, int, str]] = []
|
||||
for item in matches:
|
||||
if any(item[0] < current[1] and item[1] > current[0] for current in deduplicated):
|
||||
continue
|
||||
deduplicated.append(item)
|
||||
return deduplicated
|
||||
|
||||
|
||||
def _candidate_tokens(fragment: str) -> list[str]:
|
||||
return [item[2] for item in _candidate_token_matches(fragment)]
|
||||
|
||||
|
||||
def _first_candidate_start(fragment: str) -> int | None:
|
||||
matches = _candidate_token_matches(fragment)
|
||||
return matches[0][0] if matches else None
|
||||
|
||||
|
||||
def _action_from_match(match: re.Match[str]) -> FeedbackAction:
|
||||
for action in ("strict_rejected", "rejected", "final", "liked"):
|
||||
if match.group(action) is not None:
|
||||
return action # type: ignore[return-value]
|
||||
raise AssertionError("feedback action regex produced no named group")
|
||||
|
||||
|
||||
def _contains_directive_marker(fragment: str) -> bool:
|
||||
return bool(_NEXT_ROUND_MARKER_RE.search(fragment) or _MATERIAL_MARKER_RE.search(fragment))
|
||||
|
||||
|
||||
def _extract_directives(text: str) -> tuple[str | None, str | None]:
|
||||
material_values: list[str] = []
|
||||
next_round_values: list[str] = []
|
||||
for fragment in _feedback_fragments(text):
|
||||
markers: list[tuple[int, int, Literal["material", "next_round"]]] = []
|
||||
markers.extend(
|
||||
(match.start(), match.end(), "material")
|
||||
for match in _MATERIAL_MARKER_RE.finditer(fragment)
|
||||
)
|
||||
markers.extend(
|
||||
(match.start(), match.end(), "next_round")
|
||||
for match in _NEXT_ROUND_MARKER_RE.finditer(fragment)
|
||||
)
|
||||
markers.sort(key=lambda item: item[0])
|
||||
for index, (_, marker_end, kind) in enumerate(markers):
|
||||
value_end = markers[index + 1][0] if index + 1 < len(markers) else len(fragment)
|
||||
value = fragment[marker_end:value_end]
|
||||
value = _clean_directive_value(value, kind=kind)
|
||||
if not value:
|
||||
continue
|
||||
target = material_values if kind == "material" else next_round_values
|
||||
if value not in target:
|
||||
target.append(value)
|
||||
return _join_directive_values(material_values), _join_directive_values(next_round_values)
|
||||
|
||||
|
||||
def _clean_directive_value(value: str, *, kind: Literal["material", "next_round"]) -> str:
|
||||
cleaned = value.strip(" \t::=-")
|
||||
if kind == "material":
|
||||
cleaned = re.sub(r"^(?:请|要|用|使用|选择|改成|设为)\s*", "", cleaned, flags=re.IGNORECASE)
|
||||
else:
|
||||
cleaned = re.sub(r"^(?:请|要|改成|调整为|设为)\s*", "", cleaned, flags=re.IGNORECASE)
|
||||
cleaned = re.sub(r"(?:并且|以及|和|and)\s*$", "", cleaned, flags=re.IGNORECASE)
|
||||
return cleaned.strip(" \t::=-") or ""
|
||||
|
||||
|
||||
def _join_directive_values(values: Sequence[str]) -> str | None:
|
||||
return ";".join(values) if values else None
|
||||
|
||||
|
||||
def _llm_display_codes(value: Any, *, field: str) -> list[str]:
|
||||
if value is None or value == "":
|
||||
return []
|
||||
items: Sequence[Any]
|
||||
if isinstance(value, (str, Mapping)):
|
||||
items = [value]
|
||||
elif isinstance(value, Sequence) and not isinstance(value, (bytes, bytearray)):
|
||||
items = value
|
||||
else:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_llm_result",
|
||||
f"LLM field {field!r} must contain display codes",
|
||||
{"field": field},
|
||||
)
|
||||
display_codes: list[str] = []
|
||||
for item in items:
|
||||
if isinstance(item, str):
|
||||
display_code = item.strip()
|
||||
elif isinstance(item, Mapping):
|
||||
raw_display_code = item.get("display_code", item.get("code"))
|
||||
display_code = raw_display_code.strip() if isinstance(raw_display_code, str) else ""
|
||||
else:
|
||||
display_code = ""
|
||||
if not display_code:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_llm_result",
|
||||
f"LLM field {field!r} contains an item without display_code",
|
||||
{"field": field},
|
||||
)
|
||||
display_codes.append(display_code)
|
||||
return display_codes
|
||||
|
||||
|
||||
def _reject_llm_candidate_ids(value: Any, *, path: str = "$") -> None:
|
||||
"""Reject model output that tries to bypass display-code resolution.
|
||||
|
||||
Candidate IDs are server-owned opaque identifiers. Even when an object
|
||||
also contains a valid display code, accepting the ID would make the
|
||||
contract ambiguous and encourages callers to trust model-invented IDs.
|
||||
"""
|
||||
|
||||
if isinstance(value, Mapping):
|
||||
for key, item in value.items():
|
||||
normalized = str(key).strip().lower()
|
||||
if normalized in {"candidate_id", "candidate_ids"}:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_llm_result",
|
||||
"LLM feedback must reference candidates by display_code only",
|
||||
{"field": f"{path}.{key}", "reason": "llm_candidate_id_forbidden"},
|
||||
)
|
||||
_reject_llm_candidate_ids(item, path=f"{path}.{key}")
|
||||
elif isinstance(value, Sequence) and not isinstance(value, (str, bytes, bytearray)):
|
||||
for index, item in enumerate(value):
|
||||
_reject_llm_candidate_ids(item, path=f"{path}[{index}]")
|
||||
|
||||
|
||||
def _optional_text(value: Any, *, field: str) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
if not isinstance(value, str):
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_llm_result",
|
||||
f"LLM field {field!r} must be text or null",
|
||||
{"field": field},
|
||||
)
|
||||
normalized = value.strip()
|
||||
return normalized or None
|
||||
|
||||
|
||||
def _validate_action_conflicts(
|
||||
actions: Mapping[FeedbackAction, Sequence[WorkflowFeedbackCandidateRef]],
|
||||
) -> None:
|
||||
feedback_kinds = ("liked", "rejected", "strict_rejected")
|
||||
ownership: dict[str, str] = {}
|
||||
for kind in feedback_kinds:
|
||||
for candidate in actions[kind]:
|
||||
previous = ownership.get(candidate.candidate_id)
|
||||
if previous and previous != kind:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"conflicting_candidate_feedback",
|
||||
"one candidate cannot receive conflicting feedback actions",
|
||||
{
|
||||
"candidate_id": candidate.candidate_id,
|
||||
"display_code": candidate.display_code,
|
||||
"actions": [previous, kind],
|
||||
},
|
||||
)
|
||||
ownership[candidate.candidate_id] = kind
|
||||
rejected_ids = {
|
||||
candidate.candidate_id
|
||||
for kind in ("rejected", "strict_rejected")
|
||||
for candidate in actions[kind]
|
||||
}
|
||||
for candidate in actions["final"]:
|
||||
if candidate.candidate_id in rejected_ids:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"final_candidate_rejected",
|
||||
"a rejected candidate cannot be marked final",
|
||||
{
|
||||
"candidate_id": candidate.candidate_id,
|
||||
"display_code": candidate.display_code,
|
||||
},
|
||||
)
|
||||
|
|
@ -0,0 +1,781 @@
|
|||
"""Generation-based cross-review orchestration for detailed character rounds."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.services.generation_cross_review_service import (
|
||||
GENERATION_CROSS_REVIEW_VERSION,
|
||||
GenerationCrossReviewPlanner,
|
||||
)
|
||||
from backend.services.workflow_round_service import WorkflowRoundService
|
||||
from core.audit_service import AuditService
|
||||
from core.identity import RequestContext
|
||||
from database.repositories.workflow_cross_review_repository import WorkflowCrossReviewRepository
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
from database.repositories.workflow_round_repository import (
|
||||
WorkflowRoundRepository,
|
||||
WorkflowRoundWriteError,
|
||||
)
|
||||
|
||||
|
||||
class WorkflowGenerationCrossReviewService:
|
||||
"""Coordinate review decisions, model-diverse rounds and final approval.
|
||||
|
||||
VLM inspection remains in ``VisualQualityReviewService``. This service
|
||||
only creates image-generation candidates and therefore rejects routes with
|
||||
any other modality before a task is persisted.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
cross_review_repository: WorkflowCrossReviewRepository,
|
||||
round_repository: WorkflowRoundRepository,
|
||||
workflow_repository: WorkflowRepository,
|
||||
round_service: WorkflowRoundService,
|
||||
planner: GenerationCrossReviewPlanner,
|
||||
audit_service: AuditService,
|
||||
config: dict[str, Any] | None = None,
|
||||
):
|
||||
self.cross_review_repository = cross_review_repository
|
||||
self.round_repository = round_repository
|
||||
self.workflow_repository = workflow_repository
|
||||
self.round_service = round_service
|
||||
self.planner = planner
|
||||
self.audit_service = audit_service
|
||||
self.config = config or {}
|
||||
|
||||
def decide(
|
||||
self,
|
||||
source_round_id: str,
|
||||
payload: dict[str, Any],
|
||||
ctx: RequestContext,
|
||||
) -> dict[str, Any]:
|
||||
decision = str(payload.get("decision") or payload.get("action") or "").strip().lower()
|
||||
if decision not in {"review", "skip"}:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"cross-review decision must be review or skip",
|
||||
{"field": "decision", "allowed": ["review", "skip"]},
|
||||
)
|
||||
candidate_id = _required_text(payload.get("candidate_id"), "candidate_id")
|
||||
idempotency_key = _bounded_text(payload.get("idempotency_key"), "idempotency_key", 200)
|
||||
if decision == "skip":
|
||||
return self._skip(
|
||||
source_round_id,
|
||||
candidate_id=candidate_id,
|
||||
idempotency_key=idempotency_key,
|
||||
ctx=ctx,
|
||||
)
|
||||
return self._request_review(
|
||||
source_round_id,
|
||||
candidate_id=candidate_id,
|
||||
idempotency_key=idempotency_key,
|
||||
payload=payload,
|
||||
ctx=ctx,
|
||||
)
|
||||
|
||||
def regenerate(
|
||||
self,
|
||||
parent_round_id: str,
|
||||
payload: dict[str, Any],
|
||||
ctx: RequestContext,
|
||||
) -> dict[str, Any]:
|
||||
parent_detail = self.round_service.get_round(parent_round_id, ctx)
|
||||
parent = parent_detail.round_record
|
||||
if parent.round_kind != "cross_review":
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"only a cross-review round can regenerate review candidates",
|
||||
{"round_id": parent_round_id, "round_kind": parent.round_kind},
|
||||
)
|
||||
selected_ids = _string_list(payload.get("candidate_ids"))
|
||||
if not selected_ids:
|
||||
single = _optional_text(payload.get("candidate_id"))
|
||||
selected_ids = [single] if single else []
|
||||
if not selected_ids:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"select at least one cross-review candidate to regenerate",
|
||||
{"field": "candidate_ids", "minimum": 1},
|
||||
)
|
||||
prior_candidates: list[dict[str, Any]] = []
|
||||
for view in parent_detail.candidates:
|
||||
candidate = view.candidate
|
||||
if candidate.generation_status != "succeeded" or not candidate.image_id:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"every preserved review slot must have a usable image before creating a follow-up",
|
||||
{
|
||||
"round_id": parent_round_id,
|
||||
"candidate_id": candidate.candidate_id,
|
||||
"generation_status": candidate.generation_status,
|
||||
"required_action": "retry_or_remove_failed_review_candidate",
|
||||
},
|
||||
)
|
||||
prior_candidates.append(
|
||||
{
|
||||
"candidate_id": candidate.candidate_id,
|
||||
"candidate_index": candidate.candidate_index,
|
||||
"image_id": candidate.image_id,
|
||||
"model_route": candidate.model_route,
|
||||
"provider_id": candidate.provider_id,
|
||||
"provider_model": candidate.provider_model,
|
||||
"prompt_snapshot": candidate.prompt_snapshot,
|
||||
}
|
||||
)
|
||||
plan = self.planner.plan_followup(
|
||||
candidates=prior_candidates,
|
||||
selected_for_regeneration=selected_ids,
|
||||
)
|
||||
node, workflow = self._node_and_workflow(parent.node_id, ctx)
|
||||
prompt_patch = _bounded_text(payload.get("prompt_patch"), "prompt_patch", 8_000)
|
||||
negative_patch = _bounded_text(payload.get("negative_prompt_patch"), "negative_prompt_patch", 8_000)
|
||||
route_summaries = self._route_summaries(plan)
|
||||
parent_by_id = {item.candidate.candidate_id: item.candidate for item in parent_detail.candidates}
|
||||
candidate_payloads: list[dict[str, Any]] = []
|
||||
for item in plan["candidates"]:
|
||||
source = parent_by_id[str(item["source_candidate_id"])]
|
||||
route = route_summaries[str(item["model_route"])]
|
||||
prompt_snapshot = _patched_prompt_snapshot(source.prompt_snapshot, prompt_patch, negative_patch)
|
||||
generated = bool(item["generate"])
|
||||
prepared = (
|
||||
self._prepared_generation(
|
||||
node=node,
|
||||
workflow=workflow,
|
||||
route=route,
|
||||
prompt_snapshot=prompt_snapshot,
|
||||
reference_snapshot=parent.reference_snapshot,
|
||||
source_image_id=str(item["source_image_id"]),
|
||||
source_round_id=parent_round_id,
|
||||
payload=payload,
|
||||
)
|
||||
if generated
|
||||
else None
|
||||
)
|
||||
candidate_payloads.append(
|
||||
self._candidate_payload(
|
||||
plan_item=item,
|
||||
prompt_snapshot=prompt_snapshot,
|
||||
prepared=prepared,
|
||||
preserve_image_id=None if generated else str(item["source_image_id"]),
|
||||
)
|
||||
)
|
||||
root_round_id = _optional_text(parent.metadata.get("root_source_round_id"))
|
||||
if not root_round_id:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"cross-review round is missing its primary source reference",
|
||||
{"round_id": parent_round_id, "reason": "cross_review_source_missing"},
|
||||
)
|
||||
round_id = f"round_{uuid4().hex}"
|
||||
idempotency_key = _bounded_text(payload.get("idempotency_key"), "idempotency_key", 200)
|
||||
round_payload = {
|
||||
"round_id": round_id,
|
||||
"workflow_id": parent.workflow_id,
|
||||
"node_id": parent.node_id,
|
||||
"branch_name": parent.branch_name,
|
||||
"parent_round_id": parent_round_id,
|
||||
"display_prefix": _display_prefix(parent.display_code),
|
||||
"status": "draft",
|
||||
"idempotency_key": idempotency_key,
|
||||
"prompt_snapshot": _patched_prompt_snapshot(parent.prompt_snapshot, prompt_patch, negative_patch),
|
||||
"reference_snapshot": parent.reference_snapshot,
|
||||
"feedback_snapshot": {
|
||||
"selected_for_regeneration": selected_ids,
|
||||
"prompt_patch": prompt_patch,
|
||||
"negative_prompt_patch": negative_patch,
|
||||
},
|
||||
"metadata": {
|
||||
"schema_version": GENERATION_CROSS_REVIEW_VERSION,
|
||||
"root_source_round_id": root_round_id,
|
||||
"parent_review_round_id": parent_round_id,
|
||||
"review_plan": plan,
|
||||
"created_by": ctx.actor_id or "system",
|
||||
**_chat_notification_metadata(
|
||||
payload,
|
||||
parent.metadata,
|
||||
allow_payload=str(ctx.source or "").startswith("astrbot_chat:"),
|
||||
),
|
||||
},
|
||||
}
|
||||
try:
|
||||
created = self.cross_review_repository.create_followup_round_atomic(
|
||||
parent_round_id=parent_round_id,
|
||||
round_payload=round_payload,
|
||||
candidate_payloads=candidate_payloads,
|
||||
selected_candidate_ids=selected_ids,
|
||||
requested_by=ctx.actor_id or "system",
|
||||
)
|
||||
except WorkflowRoundWriteError as exc:
|
||||
raise _write_error(exc) from exc
|
||||
self._ensure_enqueued(str(created["round_id"]), ctx)
|
||||
result = {
|
||||
**self.round_service.get_round(str(created["round_id"]), ctx).to_dict(),
|
||||
"review_operation": created,
|
||||
}
|
||||
self.audit_service.record(
|
||||
ctx=ctx,
|
||||
action="create_generation_cross_review_followup",
|
||||
target_type="workflow_generation_round",
|
||||
target_id=str(created["round_id"]),
|
||||
after_snapshot={
|
||||
"parent_round_id": parent_round_id,
|
||||
"selected_candidate_ids": selected_ids,
|
||||
"created": bool(created.get("created")),
|
||||
},
|
||||
)
|
||||
return result
|
||||
|
||||
def approve(
|
||||
self,
|
||||
round_id: str,
|
||||
payload: dict[str, Any],
|
||||
ctx: RequestContext,
|
||||
) -> dict[str, Any]:
|
||||
self.round_service.get_round(round_id, ctx)
|
||||
candidate_id = _required_text(payload.get("candidate_id"), "candidate_id")
|
||||
idempotency_key = _bounded_text(payload.get("idempotency_key"), "idempotency_key", 200)
|
||||
try:
|
||||
approval = self.cross_review_repository.approve_candidate_atomic(
|
||||
round_id=round_id,
|
||||
candidate_id=candidate_id,
|
||||
idempotency_key=idempotency_key,
|
||||
approved_by=ctx.actor_id or "system",
|
||||
)
|
||||
except WorkflowRoundWriteError as exc:
|
||||
raise _write_error(exc) from exc
|
||||
self.audit_service.record(
|
||||
ctx=ctx,
|
||||
action="approve_generation_cross_review_candidate",
|
||||
target_type="workflow_generation_round",
|
||||
target_id=round_id,
|
||||
after_snapshot=approval,
|
||||
)
|
||||
return {
|
||||
**self.round_service.get_round(round_id, ctx).to_dict(),
|
||||
"approval": approval,
|
||||
"next_node": (
|
||||
self.workflow_repository.get_node(str(approval["next_node_id"])).to_dict()
|
||||
if approval.get("next_node_id") and self.workflow_repository.get_node(str(approval["next_node_id"]))
|
||||
else None
|
||||
),
|
||||
}
|
||||
|
||||
def _request_review(
|
||||
self,
|
||||
source_round_id: str,
|
||||
*,
|
||||
candidate_id: str,
|
||||
idempotency_key: str | None,
|
||||
payload: dict[str, Any],
|
||||
ctx: RequestContext,
|
||||
) -> dict[str, Any]:
|
||||
source_detail = self.round_service.get_round(source_round_id, ctx)
|
||||
source = source_detail.round_record
|
||||
if source.round_kind != "primary":
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"initial cross review must start from a primary round",
|
||||
{"round_id": source_round_id, "round_kind": source.round_kind},
|
||||
)
|
||||
source_view = next((item for item in source_detail.candidates if item.candidate.candidate_id == candidate_id), None)
|
||||
if source_view is None:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"candidate does not belong to the primary round",
|
||||
{"round_id": source_round_id, "candidate_id": candidate_id},
|
||||
)
|
||||
candidate = source_view.candidate
|
||||
if candidate.generation_status != "succeeded" or not candidate.image_id:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"only a succeeded primary candidate can enter cross review",
|
||||
{"candidate_id": candidate_id, "generation_status": candidate.generation_status},
|
||||
)
|
||||
primary_route = _optional_text(candidate.model_route or source.prompt_snapshot.get("model_route"))
|
||||
if not primary_route:
|
||||
raise ApiError(
|
||||
ErrorCode.MODEL_CAPABILITY_UNCONFIRMED,
|
||||
"primary candidate has no image-generation route snapshot",
|
||||
{
|
||||
"candidate_id": candidate_id,
|
||||
"required_action": "regenerate_primary_candidate_with_configured_route",
|
||||
},
|
||||
)
|
||||
configured_routes = self._configured_routes(payload)
|
||||
plan = self.planner.plan(
|
||||
primary_route=primary_route,
|
||||
configured_routes=configured_routes,
|
||||
source_candidate_id=candidate_id,
|
||||
source_image_id=candidate.image_id,
|
||||
)
|
||||
node, workflow = self._node_and_workflow(source.node_id, ctx)
|
||||
prompt_patch = _bounded_text(payload.get("prompt_patch"), "prompt_patch", 8_000)
|
||||
negative_patch = _bounded_text(payload.get("negative_prompt_patch"), "negative_prompt_patch", 8_000)
|
||||
prompt_snapshot = _patched_prompt_snapshot(candidate.prompt_snapshot or source.prompt_snapshot, prompt_patch, negative_patch)
|
||||
route_summaries = self._route_summaries(plan)
|
||||
candidate_payloads: list[dict[str, Any]] = []
|
||||
for item in plan["candidates"]:
|
||||
route = route_summaries[str(item["model_route"])]
|
||||
generated = bool(item["generate"])
|
||||
prepared = (
|
||||
self._prepared_generation(
|
||||
node=node,
|
||||
workflow=workflow,
|
||||
route=route,
|
||||
prompt_snapshot=prompt_snapshot,
|
||||
reference_snapshot=source.reference_snapshot,
|
||||
source_image_id=candidate.image_id,
|
||||
source_round_id=source_round_id,
|
||||
payload=payload,
|
||||
)
|
||||
if generated
|
||||
else None
|
||||
)
|
||||
candidate_payloads.append(
|
||||
self._candidate_payload(
|
||||
plan_item=item,
|
||||
prompt_snapshot=prompt_snapshot,
|
||||
prepared=prepared,
|
||||
preserve_image_id=candidate.image_id if not generated else None,
|
||||
)
|
||||
)
|
||||
round_id = f"round_{uuid4().hex}"
|
||||
round_payload = {
|
||||
"round_id": round_id,
|
||||
"workflow_id": source.workflow_id,
|
||||
"node_id": source.node_id,
|
||||
"branch_name": source.branch_name,
|
||||
"parent_round_id": source_round_id,
|
||||
"display_prefix": f"{_display_prefix(source.display_code)}SC",
|
||||
"status": "draft",
|
||||
"idempotency_key": idempotency_key,
|
||||
"prompt_snapshot": prompt_snapshot,
|
||||
"reference_snapshot": source.reference_snapshot,
|
||||
"feedback_snapshot": {
|
||||
"decision": "review",
|
||||
"source_candidate_id": candidate_id,
|
||||
"prompt_patch": prompt_patch,
|
||||
"negative_prompt_patch": negative_patch,
|
||||
},
|
||||
"metadata": {
|
||||
"schema_version": GENERATION_CROSS_REVIEW_VERSION,
|
||||
"root_source_round_id": source_round_id,
|
||||
"source_candidate_id": candidate_id,
|
||||
"review_plan": plan,
|
||||
"created_by": ctx.actor_id or "system",
|
||||
**_chat_notification_metadata(
|
||||
payload,
|
||||
source.metadata,
|
||||
allow_payload=str(ctx.source or "").startswith("astrbot_chat:"),
|
||||
),
|
||||
},
|
||||
}
|
||||
try:
|
||||
created = self.cross_review_repository.create_review_round_atomic(
|
||||
source_round_id=source_round_id,
|
||||
source_candidate_id=candidate_id,
|
||||
round_payload=round_payload,
|
||||
candidate_payloads=candidate_payloads,
|
||||
requested_by=ctx.actor_id or "system",
|
||||
)
|
||||
except WorkflowRoundWriteError as exc:
|
||||
raise _write_error(exc) from exc
|
||||
self._ensure_enqueued(str(created["round_id"]), ctx)
|
||||
result = {
|
||||
**self.round_service.get_round(str(created["round_id"]), ctx).to_dict(),
|
||||
"review_operation": created,
|
||||
}
|
||||
self.audit_service.record(
|
||||
ctx=ctx,
|
||||
action="request_generation_cross_review",
|
||||
target_type="workflow_generation_round",
|
||||
target_id=str(created["round_id"]),
|
||||
after_snapshot={
|
||||
"source_round_id": source_round_id,
|
||||
"source_candidate_id": candidate_id,
|
||||
"created": bool(created.get("created")),
|
||||
},
|
||||
)
|
||||
return result
|
||||
|
||||
def _skip(
|
||||
self,
|
||||
source_round_id: str,
|
||||
*,
|
||||
candidate_id: str,
|
||||
idempotency_key: str | None,
|
||||
ctx: RequestContext,
|
||||
) -> dict[str, Any]:
|
||||
self.round_service.get_round(source_round_id, ctx)
|
||||
try:
|
||||
decision = self.cross_review_repository.record_skip_atomic(
|
||||
source_round_id=source_round_id,
|
||||
source_candidate_id=candidate_id,
|
||||
idempotency_key=idempotency_key,
|
||||
skipped_by=ctx.actor_id or "system",
|
||||
)
|
||||
except WorkflowRoundWriteError as exc:
|
||||
raise _write_error(exc) from exc
|
||||
if decision.get("already_confirmed"):
|
||||
result = self.round_service.get_round(source_round_id, ctx).to_dict()
|
||||
else:
|
||||
result = self.round_service.confirm_candidate(
|
||||
source_round_id,
|
||||
{
|
||||
"candidate_id": candidate_id,
|
||||
"idempotency_key": f"{idempotency_key}:confirm" if idempotency_key else None,
|
||||
},
|
||||
ctx,
|
||||
)
|
||||
try:
|
||||
restoration_handoff = (
|
||||
self.cross_review_repository.record_skip_restoration_handoff_atomic(
|
||||
source_round_id=source_round_id,
|
||||
source_candidate_id=candidate_id,
|
||||
handed_off_by=ctx.actor_id or "system",
|
||||
)
|
||||
)
|
||||
except WorkflowRoundWriteError as exc:
|
||||
raise _write_error(exc) from exc
|
||||
self.audit_service.record(
|
||||
ctx=ctx,
|
||||
action="skip_generation_cross_review",
|
||||
target_type="workflow_generation_round",
|
||||
target_id=source_round_id,
|
||||
after_snapshot=decision,
|
||||
)
|
||||
return {
|
||||
**result,
|
||||
"review_decision": {
|
||||
"decision": "skip",
|
||||
**decision,
|
||||
"restoration_handoff": restoration_handoff,
|
||||
},
|
||||
}
|
||||
|
||||
def _configured_routes(self, payload: dict[str, Any]) -> list[str]:
|
||||
assignments = self.config.get("model_assignments")
|
||||
assignments = assignments if isinstance(assignments, dict) else {}
|
||||
configured: list[str] = []
|
||||
payload_routes = payload.get("model_routes")
|
||||
if isinstance(payload_routes, list):
|
||||
configured.extend(_string_list(payload_routes))
|
||||
configured.extend(_string_list(assignments.get("generation_cross_review_routes")))
|
||||
configured.extend(
|
||||
value
|
||||
for value in (
|
||||
_optional_text(assignments.get("generation_cross_review_primary")),
|
||||
_optional_text(assignments.get("generation_cross_review_secondary")),
|
||||
)
|
||||
if value
|
||||
)
|
||||
return list(dict.fromkeys(configured))
|
||||
|
||||
def _route_summaries(self, plan: dict[str, Any]) -> dict[str, dict[str, Any]]:
|
||||
summaries: dict[str, dict[str, Any]] = {}
|
||||
for value in (plan.get("primary_route"), *(plan.get("secondary_routes") or [])):
|
||||
if isinstance(value, dict) and value.get("route_name"):
|
||||
summaries[str(value["route_name"])] = dict(value)
|
||||
for item in plan.get("candidates") or []:
|
||||
route_name = str(item.get("model_route") or "")
|
||||
if not route_name or route_name in summaries:
|
||||
continue
|
||||
resolved = self.planner._resolve_image_route(route_name)
|
||||
provider = resolved.get("provider") if isinstance(resolved.get("provider"), dict) else {}
|
||||
summaries[route_name] = {
|
||||
"route_name": route_name,
|
||||
"route_id": resolved.get("route_id"),
|
||||
"provider_id": resolved.get("provider_id"),
|
||||
"provider_name": provider.get("display_name"),
|
||||
"provider_type": provider.get("provider_type"),
|
||||
"model_id": resolved.get("model_id"),
|
||||
"modality": resolved.get("modality"),
|
||||
"status": "resolved",
|
||||
}
|
||||
return summaries
|
||||
|
||||
def _candidate_payload(
|
||||
self,
|
||||
*,
|
||||
plan_item: dict[str, Any],
|
||||
prompt_snapshot: dict[str, Any],
|
||||
prepared: dict[str, Any] | None,
|
||||
preserve_image_id: str | None,
|
||||
) -> dict[str, Any]:
|
||||
generated = bool(plan_item.get("generate"))
|
||||
role = str(plan_item.get("role") or "cross_review_model")
|
||||
provider_name = _optional_text(plan_item.get("provider_name"))
|
||||
metadata = {
|
||||
"schema_version": GENERATION_CROSS_REVIEW_VERSION,
|
||||
"review_role": role,
|
||||
"preserved": not generated,
|
||||
"provider_label": provider_name,
|
||||
"model_label": plan_item.get("model_id"),
|
||||
**({"cross_review_prepared_generation": prepared} if prepared is not None else {}),
|
||||
}
|
||||
return {
|
||||
"candidate_id": f"candidate_{uuid4().hex}",
|
||||
"candidate_index": int(plan_item["candidate_index"]),
|
||||
"generation_status": "planned" if generated else "succeeded",
|
||||
"image_id": preserve_image_id,
|
||||
"source_candidate_id": plan_item.get("source_candidate_id"),
|
||||
"model_route": plan_item.get("model_route"),
|
||||
"provider_id": plan_item.get("provider_id"),
|
||||
"provider_model": plan_item.get("model_id"),
|
||||
"prompt_snapshot": prompt_snapshot,
|
||||
"failure": {},
|
||||
"metadata": metadata,
|
||||
"pool_state": "new" if generated else "preserved",
|
||||
"pool_metadata": {
|
||||
"review_role": role,
|
||||
"provider_label": provider_name,
|
||||
"model_label": plan_item.get("model_id"),
|
||||
"preserved": not generated,
|
||||
},
|
||||
}
|
||||
|
||||
def _prepared_generation(
|
||||
self,
|
||||
*,
|
||||
node: Any,
|
||||
workflow: Any,
|
||||
route: dict[str, Any],
|
||||
prompt_snapshot: dict[str, Any],
|
||||
reference_snapshot: dict[str, Any],
|
||||
source_image_id: str,
|
||||
source_round_id: str,
|
||||
payload: dict[str, Any],
|
||||
) -> dict[str, Any]:
|
||||
route_snapshot = {
|
||||
key: route.get(key)
|
||||
for key in (
|
||||
"route_name",
|
||||
"route_id",
|
||||
"status",
|
||||
"provider_id",
|
||||
"provider_type",
|
||||
"model_id",
|
||||
"modality",
|
||||
)
|
||||
if route.get(key) is not None
|
||||
}
|
||||
positive = _optional_text(prompt_snapshot.get("positive_prompt")) or _optional_text(node.prompt) or ""
|
||||
negative = _optional_text(prompt_snapshot.get("negative_prompt")) or _optional_text(node.negative_prompt) or ""
|
||||
frozen_reference = copy.deepcopy(reference_snapshot or {})
|
||||
frozen_reference["generation_cross_review"] = {
|
||||
"source_round_id": source_round_id,
|
||||
"source_image_id": source_image_id,
|
||||
"model_route": route.get("route_name"),
|
||||
}
|
||||
generation_request = {
|
||||
"count": 1,
|
||||
"model_route": route.get("route_name"),
|
||||
"resolved_model_route": route_snapshot,
|
||||
"expanded_prompt": positive,
|
||||
"expanded_negative_prompt": negative,
|
||||
"prompt": positive,
|
||||
"negative_prompt": negative,
|
||||
"selected_draft_image_ids": [source_image_id],
|
||||
"reference_snapshot": frozen_reference,
|
||||
"generation_cross_review": {
|
||||
"enabled": True,
|
||||
"source_round_id": source_round_id,
|
||||
"source_image_id": source_image_id,
|
||||
"preserve_composition": bool(payload.get("preserve_composition", True)),
|
||||
},
|
||||
}
|
||||
if payload.get("timeout_seconds") is not None:
|
||||
generation_request["timeout_seconds"] = payload.get("timeout_seconds")
|
||||
return {
|
||||
"profile_id": workflow.profile_id,
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"node_id": node.node_id,
|
||||
"node_key": node.node_key,
|
||||
"node_title": node.title,
|
||||
"branch_name": node.branch_name,
|
||||
"model_route": route.get("route_name"),
|
||||
"route_resolution": route_snapshot,
|
||||
"prompt_snapshot": prompt_snapshot,
|
||||
"reference_snapshot": frozen_reference,
|
||||
"generation_request": generation_request,
|
||||
"planned_count": 1,
|
||||
}
|
||||
|
||||
def _ensure_enqueued(self, round_id: str, ctx: RequestContext) -> None:
|
||||
generation_service = self.round_service.generation_service
|
||||
if generation_service is None:
|
||||
return
|
||||
round_record = self.round_repository.get_round(round_id)
|
||||
if round_record is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "cross-review round not found", {"round_id": round_id})
|
||||
pool = {entry.candidate_id: entry for entry in self.round_repository.list_pool(round_id)}
|
||||
task_ids: list[str] = []
|
||||
for candidate in self.round_repository.list_candidates(round_id):
|
||||
prepared = candidate.metadata.get("cross_review_prepared_generation")
|
||||
if not isinstance(prepared, dict):
|
||||
continue
|
||||
if candidate.current_task_id:
|
||||
task_ids.append(candidate.current_task_id)
|
||||
continue
|
||||
entry = pool.get(candidate.candidate_id)
|
||||
if entry is None:
|
||||
continue
|
||||
try:
|
||||
task = generation_service.enqueue_prepared_generation(
|
||||
prepared,
|
||||
round_id=round_id,
|
||||
round_display_code=round_record.display_code,
|
||||
candidate_id=candidate.candidate_id,
|
||||
candidate_display_code=entry.display_code,
|
||||
candidate_index=candidate.candidate_index,
|
||||
ctx=ctx,
|
||||
)
|
||||
task_ids.append(task.task_id)
|
||||
route = prepared.get("route_resolution") if isinstance(prepared.get("route_resolution"), dict) else {}
|
||||
self.round_repository.update_candidate(
|
||||
candidate.candidate_id,
|
||||
{
|
||||
"generation_status": "queued",
|
||||
"current_task_id": task.task_id,
|
||||
"model_route": task.model_route,
|
||||
"provider_id": route.get("provider_id"),
|
||||
"provider_model": route.get("model_id"),
|
||||
"failure": {},
|
||||
"metadata": {
|
||||
**candidate.metadata,
|
||||
"task_idempotency_key": task.idempotency_key,
|
||||
},
|
||||
},
|
||||
)
|
||||
except Exception as exc:
|
||||
self.round_repository.update_candidate(
|
||||
candidate.candidate_id,
|
||||
{
|
||||
"generation_status": "failed",
|
||||
"failure": {
|
||||
"phase": "cross_review_enqueue",
|
||||
"error_type": type(exc).__name__,
|
||||
"message": _safe_message(exc),
|
||||
},
|
||||
},
|
||||
)
|
||||
current = self.round_repository.get_round(round_id) or round_record
|
||||
self.round_repository.update_round(
|
||||
round_id,
|
||||
{
|
||||
"status": "queued" if task_ids else current.status,
|
||||
"metadata": {**current.metadata, "task_ids": task_ids, "planned_only": False},
|
||||
},
|
||||
)
|
||||
self.round_service.reconcile_round(round_id, ctx)
|
||||
|
||||
def _node_and_workflow(
|
||||
self,
|
||||
node_id: str,
|
||||
ctx: RequestContext,
|
||||
) -> tuple[Any, Any]:
|
||||
node = self.round_service.resource_access_service.require_node(node_id, ctx)
|
||||
if node is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "workflow node not found", {"node_id": node_id})
|
||||
workflow = self.workflow_repository.get_workflow(node.workflow_id)
|
||||
if workflow is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "workflow not found", {"workflow_id": node.workflow_id})
|
||||
return node, workflow
|
||||
|
||||
|
||||
def _patched_prompt_snapshot(
|
||||
source: dict[str, Any],
|
||||
positive_patch: str | None,
|
||||
negative_patch: str | None,
|
||||
) -> dict[str, Any]:
|
||||
result = dict(source or {})
|
||||
result["positive_prompt"] = _join_text(_optional_text(result.get("positive_prompt")), positive_patch)
|
||||
result["negative_prompt"] = _join_text(_optional_text(result.get("negative_prompt")), negative_patch)
|
||||
return result
|
||||
|
||||
|
||||
def _join_text(*parts: str | None) -> str | None:
|
||||
values = [str(part).strip() for part in parts if str(part or "").strip()]
|
||||
return ", ".join(values) if values else None
|
||||
|
||||
|
||||
def _display_prefix(display_code: str) -> str:
|
||||
text = str(display_code or "RDSC").strip()
|
||||
head, separator, tail = text.rpartition("-")
|
||||
if separator and tail.isdigit():
|
||||
text = head
|
||||
return "".join(character for character in text.upper() if character.isalnum())[:12] or "RDSC"
|
||||
|
||||
|
||||
def _required_text(value: Any, field: str) -> str:
|
||||
text = _optional_text(value)
|
||||
if not text:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, f"{field} is required", {"field": field})
|
||||
return text
|
||||
|
||||
|
||||
def _bounded_text(value: Any, field: str, maximum: int) -> str | None:
|
||||
text = _optional_text(value)
|
||||
if text and len(text) > maximum:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
f"{field} is too long",
|
||||
{"field": field, "maximum": maximum, "length": len(text)},
|
||||
)
|
||||
return text
|
||||
|
||||
|
||||
def _optional_text(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
text = str(value).strip()
|
||||
return text or None
|
||||
|
||||
|
||||
def _string_list(value: Any) -> list[str]:
|
||||
if not isinstance(value, (list, tuple, set)):
|
||||
return []
|
||||
return list(dict.fromkeys(str(item).strip() for item in value if str(item).strip()))
|
||||
|
||||
|
||||
def _chat_notification_metadata(
|
||||
payload: dict[str, Any],
|
||||
inherited_metadata: dict[str, Any] | None,
|
||||
*,
|
||||
allow_payload: bool,
|
||||
) -> dict[str, Any]:
|
||||
notification = payload.get("chat_notification") if allow_payload else None
|
||||
if not isinstance(notification, dict):
|
||||
inherited = inherited_metadata if isinstance(inherited_metadata, dict) else {}
|
||||
notification = inherited.get("chat_notification")
|
||||
if not isinstance(notification, dict):
|
||||
return {}
|
||||
reply = notification.get("reply_target")
|
||||
if not isinstance(reply, dict) or not _optional_text(reply.get("message_origin")):
|
||||
return {}
|
||||
return {
|
||||
"chat_notification": {
|
||||
"notify_on_terminal": bool(notification.get("notify_on_terminal", True)),
|
||||
"source": "astrbot_chat",
|
||||
"reply_target": {
|
||||
key: _optional_text(reply.get(key))
|
||||
for key in ("platform", "message_origin", "message_id", "user_id", "group_id")
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def _write_error(exc: WorkflowRoundWriteError) -> ApiError:
|
||||
code = ErrorCode.RESOURCE_NOT_FOUND if exc.reason in {"round_not_found", "round_owner_missing"} else ErrorCode.VALIDATION_FAILED
|
||||
return ApiError(code, exc.message, exc.details)
|
||||
|
||||
|
||||
def _safe_message(exc: Exception) -> str:
|
||||
text = str(exc).strip().replace("\r", " ").replace("\n", " ")
|
||||
return text[:500] or exc.__class__.__name__
|
||||
|
|
@ -0,0 +1,383 @@
|
|||
"""Freeze workflow material/reference inputs for one immutable generation round."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import PurePosixPath
|
||||
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
|
||||
|
||||
|
||||
WORKFLOW_MATERIAL_SNAPSHOT_VERSION = "2026-07-15.workflow-material-snapshot.v1"
|
||||
_SAFE_MATERIAL_METADATA_KEYS = frozenset(
|
||||
{
|
||||
"normalized_asset_profile",
|
||||
"generation_capsule",
|
||||
"generation_capsules",
|
||||
"body_slot",
|
||||
"wear_layer",
|
||||
"material_type",
|
||||
"colors",
|
||||
"confirmed",
|
||||
"user_locked",
|
||||
"stackable",
|
||||
"note",
|
||||
"library_kind",
|
||||
"priority_class",
|
||||
"material_priority",
|
||||
"verification",
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class WorkflowMaterialSnapshotService:
|
||||
def __init__(
|
||||
self,
|
||||
workflows: WorkflowRepository,
|
||||
materials: MaterialRepository | MaterialCatalogResolver,
|
||||
):
|
||||
self.workflows = workflows
|
||||
self.materials = materials
|
||||
|
||||
def freeze(
|
||||
self,
|
||||
*,
|
||||
workflow_id: str,
|
||||
node_id: str,
|
||||
branch_name: str,
|
||||
material_bindings: dict[str, Any] | None = None,
|
||||
temporary_references: list[dict[str, Any]] | None = None,
|
||||
navigation_context: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
node = self.workflows.get_node(node_id)
|
||||
workflow = self.workflows.get_workflow(workflow_id)
|
||||
normalized_branch = str(branch_name or "main").strip() or "main"
|
||||
if workflow is None or node is None or node.workflow_id != workflow_id:
|
||||
raise ApiError(
|
||||
ErrorCode.RESOURCE_NOT_FOUND,
|
||||
"workflow material snapshot target does not exist",
|
||||
{"workflow_id": workflow_id, "node_id": node_id},
|
||||
)
|
||||
if str(node.branch_name or "main") != normalized_branch:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow material snapshot branch does not match the node",
|
||||
{
|
||||
"workflow_id": workflow_id,
|
||||
"node_id": node_id,
|
||||
"branch_name": normalized_branch,
|
||||
"node_branch_name": node.branch_name,
|
||||
},
|
||||
)
|
||||
|
||||
bindings = material_bindings if isinstance(material_bindings, dict) else {}
|
||||
binding_by_material = _binding_index(bindings)
|
||||
material_ids = _material_ids(bindings)
|
||||
frozen_materials: list[dict[str, Any]] = []
|
||||
for material_id in material_ids:
|
||||
material = self.materials.get_material(material_id)
|
||||
if material is None or material.recognition_status == "archived":
|
||||
raise ApiError(
|
||||
ErrorCode.RESOURCE_NOT_FOUND,
|
||||
"workflow reference material does not exist",
|
||||
{"material_id": material_id, "node_id": node_id},
|
||||
)
|
||||
if material.recognition_status != "confirmed":
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow reference material must be recognized and confirmed before use",
|
||||
{
|
||||
"material_id": material_id,
|
||||
"recognition_status": material.recognition_status,
|
||||
"required_action": "recognize_and_confirm_material",
|
||||
},
|
||||
)
|
||||
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(
|
||||
{
|
||||
"material_id": material.material_id,
|
||||
"display_name": material.display_name,
|
||||
"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,
|
||||
"tags": list(material.tags),
|
||||
"recognition_status": material.recognition_status,
|
||||
"reference_strength": _plain_value(binding.get("reference_strength")),
|
||||
"target_node_keys": _string_list(binding.get("target_node_keys")),
|
||||
"metadata": metadata,
|
||||
"source_updated_at": material.updated_at,
|
||||
}
|
||||
)
|
||||
|
||||
frozen_temporary = normalize_temporary_references(temporary_references)
|
||||
navigation = _navigation_context(
|
||||
workflow_id=workflow_id,
|
||||
branch_name=normalized_branch,
|
||||
node_id=node_id,
|
||||
payload=navigation_context,
|
||||
)
|
||||
stable_payload = {
|
||||
"schema_version": WORKFLOW_MATERIAL_SNAPSHOT_VERSION,
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": normalized_branch,
|
||||
"node_id": node_id,
|
||||
"materials": frozen_materials,
|
||||
"temporary_references": frozen_temporary,
|
||||
"navigation_context": navigation,
|
||||
}
|
||||
digest = hashlib.sha256(
|
||||
json.dumps(stable_payload, ensure_ascii=False, sort_keys=True, separators=(",", ":")).encode("utf-8")
|
||||
).hexdigest()
|
||||
return {
|
||||
**stable_payload,
|
||||
"snapshot_hash": digest,
|
||||
"created_at": datetime.now(UTC).isoformat(),
|
||||
"material_ids": [item["material_id"] for item in frozen_materials],
|
||||
"reference_uris": [
|
||||
*[
|
||||
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": [
|
||||
item["prompt"] for item in [*frozen_materials, *frozen_temporary] if item.get("prompt")
|
||||
],
|
||||
"negative_prompt_context": [
|
||||
item["negative_prompt"]
|
||||
for item in [*frozen_materials, *frozen_temporary]
|
||||
if item.get("negative_prompt")
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _material_ids(bindings: dict[str, Any]) -> list[str]:
|
||||
values: list[Any] = []
|
||||
values.extend(bindings.get("selected_material_ids", []) if isinstance(bindings.get("selected_material_ids"), list) else [])
|
||||
generation_params = bindings.get("generation_params")
|
||||
if isinstance(generation_params, dict) and isinstance(generation_params.get("reference_material_ids"), list):
|
||||
values.extend(generation_params["reference_material_ids"])
|
||||
active_bindings = bindings.get("active_bindings")
|
||||
if isinstance(active_bindings, list):
|
||||
for binding in active_bindings:
|
||||
if not isinstance(binding, dict):
|
||||
continue
|
||||
material = binding.get("material") if isinstance(binding.get("material"), dict) else {}
|
||||
values.append(material.get("material_id") or binding.get("material_id"))
|
||||
result: list[str] = []
|
||||
for value in values:
|
||||
text = str(value or "").strip()
|
||||
if text and text not in result:
|
||||
result.append(text)
|
||||
return result
|
||||
|
||||
|
||||
def _binding_index(bindings: dict[str, Any]) -> dict[str, dict[str, Any]]:
|
||||
result: dict[str, dict[str, Any]] = {}
|
||||
active_bindings = bindings.get("active_bindings")
|
||||
if not isinstance(active_bindings, list):
|
||||
return result
|
||||
for binding in active_bindings:
|
||||
if not isinstance(binding, dict):
|
||||
continue
|
||||
material = binding.get("material") if isinstance(binding.get("material"), dict) else {}
|
||||
material_id = str(material.get("material_id") or binding.get("material_id") or "").strip()
|
||||
if material_id:
|
||||
result[material_id] = binding
|
||||
return result
|
||||
|
||||
|
||||
def _freeze_temporary_reference(item: dict[str, Any], *, index: int) -> dict[str, Any]:
|
||||
image_uri = normalize_persistent_reference_uri(
|
||||
item.get("image_uri"),
|
||||
field=f"temporary_references[{index}].image_uri",
|
||||
)
|
||||
return {
|
||||
"temporary_reference_id": str(item.get("temporary_reference_id") or f"temporary-{index:02d}"),
|
||||
"display_name": str(item.get("display_name") or f"临时参考 {index}").strip(),
|
||||
"image_uri": image_uri,
|
||||
"thumbnail_uri": _optional_persistent_uri(item.get("thumbnail_uri")),
|
||||
"prompt": _optional_text(item.get("prompt")),
|
||||
"negative_prompt": _optional_text(item.get("negative_prompt")),
|
||||
"tags": _string_list(item.get("tags")),
|
||||
"reference_strength": _plain_value(item.get("reference_strength")),
|
||||
"source": "temporary_reference",
|
||||
}
|
||||
|
||||
|
||||
def _navigation_context(
|
||||
*,
|
||||
workflow_id: str,
|
||||
branch_name: str,
|
||||
node_id: str,
|
||||
payload: dict[str, Any] | None,
|
||||
) -> dict[str, Any]:
|
||||
source = payload if isinstance(payload, dict) else {}
|
||||
for field, expected in (
|
||||
("workflow_id", workflow_id),
|
||||
("branch_name", branch_name),
|
||||
("node_id", node_id),
|
||||
):
|
||||
supplied = str(source.get(field) or "").strip()
|
||||
if supplied and supplied != expected:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"material navigation context does not match the active workflow stage",
|
||||
{"field": field, "expected": expected, "actual": supplied},
|
||||
)
|
||||
return {
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": branch_name,
|
||||
"node_id": node_id,
|
||||
"round_id": _optional_text(source.get("round_id")),
|
||||
"return_view": _optional_text(source.get("return_view")) or "generate",
|
||||
}
|
||||
|
||||
|
||||
def normalize_temporary_references(value: Any) -> list[dict[str, Any]]:
|
||||
"""Normalize persistent temporary references without retaining data URLs."""
|
||||
|
||||
if value is None:
|
||||
return []
|
||||
if not isinstance(value, list):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"temporary_references must be an array",
|
||||
{"field": "temporary_references"},
|
||||
)
|
||||
return [
|
||||
_freeze_temporary_reference(item, index=index)
|
||||
for index, item in enumerate(value, start=1)
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
|
||||
|
||||
def normalize_persistent_reference_uri(value: Any, *, field: str = "image_uri") -> str:
|
||||
uri = str(value or "").strip()
|
||||
if not uri:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "reference image URI is required", {"field": field})
|
||||
if uri.lower().startswith("data:"):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"inline image data cannot be persisted in an asynchronous workflow round",
|
||||
{"field": field, "required_action": "upload_or_import_reference_first"},
|
||||
)
|
||||
if uri.startswith("storage://"):
|
||||
rest = uri[len("storage://") :]
|
||||
if "/" not in rest:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"storage reference must include a root and path",
|
||||
{"field": field},
|
||||
)
|
||||
root, relative = rest.split("/", 1)
|
||||
path = PurePosixPath(relative)
|
||||
if (
|
||||
not root
|
||||
or not relative
|
||||
or path.is_absolute()
|
||||
or ".." in path.parts
|
||||
or "\\" in relative
|
||||
):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"storage reference path is invalid",
|
||||
{"field": field},
|
||||
)
|
||||
return uri
|
||||
parsed = urlsplit(uri)
|
||||
if (
|
||||
parsed.scheme.lower() in {"http", "https"}
|
||||
and parsed.hostname
|
||||
and parsed.username is None
|
||||
and parsed.password is None
|
||||
):
|
||||
return uri
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"reference image must use storage:// or HTTP(S) before it can be frozen",
|
||||
{"field": field},
|
||||
)
|
||||
|
||||
|
||||
def _optional_persistent_uri(value: Any) -> str | None:
|
||||
if value is None or not str(value).strip():
|
||||
return None
|
||||
return normalize_persistent_reference_uri(value, field="thumbnail_uri")
|
||||
|
||||
|
||||
def _safe_material_metadata(value: Any) -> dict[str, Any]:
|
||||
source = value if isinstance(value, dict) else {}
|
||||
return {
|
||||
key: _plain_value(source[key])
|
||||
for key in sorted(_SAFE_MATERIAL_METADATA_KEYS)
|
||||
if key in source
|
||||
}
|
||||
|
||||
|
||||
def _plain_value(value: Any) -> Any:
|
||||
if isinstance(value, dict):
|
||||
return {
|
||||
str(key): _plain_value(child)
|
||||
for key, child in value.items()
|
||||
if not (isinstance(child, str) and child.lower().startswith("data:"))
|
||||
}
|
||||
if isinstance(value, (list, tuple)):
|
||||
return [
|
||||
_plain_value(child)
|
||||
for child in value
|
||||
if not (isinstance(child, str) and child.lower().startswith("data:"))
|
||||
]
|
||||
if isinstance(value, (str, int, float, bool)) or value is None:
|
||||
return value
|
||||
return str(value)
|
||||
|
||||
|
||||
def _string_list(value: Any) -> list[str]:
|
||||
if isinstance(value, str):
|
||||
values = [value]
|
||||
elif isinstance(value, (list, tuple, set)):
|
||||
values = list(value)
|
||||
else:
|
||||
return []
|
||||
result: list[str] = []
|
||||
for item in values:
|
||||
text = str(item or "").strip()
|
||||
if text and text not in result:
|
||||
result.append(text)
|
||||
return result
|
||||
|
||||
|
||||
def _optional_text(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
text = str(value).strip()
|
||||
return text or None
|
||||
|
|
@ -0,0 +1,503 @@
|
|||
"""Node-scoped material bindings for the existing gradual workflow.
|
||||
|
||||
The planning-stage ``material_bind`` node remains the workflow-wide default.
|
||||
This service adds a small override on each currently writable image node so a
|
||||
user can add or remove references between generation rounds without creating a
|
||||
parallel workflow or copying material-library records.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from copy import deepcopy
|
||||
from datetime import UTC, datetime
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.services.branch_lineage_service import workflow_metadata_for_branch
|
||||
from backend.services.material_binding_schema_service import (
|
||||
NODE_MATERIAL_BINDING_METADATA_KEY,
|
||||
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,
|
||||
normalize_temporary_references,
|
||||
)
|
||||
from backend.services.workflow_service import WorkflowService
|
||||
from core.identity import RequestContext
|
||||
from database.repositories.material_repository import MaterialRepository
|
||||
from database.repositories.workflow_round_repository import WorkflowRoundRepository
|
||||
from engines.character_flow import canonical_gradual_node_key
|
||||
|
||||
|
||||
WORKFLOW_IMAGE_NODE_KEYS = frozenset(
|
||||
{
|
||||
"headshot",
|
||||
"upper_body",
|
||||
"lower_body",
|
||||
"full_body",
|
||||
"turnaround",
|
||||
"design_sheet",
|
||||
"style_lock",
|
||||
}
|
||||
)
|
||||
_RETURN_VIEW_RE = re.compile(r"^[A-Za-z0-9_-]{1,64}$")
|
||||
_STRENGTHS: dict[str, tuple[str, float]] = {
|
||||
"strong": ("strong", 0.85),
|
||||
"high": ("strong", 0.85),
|
||||
"medium": ("medium", 0.55),
|
||||
"mid": ("medium", 0.55),
|
||||
"weak": ("weak", 0.30),
|
||||
"low": ("weak", 0.30),
|
||||
}
|
||||
|
||||
|
||||
class WorkflowNodeMaterialService:
|
||||
def __init__(
|
||||
self,
|
||||
workflow_service: WorkflowService,
|
||||
material_repository: MaterialRepository | MaterialCatalogResolver,
|
||||
*,
|
||||
round_repository: WorkflowRoundRepository | None = None,
|
||||
access_service: ResourceAccessService | None = None,
|
||||
):
|
||||
self.workflow_service = workflow_service
|
||||
self.materials = material_repository
|
||||
self.rounds = round_repository
|
||||
self.access = access_service
|
||||
|
||||
def get_node_bindings(
|
||||
self,
|
||||
node_id: str,
|
||||
ctx: RequestContext,
|
||||
*,
|
||||
navigation_context: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
node, workflow = self._require_target(node_id, ctx)
|
||||
branch_nodes = self.workflow_service.workflow_repository.list_nodes(
|
||||
workflow.workflow_id,
|
||||
branch_name=node.branch_name,
|
||||
)
|
||||
branch_metadata = workflow_metadata_for_branch(workflow.metadata, node.branch_name)
|
||||
material_bindings = material_bindings_for_node(branch_metadata, node, branch_nodes)
|
||||
stored_navigation = (
|
||||
material_bindings.get("navigation_context")
|
||||
if isinstance(material_bindings.get("navigation_context"), dict)
|
||||
else {}
|
||||
)
|
||||
navigation = self._navigation_context(
|
||||
workflow_id=workflow.workflow_id,
|
||||
branch_name=node.branch_name,
|
||||
node_id=node.node_id,
|
||||
source=navigation_context if isinstance(navigation_context, dict) else stored_navigation,
|
||||
)
|
||||
writable, lock = self._write_state(node)
|
||||
state = (
|
||||
deepcopy(node.metadata.get(NODE_MATERIAL_BINDING_METADATA_KEY))
|
||||
if isinstance(node.metadata, dict)
|
||||
and isinstance(node.metadata.get(NODE_MATERIAL_BINDING_METADATA_KEY), dict)
|
||||
else None
|
||||
)
|
||||
return {
|
||||
**navigation,
|
||||
"navigation_context": navigation,
|
||||
"workflow_status": workflow.status,
|
||||
"node_key": node.node_key,
|
||||
"node_title": node.title,
|
||||
"node_status": node.status,
|
||||
"writable": writable,
|
||||
"lock": lock,
|
||||
"material_bindings": material_bindings,
|
||||
"temporary_references": deepcopy(material_bindings.get("temporary_references") or []),
|
||||
"binding_state": state,
|
||||
}
|
||||
|
||||
def save_node_bindings(
|
||||
self,
|
||||
node_id: str,
|
||||
payload: dict[str, Any],
|
||||
ctx: RequestContext,
|
||||
) -> dict[str, Any]:
|
||||
node, workflow = self._require_target(node_id, ctx)
|
||||
# ``completed`` means a candidate round has finished, not that the
|
||||
# stage was finally adopted. The progression guard correctly permits
|
||||
# that current node while still rejecting selected ancestors/future
|
||||
# nodes and archived workflows.
|
||||
self.workflow_service.ensure_node_action_allowed(node.node_id, "update", node=node)
|
||||
|
||||
current = self.get_node_bindings(node.node_id, ctx)
|
||||
current_bindings = (
|
||||
current.get("material_bindings")
|
||||
if isinstance(current.get("material_bindings"), dict)
|
||||
else {}
|
||||
)
|
||||
source, selection_explicit = _binding_source(payload)
|
||||
if selection_explicit:
|
||||
selected_material_ids = _selected_material_ids(source)
|
||||
else:
|
||||
selected_material_ids = _selected_material_ids(current_bindings)
|
||||
|
||||
input_bindings = _binding_input_index(source if selection_explicit else current_bindings)
|
||||
active_bindings = [
|
||||
self._material_binding(material_id, node, input_bindings.get(material_id, {}))
|
||||
for material_id in selected_material_ids
|
||||
]
|
||||
|
||||
temporary_explicit, raw_temporary = _temporary_reference_input(payload, source)
|
||||
temporary_references = normalize_temporary_references(
|
||||
raw_temporary
|
||||
if temporary_explicit
|
||||
else current_bindings.get("temporary_references") or []
|
||||
)
|
||||
navigation_source = (
|
||||
payload.get("navigation_context")
|
||||
if isinstance(payload.get("navigation_context"), dict)
|
||||
else {
|
||||
key: payload.get(key)
|
||||
for key in ("workflow_id", "branch_name", "node_id", "round_id", "return_view")
|
||||
if key in payload
|
||||
}
|
||||
)
|
||||
if not navigation_source:
|
||||
navigation_source = current.get("navigation_context") or {}
|
||||
navigation = self._navigation_context(
|
||||
workflow_id=workflow.workflow_id,
|
||||
branch_name=node.branch_name,
|
||||
node_id=node.node_id,
|
||||
source=navigation_source,
|
||||
)
|
||||
state = {
|
||||
"schema_version": NODE_MATERIAL_BINDING_SCHEMA_VERSION,
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"branch_name": node.branch_name,
|
||||
"node_id": node.node_id,
|
||||
"selected_material_ids": selected_material_ids,
|
||||
"active_bindings": active_bindings,
|
||||
"temporary_references": temporary_references,
|
||||
"navigation_context": navigation,
|
||||
"updated_at": datetime.now(UTC).isoformat(),
|
||||
"updated_by": ctx.actor_id or "system",
|
||||
}
|
||||
before_state = (
|
||||
deepcopy(node.metadata.get(NODE_MATERIAL_BINDING_METADATA_KEY))
|
||||
if isinstance(node.metadata, dict)
|
||||
else None
|
||||
)
|
||||
updated = self.workflow_service.workflow_repository.update_node(
|
||||
node.node_id,
|
||||
{
|
||||
"metadata": {
|
||||
**(node.metadata if isinstance(node.metadata, dict) else {}),
|
||||
NODE_MATERIAL_BINDING_METADATA_KEY: state,
|
||||
}
|
||||
},
|
||||
)
|
||||
if updated is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "node not found", {"node_id": node.node_id})
|
||||
self.workflow_service.audit_service.record(
|
||||
ctx=ctx,
|
||||
action="update_workflow_node_material_bindings",
|
||||
target_type="workflow_node",
|
||||
target_id=node.node_id,
|
||||
before_snapshot={"material_bindings": before_state},
|
||||
after_snapshot={"material_bindings": state},
|
||||
)
|
||||
if self.workflow_service.event_bus:
|
||||
self.workflow_service.event_bus.publish(
|
||||
"workflow.node_material_bindings_updated",
|
||||
{
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"branch_name": node.branch_name,
|
||||
"node_id": node.node_id,
|
||||
"material_count": len(selected_material_ids),
|
||||
"temporary_reference_count": len(temporary_references),
|
||||
},
|
||||
)
|
||||
return self.get_node_bindings(
|
||||
updated.node_id,
|
||||
ctx,
|
||||
navigation_context=navigation,
|
||||
)
|
||||
|
||||
def _require_target(self, node_id: str, ctx: RequestContext):
|
||||
if self.access is not None:
|
||||
node = self.access.require_node(node_id, ctx)
|
||||
else:
|
||||
node = self.workflow_service.get_node(node_id)
|
||||
workflow_for_owner = self.workflow_service.workflow_repository.get_workflow(node.workflow_id)
|
||||
profile = (
|
||||
self.workflow_service.profile_repository.get(workflow_for_owner.profile_id)
|
||||
if workflow_for_owner is not None
|
||||
else None
|
||||
)
|
||||
owner_id = getattr(profile, "owner_user_id", None)
|
||||
if (
|
||||
workflow_for_owner is None
|
||||
or profile is None
|
||||
or not (
|
||||
ctx.has_permission("can_manage_all_profiles")
|
||||
or ctx.has_permission("*")
|
||||
or (ctx.actor_id and owner_id and ctx.actor_id == owner_id)
|
||||
)
|
||||
):
|
||||
raise ApiError(
|
||||
ErrorCode.RESOURCE_NOT_FOUND,
|
||||
"workflow node not found",
|
||||
{"resource_id": node_id},
|
||||
)
|
||||
workflow = self.workflow_service.workflow_repository.get_workflow(node.workflow_id)
|
||||
if workflow is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "workflow not found", {"workflow_id": node.workflow_id})
|
||||
canonical_key = canonical_gradual_node_key(node.node_key)
|
||||
if (
|
||||
workflow.workflow_type != "character_gradual"
|
||||
or node.node_type != "generation"
|
||||
or canonical_key not in WORKFLOW_IMAGE_NODE_KEYS
|
||||
):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"node material bindings are available only on detailed-workflow image stages",
|
||||
{
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"workflow_type": workflow.workflow_type,
|
||||
"node_id": node.node_id,
|
||||
"node_key": node.node_key,
|
||||
"node_type": node.node_type,
|
||||
"allowed_node_keys": sorted(WORKFLOW_IMAGE_NODE_KEYS),
|
||||
},
|
||||
)
|
||||
return node, workflow
|
||||
|
||||
def _write_state(self, node) -> tuple[bool, dict[str, Any] | None]:
|
||||
try:
|
||||
self.workflow_service.ensure_node_action_allowed(node.node_id, "update", node=node)
|
||||
except ApiError as exc:
|
||||
return False, {
|
||||
"error_code": str(exc.error_code),
|
||||
"message": exc.message,
|
||||
"details": deepcopy(exc.details),
|
||||
}
|
||||
return True, None
|
||||
|
||||
def _material_binding(self, material_id: str, node, source: dict[str, Any]) -> dict[str, Any]:
|
||||
material = self.materials.get_material(material_id)
|
||||
if material is None or material.recognition_status == "archived":
|
||||
raise ApiError(
|
||||
ErrorCode.RESOURCE_NOT_FOUND,
|
||||
"workflow reference material does not exist",
|
||||
{"material_id": material_id, "node_id": node.node_id},
|
||||
)
|
||||
if material.recognition_status != "confirmed":
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow reference material must be recognized and confirmed before use",
|
||||
{
|
||||
"material_id": material_id,
|
||||
"recognition_status": material.recognition_status,
|
||||
"required_action": "recognize_and_confirm_material",
|
||||
},
|
||||
)
|
||||
normalize_persistent_reference_uri(material.image_uri, field=f"materials[{material_id}].image_uri")
|
||||
category = self.materials.get_category(material.category_id) if material.category_id else None
|
||||
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(
|
||||
source.get("category")
|
||||
or (category.category_type if category is not None else "material")
|
||||
),
|
||||
"use_reference": True,
|
||||
"required": bool(source.get("required")),
|
||||
"status": "ready",
|
||||
"material": {
|
||||
"material_id": material.material_id,
|
||||
"label": material.display_name,
|
||||
"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)],
|
||||
"inheritance_rule": "node-scoped explicit reference",
|
||||
"reference_strength": strength,
|
||||
"generation_params": {
|
||||
"reference_policy": "explicit_only",
|
||||
"reference_strength": strength["key"],
|
||||
"reference_weight": strength["weight"],
|
||||
},
|
||||
"source": "node_midflow_material_binding",
|
||||
}
|
||||
|
||||
def _navigation_context(
|
||||
self,
|
||||
*,
|
||||
workflow_id: str,
|
||||
branch_name: str,
|
||||
node_id: str,
|
||||
source: dict[str, Any] | None,
|
||||
) -> dict[str, Any]:
|
||||
payload = source if isinstance(source, dict) else {}
|
||||
for field, expected in (
|
||||
("workflow_id", workflow_id),
|
||||
("branch_name", branch_name),
|
||||
("node_id", node_id),
|
||||
):
|
||||
supplied = str(payload.get(field) or "").strip()
|
||||
if supplied and supplied != expected:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"material navigation context does not match the active workflow stage",
|
||||
{"field": field, "expected": expected, "actual": supplied},
|
||||
)
|
||||
round_id = _optional_text(payload.get("round_id"))
|
||||
if round_id:
|
||||
if self.rounds is None:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"round navigation cannot be validated",
|
||||
{"round_id": round_id},
|
||||
)
|
||||
round_record = self.rounds.get_round(round_id)
|
||||
if round_record is None:
|
||||
raise ApiError(
|
||||
ErrorCode.RESOURCE_NOT_FOUND,
|
||||
"workflow generation round not found",
|
||||
{"round_id": round_id},
|
||||
)
|
||||
if (
|
||||
round_record.workflow_id != workflow_id
|
||||
or round_record.branch_name != branch_name
|
||||
or round_record.node_id != node_id
|
||||
):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"round navigation belongs to another workflow stage",
|
||||
{
|
||||
"reason": "workflow_material_round_context_mismatch",
|
||||
"round_id": round_id,
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": branch_name,
|
||||
"node_id": node_id,
|
||||
},
|
||||
)
|
||||
return_view = _optional_text(payload.get("return_view")) or "generate"
|
||||
if not _RETURN_VIEW_RE.fullmatch(return_view):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"return_view must be a simple application view name",
|
||||
{"field": "return_view"},
|
||||
)
|
||||
return {
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": branch_name,
|
||||
"node_id": node_id,
|
||||
"round_id": round_id,
|
||||
"return_view": return_view,
|
||||
}
|
||||
|
||||
|
||||
def _binding_source(payload: dict[str, Any]) -> tuple[dict[str, Any], bool]:
|
||||
nested = payload.get("material_bindings")
|
||||
source = nested if isinstance(nested, dict) else payload
|
||||
explicit_fields = {
|
||||
"selected_material_ids",
|
||||
"material_ids",
|
||||
"active_bindings",
|
||||
"rules",
|
||||
"generation_params",
|
||||
}
|
||||
return source, any(field in source for field in explicit_fields)
|
||||
|
||||
|
||||
def _selected_material_ids(source: dict[str, Any]) -> list[str]:
|
||||
if "selected_material_ids" in source:
|
||||
return _unique_texts(source.get("selected_material_ids"))
|
||||
if "material_ids" in source:
|
||||
return _unique_texts(source.get("material_ids"))
|
||||
generation_params = source.get("generation_params")
|
||||
if isinstance(generation_params, dict) and "reference_material_ids" in generation_params:
|
||||
return _unique_texts(generation_params.get("reference_material_ids"))
|
||||
values: list[Any] = []
|
||||
for key in ("active_bindings", "rules"):
|
||||
for item in source.get(key, []) if isinstance(source.get(key), list) else []:
|
||||
if not isinstance(item, dict) or item.get("use_reference") is False:
|
||||
continue
|
||||
material = item.get("material") if isinstance(item.get("material"), dict) else {}
|
||||
values.append(material.get("material_id") or item.get("material_id"))
|
||||
return _unique_texts(values)
|
||||
|
||||
|
||||
def _binding_input_index(source: dict[str, Any]) -> dict[str, dict[str, Any]]:
|
||||
result: dict[str, dict[str, Any]] = {}
|
||||
for key in ("active_bindings", "rules"):
|
||||
values = source.get(key)
|
||||
if not isinstance(values, list):
|
||||
continue
|
||||
for item in values:
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
material = item.get("material") if isinstance(item.get("material"), dict) else {}
|
||||
material_id = _optional_text(material.get("material_id") or item.get("material_id"))
|
||||
if material_id:
|
||||
result[material_id] = item
|
||||
return result
|
||||
|
||||
|
||||
def _temporary_reference_input(
|
||||
payload: dict[str, Any],
|
||||
source: dict[str, Any],
|
||||
) -> tuple[bool, Any]:
|
||||
if "temporary_references" in payload:
|
||||
return True, payload.get("temporary_references")
|
||||
if "temporary_references" in source:
|
||||
return True, source.get("temporary_references")
|
||||
return False, None
|
||||
|
||||
|
||||
def _reference_strength(value: Any) -> dict[str, Any]:
|
||||
if isinstance(value, dict):
|
||||
key = str(value.get("key") or value.get("label") or "medium").strip().lower()
|
||||
default_key, default_weight = _STRENGTHS.get(key, ("medium", 0.55))
|
||||
try:
|
||||
weight = float(value.get("weight", default_weight))
|
||||
except (TypeError, ValueError):
|
||||
weight = default_weight
|
||||
return {
|
||||
"label": str(value.get("label") or default_key),
|
||||
"key": default_key,
|
||||
"weight": min(max(weight, 0.0), 1.0),
|
||||
}
|
||||
key = str(value or "medium").strip().lower()
|
||||
normalized_key, weight = _STRENGTHS.get(key, ("medium", 0.55))
|
||||
return {"label": normalized_key, "key": normalized_key, "weight": weight}
|
||||
|
||||
|
||||
def _unique_texts(value: Any) -> list[str]:
|
||||
values = value if isinstance(value, (list, tuple, set)) else [value]
|
||||
result: list[str] = []
|
||||
for item in values:
|
||||
text = str(item or "").strip()
|
||||
if text and text not in result:
|
||||
result.append(text)
|
||||
return result
|
||||
|
||||
|
||||
def _string_list(value: Any) -> list[str]:
|
||||
return _unique_texts(value) if value is not None else []
|
||||
|
||||
|
||||
def _optional_text(value: Any) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
text = str(value).strip()
|
||||
return text or None
|
||||
|
|
@ -0,0 +1,240 @@
|
|||
"""Sequential access rules for the detailed character workflow.
|
||||
|
||||
The gradual workflow is intentionally readable as a whole, while mutations are
|
||||
limited to the current step. Progress is derived from persisted workflow/node
|
||||
state so older databases do not need a schema migration.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Iterable
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.dto.workflow_dto import ProfileWorkflow, WorkflowNode
|
||||
from engines.character_flow import canonical_gradual_node_key
|
||||
|
||||
|
||||
CHARACTER_GRADUAL_PROGRESSION_VERSION = "character_gradual_sequential_v1"
|
||||
PROGRESSION_COMPLETED_NODE_STATUSES = frozenset({"selected", "skipped"})
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class CharacterGradualProgression:
|
||||
workflow_id: str
|
||||
branch_name: str
|
||||
import_confirmed: bool
|
||||
nodes: tuple[WorkflowNode, ...]
|
||||
current_node: WorkflowNode | None
|
||||
completed_node_ids: tuple[str, ...]
|
||||
|
||||
@property
|
||||
def all_completed(self) -> bool:
|
||||
return self.import_confirmed and bool(self.nodes) and self.current_node is None
|
||||
|
||||
def is_accessible(self, node: WorkflowNode) -> bool:
|
||||
"""Return whether an operation may target ``node``.
|
||||
|
||||
Completed ancestors remain reviewable/readable. Future nodes are never
|
||||
made writable by stale ``current_node_id`` or old prompt preparation;
|
||||
recovery must happen at the derived current node.
|
||||
"""
|
||||
|
||||
if node.workflow_id != self.workflow_id or node.branch_name != self.branch_name:
|
||||
return False
|
||||
if node.node_id in self.completed_node_ids:
|
||||
return True
|
||||
if self.current_node is not None and node.node_id == self.current_node.node_id:
|
||||
return True
|
||||
return False
|
||||
|
||||
def to_dict(self) -> dict[str, Any]:
|
||||
current = self.current_node
|
||||
return {
|
||||
"version": CHARACTER_GRADUAL_PROGRESSION_VERSION,
|
||||
"mode": "sequential",
|
||||
"import_confirmed": self.import_confirmed,
|
||||
"branch_name": self.branch_name,
|
||||
"current_node_id": current.node_id if current else None,
|
||||
"current_node_key": current.node_key if current else None,
|
||||
"completed_node_ids": list(self.completed_node_ids),
|
||||
"all_completed": self.all_completed,
|
||||
}
|
||||
|
||||
|
||||
def character_gradual_progression(
|
||||
workflow: ProfileWorkflow,
|
||||
nodes: Iterable[WorkflowNode],
|
||||
*,
|
||||
branch_name: str | None = None,
|
||||
) -> CharacterGradualProgression | None:
|
||||
if workflow.workflow_type != "character_gradual":
|
||||
return None
|
||||
selected_branch = str(branch_name or workflow.current_branch or "main")
|
||||
ordered = tuple(
|
||||
sorted(
|
||||
(node for node in nodes if node.branch_name == selected_branch),
|
||||
key=lambda item: (item.sort_order, item.created_at, item.node_id),
|
||||
)
|
||||
)
|
||||
import_confirmed = _is_import_confirmed(workflow)
|
||||
completed_prefix: list[str] = []
|
||||
current_node: WorkflowNode | None = None
|
||||
if import_confirmed:
|
||||
allow_legacy_planning_metadata = _allows_legacy_planning_metadata_completion(workflow)
|
||||
for node in ordered:
|
||||
if is_character_gradual_node_completed(
|
||||
node,
|
||||
allow_legacy_planning_metadata=allow_legacy_planning_metadata,
|
||||
):
|
||||
completed_prefix.append(node.node_id)
|
||||
continue
|
||||
current_node = node
|
||||
break
|
||||
return CharacterGradualProgression(
|
||||
workflow_id=workflow.workflow_id,
|
||||
branch_name=selected_branch,
|
||||
import_confirmed=import_confirmed,
|
||||
nodes=ordered,
|
||||
current_node=current_node,
|
||||
completed_node_ids=tuple(completed_prefix),
|
||||
)
|
||||
|
||||
|
||||
def ensure_character_gradual_node_access(
|
||||
workflow: ProfileWorkflow,
|
||||
nodes: Iterable[WorkflowNode],
|
||||
node: WorkflowNode,
|
||||
*,
|
||||
operation: str,
|
||||
) -> CharacterGradualProgression | None:
|
||||
progression = character_gradual_progression(workflow, nodes, branch_name=node.branch_name)
|
||||
if progression is None:
|
||||
return progression
|
||||
|
||||
normalized_operation = str(operation or "update").strip().lower()
|
||||
if (
|
||||
node.node_id in progression.completed_node_ids
|
||||
and normalized_operation in {"update", "generate", "retry", "complete"}
|
||||
):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"completed character workflow node is locked",
|
||||
{
|
||||
"reason": "character_gradual_node_already_completed",
|
||||
"operation": normalized_operation,
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"node_id": node.node_id,
|
||||
"node_key": node.node_key,
|
||||
"branch_name": node.branch_name,
|
||||
"required_action": "create_branch",
|
||||
"progression_version": CHARACTER_GRADUAL_PROGRESSION_VERSION,
|
||||
},
|
||||
)
|
||||
if progression.is_accessible(node):
|
||||
return progression
|
||||
|
||||
current = progression.current_node
|
||||
details: dict[str, Any] = {
|
||||
"reason": "character_gradual_progression_locked",
|
||||
"operation": str(operation or "update"),
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"workflow_type": workflow.workflow_type,
|
||||
"node_id": node.node_id,
|
||||
"node_key": node.node_key,
|
||||
"branch_name": node.branch_name,
|
||||
"progression_version": CHARACTER_GRADUAL_PROGRESSION_VERSION,
|
||||
"import_confirmed": progression.import_confirmed,
|
||||
"current_node_id": current.node_id if current else None,
|
||||
"current_node_key": current.node_key if current else None,
|
||||
"required_previous_node_id": current.node_id if current else None,
|
||||
"required_previous_node_key": current.node_key if current else None,
|
||||
}
|
||||
if not progression.import_confirmed:
|
||||
details.update(
|
||||
{
|
||||
"required_action": "execute_character_workflow",
|
||||
"required_previous_node_key": "profile_import",
|
||||
"message": "confirm the imported character brief before using workflow nodes",
|
||||
}
|
||||
)
|
||||
else:
|
||||
details.update(
|
||||
{
|
||||
"required_action": "complete_current_node",
|
||||
"message": "complete or confirm the current node before using a later node",
|
||||
}
|
||||
)
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"character workflow node is locked by sequential progression",
|
||||
details,
|
||||
)
|
||||
|
||||
|
||||
def is_character_gradual_node_completed(
|
||||
node: WorkflowNode,
|
||||
*,
|
||||
allow_legacy_planning_metadata: bool = False,
|
||||
) -> bool:
|
||||
if node.status in PROGRESSION_COMPLETED_NODE_STATUSES:
|
||||
return True
|
||||
metadata = node.metadata if isinstance(node.metadata, dict) else {}
|
||||
if node.status == "completed" and (
|
||||
node.node_type in {"planning", "reference_binding"}
|
||||
or canonical_gradual_node_key(node.node_key) == "profile_card"
|
||||
):
|
||||
return True
|
||||
# Older standalone UI versions persisted these planning steps without
|
||||
# changing the node status. Only workflows without the sequential-v1
|
||||
# marker receive that compatibility rule; new workflows require the
|
||||
# explicit confirm/complete action after saving a draft.
|
||||
if allow_legacy_planning_metadata:
|
||||
if node.node_key == "prompt_plan":
|
||||
return "prompt_plan" in metadata and metadata.get("prompt_plan") is not None
|
||||
if node.node_key == "material_bind":
|
||||
# Presence (not truthiness) is intentional because selecting no
|
||||
# references is a valid legacy decision.
|
||||
return "material_bindings" in metadata and metadata.get("material_bindings") is not None
|
||||
return False
|
||||
|
||||
|
||||
def character_execution_metadata(
|
||||
workflow: ProfileWorkflow,
|
||||
progression: CharacterGradualProgression,
|
||||
*,
|
||||
existing: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Build the server-owned progression portion of character_execution."""
|
||||
|
||||
return {
|
||||
**(existing or {}),
|
||||
"executor": "character_workflow",
|
||||
"workflow_type": workflow.workflow_type,
|
||||
"import_confirmed": True,
|
||||
"progression_version": CHARACTER_GRADUAL_PROGRESSION_VERSION,
|
||||
"completed_node_ids": list(progression.completed_node_ids),
|
||||
"current_node_id": progression.current_node.node_id if progression.current_node else None,
|
||||
"current_node_key": progression.current_node.node_key if progression.current_node else None,
|
||||
"all_completed": progression.all_completed,
|
||||
}
|
||||
|
||||
|
||||
def _is_import_confirmed(workflow: ProfileWorkflow) -> bool:
|
||||
metadata = workflow.metadata if isinstance(workflow.metadata, dict) else {}
|
||||
execution = metadata.get("character_execution")
|
||||
if not isinstance(execution, dict):
|
||||
return False
|
||||
# Older plugin versions already wrote character_execution without the
|
||||
# explicit flag. Treat that persisted execution as an imported brief.
|
||||
if execution.get("import_confirmed") is False:
|
||||
return False
|
||||
return bool(execution.get("import_confirmed") or execution.get("executor") == "character_workflow")
|
||||
|
||||
|
||||
def _allows_legacy_planning_metadata_completion(workflow: ProfileWorkflow) -> bool:
|
||||
metadata = workflow.metadata if isinstance(workflow.metadata, dict) else {}
|
||||
execution = metadata.get("character_execution")
|
||||
if not isinstance(execution, dict):
|
||||
return False
|
||||
return execution.get("progression_version") != CHARACTER_GRADUAL_PROGRESSION_VERSION
|
||||
|
|
@ -0,0 +1,265 @@
|
|||
"""Conversational feedback planning for the existing workflow round pool.
|
||||
|
||||
The service is intentionally persistence-free. It gives Web and AstrBot one
|
||||
shared interpretation contract while the owning ``WorkflowRoundService``
|
||||
keeps authorization, transactions and next-round creation server-side.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable, Mapping
|
||||
from dataclasses import replace
|
||||
import json
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.workflow_feedback_dto import WorkflowFeedbackInterpretation
|
||||
from backend.services.workflow_feedback_interpreter import (
|
||||
WorkflowFeedbackInterpretationError,
|
||||
WorkflowFeedbackInterpreter,
|
||||
)
|
||||
|
||||
|
||||
StructuredFeedbackCallable = Callable[[dict[str, Any]], dict[str, Any] | str]
|
||||
MAX_FEEDBACK_TEXT_LENGTH = 12_000
|
||||
MAX_PATCH_LENGTH = 4_000
|
||||
MAX_MATERIAL_INTENT_LENGTH = 2_000
|
||||
|
||||
|
||||
class WorkflowRoundFeedbackService:
|
||||
"""Use the configured text model when available, with a safe local fallback."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
interpreter: WorkflowFeedbackInterpreter | None = None,
|
||||
structured_text_callable: StructuredFeedbackCallable | None = None,
|
||||
model_route: str | None = None,
|
||||
) -> None:
|
||||
self.interpreter = interpreter or WorkflowFeedbackInterpreter()
|
||||
self.structured_text_callable = structured_text_callable
|
||||
self.model_route = str(model_route or "").strip() or None
|
||||
|
||||
def interpret(
|
||||
self,
|
||||
text: str,
|
||||
*,
|
||||
candidate_whitelist: Mapping[str, str],
|
||||
candidate_context: Mapping[str, Mapping[str, Any]] | None = None,
|
||||
) -> WorkflowFeedbackInterpretation:
|
||||
source_text = _bounded_text(text, field="feedback_text", maximum=MAX_FEEDBACK_TEXT_LENGTH)
|
||||
if not source_text:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"feedback_text_required",
|
||||
"feedback_text is required",
|
||||
{"field": "feedback_text"},
|
||||
)
|
||||
fallback_reason: str | None = None
|
||||
if self.structured_text_callable is not None:
|
||||
try:
|
||||
raw = self.structured_text_callable(
|
||||
_model_request(
|
||||
source_text,
|
||||
candidate_whitelist,
|
||||
self.model_route,
|
||||
candidate_context=candidate_context,
|
||||
)
|
||||
)
|
||||
payload = _normalize_model_payload(raw)
|
||||
interpretation = self.interpreter.validate_llm_result(
|
||||
payload,
|
||||
candidate_whitelist=candidate_whitelist,
|
||||
source_text=source_text,
|
||||
)
|
||||
return _bounded_interpretation(
|
||||
replace(
|
||||
interpretation,
|
||||
metadata={
|
||||
"model_attempted": True,
|
||||
"model_route": self.model_route,
|
||||
"fallback_used": False,
|
||||
},
|
||||
)
|
||||
)
|
||||
except Exception as exc:
|
||||
# Never expose provider messages here: they can contain URLs,
|
||||
# headers or vendor-specific request fragments. The caller
|
||||
# receives only a stable error class and then the deterministic
|
||||
# parser evaluates the original user text from scratch.
|
||||
fallback_reason = _safe_failure_code(exc)
|
||||
elif self.model_route:
|
||||
fallback_reason = "model_callable_unavailable"
|
||||
|
||||
interpretation = self.interpreter.interpret(
|
||||
source_text,
|
||||
candidate_whitelist=candidate_whitelist,
|
||||
)
|
||||
return _bounded_interpretation(
|
||||
replace(
|
||||
interpretation,
|
||||
positive_patch=interpretation.positive_patch
|
||||
or interpretation.next_round_prompt_patch,
|
||||
source="deterministic_fallback" if fallback_reason else "deterministic",
|
||||
metadata={
|
||||
"model_attempted": self.structured_text_callable is not None,
|
||||
"model_route": self.model_route,
|
||||
"fallback_used": bool(fallback_reason),
|
||||
**({"fallback_reason": fallback_reason} if fallback_reason else {}),
|
||||
},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _model_request(
|
||||
source_text: str,
|
||||
candidate_whitelist: Mapping[str, str],
|
||||
model_route: str | None,
|
||||
*,
|
||||
candidate_context: Mapping[str, Mapping[str, Any]] | None,
|
||||
) -> dict[str, Any]:
|
||||
# Opaque candidate IDs deliberately never cross the model boundary.
|
||||
display_codes = [str(code).strip() for code in candidate_whitelist if str(code).strip()]
|
||||
context = candidate_context if isinstance(candidate_context, Mapping) else {}
|
||||
return {
|
||||
"purpose": "workflow candidate feedback interpretation and next-round prompt patch planning",
|
||||
"user_text": source_text,
|
||||
"candidate_whitelist": [
|
||||
{
|
||||
"display_code": code,
|
||||
"ordinal": index,
|
||||
"positive_prompt": _bounded_context_text(
|
||||
(context.get(code) or {}).get("positive_prompt"),
|
||||
maximum=4_000,
|
||||
),
|
||||
"negative_prompt": _bounded_context_text(
|
||||
(context.get(code) or {}).get("negative_prompt"),
|
||||
maximum=2_000,
|
||||
),
|
||||
"model_label": _bounded_context_text(
|
||||
(context.get(code) or {}).get("model_label"),
|
||||
maximum=200,
|
||||
),
|
||||
}
|
||||
for index, code in enumerate(display_codes, start=1)
|
||||
],
|
||||
"response_schema": {
|
||||
"feedback": {
|
||||
"liked": ["display_code"],
|
||||
"rejected": ["display_code"],
|
||||
"strict_rejected": ["display_code"],
|
||||
"final": "display_code|null",
|
||||
},
|
||||
"positive_patch": "string|null",
|
||||
"negative_patch": "string|null",
|
||||
"material_intent": "string|null",
|
||||
},
|
||||
"rules": [
|
||||
"Use only display_code values from candidate_whitelist.",
|
||||
"Never return opaque server identifiers.",
|
||||
"Keep strict rejections in negative_patch.",
|
||||
"Do not invent visual details absent from user_text.",
|
||||
],
|
||||
**({"model_route": model_route} if model_route else {}),
|
||||
}
|
||||
|
||||
|
||||
def _normalize_model_payload(value: dict[str, Any] | str) -> dict[str, Any]:
|
||||
if isinstance(value, str):
|
||||
cleaned = re.sub(r"^```(?:json)?\s*|\s*```$", "", value.strip(), flags=re.IGNORECASE)
|
||||
parsed = json.loads(cleaned)
|
||||
else:
|
||||
parsed = value
|
||||
if not isinstance(parsed, Mapping):
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_llm_result",
|
||||
"LLM feedback result must be an object",
|
||||
)
|
||||
_reject_opaque_identifier_keys(parsed)
|
||||
feedback = parsed.get("feedback")
|
||||
feedback = dict(feedback) if isinstance(feedback, Mapping) else {}
|
||||
merged = {
|
||||
**feedback,
|
||||
"positive_patch": parsed.get(
|
||||
"positive_patch",
|
||||
feedback.get("positive_patch", parsed.get("next_round_prompt_patch")),
|
||||
),
|
||||
"negative_patch": parsed.get("negative_patch", feedback.get("negative_patch")),
|
||||
"material_intent": parsed.get("material_intent", feedback.get("material_intent")),
|
||||
}
|
||||
return merged
|
||||
|
||||
|
||||
def _bounded_interpretation(
|
||||
interpretation: WorkflowFeedbackInterpretation,
|
||||
) -> WorkflowFeedbackInterpretation:
|
||||
positive = _bounded_optional(
|
||||
interpretation.positive_patch or interpretation.next_round_prompt_patch,
|
||||
field="positive_patch",
|
||||
maximum=MAX_PATCH_LENGTH,
|
||||
)
|
||||
negative = _bounded_optional(
|
||||
interpretation.negative_patch,
|
||||
field="negative_patch",
|
||||
maximum=MAX_PATCH_LENGTH,
|
||||
)
|
||||
material = _bounded_optional(
|
||||
interpretation.material_intent,
|
||||
field="material_intent",
|
||||
maximum=MAX_MATERIAL_INTENT_LENGTH,
|
||||
)
|
||||
return replace(
|
||||
interpretation,
|
||||
next_round_prompt_patch=positive,
|
||||
positive_patch=positive,
|
||||
negative_patch=negative,
|
||||
material_intent=material,
|
||||
)
|
||||
|
||||
|
||||
def _bounded_optional(value: Any, *, field: str, maximum: int) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
return _bounded_text(value, field=field, maximum=maximum) or None
|
||||
|
||||
|
||||
def _bounded_text(value: Any, *, field: str, maximum: int) -> str:
|
||||
if not isinstance(value, str):
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_feedback_text",
|
||||
f"{field} must be text",
|
||||
{"field": field},
|
||||
)
|
||||
normalized = value.strip()
|
||||
if len(normalized) > maximum:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"feedback_text_too_long",
|
||||
f"{field} is too long",
|
||||
{"field": field, "maximum": maximum},
|
||||
)
|
||||
return normalized
|
||||
|
||||
|
||||
def _safe_failure_code(exc: Exception) -> str:
|
||||
if isinstance(exc, WorkflowFeedbackInterpretationError):
|
||||
return exc.reason
|
||||
return type(exc).__name__[:80]
|
||||
|
||||
|
||||
def _bounded_context_text(value: Any, *, maximum: int) -> str | None:
|
||||
text = " ".join(str(value or "").split())
|
||||
return text[:maximum] or None
|
||||
|
||||
|
||||
def _reject_opaque_identifier_keys(value: Any, *, path: str = "$") -> None:
|
||||
if isinstance(value, Mapping):
|
||||
for key, item in value.items():
|
||||
if str(key).strip().lower() in {"candidate_id", "candidate_ids"}:
|
||||
raise WorkflowFeedbackInterpretationError(
|
||||
"invalid_llm_result",
|
||||
"LLM feedback must reference candidates by display_code only",
|
||||
{"field": f"{path}.{key}", "reason": "llm_candidate_id_forbidden"},
|
||||
)
|
||||
_reject_opaque_identifier_keys(item, path=f"{path}.{key}")
|
||||
elif isinstance(value, (list, tuple)):
|
||||
for index, item in enumerate(value):
|
||||
_reject_opaque_identifier_keys(item, path=f"{path}[{index}]")
|
||||
文件差异内容过多而无法显示
加载差异
|
|
@ -0,0 +1,268 @@
|
|||
"""AstrBot lifecycle wrapper for detailed-workflow candidate workers."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import threading
|
||||
from collections.abc import Callable
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.job_dto import GenerationTask
|
||||
from backend.routes.node_routes import create_generation_service
|
||||
from backend.services.generation_service import GenerationService
|
||||
from backend.services.media_service import MediaService
|
||||
from backend.services.worker_service import QueueWorkerPool, RetryPolicy, WorkerTaskUpdate
|
||||
from backend.services.workflow_round_service import WorkflowRoundService
|
||||
from core.identity import RequestContext
|
||||
from database.repositories.workflow_round_repository import WorkflowRoundRepository
|
||||
|
||||
|
||||
class WorkflowRoundWorkerRuntime:
|
||||
def __init__(
|
||||
self,
|
||||
generation_service: GenerationService,
|
||||
round_service: WorkflowRoundService,
|
||||
*,
|
||||
worker_count: int = 5,
|
||||
stale_after_seconds: int = 300,
|
||||
interval_seconds: float = 0.25,
|
||||
terminal_notification_callback: Callable[[dict[str, Any]], None] | None = None,
|
||||
):
|
||||
self.generation_service = generation_service
|
||||
self.round_service = round_service
|
||||
self.queue_service = generation_service.queue_service
|
||||
self.ctx = RequestContext.system("workflow-round-worker")
|
||||
self.stale_after_seconds = max(int(stale_after_seconds), 1)
|
||||
self.terminal_notification_callback = terminal_notification_callback
|
||||
self._notification_lock = threading.RLock()
|
||||
self._scheduled_notifications: set[str] = set()
|
||||
self._media_service = MediaService()
|
||||
self.pool = QueueWorkerPool(
|
||||
self.queue_service,
|
||||
self._execute,
|
||||
worker_count=worker_count,
|
||||
worker_id_prefix="imagestudio-round",
|
||||
interval_seconds=interval_seconds,
|
||||
retry_policy=RetryPolicy(max_attempts=1),
|
||||
task_filter=_is_workflow_round_task,
|
||||
terminal_callback=self._on_terminal,
|
||||
per_route_limit=worker_count,
|
||||
)
|
||||
self.start_result: dict[str, Any] | None = None
|
||||
|
||||
def start(self) -> dict[str, Any]:
|
||||
if self.pool.is_running():
|
||||
return self.start_result or {"running": True, "already_running": True}
|
||||
recovery = self.queue_service.recover_stale_tasks(
|
||||
self.ctx,
|
||||
stale_after_seconds=self.stale_after_seconds,
|
||||
source="workflow_round",
|
||||
)
|
||||
for task_id in recovery.get("recovered_task_ids", []):
|
||||
task = self.queue_service.get_job(str(task_id))
|
||||
self.round_service.reconcile_task(task, self.ctx)
|
||||
pool = self.pool.start(self.ctx)
|
||||
self.start_result = {"running": pool["running"], "recovery": recovery, "pool": pool}
|
||||
return self.start_result
|
||||
|
||||
def stop(self, *, timeout_seconds: float = 5.0) -> dict[str, Any]:
|
||||
result = self.pool.stop(timeout_seconds=timeout_seconds)
|
||||
return {"running": result["running"], "pool": result}
|
||||
|
||||
def _execute(self, task: GenerationTask) -> WorkerTaskUpdate:
|
||||
self.round_service.mark_task_running(task, self.ctx)
|
||||
result = self.generation_service.execute_claimed_generation(task, self.ctx)
|
||||
return WorkerTaskUpdate(
|
||||
status="succeeded",
|
||||
progress=100,
|
||||
result=result,
|
||||
metadata={
|
||||
"round_execution": {
|
||||
"round_id": task.metadata.get("round_id"),
|
||||
"candidate_id": task.metadata.get("candidate_id"),
|
||||
"status": "succeeded",
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
def _on_terminal(
|
||||
self,
|
||||
claimed_task: GenerationTask,
|
||||
current_task: GenerationTask,
|
||||
exc: Exception | None,
|
||||
) -> None:
|
||||
result = self.round_service.handle_task_terminal(claimed_task, current_task, exc, self.ctx)
|
||||
if result and result.get("round_status") in {
|
||||
"completed",
|
||||
"partial_success",
|
||||
"all_failed",
|
||||
"cancelled",
|
||||
}:
|
||||
self._schedule_terminal_notification(str(result["round_id"]))
|
||||
|
||||
def pending_terminal_notifications(self) -> list[dict[str, Any]]:
|
||||
"""Rebuild undelivered chat callbacks from durable round metadata."""
|
||||
|
||||
payloads: list[dict[str, Any]] = []
|
||||
for round_record in self.round_service.round_repository.list_rounds():
|
||||
if round_record.status not in {"completed", "partial_success", "all_failed", "cancelled"}:
|
||||
continue
|
||||
payload = self._claim_notification_payload(round_record.round_id)
|
||||
if payload is not None:
|
||||
payloads.append(payload)
|
||||
return payloads
|
||||
|
||||
def mark_terminal_notification(
|
||||
self,
|
||||
round_id: str,
|
||||
*,
|
||||
delivered: bool,
|
||||
error_message: str | None = None,
|
||||
) -> None:
|
||||
with self._notification_lock:
|
||||
record = self.round_service.round_repository.get_round(round_id)
|
||||
if record is None:
|
||||
self._scheduled_notifications.discard(round_id)
|
||||
return
|
||||
metadata = dict(record.metadata or {})
|
||||
previous = metadata.get("terminal_notification_delivery")
|
||||
previous = previous if isinstance(previous, dict) else {}
|
||||
metadata["terminal_notification_delivery"] = {
|
||||
**previous,
|
||||
"state": "delivered" if delivered else "pending",
|
||||
"attempts": int(previous.get("attempts") or 0) + 1,
|
||||
"error_message": None if delivered else _safe_error(error_message),
|
||||
}
|
||||
self.round_service.round_repository.update_round(round_id, {"metadata": metadata})
|
||||
self._scheduled_notifications.discard(round_id)
|
||||
|
||||
def _schedule_terminal_notification(self, round_id: str) -> None:
|
||||
payload = self._claim_notification_payload(round_id)
|
||||
if payload is None:
|
||||
return
|
||||
if self.terminal_notification_callback is None:
|
||||
self.mark_terminal_notification(
|
||||
round_id,
|
||||
delivered=False,
|
||||
error_message="AstrBot terminal notification callback is unavailable",
|
||||
)
|
||||
return
|
||||
try:
|
||||
self.terminal_notification_callback(payload)
|
||||
except Exception as exc:
|
||||
self.mark_terminal_notification(round_id, delivered=False, error_message=str(exc))
|
||||
|
||||
def _claim_notification_payload(self, round_id: str) -> dict[str, Any] | None:
|
||||
with self._notification_lock:
|
||||
if round_id in self._scheduled_notifications:
|
||||
return None
|
||||
record = self.round_service.round_repository.get_round(round_id)
|
||||
if record is None:
|
||||
return None
|
||||
notification = record.metadata.get("chat_notification")
|
||||
notification = notification if isinstance(notification, dict) else {}
|
||||
reply_target = notification.get("reply_target")
|
||||
delivery = record.metadata.get("terminal_notification_delivery")
|
||||
delivery = delivery if isinstance(delivery, dict) else {}
|
||||
if (
|
||||
not notification.get("notify_on_terminal")
|
||||
or not isinstance(reply_target, dict)
|
||||
or not str(reply_target.get("message_origin") or "").strip()
|
||||
or delivery.get("state") == "delivered"
|
||||
):
|
||||
return None
|
||||
self._scheduled_notifications.add(round_id)
|
||||
metadata = {
|
||||
**record.metadata,
|
||||
"terminal_notification_delivery": {
|
||||
**delivery,
|
||||
"state": "scheduled",
|
||||
},
|
||||
}
|
||||
self.round_service.round_repository.update_round(round_id, {"metadata": metadata})
|
||||
detail = self.round_service.get_round(round_id, self.ctx).to_dict()
|
||||
return _terminal_notification_payload(detail, reply_target, self._media_service)
|
||||
|
||||
|
||||
def create_workflow_round_worker_runtime(
|
||||
*,
|
||||
worker_count: int = 5,
|
||||
stale_after_seconds: int = 300,
|
||||
terminal_notification_callback: Callable[[dict[str, Any]], None] | None = None,
|
||||
) -> WorkflowRoundWorkerRuntime:
|
||||
generation_service = create_generation_service()
|
||||
database = generation_service.queue_service.repository.database
|
||||
round_service = WorkflowRoundService(
|
||||
round_repository=WorkflowRoundRepository(database),
|
||||
workflow_repository=generation_service.workflow_service.workflow_repository,
|
||||
image_repository=generation_service.image_service.image_repository,
|
||||
job_repository=generation_service.queue_service.repository,
|
||||
audit_service=generation_service.workflow_service.audit_service,
|
||||
generation_service=generation_service,
|
||||
)
|
||||
return WorkflowRoundWorkerRuntime(
|
||||
generation_service,
|
||||
round_service,
|
||||
worker_count=worker_count,
|
||||
stale_after_seconds=stale_after_seconds,
|
||||
terminal_notification_callback=terminal_notification_callback,
|
||||
)
|
||||
|
||||
|
||||
def _is_workflow_round_task(task: GenerationTask) -> bool:
|
||||
return task.metadata.get("source") == "workflow_round"
|
||||
|
||||
|
||||
def _terminal_notification_payload(
|
||||
detail: dict[str, Any],
|
||||
reply_target: dict[str, Any],
|
||||
media_service: MediaService,
|
||||
) -> dict[str, Any]:
|
||||
round_record = detail.get("round") if isinstance(detail.get("round"), dict) else {}
|
||||
candidates: list[dict[str, Any]] = []
|
||||
for item in detail.get("candidates", []):
|
||||
if not isinstance(item, dict):
|
||||
continue
|
||||
candidate = item.get("candidate") if isinstance(item.get("candidate"), dict) else {}
|
||||
image = item.get("image") if isinstance(item.get("image"), dict) else None
|
||||
image_url = None
|
||||
thumbnail_url = None
|
||||
if image:
|
||||
image_url = _signed_url(media_service, image.get("image_uri"))
|
||||
thumbnail_url = _signed_url(media_service, image.get("thumbnail_uri"))
|
||||
candidates.append(
|
||||
{
|
||||
"candidate_id": candidate.get("candidate_id") or item.get("candidate_id"),
|
||||
"display_code": item.get("display_code"),
|
||||
"generation_status": candidate.get("generation_status"),
|
||||
"image_id": image.get("image_id") if image else None,
|
||||
"image_url": image_url,
|
||||
"thumbnail_url": thumbnail_url,
|
||||
"failure": candidate.get("failure") or {},
|
||||
}
|
||||
)
|
||||
return {
|
||||
"round_id": round_record.get("round_id"),
|
||||
"display_code": round_record.get("display_code"),
|
||||
"status": round_record.get("status"),
|
||||
"workflow_id": round_record.get("workflow_id"),
|
||||
"node_id": round_record.get("node_id"),
|
||||
"reply_target": {
|
||||
key: (str(reply_target.get(key)) if reply_target.get(key) is not None else None)
|
||||
for key in ("platform", "message_origin", "message_id", "user_id", "group_id")
|
||||
},
|
||||
"candidates": candidates,
|
||||
}
|
||||
|
||||
|
||||
def _signed_url(media_service: MediaService, uri: Any) -> str | None:
|
||||
if not isinstance(uri, str) or not uri:
|
||||
return None
|
||||
try:
|
||||
return media_service.sign_media(uri).get("url")
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _safe_error(value: Any) -> str | None:
|
||||
text = " ".join(str(value or "").split())[:500]
|
||||
return text or None
|
||||
|
|
@ -7,6 +7,14 @@ from uuid import uuid4
|
|||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.dto.workflow_dto import ProfileWorkflow, WorkflowBranch, WorkflowDetail, WorkflowNode
|
||||
from backend.services.branch_lineage_service import (
|
||||
BranchLineageResolver,
|
||||
activate_branch_state,
|
||||
branch_state_for,
|
||||
branch_state_patch,
|
||||
remap_branch_state_node_ids,
|
||||
with_branch_state,
|
||||
)
|
||||
from backend.services.material_binding_schema_service import (
|
||||
MATERIAL_BINDING_SCHEMA_VERSION,
|
||||
normalize_material_bindings,
|
||||
|
|
@ -16,13 +24,24 @@ from backend.services.prompt_plan_schema_service import (
|
|||
normalize_prompt_plan,
|
||||
)
|
||||
from backend.services.queue_service import QueueService
|
||||
from backend.services.workflow_progression_service import (
|
||||
character_execution_metadata,
|
||||
character_gradual_progression,
|
||||
ensure_character_gradual_node_access,
|
||||
is_character_gradual_node_completed,
|
||||
)
|
||||
from core.audit_service import AuditService
|
||||
from core.event_bus import EventBus
|
||||
from core.fsm import node_transition, workflow_transition
|
||||
from core.identity import RequestContext
|
||||
from database.repositories.profile_repository import ProfileRepository
|
||||
from database.repositories.workflow_repository import WorkflowRepository
|
||||
from engines.character_flow import gradual_node_templates
|
||||
from engines.character_flow import (
|
||||
CHARACTER_GRADUAL_STEPS,
|
||||
CHARACTER_GRADUAL_TEMPLATE_VERSION,
|
||||
canonical_gradual_node_key,
|
||||
gradual_node_templates,
|
||||
)
|
||||
from engines.fast_character_flow import fast_character_node_templates
|
||||
from engines.quick_profile_flow import quick_profile_node_templates
|
||||
|
||||
|
|
@ -42,6 +61,15 @@ NODE_STATUSES = {
|
|||
"discarded",
|
||||
}
|
||||
LOCKED_NODE_STATUSES = {"completed", "selected"}
|
||||
QUICK_CHARACTER_IMMUTABLE_METADATA_KEYS = {
|
||||
"quick_character_identity_managed",
|
||||
"quick_character_batch_id",
|
||||
"identity_lock_hash",
|
||||
"identity_lock",
|
||||
"quick_character_route_snapshot",
|
||||
"quick_character_target",
|
||||
"quick_character_targets",
|
||||
}
|
||||
|
||||
|
||||
class WorkflowService:
|
||||
|
|
@ -127,18 +155,105 @@ class WorkflowService:
|
|||
"workflow not found",
|
||||
{"workflow_id": workflow_id},
|
||||
)
|
||||
return WorkflowDetail(
|
||||
workflow=workflow,
|
||||
nodes=self.workflow_repository.list_nodes(workflow_id),
|
||||
nodes = self.workflow_repository.list_nodes(workflow_id)
|
||||
if workflow.workflow_type == "character_gradual" and workflow.status != "archived":
|
||||
nodes = self._ensure_character_gradual_catalog(workflow, nodes)
|
||||
refreshed = self.workflow_repository.get_workflow(workflow_id)
|
||||
if refreshed is not None:
|
||||
workflow = refreshed
|
||||
return WorkflowDetail(workflow=workflow, nodes=nodes)
|
||||
|
||||
def _ensure_character_gradual_catalog(
|
||||
self,
|
||||
workflow: ProfileWorkflow,
|
||||
nodes: list[WorkflowNode],
|
||||
) -> list[WorkflowNode]:
|
||||
"""Idempotently add canonical stages that did not exist in v1 data.
|
||||
|
||||
Legacy keys stay untouched so persisted ids, images and history remain
|
||||
valid. The only normal v1 gap is ``lower_body``; the generic loop also
|
||||
repairs partially-created workflows without rebuilding them.
|
||||
"""
|
||||
|
||||
branches = sorted({node.branch_name for node in nodes} or {"main"})
|
||||
created_any = False
|
||||
for branch_name in branches:
|
||||
branch_nodes = [node for node in nodes if node.branch_name == branch_name]
|
||||
canonical_keys = {canonical_gradual_node_key(node.node_key) for node in branch_nodes}
|
||||
for template in CHARACTER_GRADUAL_STEPS:
|
||||
node_key = str(template["node_key"])
|
||||
if node_key in canonical_keys:
|
||||
continue
|
||||
# Existing v1 workflows are structurally complete except for
|
||||
# lower_body. Do not manufacture unrelated missing stages in
|
||||
# a branch that was intentionally copied without all nodes.
|
||||
if node_key != "lower_body" and branch_nodes:
|
||||
continue
|
||||
sort_order = _compatible_stage_sort_order(node_key, template, branch_nodes)
|
||||
status = _compatible_stage_status(sort_order, branch_nodes)
|
||||
created = self.workflow_repository.create_node(
|
||||
{
|
||||
"node_id": new_node_id(),
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"node_key": node_key,
|
||||
"title": template["title"],
|
||||
"description": template.get("description"),
|
||||
"step_name": template.get("step_name"),
|
||||
"node_type": template.get("node_type", "generation"),
|
||||
"status": status,
|
||||
"branch_name": branch_name,
|
||||
"sort_order": sort_order,
|
||||
"metadata": {
|
||||
"template": CHARACTER_GRADUAL_TEMPLATE_VERSION,
|
||||
"canonical_node_key": node_key,
|
||||
"stage_code": template.get("stage_code"),
|
||||
"compatibility_added": True,
|
||||
"compatibility_source": "character_gradual_v1",
|
||||
},
|
||||
}
|
||||
)
|
||||
nodes.append(created)
|
||||
branch_nodes.append(created)
|
||||
canonical_keys.add(node_key)
|
||||
created_any = True
|
||||
if created_any:
|
||||
metadata = dict(workflow.metadata or {})
|
||||
metadata["character_catalog_version"] = CHARACTER_GRADUAL_TEMPLATE_VERSION
|
||||
metadata["character_catalog_migrated"] = True
|
||||
self.workflow_repository.update_workflow(workflow.workflow_id, {"metadata": metadata})
|
||||
return sorted(nodes, key=lambda item: (item.branch_name, item.sort_order, item.created_at, item.node_id))
|
||||
|
||||
def update_workflow(
|
||||
self,
|
||||
workflow_id: str,
|
||||
payload: dict[str, Any],
|
||||
ctx: RequestContext,
|
||||
*,
|
||||
allow_character_execution_update: bool = False,
|
||||
) -> ProfileWorkflow:
|
||||
before = self.get_workflow(workflow_id).workflow
|
||||
self._ensure_workflow_mutable(before, "update_workflow")
|
||||
if before.workflow_type == "character_gradual" and payload.get("status") == "completed":
|
||||
progression = character_gradual_progression(
|
||||
before,
|
||||
self.workflow_repository.list_nodes(
|
||||
before.workflow_id,
|
||||
branch_name=before.current_branch,
|
||||
),
|
||||
branch_name=before.current_branch,
|
||||
)
|
||||
if progression is not None and not progression.all_completed:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"gradual workflow cannot be completed before every node is confirmed",
|
||||
{
|
||||
"reason": "character_gradual_workflow_incomplete",
|
||||
"workflow_id": before.workflow_id,
|
||||
"current_node_id": (
|
||||
progression.current_node.node_id if progression.current_node else None
|
||||
),
|
||||
},
|
||||
)
|
||||
if "status" in payload:
|
||||
self._validate_workflow_status_transition(before.status, str(payload["status"]))
|
||||
if "display_name" in payload and not str(payload["display_name"]).strip():
|
||||
|
|
@ -150,8 +265,16 @@ class WorkflowService:
|
|||
update_payload = dict(payload)
|
||||
if "display_name" in update_payload:
|
||||
update_payload["display_name"] = str(update_payload["display_name"]).strip()
|
||||
if "metadata" in update_payload and isinstance(update_payload["metadata"], dict):
|
||||
update_payload["metadata"] = self._normalize_workflow_metadata(before, update_payload["metadata"])
|
||||
if "metadata" in update_payload:
|
||||
if isinstance(update_payload["metadata"], dict):
|
||||
update_payload["metadata"] = self._normalize_workflow_metadata(
|
||||
before,
|
||||
update_payload["metadata"],
|
||||
allow_character_execution_update=allow_character_execution_update,
|
||||
branch_name=str(update_payload.get("current_branch") or before.current_branch),
|
||||
)
|
||||
elif before.workflow_type == "character_gradual" and not allow_character_execution_update:
|
||||
update_payload["metadata"] = dict(before.metadata or {})
|
||||
after = self.workflow_repository.update_workflow(workflow_id, update_payload)
|
||||
if after is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "workflow not found", {"workflow_id": workflow_id})
|
||||
|
|
@ -186,6 +309,7 @@ class WorkflowService:
|
|||
|
||||
while True:
|
||||
before = self.get_workflow(workflow_id).workflow
|
||||
self._ensure_workflow_mutable(before, "cancel_workflow")
|
||||
if before.status == "cancelled":
|
||||
after = before
|
||||
break
|
||||
|
|
@ -278,9 +402,16 @@ class WorkflowService:
|
|||
ctx: RequestContext,
|
||||
) -> WorkflowBranch:
|
||||
workflow = self._ensure_workflow_exists(workflow_id)
|
||||
self._ensure_workflow_mutable(workflow, "create_branch")
|
||||
branch_name = str(payload.get("branch_name") or "").strip()
|
||||
if not branch_name:
|
||||
raise ApiError(ErrorCode.VALIDATION_FAILED, "branch_name is required", {"field": "branch_name"})
|
||||
if branch_name == "main":
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"main is a reserved workflow branch",
|
||||
{"reason": "workflow_branch_main_reserved", "workflow_id": workflow_id},
|
||||
)
|
||||
if self.workflow_repository.get_branch(workflow_id, branch_name):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
|
|
@ -289,23 +420,90 @@ class WorkflowService:
|
|||
)
|
||||
base_branch_name = str(payload.get("base_branch_name") or workflow.current_branch or "main").strip()
|
||||
base_node_id = payload.get("base_node_id") or workflow.current_node_id
|
||||
copy_nodes = payload.get("copy_nodes", True)
|
||||
if copy_nodes or base_node_id:
|
||||
self._resolve_branch_copy_base(workflow_id, base_branch_name, base_node_id)
|
||||
branch = self.workflow_repository.create_branch(
|
||||
status = str(payload.get("status") or "active").strip().lower()
|
||||
if status not in BRANCH_STATUSES:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"unsupported workflow branch status",
|
||||
{"field": "status", "allowed": sorted(BRANCH_STATUSES), "status": status},
|
||||
)
|
||||
if base_branch_name == branch_name:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow branch cannot be its own parent",
|
||||
{
|
||||
"reason": "workflow_branch_self_parent",
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": branch_name,
|
||||
},
|
||||
)
|
||||
self._validate_branch_parent_chain(workflow_id, base_branch_name, branch_name)
|
||||
source_nodes, base_sort_order = self._resolve_branch_copy_base(
|
||||
workflow_id,
|
||||
base_branch_name,
|
||||
base_node_id,
|
||||
)
|
||||
base_node = next((node for node in source_nodes if node.node_id == base_node_id), None)
|
||||
if base_node is None or base_node.status not in LOCKED_NODE_STATUSES:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow branches can only start from a completed node",
|
||||
{
|
||||
"reason": "workflow_branch_base_not_completed",
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": branch_name,
|
||||
"base_branch_name": base_branch_name,
|
||||
"base_node_id": base_node_id,
|
||||
"base_node_status": base_node.status if base_node else None,
|
||||
"allowed_statuses": sorted(LOCKED_NODE_STATUSES),
|
||||
},
|
||||
)
|
||||
copy_nodes = payload.get("copy_nodes", True) is not False
|
||||
node_payloads = (
|
||||
self._branch_node_payloads(
|
||||
workflow_id,
|
||||
source_nodes,
|
||||
to_branch=branch_name,
|
||||
base_sort_order=base_sort_order,
|
||||
)
|
||||
if copy_nodes
|
||||
else []
|
||||
)
|
||||
branch_metadata = {
|
||||
**(payload.get("metadata") if isinstance(payload.get("metadata"), dict) else {}),
|
||||
"lineage_version": "workflow_branch_lineage_v1",
|
||||
"parent_branch": base_branch_name,
|
||||
"base_sort_order": base_sort_order,
|
||||
}
|
||||
branch, copied_nodes = self.workflow_repository.create_branch_with_nodes(
|
||||
{
|
||||
"branch_id": payload.get("branch_id") or new_branch_id(),
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": branch_name,
|
||||
"base_branch_name": base_branch_name,
|
||||
"base_node_id": base_node_id,
|
||||
"status": payload.get("status", "active"),
|
||||
"metadata": payload.get("metadata") or {},
|
||||
}
|
||||
"status": status,
|
||||
"metadata": branch_metadata,
|
||||
},
|
||||
node_payloads,
|
||||
)
|
||||
copied_nodes = []
|
||||
if copy_nodes:
|
||||
copied_nodes = self._copy_branch_nodes(workflow_id, base_branch_name, branch_name, ctx, base_node_id)
|
||||
if copied_nodes:
|
||||
node_id_map = {
|
||||
str(node.metadata.get("copied_from_node_id")): node.node_id
|
||||
for node in copied_nodes
|
||||
if isinstance(node.metadata, dict) and node.metadata.get("copied_from_node_id")
|
||||
}
|
||||
inherited_state = remap_branch_state_node_ids(
|
||||
branch_state_for(workflow.metadata, base_branch_name),
|
||||
node_id_map,
|
||||
)
|
||||
updated_metadata = with_branch_state(
|
||||
workflow.metadata,
|
||||
branch_name,
|
||||
inherited_state if isinstance(inherited_state, dict) else {},
|
||||
activate=False,
|
||||
)
|
||||
self.workflow_repository.update_workflow(workflow_id, {"metadata": updated_metadata})
|
||||
self.audit_service.record(
|
||||
ctx=ctx,
|
||||
action="create_branch",
|
||||
|
|
@ -326,10 +524,8 @@ class WorkflowService:
|
|||
def list_branches(self, workflow_id: str) -> list[WorkflowBranch]:
|
||||
self._ensure_workflow_exists(workflow_id)
|
||||
branches = self.workflow_repository.list_branches(workflow_id)
|
||||
if branches:
|
||||
return branches
|
||||
return [
|
||||
WorkflowBranch(
|
||||
persisted_main = next((branch for branch in branches if branch.branch_name == "main"), None)
|
||||
main = persisted_main or WorkflowBranch(
|
||||
branch_id="branch_virtual_main",
|
||||
workflow_id=workflow_id,
|
||||
branch_name="main",
|
||||
|
|
@ -340,24 +536,46 @@ class WorkflowService:
|
|||
created_at="",
|
||||
updated_at="",
|
||||
)
|
||||
]
|
||||
return [main, *(branch for branch in branches if branch.branch_name != "main")]
|
||||
|
||||
def switch_branch(self, workflow_id: str, branch_name: str, ctx: RequestContext) -> ProfileWorkflow:
|
||||
before = self._ensure_workflow_exists(workflow_id)
|
||||
if branch_name != "main" and self.workflow_repository.get_branch(workflow_id, branch_name) is None:
|
||||
self._ensure_workflow_mutable(before, "switch_branch")
|
||||
branch = self.workflow_repository.get_branch(workflow_id, branch_name) if branch_name != "main" else None
|
||||
if branch_name != "main" and branch is None:
|
||||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "branch not found", {"workflow_id": workflow_id, "branch_name": branch_name})
|
||||
if branch is not None and branch.status != "active":
|
||||
raise ApiError(
|
||||
ErrorCode.RESOURCE_NOT_FOUND,
|
||||
"branch not found",
|
||||
{"workflow_id": workflow_id, "branch_name": branch_name},
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"archived workflow branch cannot become active",
|
||||
{
|
||||
"reason": "workflow_branch_archived",
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": branch_name,
|
||||
"status": branch.status,
|
||||
},
|
||||
)
|
||||
branch_nodes = self.workflow_repository.list_nodes(workflow_id, branch_name=branch_name)
|
||||
current_node_id = branch_nodes[0].node_id if branch_nodes else before.current_node_id
|
||||
progression = character_gradual_progression(
|
||||
before,
|
||||
branch_nodes,
|
||||
branch_name=branch_name,
|
||||
)
|
||||
if progression is not None and progression.import_confirmed:
|
||||
if progression.current_node is not None:
|
||||
current_node_id = progression.current_node.node_id
|
||||
elif branch_nodes:
|
||||
current_node_id = branch_nodes[-1].node_id
|
||||
after = self.update_workflow(
|
||||
workflow_id,
|
||||
{
|
||||
"current_branch": branch_name,
|
||||
"current_node_id": branch_nodes[0].node_id if branch_nodes else before.current_node_id,
|
||||
"current_node_id": current_node_id,
|
||||
"metadata": activate_branch_state(before.metadata, branch_name),
|
||||
},
|
||||
ctx,
|
||||
allow_character_execution_update=True,
|
||||
)
|
||||
self.audit_service.record(
|
||||
ctx=ctx,
|
||||
|
|
@ -377,7 +595,8 @@ class WorkflowService:
|
|||
*,
|
||||
audit: bool = True,
|
||||
) -> WorkflowNode:
|
||||
self._ensure_workflow_exists(workflow_id)
|
||||
workflow = self._ensure_workflow_exists(workflow_id)
|
||||
self._ensure_workflow_mutable(workflow, "create_node")
|
||||
node_key = str(payload.get("node_key") or "").strip()
|
||||
title = str(payload.get("title") or "").strip()
|
||||
if not node_key:
|
||||
|
|
@ -434,6 +653,36 @@ class WorkflowService:
|
|||
raise ApiError(ErrorCode.RESOURCE_NOT_FOUND, "node not found", {"node_id": node_id})
|
||||
return node
|
||||
|
||||
def ensure_node_action_allowed(
|
||||
self,
|
||||
node_id: str,
|
||||
operation: str,
|
||||
*,
|
||||
node: WorkflowNode | None = None,
|
||||
) -> WorkflowNode:
|
||||
"""Enforce mutation/review access without hiding readable nodes."""
|
||||
|
||||
target = node or self.get_node(node_id)
|
||||
workflow = self._ensure_workflow_exists(target.workflow_id)
|
||||
if workflow.status == "archived" and str(operation or "").strip().lower() not in {
|
||||
"read",
|
||||
"review",
|
||||
"inspect",
|
||||
"dependency",
|
||||
}:
|
||||
raise self._archived_workflow_error(workflow, operation, node_id=target.node_id)
|
||||
branch_nodes = self.workflow_repository.list_nodes(
|
||||
target.workflow_id,
|
||||
branch_name=target.branch_name,
|
||||
)
|
||||
ensure_character_gradual_node_access(
|
||||
workflow,
|
||||
branch_nodes,
|
||||
target,
|
||||
operation=operation,
|
||||
)
|
||||
return target
|
||||
|
||||
def update_node(
|
||||
self,
|
||||
node_id: str,
|
||||
|
|
@ -443,6 +692,30 @@ class WorkflowService:
|
|||
allow_locked_adoption: bool = False,
|
||||
) -> WorkflowNode:
|
||||
before = self.get_node(node_id)
|
||||
self.ensure_node_action_allowed(node_id, "update", node=before)
|
||||
workflow = self._ensure_workflow_exists(before.workflow_id)
|
||||
if workflow.workflow_type == "character_gradual":
|
||||
structural_changes = {
|
||||
key: payload[key]
|
||||
for key in ("branch_name", "sort_order")
|
||||
if key in payload
|
||||
and (
|
||||
str(payload[key]) != before.branch_name
|
||||
if key == "branch_name"
|
||||
else _int_or_original(payload[key]) != before.sort_order
|
||||
)
|
||||
}
|
||||
if structural_changes:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"gradual workflow node order and branch are immutable",
|
||||
{
|
||||
"reason": "character_gradual_structure_locked",
|
||||
"node_id": before.node_id,
|
||||
"workflow_id": before.workflow_id,
|
||||
"fields": sorted(structural_changes),
|
||||
},
|
||||
)
|
||||
if before.status in LOCKED_NODE_STATUSES and _node_payload_changes(before, payload):
|
||||
if not (allow_locked_adoption and _is_locked_image_adoption(before, payload)):
|
||||
raise locked_node_edit_error(before)
|
||||
|
|
@ -476,9 +749,64 @@ class WorkflowService:
|
|||
"after_status": after.status,
|
||||
},
|
||||
)
|
||||
if (
|
||||
is_character_gradual_node_completed(after)
|
||||
and not is_character_gradual_node_completed(before)
|
||||
):
|
||||
self._advance_character_gradual_progression(after, ctx)
|
||||
self._publish("workflow.node_updated", {"workflow_id": after.workflow_id, "node_id": node_id})
|
||||
return after
|
||||
|
||||
def _advance_character_gradual_progression(
|
||||
self,
|
||||
completed_node: WorkflowNode,
|
||||
ctx: RequestContext,
|
||||
) -> None:
|
||||
workflow = self.workflow_repository.get_workflow(completed_node.workflow_id)
|
||||
if workflow is None or workflow.workflow_type != "character_gradual":
|
||||
return
|
||||
branch_nodes = self.workflow_repository.list_nodes(
|
||||
workflow.workflow_id,
|
||||
branch_name=completed_node.branch_name,
|
||||
)
|
||||
progression = character_gradual_progression(
|
||||
workflow,
|
||||
branch_nodes,
|
||||
branch_name=completed_node.branch_name,
|
||||
)
|
||||
if progression is None or not progression.import_confirmed:
|
||||
return
|
||||
existing_execution = (
|
||||
workflow.metadata.get("character_execution")
|
||||
if isinstance(workflow.metadata, dict)
|
||||
and isinstance(workflow.metadata.get("character_execution"), dict)
|
||||
else {}
|
||||
)
|
||||
target_node_id = (
|
||||
progression.current_node.node_id
|
||||
if progression.current_node is not None
|
||||
else completed_node.node_id
|
||||
)
|
||||
update_payload: dict[str, Any] = {
|
||||
"current_node_id": target_node_id,
|
||||
"metadata": {
|
||||
**(workflow.metadata or {}),
|
||||
"character_execution": character_execution_metadata(
|
||||
workflow,
|
||||
progression,
|
||||
existing=existing_execution,
|
||||
),
|
||||
},
|
||||
}
|
||||
if progression.all_completed and workflow.status in {"draft", "active", "running"}:
|
||||
update_payload["status"] = "completed"
|
||||
self.update_workflow(
|
||||
workflow.workflow_id,
|
||||
update_payload,
|
||||
ctx,
|
||||
allow_character_execution_update=True,
|
||||
)
|
||||
|
||||
def _initial_node_payloads(
|
||||
self,
|
||||
workflow_id: str,
|
||||
|
|
@ -515,19 +843,18 @@ class WorkflowService:
|
|||
)
|
||||
return nodes
|
||||
|
||||
def _copy_branch_nodes(
|
||||
def _branch_node_payloads(
|
||||
self,
|
||||
workflow_id: str,
|
||||
from_branch: str,
|
||||
source_nodes: list[WorkflowNode],
|
||||
*,
|
||||
to_branch: str,
|
||||
ctx: RequestContext,
|
||||
base_node_id: str | None = None,
|
||||
) -> list[WorkflowNode]:
|
||||
source_nodes, base_sort_order = self._resolve_branch_copy_base(workflow_id, from_branch, base_node_id)
|
||||
base_sort_order: int | None,
|
||||
) -> list[dict[str, Any]]:
|
||||
return [
|
||||
self.create_node(
|
||||
workflow_id,
|
||||
{
|
||||
"node_id": new_node_id(),
|
||||
"workflow_id": workflow_id,
|
||||
"node_key": node.node_key,
|
||||
"title": node.title,
|
||||
"description": node.description,
|
||||
|
|
@ -541,10 +868,7 @@ class WorkflowService:
|
|||
"selected_image_id": _branch_copy_selected_image_id(node, base_sort_order),
|
||||
"sort_order": node.sort_order,
|
||||
"metadata": _branch_copy_metadata(node, base_sort_order),
|
||||
},
|
||||
ctx,
|
||||
audit=False,
|
||||
)
|
||||
}
|
||||
for node in source_nodes
|
||||
]
|
||||
|
||||
|
|
@ -555,8 +879,16 @@ class WorkflowService:
|
|||
base_node_id: str | None = None,
|
||||
) -> tuple[list[WorkflowNode], int | None]:
|
||||
source_nodes = self.workflow_repository.list_nodes(workflow_id, branch_name=from_branch)
|
||||
if not source_nodes and from_branch != "main":
|
||||
source_nodes = self.workflow_repository.list_nodes(workflow_id, branch_name="main")
|
||||
if not source_nodes:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"source workflow branch has no nodes",
|
||||
{
|
||||
"reason": "workflow_branch_source_empty",
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": from_branch,
|
||||
},
|
||||
)
|
||||
base_node = None
|
||||
if base_node_id:
|
||||
base_node = next((node for node in source_nodes if node.node_id == base_node_id), None)
|
||||
|
|
@ -568,6 +900,63 @@ class WorkflowService:
|
|||
)
|
||||
return source_nodes, base_node.sort_order if base_node else None
|
||||
|
||||
def _validate_branch_parent_chain(
|
||||
self,
|
||||
workflow_id: str,
|
||||
base_branch_name: str,
|
||||
new_branch_name: str,
|
||||
) -> None:
|
||||
if base_branch_name == "main":
|
||||
return
|
||||
parent = self.workflow_repository.get_branch(workflow_id, base_branch_name)
|
||||
if parent is None:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"base workflow branch does not exist",
|
||||
{
|
||||
"reason": "workflow_branch_parent_missing",
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": new_branch_name,
|
||||
"base_branch_name": base_branch_name,
|
||||
},
|
||||
)
|
||||
chain = BranchLineageResolver(self.workflow_repository).branch_chain(workflow_id, base_branch_name)
|
||||
if new_branch_name in chain:
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"workflow branch parent cycle detected",
|
||||
{
|
||||
"reason": "workflow_branch_parent_cycle",
|
||||
"workflow_id": workflow_id,
|
||||
"branch_name": new_branch_name,
|
||||
"base_branch_name": base_branch_name,
|
||||
"lineage": list(chain),
|
||||
},
|
||||
)
|
||||
|
||||
def _ensure_workflow_mutable(self, workflow: ProfileWorkflow, operation: str) -> None:
|
||||
if workflow.status == "archived":
|
||||
raise self._archived_workflow_error(workflow, operation)
|
||||
|
||||
@staticmethod
|
||||
def _archived_workflow_error(
|
||||
workflow: ProfileWorkflow,
|
||||
operation: str,
|
||||
*,
|
||||
node_id: str | None = None,
|
||||
) -> ApiError:
|
||||
return ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"archived workflow is read-only",
|
||||
{
|
||||
"reason": "workflow_archived_read_only",
|
||||
"workflow_id": workflow.workflow_id,
|
||||
"status": workflow.status,
|
||||
"operation": str(operation or "write"),
|
||||
**({"node_id": node_id} if node_id else {}),
|
||||
},
|
||||
)
|
||||
|
||||
def _ensure_profile_exists(self, profile_id: str) -> None:
|
||||
profile = self.profile_repository.get(profile_id)
|
||||
if profile is None or profile.status == "deleted":
|
||||
|
|
@ -628,13 +1017,37 @@ class WorkflowService:
|
|||
self,
|
||||
workflow: ProfileWorkflow,
|
||||
metadata: dict[str, Any],
|
||||
*,
|
||||
allow_character_execution_update: bool = False,
|
||||
branch_name: str | None = None,
|
||||
) -> dict[str, Any]:
|
||||
material_bindings = metadata.get("material_bindings")
|
||||
prompt_plan = metadata.get("prompt_plan") or metadata.get("prompt_matrix")
|
||||
if material_bindings is None and prompt_plan is None:
|
||||
return metadata
|
||||
nodes = self.workflow_repository.list_nodes(workflow.workflow_id, branch_name=workflow.current_branch)
|
||||
normalized = dict(metadata)
|
||||
current_metadata = workflow.metadata if isinstance(workflow.metadata, dict) else {}
|
||||
selected_branch = str(branch_name or workflow.current_branch or "main")
|
||||
if workflow.workflow_type == "character_gradual" and not allow_character_execution_update:
|
||||
if "character_execution" in current_metadata:
|
||||
normalized["character_execution"] = current_metadata["character_execution"]
|
||||
else:
|
||||
normalized.pop("character_execution", None)
|
||||
if (
|
||||
workflow.workflow_type == "character_fast"
|
||||
and current_metadata.get("quick_character_identity_managed") == "generation_service_v1"
|
||||
):
|
||||
# Browser clients may PUT a stale whole metadata object after the
|
||||
# four requests finish. Preserve server-owned batch identity and
|
||||
# route fields once initialized so clients cannot clear or mutate
|
||||
# the lock accidentally (or deliberately).
|
||||
for key in QUICK_CHARACTER_IMMUTABLE_METADATA_KEYS:
|
||||
if key in current_metadata:
|
||||
normalized[key] = current_metadata[key]
|
||||
|
||||
material_bindings = normalized.get("material_bindings")
|
||||
prompt_plan = normalized.get("prompt_plan") or normalized.get("prompt_matrix")
|
||||
nodes = (
|
||||
self.workflow_repository.list_nodes(workflow.workflow_id, branch_name=selected_branch)
|
||||
if material_bindings is not None or prompt_plan is not None
|
||||
else []
|
||||
)
|
||||
if material_bindings is not None:
|
||||
normalized["material_binding_schema_version"] = MATERIAL_BINDING_SCHEMA_VERSION
|
||||
normalized["material_bindings"] = normalize_material_bindings(material_bindings, nodes)
|
||||
|
|
@ -642,6 +1055,17 @@ class WorkflowService:
|
|||
normalized["prompt_plan_schema_version"] = PROMPT_PLAN_SCHEMA_VERSION
|
||||
normalized["prompt_plan"] = normalize_prompt_plan(prompt_plan, nodes)
|
||||
normalized.pop("prompt_matrix", None)
|
||||
if workflow.workflow_type == "character_gradual":
|
||||
if isinstance(current_metadata.get("branch_state"), dict):
|
||||
# Clients edit only the active projection. Sibling branch
|
||||
# snapshots are server-owned and must survive stale PUTs.
|
||||
normalized["branch_state"] = current_metadata["branch_state"]
|
||||
normalized = with_branch_state(
|
||||
normalized,
|
||||
selected_branch,
|
||||
branch_state_patch(normalized),
|
||||
activate=True,
|
||||
)
|
||||
return normalized
|
||||
|
||||
def _sync_material_bindings_from_node(self, node: WorkflowNode) -> None:
|
||||
|
|
@ -655,17 +1079,20 @@ class WorkflowService:
|
|||
return
|
||||
nodes = self.workflow_repository.list_nodes(node.workflow_id, branch_name=node.branch_name)
|
||||
normalized = normalize_material_bindings(raw_bindings, nodes)
|
||||
self.workflow_repository.update_workflow(
|
||||
workflow.workflow_id,
|
||||
metadata = with_branch_state(
|
||||
workflow.metadata,
|
||||
node.branch_name,
|
||||
{
|
||||
"metadata": {
|
||||
**(workflow.metadata or {}),
|
||||
"material_binding_schema_version": MATERIAL_BINDING_SCHEMA_VERSION,
|
||||
"material_binding_source_node_id": node.node_id,
|
||||
"material_binding_updated_at": node.updated_at,
|
||||
"material_bindings": normalized,
|
||||
}
|
||||
},
|
||||
activate=workflow.current_branch == node.branch_name,
|
||||
)
|
||||
self.workflow_repository.update_workflow(
|
||||
workflow.workflow_id,
|
||||
{"metadata": metadata},
|
||||
)
|
||||
|
||||
def _sync_prompt_plan_from_node(self, node: WorkflowNode) -> None:
|
||||
|
|
@ -679,17 +1106,20 @@ class WorkflowService:
|
|||
return
|
||||
nodes = self.workflow_repository.list_nodes(node.workflow_id, branch_name=node.branch_name)
|
||||
normalized = normalize_prompt_plan(raw_plan, nodes)
|
||||
self.workflow_repository.update_workflow(
|
||||
workflow.workflow_id,
|
||||
metadata = with_branch_state(
|
||||
workflow.metadata,
|
||||
node.branch_name,
|
||||
{
|
||||
"metadata": {
|
||||
**(workflow.metadata or {}),
|
||||
"prompt_plan_schema_version": PROMPT_PLAN_SCHEMA_VERSION,
|
||||
"prompt_plan_source_node_id": node.node_id,
|
||||
"prompt_plan_updated_at": node.updated_at,
|
||||
"prompt_plan": normalized,
|
||||
}
|
||||
},
|
||||
activate=workflow.current_branch == node.branch_name,
|
||||
)
|
||||
self.workflow_repository.update_workflow(
|
||||
workflow.workflow_id,
|
||||
{"metadata": metadata},
|
||||
)
|
||||
|
||||
|
||||
|
|
@ -759,6 +1189,13 @@ def _node_payload_changes(node: WorkflowNode, payload: dict[str, Any]) -> bool:
|
|||
return False
|
||||
|
||||
|
||||
def _int_or_original(value: Any) -> Any:
|
||||
try:
|
||||
return int(value)
|
||||
except (TypeError, ValueError):
|
||||
return value
|
||||
|
||||
|
||||
def _is_locked_image_adoption(node: WorkflowNode, payload: dict[str, Any]) -> bool:
|
||||
allowed_keys = {"status", "selected_image_id"}
|
||||
if any(key not in allowed_keys for key in payload):
|
||||
|
|
@ -807,6 +1244,37 @@ def _branch_copy_metadata(node: WorkflowNode, base_sort_order: int | None) -> di
|
|||
return metadata
|
||||
|
||||
|
||||
def _compatible_stage_sort_order(
|
||||
node_key: str,
|
||||
template: dict[str, Any],
|
||||
branch_nodes: list[WorkflowNode],
|
||||
) -> int:
|
||||
if node_key != "lower_body":
|
||||
return int(template.get("sort_order") or 0)
|
||||
upper = next(
|
||||
(node for node in branch_nodes if canonical_gradual_node_key(node.node_key) == "upper_body"),
|
||||
None,
|
||||
)
|
||||
full = next(
|
||||
(node for node in branch_nodes if canonical_gradual_node_key(node.node_key) == "full_body"),
|
||||
None,
|
||||
)
|
||||
if upper is not None and full is not None and full.sort_order - upper.sort_order > 1:
|
||||
return upper.sort_order + ((full.sort_order - upper.sort_order) // 2)
|
||||
desired = int(template.get("sort_order") or 60)
|
||||
occupied = {node.sort_order for node in branch_nodes}
|
||||
while desired in occupied:
|
||||
desired += 1
|
||||
return desired
|
||||
|
||||
|
||||
def _compatible_stage_status(sort_order: int, branch_nodes: list[WorkflowNode]) -> str:
|
||||
later_nodes = [node for node in branch_nodes if node.sort_order > sort_order]
|
||||
if any(is_character_gradual_node_completed(node, allow_legacy_planning_metadata=True) for node in later_nodes):
|
||||
return "skipped"
|
||||
return "draft"
|
||||
|
||||
|
||||
def _initial_node_templates(workflow_type: str) -> list[dict[str, Any]]:
|
||||
if workflow_type == "single_image":
|
||||
return [
|
||||
|
|
|
|||
56
config.yaml
56
config.yaml
|
|
@ -1,7 +1,7 @@
|
|||
plugin:
|
||||
name: astrbot_plugin_imagestudio
|
||||
display_name: ImageStudio
|
||||
version: 1.0.4
|
||||
version: 1.0.66
|
||||
|
||||
features:
|
||||
quick_profile: true
|
||||
|
|
@ -12,7 +12,7 @@ features:
|
|||
private_companion_integration: false
|
||||
|
||||
generation:
|
||||
# default_model 是 ImageStudio 路由名;route_name 留空时复用它。
|
||||
# default_model 鏄?ImageStudio 璺敱鍚嶏紱route_name 鐣欑┖鏃跺鐢ㄥ畠銆?
|
||||
default_model: ""
|
||||
provider:
|
||||
provider_id: imagestudio_default
|
||||
|
|
@ -27,9 +27,9 @@ generation:
|
|||
enabled: true
|
||||
|
||||
vision:
|
||||
# 素材识别路由;provider 中留空的连接字段会复用 generation.provider。
|
||||
# 若地址、密钥或超时不同且 provider_id 留空,会自动派生独立 Vision provider ID,避免覆盖生图配置。
|
||||
# Vision 模型必须单独填写,不能假定生图模型具备视觉理解能力。
|
||||
# 绱犳潗璇嗗埆璺敱锛沺rovider 涓暀绌虹殑杩炴帴瀛楁浼氬鐢?generation.provider銆?
|
||||
# 鑻ュ湴鍧€銆佸瘑閽ユ垨瓒呮椂涓嶅悓涓?provider_id 鐣欑┖锛屼細鑷姩娲剧敓鐙珛 Vision provider ID锛岄伩鍏嶈鐩栫敓鍥鹃厤缃€?
|
||||
# Vision 妯″瀷蹇呴』鍗曠嫭濉啓锛屼笉鑳藉亣瀹氱敓鍥炬ā鍨嬪叿澶囪瑙夌悊瑙h兘鍔涖€?
|
||||
default_route: material_vlm
|
||||
provider:
|
||||
provider_id: ""
|
||||
|
|
@ -43,17 +43,26 @@ vision:
|
|||
priority: 10
|
||||
enabled: true
|
||||
|
||||
# 按功能分配模型路由。留空时回退到 generation.default_model 或 vision.default_route。
|
||||
# 鎸夊姛鑳藉垎閰嶆ā鍨嬭矾鐢便€傜暀绌烘椂鍥為€€鍒?generation.default_model 鎴?vision.default_route銆?
|
||||
model_assignments:
|
||||
material_recognition: material_vlm
|
||||
advanced_material_recognition: material_vlm
|
||||
# 留空时沿用进阶素材识别路由;填写后仅多图画风交叉归纳使用该视觉路由。
|
||||
advanced_style_consensus: ""
|
||||
advanced_material_validation_generation: ""
|
||||
profile_parse: ""
|
||||
persona_split: ""
|
||||
prompt_planning: ""
|
||||
visual_review_routes:
|
||||
- material_vlm
|
||||
cross_review_primary: material_vlm
|
||||
cross_review_secondary: material_vlm
|
||||
cross_review_routes:
|
||||
- material_vlm
|
||||
- material_vlm
|
||||
generation_cross_review_primary: ""
|
||||
generation_cross_review_secondary: ""
|
||||
generation_cross_review_routes: []
|
||||
quick_generation: ""
|
||||
portrait_generation: ""
|
||||
halfbody_generation: ""
|
||||
|
|
@ -63,11 +72,26 @@ model_assignments:
|
|||
|
||||
recognition_prompts:
|
||||
default_instruction: >-
|
||||
请先判断图片中素材的准确分类,再尽可能完整地提取所有可见事实,包括物品类型、主色与辅色、材质、纹理、结构、轮廓、图案、裁剪、缝线、褶皱、装饰、配件、状态、视角、光照及可复用的设计特征。不要把无法确认的推测写成事实,并明确区分确定信息与低置信度判断。根据识别结果生成一段细节充分、可直接用于生图且能够保留原素材设计的正向提示词,同时生成用于排除错误类别、错误材质、结构畸变、文字、水印和无关背景的负向提示词。仅返回 JSON,字段必须包含 prompt、negative_prompt、tags、confidence、description;tags 应简短、可检索,并包含分类、颜色、材质和关键设计元素。
|
||||
璇峰厛鍒ゆ柇鍥剧墖涓礌鏉愮殑鍑嗙‘鍒嗙被锛屽啀灏藉彲鑳藉畬鏁村湴鎻愬彇鎵€鏈夊彲瑙佷簨瀹烇紝鍖呮嫭鐗╁搧绫诲瀷銆佷富鑹蹭笌杈呰壊銆佹潗璐ㄣ€佺汗鐞嗐€佺粨鏋勩€佽疆寤撱€佸浘妗堛€佽鍓€佺紳绾裤€佽ざ鐨便€佽楗般€侀厤浠躲€佺姸鎬併€佽瑙掋€佸厜鐓у強鍙鐢ㄧ殑璁捐鐗瑰緛銆備笉瑕佹妸鏃犳硶纭鐨勬帹娴嬪啓鎴愪簨瀹烇紝骞舵槑纭尯鍒嗙‘瀹氫俊鎭笌浣庣疆淇″害鍒ゆ柇銆傛牴鎹瘑鍒粨鏋滅敓鎴愪竴娈电粏鑺傚厖鍒嗐€佸彲鐩存帴鐢ㄤ簬鐢熷浘涓旇兘澶熶繚鐣欏師绱犳潗璁捐鐨勬鍚戞彁绀鸿瘝锛屽悓鏃剁敓鎴愮敤浜庢帓闄ら敊璇被鍒€侀敊璇潗璐ㄣ€佺粨鏋勭暩鍙樸€佹枃瀛椼€佹按鍗板拰鏃犲叧鑳屾櫙鐨勮礋鍚戞彁绀鸿瘝銆備粎杩斿洖 JSON锛屽瓧娈靛繀椤诲寘鍚?prompt銆乶egative_prompt銆乼ags銆乧onfidence銆乨escription锛泃ags 搴旂畝鐭€佸彲妫€绱紝骞跺寘鍚垎绫汇€侀鑹层€佹潗璐ㄥ拰鍏抽敭璁捐鍏冪礌銆?
|
||||
category_requirements: []
|
||||
|
||||
# 提示词转译、内容审核和普通/NSFW 模型动态分流。
|
||||
# 默认只启用本地规则审核;LLM 转译与成人自动分流必须由管理员明确开启。
|
||||
# 杩涢樁绱犳潗楠岃瘉鍥剧殑鐙珛棰勭畻瀹堝崼銆傚紑鍚悗锛屾瘡娆¢獙璇侊紙鍚嚜鍔?NSFW
|
||||
# 鍥為€€锛夐兘浼氬湪璋冪敤鎻愪緵鏂瑰墠妫€鏌ユ椂闂寸獥鍙e唴鐨勪换鍔°€佸浘鐗囧拰浼扮畻璐圭敤銆?
|
||||
advanced_materials:
|
||||
recognition_concurrency: 2
|
||||
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:
|
||||
translation:
|
||||
enabled: false
|
||||
|
|
@ -115,20 +139,21 @@ chat_access:
|
|||
allow_4k: false
|
||||
|
||||
storage:
|
||||
# 这里使用逻辑 data/... 路径;AstrBot 运行时映射到
|
||||
# data/plugin_data/astrbot_plugin_imagestudio/...,本地开发仍映射到仓库 data/。
|
||||
# 杩欓噷浣跨敤閫昏緫 data/... 璺緞锛汚strBot 杩愯鏃舵槧灏勫埌
|
||||
# data/plugin_data/astrbot_plugin_imagestudio/...锛屾湰鍦板紑鍙戜粛鏄犲皠鍒颁粨搴?data/銆?
|
||||
db_path: data/db/imagestudio.db
|
||||
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:
|
||||
# auto: 默认使用 SQLite;配置并启用 PostgreSQL 后自动切换
|
||||
# sqlite: 强制使用本地 SQLite
|
||||
# postgresql: 强制使用 PostgreSQL,驱动或连接配置缺失时会直接报错
|
||||
# auto: 榛樿浣跨敤 SQLite锛涢厤缃苟鍚敤 PostgreSQL 鍚庤嚜鍔ㄥ垏鎹?
|
||||
# sqlite: 寮哄埗浣跨敤鏈湴 SQLite
|
||||
# postgresql: 寮哄埗浣跨敤 PostgreSQL锛岄┍鍔ㄦ垨杩炴帴閰嶇疆缂哄け鏃朵細鐩存帴鎶ラ敊
|
||||
backend: auto
|
||||
sqlite:
|
||||
path: data/db/imagestudio.db
|
||||
|
|
@ -145,3 +170,6 @@ security:
|
|||
integration_token_set: false
|
||||
allowed_integration_plugins:
|
||||
- astrbot_plugin_private_companion
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
384
core/astrbot_provider_bridge.py
普通文件
384
core/astrbot_provider_bridge.py
普通文件
|
|
@ -0,0 +1,384 @@
|
|||
"""Read-only bridge to AstrBot's configured chat-completion providers."""
|
||||
|
||||
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,
|
||||
)
|
||||
|
||||
|
||||
_context: Any | None = None
|
||||
_runtime_loop: asyncio.AbstractEventLoop | None = None
|
||||
ASTRBOT_VISION_TIMEOUT_SECONDS = 270
|
||||
|
||||
|
||||
def configure_astrbot_provider_bridge(context: Any | None, loop: asyncio.AbstractEventLoop | None) -> None:
|
||||
global _context, _runtime_loop
|
||||
_context = context
|
||||
_runtime_loop = loop
|
||||
|
||||
|
||||
def list_astrbot_text_models() -> list[dict[str, str]]:
|
||||
getter = getattr(_context, "get_all_providers", None)
|
||||
if not callable(getter):
|
||||
return []
|
||||
try:
|
||||
providers = getter() or []
|
||||
except Exception:
|
||||
return []
|
||||
result: list[dict[str, str]] = []
|
||||
for provider in providers:
|
||||
config = getattr(provider, "provider_config", None)
|
||||
config = config if isinstance(config, Mapping) else {}
|
||||
provider_id = str(config.get("id") or _meta_value(provider, "id") or "").strip()
|
||||
if not provider_id:
|
||||
continue
|
||||
model = str(_call_text(provider, "get_model") or config.get("model") or _meta_value(provider, "model") or "").strip()
|
||||
name = str(config.get("name") or config.get("display_name") or provider_id).strip()
|
||||
result.append(
|
||||
{
|
||||
"route": f"astrbot:{provider_id}",
|
||||
"provider_id": provider_id,
|
||||
"name": name,
|
||||
"model": model,
|
||||
"modality": "text_generation",
|
||||
"source": "astrbot",
|
||||
}
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def astrbot_structured_text_callable(route_name: str):
|
||||
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
|
||||
|
||||
def complete(request_payload: dict[str, Any]) -> dict[str, Any] | str:
|
||||
purpose = str(request_payload.get("purpose") or "structured prompt processing")
|
||||
|
||||
async def invoke() -> str:
|
||||
response = await provider.text_chat(
|
||||
prompt=json.dumps(request_payload, ensure_ascii=False),
|
||||
system_prompt=(
|
||||
f"Perform {purpose}. Return one JSON object only. Preserve the user's visual intent, "
|
||||
"preserve safety-relevant meaning, and do not invent facts."
|
||||
),
|
||||
)
|
||||
return str(getattr(response, "completion_text", "") or "").strip()
|
||||
|
||||
future = asyncio.run_coroutine_threadsafe(invoke(), loop)
|
||||
text = future.result(timeout=180)
|
||||
if not text:
|
||||
raise ValueError("AstrBot text provider returned an empty response")
|
||||
cleaned = re.sub(r"^```(?:json)?\s*|\s*```$", "", text, flags=re.IGNORECASE).strip()
|
||||
try:
|
||||
parsed = json.loads(cleaned)
|
||||
except json.JSONDecodeError:
|
||||
return text
|
||||
return parsed if isinstance(parsed, dict) else text
|
||||
|
||||
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.
|
||||
|
||||
ImageStudio's material service runs in AstrBot's web worker while provider
|
||||
clients belong to AstrBot's main asyncio loop. The adapter keeps that
|
||||
boundary explicit and uses AstrBot's public ``text_chat(image_urls=...)``
|
||||
contract, so credentials remain owned by AstrBot rather than being copied
|
||||
into the plugin configuration.
|
||||
"""
|
||||
|
||||
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
|
||||
model = str(
|
||||
_call_text(provider, "get_model")
|
||||
or _provider_config(provider).get("model")
|
||||
or _meta_value(provider, "model")
|
||||
or ""
|
||||
).strip()
|
||||
return _AstrBotVisionProvider(
|
||||
provider=provider,
|
||||
provider_id=provider_id,
|
||||
model=model,
|
||||
loop=loop,
|
||||
)
|
||||
|
||||
|
||||
class _AstrBotVisionProvider:
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
provider: Any,
|
||||
provider_id: str,
|
||||
model: str,
|
||||
loop: asyncio.AbstractEventLoop,
|
||||
) -> None:
|
||||
self.provider = provider
|
||||
self.provider_id = provider_id
|
||||
self.model = model
|
||||
self.loop = loop
|
||||
|
||||
def recognize_material(self, request: VisionRecognitionRequest) -> VisionRecognitionResult:
|
||||
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,
|
||||
error_type="invalid_request",
|
||||
)
|
||||
|
||||
async def invoke() -> str:
|
||||
response = await self.provider.text_chat(
|
||||
prompt=_material_user_prompt(request),
|
||||
image_urls=image_urls,
|
||||
system_prompt=_material_system_prompt(request),
|
||||
model=request.model or self.model or None,
|
||||
)
|
||||
return str(getattr(response, "completion_text", "") or "").strip()
|
||||
|
||||
future = asyncio.run_coroutine_threadsafe(invoke(), self.loop)
|
||||
try:
|
||||
text = future.result(timeout=ASTRBOT_VISION_TIMEOUT_SECONDS)
|
||||
except concurrent.futures.TimeoutError as exc:
|
||||
future.cancel()
|
||||
raise VisionProviderError(
|
||||
"AstrBot vision provider timed out",
|
||||
provider_id=self.provider_id,
|
||||
error_type="timeout",
|
||||
) from exc
|
||||
except Exception as exc:
|
||||
raise VisionProviderError(
|
||||
"AstrBot vision provider request failed",
|
||||
provider_id=self.provider_id,
|
||||
error_type="provider_error",
|
||||
details={"reason": _safe_error_reason(exc)},
|
||||
) from exc
|
||||
if not text:
|
||||
raise VisionProviderError(
|
||||
"AstrBot vision provider returned an empty response",
|
||||
provider_id=self.provider_id,
|
||||
error_type="invalid_response",
|
||||
)
|
||||
try:
|
||||
payload = _json_object_from_text(text)
|
||||
except (json.JSONDecodeError, ValueError) as exc:
|
||||
raise VisionProviderError(
|
||||
"AstrBot vision provider returned invalid JSON",
|
||||
provider_id=self.provider_id,
|
||||
error_type="invalid_response",
|
||||
details={"reason": str(exc), "content_preview": text[:500]},
|
||||
) from exc
|
||||
prompt = str(payload.get("prompt") or payload.get("positive_prompt") or payload.get("description") or "").strip()
|
||||
if not prompt:
|
||||
raise VisionProviderError(
|
||||
"AstrBot vision response must include prompt or description",
|
||||
provider_id=self.provider_id,
|
||||
error_type="invalid_response",
|
||||
details={"structured_keys": sorted(str(key) for key in payload)},
|
||||
)
|
||||
return VisionRecognitionResult(
|
||||
provider_id=self.provider_id,
|
||||
model=request.model or self.model or "astrbot-provider-model",
|
||||
prompt=prompt,
|
||||
negative_prompt=_optional_text(payload.get("negative_prompt")),
|
||||
tags=_string_list(payload.get("tags")),
|
||||
confidence=_optional_confidence(payload.get("confidence")),
|
||||
description=_optional_text(payload.get("description")),
|
||||
structured_data=payload,
|
||||
raw_metadata={
|
||||
"source": "astrbot_provider_bridge",
|
||||
"structured_keys": sorted(str(key) for key in payload),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def _provider_by_id(provider_id: str) -> Any | None:
|
||||
getter = getattr(_context, "get_provider_by_id", None)
|
||||
if callable(getter):
|
||||
try:
|
||||
return getter(provider_id)
|
||||
except Exception:
|
||||
return None
|
||||
return None
|
||||
|
||||
|
||||
def _provider_config(provider: Any) -> Mapping[str, Any]:
|
||||
config = getattr(provider, "provider_config", None)
|
||||
return config if isinstance(config, Mapping) else {}
|
||||
|
||||
|
||||
def _material_system_prompt(request: VisionRecognitionRequest) -> str:
|
||||
configured = str(request.metadata.get("recognition_instruction") or "").strip()
|
||||
return configured or (
|
||||
"Analyze the visible reference material exhaustively and return one JSON object only. "
|
||||
"Extract visible facts without presenting guesses as observations. Include prompt, negative_prompt, tags, "
|
||||
"confidence, description, raw_analysis.attributes, normalized_asset_profile and optional generation_capsules. "
|
||||
"Each raw attribute should include value, confidence and source_state."
|
||||
)
|
||||
|
||||
|
||||
def _material_user_prompt(request: VisionRecognitionRequest) -> str:
|
||||
return (
|
||||
f"Material name: {request.display_name}\n"
|
||||
f"Known prompt: {request.prompt or ''}\n"
|
||||
f"Known negative prompt: {request.negative_prompt or ''}\n"
|
||||
f"Known tags: {', '.join(request.tags)}\n"
|
||||
"Inspect the attached image and return the requested structured material record."
|
||||
)
|
||||
|
||||
|
||||
def _json_object_from_text(value: str) -> dict[str, Any]:
|
||||
cleaned = re.sub(r"^```(?:json)?\s*|\s*```$", "", value.strip(), flags=re.IGNORECASE).strip()
|
||||
candidates = [cleaned]
|
||||
start = cleaned.find("{")
|
||||
end = cleaned.rfind("}")
|
||||
if start >= 0 and end > start:
|
||||
candidates.append(cleaned[start : end + 1])
|
||||
last_error: json.JSONDecodeError | None = None
|
||||
for candidate in candidates:
|
||||
try:
|
||||
parsed = json.loads(re.sub(r",\s*([}\]])", r"\1", candidate))
|
||||
except json.JSONDecodeError as exc:
|
||||
last_error = exc
|
||||
continue
|
||||
if isinstance(parsed, dict):
|
||||
return parsed
|
||||
raise ValueError("vision response JSON must be an object")
|
||||
raise last_error or ValueError("vision response did not contain a JSON object")
|
||||
|
||||
|
||||
def _string_list(value: Any) -> list[str]:
|
||||
values = value.split(",") if isinstance(value, str) else value if isinstance(value, list) else []
|
||||
return sorted({str(item).strip() for item in values if str(item).strip()})
|
||||
|
||||
|
||||
def _optional_text(value: Any) -> str | None:
|
||||
text = str(value or "").strip()
|
||||
return text or None
|
||||
|
||||
|
||||
def _optional_confidence(value: Any) -> float | None:
|
||||
try:
|
||||
confidence = float(value)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
return min(max(confidence, 0.0), 1.0)
|
||||
|
||||
|
||||
def _safe_error_reason(exc: Exception) -> str:
|
||||
reason = str(exc).strip() or exc.__class__.__name__
|
||||
return reason[:500]
|
||||
|
||||
|
||||
def _meta_value(provider: Any, key: str) -> Any:
|
||||
meta = getattr(provider, "meta", None)
|
||||
if not callable(meta):
|
||||
return None
|
||||
try:
|
||||
return getattr(meta(), key, None)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
|
||||
def _call_text(provider: Any, method_name: str) -> str:
|
||||
method = getattr(provider, method_name, None)
|
||||
if not callable(method):
|
||||
return ""
|
||||
try:
|
||||
return str(method() or "")
|
||||
except Exception:
|
||||
return ""
|
||||
|
|
@ -10,11 +10,14 @@ from core.runtime_paths import resolve_config_path
|
|||
from database.db_helper import load_config_payload
|
||||
|
||||
|
||||
PLUGIN_VERSION = "1.0.66"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PluginConfig:
|
||||
name: str = "astrbot_plugin_imagestudio"
|
||||
display_name: str = "ImageStudio"
|
||||
version: str = "3.0.0"
|
||||
version: str = PLUGIN_VERSION
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
|
|
@ -22,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"
|
||||
|
|
@ -61,13 +65,19 @@ class ConfigService:
|
|||
plugin=PluginConfig(
|
||||
name=str(plugin.get("name", "astrbot_plugin_imagestudio")),
|
||||
display_name=str(plugin.get("display_name", "ImageStudio")),
|
||||
version=str(plugin.get("version", "3.0.0")),
|
||||
# The installed package version is immutable metadata. A
|
||||
# writable AstrBot config may survive plugin upgrades, so it
|
||||
# must not override the version shown by ui/bootstrap.
|
||||
version=PLUGIN_VERSION,
|
||||
),
|
||||
features={key: bool(value) for key, value in features.items()},
|
||||
storage=StorageConfig(
|
||||
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")),
|
||||
|
|
@ -77,3 +87,6 @@ class ConfigService:
|
|||
|
||||
def _resolve_path(path: str | Path) -> Path:
|
||||
return resolve_config_path(path)
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -76,4 +76,8 @@ def _format_scalar(value: Any) -> str:
|
|||
return "null"
|
||||
if isinstance(value, (int, float)):
|
||||
return str(value)
|
||||
if isinstance(value, (Mapping, list)):
|
||||
# JSON flow collections are valid YAML and preserve nested adapter
|
||||
# objects without requiring a full YAML dependency.
|
||||
return json.dumps(value, ensure_ascii=False)
|
||||
return json.dumps(str(value), ensure_ascii=False)
|
||||
|
|
|
|||
|
|
@ -41,6 +41,64 @@ def provider_failure_attribution(
|
|||
return attribution
|
||||
|
||||
|
||||
def provider_failure_user_message(failure: dict[str, Any]) -> str:
|
||||
"""Build a concise, sanitized error that is useful in the UI and logs."""
|
||||
|
||||
category = str(failure.get("category") or "provider_error")
|
||||
labels = {
|
||||
"auth_error": "生成服务鉴权失败",
|
||||
"configuration_error": "生成服务配置错误",
|
||||
"network_error": "无法连接生成服务",
|
||||
"timeout": "生成服务请求超时",
|
||||
"rate_limited": "生成服务请求过于频繁",
|
||||
"provider_unavailable": "生成服务暂时不可用",
|
||||
"request_error": "生成服务拒绝了请求",
|
||||
"invalid_response": "生成服务返回了无法识别的数据",
|
||||
"provider_pool_exhausted": "所有候选生成服务均调用失败",
|
||||
}
|
||||
label = labels.get(category, "图像生成服务调用失败")
|
||||
status_code = failure.get("status_code")
|
||||
if status_code is not None:
|
||||
label = f"{label}(HTTP {status_code})"
|
||||
detail = _provider_response_message(failure.get("details"))
|
||||
route_name = _first_text(failure.get("route_name"), failure.get("model_id"), "")
|
||||
suffix = f";路由:{route_name}" if route_name != "unknown" else ""
|
||||
return f"{label}{f':{detail}' if detail else ''}{suffix}"
|
||||
|
||||
|
||||
def _provider_response_message(details: Any) -> str:
|
||||
if not isinstance(details, dict):
|
||||
return ""
|
||||
body = details.get("body_preview")
|
||||
if isinstance(body, str) and body.strip():
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(body)
|
||||
except (ValueError, TypeError):
|
||||
return _safe_message(body, limit=180)
|
||||
if isinstance(parsed, dict):
|
||||
error = parsed.get("error")
|
||||
if isinstance(error, dict):
|
||||
message = error.get("message") or error.get("detail") or error.get("type")
|
||||
if message:
|
||||
return _safe_message(str(message), limit=180)
|
||||
for key in ("message", "detail", "error"):
|
||||
value = parsed.get(key)
|
||||
if isinstance(value, str) and value.strip():
|
||||
return _safe_message(value, limit=180)
|
||||
reason = details.get("reason")
|
||||
if not reason:
|
||||
return ""
|
||||
safe_reason = _safe_message(str(reason), limit=180)
|
||||
lowered = safe_reason.lower()
|
||||
if "unexpected_eof_while_reading" in lowered or "eof occurred in violation of protocol" in lowered:
|
||||
return "上游生成服务在返回结果前提前关闭了 TLS 连接,通常是线路波动或网关超时,请稍后在队列中重试"
|
||||
if "read operation timed out" in lowered or "readtimeout" in lowered:
|
||||
return "上游生成服务长时间未返回结果,请稍后在队列中重试或提高适配器超时时间"
|
||||
return safe_reason
|
||||
|
||||
|
||||
def _category(error_type: str, status_code: int | None) -> str:
|
||||
if error_type == "configuration_error":
|
||||
return "configuration_error"
|
||||
|
|
|
|||
|
|
@ -53,6 +53,8 @@ class RequestContext:
|
|||
user_id: str | None = None,
|
||||
client_ip: str | None = None,
|
||||
source: str = "standalone",
|
||||
platform: str = "astrbot",
|
||||
session_key: str | None = None,
|
||||
permissions: set[str] | frozenset[str] | None = None,
|
||||
) -> "RequestContext":
|
||||
return cls(
|
||||
|
|
@ -60,6 +62,8 @@ class RequestContext:
|
|||
user_id=user_id,
|
||||
client_ip=client_ip,
|
||||
source=source,
|
||||
platform=platform,
|
||||
session_key=session_key,
|
||||
is_authenticated=bool(username or user_id),
|
||||
permissions=frozenset(permissions or set()),
|
||||
)
|
||||
|
|
|
|||
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,
|
||||
|
|
|
|||
|
|
@ -8,10 +8,12 @@ from __future__ import annotations
|
|||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import sqlite3
|
||||
import ast
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Any, Iterable, Mapping
|
||||
from typing import Any, Callable, Iterable, Mapping
|
||||
|
||||
from core.runtime_paths import (
|
||||
REPO_ROOT,
|
||||
|
|
@ -22,6 +24,7 @@ from core.runtime_paths import (
|
|||
)
|
||||
|
||||
_RUNTIME_CONFIG_OVERRIDES: dict[str, Any] = {}
|
||||
_RUNTIME_CONFIG_PERSISTER: Callable[[Mapping[str, Any]], Mapping[str, Any] | None] | None = None
|
||||
|
||||
|
||||
def set_runtime_config_overrides(payload: Mapping[str, Any] | None) -> None:
|
||||
|
|
@ -34,6 +37,30 @@ def get_runtime_config_overrides() -> dict[str, Any]:
|
|||
return _copy_plain_mapping(_RUNTIME_CONFIG_OVERRIDES)
|
||||
|
||||
|
||||
def set_runtime_config_persister(
|
||||
persister: Callable[[Mapping[str, Any]], Mapping[str, Any] | None] | None,
|
||||
) -> None:
|
||||
"""Register the AstrBot-owned plugin-config writer for the current runtime."""
|
||||
|
||||
global _RUNTIME_CONFIG_PERSISTER
|
||||
_RUNTIME_CONFIG_PERSISTER = persister
|
||||
|
||||
|
||||
def persist_runtime_config_values(values: Mapping[str, Any]) -> dict[str, Any]:
|
||||
"""Persist flattened ImageStudio paths through AstrBot's config object."""
|
||||
|
||||
if not values:
|
||||
return {"available": _RUNTIME_CONFIG_PERSISTER is not None, "saved": False, "paths": []}
|
||||
if _RUNTIME_CONFIG_PERSISTER is None:
|
||||
return {"available": False, "saved": False, "paths": sorted(str(path) for path in values)}
|
||||
result = _RUNTIME_CONFIG_PERSISTER(_copy_plain_mapping(values))
|
||||
payload = dict(result) if isinstance(result, Mapping) else {}
|
||||
payload.setdefault("available", True)
|
||||
payload.setdefault("saved", True)
|
||||
payload.setdefault("paths", sorted(str(path) for path in values))
|
||||
return payload
|
||||
|
||||
|
||||
def load_config_payload(config_path: str | Path = "config.yaml") -> dict[str, Any]:
|
||||
path = resolve_config_path(config_path)
|
||||
payload = load_config_file_payload(path)
|
||||
|
|
@ -133,9 +160,13 @@ class Database:
|
|||
if self.backend == "sqlite":
|
||||
db_path = self._resolve_sqlite_path()
|
||||
db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
conn = sqlite3.connect(db_path)
|
||||
# Parallel image tasks briefly overlap while recording queue,
|
||||
# node, image, and audit rows. Give SQLite enough time to hand the
|
||||
# writer lock to the next task instead of failing immediately.
|
||||
conn = sqlite3.connect(db_path, timeout=30.0)
|
||||
conn.row_factory = sqlite3.Row
|
||||
conn.execute("PRAGMA foreign_keys = ON")
|
||||
conn.execute("PRAGMA busy_timeout = 30000")
|
||||
return conn
|
||||
return self._connect_postgresql()
|
||||
|
||||
|
|
@ -145,9 +176,11 @@ class Database:
|
|||
self._ensure_schema_migrations(conn)
|
||||
for migration_path in self._iter_migrations(migrations_dir):
|
||||
version = migration_path.stem
|
||||
if self._migration_applied(conn, version):
|
||||
script = migration_path.read_text(encoding="utf-8")
|
||||
if self._migration_applied(conn, version) and self._migration_tables_exist(conn, script):
|
||||
continue
|
||||
self._execute_script(conn, migration_path.read_text(encoding="utf-8"))
|
||||
self._execute_script(conn, script)
|
||||
if not self._migration_applied(conn, version):
|
||||
self._mark_migration_applied(conn, version)
|
||||
if self.backend == "postgresql":
|
||||
conn.commit()
|
||||
|
|
@ -226,6 +259,24 @@ class Database:
|
|||
(version,),
|
||||
)
|
||||
|
||||
def _migration_tables_exist(self, conn, script: str) -> bool:
|
||||
table_names = re.findall(
|
||||
r"CREATE\s+TABLE\s+IF\s+NOT\s+EXISTS\s+([A-Za-z_][A-Za-z0-9_]*)",
|
||||
script,
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
if not table_names:
|
||||
return True
|
||||
placeholder = self._placeholder
|
||||
if self.backend == "sqlite":
|
||||
query = f"SELECT name FROM sqlite_master WHERE type = 'table' AND name = {placeholder}"
|
||||
else:
|
||||
query = (
|
||||
"SELECT table_name FROM information_schema.tables "
|
||||
f"WHERE table_schema = current_schema() AND table_name = {placeholder}"
|
||||
)
|
||||
return all(conn.execute(query, (table_name,)).fetchone() is not None for table_name in table_names)
|
||||
|
||||
def _execute_script(self, conn, script: str) -> None:
|
||||
if self.backend == "sqlite":
|
||||
conn.executescript(script)
|
||||
|
|
@ -259,6 +310,9 @@ class _Psycopg2ConnectionAdapter:
|
|||
def commit(self) -> None:
|
||||
self.connection.commit()
|
||||
|
||||
def rollback(self) -> None:
|
||||
self.connection.rollback()
|
||||
|
||||
def close(self) -> None:
|
||||
self.connection.close()
|
||||
|
||||
|
|
@ -360,9 +414,22 @@ def _load_yaml_subset(text: str) -> dict[str, Any]:
|
|||
def _parse_scalar(value: str) -> Any:
|
||||
if len(value) >= 2 and value[0] == value[-1] == '"':
|
||||
try:
|
||||
return json.loads(value)
|
||||
parsed = json.loads(value)
|
||||
if isinstance(parsed, str) and parsed.lstrip().startswith(("{", "[")):
|
||||
try:
|
||||
legacy = ast.literal_eval(parsed)
|
||||
except (SyntaxError, ValueError):
|
||||
return parsed
|
||||
if isinstance(legacy, (dict, list)):
|
||||
return legacy
|
||||
return parsed
|
||||
except json.JSONDecodeError:
|
||||
return value[1:-1]
|
||||
if value.startswith(("{", "[")):
|
||||
try:
|
||||
return json.loads(value)
|
||||
except json.JSONDecodeError:
|
||||
pass
|
||||
if len(value) >= 2 and value[0] == value[-1] == "'":
|
||||
return value[1:-1].replace("''", "'")
|
||||
lowered = value.lower()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,102 @@
|
|||
CREATE TABLE IF NOT EXISTS workflow_generation_rounds (
|
||||
round_id TEXT PRIMARY KEY,
|
||||
workflow_id TEXT NOT NULL,
|
||||
node_id TEXT NOT NULL,
|
||||
branch_name TEXT NOT NULL DEFAULT 'main',
|
||||
round_kind TEXT NOT NULL DEFAULT 'primary',
|
||||
round_number INTEGER NOT NULL,
|
||||
display_code TEXT NOT NULL,
|
||||
parent_round_id TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'draft',
|
||||
requested_count INTEGER NOT NULL DEFAULT 0,
|
||||
idempotency_key TEXT,
|
||||
prompt_snapshot_json TEXT,
|
||||
reference_snapshot_json TEXT,
|
||||
feedback_snapshot_json TEXT,
|
||||
metadata_json TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(workflow_id) REFERENCES profile_workflows(workflow_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(node_id) REFERENCES workflow_nodes(node_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(parent_round_id) REFERENCES workflow_generation_rounds(round_id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_workflow_rounds_sequence
|
||||
ON workflow_generation_rounds(node_id, branch_name, round_kind, round_number);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_workflow_rounds_idempotency
|
||||
ON workflow_generation_rounds(idempotency_key)
|
||||
WHERE idempotency_key IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_workflow_rounds_workflow
|
||||
ON workflow_generation_rounds(workflow_id, branch_name, created_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_workflow_rounds_node
|
||||
ON workflow_generation_rounds(node_id, round_kind, created_at);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_workflow_rounds_status
|
||||
ON workflow_generation_rounds(status, updated_at);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS workflow_generation_candidates (
|
||||
candidate_id TEXT PRIMARY KEY,
|
||||
origin_round_id TEXT NOT NULL,
|
||||
candidate_index INTEGER NOT NULL,
|
||||
generation_status TEXT NOT NULL DEFAULT 'planned',
|
||||
image_id TEXT,
|
||||
current_task_id TEXT,
|
||||
source_candidate_id TEXT,
|
||||
model_route TEXT,
|
||||
provider_id TEXT,
|
||||
provider_model TEXT,
|
||||
prompt_snapshot_json TEXT,
|
||||
failure_json TEXT,
|
||||
metadata_json TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(origin_round_id) REFERENCES workflow_generation_rounds(round_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(image_id) REFERENCES generation_images(image_id) ON DELETE SET NULL,
|
||||
FOREIGN KEY(current_task_id) REFERENCES generation_tasks(task_id) ON DELETE SET NULL,
|
||||
FOREIGN KEY(source_candidate_id) REFERENCES workflow_generation_candidates(candidate_id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_workflow_candidates_origin_index
|
||||
ON workflow_generation_candidates(origin_round_id, candidate_index);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_workflow_candidates_image
|
||||
ON workflow_generation_candidates(image_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_workflow_candidates_task
|
||||
ON workflow_generation_candidates(current_task_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_workflow_candidates_source
|
||||
ON workflow_generation_candidates(source_candidate_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS workflow_round_candidate_pool (
|
||||
round_id TEXT NOT NULL,
|
||||
candidate_id TEXT NOT NULL,
|
||||
display_index INTEGER NOT NULL,
|
||||
display_code TEXT NOT NULL,
|
||||
pool_state TEXT NOT NULL DEFAULT 'new',
|
||||
preference_status TEXT NOT NULL DEFAULT 'neutral',
|
||||
preference_reason TEXT,
|
||||
is_final INTEGER NOT NULL DEFAULT 0,
|
||||
metadata_json TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY(round_id, candidate_id),
|
||||
FOREIGN KEY(round_id) REFERENCES workflow_generation_rounds(round_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(candidate_id) REFERENCES workflow_generation_candidates(candidate_id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_round_candidate_pool_display_index
|
||||
ON workflow_round_candidate_pool(round_id, display_index);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_round_candidate_pool_display_code
|
||||
ON workflow_round_candidate_pool(round_id, display_code);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_round_candidate_pool_final
|
||||
ON workflow_round_candidate_pool(round_id)
|
||||
WHERE is_final = 1;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_round_candidate_pool_candidate
|
||||
ON workflow_round_candidate_pool(candidate_id, created_at);
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
CREATE TABLE IF NOT EXISTS character_profile_cards (
|
||||
card_id TEXT PRIMARY KEY,
|
||||
profile_id TEXT NOT NULL,
|
||||
workflow_id TEXT NOT NULL,
|
||||
branch_name TEXT NOT NULL DEFAULT 'main',
|
||||
version INTEGER NOT NULL DEFAULT 1,
|
||||
status TEXT NOT NULL DEFAULT 'sealed',
|
||||
persona_document TEXT NOT NULL,
|
||||
idempotency_key TEXT UNIQUE,
|
||||
metadata_json TEXT,
|
||||
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(workflow_id) REFERENCES profile_workflows(workflow_id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_profile_cards_profile_version
|
||||
ON character_profile_cards(profile_id, version);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_profile_cards_workflow
|
||||
ON character_profile_cards(workflow_id, branch_name, status);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS character_profile_card_assets (
|
||||
asset_id TEXT PRIMARY KEY,
|
||||
card_id TEXT NOT NULL,
|
||||
asset_role TEXT NOT NULL,
|
||||
image_id TEXT NOT NULL,
|
||||
image_uri TEXT NOT NULL,
|
||||
metadata_json TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(card_id) REFERENCES character_profile_cards(card_id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(image_id) REFERENCES generation_images(image_id) ON DELETE RESTRICT
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_profile_card_asset_role
|
||||
ON character_profile_card_assets(card_id, asset_role);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_profile_card_assets_image
|
||||
ON character_profile_card_assets(image_id);
|
||||
|
|
@ -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);
|
||||
|
|
@ -64,11 +64,150 @@ TABLES: tuple[TableSpec, ...] = (
|
|||
"task_id",
|
||||
(
|
||||
ColumnSpec("task_id", nullable=False),
|
||||
ColumnSpec("profile_id"),
|
||||
ColumnSpec("workflow_id"),
|
||||
ColumnSpec("node_id"),
|
||||
ColumnSpec("task_type", nullable=False),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("model_route"),
|
||||
ColumnSpec("progress", nullable=False),
|
||||
ColumnSpec("idempotency_key"),
|
||||
ColumnSpec("request_json"),
|
||||
ColumnSpec("result_json"),
|
||||
ColumnSpec("error_message"),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"generation_images",
|
||||
"image_id",
|
||||
(
|
||||
ColumnSpec("image_id", nullable=False),
|
||||
ColumnSpec("profile_id"),
|
||||
ColumnSpec("workflow_id"),
|
||||
ColumnSpec("node_id"),
|
||||
ColumnSpec("task_id"),
|
||||
ColumnSpec("batch_id"),
|
||||
ColumnSpec("image_uri", nullable=False),
|
||||
ColumnSpec("thumbnail_uri"),
|
||||
ColumnSpec("positive_prompt"),
|
||||
ColumnSpec("negative_prompt"),
|
||||
ColumnSpec("model_used"),
|
||||
ColumnSpec("model_route"),
|
||||
ColumnSpec("is_selected", nullable=False),
|
||||
ColumnSpec("is_archived", nullable=False),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"workflow_history",
|
||||
"history_id",
|
||||
(
|
||||
ColumnSpec("history_id", nullable=False),
|
||||
ColumnSpec("profile_id"),
|
||||
ColumnSpec("workflow_id", nullable=False),
|
||||
ColumnSpec("node_id"),
|
||||
ColumnSpec("image_id"),
|
||||
ColumnSpec("branch_name", nullable=False),
|
||||
ColumnSpec("action", nullable=False),
|
||||
ColumnSpec("title"),
|
||||
ColumnSpec("prompt"),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"workflow_branches",
|
||||
"branch_id",
|
||||
(
|
||||
ColumnSpec("branch_id", nullable=False),
|
||||
ColumnSpec("workflow_id", nullable=False),
|
||||
ColumnSpec("branch_name", nullable=False),
|
||||
ColumnSpec("base_branch_name"),
|
||||
ColumnSpec("base_node_id"),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"workflow_generation_rounds",
|
||||
"round_id",
|
||||
(
|
||||
ColumnSpec("round_id", nullable=False),
|
||||
ColumnSpec("workflow_id", nullable=False),
|
||||
ColumnSpec("node_id", nullable=False),
|
||||
ColumnSpec("branch_name", nullable=False),
|
||||
ColumnSpec("round_kind", nullable=False),
|
||||
ColumnSpec("round_number", nullable=False),
|
||||
ColumnSpec("display_code", nullable=False),
|
||||
ColumnSpec("parent_round_id"),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("requested_count", nullable=False),
|
||||
ColumnSpec("idempotency_key"),
|
||||
ColumnSpec("prompt_snapshot_json"),
|
||||
ColumnSpec("reference_snapshot_json"),
|
||||
ColumnSpec("feedback_snapshot_json"),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"workflow_generation_candidates",
|
||||
"candidate_id",
|
||||
(
|
||||
ColumnSpec("candidate_id", nullable=False),
|
||||
ColumnSpec("origin_round_id", nullable=False),
|
||||
ColumnSpec("candidate_index", nullable=False),
|
||||
ColumnSpec("generation_status", nullable=False),
|
||||
ColumnSpec("image_id"),
|
||||
ColumnSpec("current_task_id"),
|
||||
ColumnSpec("source_candidate_id"),
|
||||
ColumnSpec("model_route"),
|
||||
ColumnSpec("provider_id"),
|
||||
ColumnSpec("provider_model"),
|
||||
ColumnSpec("prompt_snapshot_json"),
|
||||
ColumnSpec("failure_json"),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"workflow_round_candidate_pool",
|
||||
"round_id,candidate_id",
|
||||
(
|
||||
ColumnSpec("round_id", nullable=False),
|
||||
ColumnSpec("candidate_id", nullable=False),
|
||||
ColumnSpec("display_index", nullable=False),
|
||||
ColumnSpec("display_code", nullable=False),
|
||||
ColumnSpec("pool_state", nullable=False),
|
||||
ColumnSpec("preference_status", nullable=False),
|
||||
ColumnSpec("preference_reason"),
|
||||
ColumnSpec("is_final", nullable=False),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"character_profile_cards",
|
||||
"card_id",
|
||||
(
|
||||
ColumnSpec("card_id", nullable=False),
|
||||
ColumnSpec("profile_id", nullable=False),
|
||||
ColumnSpec("workflow_id", nullable=False),
|
||||
ColumnSpec("branch_name", nullable=False),
|
||||
ColumnSpec("version", nullable=False),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("persona_document", nullable=False),
|
||||
ColumnSpec("idempotency_key"),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"character_profile_card_assets",
|
||||
"asset_id",
|
||||
(
|
||||
ColumnSpec("asset_id", nullable=False),
|
||||
ColumnSpec("card_id", nullable=False),
|
||||
ColumnSpec("asset_role", nullable=False),
|
||||
ColumnSpec("image_id", nullable=False),
|
||||
ColumnSpec("image_uri", nullable=False),
|
||||
ColumnSpec("metadata_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
|
|
@ -93,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",
|
||||
|
|
@ -114,6 +408,64 @@ TABLES: tuple[TableSpec, ...] = (
|
|||
ColumnSpec("config_json", nullable=False, sensitive=True),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"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(
|
||||
"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",
|
||||
(
|
||||
ColumnSpec("integration_log_id", nullable=False),
|
||||
ColumnSpec("integration_name", nullable=False),
|
||||
ColumnSpec("action", nullable=False),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("request_json"),
|
||||
ColumnSpec("response_json"),
|
||||
ColumnSpec("error_message"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"backend_capability_profiles",
|
||||
"capability_profile_id",
|
||||
(
|
||||
ColumnSpec("capability_profile_id", nullable=False),
|
||||
ColumnSpec("provider_id", nullable=False),
|
||||
ColumnSpec("model_id", nullable=False),
|
||||
ColumnSpec("dialect", nullable=False),
|
||||
ColumnSpec("status", nullable=False),
|
||||
ColumnSpec("capabilities_json"),
|
||||
ColumnSpec("probe_result_json"),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
"model_providers",
|
||||
"provider_id",
|
||||
|
|
@ -122,7 +474,9 @@ TABLES: tuple[TableSpec, ...] = (
|
|||
ColumnSpec("display_name", nullable=False),
|
||||
ColumnSpec("provider_type", nullable=False),
|
||||
ColumnSpec("base_url", nullable=False),
|
||||
ColumnSpec("api_key_env_var", sensitive=True),
|
||||
ColumnSpec("api_key_secret_ref", sensitive=True),
|
||||
ColumnSpec("extra_headers_json", sensitive=True),
|
||||
),
|
||||
),
|
||||
TableSpec(
|
||||
|
|
|
|||
文件差异内容过多而无法显示
加载差异
|
|
@ -156,6 +156,53 @@ class JobRepository:
|
|||
conn.close()
|
||||
return [_row_to_task(row) for row in rows], int(total_row["total"] if total_row else 0)
|
||||
|
||||
def list_stale(
|
||||
self,
|
||||
*,
|
||||
statuses: set[str] | tuple[str, ...] | list[str],
|
||||
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. 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(
|
||||
sorted({str(status).strip() for status in statuses if str(status).strip()})
|
||||
)
|
||||
if not normalized_statuses:
|
||||
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(
|
||||
f"""
|
||||
SELECT {TASK_COLUMNS}
|
||||
FROM generation_tasks
|
||||
WHERE status IN ({status_placeholders})
|
||||
{age_clause}
|
||||
ORDER BY updated_at ASC, created_at ASC, task_id ASC
|
||||
{limit_clause}
|
||||
""",
|
||||
tuple(params),
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
return [_row_to_task(row) for row in rows]
|
||||
|
||||
def update(self, task_id: str, payload: dict[str, Any]) -> GenerationTask | None:
|
||||
placeholder = self.placeholder
|
||||
assignments, params = _update_assignments(payload, placeholder)
|
||||
|
|
@ -312,6 +359,77 @@ class JobRepository:
|
|||
conn.close()
|
||||
return [_row_to_task(row) for row in rows]
|
||||
|
||||
def delete_if_status(
|
||||
self,
|
||||
task_id: str,
|
||||
expected_statuses: set[str] | tuple[str, ...] | list[str],
|
||||
) -> dict[str, Any] | None:
|
||||
"""Delete a terminal task and release a node reference owned by it.
|
||||
|
||||
Queue rows are not the source of generated image metadata, so terminal
|
||||
task records can be removed without touching gallery images. The
|
||||
node's JSON reference is repaired in the same transaction to avoid a
|
||||
dangling ``last_task_id`` blocking a later submission.
|
||||
"""
|
||||
|
||||
normalized_statuses = tuple(
|
||||
sorted({str(status).strip() for status in expected_statuses if str(status).strip()})
|
||||
)
|
||||
if not normalized_statuses:
|
||||
return None
|
||||
placeholder = self.placeholder
|
||||
status_placeholders = ", ".join(placeholder for _ in normalized_statuses)
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
row = conn.execute(
|
||||
f"""
|
||||
SELECT {TASK_COLUMNS}
|
||||
FROM generation_tasks
|
||||
WHERE task_id = {placeholder}
|
||||
AND status IN ({status_placeholders})
|
||||
""",
|
||||
(task_id, *normalized_statuses),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
task = _row_to_task(row)
|
||||
cursor = conn.execute(
|
||||
f"""
|
||||
DELETE FROM generation_tasks
|
||||
WHERE task_id = {placeholder}
|
||||
AND status IN ({status_placeholders})
|
||||
""",
|
||||
(task_id, *normalized_statuses),
|
||||
)
|
||||
if int(cursor.rowcount or 0) <= 0:
|
||||
return None
|
||||
# Delete/claim the terminal row before touching its node. This
|
||||
# prevents a concurrent retry claim from succeeding after we have
|
||||
# already repaired the node metadata. The node read is locked on
|
||||
# PostgreSQL; SQLite already holds the database write lock after
|
||||
# the DELETE above.
|
||||
node_cleanup = _release_task_node_reference(
|
||||
conn,
|
||||
placeholder,
|
||||
task,
|
||||
lock_row=self.database.backend == "postgresql",
|
||||
)
|
||||
workflow_cleanup = _recover_failed_character_workflow(
|
||||
conn,
|
||||
placeholder,
|
||||
task,
|
||||
node_cleanup=node_cleanup,
|
||||
lock_row=self.database.backend == "postgresql",
|
||||
)
|
||||
conn.commit()
|
||||
return {
|
||||
"task": task,
|
||||
"node_cleanup": node_cleanup,
|
||||
"workflow_cleanup": workflow_cleanup,
|
||||
}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def _row_to_task(row) -> GenerationTask:
|
||||
return GenerationTask(
|
||||
|
|
@ -333,6 +451,114 @@ def _row_to_task(row) -> GenerationTask:
|
|||
)
|
||||
|
||||
|
||||
def _release_task_node_reference(
|
||||
conn,
|
||||
placeholder: str,
|
||||
task: GenerationTask,
|
||||
*,
|
||||
lock_row: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
if not task.node_id:
|
||||
return {"released": False, "reason": "task_has_no_node"}
|
||||
lock_clause = " FOR UPDATE" if lock_row else ""
|
||||
node = conn.execute(
|
||||
f"SELECT status, metadata_json FROM workflow_nodes WHERE node_id = {placeholder}{lock_clause}",
|
||||
(task.node_id,),
|
||||
).fetchone()
|
||||
if node is None:
|
||||
return {"released": False, "reason": "node_not_found", "node_id": task.node_id}
|
||||
metadata = _json_loads(node["metadata_json"])
|
||||
if str(metadata.get("last_task_id") or "").strip() != task.task_id:
|
||||
return {
|
||||
"released": False,
|
||||
"reason": "node_owned_by_other_task",
|
||||
"node_id": task.node_id,
|
||||
"active_task_id": metadata.get("last_task_id"),
|
||||
}
|
||||
|
||||
metadata.pop("last_task_id", None)
|
||||
metadata.pop("last_generate_request", None)
|
||||
metadata.pop("generation_failure", None)
|
||||
preparation = metadata.get("retry_preparation")
|
||||
if isinstance(preparation, dict) and str(preparation.get("task_id") or "").strip() == task.task_id:
|
||||
metadata.pop("retry_preparation", None)
|
||||
previous_status = str(node["status"] or "")
|
||||
next_status = "draft" if previous_status in {"failed", "cancelled"} else previous_status
|
||||
conn.execute(
|
||||
f"""
|
||||
UPDATE workflow_nodes
|
||||
SET status = {placeholder}, metadata_json = {placeholder}, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE node_id = {placeholder}
|
||||
""",
|
||||
(next_status, _json_or_none(metadata), task.node_id),
|
||||
)
|
||||
return {
|
||||
"released": True,
|
||||
"node_id": task.node_id,
|
||||
"previous_status": previous_status,
|
||||
"status": next_status,
|
||||
}
|
||||
|
||||
|
||||
def _recover_failed_character_workflow(
|
||||
conn,
|
||||
placeholder: str,
|
||||
task: GenerationTask,
|
||||
*,
|
||||
node_cleanup: dict[str, Any],
|
||||
lock_row: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
if task.status != "failed" or not task.workflow_id or node_cleanup.get("released") is not True:
|
||||
return {"recovered": False, "reason": "task_did_not_release_failed_node"}
|
||||
lock_clause = " FOR UPDATE" if lock_row else ""
|
||||
workflow = conn.execute(
|
||||
(
|
||||
"SELECT workflow_type, status, metadata_json "
|
||||
f"FROM profile_workflows WHERE workflow_id = {placeholder}{lock_clause}"
|
||||
),
|
||||
(task.workflow_id,),
|
||||
).fetchone()
|
||||
if workflow is None:
|
||||
return {"recovered": False, "reason": "workflow_not_found", "workflow_id": task.workflow_id}
|
||||
workflow_type = str(workflow["workflow_type"] or "")
|
||||
status = str(workflow["status"] or "")
|
||||
if workflow_type not in {"character_fast", "character_gradual"}:
|
||||
return {
|
||||
"recovered": False,
|
||||
"reason": "workflow_type_not_character_generation",
|
||||
"workflow_id": task.workflow_id,
|
||||
"workflow_type": workflow_type,
|
||||
}
|
||||
if status != "failed":
|
||||
return {
|
||||
"recovered": False,
|
||||
"reason": "workflow_status_not_failed",
|
||||
"workflow_id": task.workflow_id,
|
||||
"status": status,
|
||||
}
|
||||
metadata = _json_loads(workflow["metadata_json"])
|
||||
metadata.pop("quick_character_failure", None)
|
||||
preparation = metadata.get("retry_preparation")
|
||||
if isinstance(preparation, dict) and str(preparation.get("task_id") or "").strip() == task.task_id:
|
||||
metadata.pop("retry_preparation", None)
|
||||
conn.execute(
|
||||
f"""
|
||||
UPDATE profile_workflows
|
||||
SET status = {placeholder}, metadata_json = {placeholder}, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE workflow_id = {placeholder}
|
||||
AND status = {placeholder}
|
||||
""",
|
||||
("running", _json_or_none(metadata), task.workflow_id, "failed"),
|
||||
)
|
||||
return {
|
||||
"recovered": True,
|
||||
"workflow_id": task.workflow_id,
|
||||
"workflow_type": workflow_type,
|
||||
"previous_status": "failed",
|
||||
"status": "running",
|
||||
}
|
||||
|
||||
|
||||
def _json_or_none(value: Any | None) -> str | None:
|
||||
if value is None:
|
||||
return None
|
||||
|
|
|
|||
|
|
@ -271,6 +271,50 @@ class ModelProviderRepository:
|
|||
conn.close()
|
||||
return _row_to_resolved_route(row) if row else None
|
||||
|
||||
def resolve_provider_model(
|
||||
self,
|
||||
provider_id: str,
|
||||
model_id: str,
|
||||
*,
|
||||
modality: str | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Resolve an enabled route from the provider/model identity used by discovery UIs."""
|
||||
|
||||
params: list[Any] = [provider_id, model_id]
|
||||
modality_clause = ""
|
||||
if modality:
|
||||
modality_clause = f"AND route.modality = {self.placeholder}"
|
||||
params.append(modality)
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
row = conn.execute(
|
||||
f"""
|
||||
SELECT
|
||||
route.*,
|
||||
provider.display_name AS provider_display_name,
|
||||
provider.provider_type,
|
||||
provider.base_url,
|
||||
provider.image_endpoint,
|
||||
provider.api_key_env_var,
|
||||
provider.api_key_secret_ref,
|
||||
provider.timeout_seconds,
|
||||
provider.capabilities_json AS provider_capabilities_json,
|
||||
provider.extra_headers_json AS provider_extra_headers_json
|
||||
FROM model_routes route
|
||||
JOIN model_providers provider ON provider.provider_id = route.provider_id
|
||||
WHERE route.provider_id = {self.placeholder}
|
||||
AND route.model_id = {self.placeholder}
|
||||
AND route.enabled = 1
|
||||
AND provider.enabled = 1
|
||||
{modality_clause}
|
||||
ORDER BY route.priority ASC, route.route_id ASC
|
||||
""",
|
||||
tuple(params),
|
||||
).fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
return _row_to_resolved_route(row) if row else None
|
||||
|
||||
|
||||
def _row_to_provider(row) -> dict[str, Any]:
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,463 @@
|
|||
"""Atomic persistence for sealed character profile cards."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any
|
||||
|
||||
from backend.dto.common import ApiError, ErrorCode
|
||||
from backend.dto.profile_card_dto import CharacterProfileCard, CharacterProfileCardAsset
|
||||
from database.db_helper import Database
|
||||
from engines.character_flow import canonical_gradual_node_key
|
||||
|
||||
|
||||
CARD_COLUMNS = """
|
||||
card_id, profile_id, workflow_id, branch_name, version, status,
|
||||
persona_document, idempotency_key, metadata_json, created_at, updated_at
|
||||
"""
|
||||
|
||||
ASSET_COLUMNS = """
|
||||
asset_id, card_id, asset_role, image_id, image_uri, metadata_json, created_at
|
||||
"""
|
||||
|
||||
PROFILE_CARD_ASSET_ROLES = ("avatar", "persona_image", "standing", "pv")
|
||||
PROFILE_CARD_ROLE_STAGES: dict[str, frozenset[str]] = {
|
||||
"avatar": frozenset({"headshot"}),
|
||||
"persona_image": frozenset({"turnaround", "design_sheet"}),
|
||||
"standing": frozenset({"full_body"}),
|
||||
"pv": frozenset({"style_lock"}),
|
||||
}
|
||||
|
||||
|
||||
class ProfileCardRepository:
|
||||
def __init__(self, database: Database):
|
||||
self.database = database
|
||||
|
||||
@property
|
||||
def placeholder(self) -> str:
|
||||
return self.database._placeholder
|
||||
|
||||
def get(self, card_id: str) -> CharacterProfileCard | None:
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
row = conn.execute(
|
||||
f"SELECT {CARD_COLUMNS} FROM character_profile_cards WHERE card_id = {self.placeholder}",
|
||||
(card_id,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
assets = conn.execute(
|
||||
f"SELECT {ASSET_COLUMNS} FROM character_profile_card_assets "
|
||||
f"WHERE card_id = {self.placeholder} ORDER BY created_at, asset_role",
|
||||
(card_id,),
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
return _row_to_card(row, assets)
|
||||
|
||||
def get_by_idempotency_key(self, idempotency_key: str) -> CharacterProfileCard | None:
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
row = conn.execute(
|
||||
f"SELECT card_id FROM character_profile_cards WHERE idempotency_key = {self.placeholder}",
|
||||
(idempotency_key,),
|
||||
).fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
return self.get(str(row["card_id"])) if row else None
|
||||
|
||||
def list_by_profile(self, profile_id: str) -> list[CharacterProfileCard]:
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
rows = conn.execute(
|
||||
f"SELECT card_id FROM character_profile_cards WHERE profile_id = {self.placeholder} "
|
||||
"ORDER BY version DESC, created_at DESC",
|
||||
(profile_id,),
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
return [card for row in rows if (card := self.get(str(row["card_id"]))) is not None]
|
||||
|
||||
def finalize(self, payload: dict[str, Any]) -> CharacterProfileCard:
|
||||
placeholder = self.placeholder
|
||||
conn = self.database.connect()
|
||||
result_card_id = str(payload["card_id"])
|
||||
try:
|
||||
if self.database.backend == "sqlite":
|
||||
conn.execute("BEGIN IMMEDIATE")
|
||||
else:
|
||||
# Serialize per-profile version allocation and the workflow
|
||||
# archive transition on PostgreSQL as one finalization unit.
|
||||
conn.execute(
|
||||
f"SELECT profile_id FROM character_profiles WHERE profile_id = {placeholder} FOR UPDATE",
|
||||
(payload["profile_id"],),
|
||||
).fetchone()
|
||||
if payload.get("idempotency_key"):
|
||||
existing = conn.execute(
|
||||
f"SELECT card_id, workflow_id FROM character_profile_cards WHERE idempotency_key = {placeholder}",
|
||||
(payload["idempotency_key"],),
|
||||
).fetchone()
|
||||
if existing:
|
||||
if str(existing["workflow_id"]) != str(payload["workflow_id"]):
|
||||
raise ApiError(
|
||||
ErrorCode.VALIDATION_FAILED,
|
||||
"profile-card idempotency key belongs to another workflow",
|
||||
{
|
||||
"idempotency_key": payload["idempotency_key"],
|
||||
"workflow_id": payload["workflow_id"],
|
||||
"existing_workflow_id": str(existing["workflow_id"]),
|
||||
},
|
||||
)
|
||||
result_card_id = str(existing["card_id"])
|
||||
else:
|
||||
self._finalize_new(conn, payload)
|
||||
else:
|
||||
self._finalize_new(conn, payload)
|
||||
conn.commit()
|
||||
except Exception:
|
||||
rollback = getattr(conn, "rollback", None)
|
||||
if callable(rollback):
|
||||
rollback()
|
||||
raise
|
||||
finally:
|
||||
try:
|
||||
conn.close()
|
||||
except Exception:
|
||||
pass
|
||||
card = self.get(result_card_id)
|
||||
if card is None:
|
||||
raise RuntimeError("profile card was finalized but could not be read")
|
||||
return card
|
||||
|
||||
def _finalize_new(self, conn, payload: dict[str, Any]) -> None:
|
||||
placeholder = self.placeholder
|
||||
validated = _validate_finalization_state(conn, placeholder, payload)
|
||||
version_row = conn.execute(
|
||||
f"SELECT MAX(version) AS latest FROM character_profile_cards WHERE profile_id = {placeholder}",
|
||||
(payload["profile_id"],),
|
||||
).fetchone()
|
||||
version = int(version_row["latest"] or 0) + 1 if version_row else 1
|
||||
assets = validated["assets"]
|
||||
workflow_metadata = validated["workflow_metadata"]
|
||||
profile_metadata = validated["profile_metadata"]
|
||||
|
||||
conn.execute(
|
||||
f"""
|
||||
INSERT INTO character_profile_cards (
|
||||
card_id, profile_id, workflow_id, branch_name, version,
|
||||
status, persona_document, idempotency_key, metadata_json
|
||||
) VALUES (
|
||||
{placeholder}, {placeholder}, {placeholder}, {placeholder}, {placeholder},
|
||||
{placeholder}, {placeholder}, {placeholder}, {placeholder}
|
||||
)
|
||||
""",
|
||||
(
|
||||
payload["card_id"],
|
||||
payload["profile_id"],
|
||||
payload["workflow_id"],
|
||||
payload["branch_name"],
|
||||
version,
|
||||
payload.get("status", "sealed"),
|
||||
payload["persona_document"],
|
||||
payload.get("idempotency_key"),
|
||||
_json(payload.get("metadata")),
|
||||
),
|
||||
)
|
||||
for asset in assets:
|
||||
conn.execute(
|
||||
f"""
|
||||
INSERT INTO character_profile_card_assets (
|
||||
asset_id, card_id, asset_role, image_id, image_uri, metadata_json
|
||||
) VALUES (
|
||||
{placeholder}, {placeholder}, {placeholder},
|
||||
{placeholder}, {placeholder}, {placeholder}
|
||||
)
|
||||
""",
|
||||
(
|
||||
asset["asset_id"],
|
||||
payload["card_id"],
|
||||
asset["asset_role"],
|
||||
asset["image_id"],
|
||||
asset["image_uri"],
|
||||
_json(asset.get("metadata")),
|
||||
),
|
||||
)
|
||||
|
||||
conn.execute(
|
||||
f"""
|
||||
UPDATE character_profiles
|
||||
SET avatar_image_uri = {placeholder}, metadata_json = {placeholder}, updated_at = CURRENT_TIMESTAMP
|
||||
WHERE profile_id = {placeholder}
|
||||
""",
|
||||
(
|
||||
validated["avatar_image_uri"],
|
||||
_json(
|
||||
{
|
||||
**profile_metadata,
|
||||
"profile_card_id": payload["card_id"],
|
||||
"profile_card_version": version,
|
||||
"profile_category": "profile_card",
|
||||
}
|
||||
),
|
||||
payload["profile_id"],
|
||||
),
|
||||
)
|
||||
conn.execute(
|
||||
f"""
|
||||
UPDATE profile_workflows
|
||||
SET status = 'archived', current_node_id = {placeholder}, metadata_json = {placeholder},
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE workflow_id = {placeholder} AND current_branch = {placeholder}
|
||||
""",
|
||||
(
|
||||
payload["profile_card_node_id"],
|
||||
_json(
|
||||
{
|
||||
**workflow_metadata,
|
||||
"profile_card_id": payload["card_id"],
|
||||
"profile_card_version": version,
|
||||
"profile_card_finalized": True,
|
||||
"profile_card_asset_ids": [asset["asset_id"] for asset in assets],
|
||||
}
|
||||
),
|
||||
payload["workflow_id"],
|
||||
payload["branch_name"],
|
||||
),
|
||||
)
|
||||
conn.execute(
|
||||
f"""
|
||||
UPDATE workflow_nodes
|
||||
SET status = 'completed', updated_at = CURRENT_TIMESTAMP
|
||||
WHERE node_id = {placeholder} AND workflow_id = {placeholder} AND branch_name = {placeholder}
|
||||
""",
|
||||
(payload["profile_card_node_id"], payload["workflow_id"], payload["branch_name"]),
|
||||
)
|
||||
conn.execute(
|
||||
f"""
|
||||
INSERT INTO workflow_history (
|
||||
history_id, profile_id, workflow_id, node_id, image_id,
|
||||
branch_name, action, title, prompt, metadata_json
|
||||
) VALUES (
|
||||
{placeholder}, {placeholder}, {placeholder}, {placeholder}, {placeholder},
|
||||
{placeholder}, {placeholder}, {placeholder}, {placeholder}, {placeholder}
|
||||
)
|
||||
""",
|
||||
(
|
||||
payload["history_id"],
|
||||
payload["profile_id"],
|
||||
payload["workflow_id"],
|
||||
payload["profile_card_node_id"],
|
||||
validated["avatar_image_id"],
|
||||
payload["branch_name"],
|
||||
"profile_card_finalized",
|
||||
f"{payload['title_prefix']} v{version}",
|
||||
payload["persona_document"],
|
||||
_json(
|
||||
{
|
||||
"source": "profile_card_finalize",
|
||||
"card_id": payload["card_id"],
|
||||
"card_version": version,
|
||||
"asset_roles": [asset["asset_role"] for asset in assets],
|
||||
}
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
def next_version(self, profile_id: str) -> int:
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
row = conn.execute(
|
||||
f"SELECT MAX(version) AS latest FROM character_profile_cards WHERE profile_id = {self.placeholder}",
|
||||
(profile_id,),
|
||||
).fetchone()
|
||||
finally:
|
||||
conn.close()
|
||||
return int(row["latest"] or 0) + 1 if row else 1
|
||||
|
||||
|
||||
def _validate_finalization_state(conn, placeholder: str, payload: dict[str, Any]) -> dict[str, Any]:
|
||||
workflow = conn.execute(
|
||||
f"""
|
||||
SELECT profile_id, workflow_type, status, current_branch, metadata_json
|
||||
FROM profile_workflows
|
||||
WHERE workflow_id = {placeholder}
|
||||
""",
|
||||
(payload["workflow_id"],),
|
||||
).fetchone()
|
||||
if workflow is None:
|
||||
raise _validation("workflow not found", workflow_id=payload["workflow_id"])
|
||||
if str(workflow["profile_id"]) != str(payload["profile_id"]):
|
||||
raise _validation("profile-card workflow/profile ownership changed", workflow_id=payload["workflow_id"])
|
||||
if str(workflow["workflow_type"]) != "character_gradual":
|
||||
raise _validation("only a detailed character workflow can create a profile card")
|
||||
if str(workflow["status"]) in {"cancelled", "archived"}:
|
||||
raise _validation("workflow cannot be finalized from its current status", status=str(workflow["status"]))
|
||||
active_branch = str(workflow["current_branch"] or "main")
|
||||
if active_branch != str(payload["branch_name"]):
|
||||
raise _validation(
|
||||
"workflow branch changed before profile-card finalization",
|
||||
expected_branch=payload["branch_name"],
|
||||
active_branch=active_branch,
|
||||
)
|
||||
|
||||
card_node = conn.execute(
|
||||
f"""
|
||||
SELECT node_key, sort_order, status
|
||||
FROM workflow_nodes
|
||||
WHERE node_id = {placeholder} AND workflow_id = {placeholder} AND branch_name = {placeholder}
|
||||
""",
|
||||
(payload["profile_card_node_id"], payload["workflow_id"], active_branch),
|
||||
).fetchone()
|
||||
if card_node is None or canonical_gradual_node_key(str(card_node["node_key"])) != "profile_card":
|
||||
raise _validation(
|
||||
"workflow does not contain a valid profile-card node in the active branch",
|
||||
node_id=payload["profile_card_node_id"],
|
||||
active_branch=active_branch,
|
||||
)
|
||||
incomplete = conn.execute(
|
||||
f"""
|
||||
SELECT node_key
|
||||
FROM workflow_nodes
|
||||
WHERE workflow_id = {placeholder} AND branch_name = {placeholder}
|
||||
AND sort_order < {placeholder}
|
||||
AND status NOT IN ('selected', 'skipped', 'completed')
|
||||
ORDER BY sort_order, node_id
|
||||
""",
|
||||
(payload["workflow_id"], active_branch, int(card_node["sort_order"] or 0)),
|
||||
).fetchall()
|
||||
if incomplete:
|
||||
raise _validation(
|
||||
"workflow stages must be completed before creating a profile card",
|
||||
incomplete_node_keys=[str(row["node_key"]) for row in incomplete],
|
||||
)
|
||||
|
||||
profile = conn.execute(
|
||||
f"SELECT metadata_json FROM character_profiles WHERE profile_id = {placeholder}",
|
||||
(payload["profile_id"],),
|
||||
).fetchone()
|
||||
if profile is None:
|
||||
raise _validation("profile not found", profile_id=payload["profile_id"])
|
||||
|
||||
assets = payload.get("assets") if isinstance(payload.get("assets"), list) else []
|
||||
by_role = {str(asset.get("asset_role")): dict(asset) for asset in assets if isinstance(asset, dict)}
|
||||
missing_roles = [role for role in PROFILE_CARD_ASSET_ROLES if role not in by_role]
|
||||
if missing_roles:
|
||||
raise _validation(
|
||||
"profile-card assets are incomplete",
|
||||
required_roles=list(PROFILE_CARD_ASSET_ROLES),
|
||||
missing_roles=missing_roles,
|
||||
)
|
||||
image_ids = [str(by_role[role].get("image_id") or "") for role in PROFILE_CARD_ASSET_ROLES]
|
||||
if len(set(image_ids)) != len(image_ids):
|
||||
raise _validation("each profile-card asset role must use a distinct confirmed image")
|
||||
|
||||
validated_assets: list[dict[str, Any]] = []
|
||||
avatar_image_id = ""
|
||||
avatar_image_uri = ""
|
||||
for role in PROFILE_CARD_ASSET_ROLES:
|
||||
asset = by_role[role]
|
||||
image_id = str(asset.get("image_id") or "")
|
||||
row = conn.execute(
|
||||
f"""
|
||||
SELECT image.profile_id, image.workflow_id, image.node_id, image.image_uri,
|
||||
image.is_selected, image.is_archived, image.metadata_json,
|
||||
node.node_key, node.branch_name, node.selected_image_id
|
||||
FROM generation_images AS image
|
||||
JOIN workflow_nodes AS node ON node.node_id = image.node_id
|
||||
WHERE image.image_id = {placeholder}
|
||||
""",
|
||||
(image_id,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
raise _validation("profile-card asset image not found", asset_role=role, image_id=image_id)
|
||||
if str(row["profile_id"] or "") != str(payload["profile_id"]):
|
||||
raise _validation("profile-card asset belongs to another profile", asset_role=role, image_id=image_id)
|
||||
if str(row["workflow_id"] or "") != str(payload["workflow_id"]):
|
||||
raise _validation("profile-card asset belongs to another workflow", asset_role=role, image_id=image_id)
|
||||
if str(row["branch_name"] or "main") != active_branch:
|
||||
raise _validation(
|
||||
"profile-card asset belongs to another branch",
|
||||
asset_role=role,
|
||||
image_id=image_id,
|
||||
active_branch=active_branch,
|
||||
)
|
||||
metadata = _json_load(row["metadata_json"])
|
||||
if bool(row["is_archived"]) or metadata.get("quarantined") or metadata.get("gallery_visibility") == "restricted":
|
||||
raise _validation("profile-card asset is archived or restricted", asset_role=role, image_id=image_id)
|
||||
if not bool(row["is_selected"]) and str(row["selected_image_id"] or "") != image_id:
|
||||
raise _validation("profile-card asset is not a confirmed candidate", asset_role=role, image_id=image_id)
|
||||
source_stage = _profile_card_stage_key(str(row["node_key"] or ""))
|
||||
if source_stage not in PROFILE_CARD_ROLE_STAGES[role]:
|
||||
raise _validation(
|
||||
"profile-card asset comes from an incompatible workflow stage",
|
||||
asset_role=role,
|
||||
image_id=image_id,
|
||||
source_stage=source_stage,
|
||||
allowed_stages=sorted(PROFILE_CARD_ROLE_STAGES[role]),
|
||||
)
|
||||
asset["image_uri"] = str(row["image_uri"])
|
||||
asset_metadata = asset.get("metadata") if isinstance(asset.get("metadata"), dict) else {}
|
||||
asset["metadata"] = {
|
||||
**asset_metadata,
|
||||
"source_node_id": str(row["node_id"]),
|
||||
"source_node_key": str(row["node_key"]),
|
||||
"source_stage": source_stage,
|
||||
"source_branch": active_branch,
|
||||
}
|
||||
validated_assets.append(asset)
|
||||
if role == "avatar":
|
||||
avatar_image_id = image_id
|
||||
avatar_image_uri = str(row["image_uri"])
|
||||
|
||||
return {
|
||||
"assets": validated_assets,
|
||||
"avatar_image_id": avatar_image_id,
|
||||
"avatar_image_uri": avatar_image_uri,
|
||||
"profile_metadata": _json_load(profile["metadata_json"]),
|
||||
"workflow_metadata": _json_load(workflow["metadata_json"]),
|
||||
}
|
||||
|
||||
|
||||
def _profile_card_stage_key(node_key: str | None) -> str:
|
||||
canonical = canonical_gradual_node_key(node_key)
|
||||
return "style_lock" if canonical in {"style_lock", "hires_fix"} else canonical
|
||||
|
||||
|
||||
def _validation(message: str, **details: Any) -> ApiError:
|
||||
return ApiError(ErrorCode.VALIDATION_FAILED, message, details)
|
||||
|
||||
|
||||
def _row_to_card(row, asset_rows) -> CharacterProfileCard:
|
||||
return CharacterProfileCard(
|
||||
card_id=str(row["card_id"]),
|
||||
profile_id=str(row["profile_id"]),
|
||||
workflow_id=str(row["workflow_id"]),
|
||||
branch_name=str(row["branch_name"] or "main"),
|
||||
version=int(row["version"] or 1),
|
||||
status=str(row["status"]),
|
||||
persona_document=str(row["persona_document"]),
|
||||
idempotency_key=row["idempotency_key"],
|
||||
metadata=_json_load(row["metadata_json"]),
|
||||
created_at=str(row["created_at"]),
|
||||
updated_at=str(row["updated_at"]),
|
||||
assets=tuple(
|
||||
CharacterProfileCardAsset(
|
||||
asset_id=str(asset["asset_id"]),
|
||||
card_id=str(asset["card_id"]),
|
||||
asset_role=str(asset["asset_role"]),
|
||||
image_id=str(asset["image_id"]),
|
||||
image_uri=str(asset["image_uri"]),
|
||||
metadata=_json_load(asset["metadata_json"]),
|
||||
created_at=str(asset["created_at"]),
|
||||
)
|
||||
for asset in asset_rows
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _json(value: Any) -> str:
|
||||
return json.dumps(value or {}, ensure_ascii=False, sort_keys=True)
|
||||
|
||||
|
||||
def _json_load(value: str | None) -> dict[str, Any]:
|
||||
return json.loads(value) if value else {}
|
||||
|
|
@ -33,22 +33,89 @@ PROFILE_GENERATION_KIND_COLUMN = """
|
|||
CASE
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM profile_workflows AS detailed_workflow
|
||||
WHERE detailed_workflow.profile_id = character_profiles.profile_id
|
||||
AND detailed_workflow.workflow_type = 'character_gradual'
|
||||
AND LOWER(COALESCE(detailed_workflow.status, '')) <> 'cancelled'
|
||||
FROM character_profile_cards AS profile_card
|
||||
WHERE profile_card.profile_id = character_profiles.profile_id
|
||||
AND LOWER(COALESCE(profile_card.status, '')) = 'sealed'
|
||||
) THEN 'detailed'
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM profile_workflows AS legacy_card_workflow
|
||||
WHERE legacy_card_workflow.profile_id = character_profiles.profile_id
|
||||
AND legacy_card_workflow.workflow_type = 'character_gradual'
|
||||
AND LOWER(COALESCE(legacy_card_workflow.status, '')) IN ('completed', 'archived')
|
||||
AND COALESCE(legacy_card_workflow.metadata_json, '') LIKE '%"final_archive_history_id"%'
|
||||
) THEN 'detailed'
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM profile_workflows AS quick_workflow
|
||||
WHERE quick_workflow.profile_id = character_profiles.profile_id
|
||||
AND quick_workflow.workflow_type = 'character_fast'
|
||||
AND LOWER(COALESCE(quick_workflow.status, '')) <> 'cancelled'
|
||||
AND LOWER(COALESCE(quick_workflow.status, '')) IN ('active', 'running', 'review', 'completed')
|
||||
) THEN 'quick'
|
||||
ELSE 'ungenerated'
|
||||
END AS generation_kind
|
||||
"""
|
||||
|
||||
PROFILE_CATEGORY_COLUMN = """
|
||||
CASE
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM character_profile_cards AS profile_card
|
||||
WHERE profile_card.profile_id = character_profiles.profile_id
|
||||
AND LOWER(COALESCE(profile_card.status, '')) = 'sealed'
|
||||
) THEN 'profile_card'
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM profile_workflows AS legacy_card_workflow
|
||||
WHERE legacy_card_workflow.profile_id = character_profiles.profile_id
|
||||
AND legacy_card_workflow.workflow_type = 'character_gradual'
|
||||
AND LOWER(COALESCE(legacy_card_workflow.status, '')) IN ('completed', 'archived')
|
||||
AND COALESCE(legacy_card_workflow.metadata_json, '') LIKE '%"final_archive_history_id"%'
|
||||
) THEN 'profile_card'
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM profile_workflows AS quick_workflow
|
||||
WHERE quick_workflow.profile_id = character_profiles.profile_id
|
||||
AND quick_workflow.workflow_type = 'character_fast'
|
||||
AND LOWER(COALESCE(quick_workflow.status, '')) IN ('active', 'running', 'review', 'completed', 'archived')
|
||||
) THEN 'quick'
|
||||
ELSE 'ungenerated'
|
||||
END AS profile_category
|
||||
"""
|
||||
|
||||
DETAILED_WORKFLOW_STATE_COLUMN = """
|
||||
CASE
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM character_profile_cards AS profile_card
|
||||
WHERE profile_card.profile_id = character_profiles.profile_id
|
||||
AND LOWER(COALESCE(profile_card.status, '')) = 'sealed'
|
||||
) OR EXISTS (
|
||||
SELECT 1
|
||||
FROM profile_workflows AS legacy_card_workflow
|
||||
WHERE legacy_card_workflow.profile_id = character_profiles.profile_id
|
||||
AND legacy_card_workflow.workflow_type = 'character_gradual'
|
||||
AND LOWER(COALESCE(legacy_card_workflow.status, '')) IN ('completed', 'archived')
|
||||
AND COALESCE(legacy_card_workflow.metadata_json, '') LIKE '%"final_archive_history_id"%'
|
||||
) THEN 'archived'
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM profile_workflows AS detailed_workflow
|
||||
WHERE detailed_workflow.profile_id = character_profiles.profile_id
|
||||
AND detailed_workflow.workflow_type = 'character_gradual'
|
||||
AND LOWER(COALESCE(detailed_workflow.status, '')) IN ('active', 'running', 'review')
|
||||
) THEN 'in_progress'
|
||||
WHEN EXISTS (
|
||||
SELECT 1
|
||||
FROM profile_workflows AS detailed_workflow
|
||||
WHERE detailed_workflow.profile_id = character_profiles.profile_id
|
||||
AND detailed_workflow.workflow_type = 'character_gradual'
|
||||
AND LOWER(COALESCE(detailed_workflow.status, '')) = 'completed'
|
||||
) THEN 'ready_to_archive'
|
||||
ELSE NULL
|
||||
END AS detailed_workflow_state
|
||||
"""
|
||||
|
||||
|
||||
class ProfileRepository:
|
||||
def __init__(self, database: Database):
|
||||
|
|
@ -118,7 +185,8 @@ class ProfileRepository:
|
|||
try:
|
||||
row = conn.execute(
|
||||
f"""
|
||||
SELECT {PROFILE_COLUMNS}, {PROFILE_GENERATION_KIND_COLUMN}
|
||||
SELECT {PROFILE_COLUMNS}, {PROFILE_GENERATION_KIND_COLUMN},
|
||||
{PROFILE_CATEGORY_COLUMN}, {DETAILED_WORKFLOW_STATE_COLUMN}
|
||||
FROM character_profiles
|
||||
WHERE profile_id = {placeholder}
|
||||
""",
|
||||
|
|
@ -153,7 +221,8 @@ class ProfileRepository:
|
|||
).fetchone()
|
||||
rows = conn.execute(
|
||||
f"""
|
||||
SELECT {PROFILE_COLUMNS}, {PROFILE_GENERATION_KIND_COLUMN}
|
||||
SELECT {PROFILE_COLUMNS}, {PROFILE_GENERATION_KIND_COLUMN},
|
||||
{PROFILE_CATEGORY_COLUMN}, {DETAILED_WORKFLOW_STATE_COLUMN}
|
||||
FROM character_profiles
|
||||
{where}
|
||||
ORDER BY updated_at DESC, created_at DESC
|
||||
|
|
@ -267,6 +336,12 @@ def _row_to_profile(row) -> CharacterProfile:
|
|||
created_at=str(row["created_at"]),
|
||||
updated_at=str(row["updated_at"]),
|
||||
generation_kind=str(row["generation_kind"] or "ungenerated"),
|
||||
profile_category=str(row["profile_category"] or "ungenerated"),
|
||||
detailed_workflow_state=(
|
||||
str(row["detailed_workflow_state"])
|
||||
if row["detailed_workflow_state"] is not None
|
||||
else None
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
文件差异内容过多而无法显示
加载差异
|
|
@ -458,6 +458,16 @@ class WorkflowRepository:
|
|||
return [_row_to_history(row) for row in rows]
|
||||
|
||||
def create_branch(self, payload: dict[str, Any]) -> WorkflowBranch:
|
||||
branch, _ = self.create_branch_with_nodes(payload, [])
|
||||
return branch
|
||||
|
||||
def create_branch_with_nodes(
|
||||
self,
|
||||
payload: dict[str, Any],
|
||||
node_payloads: list[dict[str, Any]],
|
||||
) -> tuple[WorkflowBranch, list[WorkflowNode]]:
|
||||
"""Create a branch and all physical node copies in one transaction."""
|
||||
|
||||
placeholder = self.placeholder
|
||||
conn = self.database.connect()
|
||||
try:
|
||||
|
|
@ -486,13 +496,25 @@ class WorkflowRepository:
|
|||
_json_or_none(payload.get("metadata")),
|
||||
),
|
||||
)
|
||||
for node_payload in node_payloads:
|
||||
_insert_node(conn, placeholder, node_payload)
|
||||
conn.commit()
|
||||
except Exception:
|
||||
conn.rollback()
|
||||
raise
|
||||
finally:
|
||||
conn.close()
|
||||
branch = self.get_branch(payload["workflow_id"], payload["branch_name"])
|
||||
if branch is None:
|
||||
raise RuntimeError("workflow branch was inserted but could not be read back")
|
||||
return branch
|
||||
copied_nodes = [
|
||||
node
|
||||
for node_payload in node_payloads
|
||||
if (node := self.get_node(node_payload["node_id"])) is not None
|
||||
]
|
||||
if len(copied_nodes) != len(node_payloads):
|
||||
raise RuntimeError("workflow branch nodes were inserted but could not be read back")
|
||||
return branch, copied_nodes
|
||||
|
||||
def get_branch(self, workflow_id: str, branch_name: str) -> WorkflowBranch | None:
|
||||
placeholder = self.placeholder
|
||||
|
|
|
|||
文件差异内容过多而无法显示
加载差异
|
|
@ -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. 全量自动化测试与静态构建通过,测试过程不调用真实付费模型。
|
||||
文件差异内容过多而无法显示
加载差异
某些文件未显示,因为此 diff 中更改的文件太多 显示更多
正在加载…
添加表格
在新工单中引用