@copilotkit/react-core 0.37.0 → 0.38.0-beta.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.
- package/.turbo/turbo-build.log +187 -234
- package/CHANGELOG.md +12 -0
- package/dist/{chunk-HUKLBIUK.mjs → chunk-5CKW6KE7.mjs} +13 -22
- package/dist/chunk-5CKW6KE7.mjs.map +1 -0
- package/dist/{chunk-S5LWO5V3.mjs → chunk-7GPIOOXB.mjs} +2 -2
- package/dist/{chunk-LLU5URI5.mjs → chunk-DDHJCFFV.mjs} +18 -33
- package/dist/chunk-DDHJCFFV.mjs.map +1 -0
- package/dist/{chunk-SR4RW4CU.mjs → chunk-IQCLNCGL.mjs} +6 -6
- package/dist/chunk-IQCLNCGL.mjs.map +1 -0
- package/dist/{chunk-DY63PD22.mjs → chunk-K6EV2CNB.mjs} +4 -5
- package/dist/chunk-K6EV2CNB.mjs.map +1 -0
- package/dist/{chunk-NSUYO6TP.mjs → chunk-LZVHW3T3.mjs} +9 -9
- package/dist/chunk-LZVHW3T3.mjs.map +1 -0
- package/dist/{chunk-Z5FB4WBL.mjs → chunk-NY6QSOU7.mjs} +43 -40
- package/dist/chunk-NY6QSOU7.mjs.map +1 -0
- package/dist/chunk-PHMHNAYC.mjs +192 -0
- package/dist/chunk-PHMHNAYC.mjs.map +1 -0
- package/dist/{chunk-B244LK6F.mjs → chunk-QNJQKKD4.mjs} +2 -2
- package/dist/{chunk-4OIVQMEM.mjs → chunk-WU3I3G3G.mjs} +60 -42
- package/dist/chunk-WU3I3G3G.mjs.map +1 -0
- package/dist/chunk-YJLRG5U3.mjs +1 -0
- package/dist/{chunk-MNZXQ6UH.mjs → chunk-YM2JV2YQ.mjs} +2 -2
- package/dist/components/copilot-provider/copilotkit-props.d.ts +2 -2
- package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.js +18 -33
- package/dist/components/copilot-provider/copilotkit.js.map +1 -1
- package/dist/components/copilot-provider/copilotkit.mjs +2 -2
- package/dist/components/copilot-provider/index.js +18 -33
- package/dist/components/copilot-provider/index.js.map +1 -1
- package/dist/components/copilot-provider/index.mjs +2 -2
- package/dist/components/index.js +18 -33
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +2 -2
- package/dist/context/copilot-context.d.ts +7 -18
- package/dist/context/copilot-context.js +3 -4
- package/dist/context/copilot-context.js.map +1 -1
- package/dist/context/copilot-context.mjs +1 -1
- package/dist/context/index.d.ts +1 -0
- package/dist/context/index.js +3 -4
- package/dist/context/index.js.map +1 -1
- package/dist/context/index.mjs +1 -1
- package/dist/hooks/index.d.ts +2 -1
- package/dist/hooks/index.js +140 -229
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +9 -10
- package/dist/hooks/use-chat.d.ts +25 -51
- package/dist/hooks/use-chat.js +109 -203
- package/dist/hooks/use-chat.js.map +1 -1
- package/dist/hooks/use-chat.mjs +1 -2
- package/dist/hooks/use-copilot-action.js +10 -11
- package/dist/hooks/use-copilot-action.js.map +1 -1
- package/dist/hooks/use-copilot-action.mjs +2 -2
- package/dist/hooks/use-copilot-chat.d.ts +1 -1
- package/dist/hooks/use-copilot-chat.js +123 -212
- package/dist/hooks/use-copilot-chat.js.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +4 -5
- package/dist/hooks/use-copilot-readable.js +3 -4
- package/dist/hooks/use-copilot-readable.js.map +1 -1
- package/dist/hooks/use-copilot-readable.mjs +2 -2
- package/dist/hooks/use-make-copilot-actionable.js +7 -8
- package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
- package/dist/hooks/use-make-copilot-document-readable.js +3 -4
- package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
- package/dist/hooks/use-make-copilot-readable.js +3 -4
- package/dist/hooks/use-make-copilot-readable.js.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +2 -2
- package/dist/index.d.ts +1 -3
- package/dist/index.js +239 -497
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +12 -31
- package/dist/lib/copilot-task.d.ts +1 -0
- package/dist/lib/copilot-task.js +34 -110
- package/dist/lib/copilot-task.js.map +1 -1
- package/dist/lib/copilot-task.mjs +3 -4
- package/dist/lib/index.d.ts +1 -0
- package/dist/lib/index.js +36 -110
- package/dist/lib/index.js.map +1 -1
- package/dist/lib/index.mjs +3 -4
- package/dist/utils/extract.d.ts +1 -0
- package/dist/utils/extract.js +53 -109
- package/dist/utils/extract.js.map +1 -1
- package/dist/utils/extract.mjs +3 -4
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +56 -132
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/index.mjs +5 -13
- package/package.json +6 -5
- package/src/components/copilot-provider/copilotkit-props.tsx +2 -2
- package/src/components/copilot-provider/copilotkit.tsx +16 -37
- package/src/context/copilot-context.tsx +11 -30
- package/src/hooks/use-chat.ts +179 -208
- package/src/hooks/use-copilot-action.ts +7 -8
- package/src/hooks/use-copilot-chat.ts +9 -23
- package/src/hooks/use-make-copilot-actionable.ts +4 -4
- package/src/index.tsx +0 -1
- package/src/lib/copilot-task.ts +45 -42
- package/src/utils/extract.ts +64 -36
- package/src/utils/index.ts +0 -7
- package/dist/chunk-2EQGN5QK.mjs +0 -137
- package/dist/chunk-2EQGN5QK.mjs.map +0 -1
- package/dist/chunk-4OIVQMEM.mjs.map +0 -1
- package/dist/chunk-7GFKOIO7.mjs +0 -1
- package/dist/chunk-B2H3NC4E.mjs +0 -204
- package/dist/chunk-B2H3NC4E.mjs.map +0 -1
- package/dist/chunk-BABVSMJR.mjs +0 -1
- package/dist/chunk-BABVSMJR.mjs.map +0 -1
- package/dist/chunk-CYDWEPFL.mjs +0 -1
- package/dist/chunk-CYDWEPFL.mjs.map +0 -1
- package/dist/chunk-DY63PD22.mjs.map +0 -1
- package/dist/chunk-FRAKUJWH.mjs +0 -1
- package/dist/chunk-FRAKUJWH.mjs.map +0 -1
- package/dist/chunk-HUKLBIUK.mjs.map +0 -1
- package/dist/chunk-LLU5URI5.mjs.map +0 -1
- package/dist/chunk-MJKBCG4U.mjs +0 -91
- package/dist/chunk-MJKBCG4U.mjs.map +0 -1
- package/dist/chunk-NSUYO6TP.mjs.map +0 -1
- package/dist/chunk-SR4RW4CU.mjs.map +0 -1
- package/dist/chunk-Z5FB4WBL.mjs.map +0 -1
- package/dist/chunk-ZFS5SQUT.mjs +0 -31
- package/dist/chunk-ZFS5SQUT.mjs.map +0 -1
- package/dist/openai-assistants/hooks/index.d.ts +0 -2
- package/dist/openai-assistants/hooks/index.js +0 -277
- package/dist/openai-assistants/hooks/index.js.map +0 -1
- package/dist/openai-assistants/hooks/index.mjs +0 -18
- package/dist/openai-assistants/hooks/index.mjs.map +0 -1
- package/dist/openai-assistants/hooks/use-assistants.d.ts +0 -17
- package/dist/openai-assistants/hooks/use-assistants.js +0 -154
- package/dist/openai-assistants/hooks/use-assistants.js.map +0 -1
- package/dist/openai-assistants/hooks/use-assistants.mjs +0 -92
- package/dist/openai-assistants/hooks/use-assistants.mjs.map +0 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.d.ts +0 -44
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +0 -277
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +0 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +0 -19
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +0 -1
- package/dist/openai-assistants/index.d.ts +0 -3
- package/dist/openai-assistants/index.js +0 -279
- package/dist/openai-assistants/index.js.map +0 -1
- package/dist/openai-assistants/index.mjs +0 -22
- package/dist/openai-assistants/index.mjs.map +0 -1
- package/dist/openai-assistants/utils/index.d.ts +0 -1
- package/dist/openai-assistants/utils/index.js +0 -73
- package/dist/openai-assistants/utils/index.js.map +0 -1
- package/dist/openai-assistants/utils/index.mjs +0 -9
- package/dist/openai-assistants/utils/index.mjs.map +0 -1
- package/dist/openai-assistants/utils/process-message-stream.d.ts +0 -3
- package/dist/openai-assistants/utils/process-message-stream.js +0 -71
- package/dist/openai-assistants/utils/process-message-stream.js.map +0 -1
- package/dist/openai-assistants/utils/process-message-stream.mjs +0 -8
- package/dist/openai-assistants/utils/process-message-stream.mjs.map +0 -1
- package/dist/utils/fetch-chat-completion.d.ts +0 -36
- package/dist/utils/fetch-chat-completion.js +0 -141
- package/dist/utils/fetch-chat-completion.js.map +0 -1
- package/dist/utils/fetch-chat-completion.mjs +0 -12
- package/dist/utils/fetch-chat-completion.mjs.map +0 -1
- package/src/openai-assistants/hooks/index.ts +0 -9
- package/src/openai-assistants/hooks/use-assistants.ts +0 -112
- package/src/openai-assistants/hooks/use-copilot-chat-v2.ts +0 -189
- package/src/openai-assistants/index.ts +0 -2
- package/src/openai-assistants/utils/index.ts +0 -1
- package/src/openai-assistants/utils/process-message-stream.ts +0 -25
- package/src/utils/fetch-chat-completion.ts +0 -120
- /package/dist/{chunk-S5LWO5V3.mjs.map → chunk-7GPIOOXB.mjs.map} +0 -0
- /package/dist/{chunk-B244LK6F.mjs.map → chunk-QNJQKKD4.mjs.map} +0 -0
- /package/dist/{chunk-7GFKOIO7.mjs.map → chunk-YJLRG5U3.mjs.map} +0 -0
- /package/dist/{chunk-MNZXQ6UH.mjs.map → chunk-YM2JV2YQ.mjs.map} +0 -0
package/src/lib/copilot-task.ts
CHANGED
|
@@ -89,11 +89,20 @@
|
|
|
89
89
|
* ```
|
|
90
90
|
*/
|
|
91
91
|
|
|
92
|
-
import {
|
|
92
|
+
import {
|
|
93
|
+
ActionExecutionMessage,
|
|
94
|
+
CopilotRuntimeClient,
|
|
95
|
+
Message,
|
|
96
|
+
Role,
|
|
97
|
+
TextMessage,
|
|
98
|
+
convertGqlOutputToMessages,
|
|
99
|
+
convertMessagesToGqlInput,
|
|
100
|
+
} from "@copilotkit/runtime-client-gql";
|
|
93
101
|
import { FrontendAction } from "../types/frontend-action";
|
|
94
102
|
import { CopilotContextParams } from "../context";
|
|
95
103
|
import { defaultCopilotContextCategories } from "../components";
|
|
96
|
-
import {
|
|
104
|
+
import { MessageStatusCode } from "@copilotkit/runtime-client-gql";
|
|
105
|
+
import { actionParametersToJsonSchema } from "@copilotkit/shared";
|
|
97
106
|
|
|
98
107
|
export interface CopilotTaskConfig {
|
|
99
108
|
/**
|
|
@@ -141,11 +150,11 @@ export class CopilotTask<T = any> {
|
|
|
141
150
|
* @param data The data to use for the task.
|
|
142
151
|
*/
|
|
143
152
|
async run(context: CopilotContextParams, data?: T): Promise<void> {
|
|
144
|
-
const
|
|
153
|
+
const actions = this.includeCopilotActions ? Object.assign({}, context.actions) : {};
|
|
145
154
|
|
|
146
155
|
// merge functions into entry points
|
|
147
156
|
for (const fn of this.actions) {
|
|
148
|
-
|
|
157
|
+
actions[fn.name] = fn;
|
|
149
158
|
}
|
|
150
159
|
|
|
151
160
|
let contextString = "";
|
|
@@ -158,52 +167,46 @@ export class CopilotTask<T = any> {
|
|
|
158
167
|
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
159
168
|
}
|
|
160
169
|
|
|
161
|
-
const systemMessage
|
|
162
|
-
id: "system",
|
|
170
|
+
const systemMessage = new TextMessage({
|
|
163
171
|
content: taskSystemMessage(contextString, this.instructions),
|
|
164
|
-
role:
|
|
165
|
-
};
|
|
172
|
+
role: Role.System,
|
|
173
|
+
});
|
|
166
174
|
|
|
167
|
-
const messages = [systemMessage];
|
|
175
|
+
const messages: Message[] = [systemMessage];
|
|
168
176
|
|
|
169
|
-
const
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
tools: context.getChatCompletionFunctionDescriptions(entryPoints),
|
|
177
|
+
const runtimeClient = new CopilotRuntimeClient({
|
|
178
|
+
url: context.copilotApiConfig.chatApiEndpoint,
|
|
179
|
+
publicApiKey: context.copilotApiConfig.publicApiKey,
|
|
173
180
|
headers: context.copilotApiConfig.headers,
|
|
174
|
-
body: context.copilotApiConfig.body,
|
|
175
181
|
});
|
|
176
182
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (!functionCalls.length) {
|
|
201
|
-
throw new Error("No function call occurred");
|
|
202
|
-
}
|
|
183
|
+
const response = await runtimeClient
|
|
184
|
+
.generateCopilotResponse(
|
|
185
|
+
{
|
|
186
|
+
frontend: {
|
|
187
|
+
actions: Object.values(actions).map((action) => ({
|
|
188
|
+
name: action.name,
|
|
189
|
+
description: action.description || "",
|
|
190
|
+
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters || [])),
|
|
191
|
+
})),
|
|
192
|
+
},
|
|
193
|
+
messages: convertMessagesToGqlInput(messages),
|
|
194
|
+
},
|
|
195
|
+
context.copilotApiConfig.properties,
|
|
196
|
+
)
|
|
197
|
+
.toPromise();
|
|
198
|
+
|
|
199
|
+
const functionCallHandler = context.getFunctionCallHandler(actions);
|
|
200
|
+
const functionCalls = convertGqlOutputToMessages(
|
|
201
|
+
response.data?.generateCopilotResponse?.messages || [],
|
|
202
|
+
).filter((m): m is ActionExecutionMessage => m instanceof ActionExecutionMessage);
|
|
203
203
|
|
|
204
|
-
const functionCallHandler = context.getFunctionCallHandler(entryPoints);
|
|
205
204
|
for (const functionCall of functionCalls) {
|
|
206
|
-
await functionCallHandler(
|
|
205
|
+
await functionCallHandler({
|
|
206
|
+
messages,
|
|
207
|
+
name: functionCall.name,
|
|
208
|
+
args: functionCall.arguments,
|
|
209
|
+
});
|
|
207
210
|
}
|
|
208
211
|
}
|
|
209
212
|
}
|
package/src/utils/extract.ts
CHANGED
|
@@ -2,13 +2,20 @@ import {
|
|
|
2
2
|
Action,
|
|
3
3
|
COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,
|
|
4
4
|
MappedParameterTypes,
|
|
5
|
-
Message,
|
|
6
5
|
Parameter,
|
|
6
|
+
actionParametersToJsonSchema,
|
|
7
7
|
} from "@copilotkit/shared";
|
|
8
|
+
import {
|
|
9
|
+
ActionExecutionMessage,
|
|
10
|
+
Message,
|
|
11
|
+
Role,
|
|
12
|
+
TextMessage,
|
|
13
|
+
convertGqlOutputToMessages,
|
|
14
|
+
} from "@copilotkit/runtime-client-gql";
|
|
8
15
|
import { CopilotContextParams } from "../context";
|
|
9
16
|
import { defaultCopilotContextCategories } from "../components";
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
17
|
+
import { CopilotRuntimeClient } from "@copilotkit/runtime-client-gql";
|
|
18
|
+
import { convertMessagesToGqlInput } from "@copilotkit/runtime-client-gql";
|
|
12
19
|
|
|
13
20
|
interface InitialState<T extends Parameter[] | [] = []> {
|
|
14
21
|
status: "initial";
|
|
@@ -75,11 +82,10 @@ export async function extract<const T extends Parameter[]>({
|
|
|
75
82
|
contextString += context.getContextString([], defaultCopilotContextCategories);
|
|
76
83
|
}
|
|
77
84
|
|
|
78
|
-
const systemMessage: Message = {
|
|
79
|
-
id: "system",
|
|
85
|
+
const systemMessage: Message = new TextMessage({
|
|
80
86
|
content: makeSystemMessage(contextString, instructions),
|
|
81
|
-
role:
|
|
82
|
-
};
|
|
87
|
+
role: Role.System,
|
|
88
|
+
});
|
|
83
89
|
|
|
84
90
|
const headers = {
|
|
85
91
|
...(context.copilotApiConfig.headers || {}),
|
|
@@ -88,23 +94,40 @@ export async function extract<const T extends Parameter[]>({
|
|
|
88
94
|
: {}),
|
|
89
95
|
};
|
|
90
96
|
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
tools: context.getChatCompletionFunctionDescriptions({ extract: action }),
|
|
97
|
+
const runtimeClient = new CopilotRuntimeClient({
|
|
98
|
+
url: context.copilotApiConfig.chatApiEndpoint,
|
|
99
|
+
publicApiKey: context.copilotApiConfig.publicApiKey,
|
|
95
100
|
headers,
|
|
96
|
-
body: context.copilotApiConfig.body,
|
|
97
|
-
toolChoice: { type: "function", function: { name: "extract" } },
|
|
98
|
-
signal: abortSignal,
|
|
99
101
|
});
|
|
100
102
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
103
|
+
const response = CopilotRuntimeClient.asStream(
|
|
104
|
+
runtimeClient.generateCopilotResponse(
|
|
105
|
+
{
|
|
106
|
+
frontend: {
|
|
107
|
+
actions: [
|
|
108
|
+
{
|
|
109
|
+
name: action.name,
|
|
110
|
+
description: action.description || "",
|
|
111
|
+
jsonSchema: JSON.stringify(actionParametersToJsonSchema(action.parameters || [])),
|
|
112
|
+
},
|
|
113
|
+
],
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
messages: convertMessagesToGqlInput(
|
|
117
|
+
includeMessages ? [systemMessage, ...messages] : [systemMessage],
|
|
118
|
+
),
|
|
119
|
+
},
|
|
120
|
+
context.copilotApiConfig.properties,
|
|
121
|
+
abortSignal,
|
|
122
|
+
),
|
|
123
|
+
);
|
|
124
|
+
|
|
125
|
+
const reader = response.getReader();
|
|
104
126
|
|
|
105
|
-
const reader = response.events.getReader();
|
|
106
127
|
let isInitial = true;
|
|
107
128
|
|
|
129
|
+
let actionExecutionMessage: ActionExecutionMessage | undefined = undefined;
|
|
130
|
+
|
|
108
131
|
while (true) {
|
|
109
132
|
const { done, value } = await reader.read();
|
|
110
133
|
|
|
@@ -112,27 +135,32 @@ export async function extract<const T extends Parameter[]>({
|
|
|
112
135
|
break;
|
|
113
136
|
}
|
|
114
137
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
stream?.({
|
|
119
|
-
status: isInitial ? "initial" : "inProgress",
|
|
120
|
-
args: partialArguments as Partial<MappedParameterTypes<T>>,
|
|
121
|
-
});
|
|
122
|
-
isInitial = false;
|
|
123
|
-
} catch (e) {}
|
|
124
|
-
}
|
|
138
|
+
actionExecutionMessage = convertGqlOutputToMessages(
|
|
139
|
+
value.generateCopilotResponse.messages,
|
|
140
|
+
).find((msg) => msg instanceof ActionExecutionMessage) as ActionExecutionMessage | undefined;
|
|
125
141
|
|
|
126
|
-
if (
|
|
127
|
-
|
|
128
|
-
status: "complete",
|
|
129
|
-
args: value.arguments as MappedParameterTypes<T>,
|
|
130
|
-
});
|
|
131
|
-
return value.arguments as MappedParameterTypes<T>;
|
|
142
|
+
if (!actionExecutionMessage) {
|
|
143
|
+
continue;
|
|
132
144
|
}
|
|
145
|
+
|
|
146
|
+
stream?.({
|
|
147
|
+
status: isInitial ? "initial" : "inProgress",
|
|
148
|
+
args: actionExecutionMessage.arguments as Partial<MappedParameterTypes<T>>,
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
isInitial = false;
|
|
133
152
|
}
|
|
134
153
|
|
|
135
|
-
|
|
154
|
+
if (!actionExecutionMessage) {
|
|
155
|
+
throw new Error("extract() failed: No function call occurred");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
stream?.({
|
|
159
|
+
status: "complete",
|
|
160
|
+
args: actionExecutionMessage.arguments as MappedParameterTypes<T>,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
return actionExecutionMessage.arguments as MappedParameterTypes<T>;
|
|
136
164
|
}
|
|
137
165
|
|
|
138
166
|
function makeSystemMessage(contextString: string, instructions: string): string {
|
|
@@ -147,7 +175,7 @@ The user has provided you with the following context:
|
|
|
147
175
|
${contextString}
|
|
148
176
|
\`\`\`
|
|
149
177
|
|
|
150
|
-
They have also provided you with a function you MUST call to initiate actions on their behalf.
|
|
178
|
+
They have also provided you with a function called extract you MUST call to initiate actions on their behalf.
|
|
151
179
|
|
|
152
180
|
Please assist them as best you can.
|
|
153
181
|
|
package/src/utils/index.ts
CHANGED
package/dist/chunk-2EQGN5QK.mjs
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
processMessageStream
|
|
3
|
-
} from "./chunk-ZFS5SQUT.mjs";
|
|
4
|
-
import {
|
|
5
|
-
defaultCopilotContextCategories
|
|
6
|
-
} from "./chunk-LLU5URI5.mjs";
|
|
7
|
-
import {
|
|
8
|
-
CopilotContext
|
|
9
|
-
} from "./chunk-DY63PD22.mjs";
|
|
10
|
-
import {
|
|
11
|
-
__async,
|
|
12
|
-
__spreadValues
|
|
13
|
-
} from "./chunk-SKC7AJIV.mjs";
|
|
14
|
-
|
|
15
|
-
// src/openai-assistants/hooks/use-copilot-chat-v2.ts
|
|
16
|
-
import { useContext, useMemo, useState } from "react";
|
|
17
|
-
import { parseStreamPart } from "@copilotkit/shared";
|
|
18
|
-
function useCopilotChatV2(options) {
|
|
19
|
-
const {
|
|
20
|
-
getContextString,
|
|
21
|
-
getChatCompletionFunctionDescriptions,
|
|
22
|
-
getFunctionCallHandler,
|
|
23
|
-
copilotApiConfig
|
|
24
|
-
} = useContext(CopilotContext);
|
|
25
|
-
const [messages, setMessages] = useState([]);
|
|
26
|
-
const [input, setInput] = useState("");
|
|
27
|
-
const [threadId, setThreadId] = useState(void 0);
|
|
28
|
-
const [status, setStatus] = useState("awaiting_message");
|
|
29
|
-
const [error, setError] = useState(void 0);
|
|
30
|
-
const systemMessage = useMemo(() => {
|
|
31
|
-
const systemMessageMaker = options.makeSystemMessage || defaultSystemMessage;
|
|
32
|
-
const contextString = getContextString([], defaultCopilotContextCategories);
|
|
33
|
-
return {
|
|
34
|
-
id: "system",
|
|
35
|
-
content: systemMessageMaker(contextString),
|
|
36
|
-
role: "system"
|
|
37
|
-
};
|
|
38
|
-
}, [getContextString, options.makeSystemMessage]);
|
|
39
|
-
const handleInputChange = (e) => {
|
|
40
|
-
setInput(e.target.value);
|
|
41
|
-
};
|
|
42
|
-
const submitMessage = (e) => __async(this, null, function* () {
|
|
43
|
-
var _a, _b;
|
|
44
|
-
e.preventDefault();
|
|
45
|
-
if (input === "") {
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
setStatus("in_progress");
|
|
49
|
-
setMessages((messages2) => [...messages2, { id: "", role: "user", content: input }]);
|
|
50
|
-
setInput("");
|
|
51
|
-
const apiUrl = copilotApiConfig.chatApiEndpointV2;
|
|
52
|
-
const functions = getChatCompletionFunctionDescriptions();
|
|
53
|
-
const result = yield fetch(apiUrl, {
|
|
54
|
-
method: "POST",
|
|
55
|
-
headers: __spreadValues(__spreadValues({
|
|
56
|
-
"Content-Type": "application/json"
|
|
57
|
-
}, copilotApiConfig.headers), options.headers),
|
|
58
|
-
body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues({
|
|
59
|
-
// always use user-provided threadId when available:
|
|
60
|
-
threadId: (_b = (_a = options.threadId) != null ? _a : threadId) != null ? _b : null,
|
|
61
|
-
message: input
|
|
62
|
-
}, functions.length > 0 && { functions }), copilotApiConfig.body), options.body))
|
|
63
|
-
});
|
|
64
|
-
if (result.body == null) {
|
|
65
|
-
throw new Error("The response body is empty.");
|
|
66
|
-
}
|
|
67
|
-
yield processMessageStream(result.body.getReader(), (message) => {
|
|
68
|
-
try {
|
|
69
|
-
const { type, value } = parseStreamPart(message);
|
|
70
|
-
switch (type) {
|
|
71
|
-
case "assistant_message": {
|
|
72
|
-
setMessages((messages2) => [
|
|
73
|
-
...messages2,
|
|
74
|
-
{
|
|
75
|
-
id: value.id,
|
|
76
|
-
role: value.role,
|
|
77
|
-
content: value.content[0].text.value
|
|
78
|
-
}
|
|
79
|
-
]);
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
case "assistant_control_data": {
|
|
83
|
-
setThreadId(value.threadId);
|
|
84
|
-
setMessages((messages2) => {
|
|
85
|
-
const lastMessage = messages2[messages2.length - 1];
|
|
86
|
-
lastMessage.id = value.messageId;
|
|
87
|
-
return [...messages2.slice(0, messages2.length - 1), lastMessage];
|
|
88
|
-
});
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
case "error": {
|
|
92
|
-
setError(value);
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
} catch (error2) {
|
|
97
|
-
setError(error2);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
setStatus("awaiting_message");
|
|
101
|
-
});
|
|
102
|
-
return {
|
|
103
|
-
messages,
|
|
104
|
-
input,
|
|
105
|
-
handleInputChange,
|
|
106
|
-
submitMessage,
|
|
107
|
-
status,
|
|
108
|
-
error
|
|
109
|
-
};
|
|
110
|
-
}
|
|
111
|
-
function defaultSystemMessage(contextString) {
|
|
112
|
-
return `
|
|
113
|
-
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
114
|
-
|
|
115
|
-
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.
|
|
116
|
-
Always be polite and respectful, and prefer brevity over verbosity.
|
|
117
|
-
|
|
118
|
-
The user has provided you with the following context:
|
|
119
|
-
\`\`\`
|
|
120
|
-
${contextString}
|
|
121
|
-
\`\`\`
|
|
122
|
-
|
|
123
|
-
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.
|
|
124
|
-
|
|
125
|
-
Please assist them as best you can.
|
|
126
|
-
|
|
127
|
-
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.
|
|
128
|
-
|
|
129
|
-
If you would like to call a function, call it without saying anything else.
|
|
130
|
-
`;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export {
|
|
134
|
-
useCopilotChatV2,
|
|
135
|
-
defaultSystemMessage
|
|
136
|
-
};
|
|
137
|
-
//# sourceMappingURL=chunk-2EQGN5QK.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/openai-assistants/hooks/use-copilot-chat-v2.ts"],"sourcesContent":["import { useContext, useMemo, useState } from \"react\";\nimport { processMessageStream } from \"../utils\";\nimport { Message, parseStreamPart } from \"@copilotkit/shared\";\nimport { CopilotContext } from \"../../context\";\nimport { defaultCopilotContextCategories } from \"../../components\";\n\nexport type AssistantStatus = \"in_progress\" | \"awaiting_message\";\n\nexport interface RequestForwardingOptions {\n /**\n * The credentials mode to be used for the fetch request.\n * Possible values are: 'omit', 'same-origin', 'include'.\n * Defaults to 'same-origin'.\n */\n credentials?: RequestCredentials;\n /**\n * HTTP headers to be sent with the API request.\n */\n headers?: Record<string, string> | Headers;\n /**\n * Extra body object to be sent with the API request.\n * @example\n * Send a `sessionId` to the API along with the messages.\n * ```js\n * useChat({\n * body: {\n * sessionId: '123',\n * }\n * })\n * ```\n */\n body?: object;\n}\nexport interface UseCopilotChatOptionsV2 extends RequestForwardingOptions {\n makeSystemMessage?: (contextString: string) => string;\n threadId?: string | undefined;\n}\n\nexport interface UseCopilotChatV2Result {\n messages: Message[];\n input: string;\n handleInputChange: (e: any) => void;\n submitMessage: (e: any) => Promise<void>;\n status: AssistantStatus;\n error: unknown;\n}\n\nexport function useCopilotChatV2(options: UseCopilotChatOptionsV2): UseCopilotChatV2Result {\n const {\n getContextString,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n copilotApiConfig,\n } = useContext(CopilotContext);\n\n const [messages, setMessages] = useState<Message[]>([]);\n const [input, setInput] = useState(\"\");\n const [threadId, setThreadId] = useState<string | undefined>(undefined);\n const [status, setStatus] = useState<AssistantStatus>(\"awaiting_message\");\n const [error, setError] = useState<unknown | undefined>(undefined);\n\n const systemMessage: Message = useMemo(() => {\n const systemMessageMaker = options.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, options.makeSystemMessage]);\n\n const handleInputChange = (e: any) => {\n setInput(e.target.value);\n };\n\n const submitMessage = async (e: any) => {\n e.preventDefault();\n\n if (input === \"\") {\n return;\n }\n\n setStatus(\"in_progress\");\n\n setMessages((messages) => [...messages, { id: \"\", role: \"user\", content: input }]);\n\n setInput(\"\");\n\n const apiUrl = copilotApiConfig.chatApiEndpointV2;\n\n const functions = getChatCompletionFunctionDescriptions();\n\n const result = await fetch(apiUrl, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...copilotApiConfig.headers,\n ...options.headers,\n },\n body: JSON.stringify({\n // always use user-provided threadId when available:\n threadId: options.threadId ?? threadId ?? null,\n message: input,\n ...(functions.length > 0 && { functions: functions }),\n ...copilotApiConfig.body,\n ...options.body,\n }),\n });\n\n if (result.body == null) {\n throw new Error(\"The response body is empty.\");\n }\n\n await processMessageStream(result.body.getReader(), (message: string) => {\n try {\n const { type, value } = parseStreamPart(message);\n\n switch (type) {\n case \"assistant_message\": {\n // append message:\n setMessages((messages) => [\n ...messages,\n {\n id: value.id,\n role: value.role,\n content: value.content[0].text.value,\n },\n ]);\n break;\n }\n\n case \"assistant_control_data\": {\n setThreadId(value.threadId);\n\n // set id of last message:\n setMessages((messages) => {\n const lastMessage = messages[messages.length - 1];\n lastMessage.id = value.messageId;\n return [...messages.slice(0, messages.length - 1), lastMessage];\n });\n\n break;\n }\n\n case \"error\": {\n setError(value);\n break;\n }\n }\n } catch (error) {\n setError(error);\n }\n });\n\n setStatus(\"awaiting_message\");\n };\n\n return {\n messages,\n input,\n handleInputChange,\n submitMessage,\n status,\n error,\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'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E,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWF;","names":["messages","error"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/utils/extract.ts"],"sourcesContent":["import {\n Action,\n COPILOT_CLOUD_PUBLIC_API_KEY_HEADER,\n MappedParameterTypes,\n Message,\n Parameter,\n} from \"@copilotkit/shared\";\nimport { CopilotContextParams } from \"../context\";\nimport { defaultCopilotContextCategories } from \"../components\";\nimport { fetchAndDecodeChatCompletion } from \"./fetch-chat-completion\";\nimport untruncateJson from \"untruncate-json\";\n\ninterface InitialState<T extends Parameter[] | [] = []> {\n status: \"initial\";\n args: Partial<MappedParameterTypes<T>>;\n}\n\ninterface InProgressState<T extends Parameter[] | [] = []> {\n status: \"inProgress\";\n args: Partial<MappedParameterTypes<T>>;\n}\n\ninterface CompleteState<T extends Parameter[] | [] = []> {\n status: \"complete\";\n args: MappedParameterTypes<T>;\n}\n\ntype StreamHandlerArgs<T extends Parameter[] | [] = []> =\n | InitialState<T>\n | InProgressState<T>\n | CompleteState<T>;\n\ninterface ExtractOptions<T extends Parameter[]> {\n context: CopilotContextParams;\n instructions: string;\n parameters: T;\n include?: IncludeOptions;\n data?: any;\n abortSignal?: AbortSignal;\n stream?: (args: StreamHandlerArgs<T>) => void;\n}\n\ninterface IncludeOptions {\n readable?: boolean;\n messages?: boolean;\n}\n\nexport async function extract<const T extends Parameter[]>({\n context,\n instructions,\n parameters,\n include,\n data,\n abortSignal,\n stream,\n}: ExtractOptions<T>): Promise<MappedParameterTypes<T>> {\n const { messages } = context;\n\n const action: Action<any> = {\n name: \"extract\",\n parameters,\n handler: (args: any) => {},\n };\n\n const includeReadable = include?.readable ?? false;\n const includeMessages = include?.messages ?? false;\n\n let contextString = \"\";\n\n if (data) {\n contextString = (typeof data === \"string\" ? data : JSON.stringify(data)) + \"\\n\\n\";\n }\n\n if (includeReadable) {\n contextString += context.getContextString([], defaultCopilotContextCategories);\n }\n\n const systemMessage: Message = {\n id: \"system\",\n content: makeSystemMessage(contextString, instructions),\n role: \"system\",\n };\n\n const headers = {\n ...(context.copilotApiConfig.headers || {}),\n ...(context.copilotApiConfig.publicApiKey\n ? { [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: context.copilotApiConfig.publicApiKey }\n : {}),\n };\n\n const response = await fetchAndDecodeChatCompletion({\n copilotConfig: context.copilotApiConfig,\n messages: includeMessages ? [systemMessage, ...messages] : [systemMessage],\n tools: context.getChatCompletionFunctionDescriptions({ extract: action }),\n headers,\n body: context.copilotApiConfig.body,\n toolChoice: { type: \"function\", function: { name: \"extract\" } },\n signal: abortSignal,\n });\n\n if (!response.events) {\n throw new Error(\"extract() failed: Could not fetch chat completion\");\n }\n\n const reader = response.events.getReader();\n let isInitial = true;\n\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n break;\n }\n\n if (value.type === \"partial\") {\n try {\n let partialArguments = JSON.parse(untruncateJson(value.arguments));\n stream?.({\n status: isInitial ? \"initial\" : \"inProgress\",\n args: partialArguments as Partial<MappedParameterTypes<T>>,\n });\n isInitial = false;\n } catch (e) {}\n }\n\n if (value.type === \"function\") {\n stream?.({\n status: \"complete\",\n args: value.arguments as MappedParameterTypes<T>,\n });\n return value.arguments as MappedParameterTypes<T>;\n }\n }\n\n throw new Error(\"extract() failed: No function call occurred\");\n}\n\nfunction makeSystemMessage(contextString: string, instructions: 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 a function you MUST call to initiate actions on their behalf.\n\nPlease assist them as best you can.\n\nThis is not a conversation, so please do not ask questions. Just call the function without saying anything else.\n\nThe user has given you the following task to complete:\n\n\\`\\`\\`\n${instructions}\n\\`\\`\\`\n\nAny additional messages provided are for providing context only and should not be used to ask questions or engage in conversation.\n`;\n}\n"],"mappings":";;;;;;;;;;;;AAAA;AAAA,EAEE;AAAA,OAIK;AAIP,OAAO,oBAAoB;AAqC3B,SAAsB,QAAqC,IAQH;AAAA,6CARG;AAAA,IACzD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAwD;AAvDxD;AAwDE,UAAM,EAAE,SAAS,IAAI;AAErB,UAAM,SAAsB;AAAA,MAC1B,MAAM;AAAA,MACN;AAAA,MACA,SAAS,CAAC,SAAc;AAAA,MAAC;AAAA,IAC3B;AAEA,UAAM,mBAAkB,wCAAS,aAAT,YAAqB;AAC7C,UAAM,mBAAkB,wCAAS,aAAT,YAAqB;AAE7C,QAAI,gBAAgB;AAEpB,QAAI,MAAM;AACR,uBAAiB,OAAO,SAAS,WAAW,OAAO,KAAK,UAAU,IAAI,KAAK;AAAA,IAC7E;AAEA,QAAI,iBAAiB;AACnB,uBAAiB,QAAQ,iBAAiB,CAAC,GAAG,+BAA+B;AAAA,IAC/E;AAEA,UAAM,gBAAyB;AAAA,MAC7B,IAAI;AAAA,MACJ,SAAS,kBAAkB,eAAe,YAAY;AAAA,MACtD,MAAM;AAAA,IACR;AAEA,UAAM,UAAU,kCACV,QAAQ,iBAAiB,WAAW,CAAC,IACrC,QAAQ,iBAAiB,eACzB,EAAE,CAAC,mCAAmC,GAAG,QAAQ,iBAAiB,aAAa,IAC/E,CAAC;AAGP,UAAM,WAAW,MAAM,6BAA6B;AAAA,MAClD,eAAe,QAAQ;AAAA,MACvB,UAAU,kBAAkB,CAAC,eAAe,GAAG,QAAQ,IAAI,CAAC,aAAa;AAAA,MACzE,OAAO,QAAQ,sCAAsC,EAAE,SAAS,OAAO,CAAC;AAAA,MACxE;AAAA,MACA,MAAM,QAAQ,iBAAiB;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
-
import {
|
|
5
|
-
__async,
|
|
6
|
-
__spreadProps,
|
|
7
|
-
__spreadValues
|
|
8
|
-
} from "./chunk-SKC7AJIV.mjs";
|
|
9
|
-
|
|
10
|
-
// src/hooks/use-chat.ts
|
|
11
|
-
import { useRef } from "react";
|
|
12
|
-
import {
|
|
13
|
-
encodeResult,
|
|
14
|
-
COPILOT_CLOUD_PUBLIC_API_KEY_HEADER
|
|
15
|
-
} from "@copilotkit/shared";
|
|
16
|
-
import { nanoid } from "nanoid";
|
|
17
|
-
import untruncateJson from "untruncate-json";
|
|
18
|
-
function useChat(options) {
|
|
19
|
-
const { messages, setMessages, makeSystemMessageCallback } = options;
|
|
20
|
-
const abortControllerRef = useRef();
|
|
21
|
-
const threadIdRef = useRef(null);
|
|
22
|
-
const runIdRef = useRef(null);
|
|
23
|
-
const publicApiKey = options.copilotConfig.publicApiKey;
|
|
24
|
-
const headers = __spreadValues(__spreadValues({}, options.headers || {}), publicApiKey ? { [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
|
|
25
|
-
const runChatCompletion = (messages2) => __async(this, null, function* () {
|
|
26
|
-
options.setIsLoading(true);
|
|
27
|
-
const newMessages = [
|
|
28
|
-
{
|
|
29
|
-
id: nanoid(),
|
|
30
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
31
|
-
content: "",
|
|
32
|
-
role: "assistant"
|
|
33
|
-
}
|
|
34
|
-
];
|
|
35
|
-
const abortController = new AbortController();
|
|
36
|
-
abortControllerRef.current = abortController;
|
|
37
|
-
setMessages([...messages2, ...newMessages]);
|
|
38
|
-
const copilotConfigBody = options.copilotConfig.body || {};
|
|
39
|
-
if (threadIdRef.current) {
|
|
40
|
-
copilotConfigBody.threadId = threadIdRef.current;
|
|
41
|
-
}
|
|
42
|
-
if (runIdRef.current) {
|
|
43
|
-
copilotConfigBody.runId = runIdRef.current;
|
|
44
|
-
}
|
|
45
|
-
const systemMessage = makeSystemMessageCallback();
|
|
46
|
-
const messagesWithContext = [systemMessage, ...options.initialMessages || [], ...messages2];
|
|
47
|
-
const response = yield fetchAndDecodeChatCompletion({
|
|
48
|
-
copilotConfig: __spreadProps(__spreadValues({}, options.copilotConfig), { body: copilotConfigBody }),
|
|
49
|
-
messages: messagesWithContext,
|
|
50
|
-
tools: options.tools,
|
|
51
|
-
headers,
|
|
52
|
-
signal: abortController.signal
|
|
53
|
-
});
|
|
54
|
-
if (response.headers.get("threadid")) {
|
|
55
|
-
threadIdRef.current = response.headers.get("threadid");
|
|
56
|
-
}
|
|
57
|
-
if (response.headers.get("runid")) {
|
|
58
|
-
runIdRef.current = response.headers.get("runid");
|
|
59
|
-
}
|
|
60
|
-
if (!response.events) {
|
|
61
|
-
setMessages([
|
|
62
|
-
...messages2,
|
|
63
|
-
{
|
|
64
|
-
id: nanoid(),
|
|
65
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
66
|
-
content: response.statusText,
|
|
67
|
-
role: "assistant"
|
|
68
|
-
}
|
|
69
|
-
]);
|
|
70
|
-
options.setIsLoading(false);
|
|
71
|
-
throw new Error("Failed to fetch chat completion");
|
|
72
|
-
}
|
|
73
|
-
const reader = response.events.getReader();
|
|
74
|
-
let feedback = false;
|
|
75
|
-
try {
|
|
76
|
-
while (true) {
|
|
77
|
-
const { done, value } = yield reader.read();
|
|
78
|
-
if (done) {
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
let currentMessage = Object.assign({}, newMessages[newMessages.length - 1]);
|
|
82
|
-
if (value.type === "content") {
|
|
83
|
-
if (currentMessage.function_call || currentMessage.role === "function") {
|
|
84
|
-
currentMessage = {
|
|
85
|
-
id: nanoid(),
|
|
86
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
87
|
-
content: "",
|
|
88
|
-
role: "assistant"
|
|
89
|
-
};
|
|
90
|
-
newMessages.push(currentMessage);
|
|
91
|
-
}
|
|
92
|
-
currentMessage.content += value.content;
|
|
93
|
-
newMessages[newMessages.length - 1] = currentMessage;
|
|
94
|
-
setMessages([...messages2, ...newMessages]);
|
|
95
|
-
} else if (value.type === "result") {
|
|
96
|
-
currentMessage = {
|
|
97
|
-
id: nanoid(),
|
|
98
|
-
role: "function",
|
|
99
|
-
content: value.content,
|
|
100
|
-
name: value.name
|
|
101
|
-
};
|
|
102
|
-
newMessages.push(currentMessage);
|
|
103
|
-
setMessages([...messages2, ...newMessages]);
|
|
104
|
-
feedback = true;
|
|
105
|
-
} else if (value.type === "function" || value.type === "partial") {
|
|
106
|
-
if (currentMessage.content != "" || currentMessage.function_call || currentMessage.role == "function") {
|
|
107
|
-
currentMessage = {
|
|
108
|
-
id: nanoid(),
|
|
109
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
110
|
-
content: "",
|
|
111
|
-
role: "assistant"
|
|
112
|
-
};
|
|
113
|
-
newMessages.push(currentMessage);
|
|
114
|
-
}
|
|
115
|
-
if (value.type === "function") {
|
|
116
|
-
currentMessage.function_call = {
|
|
117
|
-
name: value.name,
|
|
118
|
-
arguments: JSON.stringify(value.arguments),
|
|
119
|
-
scope: value.scope
|
|
120
|
-
};
|
|
121
|
-
} else if (value.type === "partial") {
|
|
122
|
-
let partialArguments = {};
|
|
123
|
-
try {
|
|
124
|
-
partialArguments = JSON.parse(untruncateJson(value.arguments));
|
|
125
|
-
} catch (e) {
|
|
126
|
-
}
|
|
127
|
-
currentMessage.partialFunctionCall = {
|
|
128
|
-
name: value.name,
|
|
129
|
-
arguments: partialArguments
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
newMessages[newMessages.length - 1] = currentMessage;
|
|
133
|
-
setMessages([...messages2, ...newMessages]);
|
|
134
|
-
if (value.type === "function") {
|
|
135
|
-
try {
|
|
136
|
-
if (options.onFunctionCall && value.scope === "client") {
|
|
137
|
-
const result = yield options.onFunctionCall(
|
|
138
|
-
messages2,
|
|
139
|
-
currentMessage.function_call
|
|
140
|
-
);
|
|
141
|
-
currentMessage = {
|
|
142
|
-
id: nanoid(),
|
|
143
|
-
role: "function",
|
|
144
|
-
content: encodeResult(result),
|
|
145
|
-
name: currentMessage.function_call.name
|
|
146
|
-
};
|
|
147
|
-
newMessages.push(currentMessage);
|
|
148
|
-
setMessages([...messages2, ...newMessages]);
|
|
149
|
-
feedback = true;
|
|
150
|
-
}
|
|
151
|
-
} catch (error) {
|
|
152
|
-
console.error("Failed to execute function call", error);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
if (feedback) {
|
|
158
|
-
yield new Promise((resolve) => setTimeout(resolve, 10));
|
|
159
|
-
return yield runChatCompletion([...messages2, ...newMessages]);
|
|
160
|
-
} else {
|
|
161
|
-
return newMessages.slice();
|
|
162
|
-
}
|
|
163
|
-
} finally {
|
|
164
|
-
options.setIsLoading(false);
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
const runChatCompletionAndHandleFunctionCall = (messages2) => __async(this, null, function* () {
|
|
168
|
-
yield runChatCompletion(messages2);
|
|
169
|
-
});
|
|
170
|
-
const append = (message) => __async(this, null, function* () {
|
|
171
|
-
if (options.isLoading) {
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
const newMessages = [...messages, message];
|
|
175
|
-
setMessages(newMessages);
|
|
176
|
-
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
177
|
-
});
|
|
178
|
-
const reload = () => __async(this, null, function* () {
|
|
179
|
-
if (options.isLoading || messages.length === 0) {
|
|
180
|
-
return;
|
|
181
|
-
}
|
|
182
|
-
let newMessages = [...messages];
|
|
183
|
-
const lastMessage = messages[messages.length - 1];
|
|
184
|
-
if (lastMessage.role === "assistant") {
|
|
185
|
-
newMessages = newMessages.slice(0, -1);
|
|
186
|
-
}
|
|
187
|
-
setMessages(newMessages);
|
|
188
|
-
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
189
|
-
});
|
|
190
|
-
const stop = () => {
|
|
191
|
-
var _a;
|
|
192
|
-
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
193
|
-
};
|
|
194
|
-
return {
|
|
195
|
-
append,
|
|
196
|
-
reload,
|
|
197
|
-
stop
|
|
198
|
-
};
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
export {
|
|
202
|
-
useChat
|
|
203
|
-
};
|
|
204
|
-
//# sourceMappingURL=chunk-B2H3NC4E.mjs.map
|