@harnessio/ai-chat-core 0.0.22 → 0.0.24

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
@@ -1,7 +1,7 @@
1
1
  var H = Object.defineProperty;
2
2
  var J = (n, s, t) => s in n ? H(n, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[s] = t;
3
3
  var u = (n, s, t) => J(n, typeof s != "symbol" ? s + "" : s, t);
4
- import q, { createContext as U, useContext as B, useState as y, useEffect as m, useReducer as V, useCallback as C, useMemo as z } from "react";
4
+ import q, { createContext as B, useContext as U, useState as y, useEffect as b, useReducer as V, useCallback as C, useMemo as z } from "react";
5
5
  class G {
6
6
  constructor() {
7
7
  u(this, "plugins", /* @__PURE__ */ new Map());
@@ -414,7 +414,7 @@ let tt = 0, et = 0;
414
414
  function v() {
415
415
  return `msg-${Date.now()}-${++tt}`;
416
416
  }
417
- function k() {
417
+ function F() {
418
418
  return `thread-${Date.now()}-${++et}`;
419
419
  }
420
420
  class T extends _ {
@@ -553,6 +553,23 @@ class R extends _ {
553
553
  async sendSystemEvent(t) {
554
554
  await this._core.waitForIdle(), await this._core.startSystemEventRun(t);
555
555
  }
556
+ /**
557
+ * Registers a generic background task that keeps the thread reported as
558
+ * running for as long as it is active, even when no stream run is in
559
+ * flight. Use this for UI-driven async work (e.g. a card polling a status
560
+ * API) so the composer shows a running/stop affordance. The work is
561
+ * cancelled when the user presses stop (cancelRun), which invokes the
562
+ * provided onCancel callback. Returns an opaque id for stopBackgroundTask.
563
+ */
564
+ startBackgroundTask(t) {
565
+ return this._core.startBackgroundTask(t);
566
+ }
567
+ /**
568
+ * Removes a previously registered background task by its id.
569
+ */
570
+ stopBackgroundTask(t) {
571
+ this._core.stopBackgroundTask(t);
572
+ }
556
573
  cancelRun() {
557
574
  this._core.cancelRun();
558
575
  }
@@ -591,6 +608,13 @@ class M extends _ {
591
608
  // dispatch against an in-progress run instead of having their error
592
609
  // swallowed by the caller's try/catch.
593
610
  u(this, "_idleWaiters", []);
611
+ // Generic background tasks that hold the thread in a "running" state
612
+ // without an active stream run. Any UI-driven async work (e.g. a card
613
+ // that polls a status API) can register one to keep the composer
614
+ // showing a running/stop affordance, and is cancelled alongside the
615
+ // run when the user presses stop. Keyed by an opaque task id; the
616
+ // optional onCancel is invoked when the task is cancelled via cancelRun.
617
+ u(this, "_backgroundTasks", /* @__PURE__ */ new Map());
594
618
  // Track current part being accumulated
595
619
  u(this, "_currentPart", null);
596
620
  this.config = t, t.initialMessages && (this._messages = [...t.initialMessages]);
@@ -599,7 +623,7 @@ class M extends _ {
599
623
  return this._messages;
600
624
  }
601
625
  get isRunning() {
602
- return this._isRunning;
626
+ return this._isRunning || this._backgroundTasks.size > 0;
603
627
  }
604
628
  get isDisabled() {
605
629
  return this._isDisabled;
@@ -665,6 +689,40 @@ class M extends _ {
665
689
  } catch {
666
690
  }
667
691
  }
692
+ /**
693
+ * Registers a generic background task that keeps the thread reported as
694
+ * running (isRunning === true) for as long as it is active, even when no
695
+ * stream run is in flight. Use this for UI-driven async work — e.g. a card
696
+ * that polls a status API — so the composer shows a running/stop
697
+ * affordance and the work is cancelled when the user presses stop.
698
+ *
699
+ * Returns an opaque id to pass to stopBackgroundTask. The optional onCancel
700
+ * callback is invoked if the task is cancelled via cancelRun (stop button).
701
+ */
702
+ startBackgroundTask(t) {
703
+ const e = `bg-${Date.now()}-${Math.random().toString(36).slice(2)}`;
704
+ return this._backgroundTasks.set(e, { onCancel: t == null ? void 0 : t.onCancel }), this.notifySubscribers(), e;
705
+ }
706
+ /**
707
+ * Removes a previously registered background task. Once the last task is
708
+ * removed (and no run is in flight) any idle waiters are drained so queued
709
+ * dispatches can proceed.
710
+ */
711
+ stopBackgroundTask(t) {
712
+ this._backgroundTasks.delete(t) && (this.isRunning || this.drainIdleWaiters(), this.notifySubscribers());
713
+ }
714
+ cancelBackgroundTasks() {
715
+ var e;
716
+ if (this._backgroundTasks.size === 0) return;
717
+ const t = [...this._backgroundTasks.values()];
718
+ this._backgroundTasks.clear();
719
+ for (const i of t)
720
+ try {
721
+ (e = i.onCancel) == null || e.call(i);
722
+ } catch {
723
+ }
724
+ this.isRunning || this.drainIdleWaiters(), this.notifySubscribers();
725
+ }
668
726
  async startSystemEventRun(t) {
669
727
  if (this._isRunning)
670
728
  throw new Error("A run is already in progress");
@@ -912,7 +970,7 @@ class M extends _ {
912
970
  this.updateMessages([]);
913
971
  }
914
972
  cancelRun() {
915
- !this._isRunning || !this._abortController || this._abortController.abort();
973
+ this._isRunning && this._abortController && this._abortController.abort(), this.cancelBackgroundTasks();
916
974
  }
917
975
  reset(t = []) {
918
976
  this.updateMessages([...t]);
@@ -945,7 +1003,7 @@ class it extends _ {
945
1003
  u(this, "_searchQuery");
946
1004
  u(this, "_abortController", null);
947
1005
  u(this, "main");
948
- this.config = t, this._mainThreadId = k();
1006
+ this.config = t, this._mainThreadId = F();
949
1007
  const e = new M({
950
1008
  streamAdapter: t.streamAdapter,
951
1009
  capabilityExecutionManager: t.capabilityExecutionManager
@@ -1012,7 +1070,7 @@ class it extends _ {
1012
1070
  this.notifySubscribers();
1013
1071
  }
1014
1072
  async switchToNewThread() {
1015
- const t = k(), e = new M({
1073
+ const t = F(), e = new M({
1016
1074
  streamAdapter: this.config.streamAdapter,
1017
1075
  capabilityExecutionManager: this.config.capabilityExecutionManager
1018
1076
  }), i = new R(e);
@@ -1238,8 +1296,8 @@ function rt() {
1238
1296
  s.call(o, d) && (c[d] = o[d]);
1239
1297
  if (n) {
1240
1298
  h = n(o);
1241
- for (var p = 0; p < h.length; p++)
1242
- t.call(o, h[p]) && (c[h[p]] = o[h[p]]);
1299
+ for (var g = 0; g < h.length; g++)
1300
+ t.call(o, h[g]) && (c[h[g]] = o[h[g]]);
1243
1301
  }
1244
1302
  }
1245
1303
  return c;
@@ -1264,11 +1322,11 @@ function nt() {
1264
1322
  }
1265
1323
  var e = n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, i = Object.prototype.hasOwnProperty, r = { key: !0, ref: !0, __self: !0, __source: !0 };
1266
1324
  function a(o, c, h) {
1267
- var l, d = {}, p = null, g = null;
1268
- h !== void 0 && (p = "" + h), c.key !== void 0 && (p = "" + c.key), c.ref !== void 0 && (g = c.ref);
1325
+ var l, d = {}, g = null, p = null;
1326
+ h !== void 0 && (g = "" + h), c.key !== void 0 && (g = "" + c.key), c.ref !== void 0 && (p = c.ref);
1269
1327
  for (l in c) i.call(c, l) && !r.hasOwnProperty(l) && (d[l] = c[l]);
1270
1328
  if (o && o.defaultProps) for (l in c = o.defaultProps, c) d[l] === void 0 && (d[l] = c[l]);
1271
- return { $$typeof: s, type: o, key: p, ref: g, props: d, _owner: e.current };
1329
+ return { $$typeof: s, type: o, key: g, ref: p, props: d, _owner: e.current };
1272
1330
  }
1273
1331
  return x.jsx = a, x.jsxs = a, x;
1274
1332
  }
@@ -1277,12 +1335,12 @@ function at() {
1277
1335
  return O || (O = 1, E.exports = nt()), E.exports;
1278
1336
  }
1279
1337
  var w = at();
1280
- const W = U(null);
1338
+ const W = B(null);
1281
1339
  function _t({ runtime: n, children: s }) {
1282
1340
  return /* @__PURE__ */ w.jsx(W.Provider, { value: n, children: s });
1283
1341
  }
1284
1342
  function ot() {
1285
- const n = B(W);
1343
+ const n = U(W);
1286
1344
  if (!n)
1287
1345
  throw new Error("useAssistantRuntimeContext must be used within AssistantRuntimeProvider");
1288
1346
  return n;
@@ -1308,17 +1366,17 @@ function St() {
1308
1366
  }
1309
1367
  function xt() {
1310
1368
  const n = f(), [s, t] = y(n.threads.getState());
1311
- return m(() => n.threads.subscribe(() => {
1369
+ return b(() => n.threads.subscribe(() => {
1312
1370
  t(n.threads.getState());
1313
1371
  }), [n]), s;
1314
1372
  }
1315
1373
  function ct() {
1316
1374
  const s = f().threads.getMainThread(), [, t] = y({});
1317
- return m(() => s.subscribe(() => {
1375
+ return b(() => s.subscribe(() => {
1318
1376
  t({});
1319
1377
  }), [s]), s;
1320
1378
  }
1321
- var P = { exports: {} }, F = {};
1379
+ var k = { exports: {} }, P = {};
1322
1380
  /**
1323
1381
  * @license React
1324
1382
  * use-sync-external-store-shim.production.js
@@ -1330,48 +1388,48 @@ var P = { exports: {} }, F = {};
1330
1388
  */
1331
1389
  var j;
1332
1390
  function ut() {
1333
- if (j) return F;
1391
+ if (j) return P;
1334
1392
  j = 1;
1335
1393
  var n = q;
1336
- function s(d, p) {
1337
- return d === p && (d !== 0 || 1 / d === 1 / p) || d !== d && p !== p;
1394
+ function s(d, g) {
1395
+ return d === g && (d !== 0 || 1 / d === 1 / g) || d !== d && g !== g;
1338
1396
  }
1339
1397
  var t = typeof Object.is == "function" ? Object.is : s, e = n.useState, i = n.useEffect, r = n.useLayoutEffect, a = n.useDebugValue;
1340
- function o(d, p) {
1341
- var g = p(), S = e({ inst: { value: g, getSnapshot: p } }), b = S[0].inst, I = S[1];
1398
+ function o(d, g) {
1399
+ var p = g(), S = e({ inst: { value: p, getSnapshot: g } }), m = S[0].inst, I = S[1];
1342
1400
  return r(
1343
1401
  function() {
1344
- b.value = g, b.getSnapshot = p, c(b) && I({ inst: b });
1402
+ m.value = p, m.getSnapshot = g, c(m) && I({ inst: m });
1345
1403
  },
1346
- [d, g, p]
1404
+ [d, p, g]
1347
1405
  ), i(
1348
1406
  function() {
1349
- return c(b) && I({ inst: b }), d(function() {
1350
- c(b) && I({ inst: b });
1407
+ return c(m) && I({ inst: m }), d(function() {
1408
+ c(m) && I({ inst: m });
1351
1409
  });
1352
1410
  },
1353
1411
  [d]
1354
- ), a(g), g;
1412
+ ), a(p), p;
1355
1413
  }
1356
1414
  function c(d) {
1357
- var p = d.getSnapshot;
1415
+ var g = d.getSnapshot;
1358
1416
  d = d.value;
1359
1417
  try {
1360
- var g = p();
1361
- return !t(d, g);
1418
+ var p = g();
1419
+ return !t(d, p);
1362
1420
  } catch {
1363
1421
  return !0;
1364
1422
  }
1365
1423
  }
1366
- function h(d, p) {
1367
- return p();
1424
+ function h(d, g) {
1425
+ return g();
1368
1426
  }
1369
1427
  var l = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? h : o;
1370
- return F.useSyncExternalStore = n.useSyncExternalStore !== void 0 ? n.useSyncExternalStore : l, F;
1428
+ return P.useSyncExternalStore = n.useSyncExternalStore !== void 0 ? n.useSyncExternalStore : l, P;
1371
1429
  }
1372
1430
  var L;
1373
1431
  function ht() {
1374
- return L || (L = 1, P.exports = ut()), P.exports;
1432
+ return L || (L = 1, k.exports = ut()), k.exports;
1375
1433
  }
1376
1434
  var dt = ht();
1377
1435
  function wt() {
@@ -1406,7 +1464,7 @@ function Ct() {
1406
1464
  append: (a) => t((o) => o + a)
1407
1465
  };
1408
1466
  }
1409
- function Q(n, s) {
1467
+ function $(n, s) {
1410
1468
  var r, a, o, c;
1411
1469
  const e = f().pluginRegistry.getBestRendererForType(n);
1412
1470
  if (!e)
@@ -1428,7 +1486,7 @@ function Q(n, s) {
1428
1486
  }
1429
1487
  function It() {
1430
1488
  const n = f(), [s, t] = y(n.contentFocus.state);
1431
- return m(() => n.contentFocus.subscribe(() => {
1489
+ return b(() => n.contentFocus.subscribe(() => {
1432
1490
  t(n.contentFocus.state);
1433
1491
  }), [n]), {
1434
1492
  ...s,
@@ -1442,7 +1500,7 @@ function It() {
1442
1500
  }
1443
1501
  function lt(n) {
1444
1502
  const s = f();
1445
- m(() => {
1503
+ b(() => {
1446
1504
  const t = n.priority ?? 0;
1447
1505
  return n.execute && s.capabilityRegistry.registerHandler(n.name, { execute: n.execute }, t), n.render && s.capabilityRegistry.registerRenderer(
1448
1506
  n.name,
@@ -1455,11 +1513,11 @@ function lt(n) {
1455
1513
  };
1456
1514
  }, [n.name, s]);
1457
1515
  }
1458
- function pt(n) {
1516
+ function gt(n) {
1459
1517
  const s = f(), [t, e] = y(
1460
1518
  () => s.capabilityExecutionManager.getExecution(n)
1461
1519
  ), [, i] = V((r) => r + 1, 0);
1462
- return m(() => {
1520
+ return b(() => {
1463
1521
  const r = s.capabilityExecutionManager.getExecution(n);
1464
1522
  return e(r), s.capabilityExecutionManager.subscribe((o) => {
1465
1523
  if (o === n) {
@@ -1469,13 +1527,13 @@ function pt(n) {
1469
1527
  });
1470
1528
  }, [n, s]), t;
1471
1529
  }
1472
- const $ = U(void 0);
1530
+ const Q = B(void 0);
1473
1531
  function vt({ children: n }) {
1474
1532
  const [s, t] = y({}), e = C((c, h) => {
1475
1533
  t((l) => {
1476
1534
  if (h === null) {
1477
- const { [c]: d, ...p } = l;
1478
- return p;
1535
+ const { [c]: d, ...g } = l;
1536
+ return g;
1479
1537
  }
1480
1538
  return {
1481
1539
  ...l,
@@ -1490,9 +1548,9 @@ function vt({ children: n }) {
1490
1548
  }, []), r = C(() => Object.values(s).reduce(
1491
1549
  (c, h) => {
1492
1550
  const l = Object.entries(h.data).reduce(
1493
- (d, [p, g]) => ({
1551
+ (d, [g, p]) => ({
1494
1552
  ...d,
1495
- [p]: typeof g == "string" ? g : JSON.stringify(g)
1553
+ [g]: typeof p == "string" ? p : JSON.stringify(p)
1496
1554
  }),
1497
1555
  {}
1498
1556
  );
@@ -1511,29 +1569,29 @@ function vt({ children: n }) {
1511
1569
  }),
1512
1570
  [s, e, i, r, a]
1513
1571
  );
1514
- return /* @__PURE__ */ w.jsx($.Provider, { value: o, children: n });
1572
+ return /* @__PURE__ */ w.jsx(Q.Provider, { value: o, children: n });
1515
1573
  }
1516
- function gt() {
1517
- const n = B($);
1574
+ function pt() {
1575
+ const n = U(Q);
1518
1576
  if (!n)
1519
1577
  throw new Error("useChatContext must be used within a ChatContextProvider");
1520
1578
  return n;
1521
1579
  }
1522
1580
  function Tt(n) {
1523
- const { setContext: s, removeContext: t } = gt(), { id: e = "currentPage", ...i } = n;
1524
- m(() => (s(e, i), () => {
1581
+ const { setContext: s, removeContext: t } = pt(), { id: e = "currentPage", ...i } = n;
1582
+ b(() => (s(e, i), () => {
1525
1583
  t(e);
1526
1584
  }), [e, i.displayName, JSON.stringify(i.data), i.priority, i.icon]);
1527
1585
  }
1528
1586
  function Rt(n, s = "merge") {
1529
1587
  const t = f();
1530
- m(() => (t.quickActionRegistry.activateScope(n, s), () => {
1588
+ b(() => (t.quickActionRegistry.activateScope(n, s), () => {
1531
1589
  t.quickActionRegistry.deactivateScope(n);
1532
1590
  }), [n, s, t]);
1533
1591
  }
1534
1592
  function Mt(n, s) {
1535
1593
  const t = f();
1536
- m(() => {
1594
+ b(() => {
1537
1595
  const e = s || t.quickActionRegistry.getActiveScope();
1538
1596
  return t.quickActionRegistry.register(n, e), () => {
1539
1597
  t.quickActionRegistry.unregister(n.id, e);
@@ -1552,12 +1610,25 @@ function Mt(n, s) {
1552
1610
  }
1553
1611
  function Et(n) {
1554
1612
  const s = f(), [t, e] = y([]);
1555
- return m(() => (s.quickActionRegistry.getVisible(n).then(e), s.subscribe(() => {
1613
+ return b(() => (s.quickActionRegistry.getVisible(n).then(e), s.subscribe(() => {
1556
1614
  s.quickActionRegistry.getVisible(n).then(e);
1557
1615
  })), [s, n == null ? void 0 : n.scope]), t;
1558
1616
  }
1617
+ function At(n, s) {
1618
+ const t = f();
1619
+ b(() => {
1620
+ const e = s || t.quickActionRegistry.getActiveScope();
1621
+ return n.forEach((i) => {
1622
+ t.quickActionRegistry.register(i, e);
1623
+ }), () => {
1624
+ n.forEach((i) => {
1625
+ t.quickActionRegistry.unregister(i.id, e);
1626
+ });
1627
+ };
1628
+ }, [n, s, t]);
1629
+ }
1559
1630
  function ft({ message: n, content: s }) {
1560
- const t = f(), e = s, i = pt(e.capabilityId), r = t.capabilityRegistry.getRenderer(e.capabilityName);
1631
+ const t = f(), e = s, i = gt(e.capabilityId), r = t.capabilityRegistry.getRenderer(e.capabilityName);
1561
1632
  if (!r || !i)
1562
1633
  return null;
1563
1634
  const a = r.component;
@@ -1573,8 +1644,8 @@ function ft({ message: n, content: s }) {
1573
1644
  }
1574
1645
  );
1575
1646
  }
1576
- function At({ message: n, content: s }) {
1577
- const { component: t } = Q(s.type);
1647
+ function kt({ message: n, content: s }) {
1648
+ const { component: t } = $(s.type);
1578
1649
  if (s.type === "capability")
1579
1650
  return /* @__PURE__ */ w.jsx(ft, { message: n, content: s });
1580
1651
  if (!t)
@@ -1590,7 +1661,7 @@ function Pt({
1590
1661
  onNavigate: i,
1591
1662
  onSwitchContext: r
1592
1663
  }) {
1593
- const { auxiliaryComponent: a } = Q(s.type, t);
1664
+ const { auxiliaryComponent: a } = $(s.type, t);
1594
1665
  if (!a)
1595
1666
  return null;
1596
1667
  const o = a;
@@ -1610,7 +1681,7 @@ function Ft(n) {
1610
1681
  const s = () => (lt(n), null);
1611
1682
  return s.displayName = `Capability(${n.name})`, s;
1612
1683
  }
1613
- class kt {
1684
+ class Dt {
1614
1685
  getAllowedEvents() {
1615
1686
  return null;
1616
1687
  }
@@ -1649,15 +1720,15 @@ class kt {
1649
1720
  continue;
1650
1721
  }
1651
1722
  if (d.startsWith("data:")) {
1652
- const p = d.substring(5).trim();
1653
- if (p === "eof" || d === "eof")
1723
+ const g = d.substring(5).trim();
1724
+ if (g === "eof" || d === "eof")
1654
1725
  break;
1655
1726
  try {
1656
- const g = JSON.parse(p);
1727
+ const p = JSON.parse(g);
1657
1728
  if (a && this.shouldProcessEvent(a)) {
1658
1729
  const S = this.convertEvent({
1659
1730
  event: a,
1660
- data: g
1731
+ data: p
1661
1732
  });
1662
1733
  S && (yield S);
1663
1734
  }
@@ -1677,13 +1748,13 @@ export {
1677
1748
  yt as AssistantRuntime,
1678
1749
  _t as AssistantRuntimeProvider,
1679
1750
  Pt as AuxiliaryRenderer,
1680
- kt as BaseSSEStreamAdapter,
1751
+ Dt as BaseSSEStreamAdapter,
1681
1752
  K as CapabilityExecutionManager,
1682
1753
  Y as CapabilityRegistry,
1683
1754
  ft as CapabilityRendererComp,
1684
1755
  vt as ChatContextProvider,
1685
1756
  st as ComposerRuntime,
1686
- At as ContentRenderer,
1757
+ kt as ContentRenderer,
1687
1758
  G as PluginRegistry,
1688
1759
  X as QuickActionRegistry,
1689
1760
  T as ThreadListItemRuntime,
@@ -1694,17 +1765,18 @@ export {
1694
1765
  Ft as makeCapability,
1695
1766
  f as useAssistantRuntime,
1696
1767
  lt as useCapability,
1697
- pt as useCapabilityExecution,
1698
- gt as useChatContext,
1768
+ gt as useCapabilityExecution,
1769
+ pt as useChatContext,
1699
1770
  Ct as useComposer,
1700
1771
  It as useContentFocus,
1701
- Q as useContentRenderer,
1772
+ $ as useContentRenderer,
1702
1773
  ct as useCurrentThread,
1703
1774
  wt as useMessages,
1704
1775
  Tt as usePageContext,
1705
1776
  Mt as useQuickAction,
1706
1777
  Rt as useQuickActionScope,
1707
1778
  Et as useQuickActions,
1779
+ At as useRegisterQuickActions,
1708
1780
  St as useThreadList,
1709
1781
  xt as useThreadListState
1710
1782
  };