Open History to see the last commit
1export type HoverOrigin = 'timeline' | 'preview'
2
3export interface HoverTarget {
4 messageIds: Array<string>
5 partIds: Array<string>
6 origin: HoverOrigin
7}
8
9export interface PreviewJsonItem {
10 label: string
11 value: unknown
12}
13
14export interface StructuredOutputJsonSource {
15 data?: unknown
16 partial?: unknown
17 raw?: unknown
18 reasoning?: unknown
19 errorMessage?: unknown
20}
21
22export function createHoverTarget(input: {
23 messageIds?: Array<string | null | undefined>
24 partIds?: Array<string | null | undefined>
25 origin?: HoverOrigin
26}): HoverTarget {
27 return {
28 messageIds: uniqueStrings(input.messageIds ?? []),
29 partIds: uniqueStrings(input.partIds ?? []),
30 origin: input.origin ?? 'timeline',
31 }
32}
33
34export function createHoverTargetFromDataAttributes(input: {
35 messageIds?: string | null
36 partIds?: string | null
37 origin?: HoverOrigin
38}): HoverTarget | null {
39 const target = createHoverTarget({
40 messageIds: splitDataIds(input.messageIds),
41 partIds: splitDataIds(input.partIds),
42 origin: input.origin,
43 })
44
45 if (target.messageIds.length === 0 && target.partIds.length === 0) {
46 return null
47 }
48
49 return target
50}
51
52export function isMessageHighlighted(
53 messageId: string,
54 target: HoverTarget | null,
55): boolean {
56 return target?.messageIds.includes(messageId) ?? false
57}
58
59export function isPartHighlighted(
60 messageId: string,
61 partId: string,
62 target: HoverTarget | null,
63): boolean {
64 if (!target) return false
65 return (
66 target.partIds.includes(partId) || target.messageIds.includes(messageId)
67 )
68}
69
70export function isMessageOrPartHighlighted(
71 messageId: string,
72 partIds: Array<string>,
73 target: HoverTarget | null,
74): boolean {
75 if (!target) return false
76 return (
77 target.messageIds.includes(messageId) ||
78 partIds.some((partId) => target.partIds.includes(partId))
79 )
80}
81
82export function getHoverDataAttributes(target: {
83 messageIds?: Array<string>
84 partIds?: Array<string>
85}): Record<string, string> {
86 return {
87 'data-ai-devtools-hover-message-ids': (target.messageIds ?? []).join(','),
88 'data-ai-devtools-hover-part-ids': (target.partIds ?? []).join(','),
89 }
90}
91
92export function hoverTargetMatchesElement(
93 target: HoverTarget,
94 elementTarget: {
95 messageIds?: string | null
96 partIds?: string | null
97 },
98): boolean {
99 const messageIds = splitDataIds(elementTarget.messageIds)
100 const partIds = splitDataIds(elementTarget.partIds)
101
102 return (
103 target.messageIds.some((id) => messageIds.includes(id)) ||
104 target.partIds.some((id) => partIds.includes(id))
105 )
106}
107
108export function toolCallPartId(toolCallId: string): string {
109 return `tool-call:${toolCallId}`
110}
111
112export function toolResultPartId(toolCallId: string): string {
113 return `tool-result:${toolCallId}`
114}
115
116export function structuredOutputPartId(messageId: string): string {
117 return `structured-output:${messageId}`
118}
119
120export function parseJsonishValue(value: unknown): unknown {
121 if (typeof value !== 'string') return value
122
123 const trimmed = value.trim()
124 if (!trimmed) return value
125 if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) return value
126
127 try {
128 return JSON.parse(trimmed)
129 } catch {
130 return value
131 }
132}
133
134export function structuredOutputJsonItems(
135 source: StructuredOutputJsonSource,
136): Array<PreviewJsonItem> {
137 const raw = typeof source.raw === 'string' ? source.raw : undefined
138 const items: Array<PreviewJsonItem> = []
139 const partialValue =
140 source.partial !== undefined
141 ? source.partial
142 : source.data !== undefined
143 ? source.data
144 : parseJsonishValue(raw)
145
146 if (raw !== undefined) {
147 items.push({
148 label: 'Raw',
149 value: raw,
150 })
151 }
152
153 if (partialValue !== undefined) {
154 items.push({
155 label: 'Partial',
156 value: partialValue,
157 })
158 }
159
160 if (source.reasoning !== undefined) {
161 items.push({
162 label: 'Reasoning',
163 value: source.reasoning,
164 })
165 }
166
167 if (source.errorMessage !== undefined) {
168 items.push({
169 label: 'Error',
170 value: source.errorMessage,
171 })
172 }
173
174 return items
175}
176
177function uniqueStrings(
178 values: Array<string | null | undefined>,
179): Array<string> {
180 const result: Array<string> = []
181 const seen = new Set<string>()
182
183 for (const value of values) {
184 if (!value || seen.has(value)) continue
185 seen.add(value)
186 result.push(value)
187 }
188
189 return result
190}
191
192function splitDataIds(value: string | null | undefined): Array<string> {
193 if (!value) return []
194 return value.split(',').filter(Boolean)
195}