@spteck/react-controls-v2 2.0.9 → 2.0.11
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/dist/index.mjs
CHANGED
|
@@ -25437,15 +25437,63 @@ function useStreamRequest() {
|
|
|
25437
25437
|
const decoder = new TextDecoder("utf-8");
|
|
25438
25438
|
let done = false;
|
|
25439
25439
|
let accumulatedData = "";
|
|
25440
|
+
let buffer = "";
|
|
25440
25441
|
while (!done) {
|
|
25441
25442
|
const { value, done: doneReading } = await reader.read();
|
|
25442
25443
|
done = doneReading;
|
|
25443
25444
|
if (value) {
|
|
25444
25445
|
const chunk = decoder.decode(value, { stream: true });
|
|
25445
|
-
|
|
25446
|
-
|
|
25447
|
-
|
|
25448
|
-
|
|
25446
|
+
buffer += chunk;
|
|
25447
|
+
const lines = buffer.split("\n");
|
|
25448
|
+
buffer = lines.pop() || "";
|
|
25449
|
+
let currentEvent = "";
|
|
25450
|
+
let currentData = "";
|
|
25451
|
+
for (const line2 of lines) {
|
|
25452
|
+
if (line2.startsWith("event:")) {
|
|
25453
|
+
currentEvent = line2.substring(6).trim();
|
|
25454
|
+
} else if (line2.startsWith("data:")) {
|
|
25455
|
+
currentData = line2.substring(5).trim();
|
|
25456
|
+
} else if (line2 === "") {
|
|
25457
|
+
if (currentEvent === "error") {
|
|
25458
|
+
try {
|
|
25459
|
+
const errorData = JSON.parse(currentData);
|
|
25460
|
+
const errorMessage = errorData.message || "An error occurred";
|
|
25461
|
+
if (errorMessage.includes("429") || errorMessage.toLowerCase().includes("rate limit")) {
|
|
25462
|
+
throw new Error("Rate limit exceeded. Please wait a moment and try again.");
|
|
25463
|
+
} else if (errorMessage.includes("quota")) {
|
|
25464
|
+
throw new Error("API quota exceeded. Please try again later.");
|
|
25465
|
+
} else {
|
|
25466
|
+
throw new Error(errorMessage);
|
|
25467
|
+
}
|
|
25468
|
+
} catch (parseError) {
|
|
25469
|
+
if (parseError instanceof Error && parseError.message.includes("Rate limit")) {
|
|
25470
|
+
throw parseError;
|
|
25471
|
+
}
|
|
25472
|
+
throw new Error(currentData || "An error occurred during streaming");
|
|
25473
|
+
}
|
|
25474
|
+
} else if (currentEvent === "done") {
|
|
25475
|
+
done = true;
|
|
25476
|
+
} else if (currentData && currentEvent !== "tool") {
|
|
25477
|
+
try {
|
|
25478
|
+
const parsedData = JSON.parse(currentData);
|
|
25479
|
+
if (!parsedData.status || Object.keys(parsedData).length > 1) {
|
|
25480
|
+
accumulatedData += currentData;
|
|
25481
|
+
setData(accumulatedData);
|
|
25482
|
+
if (onChunk) {
|
|
25483
|
+
onChunk(currentData);
|
|
25484
|
+
}
|
|
25485
|
+
}
|
|
25486
|
+
} catch {
|
|
25487
|
+
accumulatedData += currentData;
|
|
25488
|
+
setData(accumulatedData);
|
|
25489
|
+
if (onChunk) {
|
|
25490
|
+
onChunk(currentData);
|
|
25491
|
+
}
|
|
25492
|
+
}
|
|
25493
|
+
}
|
|
25494
|
+
currentEvent = "";
|
|
25495
|
+
currentData = "";
|
|
25496
|
+
}
|
|
25449
25497
|
}
|
|
25450
25498
|
}
|
|
25451
25499
|
}
|
|
@@ -25575,11 +25623,23 @@ const AIAssistant = (props) => {
|
|
|
25575
25623
|
});
|
|
25576
25624
|
} catch (err) {
|
|
25577
25625
|
console.error("Error during streaming:", err);
|
|
25626
|
+
let errorMessage = "Error occurred while processing your request.";
|
|
25627
|
+
if (err instanceof Error) {
|
|
25628
|
+
if (err.message.includes("Rate limit") || err.message.includes("429")) {
|
|
25629
|
+
errorMessage = "⚠️ Rate limit exceeded. Please wait a moment before sending another message.";
|
|
25630
|
+
} else if (err.message.includes("quota")) {
|
|
25631
|
+
errorMessage = "⚠️ API quota exceeded. Please try again later.";
|
|
25632
|
+
} else if (err.message.includes("abort")) {
|
|
25633
|
+
errorMessage = "Request was cancelled.";
|
|
25634
|
+
} else {
|
|
25635
|
+
errorMessage = err.message;
|
|
25636
|
+
}
|
|
25637
|
+
}
|
|
25578
25638
|
dispatch({
|
|
25579
25639
|
type: "UPDATE_LAST_ASSISTANT_MESSAGE",
|
|
25580
25640
|
message: {
|
|
25581
25641
|
...assistantMessage,
|
|
25582
|
-
message:
|
|
25642
|
+
message: errorMessage,
|
|
25583
25643
|
status: "failed"
|
|
25584
25644
|
}
|
|
25585
25645
|
});
|