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

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 (51) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +28 -2
  4. package/client.js +2256 -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 +704 -0
  9. package/engine/src/adclip/catalog.py +341 -0
  10. package/engine/src/adclip/image_params.py +45 -0
  11. package/engine/src/adclip/intents.py +209 -0
  12. package/engine/src/adclip/llm.py +142 -0
  13. package/engine/src/adclip/media/__init__.py +0 -0
  14. package/engine/src/adclip/media/asr.py +79 -0
  15. package/engine/src/adclip/media/ffmpeg.py +294 -0
  16. package/engine/src/adclip/media/stitch.py +124 -0
  17. package/engine/src/adclip/media/subtitles.py +45 -0
  18. package/engine/src/adclip/media/vision.py +105 -0
  19. package/engine/src/adclip/models.py +127 -0
  20. package/engine/src/adclip/params.py +158 -0
  21. package/engine/src/adclip/personas.py +60 -0
  22. package/engine/src/adclip/pipeline/__init__.py +0 -0
  23. package/engine/src/adclip/pipeline/analyze.py +133 -0
  24. package/engine/src/adclip/pipeline/generate.py +196 -0
  25. package/engine/src/adclip/pipeline/image_runner.py +146 -0
  26. package/engine/src/adclip/pipeline/persona.py +56 -0
  27. package/engine/src/adclip/pipeline/post.py +40 -0
  28. package/engine/src/adclip/pipeline/qa.py +44 -0
  29. package/engine/src/adclip/pipeline/replace.py +191 -0
  30. package/engine/src/adclip/pipeline/runner.py +138 -0
  31. package/engine/src/adclip/pipeline/script.py +226 -0
  32. package/engine/src/adclip/pipeline/segment.py +87 -0
  33. package/engine/src/adclip/providers/__init__.py +0 -0
  34. package/engine/src/adclip/providers/ark.py +122 -0
  35. package/engine/src/adclip/providers/ark_image.py +114 -0
  36. package/engine/src/adclip/providers/base.py +30 -0
  37. package/engine/src/adclip/providers/discovery.py +56 -0
  38. package/engine/src/adclip/providers/kling.py +101 -0
  39. package/engine/src/adclip/providers/minimax.py +155 -0
  40. package/engine/src/adclip/providers/minimax_image.py +89 -0
  41. package/engine/src/adclip/settings.py +109 -0
  42. package/engine/src/adclip/store.py +378 -0
  43. package/host/dsh.js +60 -0
  44. package/host/engine.js +265 -0
  45. package/host/proxy.js +142 -0
  46. package/host/supervisor.js +251 -0
  47. package/icon.svg +6 -0
  48. package/index.js +39 -0
  49. package/locale/en.json +12 -0
  50. package/locale/zh.json +12 -0
  51. package/package.json +47 -4
@@ -0,0 +1,378 @@
1
+ """Filesystem store for AdClip.
2
+
3
+ `data/runs/<id>/run.json` is the single source of truth. Assets and personas
4
+ have sidecar JSON records. No database is used in the first slice.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import hashlib
9
+ import json
10
+ import os
11
+ import secrets
12
+ import shutil
13
+ import threading
14
+ import time
15
+ from pathlib import Path
16
+ from typing import Any
17
+
18
+ from .models import Asset, Persona, Run
19
+
20
+ # `<root>/src/adclip/store.py` -> `<root>`, in this checkout and in the
21
+ # published package (`<engine>/src/adclip` -> `<engine>`), so the default data
22
+ # directory and the legacy copy stay where they always were.
23
+ BASE = Path(__file__).resolve().parents[2]
24
+ DATA = Path(os.environ.get("ADSTUDIO_DATA_DIR", BASE / "data")).resolve()
25
+ LEGACY_DATA = (BASE / "data").resolve()
26
+ ASSETS = DATA / "assets"
27
+ RUNS = DATA / "runs"
28
+ PERSONAS = DATA / "personas"
29
+ CONTEXT = DATA / "context.json"
30
+ LOCK = threading.RLock()
31
+
32
+
33
+ def now() -> str:
34
+ return time.strftime("%Y-%m-%dT%H:%M:%S%z")
35
+
36
+
37
+ def migrate_legacy() -> dict[str, Any]:
38
+ """One-time copy from the old engineRoot/data into the stable data dir.
39
+
40
+ The legacy directory is never deleted; if the target already has content it
41
+ is left alone. This makes plugin/engine upgrades non-destructive.
42
+ """
43
+ if DATA == LEGACY_DATA or not LEGACY_DATA.is_dir():
44
+ return {"migrated": False, "reason": "not needed"}
45
+ try:
46
+ has_target = DATA.is_dir() and any(DATA.iterdir())
47
+ except OSError:
48
+ has_target = False
49
+ if has_target:
50
+ return {"migrated": False, "reason": "target already has data"}
51
+ DATA.mkdir(parents=True, exist_ok=True)
52
+ shutil.copytree(LEGACY_DATA, DATA, dirs_exist_ok=True)
53
+ atomic_write_json(DATA / ".migrated_from.json", {"source": str(LEGACY_DATA), "at": now()})
54
+ return {"migrated": True, "source": str(LEGACY_DATA), "target": str(DATA)}
55
+
56
+
57
+ def ensure_dirs() -> None:
58
+ for path in (DATA, ASSETS, RUNS, PERSONAS):
59
+ path.mkdir(parents=True, exist_ok=True)
60
+
61
+
62
+ def new_id(prefix: str) -> str:
63
+ return f"{prefix}_{int(time.time() * 1000):x}_{secrets.token_hex(3)}"
64
+
65
+
66
+ def atomic_write_json(path: Path, payload: Any) -> None:
67
+ path.parent.mkdir(parents=True, exist_ok=True)
68
+ tmp = path.with_suffix(path.suffix + ".tmp")
69
+ tmp.write_text(json.dumps(payload, ensure_ascii=False, indent=2), encoding="utf-8")
70
+ os.replace(tmp, path)
71
+
72
+
73
+ def read_json(path: Path, default: Any = None) -> Any:
74
+ try:
75
+ return json.loads(path.read_text(encoding="utf-8"))
76
+ except (OSError, json.JSONDecodeError):
77
+ return default
78
+
79
+
80
+ def rel_to_data(path: Path) -> str:
81
+ try:
82
+ return path.resolve().relative_to(DATA).as_posix()
83
+ except (ValueError, OSError):
84
+ return str(path)
85
+
86
+
87
+ def path_from_rel(rel: str) -> Path:
88
+ target = (DATA / rel).resolve()
89
+ if DATA not in target.parents and target != DATA:
90
+ raise ValueError("path outside AdClip data")
91
+ return target
92
+
93
+
94
+ def safe_media_path(rel: str) -> Path:
95
+ target = path_from_rel(rel)
96
+ if not target.is_file():
97
+ raise FileNotFoundError(rel)
98
+ if not any(target == root or root in target.parents for root in (ASSETS, RUNS)):
99
+ raise ValueError("path outside media roots")
100
+ return target
101
+
102
+
103
+ # ---------------------------------------------------------------------------
104
+ # assets
105
+ # ---------------------------------------------------------------------------
106
+ def create_asset(raw: bytes, name: str, kind: str, mime: str = "application/octet-stream") -> Asset:
107
+ ensure_dirs()
108
+ if not raw:
109
+ raise ValueError("empty file")
110
+ digest = hashlib.sha256(raw).hexdigest()
111
+ suffix = Path(name or "").suffix.lower()
112
+ if not suffix:
113
+ suffix = {"video": ".mp4", "image": ".png", "audio": ".mp3"}.get(kind, ".bin")
114
+ target = ASSETS / f"{digest[:24]}{suffix}"
115
+ if not target.is_file():
116
+ tmp = target.with_suffix(target.suffix + ".tmp")
117
+ tmp.write_bytes(raw)
118
+ os.replace(tmp, target)
119
+ record = Asset(
120
+ id=f"a_{digest[:16]}",
121
+ name=name or target.name,
122
+ original_name=name or target.name,
123
+ kind=kind if kind in {"video", "image", "audio", "file"} else "file",
124
+ path=rel_to_data(target),
125
+ sha256=digest,
126
+ bytes=len(raw),
127
+ mime=mime,
128
+ created_at=now(),
129
+ )
130
+ atomic_write_json(ASSETS / f"{record.id}.json", record.model_dump())
131
+ return record
132
+
133
+
134
+ def update_asset(asset: Asset) -> Asset:
135
+ atomic_write_json(ASSETS / f"{asset.id}.json", asset.model_dump())
136
+ return asset
137
+
138
+
139
+ def list_assets(kind: str = "", query: str = "", limit: int = 300) -> list[dict[str, Any]]:
140
+ ensure_dirs()
141
+ rows: list[dict[str, Any]] = []
142
+ for path in ASSETS.glob("*.json"):
143
+ data = read_json(path)
144
+ if not isinstance(data, dict):
145
+ continue
146
+ if kind and data.get("kind") != kind:
147
+ continue
148
+ if query:
149
+ haystack = f"{data.get('name', '')} {data.get('original_name', '')}".lower()
150
+ if query.lower() not in haystack:
151
+ continue
152
+ rows.append(data)
153
+ rows.sort(key=lambda item: item.get("created_at") or "", reverse=True)
154
+ return rows[: max(1, min(limit, 1000))]
155
+
156
+
157
+ def list_assets_page(kind: str = "", query: str = "", offset: int = 0, limit: int = 24, date_from: str = "", date_to: str = "") -> tuple[list[dict[str, Any]], int]:
158
+ ensure_dirs()
159
+ rows: list[dict[str, Any]] = []
160
+ seen: set[str] = set()
161
+ for path in ASSETS.glob("*.json"):
162
+ data = read_json(path)
163
+ if not isinstance(data, dict):
164
+ continue
165
+ if kind and data.get("kind") != kind:
166
+ continue
167
+ if query:
168
+ haystack = f"{data.get('name', '')} {data.get('original_name', '')}".lower()
169
+ if query.lower() not in haystack:
170
+ continue
171
+ created = str(data.get("created_at") or "")
172
+ if date_from and created[:10] < date_from:
173
+ continue
174
+ if date_to and created[:10] > date_to:
175
+ continue
176
+ sha = str(data.get("sha256") or "")
177
+ if sha and sha in seen:
178
+ continue
179
+ if sha:
180
+ seen.add(sha)
181
+ rows.append(data)
182
+ rows.sort(key=lambda item: item.get("created_at") or "", reverse=True)
183
+ total = len(rows)
184
+ offset = max(0, int(offset))
185
+ limit = max(1, min(int(limit), 200))
186
+ return rows[offset:offset + limit], total
187
+
188
+
189
+ def get_asset(asset_id: str) -> dict[str, Any] | None:
190
+ return read_json(ASSETS / f"{asset_id}.json")
191
+
192
+
193
+ def get_asset_by_sha(sha: str) -> dict[str, Any] | None:
194
+ ensure_dirs()
195
+ for path in ASSETS.glob("*.json"):
196
+ row = read_json(path)
197
+ if isinstance(row, dict) and row.get("sha256") == sha:
198
+ return row
199
+ return None
200
+
201
+
202
+ # ---------------------------------------------------------------------------
203
+ # personas
204
+ # ---------------------------------------------------------------------------
205
+ def save_persona(persona: Persona) -> Persona:
206
+ ensure_dirs()
207
+ atomic_write_json(PERSONAS / f"{persona.id}.json", persona.model_dump())
208
+ return persona
209
+
210
+
211
+ def get_persona(persona_id: str) -> dict[str, Any] | None:
212
+ return read_json(PERSONAS / f"{persona_id}.json")
213
+
214
+
215
+ def list_personas() -> list[dict[str, Any]]:
216
+ ensure_dirs()
217
+ rows = []
218
+ for path in PERSONAS.glob("*.json"):
219
+ data = read_json(path)
220
+ if isinstance(data, dict):
221
+ rows.append(data)
222
+ rows.sort(key=lambda item: item.get("created_at") or "", reverse=True)
223
+ return rows
224
+
225
+
226
+ def delete_persona(persona_id: str) -> bool:
227
+ path = PERSONAS / f"{persona_id}.json"
228
+ if not path.is_file():
229
+ return False
230
+ path.unlink()
231
+ return True
232
+
233
+
234
+ # ---------------------------------------------------------------------------
235
+ # runs
236
+ # ---------------------------------------------------------------------------
237
+ def run_dir(run_id: str) -> Path:
238
+ return RUNS / run_id
239
+
240
+
241
+ def run_path(run_id: str) -> Path:
242
+ return run_dir(run_id) / "run.json"
243
+
244
+
245
+ def write_run(run: Run) -> Run:
246
+ atomic_write_json(run_path(run.id), run.model_dump())
247
+ return run
248
+
249
+
250
+ def read_run(run_id: str) -> Run | None:
251
+ data = read_json(run_path(run_id))
252
+ if not isinstance(data, dict):
253
+ return None
254
+ try:
255
+ return Run.model_validate(data)
256
+ except Exception:
257
+ return None
258
+
259
+
260
+ def run_media_rel(run_id: str, filename: str) -> str:
261
+ return f"runs/{run_id}/artifacts/{filename}"
262
+
263
+
264
+ def run_artifact(run_id: str, filename: str) -> Path:
265
+ path = run_dir(run_id) / "artifacts" / filename
266
+ path.parent.mkdir(parents=True, exist_ok=True)
267
+ return path
268
+
269
+
270
+ def list_runs(limit: int = 30) -> list[dict[str, Any]]:
271
+ ensure_dirs()
272
+ rows: list[dict[str, Any]] = []
273
+ for path in RUNS.glob("*/run.json"):
274
+ data = read_json(path)
275
+ if isinstance(data, dict):
276
+ rows.append(data)
277
+ rows.sort(key=lambda item: item.get("created_at") or "", reverse=True)
278
+ return rows[: max(1, min(limit, 200))]
279
+
280
+
281
+ def list_runs_page(
282
+ *, offset: int = 0, limit: int = 20, query: str = "",
283
+ date_from: str = "", date_to: str = "", status: str = "",
284
+ scenario: str = "", sort: str = "created_desc",
285
+ ) -> tuple[list[dict[str, Any]], int]:
286
+ ensure_dirs()
287
+ rows: list[dict[str, Any]] = []
288
+ needle = query.lower().strip()
289
+ for path in RUNS.glob("*/run.json"):
290
+ data = read_json(path)
291
+ if not isinstance(data, dict):
292
+ continue
293
+ if needle and needle not in str(data.get("title") or "").lower() and needle not in str(data.get("id") or "").lower():
294
+ continue
295
+ created = str(data.get("created_at") or "")
296
+ if date_from and created[:10] < date_from:
297
+ continue
298
+ if date_to and created[:10] > date_to:
299
+ continue
300
+ if status and data.get("status") != status:
301
+ continue
302
+ if scenario and data.get("scenario") != scenario:
303
+ continue
304
+ rows.append(data)
305
+ if sort == "created_asc":
306
+ rows.sort(key=lambda item: item.get("created_at") or "")
307
+ elif sort == "finished_desc":
308
+ rows.sort(key=lambda item: item.get("finished_at") or item.get("created_at") or "", reverse=True)
309
+ else:
310
+ rows.sort(key=lambda item: item.get("created_at") or "", reverse=True)
311
+ total = len(rows)
312
+ offset = max(0, int(offset))
313
+ limit = max(1, min(int(limit), 200))
314
+ return rows[offset:offset + limit], total
315
+
316
+
317
+ def update_run(run: Run, **fields: Any) -> Run:
318
+ data = run.model_dump()
319
+ data.update(fields)
320
+ updated = Run.model_validate(data)
321
+ return write_run(updated)
322
+
323
+
324
+ # ---------------------------------------------------------------------------
325
+ # context
326
+ # ---------------------------------------------------------------------------
327
+ def read_context() -> dict[str, Any]:
328
+ data = read_json(CONTEXT, {})
329
+ return data if isinstance(data, dict) else {}
330
+
331
+
332
+ def write_context(values: dict[str, Any]) -> dict[str, Any]:
333
+ current = read_context()
334
+ current.update(values or {})
335
+ atomic_write_json(CONTEXT, current)
336
+ return current
337
+
338
+
339
+ # ---------------------------------------------------------------------------
340
+ # media helpers
341
+ # ---------------------------------------------------------------------------
342
+ def asset_media_path(asset_id: str) -> Path:
343
+ record = get_asset(asset_id)
344
+ if record is None:
345
+ raise FileNotFoundError(asset_id)
346
+ return safe_media_path(str(record["path"]))
347
+
348
+
349
+ def asset_media_url(asset_id: str) -> str:
350
+ record = get_asset(asset_id)
351
+ if record is None:
352
+ return ""
353
+ return f"/media?path={record['path']}"
354
+
355
+
356
+ def default_persona() -> dict[str, Any]:
357
+ return {
358
+ "id": "persona_auto",
359
+ "name": "自动模特",
360
+ "type": "ai",
361
+ "gender": "any",
362
+ "age": "youth",
363
+ "ethnicity": "asian",
364
+ "body_type": "standard",
365
+ "exact_age": None,
366
+ "height_cm": None,
367
+ "hair": "",
368
+ "outfit": "",
369
+ "temperament": "",
370
+ "voice_tone": "",
371
+ "reference_audio_asset_id": None,
372
+ "reference_images": [],
373
+ "role": "both",
374
+ "locks": {},
375
+ "consent": {"confirmed": True, "note": "自动生成角色,不涉及真人"},
376
+ "created_at": now(),
377
+ "used_in_runs": [],
378
+ }
package/host/dsh.js ADDED
@@ -0,0 +1,60 @@
1
+ /** Read the host's configured LLM catalogue and, optionally, run a text call.
2
+ *
3
+ * DSH already owns model routing and credentials. AdClip should not ask the user
4
+ * to re-enter an LLM key for copy/analysis when a DSH model is configured.
5
+ * Video generation is different: DSH exposes LLM calls, not video tasks.
6
+ */
7
+ export function createDshBridge(ctx) {
8
+ const llm = ctx.get('llm')
9
+ const defaults = ctx.get('agentDefaultModel')
10
+
11
+ async function list() {
12
+ if (!llm) return { available: false, providers: [], reason: 'llm service not mounted' }
13
+ const providers = []
14
+ for (const provider of llm.listProviders()) {
15
+ try {
16
+ const models = await llm.listModels(provider.id)
17
+ providers.push({ id: provider.id, name: provider.name, models })
18
+ } catch (error) {
19
+ providers.push({ id: provider.id, name: provider.name, models: [], error: String(error?.message || error) })
20
+ }
21
+ }
22
+ let selection = null
23
+ try { selection = defaults?.currentSelection?.() || null } catch { selection = null }
24
+ return { available: true, providers, selection }
25
+ }
26
+
27
+ async function chat({ provider, model, system, messages, maxTokens = 1200 }) {
28
+ if (!llm) throw new Error('DSH llm service is not mounted')
29
+ let selectedProvider = provider
30
+ let selectedModel = model
31
+ if ((!selectedProvider || !selectedModel) && defaults?.currentSelection) {
32
+ const selection = defaults.currentSelection()
33
+ selectedProvider = selectedProvider || selection.provider
34
+ selectedModel = selectedModel || selection.model
35
+ }
36
+ if (!selectedProvider || !selectedModel) {
37
+ const first = (await list()).providers.find((p) => (p.models || []).length)
38
+ if (!first) throw new Error('no DSH model is configured')
39
+ selectedProvider = first.id
40
+ selectedModel = first.models[0].id
41
+ }
42
+ const request = {
43
+ provider: selectedProvider,
44
+ model: selectedModel,
45
+ system,
46
+ maxTokens,
47
+ messages: (messages && messages.length ? messages : [{ role: 'user', content: [{ type: 'text', text: '你好' }] }]),
48
+ }
49
+ let text = ''
50
+ for await (const chunk of llm.stream(request)) {
51
+ if (chunk.type === 'text-delta') text += chunk.text
52
+ if (chunk.type === 'finish' && chunk.reason && chunk.reason.kind === 'error') {
53
+ throw new Error(chunk.reason.failure?.message || 'DSH model call failed')
54
+ }
55
+ }
56
+ return { provider: selectedProvider, model: selectedModel, text }
57
+ }
58
+
59
+ return { list, chat }
60
+ }