@spteck/react-controls-v2 2.0.9 → 2.0.10
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,11 +25437,45 @@ 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 });
|
|
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
|
+
}
|
|
25475
|
+
currentEvent = "";
|
|
25476
|
+
currentData = "";
|
|
25477
|
+
}
|
|
25478
|
+
}
|
|
25445
25479
|
accumulatedData += chunk;
|
|
25446
25480
|
setData(accumulatedData);
|
|
25447
25481
|
if (onChunk) {
|
|
@@ -25575,11 +25609,23 @@ const AIAssistant = (props) => {
|
|
|
25575
25609
|
});
|
|
25576
25610
|
} catch (err) {
|
|
25577
25611
|
console.error("Error during streaming:", err);
|
|
25612
|
+
let errorMessage = "Error occurred while processing your request.";
|
|
25613
|
+
if (err instanceof Error) {
|
|
25614
|
+
if (err.message.includes("Rate limit") || err.message.includes("429")) {
|
|
25615
|
+
errorMessage = "⚠️ Rate limit exceeded. Please wait a moment before sending another message.";
|
|
25616
|
+
} else if (err.message.includes("quota")) {
|
|
25617
|
+
errorMessage = "⚠️ API quota exceeded. Please try again later.";
|
|
25618
|
+
} else if (err.message.includes("abort")) {
|
|
25619
|
+
errorMessage = "Request was cancelled.";
|
|
25620
|
+
} else {
|
|
25621
|
+
errorMessage = err.message;
|
|
25622
|
+
}
|
|
25623
|
+
}
|
|
25578
25624
|
dispatch({
|
|
25579
25625
|
type: "UPDATE_LAST_ASSISTANT_MESSAGE",
|
|
25580
25626
|
message: {
|
|
25581
25627
|
...assistantMessage,
|
|
25582
|
-
message:
|
|
25628
|
+
message: errorMessage,
|
|
25583
25629
|
status: "failed"
|
|
25584
25630
|
}
|
|
25585
25631
|
});
|