asta-ui 0.9.5 → 0.9.7

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 (144) hide show
  1. package/css/asta.css +2 -2
  2. package/css/asta.js +1461 -1
  3. package/css/components.css +1900 -138
  4. package/css/runtime/agent.css +136 -0
  5. package/css/runtime/agent.js +173 -0
  6. package/css/runtime/assist.css +79 -0
  7. package/css/runtime/assist.js +799 -0
  8. package/css/runtime/controls.css +48 -0
  9. package/css/runtime/controls.js +482 -0
  10. package/css/src/agent.css +484 -0
  11. package/css/src/assist.css +598 -0
  12. package/css/src/controls.css +424 -0
  13. package/css/tokens.css +16 -0
  14. package/dist/agent-plan.d.ts +2 -0
  15. package/dist/agent-plan.js +11 -0
  16. package/dist/agent-team.d.ts +2 -0
  17. package/dist/agent-team.js +7 -0
  18. package/dist/api-keys.d.ts +2 -0
  19. package/dist/api-keys.js +10 -0
  20. package/dist/autonomy-dial.d.ts +2 -0
  21. package/dist/autonomy-dial.js +7 -0
  22. package/dist/base/agent-plan.d.ts +2 -0
  23. package/dist/base/agent-plan.js +11 -0
  24. package/dist/base/agent-team.d.ts +2 -0
  25. package/dist/base/agent-team.js +7 -0
  26. package/dist/base/api-keys.d.ts +2 -0
  27. package/dist/base/api-keys.js +10 -0
  28. package/dist/base/autonomy-dial.d.ts +2 -0
  29. package/dist/base/autonomy-dial.js +7 -0
  30. package/dist/base/change-ledger.d.ts +2 -0
  31. package/dist/base/change-ledger.js +7 -0
  32. package/dist/base/citations.d.ts +2 -0
  33. package/dist/base/citations.js +9 -0
  34. package/dist/base/context-meter.d.ts +2 -0
  35. package/dist/base/context-meter.js +14 -0
  36. package/dist/base/fallback-chain.d.ts +2 -0
  37. package/dist/base/fallback-chain.js +7 -0
  38. package/dist/base/model-picker.d.ts +2 -0
  39. package/dist/base/model-picker.js +9 -0
  40. package/dist/base/navbar.js +1 -1
  41. package/dist/base/prompt-composer.d.ts +2 -0
  42. package/dist/base/prompt-composer.js +9 -0
  43. package/dist/base/scrub-input.d.ts +2 -0
  44. package/dist/base/scrub-input.js +13 -0
  45. package/dist/base/sidebar.js +1 -1
  46. package/dist/base/thinking.d.ts +2 -0
  47. package/dist/base/thinking.js +11 -0
  48. package/dist/base/tool-approval.d.ts +2 -0
  49. package/dist/base/tool-approval.js +9 -0
  50. package/dist/base/trace-waterfall.d.ts +2 -0
  51. package/dist/base/trace-waterfall.js +9 -0
  52. package/dist/base.js +204 -136
  53. package/dist/change-ledger.d.ts +2 -0
  54. package/dist/change-ledger.js +7 -0
  55. package/dist/chunk-24O5V2PQ.js +253 -0
  56. package/dist/chunk-44DNL43W.js +490 -0
  57. package/dist/chunk-45IXJ7W7.js +245 -0
  58. package/dist/chunk-ANL6VX4X.js +326 -0
  59. package/dist/chunk-DNROUJ62.js +432 -0
  60. package/dist/{chunk-2EBWWZPS.js → chunk-DWPGFNNF.js} +38 -9
  61. package/dist/{chunk-CACW5LH3.js → chunk-EAHHWF3V.js} +38 -9
  62. package/dist/chunk-F4CWSZXK.js +226 -0
  63. package/dist/chunk-FT66LAUZ.js +388 -0
  64. package/dist/chunk-GBOJUMMY.js +163 -0
  65. package/dist/chunk-H4NFBYKY.js +275 -0
  66. package/dist/chunk-KFPGQ5RT.js +321 -0
  67. package/dist/chunk-OIIMYSVT.js +342 -0
  68. package/dist/chunk-SUWA7DM6.js +388 -0
  69. package/dist/chunk-TL32C2SQ.js +221 -0
  70. package/dist/chunk-UBTYMAKF.js +240 -0
  71. package/dist/chunk-W6YVDJHX.js +235 -0
  72. package/dist/chunk-XYQI4ZYV.js +314 -0
  73. package/dist/citations.d.ts +2 -0
  74. package/dist/citations.js +9 -0
  75. package/dist/context-meter.d.ts +2 -0
  76. package/dist/context-meter.js +14 -0
  77. package/dist/fallback-chain.d.ts +2 -0
  78. package/dist/fallback-chain.js +7 -0
  79. package/dist/index.js +204 -136
  80. package/dist/model-picker.d.ts +2 -0
  81. package/dist/model-picker.js +9 -0
  82. package/dist/navbar.js +1 -1
  83. package/dist/prompt-composer.d.ts +2 -0
  84. package/dist/prompt-composer.js +9 -0
  85. package/dist/scrub-input.d.ts +2 -0
  86. package/dist/scrub-input.js +13 -0
  87. package/dist/sidebar.js +1 -1
  88. package/dist/thinking.d.ts +2 -0
  89. package/dist/thinking.js +11 -0
  90. package/dist/tool-approval.d.ts +2 -0
  91. package/dist/tool-approval.js +9 -0
  92. package/dist/trace-waterfall.d.ts +2 -0
  93. package/dist/trace-waterfall.js +9 -0
  94. package/dist/types/components/kit/agent-plan.d.ts +34 -0
  95. package/dist/types/components/kit/api-keys.d.ts +62 -0
  96. package/dist/types/components/kit/base/navbar.d.ts +4 -1
  97. package/dist/types/components/kit/base/sidebar.d.ts +29 -7
  98. package/dist/types/components/kit/citations.d.ts +34 -0
  99. package/dist/types/components/kit/context-meter.d.ts +38 -0
  100. package/dist/types/components/kit/model-picker.d.ts +46 -0
  101. package/dist/types/components/kit/navbar.d.ts +4 -1
  102. package/dist/types/components/kit/prompt-composer.d.ts +61 -0
  103. package/dist/types/components/kit/scrub-input.d.ts +44 -0
  104. package/dist/types/components/kit/sidebar.d.ts +29 -7
  105. package/dist/types/components/kit/thinking.d.ts +28 -0
  106. package/dist/types/components/kit/tool-approval.d.ts +43 -0
  107. package/dist/types/components/kit/trace-waterfall.d.ts +44 -0
  108. package/dist/types/components/lab/blocks/AgentTeam.d.ts +2 -0
  109. package/dist/types/components/lab/blocks/AutonomyDial.d.ts +2 -0
  110. package/dist/types/components/lab/blocks/ChangeLedger.d.ts +2 -0
  111. package/dist/types/components/lab/blocks/FallbackChain.d.ts +2 -0
  112. package/dist/types/packages/asta-ui/src/agent-plan.d.ts +2 -0
  113. package/dist/types/packages/asta-ui/src/agent-team.d.ts +2 -0
  114. package/dist/types/packages/asta-ui/src/api-keys.d.ts +2 -0
  115. package/dist/types/packages/asta-ui/src/autonomy-dial.d.ts +2 -0
  116. package/dist/types/packages/asta-ui/src/base/agent-plan.d.ts +2 -0
  117. package/dist/types/packages/asta-ui/src/base/agent-team.d.ts +2 -0
  118. package/dist/types/packages/asta-ui/src/base/api-keys.d.ts +2 -0
  119. package/dist/types/packages/asta-ui/src/base/autonomy-dial.d.ts +2 -0
  120. package/dist/types/packages/asta-ui/src/base/change-ledger.d.ts +2 -0
  121. package/dist/types/packages/asta-ui/src/base/citations.d.ts +2 -0
  122. package/dist/types/packages/asta-ui/src/base/context-meter.d.ts +2 -0
  123. package/dist/types/packages/asta-ui/src/base/fallback-chain.d.ts +2 -0
  124. package/dist/types/packages/asta-ui/src/base/model-picker.d.ts +2 -0
  125. package/dist/types/packages/asta-ui/src/base/prompt-composer.d.ts +2 -0
  126. package/dist/types/packages/asta-ui/src/base/scrub-input.d.ts +2 -0
  127. package/dist/types/packages/asta-ui/src/base/thinking.d.ts +2 -0
  128. package/dist/types/packages/asta-ui/src/base/tool-approval.d.ts +2 -0
  129. package/dist/types/packages/asta-ui/src/base/trace-waterfall.d.ts +2 -0
  130. package/dist/types/packages/asta-ui/src/base.d.ts +26 -2
  131. package/dist/types/packages/asta-ui/src/change-ledger.d.ts +2 -0
  132. package/dist/types/packages/asta-ui/src/citations.d.ts +2 -0
  133. package/dist/types/packages/asta-ui/src/context-meter.d.ts +2 -0
  134. package/dist/types/packages/asta-ui/src/fallback-chain.d.ts +2 -0
  135. package/dist/types/packages/asta-ui/src/index.d.ts +26 -2
  136. package/dist/types/packages/asta-ui/src/model-picker.d.ts +2 -0
  137. package/dist/types/packages/asta-ui/src/prompt-composer.d.ts +2 -0
  138. package/dist/types/packages/asta-ui/src/scrub-input.d.ts +2 -0
  139. package/dist/types/packages/asta-ui/src/thinking.d.ts +2 -0
  140. package/dist/types/packages/asta-ui/src/tool-approval.d.ts +2 -0
  141. package/dist/types/packages/asta-ui/src/trace-waterfall.d.ts +2 -0
  142. package/package.json +1 -1
  143. package/dist/chunk-MVE7RJJ6.js +0 -322
  144. package/dist/chunk-TBMUSER2.js +0 -322
@@ -0,0 +1,48 @@
1
+ /* Controls: scrub input, API keys.
2
+ * Only what Tailwind classes can't express. Motion is transform and opacity only, with the kit's easing. */
3
+
4
+ @keyframes asta-scrub-tip { from { opacity: 0; transform: translateY(-3px) } }
5
+ @keyframes asta-ak-in { from { opacity: 0; transform: translateY(6px) } }
6
+ @keyframes asta-ak-count { from { transform: scaleX(1) } to { transform: scaleX(0) } }
7
+ @keyframes asta-ak-pulse { 50% { opacity: 0.45 } }
8
+
9
+ /* ---------- Scrub input: the error tip drops in ---------- */
10
+
11
+ .asta-scrub:has(> [aria-invalid="true"]) > .asta-scrub-tip {
12
+ animation: asta-scrub-tip 0.2s cubic-bezier(0.16, 1, 0.3, 1);
13
+ }
14
+
15
+ /* ---------- API keys: form, new key, confirm and undo rise in; the undo bar runs down ---------- */
16
+
17
+ .asta-api-keys-form:not([hidden]),
18
+ .asta-api-keys-reveal,
19
+ .asta-api-keys-confirm:not([hidden]),
20
+ .asta-api-keys-row[data-revoked] > .asta-api-keys-undo {
21
+ animation: asta-ak-in 0.3s cubic-bezier(0.16, 1, 0.3, 1);
22
+ }
23
+ .asta-api-keys-row[data-revoked] .asta-api-keys-countdown {
24
+ animation: asta-ak-count var(--asta-undo, 8s) linear forwards;
25
+ }
26
+ .asta-api-keys-skeleton {
27
+ animation: asta-ak-pulse 1.6s ease-in-out infinite;
28
+ }
29
+ .asta-api-keys-skeleton:nth-child(2) { animation-delay: 150ms }
30
+ .asta-api-keys-skeleton:nth-child(3) { animation-delay: 300ms }
31
+
32
+ @media (prefers-reduced-motion: reduce) {
33
+ .asta-scrub-input,
34
+ .asta-scrub-tip,
35
+ .asta-scrub-limit,
36
+ .asta-api-keys,
37
+ .asta-api-keys * {
38
+ animation-duration: 0s !important;
39
+ transition-duration: 0s !important;
40
+ }
41
+ .asta-api-keys-row[data-revoked] .asta-api-keys-countdown {
42
+ animation: none !important;
43
+ opacity: 0.5;
44
+ }
45
+ .asta-api-keys-skeleton {
46
+ animation: none !important;
47
+ }
48
+ }
@@ -0,0 +1,482 @@
1
+ /* Controls: scrub input, API keys.
2
+ * Event delegation on document, so markup swapped in by htmx works without re-init.
3
+ * Scrub [data-asta-scrub] (data-min, data-max, data-step, data-precision, data-unit): drag the
4
+ * [data-asta-scrub-handle] to scrub (Shift ×10, Alt ×0.1), or type numbers, units and sums
5
+ * ("1.5rem", "24*2", "+8") and press Enter. Arrows step. Sets data-scrubbing, data-mod, data-limit and
6
+ * aria-invalid, keeps a hidden input in sync and fires "asta:scrub" with detail.value on every change
7
+ * and "change" on the hidden input when a drag or edit ends
8
+ * API keys [data-asta-keys] (data-undo-ms): [data-asta-keys-create|cancel] open and close the form, which is
9
+ * checked on submit (a name, one scope at least). Without hx-post or action it makes a demo key and
10
+ * fires "asta:key-create". [data-asta-keys-dismiss] closes the one-time secret (warns once if it wasn't
11
+ * copied). [data-asta-keys-revoke|keep|confirm|undo] ask, revoke with an undo window, then fire
12
+ * "asta:key-revoke" with detail.id and remove the row
13
+ */
14
+ (() => {
15
+ if (window.astaControls) return;
16
+ window.astaControls = true;
17
+ const el = (e) => (e.target instanceof Element ? e.target : null);
18
+ const near = (e, sel) => el(e)?.closest(sel) ?? null;
19
+ const fire = (node, name, detail) => node.dispatchEvent(new CustomEvent(name, { bubbles: true, detail }));
20
+
21
+ /* ---------- Scrub input: the parser ---------- */
22
+
23
+ const UNITS = { px: ["len", 1], rem: ["len", 16], em: ["len", 16], pt: ["len", 4 / 3], deg: ["ang", 1], "°": ["ang", 1], rad: ["ang", 180 / Math.PI], turn: ["ang", 360], "%": ["pct", 1], ms: ["time", 1], s: ["time", 1000] };
24
+
25
+ function tokenize(src, unit) {
26
+ const out = [];
27
+ const base = unit ? UNITS[unit] : undefined;
28
+ const re = /\s*(?:(\d*\.?\d+(?:e[+-]?\d+)?)\s*([a-z%°]*)|([-+*/×÷()]))\s*/giy;
29
+ let i = 0;
30
+ while (i < src.length) {
31
+ re.lastIndex = i;
32
+ const m = re.exec(src);
33
+ if (!m) return null;
34
+ i = re.lastIndex;
35
+ if (m[3]) {
36
+ out.push({ op: m[3] === "×" ? "*" : m[3] === "÷" ? "/" : m[3] });
37
+ continue;
38
+ }
39
+ let v = Number(m[1]);
40
+ const u = m[2].toLowerCase();
41
+ if (u && u !== unit) {
42
+ const typed = UNITS[u];
43
+ if (!typed || !base || typed[0] !== base[0]) return null;
44
+ v = (v * typed[1]) / base[1];
45
+ }
46
+ out.push({ num: v });
47
+ }
48
+ return out;
49
+ }
50
+
51
+ // Numbers with units, + − × ÷ and brackets, without eval. A leading +, * or / works on the current value.
52
+ function parse(text, current, unit) {
53
+ let src = text.trim().replace(/,/g, ".").replace(/−/g, "-");
54
+ if (!src || src.length > 64) return null;
55
+ let rel = null;
56
+ if (/^[+*/×÷]/.test(src)) {
57
+ rel = src[0] === "×" ? "*" : src[0] === "÷" ? "/" : src[0];
58
+ src = src.slice(1);
59
+ }
60
+ const tk = tokenize(src, unit);
61
+ if (!tk?.length) return null;
62
+ let p = 0;
63
+ const isOp = (...ops) => tk[p] && ops.includes(tk[p].op);
64
+ const factor = () => {
65
+ const t = tk[p++];
66
+ if (!t) return NaN;
67
+ if (t.num !== undefined) return t.num;
68
+ if (t.op === "-") return -factor();
69
+ if (t.op === "+") return factor();
70
+ if (t.op === "(") {
71
+ const v = expr();
72
+ return tk[p++]?.op === ")" ? v : NaN;
73
+ }
74
+ return NaN;
75
+ };
76
+ const term = () => {
77
+ let v = factor();
78
+ while (isOp("*", "/")) v = tk[p++].op === "*" ? v * factor() : v / factor();
79
+ return v;
80
+ };
81
+ const expr = () => {
82
+ let v = term();
83
+ while (isOp("+", "-")) v = tk[p++].op === "+" ? v + term() : v - term();
84
+ return v;
85
+ };
86
+ let v = expr();
87
+ if (p !== tk.length || !Number.isFinite(v)) return null;
88
+ if (rel === "+") v = current + v;
89
+ else if (rel === "*") v = current * v;
90
+ else if (rel === "/") v = v === 0 ? NaN : current / v;
91
+ return Number.isFinite(v) ? v : null;
92
+ }
93
+ window.astaParseScrub = parse;
94
+
95
+ /* ---------- Scrub input: the field ---------- */
96
+
97
+ const num = (s, d) => (s == null || s === "" || Number.isNaN(Number(s)) ? d : Number(s));
98
+ function cfg(root) {
99
+ const d = root.dataset;
100
+ return { min: num(d.min, -Infinity), max: num(d.max, Infinity), step: num(d.step, 1), precision: num(d.precision, 2), unit: d.unit || undefined };
101
+ }
102
+ const round = (v, p) => Math.round(v * 10 ** p) / 10 ** p;
103
+ const fmt = (v, p) => String(round(v, p)).replace(/^-0$/, "0");
104
+ const field = (root) => root.querySelector("input:not([type=hidden])");
105
+ function valueOf(root) {
106
+ if (typeof root._v !== "number") {
107
+ const c = cfg(root);
108
+ root._v = parse(field(root)?.value ?? "0", 0, c.unit) ?? 0;
109
+ }
110
+ return root._v;
111
+ }
112
+
113
+ function flagLimit(root, which) {
114
+ const c = cfg(root);
115
+ root.setAttribute("data-limit", which);
116
+ clearTimeout(root._lt);
117
+ root._lt = setTimeout(() => root.removeAttribute("data-limit"), 420);
118
+ const status = root.querySelector("[data-asta-scrub-status]");
119
+ if (status) status.textContent = `${which === "max" ? "Maximum" : "Minimum"} is ${fmt(which === "max" ? c.max : c.min, c.precision)}${c.unit || ""}`;
120
+ }
121
+
122
+ // Clamps, writes the field and the hidden input, and flags the edge when a value had to be held back.
123
+ function set(root, want) {
124
+ const c = cfg(root);
125
+ const input = field(root);
126
+ let v = round(want, c.precision);
127
+ if (v > c.max) {
128
+ v = c.max;
129
+ flagLimit(root, "max");
130
+ } else if (v < c.min) {
131
+ v = c.min;
132
+ flagLimit(root, "min");
133
+ }
134
+ const changed = v !== root._v;
135
+ root._v = v;
136
+ if (input) {
137
+ input.value = fmt(v, c.precision);
138
+ input.setAttribute("aria-valuenow", String(v));
139
+ input.setAttribute("aria-valuetext", `${fmt(v, c.precision)}${c.unit ? ` ${c.unit}` : ""}`);
140
+ input.removeAttribute("aria-invalid");
141
+ }
142
+ const hidden = root.querySelector("input[type=hidden]");
143
+ if (hidden) hidden.value = String(v);
144
+ if (changed) fire(root, "asta:scrub", { value: v });
145
+ return v;
146
+ }
147
+ const settle = (root) => root.querySelector("input[type=hidden]")?.dispatchEvent(new Event("change", { bubbles: true }));
148
+
149
+ function commit(root, keepFocus) {
150
+ const input = field(root);
151
+ if (!input) return;
152
+ const c = cfg(root);
153
+ const cur = valueOf(root);
154
+ const v = parse(input.value, cur, c.unit);
155
+ if (v === null) {
156
+ if (keepFocus) {
157
+ input.setAttribute("aria-invalid", "true");
158
+ // Keep the tip inside the panel: align it right when it would run past the edge.
159
+ const box = root.closest("[role=group],section,form") ?? document.body;
160
+ root.toggleAttribute("data-tip-end", root.getBoundingClientRect().left + 220 > box.getBoundingClientRect().right);
161
+ return;
162
+ }
163
+ input.value = fmt(cur, c.precision);
164
+ input.removeAttribute("aria-invalid");
165
+ const status = root.querySelector("[data-asta-scrub-status]");
166
+ if (status) status.textContent = `Kept ${fmt(cur, c.precision)}${c.unit || ""}`;
167
+ return;
168
+ }
169
+ set(root, v);
170
+ settle(root);
171
+ if (keepFocus) input.select();
172
+ }
173
+
174
+ let drag = null;
175
+ document.addEventListener("pointerdown", (e) => {
176
+ const handle = near(e, "[data-asta-scrub-handle]");
177
+ const root = handle?.closest("[data-asta-scrub]");
178
+ if (!root || field(root)?.disabled || (e.pointerType === "mouse" && e.button !== 0)) return;
179
+ e.preventDefault();
180
+ try {
181
+ handle.setPointerCapture(e.pointerId);
182
+ } catch {}
183
+ drag = { root, handle, id: e.pointerId, x0: e.clientX, x: e.clientX, raw: valueOf(root), moved: false };
184
+ });
185
+ document.addEventListener("pointermove", (e) => {
186
+ if (!drag || e.pointerId !== drag.id) return;
187
+ const { root } = drag;
188
+ if (!drag.moved) {
189
+ if (Math.abs(e.clientX - drag.x0) < 3) return;
190
+ drag.moved = true;
191
+ root.setAttribute("data-scrubbing", "");
192
+ document.documentElement.style.cursor = "ew-resize";
193
+ }
194
+ const c = cfg(root);
195
+ const f = e.shiftKey ? 10 : e.altKey ? 0.1 : 1;
196
+ const badge = root.querySelector(".asta-scrub-badge");
197
+ if (f === 1) root.removeAttribute("data-mod");
198
+ else {
199
+ root.setAttribute("data-mod", String(f));
200
+ if (badge) badge.textContent = `×${f}`;
201
+ }
202
+ // Half a step per pixel, snapped to the step so a drag never leaves 540.5 behind.
203
+ const grid = c.step * f;
204
+ const want = drag.raw + ((e.clientX - drag.x) / 2) * grid;
205
+ drag.x = e.clientX;
206
+ drag.raw = Math.min(c.max, Math.max(c.min, want));
207
+ if (want > c.max) flagLimit(root, "max");
208
+ else if (want < c.min) flagLimit(root, "min");
209
+ set(root, Math.round(drag.raw / grid) * grid);
210
+ });
211
+ function endDrag(e) {
212
+ if (!drag || e.pointerId !== drag.id) return;
213
+ const { root, moved } = drag;
214
+ drag = null;
215
+ root.removeAttribute("data-scrubbing");
216
+ root.removeAttribute("data-mod");
217
+ document.documentElement.style.cursor = "";
218
+ if (moved) settle(root);
219
+ else {
220
+ const input = field(root);
221
+ input?.focus();
222
+ input?.select();
223
+ }
224
+ }
225
+ document.addEventListener("pointerup", endDrag);
226
+ document.addEventListener("pointercancel", endDrag);
227
+
228
+ document.addEventListener("focusin", (e) => {
229
+ if (e.target instanceof HTMLInputElement && e.target.matches(".asta-api-keys-secret > input")) return e.target.select();
230
+ const root = near(e, "[data-asta-scrub]");
231
+ if (root && e.target === field(root)) {
232
+ valueOf(root);
233
+ e.target.select();
234
+ }
235
+ });
236
+ document.addEventListener("focusout", (e) => {
237
+ const root = near(e, "[data-asta-scrub]");
238
+ if (root && e.target === field(root)) commit(root, false);
239
+ });
240
+ document.addEventListener("input", (e) => {
241
+ const root = near(e, "[data-asta-scrub]");
242
+ if (root && e.target === field(root)) e.target.removeAttribute("aria-invalid");
243
+ });
244
+
245
+ /* ---------- API keys ---------- */
246
+
247
+ const keysRoot = (e) => near(e, "[data-asta-keys]");
248
+ const focusSoon = (node) => node && requestAnimationFrame(() => node.focus());
249
+ const formOf = (root) => root.querySelector("[data-asta-keys-form]");
250
+ const createBtn = (root) => root.querySelector("[data-asta-keys-create]");
251
+
252
+ function openForm(root) {
253
+ const form = formOf(root);
254
+ if (!form) return;
255
+ form.hidden = false;
256
+ createBtn(root)?.setAttribute("aria-expanded", "true");
257
+ focusSoon(form.querySelector("input[name=name]"));
258
+ }
259
+ function closeForm(root, focus = true) {
260
+ const form = formOf(root);
261
+ if (!form) return;
262
+ form.hidden = true;
263
+ form.reset();
264
+ form.querySelectorAll("[aria-invalid]").forEach((n) => n.removeAttribute("aria-invalid"));
265
+ form.querySelectorAll(".asta-api-keys-msg").forEach((m) => showMsg(m, null));
266
+ form.querySelector("[data-asta-keys-alert]")?.setAttribute("hidden", "");
267
+ createBtn(root)?.setAttribute("aria-expanded", "false");
268
+ if (focus) focusSoon(createBtn(root));
269
+ }
270
+ // A hint line keeps its hint in data-hint and its error in data-error-text.
271
+ function showMsg(msg, on) {
272
+ const text = msg.querySelector("span:last-child") ?? msg;
273
+ msg.toggleAttribute("data-error", !!on);
274
+ text.textContent = on ? msg.dataset.errorText : msg.dataset.hint;
275
+ }
276
+ function check(form) {
277
+ const name = form.querySelector("input[name=name]");
278
+ const scopes = [...form.querySelectorAll("input[name=scopes]")];
279
+ const noName = !name?.value.trim();
280
+ const noScope = scopes.length > 0 && !scopes.some((s) => s.checked);
281
+ if (name && noName) name.setAttribute("aria-invalid", "true");
282
+ else name?.removeAttribute("aria-invalid");
283
+ const nameMsg = name && document.getElementById(name.getAttribute("aria-describedby"));
284
+ if (nameMsg) showMsg(nameMsg, noName);
285
+ const fs = form.querySelector(".asta-api-keys-scopes");
286
+ const scopeMsg = fs && document.getElementById(fs.getAttribute("aria-describedby"));
287
+ if (scopeMsg) showMsg(scopeMsg, noScope);
288
+ if (noName) name.focus();
289
+ else if (noScope) scopes[0].focus();
290
+ return !noName && !noScope;
291
+ }
292
+
293
+ const ALPHABET = "abcdefghjkmnpqrstuvwxyz23456789";
294
+ const tail = () => Array.from({ length: 24 }, () => ALPHABET[Math.floor(Math.random() * ALPHABET.length)]).join("");
295
+
296
+ // Demo create: fill the reveal row with a key that is clearly marked demo and put it on top.
297
+ function demoCreate(root, form) {
298
+ const list = root.querySelector(".asta-api-keys-list");
299
+ const tpl = root.querySelector("template[data-asta-keys-reveal]");
300
+ if (!list || !tpl) return;
301
+ const name = form.querySelector("input[name=name]").value.trim();
302
+ const scopes = [...form.querySelectorAll("input[name=scopes]:checked")].map((s) => s.value);
303
+ const secret = `ak_live_demo_${tail()}`;
304
+ const li = tpl.content.firstElementChild.cloneNode(true);
305
+ li.dataset.name = name;
306
+ li.dataset.scopes = scopes.join(",");
307
+ li.dataset.secret = secret;
308
+ li.querySelectorAll("[data-slot=name]").forEach((n) => (n.textContent = name));
309
+ const input = li.querySelector("input[readonly]");
310
+ if (input) {
311
+ input.value = secret;
312
+ input.setAttribute("aria-label", `Secret for ${name}`);
313
+ }
314
+ li.querySelector("[data-asta-copy]")?.setAttribute("data-asta-copy", secret);
315
+ list.prepend(li);
316
+ closeForm(root, false);
317
+ fire(root, "asta:key-create", { name, scopes, expires: form.querySelector("select")?.value ?? null });
318
+ focusSoon(li.querySelector("[data-asta-copy]"));
319
+ }
320
+
321
+ // After the secret is dismissed, the key joins the list masked, built from the row template.
322
+ function rowFromReveal(root, reveal) {
323
+ const tpl = root.querySelector("template[data-asta-keys-row]");
324
+ if (!tpl || !reveal.dataset.secret) return null;
325
+ const li = tpl.content.firstElementChild.cloneNode(true);
326
+ const name = reveal.dataset.name;
327
+ const masked = `ak_live_…${reveal.dataset.secret.slice(-4)}`;
328
+ const scopes = (reveal.dataset.scopes || "").split(",").filter(Boolean);
329
+ li.dataset.id = `k${Date.now().toString(36)}`;
330
+ li.dataset.name = name;
331
+ li.dataset.key = masked;
332
+ li.querySelectorAll("[data-slot=name]").forEach((n) => (n.textContent = name));
333
+ li.querySelectorAll("[data-slot=key]").forEach((n) => (n.textContent = masked));
334
+ li.querySelectorAll("[data-slot=created]").forEach((n) => (n.textContent = `Created ${new Date().toLocaleDateString("en-US", { month: "short", day: "numeric" })}`));
335
+ const chips = li.querySelector("[data-slot=scopes]");
336
+ if (chips) {
337
+ chips.textContent = "";
338
+ const code = document.createElement("code");
339
+ code.textContent = scopes[0] ?? "";
340
+ chips.append(code);
341
+ if (scopes.length > 1) {
342
+ const more = document.createElement("span");
343
+ more.title = scopes.slice(1).join(", ");
344
+ more.textContent = `+${scopes.length - 1}`;
345
+ chips.append(more);
346
+ }
347
+ }
348
+ li.querySelector("[data-asta-keys-revoke]")?.setAttribute("aria-label", `Revoke ${name}`);
349
+ return li;
350
+ }
351
+
352
+ function dismiss(root, reveal) {
353
+ const note = reveal.querySelector(".asta-api-keys-note");
354
+ if (!reveal.hasAttribute("data-copied") && note && !note.hasAttribute("data-warn")) {
355
+ note.setAttribute("data-warn", "");
356
+ return;
357
+ }
358
+ const row = rowFromReveal(root, reveal);
359
+ if (row) reveal.replaceWith(row);
360
+ else reveal.remove();
361
+ fire(root, "asta:key-dismiss");
362
+ focusSoon(root.querySelector("[data-asta-keys-heading]") ?? createBtn(root));
363
+ }
364
+
365
+ const timers = new WeakMap();
366
+ function askRevoke(row, open) {
367
+ const confirm = row.querySelector(".asta-api-keys-confirm");
368
+ if (!confirm) return;
369
+ confirm.hidden = !open;
370
+ const btn = row.querySelector("[data-asta-keys-revoke]");
371
+ btn?.setAttribute("aria-expanded", String(open));
372
+ focusSoon(open ? confirm.querySelector("[data-asta-keys-keep]") : btn);
373
+ }
374
+ function revoke(root, row) {
375
+ const ms = Number(root.dataset.undoMs) || 8000;
376
+ row.querySelector(".asta-api-keys-confirm")?.setAttribute("hidden", "");
377
+ row.querySelector("[data-asta-keys-revoke]")?.setAttribute("aria-expanded", "false");
378
+ row.style.setProperty("--asta-undo", `${ms}ms`);
379
+ row.setAttribute("data-revoked", "");
380
+ focusSoon(row.querySelector("[data-asta-keys-undo]"));
381
+ timers.set(
382
+ row,
383
+ setTimeout(() => {
384
+ timers.delete(row);
385
+ const hadFocus = row.contains(document.activeElement);
386
+ const next = row.nextElementSibling ?? row.previousElementSibling;
387
+ fire(row, "asta:key-revoke", { id: row.dataset.id });
388
+ row.remove();
389
+ if (hadFocus) focusSoon(next?.querySelector("[data-asta-keys-revoke],[data-asta-copy]") ?? createBtn(root));
390
+ }, ms),
391
+ );
392
+ }
393
+ function undo(row) {
394
+ clearTimeout(timers.get(row));
395
+ timers.delete(row);
396
+ row.removeAttribute("data-revoked");
397
+ fire(row, "asta:key-restore", { id: row.dataset.id });
398
+ focusSoon(row.querySelector("[data-asta-keys-revoke]"));
399
+ }
400
+
401
+ document.addEventListener("click", (e) => {
402
+ const root = keysRoot(e);
403
+ if (!root) return;
404
+ const row = near(e, ".asta-api-keys-row");
405
+ if (near(e, "[data-asta-keys-create]")) {
406
+ const form = formOf(root);
407
+ if (form && !form.hidden) form.querySelector("input[name=name]")?.focus();
408
+ else openForm(root);
409
+ } else if (near(e, "[data-asta-keys-cancel]")) closeForm(root);
410
+ else if (near(e, "[data-asta-keys-dismiss]")) dismiss(root, near(e, ".asta-api-keys-reveal"));
411
+ else if (row && near(e, "[data-asta-keys-revoke]")) askRevoke(row, row.querySelector(".asta-api-keys-confirm")?.hidden ?? false);
412
+ else if (row && near(e, "[data-asta-keys-keep]")) askRevoke(row, false);
413
+ else if (row && near(e, "[data-asta-keys-confirm]")) revoke(root, row);
414
+ else if (row && near(e, "[data-asta-keys-undo]")) undo(row);
415
+ });
416
+
417
+ // A copy inside the reveal row counts as saved.
418
+ document.addEventListener("asta:copy", (e) => {
419
+ const reveal = near(e, ".asta-api-keys-reveal");
420
+ if (!reveal) return;
421
+ reveal.setAttribute("data-copied", "");
422
+ reveal.querySelector(".asta-api-keys-note")?.removeAttribute("data-warn");
423
+ });
424
+
425
+ // Capture phase, so an invalid form never reaches htmx or the browser.
426
+ document.addEventListener(
427
+ "submit",
428
+ (e) => {
429
+ const form = e.target instanceof HTMLFormElement && e.target.matches("[data-asta-keys-form]") ? e.target : null;
430
+ const root = form?.closest("[data-asta-keys]");
431
+ if (!root) return;
432
+ if (!check(form)) {
433
+ e.preventDefault();
434
+ e.stopPropagation();
435
+ return;
436
+ }
437
+ const remote = form.hasAttribute("action") || [...form.attributes].some((a) => /^(hx|data-hx)-(post|put)$/.test(a.name));
438
+ if (!remote) {
439
+ e.preventDefault();
440
+ demoCreate(root, form);
441
+ }
442
+ },
443
+ true,
444
+ );
445
+
446
+ document.addEventListener("keydown", (e) => {
447
+ const root = keysRoot(e);
448
+ if (root) {
449
+ if (e.key !== "Escape") return;
450
+ if (near(e, "[data-asta-keys-form]")) {
451
+ e.preventDefault();
452
+ closeForm(root);
453
+ } else if (near(e, ".asta-api-keys-confirm")) {
454
+ e.preventDefault();
455
+ askRevoke(near(e, ".asta-api-keys-row"), false);
456
+ }
457
+ return;
458
+ }
459
+ const sroot = near(e, "[data-asta-scrub]");
460
+ if (!sroot || e.target !== field(sroot)) return;
461
+ const c = cfg(sroot);
462
+ const input = e.target;
463
+ if (e.key === "Enter") {
464
+ e.preventDefault();
465
+ commit(sroot, true);
466
+ } else if (e.key === "Escape") {
467
+ e.preventDefault();
468
+ input.value = fmt(valueOf(sroot), c.precision);
469
+ input.removeAttribute("aria-invalid");
470
+ input.select();
471
+ } else if (e.key === "ArrowUp" || e.key === "ArrowDown" || e.key === "PageUp" || e.key === "PageDown") {
472
+ e.preventDefault();
473
+ const page = e.key.startsWith("Page");
474
+ const f = page ? 10 : e.shiftKey ? 10 : e.altKey ? 0.1 : 1;
475
+ const dir = e.key === "ArrowUp" || e.key === "PageUp" ? 1 : -1;
476
+ const typed = parse(input.value, valueOf(sroot), c.unit);
477
+ set(sroot, (typed ?? valueOf(sroot)) + dir * c.step * f);
478
+ settle(sroot);
479
+ input.select();
480
+ }
481
+ });
482
+ })();