@anvia/react-ui 0.4.3 → 0.5.1

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 (42) hide show
  1. package/dist/attachment/index.js +2 -2
  2. package/dist/chat/index.d.ts +3 -2
  3. package/dist/chat/index.js +4 -4
  4. package/dist/{chunk-657E5BPN.js → chunk-2N5PXLAS.js} +2 -2
  5. package/dist/{chunk-GXB4VINL.js → chunk-GZHTC36V.js} +7 -11
  6. package/dist/chunk-GZHTC36V.js.map +1 -0
  7. package/dist/{chunk-JUNV3MUR.js → chunk-KNOZLODL.js} +7 -6
  8. package/dist/chunk-KNOZLODL.js.map +1 -0
  9. package/dist/{chunk-SE7I6ZXJ.js → chunk-NGEDM4XU.js} +10 -10
  10. package/dist/chunk-NGEDM4XU.js.map +1 -0
  11. package/dist/{chunk-7L2C5UUZ.js → chunk-SWGMBZR2.js} +56 -48
  12. package/dist/chunk-SWGMBZR2.js.map +1 -0
  13. package/dist/{chunk-TOBAQM2G.js → chunk-T6GPGCK4.js} +2 -2
  14. package/dist/{chunk-32QFNRXR.js → chunk-WUMACFYS.js} +2 -2
  15. package/dist/{chunk-BCCU3VT5.js → chunk-YQJLVBQD.js} +1 -1
  16. package/dist/chunk-YQJLVBQD.js.map +1 -0
  17. package/dist/chunk-ZODMCETL.js +996 -0
  18. package/dist/chunk-ZODMCETL.js.map +1 -0
  19. package/dist/completion/index.js +2 -2
  20. package/dist/{thread-C2-Xo08o.d.ts → composer-CzsbDFPt.d.ts} +7 -10
  21. package/dist/human-input/index.js +2 -2
  22. package/dist/image/index.js +2 -2
  23. package/dist/index.d.ts +3 -2
  24. package/dist/index.js +11 -11
  25. package/dist/message/index.d.ts +11 -1
  26. package/dist/message/index.js +3 -3
  27. package/dist/selection-toolbar/index.js +2 -2
  28. package/dist/shared.d.ts +2 -1
  29. package/dist/shared.js +1 -1
  30. package/dist/thread-list/index.js +2 -2
  31. package/dist/thread-uEm8hH0s.d.ts +11 -0
  32. package/package.json +2 -2
  33. package/dist/chunk-7L2C5UUZ.js.map +0 -1
  34. package/dist/chunk-BCCU3VT5.js.map +0 -1
  35. package/dist/chunk-GXB4VINL.js.map +0 -1
  36. package/dist/chunk-JUNV3MUR.js.map +0 -1
  37. package/dist/chunk-LWH6BMAD.js +0 -580
  38. package/dist/chunk-LWH6BMAD.js.map +0 -1
  39. package/dist/chunk-SE7I6ZXJ.js.map +0 -1
  40. /package/dist/{chunk-657E5BPN.js.map → chunk-2N5PXLAS.js.map} +0 -0
  41. /package/dist/{chunk-TOBAQM2G.js.map → chunk-T6GPGCK4.js.map} +0 -0
  42. /package/dist/{chunk-32QFNRXR.js.map → chunk-WUMACFYS.js.map} +0 -0
@@ -0,0 +1,996 @@
1
+ import {
2
+ Attachment
3
+ } from "./chunk-WUMACFYS.js";
4
+ import {
5
+ renderPrimitive
6
+ } from "./chunk-T3ZFZ3NP.js";
7
+ import {
8
+ InternalAttachmentProvider,
9
+ InternalMessagePartProvider,
10
+ useChatContext,
11
+ useMessage,
12
+ useMessagePart,
13
+ useOptionalMessagePart
14
+ } from "./chunk-YQJLVBQD.js";
15
+
16
+ // src/message/actions.tsx
17
+ import { forwardRef, useCallback, useEffect, useRef, useState } from "react";
18
+
19
+ // src/format/message.ts
20
+ function messageText(message) {
21
+ return message.parts.filter((part) => part.type === "text").map((part) => part.text).join("");
22
+ }
23
+
24
+ // src/format/value.ts
25
+ function stringifyValue(value) {
26
+ if (value === void 0) {
27
+ return "";
28
+ }
29
+ if (typeof value === "string") {
30
+ return value;
31
+ }
32
+ try {
33
+ return JSON.stringify(value, null, 2);
34
+ } catch {
35
+ return String(value);
36
+ }
37
+ }
38
+
39
+ // src/message/actions.tsx
40
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
41
+ var MessageActions = forwardRef(
42
+ function MessageActions2(props, ref) {
43
+ return renderPrimitive(
44
+ "div",
45
+ {
46
+ ...props,
47
+ children: props.children ?? /* @__PURE__ */ jsxs(Fragment, { children: [
48
+ /* @__PURE__ */ jsx(MessageCopy, {}),
49
+ /* @__PURE__ */ jsx(MessageRegenerate, {})
50
+ ] }),
51
+ "data-anvia-message-actions": ""
52
+ },
53
+ ref
54
+ );
55
+ }
56
+ );
57
+ var MessageCopy = forwardRef(function MessageCopy2({ onClick, ...props }, ref) {
58
+ const { message } = useMessage();
59
+ const [copyState, setCopyState] = useState("idle");
60
+ const text = messageText(message);
61
+ const previousTextRef = useRef(text);
62
+ const disabled = props.disabled ?? text.length === 0;
63
+ useEffect(() => {
64
+ if (previousTextRef.current !== text) {
65
+ previousTextRef.current = text;
66
+ setCopyState("idle");
67
+ }
68
+ }, [text]);
69
+ const handleClick = useCallback(
70
+ async (event) => {
71
+ onClick?.(event);
72
+ if (event.defaultPrevented || disabled) {
73
+ return;
74
+ }
75
+ const writeText = navigator.clipboard?.writeText;
76
+ if (writeText === void 0) {
77
+ setCopyState("error");
78
+ return;
79
+ }
80
+ try {
81
+ await writeText.call(navigator.clipboard, text);
82
+ setCopyState("copied");
83
+ } catch {
84
+ setCopyState("error");
85
+ }
86
+ },
87
+ [disabled, onClick, text]
88
+ );
89
+ return renderPrimitive(
90
+ "button",
91
+ {
92
+ ...props,
93
+ children: props.children ?? "Copy",
94
+ disabled,
95
+ onClick: handleClick,
96
+ type: props.type ?? "button",
97
+ "data-anvia-copy": "",
98
+ "data-state": copyState
99
+ },
100
+ ref
101
+ );
102
+ });
103
+ var MessageRegenerate = forwardRef(
104
+ function MessageRegenerate2({ onClick, ...props }, ref) {
105
+ const chat = useChatContext();
106
+ const { message } = useMessage();
107
+ const latestAssistantMessage = [...chat.messages].reverse().find((item) => item.role === "assistant");
108
+ const disabled = props.disabled ?? (chat.status === "streaming" || message.role !== "assistant" || latestAssistantMessage?.id !== message.id);
109
+ const handleClick = useCallback(
110
+ (event) => {
111
+ onClick?.(event);
112
+ if (event.defaultPrevented || disabled) {
113
+ return;
114
+ }
115
+ void chat.regenerate();
116
+ },
117
+ [chat, disabled, onClick]
118
+ );
119
+ return renderPrimitive(
120
+ "button",
121
+ {
122
+ ...props,
123
+ children: props.children ?? "Regenerate",
124
+ disabled,
125
+ onClick: handleClick,
126
+ type: props.type ?? "button",
127
+ "data-anvia-regenerate": "",
128
+ "data-state": disabled ? "disabled" : "idle"
129
+ },
130
+ ref
131
+ );
132
+ }
133
+ );
134
+
135
+ // src/message/entity.tsx
136
+ import { forwardRef as forwardRef2 } from "react";
137
+ import { jsx as jsx2 } from "react/jsx-runtime";
138
+ var MessageEntity = forwardRef2(function MessageEntity2({ children, entity, ...props }, ref) {
139
+ return /* @__PURE__ */ jsx2(
140
+ "span",
141
+ {
142
+ ...props,
143
+ "data-anvia-message-entity": "",
144
+ "data-entity-id": entity.id,
145
+ "data-trigger-id": entity.triggerId,
146
+ ref,
147
+ children: children ?? entity.text
148
+ }
149
+ );
150
+ });
151
+
152
+ // src/message/parts.tsx
153
+ import {
154
+ useSmoothStreamText
155
+ } from "@anvia/react";
156
+ import { createElement, forwardRef as forwardRef3 } from "react";
157
+ import ReactMarkdown from "react-markdown";
158
+ import remarkGfm from "remark-gfm";
159
+
160
+ // src/entities.ts
161
+ function messageTextLayout(parts) {
162
+ const textParts = parts.filter(
163
+ (part) => part.type === "text"
164
+ );
165
+ const segments = [];
166
+ let offset = 0;
167
+ for (const [index, part] of textParts.entries()) {
168
+ const from = offset;
169
+ const to = from + part.text.length;
170
+ segments.push({ part, from, to });
171
+ offset = to + (index === textParts.length - 1 ? 0 : 2);
172
+ }
173
+ return {
174
+ text: textParts.map((part) => part.text).join("\n\n"),
175
+ segments
176
+ };
177
+ }
178
+ function validComposerEntities(text, metadata) {
179
+ const candidates = composerEntityCandidates(metadata);
180
+ const individuallyValid = candidates.flatMap(
181
+ (entity, index) => isValidComposerEntity(entity, text) ? [{ entity, index }] : []
182
+ );
183
+ individuallyValid.sort(
184
+ (left, right) => left.entity.range.from - right.entity.range.from || left.entity.range.to - right.entity.range.to || left.index - right.index
185
+ );
186
+ const overlapping = /* @__PURE__ */ new Set();
187
+ let groupStart = 0;
188
+ let groupEnd = individuallyValid[0]?.entity.range.to ?? 0;
189
+ let groupHasOverlap = false;
190
+ for (let index = 1; index <= individuallyValid.length; index += 1) {
191
+ const current = individuallyValid[index];
192
+ if (current !== void 0 && current.entity.range.from < groupEnd) {
193
+ groupHasOverlap = true;
194
+ groupEnd = Math.max(groupEnd, current.entity.range.to);
195
+ continue;
196
+ }
197
+ if (groupHasOverlap) {
198
+ for (let groupIndex = groupStart; groupIndex < index; groupIndex += 1) {
199
+ const candidate = individuallyValid[groupIndex];
200
+ if (candidate !== void 0) {
201
+ overlapping.add(candidate.index);
202
+ }
203
+ }
204
+ }
205
+ groupStart = index;
206
+ groupEnd = current?.entity.range.to ?? 0;
207
+ groupHasOverlap = false;
208
+ }
209
+ return individuallyValid.filter((candidate) => !overlapping.has(candidate.index)).map((candidate) => candidate.entity);
210
+ }
211
+ function entitiesForTextSegment(entities, segment) {
212
+ return entities.flatMap((entity) => {
213
+ if (entity.range.from < segment.from || entity.range.to > segment.to) {
214
+ return [];
215
+ }
216
+ return [
217
+ {
218
+ ...entity,
219
+ range: {
220
+ from: entity.range.from - segment.from,
221
+ to: entity.range.to - segment.from
222
+ }
223
+ }
224
+ ];
225
+ });
226
+ }
227
+ function shiftComposerEntityRanges(entities, offset) {
228
+ if (offset === 0) {
229
+ return entities;
230
+ }
231
+ return entities.map((entity) => ({
232
+ ...entity,
233
+ range: {
234
+ from: entity.range.from + offset,
235
+ to: entity.range.to + offset
236
+ }
237
+ }));
238
+ }
239
+ function composerEntityCandidates(metadata) {
240
+ if (!isRecord(metadata) || !isRecord(metadata.composer)) {
241
+ return [];
242
+ }
243
+ return Array.isArray(metadata.composer.entities) ? metadata.composer.entities : [];
244
+ }
245
+ function isValidComposerEntity(value, text) {
246
+ if (!isRecord(value) || !isRecord(value.range)) {
247
+ return false;
248
+ }
249
+ if (!isNonEmptyString(value.id) || !isNonEmptyString(value.triggerId) || !isNonEmptyString(value.trigger) || !isNonEmptyString(value.label) || !isNonEmptyString(value.text)) {
250
+ return false;
251
+ }
252
+ const { from, to } = value.range;
253
+ return typeof from === "number" && typeof to === "number" && Number.isInteger(from) && Number.isInteger(to) && from >= 0 && to > from && to <= text.length && text.slice(from, to) === value.text;
254
+ }
255
+ function isNonEmptyString(value) {
256
+ return typeof value === "string" && value.length > 0;
257
+ }
258
+ function isRecord(value) {
259
+ return typeof value === "object" && value !== null && !Array.isArray(value);
260
+ }
261
+
262
+ // src/message/markdown-entities.ts
263
+ var phrasingParentTypes = /* @__PURE__ */ new Set([
264
+ "paragraph",
265
+ "heading",
266
+ "emphasis",
267
+ "strong",
268
+ "delete",
269
+ "link",
270
+ "linkReference",
271
+ "tableCell"
272
+ ]);
273
+ var literalNodeTypes = /* @__PURE__ */ new Set(["code", "inlineCode", "html", "yaml"]);
274
+ function createMessageEntityRemarkPlugin(markdown, entities) {
275
+ return function messageEntityRemarkPlugin() {
276
+ return (tree) => {
277
+ const rendered = /* @__PURE__ */ new Set();
278
+ transformNode(tree, markdown, entities, rendered);
279
+ };
280
+ };
281
+ }
282
+ function messageEntityRehypeOptions() {
283
+ return {
284
+ handlers: {
285
+ messageEntity(_state, node) {
286
+ const entityIndex = node.entityIndex;
287
+ return {
288
+ type: "element",
289
+ tagName: "span",
290
+ properties: {
291
+ dataAnviaMessageEntity: "",
292
+ dataAnviaEntityIndex: entityIndex === void 0 ? "" : String(entityIndex)
293
+ },
294
+ children: [{ type: "text", value: node.value ?? "" }]
295
+ };
296
+ }
297
+ }
298
+ };
299
+ }
300
+ function transformNode(node, markdown, entities, rendered) {
301
+ if (literalNodeTypes.has(node.type) || node.children === void 0) {
302
+ return;
303
+ }
304
+ for (const child of node.children) {
305
+ transformNode(child, markdown, entities, rendered);
306
+ }
307
+ if (!phrasingParentTypes.has(node.type)) {
308
+ return;
309
+ }
310
+ for (let entityIndex = entities.length - 1; entityIndex >= 0; entityIndex -= 1) {
311
+ if (rendered.has(entityIndex)) {
312
+ continue;
313
+ }
314
+ const entity = entities[entityIndex];
315
+ if (entity !== void 0 && replaceEntity(node, markdown, entity, entityIndex)) {
316
+ rendered.add(entityIndex);
317
+ }
318
+ }
319
+ }
320
+ function replaceEntity(parent, markdown, entity, entityIndex) {
321
+ const children = parent.children;
322
+ if (children === void 0) {
323
+ return false;
324
+ }
325
+ const firstIndex = children.findIndex((child) => containsStart(child, entity.range.from));
326
+ const lastIndex = findLastIndex(children, (child) => containsEnd(child, entity.range.to));
327
+ if (firstIndex < 0 || lastIndex < firstIndex) {
328
+ return false;
329
+ }
330
+ const first = children[firstIndex];
331
+ const last = children[lastIndex];
332
+ const firstPosition = offsets(first);
333
+ const lastPosition = offsets(last);
334
+ if (first === void 0 || last === void 0 || firstPosition === void 0 || lastPosition === void 0 || entity.range.from < firstPosition.from || entity.range.to > lastPosition.to) {
335
+ return false;
336
+ }
337
+ if (firstIndex === lastIndex && literalNodeTypes.has(first.type)) {
338
+ return false;
339
+ }
340
+ const before = contentBefore(first, markdown, entity);
341
+ const after = contentAfter(last, markdown, entity);
342
+ if (before === void 0 || after === void 0) {
343
+ return false;
344
+ }
345
+ const replacement = [];
346
+ replacement.push(...before);
347
+ replacement.push({
348
+ type: "messageEntity",
349
+ value: entity.text,
350
+ entityIndex,
351
+ position: {
352
+ start: { offset: entity.range.from },
353
+ end: { offset: entity.range.to }
354
+ }
355
+ });
356
+ replacement.push(...after);
357
+ children.splice(firstIndex, lastIndex - firstIndex + 1, ...replacement);
358
+ return true;
359
+ }
360
+ function contentBefore(node, markdown, entity) {
361
+ const position = offsets(node);
362
+ if (position === void 0) {
363
+ return void 0;
364
+ }
365
+ if (entity.range.from === position.from) {
366
+ return [];
367
+ }
368
+ if (node.type !== "text" || node.value === void 0) {
369
+ if (node.children === void 0 || literalNodeTypes.has(node.type)) {
370
+ return void 0;
371
+ }
372
+ const childIndex = node.children.findIndex((child2) => containsStart(child2, entity.range.from));
373
+ const child = node.children[childIndex];
374
+ if (child === void 0) {
375
+ return void 0;
376
+ }
377
+ const childContent = contentBefore(child, markdown, entity);
378
+ return childContent === void 0 ? void 0 : [...node.children.slice(0, childIndex), ...childContent];
379
+ }
380
+ const split = textValueRange(node, markdown, entity);
381
+ if (split === void 0) {
382
+ return void 0;
383
+ }
384
+ return [
385
+ {
386
+ ...node,
387
+ value: node.value.slice(0, split.from),
388
+ position: {
389
+ start: node.position?.start ?? {},
390
+ end: { offset: entity.range.from }
391
+ }
392
+ }
393
+ ];
394
+ }
395
+ function contentAfter(node, markdown, entity) {
396
+ const position = offsets(node);
397
+ if (position === void 0) {
398
+ return void 0;
399
+ }
400
+ if (entity.range.to === position.to) {
401
+ return [];
402
+ }
403
+ if (node.type !== "text" || node.value === void 0) {
404
+ if (node.children === void 0 || literalNodeTypes.has(node.type)) {
405
+ return void 0;
406
+ }
407
+ const childIndex = findLastIndex(node.children, (child2) => containsEnd(child2, entity.range.to));
408
+ const child = node.children[childIndex];
409
+ if (child === void 0) {
410
+ return void 0;
411
+ }
412
+ const childContent = contentAfter(child, markdown, entity);
413
+ return childContent === void 0 ? void 0 : [...childContent, ...node.children.slice(childIndex + 1)];
414
+ }
415
+ const split = textValueRange(node, markdown, entity);
416
+ if (split === void 0) {
417
+ return void 0;
418
+ }
419
+ return [
420
+ {
421
+ ...node,
422
+ value: node.value.slice(split.to),
423
+ position: {
424
+ start: { offset: entity.range.to },
425
+ end: node.position?.end ?? {}
426
+ }
427
+ }
428
+ ];
429
+ }
430
+ function textValueRange(node, markdown, entity) {
431
+ const position = offsets(node);
432
+ const value = node.value;
433
+ if (position === void 0 || value === void 0) {
434
+ return void 0;
435
+ }
436
+ const raw = markdown.slice(position.from, position.to);
437
+ const relativeFrom = entity.range.from - position.from;
438
+ const relativeTo = entity.range.to - position.from;
439
+ if (raw === value) {
440
+ return { from: relativeFrom, to: relativeTo };
441
+ }
442
+ const occurrence = countOccurrences(raw.slice(0, relativeFrom), entity.text);
443
+ const valueFrom = occurrenceIndex(value, entity.text, occurrence);
444
+ return valueFrom === void 0 ? void 0 : { from: valueFrom, to: valueFrom + entity.text.length };
445
+ }
446
+ function countOccurrences(value, search) {
447
+ let count = 0;
448
+ let offset = 0;
449
+ while (offset <= value.length - search.length) {
450
+ const index = value.indexOf(search, offset);
451
+ if (index < 0) {
452
+ break;
453
+ }
454
+ count += 1;
455
+ offset = index + search.length;
456
+ }
457
+ return count;
458
+ }
459
+ function occurrenceIndex(value, search, occurrence) {
460
+ let offset = 0;
461
+ for (let index = 0; index <= occurrence; index += 1) {
462
+ const found = value.indexOf(search, offset);
463
+ if (found < 0) {
464
+ return void 0;
465
+ }
466
+ if (index === occurrence) {
467
+ return found;
468
+ }
469
+ offset = found + search.length;
470
+ }
471
+ return void 0;
472
+ }
473
+ function containsStart(node, offset) {
474
+ const position = offsets(node);
475
+ return position !== void 0 && offset >= position.from && offset < position.to;
476
+ }
477
+ function containsEnd(node, offset) {
478
+ const position = offsets(node);
479
+ return position !== void 0 && offset > position.from && offset <= position.to;
480
+ }
481
+ function offsets(node) {
482
+ const from = node?.position?.start.offset;
483
+ const to = node?.position?.end.offset;
484
+ return typeof from === "number" && typeof to === "number" ? { from, to } : void 0;
485
+ }
486
+ function findLastIndex(items, predicate) {
487
+ for (let index = items.length - 1; index >= 0; index -= 1) {
488
+ const item = items[index];
489
+ if (item !== void 0 && predicate(item)) {
490
+ return index;
491
+ }
492
+ }
493
+ return -1;
494
+ }
495
+
496
+ // src/message/parts.tsx
497
+ import { Fragment as Fragment2, jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
498
+ var MessageParts = forwardRef3(function MessageParts2({ children, filter, ...props }, ref) {
499
+ const { message } = useMessage();
500
+ const parts = filter === void 0 ? message.parts : message.parts.filter(filter);
501
+ return renderPrimitive(
502
+ "div",
503
+ {
504
+ ...props,
505
+ children: parts.map((part) => /* @__PURE__ */ jsx3(InternalMessagePartProvider, { part, children: typeof children === "function" ? children(part) : children ?? /* @__PURE__ */ jsx3(MessagePart, {}) }, part.id)),
506
+ "data-anvia-message-parts": ""
507
+ },
508
+ ref
509
+ );
510
+ });
511
+ var MessagePart = forwardRef3(function MessagePart2({ children, ...props }, ref) {
512
+ const { part } = useMessagePart();
513
+ const renderedChildren = typeof children === "function" ? children(part) : children ?? defaultPart(part);
514
+ return renderPrimitive(
515
+ "div",
516
+ {
517
+ ...props,
518
+ children: renderedChildren,
519
+ "data-anvia-part": "",
520
+ "data-part": part.type
521
+ },
522
+ ref
523
+ );
524
+ });
525
+ var MessageText = forwardRef3(function MessageText2(props, ref) {
526
+ const { part } = useMessagePart();
527
+ if (part.type !== "text") {
528
+ return null;
529
+ }
530
+ return /* @__PURE__ */ jsx3(MessageTextContent, { ...props, content: part.text, ref });
531
+ });
532
+ var MessageTextContent = forwardRef3(
533
+ function MessageTextContent2({
534
+ animate = false,
535
+ animationMode = "smooth",
536
+ isStreaming,
537
+ smoothingPreset,
538
+ reducedMotion,
539
+ content,
540
+ ...props
541
+ }, ref) {
542
+ const animationEnabled = animate && props.children === void 0;
543
+ const smooth = useSmoothStreamText(
544
+ content,
545
+ smoothStreamOptions(
546
+ animationEnabled,
547
+ animationMode,
548
+ isStreaming,
549
+ smoothingPreset,
550
+ reducedMotion
551
+ )
552
+ );
553
+ const animationActive = animationEnabled && animationMode !== "none" && isStreaming !== false && reducedMotion !== true;
554
+ return renderPrimitive(
555
+ "span",
556
+ {
557
+ ...props,
558
+ children: props.children ?? (animationEnabled ? smooth.text : content),
559
+ "data-anvia-text": "",
560
+ "data-anvia-stream-animation": animationEnabled ? animationMode : void 0,
561
+ "data-streaming": animationActive ? "" : void 0
562
+ },
563
+ ref
564
+ );
565
+ }
566
+ );
567
+ var MessageMarkdown = forwardRef3(function MessageMarkdown2({ children, ...props }, ref) {
568
+ const { message } = useMessage();
569
+ const partContext = useOptionalMessagePart();
570
+ const part = partContext?.part;
571
+ if (part !== void 0 && part.type !== "text") {
572
+ return null;
573
+ }
574
+ const layout = messageTextLayout(message.parts);
575
+ const markdown = children ?? (part?.type === "text" ? part.text : layout.text);
576
+ const expectedMarkdown = part?.type === "text" ? part.text : layout.text;
577
+ const validEntities = children === void 0 || children === expectedMarkdown ? validComposerEntities(layout.text, message.metadata) : [];
578
+ const segment = part?.type === "text" ? layout.segments.find((candidate) => candidate.part.id === part.id) : void 0;
579
+ const entities = segment === void 0 ? validEntities : entitiesForTextSegment(validEntities, segment);
580
+ return /* @__PURE__ */ jsx3(MessageMarkdownContent, { ...props, entities, markdown, ref });
581
+ });
582
+ var MessageMarkdownContent = forwardRef3(
583
+ function MessageMarkdownContent2({
584
+ animate = false,
585
+ animationMode = "smooth",
586
+ isStreaming,
587
+ smoothingPreset,
588
+ reducedMotion,
589
+ components,
590
+ renderEntity,
591
+ remarkPlugins,
592
+ entities,
593
+ markdown,
594
+ ...props
595
+ }, ref) {
596
+ const smooth = useSmoothStreamText(
597
+ markdown,
598
+ smoothStreamOptions(animate, animationMode, isStreaming, smoothingPreset, reducedMotion)
599
+ );
600
+ const animationActive = animate && animationMode !== "none" && isStreaming !== false && reducedMotion !== true;
601
+ const renderedMarkdown = animate ? smooth.text : markdown;
602
+ const renderedEntities = validComposerEntities(renderedMarkdown, {
603
+ composer: { entities }
604
+ });
605
+ return renderPrimitive(
606
+ "div",
607
+ {
608
+ ...props,
609
+ children: /* @__PURE__ */ jsx3(
610
+ ReactMarkdown,
611
+ {
612
+ components: markdownComponents(components, renderedEntities, renderEntity),
613
+ remarkPlugins: [
614
+ createMessageEntityRemarkPlugin(renderedMarkdown, renderedEntities),
615
+ ...remarkPlugins ?? [remarkGfm]
616
+ ],
617
+ remarkRehypeOptions: messageEntityRehypeOptions(),
618
+ children: renderedMarkdown
619
+ }
620
+ ),
621
+ "data-anvia-markdown": "",
622
+ "data-anvia-stream-animation": animate ? animationMode : void 0,
623
+ "data-streaming": animationActive ? "" : void 0
624
+ },
625
+ ref
626
+ );
627
+ }
628
+ );
629
+ function markdownComponents(components, entities, renderEntity) {
630
+ const consumerSpan = components?.span;
631
+ return {
632
+ code: defaultCodeComponent,
633
+ pre: defaultPreComponent,
634
+ ...components,
635
+ span(spanProps) {
636
+ const internalProps = spanProps;
637
+ const entityIndexValue = internalProps["data-anvia-entity-index"];
638
+ const entityIndex = typeof entityIndexValue === "string" && /^\d+$/.test(entityIndexValue) ? Number(entityIndexValue) : void 0;
639
+ const entity = entityIndex === void 0 ? void 0 : entities[entityIndex];
640
+ const consumerProps = { ...internalProps };
641
+ delete consumerProps["data-anvia-entity-index"];
642
+ if (entity !== void 0) {
643
+ if (renderEntity !== void 0) {
644
+ return renderEntity(entity);
645
+ }
646
+ const entityProps = {
647
+ ...consumerProps,
648
+ "data-anvia-message-entity": "",
649
+ "data-entity-id": entity.id,
650
+ "data-trigger-id": entity.triggerId
651
+ };
652
+ if (consumerSpan !== void 0) {
653
+ return createElement(consumerSpan, entityProps);
654
+ }
655
+ const { node: _node2, ...elementProps2 } = entityProps;
656
+ return /* @__PURE__ */ jsx3(MessageEntity, { ...elementProps2, entity });
657
+ }
658
+ if (consumerSpan !== void 0) {
659
+ return createElement(consumerSpan, consumerProps);
660
+ }
661
+ const { node: _node, ...elementProps } = consumerProps;
662
+ return /* @__PURE__ */ jsx3("span", { ...elementProps });
663
+ }
664
+ };
665
+ }
666
+ function smoothStreamOptions(enabled, mode, isStreaming, preset, reducedMotion) {
667
+ const options = { enabled, mode };
668
+ if (isStreaming !== void 0) options.isStreaming = isStreaming;
669
+ if (preset !== void 0) options.preset = preset;
670
+ if (reducedMotion !== void 0) options.reducedMotion = reducedMotion;
671
+ return options;
672
+ }
673
+ var MessageCodeBlock = forwardRef3(
674
+ function MessageCodeBlock2({ children, code, language, ...props }, ref) {
675
+ return renderPrimitive(
676
+ "pre",
677
+ {
678
+ ...props,
679
+ children: children ?? /* @__PURE__ */ jsx3("code", { "data-anvia-code": "", "data-language": language, children: code ?? "" }),
680
+ "data-anvia-code-block": "",
681
+ "data-language": language
682
+ },
683
+ ref
684
+ );
685
+ }
686
+ );
687
+ var MessageReasoning = forwardRef3(
688
+ function MessageReasoning2(props, ref) {
689
+ const { part } = useMessagePart();
690
+ if (part.type !== "reasoning") {
691
+ return null;
692
+ }
693
+ return renderPrimitive(
694
+ "details",
695
+ {
696
+ ...props,
697
+ children: props.children ?? /* @__PURE__ */ jsxs2(Fragment2, { children: [
698
+ /* @__PURE__ */ jsx3("summary", { children: "Reasoning" }),
699
+ /* @__PURE__ */ jsx3("pre", { children: part.text })
700
+ ] }),
701
+ "data-anvia-reasoning": ""
702
+ },
703
+ ref
704
+ );
705
+ }
706
+ );
707
+ var MessageTool = forwardRef3(function MessageTool2({ children, renderWhen = "always", ...props }, ref) {
708
+ const { part } = useMessagePart();
709
+ if (part.type !== "tool" || !shouldRenderTool(part, renderWhen)) {
710
+ return null;
711
+ }
712
+ const renderedChildren = typeof children === "function" ? children(part) : children ?? defaultToolContent();
713
+ return renderPrimitive(
714
+ "div",
715
+ {
716
+ ...props,
717
+ children: renderedChildren,
718
+ "data-anvia-tool": "",
719
+ "data-state": part.state
720
+ },
721
+ ref
722
+ );
723
+ });
724
+ var MessageToolName = forwardRef3(
725
+ function MessageToolName2(props, ref) {
726
+ const { part } = useMessagePart();
727
+ if (part.type !== "tool") {
728
+ return null;
729
+ }
730
+ return renderPrimitive(
731
+ "span",
732
+ {
733
+ ...props,
734
+ children: props.children ?? part.toolName,
735
+ "data-anvia-tool-name": ""
736
+ },
737
+ ref
738
+ );
739
+ }
740
+ );
741
+ var MessageToolStatus = forwardRef3(
742
+ function MessageToolStatus2(props, ref) {
743
+ const { part } = useMessagePart();
744
+ if (part.type !== "tool") {
745
+ return null;
746
+ }
747
+ return renderPrimitive(
748
+ "span",
749
+ {
750
+ ...props,
751
+ children: props.children ?? toolStateLabel(part.state),
752
+ "data-anvia-tool-status": "",
753
+ "data-state": part.state
754
+ },
755
+ ref
756
+ );
757
+ }
758
+ );
759
+ var MessageToolInput = forwardRef3(
760
+ function MessageToolInput2(props, ref) {
761
+ const { part } = useMessagePart();
762
+ if (part.type !== "tool" || part.input === void 0) {
763
+ return null;
764
+ }
765
+ return renderPrimitive(
766
+ "pre",
767
+ {
768
+ ...props,
769
+ children: props.children ?? stringifyValue(part.input),
770
+ "data-anvia-tool-input": ""
771
+ },
772
+ ref
773
+ );
774
+ }
775
+ );
776
+ var MessageToolOutput = forwardRef3(
777
+ function MessageToolOutput2(props, ref) {
778
+ const { part } = useMessagePart();
779
+ if (part.type !== "tool" || part.output === void 0) {
780
+ return null;
781
+ }
782
+ return renderPrimitive(
783
+ "pre",
784
+ {
785
+ ...props,
786
+ children: props.children ?? stringifyValue(part.output),
787
+ "data-anvia-tool-output": ""
788
+ },
789
+ ref
790
+ );
791
+ }
792
+ );
793
+ var MessageToolError = forwardRef3(
794
+ function MessageToolError2(props, ref) {
795
+ const { part } = useMessagePart();
796
+ if (part.type !== "tool" || part.error === void 0) {
797
+ return null;
798
+ }
799
+ return renderPrimitive(
800
+ "div",
801
+ {
802
+ ...props,
803
+ children: props.children ?? part.error.message,
804
+ "data-anvia-tool-error": "",
805
+ role: props.role ?? "alert"
806
+ },
807
+ ref
808
+ );
809
+ }
810
+ );
811
+ var MessageAttachment = forwardRef3(
812
+ function MessageAttachment2({ children, ...props }, ref) {
813
+ const { part } = useMessagePart();
814
+ if (part.type !== "attachment") {
815
+ return null;
816
+ }
817
+ const renderedChildren = typeof children === "function" ? children(part.attachment) : children ?? /* @__PURE__ */ jsx3(Attachment.Root, {});
818
+ return /* @__PURE__ */ jsx3(InternalAttachmentProvider, { value: { attachment: part.attachment }, children: renderPrimitive(
819
+ "div",
820
+ {
821
+ ...props,
822
+ children: renderedChildren,
823
+ "data-anvia-message-attachment": ""
824
+ },
825
+ ref
826
+ ) });
827
+ }
828
+ );
829
+ var MessageData = forwardRef3(
830
+ function MessageData2(props, ref) {
831
+ const { part } = useMessagePart();
832
+ if (part.type !== "data") {
833
+ return null;
834
+ }
835
+ return renderPrimitive(
836
+ "pre",
837
+ {
838
+ ...props,
839
+ children: props.children ?? stringifyValue(part.data),
840
+ "data-anvia-data": "",
841
+ "data-name": part.name
842
+ },
843
+ ref
844
+ );
845
+ }
846
+ );
847
+ var MessageError = forwardRef3(
848
+ function MessageError2(props, ref) {
849
+ const { part } = useMessagePart();
850
+ if (part.type !== "error") {
851
+ return null;
852
+ }
853
+ return renderPrimitive(
854
+ "div",
855
+ {
856
+ ...props,
857
+ children: props.children ?? part.error.message,
858
+ "data-anvia-error": "",
859
+ role: props.role ?? "alert"
860
+ },
861
+ ref
862
+ );
863
+ }
864
+ );
865
+ function defaultPart(part) {
866
+ if (part.type === "text") {
867
+ return /* @__PURE__ */ jsx3(MessageText, {});
868
+ }
869
+ if (part.type === "reasoning") {
870
+ return /* @__PURE__ */ jsx3(MessageReasoning, {});
871
+ }
872
+ if (part.type === "tool") {
873
+ return /* @__PURE__ */ jsx3(MessageTool, {});
874
+ }
875
+ if (part.type === "attachment") {
876
+ return /* @__PURE__ */ jsx3(MessageAttachment, {});
877
+ }
878
+ if (part.type === "data") {
879
+ return /* @__PURE__ */ jsx3(MessageData, {});
880
+ }
881
+ return /* @__PURE__ */ jsx3(MessageError, {});
882
+ }
883
+ function defaultToolContent() {
884
+ return /* @__PURE__ */ jsxs2(Fragment2, { children: [
885
+ /* @__PURE__ */ jsx3(MessageToolName, {}),
886
+ /* @__PURE__ */ jsx3(MessageToolStatus, {}),
887
+ /* @__PURE__ */ jsx3(MessageToolInput, {}),
888
+ /* @__PURE__ */ jsx3(MessageToolOutput, {}),
889
+ /* @__PURE__ */ jsx3(MessageToolError, {})
890
+ ] });
891
+ }
892
+ function defaultCodeComponent({
893
+ children,
894
+ className,
895
+ node: _node,
896
+ ...props
897
+ }) {
898
+ const language = /language-(\S+)/.exec(className ?? "")?.[1];
899
+ return /* @__PURE__ */ jsx3(
900
+ "code",
901
+ {
902
+ className,
903
+ "data-anvia-code": "",
904
+ "data-anvia-inline-code": language === void 0 ? "" : void 0,
905
+ "data-language": language,
906
+ ...props,
907
+ children
908
+ }
909
+ );
910
+ }
911
+ function defaultPreComponent({
912
+ children,
913
+ node: _node,
914
+ ...props
915
+ }) {
916
+ return /* @__PURE__ */ jsx3(MessageCodeBlock, { ...props, children });
917
+ }
918
+ function shouldRenderTool(part, renderWhen) {
919
+ if (renderWhen === "always") {
920
+ return true;
921
+ }
922
+ if (renderWhen === "pending") {
923
+ return part.state === "input-streaming" || part.state === "input-available";
924
+ }
925
+ return part.state === "output-available" || part.state === "error";
926
+ }
927
+ function toolStateLabel(state) {
928
+ if (state === "output-available") {
929
+ return "Done";
930
+ }
931
+ if (state === "error") {
932
+ return "Error";
933
+ }
934
+ return "Running";
935
+ }
936
+
937
+ // src/message/root.tsx
938
+ import { forwardRef as forwardRef4 } from "react";
939
+ var MessageRoot = forwardRef4(function MessageRoot2({ children, ...props }, ref) {
940
+ const { message } = useMessage();
941
+ const renderedChildren = typeof children === "function" ? children(message) : children;
942
+ return renderPrimitive(
943
+ "article",
944
+ {
945
+ ...props,
946
+ children: renderedChildren,
947
+ "data-anvia-message": "",
948
+ "data-anvia-message-id": message.id,
949
+ "data-role": message.role
950
+ },
951
+ ref
952
+ );
953
+ });
954
+ var MessageContent = forwardRef4(
955
+ function MessageContent2(props, ref) {
956
+ return renderPrimitive(
957
+ "div",
958
+ {
959
+ ...props,
960
+ "data-anvia-message-content": ""
961
+ },
962
+ ref
963
+ );
964
+ }
965
+ );
966
+
967
+ // src/message/index.ts
968
+ var Message = {
969
+ Root: MessageRoot,
970
+ Content: MessageContent,
971
+ Parts: MessageParts,
972
+ Part: MessagePart,
973
+ Text: MessageText,
974
+ Markdown: MessageMarkdown,
975
+ Entity: MessageEntity,
976
+ CodeBlock: MessageCodeBlock,
977
+ Reasoning: MessageReasoning,
978
+ Tool: MessageTool,
979
+ ToolName: MessageToolName,
980
+ ToolInput: MessageToolInput,
981
+ ToolOutput: MessageToolOutput,
982
+ ToolError: MessageToolError,
983
+ ToolStatus: MessageToolStatus,
984
+ Attachment: MessageAttachment,
985
+ Data: MessageData,
986
+ Error: MessageError,
987
+ Actions: MessageActions,
988
+ Copy: MessageCopy,
989
+ Regenerate: MessageRegenerate
990
+ };
991
+
992
+ export {
993
+ shiftComposerEntityRanges,
994
+ Message
995
+ };
996
+ //# sourceMappingURL=chunk-ZODMCETL.js.map