@terryavg/neptune-ai-chatbot 1.0.0 → 1.0.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.
- package/README.md +151 -0
- package/dist/chat-input-42V4ESQB.mjs +478 -0
- package/dist/chat-input-4T42R7FR.mjs +481 -0
- package/dist/chat-input-BJVIIYOP.mjs +484 -0
- package/dist/chat-input-HT47ZLQN.mjs +456 -0
- package/dist/chat-input-JFGPZBBQ.mjs +495 -0
- package/dist/chat-input-JQ7PSHNW.mjs +515 -0
- package/dist/chat-input-USWVSAYY.mjs +461 -0
- package/dist/chat-input-VTCTV5ED.mjs +495 -0
- package/dist/chat-input-WJMGLQJI.mjs +495 -0
- package/dist/chat-message-2VQW74CO.mjs +2230 -0
- package/dist/chat-message-35ZWSSQV.mjs +1926 -0
- package/dist/chat-message-3CUUWTZG.mjs +2429 -0
- package/dist/chat-message-3NHSJNKL.mjs +2241 -0
- package/dist/chat-message-3Z7B2WYC.mjs +2228 -0
- package/dist/chat-message-564AHZHH.mjs +2186 -0
- package/dist/chat-message-5QLTW4K7.mjs +2122 -0
- package/dist/chat-message-7EH2TDZG.mjs +1910 -0
- package/dist/chat-message-7QHJRHQG.mjs +1909 -0
- package/dist/chat-message-AAXNH5TF.mjs +2088 -0
- package/dist/chat-message-D36YF274.mjs +1910 -0
- package/dist/chat-message-DVCXEL4Z.mjs +2111 -0
- package/dist/chat-message-E6KB3AST.mjs +1906 -0
- package/dist/chat-message-G2NWPAXK.mjs +1904 -0
- package/dist/chat-message-H62Z3DW5.mjs +2368 -0
- package/dist/chat-message-HIEZ7B5R.mjs +2190 -0
- package/dist/chat-message-I6AKFPK6.mjs +2156 -0
- package/dist/chat-message-IKYSAVAB.mjs +1918 -0
- package/dist/chat-message-IZL6JHV2.mjs +2429 -0
- package/dist/chat-message-J7PVUQO6.mjs +1878 -0
- package/dist/chat-message-K6QILTW5.mjs +1897 -0
- package/dist/chat-message-MDORLI2R.mjs +2228 -0
- package/dist/chat-message-MFUY6KOE.mjs +1910 -0
- package/dist/chat-message-MYKOR5OF.mjs +1890 -0
- package/dist/chat-message-NKMTAMGG.mjs +1906 -0
- package/dist/chat-message-NNGD3FUH.mjs +2168 -0
- package/dist/chat-message-QAPRO542.mjs +1915 -0
- package/dist/chat-message-R4IJ3MXU.mjs +2146 -0
- package/dist/chat-message-U7UCBLHI.mjs +2145 -0
- package/dist/chat-message-UKXGFKUR.mjs +1903 -0
- package/dist/chat-message-UNIBCF3T.mjs +1900 -0
- package/dist/chat-message-UXLPL76T.mjs +1890 -0
- package/dist/chat-message-VB54UOHB.mjs +2306 -0
- package/dist/chat-message-WUNUZLKI.mjs +2228 -0
- package/dist/chat-message-WYN5UZRD.mjs +1927 -0
- package/dist/chat-message-Y5OJSR2O.mjs +2228 -0
- package/dist/chat-message-YPDHL6WW.mjs +2114 -0
- package/dist/chunk-26656QB3.mjs +406 -0
- package/dist/chunk-2RXQ2EZ2.mjs +295 -0
- package/dist/chunk-DVWFDUN4.mjs +407 -0
- package/dist/chunk-JLRHY3SB.mjs +405 -0
- package/dist/chunk-XFSEOBLD.mjs +432 -0
- package/dist/index.css +174 -43
- package/dist/index.d.mts +218 -1
- package/dist/index.d.ts +218 -1
- package/dist/index.js +2088 -1400
- package/dist/index.mjs +606 -413
- package/dist/styles.css +1 -1
- package/package.json +3 -2
- package/README +0 -16
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import {
|
|
2
|
+
__spreadProps,
|
|
3
|
+
__spreadValues
|
|
4
|
+
} from "./chunk-FWCSY2DS.mjs";
|
|
5
|
+
|
|
6
|
+
// app/api/chat-client.ts
|
|
7
|
+
var chatConfig = {};
|
|
8
|
+
function configureChatClient(config) {
|
|
9
|
+
chatConfig.username = config.username;
|
|
10
|
+
chatConfig.password = config.password;
|
|
11
|
+
chatConfig.baseUrl = config.baseUrl;
|
|
12
|
+
}
|
|
13
|
+
function isJson(content) {
|
|
14
|
+
if (typeof content === "object" && content !== null) {
|
|
15
|
+
return Array.isArray(content) || Object.prototype.toString.call(content) === "[object Object]";
|
|
16
|
+
}
|
|
17
|
+
if (typeof content === "string") {
|
|
18
|
+
try {
|
|
19
|
+
const parsed = JSON.parse(content);
|
|
20
|
+
return typeof parsed === "object" && parsed !== null;
|
|
21
|
+
} catch (e) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
async function fetchWithErrorHandling(url, options = {}) {
|
|
28
|
+
const headers = new Headers(options.headers);
|
|
29
|
+
headers.set("Content-Type", "application/json");
|
|
30
|
+
if (chatConfig.username && chatConfig.password) {
|
|
31
|
+
headers.set(
|
|
32
|
+
"Authorization",
|
|
33
|
+
"Basic " + btoa(chatConfig.username + ":" + chatConfig.password)
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
const finalUrl = chatConfig.baseUrl ? `${chatConfig.baseUrl}${url}` : url;
|
|
37
|
+
let response;
|
|
38
|
+
try {
|
|
39
|
+
response = await fetch(finalUrl, __spreadProps(__spreadValues({}, options), {
|
|
40
|
+
headers
|
|
41
|
+
}));
|
|
42
|
+
} catch (networkError) {
|
|
43
|
+
console.error("Network error:", networkError);
|
|
44
|
+
throw {
|
|
45
|
+
isNetworkError: true,
|
|
46
|
+
message: "Failed to connect to the server. Please check your network.",
|
|
47
|
+
status: 0
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if (!response.ok) {
|
|
51
|
+
let errorBody = {
|
|
52
|
+
message: `API request failed: ${response.status} ${response.statusText}`,
|
|
53
|
+
statusText: response.statusText
|
|
54
|
+
};
|
|
55
|
+
try {
|
|
56
|
+
const contentType2 = response.headers.get("content-type");
|
|
57
|
+
if (contentType2 && contentType2.includes("application/json")) {
|
|
58
|
+
const jsonError = await response.json();
|
|
59
|
+
errorBody = __spreadProps(__spreadValues({}, jsonError), {
|
|
60
|
+
// Include fields like status, message from API
|
|
61
|
+
statusText: response.statusText
|
|
62
|
+
// Keep original statusText
|
|
63
|
+
});
|
|
64
|
+
} else {
|
|
65
|
+
const textError = await response.text();
|
|
66
|
+
errorBody.message = textError || errorBody.message;
|
|
67
|
+
}
|
|
68
|
+
} catch (parseError) {
|
|
69
|
+
console.error("Failed to parse error response body:", parseError);
|
|
70
|
+
}
|
|
71
|
+
throw __spreadProps(__spreadValues({}, errorBody), { status: response.status, isApiError: true });
|
|
72
|
+
}
|
|
73
|
+
const contentType = response.headers.get("content-type");
|
|
74
|
+
if (!contentType || !contentType.includes("application/json")) {
|
|
75
|
+
if (response.status === 204 || !contentType) {
|
|
76
|
+
return { success: true };
|
|
77
|
+
}
|
|
78
|
+
const text = await response.text();
|
|
79
|
+
if (!text.trim()) {
|
|
80
|
+
return { success: true };
|
|
81
|
+
}
|
|
82
|
+
console.warn(
|
|
83
|
+
`API returned non-JSON response for ${url}, status: ${response.status}`
|
|
84
|
+
);
|
|
85
|
+
return { success: true, nonJsonResponse: text };
|
|
86
|
+
}
|
|
87
|
+
return response.json();
|
|
88
|
+
}
|
|
89
|
+
async function createAgentStream(agentId, messages, conversationId, signal, stepData, streaming = true) {
|
|
90
|
+
const lastMessage = messages[messages.length - 1];
|
|
91
|
+
let input = "";
|
|
92
|
+
if (lastMessage && lastMessage.content) {
|
|
93
|
+
if (typeof lastMessage.content === "string") {
|
|
94
|
+
input = lastMessage.content;
|
|
95
|
+
} else if (Array.isArray(lastMessage.content)) {
|
|
96
|
+
const textContent = lastMessage.content.find(
|
|
97
|
+
(c) => c.type === "text"
|
|
98
|
+
);
|
|
99
|
+
if (textContent) {
|
|
100
|
+
input = textContent.text;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const formData = new FormData();
|
|
105
|
+
formData.append("aiAgent", agentId);
|
|
106
|
+
formData.append("input", input);
|
|
107
|
+
formData.append("stream", streaming ? "true" : "false");
|
|
108
|
+
if (conversationId && conversationId.trim() !== "" && !conversationId.startsWith("temp-")) {
|
|
109
|
+
formData.append("threadID", conversationId);
|
|
110
|
+
}
|
|
111
|
+
if (stepData) {
|
|
112
|
+
formData.append("variables", JSON.stringify(stepData));
|
|
113
|
+
}
|
|
114
|
+
if (lastMessage && Array.isArray(lastMessage.content)) {
|
|
115
|
+
for (const content of lastMessage.content) {
|
|
116
|
+
if (content.type === "image") {
|
|
117
|
+
const imageContent = content;
|
|
118
|
+
const base64Data = imageContent.image.includes(",") ? imageContent.image.split(",")[1] : imageContent.image;
|
|
119
|
+
const byteCharacters = atob(base64Data);
|
|
120
|
+
const byteNumbers = new Array(byteCharacters.length);
|
|
121
|
+
for (let i = 0; i < byteCharacters.length; i++) {
|
|
122
|
+
byteNumbers[i] = byteCharacters.charCodeAt(i);
|
|
123
|
+
}
|
|
124
|
+
const byteArray = new Uint8Array(byteNumbers);
|
|
125
|
+
const blob = new Blob([byteArray], {
|
|
126
|
+
type: imageContent.mediaType
|
|
127
|
+
});
|
|
128
|
+
const file = new File(
|
|
129
|
+
[blob],
|
|
130
|
+
`image.${imageContent.mediaType.split("/")[1]}`,
|
|
131
|
+
{
|
|
132
|
+
type: imageContent.mediaType
|
|
133
|
+
}
|
|
134
|
+
);
|
|
135
|
+
formData.append("files", file);
|
|
136
|
+
} else if (content.type === "file") {
|
|
137
|
+
const fileContent = content;
|
|
138
|
+
const base64Data = fileContent.data.includes(",") ? fileContent.data.split(",")[1] : fileContent.data;
|
|
139
|
+
const byteCharacters = atob(base64Data);
|
|
140
|
+
const byteNumbers = new Array(byteCharacters.length);
|
|
141
|
+
for (let i = 0; i < byteCharacters.length; i++) {
|
|
142
|
+
byteNumbers[i] = byteCharacters.charCodeAt(i);
|
|
143
|
+
}
|
|
144
|
+
const byteArray = new Uint8Array(byteNumbers);
|
|
145
|
+
const blob = new Blob([byteArray], {
|
|
146
|
+
type: fileContent.mediaType
|
|
147
|
+
});
|
|
148
|
+
const file = new File([blob], fileContent.filename, {
|
|
149
|
+
type: fileContent.mediaType
|
|
150
|
+
});
|
|
151
|
+
formData.append("files", file);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const url = chatConfig.baseUrl ? `${chatConfig.baseUrl}/api/AIBuildersKit/createCompletionAI` : "/api/AIBuildersKit/createCompletionAI";
|
|
156
|
+
const headers = {};
|
|
157
|
+
if (chatConfig.username && chatConfig.password) {
|
|
158
|
+
headers["Authorization"] = "Basic " + btoa(chatConfig.username + ":" + chatConfig.password);
|
|
159
|
+
}
|
|
160
|
+
const response = await fetch(url, {
|
|
161
|
+
method: "POST",
|
|
162
|
+
headers,
|
|
163
|
+
body: formData,
|
|
164
|
+
signal
|
|
165
|
+
});
|
|
166
|
+
if (!response.ok) {
|
|
167
|
+
let errorDetails = `API request failed: ${response.status} ${response.statusText}`;
|
|
168
|
+
try {
|
|
169
|
+
const errorData = await response.json();
|
|
170
|
+
if (errorData && errorData.message) {
|
|
171
|
+
errorDetails = errorData.message;
|
|
172
|
+
}
|
|
173
|
+
} catch (e) {
|
|
174
|
+
}
|
|
175
|
+
throw new Error(errorDetails);
|
|
176
|
+
}
|
|
177
|
+
if (!response.body) {
|
|
178
|
+
throw new Error("Stream not supported by the response");
|
|
179
|
+
}
|
|
180
|
+
return response.body;
|
|
181
|
+
}
|
|
182
|
+
var chatClient = {
|
|
183
|
+
conversations: {
|
|
184
|
+
getAll: async (agentId) => {
|
|
185
|
+
const response = await fetchWithErrorHandling(
|
|
186
|
+
"/api/functions/AIAgent/listUserThreads",
|
|
187
|
+
{
|
|
188
|
+
method: "POST",
|
|
189
|
+
body: JSON.stringify({ agentId })
|
|
190
|
+
}
|
|
191
|
+
);
|
|
192
|
+
if (Array.isArray(response)) {
|
|
193
|
+
return response;
|
|
194
|
+
} else if (response && response.conversations && Array.isArray(response.conversations)) {
|
|
195
|
+
return response.conversations;
|
|
196
|
+
} else if (response && response.data && Array.isArray(response.data)) {
|
|
197
|
+
return response.data;
|
|
198
|
+
} else {
|
|
199
|
+
console.error(
|
|
200
|
+
"Unexpected response format from list-conversations:",
|
|
201
|
+
response
|
|
202
|
+
);
|
|
203
|
+
return [];
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
get: async (id) => {
|
|
207
|
+
const response = await fetchWithErrorHandling(
|
|
208
|
+
`/api/functions/AIAgent/getConversation`,
|
|
209
|
+
{
|
|
210
|
+
method: "POST",
|
|
211
|
+
body: JSON.stringify({ id })
|
|
212
|
+
}
|
|
213
|
+
);
|
|
214
|
+
if (Array.isArray(response)) {
|
|
215
|
+
const logs = response;
|
|
216
|
+
const messages = [];
|
|
217
|
+
logs.forEach((log) => {
|
|
218
|
+
let userContent = log.input;
|
|
219
|
+
if (log.files && log.files.length > 0) {
|
|
220
|
+
const contentParts = [];
|
|
221
|
+
if (log.input && log.input.trim()) {
|
|
222
|
+
contentParts.push({
|
|
223
|
+
type: "text",
|
|
224
|
+
text: log.input
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
log.files.forEach((file) => {
|
|
228
|
+
if (file.mimeType.startsWith("image/")) {
|
|
229
|
+
contentParts.push({
|
|
230
|
+
type: "image",
|
|
231
|
+
image: `data:${file.mimeType};base64,${file.data}`,
|
|
232
|
+
mediaType: file.mimeType
|
|
233
|
+
});
|
|
234
|
+
} else {
|
|
235
|
+
contentParts.push({
|
|
236
|
+
type: "file",
|
|
237
|
+
data: `data:${file.mimeType};base64,${file.data}`,
|
|
238
|
+
mediaType: file.mimeType,
|
|
239
|
+
filename: file.name
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
userContent = contentParts;
|
|
244
|
+
}
|
|
245
|
+
messages.push({
|
|
246
|
+
id: `${log.id}-user`,
|
|
247
|
+
content: userContent,
|
|
248
|
+
role: "user",
|
|
249
|
+
createdAt: log.createdAt
|
|
250
|
+
});
|
|
251
|
+
const assistantMessage = {
|
|
252
|
+
id: `${log.id}-assistant`,
|
|
253
|
+
content: log.output,
|
|
254
|
+
role: "assistant",
|
|
255
|
+
createdAt: log.updatedAt
|
|
256
|
+
};
|
|
257
|
+
if (log.steps && log.steps.length > 0 || log.vectors && log.vectors.length > 0) {
|
|
258
|
+
assistantMessage.metadata = {
|
|
259
|
+
logId: log.id,
|
|
260
|
+
steps: log.steps,
|
|
261
|
+
vectors: log.vectors
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
messages.push(assistantMessage);
|
|
265
|
+
});
|
|
266
|
+
const conversation2 = {
|
|
267
|
+
id,
|
|
268
|
+
title: logs.length > 0 ? logs[0].input.substring(0, 50) : "Conversation",
|
|
269
|
+
messages,
|
|
270
|
+
createdAt: logs.length > 0 ? logs[0].createdAt : (/* @__PURE__ */ new Date()).toISOString(),
|
|
271
|
+
updatedAt: logs.length > 0 ? logs[logs.length - 1].updatedAt : (/* @__PURE__ */ new Date()).toISOString()
|
|
272
|
+
};
|
|
273
|
+
return conversation2;
|
|
274
|
+
}
|
|
275
|
+
let conversation;
|
|
276
|
+
if (response && response.id) {
|
|
277
|
+
conversation = response;
|
|
278
|
+
} else if (response && response.conversation) {
|
|
279
|
+
conversation = response.conversation;
|
|
280
|
+
} else if (response && response.data) {
|
|
281
|
+
conversation = response.data;
|
|
282
|
+
} else {
|
|
283
|
+
throw new Error(
|
|
284
|
+
"Unexpected response format from get-conversation"
|
|
285
|
+
);
|
|
286
|
+
}
|
|
287
|
+
if (conversation.messages && Array.isArray(conversation.messages)) {
|
|
288
|
+
conversation.messages = conversation.messages.map((message) => {
|
|
289
|
+
if (message.role === "assistant" && typeof message.content === "string") {
|
|
290
|
+
const content = message.content;
|
|
291
|
+
if (isJson(content) && !content.includes("```json:")) {
|
|
292
|
+
const jsonObj = typeof content === "string" ? JSON.parse(content) : content;
|
|
293
|
+
return __spreadProps(__spreadValues({}, message), {
|
|
294
|
+
content: `\`\`\`json:Response
|
|
295
|
+
${JSON.stringify(
|
|
296
|
+
jsonObj,
|
|
297
|
+
null,
|
|
298
|
+
2
|
|
299
|
+
)}
|
|
300
|
+
\`\`\``
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
return message;
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
return conversation;
|
|
308
|
+
},
|
|
309
|
+
create: async (title, agentId) => {
|
|
310
|
+
return fetchWithErrorHandling(
|
|
311
|
+
`/api/functions/AIAgent/createThread`,
|
|
312
|
+
{
|
|
313
|
+
method: "POST",
|
|
314
|
+
body: JSON.stringify(__spreadValues(__spreadValues({}, title ? { title } : {}), agentId ? { agentId } : {}))
|
|
315
|
+
}
|
|
316
|
+
);
|
|
317
|
+
},
|
|
318
|
+
delete: async (id) => {
|
|
319
|
+
return fetchWithErrorHandling(
|
|
320
|
+
`/api/functions/AIAgent/deleteThreadByIdAndUser`,
|
|
321
|
+
{
|
|
322
|
+
method: "POST",
|
|
323
|
+
body: JSON.stringify({ id })
|
|
324
|
+
}
|
|
325
|
+
);
|
|
326
|
+
},
|
|
327
|
+
update: async (id, data) => {
|
|
328
|
+
return fetchWithErrorHandling(
|
|
329
|
+
`/api/functions/AIAgent/renameThread`,
|
|
330
|
+
{
|
|
331
|
+
method: "POST",
|
|
332
|
+
body: JSON.stringify({ id, title: data.title })
|
|
333
|
+
}
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
},
|
|
337
|
+
messages: {
|
|
338
|
+
getLastMessages: async (conversationId, limit = 100) => {
|
|
339
|
+
const conversation = await chatClient.conversations.get(
|
|
340
|
+
conversationId
|
|
341
|
+
);
|
|
342
|
+
return conversation.messages.slice(-limit);
|
|
343
|
+
},
|
|
344
|
+
sendMessage: async (agentId, conversationId, content, existingMessages, signal, debug, stepData, isTemporary, streaming = true) => {
|
|
345
|
+
const idToUse = agentId;
|
|
346
|
+
if (!idToUse || typeof idToUse !== "string" || idToUse.trim() === "") {
|
|
347
|
+
const error = new Error(
|
|
348
|
+
"Assistant or Agent ID is missing or invalid. Please provide a valid Assistant or Agent ID in the URL."
|
|
349
|
+
);
|
|
350
|
+
error.isConfigurationError = true;
|
|
351
|
+
error.status = 400;
|
|
352
|
+
throw error;
|
|
353
|
+
}
|
|
354
|
+
let actualConversationId = conversationId;
|
|
355
|
+
if (isTemporary && agentId) {
|
|
356
|
+
let title = "New Chat";
|
|
357
|
+
if (typeof content === "string") {
|
|
358
|
+
title = content.substring(0, 50);
|
|
359
|
+
} else if (Array.isArray(content)) {
|
|
360
|
+
const textContent = content.find(
|
|
361
|
+
(c) => c.type === "text"
|
|
362
|
+
);
|
|
363
|
+
if (textContent) {
|
|
364
|
+
title = textContent.text.substring(0, 50);
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
try {
|
|
368
|
+
const newConversation = await chatClient.conversations.create(title, agentId);
|
|
369
|
+
actualConversationId = newConversation.id;
|
|
370
|
+
} catch (error) {
|
|
371
|
+
console.error("Failed to create conversation:", error);
|
|
372
|
+
throw error;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const userMessage = {
|
|
376
|
+
role: "user",
|
|
377
|
+
content
|
|
378
|
+
};
|
|
379
|
+
let contextMessages = [];
|
|
380
|
+
if (existingMessages && Array.isArray(existingMessages)) {
|
|
381
|
+
const formattedMessages = existingMessages.map((msg) => ({
|
|
382
|
+
role: msg.role,
|
|
383
|
+
content: msg.content
|
|
384
|
+
}));
|
|
385
|
+
contextMessages = [...formattedMessages, userMessage];
|
|
386
|
+
contextMessages = contextMessages.slice(-10);
|
|
387
|
+
} else {
|
|
388
|
+
contextMessages = [userMessage];
|
|
389
|
+
}
|
|
390
|
+
const stream = await createAgentStream(
|
|
391
|
+
idToUse,
|
|
392
|
+
contextMessages,
|
|
393
|
+
actualConversationId,
|
|
394
|
+
signal,
|
|
395
|
+
stepData,
|
|
396
|
+
streaming
|
|
397
|
+
);
|
|
398
|
+
return { stream, threadId: actualConversationId };
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
};
|
|
402
|
+
|
|
403
|
+
export {
|
|
404
|
+
configureChatClient,
|
|
405
|
+
chatClient
|
|
406
|
+
};
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
// app/components/tool-execution.tsx
|
|
2
|
+
import { useState, memo, useMemo } from "react";
|
|
3
|
+
import {
|
|
4
|
+
ChevronDown,
|
|
5
|
+
ChevronRight,
|
|
6
|
+
Loader2,
|
|
7
|
+
CheckCircle2,
|
|
8
|
+
XCircle,
|
|
9
|
+
Copy,
|
|
10
|
+
Check
|
|
11
|
+
} from "lucide-react";
|
|
12
|
+
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
|
|
13
|
+
import {
|
|
14
|
+
vscDarkPlus,
|
|
15
|
+
oneLight
|
|
16
|
+
} from "react-syntax-highlighter/dist/esm/styles/prism";
|
|
17
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
18
|
+
var ToolExecutionIndicator = memo(
|
|
19
|
+
({ toolName }) => {
|
|
20
|
+
return /* @__PURE__ */ jsx("div", { className: "my-4 p-4 border border-blue-200 dark:border-blue-800 rounded-lg bg-blue-50 dark:bg-blue-950/30 animate-pulse", children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-3", children: [
|
|
21
|
+
/* @__PURE__ */ jsx(
|
|
22
|
+
Loader2,
|
|
23
|
+
{
|
|
24
|
+
size: 20,
|
|
25
|
+
className: "text-blue-600 dark:text-blue-400 animate-spin"
|
|
26
|
+
}
|
|
27
|
+
),
|
|
28
|
+
/* @__PURE__ */ jsxs("div", { className: "flex-1", children: [
|
|
29
|
+
/* @__PURE__ */ jsx("div", { className: "font-semibold text-blue-900 dark:text-blue-100", children: "Executing Tool" }),
|
|
30
|
+
/* @__PURE__ */ jsx("div", { className: "text-sm text-blue-700 dark:text-blue-300 mt-0.5", children: toolName })
|
|
31
|
+
] })
|
|
32
|
+
] }) });
|
|
33
|
+
}
|
|
34
|
+
);
|
|
35
|
+
ToolExecutionIndicator.displayName = "ToolExecutionIndicator";
|
|
36
|
+
var ToolCallDetail = memo(
|
|
37
|
+
({ tool, theme }) => {
|
|
38
|
+
const [isExpanded, setIsExpanded] = useState(false);
|
|
39
|
+
const [copiedField, setCopiedField] = useState(null);
|
|
40
|
+
const isDark = theme === "dark";
|
|
41
|
+
const syntaxTheme = isDark ? vscDarkPlus : oneLight;
|
|
42
|
+
const bgColor = isDark ? "#1e1e1e" : "#efefef";
|
|
43
|
+
const formatJson = (obj) => {
|
|
44
|
+
try {
|
|
45
|
+
return JSON.stringify(obj, null, 2);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
return String(obj);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const handleCopy = (content, fieldName) => {
|
|
51
|
+
navigator.clipboard.writeText(content).then(() => {
|
|
52
|
+
setCopiedField(fieldName);
|
|
53
|
+
setTimeout(() => setCopiedField(null), 2e3);
|
|
54
|
+
}).catch((err) => console.error("Failed to copy: ", err));
|
|
55
|
+
};
|
|
56
|
+
const getStatusIcon = () => {
|
|
57
|
+
if (tool.status === "SUCCESS") {
|
|
58
|
+
return /* @__PURE__ */ jsx(
|
|
59
|
+
CheckCircle2,
|
|
60
|
+
{
|
|
61
|
+
size: 16,
|
|
62
|
+
className: "text-green-600 dark:text-green-400"
|
|
63
|
+
}
|
|
64
|
+
);
|
|
65
|
+
} else if (tool.error) {
|
|
66
|
+
return /* @__PURE__ */ jsx(
|
|
67
|
+
XCircle,
|
|
68
|
+
{
|
|
69
|
+
size: 16,
|
|
70
|
+
className: "text-red-600 dark:text-red-400"
|
|
71
|
+
}
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
};
|
|
76
|
+
const getStatusColor = () => {
|
|
77
|
+
if (tool.status === "SUCCESS") {
|
|
78
|
+
return "bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300";
|
|
79
|
+
} else if (tool.error) {
|
|
80
|
+
return "bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300";
|
|
81
|
+
}
|
|
82
|
+
return "bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300";
|
|
83
|
+
};
|
|
84
|
+
const argsString = useMemo(() => formatJson(tool.args), [tool.args]);
|
|
85
|
+
const resultString = useMemo(
|
|
86
|
+
() => formatJson(tool.result),
|
|
87
|
+
[tool.result]
|
|
88
|
+
);
|
|
89
|
+
return /* @__PURE__ */ jsxs(
|
|
90
|
+
"div",
|
|
91
|
+
{
|
|
92
|
+
className: "my-4 border-2 border-gray-300 dark:border-gray-600 rounded-lg overflow-hidden bg-white dark:bg-gray-900 shadow-sm",
|
|
93
|
+
children: [
|
|
94
|
+
/* @__PURE__ */ jsx(
|
|
95
|
+
"div",
|
|
96
|
+
{
|
|
97
|
+
onClick: () => setIsExpanded(!isExpanded),
|
|
98
|
+
className: "flex items-center justify-between p-3 cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors bg-white dark:bg-gray-900",
|
|
99
|
+
children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2 flex-1 min-w-0", children: [
|
|
100
|
+
isExpanded ? /* @__PURE__ */ jsx(
|
|
101
|
+
ChevronDown,
|
|
102
|
+
{
|
|
103
|
+
size: 18,
|
|
104
|
+
className: "text-gray-600 dark:text-gray-300 flex-shrink-0"
|
|
105
|
+
}
|
|
106
|
+
) : /* @__PURE__ */ jsx(
|
|
107
|
+
ChevronRight,
|
|
108
|
+
{
|
|
109
|
+
size: 18,
|
|
110
|
+
className: "text-gray-600 dark:text-gray-300 flex-shrink-0"
|
|
111
|
+
}
|
|
112
|
+
),
|
|
113
|
+
getStatusIcon(),
|
|
114
|
+
/* @__PURE__ */ jsx("span", { className: "font-mono text-sm font-semibold text-gray-900 dark:text-gray-100 truncate", children: tool.toolName }),
|
|
115
|
+
/* @__PURE__ */ jsx(
|
|
116
|
+
"span",
|
|
117
|
+
{
|
|
118
|
+
className: `text-xs px-2 py-0.5 rounded-full flex-shrink-0 ${getStatusColor()}`,
|
|
119
|
+
children: tool.status
|
|
120
|
+
}
|
|
121
|
+
)
|
|
122
|
+
] })
|
|
123
|
+
}
|
|
124
|
+
),
|
|
125
|
+
isExpanded && /* @__PURE__ */ jsxs("div", { className: "border-t border-gray-200 dark:border-gray-700 bg-gray-50 dark:bg-gray-900/50", children: [
|
|
126
|
+
/* @__PURE__ */ jsxs("div", { className: "p-3 border-b border-gray-200 dark:border-gray-700 bg-white dark:bg-gray-800", children: [
|
|
127
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between mb-2", children: [
|
|
128
|
+
/* @__PURE__ */ jsx("span", { className: "text-xs font-semibold text-gray-700 dark:text-gray-300 uppercase", children: "Input" }),
|
|
129
|
+
/* @__PURE__ */ jsx(
|
|
130
|
+
"button",
|
|
131
|
+
{
|
|
132
|
+
onClick: (e) => {
|
|
133
|
+
e.stopPropagation();
|
|
134
|
+
handleCopy(argsString, "input");
|
|
135
|
+
},
|
|
136
|
+
className: "p-1 hover:bg-gray-200 dark:hover:bg-gray-700 rounded transition-colors",
|
|
137
|
+
title: "Copy input",
|
|
138
|
+
children: copiedField === "input" ? /* @__PURE__ */ jsx(
|
|
139
|
+
Check,
|
|
140
|
+
{
|
|
141
|
+
size: 14,
|
|
142
|
+
className: "text-green-600"
|
|
143
|
+
}
|
|
144
|
+
) : /* @__PURE__ */ jsx(
|
|
145
|
+
Copy,
|
|
146
|
+
{
|
|
147
|
+
size: 14,
|
|
148
|
+
className: "text-gray-500 dark:text-gray-400"
|
|
149
|
+
}
|
|
150
|
+
)
|
|
151
|
+
}
|
|
152
|
+
)
|
|
153
|
+
] }),
|
|
154
|
+
/* @__PURE__ */ jsx(
|
|
155
|
+
SyntaxHighlighter,
|
|
156
|
+
{
|
|
157
|
+
language: "json",
|
|
158
|
+
style: syntaxTheme,
|
|
159
|
+
customStyle: {
|
|
160
|
+
margin: 0,
|
|
161
|
+
background: bgColor,
|
|
162
|
+
fontSize: "0.875rem",
|
|
163
|
+
padding: "1rem"
|
|
164
|
+
},
|
|
165
|
+
codeTagProps: {
|
|
166
|
+
style: {
|
|
167
|
+
backgroundColor: bgColor
|
|
168
|
+
}
|
|
169
|
+
},
|
|
170
|
+
children: argsString
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
] }),
|
|
174
|
+
/* @__PURE__ */ jsxs("div", { className: "p-3 bg-white dark:bg-gray-800", children: [
|
|
175
|
+
/* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between mb-2", children: [
|
|
176
|
+
/* @__PURE__ */ jsx("span", { className: "text-xs font-semibold text-gray-700 dark:text-gray-300 uppercase", children: "Output" }),
|
|
177
|
+
/* @__PURE__ */ jsx(
|
|
178
|
+
"button",
|
|
179
|
+
{
|
|
180
|
+
onClick: (e) => {
|
|
181
|
+
e.stopPropagation();
|
|
182
|
+
handleCopy(resultString, "output");
|
|
183
|
+
},
|
|
184
|
+
className: "p-1 hover:bg-gray-200 dark:hover:bg-gray-700 rounded transition-colors",
|
|
185
|
+
title: "Copy output",
|
|
186
|
+
children: copiedField === "output" ? /* @__PURE__ */ jsx(
|
|
187
|
+
Check,
|
|
188
|
+
{
|
|
189
|
+
size: 14,
|
|
190
|
+
className: "text-green-600"
|
|
191
|
+
}
|
|
192
|
+
) : /* @__PURE__ */ jsx(
|
|
193
|
+
Copy,
|
|
194
|
+
{
|
|
195
|
+
size: 14,
|
|
196
|
+
className: "text-gray-500 dark:text-gray-400"
|
|
197
|
+
}
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
)
|
|
201
|
+
] }),
|
|
202
|
+
/* @__PURE__ */ jsx(
|
|
203
|
+
SyntaxHighlighter,
|
|
204
|
+
{
|
|
205
|
+
language: "json",
|
|
206
|
+
style: syntaxTheme,
|
|
207
|
+
customStyle: {
|
|
208
|
+
margin: 0,
|
|
209
|
+
background: bgColor,
|
|
210
|
+
fontSize: "0.875rem",
|
|
211
|
+
padding: "1rem"
|
|
212
|
+
},
|
|
213
|
+
codeTagProps: {
|
|
214
|
+
style: {
|
|
215
|
+
backgroundColor: bgColor
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
children: tool.error ? tool.error : resultString
|
|
219
|
+
}
|
|
220
|
+
)
|
|
221
|
+
] })
|
|
222
|
+
] })
|
|
223
|
+
]
|
|
224
|
+
}
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
);
|
|
228
|
+
ToolCallDetail.displayName = "ToolCallDetail";
|
|
229
|
+
var ToolExecutionWidget = memo(
|
|
230
|
+
({ steps, theme }) => {
|
|
231
|
+
const [isExpanded, setIsExpanded] = useState(false);
|
|
232
|
+
const allTools = useMemo(() => {
|
|
233
|
+
return steps.filter((step) => step.tools && step.tools.length > 0).flatMap((step) => step.tools || []);
|
|
234
|
+
}, [steps]);
|
|
235
|
+
if (allTools.length === 0) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
const successCount = allTools.filter(
|
|
239
|
+
(t) => t.status === "SUCCESS"
|
|
240
|
+
).length;
|
|
241
|
+
const errorCount = allTools.filter((t) => t.error).length;
|
|
242
|
+
return /* @__PURE__ */ jsxs("div", { className: "my-4 border-2 border-gray-300 dark:border-gray-600 rounded-lg overflow-hidden bg-white dark:bg-gray-900 shadow-sm", children: [
|
|
243
|
+
/* @__PURE__ */ jsx(
|
|
244
|
+
"div",
|
|
245
|
+
{
|
|
246
|
+
onClick: () => setIsExpanded(!isExpanded),
|
|
247
|
+
className: "flex items-center justify-between p-3 cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors",
|
|
248
|
+
children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
|
|
249
|
+
isExpanded ? /* @__PURE__ */ jsx(
|
|
250
|
+
ChevronDown,
|
|
251
|
+
{
|
|
252
|
+
size: 20,
|
|
253
|
+
className: "text-gray-600 dark:text-gray-300"
|
|
254
|
+
}
|
|
255
|
+
) : /* @__PURE__ */ jsx(
|
|
256
|
+
ChevronRight,
|
|
257
|
+
{
|
|
258
|
+
size: 20,
|
|
259
|
+
className: "text-gray-600 dark:text-gray-300"
|
|
260
|
+
}
|
|
261
|
+
),
|
|
262
|
+
/* @__PURE__ */ jsx("span", { className: "font-semibold text-gray-800 dark:text-white", children: "Tool Execution" }),
|
|
263
|
+
/* @__PURE__ */ jsxs("span", { className: "text-xs px-2 py-0.5 bg-purple-100 dark:bg-purple-900/30 text-purple-700 dark:text-purple-300 rounded-full", children: [
|
|
264
|
+
allTools.length,
|
|
265
|
+
" ",
|
|
266
|
+
allTools.length === 1 ? "tool" : "tools"
|
|
267
|
+
] }),
|
|
268
|
+
successCount > 0 && /* @__PURE__ */ jsxs("span", { className: "text-xs px-2 py-0.5 bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300 rounded-full flex items-center gap-1", children: [
|
|
269
|
+
/* @__PURE__ */ jsx(CheckCircle2, { size: 12 }),
|
|
270
|
+
successCount
|
|
271
|
+
] }),
|
|
272
|
+
errorCount > 0 && /* @__PURE__ */ jsxs("span", { className: "text-xs px-2 py-0.5 bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300 rounded-full flex items-center gap-1", children: [
|
|
273
|
+
/* @__PURE__ */ jsx(XCircle, { size: 12 }),
|
|
274
|
+
errorCount
|
|
275
|
+
] })
|
|
276
|
+
] })
|
|
277
|
+
}
|
|
278
|
+
),
|
|
279
|
+
isExpanded && /* @__PURE__ */ jsx("div", { className: "border-t border-gray-200 dark:border-gray-700 p-3 space-y-2 bg-gray-50 dark:bg-gray-800", children: allTools.map((tool) => /* @__PURE__ */ jsx(
|
|
280
|
+
ToolCallDetail,
|
|
281
|
+
{
|
|
282
|
+
tool,
|
|
283
|
+
theme
|
|
284
|
+
},
|
|
285
|
+
tool.id
|
|
286
|
+
)) })
|
|
287
|
+
] });
|
|
288
|
+
}
|
|
289
|
+
);
|
|
290
|
+
ToolExecutionWidget.displayName = "ToolExecutionWidget";
|
|
291
|
+
|
|
292
|
+
export {
|
|
293
|
+
ToolExecutionIndicator,
|
|
294
|
+
ToolExecutionWidget
|
|
295
|
+
};
|