@zeyos/zx 5.5.0 → 5.7.0

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 (235) hide show
  1. package/CHANGELOG.md +210 -0
  2. package/README.md +73 -7
  3. package/bin/zx-css.js +328 -0
  4. package/dist/types/components/account-menu/account-menu.d.ts +4 -2
  5. package/dist/types/components/activity-list/activity-list.d.ts +14 -8
  6. package/dist/types/components/agent-thread/ag-ui.d.ts +340 -0
  7. package/dist/types/components/agent-thread/agent-thread.d.ts +711 -0
  8. package/dist/types/components/agent-thread/ui-message-stream.d.ts +401 -0
  9. package/dist/types/components/app-sidebar/app-sidebar.d.ts +14 -8
  10. package/dist/types/components/approval-card/approval-card.d.ts +596 -0
  11. package/dist/types/components/avatar/avatar.d.ts +91 -4
  12. package/dist/types/components/block-view/block-view.d.ts +205 -0
  13. package/dist/types/components/breadcrumb/breadcrumb.d.ts +3 -3
  14. package/dist/types/components/calendar/calendar-model.d.ts +9 -2
  15. package/dist/types/components/calendar/calendar.d.ts +12 -4
  16. package/dist/types/components/chart/chart.d.ts +25 -13
  17. package/dist/types/components/code-editor/code-editor.d.ts +7 -2
  18. package/dist/types/components/composer/composer.d.ts +938 -0
  19. package/dist/types/components/composer/queue.d.ts +76 -0
  20. package/dist/types/components/copy/copy-button.d.ts +6 -4
  21. package/dist/types/components/copy/copy-input.d.ts +10 -6
  22. package/dist/types/components/datebox/date-range-box.d.ts +33 -0
  23. package/dist/types/components/datebox/placeholder.d.ts +7 -0
  24. package/dist/types/components/dock/dock.d.ts +12 -3
  25. package/dist/types/components/entity-ref/entity-ref.d.ts +5 -3
  26. package/dist/types/components/field-upload/field-upload.d.ts +3 -2
  27. package/dist/types/components/file-item/file-item.d.ts +3 -2
  28. package/dist/types/components/file-list/file-list.d.ts +14 -8
  29. package/dist/types/components/filter/filter-model.d.ts +31 -1
  30. package/dist/types/components/filter/filter.d.ts +20 -2
  31. package/dist/types/components/finder/finder.d.ts +14 -6
  32. package/dist/types/components/flag/flag.d.ts +99 -0
  33. package/dist/types/components/grid/grid.d.ts +1 -1
  34. package/dist/types/components/kanban-view/kanban-view.d.ts +8 -66
  35. package/dist/types/components/launcher/launcher.d.ts +2 -0
  36. package/dist/types/components/multi-value-editor/multi-value-editor.d.ts +3 -2
  37. package/dist/types/components/number-field/number-field.d.ts +17 -6
  38. package/dist/types/components/prompt-starters/prompt-starters.d.ts +107 -0
  39. package/dist/types/components/prose/prose.d.ts +9 -6
  40. package/dist/types/components/questionnaire/conditions.d.ts +270 -0
  41. package/dist/types/components/questionnaire/items.d.ts +52 -30
  42. package/dist/types/components/questionnaire/questionnaire.d.ts +44 -4
  43. package/dist/types/components/rating/rating.d.ts +5 -3
  44. package/dist/types/components/run-status/run-status.d.ts +393 -0
  45. package/dist/types/components/run-trace/run-trace.d.ts +624 -0
  46. package/dist/types/components/select/country.d.ts +106 -0
  47. package/dist/types/components/select/field-adapter.d.ts +2 -1
  48. package/dist/types/components/select/locale.d.ts +102 -0
  49. package/dist/types/components/select/select.d.ts +41 -2
  50. package/dist/types/components/slider/slider.d.ts +1 -0
  51. package/dist/types/components/sort-control/sort-control.d.ts +7 -3
  52. package/dist/types/components/source-list/source-list.d.ts +179 -0
  53. package/dist/types/components/split-view/split-view.d.ts +5 -3
  54. package/dist/types/components/surface/catalog.d.ts +260 -0
  55. package/dist/types/components/surface/elicit.d.ts +81 -0
  56. package/dist/types/components/surface/mcp.d.ts +165 -0
  57. package/dist/types/components/surface/schema.d.ts +314 -0
  58. package/dist/types/components/surface/stream.d.ts +118 -0
  59. package/dist/types/components/surface/surface.d.ts +718 -0
  60. package/dist/types/components/surface/text.d.ts +277 -0
  61. package/dist/types/components/surface/validate.d.ts +339 -0
  62. package/dist/types/components/table/table.d.ts +55 -3
  63. package/dist/types/components/table/window.d.ts +203 -0
  64. package/dist/types/components/toolbar/toolbar.d.ts +10 -6
  65. package/dist/types/components/tooltip/tooltip.d.ts +2 -2
  66. package/dist/types/components/tree/tree.d.ts +20 -13
  67. package/dist/types/core/component.d.ts +10 -0
  68. package/dist/types/core/date.d.ts +8 -1
  69. package/dist/types/core/dom.d.ts +1 -0
  70. package/dist/types/core/http.d.ts +10 -0
  71. package/dist/types/core/i18n.d.ts +38 -0
  72. package/dist/types/core/messages.d.ts +479 -1
  73. package/dist/types/core/storage.d.ts +7 -2
  74. package/dist/types/elements/define.d.ts +21 -46
  75. package/dist/types/elements/definitions.d.ts +147 -0
  76. package/dist/types/elements/kit.d.ts +7 -0
  77. package/dist/types/elements/reflect.d.ts +6 -3
  78. package/dist/types/index.d.ts +18 -1
  79. package/dist/types/internal/agent-model.d.ts +587 -0
  80. package/dist/types/internal/date-locale.d.ts +7 -0
  81. package/dist/types/internal/highlight.d.ts +24 -0
  82. package/dist/types/internal/json-patch.d.ts +146 -0
  83. package/dist/types/internal/presence-loop.d.ts +148 -0
  84. package/dist/types/locales/de.d.ts +13 -0
  85. package/dist/types/locales/en.d.ts +2 -0
  86. package/dist/types/locales/index.d.ts +823 -0
  87. package/dist/types/zeyos/view.d.ts +8 -1
  88. package/dist/zx-zeyos.esm.js +20505 -18748
  89. package/dist/zx-zeyos.esm.min.js +5 -5
  90. package/dist/zx.core.css +1276 -0
  91. package/dist/zx.core.min.css +1 -0
  92. package/dist/zx.css +2424 -134
  93. package/dist/zx.esm.js +46167 -28468
  94. package/dist/zx.esm.min.js +40 -8
  95. package/dist/zx.global.js +46167 -28468
  96. package/dist/zx.global.min.js +40 -8
  97. package/dist/zx.layered.css +17302 -0
  98. package/dist/zx.layered.min.css +1 -0
  99. package/dist/zx.min.css +1 -1
  100. package/docs/agent-catalog.json +12501 -0
  101. package/docs/agent-surfaces.md +340 -0
  102. package/docs/api.json +2057 -166
  103. package/docs/llms.md +1458 -58
  104. package/docs/llms.txt +38 -8
  105. package/package.json +29 -6
  106. package/src/components/account-menu/account-menu.js +6 -3
  107. package/src/components/activity-item/activity-item.js +2 -1
  108. package/src/components/activity-list/activity-list.js +16 -10
  109. package/src/components/agent-thread/ag-ui.js +1439 -0
  110. package/src/components/agent-thread/agent-thread.css +417 -0
  111. package/src/components/agent-thread/agent-thread.js +1670 -0
  112. package/src/components/agent-thread/ui-message-stream.js +1281 -0
  113. package/src/components/app-sidebar/app-sidebar.js +18 -8
  114. package/src/components/approval-card/approval-card.css +289 -0
  115. package/src/components/approval-card/approval-card.js +1378 -0
  116. package/src/components/avatar/avatar.css +56 -1
  117. package/src/components/avatar/avatar.js +148 -26
  118. package/src/components/block-view/block-view.css +300 -0
  119. package/src/components/block-view/block-view.js +524 -0
  120. package/src/components/breadcrumb/breadcrumb.js +8 -5
  121. package/src/components/calendar/calendar-model.js +8 -2
  122. package/src/components/calendar/calendar.js +53 -18
  123. package/src/components/chart/chart.js +54 -23
  124. package/src/components/code-editor/code-editor.js +45 -8
  125. package/src/components/code-editor/field-adapter.js +1 -1
  126. package/src/components/composer/composer.css +482 -0
  127. package/src/components/composer/composer.js +1815 -0
  128. package/src/components/composer/queue.js +78 -0
  129. package/src/components/context-menu/context-menu.js +12 -1
  130. package/src/components/copy/copy-button.js +9 -7
  131. package/src/components/copy/copy-input.js +10 -6
  132. package/src/components/date-picker/date-picker.js +2 -1
  133. package/src/components/date-picker/date-range-picker.js +8 -7
  134. package/src/components/datebox/date-range-box.js +3 -9
  135. package/src/components/datebox/datebox.js +18 -21
  136. package/src/components/datebox/placeholder.js +18 -0
  137. package/src/components/dialog/dialog.js +1 -1
  138. package/src/components/dock/dock.js +34 -13
  139. package/src/components/entity-ref/entity-ref.js +16 -5
  140. package/src/components/field-upload/field-upload.js +81 -38
  141. package/src/components/file-item/file-item.js +70 -40
  142. package/src/components/file-list/file-list.js +15 -9
  143. package/src/components/filter/filter-model.js +46 -8
  144. package/src/components/filter/filter.css +3 -3
  145. package/src/components/filter/filter.js +29 -10
  146. package/src/components/filter-panel/filter-panel.js +13 -5
  147. package/src/components/finder/finder.js +50 -26
  148. package/src/components/flag/flag.css +40 -0
  149. package/src/components/flag/flag.js +177 -0
  150. package/src/components/form/form.js +1 -1
  151. package/src/components/grid/grid.js +22 -8
  152. package/src/components/kanban-view/kanban-view.js +51 -81
  153. package/src/components/launcher/launcher.js +17 -2
  154. package/src/components/list-toolbar/list-toolbar.js +2 -1
  155. package/src/components/master-panel/master-panel.js +1 -1
  156. package/src/components/menu-button/menu-button.js +4 -0
  157. package/src/components/message/message.js +3 -0
  158. package/src/components/multi-value-editor/multi-value-editor.js +10 -9
  159. package/src/components/navigation-bar/navigation-bar.js +3 -3
  160. package/src/components/number-field/number-field.js +29 -13
  161. package/src/components/prompt-starters/prompt-starters.css +91 -0
  162. package/src/components/prompt-starters/prompt-starters.js +206 -0
  163. package/src/components/prose/prose.js +14 -10
  164. package/src/components/questionnaire/conditions.js +666 -0
  165. package/src/components/questionnaire/items.js +150 -41
  166. package/src/components/questionnaire/questionnaire.js +126 -32
  167. package/src/components/rating/rating.js +5 -4
  168. package/src/components/run-status/run-status.css +208 -0
  169. package/src/components/run-status/run-status.js +782 -0
  170. package/src/components/run-trace/run-trace.css +443 -0
  171. package/src/components/run-trace/run-trace.js +1209 -0
  172. package/src/components/select/country.js +136 -0
  173. package/src/components/select/field-adapter.js +7 -2
  174. package/src/components/select/locale.js +219 -0
  175. package/src/components/select/select.css +20 -22
  176. package/src/components/select/select.js +94 -12
  177. package/src/components/sheet/sheet-stack.js +10 -1
  178. package/src/components/slider/slider.js +7 -2
  179. package/src/components/sort-control/sort-control.js +15 -5
  180. package/src/components/source-list/source-list.css +208 -0
  181. package/src/components/source-list/source-list.js +358 -0
  182. package/src/components/split-view/split-view.js +5 -3
  183. package/src/components/surface/catalog.js +1334 -0
  184. package/src/components/surface/elicit.js +107 -0
  185. package/src/components/surface/mcp.js +243 -0
  186. package/src/components/surface/schema.js +644 -0
  187. package/src/components/surface/stream.js +245 -0
  188. package/src/components/surface/surface.css +308 -0
  189. package/src/components/surface/surface.js +2291 -0
  190. package/src/components/surface/text.js +513 -0
  191. package/src/components/surface/validate.js +2218 -0
  192. package/src/components/tabbox/tabbox.js +9 -0
  193. package/src/components/table/table.css +63 -3
  194. package/src/components/table/table.js +167 -28
  195. package/src/components/table/window.js +317 -0
  196. package/src/components/toolbar/toolbar.js +12 -7
  197. package/src/components/tooltip/tooltip.css +2 -3
  198. package/src/components/tooltip/tooltip.js +32 -5
  199. package/src/components/tree/tree.js +53 -23
  200. package/src/core/component.js +25 -1
  201. package/src/core/date.js +62 -16
  202. package/src/core/dom.js +22 -2
  203. package/src/core/format.js +15 -13
  204. package/src/core/http.js +29 -20
  205. package/src/core/i18n.js +83 -10
  206. package/src/core/keyboard.js +79 -8
  207. package/src/core/messages.js +519 -2
  208. package/src/core/overlay-host.js +1 -0
  209. package/src/core/storage.js +59 -6
  210. package/src/elements/define.js +178 -130
  211. package/src/elements/definitions.js +229 -0
  212. package/src/elements/kit.js +444 -0
  213. package/src/elements/reflect.js +8 -3
  214. package/src/index.js +18 -1
  215. package/src/internal/agent-model.js +488 -0
  216. package/src/internal/date-locale.js +11 -0
  217. package/src/internal/highlight.js +104 -0
  218. package/src/internal/json-patch.js +405 -0
  219. package/src/internal/presence-loop.js +343 -0
  220. package/src/locales/de.js +911 -0
  221. package/src/locales/en.js +2 -0
  222. package/src/locales/index.js +7 -0
  223. package/src/zeyos/calendar.js +2 -1
  224. package/src/zeyos/form.js +2 -1
  225. package/src/zeyos/launcher.js +7 -6
  226. package/src/zeyos/module-bar.js +2 -1
  227. package/src/zeyos/schema.js +37 -23
  228. package/src/zeyos/select.js +4 -3
  229. package/src/zeyos/table.js +2 -13
  230. package/src/zeyos/view.js +57 -17
  231. package/styles/core.css +11 -0
  232. package/styles/tokens/semantic.css +1 -0
  233. package/styles/zeyos.css +2 -0
  234. package/styles/zx.css +11 -8
  235. package/styles/zx.layered.css +1 -0
@@ -0,0 +1,524 @@
1
+ // @ts-check
2
+ import { Component } from '../../core/component.js';
3
+ import { h, restoreTarget, snapshotTarget } from '../../core/dom.js';
4
+ import { translate } from '../../core/i18n.js';
5
+ import { icon } from '../../core/icons.js';
6
+ import { blocksToMarkdown, blocksToText, figureRow, parseInline } from '../../internal/agent-model.js';
7
+ import { highlight } from '../../internal/highlight.js';
8
+ import { copyButton } from '../copy/copy-button.js';
9
+
10
+ /** @typedef {import('../../internal/agent-model.js').Block} Block */
11
+ /** @typedef {import('../../internal/agent-model.js').Source} Source */
12
+
13
+ /**
14
+ * @typedef {Object} BlockViewOptions
15
+ * @property {Block[]} [blocks=[]] The answer's blocks. Model text is data: every string reaches
16
+ * the page as text, through the inline grammar of `**bold**`, `` `code` `` and `[n]` citations.
17
+ * @property {2|3|4|5} [headingLevel=3] Element level of a `heading` block of level 2; a level-3
18
+ * heading goes one deeper. Set it one below the heading that names the answer.
19
+ * @property {'sm'|'md'} [size='sm'] Prose size: `'sm'` sets the answer at the interface size, for
20
+ * a conversation; `'md'` at the reading size, for a document.
21
+ * @property {boolean} [streaming=false] Whether more is still being written. The last block stays
22
+ * open to `appendText()`, and the root is busy until `end()` or `stop()`.
23
+ * @property {Source[]|null} [sources=null] The sources citations point at; their titles name the
24
+ * citation buttons.
25
+ * @property {Record<string, string>|Record<string, Record<string, string>>} [msg] Localized messages.
26
+ * @property {(event: CustomEvent<{n: number, source: Source|null}>) => void} [oncite] Citation listener.
27
+ * @property {(event: CustomEvent<{id: string}>) => void} [onartifact] Artifact listener.
28
+ * @property {(event: CustomEvent<Record<string, never>>) => void} [onend] End-of-stream listener.
29
+ */
30
+
31
+ /**
32
+ * Options for drawing blocks without a component.
33
+ * @typedef {Object} RenderBlocksOptions
34
+ * @property {2|3|4|5} [headingLevel=3] Element level of a level-2 `heading` block.
35
+ * @property {(key: string, ...args: unknown[]) => string} [msg] Message lookup for the few words
36
+ * the blocks add (code copy, artifact kinds).
37
+ */
38
+
39
+ const MESSAGES = Object.freeze({
40
+ 'blockView.cite': 'Source %1',
41
+ 'blockView.citeTitled': 'Source %1: %2',
42
+ 'blockView.copyCode': 'Copy code',
43
+ 'blockView.code': 'Code',
44
+ 'blockView.document': 'Document',
45
+ 'blockView.versions': '%1 versions',
46
+ 'blockView.version': '1 version',
47
+ 'blockView.table': 'Table',
48
+ 'blockView.stopped': 'Stopped here.'
49
+ });
50
+
51
+ /** Glyph for each callout tone. */
52
+ const CALLOUT_ICONS = Object.freeze({ info: 'info', success: 'success', warning: 'warning', danger: 'error' });
53
+
54
+ /**
55
+ * An answer an agent wrote, drawn block by block as semantic elements inside `.zx-prose`, and
56
+ * written to as it streams. Finished blocks are never redrawn: `appendText()` and `append()` touch
57
+ * only the last block, so a reader's selection or focus in an earlier paragraph survives the rest
58
+ * of the answer arriving.
59
+ *
60
+ * @fires BlockView#cite
61
+ * @fires BlockView#artifact
62
+ * @fires BlockView#end
63
+ * @extends {Component<BlockViewOptions>}
64
+ */
65
+ export class BlockView extends Component {
66
+ static cssName = 'block-view';
67
+
68
+ /** @type {Readonly<BlockViewOptions>} */
69
+ static defaults = {
70
+ blocks: [],
71
+ headingLevel: 3,
72
+ size: 'sm',
73
+ streaming: false,
74
+ sources: null,
75
+ msg: MESSAGES
76
+ };
77
+
78
+ /** @returns {HTMLElement} */
79
+ render() {
80
+ const created = !this.el;
81
+ const root = /** @type {HTMLElement} */ (this.el ?? h('div'));
82
+ this.el = root;
83
+ this._original = created ? null : snapshotTarget(root);
84
+ if (!created) root.replaceChildren();
85
+ root.classList.add('zx-prose');
86
+ /** @type {Block[]} */
87
+ this._blocks = [];
88
+ /** @type {HTMLElement[]} */
89
+ this._nodes = [];
90
+ /** @type {Source[]} */
91
+ this._sources = Array.isArray(this.options.sources) ? [...this.options.sources] : [];
92
+ this._streaming = Boolean(this.options.streaming);
93
+ this._stopped = false;
94
+ /** @type {HTMLElement|null} */
95
+ this._stoppedNote = null;
96
+ if (this.options.size !== 'md') root.dataset.size = 'sm';
97
+ root.dataset.zxMeasure = 'full';
98
+ this._syncState();
99
+ this.listen(root, 'click', (event) => this._onClick(/** @type {MouseEvent} */ (event)));
100
+ this._observer = typeof ResizeObserver === 'function'
101
+ ? new ResizeObserver(() => this._syncScrollers())
102
+ : null;
103
+ this._observer?.observe(root);
104
+ for (const block of Array.isArray(this.options.blocks) ? this.options.blocks : []) this._push(block);
105
+ return root;
106
+ }
107
+
108
+ /**
109
+ * Replaces every block, clearing a stopped state.
110
+ * @param {Block[]} blocks
111
+ * @param {{streaming?: boolean}} [options={}] `streaming: true` starts a new streamed answer
112
+ * from these blocks; otherwise the streaming state is kept.
113
+ * @returns {this}
114
+ */
115
+ setBlocks(blocks, options = {}) {
116
+ for (const node of this._nodes) node.remove();
117
+ this._blocks = [];
118
+ this._nodes = [];
119
+ this._stoppedNote?.remove();
120
+ this._stoppedNote = null;
121
+ this._stopped = false;
122
+ if (typeof options.streaming === 'boolean') this._streaming = options.streaming;
123
+ for (const block of Array.isArray(blocks) ? blocks : []) this._push(block);
124
+ this._syncState();
125
+ return this;
126
+ }
127
+
128
+ /**
129
+ * Adds a block after the last one. The blocks before it are left as they are.
130
+ * @param {Block} block
131
+ * @returns {this}
132
+ */
133
+ append(block) {
134
+ this._push(block);
135
+ return this;
136
+ }
137
+
138
+ /**
139
+ * Streams text into the answer. It continues the last paragraph, or opens one when the answer
140
+ * ends in anything else; a blank line in the text starts a new paragraph. Only the last
141
+ * paragraph is redrawn, so a marker split across two calls (`**bo` … `ld**`) resolves as soon as
142
+ * its close arrives.
143
+ * @param {string} text
144
+ * @returns {this}
145
+ */
146
+ appendText(text) {
147
+ const chunk = String(text ?? '');
148
+ if (!chunk) return this;
149
+ const last = this._blocks.at(-1);
150
+ if (last?.type !== 'p') {
151
+ this._push({ type: 'p', text: '' });
152
+ }
153
+ const tail = /** @type {{type: 'p', text: string}} */ (this._blocks.at(-1));
154
+ // Split on a blank line only once text follows it: a chunk may end between the two newlines,
155
+ // or right after them, and the paragraph must not open until there is something to put in it.
156
+ const parts = (tail.text + chunk).split(/\n{2,}(?=\S)/);
157
+ tail.text = /** @type {string} */ (parts.shift());
158
+ this._redrawLast();
159
+ for (const part of parts) this._push({ type: 'p', text: part });
160
+ return this;
161
+ }
162
+
163
+ /**
164
+ * Marks the answer finished.
165
+ * @returns {this}
166
+ */
167
+ end() {
168
+ const wasStreaming = this._streaming;
169
+ this._streaming = false;
170
+ this._syncState();
171
+ if (wasStreaming) this.emit('end', {});
172
+ return this;
173
+ }
174
+
175
+ /**
176
+ * Marks the answer stopped where it is, with a line saying so.
177
+ * @returns {this}
178
+ */
179
+ stop() {
180
+ this._streaming = false;
181
+ if (!this._stopped) {
182
+ this._stopped = true;
183
+ this._stoppedNote = h('p', { class: 'zx-block-view__stopped' }, this.msg('blockView.stopped'));
184
+ this.el.append(this._stoppedNote);
185
+ }
186
+ this._syncState();
187
+ return this;
188
+ }
189
+
190
+ /**
191
+ * Sets the sources citations point at.
192
+ * @param {Source[]} sources
193
+ * @returns {this}
194
+ */
195
+ setSources(sources) {
196
+ this._sources = Array.isArray(sources) ? [...sources] : [];
197
+ for (const button of this.el.querySelectorAll('[data-cite]')) {
198
+ button.setAttribute('aria-label', this._citeLabel(Number(/** @type {HTMLElement} */ (button).dataset.cite)));
199
+ }
200
+ return this;
201
+ }
202
+
203
+ /**
204
+ * Says whether an artifact's card has its artifact open somewhere — an inspector, a drawer — so
205
+ * the card reads as pressed and names what it controls. The application owns the open state;
206
+ * the card only reflects it.
207
+ * @param {string} id Artifact id.
208
+ * @param {{expanded?: boolean|null, controls?: string|null}} state `expanded` sets or (null)
209
+ * removes `aria-expanded`; `controls` sets or (null) removes `aria-controls`.
210
+ * @returns {this}
211
+ */
212
+ setArtifactState(id, state = {}) {
213
+ for (const card of /** @type {NodeListOf<HTMLElement>} */ (this.el.querySelectorAll('button[data-artifact]'))) {
214
+ if (card.dataset.artifact !== String(id)) continue;
215
+ if ('expanded' in state) {
216
+ if (state.expanded == null) card.removeAttribute('aria-expanded');
217
+ else card.setAttribute('aria-expanded', String(Boolean(state.expanded)));
218
+ }
219
+ if ('controls' in state) {
220
+ if (state.controls) card.setAttribute('aria-controls', String(state.controls));
221
+ else card.removeAttribute('aria-controls');
222
+ }
223
+ }
224
+ return this;
225
+ }
226
+
227
+ /** @returns {Block[]} A copy of the blocks. */
228
+ getBlocks() {
229
+ return structuredClone(this._blocks);
230
+ }
231
+
232
+ /** @returns {boolean} Whether more is still being written. */
233
+ isStreaming() {
234
+ return this._streaming;
235
+ }
236
+
237
+ /** @returns {string} The answer as plain text. */
238
+ toText() {
239
+ return blocksToText(this._blocks);
240
+ }
241
+
242
+ /** @returns {string} The answer as Markdown. */
243
+ toMarkdown() {
244
+ return blocksToMarkdown(this._blocks);
245
+ }
246
+
247
+ /** @returns {void} */
248
+ destroy() {
249
+ if (this._destroyed) return;
250
+ this._destroyed = true;
251
+ this._observer?.disconnect();
252
+ this._observer = null;
253
+ super.destroy();
254
+ if (this._original) restoreTarget(this.el, this._original);
255
+ }
256
+
257
+ /**
258
+ * @private
259
+ * @param {Block} block
260
+ * @returns {void}
261
+ */
262
+ _push(block) {
263
+ if (!block || typeof block !== 'object') return;
264
+ const copy = /** @type {Block} */ (structuredClone(block));
265
+ const node = renderBlock(copy, this._renderOptions());
266
+ this._blocks.push(copy);
267
+ this._nodes.push(node);
268
+ if (this._stoppedNote) this._stoppedNote.before(node);
269
+ else this.el.append(node);
270
+ if (copy.type === 'table') this._syncScrollers();
271
+ }
272
+
273
+ /** @private @returns {void} */
274
+ _redrawLast() {
275
+ const index = this._blocks.length - 1;
276
+ if (index < 0) return;
277
+ const node = renderBlock(this._blocks[index], this._renderOptions());
278
+ this._nodes[index].replaceWith(node);
279
+ this._nodes[index] = node;
280
+ }
281
+
282
+ /** @private @returns {RenderBlocksOptions & {cite: (n: number) => Node, artifact: boolean}} */
283
+ _renderOptions() {
284
+ return {
285
+ headingLevel: this.options.headingLevel,
286
+ msg: (key, ...args) => this.msg(key, ...args),
287
+ cite: (n) => h('sup', { class: 'zx-block-view__cite' }, h('button', {
288
+ type: 'button', class: 'zx-block-view__cite-button', dataset: { cite: String(n) }, 'aria-label': this._citeLabel(n)
289
+ }, String(n))),
290
+ artifact: true
291
+ };
292
+ }
293
+
294
+ /** @private @param {number} n @returns {string} */
295
+ _citeLabel(n) {
296
+ const source = this._sourceAt(n);
297
+ return source ? this.msg('blockView.citeTitled', n, source.title) : this.msg('blockView.cite', n);
298
+ }
299
+
300
+ /** @private @param {number} n @returns {Source|null} */
301
+ _sourceAt(n) {
302
+ return this._sources[n - 1] ?? null;
303
+ }
304
+
305
+ /** @private @returns {void} */
306
+ _syncState() {
307
+ const root = /** @type {HTMLElement} */ (this.el);
308
+ root.dataset.state = this._stopped ? 'stopped' : this._streaming ? 'streaming' : 'done';
309
+ if (this._streaming) root.setAttribute('aria-busy', 'true');
310
+ else root.removeAttribute('aria-busy');
311
+ }
312
+
313
+ /**
314
+ * A wide table scrolls inside the answer; while it does, its wrapper is a named, focusable
315
+ * region so the keyboard can scroll it too. A table that fits is not a tab stop.
316
+ * @private
317
+ * @returns {void}
318
+ */
319
+ _syncScrollers() {
320
+ for (const wrap of /** @type {NodeListOf<HTMLElement>} */ (this.el.querySelectorAll('.zx-block-view__table'))) {
321
+ const overflowing = wrap.scrollWidth > wrap.clientWidth + 1;
322
+ if (overflowing && !wrap.hasAttribute('tabindex')) {
323
+ wrap.tabIndex = 0;
324
+ wrap.setAttribute('role', 'region');
325
+ wrap.setAttribute('aria-label', this.msg('blockView.table'));
326
+ } else if (!overflowing && wrap.hasAttribute('tabindex')) {
327
+ wrap.removeAttribute('tabindex');
328
+ wrap.removeAttribute('role');
329
+ wrap.removeAttribute('aria-label');
330
+ }
331
+ }
332
+ }
333
+
334
+ /** @private @param {MouseEvent} event @returns {void} */
335
+ _onClick(event) {
336
+ const target = /** @type {Element|null} */ (event.target instanceof Element ? event.target : null);
337
+ const cite = /** @type {HTMLElement|null} */ (target?.closest('[data-cite]') ?? null);
338
+ if (cite && this.el.contains(cite)) {
339
+ const n = Number(cite.dataset.cite);
340
+ this.emit('cite', { n, source: this._sourceAt(n) });
341
+ return;
342
+ }
343
+ const artifact = /** @type {HTMLElement|null} */ (target?.closest('[data-artifact]') ?? null);
344
+ if (artifact && this.el.contains(artifact)) this.emit('artifact', { id: String(artifact.dataset.artifact) });
345
+ }
346
+ }
347
+
348
+ /**
349
+ * Citation listener payload.
350
+ * @event BlockView#cite
351
+ * @type {CustomEvent<{n: number, source: Source|null}>}
352
+ */
353
+ /**
354
+ * Artifact card listener payload.
355
+ * @event BlockView#artifact
356
+ * @type {CustomEvent<{id: string}>}
357
+ */
358
+ /**
359
+ * Fired when a streaming answer is marked finished.
360
+ * @event BlockView#end
361
+ * @type {CustomEvent<Record<string, never>>}
362
+ */
363
+
364
+ /**
365
+ * Draws blocks without a component, for a static answer: a transcript, a print view, a preview.
366
+ * Citations are plain superscripts and artifacts plain cards; use `BlockView` for them to act.
367
+ * @param {Block[]} blocks
368
+ * @param {RenderBlocksOptions} [options={}]
369
+ * @returns {DocumentFragment}
370
+ */
371
+ export function renderBlocks(blocks, options = {}) {
372
+ const fragment = document.createDocumentFragment();
373
+ for (const block of Array.isArray(blocks) ? blocks : []) {
374
+ if (block && typeof block === 'object') fragment.append(renderBlock(block, options));
375
+ }
376
+ return fragment;
377
+ }
378
+
379
+ /**
380
+ * Resolves a message when no component does: the host translator, then the catalogue.
381
+ * @param {string} key Message key.
382
+ * @param {...unknown} args Placeholder values.
383
+ * @returns {string}
384
+ */
385
+ function catalogueMessage(key, ...args) {
386
+ return translate(key, args);
387
+ }
388
+
389
+ /**
390
+ * Draws a run of model text as text nodes, `<strong>`, `<code>`, and citations — never as HTML.
391
+ * @param {string} text
392
+ * @param {((n: number) => Node)|undefined} cite Draws a citation; a plain superscript without it.
393
+ * @returns {Array<Node|string>}
394
+ */
395
+ function inlineNodes(text, cite) {
396
+ return parseInline(text).map((segment) => {
397
+ if (segment.kind === 'strong') return h('strong', {}, segment.text);
398
+ if (segment.kind === 'code') return h('code', {}, segment.text);
399
+ if (segment.kind === 'cite') return cite ? cite(segment.n) : h('sup', { class: 'zx-block-view__cite' }, String(segment.n));
400
+ return segment.text;
401
+ });
402
+ }
403
+
404
+ /**
405
+ * @param {Block} block
406
+ * @param {RenderBlocksOptions & {cite?: (n: number) => Node, artifact?: boolean}} options
407
+ * @returns {HTMLElement}
408
+ */
409
+ function renderBlock(block, options) {
410
+ const msg = options.msg ?? catalogueMessage;
411
+ const cite = options.cite;
412
+ const base = Math.min(5, Math.max(2, Number(options.headingLevel) || 3));
413
+ switch (block.type) {
414
+ case 'heading':
415
+ return h(`h${block.level === 3 ? base + 1 : base}`, {}, inlineNodes(block.text, cite));
416
+ case 'quote':
417
+ return h('blockquote', { class: 'zx-block-view__quote' },
418
+ String(block.text ?? '').split(/\n{2,}/).map((paragraph) => h('p', {}, inlineNodes(paragraph, cite))));
419
+ case 'list':
420
+ return h(block.ordered ? 'ol' : 'ul', {},
421
+ (Array.isArray(block.items) ? block.items : []).map((item) => h('li', {}, inlineNodes(item, cite))));
422
+ case 'code':
423
+ return renderCode(block, msg);
424
+ case 'table':
425
+ return renderTable(block, cite);
426
+ case 'figures':
427
+ return h('dl', { class: 'zx-block-view__figures' }, (Array.isArray(block.rows) ? block.rows : []).map((entry) => {
428
+ const row = figureRow(entry);
429
+ return h('div', { class: 'zx-block-view__figure' },
430
+ h('dt', { class: 'zx-block-view__figure-label' }, row.label),
431
+ row.detail ? h('dd', { class: 'zx-block-view__figure-detail' }, row.detail) : null,
432
+ h('dd', { class: 'zx-block-view__figure-value' }, row.value));
433
+ }));
434
+ case 'callout': {
435
+ const tone = Object.hasOwn(CALLOUT_ICONS, String(block.tone)) ? String(block.tone) : null;
436
+ return h('div', { class: 'zx-callout zx-block-view__callout', dataset: { kind: tone } },
437
+ tone ? h('span', { class: 'zx-callout__icon', 'aria-hidden': 'true' },
438
+ icon(CALLOUT_ICONS[/** @type {keyof typeof CALLOUT_ICONS} */ (tone)], { size: 16 })) : null,
439
+ h('div', { class: 'zx-callout__body' },
440
+ block.title ? h('p', { class: 'zx-callout__title' }, block.title) : null,
441
+ h('p', {}, inlineNodes(block.text, cite))));
442
+ }
443
+ case 'artifact':
444
+ return renderArtifact(block, msg, Boolean(options.artifact));
445
+ default:
446
+ return h('p', {}, inlineNodes(/** @type {{text?: string}} */ (block).text ?? '', cite));
447
+ }
448
+ }
449
+
450
+ /**
451
+ * @param {Extract<Block, {type: 'code'}>} block
452
+ * @param {(key: string, ...args: unknown[]) => string} msg
453
+ * @returns {HTMLElement}
454
+ */
455
+ function renderCode(block, msg) {
456
+ const language = typeof block.language === 'string' ? block.language : '';
457
+ return h('figure', { class: 'zx-block-view__code' },
458
+ h('figcaption', { class: 'zx-block-view__code-head' },
459
+ h('span', { class: 'zx-block-view__code-language' }, language || msg('blockView.code')),
460
+ copyButton({ text: String(block.code ?? ''), title: msg('blockView.copyCode'), size: 'sm' })),
461
+ block.lines
462
+ ? h('pre', { class: 'zx-block-view__pre', dataset: { lines: '' }, tabindex: '0' }, h('code', {},
463
+ String(block.code ?? '').split('\n').map((line) => h('span', {
464
+ class: 'zx-block-view__line', style: { '--zx-block-view-indent': `${/^\s*/.exec(line)?.[0].length ?? 0}ch` }
465
+ }, codeSegments(language, line)))))
466
+ : h('pre', { class: 'zx-block-view__pre', tabindex: '0' }, h('code', {}, codeSegments(language, block.code))));
467
+ }
468
+
469
+ /**
470
+ * Coloured code as text nodes and spans, never HTML.
471
+ * @param {string} language
472
+ * @param {string} code
473
+ * @returns {Array<Node|string>}
474
+ */
475
+ function codeSegments(language, code) {
476
+ return highlight(language, code).map((segment) => (segment.token
477
+ ? h('span', { class: 'zx-block-view__token', dataset: { token: segment.token } }, segment.text)
478
+ : segment.text));
479
+ }
480
+
481
+ /**
482
+ * @param {Extract<Block, {type: 'table'}>} block
483
+ * @param {((n: number) => Node)|undefined} cite
484
+ * @returns {HTMLElement}
485
+ */
486
+ function renderTable(block, cite) {
487
+ const columns = Array.isArray(block.columns) ? block.columns : [];
488
+ const rows = Array.isArray(block.rows) ? block.rows : [];
489
+ const align = (/** @type {number} */ index) => {
490
+ const value = block.align?.[index];
491
+ return value === 'end' || value === 'center' ? value : null;
492
+ };
493
+ return h('div', { class: 'zx-block-view__table' }, h('table', {},
494
+ h('thead', {}, h('tr', {}, columns.map((column, index) => h('th', { scope: 'col', dataset: { align: align(index) } },
495
+ inlineNodes(column, cite))))),
496
+ h('tbody', {}, rows.map((row) => h('tr', {}, (Array.isArray(row) ? row : []).map((cell, index) => h('td', {
497
+ dataset: { align: align(index) }
498
+ }, inlineNodes(cell, cite))))))));
499
+ }
500
+
501
+ /**
502
+ * @param {Extract<Block, {type: 'artifact'}>} block
503
+ * @param {(key: string, ...args: unknown[]) => string} msg
504
+ * @param {boolean} interactive
505
+ * @returns {HTMLElement}
506
+ */
507
+ function renderArtifact(block, msg, interactive) {
508
+ const kind = block.kind === 'code' ? 'code' : 'document';
509
+ const versions = Number(block.versions) || 0;
510
+ const meta = [msg(kind === 'code' ? 'blockView.code' : 'blockView.document'),
511
+ versions > 1 ? msg('blockView.versions', versions) : versions === 1 ? msg('blockView.version') : null].filter(Boolean).join(' · ');
512
+ const content = [
513
+ h('span', { class: 'zx-block-view__artifact-tile', dataset: { kind }, 'aria-hidden': 'true' },
514
+ icon(kind === 'code' ? 'code' : 'file', { size: 16 })),
515
+ h('span', { class: 'zx-block-view__artifact-copy' },
516
+ h('span', { class: 'zx-block-view__artifact-name' }, String(block.title ?? '')),
517
+ h('span', { class: 'zx-block-view__artifact-meta' }, meta)),
518
+ interactive ? h('span', { class: 'zx-block-view__artifact-chevron', 'aria-hidden': 'true' }, icon('chevron-right', { size: 14 })) : null
519
+ ];
520
+ return interactive
521
+ ? h('p', { class: 'zx-block-view__artifact-row' },
522
+ h('button', { type: 'button', class: 'zx-block-view__artifact', dataset: { artifact: String(block.id ?? '') } }, content))
523
+ : h('p', { class: 'zx-block-view__artifact-row' }, h('span', { class: 'zx-block-view__artifact' }, content));
524
+ }
@@ -104,7 +104,7 @@ export class Breadcrumb extends Component {
104
104
  const control = /** @type {Element} */ (event.target).closest?.('[data-crumb]');
105
105
  if (!control || !list.contains(control)) return;
106
106
  // A crumb with an href is an ordinary link: report the selection and let the browser follow.
107
- this._select(/** @type {HTMLElement} */ (control).dataset.crumb);
107
+ if (this._select(/** @type {HTMLElement} */ (control).dataset.crumb)?.defaultPrevented) event.preventDefault();
108
108
  });
109
109
  return root;
110
110
  }
@@ -283,11 +283,14 @@ export class Breadcrumb extends Component {
283
283
  const trigger = this._overflow.toElement();
284
284
  trigger.classList.add('zx-breadcrumb__more');
285
285
  trigger.setAttribute('aria-label', this._message('breadcrumb.more', 'Show hidden levels'));
286
- this._overflow.on('select', (event) => this._select(String(event.detail.value)));
286
+ this._overflow.on('select', (event) => {
287
+ if (this._select(String(event.detail.value))?.defaultPrevented) event.preventDefault();
288
+ });
287
289
  }
288
290
  this._overflow.setItems(hidden.map((item) => ({
289
291
  label: item.label,
290
292
  value: item.name,
293
+ href: item.href ?? undefined,
291
294
  icon: item.icon ?? undefined
292
295
  })));
293
296
  return /** @type {HTMLElement} */ (this._overflow.toElement());
@@ -301,13 +304,13 @@ export class Breadcrumb extends Component {
301
304
 
302
305
  /**
303
306
  * @param {string|undefined} name Item name from a control's `data-crumb`.
304
- * @returns {void}
307
+ * @returns {CustomEvent<BreadcrumbSelectDetail>|undefined}
305
308
  * @fires Breadcrumb#select
306
309
  */
307
310
  _select(name) {
308
311
  const index = this._items.findIndex((item) => item.name === name);
309
312
  if (index < 0) return;
310
- this.emit('select', { name: this._items[index].name, item: { ...this._items[index] }, index });
313
+ return this.emit('select', { name: this._items[index].name, item: { ...this._items[index] }, index });
311
314
  }
312
315
 
313
316
  /**
@@ -325,7 +328,7 @@ export class Breadcrumb extends Component {
325
328
 
326
329
  /**
327
330
  * Fired when an interactive crumb is chosen, from the trail itself or from the overflow menu. A
328
- * crumb with an `href` still navigates: the event reports the choice, it does not gate it.
331
+ * crumb with an `href` navigates natively unless the selection is prevented.
329
332
  * @event Breadcrumb#select
330
333
  * @type {CustomEvent<BreadcrumbSelectDetail>}
331
334
  */
@@ -1,3 +1,5 @@
1
+ import { translate } from '../../core/i18n.js';
2
+
1
3
  /** @typedef {'agenda'|'day'|'week'|'month'|'year'} CalendarView */
2
4
  /** @typedef {string|((record: Record<string, any>) => unknown)} CalendarReader */
3
5
 
@@ -27,6 +29,8 @@
27
29
  * @property {CalendarReader} [eventEditable='editable'] Move-policy reader.
28
30
  * @property {CalendarReader} [eventDurationEditable='durationEditable'] Resize-policy reader.
29
31
  * @property {'milliseconds'|'seconds'} [dateUnit='milliseconds'] Unit used for numeric dates.
32
+ * @property {string} [untitledTitle] Title of an event without one, by default the `calendar.untitled`
33
+ * message ("Untitled event").
30
34
  */
31
35
 
32
36
  /** @type {readonly CalendarView[]} */
@@ -152,7 +156,8 @@ export function normalizeCalendarEvents(records, options = {}) {
152
156
  : calendarDate(rawEnd, unit, `event ${key} end`);
153
157
  if (end < start) throw new RangeError(`Calendar event ${key} ends before it starts`);
154
158
 
155
- const title = String(readCalendarValue(record, readers.title) ?? '').trim() || 'Untitled event';
159
+ const title = String(readCalendarValue(record, readers.title) ?? '').trim()
160
+ || (options.untitledTitle ?? translate('calendar.untitled'));
156
161
  const location = String(readCalendarValue(record, readers.location) ?? '').trim();
157
162
  const editable = optionalBoolean(readCalendarValue(record, readers.editable));
158
163
  const durationEditable = optionalBoolean(readCalendarValue(record, readers.durationEditable));
@@ -408,7 +413,8 @@ function dateAtMinutes(day, minutes) {
408
413
 
409
414
  /** @param {Date} day @param {Date} value @returns {number} */
410
415
  function minutesSinceDay(day, value) {
411
- return (value.getTime() - day.getTime()) / 60_000;
416
+ return calendarDayDifference(day, value) * 1440 + value.getHours() * 60
417
+ + value.getMinutes() + value.getSeconds() / 60 + value.getMilliseconds() / 60_000;
412
418
  }
413
419
 
414
420
  /** @param {unknown} value @param {string} label @returns {Date} */