@copilotkit/react-core 0.38.0-mme-pre.0 → 0.38.0-mme-alpha.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 (165) hide show
  1. package/.turbo/turbo-build.log +187 -234
  2. package/CHANGELOG.md +4 -3
  3. package/dist/{chunk-NKIUZSGZ.mjs → chunk-5Q7DU3XW.mjs} +40 -40
  4. package/dist/chunk-5Q7DU3XW.mjs.map +1 -0
  5. package/dist/{chunk-B244LK6F.mjs → chunk-ATWK243A.mjs} +2 -2
  6. package/dist/{chunk-AJSY6LS5.mjs → chunk-FY3TQ7Q6.mjs} +17 -32
  7. package/dist/chunk-FY3TQ7Q6.mjs.map +1 -0
  8. package/dist/{chunk-SR4RW4CU.mjs → chunk-GFQBAEJL.mjs} +6 -6
  9. package/dist/chunk-GFQBAEJL.mjs.map +1 -0
  10. package/dist/{chunk-DY63PD22.mjs → chunk-GLPZSBNA.mjs} +4 -5
  11. package/dist/chunk-GLPZSBNA.mjs.map +1 -0
  12. package/dist/{chunk-S5LWO5V3.mjs → chunk-IBOL6LE5.mjs} +2 -2
  13. package/dist/{chunk-NSUYO6TP.mjs → chunk-MI7BERU7.mjs} +9 -9
  14. package/dist/chunk-MI7BERU7.mjs.map +1 -0
  15. package/dist/chunk-MW2IVCDP.mjs +188 -0
  16. package/dist/chunk-MW2IVCDP.mjs.map +1 -0
  17. package/dist/{chunk-ZHYCHXIO.mjs → chunk-PSZAPRNZ.mjs} +56 -42
  18. package/dist/chunk-PSZAPRNZ.mjs.map +1 -0
  19. package/dist/{chunk-DY4YVUO3.mjs → chunk-WJIS7HTH.mjs} +14 -25
  20. package/dist/chunk-WJIS7HTH.mjs.map +1 -0
  21. package/dist/{chunk-MNZXQ6UH.mjs → chunk-WTGFKR3D.mjs} +2 -2
  22. package/dist/chunk-YJLRG5U3.mjs +1 -0
  23. package/dist/components/copilot-provider/copilotkit.js +17 -32
  24. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  25. package/dist/components/copilot-provider/copilotkit.mjs +2 -2
  26. package/dist/components/copilot-provider/index.js +17 -32
  27. package/dist/components/copilot-provider/index.js.map +1 -1
  28. package/dist/components/copilot-provider/index.mjs +2 -2
  29. package/dist/components/index.js +17 -32
  30. package/dist/components/index.js.map +1 -1
  31. package/dist/components/index.mjs +2 -2
  32. package/dist/context/copilot-context.d.ts +5 -5
  33. package/dist/context/copilot-context.js +3 -4
  34. package/dist/context/copilot-context.js.map +1 -1
  35. package/dist/context/copilot-context.mjs +1 -1
  36. package/dist/context/index.d.ts +1 -0
  37. package/dist/context/index.js +3 -4
  38. package/dist/context/index.js.map +1 -1
  39. package/dist/context/index.mjs +1 -1
  40. package/dist/hooks/index.d.ts +2 -1
  41. package/dist/hooks/index.js +136 -232
  42. package/dist/hooks/index.js.map +1 -1
  43. package/dist/hooks/index.mjs +9 -10
  44. package/dist/hooks/use-chat.d.ts +26 -52
  45. package/dist/hooks/use-chat.js +104 -203
  46. package/dist/hooks/use-chat.js.map +1 -1
  47. package/dist/hooks/use-chat.mjs +1 -2
  48. package/dist/hooks/use-copilot-action.js +10 -11
  49. package/dist/hooks/use-copilot-action.js.map +1 -1
  50. package/dist/hooks/use-copilot-action.mjs +2 -2
  51. package/dist/hooks/use-copilot-chat.d.ts +1 -1
  52. package/dist/hooks/use-copilot-chat.js +119 -215
  53. package/dist/hooks/use-copilot-chat.js.map +1 -1
  54. package/dist/hooks/use-copilot-chat.mjs +4 -5
  55. package/dist/hooks/use-copilot-readable.js +3 -4
  56. package/dist/hooks/use-copilot-readable.js.map +1 -1
  57. package/dist/hooks/use-copilot-readable.mjs +2 -2
  58. package/dist/hooks/use-make-copilot-actionable.js +7 -8
  59. package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
  60. package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
  61. package/dist/hooks/use-make-copilot-document-readable.js +3 -4
  62. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  63. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  64. package/dist/hooks/use-make-copilot-readable.js +3 -4
  65. package/dist/hooks/use-make-copilot-readable.js.map +1 -1
  66. package/dist/hooks/use-make-copilot-readable.mjs +2 -2
  67. package/dist/index.d.ts +1 -3
  68. package/dist/index.js +227 -499
  69. package/dist/index.js.map +1 -1
  70. package/dist/index.mjs +12 -31
  71. package/dist/lib/copilot-task.d.ts +1 -0
  72. package/dist/lib/copilot-task.js +31 -110
  73. package/dist/lib/copilot-task.js.map +1 -1
  74. package/dist/lib/copilot-task.mjs +3 -4
  75. package/dist/lib/index.d.ts +1 -0
  76. package/dist/lib/index.js +33 -110
  77. package/dist/lib/index.js.map +1 -1
  78. package/dist/lib/index.mjs +3 -4
  79. package/dist/utils/extract.d.ts +1 -0
  80. package/dist/utils/extract.js +49 -109
  81. package/dist/utils/extract.js.map +1 -1
  82. package/dist/utils/extract.mjs +3 -4
  83. package/dist/utils/index.d.ts +1 -1
  84. package/dist/utils/index.js +52 -132
  85. package/dist/utils/index.js.map +1 -1
  86. package/dist/utils/index.mjs +5 -13
  87. package/package.json +6 -5
  88. package/src/components/copilot-provider/copilotkit.tsx +16 -33
  89. package/src/context/copilot-context.tsx +9 -16
  90. package/src/hooks/use-chat.ts +204 -212
  91. package/src/hooks/use-copilot-action.ts +7 -8
  92. package/src/hooks/use-copilot-chat.ts +12 -36
  93. package/src/hooks/use-make-copilot-actionable.ts +4 -4
  94. package/src/index.tsx +0 -1
  95. package/src/lib/copilot-task.ts +42 -42
  96. package/src/utils/extract.ts +61 -36
  97. package/src/utils/index.ts +0 -7
  98. package/dist/chunk-36FKUOWM.mjs +0 -137
  99. package/dist/chunk-36FKUOWM.mjs.map +0 -1
  100. package/dist/chunk-3XVO5UEG.mjs +0 -205
  101. package/dist/chunk-3XVO5UEG.mjs.map +0 -1
  102. package/dist/chunk-7GFKOIO7.mjs +0 -1
  103. package/dist/chunk-AJSY6LS5.mjs.map +0 -1
  104. package/dist/chunk-BABVSMJR.mjs +0 -1
  105. package/dist/chunk-BABVSMJR.mjs.map +0 -1
  106. package/dist/chunk-CYDWEPFL.mjs +0 -1
  107. package/dist/chunk-CYDWEPFL.mjs.map +0 -1
  108. package/dist/chunk-DY4YVUO3.mjs.map +0 -1
  109. package/dist/chunk-DY63PD22.mjs.map +0 -1
  110. package/dist/chunk-FRAKUJWH.mjs +0 -1
  111. package/dist/chunk-FRAKUJWH.mjs.map +0 -1
  112. package/dist/chunk-MJKBCG4U.mjs +0 -91
  113. package/dist/chunk-MJKBCG4U.mjs.map +0 -1
  114. package/dist/chunk-NKIUZSGZ.mjs.map +0 -1
  115. package/dist/chunk-NSUYO6TP.mjs.map +0 -1
  116. package/dist/chunk-SR4RW4CU.mjs.map +0 -1
  117. package/dist/chunk-ZFS5SQUT.mjs +0 -31
  118. package/dist/chunk-ZFS5SQUT.mjs.map +0 -1
  119. package/dist/chunk-ZHYCHXIO.mjs.map +0 -1
  120. package/dist/openai-assistants/hooks/index.d.ts +0 -2
  121. package/dist/openai-assistants/hooks/index.js +0 -277
  122. package/dist/openai-assistants/hooks/index.js.map +0 -1
  123. package/dist/openai-assistants/hooks/index.mjs +0 -18
  124. package/dist/openai-assistants/hooks/index.mjs.map +0 -1
  125. package/dist/openai-assistants/hooks/use-assistants.d.ts +0 -17
  126. package/dist/openai-assistants/hooks/use-assistants.js +0 -154
  127. package/dist/openai-assistants/hooks/use-assistants.js.map +0 -1
  128. package/dist/openai-assistants/hooks/use-assistants.mjs +0 -92
  129. package/dist/openai-assistants/hooks/use-assistants.mjs.map +0 -1
  130. package/dist/openai-assistants/hooks/use-copilot-chat-v2.d.ts +0 -44
  131. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +0 -277
  132. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +0 -1
  133. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +0 -19
  134. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +0 -1
  135. package/dist/openai-assistants/index.d.ts +0 -3
  136. package/dist/openai-assistants/index.js +0 -279
  137. package/dist/openai-assistants/index.js.map +0 -1
  138. package/dist/openai-assistants/index.mjs +0 -22
  139. package/dist/openai-assistants/index.mjs.map +0 -1
  140. package/dist/openai-assistants/utils/index.d.ts +0 -1
  141. package/dist/openai-assistants/utils/index.js +0 -73
  142. package/dist/openai-assistants/utils/index.js.map +0 -1
  143. package/dist/openai-assistants/utils/index.mjs +0 -9
  144. package/dist/openai-assistants/utils/index.mjs.map +0 -1
  145. package/dist/openai-assistants/utils/process-message-stream.d.ts +0 -3
  146. package/dist/openai-assistants/utils/process-message-stream.js +0 -71
  147. package/dist/openai-assistants/utils/process-message-stream.js.map +0 -1
  148. package/dist/openai-assistants/utils/process-message-stream.mjs +0 -8
  149. package/dist/openai-assistants/utils/process-message-stream.mjs.map +0 -1
  150. package/dist/utils/fetch-chat-completion.d.ts +0 -36
  151. package/dist/utils/fetch-chat-completion.js +0 -141
  152. package/dist/utils/fetch-chat-completion.js.map +0 -1
  153. package/dist/utils/fetch-chat-completion.mjs +0 -12
  154. package/dist/utils/fetch-chat-completion.mjs.map +0 -1
  155. package/src/openai-assistants/hooks/index.ts +0 -9
  156. package/src/openai-assistants/hooks/use-assistants.ts +0 -112
  157. package/src/openai-assistants/hooks/use-copilot-chat-v2.ts +0 -189
  158. package/src/openai-assistants/index.ts +0 -2
  159. package/src/openai-assistants/utils/index.ts +0 -1
  160. package/src/openai-assistants/utils/process-message-stream.ts +0 -25
  161. package/src/utils/fetch-chat-completion.ts +0 -120
  162. /package/dist/{chunk-B244LK6F.mjs.map → chunk-ATWK243A.mjs.map} +0 -0
  163. /package/dist/{chunk-S5LWO5V3.mjs.map → chunk-IBOL6LE5.mjs.map} +0 -0
  164. /package/dist/{chunk-MNZXQ6UH.mjs.map → chunk-WTGFKR3D.mjs.map} +0 -0
  165. /package/dist/{chunk-7GFKOIO7.mjs.map → chunk-YJLRG5U3.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
- }