@copilotkit/react-core 0.16.0 → 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 (142) hide show
  1. package/.turbo/turbo-build.log +168 -134
  2. package/CHANGELOG.md +6 -0
  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 +3 -3
  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-copilot-api-config.d.ts +3 -3
  13. package/dist/components/copilot-provider/standard-copilot-api-config.mjs +12 -3
  14. package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +1 -1
  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 +4 -8
  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 +4 -4
  88. package/src/components/copilot-provider/copilot-provider.tsx +4 -5
  89. package/src/context/copilot-context.tsx +1 -12
  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-7GFKOIO7.mjs +0 -3
  104. package/dist/chunk-7GFKOIO7.mjs.map +0 -1
  105. package/dist/chunk-BABVSMJR.mjs +0 -3
  106. package/dist/chunk-BABVSMJR.mjs.map +0 -1
  107. package/dist/chunk-DE37LEZJ.mjs +0 -79
  108. package/dist/chunk-DE37LEZJ.mjs.map +0 -1
  109. package/dist/chunk-EFZPSZWO.mjs +0 -3
  110. package/dist/chunk-EFZPSZWO.mjs.map +0 -1
  111. package/dist/chunk-EV26IMLL.mjs +0 -165
  112. package/dist/chunk-EV26IMLL.mjs.map +0 -1
  113. package/dist/chunk-F2JIAPZQ.mjs +0 -13
  114. package/dist/chunk-F2JIAPZQ.mjs.map +0 -1
  115. package/dist/chunk-FRAKUJWH.mjs +0 -3
  116. package/dist/chunk-FRAKUJWH.mjs.map +0 -1
  117. package/dist/chunk-IF64NU27.mjs +0 -125
  118. package/dist/chunk-IF64NU27.mjs.map +0 -1
  119. package/dist/chunk-JD7BAH7U.mjs +0 -3
  120. package/dist/chunk-JD7BAH7U.mjs.map +0 -1
  121. package/dist/chunk-MRXNTQOX.mjs +0 -55
  122. package/dist/chunk-MRXNTQOX.mjs.map +0 -1
  123. package/dist/chunk-MZ5UN3BY.mjs +0 -28
  124. package/dist/chunk-MZ5UN3BY.mjs.map +0 -1
  125. package/dist/chunk-PF7LXYPO.mjs +0 -19
  126. package/dist/chunk-PF7LXYPO.mjs.map +0 -1
  127. package/dist/chunk-QACD2U6P.mjs +0 -3
  128. package/dist/chunk-QACD2U6P.mjs.map +0 -1
  129. package/dist/chunk-RFZQHCNS.mjs +0 -19
  130. package/dist/chunk-RFZQHCNS.mjs.map +0 -1
  131. package/dist/chunk-SPCZTZCY.mjs +0 -3
  132. package/dist/chunk-SPCZTZCY.mjs.map +0 -1
  133. package/dist/chunk-VUY2K2DI.mjs +0 -135
  134. package/dist/chunk-VUY2K2DI.mjs.map +0 -1
  135. package/dist/chunk-WL2MC3E2.mjs +0 -27
  136. package/dist/chunk-WL2MC3E2.mjs.map +0 -1
  137. package/dist/chunk-YGJFU4ZP.mjs +0 -58
  138. package/dist/chunk-YGJFU4ZP.mjs.map +0 -1
  139. package/dist/chunk-YPSGKPDA.mjs +0 -3
  140. package/dist/chunk-YPSGKPDA.mjs.map +0 -1
  141. package/dist/chunk-YULKJPY3.mjs +0 -70
  142. package/dist/chunk-YULKJPY3.mjs.map +0 -1
@@ -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,3 +0,0 @@
1
-
2
- //# sourceMappingURL=out.js.map
3
- //# sourceMappingURL=chunk-7GFKOIO7.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,3 +0,0 @@
1
-
2
- //# sourceMappingURL=out.js.map
3
- //# sourceMappingURL=chunk-BABVSMJR.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":""}
@@ -1,79 +0,0 @@
1
- import { defaultCopilotContextCategories } from './chunk-EV26IMLL.mjs';
2
- import { CopilotContext, copilotApiConfigExtrapolator } from './chunk-YGJFU4ZP.mjs';
3
- import { __objRest, __spreadProps, __spreadValues } from './chunk-MRXNTQOX.mjs';
4
- import { useContext, useMemo } from 'react';
5
- import { useChat } from 'ai/react';
6
-
7
- function useCopilotChat(_a) {
8
- var _b = _a, {
9
- makeSystemMessage
10
- } = _b, options = __objRest(_b, [
11
- "makeSystemMessage"
12
- ]);
13
- const {
14
- getContextString,
15
- getChatCompletionFunctionDescriptions,
16
- getFunctionCallHandler,
17
- copilotApiConfig
18
- } = useContext(CopilotContext);
19
- const systemMessage = useMemo(() => {
20
- const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
21
- const contextString = getContextString([], defaultCopilotContextCategories);
22
- return {
23
- id: "system",
24
- content: systemMessageMaker(contextString),
25
- role: "system"
26
- };
27
- }, [getContextString, makeSystemMessage]);
28
- const initialMessagesWithContext = [systemMessage].concat(options.initialMessages || []);
29
- const functionDescriptions = useMemo(() => {
30
- return getChatCompletionFunctionDescriptions();
31
- }, [getChatCompletionFunctionDescriptions]);
32
- const { messages, append, reload, stop, isLoading, input, setInput } = useChat(__spreadProps(__spreadValues({}, options), {
33
- api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,
34
- id: options.id,
35
- initialMessages: initialMessagesWithContext,
36
- experimental_onFunctionCall: getFunctionCallHandler(),
37
- headers: __spreadValues(__spreadValues({}, copilotApiConfig.headers), options.headers),
38
- body: __spreadValues(__spreadValues(__spreadValues({
39
- id: options.id
40
- }, functionDescriptions.length > 0 && { functions: functionDescriptions }), copilotApiConfig.body), options.body)
41
- }));
42
- const visibleMessages = messages.filter(
43
- (message) => message.role === "user" || message.role === "assistant"
44
- );
45
- return {
46
- visibleMessages,
47
- append,
48
- reload,
49
- stop,
50
- isLoading,
51
- input,
52
- setInput
53
- };
54
- }
55
- function defaultSystemMessage(contextString) {
56
- return `
57
- Please act as an efficient, competent, conscientious, and industrious professional assistant.
58
-
59
- 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.
60
- Always be polite and respectful, and prefer brevity over verbosity.
61
-
62
- The user has provided you with the following context:
63
- \`\`\`
64
- ${contextString}
65
- \`\`\`
66
-
67
- 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.
68
-
69
- Please assist them as best you can.
70
-
71
- 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.
72
-
73
- If you would like to call a function, call it without saying anything else.
74
- `;
75
- }
76
-
77
- export { defaultSystemMessage, useCopilotChat };
78
- //# sourceMappingURL=out.js.map
79
- //# sourceMappingURL=chunk-DE37LEZJ.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/hooks/use-copilot-chat.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;AAMpC,SAAS,eAAe;AAuBjB,SAAS,eAAe,IAGiB;AAHjB,eAC7B;AAAA;AAAA,EA9BF,IA6B+B,IAE1B,oBAF0B,IAE1B;AAAA,IADH;AAAA;AAGA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,WAAW,cAAc;AAE7B,QAAM,gBAAyB,QAAQ,MAAM;AAC3C,UAAM,qBAAqB,qBAAqB;AAChD,UAAM,gBAAgB,iBAAiB,CAAC,GAAG,+BAA+B;AAE1E,WAAO;AAAA,MACL,IAAI;AAAA,MACJ,SAAS,mBAAmB,aAAa;AAAA,MACzC,MAAM;AAAA,IACR;AAAA,EACF,GAAG,CAAC,kBAAkB,iBAAiB,CAAC;AAExC,QAAM,6BAA6B,CAAC,aAAa,EAAE,OAAO,QAAQ,mBAAmB,CAAC,CAAC;AAEvF,QAAM,uBAA8D,QAAQ,MAAM;AAChF,WAAO,sCAAsC;AAAA,EAC/C,GAAG,CAAC,qCAAqC,CAAC;AAE1C,QAAM,EAAE,UAAU,QAAQ,QAAQ,MAAM,WAAW,OAAO,SAAS,IAAI,QAAQ,iCAC1E,UAD0E;AAAA,IAE7E,KAAK,6BAA6B,gBAAgB,EAAE;AAAA,IACpD,IAAI,QAAQ;AAAA,IACZ,iBAAiB;AAAA,IACjB,6BAA6B,uBAAuB;AAAA,IACpD,SAAS,kCAAK,iBAAiB,UAAY,QAAQ;AAAA,IACnD,MAAM;AAAA,MACJ,IAAI,QAAQ;AAAA,OACR,qBAAqB,SAAS,KAAK,EAAE,WAAW,qBAAqB,IACtE,iBAAiB,OACjB,QAAQ;AAAA,EAEf,EAAC;AAED,QAAM,kBAAkB,SAAS;AAAA,IAC/B,CAAC,YAAY,QAAQ,SAAS,UAAU,QAAQ,SAAS;AAAA,EAC3D;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,SAAS,qBAAqB,eAA+B;AAClE,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWF","sourcesContent":["import { useMemo, useContext } from \"react\";\nimport {\n CopilotContext,\n CopilotContextParams,\n copilotApiConfigExtrapolator,\n} from \"../context/copilot-context\";\nimport { useChat } from \"ai/react\";\nimport { ChatRequestOptions, CreateMessage, Message } from \"ai\";\nimport { UseChatOptions } from \"ai\";\nimport { defaultCopilotContextCategories } from \"../components\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\n\nexport interface UseCopilotChatOptions extends UseChatOptions {\n makeSystemMessage?: (contextString: string) => string;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n append: (\n message: Message | CreateMessage,\n chatRequestOptions?: ChatRequestOptions,\n ) => Promise<string | null | undefined>;\n reload: (chatRequestOptions?: ChatRequestOptions) => Promise<string | null | undefined>;\n stop: () => void;\n isLoading: boolean;\n input: string;\n setInput: React.Dispatch<React.SetStateAction<string>>;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n ...options\n}: UseCopilotChatOptions): UseCopilotChatReturn {\n const {\n getContextString,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n copilotApiConfig,\n } = useContext(CopilotContext);\n\n const systemMessage: Message = useMemo(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n const contextString = getContextString([], defaultCopilotContextCategories); // TODO: make the context categories configurable\n\n return {\n id: \"system\",\n content: systemMessageMaker(contextString),\n role: \"system\",\n };\n }, [getContextString, makeSystemMessage]);\n\n const initialMessagesWithContext = [systemMessage].concat(options.initialMessages || []);\n\n const functionDescriptions: ChatCompletionCreateParams.Function[] = useMemo(() => {\n return getChatCompletionFunctionDescriptions();\n }, [getChatCompletionFunctionDescriptions]);\n\n const { messages, append, reload, stop, isLoading, input, setInput } = useChat({\n ...options,\n api: copilotApiConfigExtrapolator(copilotApiConfig).chatApiEndpoint,\n id: options.id,\n initialMessages: initialMessagesWithContext,\n experimental_onFunctionCall: getFunctionCallHandler(),\n headers: { ...copilotApiConfig.headers, ...options.headers },\n body: {\n id: options.id,\n ...(functionDescriptions.length > 0 && { functions: functionDescriptions }),\n ...copilotApiConfig.body,\n ...options.body,\n },\n });\n\n const visibleMessages = messages.filter(\n (message) => message.role === \"user\" || message.role === \"assistant\",\n );\n\n return {\n visibleMessages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n\nexport function defaultSystemMessage(contextString: string): string {\n return `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp 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.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.\n\nPlease assist them as best you can.\n\nYou 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.\n\nIf you would like to call a function, call it without saying anything else.\n`;\n}\n"]}
@@ -1,3 +0,0 @@
1
-
2
- //# sourceMappingURL=out.js.map
3
- //# sourceMappingURL=chunk-EFZPSZWO.mjs.map