@spteck/react-controls-v2 2.0.11 → 2.0.13

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,116 @@ 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
+ const dataContent = line2.substring(5).trim();
25458
+ if (currentData && dataContent) {
25459
+ currentData += "\n" + dataContent;
25460
+ } else {
25461
+ currentData = dataContent;
25462
+ }
25463
+ } else if (line2 === "") {
25464
+ if (!currentData) {
25465
+ currentEvent = "";
25466
+ currentData = "";
25467
+ continue;
25468
+ }
25469
+ if (currentEvent === "error") {
25470
+ try {
25471
+ const errorData = JSON.parse(currentData);
25472
+ const errorMessage = errorData.message || "An error occurred";
25473
+ if (errorMessage.includes("429") || errorMessage.toLowerCase().includes("rate limit")) {
25474
+ throw new Error("Rate limit exceeded. Please wait a moment and try again.");
25475
+ } else if (errorMessage.includes("quota")) {
25476
+ throw new Error("API quota exceeded. Please try again later.");
25477
+ } else {
25478
+ throw new Error(errorMessage);
25479
+ }
25480
+ } catch (parseError) {
25481
+ if (parseError instanceof Error && parseError.message.includes("Rate limit")) {
25482
+ throw parseError;
25483
+ }
25484
+ throw new Error(currentData || "An error occurred during streaming");
25467
25485
  }
25468
- } catch (parseError) {
25469
- if (parseError instanceof Error && parseError.message.includes("Rate limit")) {
25470
- throw parseError;
25486
+ } else if (currentEvent === "done") {
25487
+ done = true;
25488
+ } else if (currentEvent === "tool") {
25489
+ } else {
25490
+ try {
25491
+ const parsed = JSON.parse(currentData);
25492
+ if (parsed.status && Object.keys(parsed).length === 1) {
25493
+ } else if (parsed.content) {
25494
+ const contentStr = typeof parsed.content === "string" ? parsed.content : JSON.stringify(parsed.content, null, 2);
25495
+ accumulatedData += contentStr;
25496
+ setData(accumulatedData);
25497
+ if (onChunk) {
25498
+ onChunk(contentStr);
25499
+ }
25500
+ } else {
25501
+ const formattedJson = JSON.stringify(parsed, null, 2);
25502
+ accumulatedData += formattedJson + "\n";
25503
+ setData(accumulatedData);
25504
+ if (onChunk) {
25505
+ onChunk(formattedJson);
25506
+ }
25507
+ }
25508
+ } catch {
25509
+ accumulatedData += currentData + "\n";
25510
+ setData(accumulatedData);
25511
+ if (onChunk) {
25512
+ onChunk(currentData);
25513
+ }
25471
25514
  }
25472
- throw new Error(currentData || "An error occurred during streaming");
25473
25515
  }
25474
- } else if (currentEvent === "done") {
25475
- done = true;
25476
- } else if (currentData && currentEvent !== "tool") {
25516
+ currentEvent = "";
25517
+ currentData = "";
25518
+ }
25519
+ }
25520
+ } else {
25521
+ const lines = buffer.split("\n");
25522
+ buffer = lines.pop() || "";
25523
+ for (const line2 of lines) {
25524
+ if (line2.trim()) {
25477
25525
  try {
25478
- const parsedData = JSON.parse(currentData);
25479
- if (!parsedData.status || Object.keys(parsedData).length > 1) {
25480
- accumulatedData += currentData;
25526
+ const parsed = JSON.parse(line2);
25527
+ if ((_d2 = (_c2 = (_b2 = parsed.choices) == null ? void 0 : _b2[0]) == null ? void 0 : _c2.delta) == null ? void 0 : _d2.content) {
25528
+ const content = parsed.choices[0].delta.content;
25529
+ accumulatedData += content;
25481
25530
  setData(accumulatedData);
25482
25531
  if (onChunk) {
25483
- onChunk(currentData);
25532
+ onChunk(content);
25533
+ }
25534
+ } else if (parsed.content) {
25535
+ accumulatedData += parsed.content;
25536
+ setData(accumulatedData);
25537
+ if (onChunk) {
25538
+ onChunk(parsed.content);
25539
+ }
25540
+ } else if (parsed.done === true || parsed.type === "done") {
25541
+ done = true;
25542
+ } else if (!parsed.status) {
25543
+ accumulatedData += line2;
25544
+ setData(accumulatedData);
25545
+ if (onChunk) {
25546
+ onChunk(line2);
25484
25547
  }
25485
25548
  }
25486
25549
  } catch {
25487
- accumulatedData += currentData;
25550
+ accumulatedData += line2 + "\n";
25488
25551
  setData(accumulatedData);
25489
25552
  if (onChunk) {
25490
- onChunk(currentData);
25553
+ onChunk(line2 + "\n");
25491
25554
  }
25492
25555
  }
25493
25556
  }
25494
- currentEvent = "";
25495
- currentData = "";
25496
25557
  }
25497
25558
  }
25498
25559
  }