forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatContext.tsx
More file actions
248 lines (213 loc) · 5.3 KB
/
Copy pathChatContext.tsx
File metadata and controls
248 lines (213 loc) · 5.3 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
247
248
import React, { useMemo, useState } from "react";
import * as DefaultIcons from "./Icons";
import { ThumbsDownIcon, ThumbsUpIcon } from "./Icons";
/**
* Icons for CopilotChat component.
*/
export interface CopilotChatIcons {
/**
* The icon to use for the open chat button.
* @default <OpenIcon />
*/
openIcon?: React.ReactNode;
/**
* The icon to use for the close chat button.
* @default <CloseIcon />
*/
closeIcon?: React.ReactNode;
/**
* The icon to use for the close chat button in the header.
* @default <HeaderCloseIcon />
*/
headerCloseIcon?: React.ReactNode;
/**
* The icon to use for the send button.
* @default <SendIcon />
*/
sendIcon?: React.ReactNode;
/**
* The icon to use for the activity indicator.
* @default <ActivityIcon />
*/
activityIcon?: React.ReactNode;
/**
* The icon to use for the spinner.
* @default <SpinnerIcon />
*/
spinnerIcon?: React.ReactNode;
/**
* The icon to use for the stop button.
* @default <StopIcon />
*/
stopIcon?: React.ReactNode;
/**
* The icon to use for the regenerate button.
* @default <RegenerateIcon />
*/
regenerateIcon?: React.ReactNode;
/**
* The icons to use for push to talk.
* @default <PushToTalkIcon />
*/
pushToTalkIcon?: React.ReactNode;
/**
* The icons to use for copy assistant response
* @default <CopyIcon />
*/
copyIcon?: React.ReactNode;
/**
* The icon to use for thumbs up/response approval.
* @default <ThumbsUpIcon />
*/
thumbsUpIcon?: React.ReactNode;
/**
* The icon to use for thumbs down/response rejection.
* @default <ThumbsDownIcon />
*/
thumbsDownIcon?: React.ReactNode;
/**
* The icon to use for the upload button.
* @default <UploadIcon />
*/
uploadIcon?: React.ReactNode;
}
/**
* Labels for CopilotChat component.
*/
export interface CopilotChatLabels {
/**
* The initial message(s) to display in the chat window.
*/
initial?: string | string[];
/**
* The title to display in the header.
* @default "CopilotKit"
*/
title?: string;
/**
* The placeholder to display in the input.
* @default "Type a message..."
*/
placeholder?: string;
/**
* The message to display when an error occurs.
* @default "❌ An error occurred. Please try again."
*/
error?: string;
/**
* The label to display on the stop button.
* @default "Stop generating"
*/
stopGenerating?: string;
/**
* The label to display on the regenerate button.
* @default "Regenerate response"
*/
regenerateResponse?: string;
/**
* The label for the copy button.
* @default "Copy to clipboard"
*/
copyToClipboard?: string;
/**
* The label for the thumbs up button.
* @default "Thumbs up"
*/
thumbsUp?: string;
/**
* The label for the thumbs down button.
* @default "Thumbs down"
*/
thumbsDown?: string;
/**
* The text to display when content is copied.
* @default "Copied!"
*/
copied?: string;
}
interface ChatContext {
labels: Required<CopilotChatLabels>;
icons: Required<CopilotChatIcons>;
open: boolean;
setOpen: (open: boolean) => void;
}
export const ChatContext = React.createContext<ChatContext | undefined>(
undefined,
);
export function useChatContext(): ChatContext {
const context = React.useContext(ChatContext);
if (context === undefined) {
throw new Error(
"Context not found. Did you forget to wrap your app in a <ChatContextProvider> component?",
);
}
return context;
}
interface ChatContextProps {
// temperature?: number;
// instructions?: string;
// maxFeedback?: number;
labels?: CopilotChatLabels;
icons?: CopilotChatIcons;
children?: React.ReactNode;
open: boolean;
setOpen: (open: boolean) => void;
}
export const ChatContextProvider = ({
// temperature,
// instructions,
// maxFeedback,
labels,
icons,
children,
open,
setOpen,
}: ChatContextProps) => {
const memoizedLabels = useMemo(
() => ({
initial: "",
title: "CopilotKit",
placeholder: "Type a message...",
error: "❌ An error occurred. Please try again.",
stopGenerating: "Stop generating",
regenerateResponse: "Regenerate response",
copyToClipboard: "Copy to clipboard",
thumbsUp: "Thumbs up",
thumbsDown: "Thumbs down",
copied: "Copied!",
...labels,
}),
[labels],
);
const memoizedIcons = useMemo(
() => ({
openIcon: DefaultIcons.OpenIcon,
closeIcon: DefaultIcons.CloseIcon,
headerCloseIcon: DefaultIcons.HeaderCloseIcon,
sendIcon: DefaultIcons.SendIcon,
activityIcon: DefaultIcons.ActivityIcon,
spinnerIcon: DefaultIcons.SpinnerIcon,
stopIcon: DefaultIcons.StopIcon,
regenerateIcon: DefaultIcons.RegenerateIcon,
pushToTalkIcon: DefaultIcons.MicrophoneIcon,
copyIcon: DefaultIcons.CopyIcon,
thumbsUpIcon: DefaultIcons.ThumbsUpIcon,
thumbsDownIcon: DefaultIcons.ThumbsDownIcon,
uploadIcon: DefaultIcons.UploadIcon,
...icons,
}),
[icons],
);
const context = useMemo(
() => ({
labels: memoizedLabels,
icons: memoizedIcons,
open,
setOpen,
}),
[memoizedLabels, memoizedIcons, open, setOpen],
);
return (
<ChatContext.Provider value={context}>{children}</ChatContext.Provider>
);
};