@apia/ai 5.2.0 → 5.2.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 (41) hide show
  1. package/dist/assistant/AssistantController.d.ts +2 -0
  2. package/dist/assistant/AssistantController.js.map +1 -1
  3. package/dist/assistant/PanelAssistantTransport.d.ts +1 -0
  4. package/dist/assistant/PanelAssistantTransport.js +13 -4
  5. package/dist/assistant/PanelAssistantTransport.js.map +1 -1
  6. package/dist/chat/ChatController/ChatController.d.ts +10 -1
  7. package/dist/chat/ChatController/ChatController.js +89 -4
  8. package/dist/chat/ChatController/ChatController.js.map +1 -1
  9. package/dist/chat/ChatController/annotationStyles.js +165 -0
  10. package/dist/chat/ChatController/annotationStyles.js.map +1 -0
  11. package/dist/chat/ChatController/annotationTargets.d.ts +10 -0
  12. package/dist/chat/ChatController/annotationTargets.d.ts.map +1 -0
  13. package/dist/chat/ChatController/annotationTargets.js +35 -0
  14. package/dist/chat/ChatController/annotationTargets.js.map +1 -0
  15. package/dist/chat/ChatController/components/ChatMessage.d.ts +5 -2
  16. package/dist/chat/ChatController/components/ChatMessage.js +14 -3
  17. package/dist/chat/ChatController/components/ChatMessage.js.map +1 -1
  18. package/dist/chat/ChatController/components/History.d.ts +11 -1
  19. package/dist/chat/ChatController/components/History.js +262 -41
  20. package/dist/chat/ChatController/components/History.js.map +1 -1
  21. package/dist/chat/ChatController/components/Message.d.ts +3 -2
  22. package/dist/chat/ChatController/components/Message.js +8 -3
  23. package/dist/chat/ChatController/components/Message.js.map +1 -1
  24. package/dist/chat/ChatController/components/MessageBlocks.d.ts +5 -2
  25. package/dist/chat/ChatController/components/MessageBlocks.js +48 -6
  26. package/dist/chat/ChatController/components/MessageBlocks.js.map +1 -1
  27. package/dist/chat/ChatController/components/PendingMessagesPanel.js +3 -0
  28. package/dist/chat/ChatController/components/PendingMessagesPanel.js.map +1 -1
  29. package/dist/chat/ChatController/components/SentAnnotations.js +24 -0
  30. package/dist/chat/ChatController/components/SentAnnotations.js.map +1 -0
  31. package/dist/chat/ChatController/components/TextArea.d.ts +5 -2
  32. package/dist/chat/ChatController/components/TextArea.js +67 -4
  33. package/dist/chat/ChatController/components/TextArea.js.map +1 -1
  34. package/dist/chat/ChatController/types.d.ts +21 -1
  35. package/dist/index.d.ts +4 -3
  36. package/dist/index.js +3 -2
  37. package/dist/index.js.map +1 -1
  38. package/dist/stream/ResponseStream.d.ts +6 -1
  39. package/dist/stream/ResponseStream.js +26 -8
  40. package/dist/stream/ResponseStream.js.map +1 -1
  41. package/package.json +8 -8
@@ -1,56 +1,277 @@
1
- import { jsx, jsxs } from '@apia/theme/jsx-runtime';
1
+ import { jsxs, Fragment, jsx } from '@apia/theme/jsx-runtime';
2
2
  import { observer } from 'mobx-react-lite';
3
3
  import { Box, getVariant } from '@apia/theme';
4
4
  import { Message } from './Message.js';
5
5
  import { AutoscrollContainer } from '../../AutoscrollContainer.js';
6
+ import { useRef, useState, useEffect } from 'react';
7
+ import { createPortal } from 'react-dom';
8
+ import { annotationPopoverStyles } from '../annotationStyles.js';
6
9
 
10
+ function positionForRange(range, width = 360, height = 210) {
11
+ const rect = range.getBoundingClientRect();
12
+ return {
13
+ x: Math.min(Math.max(rect.left, 8), Math.max(8, window.innerWidth - width - 8)),
14
+ y: rect.bottom + height + 8 > window.innerHeight ? Math.max(8, rect.top - height - 8) : Math.max(8, rect.bottom + 8)
15
+ };
16
+ }
7
17
  const History = observer(
8
18
  ({
9
19
  messages,
10
20
  bottomInset = 0,
11
- className
21
+ className,
22
+ canAnnotate = false,
23
+ editingAnnotation,
24
+ onDismissEdit,
25
+ onUpdateAnnotationComment,
26
+ resolveAnnotations,
27
+ onAnnotate
12
28
  }) => {
29
+ const historyRef = useRef(null);
30
+ const popoverRef = useRef(null);
31
+ const [selectionAction, setSelectionAction] = useState(null);
32
+ const [comment, setComment] = useState("");
33
+ const dismissSelection = () => {
34
+ if (selectionAction?.editingId)
35
+ onDismissEdit?.();
36
+ setSelectionAction(null);
37
+ setComment("");
38
+ };
39
+ useEffect(() => {
40
+ if (!selectionAction)
41
+ return;
42
+ const dismiss = () => {
43
+ if (selectionAction.editingId)
44
+ onDismissEdit?.();
45
+ setSelectionAction(null);
46
+ setComment("");
47
+ };
48
+ const onPointerDown = (event) => {
49
+ const target = event.target;
50
+ if (!(target instanceof Node) || popoverRef.current?.contains(target) || historyRef.current?.contains(target))
51
+ return;
52
+ dismiss();
53
+ };
54
+ const onKeyDown = (event) => {
55
+ if (event.key === "Escape")
56
+ dismiss();
57
+ };
58
+ document.addEventListener("pointerdown", onPointerDown);
59
+ document.addEventListener("keydown", onKeyDown);
60
+ return () => {
61
+ document.removeEventListener("pointerdown", onPointerDown);
62
+ document.removeEventListener("keydown", onKeyDown);
63
+ };
64
+ }, [Boolean(selectionAction), selectionAction?.editingId]);
65
+ useEffect(() => {
66
+ if (!editingAnnotation) {
67
+ setSelectionAction((current) => current?.editingId ? null : current);
68
+ return;
69
+ }
70
+ if (!historyRef.current)
71
+ return;
72
+ const { annotation, range } = editingAnnotation;
73
+ if (!range?.startContainer.isConnected || !historyRef.current.contains(range.commonAncestorContainer)) {
74
+ onDismissEdit?.();
75
+ return;
76
+ }
77
+ const selection = window.getSelection();
78
+ selection?.removeAllRanges();
79
+ selection?.addRange(range.cloneRange());
80
+ range.startContainer.parentElement?.scrollIntoView({ block: "center", behavior: "auto" });
81
+ const frame = requestAnimationFrame(() => {
82
+ setSelectionAction({
83
+ annotations: [annotation],
84
+ text: annotation.text,
85
+ ...positionForRange(range),
86
+ range,
87
+ editingId: annotation.id,
88
+ editorOpen: true
89
+ });
90
+ setComment(annotation.comment || "");
91
+ });
92
+ return () => cancelAnimationFrame(frame);
93
+ }, [editingAnnotation?.annotation.id]);
94
+ useEffect(() => {
95
+ const range = selectionAction?.range;
96
+ if (!range)
97
+ return;
98
+ const updatePosition = () => {
99
+ if (!range.startContainer.isConnected) {
100
+ setSelectionAction(null);
101
+ onDismissEdit?.();
102
+ return;
103
+ }
104
+ const position = positionForRange(
105
+ range,
106
+ popoverRef.current?.offsetWidth,
107
+ popoverRef.current?.offsetHeight
108
+ );
109
+ setSelectionAction((current) => {
110
+ if (!current || current.range !== range || current.x === position.x && current.y === position.y)
111
+ return current;
112
+ return { ...current, ...position };
113
+ });
114
+ };
115
+ updatePosition();
116
+ window.addEventListener("scroll", updatePosition, true);
117
+ window.addEventListener("resize", updatePosition);
118
+ const observer2 = new ResizeObserver(updatePosition);
119
+ if (popoverRef.current)
120
+ observer2.observe(popoverRef.current);
121
+ return () => {
122
+ window.removeEventListener("scroll", updatePosition, true);
123
+ window.removeEventListener("resize", updatePosition);
124
+ observer2.disconnect();
125
+ };
126
+ }, [selectionAction?.range]);
127
+ const inspectSelection = () => {
128
+ if (!canAnnotate || !historyRef.current)
129
+ return;
130
+ const selection = window.getSelection();
131
+ const root = historyRef.current;
132
+ if (!selection || selection.isCollapsed || !selection.anchorNode || !selection.focusNode || !root.contains(selection.anchorNode) || !root.contains(selection.focusNode) || selection.rangeCount === 0) {
133
+ dismissSelection();
134
+ return;
135
+ }
136
+ const text = selection.toString().trim();
137
+ if (!text) {
138
+ dismissSelection();
139
+ return;
140
+ }
141
+ const range = selection.getRangeAt(0);
142
+ const sourceMessageIds = Array.from(
143
+ root.querySelectorAll("[data-chat-message-id]")
144
+ ).filter((element) => range.intersectsNode(element)).map((element) => Number(element.dataset.chatMessageId)).filter(Number.isFinite);
145
+ if (sourceMessageIds.length === 0) {
146
+ dismissSelection();
147
+ return;
148
+ }
149
+ const annotations = resolveAnnotations?.({
150
+ selection,
151
+ root,
152
+ text,
153
+ sourceMessageIds
154
+ });
155
+ if (!annotations?.length) {
156
+ dismissSelection();
157
+ return;
158
+ }
159
+ if (editingAnnotation)
160
+ onDismissEdit?.();
161
+ setSelectionAction({
162
+ annotations,
163
+ text,
164
+ ...positionForRange(range, 140, 36),
165
+ range: range.cloneRange(),
166
+ editorOpen: false
167
+ });
168
+ setComment("");
169
+ };
13
170
  let currentType = "";
14
- return /* @__PURE__ */ jsx(AutoscrollContainer, { className, children: /* @__PURE__ */ jsxs(
15
- Box,
16
- {
17
- ...getVariant("layout.common.chat.history"),
18
- className: "chat_history",
19
- children: [
20
- messages.map((current, i) => {
21
- let first = false;
22
- let last = false;
23
- if (currentType !== current.messageType) {
24
- currentType = current.messageType;
25
- first = true;
26
- }
27
- if (i === messages.length - 1 || messages[i + 1].messageType !== current.messageType) {
28
- last = true;
29
- }
30
- return /* @__PURE__ */ jsx(
31
- Message,
171
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
172
+ /* @__PURE__ */ jsx(AutoscrollContainer, { className, children: /* @__PURE__ */ jsxs(
173
+ Box,
174
+ {
175
+ ref: historyRef,
176
+ onMouseUp: inspectSelection,
177
+ onKeyUp: inspectSelection,
178
+ ...getVariant("layout.common.chat.history"),
179
+ className: "chat_history",
180
+ children: [
181
+ messages.map((current, i) => {
182
+ let first = false;
183
+ let last = false;
184
+ if (currentType !== current.messageType) {
185
+ currentType = current.messageType;
186
+ first = true;
187
+ }
188
+ if (i === messages.length - 1 || messages[i + 1].messageType !== current.messageType) {
189
+ last = true;
190
+ }
191
+ return /* @__PURE__ */ jsx(
192
+ Message,
193
+ {
194
+ className: first ? "first" : last ? "last" : void 0,
195
+ type: current.messageType,
196
+ id: String(current.id),
197
+ attachments: current.attachments,
198
+ annotations: current.annotations,
199
+ reference: current.reference,
200
+ message: current.message
201
+ },
202
+ String(current.id)
203
+ );
204
+ }),
205
+ bottomInset > 0 && /* @__PURE__ */ jsx(
206
+ Box,
32
207
  {
33
- className: first ? "first" : last ? "last" : void 0,
34
- type: current.messageType,
35
- id: String(current.id),
36
- attachments: current.attachments,
37
- reference: current.reference,
38
- message: current.message
39
- },
40
- String(current.id)
41
- );
42
- }),
43
- bottomInset > 0 && /* @__PURE__ */ jsx(
44
- Box,
45
- {
46
- "aria-hidden": true,
47
- className: "chatHistory__bottomInset",
48
- style: { height: bottomInset }
49
- }
50
- )
51
- ]
52
- }
53
- ) });
208
+ "aria-hidden": true,
209
+ className: "chatHistory__bottomInset",
210
+ style: { height: bottomInset }
211
+ }
212
+ )
213
+ ]
214
+ }
215
+ ) }),
216
+ selectionAction && createPortal(
217
+ /* @__PURE__ */ jsx(
218
+ Box,
219
+ {
220
+ ref: popoverRef,
221
+ className: `chatAnnotationPopover${selectionAction.editorOpen ? "" : " chatAnnotationPopover--offer"}`,
222
+ sx: annotationPopoverStyles,
223
+ style: { left: selectionAction.x, top: selectionAction.y },
224
+ children: !selectionAction.editorOpen ? /* @__PURE__ */ jsx(
225
+ "button",
226
+ {
227
+ type: "button",
228
+ className: "chatAnnotationPopover__offer",
229
+ onMouseDown: (event) => event.preventDefault(),
230
+ onClick: () => setSelectionAction((current) => current ? { ...current, editorOpen: true } : current),
231
+ children: "A\xF1adir al chat"
232
+ }
233
+ ) : /* @__PURE__ */ jsxs(Fragment, { children: [
234
+ /* @__PURE__ */ jsx(Box, { className: "chatAnnotationPopover__preview", title: selectionAction.text, children: selectionAction.text }),
235
+ /* @__PURE__ */ jsx(
236
+ "textarea",
237
+ {
238
+ autoFocus: true,
239
+ "aria-label": "Comentario sobre la selecci\xF3n",
240
+ className: "chatAnnotationPopover__comment",
241
+ placeholder: "A\xF1ade un comentario opcional...",
242
+ value: comment,
243
+ onChange: (event) => setComment(event.target.value),
244
+ onKeyDown: (event) => {
245
+ if (event.key === "Escape")
246
+ dismissSelection();
247
+ }
248
+ }
249
+ ),
250
+ /* @__PURE__ */ jsxs(Box, { className: "chatAnnotationPopover__actions", children: [
251
+ /* @__PURE__ */ jsx("button", { type: "button", onClick: dismissSelection, children: "Cancelar" }),
252
+ /* @__PURE__ */ jsx(
253
+ "button",
254
+ {
255
+ type: "button",
256
+ onClick: () => {
257
+ if (selectionAction.editingId) {
258
+ onUpdateAnnotationComment?.(selectionAction.editingId, comment);
259
+ } else {
260
+ onAnnotate?.(selectionAction.annotations, comment, selectionAction.range);
261
+ }
262
+ window.getSelection()?.removeAllRanges();
263
+ dismissSelection();
264
+ },
265
+ children: selectionAction.editingId ? "Guardar" : "A\xF1adir al chat"
266
+ }
267
+ )
268
+ ] })
269
+ ] })
270
+ }
271
+ ),
272
+ document.body
273
+ )
274
+ ] });
54
275
  }
55
276
  );
56
277
 
@@ -1 +1 @@
1
- {"version":3,"file":"History.js","sources":["../../../../src/chat/ChatController/components/History.tsx"],"sourcesContent":["import { observer } from 'mobx-react-lite';\r\nimport { Box, getVariant } from '@apia/theme';\r\nimport { ChatMessage } from './ChatMessage';\r\nimport { Message } from './Message';\r\nimport { AutoscrollContainer } from '../../AutoscrollContainer';\r\n\r\nexport const History = observer(\r\n ({\r\n messages,\r\n bottomInset = 0,\r\n className,\r\n }: {\r\n messages: ChatMessage[];\r\n bottomInset?: number;\r\n className?: string;\r\n }) => {\r\n let currentType: string = '';\r\n\r\n return (\r\n <AutoscrollContainer className={className}>\r\n <Box\r\n {...getVariant('layout.common.chat.history')}\r\n className=\"chat_history\"\r\n >\r\n {messages.map((current, i) => {\r\n let first = false;\r\n let last = false;\r\n\r\n if (currentType !== current.messageType) {\r\n currentType = current.messageType;\r\n first = true;\r\n }\r\n\r\n if (\r\n i === messages.length - 1 ||\r\n messages[i + 1].messageType !== current.messageType\r\n ) {\r\n last = true;\r\n }\r\n\r\n return (\r\n <Message\r\n key={String(current.id)}\r\n className={first ? 'first' : last ? 'last' : undefined}\r\n type={current.messageType}\r\n id={String(current.id)}\r\n attachments={current.attachments}\r\n reference={current.reference}\r\n message={current.message}\r\n />\r\n );\r\n })}\r\n {bottomInset > 0 && (\r\n <Box\r\n aria-hidden\r\n className=\"chatHistory__bottomInset\"\r\n style={{ height: bottomInset }}\r\n />\r\n )}\r\n </Box>\r\n </AutoscrollContainer>\r\n );\r\n },\r\n);\r\n"],"names":[],"mappings":";;;;;;AAMO,MAAM,OAAA,GAAU,QAAA;AAAA,EACrB,CAAC;AAAA,IACC,QAAA;AAAA,IACA,WAAA,GAAc,CAAA;AAAA,IACd;AAA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0BAAA;AAAA,cACV,KAAA,EAAO,EAAE,MAAA,EAAQ,WAAA;AAAY;AAAA;AAC/B;AAAA;AAAA,KAEJ,EACF,CAAA;AAAA;AAGN;;;;"}
1
+ {"version":3,"file":"History.js","sources":["../../../../src/chat/ChatController/components/History.tsx"],"sourcesContent":["import { observer } from 'mobx-react-lite';\r\nimport { Box, getVariant } from '@apia/theme';\r\nimport { ChatMessage } from './ChatMessage';\r\nimport { Message } from './Message';\r\nimport { AutoscrollContainer } from '../../AutoscrollContainer';\nimport { useEffect, useRef, useState } from 'react';\nimport { createPortal } from 'react-dom';\nimport type { TChatAnnotation, TChatAnnotationSelection } from '../types';\nimport { annotationPopoverStyles } from '../annotationStyles';\n\nfunction positionForRange(range: Range, width = 360, height = 210) {\r\n const rect = range.getBoundingClientRect();\n return {\n x: Math.min(Math.max(rect.left, 8), Math.max(8, window.innerWidth - width - 8)),\r\n y: rect.bottom + height + 8 > window.innerHeight\r\n ? Math.max(8, rect.top - height - 8)\r\n : Math.max(8, rect.bottom + 8),\n };\n}\n\r\nexport const History = observer(\r\n ({\r\n messages,\r\n bottomInset = 0,\r\n className,\n canAnnotate = false,\n editingAnnotation,\n onDismissEdit,\n onUpdateAnnotationComment,\n resolveAnnotations,\n onAnnotate,\n }: {\n messages: ChatMessage[];\r\n bottomInset?: number;\r\n className?: string;\n canAnnotate?: boolean;\n editingAnnotation?: { annotation: TChatAnnotation; range?: Range };\n onDismissEdit?: () => void;\n onUpdateAnnotationComment?: (id: string, comment: string) => void;\n resolveAnnotations?: (selection: TChatAnnotationSelection) => TChatAnnotation[];\n onAnnotate?: (annotations: TChatAnnotation[], comment?: string, range?: Range) => void;\n }) => {\n const historyRef = useRef<HTMLDivElement>(null);\n const popoverRef = useRef<HTMLDivElement>(null);\r\n const [selectionAction, setSelectionAction] = useState<{\n annotations: TChatAnnotation[];\n text: string;\n x: number;\n y: number;\n range?: Range;\n editingId?: string;\n editorOpen: boolean;\r\n } | null>(null);\n const [comment, setComment] = useState('');\n\n const dismissSelection = () => {\n if (selectionAction?.editingId) onDismissEdit?.();\n setSelectionAction(null);\n setComment('');\n };\n\r\n useEffect(() => {\r\n if (!selectionAction) return;\r\n const dismiss = () => {\r\n if (selectionAction.editingId) onDismissEdit?.();\r\n setSelectionAction(null);\r\n setComment('');\r\n };\r\n const onPointerDown = (event: PointerEvent) => {\r\n const target = event.target;\r\n if (!(target instanceof Node) || popoverRef.current?.contains(target) || historyRef.current?.contains(target)) return;\r\n dismiss();\r\n };\r\n const onKeyDown = (event: KeyboardEvent) => {\r\n if (event.key === 'Escape') dismiss();\r\n };\r\n document.addEventListener('pointerdown', onPointerDown);\r\n document.addEventListener('keydown', onKeyDown);\r\n return () => {\r\n document.removeEventListener('pointerdown', onPointerDown);\r\n document.removeEventListener('keydown', onKeyDown);\r\n };\r\n }, [Boolean(selectionAction), selectionAction?.editingId]);\r\n\n useEffect(() => {\n if (!editingAnnotation) {\r\n setSelectionAction((current) => current?.editingId ? null : current);\r\n return;\r\n }\r\n if (!historyRef.current) return;\r\n const { annotation, range } = editingAnnotation;\n if (!range?.startContainer.isConnected || !historyRef.current.contains(range.commonAncestorContainer)) {\n onDismissEdit?.();\n return;\n }\n const selection = window.getSelection();\n selection?.removeAllRanges();\n selection?.addRange(range.cloneRange());\n range.startContainer.parentElement?.scrollIntoView({ block: 'center', behavior: 'auto' });\n const frame = requestAnimationFrame(() => {\n setSelectionAction({\n annotations: [annotation],\n text: annotation.text,\n ...positionForRange(range),\n range,\r\n editingId: annotation.id,\n editorOpen: true,\r\n });\n setComment(annotation.comment || '');\n });\n return () => cancelAnimationFrame(frame);\n }, [editingAnnotation?.annotation.id]);\n\r\n useEffect(() => {\r\n const range = selectionAction?.range;\r\n if (!range) return;\r\n const updatePosition = () => {\r\n if (!range.startContainer.isConnected) {\r\n setSelectionAction(null);\r\n onDismissEdit?.();\r\n return;\r\n }\r\n const position = positionForRange(\r\n range,\r\n popoverRef.current?.offsetWidth,\r\n popoverRef.current?.offsetHeight,\r\n );\r\n setSelectionAction((current) => {\r\n if (!current || current.range !== range || (current.x === position.x && current.y === position.y)) return current;\r\n return { ...current, ...position };\r\n });\r\n };\r\n updatePosition();\r\n window.addEventListener('scroll', updatePosition, true);\r\n window.addEventListener('resize', updatePosition);\r\n const observer = new ResizeObserver(updatePosition);\r\n if (popoverRef.current) observer.observe(popoverRef.current);\r\n return () => {\r\n window.removeEventListener('scroll', updatePosition, true);\r\n window.removeEventListener('resize', updatePosition);\r\n observer.disconnect();\r\n };\r\n }, [selectionAction?.range]);\r\n\n const inspectSelection = () => {\n if (!canAnnotate || !historyRef.current) return;\n const selection = window.getSelection();\n const root = historyRef.current;\n if (\n !selection ||\n selection.isCollapsed ||\n !selection.anchorNode ||\n !selection.focusNode ||\n !root.contains(selection.anchorNode) ||\n !root.contains(selection.focusNode) ||\n selection.rangeCount === 0\n ) {\n dismissSelection();\n return;\n }\n const text = selection.toString().trim();\n if (!text) {\n dismissSelection();\n return;\n }\n const range = selection.getRangeAt(0);\n const sourceMessageIds = Array.from(\n root.querySelectorAll<HTMLElement>('[data-chat-message-id]'),\n )\n .filter((element) => range.intersectsNode(element))\n .map((element) => Number(element.dataset.chatMessageId))\n .filter(Number.isFinite);\n if (sourceMessageIds.length === 0) {\n dismissSelection();\n return;\n }\n const annotations = resolveAnnotations?.({\n selection,\n root,\n text,\n sourceMessageIds,\n });\n if (!annotations?.length) {\n dismissSelection();\n return;\n }\n if (editingAnnotation) onDismissEdit?.();\n setSelectionAction({\n annotations,\n text,\n ...positionForRange(range, 140, 36),\r\n range: range.cloneRange(),\n editorOpen: false,\r\n });\n setComment('');\n };\n let currentType: string = '';\n\r\n return (\n <>\n <AutoscrollContainer className={className}>\n <Box\n ref={historyRef}\n onMouseUp={inspectSelection}\n onKeyUp={inspectSelection}\n {...getVariant('layout.common.chat.history')}\n className=\"chat_history\"\r\n >\r\n {messages.map((current, i) => {\r\n let first = false;\r\n let last = false;\r\n\r\n if (currentType !== current.messageType) {\r\n currentType = current.messageType;\r\n first = true;\r\n }\r\n\r\n if (\r\n i === messages.length - 1 ||\r\n messages[i + 1].messageType !== current.messageType\r\n ) {\r\n last = true;\r\n }\r\n\r\n return (\r\n <Message\r\n key={String(current.id)}\r\n className={first ? 'first' : last ? 'last' : undefined}\r\n type={current.messageType}\r\n id={String(current.id)}\r\n attachments={current.attachments}\n annotations={current.annotations}\n reference={current.reference}\r\n message={current.message}\r\n />\r\n );\r\n })}\r\n {bottomInset > 0 && (\n <Box\r\n aria-hidden\r\n className=\"chatHistory__bottomInset\"\r\n style={{ height: bottomInset }}\r\n />\r\n )}\n </Box>\n </AutoscrollContainer>\n {selectionAction && createPortal(\n <Box\n ref={popoverRef}\r\n className={`chatAnnotationPopover${selectionAction.editorOpen ? '' : ' chatAnnotationPopover--offer'}`}\r\n sx={annotationPopoverStyles}\n style={{ left: selectionAction.x, top: selectionAction.y }}\n >\n {!selectionAction.editorOpen ? (\r\n <button\r\n type=\"button\"\r\n className=\"chatAnnotationPopover__offer\"\r\n onMouseDown={(event) => event.preventDefault()}\r\n onClick={() => setSelectionAction((current) => current ? { ...current, editorOpen: true } : current)}\r\n >\r\n Añadir al chat\r\n </button>\r\n ) : (\r\n <>\r\n <Box className=\"chatAnnotationPopover__preview\" title={selectionAction.text}>\n {selectionAction.text}\n </Box>\n <textarea\n autoFocus\n aria-label=\"Comentario sobre la selección\"\n className=\"chatAnnotationPopover__comment\"\n placeholder=\"Añade un comentario opcional...\"\n value={comment}\n onChange={(event) => setComment(event.target.value)}\n onKeyDown={(event) => {\n if (event.key === 'Escape') dismissSelection();\n }}\n />\n <Box className=\"chatAnnotationPopover__actions\">\n <button type=\"button\" onClick={dismissSelection}>Cancelar</button>\n <button\n type=\"button\"\n onClick={() => {\n if (selectionAction.editingId) {\n onUpdateAnnotationComment?.(selectionAction.editingId, comment);\n } else {\n onAnnotate?.(selectionAction.annotations, comment, selectionAction.range);\n }\n window.getSelection()?.removeAllRanges();\n dismissSelection();\n }}\n >\n {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E,sBAAA,MAAA,CAAO,YAAA,IAAgB,eAAA,EAAgB;AACvC,sBAAA,gBAAA,EAAiB;AAAA,qBACnB;AAAA,oBAEC,QAAA,EAAA,eAAA,CAAgB,YAAY,SAAA,GAAY;AAAA;AAAA;AAC3C,eAAA,EACF;AAAA,aAAA,EACE;AAAA;AAAA,SAEJ;AAAA,QACA,QAAA,CAAS;AAAA;AACX,KAAA,EACA,CAAA;AAAA;AAGN;;;;"}
@@ -1,13 +1,14 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode } from 'react';
3
- import { TMessageType, IAttachment } from '../types.js';
3
+ import { TMessageType, IAttachment, TChatAnnotation } from '../types.js';
4
4
 
5
- declare const Message: (({ className, type, id, message, attachments, reference, }: {
5
+ declare const Message: (({ className, type, id, message, attachments, annotations, reference, }: {
6
6
  className?: string;
7
7
  type: TMessageType;
8
8
  id: string;
9
9
  message: ReactNode;
10
10
  attachments: IAttachment[];
11
+ annotations: TChatAnnotation[];
11
12
  reference: ReactNode;
12
13
  }) => react.JSX.Element) & {
13
14
  displayName: string;
@@ -2,7 +2,8 @@ import { jsxs, jsx } from '@apia/theme/jsx-runtime';
2
2
  import { Box } from '@apia/theme';
3
3
  import { observer } from 'mobx-react-lite';
4
4
  import { Attachments } from './Attachments.js';
5
- import { MessageBlocks } from './MessageBlocks.js';
5
+ import { hasResolvedAttachmentPlaceholder, MessageBlocks } from './MessageBlocks.js';
6
+ import { SentAnnotations } from './SentAnnotations.js';
6
7
 
7
8
  const Message = observer(
8
9
  ({
@@ -11,9 +12,11 @@ const Message = observer(
11
12
  id,
12
13
  message,
13
14
  attachments,
15
+ annotations,
14
16
  reference
15
17
  }) => {
16
- return /* @__PURE__ */ jsxs(Box, { as: "pre", className: `history_message ${className || ""} ${type}`, children: [
18
+ const rendersAttachmentInline = typeof message === "string" && hasResolvedAttachmentPlaceholder(message, attachments);
19
+ return /* @__PURE__ */ jsxs(Box, { as: "pre", "data-chat-message-id": id, className: `history_message ${className || ""} ${type}`, children: [
17
20
  /* @__PURE__ */ jsx(
18
21
  Box,
19
22
  {
@@ -22,13 +25,15 @@ const Message = observer(
22
25
  children: /* @__PURE__ */ jsx(
23
26
  MessageBlocks,
24
27
  {
28
+ attachments,
25
29
  streamParts: [{ blockType: "text", str: message }]
26
30
  }
27
31
  )
28
32
  } : { children: message }
29
33
  }
30
34
  ),
31
- attachments.length > 0 && /* @__PURE__ */ jsx(Attachments, { attachments }),
35
+ attachments.length > 0 && !rendersAttachmentInline && /* @__PURE__ */ jsx(Attachments, { attachments }),
36
+ annotations.length > 0 && /* @__PURE__ */ jsx(SentAnnotations, { annotations }),
32
37
  reference && /* @__PURE__ */ jsx(Box, { className: "history_message__references", children: reference })
33
38
  ] });
34
39
  }
@@ -1 +1 @@
1
- {"version":3,"file":"Message.js","sources":["../../../../src/chat/ChatController/components/Message.tsx"],"sourcesContent":["import { Box } from '@apia/theme';\r\nimport { observer } from 'mobx-react-lite';\r\nimport { ReactNode } from 'react';\r\nimport { Attachments } from './Attachments';\r\nimport { IAttachment, TMessageType } from '../types';\r\nimport { MessageBlocks } from './MessageBlocks';\r\n\r\nexport const Message = observer(\r\n ({\r\n className,\r\n type,\r\n id,\r\n message,\r\n attachments,\r\n reference,\r\n }: {\r\n className?: string;\r\n type: TMessageType;\r\n id: string;\r\n message: ReactNode;\r\n attachments: IAttachment[];\r\n reference: ReactNode;\r\n }) => {\r\n return (\r\n <Box as=\"pre\" className={`history_message ${className || ''} ${type}`}>\r\n <Box\r\n className=\"history_message__content\"\r\n {...(typeof message === 'string'\r\n ? {\r\n children: (\r\n <MessageBlocks\r\n streamParts={[{ blockType: 'text', str: message }]}\r\n ></MessageBlocks>\r\n ),\r\n }\r\n : { children: message as string })}\r\n />\r\n {attachments.length > 0 && (\r\n <Attachments attachments={attachments}></Attachments>\r\n )}\r\n {reference && (\r\n <Box className=\"history_message__references\">{reference}</Box>\r\n )}\r\n </Box>\r\n );\r\n },\r\n);\r\n"],"names":[],"mappings":";;;;;;AAOO,MAAM,OAAA,GAAU,QAAA;AAAA,EACrB,CAAC;AAAA,IACC,SAAA;AAAA,IACA,IAAA;AAAA,IACA,EAAA;AAAA,IACA,OAAA;AAAA,IACA,WAAA;AAAA,IACA;AAAA,GACF,KAOM;AACJ,IAAA,uBACE,IAAA,CAAC,GAAA,EAAA,EAAI,EAAA,EAAG,KAAA,EAAM,SAAA,EAAW,mBAAmB,SAAA,IAAa,EAAE,CAAA,CAAA,EAAI,IAAI,CAAA,CAAA,EACjE,QAAA,EAAA;AAAA,sBAAA,GAAA;AAAA,QAAC,GAAA;AAAA,QAAA;AAAA,UACC,SAAA,EAAU,0BAAA;AAAA,UACT,GAAI,OAAO,OAAA,KAAY,QAAA,GACpB;AAAA,YACE,QAAA,kBACE,GAAA;AAAA,cAAC,aAAA;AAAA,cAAA;AAAA,gBACC,aAAa,CAAC,EAAE,WAAW,MAAA,EAAQ,GAAA,EAAK,SAAS;AAAA;AAAA;AAClD,WAEL,GACA,EAAE,QAAA,EAAU,OAAA;AAAkB;AAAA,OACpC;AAAA,MACC,WAAA,CAAY,MAAA,GAAS,CAAA,oBACpB,GAAA,CAAC,eAAY,WAAA,EAA0B,CAAA;AAAA,MAExC,SAAA,oBACC,GAAA,CAAC,GAAA,EAAA,EAAI,SAAA,EAAU,+BAA+B,QAAA,EAAA,SAAA,EAAU;AAAA,KAAA,EAE5D,CAAA;AAAA;AAGN;;;;"}
1
+ {"version":3,"file":"Message.js","sources":["../../../../src/chat/ChatController/components/Message.tsx"],"sourcesContent":["import { Box } from '@apia/theme';\r\nimport { observer } from 'mobx-react-lite';\r\nimport { ReactNode } from 'react';\r\nimport { Attachments } from './Attachments';\r\nimport { IAttachment, TChatAnnotation, TMessageType } from '../types';\nimport {\n hasResolvedAttachmentPlaceholder,\n MessageBlocks,\n} from './MessageBlocks';\nimport { SentAnnotations } from './SentAnnotations';\r\n\r\nexport const Message = observer(\r\n ({\r\n className,\r\n type,\r\n id,\r\n message,\r\n attachments,\n annotations,\n reference,\r\n }: {\r\n className?: string;\r\n type: TMessageType;\r\n id: string;\r\n message: ReactNode;\r\n attachments: IAttachment[];\n annotations: TChatAnnotation[];\n reference: ReactNode;\r\n }) => {\r\n const rendersAttachmentInline =\r\n typeof message === 'string' &&\r\n hasResolvedAttachmentPlaceholder(message, attachments);\r\n\r\n return (\r\n <Box as=\"pre\" data-chat-message-id={id} className={`history_message ${className || ''} ${type}`}>\n <Box\r\n className=\"history_message__content\"\r\n {...(typeof message === 'string'\r\n ? {\r\n children: (\r\n <MessageBlocks\r\n attachments={attachments}\r\n streamParts={[{ blockType: 'text', str: message }]}\r\n ></MessageBlocks>\r\n ),\r\n }\r\n : { children: message as string })}\r\n />\r\n {attachments.length > 0 && !rendersAttachmentInline && (\n <Attachments attachments={attachments}></Attachments>\n )}\n {annotations.length > 0 && (\n <SentAnnotations annotations={annotations} />\r\n )}\n {reference && (\r\n <Box className=\"history_message__references\">{reference}</Box>\r\n )}\r\n </Box>\r\n );\r\n },\r\n);\r\n"],"names":[],"mappings":";;;;;;;AAWO,MAAM,OAAA,GAAU,QAAA;AAAA,EACrB,CAAC;AAAA,IACC,SAAA;AAAA,IACA,IAAA;AAAA,IACA,EAAA;AAAA,IACA,OAAA;AAAA,IACA,WAAA;AAAA,IACA,WAAA;AAAA,IACA;AAAA,GACF,KAQM;AACJ,IAAA,MAAM,0BACJ,OAAO,OAAA,KAAY,QAAA,IACnB,gCAAA,CAAiC,SAAS,WAAW,CAAA;AAEvD,IAAA,uBACE,IAAA,CAAC,GAAA,EAAA,EAAI,EAAA,EAAG,KAAA,EAAM,sBAAA,EAAsB,EAAA,EAAI,SAAA,EAAW,CAAA,gBAAA,EAAmB,SAAA,IAAa,EAAE,CAAA,CAAA,EAAI,IAAI,CAAA,CAAA,EAC3F,QAAA,EAAA;AAAA,sBAAA,GAAA;AAAA,QAAC,GAAA;AAAA,QAAA;AAAA,UACC,SAAA,EAAU,0BAAA;AAAA,UACT,GAAI,OAAO,OAAA,KAAY,QAAA,GACpB;AAAA,YACE,QAAA,kBACE,GAAA;AAAA,cAAC,aAAA;AAAA,cAAA;AAAA,gBACC,WAAA;AAAA,gBACA,aAAa,CAAC,EAAE,WAAW,MAAA,EAAQ,GAAA,EAAK,SAAS;AAAA;AAAA;AAClD,WAEL,GACA,EAAE,QAAA,EAAU,OAAA;AAAkB;AAAA,OACpC;AAAA,MACC,YAAY,MAAA,GAAS,CAAA,IAAK,CAAC,uBAAA,oBAC1B,GAAA,CAAC,eAAY,WAAA,EAA0B,CAAA;AAAA,MAExC,WAAA,CAAY,MAAA,GAAS,CAAA,oBACpB,GAAA,CAAC,mBAAgB,WAAA,EAA0B,CAAA;AAAA,MAE5C,SAAA,oBACC,GAAA,CAAC,GAAA,EAAA,EAAI,SAAA,EAAU,+BAA+B,QAAA,EAAA,SAAA,EAAU;AAAA,KAAA,EAE5D,CAAA;AAAA;AAGN;;;;"}
@@ -1,6 +1,8 @@
1
1
  import * as react from 'react';
2
+ import { IAttachment } from '../types.js';
2
3
  import { TDescriberBlock, TQuestionBlock } from '../StreamMessage.js';
3
4
 
5
+ declare function hasResolvedAttachmentPlaceholder(text: string, attachments: IAttachment[]): boolean;
4
6
  type TBlockType = 'text' | 'describer' | 'question';
5
7
  type TBaseStreamBlock<T extends TBlockType = TBlockType> = {
6
8
  blockType: T;
@@ -15,12 +17,13 @@ type TQuestionStreamBlock = TBaseStreamBlock<'question'> & {
15
17
  question: TQuestionBlock;
16
18
  };
17
19
  type TStreamBlock = TTextStreamBlock | TDescriberStreamBlock | TQuestionStreamBlock;
18
- declare const MessageBlocks: (({ streamParts, showThinkingSkeleton, }: {
20
+ declare const MessageBlocks: (({ attachments, streamParts, showThinkingSkeleton, }: {
21
+ attachments?: IAttachment[];
19
22
  streamParts: TStreamBlock[];
20
23
  showThinkingSkeleton?: boolean;
21
24
  }) => react.JSX.Element[]) & {
22
25
  displayName: string;
23
26
  };
24
27
 
25
- export { MessageBlocks, type TBaseStreamBlock, type TBlockType, type TDescriberStreamBlock, type TQuestionStreamBlock, type TStreamBlock, type TTextStreamBlock };
28
+ export { MessageBlocks, type TBaseStreamBlock, type TBlockType, type TDescriberStreamBlock, type TQuestionStreamBlock, type TStreamBlock, type TTextStreamBlock, hasResolvedAttachmentPlaceholder };
26
29
  //# sourceMappingURL=MessageBlocks.d.ts.map
@@ -6,9 +6,53 @@ import { Box } from '@apia/theme';
6
6
  import { observer } from 'mobx-react-lite';
7
7
  import { ThinkingBlocks } from './ThinkingBlocks.js';
8
8
  import { QuestionBlocks } from './QuestionBlocks.js';
9
+ import { FileAttachment } from './FileAttachment.js';
9
10
 
11
+ const ATTACHMENT_PLACEHOLDER_PATTERN = /\[\[attachment:([^\r\n]+?)\]\]/g;
12
+ function resolveAttachment(reference, attachments) {
13
+ const normalizedReference = reference.trim();
14
+ return attachments.find(
15
+ (attachment) => attachment.id === normalizedReference || attachment.name === normalizedReference
16
+ );
17
+ }
18
+ function hasResolvedAttachmentPlaceholder(text, attachments) {
19
+ ATTACHMENT_PLACEHOLDER_PATTERN.lastIndex = 0;
20
+ let match;
21
+ while (match = ATTACHMENT_PLACEHOLDER_PATTERN.exec(text)) {
22
+ if (resolveAttachment(match[1], attachments)) {
23
+ return true;
24
+ }
25
+ }
26
+ return false;
27
+ }
28
+ function renderTextWithAttachments(text, attachments, keyPrefix) {
29
+ return text.split(/(\[\[attachment:[^\r\n]+?\]\])/g).map((chunk, chunkIndex) => {
30
+ const match = chunk.match(/^\[\[attachment:([^\r\n]+?)\]\]$/);
31
+ if (!match) {
32
+ return /* @__PURE__ */ jsx(MDRenderer, { str: chunk }, `${keyPrefix}_${chunkIndex}`);
33
+ }
34
+ const attachment = resolveAttachment(match[1], attachments);
35
+ if (!attachment) {
36
+ return /* @__PURE__ */ jsx(
37
+ MDRenderer,
38
+ {
39
+ str: match[1].trim()
40
+ },
41
+ `${keyPrefix}_${chunkIndex}`
42
+ );
43
+ }
44
+ return /* @__PURE__ */ jsx(
45
+ FileAttachment,
46
+ {
47
+ attachment
48
+ },
49
+ `${keyPrefix}_${chunkIndex}`
50
+ );
51
+ });
52
+ }
10
53
  const MessageBlocks = observer(
11
54
  ({
55
+ attachments = [],
12
56
  streamParts,
13
57
  showThinkingSkeleton = true
14
58
  }) => {
@@ -69,11 +113,9 @@ const MessageBlocks = observer(
69
113
  `text_content_${groupIndex}_${blockIndex}_${chunkIndex}`
70
114
  );
71
115
  }
72
- return /* @__PURE__ */ jsx(
73
- MDRenderer,
74
- {
75
- str: chunk
76
- },
116
+ return renderTextWithAttachments(
117
+ chunk,
118
+ attachments,
77
119
  `text_content_${groupIndex}_${blockIndex}_${chunkIndex}`
78
120
  );
79
121
  });
@@ -83,5 +125,5 @@ const MessageBlocks = observer(
83
125
  }
84
126
  );
85
127
 
86
- export { MessageBlocks };
128
+ export { MessageBlocks, hasResolvedAttachmentPlaceholder };
87
129
  //# sourceMappingURL=MessageBlocks.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"MessageBlocks.js","sources":["../../../../src/chat/ChatController/components/MessageBlocks.tsx"],"sourcesContent":["import { decodeBase64ToUtf8 } from '@apia/util';\r\nimport { TComponentRender } from '../types';\r\nimport { MessageBlock } from './MessageBlock';\r\nimport { MDRenderer } from '@apia/components';\r\nimport { Box } from '@apia/theme';\r\nimport { TDescriberBlock, TQuestionBlock } from '../StreamMessage';\r\nimport { observer } from 'mobx-react-lite';\r\nimport { ThinkingBlocks } from './ThinkingBlocks';\r\nimport { QuestionBlocks } from './QuestionBlocks';\r\n\r\nexport type TBlockType = 'text' | 'describer' | 'question';\r\n\r\nexport type TBaseStreamBlock<T extends TBlockType = TBlockType> = {\r\n blockType: T;\r\n};\r\n\r\nexport type TTextStreamBlock = TBaseStreamBlock<'text'> & { str: string };\r\nexport type TDescriberStreamBlock = TBaseStreamBlock<'describer'> & {\r\n describer: TDescriberBlock;\r\n};\r\nexport type TQuestionStreamBlock = TBaseStreamBlock<'question'> & {\r\n question: TQuestionBlock;\r\n};\r\n\r\nexport type TStreamBlock =\r\n | TTextStreamBlock\r\n | TDescriberStreamBlock\r\n | TQuestionStreamBlock;\r\n\r\ntype TGroupedStreamBlock = {\r\n blockType: TBlockType;\r\n blocks: TStreamBlock[];\r\n};\r\n\r\nexport const MessageBlocks = observer(\r\n ({\r\n streamParts,\r\n showThinkingSkeleton = true,\r\n }: {\r\n streamParts: TStreamBlock[];\r\n showThinkingSkeleton?: boolean;\r\n }) => {\r\n const groupedParts = streamParts.reduce<TGroupedStreamBlock[]>(\r\n (groups, part) => {\r\n const currentGroup = groups[groups.length - 1];\r\n\r\n if (!currentGroup || currentGroup.blockType !== part.blockType) {\r\n groups.push({\r\n blockType: part.blockType,\r\n blocks: [part],\r\n });\r\n return groups;\r\n }\r\n\r\n currentGroup.blocks.push(part);\r\n return groups;\r\n },\r\n [],\r\n );\r\n\r\n const parts = groupedParts.map((group, groupIndex) => {\r\n const isLastGroup = !groupedParts[groupIndex + 1];\r\n\r\n if (group.blockType === 'describer') {\r\n return (\r\n <ThinkingBlocks\r\n key={`tool_calls_${groupIndex}`}\r\n blocks={group.blocks as TDescriberStreamBlock[]}\r\n groupIndex={groupIndex}\r\n isThinking={showThinkingSkeleton && isLastGroup}\r\n />\r\n );\r\n }\r\n\r\n if (group.blockType === 'question') {\r\n return (\r\n <QuestionBlocks\r\n key={`question_content_${groupIndex}`}\r\n blocks={group.blocks as TQuestionStreamBlock[]}\r\n />\r\n );\r\n }\r\n\r\n return (\r\n <Box className=\"text__content\" key={`text_content_${groupIndex}`}>\r\n {group.blocks.flatMap((block, blockIndex) => {\r\n const textBlock = block as TTextStreamBlock;\r\n\r\n return textBlock.str\r\n .split(/(!!\\[[^]+])/g)\r\n .map((chunk, chunkIndex) => {\r\n if (chunk.match(/^!!\\[[^]+]$/)) {\r\n const definition = JSON.parse(\r\n decodeBase64ToUtf8(\r\n (chunk.match(/^!!\\[([^]+)]$/) ?? [\r\n undefined,\r\n undefined,\r\n ])[1] ?? '',\r\n ),\r\n ) as TComponentRender;\r\n\r\n return (\r\n <MessageBlock\r\n key={`text_content_${groupIndex}_${blockIndex}_${chunkIndex}`}\r\n componentData={definition}\r\n ></MessageBlock>\r\n );\r\n }\r\n\r\n return (\r\n <MDRenderer\r\n str={chunk}\r\n key={`text_content_${groupIndex}_${blockIndex}_${chunkIndex}`}\r\n ></MDRenderer>\r\n );\r\n });\r\n })}\r\n </Box>\r\n );\r\n });\r\n\r\n return parts;\r\n },\r\n);\r\n"],"names":[],"mappings":";;;;;;;;;AAkCO,MAAM,aAAA,GAAgB,QAAA;AAAA,EAC3B,CAAC;AAAA,IACC,WAAA;AAAA,IACA,oBAAA,GAAuB;AAAA,GACzB,KAGM;AACJ,IAAA,MAAM,eAAe,WAAA,CAAY,MAAA;AAAA,MAC/B,CAAC,QAAQ,IAAA,KAAS;AAChB,QAAA,MAAM,YAAA,GAAe,MAAA,CAAO,MAAA,CAAO,MAAA,GAAS,CAAC,CAAA;AAE7C,QAAA,IAAI,CAAC,YAAA,IAAgB,YAAA,CAAa,SAAA,KAAc,KAAK,SAAA,EAAW;AAC9D,UAAA,MAAA,CAAO,IAAA,CAAK;AAAA,YACV,WAAW,IAAA,CAAK,SAAA;AAAA,YAChB,MAAA,EAAQ,CAAC,IAAI;AAAA,WACd,CAAA;AACD,UAAA,OAAO,MAAA;AAAA;AAGT,QAAA,YAAA,CAAa,MAAA,CAAO,KAAK,IAAI,CAAA;AAC7B,QAAA,OAAO,MAAA;AAAA,OACT;AAAA,MACA;AAAC,KACH;AAEA,IAAA,MAAM,KAAA,GAAQ,YAAA,CAAa,GAAA,CAAI,CAAC,OAAO,UAAA,KAAe;AACpD,MAAA,MAAM,WAAA,GAAc,CAAC,YAAA,CAAa,UAAA,GAAa,CAAC,CAAA;AAEhD,MAAA,IAAI,KAAA,CAAM,cAAc,WAAA,EAAa;AACnC,QAAA,uBACE,GAAA;AAAA,UAAC,cAAA;AAAA,UAAA;AAAA,YAEC,QAAQ,KAAA,CAAM,MAAA;AAAA,YACd,UAAA;AAAA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AAA,aAAA,EAAgB,UAAU,CAAA,CAiC9D,CAAA;AAAA,KAEH,CAAA;AAED,IAAA,OAAO,KAAA;AAAA;AAEX;;;;"}
1
+ {"version":3,"file":"MessageBlocks.js","sources":["../../../../src/chat/ChatController/components/MessageBlocks.tsx"],"sourcesContent":["import { decodeBase64ToUtf8 } from '@apia/util';\r\nimport type { IAttachment, TComponentRender } from '../types';\r\nimport { MessageBlock } from './MessageBlock';\r\nimport { MDRenderer } from '@apia/components';\r\nimport { Box } from '@apia/theme';\r\nimport { TDescriberBlock, TQuestionBlock } from '../StreamMessage';\r\nimport { observer } from 'mobx-react-lite';\r\nimport { ThinkingBlocks } from './ThinkingBlocks';\r\nimport { QuestionBlocks } from './QuestionBlocks';\r\nimport { FileAttachment } from './FileAttachment';\r\n\r\nconst ATTACHMENT_PLACEHOLDER_PATTERN =\r\n /\\[\\[attachment:([^\\r\\n]+?)\\]\\]/g;\r\n\r\nfunction resolveAttachment(\r\n reference: string,\r\n attachments: IAttachment[],\r\n) {\r\n const normalizedReference = reference.trim();\r\n return attachments.find(\r\n (attachment) =>\r\n attachment.id === normalizedReference ||\r\n attachment.name === normalizedReference,\r\n );\r\n}\r\n\r\nexport function hasResolvedAttachmentPlaceholder(\r\n text: string,\r\n attachments: IAttachment[],\r\n) {\r\n ATTACHMENT_PLACEHOLDER_PATTERN.lastIndex = 0;\r\n let match: RegExpExecArray | null;\r\n while ((match = ATTACHMENT_PLACEHOLDER_PATTERN.exec(text))) {\r\n if (resolveAttachment(match[1], attachments)) {\r\n return true;\r\n }\r\n }\r\n return false;\r\n}\r\n\r\nfunction renderTextWithAttachments(\r\n text: string,\r\n attachments: IAttachment[],\r\n keyPrefix: string,\r\n) {\r\n return text\r\n .split(/(\\[\\[attachment:[^\\r\\n]+?\\]\\])/g)\r\n .map((chunk, chunkIndex) => {\r\n const match = chunk.match(/^\\[\\[attachment:([^\\r\\n]+?)\\]\\]$/);\r\n if (!match) {\r\n return <MDRenderer str={chunk} key={`${keyPrefix}_${chunkIndex}`} />;\r\n }\r\n\r\n const attachment = resolveAttachment(match[1], attachments);\r\n if (!attachment) {\r\n return (\r\n <MDRenderer\r\n str={match[1].trim()}\r\n key={`${keyPrefix}_${chunkIndex}`}\r\n />\r\n );\r\n }\r\n\r\n return (\r\n <FileAttachment\r\n attachment={attachment}\r\n key={`${keyPrefix}_${chunkIndex}`}\r\n />\r\n );\r\n });\r\n}\r\n\r\nexport type TBlockType = 'text' | 'describer' | 'question';\r\n\r\nexport type TBaseStreamBlock<T extends TBlockType = TBlockType> = {\r\n blockType: T;\r\n};\r\n\r\nexport type TTextStreamBlock = TBaseStreamBlock<'text'> & { str: string };\r\nexport type TDescriberStreamBlock = TBaseStreamBlock<'describer'> & {\r\n describer: TDescriberBlock;\r\n};\r\nexport type TQuestionStreamBlock = TBaseStreamBlock<'question'> & {\r\n question: TQuestionBlock;\r\n};\r\n\r\nexport type TStreamBlock =\r\n | TTextStreamBlock\r\n | TDescriberStreamBlock\r\n | TQuestionStreamBlock;\r\n\r\ntype TGroupedStreamBlock = {\r\n blockType: TBlockType;\r\n blocks: TStreamBlock[];\r\n};\r\n\r\nexport const MessageBlocks = observer(\r\n ({\r\n attachments = [],\r\n streamParts,\r\n showThinkingSkeleton = true,\r\n }: {\r\n attachments?: IAttachment[];\r\n streamParts: TStreamBlock[];\r\n showThinkingSkeleton?: boolean;\r\n }) => {\r\n const groupedParts = streamParts.reduce<TGroupedStreamBlock[]>(\r\n (groups, part) => {\r\n const currentGroup = groups[groups.length - 1];\r\n\r\n if (!currentGroup || currentGroup.blockType !== part.blockType) {\r\n groups.push({\r\n blockType: part.blockType,\r\n blocks: [part],\r\n });\r\n return groups;\r\n }\r\n\r\n currentGroup.blocks.push(part);\r\n return groups;\r\n },\r\n [],\r\n );\r\n\r\n const parts = groupedParts.map((group, groupIndex) => {\r\n const isLastGroup = !groupedParts[groupIndex + 1];\r\n\r\n if (group.blockType === 'describer') {\r\n return (\r\n <ThinkingBlocks\r\n key={`tool_calls_${groupIndex}`}\r\n blocks={group.blocks as TDescriberStreamBlock[]}\r\n groupIndex={groupIndex}\r\n isThinking={showThinkingSkeleton && isLastGroup}\r\n />\r\n );\r\n }\r\n\r\n if (group.blockType === 'question') {\r\n return (\r\n <QuestionBlocks\r\n key={`question_content_${groupIndex}`}\r\n blocks={group.blocks as TQuestionStreamBlock[]}\r\n />\r\n );\r\n }\r\n\r\n return (\r\n <Box className=\"text__content\" key={`text_content_${groupIndex}`}>\r\n {group.blocks.flatMap((block, blockIndex) => {\r\n const textBlock = block as TTextStreamBlock;\r\n\r\n return textBlock.str\r\n .split(/(!!\\[[^]+])/g)\r\n .map((chunk, chunkIndex) => {\r\n if (chunk.match(/^!!\\[[^]+]$/)) {\r\n const definition = JSON.parse(\r\n decodeBase64ToUtf8(\r\n (chunk.match(/^!!\\[([^]+)]$/) ?? [\r\n undefined,\r\n undefined,\r\n ])[1] ?? '',\r\n ),\r\n ) as TComponentRender;\r\n\r\n return (\r\n <MessageBlock\r\n key={`text_content_${groupIndex}_${blockIndex}_${chunkIndex}`}\r\n componentData={definition}\r\n ></MessageBlock>\r\n );\r\n }\r\n\r\n return renderTextWithAttachments(\r\n chunk,\r\n attachments,\r\n `text_content_${groupIndex}_${blockIndex}_${chunkIndex}`,\r\n );\r\n });\r\n })}\r\n </Box>\r\n );\r\n });\r\n\r\n return parts;\r\n },\r\n);\r\n"],"names":[],"mappings":";;;;;;;;;;AAWA,MAAM,8BAAA,GACJ,iCAAA;AAEF,SAAS,iBAAA,CACP,WACA,WAAA,EACA;AACA,EAAA,MAAM,mBAAA,GAAsB,UAAU,IAAA,EAAK;AAC3C,EAAA,OAAO,WAAA,CAAY,IAAA;AAAA,IACjB,CAAC,UAAA,KACC,UAAA,CAAW,EAAA,KAAO,mBAAA,IAClB,WAAW,IAAA,KAAS;AAAA,GACxB;AACF;AAEO,SAAS,gCAAA,CACd,MACA,WAAA,EACA;AACA,EAAA,8BAAA,CAA+B,SAAA,GAAY,CAAA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,EAAO,UAAA,KAAe;AAC3C,QAAA,MAAM,SAAA,GAAY,KAAA;AAElB,QAAA,OAAO,SAAA,CAAU,IACd,KAAA,CAAM,cAAc,EACpB,GAAA,CAAI,CAAC,OAAO,UAAA,KAAe;AAC1B,UAAA,IAAI,KAAA,CAAM,KAAA,CAAM,aAAa,CAAA,EAAG;AAC9B,YAAA,MAAM,aAAa,IAAA,CAAK,KAAA;AAAA,cACtB,kBAAA;AAAA,gBAAA,CACG,KAAA,CAAM,KAAA,CAAM,eAAe,CAAA,IAAK;AAAA,kBAC/B,MAAA;AAAA,kBACA;AAAA,iBACF,EAAG,CAAC,CAAA,IAAK;AAAA;AACX,aACF;AAEA,YAAA,uBACE,GAAA;AAAA,cAAC,YAAA;AAAA,cAAA;AAAA,gBAEC,aAAA,EAAe;AAAA,eAAA;AAAA,cADV,CAAA,aAAA,EAAgB,UAAU,CAAA,CAAA,EAAI,UAAU,IAAI,UAAU,CAAA;AAAA,aAE5D;AAAA;AAIL,UAAA,OAAO,yBAAA;AAAA,YACL,KAAA;AAAA,YACA,WAAA;AAAA,YACA,CAAA,aAAA,EAAgB,UAAU,CAAA,CAAA,EAAI,UAAU,IAAI,UAAU,CAAA;AAAA,WACxD;AAAA,SACD,CAAA;AAAA,OACJ,CAAA,EAAA,EA/BiC,CAAA,aAAA,EAAgB,UAAU,CAAA,CAgC9D,CAAA;AAAA,KAEH,CAAA;AAED,IAAA,OAAO,KAAA;AAAA;AAEX;;;;"}
@@ -38,6 +38,9 @@ function getMessagePreview(message) {
38
38
  "archivos"
39
39
  )}`;
40
40
  }
41
+ if (message.annotations.length > 0) {
42
+ return message.annotations.map((annotation) => annotation.label).join(", ");
43
+ }
41
44
  return labelText("lblMessage", "Mensaje");
42
45
  }
43
46
  function stopPropagation(ev) {