@copilotkit/react-core 0.18.0-alpha.1 → 0.18.0-alpha.3
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.
- package/.turbo/turbo-build.log +184 -122
- package/CHANGELOG.md +25 -1
- package/dist/{chunk-6A4PCNMZ.mjs → chunk-4CFJ6A6A.mjs} +4 -10
- package/dist/chunk-4CFJ6A6A.mjs.map +1 -0
- package/dist/{chunk-FY7XNRJP.mjs → chunk-5GS7FQPW.mjs} +4 -4
- package/dist/chunk-5GS7FQPW.mjs.map +1 -0
- package/dist/{chunk-FP4EXCGS.mjs → chunk-DGUVNY3V.mjs} +2 -2
- package/dist/chunk-DGUVNY3V.mjs.map +1 -0
- package/dist/{chunk-D4NQCVHE.mjs → chunk-GBZZPQ5X.mjs} +11 -38
- package/dist/chunk-GBZZPQ5X.mjs.map +1 -0
- package/dist/chunk-IQEWY7P4.mjs +102 -0
- package/dist/chunk-IQEWY7P4.mjs.map +1 -0
- package/dist/{chunk-7P4OYNP3.mjs → chunk-IQH5AQQC.mjs} +2 -2
- package/dist/chunk-UAKLDQMP.mjs +61 -0
- package/dist/chunk-UAKLDQMP.mjs.map +1 -0
- package/dist/{chunk-P34OLW2N.mjs → chunk-VP2EU4US.mjs} +3 -3
- package/dist/{chunk-HW5IH6PV.mjs → chunk-YOXWT7I6.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit-props.d.ts +39 -0
- package/dist/components/copilot-provider/copilotkit-props.js +4 -0
- package/dist/components/copilot-provider/copilotkit-props.mjs +3 -0
- package/dist/components/copilot-provider/{copilot-provider.d.ts → copilotkit.d.ts} +9 -15
- package/dist/components/copilot-provider/copilotkit.js +426 -0
- package/dist/components/copilot-provider/copilotkit.js.map +1 -0
- package/dist/components/copilot-provider/copilotkit.mjs +8 -0
- package/dist/components/copilot-provider/index.d.ts +2 -8
- package/dist/components/copilot-provider/index.js +426 -0
- package/dist/components/copilot-provider/index.js.map +1 -0
- package/dist/components/copilot-provider/index.mjs +2 -2
- package/dist/components/copilot-provider/standard-copilot-api-config.d.ts +2 -4
- package/dist/components/copilot-provider/standard-copilot-api-config.js +15 -0
- package/dist/components/copilot-provider/standard-copilot-api-config.js.map +1 -0
- package/dist/components/index.d.ts +2 -8
- package/dist/components/index.js +426 -0
- package/dist/components/index.js.map +1 -0
- package/dist/components/index.mjs +2 -2
- package/dist/context/copilot-context.d.ts +3 -5
- package/dist/context/copilot-context.js +47 -0
- package/dist/context/copilot-context.js.map +1 -0
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +2 -4
- package/dist/context/index.js +47 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.d.ts +2 -4
- package/dist/hooks/index.js +358 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/index.mjs +8 -9
- package/dist/hooks/use-chat.d.ts +2 -4
- package/dist/hooks/use-chat.js +186 -0
- package/dist/hooks/use-chat.js.map +1 -0
- package/dist/hooks/use-chat.mjs +2 -3
- package/dist/hooks/use-copilot-chat.d.ts +2 -4
- package/dist/hooks/use-copilot-chat.js +313 -0
- package/dist/hooks/use-copilot-chat.js.map +1 -0
- package/dist/hooks/use-copilot-chat.mjs +5 -6
- package/dist/hooks/use-flat-category-store.js +72 -0
- package/dist/hooks/use-flat-category-store.js.map +1 -0
- package/dist/hooks/use-make-copilot-actionable.d.ts +1 -1
- package/dist/hooks/use-make-copilot-actionable.js +67 -0
- package/dist/hooks/use-make-copilot-actionable.js.map +1 -0
- package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
- package/dist/hooks/use-make-copilot-document-readable.js +61 -0
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -0
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/hooks/use-make-copilot-readable.js +61 -0
- package/dist/hooks/use-make-copilot-readable.js.map +1 -0
- package/dist/hooks/use-make-copilot-readable.mjs +2 -2
- package/dist/hooks/use-tree.js +155 -0
- package/dist/hooks/use-tree.js.map +1 -0
- package/dist/index.d.ts +4 -9
- package/dist/index.js +838 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +10 -13
- package/dist/openai-assistants/hooks/index.js +225 -0
- package/dist/openai-assistants/hooks/index.js.map +1 -0
- package/dist/openai-assistants/hooks/index.mjs +3 -3
- package/dist/openai-assistants/hooks/use-assistants.js +129 -0
- package/dist/openai-assistants/hooks/use-assistants.js.map +1 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +226 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -0
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +3 -3
- package/dist/openai-assistants/index.js +226 -0
- package/dist/openai-assistants/index.js.map +1 -0
- package/dist/openai-assistants/index.mjs +3 -3
- package/dist/openai-assistants/utils/index.js +49 -0
- package/dist/openai-assistants/utils/index.js.map +1 -0
- package/dist/openai-assistants/utils/process-message-stream.js +49 -0
- package/dist/openai-assistants/utils/process-message-stream.js.map +1 -0
- package/dist/types/document-pointer.js +4 -0
- package/dist/types/index.d.ts +0 -2
- package/dist/types/index.js +4 -0
- package/dist/types/system-message.js +4 -0
- package/dist/utils/fetch-chat-completion.d.ts +28 -0
- package/dist/utils/fetch-chat-completion.js +103 -0
- package/dist/utils/fetch-chat-completion.js.map +1 -0
- package/dist/utils/fetch-chat-completion.mjs +4 -0
- package/dist/utils/utils.js +4 -0
- package/dist/utils/utils.test.js +10 -0
- package/dist/utils/utils.test.js.map +1 -0
- package/package.json +10 -10
- package/src/components/copilot-provider/copilotkit-props.tsx +42 -0
- package/src/components/copilot-provider/{copilot-provider.tsx → copilotkit.tsx} +22 -57
- package/src/components/copilot-provider/index.ts +2 -6
- package/src/context/copilot-context.tsx +5 -13
- package/src/hooks/use-chat.ts +54 -72
- package/src/hooks/use-copilot-chat.ts +4 -4
- package/src/hooks/use-make-copilot-actionable.ts +1 -1
- package/src/index.tsx +6 -1
- package/src/types/index.ts +0 -3
- package/src/utils/fetch-chat-completion.ts +95 -0
- package/tsup.config.ts +1 -2
- package/dist/chunk-6A4PCNMZ.mjs.map +0 -1
- package/dist/chunk-6SOQYBNX.mjs +0 -44
- package/dist/chunk-6SOQYBNX.mjs.map +0 -1
- package/dist/chunk-D4NQCVHE.mjs.map +0 -1
- package/dist/chunk-FP4EXCGS.mjs.map +0 -1
- package/dist/chunk-FY7XNRJP.mjs.map +0 -1
- package/dist/chunk-LMZN4NSM.mjs +0 -113
- package/dist/chunk-LMZN4NSM.mjs.map +0 -1
- package/dist/chunk-PUFR5OAI.mjs +0 -3
- package/dist/chunk-TKXPMMND.mjs +0 -177
- package/dist/chunk-TKXPMMND.mjs.map +0 -1
- package/dist/chunk-VK25FIML.mjs +0 -144
- package/dist/chunk-VK25FIML.mjs.map +0 -1
- package/dist/components/copilot-provider/copilot-provider-props.d.ts +0 -50
- package/dist/components/copilot-provider/copilot-provider-props.mjs +0 -3
- package/dist/components/copilot-provider/copilot-provider.mjs +0 -8
- package/dist/openai/chat-completion-client.d.ts +0 -52
- package/dist/openai/chat-completion-client.mjs +0 -5
- package/dist/openai/chat-completion-stream.d.ts +0 -21
- package/dist/openai/chat-completion-stream.mjs +0 -5
- package/dist/openai/chat-completion-transport.d.ts +0 -38
- package/dist/openai/chat-completion-transport.mjs +0 -4
- package/dist/openai/index.d.ts +0 -10
- package/dist/openai/index.mjs +0 -6
- package/dist/types/annotated-function.d.ts +0 -24
- package/dist/types/annotated-function.mjs +0 -3
- package/dist/types/base.d.ts +0 -56
- package/dist/types/base.mjs +0 -3
- package/dist/types/base.mjs.map +0 -1
- package/src/components/copilot-provider/copilot-provider-props.tsx +0 -50
- package/src/openai/chat-completion-client.ts +0 -229
- package/src/openai/chat-completion-stream.ts +0 -54
- package/src/openai/chat-completion-transport.ts +0 -186
- package/src/openai/index.tsx +0 -5
- package/src/types/annotated-function.ts +0 -27
- package/src/types/base.ts +0 -61
- /package/dist/{chunk-7P4OYNP3.mjs.map → chunk-IQH5AQQC.mjs.map} +0 -0
- /package/dist/{chunk-P34OLW2N.mjs.map → chunk-VP2EU4US.mjs.map} +0 -0
- /package/dist/{chunk-HW5IH6PV.mjs.map → chunk-YOXWT7I6.mjs.map} +0 -0
- /package/dist/{chunk-PUFR5OAI.mjs.map → components/copilot-provider/copilotkit-props.js.map} +0 -0
- /package/dist/components/copilot-provider/{copilot-provider-props.mjs.map → copilotkit-props.mjs.map} +0 -0
- /package/dist/components/copilot-provider/{copilot-provider.mjs.map → copilotkit.mjs.map} +0 -0
- /package/dist/{openai/chat-completion-client.mjs.map → types/document-pointer.js.map} +0 -0
- /package/dist/{openai/chat-completion-stream.mjs.map → types/index.js.map} +0 -0
- /package/dist/{openai/chat-completion-transport.mjs.map → types/system-message.js.map} +0 -0
- /package/dist/{openai/index.mjs.map → utils/fetch-chat-completion.mjs.map} +0 -0
- /package/dist/{types/annotated-function.mjs.map → utils/utils.js.map} +0 -0
package/dist/types/base.mjs
DELETED
package/dist/types/base.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { ReactNode } from "react";
|
|
3
|
-
import { CopilotApiConfig } from "../../context/copilot-context";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Props for the CopilotProvider when using a chat API endpoint.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
export interface CopilotProviderApiEndpointProps {
|
|
10
|
-
/**
|
|
11
|
-
* The endpoint for the chat API.
|
|
12
|
-
*/
|
|
13
|
-
chatApiEndpoint: string;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* The endpoint for the chat API v2.
|
|
17
|
-
* If not provided, defaults to chatApiEndpoint + "/v2".
|
|
18
|
-
* This is used for the chat API v2.
|
|
19
|
-
* If you are not using the chat API v2, you can ignore this.
|
|
20
|
-
* @default chatApiEndpoint + "/v2"
|
|
21
|
-
* @optional
|
|
22
|
-
*/
|
|
23
|
-
chatApiEndpointV2?: string;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* The children to be rendered within the CopilotProvider.
|
|
27
|
-
*/
|
|
28
|
-
children: ReactNode;
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Props for the CopilotProvider when using a CopilotApiConfig.
|
|
32
|
-
*/
|
|
33
|
-
|
|
34
|
-
export interface CopilotProviderApiConfigProps {
|
|
35
|
-
/**
|
|
36
|
-
* The configuration for the Copilot API.
|
|
37
|
-
*/
|
|
38
|
-
chatApiConfig: CopilotApiConfig;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* The children to be rendered within the CopilotProvider.
|
|
42
|
-
*/
|
|
43
|
-
children: ReactNode;
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Props for the CopilotProvider component.
|
|
47
|
-
* Can be either CopilotProviderApiEndpointProps or CopilotProviderApiConfigProps.
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
export type CopilotProviderProps = CopilotProviderApiEndpointProps | CopilotProviderApiConfigProps;
|
|
@@ -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
|
-
}
|
package/src/openai/index.tsx
DELETED
|
@@ -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>;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/dist/{chunk-PUFR5OAI.mjs.map → components/copilot-provider/copilotkit-props.js.map}
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|