ochreui 0.0.0-stage → 0.0.2

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 (168) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +153 -2
  3. package/cli/ochreui.mjs +601 -0
  4. package/mcp/core.mjs +126 -0
  5. package/mcp/docs.json +906 -0
  6. package/mcp/server.mjs +23 -0
  7. package/package.json +393 -3
  8. package/registry/agent-team.ts +350 -0
  9. package/registry/avatar.ts +300 -0
  10. package/registry/badge.ts +207 -0
  11. package/registry/breadcrumb.ts +435 -0
  12. package/registry/button.ts +238 -0
  13. package/registry/card.ts +221 -0
  14. package/registry/change-summary.ts +200 -0
  15. package/registry/checkbox.ts +204 -0
  16. package/registry/code-block.ts +332 -0
  17. package/registry/color-picker.ts +720 -0
  18. package/registry/comment-thread.ts +503 -0
  19. package/registry/context-menu.ts +281 -0
  20. package/registry/cover-flow.ts +623 -0
  21. package/registry/date-picker.ts +799 -0
  22. package/registry/decision-inbox.ts +340 -0
  23. package/registry/dialog.ts +577 -0
  24. package/registry/dropdown-menu.ts +864 -0
  25. package/registry/email-draft.ts +916 -0
  26. package/registry/empty-calendar.ts +199 -0
  27. package/registry/empty-state.ts +164 -0
  28. package/registry/enrichment-orbit.ts +164 -0
  29. package/registry/filter-menu.ts +541 -0
  30. package/registry/form-field.ts +308 -0
  31. package/registry/frost-cloud.ts +381 -0
  32. package/registry/ghosted-chat.ts +210 -0
  33. package/registry/glow-cloud.ts +402 -0
  34. package/registry/grid-background.ts +164 -0
  35. package/registry/hover-card.ts +401 -0
  36. package/registry/icon-button.ts +133 -0
  37. package/registry/index.json +2124 -0
  38. package/registry/integration-beam.ts +588 -0
  39. package/registry/integration-grid.ts +209 -0
  40. package/registry/kbd.ts +176 -0
  41. package/registry/layer-stack.ts +351 -0
  42. package/registry/lead-sourcing.ts +285 -0
  43. package/registry/logo-cluster.ts +239 -0
  44. package/registry/logo-marquee.ts +505 -0
  45. package/registry/message-actions.ts +833 -0
  46. package/registry/model-orbit.ts +583 -0
  47. package/registry/model-wheel.ts +372 -0
  48. package/registry/notification-stack.ts +301 -0
  49. package/registry/number-input.ts +552 -0
  50. package/registry/number-ticker.ts +261 -0
  51. package/registry/ochre.css +917 -0
  52. package/registry/otp-input.ts +704 -0
  53. package/registry/overlay.ts +312 -0
  54. package/registry/pagination.ts +362 -0
  55. package/registry/popover.ts +296 -0
  56. package/registry/profile-menu.ts +332 -0
  57. package/registry/progress-tracker.ts +291 -0
  58. package/registry/rating.ts +461 -0
  59. package/registry/reaction-bar.ts +322 -0
  60. package/registry/region-map.ts +537 -0
  61. package/registry/resizable.ts +489 -0
  62. package/registry/scroll-area.ts +271 -0
  63. package/registry/segmented-control.ts +433 -0
  64. package/registry/select.ts +769 -0
  65. package/registry/separator.ts +97 -0
  66. package/registry/shimmer-loader.ts +211 -0
  67. package/registry/side-folder.ts +152 -0
  68. package/registry/skeleton.ts +136 -0
  69. package/registry/skill/INSTRUCTIONS.md +11 -0
  70. package/registry/skill/SKILL.md +65 -0
  71. package/registry/skill/checklist.md +45 -0
  72. package/registry/skill/references/agent-team.md +105 -0
  73. package/registry/skill/references/avatar.md +88 -0
  74. package/registry/skill/references/badge.md +92 -0
  75. package/registry/skill/references/breadcrumb.md +99 -0
  76. package/registry/skill/references/button.md +82 -0
  77. package/registry/skill/references/card.md +111 -0
  78. package/registry/skill/references/catalog.md +114 -0
  79. package/registry/skill/references/change-summary.md +103 -0
  80. package/registry/skill/references/checkbox.md +105 -0
  81. package/registry/skill/references/code-block.md +109 -0
  82. package/registry/skill/references/color-picker.md +138 -0
  83. package/registry/skill/references/comment-thread.md +164 -0
  84. package/registry/skill/references/context-menu.md +205 -0
  85. package/registry/skill/references/cover-flow.md +128 -0
  86. package/registry/skill/references/date-picker.md +154 -0
  87. package/registry/skill/references/decision-inbox.md +126 -0
  88. package/registry/skill/references/dialog.md +155 -0
  89. package/registry/skill/references/dropdown-menu.md +144 -0
  90. package/registry/skill/references/email-draft.md +182 -0
  91. package/registry/skill/references/empty-calendar.md +74 -0
  92. package/registry/skill/references/empty-state.md +118 -0
  93. package/registry/skill/references/enrichment-orbit.md +71 -0
  94. package/registry/skill/references/filter-menu.md +143 -0
  95. package/registry/skill/references/form-field.md +118 -0
  96. package/registry/skill/references/frost-cloud.md +80 -0
  97. package/registry/skill/references/ghosted-chat.md +75 -0
  98. package/registry/skill/references/glow-cloud.md +84 -0
  99. package/registry/skill/references/grid-background.md +76 -0
  100. package/registry/skill/references/hover-card.md +125 -0
  101. package/registry/skill/references/icon-button.md +99 -0
  102. package/registry/skill/references/integration-beam.md +107 -0
  103. package/registry/skill/references/integration-grid.md +100 -0
  104. package/registry/skill/references/kbd.md +65 -0
  105. package/registry/skill/references/layer-stack.md +115 -0
  106. package/registry/skill/references/lead-sourcing.md +92 -0
  107. package/registry/skill/references/logo-cluster.md +92 -0
  108. package/registry/skill/references/logo-marquee.md +123 -0
  109. package/registry/skill/references/message-actions.md +146 -0
  110. package/registry/skill/references/model-orbit.md +106 -0
  111. package/registry/skill/references/model-wheel.md +84 -0
  112. package/registry/skill/references/notification-stack.md +138 -0
  113. package/registry/skill/references/number-input.md +117 -0
  114. package/registry/skill/references/number-ticker.md +73 -0
  115. package/registry/skill/references/otp-input.md +142 -0
  116. package/registry/skill/references/pagination.md +112 -0
  117. package/registry/skill/references/popover.md +143 -0
  118. package/registry/skill/references/profile-menu.md +123 -0
  119. package/registry/skill/references/progress-tracker.md +122 -0
  120. package/registry/skill/references/rating.md +111 -0
  121. package/registry/skill/references/reaction-bar.md +128 -0
  122. package/registry/skill/references/region-map.md +109 -0
  123. package/registry/skill/references/resizable.md +127 -0
  124. package/registry/skill/references/scroll-area.md +113 -0
  125. package/registry/skill/references/segmented-control.md +96 -0
  126. package/registry/skill/references/select.md +122 -0
  127. package/registry/skill/references/separator.md +69 -0
  128. package/registry/skill/references/shimmer-loader.md +83 -0
  129. package/registry/skill/references/side-folder.md +75 -0
  130. package/registry/skill/references/skeleton.md +95 -0
  131. package/registry/skill/references/smart-recommendation.md +132 -0
  132. package/registry/skill/references/spinner.md +84 -0
  133. package/registry/skill/references/switch.md +79 -0
  134. package/registry/skill/references/tab-overload.md +70 -0
  135. package/registry/skill/references/tag-input.md +126 -0
  136. package/registry/skill/references/team-invite.md +94 -0
  137. package/registry/skill/references/testimonial-wall.md +110 -0
  138. package/registry/skill/references/text-area.md +115 -0
  139. package/registry/skill/references/text-field.md +126 -0
  140. package/registry/skill/references/text-reveal.md +71 -0
  141. package/registry/skill/references/thinking-loader.md +87 -0
  142. package/registry/skill/references/time-picker.md +122 -0
  143. package/registry/skill/references/tips-panel.md +115 -0
  144. package/registry/skill/references/toggle-group.md +98 -0
  145. package/registry/skill/references/tool-call-card.md +151 -0
  146. package/registry/skill/references/tooltip.md +94 -0
  147. package/registry/skill/references/tree-view.md +172 -0
  148. package/registry/skill/theming.md +57 -0
  149. package/registry/smart-recommendation.ts +415 -0
  150. package/registry/spinner.ts +67 -0
  151. package/registry/switch.ts +146 -0
  152. package/registry/tab-overload.ts +211 -0
  153. package/registry/tag-input.ts +620 -0
  154. package/registry/team-invite.ts +248 -0
  155. package/registry/testimonial-wall.ts +521 -0
  156. package/registry/text-area.ts +432 -0
  157. package/registry/text-field.ts +415 -0
  158. package/registry/text-reveal.ts +175 -0
  159. package/registry/theme.ts +68 -0
  160. package/registry/thinking-loader.ts +302 -0
  161. package/registry/time-picker.ts +828 -0
  162. package/registry/tips-panel.ts +312 -0
  163. package/registry/toggle-group.ts +357 -0
  164. package/registry/tool-call-card.ts +578 -0
  165. package/registry/tooltip.ts +486 -0
  166. package/registry/tree-view.ts +454 -0
  167. package/styles/base.css +500 -0
  168. package/styles/tokens.css +415 -0
@@ -0,0 +1,833 @@
1
+ // Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
2
+ /**
3
+ * Message Actions, from Ochre (https://ochreui.dev).
4
+ * Needs ochre.css (the --ui-* tokens and shared classes), which `npx ochreui init` adds once.
5
+ *
6
+ * <article uiMessage>
7
+ * <p uiMessageText [version]="v()">{{ answers[v()] }}</p>
8
+ * <ui-message-actions [text]="answers[v()]" [versions]="answers" [(version)]="v" (regenerate)="retry()" (feedback)="save($event)" />
9
+ * </article>
10
+ * The quiet row under an assistant's answer: copy, regenerate with versions, thumbs up and down, read aloud.
11
+ * `uiMessage` lights the bar while the message is hovered or focused.
12
+ *
13
+ * Public styling hooks (set them on the element or any ancestor):
14
+ * --ui-message-actions-quiet bar opacity at rest (default 0.6)
15
+ * --ui-message-actions-size button size (default 28px, Classic 30px, touch 36px)
16
+ * --ui-message-actions-bars read-aloud equalizer color (default --ui-info)
17
+ */
18
+ import {
19
+ Component,
20
+ DOCUMENT,
21
+ DestroyRef,
22
+ Directive,
23
+ ElementRef,
24
+ afterNextRender,
25
+ afterRenderEffect,
26
+ booleanAttribute,
27
+ computed,
28
+ effect,
29
+ inject,
30
+ input,
31
+ isDevMode,
32
+ model,
33
+ numberAttribute,
34
+ output,
35
+ signal,
36
+ untracked,
37
+ viewChild,
38
+ } from '@angular/core';
39
+
40
+ export type MessageActionKey = 'copy' | 'regenerate' | 'feedback' | 'speak';
41
+ export type MessageVote = 'up' | 'down' | null;
42
+
43
+ export interface MessageFeedback {
44
+ kind: MessageVote;
45
+ /** Set when the feedback row is sent: the picked reason, or the words typed under the last one. */
46
+ reason?: string;
47
+ }
48
+
49
+ export interface MessageActionsLabels {
50
+ bar: string;
51
+ copy: string;
52
+ copyTip: string;
53
+ copied: string;
54
+ copyFailed: string;
55
+ regenerate: string;
56
+ regenerateTip: string;
57
+ writing: string;
58
+ regenerating: string;
59
+ versions: string;
60
+ previous: string;
61
+ next: string;
62
+ versionOf: (n: number, total: number) => string;
63
+ good: string;
64
+ bad: string;
65
+ thanks: string;
66
+ markedGood: string;
67
+ markedBad: string;
68
+ ask: string;
69
+ close: string;
70
+ send: string;
71
+ sent: string;
72
+ otherPlaceholder: string;
73
+ otherLabel: string;
74
+ read: string;
75
+ stop: string;
76
+ stopTip: string;
77
+ reading: string;
78
+ finished: string;
79
+ stopped: string;
80
+ }
81
+
82
+ const LABELS: MessageActionsLabels = {
83
+ bar: 'Message actions',
84
+ copy: 'Copy message',
85
+ copyTip: 'Copy',
86
+ copied: 'Copied',
87
+ copyFailed: "Couldn't copy",
88
+ regenerate: 'Regenerate response',
89
+ regenerateTip: 'Regenerate',
90
+ writing: 'Writing…',
91
+ regenerating: 'Regenerating',
92
+ versions: 'Versions',
93
+ previous: 'Previous version',
94
+ next: 'Next version',
95
+ versionOf: (n, total) => `Version ${n} of ${total}`,
96
+ good: 'Good response',
97
+ bad: 'Bad response',
98
+ thanks: 'Thanks!',
99
+ markedGood: 'Marked as good response',
100
+ markedBad: 'Marked as bad response. Tell us what went wrong below',
101
+ ask: 'What went wrong?',
102
+ close: 'Close feedback',
103
+ send: 'Send',
104
+ sent: 'Thanks for the feedback',
105
+ otherPlaceholder: 'Tell us more (optional)',
106
+ otherLabel: 'What went wrong, in your words',
107
+ read: 'Read aloud',
108
+ stop: 'Stop reading',
109
+ stopTip: 'Stop',
110
+ reading: 'Reading aloud',
111
+ finished: 'Finished reading',
112
+ stopped: 'Stopped reading',
113
+ };
114
+
115
+ let nextId = 0;
116
+
117
+ /** Put it on the element that wraps one assistant message. Hovering or focusing anything inside lights the bar. */
118
+ @Directive({
119
+ selector: '[uiMessage]',
120
+ host: {
121
+ 'data-ui-message': '',
122
+ '(pointerenter)': 'over.set(true)',
123
+ '(pointerleave)': 'over.set(false)',
124
+ '(focusin)': 'focused.set(true)',
125
+ '(focusout)': 'left($event)',
126
+ },
127
+ })
128
+ export class UiMessage {
129
+ private readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
130
+ protected readonly over = signal(false);
131
+ protected readonly focused = signal(false);
132
+ readonly hot = computed(() => this.over() || this.focused());
133
+
134
+ protected left(e: FocusEvent): void {
135
+ if (!this.el.contains(e.relatedTarget as Node | null)) this.focused.set(false);
136
+ }
137
+ }
138
+
139
+ /** Put it on the message body. When `version` changes, the new text sharpens in and the height eases. */
140
+ @Directive({
141
+ selector: '[uiMessageText]',
142
+ host: { '[style.color]': "busy() ? 'var(--ui-text-muted)' : null" },
143
+ })
144
+ export class UiMessageText {
145
+ /** Any value that changes with the shown version, usually its index. */
146
+ readonly version = input<unknown>();
147
+ /** Mutes the text and pulses it slowly while a new version is written. */
148
+ readonly busy = input(false, { transform: booleanAttribute });
149
+
150
+ constructor() {
151
+ const el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
152
+ const view = inject(DOCUMENT).defaultView;
153
+ const NONE = {};
154
+ let last: unknown = NONE;
155
+ let height = 0;
156
+ let pulse: Animation | undefined;
157
+ const motion = () => {
158
+ const css = view?.getComputedStyle(el);
159
+ return { scale: Number(css?.getPropertyValue('--ui-motion-scale') || 1), easing: css?.getPropertyValue('--ui-ease-out').trim() || 'ease-out' };
160
+ };
161
+ const watch = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(() => (height = el.offsetHeight));
162
+ watch?.observe(el);
163
+ inject(DestroyRef).onDestroy(() => {
164
+ watch?.disconnect();
165
+ pulse?.cancel();
166
+ });
167
+ afterRenderEffect(() => {
168
+ const v = this.version();
169
+ untracked(() => {
170
+ const to = el.offsetHeight;
171
+ const { scale, easing } = motion();
172
+ if (last !== NONE && v !== last && scale > 0 && el.animate) {
173
+ el.animate([{ opacity: 0, filter: 'blur(2px)' }, { opacity: 1, filter: 'blur(0)' }], { duration: 260 * scale, easing });
174
+ if (height && height !== to) {
175
+ el.animate([{ height: `${height}px`, overflow: 'hidden' }, { height: `${to}px`, overflow: 'hidden' }], { duration: 420 * scale, easing });
176
+ }
177
+ }
178
+ last = v;
179
+ height = to;
180
+ });
181
+ });
182
+ afterRenderEffect(() => {
183
+ const busy = this.busy();
184
+ untracked(() => {
185
+ pulse?.cancel();
186
+ pulse = undefined;
187
+ if (busy && motion().scale > 0 && el.animate) {
188
+ pulse = el.animate([{ opacity: 1 }, { opacity: 0.55 }, { opacity: 1 }], { duration: 1000, iterations: Infinity, easing: 'ease-in-out' });
189
+ }
190
+ });
191
+ });
192
+ }
193
+ }
194
+
195
+ /** Internal: the tooltip above a bar button. The bar shows it by setting --_o to 1, after the --_w delay. */
196
+ @Component({
197
+ selector: 'ui-message-tip',
198
+ host: { 'aria-hidden': 'true' },
199
+ template: `@for (s of [text()]; track 'tip:' + s) {<span style="animation: ui-fade-in calc(220ms * var(--ui-motion-scale)) var(--ui-ease-out)">{{ s }}</span>}`,
200
+ styles: `
201
+ :host {
202
+ --_t: var(--ui-look-tray, 0);
203
+ --_o: 0;
204
+ --_w: 0s;
205
+ position: absolute;
206
+ bottom: calc(100% + 6px);
207
+ left: 50%;
208
+ z-index: 2;
209
+ display: grid;
210
+ padding: calc(2px + 1px*var(--_t)) calc(8px - 1px*var(--_t));
211
+ border-radius: 6px;
212
+ background: color-mix(in srgb, var(--ui-bg-neutral-solid) calc(100%*var(--_t)), var(--ui-bg-surface));
213
+ box-shadow: 0 0 0 1px color-mix(in srgb, var(--ui-hairline) calc(100% - 100%*var(--_t)), transparent), 0 4px 12px -2px color-mix(in srgb, var(--ui-bg-scrim) calc(30% - 30%*var(--_t)), transparent);
214
+ color: color-mix(in srgb, var(--ui-text-on-neutral) calc(100%*var(--_t)), var(--ui-text));
215
+ font: 500 calc(12px - 0.5px*var(--_t)) / calc(20px - 4px*var(--_t)) var(--ui-font-sans);
216
+ white-space: nowrap;
217
+ pointer-events: none;
218
+ opacity: var(--_o);
219
+ translate: -50% calc(3px*var(--_o) - 3px);
220
+ transition: opacity calc(140ms*var(--ui-motion-scale)) var(--ui-ease-out) var(--_w), translate calc(200ms*var(--ui-motion-scale)) var(--ui-ease-out) var(--_w);
221
+ }
222
+ @media (forced-colors: active) {
223
+ :host { border: 1px solid CanvasText; }
224
+ }
225
+ `,
226
+ })
227
+ export class UiMessageTip {
228
+ readonly text = input('');
229
+ }
230
+
231
+ /** Internal: the "‹ 2 / 3 ›" switcher that unfolds next to Regenerate. */
232
+ @Component({
233
+ selector: 'ui-message-versions',
234
+ host: {
235
+ '[attr.data-open]': 'count() > 1 || null',
236
+ '[inert]': 'count() < 2',
237
+ },
238
+ template: `
239
+ <span style="display: flex; min-width: 0; overflow: clip; overflow-clip-margin: 4px; opacity: var(--_v); transition: opacity calc(260ms*var(--_m)) var(--ui-ease-out) calc(100ms*var(--_m))">
240
+ <span role="group" [attr.aria-label]="labels().versions" style="display: flex; align-items: center; gap: 2px">
241
+ <button type="button" [attr.aria-label]="labels().previous" [attr.aria-disabled]="index() === 0 || null" (click)="step.emit(-1)">
242
+ <svg viewBox="0 0 16 16" aria-hidden="true"><path d="M9.5 4 5.5 8l4 4" /></svg>
243
+ </button>
244
+ <span aria-hidden="true" style="display: flex; gap: 2px; color: var(--ui-text-muted); font: 12px / calc(16px + 4px*var(--_t)) var(--ui-font-sans); font-variant-numeric: tabular-nums; white-space: nowrap">
245
+ @for (i of [index()]; track 'v' + i) {
246
+ <b style="color: var(--ui-text); font-weight: 500; animation: ui-fade-in calc(320ms*var(--_m)) var(--ui-ease-out) calc(100ms*var(--_m)) both">{{ i + 1 }}</b>
247
+ }
248
+ <i style="color: var(--ui-text-disabled); font-style: normal">/</i>{{ count() }}
249
+ </span>
250
+ <span class="ui-sr">{{ labels().versionOf(index() + 1, count()) }}</span>
251
+ <button type="button" [attr.aria-label]="labels().next" [attr.aria-disabled]="index() === count() - 1 || null" (click)="step.emit(1)">
252
+ <svg viewBox="0 0 16 16" aria-hidden="true"><path d="m6.5 4 4 4-4 4" /></svg>
253
+ </button>
254
+ </span>
255
+ </span>
256
+ `,
257
+ styles: `
258
+ :host {
259
+ --_t: var(--ui-look-tray, 0);
260
+ --_m: var(--ui-motion-scale);
261
+ --_v: 0;
262
+ display: grid;
263
+ grid-template-columns: 0fr;
264
+ transition: grid-template-columns calc(420ms*var(--_m)) var(--ui-ease-out);
265
+ }
266
+ :host([data-open]) { --_v: 1; grid-template-columns: 1fr; }
267
+ button {
268
+ display: grid;
269
+ place-items: center;
270
+ width: 20px;
271
+ height: var(--_s, 28px);
272
+ margin: 0;
273
+ padding: 0;
274
+ border: 0;
275
+ border-radius: calc(6px + 1px*var(--_t));
276
+ background: none;
277
+ color: var(--ui-gray-500);
278
+ cursor: pointer;
279
+ transition: color calc(160ms*var(--_m)) var(--ui-ease-out), background-color calc(160ms*var(--_m)) var(--ui-ease-out);
280
+ }
281
+ @media (hover: hover) {
282
+ button:hover:not([aria-disabled]) { background: color-mix(in srgb, var(--ui-text) calc(3.5% + 2.5%*var(--_t)), transparent); color: var(--ui-text); }
283
+ }
284
+ button:focus-visible { outline: 2px solid var(--ui-focus-ring); outline-offset: 2px; }
285
+ [aria-disabled] { opacity: var(--ui-disabled-opacity); cursor: default; }
286
+ svg {
287
+ width: calc(16px - 2px*var(--_t));
288
+ height: calc(16px - 2px*var(--_t));
289
+ fill: none;
290
+ stroke: currentColor;
291
+ stroke-width: calc(1.35px - 0.2px*var(--_t));
292
+ stroke-linecap: round;
293
+ stroke-linejoin: round;
294
+ }
295
+ svg:dir(rtl) { scale: -1 1; }
296
+ @media (pointer: coarse) {
297
+ button { width: 28px; }
298
+ }
299
+ @media (forced-colors: active) {
300
+ [aria-disabled] { color: GrayText; }
301
+ }
302
+ `,
303
+ })
304
+ export class UiMessageVersions {
305
+ readonly count = input(1);
306
+ readonly index = input(0);
307
+ readonly labels = input.required<MessageActionsLabels>();
308
+ readonly step = output<number>();
309
+ }
310
+
311
+ /** Internal: the row that thumbs down unfolds: "What went wrong?", quick reasons, Send and an optional text field. */
312
+ @Component({
313
+ selector: 'ui-message-feedback',
314
+ host: { '(keydown.escape)': 'closed.emit()' },
315
+ template: `
316
+ <div class="f" [attr.data-open]="open() || null">
317
+ <div [style]="clip">
318
+ <div role="group" [attr.aria-labelledby]="askId" style="container-type: inline-size; margin-top: calc(2px*var(--_t)); padding: calc(10px + 2px*var(--_t)) calc(8px + 6px*var(--_t)) calc(8px + 6px*var(--_t)); border-radius: calc(16px*var(--_t)); background: color-mix(in srgb, var(--ui-bg-surface) calc(100%*var(--_t)), transparent); box-shadow: 0 0 0 1px color-mix(in srgb, var(--ui-hairline) calc(60%*var(--_t)), transparent), 0 1px 3px color-mix(in srgb, var(--ui-bg-scrim) calc(20%*var(--_t)), transparent)">
319
+ <div style="display: flex; align-items: center; justify-content: space-between; gap: 8px; height: var(--_xs); margin-bottom: calc(8px - 2px*var(--_t)); color: var(--ui-text); font: 500 calc(13px - 0.5px*var(--_t)) / 20px var(--ui-font-sans)">
320
+ <span [id]="askId">{{ labels().ask }}</span>
321
+ <button type="button" class="x" [attr.aria-label]="labels().close" (click)="closed.emit()"
322
+ style="display: grid; place-items: center; width: var(--_xs); height: var(--_xs); margin: 0; padding: 0; border: var(--_bd, 0); border-radius: calc(6px + 1px*var(--_t)); background: var(--_bg); color: var(--_fg); cursor: pointer">
323
+ <svg viewBox="0 0 16 16" aria-hidden="true" [style]="icon"><path d="m4.5 4.5 7 7m0-7-7 7" /></svg>
324
+ </button>
325
+ </div>
326
+ <div style="display: flex; flex-wrap: wrap; gap: calc(8px - 2px*var(--_t))">
327
+ @for (r of reasons(); track r) {
328
+ <button type="button" class="c" dir="auto" [attr.aria-pressed]="picked() === r" [disabled]="sent()" (click)="pick(r)" [style]="chip">{{ r }}</button>
329
+ }
330
+ <button type="button" class="s" [disabled]="!picked() && !sent()" [attr.aria-disabled]="sent() || null" [attr.data-sent]="sent() || null" (click)="send()" [style]="send_">
331
+ @for (k of [sent()]; track 'sent:' + k) {
332
+ <span style="display: inline-flex; align-items: center; gap: 4px; animation: ui-blur-in calc(300ms*var(--_m)) var(--ui-ease-out)">
333
+ @if (k) {
334
+ <svg viewBox="0 0 16 16" aria-hidden="true" [style]="icon" [style.width.px]="14" [style.height.px]="14"><path d="m3 8.5 3 3 7-7" /></svg>{{ labels().thanks }}
335
+ } @else {
336
+ {{ labels().send }}
337
+ }
338
+ </span>
339
+ }
340
+ </button>
341
+ </div>
342
+ <div class="f" [attr.data-open]="typing() || null" [inert]="!typing()">
343
+ <div [style]="clip">
344
+ <input type="text" dir="auto" [placeholder]="labels().otherPlaceholder" [attr.aria-label]="labels().otherLabel" [value]="words()" [readOnly]="sent()"
345
+ (input)="words.set($any($event.target).value)" (keydown.enter)="send()"
346
+ style="box-sizing: border-box; width: 100%; height: var(--_ih); margin: 8px 0 0; padding: 0 10px; border: var(--_bd, 0); border-radius: calc(6px + 2px*var(--_t)); background: color-mix(in srgb, var(--ui-text) calc(5%*var(--_t)), var(--ui-bg-surface)); box-shadow: inset 0 0 0 1px var(--ui-border-strong); color: var(--ui-text); font: var(--_if, calc(13px - 0.5px*var(--_t))) / 20px var(--ui-font-sans)" />
347
+ </div>
348
+ </div>
349
+ </div>
350
+ </div>
351
+ </div>
352
+ `,
353
+ styles: `
354
+ :host {
355
+ --_t: var(--ui-look-tray, 0);
356
+ --_m: var(--ui-motion-scale);
357
+ --_h: calc(28px - 2px*var(--_t));
358
+ --_xs: 24px;
359
+ --_ih: calc(32px - 2px*var(--_t));
360
+ --_cur: pointer;
361
+ display: block;
362
+ font-family: var(--ui-font-sans);
363
+ }
364
+ .f {
365
+ --_v: 0;
366
+ --_od: calc(220ms*var(--_m));
367
+ --_dl: 0s;
368
+ display: grid;
369
+ grid-template-rows: 0fr;
370
+ transition: grid-template-rows calc(420ms*var(--_m)) var(--ui-ease-out);
371
+ }
372
+ .x { --_bg: none; --_fg: var(--ui-gray-500); }
373
+ .c { --_bg: color-mix(in srgb, var(--ui-text) calc(5%*var(--_t)), transparent); --_fg: color-mix(in srgb, var(--ui-text) calc(40%*var(--_t)), var(--ui-text-muted)); }
374
+ .s { --_bg: var(--ui-bg-neutral-solid); --_fg: var(--ui-text-on-neutral); }
375
+ [data-open] { --_v: 1; --_od: calc(320ms*var(--_m)); --_dl: calc(80ms*var(--_m)); grid-template-rows: 1fr; }
376
+ @media (hover: hover) {
377
+ .c:hover:enabled:not([aria-pressed='true']), .x:hover { --_bg: color-mix(in srgb, var(--ui-text) calc(3.5% + 5%*var(--_t)), transparent); --_fg: var(--ui-text); }
378
+ }
379
+ [aria-pressed='true'] {
380
+ --_bg: color-mix(in srgb, var(--ui-info-soft) calc(100%*var(--_t)), color-mix(in srgb, var(--ui-text) 6.5%, transparent));
381
+ --_fg: color-mix(in srgb, var(--ui-info) calc(100%*var(--_t)), var(--ui-text));
382
+ }
383
+ :disabled { --_cur: default; }
384
+ :disabled:not([aria-pressed='true']) { opacity: var(--_dop, var(--ui-disabled-opacity)); }
385
+ [data-sent] { --_bg: var(--ui-success-soft); --_fg: var(--ui-success); --_cur: default; }
386
+ :focus-visible { outline: 2px solid var(--ui-focus-ring); outline-offset: 2px; }
387
+ ::placeholder { color: var(--ui-text-placeholder); }
388
+ @container (max-width: 319px) {
389
+ .s { flex-basis: 100%; }
390
+ }
391
+ @media (pointer: coarse) {
392
+ :host { --_h: 34px; --_xs: 32px; --_ih: 38px; --_if: var(--ui-text-field-touch); }
393
+ }
394
+ @media (forced-colors: active) {
395
+ :host { --_bd: 1px solid CanvasText; }
396
+ [aria-pressed='true'] { forced-color-adjust: none; --_bg: Highlight; --_fg: HighlightText; }
397
+ .s:disabled { color: GrayText; }
398
+ }
399
+ `,
400
+ })
401
+ export class UiMessageFeedback {
402
+ readonly open = input(false);
403
+ readonly reasons = input<readonly string[]>([]);
404
+ readonly other = input<string | null>(null);
405
+ readonly labels = input.required<MessageActionsLabels>();
406
+ readonly submitted = output<string>({ alias: 'sent' });
407
+ readonly closed = output<void>();
408
+ readonly done = output<void>();
409
+
410
+ protected readonly clip =
411
+ 'display: grid; min-height: 0; overflow: clip; overflow-clip-margin: 4px; opacity: var(--_v); transition: opacity var(--_od) var(--ui-ease-out) var(--_dl)';
412
+ protected readonly icon =
413
+ 'width: calc(16px - 2px*var(--_t)); height: calc(16px - 2px*var(--_t)); fill: none; stroke: currentColor; stroke-width: 1.35; stroke-linecap: round; stroke-linejoin: round';
414
+ private readonly pill =
415
+ 'display: inline-flex; align-items: center; justify-content: center; height: var(--_h); margin: 0; border: var(--_bd, 0); border-radius: calc(6px + 2px*var(--_t)); font: inherit; font-size: calc(13px - 1px*var(--_t)); line-height: 20px; cursor: var(--_cur); transition: background-color calc(160ms*var(--_m)) var(--ui-ease-out), color calc(160ms*var(--_m)) var(--ui-ease-out), opacity calc(200ms*var(--_m)) var(--ui-ease-out);';
416
+ protected readonly chip =
417
+ this.pill +
418
+ 'padding: 0 calc(10px - 1px*var(--_t)); font-weight: calc(500 - 100*var(--_t)); background: var(--_bg); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--ui-hairline) calc(100% - 100%*var(--_t)), transparent); color: var(--_fg)';
419
+ protected readonly send_ =
420
+ this.pill +
421
+ '--_dop: calc(0.3 + 0.02*var(--_t)); min-width: calc(60px - 2px*var(--_t)); margin-inline-start: auto; padding: 0 calc(10px + 1px*var(--_t)); font-weight: 500; background: var(--_bg); color: var(--_fg)';
422
+
423
+ protected readonly askId = `ui-message-ask-${nextId++}`;
424
+ protected readonly picked = signal<string | null>(null);
425
+ protected readonly words = signal('');
426
+ protected readonly sent = signal(false);
427
+ protected readonly typing = computed(() => !this.sent() && this.picked() !== null && this.picked() === this.other());
428
+ private timer?: ReturnType<typeof setTimeout>;
429
+
430
+ constructor() {
431
+ effect(() => {
432
+ if (!this.open()) return;
433
+ untracked(() => {
434
+ clearTimeout(this.timer);
435
+ this.picked.set(null);
436
+ this.words.set('');
437
+ this.sent.set(false);
438
+ });
439
+ });
440
+ inject(DestroyRef).onDestroy(() => clearTimeout(this.timer));
441
+ }
442
+
443
+ protected pick(r: string): void {
444
+ this.picked.update((p) => (p === r ? null : r));
445
+ }
446
+
447
+ protected send(): void {
448
+ const picked = this.picked();
449
+ if (!picked || this.sent()) return;
450
+ const typed = this.typing() ? this.words().trim() : '';
451
+ this.sent.set(true);
452
+ this.submitted.emit(typed || picked);
453
+ this.timer = setTimeout(() => this.done.emit(), 1500);
454
+ }
455
+ }
456
+
457
+ @Component({
458
+ selector: 'ui-message-actions',
459
+ imports: [UiMessageTip, UiMessageVersions, UiMessageFeedback],
460
+ host: {
461
+ '[attr.data-hot]': 'hot() || null',
462
+ },
463
+ template: `
464
+ <div class="bar" role="group" [attr.aria-label]="name()" [attr.data-warm]="warm() || null" (pointerover)="warmUp()" (pointerleave)="coolDown()"
465
+ style="display: flex; align-items: center; gap: 2px; padding-block: calc(4px*var(--_t)); padding-inline: calc(6px*var(--_t)) calc(4px*var(--_t)); transition: opacity calc(240ms*var(--_m)) var(--_e)">
466
+ @for (key of actions(); track key) {
467
+ @switch (key) {
468
+ @case ('copy') {
469
+ <span class="slot" style="position: relative; display: grid">
470
+ <button type="button" class="b" [attr.aria-label]="t().copy" (click)="copy()">
471
+ <span class="sw" [class.on]="copyState() === 'ok'" style="display: grid; scale: var(--_z); transition: scale var(--_d) var(--_e)">
472
+ <svg viewBox="0 0 16 16" aria-hidden="true" style="grid-area: 1 / 1; opacity: var(--_a); scale: calc(0.6 + 0.4*var(--_a)); filter: blur(calc(2px - 2px*var(--_a))); transition: var(--_x)">
473
+ <g style="fill: currentColor; stroke: none; opacity: calc(1 - var(--_t))">
474
+ <path d="M3.2 1h5.6A2.2 2.2 0 0 1 11 3.2v.3H5.7a2.2 2.2 0 0 0-2.2 2.2V11h-.3A2.2 2.2 0 0 1 1 8.8V3.2A2.2 2.2 0 0 1 3.2 1Z" />
475
+ <rect x="5" y="5" width="10" height="10" rx="2.2" />
476
+ </g>
477
+ <g style="stroke-width: 1.15px; opacity: var(--_t)">
478
+ <rect x="5.5" y="5.5" width="9" height="9" rx="1.8" />
479
+ <path d="M10.5 5.5V3.3a1.8 1.8 0 0 0-1.8-1.8H3.3a1.8 1.8 0 0 0-1.8 1.8v5.4a1.8 1.8 0 0 0 1.8 1.8h2.2" />
480
+ </g>
481
+ </svg>
482
+ <svg viewBox="0 0 16 16" aria-hidden="true" style="grid-area: 1 / 1; opacity: var(--_on); scale: calc(0.6 + 0.4*var(--_on)); filter: blur(calc(2px - 2px*var(--_on))); transition: var(--_x)">
483
+ <g style="opacity: calc(1 - var(--_t))">
484
+ <circle cx="8" cy="8" r="6.5" style="fill: var(--ui-success); stroke: none" />
485
+ <path d="m5.4 8.1 1.8 1.8 3.5-3.7" style="stroke: var(--ui-bg-surface); stroke-width: 1.5px" />
486
+ </g>
487
+ <path pathLength="1" d="m3 8.4 3.3 3.3L13 4.6"
488
+ style="opacity: var(--_t); stroke: var(--ui-success); stroke-width: 1.5px; stroke-dasharray: 1; animation: var(--_draw, none) calc(340ms*var(--_m)) var(--_e) 60ms both" />
489
+ </svg>
490
+ </span>
491
+ </button>
492
+ <ui-message-tip [attr.data-show]="copyState() ? '' : null" [text]="copyState() === 'ok' ? t().copied : copyState() ? t().copyFailed : t().copyTip" />
493
+ </span>
494
+ }
495
+ @case ('regenerate') {
496
+ <span class="slot" style="position: relative; display: grid">
497
+ <button type="button" class="b" [attr.aria-label]="t().regenerate" [attr.data-spin]="busy() || null" (click)="regen()">
498
+ <svg viewBox="0 0 16 16" aria-hidden="true" style="animation: var(--_spin, none) calc(900ms*var(--_m)) linear infinite">
499
+ <path d="M13.5 8a5.5 5.5 0 1 1-1.6-3.9M12.2 1.6v2.9H9.3" />
500
+ </svg>
501
+ </button>
502
+ <ui-message-tip [text]="busy() ? t().writing : t().regenerateTip" />
503
+ </span>
504
+ <ui-message-versions [count]="count()" [index]="index()" [labels]="t()" (step)="step($event)" />
505
+ }
506
+ @case ('feedback') {
507
+ <span class="slot" style="position: relative; display: grid">
508
+ <button type="button" class="b v" [attr.aria-label]="t().good" [attr.aria-pressed]="vote() === 'up'" (click)="cast('up')">
509
+ <svg viewBox="0 0 16 16" aria-hidden="true"><g [style]="solid"><rect x="1.5" y="7" width="3.3" height="7.5" rx=".9" /><path [attr.d]="thumb" /></g></svg>
510
+ </button>
511
+ <ui-message-tip [text]="vote() === 'up' ? t().thanks : t().good" />
512
+ </span>
513
+ <span class="slot" style="position: relative; display: grid">
514
+ <button #down type="button" class="b v" [attr.aria-label]="t().bad" [attr.aria-pressed]="vote() === 'down'" [attr.aria-expanded]="open()"
515
+ [attr.aria-controls]="rowId" (click)="cast('down')" (keydown.escape)="open() && closeRow(true)">
516
+ <svg viewBox="0 0 16 16" aria-hidden="true"><g [style]="solid" transform="matrix(1 0 0 -1 0 16)"><rect x="1.5" y="7" width="3.3" height="7.5" rx=".9" /><path [attr.d]="thumb" /></g></svg>
517
+ </button>
518
+ <ui-message-tip [text]="t().bad" />
519
+ </span>
520
+ }
521
+ @case ('speak') {
522
+ <span class="slot" style="position: relative; display: grid">
523
+ <button type="button" class="b sp" [attr.aria-label]="playing() ? t().stop : t().read" [attr.aria-pressed]="playing()" (click)="speak()">
524
+ <span class="sw" [class.on]="playing()" style="display: grid; scale: var(--_z); transition: scale var(--_d) var(--_e)">
525
+ <svg viewBox="0 0 16 16" aria-hidden="true" style="grid-area: 1 / 1; opacity: var(--_a); scale: calc(0.6 + 0.4*var(--_a)); filter: blur(calc(2px - 2px*var(--_a))); transition: var(--_x)">
526
+ <path [style]="solid" d="M2.5 5.8h2L8 3v10l-3.5-2.8h-2a1 1 0 0 1-1-1V6.8a1 1 0 0 1 1-1Z" />
527
+ <path d="M10.6 6.1a2.6 2.6 0 0 1 0 3.8m2-5.7a5.3 5.3 0 0 1 0 7.6" />
528
+ </svg>
529
+ <span aria-hidden="true" style="grid-area: 1 / 1; display: flex; align-items: center; justify-content: center; gap: 2px; width: 16px; height: 16px; color: var(--ui-message-actions-bars, var(--ui-info)); opacity: var(--_on); scale: calc(0.6 + 0.4*var(--_on)); filter: blur(calc(2px - 2px*var(--_on))); transition: var(--_x)">
530
+ @for (bar of bars; track $index) {
531
+ <i [style.--_q]="bar[0]" [style.--_p]="bar[1]" [style.--_l]="bar[2]"></i>
532
+ }
533
+ </span>
534
+ </span>
535
+ </button>
536
+ <ui-message-tip [text]="playing() ? t().stopTip : t().read" />
537
+ </span>
538
+ }
539
+ }
540
+ }
541
+ </div>
542
+ @if (actions().includes('feedback')) {
543
+ <ui-message-feedback [id]="rowId" [inert]="!open()" [open]="open()" [reasons]="reasons()" [other]="otherReason()" [labels]="t()"
544
+ (sent)="sent($event)" (closed)="closeRow(true)" (done)="closeRow(false)" />
545
+ }
546
+ <span class="ui-sr" aria-live="polite">{{ live() }}</span>
547
+ `,
548
+ styles: `
549
+ :host {
550
+ --_t: var(--ui-look-tray, 0);
551
+ --_m: var(--ui-motion-scale);
552
+ --_e: var(--ui-ease-out);
553
+ --_d: calc(160ms*var(--_m));
554
+ --_x: opacity calc(200ms*var(--_m)) var(--_e), scale calc(300ms*var(--_m)) var(--_e), filter calc(300ms*var(--_m)) var(--_e);
555
+ --_s: var(--ui-message-actions-size, calc(28px + 2px*var(--_t)));
556
+ --_z: 1;
557
+ --_a: 1;
558
+ --_on: 0;
559
+ --_f: calc(1 - var(--_t));
560
+ display: block;
561
+ font-family: var(--ui-font-sans);
562
+ }
563
+ .bar { opacity: var(--ui-message-actions-quiet, 0.6); }
564
+ :host(:focus-within) .bar, :host([data-hot]) .bar { opacity: 1; }
565
+ @media (hover: hover) { :host(:hover) .bar { opacity: 1; } }
566
+ .b {
567
+ display: grid;
568
+ place-items: center;
569
+ width: var(--_s);
570
+ height: var(--_s);
571
+ margin: 0;
572
+ padding: 0;
573
+ border: 0;
574
+ border-radius: calc(6px + 2px*var(--_t));
575
+ background: none;
576
+ color: var(--ui-gray-500);
577
+ cursor: pointer;
578
+ transition: color var(--_d) var(--_e), background-color var(--_d) var(--_e), box-shadow var(--_d) var(--_e);
579
+ }
580
+ @media (hover: hover) {
581
+ .b:hover:not([aria-pressed='true']) { background: color-mix(in srgb, var(--ui-text) calc(3.5% + 2.5%*var(--_t)), transparent); color: var(--ui-text); }
582
+ }
583
+ .b:focus-visible { outline: 2px solid var(--ui-focus-ring); outline-offset: 2px; }
584
+ .b:active { --_z: 0.9; }
585
+ [aria-pressed='true'] {
586
+ --_f: 1;
587
+ background: color-mix(in srgb, var(--ui-text) calc(6.5% - 6.5%*var(--_t)), transparent);
588
+ box-shadow: inset 0 0 0 .5px color-mix(in srgb, var(--ui-hairline) calc(100% - 100%*var(--_t)), transparent), 0 1px 2px color-mix(in srgb, var(--ui-gray-1000) calc(8% - 8%*var(--_t)), transparent);
589
+ color: var(--ui-text);
590
+ }
591
+ .sp[aria-pressed='true'] { background: color-mix(in srgb, var(--ui-info-soft) calc(100%*var(--_t)), color-mix(in srgb, var(--ui-text) 6.5%, transparent)); }
592
+ svg {
593
+ width: 16px;
594
+ height: 16px;
595
+ fill: none;
596
+ stroke: currentColor;
597
+ stroke-width: calc(1.35px - 0.2px*var(--_t));
598
+ stroke-linecap: round;
599
+ stroke-linejoin: round;
600
+ }
601
+ .b > svg { scale: var(--_z); transition: scale var(--_d) var(--_e); }
602
+ .v[aria-pressed='true'] > svg { animation: pop calc(420ms*var(--_m)) var(--_e); }
603
+ @keyframes pop { 28% { scale: calc(1.21 + 0.06*var(--_t)); } 70% { scale: 0.97; } }
604
+ [data-spin] { --_spin: ui-spin; }
605
+ .on { --_a: 0; --_on: 1; --_draw: ui-draw; }
606
+ i { width: 2px; height: calc(11px + 1px*var(--_t)); border-radius: 1px; background: currentColor; scale: 1 calc(0.3 + var(--_on)*(var(--_q) - 0.3)); }
607
+ .on i { animation: eq calc(var(--_p)*var(--_m)) ease-in-out var(--_l) infinite; }
608
+ @keyframes eq { 0%, 100% { scale: 1 0.3; } 50% { scale: 1 1; } }
609
+ @media (hover: hover) {
610
+ .slot:hover > ui-message-tip { --_o: 1; --_w: 380ms; }
611
+ [data-warm] .slot:hover > ui-message-tip { --_w: 0s; }
612
+ }
613
+ .slot:has(:focus-visible) > ui-message-tip, [data-show] { --_o: 1; --_w: 0s; }
614
+ @media (pointer: coarse) {
615
+ :host { --_s: var(--ui-message-actions-size, 36px); }
616
+ .bar { opacity: 1; }
617
+ }
618
+ @media (forced-colors: active) {
619
+ [aria-pressed='true'] { forced-color-adjust: none; background: Highlight; color: HighlightText; }
620
+ }
621
+ `,
622
+ })
623
+ export class UiMessageActions {
624
+ /** The message text. Copy writes it, and an uncontrolled read aloud is timed by its length. */
625
+ readonly text = input('');
626
+ /** Which actions show, in this order. `feedback` is both thumbs. */
627
+ readonly actions = input<readonly MessageActionKey[]>(['copy', 'regenerate', 'feedback', 'speak']);
628
+ /** All versions, or how many there are. From two on, a "‹ 2 / 3 ›" switcher unfolds next to Regenerate. */
629
+ readonly versions = input<number | readonly string[]>(1);
630
+ /** The shown version, 0-based. Unbound, it shows the newest and jumps to each new one. */
631
+ readonly version = model<number | undefined>(undefined);
632
+ /** Keeps the arrow spinning and the bar lit while a new version is written. Presses are ignored meanwhile. */
633
+ readonly regenerating = input(false, { transform: booleanAttribute });
634
+ /** The vote on the shown version. A new version starts without one. */
635
+ readonly vote = model<MessageVote>(null);
636
+ /** Quick reasons in the row that thumbs down opens. Single choice. */
637
+ readonly reasons = input<readonly string[]>(['Not accurate', 'Too long', "Didn't follow instructions", 'Other']);
638
+ /** The reason that unfolds a text field; its words are sent instead. `null` for none. */
639
+ readonly otherReason = input<string | null>('Other');
640
+ /** Read aloud is playing. Bind it to drive real speech; unbound, it plays for `speakDuration` and stops. */
641
+ readonly speaking = model<boolean | undefined>(undefined);
642
+ /** How long an unbound read aloud plays, in ms. Default: 60 ms per character, at least 2.6 s. */
643
+ readonly speakDuration = input<number | undefined, unknown>(undefined, {
644
+ transform: (v: unknown) => (v == null || v === '' ? undefined : numberAttribute(v)),
645
+ });
646
+ /** Faded until the message or the bar is hovered or focused. Touch screens always show it fully. */
647
+ readonly quiet = input(true, { transform: booleanAttribute });
648
+ /** Accessible name of the bar. Default: labels.bar. */
649
+ readonly label = input<string>();
650
+ readonly labels = input<Partial<MessageActionsLabels>>({});
651
+
652
+ /** After a successful copy, with the text. */
653
+ readonly copied = output<string>();
654
+ readonly regenerate = output<void>();
655
+ /** The vote at once (`null` when taken back), then `down` with a reason when the row is sent. */
656
+ readonly feedback = output<MessageFeedback>();
657
+
658
+ private readonly message = inject(UiMessage, { optional: true });
659
+ private readonly el = inject<ElementRef<HTMLElement>>(ElementRef).nativeElement;
660
+ private readonly doc = inject(DOCUMENT);
661
+ private readonly down = viewChild<ElementRef<HTMLButtonElement>>('down');
662
+ private readonly timers = new Map<string, ReturnType<typeof setTimeout>>();
663
+
664
+ protected readonly rowId = `ui-message-feedback-${nextId++}`;
665
+ protected readonly thumb = 'm5.8 7.2 2.5-5.3c1.1 0 1.9.9 1.9 2v2.5h2.9a1.6 1.6 0 0 1 1.6 1.9l-1 5a1.6 1.6 0 0 1-1.6 1.2H5.8Z';
666
+ protected readonly solid = 'fill: currentColor; fill-opacity: var(--_f); stroke-width: calc(1.15px*var(--_t))';
667
+ protected readonly bars = [
668
+ ['0.8', '900ms', '0s'],
669
+ ['0.5', '760ms', '-300ms'],
670
+ ['0.8', '1000ms', '-520ms'],
671
+ ['0.5', '820ms', '-140ms'],
672
+ ];
673
+ protected readonly t = computed(() => ({ ...LABELS, ...this.labels() }));
674
+ protected readonly name = computed(() => this.label() ?? this.t().bar);
675
+ protected readonly count = computed(() => {
676
+ const v = this.versions();
677
+ return Math.max(1, typeof v === 'number' ? Math.floor(v) || 1 : v.length);
678
+ });
679
+ protected readonly index = computed(() => Math.min(this.count() - 1, Math.max(0, this.version() ?? this.count() - 1)));
680
+ protected readonly copyState = signal<'ok' | 'fail' | null>(null);
681
+ protected readonly turning = signal(false);
682
+ protected readonly busy = computed(() => this.regenerating() || this.turning());
683
+ protected readonly playing = computed(() => !!this.speaking());
684
+ protected readonly open = signal(false);
685
+ protected readonly warm = signal(false);
686
+ protected readonly live = signal('');
687
+ protected readonly hot = computed(
688
+ () => !this.quiet() || !!this.message?.hot() || this.busy() || this.playing() || this.open() || this.copyState() !== null,
689
+ );
690
+ private free?: boolean;
691
+ private quietStop = false;
692
+
693
+ constructor() {
694
+ let seen = 0;
695
+ effect(() => {
696
+ const n = this.count();
697
+ untracked(() => {
698
+ if (seen && n > seen) {
699
+ this.version.set(n - 1);
700
+ this.vote.set(null);
701
+ this.open.set(false);
702
+ this.say(this.t().versionOf(n, n));
703
+ }
704
+ seen = n;
705
+ });
706
+ });
707
+ let first = true;
708
+ effect(() => {
709
+ this.text();
710
+ untracked(() => {
711
+ if (!first && this.playing()) this.stopSpeaking(this.t().stopped);
712
+ first = false;
713
+ });
714
+ });
715
+ let was = false;
716
+ effect(() => {
717
+ const on = this.playing();
718
+ untracked(() => {
719
+ if (was && !on && !this.quietStop) this.say(this.t().finished);
720
+ was = on;
721
+ this.quietStop = false;
722
+ });
723
+ });
724
+ inject(DestroyRef).onDestroy(() => this.timers.forEach((id) => clearTimeout(id)));
725
+ afterNextRender(() => {
726
+ if (!isDevMode()) return;
727
+ if (!this.name()) console.warn('ui-message-actions: give the bar a name with label or labels.bar.', this.el);
728
+ if (!this.text() && this.actions().some((a) => a === 'copy' || a === 'speak')) {
729
+ console.warn('ui-message-actions: set text, so Copy and Read aloud have something to work with.', this.el);
730
+ }
731
+ });
732
+ }
733
+
734
+ protected async copy(): Promise<void> {
735
+ const text = this.text();
736
+ try {
737
+ const clipboard = this.doc.defaultView?.navigator.clipboard;
738
+ if (!clipboard) throw new Error('No clipboard');
739
+ await clipboard.writeText(text);
740
+ this.copyState.set('ok');
741
+ this.copied.emit(text);
742
+ this.say(this.t().copied);
743
+ } catch {
744
+ this.copyState.set('fail');
745
+ this.say(this.t().copyFailed);
746
+ }
747
+ this.later('copy', 1600, () => this.copyState.set(null));
748
+ }
749
+
750
+ protected regen(): void {
751
+ if (this.regenerating()) return;
752
+ this.open.set(false);
753
+ this.vote.set(null);
754
+ this.turning.set(true);
755
+ this.later('turn', 900, () => this.turning.set(false));
756
+ this.say(this.t().regenerating);
757
+ this.regenerate.emit();
758
+ }
759
+
760
+ protected step(d: number): void {
761
+ const i = this.index() + d;
762
+ if (i < 0 || i >= this.count()) return;
763
+ this.version.set(i);
764
+ this.say(this.t().versionOf(i + 1, this.count()));
765
+ }
766
+
767
+ protected cast(kind: 'up' | 'down'): void {
768
+ const next = this.vote() === kind ? null : kind;
769
+ this.vote.set(next);
770
+ this.open.set(next === 'down');
771
+ this.feedback.emit({ kind: next });
772
+ this.say(next === 'up' ? this.t().markedGood : next === 'down' ? this.t().markedBad : '');
773
+ }
774
+
775
+ protected sent(reason: string): void {
776
+ this.feedback.emit({ kind: 'down', reason });
777
+ this.say(this.t().sent);
778
+ }
779
+
780
+ protected closeRow(always: boolean): void {
781
+ const row = this.doc.getElementById(this.rowId);
782
+ if (always || row?.contains(this.doc.activeElement)) this.down()?.nativeElement.focus();
783
+ this.open.set(false);
784
+ }
785
+
786
+ protected speak(): void {
787
+ this.free ??= this.speaking() === undefined;
788
+ if (this.playing()) {
789
+ this.stopSpeaking(this.t().stopped);
790
+ return;
791
+ }
792
+ this.speaking.set(true);
793
+ this.say(this.t().reading);
794
+ if (this.free) {
795
+ this.later('speak', this.speakDuration() ?? Math.max(2600, this.text().length * 60), () => this.speaking.set(false));
796
+ }
797
+ }
798
+
799
+ protected warmUp(): void {
800
+ if (this.warm() || this.timers.has('warm')) return;
801
+ this.later('warm', 380, () => this.warm.set(true));
802
+ }
803
+
804
+ protected coolDown(): void {
805
+ clearTimeout(this.timers.get('warm'));
806
+ this.timers.delete('warm');
807
+ this.warm.set(false);
808
+ }
809
+
810
+ private stopSpeaking(message: string): void {
811
+ clearTimeout(this.timers.get('speak'));
812
+ this.timers.delete('speak');
813
+ this.quietStop = true;
814
+ this.speaking.set(false);
815
+ this.say(message);
816
+ }
817
+
818
+ private say(message: string): void {
819
+ this.live.set('');
820
+ if (message) this.later('say', 40, () => this.live.set(message));
821
+ }
822
+
823
+ private later(key: string, ms: number, fn: () => void): void {
824
+ clearTimeout(this.timers.get(key));
825
+ this.timers.set(
826
+ key,
827
+ setTimeout(() => {
828
+ this.timers.delete(key);
829
+ fn();
830
+ }, ms),
831
+ );
832
+ }
833
+ }