@anvia/react-ui 1.3.0 → 1.3.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 (133) hide show
  1. package/dist/attachment/index.d.ts +3 -21
  2. package/dist/attachment/index.js +3 -12
  3. package/dist/attachment-By2pged3.js +91 -0
  4. package/dist/attachment-By2pged3.js.map +1 -0
  5. package/dist/chat/index.d.ts +3 -152
  6. package/dist/chat/index.js +3 -25
  7. package/dist/chat-Dgz8hsVT.js +1210 -0
  8. package/dist/chat-Dgz8hsVT.js.map +1 -0
  9. package/dist/completion/index.d.ts +3 -29
  10. package/dist/completion/index.js +3 -16
  11. package/dist/completion-DDTKoZRJ.js +124 -0
  12. package/dist/completion-DDTKoZRJ.js.map +1 -0
  13. package/dist/connection-DeNq8wHS.js +203 -0
  14. package/dist/connection-DeNq8wHS.js.map +1 -0
  15. package/dist/contexts-B4ijG24z.js +212 -0
  16. package/dist/contexts-B4ijG24z.js.map +1 -0
  17. package/dist/elements-D4vmkkF8.js +19 -0
  18. package/dist/elements-D4vmkkF8.js.map +1 -0
  19. package/dist/graph-explorer/index.d.ts +54 -73
  20. package/dist/graph-explorer/index.d.ts.map +1 -0
  21. package/dist/graph-explorer/index.js +181 -248
  22. package/dist/graph-explorer/index.js.map +1 -1
  23. package/dist/human-input/index.d.ts +3 -78
  24. package/dist/human-input/index.js +3 -21
  25. package/dist/human-input-CsCbZomZ.js +314 -0
  26. package/dist/human-input-CsCbZomZ.js.map +1 -0
  27. package/dist/image/index.d.ts +3 -40
  28. package/dist/image/index.js +3 -12
  29. package/dist/image-aCWm6hFe.js +303 -0
  30. package/dist/image-aCWm6hFe.js.map +1 -0
  31. package/dist/index-B1Qs2Cjv.d.ts +145 -0
  32. package/dist/index-B1Qs2Cjv.d.ts.map +1 -0
  33. package/dist/index-BCBVcl0D.d.ts +277 -0
  34. package/dist/index-BCBVcl0D.d.ts.map +1 -0
  35. package/dist/index-CE22tsua.d.ts +20 -0
  36. package/dist/index-CE22tsua.d.ts.map +1 -0
  37. package/dist/index-CGqtpc6b.d.ts +25 -0
  38. package/dist/index-CGqtpc6b.d.ts.map +1 -0
  39. package/dist/index-DqdyM0Jm.d.ts +7 -0
  40. package/dist/index-DqdyM0Jm.d.ts.map +1 -0
  41. package/dist/index-Mw3ltwSG.d.ts +318 -0
  42. package/dist/index-Mw3ltwSG.d.ts.map +1 -0
  43. package/dist/index.d.ts +7 -40
  44. package/dist/index.js +34 -118
  45. package/dist/index.js.map +1 -1
  46. package/dist/message/index.d.ts +3 -105
  47. package/dist/message/index.js +3 -20
  48. package/dist/message-CElFEKgL.js +783 -0
  49. package/dist/message-CElFEKgL.js.map +1 -0
  50. package/dist/primitives-xfE7zfwJ.js +15 -0
  51. package/dist/primitives-xfE7zfwJ.js.map +1 -0
  52. package/dist/refs-Djj9rSvB.js +10 -0
  53. package/dist/refs-Djj9rSvB.js.map +1 -0
  54. package/dist/sandbox-desktop/index.d.ts +100 -104
  55. package/dist/sandbox-desktop/index.d.ts.map +1 -0
  56. package/dist/sandbox-desktop/index.js +344 -342
  57. package/dist/sandbox-desktop/index.js.map +1 -1
  58. package/dist/sandbox-terminal/index.d.ts +52 -56
  59. package/dist/sandbox-terminal/index.d.ts.map +1 -0
  60. package/dist/sandbox-terminal/index.js +217 -208
  61. package/dist/sandbox-terminal/index.js.map +1 -1
  62. package/dist/selection-toolbar/index.d.ts +3 -21
  63. package/dist/selection-toolbar/index.js +3 -13
  64. package/dist/selection-toolbar-CIkOoM-7.js +171 -0
  65. package/dist/selection-toolbar-CIkOoM-7.js.map +1 -0
  66. package/dist/shared-BSYElFU2.d.ts +2 -0
  67. package/dist/shared.d.ts +4 -14
  68. package/dist/shared.js +1 -43
  69. package/dist/stream/index.d.ts +2 -22
  70. package/dist/stream/index.js +1 -8
  71. package/dist/stream-CAO0x8_1.js +298 -0
  72. package/dist/stream-CAO0x8_1.js.map +1 -0
  73. package/dist/thread-list/index.d.ts +3 -47
  74. package/dist/thread-list/index.js +3 -18
  75. package/dist/thread-list-xJbGXFrE.js +228 -0
  76. package/dist/thread-list-xJbGXFrE.js.map +1 -0
  77. package/dist/ticket-CoJHBpdF.d.ts +32 -0
  78. package/dist/ticket-CoJHBpdF.d.ts.map +1 -0
  79. package/dist/url-CiYZkDks.js +52 -0
  80. package/dist/url-CiYZkDks.js.map +1 -0
  81. package/dist/value-BGGrx72L.js +13 -0
  82. package/dist/value-BGGrx72L.js.map +1 -0
  83. package/package.json +22 -22
  84. package/dist/attachment/index.js.map +0 -1
  85. package/dist/attachment-mbjsroli.d.ts +0 -9
  86. package/dist/chat/index.js.map +0 -1
  87. package/dist/chat-PSj1Fo4W.d.ts +0 -88
  88. package/dist/chunk-22EZJUPM.js +0 -120
  89. package/dist/chunk-22EZJUPM.js.map +0 -1
  90. package/dist/chunk-5FZKTDZR.js +0 -201
  91. package/dist/chunk-5FZKTDZR.js.map +0 -1
  92. package/dist/chunk-6XOJFLX4.js +0 -22
  93. package/dist/chunk-6XOJFLX4.js.map +0 -1
  94. package/dist/chunk-CKA4CE26.js +0 -25
  95. package/dist/chunk-CKA4CE26.js.map +0 -1
  96. package/dist/chunk-DLBNFBXQ.js +0 -425
  97. package/dist/chunk-DLBNFBXQ.js.map +0 -1
  98. package/dist/chunk-E73S3SOF.js +0 -404
  99. package/dist/chunk-E73S3SOF.js.map +0 -1
  100. package/dist/chunk-F6AKBUO3.js +0 -380
  101. package/dist/chunk-F6AKBUO3.js.map +0 -1
  102. package/dist/chunk-HGOPGZNQ.js +0 -195
  103. package/dist/chunk-HGOPGZNQ.js.map +0 -1
  104. package/dist/chunk-KHB5CHAU.js +0 -220
  105. package/dist/chunk-KHB5CHAU.js.map +0 -1
  106. package/dist/chunk-LHABOAOP.js +0 -316
  107. package/dist/chunk-LHABOAOP.js.map +0 -1
  108. package/dist/chunk-N5XQU36W.js +0 -410
  109. package/dist/chunk-N5XQU36W.js.map +0 -1
  110. package/dist/chunk-T3ZFZ3NP.js +0 -30
  111. package/dist/chunk-T3ZFZ3NP.js.map +0 -1
  112. package/dist/chunk-T4IYUEPH.js +0 -1612
  113. package/dist/chunk-T4IYUEPH.js.map +0 -1
  114. package/dist/chunk-W2AHKMRS.js +0 -1092
  115. package/dist/chunk-W2AHKMRS.js.map +0 -1
  116. package/dist/completion/index.js.map +0 -1
  117. package/dist/completion-input-DHlwrhxB.d.ts +0 -24
  118. package/dist/composer-6iCSIq6L.d.ts +0 -111
  119. package/dist/human-input/index.js.map +0 -1
  120. package/dist/human-input-CkLrZ8IC.d.ts +0 -27
  121. package/dist/image/index.js.map +0 -1
  122. package/dist/image-VZv6Ddsn.d.ts +0 -14
  123. package/dist/message/index.js.map +0 -1
  124. package/dist/message-CNRnZQwU.d.ts +0 -14
  125. package/dist/render-CRKWam2v.d.ts +0 -8
  126. package/dist/selection-toolbar/index.js.map +0 -1
  127. package/dist/selection-toolbar-Cwjbuv5S.d.ts +0 -14
  128. package/dist/shared.js.map +0 -1
  129. package/dist/stream/index.js.map +0 -1
  130. package/dist/thread-Bz6-1zaJ.d.ts +0 -12
  131. package/dist/thread-list/index.js.map +0 -1
  132. package/dist/thread-list-On8db4XL.d.ts +0 -34
  133. package/dist/ticket-pexI1zIa.d.ts +0 -33
@@ -0,0 +1,783 @@
1
+ import { I as useChatContext, R as InternalAttachmentProvider, d as InternalMessagePartProvider, h as useOptionalMessagePart, m as useMessagePart, p as useMessage } from "./contexts-B4ijG24z.js";
2
+ import { t as composeRefs } from "./refs-Djj9rSvB.js";
3
+ import { t as renderPrimitive } from "./primitives-xfE7zfwJ.js";
4
+ import { t as AttachmentPrimitive } from "./attachment-By2pged3.js";
5
+ import { t as stringifyValue } from "./value-BGGrx72L.js";
6
+ import { t as StreamMarkdown } from "./stream-CAO0x8_1.js";
7
+ import { n as registerMessageElement } from "./elements-D4vmkkF8.js";
8
+ import { createElement, forwardRef, useCallback, useEffect, useMemo, useRef, useState } from "react";
9
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
10
+ import { useSmoothStreamItems, useSmoothStreamText } from "@anvia/react";
11
+ import remarkGfm from "remark-gfm";
12
+ // src/strict-json.ts
13
+ function isComposerEntityData(value) {
14
+ return isComposerEntityDataWithAncestors(value, /* @__PURE__ */ new WeakSet());
15
+ }
16
+ function isComposerEntityDataWithAncestors(value, ancestors) {
17
+ if (value === null || typeof value === "string" || typeof value === "boolean") return true;
18
+ if (typeof value === "number") return Number.isFinite(value);
19
+ if (typeof value !== "object" || ancestors.has(value)) return false;
20
+ ancestors.add(value);
21
+ try {
22
+ if (Array.isArray(value)) {
23
+ if (Object.getPrototypeOf(value) !== Array.prototype || !hasOnlyDenseArrayProperties(value)) return false;
24
+ for (let index = 0; index < value.length; index += 1) if (!isComposerEntityDataWithAncestors(value[index], ancestors)) return false;
25
+ return true;
26
+ }
27
+ const prototype = Object.getPrototypeOf(value);
28
+ if (prototype !== Object.prototype && prototype !== null) return false;
29
+ for (const key of Reflect.ownKeys(value)) {
30
+ if (typeof key !== "string") return false;
31
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
32
+ if (descriptor === void 0 || !descriptor.enumerable || !("value" in descriptor) || !isComposerEntityDataWithAncestors(descriptor.value, ancestors)) return false;
33
+ }
34
+ return true;
35
+ } catch {
36
+ return false;
37
+ } finally {
38
+ ancestors.delete(value);
39
+ }
40
+ }
41
+ function hasOnlyDenseArrayProperties(value) {
42
+ const keys = Reflect.ownKeys(value);
43
+ if (keys.length !== value.length + 1 || keys.at(-1) !== "length") return false;
44
+ for (let index = 0; index < value.length; index += 1) {
45
+ const key = String(index);
46
+ if (keys[index] !== key) return false;
47
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
48
+ if (descriptor === void 0 || !descriptor.enumerable || !("value" in descriptor)) return false;
49
+ }
50
+ return true;
51
+ }
52
+ // src/entities.ts
53
+ function messageTextLayout(parts) {
54
+ const textParts = parts.filter((part) => part.type === "text");
55
+ const segments = [];
56
+ let offset = 0;
57
+ for (const [index, part] of textParts.entries()) {
58
+ const from = offset;
59
+ const to = from + part.text.length;
60
+ segments.push({
61
+ part,
62
+ from,
63
+ to
64
+ });
65
+ offset = to + (index === textParts.length - 1 ? 0 : 2);
66
+ }
67
+ return {
68
+ text: textParts.map((part) => part.text).join("\n\n"),
69
+ segments
70
+ };
71
+ }
72
+ function validComposerEntities(text, metadata) {
73
+ const individuallyValid = composerEntityCandidates(metadata).flatMap((candidate, index) => {
74
+ const entity = parseComposerEntity(candidate, text);
75
+ return entity === void 0 ? [] : [{
76
+ entity,
77
+ index
78
+ }];
79
+ });
80
+ individuallyValid.sort((left, right) => left.entity.range.from - right.entity.range.from || left.entity.range.to - right.entity.range.to || left.index - right.index);
81
+ const overlapping = /* @__PURE__ */ new Set();
82
+ let groupStart = 0;
83
+ let groupEnd = individuallyValid[0]?.entity.range.to ?? 0;
84
+ let groupHasOverlap = false;
85
+ for (let index = 1; index <= individuallyValid.length; index += 1) {
86
+ const current = individuallyValid[index];
87
+ if (current !== void 0 && current.entity.range.from < groupEnd) {
88
+ groupHasOverlap = true;
89
+ groupEnd = Math.max(groupEnd, current.entity.range.to);
90
+ continue;
91
+ }
92
+ if (groupHasOverlap) for (let groupIndex = groupStart; groupIndex < index; groupIndex += 1) {
93
+ const candidate = individuallyValid[groupIndex];
94
+ if (candidate !== void 0) overlapping.add(candidate.index);
95
+ }
96
+ groupStart = index;
97
+ groupEnd = current?.entity.range.to ?? 0;
98
+ groupHasOverlap = false;
99
+ }
100
+ return individuallyValid.filter((candidate) => !overlapping.has(candidate.index)).map((candidate) => candidate.entity);
101
+ }
102
+ function entitiesForTextSegment(entities, segment) {
103
+ return entities.flatMap((entity) => {
104
+ if (entity.range.from < segment.from || entity.range.to > segment.to) return [];
105
+ return [{
106
+ ...entity,
107
+ range: {
108
+ from: entity.range.from - segment.from,
109
+ to: entity.range.to - segment.from
110
+ }
111
+ }];
112
+ });
113
+ }
114
+ function shiftComposerEntityRanges(entities, offset) {
115
+ if (offset === 0) return entities;
116
+ return entities.map((entity) => ({
117
+ ...entity,
118
+ range: {
119
+ from: entity.range.from + offset,
120
+ to: entity.range.to + offset
121
+ }
122
+ }));
123
+ }
124
+ function composerEntityCandidates(metadata) {
125
+ if (!isRecord(metadata) || !isRecord(metadata.composer)) return [];
126
+ return Array.isArray(metadata.composer.entities) ? metadata.composer.entities : [];
127
+ }
128
+ function parseComposerEntity(value, text) {
129
+ if (!isRecord(value) || !isRecord(value.range)) return;
130
+ if (!isNonEmptyString(value.id) || !isNonEmptyString(value.triggerId) || !isNonEmptyString(value.trigger) || !isNonEmptyString(value.label) || !isNonEmptyString(value.text)) return;
131
+ const { from, to } = value.range;
132
+ if (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) return;
133
+ if (value.data !== void 0 && !isComposerEntityData(value.data)) return void 0;
134
+ const entity = {
135
+ id: value.id,
136
+ triggerId: value.triggerId,
137
+ trigger: value.trigger,
138
+ label: value.label,
139
+ text: value.text,
140
+ range: {
141
+ from,
142
+ to
143
+ }
144
+ };
145
+ if (value.data !== void 0) entity.data = value.data;
146
+ return entity;
147
+ }
148
+ function isNonEmptyString(value) {
149
+ return typeof value === "string" && value.length > 0;
150
+ }
151
+ function isRecord(value) {
152
+ return typeof value === "object" && value !== null && !Array.isArray(value);
153
+ }
154
+ // src/format/message.ts
155
+ function messageText(message) {
156
+ return message.parts.filter((part) => part.type === "text").map((part) => part.text).join("");
157
+ }
158
+ // src/message/actions.tsx
159
+ const MessageActions = forwardRef(function MessageActions(props, ref) {
160
+ const chat = useChatContext();
161
+ const { message } = useMessage();
162
+ const index = chat.messages.findIndex((item) => item.id === message.id);
163
+ if (message.role !== "assistant" || index < 0 || (props.children == null || typeof props.children === "boolean") && messageText(message).trim().length === 0) return null;
164
+ const following = chat.messages.slice(index + 1);
165
+ const nextUserIndex = following.findIndex((item) => item.role === "user");
166
+ if ((nextUserIndex < 0 ? following : following.slice(0, nextUserIndex)).some((item) => item.role === "assistant") || nextUserIndex < 0 && (chat.status === "submitted" || chat.status === "streaming")) return null;
167
+ return renderPrimitive("div", {
168
+ ...props,
169
+ children: props.children ?? /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx(MessageCopy, {}), /* @__PURE__ */ jsx(MessageRegenerate, {})] })
170
+ }, ref);
171
+ });
172
+ const MessageCopy = forwardRef(function MessageCopy({ onClick, ...props }, ref) {
173
+ const { message } = useMessage();
174
+ const [copyState, setCopyState] = useState("idle");
175
+ const text = messageText(message);
176
+ const previousTextRef = useRef(text);
177
+ const disabled = props.disabled ?? text.length === 0;
178
+ useEffect(() => {
179
+ if (previousTextRef.current !== text) {
180
+ previousTextRef.current = text;
181
+ setCopyState("idle");
182
+ }
183
+ }, [text]);
184
+ const handleClick = useCallback(async (event) => {
185
+ onClick?.(event);
186
+ if (event.defaultPrevented || disabled) return;
187
+ const writeText = navigator.clipboard?.writeText;
188
+ if (writeText === void 0) {
189
+ setCopyState("error");
190
+ return;
191
+ }
192
+ try {
193
+ await writeText.call(navigator.clipboard, text);
194
+ setCopyState("copied");
195
+ } catch {
196
+ setCopyState("error");
197
+ }
198
+ }, [
199
+ disabled,
200
+ onClick,
201
+ text
202
+ ]);
203
+ return renderPrimitive("button", {
204
+ ...props,
205
+ children: props.children ?? "Copy",
206
+ disabled,
207
+ onClick: handleClick,
208
+ type: props.type ?? "button",
209
+ "data-state": copyState
210
+ }, ref);
211
+ });
212
+ const MessageRegenerate = forwardRef(function MessageRegenerate({ onClick, ...props }, ref) {
213
+ const chat = useChatContext();
214
+ const { message } = useMessage();
215
+ const latestAssistantMessage = [...chat.messages].reverse().find((item) => item.role === "assistant");
216
+ const disabled = props.disabled ?? (chat.status === "submitted" || chat.status === "streaming" || message.role !== "assistant" || latestAssistantMessage?.id !== message.id);
217
+ const handleClick = useCallback((event) => {
218
+ onClick?.(event);
219
+ if (event.defaultPrevented || disabled) return;
220
+ chat.regenerate();
221
+ }, [
222
+ chat,
223
+ disabled,
224
+ onClick
225
+ ]);
226
+ return renderPrimitive("button", {
227
+ ...props,
228
+ children: props.children ?? "Regenerate",
229
+ disabled,
230
+ onClick: handleClick,
231
+ type: props.type ?? "button",
232
+ "data-state": disabled ? "disabled" : "idle"
233
+ }, ref);
234
+ });
235
+ // src/message/entity.tsx
236
+ const MessageEntity = forwardRef(function MessageEntity({ children, entity, ...props }, ref) {
237
+ return /* @__PURE__ */ jsx("span", {
238
+ ...props,
239
+ "data-role": "entity",
240
+ ref,
241
+ children: children ?? entity.text
242
+ });
243
+ });
244
+ // src/message/markdown-entities.ts
245
+ const phrasingParentTypes = /* @__PURE__ */ new Set([
246
+ "paragraph",
247
+ "heading",
248
+ "emphasis",
249
+ "strong",
250
+ "delete",
251
+ "link",
252
+ "linkReference",
253
+ "tableCell"
254
+ ]);
255
+ const literalNodeTypes = /* @__PURE__ */ new Set([
256
+ "code",
257
+ "inlineCode",
258
+ "html",
259
+ "yaml"
260
+ ]);
261
+ function createMessageEntityRemarkPlugin(markdown, entities) {
262
+ return function messageEntityRemarkPlugin() {
263
+ return (tree) => {
264
+ transformNode(tree, markdown, entities, /* @__PURE__ */ new Set());
265
+ };
266
+ };
267
+ }
268
+ function messageEntityRehypeOptions() {
269
+ return { handlers: { messageEntity(_state, node) {
270
+ const entityIndex = node.entityIndex;
271
+ return {
272
+ type: "element",
273
+ tagName: "span",
274
+ properties: { dataEntityIndex: entityIndex === void 0 ? "" : String(entityIndex) },
275
+ children: [{
276
+ type: "text",
277
+ value: node.value ?? ""
278
+ }]
279
+ };
280
+ } } };
281
+ }
282
+ function transformNode(node, markdown, entities, rendered) {
283
+ if (literalNodeTypes.has(node.type) || node.children === void 0) return;
284
+ for (const child of node.children) transformNode(child, markdown, entities, rendered);
285
+ if (!phrasingParentTypes.has(node.type)) return;
286
+ for (let entityIndex = entities.length - 1; entityIndex >= 0; entityIndex -= 1) {
287
+ if (rendered.has(entityIndex)) continue;
288
+ const entity = entities[entityIndex];
289
+ if (entity !== void 0 && replaceEntity(node, markdown, entity, entityIndex)) rendered.add(entityIndex);
290
+ }
291
+ }
292
+ function replaceEntity(parent, markdown, entity, entityIndex) {
293
+ const children = parent.children;
294
+ if (children === void 0) return false;
295
+ const firstIndex = children.findIndex((child) => containsStart(child, entity.range.from));
296
+ const lastIndex = findLastIndex(children, (child) => containsEnd(child, entity.range.to));
297
+ if (firstIndex < 0 || lastIndex < firstIndex) return false;
298
+ const first = children[firstIndex];
299
+ const last = children[lastIndex];
300
+ const firstPosition = offsets(first);
301
+ const lastPosition = offsets(last);
302
+ if (first === void 0 || last === void 0 || firstPosition === void 0 || lastPosition === void 0 || entity.range.from < firstPosition.from || entity.range.to > lastPosition.to) return false;
303
+ if (firstIndex === lastIndex && literalNodeTypes.has(first.type)) return false;
304
+ const before = contentBefore(first, markdown, entity);
305
+ const after = contentAfter(last, markdown, entity);
306
+ if (before === void 0 || after === void 0) return false;
307
+ const replacement = [];
308
+ replacement.push(...before);
309
+ replacement.push({
310
+ type: "messageEntity",
311
+ value: entity.text,
312
+ entityIndex,
313
+ position: {
314
+ start: { offset: entity.range.from },
315
+ end: { offset: entity.range.to }
316
+ }
317
+ });
318
+ replacement.push(...after);
319
+ children.splice(firstIndex, lastIndex - firstIndex + 1, ...replacement);
320
+ return true;
321
+ }
322
+ function contentBefore(node, markdown, entity) {
323
+ const position = offsets(node);
324
+ if (position === void 0) return;
325
+ if (entity.range.from === position.from) return [];
326
+ if (node.type !== "text" || node.value === void 0) {
327
+ if (node.children === void 0 || literalNodeTypes.has(node.type)) return;
328
+ const childIndex = node.children.findIndex((child) => containsStart(child, entity.range.from));
329
+ const child = node.children[childIndex];
330
+ if (child === void 0) return;
331
+ const childContent = contentBefore(child, markdown, entity);
332
+ return childContent === void 0 ? void 0 : [...node.children.slice(0, childIndex), ...childContent];
333
+ }
334
+ const split = textValueRange(node, markdown, entity);
335
+ if (split === void 0) return;
336
+ return [{
337
+ ...node,
338
+ value: node.value.slice(0, split.from),
339
+ position: {
340
+ start: node.position?.start ?? {},
341
+ end: { offset: entity.range.from }
342
+ }
343
+ }];
344
+ }
345
+ function contentAfter(node, markdown, entity) {
346
+ const position = offsets(node);
347
+ if (position === void 0) return;
348
+ if (entity.range.to === position.to) return [];
349
+ if (node.type !== "text" || node.value === void 0) {
350
+ if (node.children === void 0 || literalNodeTypes.has(node.type)) return;
351
+ const childIndex = findLastIndex(node.children, (child) => containsEnd(child, entity.range.to));
352
+ const child = node.children[childIndex];
353
+ if (child === void 0) return;
354
+ const childContent = contentAfter(child, markdown, entity);
355
+ return childContent === void 0 ? void 0 : [...childContent, ...node.children.slice(childIndex + 1)];
356
+ }
357
+ const split = textValueRange(node, markdown, entity);
358
+ if (split === void 0) return;
359
+ return [{
360
+ ...node,
361
+ value: node.value.slice(split.to),
362
+ position: {
363
+ start: { offset: entity.range.to },
364
+ end: node.position?.end ?? {}
365
+ }
366
+ }];
367
+ }
368
+ function textValueRange(node, markdown, entity) {
369
+ const position = offsets(node);
370
+ const value = node.value;
371
+ if (position === void 0 || value === void 0) return;
372
+ const raw = markdown.slice(position.from, position.to);
373
+ const relativeFrom = entity.range.from - position.from;
374
+ const relativeTo = entity.range.to - position.from;
375
+ if (raw === value) return {
376
+ from: relativeFrom,
377
+ to: relativeTo
378
+ };
379
+ const occurrence = countOccurrences(raw.slice(0, relativeFrom), entity.text);
380
+ const valueFrom = occurrenceIndex(value, entity.text, occurrence);
381
+ return valueFrom === void 0 ? void 0 : {
382
+ from: valueFrom,
383
+ to: valueFrom + entity.text.length
384
+ };
385
+ }
386
+ function countOccurrences(value, search) {
387
+ let count = 0;
388
+ let offset = 0;
389
+ while (offset <= value.length - search.length) {
390
+ const index = value.indexOf(search, offset);
391
+ if (index < 0) break;
392
+ count += 1;
393
+ offset = index + search.length;
394
+ }
395
+ return count;
396
+ }
397
+ function occurrenceIndex(value, search, occurrence) {
398
+ let offset = 0;
399
+ for (let index = 0; index <= occurrence; index += 1) {
400
+ const found = value.indexOf(search, offset);
401
+ if (found < 0) return;
402
+ if (index === occurrence) return found;
403
+ offset = found + search.length;
404
+ }
405
+ }
406
+ function containsStart(node, offset) {
407
+ const position = offsets(node);
408
+ return position !== void 0 && offset >= position.from && offset < position.to;
409
+ }
410
+ function containsEnd(node, offset) {
411
+ const position = offsets(node);
412
+ return position !== void 0 && offset > position.from && offset <= position.to;
413
+ }
414
+ function offsets(node) {
415
+ const from = node?.position?.start.offset;
416
+ const to = node?.position?.end.offset;
417
+ return typeof from === "number" && typeof to === "number" ? {
418
+ from,
419
+ to
420
+ } : void 0;
421
+ }
422
+ function findLastIndex(items, predicate) {
423
+ for (let index = items.length - 1; index >= 0; index -= 1) {
424
+ const item = items[index];
425
+ if (item !== void 0 && predicate(item)) return index;
426
+ }
427
+ return -1;
428
+ }
429
+ // src/message/parts.tsx
430
+ const messagePartStreamAdapter = {
431
+ getKey: (part) => part.id,
432
+ getText: (part) => part.type === "text" || part.type === "reasoning" ? part.text : void 0,
433
+ withText: (part, text) => {
434
+ if (part.type === "text" || part.type === "reasoning") return part.text === text ? part : {
435
+ ...part,
436
+ text
437
+ };
438
+ return part;
439
+ }
440
+ };
441
+ const MessageParts = forwardRef(function MessageParts({ children, filter, stream, ...props }, ref) {
442
+ const { message } = useMessage();
443
+ const parts = filter === void 0 ? message.parts : message.parts.filter(filter);
444
+ if (stream !== void 0) return /* @__PURE__ */ jsx(SmoothedMessageParts, {
445
+ ...props,
446
+ parts,
447
+ ref,
448
+ stream,
449
+ children
450
+ });
451
+ return renderPrimitive("div", {
452
+ ...props,
453
+ children: renderMessagePartNodes(parts, children),
454
+ "data-role": "parts"
455
+ }, ref);
456
+ });
457
+ const SmoothedMessageParts = forwardRef(function SmoothedMessageParts({ children, parts, stream, ...props }, ref) {
458
+ const streamOptions = {
459
+ adapter: messagePartStreamAdapter,
460
+ isStreaming: stream.isStreaming,
461
+ resetKey: stream.resetKey
462
+ };
463
+ if (stream.flushImmediately !== void 0) Object.assign(streamOptions, { flushImmediately: stream.flushImmediately });
464
+ const smooth = useSmoothStreamItems(parts, streamOptions);
465
+ return renderPrimitive("div", {
466
+ ...props,
467
+ children: renderMessagePartNodes(smooth.items, children, smooth.liveItemKey),
468
+ "data-role": "parts",
469
+ "data-state": smooth.isDraining ? "draining" : stream.isStreaming ? "streaming" : "idle"
470
+ }, ref);
471
+ });
472
+ function renderMessagePartNodes(parts, children, liveItemKey) {
473
+ return parts.map((part) => /* @__PURE__ */ jsx(InternalMessagePartProvider, {
474
+ isLive: liveItemKey === part.id,
475
+ part,
476
+ streamControlled: liveItemKey !== void 0,
477
+ children: typeof children === "function" ? children(part) : children ?? /* @__PURE__ */ jsx(MessagePart, {})
478
+ }, part.id));
479
+ }
480
+ const MessagePart = forwardRef(function MessagePart({ children, ...props }, ref) {
481
+ const { part } = useMessagePart();
482
+ const renderedChildren = typeof children === "function" ? children(part) : children ?? defaultPart(part);
483
+ return renderPrimitive("div", {
484
+ ...props,
485
+ children: renderedChildren,
486
+ "data-role": part.type
487
+ }, ref);
488
+ });
489
+ const MessageText = forwardRef(function MessageText(props, ref) {
490
+ const { part, streamControlled } = useMessagePart();
491
+ if (part.type !== "text") return null;
492
+ return /* @__PURE__ */ jsx(MessageTextContent, {
493
+ ...props,
494
+ content: part.text,
495
+ partId: part.id,
496
+ ref,
497
+ streamControlled
498
+ });
499
+ });
500
+ const MessageTextContent = forwardRef(function MessageTextContent({ content, partId, stream, streamControlled, ...props }, ref) {
501
+ const ownsStream = stream !== void 0 && !streamControlled && props.children === void 0;
502
+ const streamOptions = {
503
+ isStreaming: ownsStream ? stream.isStreaming : false,
504
+ resetKey: ownsStream ? stream.resetKey : partId
505
+ };
506
+ if (ownsStream && stream.flushImmediately !== void 0) Object.assign(streamOptions, { flushImmediately: stream.flushImmediately });
507
+ const smooth = useSmoothStreamText(content, streamOptions);
508
+ return renderPrimitive("span", {
509
+ ...props,
510
+ children: props.children ?? (ownsStream ? smooth.text : content),
511
+ "data-role": "text",
512
+ "data-state": ownsStream ? smooth.isDraining ? "draining" : stream.isStreaming ? "streaming" : "idle" : void 0
513
+ }, ref);
514
+ });
515
+ const MessageMarkdown = forwardRef(function MessageMarkdown({ children, ...props }, ref) {
516
+ const { message } = useMessage();
517
+ const partContext = useOptionalMessagePart();
518
+ const part = partContext?.part;
519
+ if (part !== void 0 && part.type !== "text") return null;
520
+ const layout = messageTextLayout(message.parts);
521
+ const markdown = children ?? (part?.type === "text" ? part.text : layout.text);
522
+ const expectedMarkdown = part?.type === "text" ? part.text : layout.text;
523
+ const validEntities = children === void 0 || children === expectedMarkdown ? validComposerEntities(layout.text, message.metadata) : [];
524
+ const segment = part?.type === "text" ? layout.segments.find((candidate) => candidate.part.id === part.id) : void 0;
525
+ const entities = segment === void 0 ? validEntities : entitiesForTextSegment(validEntities, segment);
526
+ const lastPart = message.parts.at(-1);
527
+ const liveEligible = part?.type === "text" ? lastPart?.id === part.id : lastPart?.type === "text";
528
+ return /* @__PURE__ */ jsx(MessageMarkdownContent, {
529
+ ...props,
530
+ entities,
531
+ liveEligible,
532
+ markdown,
533
+ ref,
534
+ resetKey: message.id,
535
+ streamControlled: partContext?.streamControlled ?? false,
536
+ streamLive: partContext?.isLive ?? false
537
+ });
538
+ });
539
+ const MessageMarkdownContent = forwardRef(function MessageMarkdownContent({ components, renderEntity, remarkPlugins, entities, liveEligible, markdown, resetKey, stream, streamControlled, streamLive, ...props }, ref) {
540
+ const ownsStream = stream !== void 0 && !streamControlled;
541
+ const streamOptions = {
542
+ isStreaming: ownsStream ? stream.isStreaming : false,
543
+ resetKey: ownsStream ? stream.resetKey : resetKey
544
+ };
545
+ if (ownsStream && stream.flushImmediately !== void 0) Object.assign(streamOptions, { flushImmediately: stream.flushImmediately });
546
+ const smooth = useSmoothStreamText(markdown, streamOptions);
547
+ const renderedMarkdown = ownsStream ? smooth.text : markdown;
548
+ const renderedEntities = useMemo(() => entities.length === 0 ? entities : validComposerEntities(renderedMarkdown, { composer: { entities } }), [entities, renderedMarkdown]);
549
+ const renderedComponents = useMemo(() => markdownComponents(components, renderedEntities, renderEntity), [
550
+ components,
551
+ renderEntity,
552
+ renderedEntities
553
+ ]);
554
+ const renderedRemarkPlugins = useMemo(() => renderedEntities.length === 0 ? remarkPlugins : [createMessageEntityRemarkPlugin(renderedMarkdown, renderedEntities), ...remarkPlugins ?? [remarkGfm]], [
555
+ remarkPlugins,
556
+ renderedEntities,
557
+ renderedMarkdown
558
+ ]);
559
+ const live = streamControlled ? streamLive : ownsStream && liveEligible && (stream.isStreaming || smooth.isDraining);
560
+ return /* @__PURE__ */ jsx(StreamMarkdown, {
561
+ ...props,
562
+ components: renderedComponents,
563
+ content: renderedMarkdown,
564
+ "data-state": ownsStream && smooth.isDraining ? "draining" : ownsStream && stream.isStreaming ? "streaming" : "idle",
565
+ live,
566
+ ref,
567
+ remarkPlugins: renderedRemarkPlugins,
568
+ remarkRehypeOptions: renderedEntities.length === 0 ? void 0 : messageEntityRehypeOptions()
569
+ });
570
+ });
571
+ function markdownComponents(components, entities, renderEntity) {
572
+ const consumerSpan = components?.span;
573
+ return {
574
+ code: defaultCodeComponent,
575
+ pre: defaultPreComponent,
576
+ ...components,
577
+ span(spanProps) {
578
+ const internalProps = spanProps;
579
+ const entityIndexValue = internalProps["data-entity-index"];
580
+ const entityIndex = typeof entityIndexValue === "string" && /^\d+$/.test(entityIndexValue) ? Number(entityIndexValue) : void 0;
581
+ const entity = entityIndex === void 0 ? void 0 : entities[entityIndex];
582
+ const consumerProps = { ...internalProps };
583
+ delete consumerProps["data-entity-index"];
584
+ if (entity !== void 0) {
585
+ if (renderEntity !== void 0) return renderEntity(entity);
586
+ const entityProps = {
587
+ ...consumerProps,
588
+ "data-role": "entity"
589
+ };
590
+ if (consumerSpan !== void 0) return createElement(consumerSpan, entityProps);
591
+ const { node: _node, ...elementProps } = entityProps;
592
+ return /* @__PURE__ */ jsx(MessageEntity, {
593
+ ...elementProps,
594
+ entity
595
+ });
596
+ }
597
+ if (consumerSpan !== void 0) return createElement(consumerSpan, consumerProps);
598
+ const { node: _node, ...elementProps } = consumerProps;
599
+ return /* @__PURE__ */ jsx("span", { ...elementProps });
600
+ }
601
+ };
602
+ }
603
+ const MessageCodeBlock = forwardRef(function MessageCodeBlock({ children, code, language, ...props }, ref) {
604
+ return renderPrimitive("pre", {
605
+ ...props,
606
+ "data-role": "code-block",
607
+ children: children ?? /* @__PURE__ */ jsx("code", {
608
+ className: language === void 0 ? void 0 : `language-${language}`,
609
+ children: code ?? ""
610
+ })
611
+ }, ref);
612
+ });
613
+ const MessageReasoning = forwardRef(function MessageReasoning(props, ref) {
614
+ const { part } = useMessagePart();
615
+ if (part.type !== "reasoning") return null;
616
+ return renderPrimitive("details", {
617
+ ...props,
618
+ children: props.children ?? /* @__PURE__ */ jsxs(Fragment$1, { children: [/* @__PURE__ */ jsx("summary", { children: "Reasoning" }), /* @__PURE__ */ jsx("pre", { children: part.text })] })
619
+ }, ref);
620
+ });
621
+ const MessageTool = forwardRef(function MessageTool({ children, renderWhen = "always", ...props }, ref) {
622
+ const { part } = useMessagePart();
623
+ if (part.type !== "tool" || !shouldRenderTool(part, renderWhen)) return null;
624
+ const renderedChildren = typeof children === "function" ? children(part) : children ?? defaultToolContent();
625
+ return renderPrimitive("div", {
626
+ ...props,
627
+ children: renderedChildren,
628
+ "data-role": "tool",
629
+ "data-state": part.state
630
+ }, ref);
631
+ });
632
+ const MessageToolName = forwardRef(function MessageToolName(props, ref) {
633
+ const { part } = useMessagePart();
634
+ if (part.type !== "tool") return null;
635
+ return renderPrimitive("span", {
636
+ ...props,
637
+ children: props.children ?? part.toolName
638
+ }, ref);
639
+ });
640
+ const MessageToolStatus = forwardRef(function MessageToolStatus(props, ref) {
641
+ const { part } = useMessagePart();
642
+ if (part.type !== "tool") return null;
643
+ return renderPrimitive("span", {
644
+ ...props,
645
+ children: props.children ?? toolStateLabel(part.state),
646
+ "data-state": part.state
647
+ }, ref);
648
+ });
649
+ const MessageToolInput = forwardRef(function MessageToolInput(props, ref) {
650
+ const { part } = useMessagePart();
651
+ if (part.type !== "tool") return null;
652
+ return renderPrimitive("pre", {
653
+ ...props,
654
+ children: props.children ?? stringifyValue(part.input)
655
+ }, ref);
656
+ });
657
+ const MessageToolOutput = forwardRef(function MessageToolOutput(props, ref) {
658
+ const { part } = useMessagePart();
659
+ if (part.type !== "tool" || part.state !== "output-available") return null;
660
+ return renderPrimitive("pre", {
661
+ ...props,
662
+ children: props.children ?? stringifyValue(part.output)
663
+ }, ref);
664
+ });
665
+ const MessageToolError = forwardRef(function MessageToolError(props, ref) {
666
+ const { part } = useMessagePart();
667
+ if (part.type !== "tool" || part.state !== "error") return null;
668
+ return renderPrimitive("div", {
669
+ ...props,
670
+ children: props.children ?? part.error.message,
671
+ role: props.role ?? "alert"
672
+ }, ref);
673
+ });
674
+ const MessageAttachment = forwardRef(function MessageAttachment({ children, ...props }, ref) {
675
+ const { part } = useMessagePart();
676
+ if (part.type !== "attachment") return null;
677
+ const renderedChildren = typeof children === "function" ? children(part.attachment) : children ?? /* @__PURE__ */ jsx(AttachmentPrimitive.Root, {});
678
+ return /* @__PURE__ */ jsx(InternalAttachmentProvider, {
679
+ value: { attachment: part.attachment },
680
+ children: renderPrimitive("div", {
681
+ ...props,
682
+ children: renderedChildren
683
+ }, ref)
684
+ });
685
+ });
686
+ const MessageData = forwardRef(function MessageData(props, ref) {
687
+ const { part } = useMessagePart();
688
+ if (part.type !== "data") return null;
689
+ return renderPrimitive("pre", {
690
+ ...props,
691
+ children: props.children ?? stringifyValue(part.data)
692
+ }, ref);
693
+ });
694
+ const MessageError = forwardRef(function MessageError(props, ref) {
695
+ const { part } = useMessagePart();
696
+ if (part.type !== "error") return null;
697
+ return renderPrimitive("div", {
698
+ ...props,
699
+ children: props.children ?? part.error.message,
700
+ role: props.role ?? "alert"
701
+ }, ref);
702
+ });
703
+ function defaultPart(part) {
704
+ if (part.type === "text") return /* @__PURE__ */ jsx(MessageText, {});
705
+ if (part.type === "reasoning") return /* @__PURE__ */ jsx(MessageReasoning, {});
706
+ if (part.type === "tool") return /* @__PURE__ */ jsx(MessageTool, {});
707
+ if (part.type === "attachment") return /* @__PURE__ */ jsx(MessageAttachment, {});
708
+ if (part.type === "data") return /* @__PURE__ */ jsx(MessageData, {});
709
+ return /* @__PURE__ */ jsx(MessageError, {});
710
+ }
711
+ function defaultToolContent() {
712
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [
713
+ /* @__PURE__ */ jsx(MessageToolName, {}),
714
+ /* @__PURE__ */ jsx(MessageToolStatus, {}),
715
+ /* @__PURE__ */ jsx(MessageToolInput, {}),
716
+ /* @__PURE__ */ jsx(MessageToolOutput, {}),
717
+ /* @__PURE__ */ jsx(MessageToolError, {})
718
+ ] });
719
+ }
720
+ function defaultCodeComponent({ children, className, node: _node, ...props }) {
721
+ return /* @__PURE__ */ jsx("code", {
722
+ className,
723
+ ...props,
724
+ children
725
+ });
726
+ }
727
+ function defaultPreComponent({ children, node: _node, ...props }) {
728
+ return /* @__PURE__ */ jsx(MessageCodeBlock, {
729
+ ...props,
730
+ children
731
+ });
732
+ }
733
+ function shouldRenderTool(part, renderWhen) {
734
+ if (renderWhen === "always") return true;
735
+ if (renderWhen === "pending") return part.state === "input-streaming" || part.state === "input-available";
736
+ return part.state === "output-available" || part.state === "error";
737
+ }
738
+ function toolStateLabel(state) {
739
+ if (state === "output-available") return "Done";
740
+ if (state === "error") return "Error";
741
+ return "Running";
742
+ }
743
+ // src/message/index.ts
744
+ const MessagePrimitive = {
745
+ Root: forwardRef(function MessageRoot({ children, ...props }, ref) {
746
+ const { message } = useMessage();
747
+ const renderedChildren = typeof children === "function" ? children(message) : children;
748
+ const registerElement = useCallback((element) => {
749
+ if (element !== null) registerMessageElement(element, message.id);
750
+ }, [message.id]);
751
+ const composedRef = useMemo(() => composeRefs(ref, registerElement), [ref, registerElement]);
752
+ return renderPrimitive("article", {
753
+ ...props,
754
+ children: renderedChildren,
755
+ "data-role": message.role
756
+ }, composedRef);
757
+ }),
758
+ Content: forwardRef(function MessageContent(props, ref) {
759
+ return renderPrimitive("div", { ...props }, ref);
760
+ }),
761
+ Parts: MessageParts,
762
+ Part: MessagePart,
763
+ Text: MessageText,
764
+ Markdown: MessageMarkdown,
765
+ Entity: MessageEntity,
766
+ CodeBlock: MessageCodeBlock,
767
+ Reasoning: MessageReasoning,
768
+ Tool: MessageTool,
769
+ ToolName: MessageToolName,
770
+ ToolInput: MessageToolInput,
771
+ ToolOutput: MessageToolOutput,
772
+ ToolError: MessageToolError,
773
+ ToolStatus: MessageToolStatus,
774
+ Attachment: MessageAttachment,
775
+ Data: MessageData,
776
+ Error: MessageError,
777
+ Actions: MessageActions,
778
+ Copy: MessageCopy,
779
+ Regenerate: MessageRegenerate
780
+ };
781
+ export { shiftComposerEntityRanges as n, isComposerEntityData as r, MessagePrimitive as t };
782
+
783
+ //# sourceMappingURL=message-CElFEKgL.js.map