@majapisoftwares/ui 6.7.0 → 6.8.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/AGENTS.md +2 -1
- package/THIRD_PARTY_NOTICES.md +2 -0
- package/docs/STANDARDS.md +15 -4
- package/package.json +6 -3
- package/src/components/ui/code-block.tsx +246 -0
- package/src/components/ui/marker.tsx +72 -0
- package/src/components/ui/message-scroller.tsx +2 -2
- package/src/hooks/use-highlighted-code.ts +77 -0
- package/src/lib/highlighter.tsx +124 -0
- package/src/lib/i18n.tsx +81 -0
- package/src/lib/message-scroller/components.tsx +446 -0
- package/src/lib/message-scroller/geometry.ts +387 -0
- package/src/lib/message-scroller/index.ts +31 -0
- package/src/lib/message-scroller/stores.ts +97 -0
- package/src/lib/message-scroller/types.ts +240 -0
- package/src/lib/message-scroller/use-message-scroller-commands.ts +337 -0
- package/src/lib/message-scroller/use-message-scroller-controller.ts +874 -0
- package/src/lib/message-scroller/use-message-scroller-refs.ts +200 -0
- package/src/lib/message-scroller/use-render.ts +208 -0
- package/src/lib/message-scroller/utils.ts +11 -0
- package/src/lib/utils.ts +16 -0
- package/src/patterns/chat-checkpoint.tsx +49 -0
- package/src/patterns/chat-citation.tsx +57 -0
- package/src/patterns/chat-context.tsx +82 -0
- package/src/patterns/chat-trace.tsx +739 -9
- package/src/patterns/chat.tsx +119 -4
- package/src/patterns/file-tree.tsx +122 -0
- package/src/patterns/model-selector.tsx +153 -0
- package/src/patterns/prompt-input.tsx +18 -0
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import * as React from "react"
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
areScrollStatesEqual,
|
|
5
|
+
createMessageScrollerStore,
|
|
6
|
+
createMessageScrollerVisibilityStore,
|
|
7
|
+
} from "./stores"
|
|
8
|
+
import { EMPTY_MESSAGE_SCROLLER_SCROLLABLE } from "./types"
|
|
9
|
+
import type {
|
|
10
|
+
MessageScrollerDefaultScrollPosition,
|
|
11
|
+
MessageScrollerMode,
|
|
12
|
+
MessageScrollerScrollable,
|
|
13
|
+
MessageScrollerScrollOptions,
|
|
14
|
+
MessageScrollerStore,
|
|
15
|
+
MessageScrollerVisibilityStore,
|
|
16
|
+
} from "./types"
|
|
17
|
+
|
|
18
|
+
// Shared mutable ref bag for one MessageScroller, closed over by both the
|
|
19
|
+
// controller and the commands so writes are visible across them without prop
|
|
20
|
+
// threading. stateStore and visibilityStore fan out via useSyncExternalStore.
|
|
21
|
+
type MessageScrollerRefs = {
|
|
22
|
+
autoScrollRef: React.RefObject<boolean>
|
|
23
|
+
autoscrollingRef: React.RefObject<boolean>
|
|
24
|
+
autoscrollingTimeoutRef: React.RefObject<number | null>
|
|
25
|
+
streamingTurnRef: React.RefObject<HTMLElement | null>
|
|
26
|
+
contentRef: React.RefObject<HTMLDivElement | null>
|
|
27
|
+
defaultScrollPositionAppliedRef: React.RefObject<boolean>
|
|
28
|
+
firstItemRef: React.RefObject<HTMLElement | null>
|
|
29
|
+
itemCountRef: React.RefObject<number>
|
|
30
|
+
lastScrollTopRef: React.RefObject<number>
|
|
31
|
+
messageElementsRef: React.RefObject<Map<string, HTMLElement>>
|
|
32
|
+
modeRef: React.RefObject<MessageScrollerMode>
|
|
33
|
+
pendingScrollFrameRef: React.RefObject<number | null>
|
|
34
|
+
pendingScrollToMessageRef: React.RefObject<{
|
|
35
|
+
messageId: string
|
|
36
|
+
options?: MessageScrollerScrollOptions
|
|
37
|
+
} | null>
|
|
38
|
+
prependRestoreRef: React.RefObject<{
|
|
39
|
+
element: HTMLElement
|
|
40
|
+
viewportTop: number
|
|
41
|
+
} | null>
|
|
42
|
+
preserveScrollOnPrependRef: React.RefObject<boolean>
|
|
43
|
+
pendingDefaultScrollStore: MessageScrollerStore<boolean>
|
|
44
|
+
rootRef: React.RefObject<HTMLDivElement | null>
|
|
45
|
+
scrollEdgeThresholdRef: React.RefObject<number>
|
|
46
|
+
scrollMarginRef: React.RefObject<number>
|
|
47
|
+
scrollPreviousItemPeekRef: React.RefObject<number>
|
|
48
|
+
spacerGapRef: React.RefObject<number>
|
|
49
|
+
spacerHeightRef: React.RefObject<number>
|
|
50
|
+
spacerRef: React.RefObject<HTMLDivElement | null>
|
|
51
|
+
stateFrameRef: React.RefObject<number | null>
|
|
52
|
+
stateStore: MessageScrollerStore<MessageScrollerScrollable>
|
|
53
|
+
viewportRef: React.RefObject<HTMLDivElement | null>
|
|
54
|
+
visibilityFrameRef: React.RefObject<number | null>
|
|
55
|
+
visibilityObserverRef: React.RefObject<IntersectionObserver | null>
|
|
56
|
+
visibilityStore: MessageScrollerVisibilityStore
|
|
57
|
+
visibleMessageIdsRef: React.RefObject<Set<string>>
|
|
58
|
+
handledScrollAnchorsRef: React.RefObject<WeakSet<HTMLElement>>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Builds the per-instance ref bag: the two external stores constructed once, and
|
|
62
|
+
// the latest prop values mirrored onto refs each render so callbacks stay stable.
|
|
63
|
+
function useMessageScrollerRefs({
|
|
64
|
+
autoScroll,
|
|
65
|
+
defaultScrollPosition,
|
|
66
|
+
scrollEdgeThreshold,
|
|
67
|
+
scrollMargin,
|
|
68
|
+
scrollPreviousItemPeek,
|
|
69
|
+
}: {
|
|
70
|
+
autoScroll: boolean
|
|
71
|
+
defaultScrollPosition: MessageScrollerDefaultScrollPosition
|
|
72
|
+
scrollEdgeThreshold: number
|
|
73
|
+
scrollMargin: number
|
|
74
|
+
scrollPreviousItemPeek: number
|
|
75
|
+
}): MessageScrollerRefs {
|
|
76
|
+
const autoScrollRef = React.useRef(autoScroll)
|
|
77
|
+
const autoscrollingRef = React.useRef(false)
|
|
78
|
+
const contentRef = React.useRef<HTMLDivElement | null>(null)
|
|
79
|
+
const defaultScrollPositionAppliedRef = React.useRef(false)
|
|
80
|
+
const scrollEdgeThresholdRef = React.useRef(scrollEdgeThreshold)
|
|
81
|
+
const itemCountRef = React.useRef(0)
|
|
82
|
+
const firstItemRef = React.useRef<HTMLElement | null>(null)
|
|
83
|
+
// The scrollTop seen by the previous state commit, so follow-release can tell
|
|
84
|
+
// a reader scrolling up from content growing past the live edge.
|
|
85
|
+
const lastScrollTopRef = React.useRef(0)
|
|
86
|
+
const modeRef = React.useRef<MessageScrollerMode>(
|
|
87
|
+
autoScroll ? "following-bottom" : "free-scrolling"
|
|
88
|
+
)
|
|
89
|
+
const messageElementsRef = React.useRef(new Map<string, HTMLElement>())
|
|
90
|
+
const pendingScrollToMessageRef = React.useRef<{
|
|
91
|
+
messageId: string
|
|
92
|
+
options?: MessageScrollerScrollOptions
|
|
93
|
+
} | null>(null)
|
|
94
|
+
// The row to hold steady on the next prepend: the first visible row, or a jump
|
|
95
|
+
// target seeded by scrollToElement. restorePrependedAnchor reads only this.
|
|
96
|
+
const prependRestoreRef = React.useRef<{
|
|
97
|
+
element: HTMLElement
|
|
98
|
+
viewportTop: number
|
|
99
|
+
} | null>(null)
|
|
100
|
+
// The turn held at the reading line so a reply streaming in below it can re-pin
|
|
101
|
+
// it instead of letting scrollTop clamp it loose.
|
|
102
|
+
const streamingTurnRef = React.useRef<HTMLElement | null>(null)
|
|
103
|
+
const scrollPreviousItemPeekRef = React.useRef(scrollPreviousItemPeek)
|
|
104
|
+
const preserveScrollOnPrependRef = React.useRef(true)
|
|
105
|
+
const rootRef = React.useRef<HTMLDivElement | null>(null)
|
|
106
|
+
const scrollMarginRef = React.useRef(scrollMargin)
|
|
107
|
+
const pendingScrollFrameRef = React.useRef<number | null>(null)
|
|
108
|
+
const spacerGapRef = React.useRef(0)
|
|
109
|
+
const spacerHeightRef = React.useRef(0)
|
|
110
|
+
const spacerRef = React.useRef<HTMLDivElement | null>(null)
|
|
111
|
+
const stateFrameRef = React.useRef<number | null>(null)
|
|
112
|
+
const pendingDefaultScrollStoreRef =
|
|
113
|
+
React.useRef<MessageScrollerStore<boolean> | null>(null)
|
|
114
|
+
const stateStoreRef =
|
|
115
|
+
React.useRef<MessageScrollerStore<MessageScrollerScrollable> | null>(null)
|
|
116
|
+
const autoscrollingTimeoutRef = React.useRef<number | null>(null)
|
|
117
|
+
const viewportRef = React.useRef<HTMLDivElement | null>(null)
|
|
118
|
+
const visibilityFrameRef = React.useRef<number | null>(null)
|
|
119
|
+
const visibilityObserverRef = React.useRef<IntersectionObserver | null>(null)
|
|
120
|
+
const visibilityStoreRef =
|
|
121
|
+
React.useRef<MessageScrollerVisibilityStore | null>(null)
|
|
122
|
+
const visibleMessageIdsRef = React.useRef(new Set<string>())
|
|
123
|
+
const handledScrollAnchorsRef = React.useRef(new WeakSet<HTMLElement>())
|
|
124
|
+
|
|
125
|
+
if (pendingDefaultScrollStoreRef.current === null) {
|
|
126
|
+
pendingDefaultScrollStoreRef.current = createMessageScrollerStore(
|
|
127
|
+
defaultScrollPosition === "end" ||
|
|
128
|
+
defaultScrollPosition === "last-anchor",
|
|
129
|
+
(current, next) => current === next
|
|
130
|
+
)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (stateStoreRef.current === null) {
|
|
134
|
+
stateStoreRef.current = createMessageScrollerStore(
|
|
135
|
+
EMPTY_MESSAGE_SCROLLER_SCROLLABLE,
|
|
136
|
+
areScrollStatesEqual
|
|
137
|
+
)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (visibilityStoreRef.current === null) {
|
|
141
|
+
visibilityStoreRef.current = createMessageScrollerVisibilityStore()
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Track the latest prop values on every render so callbacks read fresh values
|
|
145
|
+
// without being recreated (the useLatest pattern).
|
|
146
|
+
autoScrollRef.current = autoScroll
|
|
147
|
+
scrollEdgeThresholdRef.current = scrollEdgeThreshold
|
|
148
|
+
scrollMarginRef.current = scrollMargin
|
|
149
|
+
scrollPreviousItemPeekRef.current = scrollPreviousItemPeek
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
autoScrollRef,
|
|
153
|
+
autoscrollingRef,
|
|
154
|
+
autoscrollingTimeoutRef,
|
|
155
|
+
streamingTurnRef,
|
|
156
|
+
contentRef,
|
|
157
|
+
defaultScrollPositionAppliedRef,
|
|
158
|
+
firstItemRef,
|
|
159
|
+
itemCountRef,
|
|
160
|
+
lastScrollTopRef,
|
|
161
|
+
messageElementsRef,
|
|
162
|
+
modeRef,
|
|
163
|
+
pendingScrollFrameRef,
|
|
164
|
+
pendingScrollToMessageRef,
|
|
165
|
+
prependRestoreRef,
|
|
166
|
+
preserveScrollOnPrependRef,
|
|
167
|
+
pendingDefaultScrollStore: pendingDefaultScrollStoreRef.current,
|
|
168
|
+
rootRef,
|
|
169
|
+
scrollEdgeThresholdRef,
|
|
170
|
+
scrollMarginRef,
|
|
171
|
+
scrollPreviousItemPeekRef,
|
|
172
|
+
spacerGapRef,
|
|
173
|
+
spacerHeightRef,
|
|
174
|
+
spacerRef,
|
|
175
|
+
stateFrameRef,
|
|
176
|
+
stateStore: stateStoreRef.current,
|
|
177
|
+
viewportRef,
|
|
178
|
+
visibilityFrameRef,
|
|
179
|
+
visibilityObserverRef,
|
|
180
|
+
visibilityStore: visibilityStoreRef.current,
|
|
181
|
+
visibleMessageIdsRef,
|
|
182
|
+
handledScrollAnchorsRef,
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function clearPendingDefaultScroll(refs: MessageScrollerRefs) {
|
|
187
|
+
refs.pendingDefaultScrollStore.setSnapshot(false)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function markDefaultScrollPositionApplied(refs: MessageScrollerRefs) {
|
|
191
|
+
refs.defaultScrollPositionAppliedRef.current = true
|
|
192
|
+
clearPendingDefaultScroll(refs)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export {
|
|
196
|
+
clearPendingDefaultScroll,
|
|
197
|
+
markDefaultScrollPositionApplied,
|
|
198
|
+
useMessageScrollerRefs,
|
|
199
|
+
}
|
|
200
|
+
export type { MessageScrollerRefs }
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// A poor man's version of base-ui useRender.
|
|
2
|
+
// see https://base-ui.com/react/utils/use-render.
|
|
3
|
+
// TODO: Replace if base-ui useRender is published as a package.
|
|
4
|
+
import * as React from "react"
|
|
5
|
+
|
|
6
|
+
type RenderState = Record<string, unknown>
|
|
7
|
+
|
|
8
|
+
type RenderFunction<TState extends RenderState> = (
|
|
9
|
+
props: Record<string, unknown>,
|
|
10
|
+
state: TState
|
|
11
|
+
) => React.ReactElement | null
|
|
12
|
+
|
|
13
|
+
type RenderProp<TState extends RenderState> =
|
|
14
|
+
| React.ReactElement
|
|
15
|
+
| RenderFunction<TState>
|
|
16
|
+
|
|
17
|
+
type StateAttributesMapping<TState extends RenderState> = Partial<{
|
|
18
|
+
[K in keyof TState]: (
|
|
19
|
+
value: TState[K]
|
|
20
|
+
) => Record<string, string | undefined> | null | undefined
|
|
21
|
+
}>
|
|
22
|
+
|
|
23
|
+
type UseRenderComponentProps<
|
|
24
|
+
TElement extends React.ElementType,
|
|
25
|
+
TState extends RenderState = RenderState,
|
|
26
|
+
> = React.ComponentPropsWithRef<TElement> & {
|
|
27
|
+
render?: RenderProp<TState>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type UseRenderOptions<
|
|
31
|
+
TElement extends React.ElementType,
|
|
32
|
+
TState extends RenderState,
|
|
33
|
+
> = {
|
|
34
|
+
defaultTagName: TElement
|
|
35
|
+
props?: React.ComponentPropsWithRef<TElement>
|
|
36
|
+
render?: RenderProp<TState>
|
|
37
|
+
state?: TState
|
|
38
|
+
stateAttributesMapping?: StateAttributesMapping<TState>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function useRender<
|
|
42
|
+
TElement extends React.ElementType,
|
|
43
|
+
TState extends RenderState = RenderState,
|
|
44
|
+
>({
|
|
45
|
+
defaultTagName,
|
|
46
|
+
props,
|
|
47
|
+
render,
|
|
48
|
+
state = {} as TState,
|
|
49
|
+
stateAttributesMapping,
|
|
50
|
+
}: UseRenderOptions<TElement, TState>) {
|
|
51
|
+
const elementProps = mergeProps<TElement>(
|
|
52
|
+
getStateAttributes(state, stateAttributesMapping),
|
|
53
|
+
props
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
if (!render) {
|
|
57
|
+
return React.createElement(defaultTagName, elementProps)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (typeof render === "function") {
|
|
61
|
+
return render(elementProps, state)
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!React.isValidElement(render)) {
|
|
65
|
+
return null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const renderProps = render.props as Record<string, unknown>
|
|
69
|
+
const propsWithRenderProps = mergeProps<TElement>(elementProps, renderProps)
|
|
70
|
+
const propsWithRef = {
|
|
71
|
+
...propsWithRenderProps,
|
|
72
|
+
ref: composeRefs(
|
|
73
|
+
elementProps.ref as React.Ref<unknown> | undefined,
|
|
74
|
+
renderProps.ref as React.Ref<unknown> | undefined
|
|
75
|
+
),
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return React.cloneElement(
|
|
79
|
+
render,
|
|
80
|
+
propsWithRef as React.Attributes & Record<string, unknown>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function mergeProps<TElement extends React.ElementType>(
|
|
85
|
+
...sources: Array<
|
|
86
|
+
React.ComponentPropsWithRef<TElement> | Record<string, unknown> | undefined
|
|
87
|
+
>
|
|
88
|
+
) {
|
|
89
|
+
const result: Record<string, unknown> = {}
|
|
90
|
+
|
|
91
|
+
for (const source of sources) {
|
|
92
|
+
if (!source) {
|
|
93
|
+
continue
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const sourceProps = source as Record<string, unknown>
|
|
97
|
+
|
|
98
|
+
for (const key of Object.keys(sourceProps)) {
|
|
99
|
+
const value = sourceProps[key]
|
|
100
|
+
|
|
101
|
+
if (value === undefined) {
|
|
102
|
+
continue
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const current = result[key]
|
|
106
|
+
|
|
107
|
+
if (key === "className") {
|
|
108
|
+
result[key] = [current, value].filter(Boolean).join(" ")
|
|
109
|
+
} else if (key === "style") {
|
|
110
|
+
result[key] = {
|
|
111
|
+
...(current as React.CSSProperties | undefined),
|
|
112
|
+
...(value as React.CSSProperties | undefined),
|
|
113
|
+
}
|
|
114
|
+
} else if (key === "ref") {
|
|
115
|
+
result[key] = composeRefs(
|
|
116
|
+
current as React.Ref<unknown> | undefined,
|
|
117
|
+
value as React.Ref<unknown> | undefined
|
|
118
|
+
)
|
|
119
|
+
} else if (
|
|
120
|
+
isEventHandler(key) &&
|
|
121
|
+
typeof current === "function" &&
|
|
122
|
+
typeof value === "function"
|
|
123
|
+
) {
|
|
124
|
+
result[key] = composeEventHandlers(
|
|
125
|
+
value as EventHandler,
|
|
126
|
+
current as EventHandler
|
|
127
|
+
)
|
|
128
|
+
} else {
|
|
129
|
+
result[key] = value
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return result as React.ComponentPropsWithRef<TElement>
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function getStateAttributes<TState extends RenderState>(
|
|
138
|
+
state: TState,
|
|
139
|
+
mapping?: StateAttributesMapping<TState>
|
|
140
|
+
) {
|
|
141
|
+
const props: Record<string, unknown> = {}
|
|
142
|
+
|
|
143
|
+
for (const key of Object.keys(state) as Array<keyof TState>) {
|
|
144
|
+
const value = state[key]
|
|
145
|
+
const attributes = mapping?.[key]?.(value)
|
|
146
|
+
|
|
147
|
+
if (attributes) {
|
|
148
|
+
Object.assign(props, attributes)
|
|
149
|
+
continue
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (key === "slot") {
|
|
153
|
+
props["data-slot"] = value
|
|
154
|
+
continue
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const attribute = `data-${String(key).replace(
|
|
158
|
+
/[A-Z]/g,
|
|
159
|
+
(letter) => `-${letter.toLowerCase()}`
|
|
160
|
+
)}`
|
|
161
|
+
|
|
162
|
+
if (typeof value === "boolean") {
|
|
163
|
+
props[attribute] = value ? "" : undefined
|
|
164
|
+
} else if (value !== null && value !== undefined) {
|
|
165
|
+
props[attribute] = String(value)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return props
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
type EventHandler = (event: React.SyntheticEvent) => void
|
|
173
|
+
|
|
174
|
+
function composeEventHandlers(theirs: EventHandler, ours: EventHandler) {
|
|
175
|
+
return function handleEvent(event: React.SyntheticEvent) {
|
|
176
|
+
theirs(event)
|
|
177
|
+
|
|
178
|
+
if (!event.defaultPrevented) {
|
|
179
|
+
ours(event)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function isEventHandler(key: string) {
|
|
185
|
+
return /^on[A-Z]/.test(key)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function composeRefs<T>(
|
|
189
|
+
...refs: Array<React.Ref<T> | undefined>
|
|
190
|
+
): React.RefCallback<T> | undefined {
|
|
191
|
+
const validRefs = refs.filter(Boolean)
|
|
192
|
+
|
|
193
|
+
if (validRefs.length === 0) {
|
|
194
|
+
return undefined
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return (value) => {
|
|
198
|
+
for (const ref of validRefs) {
|
|
199
|
+
if (typeof ref === "function") {
|
|
200
|
+
ref(value)
|
|
201
|
+
} else if (ref) {
|
|
202
|
+
ref.current = value
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export { composeRefs, mergeProps, useRender, type UseRenderComponentProps }
|
package/src/lib/utils.ts
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
1
|
export { cn } from "cn";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Markdown as one line of plain text, for a `ChatQuote` of an agent's reply:
|
|
5
|
+
* no code blocks, emphasis, links, list markers or table pipes.
|
|
6
|
+
*/
|
|
7
|
+
export function quoteText(markdown: string) {
|
|
8
|
+
return markdown
|
|
9
|
+
.replace(/```[\s\S]*?(```|$)/g, " ")
|
|
10
|
+
.replace(/!?\[([^\]]*)\]\([^)]*\)/g, "$1")
|
|
11
|
+
.replace(/^\s{0,3}(#{1,6}|>|[-*+]|\d+[.)])\s+/gm, "")
|
|
12
|
+
.replace(/^[\s|:-]*-{3,}[\s|:-]*$/gm, " ")
|
|
13
|
+
.replace(/\|/g, " ")
|
|
14
|
+
.replace(/\*\*|__|~~|`|\*/g, "")
|
|
15
|
+
.replace(/\s+/g, " ")
|
|
16
|
+
.trim();
|
|
17
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
import { useUiMessages } from "../lib/i18n";
|
|
5
|
+
import { Button } from "../components/ui/button";
|
|
6
|
+
import { Marker, MarkerContent } from "../components/ui/marker";
|
|
7
|
+
|
|
8
|
+
export interface CheckpointProps {
|
|
9
|
+
/** What the turn above it changed, e.g. "Button renamed". */
|
|
10
|
+
label: ReactNode;
|
|
11
|
+
/** Keeps that turn and drops every later message. Leave it out on the last turn. */
|
|
12
|
+
onRestore?: () => void;
|
|
13
|
+
/** Accessible name of the restore button, e.g. "Restore to Button renamed". */
|
|
14
|
+
restoreLabel?: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* A point the conversation can go back to (shadcn AI Elements Checkpoint): a
|
|
19
|
+
* separator after a turn, with Restore.
|
|
20
|
+
*/
|
|
21
|
+
export function Checkpoint({
|
|
22
|
+
label,
|
|
23
|
+
onRestore,
|
|
24
|
+
restoreLabel,
|
|
25
|
+
}: CheckpointProps) {
|
|
26
|
+
const messages = useUiMessages();
|
|
27
|
+
return (
|
|
28
|
+
<Marker variant="separator">
|
|
29
|
+
<MarkerContent className="inline-flex items-center gap-2">
|
|
30
|
+
{label}
|
|
31
|
+
{onRestore ? (
|
|
32
|
+
<Button
|
|
33
|
+
size="xs"
|
|
34
|
+
variant="outline"
|
|
35
|
+
aria-label={
|
|
36
|
+
restoreLabel ??
|
|
37
|
+
(typeof label === "string"
|
|
38
|
+
? messages.restoreTo(label)
|
|
39
|
+
: undefined)
|
|
40
|
+
}
|
|
41
|
+
onClick={onRestore}
|
|
42
|
+
>
|
|
43
|
+
{messages.restore}
|
|
44
|
+
</Button>
|
|
45
|
+
) : null}
|
|
46
|
+
</MarkerContent>
|
|
47
|
+
</Marker>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useUiMessages } from "../lib/i18n";
|
|
4
|
+
import { Button } from "../components/ui/button";
|
|
5
|
+
import {
|
|
6
|
+
HoverCard,
|
|
7
|
+
HoverCardContent,
|
|
8
|
+
HoverCardTrigger,
|
|
9
|
+
} from "../components/ui/hover-card";
|
|
10
|
+
import { hostnameOf } from "./chat-trace";
|
|
11
|
+
|
|
12
|
+
export interface InlineCitationProps {
|
|
13
|
+
url: string;
|
|
14
|
+
/** Falls back to the URL's hostname. */
|
|
15
|
+
title?: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A source inside a sentence (shadcn AI Elements Inline Citation): its
|
|
21
|
+
* hostname, which shows the title, description and link on hover. For the
|
|
22
|
+
* list of sources after an answer, use Sources.
|
|
23
|
+
*/
|
|
24
|
+
export function InlineCitation({
|
|
25
|
+
url,
|
|
26
|
+
title,
|
|
27
|
+
description,
|
|
28
|
+
}: InlineCitationProps) {
|
|
29
|
+
const messages = useUiMessages();
|
|
30
|
+
const host = hostnameOf(url);
|
|
31
|
+
return (
|
|
32
|
+
<HoverCard>
|
|
33
|
+
<HoverCardTrigger
|
|
34
|
+
delay={10}
|
|
35
|
+
closeDelay={100}
|
|
36
|
+
render={<Button variant="secondary" size="xs" />}
|
|
37
|
+
>
|
|
38
|
+
{host}
|
|
39
|
+
</HoverCardTrigger>
|
|
40
|
+
<HoverCardContent className="flex w-64 flex-col gap-1">
|
|
41
|
+
<div className="font-medium">{title ?? host}</div>
|
|
42
|
+
{description ? (
|
|
43
|
+
<p className="text-muted-foreground">{description}</p>
|
|
44
|
+
) : null}
|
|
45
|
+
<a
|
|
46
|
+
href={url}
|
|
47
|
+
target="_blank"
|
|
48
|
+
rel="noreferrer"
|
|
49
|
+
className="text-xs text-muted-foreground underline-offset-4 hover:underline"
|
|
50
|
+
>
|
|
51
|
+
{host}
|
|
52
|
+
<span className="sr-only"> {messages.opensInNewTab}</span>
|
|
53
|
+
</a>
|
|
54
|
+
</HoverCardContent>
|
|
55
|
+
</HoverCard>
|
|
56
|
+
);
|
|
57
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useUiMessages } from "../lib/i18n";
|
|
4
|
+
import { Button } from "../components/ui/button";
|
|
5
|
+
import {
|
|
6
|
+
HoverCard,
|
|
7
|
+
HoverCardContent,
|
|
8
|
+
HoverCardTrigger,
|
|
9
|
+
} from "../components/ui/hover-card";
|
|
10
|
+
import { Progress } from "../components/ui/progress";
|
|
11
|
+
|
|
12
|
+
export interface ContextUsageProps {
|
|
13
|
+
/** The model's context window, in tokens (the model's, not sent by the provider). */
|
|
14
|
+
limit: number;
|
|
15
|
+
input?: number;
|
|
16
|
+
output?: number;
|
|
17
|
+
reasoning?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** 1234 → "1.2k". */
|
|
21
|
+
export function formatTokens(value: number) {
|
|
22
|
+
if (value < 1000) return String(value);
|
|
23
|
+
return `${(value / 1000).toFixed(1).replace(/\.0$/, "")}k`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* How much of the context window a conversation uses (shadcn AI Elements
|
|
28
|
+
* Context): a percent button, for the prompt input's `actions`, that shows
|
|
29
|
+
* the counts on hover.
|
|
30
|
+
*/
|
|
31
|
+
export function ContextUsage({
|
|
32
|
+
limit,
|
|
33
|
+
input = 0,
|
|
34
|
+
output = 0,
|
|
35
|
+
reasoning = 0,
|
|
36
|
+
}: ContextUsageProps) {
|
|
37
|
+
const messages = useUiMessages();
|
|
38
|
+
const used = input + output + reasoning;
|
|
39
|
+
const percent =
|
|
40
|
+
limit > 0 ? Math.min(100, Math.round((used / limit) * 100)) : 0;
|
|
41
|
+
const rows = [
|
|
42
|
+
{ label: messages.inputTokens, value: input },
|
|
43
|
+
{ label: messages.outputTokens, value: output },
|
|
44
|
+
{ label: messages.reasoningTokens, value: reasoning },
|
|
45
|
+
].filter((row) => row.value > 0);
|
|
46
|
+
return (
|
|
47
|
+
<HoverCard>
|
|
48
|
+
<HoverCardTrigger
|
|
49
|
+
delay={10}
|
|
50
|
+
closeDelay={100}
|
|
51
|
+
render={
|
|
52
|
+
<Button
|
|
53
|
+
variant="outline"
|
|
54
|
+
size="sm"
|
|
55
|
+
className="h-11 rounded-full md:h-8"
|
|
56
|
+
/>
|
|
57
|
+
}
|
|
58
|
+
>
|
|
59
|
+
{messages.contextPercent(percent)}
|
|
60
|
+
</HoverCardTrigger>
|
|
61
|
+
<HoverCardContent className="flex w-64 flex-col gap-3">
|
|
62
|
+
<div className="flex items-center justify-between gap-3 text-sm">
|
|
63
|
+
<span className="font-medium">{messages.context}</span>
|
|
64
|
+
<span className="text-muted-foreground tabular-nums">
|
|
65
|
+
{formatTokens(used)} / {formatTokens(limit)}
|
|
66
|
+
</span>
|
|
67
|
+
</div>
|
|
68
|
+
<Progress value={percent} aria-label={messages.contextUsed} />
|
|
69
|
+
{rows.length ? (
|
|
70
|
+
<div className="flex flex-col gap-1 text-sm">
|
|
71
|
+
{rows.map((row) => (
|
|
72
|
+
<div key={row.label} className="flex justify-between gap-3">
|
|
73
|
+
<span className="text-muted-foreground">{row.label}</span>
|
|
74
|
+
<span className="tabular-nums">{formatTokens(row.value)}</span>
|
|
75
|
+
</div>
|
|
76
|
+
))}
|
|
77
|
+
</div>
|
|
78
|
+
) : null}
|
|
79
|
+
</HoverCardContent>
|
|
80
|
+
</HoverCard>
|
|
81
|
+
);
|
|
82
|
+
}
|