@voltagent/core 0.1.25 → 0.1.26

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.js CHANGED
@@ -1420,51 +1420,109 @@ app.openapi(streamRoute, (c) => __async(void 0, null, function* () {
1420
1420
  const stream = new ReadableStream({
1421
1421
  start(controller) {
1422
1422
  return __async(this, null, function* () {
1423
+ var _a2;
1423
1424
  try {
1425
+ let streamClosed = false;
1426
+ const safeEnqueue = /* @__PURE__ */ __name((data) => {
1427
+ if (!streamClosed) {
1428
+ try {
1429
+ controller.enqueue(new TextEncoder().encode(data));
1430
+ } catch (e) {
1431
+ console.error("Failed to enqueue data:", e);
1432
+ streamClosed = true;
1433
+ }
1434
+ }
1435
+ }, "safeEnqueue");
1436
+ const safeClose = /* @__PURE__ */ __name(() => {
1437
+ if (!streamClosed) {
1438
+ try {
1439
+ controller.close();
1440
+ streamClosed = true;
1441
+ } catch (e) {
1442
+ console.error("Failed to close controller:", e);
1443
+ }
1444
+ }
1445
+ }, "safeClose");
1424
1446
  const response = yield agent.streamText(input, __spreadProps(__spreadValues({}, options), {
1425
1447
  provider: {
1426
1448
  maxTokens: options.maxTokens,
1427
- temperature: options.temperature
1449
+ temperature: options.temperature,
1450
+ // Add onError callback to handle streaming errors
1451
+ onError: (error2) => __async(this, null, function* () {
1452
+ var _a3;
1453
+ const errorData = {
1454
+ error: (_a3 = error2 == null ? void 0 : error2.message) != null ? _a3 : "Streaming failed",
1455
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1456
+ type: "error",
1457
+ code: error2.code || "STREAM_ERROR"
1458
+ };
1459
+ const errorMessage = `data: ${JSON.stringify(errorData)}
1460
+
1461
+ `;
1462
+ safeEnqueue(errorMessage);
1463
+ safeClose();
1464
+ })
1428
1465
  }
1429
1466
  }));
1430
1467
  try {
1431
- for (var iter = __forAwait(response.textStream), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
1432
- const chunk = temp.value;
1433
- const data = {
1434
- text: chunk,
1468
+ try {
1469
+ for (var iter = __forAwait(response.textStream), more, temp, error; more = !(temp = yield iter.next()).done; more = false) {
1470
+ const chunk = temp.value;
1471
+ if (streamClosed)
1472
+ break;
1473
+ const data = {
1474
+ text: chunk,
1475
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1476
+ type: "text"
1477
+ };
1478
+ const sseMessage = `data: ${JSON.stringify(data)}
1479
+
1480
+ `;
1481
+ safeEnqueue(sseMessage);
1482
+ }
1483
+ } catch (temp) {
1484
+ error = [temp];
1485
+ } finally {
1486
+ try {
1487
+ more && (temp = iter.return) && (yield temp.call(iter));
1488
+ } finally {
1489
+ if (error)
1490
+ throw error[0];
1491
+ }
1492
+ }
1493
+ if (!streamClosed) {
1494
+ const completionData = {
1495
+ done: true,
1435
1496
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1436
- type: "text"
1497
+ type: "completion"
1437
1498
  };
1438
- const sseMessage = `data: ${JSON.stringify(data)}
1499
+ const completionMessage = `data: ${JSON.stringify(completionData)}
1439
1500
 
1440
1501
  `;
1441
- controller.enqueue(new TextEncoder().encode(sseMessage));
1502
+ safeEnqueue(completionMessage);
1503
+ safeClose();
1442
1504
  }
1443
- } catch (temp) {
1444
- error = [temp];
1445
- } finally {
1446
- try {
1447
- more && (temp = iter.return) && (yield temp.call(iter));
1448
- } finally {
1449
- if (error)
1450
- throw error[0];
1451
- }
1452
- }
1453
- const completionData = {
1454
- done: true,
1455
- timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1456
- type: "completion"
1457
- };
1458
- const completionMessage = `data: ${JSON.stringify(completionData)}
1505
+ } catch (iterationError) {
1506
+ console.error("Error during stream iteration:", iterationError);
1507
+ const errorData = {
1508
+ error: (_a2 = iterationError == null ? void 0 : iterationError.message) != null ? _a2 : "Stream iteration failed",
1509
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1510
+ type: "error",
1511
+ code: "ITERATION_ERROR"
1512
+ };
1513
+ const errorMessage = `data: ${JSON.stringify(errorData)}
1459
1514
 
1460
1515
  `;
1461
- controller.enqueue(new TextEncoder().encode(completionMessage));
1462
- controller.close();
1516
+ safeEnqueue(errorMessage);
1517
+ safeClose();
1518
+ }
1463
1519
  } catch (error2) {
1520
+ console.error("Error during stream setup:", error2);
1464
1521
  const errorData = {
1465
- error: error2 instanceof Error ? error2.message : "Streaming failed",
1522
+ error: error2 instanceof Error ? error2.message : "Stream setup failed",
1466
1523
  timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1467
- type: "error"
1524
+ type: "error",
1525
+ code: "SETUP_ERROR"
1468
1526
  };
1469
1527
  const errorMessage = `data: ${JSON.stringify(errorData)}
1470
1528
 
@@ -1472,12 +1530,12 @@ app.openapi(streamRoute, (c) => __async(void 0, null, function* () {
1472
1530
  try {
1473
1531
  controller.enqueue(new TextEncoder().encode(errorMessage));
1474
1532
  } catch (e) {
1475
- console.error("Failed to enqueue error message:", e);
1533
+ console.error("Failed to enqueue setup error message:", e);
1476
1534
  }
1477
1535
  try {
1478
1536
  controller.close();
1479
1537
  } catch (e) {
1480
- console.error("Failed to close controller after error:", e);
1538
+ console.error("Failed to close controller after setup error:", e);
1481
1539
  }
1482
1540
  }
1483
1541
  });
@@ -1553,56 +1611,125 @@ app.openapi(streamObjectRoute, (c) => __async(void 0, null, function* () {
1553
1611
  options = {}
1554
1612
  } = c.req.valid("json");
1555
1613
  const schemaInZodObject = (0, import_zod_from_json_schema.convertJsonSchemaToZod)(schema);
1556
- const agentStream = yield agent.streamObject(input, schemaInZodObject, options);
1557
1614
  const sseStream = new ReadableStream({
1558
1615
  start(controller) {
1559
1616
  return __async(this, null, function* () {
1560
- const reader = agentStream.objectStream.getReader();
1617
+ var _a2;
1561
1618
  try {
1562
- while (true) {
1563
- const { done, value } = yield reader.read();
1564
- if (done) {
1565
- const completionData = {
1566
- done: true,
1567
- type: "completion",
1568
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
1619
+ let streamClosed = false;
1620
+ const safeEnqueue = /* @__PURE__ */ __name((data) => {
1621
+ if (!streamClosed) {
1622
+ try {
1623
+ controller.enqueue(new TextEncoder().encode(data));
1624
+ } catch (e) {
1625
+ console.error("Failed to enqueue data:", e);
1626
+ streamClosed = true;
1627
+ }
1628
+ }
1629
+ }, "safeEnqueue");
1630
+ const safeClose = /* @__PURE__ */ __name(() => {
1631
+ if (!streamClosed) {
1632
+ try {
1633
+ controller.close();
1634
+ streamClosed = true;
1635
+ } catch (e) {
1636
+ console.error("Failed to close controller:", e);
1637
+ }
1638
+ }
1639
+ }, "safeClose");
1640
+ const agentStream = yield agent.streamObject(input, schemaInZodObject, __spreadProps(__spreadValues({}, options), {
1641
+ provider: __spreadProps(__spreadValues({}, options.provider), {
1642
+ // Add onError callback to handle streaming errors
1643
+ onError: (error) => __async(this, null, function* () {
1644
+ var _a3;
1645
+ console.error("Object stream error occurred:", error);
1646
+ const errorData = {
1647
+ error: (_a3 = error == null ? void 0 : error.message) != null ? _a3 : "Object streaming failed",
1648
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1649
+ type: "error",
1650
+ code: error.code || "STREAM_ERROR"
1651
+ };
1652
+ const errorMessage = `data: ${JSON.stringify(errorData)}
1653
+
1654
+ `;
1655
+ safeEnqueue(errorMessage);
1656
+ safeClose();
1657
+ })
1658
+ })
1659
+ }));
1660
+ const reader = agentStream.objectStream.getReader();
1661
+ try {
1662
+ while (true) {
1663
+ if (streamClosed)
1664
+ break;
1665
+ const { done, value } = yield reader.read();
1666
+ if (done) {
1667
+ if (!streamClosed) {
1668
+ const completionData = {
1669
+ done: true,
1670
+ type: "completion",
1671
+ timestamp: (/* @__PURE__ */ new Date()).toISOString()
1672
+ };
1673
+ const completionMessage = `data: ${JSON.stringify(completionData)}
1674
+
1675
+ `;
1676
+ safeEnqueue(completionMessage);
1677
+ safeClose();
1678
+ }
1679
+ break;
1680
+ }
1681
+ const objectData = {
1682
+ object: value,
1683
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1684
+ type: "object"
1569
1685
  };
1570
- controller.enqueue(`data: ${JSON.stringify(completionData)}
1686
+ const sseMessage = `data: ${JSON.stringify(objectData)}
1571
1687
 
1572
- `);
1573
- break;
1688
+ `;
1689
+ safeEnqueue(sseMessage);
1574
1690
  }
1575
- controller.enqueue(`data: ${JSON.stringify(value)}
1691
+ } catch (iterationError) {
1692
+ console.error("Error during object stream iteration:", iterationError);
1693
+ const errorData = {
1694
+ error: (_a2 = iterationError == null ? void 0 : iterationError.message) != null ? _a2 : "Object stream iteration failed",
1695
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1696
+ type: "error",
1697
+ code: "ITERATION_ERROR"
1698
+ };
1699
+ const errorMessage = `data: ${JSON.stringify(errorData)}
1576
1700
 
1577
- `);
1701
+ `;
1702
+ safeEnqueue(errorMessage);
1703
+ safeClose();
1704
+ } finally {
1705
+ reader.releaseLock();
1578
1706
  }
1579
- controller.close();
1580
1707
  } catch (error) {
1708
+ console.error("Error during object stream setup:", error);
1581
1709
  const errorData = {
1582
- error: error instanceof Error ? error.message : "Object streaming failed",
1710
+ error: error instanceof Error ? error.message : "Object stream setup failed",
1711
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1583
1712
  type: "error",
1584
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
1713
+ code: "SETUP_ERROR"
1585
1714
  };
1586
- try {
1587
- controller.enqueue(`data: ${JSON.stringify(errorData)}
1715
+ const errorMessage = `data: ${JSON.stringify(errorData)}
1588
1716
 
1589
- `);
1717
+ `;
1718
+ try {
1719
+ controller.enqueue(new TextEncoder().encode(errorMessage));
1590
1720
  } catch (e) {
1591
- console.error("Failed to enqueue error message:", e);
1721
+ console.error("Failed to enqueue setup error message:", e);
1592
1722
  }
1593
1723
  try {
1594
1724
  controller.close();
1595
1725
  } catch (e) {
1596
- console.error("Failed to close controller after error:", e);
1726
+ console.error("Failed to close controller after setup error:", e);
1597
1727
  }
1598
- } finally {
1599
- reader.releaseLock();
1600
1728
  }
1601
1729
  });
1602
1730
  },
1603
1731
  cancel(reason) {
1604
1732
  console.log("Object Stream cancelled:", reason);
1605
- agentStream.cancel(reason);
1606
1733
  }
1607
1734
  });
1608
1735
  return c.body(sseStream, {