@diffra-ai/cli 0.2.1 → 0.2.3
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.
- package/CHANGELOG.md +18 -0
- package/dist/bundle/chunks/{anthropic-messages-UM2LSKVG.js → anthropic-messages-X6WVALGW.js} +1 -1
- package/dist/bundle/chunks/{azure-openai-responses-LLULQ6UR.js → azure-openai-responses-NWJUHXDZ.js} +1 -1
- package/dist/bundle/chunks/bedrock-converse-stream.js +1 -1
- package/dist/bundle/chunks/chunk-33NXM7QU.js +30 -0
- package/dist/bundle/chunks/{chunk-UBBD6YWP.js → chunk-7CGSQK56.js} +1 -1
- package/dist/bundle/chunks/{chunk-6NW7EDTU.js → chunk-C5DQBSLC.js} +6 -6
- package/dist/bundle/chunks/{chunk-37INV3TW.js → chunk-IQ4GJTRS.js} +1 -1
- package/dist/bundle/chunks/{chunk-MWJ5PWKE.js → chunk-JZRWXZHG.js} +1 -1
- package/dist/bundle/chunks/{chunk-Y5LGEDHU.js → chunk-L2DZJPGE.js} +1 -1
- package/dist/bundle/chunks/{chunk-J2CQ7AOB.js → chunk-QNL2W5AW.js} +1 -1
- package/dist/bundle/chunks/{chunk-I6QEKFFB.js → chunk-RV73LYFM.js} +1 -1
- package/dist/bundle/chunks/{chunk-BSCVN57O.js → chunk-WO7C3QOT.js} +1 -1
- package/dist/bundle/chunks/{chunk-QCEU5NDS.js → chunk-WYEDFTGD.js} +1 -1
- package/dist/bundle/chunks/{chunk-MAMCTGRH.js → chunk-XT6WUZXZ.js} +1 -1
- package/dist/bundle/chunks/{cli-VEFNOOXB.js → cli-LFWEPHUW.js} +1 -1
- package/dist/bundle/chunks/{cloudflare-workers-ai-system-one-VQHACWH5.js → cloudflare-workers-ai-system-one-TVT4UQJJ.js} +1 -1
- package/dist/bundle/chunks/github-copilot.js +1 -1
- package/dist/bundle/chunks/{google-generative-ai-YFRD2UAD.js → google-generative-ai-MBGYEF7P.js} +1 -1
- package/dist/bundle/chunks/{google-vertex-7EWCT4BL.js → google-vertex-BOTZRVRN.js} +1 -1
- package/dist/bundle/chunks/{llama-cpp-classify-D5PC6EEP.js → llama-cpp-classify-OLEUL2TP.js} +1 -1
- package/dist/bundle/chunks/{mistral-conversations-ILNLXCLF.js → mistral-conversations-5P5FZUVD.js} +1 -1
- package/dist/bundle/chunks/{openai-codex-responses-YESP5ZRN.js → openai-codex-responses-3AFER2OO.js} +1 -1
- package/dist/bundle/chunks/{openai-completions-BKYCRYVB.js → openai-completions-KCHGXEPR.js} +1 -1
- package/dist/bundle/chunks/{openai-decisions-OZVDLRU3.js → openai-decisions-V6S27AJQ.js} +1 -1
- package/dist/bundle/chunks/{openai-responses-GSXMBGMK.js → openai-responses-3SN2VI4R.js} +1 -1
- package/dist/bundle/chunks/{openrouter-images-TK5PJTOE.js → openrouter-images-QHMQBMZX.js} +1 -1
- package/dist/bundle/chunks/{pi-messages-AHAEGVU4.js → pi-messages-4P7GT7ZM.js} +1 -1
- package/dist/bundle/chunks/{typesafe-system-one-XHINCFVH.js → typesafe-system-one-QXVNQK47.js} +1 -1
- package/dist/bundle/chunks/{virtual-modules-7F3I52A6.js → virtual-modules-5D2HOQQM.js} +1 -1
- package/dist/bundle/cli-runtime.js +1 -1
- package/dist/bundle/cli.js +0 -0
- package/dist/bundle/index.js +1 -1
- package/dist/bundle/rpc-entry.js +1 -1
- package/dist/cli.js +0 -0
- package/dist/core/model-resolver.js +3 -3
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/extensions/skills/bundled/find-skills/LICENSE +21 -0
- package/dist/extensions/skills/bundled/find-skills/SKILL.md +117 -0
- package/dist/extensions/skills/bundled/grill-me/LICENSE +21 -0
- package/dist/extensions/skills/bundled/grill-me/SKILL.md +44 -0
- package/dist/extensions/skills/bundled/handoff/LICENSE +21 -0
- package/dist/extensions/skills/bundled/handoff/SKILL.md +31 -0
- package/dist/extensions/skills/bundled/mcp-builder/LICENSE.txt +202 -0
- package/dist/extensions/skills/bundled/mcp-builder/SKILL.md +260 -0
- package/dist/extensions/skills/bundled/mcp-builder/reference/evaluation.md +432 -0
- package/dist/extensions/skills/bundled/mcp-builder/reference/mcp_best_practices.md +249 -0
- package/dist/extensions/skills/bundled/mcp-builder/reference/node_mcp_server.md +972 -0
- package/dist/extensions/skills/bundled/mcp-builder/reference/node_mcp_server_v2.md +747 -0
- package/dist/extensions/skills/bundled/mcp-builder/reference/python_mcp_server.md +719 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/LICENSE +21 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/SKILL.md +251 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/app-interface.csv +33 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/charts.csv +26 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/colors.csv +195 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/google-fonts.csv +1935 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/icons.csv +106 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/landing.csv +35 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/motion.csv +21 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/products.csv +195 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/react-performance.csv +45 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/angular.csv +51 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/astro.csv +54 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/avalonia.csv +57 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/flutter.csv +53 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/html-tailwind.csv +60 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/javafx.csv +76 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/jetpack-compose.csv +53 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/laravel.csv +51 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/nextjs.csv +62 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/nuxt-ui.csv +84 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/nuxtjs.csv +68 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/pyside6-qss.csv +103 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/react-native.csv +52 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/react.csv +67 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/shadcn.csv +69 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/svelte.csv +56 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/swiftui.csv +51 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/threejs.csv +54 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/uno.csv +60 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/uwp.csv +56 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/vue.csv +50 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/winui.csv +60 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/stacks/wpf.csv +57 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/styles.csv +90 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/typography.csv +75 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/ui-reasoning.csv +195 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/data/ux-guidelines.csv +142 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/references/pro-rules.md +117 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/references/quick-reference.md +275 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/scripts/core.py +1111 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/scripts/design_system.py +1922 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/scripts/preview.py +557 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/scripts/reasoning_contract.py +123 -0
- package/dist/extensions/skills/bundled/ui-ux-pro-max/scripts/search.py +257 -0
- package/dist/extensions/skills/index.d.ts +4 -4
- package/dist/extensions/skills/index.d.ts.map +1 -1
- package/dist/extensions/skills/index.js +9 -4
- package/dist/extensions/skills/index.js.map +1 -1
- package/dist/rpc-entry.js +0 -0
- package/docs/builtin-skills.md +46 -3
- package/docs/docs.json +1 -1
- package/docs/skills.md +1 -1
- package/docs/slash-commands.md +2 -0
- package/examples/extensions/doom-overlay/doom/build/doom.wasm +0 -0
- package/examples/extensions/doom-overlay/doom/build.sh +0 -0
- package/package.json +6 -6
- package/dist/bundle/chunks/chunk-UIVU7TEF.js +0 -30
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""Self-contained HTML preview of a generated design system.
|
|
4
|
+
|
|
5
|
+
Writes preview.html with inline CSS (no JS framework, no Tailwind CDN).
|
|
6
|
+
Google Fonts are linked when the pairing provides a URL; system-font
|
|
7
|
+
fallbacks keep the page readable offline.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from html import escape
|
|
11
|
+
import os
|
|
12
|
+
import re
|
|
13
|
+
import tempfile
|
|
14
|
+
from pathlib import Path
|
|
15
|
+
|
|
16
|
+
from design_system import (
|
|
17
|
+
SEMANTIC_COLOR_ENTRIES,
|
|
18
|
+
_button_outline_text_color,
|
|
19
|
+
_contrast_ratio,
|
|
20
|
+
_palette_is_dark,
|
|
21
|
+
_write_persisted_file,
|
|
22
|
+
resolve_preview_locale,
|
|
23
|
+
safe_slug,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
_SAFE_HEX = re.compile(r"^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$")
|
|
27
|
+
_GOOGLE_FONTS_PREFIXES = (
|
|
28
|
+
"https://fonts.googleapis.com/",
|
|
29
|
+
"https://fonts.gstatic.com/",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
# Surface / on-surface pairs used for WCAG contrast badges.
|
|
33
|
+
_CONTRAST_PAIRS = (
|
|
34
|
+
("primary", "on_primary"),
|
|
35
|
+
("secondary", "on_secondary"),
|
|
36
|
+
("accent", "on_accent"),
|
|
37
|
+
("background", "foreground"),
|
|
38
|
+
("card", "card_foreground"),
|
|
39
|
+
("muted", "muted_foreground"),
|
|
40
|
+
("destructive", "on_destructive"),
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
_ON_ROLE = {surface: on for surface, on in _CONTRAST_PAIRS}
|
|
44
|
+
_ON_ROLE.update({on: surface for surface, on in _CONTRAST_PAIRS})
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _text(value) -> str:
|
|
48
|
+
if value is None:
|
|
49
|
+
return ""
|
|
50
|
+
return escape(str(value), quote=False)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _attr(value) -> str:
|
|
54
|
+
if value is None:
|
|
55
|
+
return ""
|
|
56
|
+
return escape(str(value), quote=True)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _css_color(value, fallback=None) -> str:
|
|
60
|
+
raw = (value or "").strip()
|
|
61
|
+
if _SAFE_HEX.match(raw):
|
|
62
|
+
return raw
|
|
63
|
+
if fallback and _SAFE_HEX.match(fallback.strip()):
|
|
64
|
+
return fallback.strip()
|
|
65
|
+
return ""
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _css_font(name: str) -> str:
|
|
69
|
+
cleaned = re.sub(r'["\\]', "", (name or "").strip())
|
|
70
|
+
return cleaned or "sans-serif"
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _google_fonts_href(url: str) -> str:
|
|
74
|
+
raw = (url or "").strip()
|
|
75
|
+
if any(raw.startswith(prefix) for prefix in _GOOGLE_FONTS_PREFIXES):
|
|
76
|
+
return _attr(raw)
|
|
77
|
+
return ""
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _wcag_level(ratio) -> str:
|
|
81
|
+
if ratio is None:
|
|
82
|
+
return ""
|
|
83
|
+
if ratio >= 7:
|
|
84
|
+
return "AAA"
|
|
85
|
+
if ratio >= 4.5:
|
|
86
|
+
return "AA"
|
|
87
|
+
return "Fail"
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
def _format_ratio(ratio) -> str:
|
|
91
|
+
if ratio is None:
|
|
92
|
+
return ""
|
|
93
|
+
return f"{ratio:.2f}:1"
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def _is_dark(design_system: dict) -> bool:
|
|
97
|
+
if design_system.get("color_mode") == "dark":
|
|
98
|
+
return True
|
|
99
|
+
colors = design_system.get("colors") or {}
|
|
100
|
+
return _palette_is_dark({"Background": colors.get("background", "")})
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _role_contrast(colors: dict, key: str):
|
|
104
|
+
pair_key = _ON_ROLE.get(key)
|
|
105
|
+
if not pair_key:
|
|
106
|
+
return None
|
|
107
|
+
return _contrast_ratio(colors.get(key, ""), colors.get(pair_key, ""))
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def format_preview_html(design_system: dict) -> str:
|
|
111
|
+
"""Render a dependency-free HTML page for the generated design system."""
|
|
112
|
+
project = design_system.get("project_name") or "Design system"
|
|
113
|
+
html_lang, direction = resolve_preview_locale(
|
|
114
|
+
str(project), lang=design_system.get("lang"), direction=design_system.get("direction"),
|
|
115
|
+
)
|
|
116
|
+
is_rtl = direction == "rtl"
|
|
117
|
+
html_dir = ' dir="rtl"' if is_rtl else ""
|
|
118
|
+
eyebrow_spacing = "0" if is_rtl else "0.08em"
|
|
119
|
+
eyebrow_transform = "none" if is_rtl else "uppercase"
|
|
120
|
+
badge_spacing = "0" if is_rtl else "0.04em"
|
|
121
|
+
category = design_system.get("category") or ""
|
|
122
|
+
style = design_system.get("style") or {}
|
|
123
|
+
colors = design_system.get("colors") or {}
|
|
124
|
+
typography = design_system.get("typography") or {}
|
|
125
|
+
effects = design_system.get("key_effects") or ""
|
|
126
|
+
anti_patterns = design_system.get("anti_patterns") or ""
|
|
127
|
+
notes = colors.get("notes") or ""
|
|
128
|
+
heading_font = typography.get("heading") or "sans-serif"
|
|
129
|
+
body_font = typography.get("body") or "sans-serif"
|
|
130
|
+
google_href = _google_fonts_href(typography.get("google_fonts_url") or "")
|
|
131
|
+
dark = _is_dark(design_system)
|
|
132
|
+
|
|
133
|
+
css_vars = []
|
|
134
|
+
for _label, key, css_var in SEMANTIC_COLOR_ENTRIES:
|
|
135
|
+
hex_val = _css_color(colors.get(key, ""))
|
|
136
|
+
if hex_val:
|
|
137
|
+
css_vars.append(f" {css_var}: {hex_val};")
|
|
138
|
+
|
|
139
|
+
background = _css_color(colors.get("background"), "#FFFFFF") or "#FFFFFF"
|
|
140
|
+
foreground = _css_color(colors.get("foreground"), "#111111") or "#111111"
|
|
141
|
+
card = _css_color(colors.get("card"), background) or background
|
|
142
|
+
card_fg = _css_color(colors.get("card_foreground"), foreground) or foreground
|
|
143
|
+
primary = _css_color(colors.get("primary"), "#2563EB") or "#2563EB"
|
|
144
|
+
on_primary = _css_color(colors.get("on_primary")) or _css_color(
|
|
145
|
+
colors.get("foreground"), "#FFFFFF"
|
|
146
|
+
) or "#FFFFFF"
|
|
147
|
+
secondary = _css_color(colors.get("secondary"), primary) or primary
|
|
148
|
+
on_secondary = _css_color(colors.get("on_secondary")) or on_primary
|
|
149
|
+
accent = _css_color(colors.get("accent") or colors.get("cta"), primary) or primary
|
|
150
|
+
on_accent = _css_color(colors.get("on_accent") or colors.get("on_cta")) or on_primary
|
|
151
|
+
destructive = _css_color(colors.get("destructive"), "#DC2626") or "#DC2626"
|
|
152
|
+
on_destructive = _css_color(colors.get("on_destructive")) or "#FFFFFF"
|
|
153
|
+
muted = _css_color(colors.get("muted"), card) or card
|
|
154
|
+
muted_fg = _css_color(colors.get("muted_foreground"), foreground) or foreground
|
|
155
|
+
border = _css_color(colors.get("border"), foreground) or foreground
|
|
156
|
+
ring = _css_color(colors.get("ring"), primary) or primary
|
|
157
|
+
secondary_text = _css_color(_button_outline_text_color(colors), foreground) or foreground
|
|
158
|
+
|
|
159
|
+
heading_stack = f'"{_css_font(heading_font)}", system-ui, -apple-system, "Segoe UI", sans-serif'
|
|
160
|
+
body_stack = f'"{_css_font(body_font)}", system-ui, -apple-system, "Segoe UI", sans-serif'
|
|
161
|
+
|
|
162
|
+
swatches = []
|
|
163
|
+
for label, key, css_var in SEMANTIC_COLOR_ENTRIES:
|
|
164
|
+
hex_val = _css_color(colors.get(key, ""))
|
|
165
|
+
if not hex_val:
|
|
166
|
+
continue
|
|
167
|
+
ratio = _role_contrast(colors, key)
|
|
168
|
+
pair_key = _ON_ROLE.get(key)
|
|
169
|
+
pair_hex = _css_color(colors.get(pair_key, "")) if pair_key else ""
|
|
170
|
+
level = _wcag_level(ratio)
|
|
171
|
+
ratio_html = ""
|
|
172
|
+
if ratio is not None:
|
|
173
|
+
badge_class = "badge-aaa" if level == "AAA" else "badge-aa" if level == "AA" else "badge-fail"
|
|
174
|
+
pair_label = next(
|
|
175
|
+
(entry[0] for entry in SEMANTIC_COLOR_ENTRIES if entry[1] == pair_key),
|
|
176
|
+
pair_key,
|
|
177
|
+
)
|
|
178
|
+
ratio_html = (
|
|
179
|
+
f'<p class="swatch-contrast">vs {_text(pair_label)} '
|
|
180
|
+
f'<code dir="ltr" style="unicode-bidi:isolate">{_text(pair_hex)}</code>: '
|
|
181
|
+
f'<span dir="ltr" style="unicode-bidi:isolate">{_text(_format_ratio(ratio))}</span> '
|
|
182
|
+
f'<span class="badge {badge_class}" dir="ltr" style="unicode-bidi:isolate">{_text(level)}</span></p>'
|
|
183
|
+
)
|
|
184
|
+
swatches.append(
|
|
185
|
+
f'<li class="swatch">'
|
|
186
|
+
f'<div class="swatch-chip" style="background:{hex_val}" aria-hidden="true"></div>'
|
|
187
|
+
f'<div class="swatch-meta">'
|
|
188
|
+
f'<p class="swatch-role">{_text(label)}</p>'
|
|
189
|
+
f'<p><code dir="ltr" style="unicode-bidi:isolate">{_text(hex_val)}</code> '
|
|
190
|
+
f'<code dir="ltr" style="unicode-bidi:isolate">{_text(css_var)}</code></p>'
|
|
191
|
+
f'{ratio_html}'
|
|
192
|
+
f'</div></li>'
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
anti_items = []
|
|
196
|
+
for clause in (anti_patterns.split("+") if anti_patterns else []):
|
|
197
|
+
clause = clause.strip()
|
|
198
|
+
if clause:
|
|
199
|
+
anti_items.append(f"<li>{_text(clause)}</li>")
|
|
200
|
+
if not anti_items:
|
|
201
|
+
anti_items.append("<li>None listed for this system.</li>")
|
|
202
|
+
|
|
203
|
+
font_links = ""
|
|
204
|
+
if google_href:
|
|
205
|
+
font_links = (
|
|
206
|
+
' <link rel="preconnect" href="https://fonts.googleapis.com">\n'
|
|
207
|
+
' <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>\n'
|
|
208
|
+
f' <link rel="stylesheet" href="{google_href}">\n'
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
color_scheme = "dark" if dark else "light"
|
|
212
|
+
notes_html = (
|
|
213
|
+
f'<p class="notes">{_text(notes)}</p>' if notes else ""
|
|
214
|
+
)
|
|
215
|
+
effects_html = _text(effects) if effects else "None listed for this system."
|
|
216
|
+
css_vars_block = "\n".join(css_vars)
|
|
217
|
+
swatches_html = "".join(swatches)
|
|
218
|
+
anti_html = "".join(anti_items)
|
|
219
|
+
|
|
220
|
+
return f"""<!DOCTYPE html>
|
|
221
|
+
<html lang="{escape(html_lang)}"{html_dir}>
|
|
222
|
+
<head>
|
|
223
|
+
<meta charset="utf-8">
|
|
224
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
225
|
+
<meta name="color-scheme" content="{color_scheme}">
|
|
226
|
+
<title>{_attr(project)} design system preview</title>
|
|
227
|
+
{font_links} <style>
|
|
228
|
+
:root {{
|
|
229
|
+
{css_vars_block}
|
|
230
|
+
--font-heading: {heading_stack};
|
|
231
|
+
--font-body: {body_stack};
|
|
232
|
+
}}
|
|
233
|
+
*, *::before, *::after {{ box-sizing: border-box; }}
|
|
234
|
+
html {{ font-size: 16px; }}
|
|
235
|
+
body {{
|
|
236
|
+
margin: 0;
|
|
237
|
+
min-height: 100vh;
|
|
238
|
+
font-family: var(--font-body);
|
|
239
|
+
background: {background};
|
|
240
|
+
color: {foreground};
|
|
241
|
+
line-height: 1.5;
|
|
242
|
+
}}
|
|
243
|
+
a:focus-visible,
|
|
244
|
+
button:focus-visible,
|
|
245
|
+
input:focus-visible {{
|
|
246
|
+
outline: 3px solid {ring};
|
|
247
|
+
outline-offset: 2px;
|
|
248
|
+
}}
|
|
249
|
+
header, main {{
|
|
250
|
+
width: min(1100px, calc(100% - 2rem));
|
|
251
|
+
margin-inline: auto;
|
|
252
|
+
}}
|
|
253
|
+
header {{
|
|
254
|
+
padding: 2.5rem 0 1.5rem;
|
|
255
|
+
border-bottom: 1px solid {border};
|
|
256
|
+
}}
|
|
257
|
+
.eyebrow {{
|
|
258
|
+
margin: 0 0 0.35rem;
|
|
259
|
+
text-transform: {eyebrow_transform};
|
|
260
|
+
letter-spacing: {eyebrow_spacing};
|
|
261
|
+
font-size: 0.75rem;
|
|
262
|
+
color: {muted_fg};
|
|
263
|
+
}}
|
|
264
|
+
h1, h2, h3, .specimen-heading {{
|
|
265
|
+
font-family: var(--font-heading);
|
|
266
|
+
line-height: 1.2;
|
|
267
|
+
margin: 0 0 0.5rem;
|
|
268
|
+
}}
|
|
269
|
+
html[dir="rtl"] h1, html[dir="rtl"] h2, html[dir="rtl"] h3, html[dir="rtl"] .specimen-heading {{
|
|
270
|
+
letter-spacing: 0;
|
|
271
|
+
}}
|
|
272
|
+
h1 {{ font-size: 2.25rem; }}
|
|
273
|
+
h2 {{ font-size: 1.5rem; }}
|
|
274
|
+
h3 {{ font-size: 1.2rem; }}
|
|
275
|
+
main {{ padding: 2rem 0 4rem; display: grid; gap: 2.5rem; }}
|
|
276
|
+
section {{
|
|
277
|
+
background: {card};
|
|
278
|
+
color: {card_fg};
|
|
279
|
+
border: 1px solid {border};
|
|
280
|
+
border-radius: 12px;
|
|
281
|
+
padding: 1.5rem;
|
|
282
|
+
}}
|
|
283
|
+
.swatches {{
|
|
284
|
+
list-style: none;
|
|
285
|
+
margin: 1rem 0 0;
|
|
286
|
+
padding: 0;
|
|
287
|
+
display: grid;
|
|
288
|
+
gap: 0.75rem;
|
|
289
|
+
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
|
|
290
|
+
}}
|
|
291
|
+
.swatch {{
|
|
292
|
+
display: flex;
|
|
293
|
+
gap: 0.75rem;
|
|
294
|
+
align-items: flex-start;
|
|
295
|
+
}}
|
|
296
|
+
.swatch-chip {{
|
|
297
|
+
width: 3rem;
|
|
298
|
+
height: 3rem;
|
|
299
|
+
border-radius: 8px;
|
|
300
|
+
border: 1px solid {border};
|
|
301
|
+
flex-shrink: 0;
|
|
302
|
+
}}
|
|
303
|
+
.swatch-meta p {{ margin: 0; font-size: 0.9rem; }}
|
|
304
|
+
.swatch-role {{ font-weight: 600; }}
|
|
305
|
+
.swatch-contrast {{ color: {muted_fg}; }}
|
|
306
|
+
code {{ font-size: 0.85em; }}
|
|
307
|
+
.badge {{
|
|
308
|
+
display: inline-block;
|
|
309
|
+
margin-inline-start: 0.35rem;
|
|
310
|
+
padding: 0.05rem 0.4rem;
|
|
311
|
+
border-radius: 999px;
|
|
312
|
+
font-size: 0.75rem;
|
|
313
|
+
font-weight: 700;
|
|
314
|
+
letter-spacing: {badge_spacing};
|
|
315
|
+
}}
|
|
316
|
+
.badge-aaa {{ background: {primary}; color: {on_primary}; }}
|
|
317
|
+
.badge-aa {{ background: {muted}; color: {muted_fg}; }}
|
|
318
|
+
.badge-fail {{ background: {destructive}; color: {on_destructive}; }}
|
|
319
|
+
.type-scale {{ display: grid; gap: 0.75rem; }}
|
|
320
|
+
.type-scale p {{ margin: 0; max-width: 42rem; }}
|
|
321
|
+
.btn-row {{
|
|
322
|
+
display: flex;
|
|
323
|
+
flex-wrap: wrap;
|
|
324
|
+
gap: 0.75rem;
|
|
325
|
+
margin: 0.75rem 0;
|
|
326
|
+
}}
|
|
327
|
+
.btn {{
|
|
328
|
+
font-family: var(--font-body);
|
|
329
|
+
font-weight: 600;
|
|
330
|
+
font-size: 1rem;
|
|
331
|
+
padding: 12px 24px;
|
|
332
|
+
border-radius: 8px;
|
|
333
|
+
border: 2px solid transparent;
|
|
334
|
+
cursor: pointer;
|
|
335
|
+
transition: opacity 200ms ease, transform 200ms ease, box-shadow 200ms ease;
|
|
336
|
+
}}
|
|
337
|
+
.btn-primary {{ background: {primary}; color: {on_primary}; }}
|
|
338
|
+
.btn-secondary {{
|
|
339
|
+
background: transparent;
|
|
340
|
+
color: {secondary_text};
|
|
341
|
+
border-color: {secondary};
|
|
342
|
+
}}
|
|
343
|
+
.btn-accent {{ background: {accent}; color: {on_accent}; }}
|
|
344
|
+
.btn-destructive {{ background: {destructive}; color: {on_destructive}; }}
|
|
345
|
+
.btn:hover, .btn.is-hover {{
|
|
346
|
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.18);
|
|
347
|
+
transform: translateY(-1px);
|
|
348
|
+
}}
|
|
349
|
+
.btn:focus-visible, .btn.is-focus {{
|
|
350
|
+
outline: 3px solid {ring};
|
|
351
|
+
outline-offset: 2px;
|
|
352
|
+
}}
|
|
353
|
+
.btn:disabled, .btn.is-disabled {{
|
|
354
|
+
opacity: 0.45;
|
|
355
|
+
cursor: not-allowed;
|
|
356
|
+
transform: none;
|
|
357
|
+
}}
|
|
358
|
+
.state-label {{
|
|
359
|
+
font-size: 0.8rem;
|
|
360
|
+
color: {muted_fg};
|
|
361
|
+
margin: 0.25rem 0 0;
|
|
362
|
+
}}
|
|
363
|
+
.preview-card {{
|
|
364
|
+
background: {card};
|
|
365
|
+
color: {card_fg};
|
|
366
|
+
border: 1px solid {border};
|
|
367
|
+
border-radius: 12px;
|
|
368
|
+
padding: 1.5rem;
|
|
369
|
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
|
370
|
+
max-width: 28rem;
|
|
371
|
+
}}
|
|
372
|
+
.field {{ display: grid; gap: 0.35rem; max-width: 28rem; }}
|
|
373
|
+
.field + .field {{ margin-top: 1rem; }}
|
|
374
|
+
label {{ font-weight: 600; }}
|
|
375
|
+
input[type="text"], input[type="email"] {{
|
|
376
|
+
font: inherit;
|
|
377
|
+
padding: 12px 16px;
|
|
378
|
+
border: 1px solid {border};
|
|
379
|
+
border-radius: 8px;
|
|
380
|
+
background: {background};
|
|
381
|
+
color: {foreground};
|
|
382
|
+
}}
|
|
383
|
+
input[aria-invalid="true"] {{
|
|
384
|
+
border-color: {destructive};
|
|
385
|
+
}}
|
|
386
|
+
.help {{ margin: 0; color: {muted_fg}; font-size: 0.9rem; }}
|
|
387
|
+
.error {{
|
|
388
|
+
margin: 0;
|
|
389
|
+
color: {on_destructive};
|
|
390
|
+
background: {destructive};
|
|
391
|
+
font-size: 0.9rem;
|
|
392
|
+
font-weight: 600;
|
|
393
|
+
padding: 0.4rem 0.6rem;
|
|
394
|
+
border-radius: 6px;
|
|
395
|
+
}}
|
|
396
|
+
.notes {{ color: {muted_fg}; }}
|
|
397
|
+
@media (prefers-reduced-motion: reduce) {{
|
|
398
|
+
*, *::before, *::after {{
|
|
399
|
+
animation: none !important;
|
|
400
|
+
transition: none !important;
|
|
401
|
+
scroll-behavior: auto !important;
|
|
402
|
+
}}
|
|
403
|
+
.btn:hover, .btn.is-hover {{ transform: none; }}
|
|
404
|
+
}}
|
|
405
|
+
</style>
|
|
406
|
+
</head>
|
|
407
|
+
<body>
|
|
408
|
+
<header>
|
|
409
|
+
<p class="eyebrow">Design system preview</p>
|
|
410
|
+
<h1>{_text(project)}</h1>
|
|
411
|
+
<p>{_text(style.get("name") or "Unspecified style")}{_text(" · " + category if category else "")}</p>
|
|
412
|
+
</header>
|
|
413
|
+
<main>
|
|
414
|
+
<section aria-labelledby="palette-heading">
|
|
415
|
+
<h2 id="palette-heading">Palette</h2>
|
|
416
|
+
{notes_html}
|
|
417
|
+
<ul class="swatches">
|
|
418
|
+
{swatches_html}
|
|
419
|
+
</ul>
|
|
420
|
+
</section>
|
|
421
|
+
<section aria-labelledby="type-heading">
|
|
422
|
+
<h2 id="type-heading">Typography</h2>
|
|
423
|
+
<p class="help">Heading: {_text(heading_font)} · Body: {_text(body_font)}</p>
|
|
424
|
+
<div class="type-scale">
|
|
425
|
+
<p class="specimen-heading" style="font-size:2.5rem">Display · {_text(heading_font)}</p>
|
|
426
|
+
<h3>Heading 2 · {_text(heading_font)}</h3>
|
|
427
|
+
<p>Body copy in {_text(body_font)}. The generated pairing is shown at a readable scale so you can judge hierarchy, weight, and line length before building components. Pack my box with five dozen liquor jugs.</p>
|
|
428
|
+
</div>
|
|
429
|
+
</section>
|
|
430
|
+
<section aria-labelledby="buttons-heading">
|
|
431
|
+
<h2 id="buttons-heading">Buttons</h2>
|
|
432
|
+
<p class="state-label">Default</p>
|
|
433
|
+
<div class="btn-row">
|
|
434
|
+
<button class="btn btn-primary" type="button">Primary</button>
|
|
435
|
+
<button class="btn btn-secondary" type="button">Secondary</button>
|
|
436
|
+
<button class="btn btn-accent" type="button">Accent</button>
|
|
437
|
+
<button class="btn btn-destructive" type="button">Destructive</button>
|
|
438
|
+
</div>
|
|
439
|
+
<p class="state-label">Hover</p>
|
|
440
|
+
<div class="btn-row">
|
|
441
|
+
<button class="btn btn-primary is-hover" type="button">Primary</button>
|
|
442
|
+
<button class="btn btn-secondary is-hover" type="button">Secondary</button>
|
|
443
|
+
<button class="btn btn-accent is-hover" type="button">Accent</button>
|
|
444
|
+
<button class="btn btn-destructive is-hover" type="button">Destructive</button>
|
|
445
|
+
</div>
|
|
446
|
+
<p class="state-label">Focus-visible</p>
|
|
447
|
+
<div class="btn-row">
|
|
448
|
+
<button class="btn btn-primary is-focus" type="button">Primary</button>
|
|
449
|
+
<button class="btn btn-secondary is-focus" type="button">Secondary</button>
|
|
450
|
+
<button class="btn btn-accent is-focus" type="button">Accent</button>
|
|
451
|
+
<button class="btn btn-destructive is-focus" type="button">Destructive</button>
|
|
452
|
+
</div>
|
|
453
|
+
<p class="state-label">Disabled</p>
|
|
454
|
+
<div class="btn-row">
|
|
455
|
+
<button class="btn btn-primary" type="button" disabled>Primary</button>
|
|
456
|
+
<button class="btn btn-secondary" type="button" disabled>Secondary</button>
|
|
457
|
+
<button class="btn btn-accent" type="button" disabled>Accent</button>
|
|
458
|
+
<button class="btn btn-destructive" type="button" disabled>Destructive</button>
|
|
459
|
+
</div>
|
|
460
|
+
</section>
|
|
461
|
+
<section aria-labelledby="card-heading">
|
|
462
|
+
<h2 id="card-heading">Card</h2>
|
|
463
|
+
<article class="preview-card">
|
|
464
|
+
<h3>{_text(project)}</h3>
|
|
465
|
+
<p>A sample card using the generated card surface, border, and type pairing.</p>
|
|
466
|
+
<button class="btn btn-primary" type="button">Continue</button>
|
|
467
|
+
</article>
|
|
468
|
+
</section>
|
|
469
|
+
<section aria-labelledby="form-heading">
|
|
470
|
+
<h2 id="form-heading">Form</h2>
|
|
471
|
+
<form>
|
|
472
|
+
<div class="field">
|
|
473
|
+
<label for="preview-email">Email</label>
|
|
474
|
+
<input id="preview-email" name="email" type="email" autocomplete="email" aria-describedby="preview-email-help">
|
|
475
|
+
<p id="preview-email-help" class="help">Use your work address.</p>
|
|
476
|
+
</div>
|
|
477
|
+
<div class="field">
|
|
478
|
+
<label for="preview-name">Display name</label>
|
|
479
|
+
<input id="preview-name" name="name" type="text" value="Not a valid value" aria-invalid="true" aria-describedby="preview-name-error">
|
|
480
|
+
<p id="preview-name-error" class="error">Enter a display name of at least two characters.</p>
|
|
481
|
+
</div>
|
|
482
|
+
</form>
|
|
483
|
+
</section>
|
|
484
|
+
<section aria-labelledby="style-heading">
|
|
485
|
+
<h2 id="style-heading">Style, effects, anti-patterns</h2>
|
|
486
|
+
<p><strong>Style:</strong> {_text(style.get("name") or "")}</p>
|
|
487
|
+
<p><strong>Key effects:</strong> {effects_html}</p>
|
|
488
|
+
<h3>Anti-patterns</h3>
|
|
489
|
+
<ul>
|
|
490
|
+
{anti_html}
|
|
491
|
+
</ul>
|
|
492
|
+
</section>
|
|
493
|
+
</main>
|
|
494
|
+
</body>
|
|
495
|
+
</html>
|
|
496
|
+
"""
|
|
497
|
+
|
|
498
|
+
|
|
499
|
+
def preview_destination(design_system: dict, persist: bool = False,
|
|
500
|
+
output_dir: str = None) -> Path:
|
|
501
|
+
"""Resolve where preview.html should be written (not used for temp files)."""
|
|
502
|
+
project_name = design_system.get("project_name") or "default"
|
|
503
|
+
slug = safe_slug(project_name)
|
|
504
|
+
if persist:
|
|
505
|
+
base_dir = Path(output_dir) if output_dir else Path.cwd()
|
|
506
|
+
return base_dir / "design-system" / slug / "preview.html"
|
|
507
|
+
if output_dir:
|
|
508
|
+
return Path(output_dir) / "preview.html"
|
|
509
|
+
raise ValueError("preview_destination requires persist or output_dir")
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
def write_preview_html(design_system: dict, persist: bool = False,
|
|
513
|
+
output_dir: str = None, force: bool = False) -> dict:
|
|
514
|
+
"""Write preview.html. Follows persist path rules and --force semantics.
|
|
515
|
+
|
|
516
|
+
With persist: design-system/<slug>/preview.html under output_dir or cwd.
|
|
517
|
+
Without persist: --output-dir/preview.html, or a temp file when output_dir
|
|
518
|
+
is omitted. Existing files are left untouched unless force is True.
|
|
519
|
+
"""
|
|
520
|
+
html = format_preview_html(design_system)
|
|
521
|
+
|
|
522
|
+
if not persist and not output_dir:
|
|
523
|
+
handle = tempfile.NamedTemporaryFile(
|
|
524
|
+
mode="w", encoding="utf-8", suffix=".html",
|
|
525
|
+
prefix="design-system-preview-", delete=False,
|
|
526
|
+
)
|
|
527
|
+
try:
|
|
528
|
+
handle.write(html)
|
|
529
|
+
handle.flush()
|
|
530
|
+
os.fsync(handle.fileno())
|
|
531
|
+
finally:
|
|
532
|
+
handle.close()
|
|
533
|
+
return {
|
|
534
|
+
"status": "success",
|
|
535
|
+
"preview_file": handle.name,
|
|
536
|
+
"created_files": [handle.name],
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
path = preview_destination(design_system, persist=persist, output_dir=output_dir)
|
|
540
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
541
|
+
try:
|
|
542
|
+
_write_persisted_file(path, html, force)
|
|
543
|
+
except FileExistsError:
|
|
544
|
+
return {
|
|
545
|
+
"status": "skipped_exists",
|
|
546
|
+
"preview_file": str(path),
|
|
547
|
+
"created_files": [],
|
|
548
|
+
"message": (
|
|
549
|
+
f"{path} already exists and was not modified. "
|
|
550
|
+
"Re-run with force=True / --force to overwrite."
|
|
551
|
+
),
|
|
552
|
+
}
|
|
553
|
+
return {
|
|
554
|
+
"status": "success",
|
|
555
|
+
"preview_file": str(path),
|
|
556
|
+
"created_files": [str(path)],
|
|
557
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Closed, non-executable grammar for design-system decision rules."""
|
|
3
|
+
|
|
4
|
+
import json
|
|
5
|
+
import re
|
|
6
|
+
|
|
7
|
+
CONDITION_SIGNALS = {
|
|
8
|
+
"if_booking": ("booking", "appointment", "calendar"),
|
|
9
|
+
"if_boutique": ("boutique",),
|
|
10
|
+
"if_casual": ("casual", "playful"),
|
|
11
|
+
"if_checkout": ("checkout", "payment", "purchase"),
|
|
12
|
+
"if_children": ("child", "children", "kids"),
|
|
13
|
+
"if_collaboration": ("collaboration", "multiplayer", "co-edit"),
|
|
14
|
+
"if_competitive": ("competitive", "leaderboard"),
|
|
15
|
+
"if_content_focused": ("content", "article", "reading", "documentation"),
|
|
16
|
+
"if_conversion_focused": ("conversion", "sales", "signup", "purchase"),
|
|
17
|
+
"if_creative_field": ("creative", "artist", "portfolio"),
|
|
18
|
+
"if_crop_focused": ("crop", "farm", "agriculture"),
|
|
19
|
+
"if_dashboard": ("dashboard", "operations", "monitoring"),
|
|
20
|
+
"if_data_heavy": ("data heavy", "data-heavy", "analytics", "large dataset"),
|
|
21
|
+
"if_delivery": ("delivery", "courier", "shipping"),
|
|
22
|
+
"if_discovery_focused": ("discover", "discovery", "browse", "directory"),
|
|
23
|
+
"if_engagement_metric": ("engagement", "retention", "contribution"),
|
|
24
|
+
"if_experience_focused": ("experience", "immersive", "journey"),
|
|
25
|
+
"if_gamification": ("gamification", "badges", "streak"),
|
|
26
|
+
"if_health": ("health", "medical", "patient"),
|
|
27
|
+
"if_hero_needed": ("hero", "showcase", "launch"),
|
|
28
|
+
"if_large_dataset": ("large dataset", "thousands", "millions"),
|
|
29
|
+
"if_light_mode_needed": ("light mode", "light theme"),
|
|
30
|
+
"if_low_performance": ("low performance", "low-end", "slow device"),
|
|
31
|
+
"if_luxury": ("luxury", "premium", "high-end"),
|
|
32
|
+
"if_medication": ("medication", "medicine", "prescription"),
|
|
33
|
+
"if_meditation": ("meditation", "breathing", "mindfulness"),
|
|
34
|
+
"if_minimal_portfolio": ("minimal portfolio", "simple portfolio"),
|
|
35
|
+
"if_mobile": ("mobile", "phone", "tablet", "ios", "android"),
|
|
36
|
+
"if_personalized": ("personalized", "personalised", "recommendation"),
|
|
37
|
+
"if_pre_launch": ("pre-launch", "prelaunch", "coming soon", "waitlist"),
|
|
38
|
+
"if_salary_focused": ("salary", "compensation", "pay range"),
|
|
39
|
+
"if_team_collaboration": ("team collaboration", "team workspace"),
|
|
40
|
+
"if_trust_needed": ("trust", "secure", "verified", "authority"),
|
|
41
|
+
"if_ux_focused": ("ux", "usability", "accessibility", "accessible"),
|
|
42
|
+
"if_video_ready": ("video ready", "product video", "demo video"),
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
ALLOWED_CONDITIONS = {"must_have", *CONDITION_SIGNALS}
|
|
46
|
+
ACTION_PREFIXES = {"constraint", "style", "pattern", "mode"}
|
|
47
|
+
TOKEN_ACTION_PREFIXES = {"constraint", "style"}
|
|
48
|
+
TOKEN_RE = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
|
|
49
|
+
CONDITION_PATTERNS = {
|
|
50
|
+
condition: tuple(
|
|
51
|
+
re.compile(r"(?<!\w)" + re.escape(signal) + r"(?!\w)")
|
|
52
|
+
for signal in signals
|
|
53
|
+
)
|
|
54
|
+
for condition, signals in CONDITION_SIGNALS.items()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _object_without_duplicates(pairs):
|
|
59
|
+
result = {}
|
|
60
|
+
for key, value in pairs:
|
|
61
|
+
if key in result:
|
|
62
|
+
raise ValueError("duplicate decision-rule key: {}".format(key))
|
|
63
|
+
result[key] = value
|
|
64
|
+
return result
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def parse_decision_rules(raw):
|
|
68
|
+
"""Parse the canonical condition -> action-array representation."""
|
|
69
|
+
try:
|
|
70
|
+
rules = json.loads(raw or "{}", object_pairs_hook=_object_without_duplicates)
|
|
71
|
+
except json.JSONDecodeError as error:
|
|
72
|
+
raise ValueError("invalid decision-rule JSON: {}".format(error)) from error
|
|
73
|
+
if not isinstance(rules, dict):
|
|
74
|
+
raise ValueError("decision rules must be a JSON object")
|
|
75
|
+
for condition, actions in rules.items():
|
|
76
|
+
if condition not in ALLOWED_CONDITIONS:
|
|
77
|
+
raise ValueError("unknown decision-rule condition: {}".format(condition))
|
|
78
|
+
if not isinstance(actions, list) or not actions:
|
|
79
|
+
raise ValueError("{} must map to a non-empty action array".format(condition))
|
|
80
|
+
for action in actions:
|
|
81
|
+
_validate_action(action)
|
|
82
|
+
if len(actions) != len(set(actions)):
|
|
83
|
+
raise ValueError("{} contains duplicate actions".format(condition))
|
|
84
|
+
return rules
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def _validate_action(action):
|
|
88
|
+
if not isinstance(action, str) or ":" not in action:
|
|
89
|
+
raise ValueError("action must use a known prefix: {}".format(action))
|
|
90
|
+
prefix, value = action.split(":", 1)
|
|
91
|
+
if prefix not in ACTION_PREFIXES:
|
|
92
|
+
raise ValueError("unknown decision-rule action: {}".format(action))
|
|
93
|
+
if prefix in TOKEN_ACTION_PREFIXES and not TOKEN_RE.fullmatch(value):
|
|
94
|
+
raise ValueError("invalid {} action value: {}".format(prefix, value))
|
|
95
|
+
if prefix == "pattern" and not value.strip():
|
|
96
|
+
raise ValueError("pattern action must name a pattern")
|
|
97
|
+
if prefix == "mode" and value not in {"dark", "light"}:
|
|
98
|
+
raise ValueError("mode action must be dark or light")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def apply_decision_rules(rules, query):
|
|
102
|
+
"""Return deterministic mutations and an audit trail; never execute data."""
|
|
103
|
+
normalized = str(query or "").casefold()
|
|
104
|
+
result = {"activated": [], "style_ids": [], "constraints": [],
|
|
105
|
+
"pattern": None, "mode": None}
|
|
106
|
+
for condition, actions in rules.items():
|
|
107
|
+
active = condition == "must_have" or any(
|
|
108
|
+
pattern.search(normalized)
|
|
109
|
+
for pattern in CONDITION_PATTERNS.get(condition, ()))
|
|
110
|
+
if not active:
|
|
111
|
+
continue
|
|
112
|
+
result["activated"].append({"condition": condition, "actions": list(actions)})
|
|
113
|
+
for action in actions:
|
|
114
|
+
prefix, value = action.split(":", 1)
|
|
115
|
+
if prefix == "style" and value not in result["style_ids"]:
|
|
116
|
+
result["style_ids"].append(value)
|
|
117
|
+
elif prefix == "constraint" and value not in result["constraints"]:
|
|
118
|
+
result["constraints"].append(value)
|
|
119
|
+
elif prefix == "pattern":
|
|
120
|
+
result["pattern"] = value
|
|
121
|
+
elif prefix == "mode":
|
|
122
|
+
result["mode"] = value
|
|
123
|
+
return result
|