@chad.dev/dsh-adclip 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +4 -0
  3. package/README.md +28 -2
  4. package/client.js +2157 -0
  5. package/cordis.patch.yml +10 -0
  6. package/engine/requirements.txt +9 -0
  7. package/engine/src/adclip/__init__.py +0 -0
  8. package/engine/src/adclip/api.py +676 -0
  9. package/engine/src/adclip/image_params.py +64 -0
  10. package/engine/src/adclip/intents.py +190 -0
  11. package/engine/src/adclip/llm.py +142 -0
  12. package/engine/src/adclip/media/__init__.py +0 -0
  13. package/engine/src/adclip/media/asr.py +79 -0
  14. package/engine/src/adclip/media/ffmpeg.py +294 -0
  15. package/engine/src/adclip/media/stitch.py +124 -0
  16. package/engine/src/adclip/media/subtitles.py +45 -0
  17. package/engine/src/adclip/media/vision.py +105 -0
  18. package/engine/src/adclip/models.py +127 -0
  19. package/engine/src/adclip/params.py +363 -0
  20. package/engine/src/adclip/personas.py +60 -0
  21. package/engine/src/adclip/pipeline/__init__.py +0 -0
  22. package/engine/src/adclip/pipeline/analyze.py +133 -0
  23. package/engine/src/adclip/pipeline/generate.py +201 -0
  24. package/engine/src/adclip/pipeline/image_runner.py +131 -0
  25. package/engine/src/adclip/pipeline/persona.py +56 -0
  26. package/engine/src/adclip/pipeline/post.py +33 -0
  27. package/engine/src/adclip/pipeline/qa.py +41 -0
  28. package/engine/src/adclip/pipeline/replace.py +191 -0
  29. package/engine/src/adclip/pipeline/runner.py +138 -0
  30. package/engine/src/adclip/pipeline/script.py +226 -0
  31. package/engine/src/adclip/pipeline/segment.py +87 -0
  32. package/engine/src/adclip/providers/__init__.py +0 -0
  33. package/engine/src/adclip/providers/ark.py +122 -0
  34. package/engine/src/adclip/providers/base.py +30 -0
  35. package/engine/src/adclip/providers/discovery.py +56 -0
  36. package/engine/src/adclip/providers/kling.py +101 -0
  37. package/engine/src/adclip/providers/minimax.py +155 -0
  38. package/engine/src/adclip/providers/minimax_image.py +86 -0
  39. package/engine/src/adclip/settings.py +109 -0
  40. package/engine/src/adclip/store.py +378 -0
  41. package/host/dsh.js +60 -0
  42. package/host/engine.js +265 -0
  43. package/host/proxy.js +142 -0
  44. package/host/supervisor.js +251 -0
  45. package/icon.svg +6 -0
  46. package/index.js +39 -0
  47. package/locale/en.json +12 -0
  48. package/locale/zh.json +12 -0
  49. package/package.json +47 -4
@@ -0,0 +1,127 @@
1
+ """Pydantic contracts for Ad+ Studio.
2
+
3
+ The service deliberately keeps one user-visible object: a Run. A Run owns its
4
+ storyboard, segments, versions and artifacts. There is no project/version/shot
5
+ or quote object in this service.
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from typing import Any, Literal, Optional
10
+
11
+ from pydantic import BaseModel, Field
12
+
13
+
14
+ class Asset(BaseModel):
15
+ id: str
16
+ name: str
17
+ kind: Literal["video", "image", "audio", "file"]
18
+ path: str
19
+ sha256: str
20
+ bytes: int
21
+ mime: str = "application/octet-stream"
22
+ width: Optional[int] = None
23
+ height: Optional[int] = None
24
+ duration_s: Optional[float] = None
25
+ source: str = "upload"
26
+ created_at: str
27
+ original_name: str = ""
28
+
29
+
30
+ class Persona(BaseModel):
31
+ id: str
32
+ name: str
33
+ type: Literal["ai", "real", "group"] = "ai"
34
+ gender: Literal["male", "female", "any"] = "any"
35
+ age: Literal["teenager", "youth", "middle_aged", "senior"] = "youth"
36
+ ethnicity: Literal[
37
+ "caucasian", "asian", "latino", "african", "middle_eastern", "any"
38
+ ] = "asian"
39
+ body_type: Literal["slim", "standard", "athletic", "chubby"] = "standard"
40
+ exact_age: Optional[int] = None
41
+ height_cm: Optional[int] = None
42
+ hair: str = ""
43
+ outfit: str = ""
44
+ temperament: str = ""
45
+ voice_tone: str = ""
46
+ reference_audio_asset_id: Optional[str] = None
47
+ reference_images: list[str] = Field(default_factory=list)
48
+ role: Literal["A", "B", "both", "group"] = "both"
49
+ locks: dict[str, bool] = Field(default_factory=dict)
50
+ consent: dict[str, Any] = Field(default_factory=lambda: {"confirmed": False, "note": ""})
51
+ created_at: str
52
+ used_in_runs: list[str] = Field(default_factory=list)
53
+
54
+
55
+ class RunStep(BaseModel):
56
+ name: str
57
+ label: str
58
+ status: Literal["pending", "running", "done", "failed", "skipped"] = "pending"
59
+ progress: float = 0.0
60
+ started_at: Optional[str] = None
61
+ finished_at: Optional[str] = None
62
+ error: Optional[str] = None
63
+
64
+
65
+ class StoryboardSegment(BaseModel):
66
+ id: str
67
+ index: int
68
+ start_s: float
69
+ end_s: float
70
+ function: str = "beat"
71
+ source: Literal["one_shot_slice", "generated"] = "one_shot_slice"
72
+ thumbnail: Optional[str] = None
73
+ dialogue: str = ""
74
+ action: str = ""
75
+ product: str = ""
76
+ status: Literal["ok", "warn", "fail"] = "ok"
77
+ asset_id: Optional[str] = None
78
+ take: int = 1
79
+ note: str = ""
80
+
81
+
82
+ class Run(BaseModel):
83
+ id: str
84
+ scenario: str
85
+ title: str = "未命名"
86
+ status: Literal["queued", "running", "succeeded", "failed", "canceled", "interrupted"] = "queued"
87
+ step: str = "queued"
88
+ progress: float = 0.0
89
+ generation_mode: Literal["fast", "fine"] = "fast"
90
+ inputs: dict[str, Any] = Field(default_factory=dict)
91
+ advanced: dict[str, Any] = Field(default_factory=dict)
92
+ persona_map: dict[str, str] = Field(default_factory=dict)
93
+ persona_snapshot: dict[str, Any] = Field(default_factory=dict)
94
+ steps: list[RunStep] = Field(default_factory=list)
95
+ storyboard: list[StoryboardSegment] = Field(default_factory=list)
96
+ result: Optional[dict[str, Any]] = None
97
+ error: Optional[str] = None
98
+ demo: bool = True
99
+ cancel_requested: bool = False
100
+ created_at: str = ""
101
+ finished_at: Optional[str] = None
102
+ parent_run_id: Optional[str] = None
103
+ version: int = 1
104
+ changes: list[dict[str, Any]] = Field(default_factory=list)
105
+ segment_edits: list[dict[str, Any]] = Field(default_factory=list)
106
+
107
+
108
+ class StitchClip(BaseModel):
109
+ asset_id: str
110
+ in_s: float = 0.0
111
+ out_s: Optional[float] = None
112
+
113
+
114
+ class StitchRequest(BaseModel):
115
+ clips: list[StitchClip]
116
+ output: dict[str, Any] = Field(default_factory=dict)
117
+ transition: str = "cut"
118
+ audio: str = "keep"
119
+ fit: str = "blur_pad"
120
+
121
+
122
+ class AssistantRequest(BaseModel):
123
+ message: str
124
+ context: dict[str, Any] = Field(default_factory=dict)
125
+ selected_segment_id: Optional[str] = None
126
+ assets: list[dict[str, Any]] = Field(default_factory=list)
127
+ personas: list[dict[str, Any]] = Field(default_factory=list)
@@ -0,0 +1,363 @@
1
+ """Model catalogue, scenario manifests and video parameter helpers.
2
+
3
+ No prices are exposed anywhere. The catalogue only answers: can this model be
4
+ used, which modes/durations/aspects does it support, and is native audio on.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import os
9
+ import time
10
+ from typing import Any
11
+
12
+ from . import settings
13
+
14
+
15
+ def _has(*names: str) -> bool:
16
+ return all(bool(os.environ.get(name)) for name in names)
17
+
18
+
19
+ # The first slice ships with a local demo provider. Real adapters are wired in
20
+ # the next step; their entries stay unavailable until the adapter exists.
21
+ MODELS: list[dict[str, Any]] = [
22
+ {
23
+ "id": "demo",
24
+ "provider": "demo",
25
+ "model": "local-demo",
26
+ "label": "Demo(本地占位)",
27
+ "native_audio": True,
28
+ "modes": ["text_to_video", "image_to_video", "reference_to_video"],
29
+ "durations": [5, 8, 10, 12, 15, 30],
30
+ "resolutions": ["480p", "720p", "1080p"],
31
+ "aspects": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"],
32
+ "max_duration_s": 30,
33
+ "requires": [],
34
+ "available": True,
35
+ "adapter_ready": True,
36
+ "reason": "本地占位,不调用模型",
37
+ "tags": ["本地", "无模型调用"],
38
+ },
39
+ {
40
+ "id": "ark.seedance-2.5",
41
+ "provider": "ark",
42
+ "model": "doubao-seedance-2-5-260628",
43
+ "label": "Seedance 2.5(火山方舟)",
44
+ "native_audio": True,
45
+ "modes": ["text_to_video", "image_to_video", "reference_to_video"],
46
+ "durations": [4, 5, 8, 10, 12, 15, 30],
47
+ "resolutions": ["480p", "720p"],
48
+ "aspects": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"],
49
+ "max_duration_s": 30,
50
+ "requires": ["ARK_API_KEY"],
51
+ "available": False,
52
+ "adapter_ready": True,
53
+ "reason": "适配器已实现;配置 ARK_API_KEY 后待真实账号实测",
54
+ "tags": ["中文对白首选", "原生音频"],
55
+ },
56
+ {
57
+ "id": "ark.seedance-2.0",
58
+ "provider": "ark",
59
+ "model": "doubao-seedance-2-0-260128",
60
+ "label": "Seedance 2.0(火山方舟)",
61
+ "native_audio": True,
62
+ "modes": ["text_to_video", "image_to_video", "reference_to_video"],
63
+ "durations": [4, 5, 8, 10, 12, 15],
64
+ "resolutions": ["480p", "720p", "1080p"],
65
+ "aspects": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"],
66
+ "max_duration_s": 15,
67
+ "requires": ["ARK_API_KEY"],
68
+ "available": False,
69
+ "adapter_ready": True,
70
+ "reason": "适配器已实现;配置 ARK_API_KEY 后待真实账号实测",
71
+ "tags": ["原生音频", "中文"],
72
+ },
73
+ {
74
+ "id": "ark.seedance-2.0-mini",
75
+ "provider": "ark",
76
+ "model": "doubao-seedance-2-0-mini-260615",
77
+ "label": "Seedance 2.0 Mini(火山方舟)",
78
+ "native_audio": True,
79
+ "modes": ["text_to_video", "image_to_video", "reference_to_video"],
80
+ "durations": [4, 5, 8, 10, 12, 15],
81
+ "resolutions": ["480p", "720p", "1080p"],
82
+ "aspects": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"],
83
+ "max_duration_s": 15,
84
+ "requires": ["ARK_API_KEY"],
85
+ "available": False,
86
+ "adapter_ready": True,
87
+ "reason": "真实账号已实测:720p 原生中文对白可用",
88
+ "tags": ["Mini", "原生音频", "已实测"],
89
+ },
90
+ {
91
+ "id": "ark.seedance-2.0-fast",
92
+ "provider": "ark",
93
+ "model": "doubao-seedance-2-0-fast-260128",
94
+ "label": "Seedance 2.0 Fast(火山方舟)",
95
+ "native_audio": True,
96
+ "modes": ["text_to_video", "image_to_video", "reference_to_video"],
97
+ "durations": [4, 5, 8, 10, 12, 15],
98
+ "resolutions": ["480p", "720p", "1080p"],
99
+ "aspects": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"],
100
+ "max_duration_s": 15,
101
+ "requires": ["ARK_API_KEY"],
102
+ "available": False,
103
+ "adapter_ready": True,
104
+ "reason": "真实账号已实测:720p 原生中文对白可用",
105
+ "tags": ["Fast", "原生音频", "已实测"],
106
+ },
107
+ {
108
+ "id": "kling.v3",
109
+ "provider": "kling",
110
+ "model": "kling-v3",
111
+ "label": "Kling v3(可灵)",
112
+ "native_audio": True,
113
+ "modes": ["text_to_video", "image_to_video", "reference_to_video"],
114
+ "durations": [3, 5, 8, 10, 12, 15],
115
+ "resolutions": ["720p", "1080p"],
116
+ "aspects": ["9:16", "16:9", "1:1"],
117
+ "max_duration_s": 15,
118
+ "requires": ["KLING_API_KEY"],
119
+ "available": False,
120
+ "adapter_ready": True,
121
+ "reason": "适配器已实现;配置 KLING_API_KEY 后待真实账号实测",
122
+ "tags": ["1080p 候选", "原生音频"],
123
+ },
124
+ {
125
+ "id": "minimax.hailuo-2.3-fast",
126
+ "provider": "minimax",
127
+ "model": "MiniMax-Hailuo-2.3-Fast",
128
+ "label": "MiniMax Hailuo 2.3 Fast(图生视频)",
129
+ "native_audio": False,
130
+ "modes": ["image_to_video"],
131
+ "durations": [6, 10],
132
+ "resolutions": ["768P", "1080P"],
133
+ "aspects": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"],
134
+ "max_duration_s": 10,
135
+ "requires": ["MINIMAX_API_KEY"],
136
+ "available": False,
137
+ "adapter_ready": True,
138
+ "reason": "图生视频已实测;无原生音频,只适合纯画面/后期配乐",
139
+ "tags": ["图生视频已验证", "无原生音频"],
140
+ },
141
+ {
142
+ "id": "minimax.h3",
143
+ "provider": "minimax",
144
+ "model": "MiniMax-H3",
145
+ "label": "MiniMax H3(2K · 原生对白)",
146
+ "native_audio": True,
147
+ "modes": ["text_to_video", "image_to_video"],
148
+ "durations": [4, 5, 6, 8, 10, 12, 15],
149
+ "resolutions": ["2K"],
150
+ "aspects": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"],
151
+ "max_duration_s": 15,
152
+ "requires": ["MINIMAX_API_KEY"],
153
+ "available": False,
154
+ "adapter_ready": True,
155
+ "reason": "2K 适配器已实测:中文原生对白可用",
156
+ "tags": ["2K", "中文原生对白已验证"],
157
+ },
158
+ {
159
+ "id": "volcengine.jimeng-3.0",
160
+ "provider": "volcengine",
161
+ "model": "jimeng_ti2v_v30_pro",
162
+ "label": "火山引擎即梦 3.0 Pro",
163
+ "native_audio": False,
164
+ "modes": ["text_to_video", "image_to_video"],
165
+ "durations": [5, 10],
166
+ "resolutions": ["720p", "1080p"],
167
+ "aspects": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"],
168
+ "max_duration_s": 10,
169
+ "requires": ["VOLC_ACCESSKEY", "VOLC_SECRETKEY"],
170
+ "available": False,
171
+ "reason": "无原生对白;只做无对白 B-roll",
172
+ "tags": ["无原生对白"],
173
+ },
174
+ {
175
+ "id": "veo.3.1",
176
+ "provider": "fal",
177
+ "model": "veo-3.1-generate-001",
178
+ "label": "Veo 3.1(可选)",
179
+ "native_audio": True,
180
+ "modes": ["text_to_video", "image_to_video"],
181
+ "durations": [8],
182
+ "resolutions": ["720p", "1080p"],
183
+ "aspects": ["9:16", "16:9"],
184
+ "max_duration_s": 8,
185
+ "requires": ["FAL_KEY"],
186
+ "available": False,
187
+ "reason": "可选海外通道,未接入",
188
+ "tags": ["海外", "原生音频"],
189
+ },
190
+ {
191
+ "id": "sora.2",
192
+ "provider": "fal",
193
+ "model": "sora-2",
194
+ "label": "Sora 2(可选)",
195
+ "native_audio": True,
196
+ "modes": ["text_to_video", "image_to_video"],
197
+ "durations": [10, 15],
198
+ "resolutions": ["720p"],
199
+ "aspects": ["9:16", "16:9"],
200
+ "max_duration_s": 15,
201
+ "requires": ["FAL_KEY", "OPENAI_API_KEY"],
202
+ "available": False,
203
+ "reason": "可选海外通道,未接入",
204
+ "tags": ["海外", "原生音频"],
205
+ },
206
+ ]
207
+
208
+
209
+ def catalog() -> list[dict[str, Any]]:
210
+ """Return the catalogue with live key presence and availability."""
211
+ rows: list[dict[str, Any]] = []
212
+ enabled = settings.enabled_models()
213
+ for row in MODELS:
214
+ item = dict(row)
215
+ # No preference file means all models are enabled by default. Once the
216
+ # operator saves checkboxes, only the listed ids appear in generation.
217
+ item["enabled"] = True if enabled is None else item["id"] in enabled
218
+ item["key_present"] = all(_has(name) for name in item.get("requires", []))
219
+ if item.get("adapter_ready") and item["id"] != "demo":
220
+ item["available"] = bool(item["key_present"])
221
+ if not item["key_present"]:
222
+ item["reason"] = "未配置 " + " / ".join(item.get("requires", []))
223
+ item["checked_at"] = time.strftime("%Y-%m-%dT%H:%M:%S%z")
224
+ rows.append(item)
225
+ return rows
226
+
227
+
228
+ def get_model(model_id: str) -> dict[str, Any] | None:
229
+ for row in catalog():
230
+ if row["id"] == model_id:
231
+ return row
232
+ return None
233
+
234
+
235
+ def available_models(mode: str | None = None) -> list[dict[str, Any]]:
236
+ rows = [row for row in catalog() if row.get("available")]
237
+ if mode:
238
+ rows = [row for row in rows if mode in (row.get("modes") or [])]
239
+ return rows
240
+
241
+
242
+ def default_model() -> str:
243
+ return "demo"
244
+
245
+
246
+ def supports(model_id: str, key: str, value: Any) -> bool:
247
+ model = get_model(model_id)
248
+ if model is None:
249
+ return False
250
+ if key == "duration":
251
+ return any(float(value) <= float(item) for item in model.get("durations", []))
252
+ if key == "resolution":
253
+ return value in (model.get("resolutions") or [])
254
+ if key == "aspect":
255
+ return value in (model.get("aspects") or [])
256
+ if key == "native_audio":
257
+ return bool(model.get("native_audio")) or not value
258
+ return True
259
+
260
+
261
+ ASPECT_SIZES = {
262
+ "480p": {
263
+ "9:16": (480, 854), "16:9": (854, 480), "1:1": (480, 480),
264
+ "4:3": (640, 480), "3:4": (480, 640), "21:9": (896, 384),
265
+ },
266
+ "720p": {
267
+ "9:16": (720, 1280), "16:9": (1280, 720), "1:1": (720, 720),
268
+ "4:3": (960, 720), "3:4": (720, 960), "21:9": (1280, 548),
269
+ },
270
+ "1080p": {
271
+ "9:16": (1080, 1920), "16:9": (1920, 1080), "1:1": (1080, 1080),
272
+ "4:3": (1440, 1080), "3:4": (1080, 1440), "21:9": (1920, 822),
273
+ },
274
+ "4k": {
275
+ "9:16": (2160, 3840), "16:9": (3840, 2160), "1:1": (2160, 2160),
276
+ "4:3": (2880, 2160), "3:4": (2160, 2880), "21:9": (3840, 1644),
277
+ },
278
+ }
279
+
280
+
281
+ def size_for(aspect: str, resolution: str) -> tuple[int, int]:
282
+ bucket = ASPECT_SIZES.get(resolution) or ASPECT_SIZES["720p"]
283
+ return bucket.get(aspect) or bucket["9:16"]
284
+
285
+
286
+ DEFAULT_ADVANCED = {
287
+ "generation_mode": "fast",
288
+ "duration": 12,
289
+ "aspect": "9:16",
290
+ "resolution": "720p",
291
+ "model": "auto",
292
+ "fidelity": "balanced",
293
+ "language": "zh",
294
+ "native_audio": True,
295
+ "subtitle": "sidecar",
296
+ "subtitle_size": 32,
297
+ "music": "off",
298
+ "export_bitrate": "8M",
299
+ "fps": 30,
300
+ "aigc_label": True,
301
+ }
302
+
303
+
304
+ SCENARIOS: list[dict[str, Any]] = [
305
+ {
306
+ "id": "viral-remake",
307
+ "label": "爆款复刻",
308
+ "status": "ready",
309
+ "description": "学习参考片的结构和节奏,换成你的商品重拍",
310
+ "inputs": [
311
+ {"key": "reference", "type": "video", "label": "参考视频", "required": True, "max_duration_s": 30},
312
+ {"key": "product_images", "type": "image[]", "label": "商品图", "required": False, "max_items": 9},
313
+ {"key": "brief", "type": "text", "label": "产品信息、卖点、价格及修改要求", "max_length": 2000},
314
+ ],
315
+ "advanced": [
316
+ {"key": "generation_mode", "label": "生成模式", "type": "enum", "options": ["fast", "fine"], "default": "fast"},
317
+ {"key": "duration", "label": "时长", "type": "enum", "options": [5, 8, 10, 12, 15, 30], "default": 12},
318
+ {"key": "aspect", "label": "比例", "type": "enum", "options": ["9:16", "16:9", "1:1", "4:3", "3:4", "21:9"], "default": "9:16"},
319
+ {"key": "resolution", "label": "清晰度", "type": "enum", "options": ["480p", "720p", "1080p"], "default": "720p"},
320
+ {"key": "model", "label": "模型", "type": "model", "default": "auto"},
321
+ ],
322
+ "examples": [],
323
+ },
324
+ {
325
+ "id": "text-to-video",
326
+ "label": "文生视频",
327
+ "status": "ready",
328
+ "description": "一句描述生成一段视频",
329
+ "inputs": [
330
+ {"key": "brief", "type": "text", "label": "描述产品、场景、动作、镜头和氛围", "required": True, "max_length": 1000},
331
+ {"key": "reference_image", "type": "image", "label": "参考图(可选)", "required": False},
332
+ ],
333
+ "advanced": [
334
+ {"key": "duration", "label": "时长", "type": "enum", "options": [5, 8, 10, 12, 15, 30], "default": 8},
335
+ {"key": "aspect", "label": "比例", "type": "enum", "options": ["9:16", "16:9", "1:1"], "default": "9:16"},
336
+ {"key": "resolution", "label": "清晰度", "type": "enum", "options": ["480p", "720p", "1080p"], "default": "720p"},
337
+ {"key": "model", "label": "模型", "type": "model", "default": "auto"},
338
+ ],
339
+ "examples": [],
340
+ },
341
+ {
342
+ "id": "video-fission",
343
+ "label": "视频裂变",
344
+ "status": "coming_soon",
345
+ "description": "一条模板出多组变体",
346
+ "inputs": [],
347
+ "advanced": [],
348
+ "examples": [],
349
+ },
350
+ ]
351
+
352
+
353
+ def scenario(scenario_id: str) -> dict[str, Any] | None:
354
+ for row in SCENARIOS:
355
+ if row["id"] == scenario_id:
356
+ return row
357
+ return None
358
+
359
+
360
+ def merged_advanced(values: dict[str, Any] | None) -> dict[str, Any]:
361
+ out = dict(DEFAULT_ADVANCED)
362
+ out.update(values or {})
363
+ return out
@@ -0,0 +1,60 @@
1
+ """Persona creation and Tencent-compatible defaults."""
2
+ from __future__ import annotations
3
+
4
+ from typing import Any
5
+
6
+ from . import store
7
+ from .models import Persona
8
+
9
+
10
+ def create(data: dict[str, Any]) -> dict[str, Any]:
11
+ payload = {
12
+ "id": data.get("id") or store.new_id("persona"),
13
+ "name": str(data.get("name") or "未命名角色"),
14
+ "type": data.get("type") or "ai",
15
+ "gender": data.get("gender") or "any",
16
+ "age": data.get("age") or "youth",
17
+ "ethnicity": data.get("ethnicity") or "asian",
18
+ "body_type": data.get("body_type") or "standard",
19
+ "exact_age": data.get("exact_age"),
20
+ "height_cm": data.get("height_cm"),
21
+ "hair": str(data.get("hair") or ""),
22
+ "outfit": str(data.get("outfit") or ""),
23
+ "temperament": str(data.get("temperament") or ""),
24
+ "voice_tone": str(data.get("voice_tone") or ""),
25
+ "reference_audio_asset_id": data.get("reference_audio_asset_id"),
26
+ "reference_images": list(data.get("reference_images") or []),
27
+ "role": data.get("role") or "both",
28
+ "locks": dict(data.get("locks") or {}),
29
+ "consent": dict(data.get("consent") or {"confirmed": False, "note": ""}),
30
+ "created_at": store.now(),
31
+ "used_in_runs": list(data.get("used_in_runs") or []),
32
+ }
33
+ persona = Persona.model_validate(payload)
34
+ if persona.type == "real" and not persona.consent.get("confirmed"):
35
+ raise ValueError("真人角色必须确认授权")
36
+ return store.save_persona(persona).model_dump()
37
+
38
+
39
+ def update(persona_id: str, data: dict[str, Any]) -> dict[str, Any] | None:
40
+ current = store.get_persona(persona_id)
41
+ if current is None:
42
+ return None
43
+ merged = {**current, **data, "id": persona_id, "created_at": current.get("created_at") or store.now()}
44
+ persona = Persona.model_validate(merged)
45
+ if persona.type == "real" and not persona.consent.get("confirmed"):
46
+ raise ValueError("真人角色必须确认授权")
47
+ return store.save_persona(persona).model_dump()
48
+
49
+
50
+ def delete(persona_id: str) -> bool:
51
+ return store.delete_persona(persona_id)
52
+
53
+
54
+ def ensure_default() -> None:
55
+ if not store.list_personas():
56
+ store.save_persona(Persona.model_validate(store.default_persona()))
57
+
58
+
59
+ def resolve(persona_id: str) -> dict[str, Any]:
60
+ return store.get_persona(persona_id) or store.default_persona()
File without changes