@flamingo-stack/openframe-frontend-core 0.0.609 → 0.0.610-2115.5924.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.
- package/dist/chat-protocol/card-marker.d.ts +31 -0
- package/dist/chat-protocol/card-marker.d.ts.map +1 -0
- package/dist/chat-protocol/events.d.ts +21 -2
- package/dist/chat-protocol/events.d.ts.map +1 -1
- package/dist/chat-protocol/index.cjs +211 -9
- package/dist/chat-protocol/index.cjs.map +1 -1
- package/dist/chat-protocol/index.js +211 -9
- package/dist/chat-protocol/index.js.map +1 -1
- package/dist/chat-protocol/nats-decoder.d.ts.map +1 -1
- package/dist/chat-protocol/source-metadata.d.ts +56 -0
- package/dist/chat-protocol/source-metadata.d.ts.map +1 -0
- package/dist/{chunk-LA2W5L6K.js → chunk-25YIHMZ6.js} +2 -2
- package/dist/{chunk-6MLRFM3T.js → chunk-2QBXG74C.js} +5 -5
- package/dist/{chunk-3N4FXBVJ.cjs → chunk-2YMDDD5V.cjs} +3 -3
- package/dist/{chunk-3N4FXBVJ.cjs.map → chunk-2YMDDD5V.cjs.map} +1 -1
- package/dist/{chunk-JOCVQZGT.js → chunk-37CAZTLP.js} +5 -5
- package/dist/{chunk-D3DTC7SZ.cjs → chunk-C4KQ2XPK.cjs} +15 -15
- package/dist/{chunk-D3DTC7SZ.cjs.map → chunk-C4KQ2XPK.cjs.map} +1 -1
- package/dist/{chunk-CZTOSG7Q.cjs → chunk-EANAFFAD.cjs} +22 -22
- package/dist/{chunk-CZTOSG7Q.cjs.map → chunk-EANAFFAD.cjs.map} +1 -1
- package/dist/{chunk-BSMPCPOZ.js → chunk-F2C2U7CP.js} +590 -299
- package/dist/chunk-F2C2U7CP.js.map +1 -0
- package/dist/{chunk-O2M4T34Q.cjs → chunk-FASOANK7.cjs} +4 -4
- package/dist/{chunk-O2M4T34Q.cjs.map → chunk-FASOANK7.cjs.map} +1 -1
- package/dist/{chunk-JKM5FH63.js → chunk-FS3CTGIR.js} +3 -3
- package/dist/{chunk-45XXK2VU.cjs → chunk-GFAHCXIL.cjs} +10 -10
- package/dist/{chunk-45XXK2VU.cjs.map → chunk-GFAHCXIL.cjs.map} +1 -1
- package/dist/{chunk-SEW25Y2W.cjs → chunk-GNLV326I.cjs} +10 -10
- package/dist/{chunk-SEW25Y2W.cjs.map → chunk-GNLV326I.cjs.map} +1 -1
- package/dist/{chunk-DKNXAJMP.cjs → chunk-HKJ3WKGW.cjs} +159 -159
- package/dist/{chunk-DKNXAJMP.cjs.map → chunk-HKJ3WKGW.cjs.map} +1 -1
- package/dist/{chunk-HGVW7OHI.js → chunk-HQSCJYCF.js} +2 -2
- package/dist/{chunk-QX4ANESW.js → chunk-HZS4TV34.js} +6 -6
- package/dist/{chunk-B3UXNGVM.js → chunk-IBMHEOBV.js} +3 -3
- package/dist/{chunk-BUXGWBTF.js → chunk-MGXSJI23.js} +2 -2
- package/dist/{chunk-B7LYKBCD.js → chunk-O6PO5BHE.js} +5 -5
- package/dist/{chunk-C7CTEUU7.js → chunk-OIKZDXNW.js} +2 -2
- package/dist/{chunk-TJ3GFULR.js → chunk-OJZW3GE2.js} +3 -3
- package/dist/{chunk-L2JWC5VS.cjs → chunk-OVVKVIMH.cjs} +43 -43
- package/dist/{chunk-L2JWC5VS.cjs.map → chunk-OVVKVIMH.cjs.map} +1 -1
- package/dist/{chunk-VCYRJY7J.js → chunk-Q2CNCBMK.js} +4 -4
- package/dist/{chunk-NJUSDREV.cjs → chunk-Q3APHZK5.cjs} +22 -22
- package/dist/{chunk-NJUSDREV.cjs.map → chunk-Q3APHZK5.cjs.map} +1 -1
- package/dist/{chunk-TCEALWE3.cjs → chunk-QE2Z6WME.cjs} +33 -33
- package/dist/{chunk-TCEALWE3.cjs.map → chunk-QE2Z6WME.cjs.map} +1 -1
- package/dist/{chunk-OTYV6SDN.js → chunk-R2UDEMKK.js} +3 -3
- package/dist/{chunk-JR7I2AGL.js → chunk-ROOIHDLS.js} +3 -3
- package/dist/{chunk-3XNMDLGB.cjs → chunk-THCB7NHV.cjs} +9 -9
- package/dist/{chunk-3XNMDLGB.cjs.map → chunk-THCB7NHV.cjs.map} +1 -1
- package/dist/{chunk-GMOBT5NP.cjs → chunk-THCSSPXU.cjs} +70 -70
- package/dist/{chunk-GMOBT5NP.cjs.map → chunk-THCSSPXU.cjs.map} +1 -1
- package/dist/{chunk-FYG7QNAO.cjs → chunk-UFFVMZDL.cjs} +5 -5
- package/dist/{chunk-FYG7QNAO.cjs.map → chunk-UFFVMZDL.cjs.map} +1 -1
- package/dist/{chunk-MVBRMQGU.js → chunk-V6TINNLD.js} +2 -2
- package/dist/{chunk-ZRLFBLME.cjs → chunk-WJQOYSR3.cjs} +3 -3
- package/dist/{chunk-ZRLFBLME.cjs.map → chunk-WJQOYSR3.cjs.map} +1 -1
- package/dist/{chunk-XNDTYTVT.cjs → chunk-YXTAV2E3.cjs} +974 -683
- package/dist/chunk-YXTAV2E3.cjs.map +1 -0
- package/dist/{chunk-RZM6FPOT.cjs → chunk-ZDLCWNM6.cjs} +25 -25
- package/dist/{chunk-RZM6FPOT.cjs.map → chunk-ZDLCWNM6.cjs.map} +1 -1
- package/dist/components/case-studies/index.cjs +9 -9
- package/dist/components/case-studies/index.js +3 -3
- package/dist/components/chat/chat-message-enhanced.d.ts.map +1 -1
- package/dist/components/chat/chat-message-list.d.ts.map +1 -1
- package/dist/components/chat/embeddable-chat.d.ts.map +1 -1
- package/dist/components/chat/hooks/use-sse-chat-adapter.d.ts +5 -28
- package/dist/components/chat/hooks/use-sse-chat-adapter.d.ts.map +1 -1
- package/dist/components/chat/index.cjs +2 -2
- package/dist/components/chat/index.js +1 -1
- package/dist/components/chat/remark-card-links.d.ts +5 -4
- package/dist/components/chat/remark-card-links.d.ts.map +1 -1
- package/dist/components/chat/stream/chat-stream-reducer.d.ts.map +1 -1
- package/dist/components/chat/tool-execution-display.d.ts.map +1 -1
- package/dist/components/chat/types/component.types.d.ts +27 -0
- package/dist/components/chat/types/component.types.d.ts.map +1 -1
- package/dist/components/chat/types/message.types.d.ts +81 -1
- package/dist/components/chat/types/message.types.d.ts.map +1 -1
- package/dist/components/chat/types/unified-chat-state.types.d.ts +12 -3
- package/dist/components/chat/types/unified-chat-state.types.d.ts.map +1 -1
- package/dist/components/chat/utils/cited-sources.d.ts +24 -0
- package/dist/components/chat/utils/cited-sources.d.ts.map +1 -0
- package/dist/components/chat/utils/host-message.d.ts +41 -0
- package/dist/components/chat/utils/host-message.d.ts.map +1 -0
- package/dist/components/chat/utils/process-historical-messages.d.ts.map +1 -1
- package/dist/components/contact/index.cjs +4 -4
- package/dist/components/contact/index.js +3 -3
- package/dist/components/docs/index.cjs +7 -7
- package/dist/components/docs/index.js +6 -6
- package/dist/components/embeds/index.cjs +4 -4
- package/dist/components/embeds/index.js +3 -3
- package/dist/components/faq/index.cjs +4 -4
- package/dist/components/faq/index.js +3 -3
- package/dist/components/features/index.cjs +3 -3
- package/dist/components/features/index.js +2 -2
- package/dist/components/help-center-pages/index.cjs +23 -23
- package/dist/components/help-center-pages/index.js +14 -14
- package/dist/components/index.cjs +38 -38
- package/dist/components/index.js +13 -13
- package/dist/components/meeting-scheduler/index.cjs +24 -24
- package/dist/components/meeting-scheduler/index.js +2 -2
- package/dist/components/navigation/index.cjs +6 -6
- package/dist/components/navigation/index.js +5 -5
- package/dist/components/onboarding-guides/index.cjs +6 -6
- package/dist/components/onboarding-guides/index.js +5 -5
- package/dist/components/tickets/index.cjs +7 -7
- package/dist/components/tickets/index.js +6 -6
- package/dist/components/ui/index.cjs +3 -3
- package/dist/components/ui/index.js +2 -2
- package/dist/index.cjs +7 -7
- package/dist/index.js +6 -6
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.js.map +1 -1
- package/package.json +1 -1
- package/src/chat-protocol/__tests__/card-marker.test.ts +63 -0
- package/src/chat-protocol/__tests__/source-metadata.test.ts +243 -0
- package/src/chat-protocol/card-marker.ts +40 -0
- package/src/chat-protocol/events.ts +22 -1
- package/src/chat-protocol/nats-decoder.ts +12 -0
- package/src/chat-protocol/source-metadata.ts +336 -0
- package/src/components/chat/__tests__/tool-execution-display.test.tsx +70 -0
- package/src/components/chat/chat-message-enhanced.tsx +37 -12
- package/src/components/chat/chat-message-list.tsx +14 -1
- package/src/components/chat/embeddable-chat.tsx +47 -87
- package/src/components/chat/hooks/use-sse-chat-adapter.ts +5 -28
- package/src/components/chat/remark-card-links.ts +14 -12
- package/src/components/chat/stream/chat-stream-reducer.ts +48 -3
- package/src/components/chat/tool-execution-display.tsx +18 -4
- package/src/components/chat/types/component.types.ts +27 -0
- package/src/components/chat/types/message.types.ts +83 -0
- package/src/components/chat/types/unified-chat-state.types.ts +12 -3
- package/src/components/chat/utils/__tests__/cited-sources.test.ts +80 -0
- package/src/components/chat/utils/__tests__/host-message.test.ts +92 -0
- package/src/components/chat/utils/__tests__/process-historical-messages-sources.test.ts +138 -0
- package/src/components/chat/utils/cited-sources.ts +61 -0
- package/src/components/chat/utils/host-message.ts +87 -0
- package/src/components/chat/utils/process-historical-messages.ts +25 -0
- package/dist/chunk-BSMPCPOZ.js.map +0 -1
- package/dist/chunk-XNDTYTVT.cjs.map +0 -1
- /package/dist/{chunk-LA2W5L6K.js.map → chunk-25YIHMZ6.js.map} +0 -0
- /package/dist/{chunk-6MLRFM3T.js.map → chunk-2QBXG74C.js.map} +0 -0
- /package/dist/{chunk-JOCVQZGT.js.map → chunk-37CAZTLP.js.map} +0 -0
- /package/dist/{chunk-JKM5FH63.js.map → chunk-FS3CTGIR.js.map} +0 -0
- /package/dist/{chunk-HGVW7OHI.js.map → chunk-HQSCJYCF.js.map} +0 -0
- /package/dist/{chunk-QX4ANESW.js.map → chunk-HZS4TV34.js.map} +0 -0
- /package/dist/{chunk-B3UXNGVM.js.map → chunk-IBMHEOBV.js.map} +0 -0
- /package/dist/{chunk-BUXGWBTF.js.map → chunk-MGXSJI23.js.map} +0 -0
- /package/dist/{chunk-B7LYKBCD.js.map → chunk-O6PO5BHE.js.map} +0 -0
- /package/dist/{chunk-C7CTEUU7.js.map → chunk-OIKZDXNW.js.map} +0 -0
- /package/dist/{chunk-TJ3GFULR.js.map → chunk-OJZW3GE2.js.map} +0 -0
- /package/dist/{chunk-VCYRJY7J.js.map → chunk-Q2CNCBMK.js.map} +0 -0
- /package/dist/{chunk-OTYV6SDN.js.map → chunk-R2UDEMKK.js.map} +0 -0
- /package/dist/{chunk-JR7I2AGL.js.map → chunk-ROOIHDLS.js.map} +0 -0
- /package/dist/{chunk-MVBRMQGU.js.map → chunk-V6TINNLD.js.map} +0 -0
|
@@ -0,0 +1,336 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Source-metadata payload → `{ sources, refs }`.
|
|
3
|
+
*
|
|
4
|
+
* ONE decoder for two arrival paths: the live `GUIDE`/`SOURCES` NATS chunk and
|
|
5
|
+
* the persisted row of the same name in dialog history. That is the whole point
|
|
6
|
+
* of putting it here rather than in either consumer — a reloaded thread has to
|
|
7
|
+
* render identically to the live turn, and two parsers is exactly how that stops
|
|
8
|
+
* being true.
|
|
9
|
+
*
|
|
10
|
+
* Everything is validated, and anything that fails is DROPPED rather than
|
|
11
|
+
* repaired: this payload is assembled from a remote MCP server's tool output,
|
|
12
|
+
* so a malformed row means an upstream contract slip, and rendering a chip with
|
|
13
|
+
* a blank title or a link to a non-https URL is worse than rendering nothing.
|
|
14
|
+
*
|
|
15
|
+
* Server-safe: no React, no browser APIs beyond `URL`.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { ChatRef } from '../components/chat/chat-ref.types';
|
|
19
|
+
import type { ChatSource } from '../components/chat/types/message.types';
|
|
20
|
+
import { CARD_REFERENCE } from './card-marker';
|
|
21
|
+
import type { SourcesEvent } from './events';
|
|
22
|
+
import { isRecord } from './wire-narrow';
|
|
23
|
+
|
|
24
|
+
/** YouTube ids are exactly 11 chars of the URL-safe alphabet. */
|
|
25
|
+
const YOUTUBE_ID = /^[A-Za-z0-9_-]{11}$/;
|
|
26
|
+
|
|
27
|
+
/** Video URL fields carried through verbatim, once validated as https. */
|
|
28
|
+
const VIDEO_URL_KEYS = ['videoUrl', 'highlightVideoUrl', 'videoPoster', 'highlightVideoPoster'] as const;
|
|
29
|
+
|
|
30
|
+
function text(value: unknown): string | undefined {
|
|
31
|
+
if (typeof value !== 'string') return undefined;
|
|
32
|
+
const trimmed = value.trim();
|
|
33
|
+
return trimmed || undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* `null` survives, anything non-string becomes `undefined`.
|
|
38
|
+
*
|
|
39
|
+
* The distinction is load-bearing for `targetPlatform` and `path`, where the
|
|
40
|
+
* backend uses an explicit `null` to mean "this row has no destination" — which
|
|
41
|
+
* downstream navigation treats differently from "the field was not sent".
|
|
42
|
+
*/
|
|
43
|
+
function nullableText(value: unknown): string | null | undefined {
|
|
44
|
+
return value === null ? null : text(value);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** An https URL, or nothing. Plain http and non-URLs are dropped — these become
|
|
48
|
+
* hrefs and video sources in the panel. */
|
|
49
|
+
function httpsUrl(value: unknown): string | undefined {
|
|
50
|
+
const candidate = text(value);
|
|
51
|
+
if (!candidate) return undefined;
|
|
52
|
+
try {
|
|
53
|
+
return new URL(candidate).protocol === 'https:' ? candidate : undefined;
|
|
54
|
+
} catch {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A YouTube video id, from either a bare id or any of the watch/short/embed URL
|
|
61
|
+
* shapes. Returns the ID, never a URL — the player takes an id.
|
|
62
|
+
*/
|
|
63
|
+
export function youtubeVideoId(value: unknown): string | undefined {
|
|
64
|
+
const candidate = text(value);
|
|
65
|
+
if (!candidate) return undefined;
|
|
66
|
+
if (YOUTUBE_ID.test(candidate)) return candidate;
|
|
67
|
+
|
|
68
|
+
const url = httpsUrl(candidate);
|
|
69
|
+
if (!url) return undefined;
|
|
70
|
+
const parsed = new URL(url);
|
|
71
|
+
const hostname = parsed.hostname.toLowerCase();
|
|
72
|
+
const isYoutubeHost = hostname === 'youtu.be' || hostname === 'youtube.com' || hostname.endsWith('.youtube.com');
|
|
73
|
+
if (!isYoutubeHost) return undefined;
|
|
74
|
+
|
|
75
|
+
const fromPath =
|
|
76
|
+
hostname === 'youtu.be'
|
|
77
|
+
? parsed.pathname.split('/').filter(Boolean)[0]
|
|
78
|
+
: /^\/(?:embed|v|shorts)\/([A-Za-z0-9_-]{11})(?:\/|$)/.exec(parsed.pathname)?.[1];
|
|
79
|
+
const id = fromPath ?? parsed.searchParams.get('v') ?? undefined;
|
|
80
|
+
return id && YOUTUBE_ID.test(id) ? id : undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function sourceItems(value: unknown): NonNullable<ChatSource['items']> | undefined {
|
|
84
|
+
if (!Array.isArray(value)) return undefined;
|
|
85
|
+
const items = value.filter(isRecord).flatMap(item => {
|
|
86
|
+
const id = text(item.id);
|
|
87
|
+
const documentType = text(item.documentType);
|
|
88
|
+
const name = text(item.name);
|
|
89
|
+
// A grouped row without these three has nothing to render OR navigate with.
|
|
90
|
+
if (!id || !documentType || !name) return [];
|
|
91
|
+
|
|
92
|
+
const externalUrl = httpsUrl(item.externalUrl);
|
|
93
|
+
const targetPlatform = nullableText(item.targetPlatform);
|
|
94
|
+
const path = nullableText(item.path);
|
|
95
|
+
return [
|
|
96
|
+
{
|
|
97
|
+
id,
|
|
98
|
+
documentType,
|
|
99
|
+
name,
|
|
100
|
+
...(externalUrl ? { externalUrl } : {}),
|
|
101
|
+
...(targetPlatform !== undefined ? { targetPlatform } : {}),
|
|
102
|
+
...(path !== undefined ? { path } : {}),
|
|
103
|
+
},
|
|
104
|
+
];
|
|
105
|
+
});
|
|
106
|
+
return items.length > 0 ? items : undefined;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Cited documents, keyed by their citation number.
|
|
111
|
+
*
|
|
112
|
+
* `index` and `name` are required because they ARE the chip: the number the
|
|
113
|
+
* answer's `[1]` refers to, and the text on it. `path` is NOT required — it is
|
|
114
|
+
* one of several ways a chip resolves a destination (an `externalUrl` or a
|
|
115
|
+
* grouped item can carry it instead), and a source is still worth showing when
|
|
116
|
+
* it has none.
|
|
117
|
+
*
|
|
118
|
+
* A duplicate `index` keeps the first occurrence: two chips answering to the
|
|
119
|
+
* same `[1]` is not a state the strip can render meaningfully.
|
|
120
|
+
*/
|
|
121
|
+
function sources(value: unknown): ChatSource[] {
|
|
122
|
+
if (!Array.isArray(value)) return [];
|
|
123
|
+
const claimed = new Set<number>();
|
|
124
|
+
const decoded: ChatSource[] = [];
|
|
125
|
+
|
|
126
|
+
for (const candidate of value) {
|
|
127
|
+
if (!isRecord(candidate)) continue;
|
|
128
|
+
const index = candidate.index;
|
|
129
|
+
const name = text(candidate.name);
|
|
130
|
+
const isUsableIndex = typeof index === 'number' && Number.isInteger(index) && index > 0 && !claimed.has(index);
|
|
131
|
+
if (!isUsableIndex || !name) continue;
|
|
132
|
+
claimed.add(index);
|
|
133
|
+
|
|
134
|
+
const path = text(candidate.path);
|
|
135
|
+
const documentType = text(candidate.documentType);
|
|
136
|
+
const externalUrl = httpsUrl(candidate.externalUrl);
|
|
137
|
+
const targetPlatform = nullableText(candidate.targetPlatform);
|
|
138
|
+
const id = text(candidate.id);
|
|
139
|
+
const sourceRepo = text(candidate.sourceRepo);
|
|
140
|
+
const label = text(candidate.label);
|
|
141
|
+
const items = sourceItems(candidate.items);
|
|
142
|
+
|
|
143
|
+
decoded.push({
|
|
144
|
+
index,
|
|
145
|
+
name,
|
|
146
|
+
// Both are typed non-optional on `ChatSource` and predate this decoder;
|
|
147
|
+
// '' is the established "no value" for them across the SSE path.
|
|
148
|
+
path: path ?? '',
|
|
149
|
+
documentType: documentType ?? '',
|
|
150
|
+
...(externalUrl ? { externalUrl } : {}),
|
|
151
|
+
...(targetPlatform !== undefined ? { targetPlatform } : {}),
|
|
152
|
+
...(id ? { id } : {}),
|
|
153
|
+
...(sourceRepo ? { sourceRepo } : {}),
|
|
154
|
+
...(label ? { label } : {}),
|
|
155
|
+
...(items ? { items } : {}),
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return decoded;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Player metadata for a video ref, in the key vocabulary the card dispatcher
|
|
163
|
+
* already reads (`itemVideoMetadata` / `decodeVideoMarkerId` in
|
|
164
|
+
* `entity-cards/dispatch.tsx`).
|
|
165
|
+
*
|
|
166
|
+
* `youtubeUrl` holds a bare video ID, not a URL. The name is inaccurate and
|
|
167
|
+
* predates this decoder — but it is the established key on both the producing
|
|
168
|
+
* and the consuming side, and a more honest name here would simply be a field
|
|
169
|
+
* nothing reads. Renaming it is a separate change that has to move both ends at
|
|
170
|
+
* once.
|
|
171
|
+
*
|
|
172
|
+
* `videoUrl` and `youtubeUrl` stay mutually exclusive: they select different
|
|
173
|
+
* players (a `<video>`/HLS source vs an embed facade), so carrying both would
|
|
174
|
+
* make the renderer guess.
|
|
175
|
+
*/
|
|
176
|
+
function videoMetadata(value: unknown, url: string | null): Record<string, string> {
|
|
177
|
+
const metadata: Record<string, string> = {};
|
|
178
|
+
if (isRecord(value)) {
|
|
179
|
+
for (const key of VIDEO_URL_KEYS) {
|
|
180
|
+
const validated = httpsUrl(value[key]);
|
|
181
|
+
if (validated) metadata[key] = validated;
|
|
182
|
+
}
|
|
183
|
+
const declaredId = youtubeVideoId(value.youtubeUrl);
|
|
184
|
+
if (declaredId) metadata.youtubeUrl = declaredId;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// The top-level `url` is the authority when present: it is what the backend
|
|
188
|
+
// resolved for THIS video, while `metadata` is passthrough from the row.
|
|
189
|
+
if (url) {
|
|
190
|
+
const idFromUrl = youtubeVideoId(url);
|
|
191
|
+
if (idFromUrl) {
|
|
192
|
+
metadata.youtubeUrl = idFromUrl;
|
|
193
|
+
delete metadata.videoUrl;
|
|
194
|
+
} else {
|
|
195
|
+
metadata.videoUrl = url;
|
|
196
|
+
delete metadata.youtubeUrl;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return metadata;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Video references. `ref` must parse AND be of type `video` — a mistyped row
|
|
203
|
+
* would otherwise render through the entity-card path with no player. */
|
|
204
|
+
function videos(value: unknown): ChatRef[] {
|
|
205
|
+
if (!Array.isArray(value)) return [];
|
|
206
|
+
return value.filter(isRecord).flatMap(video => {
|
|
207
|
+
const match = typeof video.ref === 'string' ? CARD_REFERENCE.exec(video.ref) : null;
|
|
208
|
+
if (!match || match[1] !== 'video') return [];
|
|
209
|
+
|
|
210
|
+
const id = match[2];
|
|
211
|
+
const url = httpsUrl(video.url) ?? null;
|
|
212
|
+
const sourceRepo = text(video.sourceRepo);
|
|
213
|
+
const metadata = videoMetadata(video.metadata, url);
|
|
214
|
+
return [
|
|
215
|
+
{
|
|
216
|
+
type: 'video',
|
|
217
|
+
id,
|
|
218
|
+
title: text(video.title) ?? id,
|
|
219
|
+
url,
|
|
220
|
+
...(sourceRepo ? { sourceRepo } : {}),
|
|
221
|
+
...(Object.keys(metadata).length > 0 ? { metadata } : {}),
|
|
222
|
+
},
|
|
223
|
+
];
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Entity-card references, enriched from `sources` where the two describe the
|
|
229
|
+
* same row.
|
|
230
|
+
*
|
|
231
|
+
* A card carries only `type` + `id` on the wire; its human title and link live
|
|
232
|
+
* on the matching source. Without the match the card still renders — with its
|
|
233
|
+
* id as the title, which is ugly but honest, and better than dropping a card
|
|
234
|
+
* the answer text explicitly points at.
|
|
235
|
+
*
|
|
236
|
+
* `entityType`/`entityId` are cross-checked against the parsed `ref` because
|
|
237
|
+
* they are three statements of the same fact, and a disagreement means the row
|
|
238
|
+
* was assembled wrongly upstream.
|
|
239
|
+
*/
|
|
240
|
+
function cards(value: unknown, decodedSources: ChatSource[]): ChatRef[] {
|
|
241
|
+
if (!Array.isArray(value)) return [];
|
|
242
|
+
return value.filter(isRecord).flatMap(card => {
|
|
243
|
+
const match = typeof card.ref === 'string' ? CARD_REFERENCE.exec(card.ref) : null;
|
|
244
|
+
// `video` is handled by `videos()`; a card claiming that type would render
|
|
245
|
+
// the wrong component.
|
|
246
|
+
if (!match || match[1] === 'video') return [];
|
|
247
|
+
|
|
248
|
+
const type = text(card.entityType);
|
|
249
|
+
const id = text(card.entityId);
|
|
250
|
+
if (!type || !id || type !== match[1] || id !== match[2]) return [];
|
|
251
|
+
|
|
252
|
+
const source = decodedSources.find(candidate => candidate.id === id && candidate.documentType === type);
|
|
253
|
+
return [
|
|
254
|
+
{
|
|
255
|
+
type,
|
|
256
|
+
id,
|
|
257
|
+
title: source?.name ?? id,
|
|
258
|
+
url: source?.externalUrl ?? null,
|
|
259
|
+
...(source?.sourceRepo ? { sourceRepo: source.sourceRepo } : {}),
|
|
260
|
+
...(source?.targetPlatform !== undefined ? { targetPlatform: source.targetPlatform } : {}),
|
|
261
|
+
...(source?.path ? { metadata: { path: source.path } } : {}),
|
|
262
|
+
},
|
|
263
|
+
];
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** Last `type:id` wins nothing — the FIRST occurrence is kept, so a payload that
|
|
268
|
+
* lists a card twice cannot flip which copy the body expands to. */
|
|
269
|
+
function deduplicateRefs(refs: ChatRef[]): ChatRef[] {
|
|
270
|
+
const byIdentity = new Map<string, ChatRef>();
|
|
271
|
+
for (const ref of refs) {
|
|
272
|
+
const identity = `${ref.type}:${ref.id}`;
|
|
273
|
+
if (!byIdentity.has(identity)) byIdentity.set(identity, ref);
|
|
274
|
+
}
|
|
275
|
+
return [...byIdentity.values()];
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Decode one source-metadata payload into its event, or `null` when there is
|
|
280
|
+
* nothing usable in it.
|
|
281
|
+
*
|
|
282
|
+
* Returning `null` for an empty result matters: the reducer stamps this onto a
|
|
283
|
+
* message, and an event carrying two empty arrays would overwrite metadata that
|
|
284
|
+
* an earlier chunk of the same turn had already supplied.
|
|
285
|
+
*/
|
|
286
|
+
export function sourceMetadataEvent(payload: unknown): SourcesEvent | null {
|
|
287
|
+
if (!isRecord(payload)) return null;
|
|
288
|
+
|
|
289
|
+
const decodedSources = sources(payload.sources);
|
|
290
|
+
const refs = deduplicateRefs([...videos(payload.videos), ...cards(payload.cards, decodedSources)]);
|
|
291
|
+
if (decodedSources.length === 0 && refs.length === 0) return null;
|
|
292
|
+
|
|
293
|
+
return {
|
|
294
|
+
type: 'sources',
|
|
295
|
+
...(decodedSources.length > 0 ? { sources: decodedSources } : {}),
|
|
296
|
+
...(refs.length > 0 ? { refs } : {}),
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** What one assistant answer carries: its citations and its expandable refs. */
|
|
301
|
+
export interface SourceMetadata {
|
|
302
|
+
sources?: ChatSource[];
|
|
303
|
+
refs?: ChatRef[];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Fold one decoded chunk into an answer's accumulated metadata.
|
|
308
|
+
*
|
|
309
|
+
* A single turn can call several remote tools, each returning its own metadata,
|
|
310
|
+
* so this merges rather than replaces — and it is shared by the live reducer and
|
|
311
|
+
* the history replay precisely because those two must agree on what a reloaded
|
|
312
|
+
* answer shows.
|
|
313
|
+
*
|
|
314
|
+
* First writer wins per identity (`index` for a source, `type:id` for a ref),
|
|
315
|
+
* the same rule the decoder applies within one payload.
|
|
316
|
+
*
|
|
317
|
+
* KNOWN LIMIT: citation numbers are assigned per tool result, so two tool calls
|
|
318
|
+
* in one turn can both claim `[1]`. The later source is dropped rather than
|
|
319
|
+
* renumbered — by then the numbers are already written into the answer text,
|
|
320
|
+
* and renumbering would point `[1]` at a document the sentence is not about.
|
|
321
|
+
*/
|
|
322
|
+
export function mergeSourceMetadata(previous: SourceMetadata | null, event: SourcesEvent): SourceMetadata {
|
|
323
|
+
const byIndex = new Map<number, ChatSource>();
|
|
324
|
+
for (const source of [...(previous?.sources ?? []), ...(event.sources ?? [])]) {
|
|
325
|
+
if (!byIndex.has(source.index)) byIndex.set(source.index, source);
|
|
326
|
+
}
|
|
327
|
+
const byIdentity = new Map<string, ChatRef>();
|
|
328
|
+
for (const ref of [...(previous?.refs ?? []), ...(event.refs ?? [])]) {
|
|
329
|
+
const identity = `${ref.type}:${ref.id}`;
|
|
330
|
+
if (!byIdentity.has(identity)) byIdentity.set(identity, ref);
|
|
331
|
+
}
|
|
332
|
+
return {
|
|
333
|
+
...(byIndex.size > 0 ? { sources: [...byIndex.values()] } : {}),
|
|
334
|
+
...(byIdentity.size > 0 ? { refs: [...byIdentity.values()] } : {}),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ToolExecutionDisplay` — the running/ran-a-tool row.
|
|
3
|
+
*
|
|
4
|
+
* These pin WHO gets an expander. A remote (MCP) read-only tool publishes no
|
|
5
|
+
* parameters and no result on purpose, so a chevron on that row opens an empty
|
|
6
|
+
* box — the failure this file exists to prevent. Local tools, and anything that
|
|
7
|
+
* actually has detail, must keep theirs.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { render, screen } from '@testing-library/react';
|
|
11
|
+
import { describe, expect, it } from 'vitest';
|
|
12
|
+
|
|
13
|
+
import { ToolExecutionDisplay } from '../tool-execution-display';
|
|
14
|
+
import type { ToolExecutionData } from '../types/message.types';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* The `EXECUTING_TOOL` shape the backend sends for a remote read-only tool: a
|
|
18
|
+
* human title, and nothing else to show.
|
|
19
|
+
*
|
|
20
|
+
* `integratedToolType: ''` is what a remote tool looks like after decoding —
|
|
21
|
+
* the wire sends `null` and the decoder normalizes it (`strOr`). That empty
|
|
22
|
+
* value is the marker "not one of our integrations".
|
|
23
|
+
*/
|
|
24
|
+
const remoteRead: ToolExecutionData = {
|
|
25
|
+
type: 'EXECUTING_TOOL',
|
|
26
|
+
integratedToolType: '',
|
|
27
|
+
toolFunction: 'search_docs',
|
|
28
|
+
toolTitle: 'Searching OpenFrame resources',
|
|
29
|
+
toolExecutionRequestId: 'execution-123',
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/** The row is a `<button>` only when it can be expanded. */
|
|
33
|
+
const expander = () => screen.queryByRole('button');
|
|
34
|
+
|
|
35
|
+
describe('ToolExecutionDisplay', () => {
|
|
36
|
+
it('renders a remote read-only tool as a static row', () => {
|
|
37
|
+
render(<ToolExecutionDisplay message={remoteRead} assistantType="mingo" />);
|
|
38
|
+
|
|
39
|
+
expect(screen.getByText('Searching OpenFrame resources')).toBeTruthy();
|
|
40
|
+
expect(expander()).toBeNull();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('keeps the expander when a remote tool does have a result', () => {
|
|
44
|
+
render(
|
|
45
|
+
<ToolExecutionDisplay
|
|
46
|
+
message={{ ...remoteRead, type: 'EXECUTED_TOOL', success: true, result: '{"tickets":[]}' }}
|
|
47
|
+
assistantType="mingo"
|
|
48
|
+
/>,
|
|
49
|
+
);
|
|
50
|
+
expect(expander()).toBeTruthy();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('keeps the expander when a remote tool does have arguments', () => {
|
|
54
|
+
render(<ToolExecutionDisplay message={{ ...remoteRead, parameters: { query: 'agent' } }} assistantType="mingo" />);
|
|
55
|
+
expect(expander()).toBeTruthy();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('keeps the expander for a local tool, whose empty body still shows progress', () => {
|
|
59
|
+
// `integratedToolType` is what marks a tool as one of ours; those rows show
|
|
60
|
+
// a live "Result:" placeholder while executing, so the body is not empty.
|
|
61
|
+
render(<ToolExecutionDisplay message={{ ...remoteRead, integratedToolType: 'FLEET' }} assistantType="mingo" />);
|
|
62
|
+
expect(expander()).toBeTruthy();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('keeps the old behaviour when the backend sent no title', () => {
|
|
66
|
+
// Collapsing to a static row here would render a blank line.
|
|
67
|
+
render(<ToolExecutionDisplay message={{ ...remoteRead, toolTitle: undefined }} assistantType="mingo" />);
|
|
68
|
+
expect(expander()).toBeTruthy();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
|
|
3
3
|
import React, { forwardRef, memo, useEffect, useMemo, useRef } from 'react';
|
|
4
|
+
import { createCardMarkerScanner } from '../../chat-protocol/card-marker';
|
|
4
5
|
import { cn } from '../../utils/cn';
|
|
5
6
|
import { isToday } from '../../utils/date-utils';
|
|
6
7
|
import { formatDate, formatTime } from '../../utils/format-date';
|
|
@@ -36,13 +37,10 @@ import type { AskSegment, MessageSegment, MessageContent, ChatMessageEnhancedPro
|
|
|
36
37
|
* `scheduledScript`); id is the mention-token charset. */
|
|
37
38
|
const MENTION_MARKER_REGEX = /(^|[^\w@])@[a-zA-Z]+:([A-Za-z0-9_.+/=-]*[A-Za-z0-9_+/=])/g;
|
|
38
39
|
|
|
39
|
-
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
|
|
43
|
-
* both files must update.
|
|
44
|
-
*/
|
|
45
|
-
const CARD_MARKER_REGEX = /\[card:\/\/([a-zA-Z0-9_-]+):([a-zA-Z0-9_-]+)[\])]/g;
|
|
40
|
+
/** Scanner for `[card://type:id]` in answer text. Built from the shared
|
|
41
|
+
* grammar so the body renderer and the metadata decoder cannot disagree on
|
|
42
|
+
* what a marker is — see `chat-protocol/card-marker.ts`. */
|
|
43
|
+
const CARD_MARKER_REGEX = createCardMarkerScanner();
|
|
46
44
|
|
|
47
45
|
/** Timestamp label: today's messages show time only ("2:47 PM"),
|
|
48
46
|
* older messages prepend a locale-formatted date ("05/05/2026 2:47 PM"
|
|
@@ -103,6 +101,7 @@ const ChatMessageEnhanced = forwardRef<HTMLDivElement, ChatMessageEnhancedProps>
|
|
|
103
101
|
renderContextItem,
|
|
104
102
|
renderMention,
|
|
105
103
|
renderEntityCard,
|
|
104
|
+
refs,
|
|
106
105
|
onAskSelect,
|
|
107
106
|
NavLinkAnchor,
|
|
108
107
|
...props
|
|
@@ -180,9 +179,29 @@ const ChatMessageEnhanced = forwardRef<HTMLDivElement, ChatMessageEnhancedProps>
|
|
|
180
179
|
// (and its open menu) survives across chunks. Invalidated per key when the
|
|
181
180
|
// render fn identity changes.
|
|
182
181
|
const renderedCardNodeCache = useRef(
|
|
183
|
-
new Map<
|
|
182
|
+
new Map<
|
|
183
|
+
string,
|
|
184
|
+
{
|
|
185
|
+
render: ((ref: ChatRef) => React.ReactNode) | undefined;
|
|
186
|
+
/** The descriptor the node was built from. Part of the cache identity
|
|
187
|
+
* because metadata can arrive AFTER the marker was first rendered —
|
|
188
|
+
* a card mounted from the bare `{type, id}` descriptor has to be
|
|
189
|
+
* rebuilt once its real title and URL land. */
|
|
190
|
+
ref: ChatRef | undefined;
|
|
191
|
+
node: React.ReactNode;
|
|
192
|
+
}
|
|
193
|
+
>(),
|
|
184
194
|
);
|
|
185
195
|
|
|
196
|
+
/** The answer's own references, by `type:id`. What turns a bare marker into
|
|
197
|
+
* a card with a title and a link, without a per-entity lookup the host may
|
|
198
|
+
* not even have an API for. */
|
|
199
|
+
const refByKey = useMemo(() => {
|
|
200
|
+
const byKey = new Map<string, ChatRef>();
|
|
201
|
+
for (const described of refs ?? []) byKey.set(`${described.type}:${described.id}`, described);
|
|
202
|
+
return byKey;
|
|
203
|
+
}, [refs]);
|
|
204
|
+
|
|
186
205
|
/**
|
|
187
206
|
* Per-message rendering plan for `[card://type:id]` markers.
|
|
188
207
|
*
|
|
@@ -241,8 +260,11 @@ const ChatMessageEnhanced = forwardRef<HTMLDivElement, ChatMessageEnhancedProps>
|
|
|
241
260
|
// stops React from re-mounting the card (and closing its open
|
|
242
261
|
// menu / re-fetching) on every stream chunk. Also dedups the same
|
|
243
262
|
// key emitted twice within one message.
|
|
263
|
+
// The answer's metadata wins when it described this reference: it
|
|
264
|
+
// carries the title, URL and source the wire marker cannot.
|
|
265
|
+
const describedRef = refByKey.get(key);
|
|
244
266
|
let entry = cache.get(key);
|
|
245
|
-
if (!entry || entry.render !== render) {
|
|
267
|
+
if (!entry || entry.render !== render || entry.ref !== describedRef) {
|
|
246
268
|
// The marker is the ONLY data on the wire: cards hydrate by id
|
|
247
269
|
// from the host's per-object APIs, so a minimal {type, id}
|
|
248
270
|
// descriptor is all the renderer needs to mount the loader.
|
|
@@ -257,13 +279,13 @@ const ChatMessageEnhanced = forwardRef<HTMLDivElement, ChatMessageEnhancedProps>
|
|
|
257
279
|
// the id rather than `undefined`) and `url` to null (matches
|
|
258
280
|
// the no-link semantics fetch-mode cards rely on — they
|
|
259
281
|
// resolve their own URL after fetch).
|
|
260
|
-
const refForRender: ChatRef = {
|
|
282
|
+
const refForRender: ChatRef = describedRef ?? {
|
|
261
283
|
type: cardType,
|
|
262
284
|
id: cardId,
|
|
263
285
|
title: cardId,
|
|
264
286
|
url: null,
|
|
265
287
|
};
|
|
266
|
-
entry = { render, node: render(refForRender) };
|
|
288
|
+
entry = { render, ref: describedRef, node: render(refForRender) };
|
|
267
289
|
cache.set(key, entry);
|
|
268
290
|
}
|
|
269
291
|
const rendered = entry.node;
|
|
@@ -326,7 +348,7 @@ const ChatMessageEnhanced = forwardRef<HTMLDivElement, ChatMessageEnhancedProps>
|
|
|
326
348
|
}
|
|
327
349
|
});
|
|
328
350
|
return { inlineByKey, partsBySegment, usedKeys };
|
|
329
|
-
}, [hasMarkerSupport, renderEntityCard, segments]);
|
|
351
|
+
}, [hasMarkerSupport, renderEntityCard, refByKey, segments]);
|
|
330
352
|
|
|
331
353
|
// Drop cached nodes for markers no longer present so the cache can't grow
|
|
332
354
|
// unbounded as a long message's markers change. Deliberately an EFFECT,
|
|
@@ -716,6 +738,9 @@ const MemoizedChatMessageEnhanced = memo(ChatMessageEnhanced, (prevProps, nextPr
|
|
|
716
738
|
// equality holds across streaming chunks.
|
|
717
739
|
prevProps.renderMention === nextProps.renderMention &&
|
|
718
740
|
prevProps.renderEntityCard === nextProps.renderEntityCard &&
|
|
741
|
+
// Reference equality: the reducer sets this once per answer and the host
|
|
742
|
+
// forwards the same array instance, so it holds across streaming chunks.
|
|
743
|
+
prevProps.refs === nextProps.refs &&
|
|
719
744
|
// Same stability contract as the renderers above: hosts pass a `useCallback`
|
|
720
745
|
// (EmbeddableChat passes its memoized `handleSend`), so this holds across
|
|
721
746
|
// streaming chunks instead of re-rendering every ask card per chunk.
|
|
@@ -137,6 +137,7 @@ const ChatMessageList = forwardRef<HTMLDivElement, ChatMessageListProps>(
|
|
|
137
137
|
resolveContextIcon,
|
|
138
138
|
renderContextItem,
|
|
139
139
|
renderMention,
|
|
140
|
+
renderAfterMessage,
|
|
140
141
|
onAskSelect,
|
|
141
142
|
NavLinkAnchor,
|
|
142
143
|
...props
|
|
@@ -985,7 +986,8 @@ const ChatMessageList = forwardRef<HTMLDivElement, ChatMessageListProps>(
|
|
|
985
986
|
!hasNonEmptyContent(message.content) &&
|
|
986
987
|
(!Array.isArray(message.content) || message.content.every(s => s.type === 'text'));
|
|
987
988
|
if (isEmptyPendingTurn) return null;
|
|
988
|
-
|
|
989
|
+
const ownedContent = renderAfterMessage?.(message, index);
|
|
990
|
+
const row = (
|
|
989
991
|
<ChatMessageEnhanced
|
|
990
992
|
key={message.id}
|
|
991
993
|
ref={getRegisterMessageEl(message.id)}
|
|
@@ -1005,10 +1007,21 @@ const ChatMessageList = forwardRef<HTMLDivElement, ChatMessageListProps>(
|
|
|
1005
1007
|
renderContextItem={renderContextItem}
|
|
1006
1008
|
renderMention={renderMention}
|
|
1007
1009
|
renderEntityCard={renderEntityCard}
|
|
1010
|
+
refs={message.refs}
|
|
1008
1011
|
onAskSelect={index > lastUserMessageIndex ? onAskSelect : undefined}
|
|
1009
1012
|
NavLinkAnchor={NavLinkAnchor}
|
|
1010
1013
|
/>
|
|
1011
1014
|
);
|
|
1015
|
+
// Only wrap when there IS owned content: the bare row keeps
|
|
1016
|
+
// its own key and its place as a direct flex child, so a
|
|
1017
|
+
// thread without any stays byte-identical to before.
|
|
1018
|
+
if (!ownedContent) return row;
|
|
1019
|
+
return (
|
|
1020
|
+
<div key={message.id} className="flex w-full min-w-0 flex-col">
|
|
1021
|
+
{row}
|
|
1022
|
+
{ownedContent}
|
|
1023
|
+
</div>
|
|
1024
|
+
);
|
|
1012
1025
|
})}
|
|
1013
1026
|
</div>
|
|
1014
1027
|
</div>
|