@copilotkit/react-core 0.18.0-alpha.1 → 0.18.0-alpha.2

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 (158) hide show
  1. package/.turbo/turbo-build.log +184 -122
  2. package/CHANGELOG.md +14 -1
  3. package/dist/{chunk-6A4PCNMZ.mjs → chunk-4CFJ6A6A.mjs} +4 -10
  4. package/dist/chunk-4CFJ6A6A.mjs.map +1 -0
  5. package/dist/{chunk-FY7XNRJP.mjs → chunk-5GS7FQPW.mjs} +4 -4
  6. package/dist/chunk-5GS7FQPW.mjs.map +1 -0
  7. package/dist/{chunk-FP4EXCGS.mjs → chunk-DGUVNY3V.mjs} +2 -2
  8. package/dist/chunk-DGUVNY3V.mjs.map +1 -0
  9. package/dist/{chunk-D4NQCVHE.mjs → chunk-GBZZPQ5X.mjs} +11 -38
  10. package/dist/chunk-GBZZPQ5X.mjs.map +1 -0
  11. package/dist/chunk-IQEWY7P4.mjs +102 -0
  12. package/dist/chunk-IQEWY7P4.mjs.map +1 -0
  13. package/dist/{chunk-7P4OYNP3.mjs → chunk-IQH5AQQC.mjs} +2 -2
  14. package/dist/chunk-UAKLDQMP.mjs +61 -0
  15. package/dist/chunk-UAKLDQMP.mjs.map +1 -0
  16. package/dist/{chunk-P34OLW2N.mjs → chunk-VP2EU4US.mjs} +3 -3
  17. package/dist/{chunk-HW5IH6PV.mjs → chunk-YOXWT7I6.mjs} +2 -2
  18. package/dist/components/copilot-provider/copilotkit-props.d.ts +39 -0
  19. package/dist/components/copilot-provider/copilotkit-props.js +4 -0
  20. package/dist/components/copilot-provider/copilotkit-props.mjs +3 -0
  21. package/dist/components/copilot-provider/{copilot-provider.d.ts → copilotkit.d.ts} +9 -15
  22. package/dist/components/copilot-provider/copilotkit.js +426 -0
  23. package/dist/components/copilot-provider/copilotkit.js.map +1 -0
  24. package/dist/components/copilot-provider/copilotkit.mjs +8 -0
  25. package/dist/components/copilot-provider/index.d.ts +2 -8
  26. package/dist/components/copilot-provider/index.js +426 -0
  27. package/dist/components/copilot-provider/index.js.map +1 -0
  28. package/dist/components/copilot-provider/index.mjs +2 -2
  29. package/dist/components/copilot-provider/standard-copilot-api-config.d.ts +2 -4
  30. package/dist/components/copilot-provider/standard-copilot-api-config.js +15 -0
  31. package/dist/components/copilot-provider/standard-copilot-api-config.js.map +1 -0
  32. package/dist/components/index.d.ts +2 -8
  33. package/dist/components/index.js +426 -0
  34. package/dist/components/index.js.map +1 -0
  35. package/dist/components/index.mjs +2 -2
  36. package/dist/context/copilot-context.d.ts +3 -5
  37. package/dist/context/copilot-context.js +47 -0
  38. package/dist/context/copilot-context.js.map +1 -0
  39. package/dist/context/copilot-context.mjs +1 -1
  40. package/dist/context/index.d.ts +2 -4
  41. package/dist/context/index.js +47 -0
  42. package/dist/context/index.js.map +1 -0
  43. package/dist/context/index.mjs +1 -1
  44. package/dist/hooks/index.d.ts +2 -4
  45. package/dist/hooks/index.js +358 -0
  46. package/dist/hooks/index.js.map +1 -0
  47. package/dist/hooks/index.mjs +8 -9
  48. package/dist/hooks/use-chat.d.ts +2 -4
  49. package/dist/hooks/use-chat.js +186 -0
  50. package/dist/hooks/use-chat.js.map +1 -0
  51. package/dist/hooks/use-chat.mjs +2 -3
  52. package/dist/hooks/use-copilot-chat.d.ts +2 -4
  53. package/dist/hooks/use-copilot-chat.js +313 -0
  54. package/dist/hooks/use-copilot-chat.js.map +1 -0
  55. package/dist/hooks/use-copilot-chat.mjs +5 -6
  56. package/dist/hooks/use-flat-category-store.js +72 -0
  57. package/dist/hooks/use-flat-category-store.js.map +1 -0
  58. package/dist/hooks/use-make-copilot-actionable.d.ts +1 -1
  59. package/dist/hooks/use-make-copilot-actionable.js +67 -0
  60. package/dist/hooks/use-make-copilot-actionable.js.map +1 -0
  61. package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
  62. package/dist/hooks/use-make-copilot-document-readable.js +61 -0
  63. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -0
  64. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  65. package/dist/hooks/use-make-copilot-readable.js +61 -0
  66. package/dist/hooks/use-make-copilot-readable.js.map +1 -0
  67. package/dist/hooks/use-make-copilot-readable.mjs +2 -2
  68. package/dist/hooks/use-tree.js +155 -0
  69. package/dist/hooks/use-tree.js.map +1 -0
  70. package/dist/index.d.ts +4 -9
  71. package/dist/index.js +838 -0
  72. package/dist/index.js.map +1 -0
  73. package/dist/index.mjs +10 -13
  74. package/dist/openai-assistants/hooks/index.js +225 -0
  75. package/dist/openai-assistants/hooks/index.js.map +1 -0
  76. package/dist/openai-assistants/hooks/index.mjs +3 -3
  77. package/dist/openai-assistants/hooks/use-assistants.js +129 -0
  78. package/dist/openai-assistants/hooks/use-assistants.js.map +1 -0
  79. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +226 -0
  80. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -0
  81. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +3 -3
  82. package/dist/openai-assistants/index.js +226 -0
  83. package/dist/openai-assistants/index.js.map +1 -0
  84. package/dist/openai-assistants/index.mjs +3 -3
  85. package/dist/openai-assistants/utils/index.js +49 -0
  86. package/dist/openai-assistants/utils/index.js.map +1 -0
  87. package/dist/openai-assistants/utils/process-message-stream.js +49 -0
  88. package/dist/openai-assistants/utils/process-message-stream.js.map +1 -0
  89. package/dist/types/document-pointer.js +4 -0
  90. package/dist/types/index.d.ts +0 -2
  91. package/dist/types/index.js +4 -0
  92. package/dist/types/system-message.js +4 -0
  93. package/dist/utils/fetch-chat-completion.d.ts +28 -0
  94. package/dist/utils/fetch-chat-completion.js +103 -0
  95. package/dist/utils/fetch-chat-completion.js.map +1 -0
  96. package/dist/utils/fetch-chat-completion.mjs +4 -0
  97. package/dist/utils/utils.js +4 -0
  98. package/dist/utils/utils.test.js +10 -0
  99. package/dist/utils/utils.test.js.map +1 -0
  100. package/package.json +8 -8
  101. package/src/components/copilot-provider/copilotkit-props.tsx +42 -0
  102. package/src/components/copilot-provider/{copilot-provider.tsx → copilotkit.tsx} +22 -57
  103. package/src/components/copilot-provider/index.ts +2 -6
  104. package/src/context/copilot-context.tsx +5 -13
  105. package/src/hooks/use-chat.ts +54 -72
  106. package/src/hooks/use-copilot-chat.ts +4 -4
  107. package/src/hooks/use-make-copilot-actionable.ts +1 -1
  108. package/src/index.tsx +6 -1
  109. package/src/types/index.ts +0 -3
  110. package/src/utils/fetch-chat-completion.ts +95 -0
  111. package/tsup.config.ts +1 -2
  112. package/dist/chunk-6A4PCNMZ.mjs.map +0 -1
  113. package/dist/chunk-6SOQYBNX.mjs +0 -44
  114. package/dist/chunk-6SOQYBNX.mjs.map +0 -1
  115. package/dist/chunk-D4NQCVHE.mjs.map +0 -1
  116. package/dist/chunk-FP4EXCGS.mjs.map +0 -1
  117. package/dist/chunk-FY7XNRJP.mjs.map +0 -1
  118. package/dist/chunk-LMZN4NSM.mjs +0 -113
  119. package/dist/chunk-LMZN4NSM.mjs.map +0 -1
  120. package/dist/chunk-PUFR5OAI.mjs +0 -3
  121. package/dist/chunk-TKXPMMND.mjs +0 -177
  122. package/dist/chunk-TKXPMMND.mjs.map +0 -1
  123. package/dist/chunk-VK25FIML.mjs +0 -144
  124. package/dist/chunk-VK25FIML.mjs.map +0 -1
  125. package/dist/components/copilot-provider/copilot-provider-props.d.ts +0 -50
  126. package/dist/components/copilot-provider/copilot-provider-props.mjs +0 -3
  127. package/dist/components/copilot-provider/copilot-provider.mjs +0 -8
  128. package/dist/openai/chat-completion-client.d.ts +0 -52
  129. package/dist/openai/chat-completion-client.mjs +0 -5
  130. package/dist/openai/chat-completion-stream.d.ts +0 -21
  131. package/dist/openai/chat-completion-stream.mjs +0 -5
  132. package/dist/openai/chat-completion-transport.d.ts +0 -38
  133. package/dist/openai/chat-completion-transport.mjs +0 -4
  134. package/dist/openai/index.d.ts +0 -10
  135. package/dist/openai/index.mjs +0 -6
  136. package/dist/types/annotated-function.d.ts +0 -24
  137. package/dist/types/annotated-function.mjs +0 -3
  138. package/dist/types/base.d.ts +0 -56
  139. package/dist/types/base.mjs +0 -3
  140. package/dist/types/base.mjs.map +0 -1
  141. package/src/components/copilot-provider/copilot-provider-props.tsx +0 -50
  142. package/src/openai/chat-completion-client.ts +0 -229
  143. package/src/openai/chat-completion-stream.ts +0 -54
  144. package/src/openai/chat-completion-transport.ts +0 -186
  145. package/src/openai/index.tsx +0 -5
  146. package/src/types/annotated-function.ts +0 -27
  147. package/src/types/base.ts +0 -61
  148. /package/dist/{chunk-7P4OYNP3.mjs.map → chunk-IQH5AQQC.mjs.map} +0 -0
  149. /package/dist/{chunk-P34OLW2N.mjs.map → chunk-VP2EU4US.mjs.map} +0 -0
  150. /package/dist/{chunk-HW5IH6PV.mjs.map → chunk-YOXWT7I6.mjs.map} +0 -0
  151. /package/dist/{chunk-PUFR5OAI.mjs.map → components/copilot-provider/copilotkit-props.js.map} +0 -0
  152. /package/dist/components/copilot-provider/{copilot-provider-props.mjs.map → copilotkit-props.mjs.map} +0 -0
  153. /package/dist/components/copilot-provider/{copilot-provider.mjs.map → copilotkit.mjs.map} +0 -0
  154. /package/dist/{openai/chat-completion-client.mjs.map → types/document-pointer.js.map} +0 -0
  155. /package/dist/{openai/chat-completion-stream.mjs.map → types/index.js.map} +0 -0
  156. /package/dist/{openai/chat-completion-transport.mjs.map → types/system-message.js.map} +0 -0
  157. /package/dist/{openai/index.mjs.map → utils/fetch-chat-completion.mjs.map} +0 -0
  158. /package/dist/{types/annotated-function.mjs.map → utils/utils.js.map} +0 -0
@@ -1,229 +0,0 @@
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
-
10
- interface ChatCompletionClientEvents {
11
- content: string;
12
- partial: [string, string];
13
- error: any;
14
- function: {
15
- name: string;
16
- arguments: any;
17
- };
18
- end: void;
19
- }
20
-
21
- export interface ChatCompletionChunk {
22
- choices: {
23
- delta: {
24
- role: Role;
25
- content?: string | null;
26
- function_call?: {
27
- name?: string;
28
- arguments?: string;
29
- };
30
- };
31
- }[];
32
- }
33
-
34
- const DEFAULT_MAX_TOKENS = 8192;
35
-
36
- export class ChatCompletionClient extends EventEmitter<ChatCompletionClientEvents> {
37
- private chatCompletionTransport: ChatCompletionTransport | null = null;
38
- private mode: "function" | "message" | null = null;
39
- private functionCallName: string = "";
40
- private functionCallArguments: string = "";
41
-
42
- constructor(params: ChatCompletionClientConfiguration) {
43
- super();
44
- }
45
-
46
- public async fetch(params: ChatCompletionTransportFetchParams) {
47
- params = { ...params };
48
- if (params.model && params.model in maxTokensByModel) {
49
- params.maxTokens ||= maxTokensByModel[params.model];
50
- } else {
51
- params.maxTokens ||= DEFAULT_MAX_TOKENS;
52
- }
53
-
54
- params.functions ||= [];
55
- params.messages = this.buildPrompt(params);
56
- return await this.runPrompt(params);
57
- }
58
-
59
- private buildPrompt(params: ChatCompletionTransportFetchParams): Message[] {
60
- let maxTokens = params.maxTokens!;
61
- const messages = params.messages!;
62
- const functions = params.functions!;
63
- const functionsNumTokens = countFunctionsTokens(functions);
64
- if (functionsNumTokens > maxTokens) {
65
- throw new Error(`Too many tokens in function calls: ${functionsNumTokens} > ${maxTokens}`);
66
- }
67
- maxTokens -= functionsNumTokens;
68
-
69
- for (const message of messages) {
70
- if (message.role === "system") {
71
- const numTokens = this.countTokens(message);
72
- maxTokens -= numTokens;
73
-
74
- if (maxTokens < 0) {
75
- throw new Error("Not enough tokens for system message.");
76
- }
77
- }
78
- }
79
-
80
- const result: Message[] = [];
81
- let cutoff: boolean = false;
82
-
83
- const reversedMessages = [...messages].reverse();
84
- for (const message of reversedMessages) {
85
- if (message.role === "system") {
86
- result.unshift(message);
87
- continue;
88
- } else if (cutoff) {
89
- continue;
90
- }
91
- let numTokens = this.countTokens(message);
92
- if (maxTokens < numTokens) {
93
- cutoff = true;
94
- continue;
95
- }
96
- result.unshift(message);
97
- maxTokens -= numTokens;
98
- }
99
-
100
- return result;
101
- }
102
-
103
- private async runPrompt(params: ChatCompletionTransportFetchParams): Promise<void> {
104
- this.chatCompletionTransport = new ChatCompletionTransport({});
105
-
106
- this.chatCompletionTransport.on("data", this.onData);
107
- this.chatCompletionTransport.on("error", this.onError);
108
- this.chatCompletionTransport.on("end", this.onEnd);
109
-
110
- await this.chatCompletionTransport.fetch(params);
111
- }
112
-
113
- private onData = (data: ChatCompletionChunk) => {
114
- // In case we are in a function call but the next message is not a function call, flush it.
115
- if (this.mode === "function" && !data.choices[0].delta.function_call) {
116
- const success = this.tryFlushFunctionCall();
117
- if (!success) {
118
- return;
119
- }
120
- }
121
-
122
- this.mode = data.choices[0].delta.function_call ? "function" : "message";
123
-
124
- if (this.mode === "message") {
125
- // if we get a message, emit the content and return;
126
-
127
- if (data.choices[0].delta.content) {
128
- this.emit("content", data.choices[0].delta.content);
129
- }
130
-
131
- return;
132
- } else if (this.mode === "function") {
133
- // if we get a function call, we buffer the name and arguments, then emit a partial event.
134
-
135
- if (data.choices[0].delta.function_call!.name) {
136
- this.functionCallName = data.choices[0].delta.function_call!.name!;
137
- }
138
- if (data.choices[0].delta.function_call!.arguments) {
139
- this.functionCallArguments += data.choices[0].delta.function_call!.arguments!;
140
- }
141
- this.emit("partial", this.functionCallName, this.functionCallArguments);
142
-
143
- return;
144
- }
145
- };
146
-
147
- private onError = (error: any) => {
148
- this.emit("error", error);
149
- this.cleanup();
150
- };
151
-
152
- private onEnd = () => {
153
- if (this.mode === "function") {
154
- const success = this.tryFlushFunctionCall();
155
- if (!success) {
156
- return;
157
- }
158
- }
159
- this.emit("end");
160
- this.cleanup();
161
- };
162
-
163
- private tryFlushFunctionCall(): boolean {
164
- let args: any = null;
165
- try {
166
- args = JSON.parse(this.functionCallArguments);
167
- } catch (error) {
168
- this.emit("error", error);
169
- this.cleanup();
170
- return false;
171
- }
172
- this.emit("function", {
173
- name: this.functionCallName,
174
- arguments: args,
175
- });
176
- this.mode = null;
177
- this.functionCallName = "";
178
- this.functionCallArguments = "";
179
- return true;
180
- }
181
-
182
- private cleanup() {
183
- if (this.chatCompletionTransport) {
184
- this.chatCompletionTransport.off("data", this.onData);
185
- this.chatCompletionTransport.off("error", this.onError);
186
- this.chatCompletionTransport.off("end", this.onEnd);
187
- }
188
- this.chatCompletionTransport = null;
189
- this.mode = null;
190
- this.functionCallName = "";
191
- this.functionCallArguments = "";
192
- }
193
-
194
- public countTokens(message: Message): number {
195
- if (message.content) {
196
- return estimateTokens(message.content);
197
- } else if (message.function_call) {
198
- return estimateTokens(JSON.stringify(message.function_call));
199
- }
200
- return 0;
201
- }
202
- }
203
-
204
- const maxTokensByModel: { [key: string]: number } = {
205
- "gpt-3.5-turbo": 4097,
206
- "gpt-3.5-turbo-16k": 16385,
207
- "gpt-4": 8192,
208
- "gpt-4-1106-preview": 8192,
209
- "gpt-4-32k": 32768,
210
- "gpt-3.5-turbo-0301": 4097,
211
- "gpt-4-0314": 8192,
212
- "gpt-4-32k-0314": 32768,
213
- "gpt-3.5-turbo-0613": 4097,
214
- "gpt-4-0613": 8192,
215
- "gpt-4-32k-0613": 32768,
216
- "gpt-3.5-turbo-16k-0613": 16385,
217
- };
218
-
219
- function estimateTokens(text: string): number {
220
- return text.length / 3;
221
- }
222
-
223
- function countFunctionsTokens(functions: Function[]): number {
224
- if (functions.length === 0) {
225
- return 0;
226
- }
227
- const json = JSON.stringify(functions);
228
- return estimateTokens(json);
229
- }
@@ -1,54 +0,0 @@
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
-
24
- const cleanup = () => {
25
- transport.off("data");
26
- transport.off("end");
27
- transport.off("error");
28
- };
29
-
30
- const stream = new ReadableStream<string>({
31
- start: (controller) => {
32
- transport.on("data", (data) => {
33
- if (data.choices[0].delta.content) {
34
- controller.enqueue(data.choices[0].delta.content);
35
- }
36
- });
37
-
38
- transport.on("error", (error) => {
39
- controller.error(error);
40
- cleanup();
41
- });
42
-
43
- transport.on("end", () => {
44
- controller.close();
45
- cleanup();
46
- });
47
- },
48
- });
49
-
50
- transport.fetch(params);
51
-
52
- return stream;
53
- }
54
- }
@@ -1,186 +0,0 @@
1
- import EventEmitter from "eventemitter3";
2
- import { Message, Function } from "../types";
3
- import { CopilotApiConfig } from "../context";
4
-
5
- export interface ChatCompletionTransportConfiguration {}
6
-
7
- interface ChatCompletionTransportEvents {
8
- end: void;
9
- data: any;
10
- error: any;
11
- }
12
-
13
- export interface ChatCompletionTransportFetchParams {
14
- copilotConfig: CopilotApiConfig;
15
- model?: string;
16
- messages: Message[];
17
- functions?: Function[];
18
- temperature?: number;
19
- maxTokens?: number;
20
- headers?: Record<string, string> | Headers;
21
- body?: object;
22
- signal?: AbortSignal;
23
- }
24
-
25
- const DEFAULT_MODEL = "gpt-4-1106-preview";
26
-
27
- export class ChatCompletionTransport extends EventEmitter<ChatCompletionTransportEvents> {
28
- private buffer = new Uint8Array();
29
- private bodyReader: ReadableStreamDefaultReader<Uint8Array> | null = null;
30
-
31
- constructor(params: ChatCompletionTransportConfiguration) {
32
- super();
33
- }
34
-
35
- private async cleanup() {
36
- if (this.bodyReader) {
37
- try {
38
- await this.bodyReader.cancel();
39
- } catch (error) {
40
- console.warn("Failed to cancel body reader:", error);
41
- }
42
- }
43
- this.bodyReader = null;
44
- this.buffer = new Uint8Array();
45
- }
46
-
47
- public async fetch({
48
- copilotConfig,
49
- model,
50
- messages,
51
- functions,
52
- temperature,
53
- headers,
54
- body,
55
- signal,
56
- }: ChatCompletionTransportFetchParams): Promise<void> {
57
- await this.cleanup();
58
-
59
- temperature ||= 0.5;
60
- functions ||= [];
61
- model ||= DEFAULT_MODEL;
62
-
63
- // clean up any extra properties from messages
64
- const cleanedMessages = messages.map((message) => {
65
- const { content, role, name, function_call } = message;
66
- return { content, role, name, function_call };
67
- });
68
-
69
- try {
70
- const response = await fetch(copilotConfig.chatApiEndpoint, {
71
- method: "POST",
72
- headers: {
73
- "Content-Type": "application/json",
74
- ...copilotConfig.headers,
75
- ...(headers ? { ...headers } : {}),
76
- },
77
- body: JSON.stringify({
78
- model,
79
- messages: cleanedMessages,
80
- stream: true,
81
- ...(functions.length ? { functions } : {}),
82
- ...(temperature ? { temperature } : {}),
83
- ...(functions.length != 0 ? { function_call: "auto" } : {}),
84
- ...copilotConfig.body,
85
- ...(body ? { ...body } : {}),
86
- }),
87
- signal,
88
- });
89
-
90
- if (!response.ok) {
91
- try {
92
- const errorText = await response.text();
93
- await this.cleanup();
94
- const msg = `Error ${response.status}: ${errorText}`;
95
- this.emit("error", new Error(msg));
96
- } catch (_error) {
97
- await this.cleanup();
98
- const msg = `Error ${response.status}: ${response.statusText}`;
99
- this.emit("error", new Error(msg));
100
- }
101
- return;
102
- }
103
-
104
- if (response.body == null) {
105
- await this.cleanup();
106
- const msg = "Response body is null";
107
- this.emit("error", new Error(msg));
108
- return;
109
- }
110
-
111
- this.bodyReader = response.body.getReader();
112
-
113
- await this.streamBody();
114
- } catch (error) {
115
- await this.cleanup();
116
- this.emit("error", error);
117
- return;
118
- }
119
- }
120
-
121
- private async streamBody() {
122
- while (true) {
123
- try {
124
- const { done, value } = await this.bodyReader!.read();
125
-
126
- if (done) {
127
- await this.cleanup();
128
- this.emit("end");
129
- return;
130
- }
131
-
132
- const shouldContinue = await this.processData(value);
133
-
134
- if (!shouldContinue) {
135
- return;
136
- }
137
- } catch (error) {
138
- await this.cleanup();
139
- this.emit("error", error);
140
- return;
141
- }
142
- }
143
- }
144
-
145
- private async processData(data: Uint8Array): Promise<boolean> {
146
- // Append new data to the temp buffer
147
- const newBuffer = new Uint8Array(this.buffer.length + data.length);
148
- newBuffer.set(this.buffer);
149
- newBuffer.set(data, this.buffer.length);
150
- this.buffer = newBuffer;
151
-
152
- const dataString = new TextDecoder("utf-8").decode(this.buffer);
153
-
154
- let lines = dataString.split("\n").filter((line) => line.trim() !== "");
155
-
156
- // If the last line isn't complete, keep it in the buffer for next time
157
- if (!dataString.endsWith("\n")) {
158
- const lastLine = lines.pop() || "";
159
- const remainingBytes = new TextEncoder().encode(lastLine);
160
- this.buffer = new Uint8Array(remainingBytes);
161
- } else {
162
- this.buffer = new Uint8Array();
163
- }
164
-
165
- for (const line of lines) {
166
- const cleanedLine = line.replace(/^data: /, "");
167
-
168
- if (cleanedLine === "[DONE]") {
169
- await this.cleanup();
170
- this.emit("end");
171
- return false;
172
- }
173
-
174
- let json;
175
- try {
176
- json = JSON.parse(cleanedLine);
177
- } catch (error) {
178
- console.error("Failed to parse JSON:", error);
179
- continue;
180
- }
181
-
182
- this.emit("data", json);
183
- }
184
- return true;
185
- }
186
- }
@@ -1,5 +0,0 @@
1
- export {
2
- ChatCompletionStream,
3
- type ChatCompletionStreamConfiguration,
4
- } from "./chat-completion-stream";
5
- export { type ChatCompletionTransportFetchParams } from "./chat-completion-transport";
@@ -1,27 +0,0 @@
1
- export interface AnnotatedFunctionSimpleArgument {
2
- name: string;
3
- type: "string" | "number" | "boolean" | "object"; // Add or change types according to your needs.
4
- description: string;
5
- required: boolean;
6
- }
7
-
8
- export interface AnnotatedFunctionArrayArgument {
9
- name: string;
10
- type: "array";
11
- items: {
12
- type: string;
13
- };
14
- description: string;
15
- required: boolean;
16
- }
17
-
18
- export type AnnotatedFunctionArgument =
19
- | AnnotatedFunctionSimpleArgument
20
- | AnnotatedFunctionArrayArgument;
21
-
22
- export interface AnnotatedFunction<Inputs extends any[]> {
23
- name: string;
24
- description: string;
25
- argumentAnnotations: AnnotatedFunctionArgument[];
26
- implementation: (...args: Inputs) => Promise<void>;
27
- }
package/src/types/base.ts DELETED
@@ -1,61 +0,0 @@
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>;