@copilotkit/react-core 0.16.0-alpha.3 → 0.17.0-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 (144) hide show
  1. package/.turbo/turbo-build.log +159 -125
  2. package/CHANGELOG.md +4 -36
  3. package/dist/components/copilot-provider/copilot-provider-props.d.ts +3 -3
  4. package/dist/components/copilot-provider/copilot-provider-props.mjs +1 -2
  5. package/dist/components/copilot-provider/copilot-provider-props.mjs.map +1 -1
  6. package/dist/components/copilot-provider/copilot-provider.d.ts +11 -6
  7. package/dist/components/copilot-provider/copilot-provider.mjs +468 -7
  8. package/dist/components/copilot-provider/copilot-provider.mjs.map +1 -1
  9. package/dist/components/copilot-provider/index.d.ts +3 -3
  10. package/dist/components/copilot-provider/index.mjs +467 -8
  11. package/dist/components/copilot-provider/index.mjs.map +1 -1
  12. package/dist/components/copilot-provider/{standard-cpilot-api-config.d.ts → standard-copilot-api-config.d.ts} +3 -3
  13. package/dist/{chunk-6N6X7K7T.mjs → components/copilot-provider/standard-copilot-api-config.mjs} +5 -5
  14. package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +1 -0
  15. package/dist/components/index.d.ts +3 -3
  16. package/dist/components/index.mjs +467 -9
  17. package/dist/components/index.mjs.map +1 -1
  18. package/dist/context/copilot-context.d.ts +6 -10
  19. package/dist/context/copilot-context.mjs +70 -3
  20. package/dist/context/copilot-context.mjs.map +1 -1
  21. package/dist/context/index.d.ts +4 -4
  22. package/dist/context/index.mjs +69 -4
  23. package/dist/context/index.mjs.map +1 -1
  24. package/dist/hooks/index.d.ts +6 -1
  25. package/dist/hooks/index.mjs +654 -14
  26. package/dist/hooks/index.mjs.map +1 -1
  27. package/dist/hooks/use-chat.d.ts +84 -0
  28. package/dist/hooks/use-chat.mjs +461 -0
  29. package/dist/hooks/use-chat.mjs.map +1 -0
  30. package/dist/hooks/use-copilot-chat.d.ts +10 -3
  31. package/dist/hooks/use-copilot-chat.mjs +599 -10
  32. package/dist/hooks/use-copilot-chat.mjs.map +1 -1
  33. package/dist/hooks/use-flat-category-store.mjs +68 -3
  34. package/dist/hooks/use-flat-category-store.mjs.map +1 -1
  35. package/dist/hooks/use-make-copilot-actionable.mjs +95 -4
  36. package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
  37. package/dist/hooks/use-make-copilot-document-readable.mjs +87 -4
  38. package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
  39. package/dist/hooks/use-make-copilot-readable.mjs +87 -4
  40. package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
  41. package/dist/hooks/use-tree.mjs +153 -3
  42. package/dist/hooks/use-tree.mjs.map +1 -1
  43. package/dist/index.d.ts +6 -2
  44. package/dist/index.mjs +1204 -21
  45. package/dist/index.mjs.map +1 -1
  46. package/dist/openai/chat-completion-client.d.ts +56 -0
  47. package/dist/openai/chat-completion-client.mjs +360 -0
  48. package/dist/openai/chat-completion-client.mjs.map +1 -0
  49. package/dist/openai/chat-completion-stream.d.ts +21 -0
  50. package/dist/openai/chat-completion-stream.mjs +221 -0
  51. package/dist/openai/chat-completion-stream.mjs.map +1 -0
  52. package/dist/openai/chat-completion-transport.d.ts +40 -0
  53. package/dist/openai/chat-completion-transport.mjs +181 -0
  54. package/dist/openai/chat-completion-transport.mjs.map +1 -0
  55. package/dist/openai/index.d.ts +10 -0
  56. package/dist/openai/index.mjs +221 -0
  57. package/dist/openai/index.mjs.map +1 -0
  58. package/dist/openai-assistants/hooks/index.mjs +235 -14
  59. package/dist/openai-assistants/hooks/index.mjs.map +1 -1
  60. package/dist/openai-assistants/hooks/use-assistants.mjs +52 -8
  61. package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -1
  62. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +236 -13
  63. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -1
  64. package/dist/openai-assistants/index.mjs +236 -15
  65. package/dist/openai-assistants/index.mjs.map +1 -1
  66. package/dist/openai-assistants/utils/index.mjs +46 -4
  67. package/dist/openai-assistants/utils/index.mjs.map +1 -1
  68. package/dist/openai-assistants/utils/process-message-stream.mjs +46 -3
  69. package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -1
  70. package/dist/types/annotated-function.mjs +0 -2
  71. package/dist/types/annotated-function.mjs.map +1 -1
  72. package/dist/types/base.d.ts +56 -0
  73. package/dist/types/base.mjs +1 -0
  74. package/dist/types/base.mjs.map +1 -0
  75. package/dist/types/document-pointer.mjs +0 -2
  76. package/dist/types/document-pointer.mjs.map +1 -1
  77. package/dist/types/index.d.ts +1 -0
  78. package/dist/types/index.mjs +0 -2
  79. package/dist/types/index.mjs.map +1 -1
  80. package/dist/types/message.d.ts +2 -0
  81. package/dist/types/message.mjs +1 -0
  82. package/dist/types/message.mjs.map +1 -0
  83. package/dist/utils/utils.mjs +0 -2
  84. package/dist/utils/utils.mjs.map +1 -1
  85. package/dist/utils/utils.test.mjs +0 -1
  86. package/dist/utils/utils.test.mjs.map +1 -1
  87. package/package.json +3 -3
  88. package/src/components/copilot-provider/copilot-provider.tsx +14 -9
  89. package/src/context/copilot-context.tsx +3 -14
  90. package/src/context/index.ts +0 -1
  91. package/src/hooks/index.ts +1 -0
  92. package/src/hooks/use-chat.ts +197 -0
  93. package/src/hooks/use-copilot-chat.ts +10 -22
  94. package/src/index.tsx +1 -0
  95. package/src/openai/chat-completion-client.ts +240 -0
  96. package/src/openai/chat-completion-stream.ts +56 -0
  97. package/src/openai/chat-completion-transport.ts +190 -0
  98. package/src/openai/index.tsx +5 -0
  99. package/src/openai-assistants/hooks/use-copilot-chat-v2.ts +2 -2
  100. package/src/types/base.ts +61 -0
  101. package/src/types/index.ts +1 -0
  102. package/src/types/message.ts +0 -0
  103. package/dist/chunk-3MAIWZNZ.mjs +0 -58
  104. package/dist/chunk-3MAIWZNZ.mjs.map +0 -1
  105. package/dist/chunk-45PUEKTG.mjs +0 -19
  106. package/dist/chunk-45PUEKTG.mjs.map +0 -1
  107. package/dist/chunk-6LKBKYRJ.mjs +0 -165
  108. package/dist/chunk-6LKBKYRJ.mjs.map +0 -1
  109. package/dist/chunk-6LNDDH6K.mjs +0 -19
  110. package/dist/chunk-6LNDDH6K.mjs.map +0 -1
  111. package/dist/chunk-6N6X7K7T.mjs.map +0 -1
  112. package/dist/chunk-7GFKOIO7.mjs +0 -3
  113. package/dist/chunk-7GFKOIO7.mjs.map +0 -1
  114. package/dist/chunk-BABVSMJR.mjs +0 -3
  115. package/dist/chunk-BABVSMJR.mjs.map +0 -1
  116. package/dist/chunk-E3P5YZO2.mjs +0 -27
  117. package/dist/chunk-E3P5YZO2.mjs.map +0 -1
  118. package/dist/chunk-EFZPSZWO.mjs +0 -3
  119. package/dist/chunk-EFZPSZWO.mjs.map +0 -1
  120. package/dist/chunk-FRAKUJWH.mjs +0 -3
  121. package/dist/chunk-FRAKUJWH.mjs.map +0 -1
  122. package/dist/chunk-JD7BAH7U.mjs +0 -3
  123. package/dist/chunk-JD7BAH7U.mjs.map +0 -1
  124. package/dist/chunk-JHJ7LUTD.mjs +0 -125
  125. package/dist/chunk-JHJ7LUTD.mjs.map +0 -1
  126. package/dist/chunk-MRXNTQOX.mjs +0 -55
  127. package/dist/chunk-MRXNTQOX.mjs.map +0 -1
  128. package/dist/chunk-MZ5UN3BY.mjs +0 -28
  129. package/dist/chunk-MZ5UN3BY.mjs.map +0 -1
  130. package/dist/chunk-OFRZZ5OF.mjs +0 -79
  131. package/dist/chunk-OFRZZ5OF.mjs.map +0 -1
  132. package/dist/chunk-QACD2U6P.mjs +0 -3
  133. package/dist/chunk-QACD2U6P.mjs.map +0 -1
  134. package/dist/chunk-SPCZTZCY.mjs +0 -3
  135. package/dist/chunk-SPCZTZCY.mjs.map +0 -1
  136. package/dist/chunk-VUY2K2DI.mjs +0 -135
  137. package/dist/chunk-VUY2K2DI.mjs.map +0 -1
  138. package/dist/chunk-YPSGKPDA.mjs +0 -3
  139. package/dist/chunk-YPSGKPDA.mjs.map +0 -1
  140. package/dist/chunk-YULKJPY3.mjs +0 -70
  141. package/dist/chunk-YULKJPY3.mjs.map +0 -1
  142. package/dist/components/copilot-provider/standard-cpilot-api-config.mjs +0 -4
  143. package/dist/components/copilot-provider/standard-cpilot-api-config.mjs.map +0 -1
  144. /package/src/components/copilot-provider/{standard-cpilot-api-config.tsx → standard-copilot-api-config.tsx} +0 -0
@@ -0,0 +1,240 @@
1
+ import EventEmitter from "eventemitter3";
2
+ import { Function, Message, Role } from "../types";
3
+ import {
4
+ ChatCompletionTransport,
5
+ ChatCompletionTransportFetchParams,
6
+ } from "./chat-completion-transport";
7
+
8
+ interface ChatCompletionClientConfiguration {
9
+ url: string;
10
+ model?: string;
11
+ }
12
+
13
+ interface ChatCompletionClientEvents {
14
+ content: string;
15
+ partial: [string, string];
16
+ error: any;
17
+ function: {
18
+ name: string;
19
+ arguments: any;
20
+ };
21
+ end: void;
22
+ }
23
+
24
+ export interface ChatCompletionChunk {
25
+ choices: {
26
+ delta: {
27
+ role: Role;
28
+ content?: string | null;
29
+ function_call?: {
30
+ name?: string;
31
+ arguments?: string;
32
+ };
33
+ };
34
+ }[];
35
+ }
36
+
37
+ const DEFAULT_MAX_TOKENS = 8192;
38
+
39
+ export class ChatCompletionClient extends EventEmitter<ChatCompletionClientEvents> {
40
+ private url: string;
41
+ private model?: string;
42
+
43
+ private chatCompletionTransport: ChatCompletionTransport | null = null;
44
+ private mode: "function" | "message" | null = null;
45
+ private functionCallName: string = "";
46
+ private functionCallArguments: string = "";
47
+
48
+ constructor(params: ChatCompletionClientConfiguration) {
49
+ super();
50
+ this.model = params.model;
51
+ this.url = params.url;
52
+ }
53
+
54
+ public async fetch(params: ChatCompletionTransportFetchParams) {
55
+ params = { ...params };
56
+ if (this.model && this.model in maxTokensByModel) {
57
+ params.maxTokens ||= maxTokensByModel[this.model];
58
+ } else {
59
+ params.maxTokens ||= DEFAULT_MAX_TOKENS;
60
+ }
61
+
62
+ params.functions ||= [];
63
+ params.model = this.model;
64
+ params.messages = this.buildPrompt(params);
65
+ return await this.runPrompt(params);
66
+ }
67
+
68
+ private buildPrompt(params: ChatCompletionTransportFetchParams): Message[] {
69
+ let maxTokens = params.maxTokens!;
70
+ const messages = params.messages!;
71
+ const functions = params.functions!;
72
+ const functionsNumTokens = countFunctionsTokens(functions);
73
+ if (functionsNumTokens > maxTokens) {
74
+ throw new Error(`Too many tokens in function calls: ${functionsNumTokens} > ${maxTokens}`);
75
+ }
76
+ maxTokens -= functionsNumTokens;
77
+
78
+ for (const message of messages) {
79
+ if (message.role === "system") {
80
+ const numTokens = this.countTokens(message);
81
+ maxTokens -= numTokens;
82
+
83
+ if (maxTokens < 0) {
84
+ throw new Error("Not enough tokens for system message.");
85
+ }
86
+ }
87
+ }
88
+
89
+ const result: Message[] = [];
90
+ let cutoff: boolean = false;
91
+
92
+ const reversedMessages = [...messages].reverse();
93
+ for (const message of reversedMessages) {
94
+ if (message.role === "system") {
95
+ result.unshift(message);
96
+ continue;
97
+ } else if (cutoff) {
98
+ continue;
99
+ }
100
+ let numTokens = this.countTokens(message);
101
+ if (maxTokens < numTokens) {
102
+ cutoff = true;
103
+ continue;
104
+ }
105
+ result.unshift(message);
106
+ maxTokens -= numTokens;
107
+ }
108
+
109
+ return result;
110
+ }
111
+
112
+ private async runPrompt(params: ChatCompletionTransportFetchParams): Promise<void> {
113
+ this.chatCompletionTransport = new ChatCompletionTransport({
114
+ url: this.url,
115
+ });
116
+
117
+ this.chatCompletionTransport.on("data", this.onData);
118
+ this.chatCompletionTransport.on("error", this.onError);
119
+ this.chatCompletionTransport.on("end", this.onEnd);
120
+
121
+ await this.chatCompletionTransport.fetch(params);
122
+ }
123
+
124
+ private onData = (data: ChatCompletionChunk) => {
125
+ // In case we are in a function call but the next message is not a function call, flush it.
126
+ if (this.mode === "function" && !data.choices[0].delta.function_call) {
127
+ const success = this.tryFlushFunctionCall();
128
+ if (!success) {
129
+ return;
130
+ }
131
+ }
132
+
133
+ this.mode = data.choices[0].delta.function_call ? "function" : "message";
134
+
135
+ if (this.mode === "message") {
136
+ // if we get a message, emit the content and return;
137
+
138
+ if (data.choices[0].delta.content) {
139
+ this.emit("content", data.choices[0].delta.content);
140
+ }
141
+
142
+ return;
143
+ } else if (this.mode === "function") {
144
+ // if we get a function call, we buffer the name and arguments, then emit a partial event.
145
+
146
+ if (data.choices[0].delta.function_call!.name) {
147
+ this.functionCallName = data.choices[0].delta.function_call!.name!;
148
+ }
149
+ if (data.choices[0].delta.function_call!.arguments) {
150
+ this.functionCallArguments += data.choices[0].delta.function_call!.arguments!;
151
+ }
152
+ this.emit("partial", this.functionCallName, this.functionCallArguments);
153
+
154
+ return;
155
+ }
156
+ };
157
+
158
+ private onError = (error: any) => {
159
+ this.emit("error", error);
160
+ this.cleanup();
161
+ };
162
+
163
+ private onEnd = () => {
164
+ if (this.mode === "function") {
165
+ const success = this.tryFlushFunctionCall();
166
+ if (!success) {
167
+ return;
168
+ }
169
+ }
170
+ this.emit("end");
171
+ this.cleanup();
172
+ };
173
+
174
+ private tryFlushFunctionCall(): boolean {
175
+ let args: any = null;
176
+ try {
177
+ args = JSON.parse(this.functionCallArguments);
178
+ } catch (error) {
179
+ this.emit("error", error);
180
+ this.cleanup();
181
+ return false;
182
+ }
183
+ this.emit("function", {
184
+ name: this.functionCallName,
185
+ arguments: args,
186
+ });
187
+ this.mode = null;
188
+ this.functionCallName = "";
189
+ this.functionCallArguments = "";
190
+ return true;
191
+ }
192
+
193
+ private cleanup() {
194
+ if (this.chatCompletionTransport) {
195
+ this.chatCompletionTransport.off("data", this.onData);
196
+ this.chatCompletionTransport.off("error", this.onError);
197
+ this.chatCompletionTransport.off("end", this.onEnd);
198
+ }
199
+ this.chatCompletionTransport = null;
200
+ this.mode = null;
201
+ this.functionCallName = "";
202
+ this.functionCallArguments = "";
203
+ }
204
+
205
+ public countTokens(message: Message): number {
206
+ if (message.content) {
207
+ return estimateTokens(message.content);
208
+ } else if (message.function_call) {
209
+ return estimateTokens(JSON.stringify(message.function_call));
210
+ }
211
+ return 0;
212
+ }
213
+ }
214
+
215
+ const maxTokensByModel: { [key: string]: number } = {
216
+ "gpt-3.5-turbo": 4097,
217
+ "gpt-3.5-turbo-16k": 16385,
218
+ "gpt-4": 8192,
219
+ "gpt-4-1106-preview": 8192,
220
+ "gpt-4-32k": 32768,
221
+ "gpt-3.5-turbo-0301": 4097,
222
+ "gpt-4-0314": 8192,
223
+ "gpt-4-32k-0314": 32768,
224
+ "gpt-3.5-turbo-0613": 4097,
225
+ "gpt-4-0613": 8192,
226
+ "gpt-4-32k-0613": 32768,
227
+ "gpt-3.5-turbo-16k-0613": 16385,
228
+ };
229
+
230
+ function estimateTokens(text: string): number {
231
+ return text.length / 3;
232
+ }
233
+
234
+ function countFunctionsTokens(functions: Function[]): number {
235
+ if (functions.length === 0) {
236
+ return 0;
237
+ }
238
+ const json = JSON.stringify(functions);
239
+ return estimateTokens(json);
240
+ }
@@ -0,0 +1,56 @@
1
+ import {
2
+ ChatCompletionTransport,
3
+ ChatCompletionTransportFetchParams,
4
+ } from "./chat-completion-transport";
5
+
6
+ export interface ChatCompletionStreamConfiguration {
7
+ url: string;
8
+ model?: string;
9
+ }
10
+
11
+ export class ChatCompletionStream {
12
+ private url: string;
13
+
14
+ constructor(params: ChatCompletionStreamConfiguration) {
15
+ this.url = params.url;
16
+ }
17
+
18
+ public async fetch(params: ChatCompletionTransportFetchParams): Promise<ReadableStream<string>> {
19
+ params = { ...params };
20
+ params.functions = undefined;
21
+
22
+ const transport = new ChatCompletionTransport({
23
+ url: this.url,
24
+ });
25
+
26
+ const cleanup = () => {
27
+ transport.off("data");
28
+ transport.off("end");
29
+ transport.off("error");
30
+ };
31
+
32
+ const stream = new ReadableStream<string>({
33
+ start: (controller) => {
34
+ transport.on("data", (data) => {
35
+ if (data.choices[0].delta.content) {
36
+ controller.enqueue(data.choices[0].delta.content);
37
+ }
38
+ });
39
+
40
+ transport.on("error", (error) => {
41
+ controller.error(error);
42
+ cleanup();
43
+ });
44
+
45
+ transport.on("end", () => {
46
+ controller.close();
47
+ cleanup();
48
+ });
49
+ },
50
+ });
51
+
52
+ transport.fetch(params);
53
+
54
+ return stream;
55
+ }
56
+ }
@@ -0,0 +1,190 @@
1
+ import EventEmitter from "eventemitter3";
2
+ import { Message, Function } from "../types";
3
+ import { CopilotApiConfig } from "../context";
4
+
5
+ export interface ChatCompletionTransportConfiguration {
6
+ url: string;
7
+ }
8
+
9
+ interface ChatCompletionTransportEvents {
10
+ end: void;
11
+ data: any;
12
+ error: any;
13
+ }
14
+
15
+ export interface ChatCompletionTransportFetchParams {
16
+ model?: string;
17
+ messages: Message[];
18
+ functions?: Function[];
19
+ temperature?: number;
20
+ maxTokens?: number;
21
+ headers?: Record<string, string> | Headers;
22
+ body?: object;
23
+ copilotConfig: CopilotApiConfig;
24
+ signal?: AbortSignal;
25
+ }
26
+
27
+ const DEFAULT_MODEL = "gpt-4-1106-preview";
28
+
29
+ export class ChatCompletionTransport extends EventEmitter<ChatCompletionTransportEvents> {
30
+ private buffer = new Uint8Array();
31
+ private bodyReader: ReadableStreamDefaultReader<Uint8Array> | null = null;
32
+ private url: string;
33
+
34
+ constructor({ url }: ChatCompletionTransportConfiguration) {
35
+ super();
36
+ this.url = url;
37
+ }
38
+
39
+ private async cleanup() {
40
+ if (this.bodyReader) {
41
+ try {
42
+ await this.bodyReader.cancel();
43
+ } catch (error) {
44
+ console.warn("Failed to cancel body reader:", error);
45
+ }
46
+ }
47
+ this.bodyReader = null;
48
+ this.buffer = new Uint8Array();
49
+ }
50
+
51
+ public async fetch({
52
+ model,
53
+ messages,
54
+ copilotConfig,
55
+ functions,
56
+ temperature,
57
+ headers,
58
+ body,
59
+ signal,
60
+ }: ChatCompletionTransportFetchParams): Promise<void> {
61
+ await this.cleanup();
62
+
63
+ temperature ||= 0.5;
64
+ functions ||= [];
65
+ model ||= DEFAULT_MODEL;
66
+
67
+ // clean up any extra properties from messages
68
+ const cleanedMessages = messages.map((message) => {
69
+ const { content, role, name, function_call } = message;
70
+ return { content, role, name, function_call };
71
+ });
72
+
73
+ try {
74
+ const response = await fetch(this.url, {
75
+ method: "POST",
76
+ headers: {
77
+ "Content-Type": "application/json",
78
+ ...copilotConfig.headers,
79
+ ...(headers ? { ...headers } : {}),
80
+ },
81
+ body: JSON.stringify({
82
+ model,
83
+ messages: cleanedMessages,
84
+ stream: true,
85
+ ...(functions.length ? { functions } : {}),
86
+ ...(temperature ? { temperature } : {}),
87
+ ...(functions.length != 0 ? { function_call: "auto" } : {}),
88
+ ...copilotConfig.body,
89
+ ...(body ? { ...body } : {}),
90
+ }),
91
+ signal,
92
+ });
93
+
94
+ if (!response.ok) {
95
+ try {
96
+ const errorText = await response.text();
97
+ await this.cleanup();
98
+ const msg = `Error ${response.status}: ${errorText}`;
99
+ this.emit("error", new Error(msg));
100
+ } catch (_error) {
101
+ await this.cleanup();
102
+ const msg = `Error ${response.status}: ${response.statusText}`;
103
+ this.emit("error", new Error(msg));
104
+ }
105
+ return;
106
+ }
107
+
108
+ if (response.body == null) {
109
+ await this.cleanup();
110
+ const msg = "Response body is null";
111
+ this.emit("error", new Error(msg));
112
+ return;
113
+ }
114
+
115
+ this.bodyReader = response.body.getReader();
116
+
117
+ await this.streamBody();
118
+ } catch (error) {
119
+ await this.cleanup();
120
+ this.emit("error", error);
121
+ return;
122
+ }
123
+ }
124
+
125
+ private async streamBody() {
126
+ while (true) {
127
+ try {
128
+ const { done, value } = await this.bodyReader!.read();
129
+
130
+ if (done) {
131
+ await this.cleanup();
132
+ this.emit("end");
133
+ return;
134
+ }
135
+
136
+ const shouldContinue = await this.processData(value);
137
+
138
+ if (!shouldContinue) {
139
+ return;
140
+ }
141
+ } catch (error) {
142
+ await this.cleanup();
143
+ this.emit("error", error);
144
+ return;
145
+ }
146
+ }
147
+ }
148
+
149
+ private async processData(data: Uint8Array): Promise<boolean> {
150
+ // Append new data to the temp buffer
151
+ const newBuffer = new Uint8Array(this.buffer.length + data.length);
152
+ newBuffer.set(this.buffer);
153
+ newBuffer.set(data, this.buffer.length);
154
+ this.buffer = newBuffer;
155
+
156
+ const dataString = new TextDecoder("utf-8").decode(this.buffer);
157
+
158
+ let lines = dataString.split("\n").filter((line) => line.trim() !== "");
159
+
160
+ // If the last line isn't complete, keep it in the buffer for next time
161
+ if (!dataString.endsWith("\n")) {
162
+ const lastLine = lines.pop() || "";
163
+ const remainingBytes = new TextEncoder().encode(lastLine);
164
+ this.buffer = new Uint8Array(remainingBytes);
165
+ } else {
166
+ this.buffer = new Uint8Array();
167
+ }
168
+
169
+ for (const line of lines) {
170
+ const cleanedLine = line.replace(/^data: /, "");
171
+
172
+ if (cleanedLine === "[DONE]") {
173
+ await this.cleanup();
174
+ this.emit("end");
175
+ return false;
176
+ }
177
+
178
+ let json;
179
+ try {
180
+ json = JSON.parse(cleanedLine);
181
+ } catch (error) {
182
+ console.error("Failed to parse JSON:", error);
183
+ continue;
184
+ }
185
+
186
+ this.emit("data", json);
187
+ }
188
+ return true;
189
+ }
190
+ }
@@ -0,0 +1,5 @@
1
+ export {
2
+ ChatCompletionStream,
3
+ type ChatCompletionStreamConfiguration,
4
+ } from "./chat-completion-stream";
5
+ export { type ChatCompletionTransportFetchParams } from "./chat-completion-transport";
@@ -1,7 +1,7 @@
1
1
  import { useContext, useMemo, useState } from "react";
2
2
  import { processMessageStream } from "../utils";
3
3
  import { Message, parseStreamPart } from "@copilotkit/shared";
4
- import { CopilotContext, copilotApiConfigExtrapolator } from "../../context";
4
+ import { CopilotContext } from "../../context";
5
5
  import { defaultCopilotContextCategories } from "../../components";
6
6
 
7
7
  export type AssistantStatus = "in_progress" | "awaiting_message";
@@ -87,7 +87,7 @@ export function useCopilotChatV2(options: UseCopilotChatOptionsV2): UseCopilotCh
87
87
 
88
88
  setInput("");
89
89
 
90
- const apiUrl = copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpointV2;
90
+ const apiUrl = copilotApiConfig.chatApiEndpointV2;
91
91
 
92
92
  const functions = getChatCompletionFunctionDescriptions();
93
93
 
@@ -0,0 +1,61 @@
1
+ export type Role = "system" | "user" | "assistant" | "function";
2
+
3
+ export interface Message {
4
+ id: string;
5
+ createdAt?: Date;
6
+ content: string;
7
+ role: Role;
8
+ /**
9
+ * If the message has a role of `function`, the `name` field is the name of the function.
10
+ * Otherwise, the name field should not be set.
11
+ */
12
+ name?: string;
13
+ /**
14
+ * If the assistant role makes a function call, the `function_call` field
15
+ * contains the function call name and arguments. Otherwise, the field should
16
+ * not be set.
17
+ */
18
+ function_call?: FunctionCall;
19
+ }
20
+
21
+ export interface FunctionCall {
22
+ /**
23
+ * The arguments to call the function with, as generated by the model in JSON
24
+ * format. Note that the model does not always generate valid JSON, and may
25
+ * hallucinate parameters not defined by your function schema. Validate the
26
+ * arguments in your code before calling your function.
27
+ */
28
+ arguments?: string;
29
+ /**
30
+ * The name of the function to call.
31
+ */
32
+ name?: string;
33
+ }
34
+
35
+ export interface Function {
36
+ /**
37
+ * The name of the function to be called. Must be a-z, A-Z, 0-9, or contain
38
+ * underscores and dashes, with a maximum length of 64.
39
+ */
40
+ name: string;
41
+ /**
42
+ * The parameters the functions accepts, described as a JSON Schema object. See the
43
+ * [guide](/docs/guides/gpt/function-calling) for examples, and the
44
+ * [JSON Schema reference](https://json-schema.org/understanding-json-schema/) for
45
+ * documentation about the format.
46
+ *
47
+ * To describe a function that accepts no parameters, provide the value
48
+ * `{"type": "object", "properties": {}}`.
49
+ */
50
+ parameters: Record<string, unknown>;
51
+ /**
52
+ * A description of what the function does, used by the model to choose when and
53
+ * how to call the function.
54
+ */
55
+ description?: string;
56
+ }
57
+
58
+ export type FunctionCallHandler = (
59
+ chatMessages: Message[],
60
+ functionCall: FunctionCall,
61
+ ) => Promise<void>;
@@ -1,3 +1,4 @@
1
1
  export type { AnnotatedFunctionArgument } from "./annotated-function";
2
2
  export type { AnnotatedFunction } from "./annotated-function";
3
3
  export type { DocumentPointer } from "./document-pointer";
4
+ export type { Role, Message, Function, FunctionCall, FunctionCallHandler } from "./base";
File without changes
@@ -1,58 +0,0 @@
1
- import './chunk-MRXNTQOX.mjs';
2
- import React from 'react';
3
-
4
- function copilotApiConfigExtrapolator(config) {
5
- return {
6
- get chatApiEndpoint() {
7
- return `${config.chatApiEndpoint}`;
8
- },
9
- get chatApiEndpointV2() {
10
- return `${config.chatApiEndpointV2}`;
11
- }
12
- };
13
- }
14
- var emptyCopilotContext = {
15
- entryPoints: {},
16
- setEntryPoint: () => {
17
- },
18
- removeEntryPoint: () => {
19
- },
20
- getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug(),
21
- getFunctionCallHandler: () => returnAndThrowInDebug(),
22
- getContextString: (documents, categories) => returnAndThrowInDebug(),
23
- addContext: () => "",
24
- removeContext: () => {
25
- },
26
- getDocumentsContext: (categories) => returnAndThrowInDebug(),
27
- addDocumentContext: () => returnAndThrowInDebug(),
28
- removeDocumentContext: () => {
29
- },
30
- copilotApiConfig: new class {
31
- get chatApiEndpoint() {
32
- throw new Error(
33
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
34
- );
35
- }
36
- get chatApiEndpointV2() {
37
- throw new Error(
38
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
39
- );
40
- }
41
- get headers() {
42
- return {};
43
- }
44
- get body() {
45
- return {};
46
- }
47
- }()
48
- };
49
- var CopilotContext = React.createContext(emptyCopilotContext);
50
- function returnAndThrowInDebug(value) {
51
- throw new Error(
52
- "Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!"
53
- );
54
- }
55
-
56
- export { CopilotContext, copilotApiConfigExtrapolator };
57
- //# sourceMappingURL=out.js.map
58
- //# sourceMappingURL=chunk-3MAIWZNZ.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/context/copilot-context.tsx"],"names":[],"mappings":";;;;;AAGA,OAAO,WAAW;AA6CX,SAAS,6BAA6B,QAA0B;AACrE,SAAO;AAAA,IACL,IAAI,kBAA0B;AAC5B,aAAO,GAAG,OAAO;AAAA,IACnB;AAAA,IACA,IAAI,oBAA4B;AAC9B,aAAO,GAAG,OAAO;AAAA,IACnB;AAAA,EACF;AACF;AAwBA,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAElE,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,qBAAqB,CAAC,eAAyB,sBAAsB,CAAC,CAAC;AAAA,EACvE,oBAAoB,MAAM,sBAAsB,EAAE;AAAA,EAClD,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAE9B,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA0B;AAC5B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,IACA,IAAI,oBAA4B;AAC9B,YAAM,IAAI;AAAA,QACR;AAAA,MACF;AAAA,IACF;AAAA,IACA,IAAI,UAAkC;AACpC,aAAO,CAAC;AAAA,IACV;AAAA,IACA,IAAI,OAA4B;AAC9B,aAAO,CAAC;AAAA,IACV;AAAA,EACF,EAAG;AACL;AAEO,IAAM,iBAAiB,MAAM,cAAoC,mBAAmB;AAE3F,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACA,SAAO;AACT","sourcesContent":["\"use client\";\n\nimport { FunctionCallHandler } from \"ai\";\nimport React from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the chat API v2.\n */\n chatApiEndpointV2: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\n}\n\nexport function copilotApiConfigExtrapolator(config: CopilotApiConfig) {\n return {\n get chatApiEndpoint(): string {\n return `${config.chatApiEndpoint}`;\n },\n get chatApiEndpointV2(): string {\n return `${config.chatApiEndpointV2}`;\n },\n };\n}\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, AnnotatedFunction<any[]>>;\n setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;\n removeEntryPoint: (id: string) => void;\n getChatCompletionFunctionDescriptions: () => ChatCompletionCreateParams.Function[];\n getFunctionCallHandler: () => FunctionCallHandler;\n\n // text context\n getContextString: (documents: DocumentPointer[], categories: string[]) => string;\n addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n\n // document context\n getDocumentsContext: (categories: string[]) => DocumentPointer[];\n addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;\n removeDocumentContext: (documentId: string) => void;\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get chatApiEndpointV2(): string {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n }\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\n \"Remember to wrap your app in a `<CopilotProvider> {...} </CopilotProvider>` !!!\",\n );\n return value;\n}\n"]}
@@ -1,19 +0,0 @@
1
- import { CopilotContext } from './chunk-3MAIWZNZ.mjs';
2
- import { useContext, useRef, useEffect } from 'react';
3
-
4
- function useMakeCopilotReadable(information, parentId, categories) {
5
- const { addContext, removeContext } = useContext(CopilotContext);
6
- const idRef = useRef();
7
- useEffect(() => {
8
- const id = addContext(information, parentId, categories);
9
- idRef.current = id;
10
- return () => {
11
- removeContext(id);
12
- };
13
- }, [information, parentId, addContext, removeContext]);
14
- return idRef.current;
15
- }
16
-
17
- export { useMakeCopilotReadable };
18
- //# sourceMappingURL=out.js.map
19
- //# sourceMappingURL=chunk-45PUEKTG.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-make-copilot-readable.ts"],"names":[],"mappings":";;;;;AAEA,SAAS,YAAY,WAAW,cAAc;AAUvC,SAAS,uBACd,aACA,UACA,YACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAI,WAAW,cAAc;AAC/D,QAAM,QAAQ,OAAe;AAE7B,YAAU,MAAM;AACd,UAAM,KAAK,WAAW,aAAa,UAAU,UAAU;AACvD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,oBAAc,EAAE;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,aAAa,UAAU,YAAY,aAAa,CAAC;AAErD,SAAO,MAAM;AACf","sourcesContent":["\"use client\";\n\nimport { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\n\n/**\n * Adds the given information to the Copilot context to make it readable by Copilot.\n * @param information - The information to be added to the Copilot context.\n * @param parentId - The ID of the parent context, if any.\n * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)\n * @returns The ID of the added context.\n */\nexport function useMakeCopilotReadable(\n information: string,\n parentId?: string,\n categories?: string[],\n): string | undefined {\n const { addContext, removeContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addContext(information, parentId, categories);\n idRef.current = id;\n\n return () => {\n removeContext(id);\n };\n }, [information, parentId, addContext, removeContext]);\n\n return idRef.current;\n}\n"]}