forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopilotChatConfigurationProvider.tsx
More file actions
196 lines (177 loc) · 6.8 KB
/
Copy pathCopilotChatConfigurationProvider.tsx
File metadata and controls
196 lines (177 loc) · 6.8 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
import React, {
createContext,
useCallback,
useContext,
ReactNode,
useEffect,
useMemo,
useRef,
useState,
} from "react";
import { DEFAULT_AGENT_ID, randomUUID } from "@copilotkit/shared";
import { useShallowStableRef } from "../lib/slots";
// Default labels
export const CopilotChatDefaultLabels = {
chatInputPlaceholder: "Type a message...",
chatInputToolbarStartTranscribeButtonLabel: "Transcribe",
chatInputToolbarCancelTranscribeButtonLabel: "Cancel",
chatInputToolbarFinishTranscribeButtonLabel: "Finish",
chatInputToolbarAddButtonLabel: "Add attachments",
chatInputToolbarToolsButtonLabel: "Tools",
assistantMessageToolbarCopyCodeLabel: "Copy",
assistantMessageToolbarCopyCodeCopiedLabel: "Copied",
assistantMessageToolbarCopyMessageLabel: "Copy",
assistantMessageToolbarThumbsUpLabel: "Good response",
assistantMessageToolbarThumbsDownLabel: "Bad response",
assistantMessageToolbarReadAloudLabel: "Read aloud",
assistantMessageToolbarRegenerateLabel: "Regenerate",
userMessageToolbarCopyMessageLabel: "Copy",
userMessageToolbarEditMessageLabel: "Edit",
chatDisclaimerText:
"AI can make mistakes. Please verify important information.",
chatToggleOpenLabel: "Open chat",
chatToggleCloseLabel: "Close chat",
modalHeaderTitle: "CopilotKit Chat",
welcomeMessageText: "How can I help you today?",
};
export type CopilotChatLabels = typeof CopilotChatDefaultLabels;
// Define the full configuration interface
export interface CopilotChatConfigurationValue {
labels: CopilotChatLabels;
agentId: string;
threadId: string;
isModalOpen: boolean;
setModalOpen: (open: boolean) => void;
// True when the current threadId was chosen by the caller rather than
// silently minted inside the provider chain. Consumers that only make
// sense against a real backend thread (e.g. /connect, suppressing the
// welcome screen on switch) gate on this instead of `!!threadId`.
hasExplicitThreadId: boolean;
}
// Create the configuration context
const CopilotChatConfiguration =
createContext<CopilotChatConfigurationValue | null>(null);
// Provider props interface
export interface CopilotChatConfigurationProviderProps {
children: ReactNode;
labels?: Partial<CopilotChatLabels>;
agentId?: string;
threadId?: string;
// Lets internal wrappers (e.g. the v1 CopilotKit bridge, which pipes a
// ThreadsProvider-minted UUID through as `threadId`) declare that the
// threadId they are supplying is NOT a caller choice. When omitted, the
// provider infers explicitness from whether the `threadId` prop itself
// was supplied.
hasExplicitThreadId?: boolean;
isModalDefaultOpen?: boolean;
}
// Provider component
export const CopilotChatConfigurationProvider: React.FC<
CopilotChatConfigurationProviderProps
> = ({
children,
labels,
agentId,
threadId,
hasExplicitThreadId,
isModalDefaultOpen,
}) => {
const parentConfig = useContext(CopilotChatConfiguration);
// Stabilize labels references so that inline objects (new reference on every
// parent render) don't invalidate mergedLabels and churn the context value.
// parentConfig?.labels is already stabilized by the parent provider's own
// useShallowStableRef, so we only need to stabilize the local labels prop.
const stableLabels = useShallowStableRef(labels);
const mergedLabels: CopilotChatLabels = useMemo(
() => ({
...CopilotChatDefaultLabels,
...parentConfig?.labels,
...stableLabels,
}),
[stableLabels, parentConfig?.labels],
);
const resolvedAgentId = agentId ?? parentConfig?.agentId ?? DEFAULT_AGENT_ID;
const resolvedThreadId = useMemo(() => {
if (threadId) {
return threadId;
}
if (parentConfig?.threadId) {
return parentConfig.threadId;
}
return randomUUID();
}, [threadId, parentConfig?.threadId]);
// If a caller passed `hasExplicitThreadId`, trust it verbatim (lets the v1
// bridge mark an auto-minted UUID as non-explicit). Otherwise infer: a
// threadId supplied as a prop here is by definition a caller choice.
const ownHasExplicitThreadId =
hasExplicitThreadId !== undefined ? hasExplicitThreadId : !!threadId;
const resolvedHasExplicitThreadId =
ownHasExplicitThreadId || !!parentConfig?.hasExplicitThreadId;
const resolvedDefaultOpen = isModalDefaultOpen ?? true;
const [internalModalOpen, setInternalModalOpen] =
useState<boolean>(resolvedDefaultOpen);
const hasExplicitDefault = isModalDefaultOpen !== undefined;
// When this provider owns its modal state, wrap the setter so that changes
// propagate upward to any ancestor provider. This allows an outer
// CopilotChatConfigurationProvider (e.g. a user's layout-level provider) to
// observe open/close events that originate deep in the tree — fixing the
// "outer hook always returns true" regression (CPK-7152 Behavior B).
const setAndSync = useCallback(
(open: boolean) => {
setInternalModalOpen(open);
parentConfig?.setModalOpen(open);
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[parentConfig?.setModalOpen],
);
// Sync parent → child: when an ancestor's modal state is changed externally
// (e.g. the user calls setModalOpen from an outer hook), reflect that change
// in our own state so the sidebar/popup responds accordingly.
// Skip the initial mount so that our own isModalDefaultOpen is respected and
// not immediately overwritten by the parent's current value.
const isMounted = useRef(false);
useEffect(() => {
if (!hasExplicitDefault) return;
if (!isMounted.current) {
isMounted.current = true;
return;
}
if (parentConfig?.isModalOpen === undefined) return;
setInternalModalOpen(parentConfig.isModalOpen);
}, [parentConfig?.isModalOpen, hasExplicitDefault]);
const resolvedIsModalOpen = hasExplicitDefault
? internalModalOpen
: (parentConfig?.isModalOpen ?? internalModalOpen);
const resolvedSetModalOpen = hasExplicitDefault
? setAndSync
: (parentConfig?.setModalOpen ?? setInternalModalOpen);
const configurationValue: CopilotChatConfigurationValue = useMemo(
() => ({
labels: mergedLabels,
agentId: resolvedAgentId,
threadId: resolvedThreadId,
hasExplicitThreadId: resolvedHasExplicitThreadId,
isModalOpen: resolvedIsModalOpen,
setModalOpen: resolvedSetModalOpen,
}),
[
mergedLabels,
resolvedAgentId,
resolvedThreadId,
resolvedHasExplicitThreadId,
resolvedIsModalOpen,
resolvedSetModalOpen,
],
);
return (
<CopilotChatConfiguration.Provider value={configurationValue}>
{children}
</CopilotChatConfiguration.Provider>
);
};
// Hook to use the full configuration
export const useCopilotChatConfiguration =
(): CopilotChatConfigurationValue | null => {
const configuration = useContext(CopilotChatConfiguration);
return configuration;
};