@copilotkit/react-core 0.37.0 → 0.38.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (168) hide show
  1. package/.turbo/turbo-build.log +187 -234
  2. package/CHANGELOG.md +12 -0
  3. package/dist/{chunk-HUKLBIUK.mjs → chunk-5CKW6KE7.mjs} +13 -22
  4. package/dist/chunk-5CKW6KE7.mjs.map +1 -0
  5. package/dist/{chunk-S5LWO5V3.mjs → chunk-7GPIOOXB.mjs} +2 -2
  6. package/dist/{chunk-LLU5URI5.mjs → chunk-DDHJCFFV.mjs} +18 -33
  7. package/dist/chunk-DDHJCFFV.mjs.map +1 -0
  8. package/dist/{chunk-SR4RW4CU.mjs → chunk-IQCLNCGL.mjs} +6 -6
  9. package/dist/chunk-IQCLNCGL.mjs.map +1 -0
  10. package/dist/{chunk-DY63PD22.mjs → chunk-K6EV2CNB.mjs} +4 -5
  11. package/dist/chunk-K6EV2CNB.mjs.map +1 -0
  12. package/dist/{chunk-NSUYO6TP.mjs → chunk-LZVHW3T3.mjs} +9 -9
  13. package/dist/chunk-LZVHW3T3.mjs.map +1 -0
  14. package/dist/{chunk-Z5FB4WBL.mjs → chunk-NY6QSOU7.mjs} +43 -40
  15. package/dist/chunk-NY6QSOU7.mjs.map +1 -0
  16. package/dist/chunk-PHMHNAYC.mjs +192 -0
  17. package/dist/chunk-PHMHNAYC.mjs.map +1 -0
  18. package/dist/{chunk-B244LK6F.mjs → chunk-QNJQKKD4.mjs} +2 -2
  19. package/dist/{chunk-4OIVQMEM.mjs → chunk-WU3I3G3G.mjs} +60 -42
  20. package/dist/chunk-WU3I3G3G.mjs.map +1 -0
  21. package/dist/chunk-YJLRG5U3.mjs +1 -0
  22. package/dist/{chunk-MNZXQ6UH.mjs → chunk-YM2JV2YQ.mjs} +2 -2
  23. package/dist/components/copilot-provider/copilotkit-props.d.ts +2 -2
  24. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
  25. package/dist/components/copilot-provider/copilotkit.js +18 -33
  26. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  27. package/dist/components/copilot-provider/copilotkit.mjs +2 -2
  28. package/dist/components/copilot-provider/index.js +18 -33
  29. package/dist/components/copilot-provider/index.js.map +1 -1
  30. package/dist/components/copilot-provider/index.mjs +2 -2
  31. package/dist/components/index.js +18 -33
  32. package/dist/components/index.js.map +1 -1
  33. package/dist/components/index.mjs +2 -2
  34. package/dist/context/copilot-context.d.ts +7 -18
  35. package/dist/context/copilot-context.js +3 -4
  36. package/dist/context/copilot-context.js.map +1 -1
  37. package/dist/context/copilot-context.mjs +1 -1
  38. package/dist/context/index.d.ts +1 -0
  39. package/dist/context/index.js +3 -4
  40. package/dist/context/index.js.map +1 -1
  41. package/dist/context/index.mjs +1 -1
  42. package/dist/hooks/index.d.ts +2 -1
  43. package/dist/hooks/index.js +140 -229
  44. package/dist/hooks/index.js.map +1 -1
  45. package/dist/hooks/index.mjs +9 -10
  46. package/dist/hooks/use-chat.d.ts +25 -51
  47. package/dist/hooks/use-chat.js +109 -203
  48. package/dist/hooks/use-chat.js.map +1 -1
  49. package/dist/hooks/use-chat.mjs +1 -2
  50. package/dist/hooks/use-copilot-action.js +10 -11
  51. package/dist/hooks/use-copilot-action.js.map +1 -1
  52. package/dist/hooks/use-copilot-action.mjs +2 -2
  53. package/dist/hooks/use-copilot-chat.d.ts +1 -1
  54. package/dist/hooks/use-copilot-chat.js +123 -212
  55. package/dist/hooks/use-copilot-chat.js.map +1 -1
  56. package/dist/hooks/use-copilot-chat.mjs +4 -5
  57. package/dist/hooks/use-copilot-readable.js +3 -4
  58. package/dist/hooks/use-copilot-readable.js.map +1 -1
  59. package/dist/hooks/use-copilot-readable.mjs +2 -2
  60. package/dist/hooks/use-make-copilot-actionable.js +7 -8
  61. package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
  62. package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
  63. package/dist/hooks/use-make-copilot-document-readable.js +3 -4
  64. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  65. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  66. package/dist/hooks/use-make-copilot-readable.js +3 -4
  67. package/dist/hooks/use-make-copilot-readable.js.map +1 -1
  68. package/dist/hooks/use-make-copilot-readable.mjs +2 -2
  69. package/dist/index.d.ts +1 -3
  70. package/dist/index.js +239 -497
  71. package/dist/index.js.map +1 -1
  72. package/dist/index.mjs +12 -31
  73. package/dist/lib/copilot-task.d.ts +1 -0
  74. package/dist/lib/copilot-task.js +34 -110
  75. package/dist/lib/copilot-task.js.map +1 -1
  76. package/dist/lib/copilot-task.mjs +3 -4
  77. package/dist/lib/index.d.ts +1 -0
  78. package/dist/lib/index.js +36 -110
  79. package/dist/lib/index.js.map +1 -1
  80. package/dist/lib/index.mjs +3 -4
  81. package/dist/utils/extract.d.ts +1 -0
  82. package/dist/utils/extract.js +53 -109
  83. package/dist/utils/extract.js.map +1 -1
  84. package/dist/utils/extract.mjs +3 -4
  85. package/dist/utils/index.d.ts +1 -1
  86. package/dist/utils/index.js +56 -132
  87. package/dist/utils/index.js.map +1 -1
  88. package/dist/utils/index.mjs +5 -13
  89. package/package.json +6 -5
  90. package/src/components/copilot-provider/copilotkit-props.tsx +2 -2
  91. package/src/components/copilot-provider/copilotkit.tsx +16 -37
  92. package/src/context/copilot-context.tsx +11 -30
  93. package/src/hooks/use-chat.ts +179 -208
  94. package/src/hooks/use-copilot-action.ts +7 -8
  95. package/src/hooks/use-copilot-chat.ts +9 -23
  96. package/src/hooks/use-make-copilot-actionable.ts +4 -4
  97. package/src/index.tsx +0 -1
  98. package/src/lib/copilot-task.ts +45 -42
  99. package/src/utils/extract.ts +64 -36
  100. package/src/utils/index.ts +0 -7
  101. package/dist/chunk-2EQGN5QK.mjs +0 -137
  102. package/dist/chunk-2EQGN5QK.mjs.map +0 -1
  103. package/dist/chunk-4OIVQMEM.mjs.map +0 -1
  104. package/dist/chunk-7GFKOIO7.mjs +0 -1
  105. package/dist/chunk-B2H3NC4E.mjs +0 -204
  106. package/dist/chunk-B2H3NC4E.mjs.map +0 -1
  107. package/dist/chunk-BABVSMJR.mjs +0 -1
  108. package/dist/chunk-BABVSMJR.mjs.map +0 -1
  109. package/dist/chunk-CYDWEPFL.mjs +0 -1
  110. package/dist/chunk-CYDWEPFL.mjs.map +0 -1
  111. package/dist/chunk-DY63PD22.mjs.map +0 -1
  112. package/dist/chunk-FRAKUJWH.mjs +0 -1
  113. package/dist/chunk-FRAKUJWH.mjs.map +0 -1
  114. package/dist/chunk-HUKLBIUK.mjs.map +0 -1
  115. package/dist/chunk-LLU5URI5.mjs.map +0 -1
  116. package/dist/chunk-MJKBCG4U.mjs +0 -91
  117. package/dist/chunk-MJKBCG4U.mjs.map +0 -1
  118. package/dist/chunk-NSUYO6TP.mjs.map +0 -1
  119. package/dist/chunk-SR4RW4CU.mjs.map +0 -1
  120. package/dist/chunk-Z5FB4WBL.mjs.map +0 -1
  121. package/dist/chunk-ZFS5SQUT.mjs +0 -31
  122. package/dist/chunk-ZFS5SQUT.mjs.map +0 -1
  123. package/dist/openai-assistants/hooks/index.d.ts +0 -2
  124. package/dist/openai-assistants/hooks/index.js +0 -277
  125. package/dist/openai-assistants/hooks/index.js.map +0 -1
  126. package/dist/openai-assistants/hooks/index.mjs +0 -18
  127. package/dist/openai-assistants/hooks/index.mjs.map +0 -1
  128. package/dist/openai-assistants/hooks/use-assistants.d.ts +0 -17
  129. package/dist/openai-assistants/hooks/use-assistants.js +0 -154
  130. package/dist/openai-assistants/hooks/use-assistants.js.map +0 -1
  131. package/dist/openai-assistants/hooks/use-assistants.mjs +0 -92
  132. package/dist/openai-assistants/hooks/use-assistants.mjs.map +0 -1
  133. package/dist/openai-assistants/hooks/use-copilot-chat-v2.d.ts +0 -44
  134. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +0 -277
  135. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +0 -1
  136. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +0 -19
  137. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +0 -1
  138. package/dist/openai-assistants/index.d.ts +0 -3
  139. package/dist/openai-assistants/index.js +0 -279
  140. package/dist/openai-assistants/index.js.map +0 -1
  141. package/dist/openai-assistants/index.mjs +0 -22
  142. package/dist/openai-assistants/index.mjs.map +0 -1
  143. package/dist/openai-assistants/utils/index.d.ts +0 -1
  144. package/dist/openai-assistants/utils/index.js +0 -73
  145. package/dist/openai-assistants/utils/index.js.map +0 -1
  146. package/dist/openai-assistants/utils/index.mjs +0 -9
  147. package/dist/openai-assistants/utils/index.mjs.map +0 -1
  148. package/dist/openai-assistants/utils/process-message-stream.d.ts +0 -3
  149. package/dist/openai-assistants/utils/process-message-stream.js +0 -71
  150. package/dist/openai-assistants/utils/process-message-stream.js.map +0 -1
  151. package/dist/openai-assistants/utils/process-message-stream.mjs +0 -8
  152. package/dist/openai-assistants/utils/process-message-stream.mjs.map +0 -1
  153. package/dist/utils/fetch-chat-completion.d.ts +0 -36
  154. package/dist/utils/fetch-chat-completion.js +0 -141
  155. package/dist/utils/fetch-chat-completion.js.map +0 -1
  156. package/dist/utils/fetch-chat-completion.mjs +0 -12
  157. package/dist/utils/fetch-chat-completion.mjs.map +0 -1
  158. package/src/openai-assistants/hooks/index.ts +0 -9
  159. package/src/openai-assistants/hooks/use-assistants.ts +0 -112
  160. package/src/openai-assistants/hooks/use-copilot-chat-v2.ts +0 -189
  161. package/src/openai-assistants/index.ts +0 -2
  162. package/src/openai-assistants/utils/index.ts +0 -1
  163. package/src/openai-assistants/utils/process-message-stream.ts +0 -25
  164. package/src/utils/fetch-chat-completion.ts +0 -120
  165. /package/dist/{chunk-S5LWO5V3.mjs.map → chunk-7GPIOOXB.mjs.map} +0 -0
  166. /package/dist/{chunk-B244LK6F.mjs.map → chunk-QNJQKKD4.mjs.map} +0 -0
  167. /package/dist/{chunk-7GFKOIO7.mjs.map → chunk-YJLRG5U3.mjs.map} +0 -0
  168. /package/dist/{chunk-MNZXQ6UH.mjs.map → chunk-YM2JV2YQ.mjs.map} +0 -0
@@ -1,189 +0,0 @@
1
- import { useContext, useMemo, useState } from "react";
2
- import { processMessageStream } from "../utils";
3
- import { Message, parseStreamPart } from "@copilotkit/shared";
4
- import { CopilotContext } from "../../context";
5
- import { defaultCopilotContextCategories } from "../../components";
6
-
7
- export type AssistantStatus = "in_progress" | "awaiting_message";
8
-
9
- export interface RequestForwardingOptions {
10
- /**
11
- * The credentials mode to be used for the fetch request.
12
- * Possible values are: 'omit', 'same-origin', 'include'.
13
- * Defaults to 'same-origin'.
14
- */
15
- credentials?: RequestCredentials;
16
- /**
17
- * HTTP headers to be sent with the API request.
18
- */
19
- headers?: Record<string, string> | Headers;
20
- /**
21
- * Extra body object to be sent with the API request.
22
- * @example
23
- * Send a `sessionId` to the API along with the messages.
24
- * ```js
25
- * useChat({
26
- * body: {
27
- * sessionId: '123',
28
- * }
29
- * })
30
- * ```
31
- */
32
- body?: object;
33
- }
34
- export interface UseCopilotChatOptionsV2 extends RequestForwardingOptions {
35
- makeSystemMessage?: (contextString: string) => string;
36
- threadId?: string | undefined;
37
- }
38
-
39
- export interface UseCopilotChatV2Result {
40
- messages: Message[];
41
- input: string;
42
- handleInputChange: (e: any) => void;
43
- submitMessage: (e: any) => Promise<void>;
44
- status: AssistantStatus;
45
- error: unknown;
46
- }
47
-
48
- export function useCopilotChatV2(options: UseCopilotChatOptionsV2): UseCopilotChatV2Result {
49
- const {
50
- getContextString,
51
- getChatCompletionFunctionDescriptions,
52
- getFunctionCallHandler,
53
- copilotApiConfig,
54
- } = useContext(CopilotContext);
55
-
56
- const [messages, setMessages] = useState<Message[]>([]);
57
- const [input, setInput] = useState("");
58
- const [threadId, setThreadId] = useState<string | undefined>(undefined);
59
- const [status, setStatus] = useState<AssistantStatus>("awaiting_message");
60
- const [error, setError] = useState<unknown | undefined>(undefined);
61
-
62
- const systemMessage: Message = useMemo(() => {
63
- const systemMessageMaker = options.makeSystemMessage || defaultSystemMessage;
64
- const contextString = getContextString([], defaultCopilotContextCategories); // TODO: make the context categories configurable
65
-
66
- return {
67
- id: "system",
68
- content: systemMessageMaker(contextString),
69
- role: "system",
70
- };
71
- }, [getContextString, options.makeSystemMessage]);
72
-
73
- const handleInputChange = (e: any) => {
74
- setInput(e.target.value);
75
- };
76
-
77
- const submitMessage = async (e: any) => {
78
- e.preventDefault();
79
-
80
- if (input === "") {
81
- return;
82
- }
83
-
84
- setStatus("in_progress");
85
-
86
- setMessages((messages) => [...messages, { id: "", role: "user", content: input }]);
87
-
88
- setInput("");
89
-
90
- const apiUrl = copilotApiConfig.chatApiEndpointV2;
91
-
92
- const functions = getChatCompletionFunctionDescriptions();
93
-
94
- const result = await fetch(apiUrl, {
95
- method: "POST",
96
- headers: {
97
- "Content-Type": "application/json",
98
- ...copilotApiConfig.headers,
99
- ...options.headers,
100
- },
101
- body: JSON.stringify({
102
- // always use user-provided threadId when available:
103
- threadId: options.threadId ?? threadId ?? null,
104
- message: input,
105
- ...(functions.length > 0 && { functions: functions }),
106
- ...copilotApiConfig.body,
107
- ...options.body,
108
- }),
109
- });
110
-
111
- if (result.body == null) {
112
- throw new Error("The response body is empty.");
113
- }
114
-
115
- await processMessageStream(result.body.getReader(), (message: string) => {
116
- try {
117
- const { type, value } = parseStreamPart(message);
118
-
119
- switch (type) {
120
- case "assistant_message": {
121
- // append message:
122
- setMessages((messages) => [
123
- ...messages,
124
- {
125
- id: value.id,
126
- role: value.role,
127
- content: value.content[0].text.value,
128
- },
129
- ]);
130
- break;
131
- }
132
-
133
- case "assistant_control_data": {
134
- setThreadId(value.threadId);
135
-
136
- // set id of last message:
137
- setMessages((messages) => {
138
- const lastMessage = messages[messages.length - 1];
139
- lastMessage.id = value.messageId;
140
- return [...messages.slice(0, messages.length - 1), lastMessage];
141
- });
142
-
143
- break;
144
- }
145
-
146
- case "error": {
147
- setError(value);
148
- break;
149
- }
150
- }
151
- } catch (error) {
152
- setError(error);
153
- }
154
- });
155
-
156
- setStatus("awaiting_message");
157
- };
158
-
159
- return {
160
- messages,
161
- input,
162
- handleInputChange,
163
- submitMessage,
164
- status,
165
- error,
166
- };
167
- }
168
-
169
- export function defaultSystemMessage(contextString: string): string {
170
- return `
171
- Please act as an efficient, competent, conscientious, and industrious professional assistant.
172
-
173
- Help the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.
174
- Always be polite and respectful, and prefer brevity over verbosity.
175
-
176
- The user has provided you with the following context:
177
- \`\`\`
178
- ${contextString}
179
- \`\`\`
180
-
181
- They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
182
-
183
- Please assist them as best you can.
184
-
185
- You can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.
186
-
187
- If you would like to call a function, call it without saying anything else.
188
- `;
189
- }
@@ -1,2 +0,0 @@
1
- export * from "./hooks";
2
- export * from "./utils";
@@ -1 +0,0 @@
1
- export { processMessageStream } from "./process-message-stream";
@@ -1,25 +0,0 @@
1
- export async function processMessageStream(
2
- reader: ReadableStreamDefaultReader<Uint8Array>,
3
- processMessage: (message: string) => void | Promise<void>,
4
- ) {
5
- const decoder = new TextDecoder();
6
- let buffer = "";
7
- while (true) {
8
- const { done, value } = await reader.read();
9
-
10
- if (done) {
11
- if (buffer.length > 0) {
12
- processMessage(buffer);
13
- }
14
- break;
15
- }
16
-
17
- buffer += decoder.decode(value, { stream: true });
18
-
19
- let endIndex: number;
20
- while ((endIndex = buffer.indexOf("\n")) !== -1) {
21
- processMessage(buffer.substring(0, endIndex).trim());
22
- buffer = buffer.substring(endIndex + 1); // Remove the processed instruction + delimiter
23
- }
24
- }
25
- }
@@ -1,120 +0,0 @@
1
- import {
2
- Message,
3
- ToolDefinition,
4
- ChatCompletionEvent,
5
- decodeChatCompletion,
6
- parseChatCompletion,
7
- decodeChatCompletionAsText,
8
- EXCLUDE_FROM_FORWARD_PROPS_KEYS,
9
- } from "@copilotkit/shared";
10
- import { CopilotApiConfig } from "../context";
11
-
12
- export interface FetchChatCompletionParams {
13
- copilotConfig: CopilotApiConfig;
14
- model?: string;
15
- messages: Message[];
16
- tools?: ToolDefinition[];
17
- temperature?: number;
18
- maxTokens?: number;
19
- headers?: Record<string, string> | Headers;
20
- body?: object;
21
- signal?: AbortSignal;
22
- toolChoice?: string | { type: "function"; function: { name: string } };
23
- }
24
-
25
- export async function fetchChatCompletion({
26
- copilotConfig,
27
- model,
28
- messages,
29
- tools,
30
- temperature,
31
- headers,
32
- body,
33
- signal,
34
- toolChoice,
35
- }: FetchChatCompletionParams): Promise<Response> {
36
- temperature ||= 0.5;
37
- tools ||= [];
38
-
39
- // clean up any extra properties from messages
40
- const cleanedMessages = messages.map((message) => {
41
- const { content, role, name, function_call } = message;
42
- return { content, role, name, function_call };
43
- });
44
-
45
- toolChoice ||= "auto";
46
-
47
- const response = await fetch(copilotConfig.chatApiEndpoint, {
48
- method: "POST",
49
- headers: {
50
- "Content-Type": "application/json",
51
- ...copilotConfig.headers,
52
- ...(headers ? { ...headers } : {}),
53
- },
54
- body: JSON.stringify({
55
- model,
56
- messages: cleanedMessages,
57
- stream: true,
58
- ...(tools.length ? { tools } : {}),
59
- ...(temperature ? { temperature } : {}),
60
- ...(tools.length != 0 ? { tool_choice: toolChoice } : {}),
61
- ...copilotConfig.body,
62
- ...copilotConfig.backendOnlyProps,
63
- ...excludeBackendOnlyProps(copilotConfig),
64
- ...(body ? { ...body } : {}),
65
- ...(copilotConfig.cloud ? { cloud: copilotConfig.cloud } : {}),
66
- }),
67
- signal,
68
- credentials: copilotConfig.credentials,
69
- });
70
-
71
- return response;
72
- }
73
-
74
- function excludeBackendOnlyProps(copilotConfig: any) {
75
- const backendOnlyProps = copilotConfig.backendOnlyProps ?? {};
76
- if (Object.keys(backendOnlyProps).length > 0) {
77
- return {
78
- [EXCLUDE_FROM_FORWARD_PROPS_KEYS]: Object.keys(backendOnlyProps),
79
- };
80
- } else {
81
- return {};
82
- }
83
- }
84
-
85
- export interface DecodedChatCompletionResponse extends Response {
86
- events: ReadableStream<ChatCompletionEvent> | null;
87
- }
88
-
89
- export async function fetchAndDecodeChatCompletion(
90
- params: FetchChatCompletionParams,
91
- ): Promise<DecodedChatCompletionResponse> {
92
- const response = await fetchChatCompletion(params);
93
- if (!response.ok || !response.body) {
94
- (response as any).events = null;
95
- } else {
96
- const events = await decodeChatCompletion(parseChatCompletion(response.body));
97
- (response as any).events = events;
98
- }
99
- return response as any;
100
- }
101
-
102
- export interface DecodedChatCompletionResponseAsText extends Response {
103
- events: ReadableStream<string> | null;
104
- }
105
-
106
- export async function fetchAndDecodeChatCompletionAsText(
107
- params: FetchChatCompletionParams,
108
- ): Promise<DecodedChatCompletionResponseAsText> {
109
- const response = await fetchChatCompletion(params);
110
- if (!response.ok || !response.body) {
111
- (response as any).events = null;
112
- } else {
113
- const events = await decodeChatCompletionAsText(
114
- decodeChatCompletion(parseChatCompletion(response.body)),
115
- );
116
- (response as any).events = events;
117
- }
118
-
119
- return response as any;
120
- }