@spteck/react-controls-v2 2.0.11 → 2.0.12

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
@@ -25413,7 +25413,7 @@ function useStreamRequest() {
25413
25413
  const abortControllerRef = useRef(null);
25414
25414
  const sendRequest = useCallback(
25415
25415
  async (endpoint, payload, options, onChunk) => {
25416
- var _a3;
25416
+ var _a3, _b2, _c2, _d2;
25417
25417
  setData("");
25418
25418
  setError(null);
25419
25419
  setIsLoading(true);
@@ -25444,55 +25444,106 @@ function useStreamRequest() {
25444
25444
  if (value) {
25445
25445
  const chunk = decoder.decode(value, { stream: true });
25446
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);
25447
+ const isSSE = buffer.includes("event:") || buffer.includes("data:");
25448
+ if (isSSE) {
25449
+ const lines = buffer.split("\n");
25450
+ buffer = lines.pop() || "";
25451
+ let currentEvent = "";
25452
+ let currentData = "";
25453
+ for (const line2 of lines) {
25454
+ if (line2.startsWith("event:")) {
25455
+ currentEvent = line2.substring(6).trim();
25456
+ } else if (line2.startsWith("data:")) {
25457
+ currentData = line2.substring(5).trim();
25458
+ } else if (line2 === "") {
25459
+ if (currentEvent === "error") {
25460
+ try {
25461
+ const errorData = JSON.parse(currentData);
25462
+ const errorMessage = errorData.message || "An error occurred";
25463
+ if (errorMessage.includes("429") || errorMessage.toLowerCase().includes("rate limit")) {
25464
+ throw new Error("Rate limit exceeded. Please wait a moment and try again.");
25465
+ } else if (errorMessage.includes("quota")) {
25466
+ throw new Error("API quota exceeded. Please try again later.");
25467
+ } else {
25468
+ throw new Error(errorMessage);
25469
+ }
25470
+ } catch (parseError) {
25471
+ if (parseError instanceof Error && parseError.message.includes("Rate limit")) {
25472
+ throw parseError;
25473
+ }
25474
+ throw new Error(currentData || "An error occurred during streaming");
25467
25475
  }
25468
- } catch (parseError) {
25469
- if (parseError instanceof Error && parseError.message.includes("Rate limit")) {
25470
- throw parseError;
25476
+ } else if (currentEvent === "done") {
25477
+ done = true;
25478
+ } else if (currentData) {
25479
+ const shouldSkip = currentEvent === "tool" || currentEvent === "" && currentData.includes('"status"') && !currentData.includes('"content"');
25480
+ if (!shouldSkip) {
25481
+ try {
25482
+ const parsed = JSON.parse(currentData);
25483
+ if (parsed.status && Object.keys(parsed).length === 1) {
25484
+ } else if (parsed.content) {
25485
+ accumulatedData += parsed.content;
25486
+ setData(accumulatedData);
25487
+ if (onChunk) {
25488
+ onChunk(parsed.content);
25489
+ }
25490
+ } else {
25491
+ accumulatedData += currentData;
25492
+ setData(accumulatedData);
25493
+ if (onChunk) {
25494
+ onChunk(currentData);
25495
+ }
25496
+ }
25497
+ } catch {
25498
+ accumulatedData += currentData;
25499
+ setData(accumulatedData);
25500
+ if (onChunk) {
25501
+ onChunk(currentData);
25502
+ }
25503
+ }
25471
25504
  }
25472
- throw new Error(currentData || "An error occurred during streaming");
25473
25505
  }
25474
- } else if (currentEvent === "done") {
25475
- done = true;
25476
- } else if (currentData && currentEvent !== "tool") {
25506
+ currentEvent = "";
25507
+ currentData = "";
25508
+ }
25509
+ }
25510
+ } else {
25511
+ const lines = buffer.split("\n");
25512
+ buffer = lines.pop() || "";
25513
+ for (const line2 of lines) {
25514
+ if (line2.trim()) {
25477
25515
  try {
25478
- const parsedData = JSON.parse(currentData);
25479
- if (!parsedData.status || Object.keys(parsedData).length > 1) {
25480
- accumulatedData += currentData;
25516
+ const parsed = JSON.parse(line2);
25517
+ if ((_d2 = (_c2 = (_b2 = parsed.choices) == null ? void 0 : _b2[0]) == null ? void 0 : _c2.delta) == null ? void 0 : _d2.content) {
25518
+ const content = parsed.choices[0].delta.content;
25519
+ accumulatedData += content;
25520
+ setData(accumulatedData);
25521
+ if (onChunk) {
25522
+ onChunk(content);
25523
+ }
25524
+ } else if (parsed.content) {
25525
+ accumulatedData += parsed.content;
25526
+ setData(accumulatedData);
25527
+ if (onChunk) {
25528
+ onChunk(parsed.content);
25529
+ }
25530
+ } else if (parsed.done === true || parsed.type === "done") {
25531
+ done = true;
25532
+ } else if (!parsed.status) {
25533
+ accumulatedData += line2;
25481
25534
  setData(accumulatedData);
25482
25535
  if (onChunk) {
25483
- onChunk(currentData);
25536
+ onChunk(line2);
25484
25537
  }
25485
25538
  }
25486
25539
  } catch {
25487
- accumulatedData += currentData;
25540
+ accumulatedData += line2 + "\n";
25488
25541
  setData(accumulatedData);
25489
25542
  if (onChunk) {
25490
- onChunk(currentData);
25543
+ onChunk(line2 + "\n");
25491
25544
  }
25492
25545
  }
25493
25546
  }
25494
- currentEvent = "";
25495
- currentData = "";
25496
25547
  }
25497
25548
  }
25498
25549
  }