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,503 @@
1
+ // Ochre (https://ochreui.dev), MIT License, Copyright (c) 2026 Ochre. Keep this notice in copies of this file.
2
+ /**
3
+ * Comment Thread, from Ochre (https://ochreui.dev).
4
+ * Needs ochre.css (the --ui-* tokens and shared keyframes), which `npx ochreui init` adds once.
5
+ *
6
+ * <ui-comment-thread [(messages)]="messages" [currentUser]="me" [assistant]="atlas" [replyHandler]="ask" closable (closed)="…" />
7
+ * One conversation: a header with the count, the messages and a reply field. A leading /command shows as a chip.
8
+ * If `replyHandler` returns a promise, the assistant shows "Thinking…" until it settles.
9
+ * Put your own <input uiThreadReply> inside to bind the draft to a form; otherwise one is drawn.
10
+ *
11
+ * Public styling hooks (set them on the element or any ancestor):
12
+ * --ui-comment-thread-width card width (default 420px, never wider than its container)
13
+ * --ui-comment-thread-list-min smallest message list height (default 290px; 250px under 400px wide)
14
+ * --ui-comment-thread-list-max list height before it scrolls (default 340px; 280px under 400px wide)
15
+ */
16
+ import { NgTemplateOutlet } from '@angular/common';
17
+ import {
18
+ Component,
19
+ DestroyRef,
20
+ Directive,
21
+ ElementRef,
22
+ LOCALE_ID,
23
+ afterNextRender,
24
+ afterRenderEffect,
25
+ booleanAttribute,
26
+ computed,
27
+ contentChild,
28
+ inject,
29
+ input,
30
+ isDevMode,
31
+ model,
32
+ output,
33
+ signal,
34
+ viewChild,
35
+ } from '@angular/core';
36
+
37
+ export interface ThreadAuthor {
38
+ id: string;
39
+ name: string;
40
+ /** Picture URL. Assistants without one get a gradient mark, people their initial. */
41
+ avatar?: string;
42
+ assistant?: boolean;
43
+ }
44
+
45
+ export type ThreadTone = 'success' | 'info' | 'warning' | 'danger';
46
+
47
+ export interface ThreadStatus {
48
+ label: string;
49
+ /** Color of the disc before the label. Default `success`. */
50
+ tone?: ThreadTone;
51
+ /** Lines folded under the status; the status becomes a button when there are any. */
52
+ details?: string[];
53
+ }
54
+
55
+ export interface ThreadMessage {
56
+ id: string;
57
+ author: ThreadAuthor;
58
+ createdAt: Date | string | number;
59
+ text?: string;
60
+ /** Shown as a chip before the text, e.g. `edit`. A leading `/word` in `text` is read as one too. */
61
+ command?: string;
62
+ status?: ThreadStatus;
63
+ }
64
+
65
+ export type ThreadReplyHandler = (text: string) => ThreadMessage | Promise<ThreadMessage | void> | void;
66
+
67
+ export type ThreadTimeUnit = 'minute' | 'hour' | 'day';
68
+
69
+ export interface CommentThreadLabels {
70
+ count: (n: number) => string;
71
+ close: string;
72
+ messages: string;
73
+ reply: string;
74
+ placeholder: (assistant?: string) => string;
75
+ send: string;
76
+ thinking: string;
77
+ isThinking: (name: string) => string;
78
+ justNow: string;
79
+ ago: (n: number, unit: ThreadTimeUnit) => string;
80
+ failed: string;
81
+ retry: string;
82
+ }
83
+
84
+ const LABELS: CommentThreadLabels = {
85
+ count: (n) => `${n} ${n === 1 ? 'message' : 'messages'}`,
86
+ close: 'Close comment',
87
+ messages: 'Messages',
88
+ reply: 'Reply',
89
+ placeholder: (a) => (a ? `Reply, or ask @${a}…` : 'Reply…'),
90
+ send: 'Send reply',
91
+ thinking: 'Thinking…',
92
+ isThinking: (name) => `${name} is thinking`,
93
+ justNow: 'just now',
94
+ ago: (n, unit) => `${n}${{ minute: 'min', hour: 'h', day: 'd' }[unit]} ago`,
95
+ failed: 'No answer came back.',
96
+ retry: 'Try again',
97
+ };
98
+
99
+ const COMMAND = /^\/(\S+)\s*/;
100
+
101
+ let nextId = 0;
102
+
103
+ const T = 'var(--_t)';
104
+ const F = 'var(--_f)';
105
+ const ELLIPSIS = 'overflow: hidden; text-overflow: ellipsis; white-space: nowrap';
106
+ const QUIET = `flex: none; color: var(--ui-text-muted); font-size: calc(12px + .5px * ${T}); line-height: calc(16px + 2.6px * ${T}); white-space: nowrap`;
107
+ const SB = `position: relative; display: inline-flex; align-items: center; gap: calc(6px + 1px * ${T}); max-inline-size: 100%; box-sizing: border-box; block-size: var(--_sb); margin: 0; margin-inline-start: calc(-4px * ${T}); padding: 0; padding-inline: calc(6px + 2px * ${T}) calc(4px + 2px * ${T}); border: 0; border-radius: calc(6px + 2px * ${T}); background: var(--_bg); color: inherit; font: calc(500 - 100 * ${T}) calc(13px + 1px * ${T}) / 20px var(--ui-font-sans); transition: background-color var(--_hv) var(--ui-ease-out)`;
108
+ const INPUT = `box-sizing: border-box; block-size: var(--_ih); padding: 1px calc(2px * ${T}); font-size: calc(13px + 1px * ${T})`;
109
+
110
+ const S = {
111
+ card: `container-type: inline-size; display: flex; flex-direction: column; overflow: hidden; padding: calc(6px * ${T}); border-radius: calc(10px + 12px * ${T}); background: color-mix(in srgb, var(--ui-tray-bg) calc(100% * ${T}), var(--ui-bg-surface)); box-shadow: 0 0 0 calc(.5px * ${F}) color-mix(in srgb, var(--ui-bg-muted) 88%, var(--ui-bg-surface)), 0 calc(1px * ${F}) calc(1px * ${F}) calc(-1px * ${F}) var(--_sh), 0 calc(1px * ${F}) calc(2px * ${F}) var(--_sh)`,
112
+ head: `display: flex; align-items: center; gap: calc(8px + 2px * ${T}); box-sizing: border-box; min-block-size: calc(49px - 3px * ${T}); padding-block: calc(10px - 4px * ${T}); padding-inline: var(--_px) 10px; border-block-end: calc(1px * ${F}) solid var(--_line)`,
113
+ title: `${ELLIPSIS}; margin: 0; font: var(--ui-weight-medium) calc(13px + 2px * ${T}) / calc(20px + 2px * ${T}) var(--ui-font-sans)`,
114
+ count: `${QUIET}; margin-inline-end: auto`,
115
+ close: `--_s: var(--_xs); position: relative; border-radius: calc(6px + 2px * ${T}); background: var(--_bg); color: var(--_cc); transition-duration: var(--_hv)`,
116
+ box: `overflow: hidden; border-radius: calc(16px * ${T}); outline: calc(1px * ${T}) solid var(--_pe); outline-offset: -1px; background: var(--ui-bg-surface); box-shadow: 0 calc(1px * ${T}) calc(2px * ${T}) var(--_sh); transition: block-size calc(380ms * var(--_ms)) var(--ui-ease-out), outline-color var(--_hv)`,
117
+ list: `display: grid; align-content: start; gap: calc(16px + 2px * ${T}); box-sizing: border-box; min-block-size: var(--_min); max-block-size: var(--_lock, var(--_max)); padding: 16px var(--_px) calc(12px + 6px * ${T}); overflow-y: auto; overscroll-behavior: contain; scrollbar-width: none; outline: 0`,
118
+ meta: `display: flex; align-items: center; gap: calc(8px + 1px * ${T}); min-inline-size: 0`,
119
+ avatar: `display: grid; place-items: center; flex: none; inline-size: calc(20px + 4px * ${T}); aspect-ratio: 1; overflow: hidden; border-radius: 50%; background: var(--ui-bg-muted); color: var(--ui-text-muted); font-size: calc(10px + 1px * ${T}); font-weight: var(--ui-weight-medium)`,
120
+ mark: 'linear-gradient(135deg, var(--ui-blue-light) 15%, var(--ui-purple-light))',
121
+ img: `inline-size: 100%; block-size: 100%; object-fit: cover; border-radius: 50%; outline: calc(.5px + .5px * ${T}) solid var(--ui-hairline); outline-offset: calc(-.5px - .5px * ${T})`,
122
+ name: `${ELLIPSIS}; font-weight: var(--ui-weight-medium); font-size: calc(13px + 1px * ${T})`,
123
+ time: `${QUIET}; margin-inline-start: auto; padding-inline-start: 8px`,
124
+ body: `margin: calc(4px + 4px * ${T}) 0 0; padding-inline-start: calc(28px * ${F}); color: color-mix(in srgb, var(--ui-text-muted) calc(55% * ${T}), var(--ui-text)); font-size: calc(13px + 1px * ${T}); line-height: calc(20px + 1px * ${T}); white-space: pre-line; overflow-wrap: anywhere`,
125
+ command: `display: inline-block; margin-inline-end: calc(1px * ${F}); padding: 0 calc(5px * ${F}); border-radius: 5px; background: color-mix(in srgb, var(--_line) calc(100% * ${F}), transparent); color: var(--ui-text-muted); font-size: calc(12px + 2px * ${T}); line-height: calc(18px + 3px * ${T}); font-weight: calc(500 - 100 * ${T})`,
126
+ status: `display: flex; flex-direction: column; align-items: flex-start; margin-block-start: calc(4px + 4px * ${T}); padding-inline-start: calc(22px * ${F})`,
127
+ toggle: `${SB}; cursor: pointer`,
128
+ line: SB,
129
+ label: ELLIPSIS,
130
+ disc: `display: grid; place-items: center; flex: none; inline-size: calc(13px + 3px * ${T}); aspect-ratio: 1; margin: calc(1.5px * ${F}); border-radius: 50%; background: var(--_icb, light-dark(var(--_tone), color-mix(in srgb, var(--_tone) calc(100% - 38% * ${T}), var(--ui-bg-canvas)))); color: var(--_icc, light-dark(var(--ui-bg-surface), color-mix(in srgb, var(--ui-text) calc(100% * ${T}), var(--ui-bg-surface))))`,
131
+ check: `inline-size: calc(9px + 3px * ${T}); fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round`,
132
+ icon: 'inline-size: 16px; color: var(--ui-icon)',
133
+ chevron: `inline-size: calc(16px - 2px * ${T}); stroke-width: 1.25; color: color-mix(in srgb, var(--_ic) calc(100% * ${T}), var(--_cc)); rotate: var(--_r); transition: rotate calc(260ms * var(--_ms)) var(--ui-ease-out), color var(--_hv) var(--ui-ease-out)`,
134
+ fold: 'align-self: stretch; display: grid; grid-template-rows: var(--_g); transition: grid-template-rows calc(320ms * var(--_ms)) var(--ui-ease-out)',
135
+ details: `display: grid; gap: calc(4px + 2px * ${T}); min-block-size: 0; margin: 0; padding: 0; padding-inline-start: 28px; overflow: clip; overflow-clip-margin: 8px; list-style: none`,
136
+ detail: `position: relative; margin-block-start: calc(var(--k) * (2px + 3px * ${T})); color: var(--ui-text-muted); line-height: calc(20px - 1px * ${T}); opacity: var(--_o); translate: 0 var(--_y); transition: opacity var(--_d1, calc(160ms * var(--_ms))) var(--ui-ease-out) calc(var(--i) * var(--_dl)), translate var(--_d2, calc(220ms * var(--_ms))) var(--ui-ease-out) calc(var(--i) * var(--_dl))`,
137
+ elbow: `position: absolute; inset-inline-start: calc(-14.5px - 2px * ${T}); inset-block-start: calc(-3.5px - var(--k) * (3px - 1px * ${T})); inline-size: calc(9px + 1.5px * ${T}); block-size: calc(13.5px - .5px * ${T} + var(--k) * (3px - 1px * ${T})); border: 0 solid color-mix(in srgb, var(--ui-border-strong) calc(100% * ${T}), var(--ui-hairline)); border-inline-start-width: 1px; border-block-end-width: 1px; border-end-start-radius: 5px`,
138
+ composer: `display: flex; align-items: center; gap: calc(8px + 2px * ${T}); margin: 0; padding-block: calc(4px + 5px * ${T}) calc(10px - 1px * ${T}); padding-inline: var(--_px) calc(10px - 1px * ${T}); box-shadow: inset 0 calc(1px * ${F}) var(--_cl); transition: box-shadow var(--_hv) var(--ui-ease-out)`,
139
+ input: INPUT,
140
+ send: `position: relative; display: grid; place-items: center; flex: none; box-sizing: border-box; inline-size: var(--_ss); block-size: var(--_ss); padding: 0; border: 1px solid transparent; border-radius: calc(6px + 9px * ${T}); opacity: calc(1 - .7 * ${T} * var(--_off, 0)); background: color-mix(in srgb, var(--ui-bg-neutral-solid) calc(100% - 100% * ${F} * var(--_off, 0)), var(--_line)); color: var(--_sc); box-shadow: 0 0 0 calc(1px * ${T}) color-mix(in srgb, var(--ui-bg-neutral-solid) 85%, var(--ui-text-muted)), 0 calc(2px * ${T}) calc(4px * ${T}) var(--_sh), inset 0 calc(1px * ${T}) light-dark(transparent, var(--_wash)); cursor: var(--_cur); transition: var(--_sd, calc((180ms - 20ms * ${T}) * var(--_ms))) var(--ui-ease-out); transition-property: background-color, color, opacity, scale`,
141
+ arrow: `inline-size: calc(16px - 2px * ${T})`,
142
+ };
143
+
144
+ /** Put it on your own `<input>` inside `<ui-comment-thread>` to bind the draft with signal forms, reactive forms or ngModel. */
145
+ @Directive({
146
+ selector: 'input[uiThreadReply]',
147
+ host: {
148
+ class: 'ui-input',
149
+ autocomplete: 'off',
150
+ enterkeyhint: 'send',
151
+ dir: 'auto',
152
+ style: INPUT,
153
+ '[attr.aria-label]': 'ariaLabel() ?? thread.text().reply',
154
+ '[attr.placeholder]': 'placeholder() ?? thread.hint()',
155
+ '(input)': 'thread.draft.set(el.value)',
156
+ },
157
+ })
158
+ export class UiThreadReply {
159
+ readonly placeholder = input<string>();
160
+ readonly ariaLabel = input<string>(undefined, { alias: 'aria-label' });
161
+ readonly el = inject<ElementRef<HTMLInputElement>>(ElementRef).nativeElement;
162
+ protected readonly thread = inject(UiCommentThread);
163
+
164
+ constructor() {
165
+ const { el, thread } = this;
166
+ const own = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(el), 'value');
167
+ if (!own?.get || !own.set) return;
168
+ const { get, set } = own;
169
+ Object.defineProperty(el, 'value', {
170
+ configurable: true,
171
+ get: () => get.call(el),
172
+ set: (v: string) => {
173
+ set.call(el, v);
174
+ queueMicrotask(() => thread.draft.set(get.call(el)));
175
+ },
176
+ });
177
+ inject(DestroyRef).onDestroy(() => Reflect.deleteProperty(el, 'value'));
178
+ }
179
+ }
180
+
181
+ @Component({
182
+ selector: 'ui-comment-thread',
183
+ imports: [NgTemplateOutlet],
184
+ host: {
185
+ style: `--_t: var(--ui-look-tray, 0); --_f: calc(1 - var(--_t)); --_ms: var(--ui-motion-scale); --_px: calc(16px + 2px * var(--_t)); --_xs: 28px; --_sb: calc(28px - 2px * var(--_t)); --_ss: calc(28px + 2px * var(--_t)); --_ih: calc(28px + 4px * var(--_t)); --_min: var(--ui-comment-thread-list-min, 290px); --_max: var(--ui-comment-thread-list-max, 340px); --_line: rgb(from var(--ui-text) r g b / calc(.065 + .035 * r / 255)); --_wash: rgb(from var(--ui-text) r g b / calc(.036 + .024 * r / 255)); --_sh: color-mix(in srgb, var(--ui-gray-1000) 6%, transparent); --_ic: color-mix(in srgb, var(--ui-text-placeholder) 85%, var(--ui-bg-surface)); --_hv: calc((160ms - 20ms * var(--_t)) * var(--_ms)); --ui-rise-distance: 4px; color: var(--ui-text); font: var(--ui-text-base) / var(--ui-leading-base) var(--ui-font-sans)`,
186
+ },
187
+ template: `
188
+ <section class="card" [style]="s.card" [attr.aria-labelledby]="uid">
189
+ <header [style]="s.head">
190
+ <h3 [style]="s.title" [id]="uid" [title]="title()" dir="auto">{{ title() }}</h3>
191
+ <span [style]="s.count" dir="auto">{{ text().count(messages().length) }}</span>
192
+ @if (closable()) {
193
+ <button type="button" class="ui-btn-icon x" [style]="s.close" [attr.aria-label]="text().close" (click)="closed.emit()">
194
+ <svg class="ui-icon" style="stroke-width: 1.25" viewBox="0 0 16 16" aria-hidden="true"><path d="m4.5 4.5 7 7m0-7-7 7" /></svg>
195
+ </button>
196
+ }
197
+ </header>
198
+ <div [style]="s.box" [style.block-size.px]="boxH()">
199
+ <div #list class="list" [style]="s.list" role="log" aria-relevant="additions" [attr.tabindex]="scrolls() ? 0 : null" [attr.aria-label]="text().messages" [attr.data-scrolled]="scrolled() || null" [style.--_lock.px]="lock()" (scroll)="scrolled.set(list.scrollTop > 0)" (transitionend)="fit()" (transitioncancel)="fit()">
200
+ @for (r of rows(); track r.m.id) {
201
+ <article style="display: grid; min-inline-size: 0" [attr.data-intro]="intro()[r.m.id]">
202
+ <ng-container *ngTemplateOutlet="meta; context: { $implicit: r.m.author, r }" />
203
+ @if (r.text || r.command) {
204
+ <p [style]="s.body" dir="auto">@if (r.command) {<span class="cmd" [style]="s.command">/{{ r.command }}</span>&ngsp;}{{ r.text }}</p>
205
+ }
206
+ @if (r.m.status; as st) {
207
+ <div [style]="s.status">
208
+ @if (st.details?.length) {
209
+ <button type="button" class="sb" [style]="s.toggle" [attr.aria-expanded]="open().has(r.m.id)" [attr.aria-controls]="uid + '-' + $index" (click)="toggle(r.m.id)">
210
+ <ng-container *ngTemplateOutlet="state; context: { $implicit: st }" />
211
+ <svg class="ui-icon" [style]="s.chevron" viewBox="0 0 16 16" aria-hidden="true"><path d="m4.5 6.5 3.5 3.5 3.5-3.5" /></svg>
212
+ </button>
213
+ <div class="fold" [style]="s.fold" [id]="uid + '-' + $index" [inert]="!open().has(r.m.id)">
214
+ <ul [style]="s.details">
215
+ @for (d of st.details; track $index) {
216
+ <li [style]="s.detail" [style.--i]="$index + 1" [style.--k]="$first ? 1 : 0"><span aria-hidden="true" [style]="s.elbow"></span><span dir="auto">{{ d }}</span></li>
217
+ }
218
+ </ul>
219
+ </div>
220
+ } @else {
221
+ <span [style]="s.line"><ng-container *ngTemplateOutlet="state; context: { $implicit: st }" /></span>
222
+ }
223
+ </div>
224
+ }
225
+ </article>
226
+ }
227
+ @if (thinking() && assistant(); as a) {
228
+ <article style="display: grid" [attr.aria-label]="text().isThinking(a.name)">
229
+ <ng-container *ngTemplateOutlet="meta; context: { $implicit: a }" />
230
+ <p [style]="s.body"><span class="ui-shimmer">{{ text().thinking }}</span></p>
231
+ </article>
232
+ }
233
+ @if (failed() !== null && assistant(); as a) {
234
+ <article class="err" style="display: grid">
235
+ <ng-container *ngTemplateOutlet="meta; context: { $implicit: a }" />
236
+ <p [style]="s.body" [style.color]="'var(--ui-text-muted)'">{{ text().failed }}</p>
237
+ <div [style]="s.status">
238
+ <button type="button" class="sb" [style]="s.toggle" (click)="retry()">
239
+ <svg class="ui-icon" [style]="s.icon" viewBox="0 0 16 16" aria-hidden="true"><path d="M3 8a5 5 0 1 0 1.5-3.5M3 2.5V5h2.5" /></svg>
240
+ <span [style]="s.label">{{ text().retry }}</span>
241
+ </button>
242
+ </div>
243
+ </article>
244
+ }
245
+ </div>
246
+ </div>
247
+ <form [style]="s.composer" (submit)="send($event)">
248
+ <ng-content select="input[uiThreadReply]">
249
+ <input #own class="ui-input" [style]="s.input" dir="auto" autocomplete="off" enterkeyhint="send" [attr.aria-label]="text().reply" [placeholder]="hint()" (input)="draft.set(own.value)" />
250
+ </ng-content>
251
+ <button type="submit" class="send" [style]="s.send" [disabled]="!ready()" [attr.aria-label]="text().send">
252
+ <svg class="ui-icon" [style]="s.arrow" viewBox="0 0 16 16" aria-hidden="true"><path d="M8 13V3M4 7l4-4 4 4" /></svg>
253
+ </button>
254
+ </form>
255
+ </section>
256
+
257
+ <ng-template #meta let-a let-r="r">
258
+ <div class="meta" [style]="s.meta">
259
+ <span [style]="s.avatar" [style.background]="a.assistant && !a.avatar ? s.mark : null" aria-hidden="true">
260
+ @if (a.avatar) {
261
+ <img [style]="s.img" [src]="a.avatar" alt="" width="20" height="20" loading="lazy" />
262
+ } @else if (!a.assistant) {
263
+ {{ a.name.trim().charAt(0) }}
264
+ }
265
+ </span>
266
+ <span [style]="s.name" dir="auto" [title]="a.name">{{ a.name }}</span>
267
+ @if (r?.iso) {
268
+ <time [style]="s.time" [attr.datetime]="r.iso" [title]="r.full">{{ r.ago }}</time>
269
+ }
270
+ </div>
271
+ </ng-template>
272
+
273
+ <ng-template #state let-st>
274
+ <span class="ic" [style]="s.disc" [style.--_tone]="'var(--ui-' + (st.tone ?? 'success') + ')'">
275
+ <svg [style]="s.check" viewBox="0 0 12 12" aria-hidden="true"><path d="M3.5 6.2 5.2 7.9 8.5 4.3" /></svg>
276
+ </span>
277
+ <span [style]="s.label" dir="auto" [title]="st.label">{{ st.label }}</span>
278
+ </ng-template>
279
+ `,
280
+ styles: `
281
+ :host { --_bg: none; --_cc: var(--_ic); --_pe: var(--ui-border); --_r: 0deg; --_g: 0fr; --_o: 0; --_y: -4px; --_dl: 0ms; --_cl: transparent; --_cur: pointer; display: block; inline-size: min(var(--ui-comment-thread-width, 420px), 100%); }
282
+ .send { --_sc: color-mix(in srgb, var(--ui-text-on-neutral) calc(100% - 100% * var(--_f) * var(--_off, 0)), var(--ui-text-disabled)); }
283
+ :host::before { content: 'x x x x x x x x' / ''; display: block; block-size: 0; overflow: hidden; font-size: 0; word-spacing: calc(var(--ui-comment-thread-width, 420px) / 7); }
284
+ .list:focus-visible { box-shadow: inset 0 0 0 var(--ui-focus-width) var(--ui-focus-ring); }
285
+ .list[data-scrolled] { mask-image: linear-gradient(transparent, var(--ui-text) 14px); }
286
+ article > * { animation: ui-rise calc(420ms * var(--_ms)) var(--ui-ease-out) both; }
287
+ article > :not(.meta) { animation-delay: calc(80ms * var(--_ms)); }
288
+ [data-intro=late] > * { animation-delay: calc(700ms * var(--_ms)); }
289
+ [data-intro=late] > :not(.meta) { animation-delay: calc(780ms * var(--_ms)); }
290
+ [data-intro=still] > * { animation: none; }
291
+ .err > p { animation: ui-nudge calc(400ms * var(--_ms)) var(--ui-ease-out); }
292
+ @container style(--ui-look-tray: 1) { .cmd { text-transform: capitalize; } }
293
+ @media (hover: hover) { :is(.x, button.sb):hover { --_bg: var(--_wash); --_cc: var(--ui-text); } }
294
+ [aria-expanded=true] { --_r: 180deg; }
295
+ [aria-expanded=true] + .fold { --_g: 1fr; --_o: 1; --_y: 0px; --_d1: calc(320ms * var(--_ms)); --_d2: calc(420ms * var(--_ms)); --_dl: calc(60ms * var(--_ms)); }
296
+ .card:has(form:focus-within) { --_pe: var(--ui-border-strong); --_cl: var(--_line); }
297
+ .send:active:enabled { scale: .94; --_sd: calc(160ms * var(--_ms)); }
298
+ .send:disabled { --_off: 1; --_cur: default; }
299
+ :is(.x, .sb, .send):focus-visible { outline: var(--ui-focus-width) solid var(--ui-focus-ring); outline-offset: var(--ui-focus-offset); }
300
+ @media (pointer: coarse) {
301
+ * { --_px: calc(12px + 6px * var(--_t)); --_xs: calc(34px - 6px * var(--_t)); --_sb: calc(34px - 8px * var(--_t)); --_ss: calc(34px - 4px * var(--_t)); --_ih: calc(34px - 2px * var(--_t)); }
302
+ :is(.x, .sb, .send)::after { content: ''; position: absolute; inset: min(0px, calc(50% - 22px)); }
303
+ }
304
+ @container (width < 400px) {
305
+ * { --_px: calc(12px + 6px * var(--_t)); --_min: var(--ui-comment-thread-list-min, 250px); --_max: var(--ui-comment-thread-list-max, 280px); }
306
+ }
307
+ @media (forced-colors: active) {
308
+ .card { outline: calc(1px * var(--_f)) solid CanvasText; }
309
+ .send:disabled { --_sc: GrayText; }
310
+ .ic { forced-color-adjust: none; --_icb: CanvasText; --_icc: Canvas; }
311
+ }
312
+ `,
313
+ })
314
+ export class UiCommentThread {
315
+ /** The conversation, oldest first. Bind `[(messages)]`: the thread appends the replies and answers itself. */
316
+ readonly messages = model<ThreadMessage[]>([]);
317
+ /** Who replies typed here are from. */
318
+ readonly currentUser = input.required<ThreadAuthor>();
319
+ /** Shown thinking while `replyHandler`'s promise is pending, and named in the placeholder. */
320
+ readonly assistant = input<ThreadAuthor>();
321
+ /** Called with each reply. Return a message (or a promise of one) and it is posted as the answer. */
322
+ readonly replyHandler = input<ThreadReplyHandler>();
323
+ /** True while an answer is on the way. The thread sets it; you can set it too. */
324
+ readonly thinking = model(false);
325
+ /** Heading of the card, and the name of its region. */
326
+ readonly title = input('Comment');
327
+ /** Shows the close button, which emits `closed`. */
328
+ readonly closable = input(false, { transform: booleanAttribute });
329
+ /** On first render the messages rise in, and assistant replies follow 700 ms later. */
330
+ readonly staged = input(true, { transform: booleanAttribute });
331
+ /** Every visible and spoken string. */
332
+ readonly labels = input<Partial<CommentThreadLabels>>({});
333
+ /** Each reply, trimmed, right after it is posted. */
334
+ readonly reply = output<string>();
335
+ readonly closed = output<void>();
336
+
337
+ protected readonly s = S;
338
+ protected readonly uid = `ui-comment-thread-${nextId++}`;
339
+ readonly text = computed<CommentThreadLabels>(() => ({ ...LABELS, ...this.labels() }));
340
+ readonly hint = computed(() => this.text().placeholder(this.assistant()?.name));
341
+ readonly draft = signal('');
342
+ protected readonly ready = computed(() => !!this.draft().trim() && !this.thinking());
343
+ protected readonly open = signal<ReadonlySet<string>>(new Set());
344
+ protected readonly scrolled = signal(false);
345
+ protected readonly scrolls = signal(false);
346
+ protected readonly lock = signal<number | null>(null);
347
+ protected readonly boxH = signal<number | null>(null);
348
+ protected readonly failed = signal<string | null>(null);
349
+ private readonly now = signal(Date.now());
350
+ private readonly locale = inject(LOCALE_ID);
351
+ private readonly fmt = new Intl.DateTimeFormat(this.locale, { dateStyle: 'medium', timeStyle: 'short' });
352
+ private readonly projected = contentChild(UiThreadReply);
353
+ private readonly own = viewChild<ElementRef<HTMLInputElement>>('own');
354
+ private readonly list = viewChild.required<ElementRef<HTMLElement>>('list');
355
+ private first?: Record<string, 'late' | 'still'>;
356
+
357
+ protected readonly intro = computed(() => {
358
+ const list = this.messages();
359
+ const pick = (m: ThreadMessage, i: number) => (!this.staged() ? 'still' : i && m.author.assistant ? 'late' : null);
360
+ return (this.first ??= Object.fromEntries(list.flatMap((m, i) => (pick(m, i) ? [[m.id, pick(m, i)!]] : []))));
361
+ });
362
+
363
+ protected readonly rows = computed(() => {
364
+ const now = this.now();
365
+ const t = this.text();
366
+ return this.messages().map((m) => {
367
+ const at = new Date(m.createdAt);
368
+ const valid = !isNaN(at.getTime());
369
+ const lead = m.command === undefined ? COMMAND.exec(m.text ?? '') : null;
370
+ const mins = Math.floor((now - at.getTime()) / 60000);
371
+ const ago =
372
+ mins < 1 ? t.justNow : mins < 60 ? t.ago(mins, 'minute') : mins < 1440 ? t.ago(Math.floor(mins / 60), 'hour') : mins < 10080 ? t.ago(Math.floor(mins / 1440), 'day') : this.fmt.format(at);
373
+ return {
374
+ m,
375
+ command: (m.command ?? lead?.[1])?.replace(/^\//, ''),
376
+ text: lead ? m.text!.slice(lead[0].length) : m.text,
377
+ iso: valid ? at.toISOString() : null,
378
+ full: valid ? this.fmt.format(at) : '',
379
+ ago,
380
+ };
381
+ });
382
+ });
383
+
384
+ constructor() {
385
+ const destroy = inject(DestroyRef);
386
+ let seen: [number, boolean, boolean] | undefined;
387
+ let anim = 0;
388
+
389
+ afterNextRender(() => {
390
+ const el = this.field();
391
+ if (el) this.draft.set(el.value);
392
+ if (isDevMode() && !this.title().trim()) console.warn('ui-comment-thread: give it a title; it names the region.');
393
+ const timer = setInterval(() => this.now.set(Date.now()), 30000);
394
+ const list = this.list().nativeElement;
395
+ const view = list.ownerDocument.defaultView;
396
+ const ro = view?.ResizeObserver
397
+ ? new view.ResizeObserver(() => {
398
+ this.boxH.set(list.offsetHeight);
399
+ this.fit();
400
+ })
401
+ : null;
402
+ ro?.observe(list);
403
+ destroy.onDestroy(() => {
404
+ clearInterval(timer);
405
+ ro?.disconnect();
406
+ view?.cancelAnimationFrame(anim);
407
+ });
408
+ });
409
+
410
+ afterRenderEffect(() => {
411
+ this.rows();
412
+ this.thinking();
413
+ this.failed();
414
+ this.open();
415
+ this.fit();
416
+ });
417
+
418
+ afterRenderEffect(() => {
419
+ const now: [number, boolean, boolean] = [this.messages().length, this.thinking(), this.failed() !== null];
420
+ const grew = seen && (now[0] > seen[0] || (now[1] && !seen[1]) || (now[2] && !seen[2]));
421
+ seen = now;
422
+ if (!grew) return;
423
+ const list = this.list().nativeElement;
424
+ const from = list.scrollTop;
425
+ const end = () => list.scrollHeight - list.clientHeight;
426
+ if (end() <= from) return;
427
+ const view = list.ownerDocument.defaultView!;
428
+ if (view.matchMedia('(prefers-reduced-motion: reduce)').matches || parseFloat(view.getComputedStyle(list).getPropertyValue('--ui-motion-scale')) === 0) {
429
+ list.scrollTop = end();
430
+ return;
431
+ }
432
+ const start = performance.now();
433
+ const step = (time: number) => {
434
+ const p = Math.min(1, (time - start) / 140);
435
+ list.scrollTop = from + (end() - from) * (1 - (1 - p) ** 3);
436
+ if (p < 1) anim = view.requestAnimationFrame(step);
437
+ };
438
+ view.cancelAnimationFrame(anim);
439
+ anim = view.requestAnimationFrame(step);
440
+ });
441
+ }
442
+
443
+ protected toggle(id: string) {
444
+ this.open.update((s) => {
445
+ const next = new Set(s);
446
+ if (!next.delete(id)) next.add(id);
447
+ return next;
448
+ });
449
+ }
450
+
451
+ protected send(event: Event) {
452
+ event.preventDefault();
453
+ const el = this.field();
454
+ const value = (el?.value ?? this.draft()).trim();
455
+ if (!value || this.thinking()) return;
456
+ if (el) {
457
+ el.value = '';
458
+ el.dispatchEvent(new Event('input', { bubbles: true }));
459
+ }
460
+ this.draft.set('');
461
+ this.lock.update((h) => h ?? this.list().nativeElement.offsetHeight);
462
+ this.failed.set(null);
463
+ this.post({ id: `${this.uid}-${Date.now().toString(36)}-${nextId++}`, author: this.currentUser(), createdAt: new Date(), text: value });
464
+ this.reply.emit(value);
465
+ this.ask(value);
466
+ }
467
+
468
+ protected retry() {
469
+ const value = this.failed();
470
+ this.failed.set(null);
471
+ if (value !== null) this.ask(value);
472
+ }
473
+
474
+ private post(message: ThreadMessage) {
475
+ this.messages.update((list) => [...list, message]);
476
+ }
477
+
478
+ private async ask(value: string) {
479
+ const result = this.replyHandler()?.(value);
480
+ if (!result || !('then' in result)) {
481
+ if (result) this.post(result);
482
+ return;
483
+ }
484
+ this.thinking.set(true);
485
+ try {
486
+ const answer = await result;
487
+ if (answer) this.post(answer);
488
+ } catch {
489
+ this.failed.set(value);
490
+ } finally {
491
+ this.thinking.set(false);
492
+ }
493
+ }
494
+
495
+ protected fit() {
496
+ const list = this.list().nativeElement;
497
+ this.scrolls.set(list.scrollHeight > list.clientHeight);
498
+ }
499
+
500
+ private field() {
501
+ return this.projected()?.el ?? this.own()?.nativeElement;
502
+ }
503
+ }