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