forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbranch.tsx
More file actions
246 lines (211 loc) · 5.94 KB
/
Copy pathbranch.tsx
File metadata and controls
246 lines (211 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
'use client'
import type { UIMessage } from 'ai'
import { ChevronLeftIcon, ChevronRightIcon } from 'lucide-react'
import {
type ComponentProps,
createContext,
type HTMLAttributes,
type ReactElement,
useContext,
useEffect,
useMemo,
useState,
} from 'react'
import { useTranslation } from 'react-i18next'
import { Button } from '@/components/ui/button'
import { cn } from '@/lib/utils'
type BranchContextType = {
currentBranch: number
totalBranches: number
goToPrevious: () => void
goToNext: () => void
branches: ReactElement[]
setBranches: (branches: ReactElement[]) => void
}
const BranchContext = createContext<BranchContextType | null>(null)
const useBranch = () => {
const context = useContext(BranchContext)
if (!context) {
throw new Error('Branch components must be used within Branch')
}
return context
}
export type BranchProps = HTMLAttributes<HTMLDivElement> & {
defaultBranch?: number
onBranchChange?: (branchIndex: number) => void
}
export const Branch = ({
defaultBranch = 0,
onBranchChange,
className,
...props
}: BranchProps) => {
const [currentBranch, setCurrentBranch] = useState(defaultBranch)
const [branches, setBranches] = useState<ReactElement[]>([])
const handleBranchChange = (newBranch: number) => {
setCurrentBranch(newBranch)
onBranchChange?.(newBranch)
}
const goToPrevious = () => {
const newBranch =
currentBranch > 0 ? currentBranch - 1 : branches.length - 1
handleBranchChange(newBranch)
}
const goToNext = () => {
const newBranch =
currentBranch < branches.length - 1 ? currentBranch + 1 : 0
handleBranchChange(newBranch)
}
const contextValue: BranchContextType = {
currentBranch,
totalBranches: branches.length,
goToPrevious,
goToNext,
branches,
setBranches,
}
return (
<BranchContext.Provider value={contextValue}>
<div
className={cn('grid w-full gap-2 [&>div]:pb-0', className)}
{...props}
/>
</BranchContext.Provider>
)
}
export type BranchMessagesProps = HTMLAttributes<HTMLDivElement>
export const BranchMessages = ({ children, ...props }: BranchMessagesProps) => {
const { currentBranch, setBranches, branches } = useBranch()
const childrenArray = useMemo(
() => (Array.isArray(children) ? children : [children]),
[children]
)
// Use useEffect to update branches when they change
useEffect(() => {
if (branches.length !== childrenArray.length) {
setBranches(childrenArray)
}
}, [childrenArray, branches, setBranches])
return childrenArray.map((branch, index) => (
<div
className={cn(
'grid gap-2 overflow-hidden [&>div]:pb-0',
index === currentBranch ? 'block' : 'hidden'
)}
key={branch.key}
{...props}
>
{branch}
</div>
))
}
export type BranchSelectorProps = HTMLAttributes<HTMLDivElement> & {
from: UIMessage['role']
}
export const BranchSelector = ({
className,
from,
...props
}: BranchSelectorProps) => {
const { totalBranches } = useBranch()
// Don't render if there's only one branch
if (totalBranches <= 1) {
return null
}
return (
<div
className={cn(
'flex items-center gap-2 self-end px-10',
from === 'assistant' ? 'justify-start' : 'justify-end',
className
)}
{...props}
/>
)
}
export type BranchPreviousProps = ComponentProps<typeof Button>
export const BranchPrevious = ({
className,
children,
...props
}: BranchPreviousProps) => {
const { t } = useTranslation()
const { goToPrevious, totalBranches } = useBranch()
return (
<Button
aria-label={t('Previous branch')}
className={cn(
'text-muted-foreground size-7 shrink-0 transition-colors',
'hover:bg-accent hover:text-foreground',
'disabled:pointer-events-none disabled:opacity-50',
className
)}
disabled={totalBranches <= 1}
onClick={goToPrevious}
size='icon'
type='button'
variant='ghost'
{...props}
>
{children ?? <ChevronLeftIcon size={14} />}
</Button>
)
}
export type BranchNextProps = ComponentProps<typeof Button>
export const BranchNext = ({
className,
children,
...props
}: BranchNextProps) => {
const { t } = useTranslation()
const { goToNext, totalBranches } = useBranch()
return (
<Button
aria-label={t('Next branch')}
className={cn(
'text-muted-foreground size-7 shrink-0 transition-colors',
'hover:bg-accent hover:text-foreground',
'disabled:pointer-events-none disabled:opacity-50',
className
)}
disabled={totalBranches <= 1}
onClick={goToNext}
size='icon'
type='button'
variant='ghost'
{...props}
>
{children ?? <ChevronRightIcon size={14} />}
</Button>
)
}
export type BranchPageProps = HTMLAttributes<HTMLSpanElement>
export const BranchPage = ({ className, ...props }: BranchPageProps) => {
const { t } = useTranslation()
const { currentBranch, totalBranches } = useBranch()
return (
<span
className={cn(
'text-muted-foreground text-xs font-medium tabular-nums',
className
)}
{...props}
>
{currentBranch + 1} {t('of')} {totalBranches}
</span>
)
}