Skip to content

Commit 6d1de58

Browse files
authored
fix: add backwards compatibility with new Headless UI implementation (CopilotKit#2260)
Signed-off-by: Tyler Slaton <tyler@copilotkit.ai>
1 parent 2fb4db4 commit 6d1de58

32 files changed

Lines changed: 13617 additions & 21275 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
"@copilotkit/react-core": patch
3+
"@copilotkit/react-ui": patch
4+
"@copilotkit/shared": patch
5+
---
6+
7+
- fix: address issues that would cause headless UI breaking changes in the next release
8+
9+
Signed-off-by: Tyler Slaton <tyler@copilotkit.ai>
10+
11+
- fix: more fixes addressing breaking changes in new Headless UI
12+
13+
Signed-off-by: Tyler Slaton <tyler@copilotkit.ai>
14+
15+
- chore: address linting issues
16+
17+
Signed-off-by: Tyler Slaton <tyler@copilotkit.ai>
18+
19+
- chore: fixing branding and docs
20+
21+
Signed-off-by: Tyler Slaton <tyler@copilotkit.ai>
22+
23+
- chore: more docs fixing
24+
25+
Signed-off-by: Tyler Slaton <tyler@copilotkit.ai>

CopilotKit/examples/next-openai/src/app/components/vacation-list.tsx

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { useCopilotChatSuggestions } from "@copilotkit/react-ui";
99
import { useEffect, useState } from "react";
1010
import { DestinationTable } from "./destination-table";
1111
import { VacationNotes } from "./vacation-notes";
12-
import { Role } from "@copilotkit/shared";
13-
import { randomId } from "@copilotkit/shared";
12+
import { MessageRole, TextMessage } from "@copilotkit/runtime-client-gql";
1413

1514
export type Destination = {
1615
name: string;
@@ -141,14 +140,11 @@ export function VacationList() {
141140
*/
142141
useEffect(() => {
143142
appendMessage(
144-
{
145-
id: randomId(),
146-
role: "assistant",
143+
new TextMessage({
144+
role: MessageRole.Assistant,
147145
content: "Hi you! 👋 Let's book your next vacation. Ask me anything.",
148-
},
149-
{
150-
followUp: false,
151-
},
146+
}),
147+
{ followUp: false },
152148
);
153149
}, []);
154150

CopilotKit/examples/next-openai/src/app/headless/page.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
CopilotKit,
55
useCoAgentStateRender,
66
useCopilotAction,
7-
useCopilotChat,
7+
useCopilotChatHeadless_c,
88
useLangGraphInterrupt,
99
} from "@copilotkit/react-core";
1010
import { CopilotSidebar, useCopilotChatSuggestions } from "@copilotkit/react-ui";
@@ -104,14 +104,14 @@ function ScrollToBottomButton() {
104104

105105
function ChatApp() {
106106
const {
107-
visibleMessages,
107+
messages,
108108
suggestions,
109109
setSuggestions,
110-
appendMessage,
110+
sendMessage,
111111
interrupt,
112112
isLoading,
113113
generateSuggestions,
114-
} = useCopilotChat();
114+
} = useCopilotChatHeadless_c();
115115
const [newMessage, setNewMessage] = useState("");
116116
const [selectedMessage, setSelectedMessage] = useState<any>(null);
117117
const [isModalOpen, setIsModalOpen] = useState(false);
@@ -290,10 +290,10 @@ function ChatApp() {
290290
maxSuggestions: 5,
291291
});
292292

293-
const sendMessage = useCallback(
293+
const callSendMessage = useCallback(
294294
async (message: string) => {
295295
// setSuggestions([]);
296-
await appendMessage(
296+
await sendMessage(
297297
{
298298
id: randomId(),
299299
role: "user",
@@ -304,7 +304,7 @@ function ChatApp() {
304304
},
305305
);
306306
},
307-
[appendMessage, setSuggestions, generateSuggestions],
307+
[sendMessage, setSuggestions, generateSuggestions],
308308
);
309309

310310
useEffect(() => {
@@ -318,13 +318,13 @@ function ChatApp() {
318318

319319
const handleSendMessage = useCallback(() => {
320320
if (newMessage.trim()) {
321-
sendMessage(newMessage);
321+
callSendMessage(newMessage);
322322
setNewMessage("");
323323
if (textareaRef.current) {
324324
textareaRef.current.style.height = "auto";
325325
}
326326
}
327-
}, [sendMessage, newMessage]);
327+
}, [callSendMessage, newMessage]);
328328

329329
const handleShowDetails = useCallback((message: any) => {
330330
setSelectedMessage(message);
@@ -350,7 +350,7 @@ function ChatApp() {
350350
{/* Messages */}
351351
<div className="flex-1 overflow-y-auto">
352352
<StickToBottom.Content className="flex flex-col h-full">
353-
{visibleMessages.length === 0 ? (
353+
{messages.length === 0 ? (
354354
<div className="flex-1 flex items-center justify-center h-full">
355355
<div className="text-center">
356356
<div className="text-4xl mb-4">💬</div>
@@ -364,7 +364,7 @@ function ChatApp() {
364364
</div>
365365
) : (
366366
<div className="max-w-4xl mx-auto w-full px-4 py-8">
367-
{visibleMessages.map((message) => (
367+
{messages.map((message) => (
368368
<div
369369
key={message.id}
370370
className={`mb-8 ${message.role === "user" ? "text-right" : "text-left"}`}
@@ -575,7 +575,7 @@ function ChatApp() {
575575
{suggestions.map((suggestion, index) => (
576576
<button
577577
key={index}
578-
onClick={() => sendMessage(suggestion.message)}
578+
onClick={() => callSendMessage(suggestion.message)}
579579
className="bg-gray-100 hover:bg-gray-200 text-gray-700 px-4 py-2 rounded-full text-sm font-medium transition-all duration-200"
580580
>
581581
{suggestion.title}

CopilotKit/examples/next-openai/src/app/multi/page.tsx

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { CopilotChat } from "@copilotkit/react-ui";
44
import "./styles.css";
55
import { CopilotKit, useCopilotAction, useCopilotChat } from "@copilotkit/react-core";
66
import { useSearchParams } from "next/navigation";
7-
import { MessageRole, TextMessage } from "@copilotkit/runtime-client-gql";
7+
import { MessageRole, TextMessage, Message } from "@copilotkit/runtime-client-gql";
88
import { randomId } from "@copilotkit/shared";
99

1010
const testMessages = [
@@ -123,14 +123,12 @@ function TravelPlanner() {
123123
},
124124
handler: async () => {
125125
appendMessage(
126-
{
126+
new TextMessage({
127127
id: randomId(),
128-
role: "assistant",
128+
role: MessageRole.Assistant,
129129
content: "What is the weather in San Francisco?",
130-
},
131-
{
132-
followUp: false,
133-
},
130+
}),
131+
{ followUp: false },
134132
);
135133
},
136134
});
@@ -155,11 +153,13 @@ function TravelPlanner() {
155153
<button
156154
className="bg-blue-500 text-white p-2 rounded-md"
157155
onClick={() =>
158-
appendMessage({
159-
id: randomId(),
160-
role: "user",
161-
content: testMessage.message,
162-
})
156+
appendMessage(
157+
new TextMessage({
158+
id: randomId(),
159+
role: MessageRole.User,
160+
content: testMessage.message,
161+
}),
162+
)
163163
}
164164
>
165165
{testMessage.name}

CopilotKit/packages/react-core/src/components/usage-banner.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import {
44
ErrorVisibility,
55
CopilotKitErrorCode,
66
} from "@copilotkit/shared";
7+
import React from "react";
78

89
interface UsageBannerProps {
910
severity?: Severity;
10-
message?: string;
11+
message?: string | React.ReactNode;
1112
onClose?: () => void;
1213
actions?: {
1314
primary?: {
@@ -219,9 +220,13 @@ export const getErrorActions = (error: CopilotKitError) => {
219220
case CopilotKitErrorCode.MISSING_PUBLIC_API_KEY_ERROR:
220221
return {
221222
primary: {
222-
label: "Get Free API Key",
223+
label: "Show me how",
223224
onClick: () =>
224-
window.open("https://cloud.copilotkit.ai", "_blank", "noopener,noreferrer"),
225+
window.open(
226+
"https://docs.copilotkit.ai/docs/guides/subscription",
227+
"_blank",
228+
"noopener,noreferrer",
229+
),
225230
},
226231
};
227232
case CopilotKitErrorCode.UPGRADE_REQUIRED_ERROR:

CopilotKit/packages/react-core/src/hooks/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
export { useCopilotChat } from "./use-copilot-chat";
2-
export type { UseCopilotChatOptions } from "./use-copilot-chat";
32
export type { UseCopilotChatReturn } from "./use-copilot-chat";
4-
5-
export { useCopilotChatLight } from "./use-copilot-chat-light";
6-
export type {
7-
UseCopilotChatLightOptions,
8-
UseCopilotChatLightReturn,
9-
} from "./use-copilot-chat-light";
3+
export type { UseCopilotChatOptions } from "./use-copilot-chat_internal";
4+
export {
5+
type UseCopilotChatReturn_c,
6+
type UseCopilotChatOptions_c,
7+
useCopilotChatHeadless_c,
8+
} from "./use-copilot-chat-headless_c";
109
export { useCopilotChat as useCopilotChatInternal } from "./use-copilot-chat_internal";
1110
export { useCopilotAction } from "./use-copilot-action";
1211
export { useCoAgentStateRender } from "./use-coagent-state-render";

CopilotKit/packages/react-core/src/hooks/use-coagent.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,7 @@
8989
*/
9090

9191
import { useCallback, useEffect, useMemo, useRef } from "react";
92-
import {
93-
CopilotContextParams,
94-
CopilotMessagesContextParams,
95-
useCopilotContext,
96-
useCopilotMessagesContext,
97-
} from "../context";
92+
import { CopilotContextParams, useCopilotContext } from "../context";
9893
import { CoagentState } from "../types/coagent-state";
9994
import { useCopilotChat } from "./use-copilot-chat_internal";
10095
import { Message } from "@copilotkit/shared";
@@ -239,7 +234,7 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
239234

240235
const { coagentStates, coagentStatesRef, setCoagentStatesWithRef, threadId, copilotApiConfig } =
241236
context;
242-
const { appendMessage, runChatCompletion } = useCopilotChat();
237+
const { sendMessage, runChatCompletion } = useCopilotChat();
243238
const headers = {
244239
...(copilotApiConfig.headers || {}),
245240
};
@@ -351,9 +346,9 @@ export function useCoAgent<T = any>(options: UseCoagentOptions<T>): UseCoagentRe
351346

352347
const runAgentCallback = useAsyncCallback(
353348
async (hint?: HintFunction) => {
354-
await runAgent(name, context, getMessagesFromTap(), appendMessage, runChatCompletion, hint);
349+
await runAgent(name, context, getMessagesFromTap(), sendMessage, runChatCompletion, hint);
355350
},
356-
[name, context, appendMessage, runChatCompletion],
351+
[name, context, sendMessage, runChatCompletion],
357352
);
358353

359354
// Return the state and setState function
@@ -406,7 +401,7 @@ export async function runAgent(
406401
name: string,
407402
context: CopilotContextParams,
408403
messages: GqlMessage[],
409-
appendMessage: (message: Message) => Promise<void>,
404+
sendMessage: (message: Message) => Promise<void>,
410405
runChatCompletion: () => Promise<Message[]>,
411406
hint?: HintFunction,
412407
) {
@@ -430,7 +425,7 @@ export async function runAgent(
430425
if (hint) {
431426
const hintMessage = hint({ previousState, currentState: state });
432427
if (hintMessage) {
433-
await appendMessage(hintMessage);
428+
await sendMessage(hintMessage);
434429
} else {
435430
await runChatCompletion();
436431
}

0 commit comments

Comments
 (0)