forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-copilot-chat.ts
More file actions
132 lines (128 loc) · 3.98 KB
/
Copy pathuse-copilot-chat.ts
File metadata and controls
132 lines (128 loc) · 3.98 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
/**
* `useCopilotChat` is a lightweight React hook for headless chat interactions.
* Perfect for controlling the prebuilt chat components programmatically.
*
* **Open Source Friendly** - Works without requiring a free public license key.
*
* <Callout title="Looking for fully headless UI?">
* Get started with [useCopilotChatHeadless_c](https://docs.copilotkit.ai/reference/v2/hooks/useCopilotChatHeadless_c).
* </Callout>
*
* ## Use Cases
*
* - **Programmatic Messaging**: Send messages without displaying chat UI
* - **Programmatic control**: Control prebuilt component programmatically
* - **Background Operations**: Trigger AI interactions in the background
* - **Fire-and-Forget**: Send messages without needing to read responses
*
* ## Usage
*
* ```tsx
* import { TextMessage, MessageRole } from "@copilotkit/runtime-client-gql";
*
* const { appendMessage } = useCopilotChat();
*
* // Example usage without naming conflicts
* const handleSendMessage = async (content: string) => {
* await appendMessage(
* new TextMessage({
* role: MessageRole.User,
* content,
* })
* );
* };
* ```
*
* ## Return Values
* The following properties are returned from the hook:
*
* <PropertyReference name="visibleMessages" type="DeprecatedGqlMessage[]" deprecated>
* Array of messages in old non-AG-UI format, use for compatibility only
* </PropertyReference>
*
* <PropertyReference name="appendMessage" type="(message: DeprecatedGqlMessage, options?) => Promise<void>" deprecated>
* Append message using old format, use `sendMessage` instead
* </PropertyReference>
*
* <PropertyReference name="reloadMessages" type="(messageId: string) => Promise<void>">
* Regenerate the response for a specific message by ID
* </PropertyReference>
*
* <PropertyReference name="stopGeneration" type="() => void">
* Stop the current message generation process
* </PropertyReference>
*
* <PropertyReference name="reset" type="() => void">
* Clear all messages and reset chat state completely
* </PropertyReference>
*
* <PropertyReference name="isLoading" type="boolean">
* Whether the chat is currently generating a response
* </PropertyReference>
*
* <PropertyReference name="runChatCompletion" type="() => Promise<Message[]>">
* Manually trigger chat completion for advanced usage
* </PropertyReference>
*
* <PropertyReference name="mcpServers" type="MCPServerConfig[]">
* Array of Model Context Protocol server configurations
* </PropertyReference>
*
* <PropertyReference name="setMcpServers" type="(servers: MCPServerConfig[]) => void">
* Update MCP server configurations for enhanced context
* </PropertyReference>
*/
import type {
UseCopilotChatOptions,
UseCopilotChatReturn as UseCopilotChatReturnInternal,
} from "./use-copilot-chat_internal";
import { useCopilotChatInternal } from "./use-copilot-chat_internal";
// Create a type that excludes message-related properties from the internal type
export type UseCopilotChatReturn = Omit<
UseCopilotChatReturnInternal,
| "messages"
| "sendMessage"
| "suggestions"
| "setSuggestions"
| "generateSuggestions"
| "isLoadingSuggestions"
| "resetSuggestions"
| "interrupt"
| "setMessages"
| "deleteMessage"
>;
/**
* A lightweight React hook for headless chat interactions.
* Perfect for programmatic messaging, background operations, and custom UI implementations.
*
* **Open Source Friendly** - Works without requiring a `publicApiKey`.
*/
// TODO: Do we need this? If so, does it work properly? test.
export function useCopilotChat(
options: UseCopilotChatOptions = {},
): UseCopilotChatReturn {
const {
visibleMessages,
appendMessage,
reloadMessages,
stopGeneration,
reset,
isLoading,
isAvailable,
runChatCompletion,
mcpServers,
setMcpServers,
} = useCopilotChatInternal(options);
return {
visibleMessages,
appendMessage,
reloadMessages,
stopGeneration,
reset,
isLoading,
isAvailable,
runChatCompletion,
mcpServers,
setMcpServers,
};
}