@harnessio/ai-chat-core 0.0.19 → 0.0.21

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
- var J = (i, n, t) => n in i ? H(i, n, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[n] = t;
3
- var u = (i, n, t) => J(i, typeof n != "symbol" ? n + "" : n, t);
4
- import U, { createContext as B, useContext as q, useState as m, useEffect as w, useReducer as z, useCallback as C, useMemo as V } from "react";
2
+ var J = (a, n, t) => n in a ? H(a, n, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[n] = t;
3
+ var u = (a, n, t) => J(a, typeof n != "symbol" ? n + "" : n, t);
4
+ import U, { createContext as B, useContext as W, useState as m, useEffect as w, useReducer as z, useCallback as C, useMemo as V } from "react";
5
5
  class G {
6
6
  constructor() {
7
7
  u(this, "plugins", /* @__PURE__ */ new Map());
@@ -16,7 +16,7 @@ class G {
16
16
  unregisterPlugin(n) {
17
17
  const t = this.plugins.get(n);
18
18
  return t ? (t.renderers.forEach((e) => {
19
- const r = (this.renderersByType.get(e.type) || []).filter((a) => !t.renderers.includes(a));
19
+ const r = (this.renderersByType.get(e.type) || []).filter((i) => !t.renderers.includes(i));
20
20
  r.length === 0 ? this.renderersByType.delete(e.type) : this.renderersByType.set(e.type, r);
21
21
  }), this.plugins.delete(n)) : !1;
22
22
  }
@@ -41,29 +41,37 @@ class Y {
41
41
  u(this, "handlers", /* @__PURE__ */ new Map());
42
42
  u(this, "renderers", /* @__PURE__ */ new Map());
43
43
  }
44
- registerHandler(n, t) {
45
- this.handlers.set(n, t);
44
+ registerHandler(n, t, e = 0) {
45
+ const s = this.handlers.get(n) || [], r = { handler: t, priority: e }, i = s.findIndex((o) => o.priority < e);
46
+ i === -1 ? s.push(r) : s.splice(i, 0, r), this.handlers.set(n, s);
46
47
  }
47
- registerRenderer(n, t) {
48
- this.renderers.set(n, t);
48
+ registerRenderer(n, t, e = 0) {
49
+ const s = this.renderers.get(n) || [], r = { renderer: t, priority: e }, i = s.findIndex((o) => o.priority < e);
50
+ i === -1 ? s.push(r) : s.splice(i, 0, r), this.renderers.set(n, s);
49
51
  }
50
52
  unregister(n) {
51
53
  this.handlers.delete(n), this.renderers.delete(n);
52
54
  }
53
55
  getHandler(n) {
54
- return this.handlers.get(n);
56
+ var e;
57
+ const t = this.handlers.get(n);
58
+ return (e = t == null ? void 0 : t[0]) == null ? void 0 : e.handler;
55
59
  }
56
60
  getRenderer(n) {
57
- return this.renderers.get(n);
61
+ var e;
62
+ const t = this.renderers.get(n);
63
+ return (e = t == null ? void 0 : t[0]) == null ? void 0 : e.renderer;
58
64
  }
59
65
  getStrategy() {
60
66
  return "queue";
61
67
  }
62
68
  hasHandler(n) {
63
- return this.handlers.has(n);
69
+ const t = this.handlers.get(n);
70
+ return t !== void 0 && t.length > 0;
64
71
  }
65
72
  hasRenderer(n) {
66
- return this.renderers.has(n);
73
+ const t = this.renderers.get(n);
74
+ return t !== void 0 && t.length > 0;
67
75
  }
68
76
  clear() {
69
77
  this.handlers.clear(), this.renderers.clear();
@@ -120,11 +128,11 @@ class K {
120
128
  });
121
129
  t.status = { type: "complete", result: r }, t.result = r;
122
130
  } catch (r) {
123
- const a = r instanceof Error ? r.message : "Unknown error";
124
- t.status = { type: "error", error: a }, t.error = a;
131
+ const i = r instanceof Error ? r.message : "Unknown error";
132
+ t.status = { type: "error", error: i }, t.error = i;
125
133
  } finally {
126
- const r = this.runningByName.get(t.name) || [], a = r.indexOf(n);
127
- a > -1 && r.splice(a, 1), r.length === 0 ? this.runningByName.delete(t.name) : this.runningByName.set(t.name, r), this.notifySubscribers(n);
134
+ const r = this.runningByName.get(t.name) || [], i = r.indexOf(n);
135
+ i > -1 && r.splice(i, 1), r.length === 0 ? this.runningByName.delete(t.name) : this.runningByName.set(t.name, r), this.notifySubscribers(n);
128
136
  }
129
137
  }
130
138
  this.isProcessing = !1;
@@ -227,40 +235,40 @@ class X extends b {
227
235
  }
228
236
  focusNext(t) {
229
237
  if (!this._state.focusedMessageId || !t.length) return;
230
- const e = t.findIndex((a) => a.id === this._state.focusedMessageId);
238
+ const e = t.findIndex((i) => i.id === this._state.focusedMessageId);
231
239
  if (e === -1) return;
232
240
  const s = t[e], r = this._state.focusedContentIndex ?? 0;
233
241
  if (r + 1 < s.content.length) {
234
- const a = s.content[r + 1];
235
- this.focus(a, s, r + 1, this._state.context || "detail");
242
+ const i = s.content[r + 1];
243
+ this.focus(i, s, r + 1, this._state.context || "detail");
236
244
  return;
237
245
  }
238
246
  if (e + 1 < t.length) {
239
- const a = t[e + 1];
240
- a.content.length > 0 && this.focus(a.content[0], a, 0, this._state.context || "detail");
247
+ const i = t[e + 1];
248
+ i.content.length > 0 && this.focus(i.content[0], i, 0, this._state.context || "detail");
241
249
  }
242
250
  }
243
251
  focusPrevious(t) {
244
252
  if (!this._state.focusedMessageId || !t.length) return;
245
- const e = t.findIndex((a) => a.id === this._state.focusedMessageId);
253
+ const e = t.findIndex((i) => i.id === this._state.focusedMessageId);
246
254
  if (e === -1) return;
247
255
  const s = t[e], r = this._state.focusedContentIndex ?? 0;
248
256
  if (r > 0) {
249
- const a = s.content[r - 1];
250
- this.focus(a, s, r - 1, this._state.context || "detail");
257
+ const i = s.content[r - 1];
258
+ this.focus(i, s, r - 1, this._state.context || "detail");
251
259
  return;
252
260
  }
253
261
  if (e > 0) {
254
- const a = t[e - 1];
255
- if (a.content.length > 0) {
256
- const o = a.content.length - 1;
257
- this.focus(a.content[o], a, o, this._state.context || "detail");
262
+ const i = t[e - 1];
263
+ if (i.content.length > 0) {
264
+ const o = i.content.length - 1;
265
+ this.focus(i.content[o], i, o, this._state.context || "detail");
258
266
  }
259
267
  }
260
268
  }
261
269
  }
262
270
  let Z = 0, tt = 0;
263
- function I() {
271
+ function T() {
264
272
  return `msg-${Date.now()}-${++Z}`;
265
273
  }
266
274
  function N() {
@@ -387,11 +395,20 @@ class R extends b {
387
395
  }
388
396
  }
389
397
  }
398
+ /**
399
+ * Dispatches a system event (e.g. a user click on an elicitation card).
400
+ *
401
+ * Prior to this change, this method wrapped startSystemEventRun in a
402
+ * try/catch that silently swallowed the "A run is already in progress"
403
+ * error thrown by the core when _isRunning was still true — causing
404
+ * clicks fired during the tail of the previous run to disappear.
405
+ *
406
+ * Now we await waitForIdle() before dispatching so the event is
407
+ * reliably delivered as soon as the in-flight run completes. Callers
408
+ * do not need their own queueing primitive.
409
+ */
390
410
  async sendSystemEvent(t) {
391
- try {
392
- await this._core.startSystemEventRun(t);
393
- } catch {
394
- }
411
+ await this._core.waitForIdle(), await this._core.startSystemEventRun(t);
395
412
  }
396
413
  cancelRun() {
397
414
  this._core.cancelRun();
@@ -423,6 +440,11 @@ class E extends b {
423
440
  u(this, "_abortController", null);
424
441
  u(this, "_conversationId");
425
442
  u(this, "_title");
443
+ // Resolvers waiting for _isRunning to flip to false. Drained in the
444
+ // finally block of every run path so callers can serialize their
445
+ // dispatch against an in-progress run instead of having their error
446
+ // swallowed by the caller's try/catch.
447
+ u(this, "_idleWaiters", []);
426
448
  // Track current part being accumulated
427
449
  u(this, "_currentPart", null);
428
450
  this.config = t, t.initialMessages && (this._messages = [...t.initialMessages]);
@@ -464,7 +486,7 @@ class E extends b {
464
486
  }
465
487
  append(t) {
466
488
  const e = {
467
- id: t.id || I(),
489
+ id: t.id || T(),
468
490
  parentId: t.parentId,
469
491
  role: t.role,
470
492
  content: t.content,
@@ -474,11 +496,34 @@ class E extends b {
474
496
  };
475
497
  this.updateMessages([...this._messages, e]);
476
498
  }
499
+ /**
500
+ * Resolves when _isRunning flips to false. Use this to queue a
501
+ * follow-on dispatch (e.g. a system event from a card click that
502
+ * fires during the tail of the previous run) without racing the
503
+ * in-progress run's guard.
504
+ *
505
+ * Resolves immediately if nothing is running.
506
+ */
507
+ waitForIdle() {
508
+ return this._isRunning ? new Promise((t) => {
509
+ this._idleWaiters.push(t);
510
+ }) : Promise.resolve();
511
+ }
512
+ drainIdleWaiters() {
513
+ if (this._idleWaiters.length === 0) return;
514
+ const t = this._idleWaiters;
515
+ this._idleWaiters = [];
516
+ for (const e of t)
517
+ try {
518
+ e();
519
+ } catch {
520
+ }
521
+ }
477
522
  async startSystemEventRun(t) {
478
523
  if (this._isRunning)
479
524
  throw new Error("A run is already in progress");
480
525
  this._isWaitingForUser = !1, this._pendingCapability = null, this._isRunning = !0, this._abortController = new AbortController(), this._currentPart = null;
481
- const e = I(), s = {
526
+ const e = T(), s = {
482
527
  id: e,
483
528
  role: "assistant",
484
529
  content: [],
@@ -493,31 +538,31 @@ class E extends b {
493
538
  signal: this._abortController.signal,
494
539
  systemEvent: t
495
540
  });
496
- for await (const a of r) {
541
+ for await (const i of r) {
497
542
  if (this._abortController.signal.aborted)
498
543
  break;
499
- this.handleStreamEvent(e, a.event);
544
+ this.handleStreamEvent(e, i.event);
500
545
  }
501
546
  this.updateMessageStatus(e, { type: "complete" });
502
547
  } catch (r) {
503
548
  if (r instanceof Error && r.name === "AbortError")
504
549
  this.completeStreamingContent(e), this.updateMessageStatus(e, { type: "cancelled" });
505
550
  else {
506
- const a = r instanceof Error ? r.message : "Unknown error";
551
+ const i = r instanceof Error ? r.message : "Unknown error";
507
552
  this.completeStreamingContent(e), this.updateMessageStatus(e, {
508
553
  type: "error",
509
- error: a
554
+ error: i
510
555
  });
511
556
  }
512
557
  } finally {
513
- this._isRunning = !1, this._abortController = null, this._currentPart = null, this.notifySubscribers();
558
+ this._isRunning = !1, this._abortController = null, this._currentPart = null, this.notifySubscribers(), this.drainIdleWaiters();
514
559
  }
515
560
  }
516
561
  async startRun(t) {
517
562
  if (this._isRunning)
518
563
  throw new Error("A run is already in progress");
519
564
  this._isWaitingForUser = !1, this._pendingCapability = null, this.append(t), this._isRunning = !0, this._abortController = new AbortController(), this._currentPart = null;
520
- const e = I(), s = {
565
+ const e = T(), s = {
521
566
  id: e,
522
567
  role: "assistant",
523
568
  content: [],
@@ -531,24 +576,24 @@ class E extends b {
531
576
  conversationId: this._conversationId,
532
577
  signal: this._abortController.signal
533
578
  });
534
- for await (const a of r) {
579
+ for await (const i of r) {
535
580
  if (this._abortController.signal.aborted)
536
581
  break;
537
- this.handleStreamEvent(e, a.event);
582
+ this.handleStreamEvent(e, i.event);
538
583
  }
539
584
  this.updateMessageStatus(e, { type: "complete" });
540
585
  } catch (r) {
541
586
  if (r instanceof Error && r.name === "AbortError")
542
587
  this.completeStreamingContent(e), this.updateMessageStatus(e, { type: "cancelled" });
543
588
  else {
544
- const a = r instanceof Error ? r.message : "Unknown error";
589
+ const i = r instanceof Error ? r.message : "Unknown error";
545
590
  this.completeStreamingContent(e), this.updateMessageStatus(e, {
546
591
  type: "error",
547
- error: a
592
+ error: i
548
593
  });
549
594
  }
550
595
  } finally {
551
- this._isRunning = !1, this._abortController = null, this._currentPart = null, this.notifySubscribers();
596
+ this._isRunning = !1, this._abortController = null, this._currentPart = null, this.notifySubscribers(), this.drainIdleWaiters();
552
597
  }
553
598
  }
554
599
  handleStreamEvent(t, e) {
@@ -617,12 +662,12 @@ class E extends b {
617
662
  }
618
663
  ];
619
664
  }
620
- const a = [
665
+ const i = [
621
666
  ...this._messages.slice(0, s),
622
667
  { ...r, timestamp: Date.now() },
623
668
  ...this._messages.slice(s + 1)
624
669
  ];
625
- this.updateMessages(a);
670
+ this.updateMessages(i);
626
671
  }
627
672
  handlePartStart(t, e) {
628
673
  const s = t.content.length;
@@ -687,7 +732,7 @@ class E extends b {
687
732
  this._currentPart = null;
688
733
  }
689
734
  updateMessageStatus(t, e) {
690
- const s = this._messages.findIndex((a) => a.id === t);
735
+ const s = this._messages.findIndex((i) => i.id === t);
691
736
  if (s === -1) return;
692
737
  const r = [
693
738
  ...this._messages.slice(0, s),
@@ -707,12 +752,12 @@ class E extends b {
707
752
  var d;
708
753
  return ((d = c.status) == null ? void 0 : d.type) === "streaming";
709
754
  })) return;
710
- const a = s.content.map((c) => {
755
+ const i = s.content.map((c) => {
711
756
  var d;
712
757
  return ((d = c.status) == null ? void 0 : d.type) === "streaming" ? { ...c, status: { type: "complete" } } : c;
713
758
  }), o = [
714
759
  ...this._messages.slice(0, e),
715
- { ...s, content: a, timestamp: Date.now() },
760
+ { ...s, content: i, timestamp: Date.now() },
716
761
  ...this._messages.slice(e + 1)
717
762
  ];
718
763
  this.updateMessages(o);
@@ -790,12 +835,12 @@ class st extends b {
790
835
  };
791
836
  }
792
837
  async switchToThread(t) {
793
- var a, o;
838
+ var i, o;
794
839
  const e = this._threads.get(t);
795
840
  if (!e)
796
841
  throw new Error(`Thread ${t} not found`);
797
842
  const s = this._threadStates.get(this._mainThreadId);
798
- s && (s.isMain = !1, this._threadStates.set(this._mainThreadId, s), (a = this._threadItems.get(this._mainThreadId)) == null || a.updateState(s)), this._mainThreadId = t;
843
+ s && (s.isMain = !1, this._threadStates.set(this._mainThreadId, s), (i = this._threadItems.get(this._mainThreadId)) == null || i.updateState(s)), this._mainThreadId = t;
799
844
  const r = this._threadStates.get(t);
800
845
  if (r && (r.isMain = !0, this._threadStates.set(t, r), (o = this._threadItems.get(t)) == null || o.updateState(r), e.setConversationId(r.conversationId), e.setTitle(r.title)), this.config.threadListAdapter && (r != null && r.conversationId)) {
801
846
  this._isLoading = !0, this.notifySubscribers();
@@ -825,18 +870,18 @@ class st extends b {
825
870
  updatedAt: Date.now()
826
871
  };
827
872
  this._threadStates.set(t, r);
828
- const a = new v({
873
+ const i = new v({
829
874
  state: r,
830
875
  onSwitchTo: this.switchToThread.bind(this),
831
876
  onRename: this.renameThread.bind(this),
832
877
  onDelete: this.deleteThread.bind(this)
833
878
  });
834
- if (this._threadItems.set(t, a), s.subscribe(() => {
879
+ if (this._threadItems.set(t, i), s.subscribe(() => {
835
880
  this.syncThreadStateFromRuntime(t, s), this.notifySubscribers();
836
881
  }), this.config.threadListAdapter)
837
882
  try {
838
883
  const o = await this.config.threadListAdapter.createThread();
839
- r.conversationId = o.conversationId, this._threadStates.set(t, r), a.updateState(r);
884
+ r.conversationId = o.conversationId, this._threadStates.set(t, r), i.updateState(r);
840
885
  } catch (o) {
841
886
  console.error("Failed to create thread:", o);
842
887
  }
@@ -861,16 +906,16 @@ class st extends b {
861
906
  const r = new E({
862
907
  streamAdapter: this.config.streamAdapter,
863
908
  capabilityExecutionManager: this.config.capabilityExecutionManager
864
- }), a = new R(r);
865
- this._threads.set(s.id, a), this._threadStates.set(s.id, s);
909
+ }), i = new R(r);
910
+ this._threads.set(s.id, i), this._threadStates.set(s.id, s);
866
911
  const o = new v({
867
912
  state: s,
868
913
  onSwitchTo: this.switchToThread.bind(this),
869
914
  onRename: this.renameThread.bind(this),
870
915
  onDelete: this.deleteThread.bind(this)
871
916
  });
872
- this._threadItems.set(s.id, o), a.subscribe(() => {
873
- this.syncThreadStateFromRuntime(s.id, a), this.notifySubscribers();
917
+ this._threadItems.set(s.id, o), i.subscribe(() => {
918
+ this.syncThreadStateFromRuntime(s.id, i), this.notifySubscribers();
874
919
  });
875
920
  }
876
921
  return e;
@@ -895,8 +940,8 @@ class st extends b {
895
940
  if (s.title = e, s.updatedAt = Date.now(), this._threadStates.set(t, s), (r = this._threadItems.get(t)) == null || r.updateState(s), this.config.threadListAdapter)
896
941
  try {
897
942
  await this.config.threadListAdapter.updateThread(t, { title: e });
898
- } catch (a) {
899
- console.error("Failed to rename thread:", a);
943
+ } catch (i) {
944
+ console.error("Failed to rename thread:", i);
900
945
  }
901
946
  this.notifySubscribers();
902
947
  }
@@ -981,10 +1026,10 @@ class mt extends b {
981
1026
  for (let s = t.length - 1; s >= 0; s--) {
982
1027
  const r = t[s];
983
1028
  if (r.role === "assistant")
984
- for (let a = r.content.length - 1; a >= 0; a--) {
985
- const o = r.content[a], c = this.pluginRegistry.getRenderersByType(o.type);
1029
+ for (let i = r.content.length - 1; i >= 0; i--) {
1030
+ const o = r.content[i], c = this.pluginRegistry.getRenderersByType(o.type);
986
1031
  if (c.length > 0 && ((e = c[0].capabilities) != null && e.supportsFocus)) {
987
- this._contentFocusRuntime.isActive && this._contentFocusRuntime.focusedMessageId === r.id && this._contentFocusRuntime.focusedContentIndex === a || this._contentFocusRuntime.focus(o, r, a, "detail");
1032
+ this._contentFocusRuntime.isActive && this._contentFocusRuntime.focusedMessageId === r.id && this._contentFocusRuntime.focusedContentIndex === i || this._contentFocusRuntime.focus(o, r, i, "detail");
988
1033
  return;
989
1034
  }
990
1035
  }
@@ -1001,7 +1046,7 @@ var A, D;
1001
1046
  function rt() {
1002
1047
  if (D) return A;
1003
1048
  D = 1;
1004
- var i = Object.getOwnPropertySymbols, n = Object.prototype.hasOwnProperty, t = Object.prototype.propertyIsEnumerable;
1049
+ var a = Object.getOwnPropertySymbols, n = Object.prototype.hasOwnProperty, t = Object.prototype.propertyIsEnumerable;
1005
1050
  function e(r) {
1006
1051
  if (r == null)
1007
1052
  throw new TypeError("Object.assign cannot be called with null or undefined");
@@ -1014,10 +1059,10 @@ function rt() {
1014
1059
  var r = new String("abc");
1015
1060
  if (r[5] = "de", Object.getOwnPropertyNames(r)[0] === "5")
1016
1061
  return !1;
1017
- for (var a = {}, o = 0; o < 10; o++)
1018
- a["_" + String.fromCharCode(o)] = o;
1019
- var c = Object.getOwnPropertyNames(a).map(function(l) {
1020
- return a[l];
1062
+ for (var i = {}, o = 0; o < 10; o++)
1063
+ i["_" + String.fromCharCode(o)] = o;
1064
+ var c = Object.getOwnPropertyNames(i).map(function(l) {
1065
+ return i[l];
1021
1066
  });
1022
1067
  if (c.join("") !== "0123456789")
1023
1068
  return !1;
@@ -1029,13 +1074,13 @@ function rt() {
1029
1074
  return !1;
1030
1075
  }
1031
1076
  }
1032
- return A = s() ? Object.assign : function(r, a) {
1077
+ return A = s() ? Object.assign : function(r, i) {
1033
1078
  for (var o, c = e(r), d, l = 1; l < arguments.length; l++) {
1034
1079
  o = Object(arguments[l]);
1035
1080
  for (var h in o)
1036
1081
  n.call(o, h) && (c[h] = o[h]);
1037
- if (i) {
1038
- d = i(o);
1082
+ if (a) {
1083
+ d = a(o);
1039
1084
  for (var g = 0; g < d.length; g++)
1040
1085
  t.call(o, d[g]) && (c[d[g]] = o[d[g]]);
1041
1086
  }
@@ -1055,60 +1100,60 @@ var j;
1055
1100
  function nt() {
1056
1101
  if (j) return x;
1057
1102
  j = 1, rt();
1058
- var i = U, n = 60103;
1103
+ var a = U, n = 60103;
1059
1104
  if (x.Fragment = 60107, typeof Symbol == "function" && Symbol.for) {
1060
1105
  var t = Symbol.for;
1061
1106
  n = t("react.element"), x.Fragment = t("react.fragment");
1062
1107
  }
1063
- var e = i.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, s = Object.prototype.hasOwnProperty, r = { key: !0, ref: !0, __self: !0, __source: !0 };
1064
- function a(o, c, d) {
1108
+ var e = a.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, s = Object.prototype.hasOwnProperty, r = { key: !0, ref: !0, __self: !0, __source: !0 };
1109
+ function i(o, c, d) {
1065
1110
  var l, h = {}, g = null, p = null;
1066
1111
  d !== void 0 && (g = "" + d), c.key !== void 0 && (g = "" + c.key), c.ref !== void 0 && (p = c.ref);
1067
1112
  for (l in c) s.call(c, l) && !r.hasOwnProperty(l) && (h[l] = c[l]);
1068
1113
  if (o && o.defaultProps) for (l in c = o.defaultProps, c) h[l] === void 0 && (h[l] = c[l]);
1069
1114
  return { $$typeof: n, type: o, key: g, ref: p, props: h, _owner: e.current };
1070
1115
  }
1071
- return x.jsx = a, x.jsxs = a, x;
1116
+ return x.jsx = i, x.jsxs = i, x;
1072
1117
  }
1073
1118
  var O;
1074
1119
  function it() {
1075
1120
  return O || (O = 1, M.exports = nt()), M.exports;
1076
1121
  }
1077
1122
  var S = it();
1078
- const W = B(null);
1079
- function bt({ runtime: i, children: n }) {
1080
- return /* @__PURE__ */ S.jsx(W.Provider, { value: i, children: n });
1123
+ const q = B(null);
1124
+ function bt({ runtime: a, children: n }) {
1125
+ return /* @__PURE__ */ S.jsx(q.Provider, { value: a, children: n });
1081
1126
  }
1082
1127
  function at() {
1083
- const i = q(W);
1084
- if (!i)
1128
+ const a = W(q);
1129
+ if (!a)
1085
1130
  throw new Error("useAssistantRuntimeContext must be used within AssistantRuntimeProvider");
1086
- return i;
1131
+ return a;
1087
1132
  }
1088
1133
  function f() {
1089
1134
  return at();
1090
1135
  }
1091
1136
  function _t() {
1092
- const i = f();
1137
+ const a = f();
1093
1138
  return {
1094
- switchToThread: (n) => i.threads.switchToThread(n),
1095
- switchToNewThread: () => i.threads.switchToNewThread(),
1096
- loadThreads: (n) => i.threads.loadThreads(n),
1139
+ switchToThread: (n) => a.threads.switchToThread(n),
1140
+ switchToNewThread: () => a.threads.switchToNewThread(),
1141
+ loadThreads: (n) => a.threads.loadThreads(n),
1097
1142
  renameThread: (n, t) => {
1098
1143
  var e, s;
1099
- return (s = (e = i.threads).renameThread) == null ? void 0 : s.call(e, n, t);
1144
+ return (s = (e = a.threads).renameThread) == null ? void 0 : s.call(e, n, t);
1100
1145
  },
1101
1146
  deleteThread: (n) => {
1102
1147
  var t, e;
1103
- return (e = (t = i.threads).deleteThread) == null ? void 0 : e.call(t, n);
1148
+ return (e = (t = a.threads).deleteThread) == null ? void 0 : e.call(t, n);
1104
1149
  }
1105
1150
  };
1106
1151
  }
1107
1152
  function xt() {
1108
- const i = f(), [n, t] = m(i.threads.getState());
1109
- return w(() => i.threads.subscribe(() => {
1110
- t(i.threads.getState());
1111
- }), [i]), n;
1153
+ const a = f(), [n, t] = m(a.threads.getState());
1154
+ return w(() => a.threads.subscribe(() => {
1155
+ t(a.threads.getState());
1156
+ }), [a]), n;
1112
1157
  }
1113
1158
  function ot() {
1114
1159
  const n = f().threads.getMainThread(), [, t] = m({});
@@ -1130,26 +1175,26 @@ var L;
1130
1175
  function ct() {
1131
1176
  if (L) return P;
1132
1177
  L = 1;
1133
- var i = U;
1178
+ var a = U;
1134
1179
  function n(h, g) {
1135
1180
  return h === g && (h !== 0 || 1 / h === 1 / g) || h !== h && g !== g;
1136
1181
  }
1137
- var t = typeof Object.is == "function" ? Object.is : n, e = i.useState, s = i.useEffect, r = i.useLayoutEffect, a = i.useDebugValue;
1182
+ var t = typeof Object.is == "function" ? Object.is : n, e = a.useState, s = a.useEffect, r = a.useLayoutEffect, i = a.useDebugValue;
1138
1183
  function o(h, g) {
1139
- var p = g(), _ = e({ inst: { value: p, getSnapshot: g } }), y = _[0].inst, T = _[1];
1184
+ var p = g(), _ = e({ inst: { value: p, getSnapshot: g } }), y = _[0].inst, I = _[1];
1140
1185
  return r(
1141
1186
  function() {
1142
- y.value = p, y.getSnapshot = g, c(y) && T({ inst: y });
1187
+ y.value = p, y.getSnapshot = g, c(y) && I({ inst: y });
1143
1188
  },
1144
1189
  [h, p, g]
1145
1190
  ), s(
1146
1191
  function() {
1147
- return c(y) && T({ inst: y }), h(function() {
1148
- c(y) && T({ inst: y });
1192
+ return c(y) && I({ inst: y }), h(function() {
1193
+ c(y) && I({ inst: y });
1149
1194
  });
1150
1195
  },
1151
1196
  [h]
1152
- ), a(p), p;
1197
+ ), i(p), p;
1153
1198
  }
1154
1199
  function c(h) {
1155
1200
  var g = h.getSnapshot;
@@ -1165,7 +1210,7 @@ function ct() {
1165
1210
  return g();
1166
1211
  }
1167
1212
  var l = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? d : o;
1168
- return P.useSyncExternalStore = i.useSyncExternalStore !== void 0 ? i.useSyncExternalStore : l, P;
1213
+ return P.useSyncExternalStore = a.useSyncExternalStore !== void 0 ? a.useSyncExternalStore : l, P;
1169
1214
  }
1170
1215
  var k;
1171
1216
  function ut() {
@@ -1173,27 +1218,27 @@ function ut() {
1173
1218
  }
1174
1219
  var ht = ut();
1175
1220
  function St() {
1176
- const i = f();
1221
+ const a = f();
1177
1222
  return ht.useSyncExternalStore(
1178
- (n) => i.subscribe(() => {
1223
+ (n) => a.subscribe(() => {
1179
1224
  n();
1180
1225
  }),
1181
- () => i.thread.messages,
1182
- () => i.thread.messages
1226
+ () => a.thread.messages,
1227
+ () => a.thread.messages
1183
1228
  );
1184
1229
  }
1185
1230
  function wt() {
1186
- const i = ot(), [n, t] = m(""), [e, s] = m(!1);
1231
+ const a = ot(), [n, t] = m(""), [e, s] = m(!1);
1187
1232
  return {
1188
1233
  text: n,
1189
1234
  setText: t,
1190
1235
  isSubmitting: e,
1191
- send: async (a) => {
1192
- if (a == null || a.preventDefault(), !n.trim() || e) return;
1236
+ send: async (i) => {
1237
+ if (i == null || i.preventDefault(), !n.trim() || e) return;
1193
1238
  const o = n;
1194
1239
  t(""), s(!0);
1195
1240
  try {
1196
- await i.send(o);
1241
+ await a.send(o);
1197
1242
  } catch (c) {
1198
1243
  console.error("Failed to send message:", c), t(o);
1199
1244
  } finally {
@@ -1201,12 +1246,12 @@ function wt() {
1201
1246
  }
1202
1247
  },
1203
1248
  clear: () => t(""),
1204
- append: (a) => t((o) => o + a)
1249
+ append: (i) => t((o) => o + i)
1205
1250
  };
1206
1251
  }
1207
- function $(i, n) {
1208
- var r, a, o, c;
1209
- const e = f().pluginRegistry.getBestRendererForType(i);
1252
+ function $(a, n) {
1253
+ var r, i, o, c;
1254
+ const e = f().pluginRegistry.getBestRendererForType(a);
1210
1255
  if (!e)
1211
1256
  return {
1212
1257
  component: null,
@@ -1219,49 +1264,56 @@ function $(i, n) {
1219
1264
  return {
1220
1265
  component: e.component,
1221
1266
  auxiliaryComponent: s,
1222
- supportsFocus: ((a = e.capabilities) == null ? void 0 : a.supportsFocus) ?? !1,
1267
+ supportsFocus: ((i = e.capabilities) == null ? void 0 : i.supportsFocus) ?? !1,
1223
1268
  supportsPreview: ((o = e.capabilities) == null ? void 0 : o.supportsPreview) ?? !1,
1224
1269
  supportsFullscreen: ((c = e.capabilities) == null ? void 0 : c.supportsFullscreen) ?? !1
1225
1270
  };
1226
1271
  }
1227
1272
  function Ct() {
1228
- const i = f(), [n, t] = m(i.contentFocus.state);
1229
- return w(() => i.contentFocus.subscribe(() => {
1230
- t(i.contentFocus.state);
1231
- }), [i]), {
1273
+ const a = f(), [n, t] = m(a.contentFocus.state);
1274
+ return w(() => a.contentFocus.subscribe(() => {
1275
+ t(a.contentFocus.state);
1276
+ }), [a]), {
1232
1277
  ...n,
1233
- focus: (e, s, r, a) => i.contentFocus.focus(e, s, r, a),
1234
- blur: () => i.contentFocus.blur(),
1235
- toggle: (e, s, r, a) => i.contentFocus.toggle(e, s, r, a),
1236
- switchContext: (e) => i.contentFocus.switchContext(e),
1237
- focusNext: () => i.contentFocus.focusNext(i.thread.messages),
1238
- focusPrevious: () => i.contentFocus.focusPrevious(i.thread.messages)
1278
+ focus: (e, s, r, i) => a.contentFocus.focus(e, s, r, i),
1279
+ blur: () => a.contentFocus.blur(),
1280
+ toggle: (e, s, r, i) => a.contentFocus.toggle(e, s, r, i),
1281
+ switchContext: (e) => a.contentFocus.switchContext(e),
1282
+ focusNext: () => a.contentFocus.focusNext(a.thread.messages),
1283
+ focusPrevious: () => a.contentFocus.focusPrevious(a.thread.messages)
1239
1284
  };
1240
1285
  }
1241
- function dt(i) {
1286
+ function dt(a) {
1242
1287
  const n = f();
1243
- w(() => (i.execute && n.capabilityRegistry.registerHandler(i.name, { execute: i.execute }), i.render && n.capabilityRegistry.registerRenderer(i.name, {
1244
- component: i.render
1245
- }), () => {
1246
- n.capabilityRegistry.unregister(i.name);
1247
- }), [i.name, n]);
1288
+ w(() => {
1289
+ const t = a.priority ?? 0;
1290
+ return a.execute && n.capabilityRegistry.registerHandler(a.name, { execute: a.execute }, t), a.render && n.capabilityRegistry.registerRenderer(
1291
+ a.name,
1292
+ {
1293
+ component: a.render
1294
+ },
1295
+ t
1296
+ ), () => {
1297
+ n.capabilityRegistry.unregister(a.name);
1298
+ };
1299
+ }, [a.name, n]);
1248
1300
  }
1249
- function lt(i) {
1301
+ function lt(a) {
1250
1302
  const n = f(), [t, e] = m(
1251
- () => n.capabilityExecutionManager.getExecution(i)
1303
+ () => n.capabilityExecutionManager.getExecution(a)
1252
1304
  ), [, s] = z((r) => r + 1, 0);
1253
1305
  return w(() => {
1254
- const r = n.capabilityExecutionManager.getExecution(i);
1306
+ const r = n.capabilityExecutionManager.getExecution(a);
1255
1307
  return e(r), n.capabilityExecutionManager.subscribe((o) => {
1256
- if (o === i) {
1257
- const c = n.capabilityExecutionManager.getExecution(i);
1308
+ if (o === a) {
1309
+ const c = n.capabilityExecutionManager.getExecution(a);
1258
1310
  e(c), s();
1259
1311
  }
1260
1312
  });
1261
- }, [i, n]), t;
1313
+ }, [a, n]), t;
1262
1314
  }
1263
1315
  const Q = B(void 0);
1264
- function Tt({ children: i }) {
1316
+ function It({ children: a }) {
1265
1317
  const [n, t] = m({}), e = C((c, d) => {
1266
1318
  t((l) => {
1267
1319
  if (d === null) {
@@ -1290,7 +1342,7 @@ function Tt({ children: i }) {
1290
1342
  return { ...c, ...l };
1291
1343
  },
1292
1344
  {}
1293
- ), [n]), a = C(() => {
1345
+ ), [n]), i = C(() => {
1294
1346
  t({});
1295
1347
  }, []), o = V(
1296
1348
  () => ({
@@ -1298,67 +1350,67 @@ function Tt({ children: i }) {
1298
1350
  setContext: e,
1299
1351
  removeContext: s,
1300
1352
  getContextData: r,
1301
- clearContexts: a
1353
+ clearContexts: i
1302
1354
  }),
1303
- [n, e, s, r, a]
1355
+ [n, e, s, r, i]
1304
1356
  );
1305
- return /* @__PURE__ */ S.jsx(Q.Provider, { value: o, children: i });
1357
+ return /* @__PURE__ */ S.jsx(Q.Provider, { value: o, children: a });
1306
1358
  }
1307
1359
  function gt() {
1308
- const i = q(Q);
1309
- if (!i)
1360
+ const a = W(Q);
1361
+ if (!a)
1310
1362
  throw new Error("useChatContext must be used within a ChatContextProvider");
1311
- return i;
1363
+ return a;
1312
1364
  }
1313
- function It(i) {
1314
- const { setContext: n, removeContext: t } = gt(), { id: e = "currentPage", ...s } = i;
1365
+ function Tt(a) {
1366
+ const { setContext: n, removeContext: t } = gt(), { id: e = "currentPage", ...s } = a;
1315
1367
  w(() => (n(e, s), () => {
1316
1368
  t(e);
1317
1369
  }), [e, s.displayName, JSON.stringify(s.data), s.priority, s.icon]);
1318
1370
  }
1319
- function pt({ message: i, content: n }) {
1371
+ function pt({ message: a, content: n }) {
1320
1372
  const t = f(), e = n, s = lt(e.capabilityId), r = t.capabilityRegistry.getRenderer(e.capabilityName);
1321
1373
  if (!r || !s)
1322
1374
  return null;
1323
- const a = r.component;
1375
+ const i = r.component;
1324
1376
  return /* @__PURE__ */ S.jsx(
1325
- a,
1377
+ i,
1326
1378
  {
1327
1379
  capabilityName: s.name,
1328
1380
  capabilityId: s.id,
1329
1381
  args: s.args,
1330
1382
  result: s.result,
1331
1383
  status: s.status,
1332
- message: i
1384
+ message: a
1333
1385
  }
1334
1386
  );
1335
1387
  }
1336
- function vt({ message: i, content: n }) {
1388
+ function vt({ message: a, content: n }) {
1337
1389
  const { component: t } = $(n.type);
1338
1390
  if (n.type === "capability")
1339
- return /* @__PURE__ */ S.jsx(pt, { message: i, content: n });
1391
+ return /* @__PURE__ */ S.jsx(pt, { message: a, content: n });
1340
1392
  if (!t)
1341
1393
  return null;
1342
1394
  const e = t;
1343
- return /* @__PURE__ */ S.jsx(e, { content: n, message: i });
1395
+ return /* @__PURE__ */ S.jsx(e, { content: n, message: a });
1344
1396
  }
1345
1397
  function Rt({
1346
- message: i,
1398
+ message: a,
1347
1399
  content: n,
1348
1400
  context: t,
1349
1401
  onClose: e,
1350
1402
  onNavigate: s,
1351
1403
  onSwitchContext: r
1352
1404
  }) {
1353
- const { auxiliaryComponent: a } = $(n.type, t);
1354
- if (!a)
1405
+ const { auxiliaryComponent: i } = $(n.type, t);
1406
+ if (!i)
1355
1407
  return null;
1356
- const o = a;
1408
+ const o = i;
1357
1409
  return /* @__PURE__ */ S.jsx(
1358
1410
  o,
1359
1411
  {
1360
1412
  content: n,
1361
- message: i,
1413
+ message: a,
1362
1414
  context: t,
1363
1415
  onClose: e,
1364
1416
  onNavigate: s,
@@ -1366,9 +1418,9 @@ function Rt({
1366
1418
  }
1367
1419
  );
1368
1420
  }
1369
- function Et(i) {
1370
- const n = () => (dt(i), null);
1371
- return n.displayName = `Capability(${i.name})`, n;
1421
+ function Et(a) {
1422
+ const n = () => (dt(a), null);
1423
+ return n.displayName = `Capability(${a.name})`, n;
1372
1424
  }
1373
1425
  class Mt {
1374
1426
  getAllowedEvents() {
@@ -1388,7 +1440,7 @@ class Mt {
1388
1440
  }
1389
1441
  async *parseSSEStream(n, t) {
1390
1442
  const e = n.getReader(), s = new TextDecoder();
1391
- let r = "", a = null;
1443
+ let r = "", i = null;
1392
1444
  try {
1393
1445
  for (; ; ) {
1394
1446
  const { done: o, value: c } = await e.read();
@@ -1400,12 +1452,12 @@ class Mt {
1400
1452
  for (const l of d) {
1401
1453
  const h = l.trim();
1402
1454
  if (!h) {
1403
- a = null;
1455
+ i = null;
1404
1456
  continue;
1405
1457
  }
1406
1458
  if (h !== ": ping") {
1407
1459
  if (h.startsWith("event:")) {
1408
- a = h.substring(6).trim();
1460
+ i = h.substring(6).trim();
1409
1461
  continue;
1410
1462
  }
1411
1463
  if (h.startsWith("data:")) {
@@ -1414,16 +1466,16 @@ class Mt {
1414
1466
  break;
1415
1467
  try {
1416
1468
  const p = JSON.parse(g);
1417
- if (a && this.shouldProcessEvent(a)) {
1469
+ if (i && this.shouldProcessEvent(i)) {
1418
1470
  const _ = this.convertEvent({
1419
- event: a,
1471
+ event: i,
1420
1472
  data: p
1421
1473
  });
1422
1474
  _ && (yield _);
1423
1475
  }
1424
1476
  } catch {
1425
1477
  }
1426
- a = null;
1478
+ i = null;
1427
1479
  }
1428
1480
  }
1429
1481
  }
@@ -1441,7 +1493,7 @@ export {
1441
1493
  K as CapabilityExecutionManager,
1442
1494
  Y as CapabilityRegistry,
1443
1495
  pt as CapabilityRendererComp,
1444
- Tt as ChatContextProvider,
1496
+ It as ChatContextProvider,
1445
1497
  et as ComposerRuntime,
1446
1498
  vt as ContentRenderer,
1447
1499
  G as PluginRegistry,
@@ -1449,7 +1501,7 @@ export {
1449
1501
  st as ThreadListRuntime,
1450
1502
  R as ThreadRuntime,
1451
1503
  E as ThreadRuntimeCore,
1452
- I as generateMessageId,
1504
+ T as generateMessageId,
1453
1505
  Et as makeCapability,
1454
1506
  f as useAssistantRuntime,
1455
1507
  dt as useCapability,
@@ -1460,7 +1512,7 @@ export {
1460
1512
  $ as useContentRenderer,
1461
1513
  ot as useCurrentThread,
1462
1514
  St as useMessages,
1463
- It as usePageContext,
1515
+ Tt as usePageContext,
1464
1516
  _t as useThreadList,
1465
1517
  xt as useThreadListState
1466
1518
  };