@teodorruskvi/chat-core 0.1.54 → 0.1.55

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.esm.js CHANGED
@@ -1,9 +1,9 @@
1
1
  var Ht = Object.defineProperty;
2
2
  var Gt = (e, t, n) => t in e ? Ht(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
3
  var at = (e, t, n) => Gt(e, typeof t != "symbol" ? t + "" : t, n);
4
- import { jsx as ie } from "react/jsx-runtime";
4
+ import { jsx as ce } from "react/jsx-runtime";
5
5
  import * as it from "react";
6
- import { createContext as Ce, useContext as je, useState as $, useRef as G, useEffect as X, useCallback as _, useMemo as Y, useReducer as Wt } from "react";
6
+ import { createContext as Ee, useContext as Ue, useState as $, useRef as G, useEffect as X, useCallback as _, useMemo as Y, useReducer as Wt } from "react";
7
7
  import Jt, { isCancel as Xt } from "axios";
8
8
  function It(e) {
9
9
  var t, n, r = "";
@@ -238,7 +238,7 @@ function un(e, ...t) {
238
238
  const D = (e) => {
239
239
  const t = (n) => n[e] || [];
240
240
  return t.isThemeGetter = !0, t;
241
- }, Tt = /^\[(?:([a-z-]+):)?(.+)\]$/i, fn = /^\d+\/\d+$/, pn = /* @__PURE__ */ new Set(["px", "full", "screen"]), gn = /^(\d+(\.\d+)?)?(xs|sm|md|lg|xl)$/, mn = /\d+(%|px|r?em|[sdl]?v([hwib]|min|max)|pt|pc|in|cm|mm|cap|ch|ex|r?lh|cq(w|h|i|b|min|max))|\b(calc|min|max|clamp)\(.+\)|^0$/, yn = /^(rgba?|hsla?|hwb|(ok)?(lab|lch))\(.+\)$/, hn = /^(inset_)?-?((\d+)?\.?(\d+)[a-z]+|0)_-?((\d+)?\.?(\d+)[a-z]+|0)/, bn = /^(url|image|image-set|cross-fade|element|(repeating-)?(linear|radial|conic)-gradient)\(.+\)$/, ue = (e) => Te(e) || pn.has(e) || fn.test(e), be = (e) => Ee(e, "length", Sn), Te = (e) => !!e && !Number.isNaN(Number(e)), qe = (e) => Ee(e, "number", Te), _e = (e) => !!e && Number.isInteger(Number(e)), wn = (e) => e.endsWith("%") && Te(e.slice(0, -1)), N = (e) => Tt.test(e), we = (e) => gn.test(e), kn = /* @__PURE__ */ new Set(["length", "size", "percentage"]), In = (e) => Ee(e, kn, St), vn = (e) => Ee(e, "position", St), xn = /* @__PURE__ */ new Set(["image", "url"]), An = (e) => Ee(e, xn, Cn), Tn = (e) => Ee(e, "", Mn), Ne = () => !0, Ee = (e, t, n) => {
241
+ }, Tt = /^\[(?:([a-z-]+):)?(.+)\]$/i, fn = /^\d+\/\d+$/, pn = /* @__PURE__ */ new Set(["px", "full", "screen"]), gn = /^(\d+(\.\d+)?)?(xs|sm|md|lg|xl)$/, mn = /\d+(%|px|r?em|[sdl]?v([hwib]|min|max)|pt|pc|in|cm|mm|cap|ch|ex|r?lh|cq(w|h|i|b|min|max))|\b(calc|min|max|clamp)\(.+\)|^0$/, yn = /^(rgba?|hsla?|hwb|(ok)?(lab|lch))\(.+\)$/, hn = /^(inset_)?-?((\d+)?\.?(\d+)[a-z]+|0)_-?((\d+)?\.?(\d+)[a-z]+|0)/, bn = /^(url|image|image-set|cross-fade|element|(repeating-)?(linear|radial|conic)-gradient)\(.+\)$/, ge = (e) => Se(e) || pn.has(e) || fn.test(e), be = (e) => Pe(e, "length", Sn), Se = (e) => !!e && !Number.isNaN(Number(e)), qe = (e) => Pe(e, "number", Se), Ne = (e) => !!e && Number.isInteger(Number(e)), wn = (e) => e.endsWith("%") && Se(e.slice(0, -1)), N = (e) => Tt.test(e), we = (e) => gn.test(e), kn = /* @__PURE__ */ new Set(["length", "size", "percentage"]), In = (e) => Pe(e, kn, St), vn = (e) => Pe(e, "position", St), xn = /* @__PURE__ */ new Set(["image", "url"]), An = (e) => Pe(e, xn, Cn), Tn = (e) => Pe(e, "", Mn), ze = () => !0, Pe = (e, t, n) => {
242
242
  const r = Tt.exec(e);
243
243
  return r ? r[1] ? typeof t == "string" ? r[1] === t : t.has(r[1]) : n(r[2]) : !1;
244
244
  }, Sn = (e) => (
@@ -247,13 +247,13 @@ const D = (e) => {
247
247
  // I could also use lookbehind assertion in `lengthUnitRegex` but that isn't supported widely enough.
248
248
  mn.test(e) && !yn.test(e)
249
249
  ), St = () => !1, Mn = (e) => hn.test(e), Cn = (e) => bn.test(e), En = () => {
250
- const e = D("colors"), t = D("spacing"), n = D("blur"), r = D("brightness"), o = D("borderColor"), s = D("borderRadius"), a = D("borderSpacing"), i = D("borderWidth"), d = D("contrast"), c = D("grayscale"), l = D("hueRotate"), f = D("invert"), p = D("gap"), g = D("gradientColorStops"), I = D("gradientColorStopPositions"), u = D("inset"), v = D("margin"), x = D("opacity"), M = D("padding"), A = D("saturate"), w = D("scale"), b = D("sepia"), h = D("skew"), T = D("space"), m = D("translate"), k = () => ["auto", "contain", "none"], E = () => ["auto", "hidden", "clip", "visible", "scroll"], j = () => ["auto", N, t], C = () => [N, t], O = () => ["", ue, be], F = () => ["auto", Te, N], P = () => ["bottom", "center", "left", "left-bottom", "left-top", "right", "right-bottom", "right-top", "top"], B = () => ["solid", "dashed", "dotted", "double", "none"], z = () => ["normal", "multiply", "screen", "overlay", "darken", "lighten", "color-dodge", "color-burn", "hard-light", "soft-light", "difference", "exclusion", "hue", "saturation", "color", "luminosity"], J = () => ["start", "end", "center", "between", "around", "evenly", "stretch"], U = () => ["", "0", N], R = () => ["auto", "avoid", "all", "avoid-page", "page", "left", "right", "column"], q = () => [Te, N];
250
+ const e = D("colors"), t = D("spacing"), n = D("blur"), r = D("brightness"), o = D("borderColor"), s = D("borderRadius"), a = D("borderSpacing"), i = D("borderWidth"), d = D("contrast"), c = D("grayscale"), l = D("hueRotate"), f = D("invert"), p = D("gap"), g = D("gradientColorStops"), I = D("gradientColorStopPositions"), u = D("inset"), v = D("margin"), x = D("opacity"), M = D("padding"), A = D("saturate"), w = D("scale"), b = D("sepia"), h = D("skew"), T = D("space"), m = D("translate"), k = () => ["auto", "contain", "none"], E = () => ["auto", "hidden", "clip", "visible", "scroll"], j = () => ["auto", N, t], C = () => [N, t], O = () => ["", ge, be], F = () => ["auto", Se, N], P = () => ["bottom", "center", "left", "left-bottom", "left-top", "right", "right-bottom", "right-top", "top"], B = () => ["solid", "dashed", "dotted", "double", "none"], z = () => ["normal", "multiply", "screen", "overlay", "darken", "lighten", "color-dodge", "color-burn", "hard-light", "soft-light", "difference", "exclusion", "hue", "saturation", "color", "luminosity"], J = () => ["start", "end", "center", "between", "around", "evenly", "stretch"], U = () => ["", "0", N], R = () => ["auto", "avoid", "all", "avoid-page", "page", "left", "right", "column"], q = () => [Se, N];
251
251
  return {
252
252
  cacheSize: 500,
253
253
  separator: ":",
254
254
  theme: {
255
- colors: [Ne],
256
- spacing: [ue, be],
255
+ colors: [ze],
256
+ spacing: [ge, be],
257
257
  blur: ["none", "", we, N],
258
258
  brightness: q(),
259
259
  borderColor: [e],
@@ -492,7 +492,7 @@ const D = (e) => {
492
492
  * @see https://tailwindcss.com/docs/z-index
493
493
  */
494
494
  z: [{
495
- z: ["auto", _e, N]
495
+ z: ["auto", Ne, N]
496
496
  }],
497
497
  // Flexbox and Grid
498
498
  /**
@@ -542,14 +542,14 @@ const D = (e) => {
542
542
  * @see https://tailwindcss.com/docs/order
543
543
  */
544
544
  order: [{
545
- order: ["first", "last", "none", _e, N]
545
+ order: ["first", "last", "none", Ne, N]
546
546
  }],
547
547
  /**
548
548
  * Grid Template Columns
549
549
  * @see https://tailwindcss.com/docs/grid-template-columns
550
550
  */
551
551
  "grid-cols": [{
552
- "grid-cols": [Ne]
552
+ "grid-cols": [ze]
553
553
  }],
554
554
  /**
555
555
  * Grid Column Start / End
@@ -557,7 +557,7 @@ const D = (e) => {
557
557
  */
558
558
  "col-start-end": [{
559
559
  col: ["auto", {
560
- span: ["full", _e, N]
560
+ span: ["full", Ne, N]
561
561
  }, N]
562
562
  }],
563
563
  /**
@@ -579,7 +579,7 @@ const D = (e) => {
579
579
  * @see https://tailwindcss.com/docs/grid-template-rows
580
580
  */
581
581
  "grid-rows": [{
582
- "grid-rows": [Ne]
582
+ "grid-rows": [ze]
583
583
  }],
584
584
  /**
585
585
  * Grid Row Start / End
@@ -587,7 +587,7 @@ const D = (e) => {
587
587
  */
588
588
  "row-start-end": [{
589
589
  row: ["auto", {
590
- span: [_e, N]
590
+ span: [Ne, N]
591
591
  }, N]
592
592
  }],
593
593
  /**
@@ -942,7 +942,7 @@ const D = (e) => {
942
942
  * @see https://tailwindcss.com/docs/font-family
943
943
  */
944
944
  "font-family": [{
945
- font: [Ne]
945
+ font: [ze]
946
946
  }],
947
947
  /**
948
948
  * Font Variant Numeric
@@ -986,14 +986,14 @@ const D = (e) => {
986
986
  * @see https://tailwindcss.com/docs/line-clamp
987
987
  */
988
988
  "line-clamp": [{
989
- "line-clamp": ["none", Te, qe]
989
+ "line-clamp": ["none", Se, qe]
990
990
  }],
991
991
  /**
992
992
  * Line Height
993
993
  * @see https://tailwindcss.com/docs/line-height
994
994
  */
995
995
  leading: [{
996
- leading: ["none", "tight", "snug", "normal", "relaxed", "loose", ue, N]
996
+ leading: ["none", "tight", "snug", "normal", "relaxed", "loose", ge, N]
997
997
  }],
998
998
  /**
999
999
  * List Style Image
@@ -1069,14 +1069,14 @@ const D = (e) => {
1069
1069
  * @see https://tailwindcss.com/docs/text-decoration-thickness
1070
1070
  */
1071
1071
  "text-decoration-thickness": [{
1072
- decoration: ["auto", "from-font", ue, be]
1072
+ decoration: ["auto", "from-font", ge, be]
1073
1073
  }],
1074
1074
  /**
1075
1075
  * Text Underline Offset
1076
1076
  * @see https://tailwindcss.com/docs/text-underline-offset
1077
1077
  */
1078
1078
  "underline-offset": [{
1079
- "underline-offset": ["auto", ue, N]
1079
+ "underline-offset": ["auto", ge, N]
1080
1080
  }],
1081
1081
  /**
1082
1082
  * Text Decoration Color
@@ -1558,14 +1558,14 @@ const D = (e) => {
1558
1558
  * @see https://tailwindcss.com/docs/outline-offset
1559
1559
  */
1560
1560
  "outline-offset": [{
1561
- "outline-offset": [ue, N]
1561
+ "outline-offset": [ge, N]
1562
1562
  }],
1563
1563
  /**
1564
1564
  * Outline Width
1565
1565
  * @see https://tailwindcss.com/docs/outline-width
1566
1566
  */
1567
1567
  "outline-w": [{
1568
- outline: [ue, be]
1568
+ outline: [ge, be]
1569
1569
  }],
1570
1570
  /**
1571
1571
  * Outline Color
@@ -1605,7 +1605,7 @@ const D = (e) => {
1605
1605
  * @see https://tailwindcss.com/docs/ring-offset-width
1606
1606
  */
1607
1607
  "ring-offset-w": [{
1608
- "ring-offset": [ue, be]
1608
+ "ring-offset": [ge, be]
1609
1609
  }],
1610
1610
  /**
1611
1611
  * Ring Offset Color
@@ -1627,7 +1627,7 @@ const D = (e) => {
1627
1627
  * @see https://tailwindcss.com/docs/box-shadow-color
1628
1628
  */
1629
1629
  "shadow-color": [{
1630
- shadow: [Ne]
1630
+ shadow: [ze]
1631
1631
  }],
1632
1632
  /**
1633
1633
  * Opacity
@@ -1906,7 +1906,7 @@ const D = (e) => {
1906
1906
  * @see https://tailwindcss.com/docs/rotate
1907
1907
  */
1908
1908
  rotate: [{
1909
- rotate: [_e, N]
1909
+ rotate: [Ne, N]
1910
1910
  }],
1911
1911
  /**
1912
1912
  * Translate X
@@ -2200,7 +2200,7 @@ const D = (e) => {
2200
2200
  * @see https://tailwindcss.com/docs/stroke-width
2201
2201
  */
2202
2202
  "stroke-w": [{
2203
- stroke: [ue, be, qe]
2203
+ stroke: [ge, be, qe]
2204
2204
  }],
2205
2205
  /**
2206
2206
  * Stroke
@@ -2416,41 +2416,41 @@ function Fn(e) {
2416
2416
  function so(e) {
2417
2417
  return e.content.some((t) => t.type === "text" ? t.text.trim().length > 0 : t.type === "image_url" || t.type === "file");
2418
2418
  }
2419
- function ce(e) {
2419
+ function de(e) {
2420
2420
  if (e == null) return e;
2421
2421
  if (typeof e == "string") {
2422
2422
  const t = e.trim();
2423
2423
  if (t.startsWith("{") || t.startsWith("[")) {
2424
2424
  const n = tt(t);
2425
2425
  if (n !== null)
2426
- return ce(n);
2426
+ return de(n);
2427
2427
  }
2428
2428
  return e;
2429
2429
  }
2430
2430
  if (typeof e == "object" && !Array.isArray(e)) {
2431
2431
  if (e.content !== void 0)
2432
- return Array.isArray(e.content) ? e.content.map((t) => ce(t)).filter(Boolean).join(`
2433
- `) : ce(e.content);
2432
+ return Array.isArray(e.content) ? e.content.map((t) => de(t)).filter(Boolean).join(`
2433
+ `) : de(e.content);
2434
2434
  if (e.value !== void 0)
2435
- return ce(e.value);
2435
+ return de(e.value);
2436
2436
  }
2437
- return typeof e == "object" && !Array.isArray(e) && (e.type === "text" && typeof e.text == "string" || typeof e.text == "string") ? e.text : Array.isArray(e) && e.length > 0 ? ce(e[0]) : e;
2437
+ return typeof e == "object" && !Array.isArray(e) && (e.type === "text" && typeof e.text == "string" || typeof e.text == "string") ? e.text : Array.isArray(e) && e.length > 0 ? de(e[0]) : e;
2438
2438
  }
2439
2439
  function On(e) {
2440
2440
  return e.replace(/[_-](\w)/g, (t, n) => n ? n.toUpperCase() : "").replace(/^\w/, (t) => t.toLowerCase());
2441
2441
  }
2442
- function le(e) {
2443
- return !e || typeof e != "object" ? e : Array.isArray(e) ? e.map(le) : Object.fromEntries(
2442
+ function ue(e) {
2443
+ return !e || typeof e != "object" ? e : Array.isArray(e) ? e.map(ue) : Object.fromEntries(
2444
2444
  Object.entries(e).map(([t, n]) => [
2445
2445
  On(t),
2446
- le(n)
2446
+ ue(n)
2447
2447
  ])
2448
2448
  );
2449
2449
  }
2450
2450
  function nt(e) {
2451
2451
  var x, M, A, w, b, h, T;
2452
2452
  if (!e || typeof e != "object") return null;
2453
- const t = le(e), n = Kn(t);
2453
+ const t = ue(e), n = Kn(t);
2454
2454
  if (!n)
2455
2455
  return null;
2456
2456
  const r = t.toolCalls ?? ((x = t.additionalKwargs) == null ? void 0 : x.toolCalls), o = Array.isArray(r) ? r.map(rt) : void 0, s = Dn(t.content ?? t.parts, !!(o != null && o.length)), a = t.id ?? `msg-${Date.now().toString(16)}-${Math.random().toString(16).slice(2)}`, i = t.createdAt ?? ((M = t.additionalKwargs) == null ? void 0 : M.createdAt) ?? (/* @__PURE__ */ new Date()).toISOString(), d = t.name ?? ((A = t.additionalKwargs) == null ? void 0 : A.agentName) ?? void 0, c = t.model ?? ((w = t.additionalKwargs) == null ? void 0 : w.model) ?? void 0, l = t.toolCallId ?? void 0, f = t.additionalKwargs ?? void 0, p = t.responseMetadata ?? void 0, g = t.checkpointId ?? ((b = t.additionalKwargs) == null ? void 0 : b.checkpointId) ?? void 0, I = t.checkpointNs ?? ((h = t.additionalKwargs) == null ? void 0 : h.checkpointNs) ?? void 0, u = typeof t.toolStreaming == "boolean" ? t.toolStreaming : !!((T = t.additionalKwargs) != null && T.toolStreaming);
@@ -2474,7 +2474,7 @@ function nt(e) {
2474
2474
  }
2475
2475
  function $n(e) {
2476
2476
  if (!e || typeof e != "object") return e;
2477
- const t = le(e);
2477
+ const t = ue(e);
2478
2478
  if (t.tool && !t.name && (t.name = t.tool), t.type === "artifact.update" && t.artifact && typeof t.artifact == "object") {
2479
2479
  const n = t.artifact;
2480
2480
  !n.type && t.name && (n.type = t.name);
@@ -2482,7 +2482,7 @@ function $n(e) {
2482
2482
  return t;
2483
2483
  }
2484
2484
  function rt(e) {
2485
- const t = le(e), n = t.content ?? t.output ?? void 0;
2485
+ const t = ue(e), n = t.content ?? t.output ?? void 0;
2486
2486
  return {
2487
2487
  ...t,
2488
2488
  type: "tool_call",
@@ -2493,12 +2493,12 @@ function rt(e) {
2493
2493
  };
2494
2494
  }
2495
2495
  function Dn(e, t) {
2496
- return typeof e == "string" ? [{ type: "text", text: e }] : Array.isArray(e) ? e.map((n) => typeof n == "string" ? { type: "text", text: n } : n.type === "tool_call" || n.type === "tool" ? t ? null : rt(le(n)) : n).filter(
2496
+ return typeof e == "string" ? [{ type: "text", text: e }] : Array.isArray(e) ? e.map((n) => typeof n == "string" ? { type: "text", text: n } : n.type === "tool_call" || n.type === "tool" ? t ? null : rt(ue(n)) : n).filter(
2497
2497
  (n) => !!(n && typeof n == "object" && typeof n.type == "string")
2498
2498
  ) : [];
2499
2499
  }
2500
2500
  function Kn(e) {
2501
- const t = le(e), n = t.role;
2501
+ const t = ue(e), n = t.role;
2502
2502
  if (n && (n === "assistant" || n === "user" || n === "system" || n === "tool"))
2503
2503
  return n;
2504
2504
  const r = t.type;
@@ -2540,7 +2540,7 @@ function qn(e, t = []) {
2540
2540
  var f, p, g;
2541
2541
  const i = He(a.id, e) || He(a.id, t), d = Array.isArray((f = i == null ? void 0 : i.additionalKwargs) == null ? void 0 : f.toolProgress) ? (p = i == null ? void 0 : i.additionalKwargs) == null ? void 0 : p.toolProgress : void 0, c = (i == null ? void 0 : i.artifact) ?? a.artifact, l = dt(a.status) ?? ((g = i == null ? void 0 : i.additionalKwargs) != null && g.toolStreaming ? "pending" : null) ?? (i ? "completed" : "pending");
2542
2542
  if (i && !a.content && l === "completed") {
2543
- const I = ce(i);
2543
+ const I = de(i);
2544
2544
  typeof I == "string" && (a.content = I);
2545
2545
  }
2546
2546
  return {
@@ -2558,7 +2558,7 @@ function qn(e, t = []) {
2558
2558
  var d, c, l;
2559
2559
  const o = He(r.id, t) ?? Vn(e, r), s = Array.isArray((d = o == null ? void 0 : o.additionalKwargs) == null ? void 0 : d.toolProgress) ? (c = o == null ? void 0 : o.additionalKwargs) == null ? void 0 : c.toolProgress : void 0, a = (o == null ? void 0 : o.artifact) ?? r.artifact, i = dt(r.status) ?? ((l = o == null ? void 0 : o.additionalKwargs) != null && l.toolStreaming ? "pending" : null) ?? (o ? "completed" : "pending");
2560
2560
  if (o && !r.content && i === "completed") {
2561
- const f = ce(o);
2561
+ const f = de(o);
2562
2562
  typeof f == "string" && (r.content = f);
2563
2563
  }
2564
2564
  return {
@@ -2573,7 +2573,7 @@ function qn(e, t = []) {
2573
2573
  function ao(e, t = []) {
2574
2574
  return qn(e, t).map((n) => n.artifact).filter((n) => !!n);
2575
2575
  }
2576
- const ze = {
2576
+ const Re = {
2577
2577
  authToken: "authToken",
2578
2578
  authTokenExpiresAt: "authTokenExpiresAt",
2579
2579
  authSessionId: "authSessionId",
@@ -2590,7 +2590,7 @@ const Qe = /* @__PURE__ */ new Set();
2590
2590
  function Pt() {
2591
2591
  return Ve;
2592
2592
  }
2593
- function Ie(e) {
2593
+ function ve(e) {
2594
2594
  Ve = e, Qe.forEach((t) => t(e));
2595
2595
  }
2596
2596
  function _t(e) {
@@ -2619,7 +2619,7 @@ ne.interceptors.response.use(
2619
2619
  if (typeof window < "u" && (e != null && e.data))
2620
2620
  try {
2621
2621
  const t = e.data, n = t.data || {}, r = typeof t.token == "string" && t.token || typeof n.token == "string" && n.token || null, o = typeof t.expires_at == "number" && t.expires_at || typeof n.expires_at == "number" && n.expires_at || typeof t.expiresAt == "number" && t.expiresAt || typeof n.expiresAt == "number" && n.expiresAt || null;
2622
- r && (localStorage.setItem(ze.authToken, r), Ie(r)), o && localStorage.setItem(ze.authTokenExpiresAt, o.toString()), (r || o) && window.dispatchEvent(
2622
+ r && (localStorage.setItem(Re.authToken, r), ve(r)), o && localStorage.setItem(Re.authTokenExpiresAt, o.toString()), (r || o) && window.dispatchEvent(
2623
2623
  new CustomEvent("smartorg:chat-token-updated", {
2624
2624
  detail: { token: r, expiresAt: o }
2625
2625
  })
@@ -2631,7 +2631,7 @@ ne.interceptors.response.use(
2631
2631
  },
2632
2632
  (e) => Promise.reject(e)
2633
2633
  );
2634
- async function se(e, t) {
2634
+ async function ae(e, t) {
2635
2635
  return ne.defaults.baseURL ? (await ne.get(e, t)).data : Promise.reject(new Error("API base URL is not configured"));
2636
2636
  }
2637
2637
  async function Ze(e, t, n) {
@@ -2643,7 +2643,7 @@ async function Xn(e, t, n) {
2643
2643
  async function Qn(e, t) {
2644
2644
  return ne.defaults.baseURL ? (await ne.delete(e, t)).data : Promise.reject(new Error("API base URL is not configured"));
2645
2645
  }
2646
- function ae(e) {
2646
+ function ie(e) {
2647
2647
  return e && typeof e == "object" && "data" in e ? e.data : e;
2648
2648
  }
2649
2649
  class Yn {
@@ -2660,7 +2660,7 @@ class Yn {
2660
2660
  const n = t.baseUrl.replace(/\/+$/, "");
2661
2661
  ne.defaults.baseURL = n;
2662
2662
  }
2663
- t.apiKey !== void 0 ? Ie(t.apiKey) : t.getToken && Ie(t.getToken()), (t.requestTimeoutMs !== void 0 || t.historyTimeoutMs !== void 0) && Wn({
2663
+ t.apiKey !== void 0 ? ve(t.apiKey) : t.getToken && ve(t.getToken()), (t.requestTimeoutMs !== void 0 || t.historyTimeoutMs !== void 0) && Wn({
2664
2664
  requestTimeoutMs: t.requestTimeoutMs,
2665
2665
  historyTimeoutMs: t.historyTimeoutMs
2666
2666
  });
@@ -2670,14 +2670,14 @@ class Yn {
2670
2670
  return Pt();
2671
2671
  }
2672
2672
  setAuthToken(t) {
2673
- Ie(t);
2673
+ ve(t);
2674
2674
  }
2675
2675
  onTokenChange(t) {
2676
2676
  return _t(t);
2677
2677
  }
2678
2678
  // ---- Thread Management ----
2679
2679
  async listThreads() {
2680
- const t = await se("/threads"), n = ae(t);
2680
+ const t = await ae("/threads"), n = ie(t);
2681
2681
  return (n == null ? void 0 : n.items) ?? (t == null ? void 0 : t.items) ?? [];
2682
2682
  }
2683
2683
  // listUsers moved to AuthApi
@@ -2685,44 +2685,44 @@ class Yn {
2685
2685
  const r = {};
2686
2686
  t && (r.title = t), n && (r.project = n);
2687
2687
  const o = await Ze("/threads", Object.keys(r).length ? r : void 0);
2688
- return ae(o);
2688
+ return ie(o);
2689
2689
  }
2690
2690
  async updateThread(t, n) {
2691
2691
  const r = await Xn(`/threads/${t}`, { title: n });
2692
- return ae(r);
2692
+ return ie(r);
2693
2693
  }
2694
2694
  async deleteThread(t) {
2695
2695
  const n = await Qn(`/threads/${t}`);
2696
- return ae(n);
2696
+ return ie(n);
2697
2697
  }
2698
2698
  async threadInfo(t) {
2699
- const n = await se(`/threads/${t}/info`);
2700
- return ae(n);
2699
+ const n = await ae(`/threads/${t}/info`);
2700
+ return ie(n);
2701
2701
  }
2702
2702
  // ---- Agents ----
2703
2703
  async listAgents() {
2704
- const t = await se("/agents"), n = ae(t);
2704
+ const t = await ae("/agents"), n = ie(t);
2705
2705
  return (n == null ? void 0 : n.items) ?? [];
2706
2706
  }
2707
2707
  async getAgent(t) {
2708
- const n = await se(`/agents/${t}`);
2709
- return ae(n);
2708
+ const n = await ae(`/agents/${t}`);
2709
+ return ie(n);
2710
2710
  }
2711
2711
  async getAgentSchema(t) {
2712
- const n = await se(`/agents/${t}/schema`);
2713
- return ae(n);
2712
+ const n = await ae(`/agents/${t}/schema`);
2713
+ return ie(n);
2714
2714
  }
2715
2715
  async getState(t) {
2716
- const n = await se(`/threads/${t.threadId}/state`, {
2716
+ const n = await ae(`/threads/${t.threadId}/state`, {
2717
2717
  params: {
2718
2718
  checkpoint_id: t.checkpointId,
2719
2719
  checkpoint_ns: t.checkpointNs
2720
2720
  }
2721
2721
  });
2722
- return ae(n);
2722
+ return ie(n);
2723
2723
  }
2724
2724
  async getStateHistory(t) {
2725
- const n = await se(`/threads/${t.threadId}/state/history`, {
2725
+ const n = await ae(`/threads/${t.threadId}/state/history`, {
2726
2726
  params: {
2727
2727
  checkpoint_id: t.checkpointId,
2728
2728
  checkpoint_ns: t.checkpointNs,
@@ -2733,12 +2733,12 @@ class Yn {
2733
2733
  timeout: Jn(),
2734
2734
  signal: t.signal
2735
2735
  });
2736
- return ae(n);
2736
+ return ie(n);
2737
2737
  }
2738
2738
  // ---- Run Management ----
2739
2739
  async cancelRun(t) {
2740
2740
  const n = await Ze(`/runs/${t}/cancel`);
2741
- return ae(n);
2741
+ return ie(n);
2742
2742
  }
2743
2743
  }
2744
2744
  const zt = new Yn();
@@ -2746,7 +2746,7 @@ class Zn {
2746
2746
  // Config logic could go here if files ever need a distinct base URL from chat
2747
2747
  // For now, it shares the chatBaseUrl via the shared httpClient
2748
2748
  async fetchFileBlob(t, n) {
2749
- return await se(t, {
2749
+ return await ae(t, {
2750
2750
  responseType: "blob",
2751
2751
  onDownloadProgress: (o) => {
2752
2752
  try {
@@ -2775,7 +2775,7 @@ class Zn {
2775
2775
  const r = await this.fetchFileBlob(`/files/${t}/download`, n), o = URL.createObjectURL(r);
2776
2776
  let s = `file_${t}`;
2777
2777
  try {
2778
- const i = await se(`/files/${t}`);
2778
+ const i = await ae(`/files/${t}`);
2779
2779
  s = (i == null ? void 0 : i.filename) ?? s;
2780
2780
  } catch {
2781
2781
  }
@@ -2783,11 +2783,11 @@ class Zn {
2783
2783
  a.href = o, a.download = s, document.body.appendChild(a), a.click(), a.remove(), setTimeout(() => URL.revokeObjectURL(o), 6e4);
2784
2784
  }
2785
2785
  async getFileInfo(t) {
2786
- return await se(`/files/${t}`);
2786
+ return await ae(`/files/${t}`);
2787
2787
  }
2788
2788
  async listFiles(t, n = 50, r = 0) {
2789
2789
  const o = { limit: n, offset: r };
2790
- return t && (o.thread_id = t), await se("/files", { params: o });
2790
+ return t && (o.thread_id = t), await ae("/files", { params: o });
2791
2791
  }
2792
2792
  async uploadFile(t, n, r) {
2793
2793
  const o = new FormData();
@@ -2820,25 +2820,25 @@ class er {
2820
2820
  const n = t.baseUrl.replace(/\/+$/, "");
2821
2821
  this._authBaseUrl = `${n}/auth`;
2822
2822
  }
2823
- t.apiKey !== void 0 ? Ie(t.apiKey) : t.getToken && Ie(t.getToken());
2823
+ t.apiKey !== void 0 ? ve(t.apiKey) : t.getToken && ve(t.getToken());
2824
2824
  }
2825
2825
  // ---- Token Management ----
2826
2826
  getAuthToken() {
2827
2827
  return Pt();
2828
2828
  }
2829
2829
  setAuthToken(t) {
2830
- Ie(t);
2830
+ ve(t);
2831
2831
  }
2832
2832
  onTokenChange(t) {
2833
2833
  return _t(t);
2834
2834
  }
2835
2835
  // ---- User Management ----
2836
2836
  async listUsers() {
2837
- const t = await se("/users", { baseURL: this.baseUrl });
2837
+ const t = await ae("/users", { baseURL: this.baseUrl });
2838
2838
  return (t == null ? void 0 : t.users) ?? [];
2839
2839
  }
2840
2840
  }
2841
- const tr = new er(), Lt = Ce(null);
2841
+ const tr = new er(), Lt = Ee(null);
2842
2842
  function nr({ children: e, apiConfig: t }) {
2843
2843
  const n = zt, r = tr;
2844
2844
  if (t) {
@@ -2855,7 +2855,7 @@ function nr({ children: e, apiConfig: t }) {
2855
2855
  fileApi: Rt,
2856
2856
  baseUrl: n.baseUrl
2857
2857
  };
2858
- return /* @__PURE__ */ ie(Lt.Provider, { value: o, children: e });
2858
+ return /* @__PURE__ */ ce(Lt.Provider, { value: o, children: e });
2859
2859
  }
2860
2860
  async function rr(e) {
2861
2861
  return await new Promise((t, n) => {
@@ -2930,8 +2930,8 @@ function lo(e) {
2930
2930
  }).join("|") : "", r = "checkpointId" in e && e.checkpointId ? `cp:${e.checkpointId}` : "", o = "checkpointNs" in e && e.checkpointNs ? `ns:${e.checkpointNs}` : "", s = [r, o].filter(Boolean).join("|");
2931
2931
  return `${e.id}:${t}:${n}${s ? `:${s}` : ""}`;
2932
2932
  }
2933
- function Me() {
2934
- const e = je(Lt);
2933
+ function Ce() {
2934
+ const e = Ue(Lt);
2935
2935
  if (!e) throw new Error("useApi must be used within ApiProvider");
2936
2936
  return e;
2937
2937
  }
@@ -2951,26 +2951,26 @@ function jt(e, t = "user") {
2951
2951
  function cr(e) {
2952
2952
  return !!e && typeof e == "object" && "data" in e;
2953
2953
  }
2954
- function Se(e) {
2954
+ function Me(e) {
2955
2955
  return typeof e == "object" && e !== null;
2956
2956
  }
2957
2957
  function lr(e) {
2958
- return Se(e) ? typeof e.id == "string" && typeof e.type == "string" && "content" in e : !1;
2958
+ return Me(e) ? typeof e.id == "string" && typeof e.type == "string" && "content" in e : !1;
2959
2959
  }
2960
2960
  function dr(e) {
2961
2961
  return Array.isArray(e) && e.every(lr);
2962
2962
  }
2963
2963
  function ur(e) {
2964
- if (!Se(e)) return {};
2964
+ if (!Me(e)) return {};
2965
2965
  const t = {};
2966
2966
  for (const [n, r] of Object.entries(e))
2967
- Se(r) && (t[n] = {
2968
- metadata: Se(r.metadata) ? r.metadata : void 0
2967
+ Me(r) && (t[n] = {
2968
+ metadata: Me(r.metadata) ? r.metadata : void 0
2969
2969
  });
2970
2970
  return t;
2971
2971
  }
2972
2972
  function fr(e, t) {
2973
- if (!e || !Se(t))
2973
+ if (!e || !Me(t))
2974
2974
  return t;
2975
2975
  if (e === "messages/partial") {
2976
2976
  const r = t;
@@ -2997,7 +2997,7 @@ function fr(e, t) {
2997
2997
  }
2998
2998
  if (e === "messages/metadata") {
2999
2999
  const r = t;
3000
- return (r == null ? void 0 : r.type) === "messages/metadata" && Se(r.metadata) ? {
3000
+ return (r == null ? void 0 : r.type) === "messages/metadata" && Me(r.metadata) ? {
3001
3001
  type: "messages/metadata",
3002
3002
  metadata: ur(r.metadata),
3003
3003
  seq: typeof r.seq == "number" ? r.seq : void 0,
@@ -3067,11 +3067,11 @@ function pr(e = {}) {
3067
3067
  if (r)
3068
3068
  U = { Authorization: `Bearer ${r}` };
3069
3069
  else if (typeof window < "u") {
3070
- const te = window.__AUTH_TOKEN__, ke = ((O = window.localStorage) == null ? void 0 : O.getItem(ze.authToken)) || null;
3070
+ const te = window.__AUTH_TOKEN__, ke = ((O = window.localStorage) == null ? void 0 : O.getItem(Re.authToken)) || null;
3071
3071
  let Q = null;
3072
3072
  try {
3073
- const ge = (F = window.localStorage) == null ? void 0 : F.getItem(ze.chatSettings);
3074
- ge && (Q = ((P = JSON.parse(ge)) == null ? void 0 : P.apiKey) || null);
3073
+ const pe = (F = window.localStorage) == null ? void 0 : F.getItem(Re.chatSettings);
3074
+ pe && (Q = ((P = JSON.parse(pe)) == null ? void 0 : P.apiKey) || null);
3075
3075
  } catch {
3076
3076
  }
3077
3077
  const Z = te || ke || Q || null;
@@ -3083,7 +3083,7 @@ function pr(e = {}) {
3083
3083
  let R = {};
3084
3084
  if (typeof window < "u")
3085
3085
  try {
3086
- const H = (B = window.localStorage) == null ? void 0 : B.getItem(ze.authSessionId);
3086
+ const H = (B = window.localStorage) == null ? void 0 : B.getItem(Re.authSessionId);
3087
3087
  H && (R = { "X-Session-Id": H });
3088
3088
  } catch {
3089
3089
  }
@@ -3115,9 +3115,9 @@ function pr(e = {}) {
3115
3115
  I(!0);
3116
3116
  const S = L.body.getReader(), W = new TextDecoder();
3117
3117
  let K = "", V = 0, ee = Date.now();
3118
- const re = setInterval(() => {
3118
+ const oe = setInterval(() => {
3119
3119
  Date.now() - ee > 18e5 && (p("Stream idle timeout"), j = "error", E.abort());
3120
- }, 5e3), de = 1024 * 1024;
3120
+ }, 5e3), fe = 1024 * 1024;
3121
3121
  try {
3122
3122
  const H = L.headers.get("X-Thread-Id") || L.headers.get("x-thread-id"), te = L.headers.get("X-Run-Id") || L.headers.get("x-run-id");
3123
3123
  for (A.current && (A.current = {
@@ -3127,54 +3127,54 @@ function pr(e = {}) {
3127
3127
  }); ; ) {
3128
3128
  const { value: ke, done: Q } = await S.read();
3129
3129
  if (Q) break;
3130
- K += W.decode(ke, { stream: !0 }), K.length > de && (K = "", console.warn("Stream buffer reset: exceeded 1MB"));
3130
+ K += W.decode(ke, { stream: !0 }), K.length > fe && (K = "", console.warn("Stream buffer reset: exceeded 1MB"));
3131
3131
  let Z;
3132
3132
  for (; (Z = K.indexOf(`
3133
3133
 
3134
3134
  `)) >= 0; ) {
3135
- const ge = K.slice(0, Z);
3135
+ const pe = K.slice(0, Z);
3136
3136
  K = K.slice(Z + 2);
3137
- const Ue = ge.split(/\r?\n/), Be = [];
3138
- let me = null;
3139
- for (const he of Ue)
3140
- if (!he.startsWith(":") && he) {
3141
- if (he.startsWith("event:")) {
3142
- const Ae = he.slice(6).trim();
3143
- Ae && (me = Ae);
3137
+ const _e = pe.split(/\r?\n/), Be = [];
3138
+ let he = null;
3139
+ for (const le of _e)
3140
+ if (!le.startsWith(":") && le) {
3141
+ if (le.startsWith("event:")) {
3142
+ const Te = le.slice(6).trim();
3143
+ Te && (he = Te);
3144
3144
  continue;
3145
3145
  }
3146
- if (he.startsWith("data:")) {
3147
- const Ae = he.slice(5);
3148
- Be.push(Ae.startsWith(" ") ? Ae.slice(1) : Ae);
3146
+ if (le.startsWith("data:")) {
3147
+ const Te = le.slice(5);
3148
+ Be.push(Te.startsWith(" ") ? Te.slice(1) : Te);
3149
3149
  }
3150
3150
  }
3151
- const ve = Be.join(`
3151
+ const xe = Be.join(`
3152
3152
  `);
3153
- if (!ve) continue;
3154
- let oe;
3153
+ if (!xe) continue;
3154
+ let se;
3155
3155
  try {
3156
- oe = JSON.parse(ve);
3156
+ se = JSON.parse(xe);
3157
3157
  } catch {
3158
- console.debug("[use-chat] malformed SSE:", ve.slice(0, 200));
3158
+ console.debug("[use-chat] malformed SSE:", xe.slice(0, 200));
3159
3159
  continue;
3160
3160
  }
3161
- oe = fr(me, oe);
3162
- const Pe = cr(oe) ? oe.data : oe;
3163
- if (!Pe) {
3164
- console.warn("[use-chat] dropped envelope event without payload", oe);
3161
+ se = fr(he, se);
3162
+ const Ie = cr(se) ? se.data : se;
3163
+ if (!Ie) {
3164
+ console.warn("[use-chat] dropped envelope event without payload", se);
3165
3165
  continue;
3166
3166
  }
3167
- const xe = $n(Pe);
3167
+ const Ae = $n(Ie);
3168
3168
  ee = Date.now();
3169
- const ye = typeof xe.seq == "number" ? xe.seq : void 0;
3170
- if (ye !== void 0) {
3171
- if (ye <= V) {
3172
- console.debug("[use-chat] drop duplicate/out-of-order event", { seq: ye, lastProcessedSeq: V, type: xe.type });
3169
+ const re = typeof Ae.seq == "number" ? Ae.seq : void 0;
3170
+ if (re !== void 0) {
3171
+ if (re <= V) {
3172
+ console.debug("[use-chat] drop duplicate/out-of-order event", { seq: re, lastProcessedSeq: V, type: Ae.type });
3173
3173
  continue;
3174
3174
  }
3175
- V = ye;
3175
+ V = re;
3176
3176
  }
3177
- d(xe);
3177
+ d(Ae);
3178
3178
  }
3179
3179
  }
3180
3180
  j === null && !E.signal.aborted && (j = "complete");
@@ -3192,7 +3192,7 @@ function pr(e = {}) {
3192
3192
  } finally {
3193
3193
  (z = S.releaseLock) == null || z.call(S);
3194
3194
  }
3195
- clearInterval(re);
3195
+ clearInterval(oe);
3196
3196
  }
3197
3197
  } catch (U) {
3198
3198
  if (U.name === "AbortError")
@@ -3360,14 +3360,14 @@ function gr({
3360
3360
  addThreadFromEvent: w
3361
3361
  };
3362
3362
  }
3363
- const Ut = Ce(null);
3363
+ const Ut = Ee(null);
3364
3364
  function mr({
3365
3365
  children: e,
3366
3366
  initialThreadId: t = null,
3367
3367
  onError: n,
3368
3368
  onThreadChange: r
3369
3369
  }) {
3370
- const { chatApi: o } = Me(), {
3370
+ const { chatApi: o } = Ce(), {
3371
3371
  threads: s,
3372
3372
  currentThreadId: a,
3373
3373
  setCurrentThreadId: i,
@@ -3403,10 +3403,10 @@ function mr({
3403
3403
  p
3404
3404
  ]
3405
3405
  );
3406
- return /* @__PURE__ */ ie(Ut.Provider, { value: g, children: e });
3406
+ return /* @__PURE__ */ ce(Ut.Provider, { value: g, children: e });
3407
3407
  }
3408
3408
  function ot(e) {
3409
- const t = je(Ut);
3409
+ const t = Ue(Ut);
3410
3410
  if (!t && !(e != null && e.optional)) throw new Error("useThreads must be used within a ThreadsProvider");
3411
3411
  return t;
3412
3412
  }
@@ -3486,16 +3486,18 @@ function wr(e, t) {
3486
3486
  case "user_message": {
3487
3487
  const { message: r, editingMessageId: o } = t.payload;
3488
3488
  if (o) {
3489
- const s = e.messages.findIndex((a) => a.id === o);
3490
- if (s !== -1)
3489
+ const s = e.messages.findIndex((a) => (a.id || a.messageId || a.id) === o);
3490
+ if (s !== -1) {
3491
+ const a = e.messages[s], i = { ...r, id: o };
3491
3492
  return {
3492
3493
  ...e,
3493
3494
  status: "streaming",
3494
- messages: [...e.messages.slice(0, s), r],
3495
+ messages: [...e.messages.slice(0, s), i],
3495
3496
  assemblingId: null,
3496
- lastCheckpointId: null,
3497
+ lastCheckpointId: a.checkpointId || e.lastCheckpointId || null,
3497
3498
  error: null
3498
3499
  };
3500
+ }
3499
3501
  }
3500
3502
  return {
3501
3503
  ...e,
@@ -3761,7 +3763,7 @@ function xr(e, t, n) {
3761
3763
  let s = n ? [...e.taskMessagesByScope[o] || []] : [...e.messages];
3762
3764
  const a = st(s, r);
3763
3765
  if (a !== -1) {
3764
- const i = s[a], d = ce(t.content), c = (i.toolCalls || []).map(
3766
+ const i = s[a], d = de(t.content), c = (i.toolCalls || []).map(
3765
3767
  (l) => l.id === r ? { ...l, content: typeof d == "string" ? d : l.content } : l
3766
3768
  );
3767
3769
  s[a] = {
@@ -3782,7 +3784,7 @@ function Ar(e, t, n) {
3782
3784
  if (!r) return e;
3783
3785
  const o = typeof t.scope == "string" && t.scope.length > 0 ? t.scope : "task";
3784
3786
  let s = n ? [...e.taskMessagesByScope[o] || []] : [...e.messages];
3785
- const a = st(s, r), i = ce(t.content ?? t.error);
3787
+ const a = st(s, r), i = de(t.content ?? t.error);
3786
3788
  if (a !== -1) {
3787
3789
  const c = s[a], l = (c.toolCalls || []).map(
3788
3790
  (f) => f.id === r ? {
@@ -3826,7 +3828,7 @@ function Tr(e, t) {
3826
3828
  });
3827
3829
  return t.partial_data && s.artifact && (s.artifact = { ...s.artifact, ...t.partial_data }), { ...e, messages: Ft(e.messages, o, s) };
3828
3830
  }
3829
- function Re(e) {
3831
+ function Le(e) {
3830
3832
  if (!(!e || typeof e != "object" || Array.isArray(e)))
3831
3833
  return { ...e };
3832
3834
  }
@@ -3848,14 +3850,14 @@ function Sr(e, t) {
3848
3850
  return null;
3849
3851
  }
3850
3852
  function gt(e) {
3851
- const t = Re(e);
3853
+ const t = Le(e);
3852
3854
  if (t)
3853
- return Re(t.configurable) ?? t;
3855
+ return Le(t.configurable) ?? t;
3854
3856
  }
3855
3857
  async function Mr(e, t) {
3856
3858
  var i, d;
3857
3859
  if (!e) return e ?? null;
3858
- const n = le(e), r = Array.isArray(n.checkpoints) ? n.checkpoints : n.checkpoints ? Object.values(n.checkpoints) : [], o = /* @__PURE__ */ new Set();
3860
+ const n = ue(e), r = Array.isArray(n.checkpoints) ? n.checkpoints : n.checkpoints ? Object.values(n.checkpoints) : [], o = /* @__PURE__ */ new Set();
3859
3861
  for (const c of r) {
3860
3862
  const l = (i = c == null ? void 0 : c.values) == null ? void 0 : i.messages;
3861
3863
  if (Array.isArray(l))
@@ -3874,7 +3876,7 @@ async function Mr(e, t) {
3874
3876
  const s = await Promise.all(
3875
3877
  Array.from(o).map(async (c) => {
3876
3878
  try {
3877
- const l = await t.getFileInfo(c), f = le(l);
3879
+ const l = await t.getFileInfo(c), f = ue(l);
3878
3880
  return [c, f];
3879
3881
  } catch {
3880
3882
  return [c, null];
@@ -3899,7 +3901,7 @@ async function Mr(e, t) {
3899
3901
  return n;
3900
3902
  }
3901
3903
  function mt(e) {
3902
- const r = [...(Array.isArray(e == null ? void 0 : e.checkpoints) ? (e == null ? void 0 : e.checkpoints) ?? [] : e != null && e.checkpoints ? Object.values(e.checkpoints) : []).map((s, a) => Cr(le(s), a)).filter((s) => !!s)].sort((s, a) => yt(a.createdAt) - yt(s.createdAt)), o = r[0] ?? null;
3904
+ const r = [...(Array.isArray(e == null ? void 0 : e.checkpoints) ? (e == null ? void 0 : e.checkpoints) ?? [] : e != null && e.checkpoints ? Object.values(e.checkpoints) : []).map((s, a) => Cr(ue(s), a)).filter((s) => !!s)].sort((s, a) => yt(a.createdAt) - yt(s.createdAt)), o = r[0] ?? null;
3903
3905
  return {
3904
3906
  threadId: (e == null ? void 0 : e.threadId) ?? "",
3905
3907
  latest: o,
@@ -3910,7 +3912,7 @@ function mt(e) {
3910
3912
  function Cr(e, t) {
3911
3913
  var b, h;
3912
3914
  if (!e) return null;
3913
- const n = ((b = e.values) == null ? void 0 : b.activeAgent) ?? null, r = (h = e.values) == null ? void 0 : h.messages, s = (Array.isArray(r) ? r : []).map((T) => Er(T, n)).filter((T) => !!T), a = Re(e.metadata) ?? {}, i = gt(e.config), d = Re(e.parentConfig), c = gt(d), l = Fe(i, ["checkpointId"]), f = Fe(i, ["checkpointNs"]), p = Fe(c, ["checkpointId"]), g = Re(e.config), I = Array.isArray(e.tasks) ? e.tasks.map((T) => ({ ...T })) : e.tasks ?? null, u = Array.isArray(e.next) ? [...e.next] : e.next ?? null, v = typeof e.createdAt == "string" && e.createdAt.trim() ? e.createdAt : new Date(Date.now() - t).toISOString(), x = {
3915
+ const n = ((b = e.values) == null ? void 0 : b.activeAgent) ?? null, r = (h = e.values) == null ? void 0 : h.messages, s = (Array.isArray(r) ? r : []).map((T) => Er(T, n)).filter((T) => !!T), a = Le(e.metadata) ?? {}, i = gt(e.config), d = Le(e.parentConfig), c = gt(d), l = Fe(i, ["checkpointId"]), f = Fe(i, ["checkpointNs"]), p = Fe(c, ["checkpointId"]), g = Le(e.config), I = Array.isArray(e.tasks) ? e.tasks.map((T) => ({ ...T })) : e.tasks ?? null, u = Array.isArray(e.next) ? [...e.next] : e.next ?? null, v = typeof e.createdAt == "string" && e.createdAt.trim() ? e.createdAt : new Date(Date.now() - t).toISOString(), x = {
3914
3916
  ...a,
3915
3917
  step: Sr(a, ["step"]) ?? 0,
3916
3918
  source: Fe(a, ["source"]) ?? null
@@ -4206,14 +4208,14 @@ function $t({
4206
4208
  clearState: F
4207
4209
  };
4208
4210
  }
4209
- const Dt = Ce(null), Kt = Ce(null);
4211
+ const Dt = Ee(null), Kt = Ee(null);
4210
4212
  function Vt(e) {
4211
- const t = je(Dt);
4213
+ const t = Ue(Dt);
4212
4214
  if (!t && !(e != null && e.optional)) throw new Error("useThreadState must be used within a ThreadStateProvider");
4213
4215
  return t;
4214
4216
  }
4215
4217
  function Lr() {
4216
- const e = je(Kt);
4218
+ const e = Ue(Kt);
4217
4219
  if (!e)
4218
4220
  throw new Error("useThreadStateUpdater must be used within a ThreadStateProvider");
4219
4221
  return e;
@@ -4227,7 +4229,7 @@ function jr({
4227
4229
  isStreaming: s,
4228
4230
  onError: a
4229
4231
  }) {
4230
- const { chatApi: i, fileApi: d } = Me(), { currentThreadId: c } = ot(), { state: l, seed: f, onEvent: p, dispatch: g } = kr(), {
4232
+ const { chatApi: i, fileApi: d } = Ce(), { currentThreadId: c } = ot(), { state: l, seed: f, onEvent: p, dispatch: g } = kr(), {
4231
4233
  threadCheckpoints: I,
4232
4234
  threadTimeline: u,
4233
4235
  checkpointIndex: v,
@@ -4366,9 +4368,9 @@ function jr({
4366
4368
  }),
4367
4369
  [F, B]
4368
4370
  );
4369
- return /* @__PURE__ */ ie(Kt.Provider, { value: L, children: /* @__PURE__ */ ie(Dt.Provider, { value: y, children: e }) });
4371
+ return /* @__PURE__ */ ce(Kt.Provider, { value: L, children: /* @__PURE__ */ ce(Dt.Provider, { value: y, children: e }) });
4370
4372
  }
4371
- const qt = Ce(null), Ur = Ce(!1);
4373
+ const qt = Ee(null), Ur = Ee(!1);
4372
4374
  function Br({
4373
4375
  children: e,
4374
4376
  onError: t,
@@ -4381,7 +4383,7 @@ function Br({
4381
4383
  onFinish: d,
4382
4384
  onStreamingChange: c
4383
4385
  }) {
4384
- const { chatApi: l, baseUrl: f } = Me(), p = f, g = fe(t), I = fe(n), u = fe(r), v = fe(o), x = fe(s), M = fe(a), A = fe(i), w = fe(d), b = fe(c), h = Y(
4386
+ const { chatApi: l, baseUrl: f } = Ce(), p = f, g = me(t), I = me(n), u = me(r), v = me(o), x = me(s), M = me(a), A = me(i), w = me(d), b = me(c), h = Y(
4385
4387
  () => ({
4386
4388
  onError: g,
4387
4389
  onAuthError: I,
@@ -4403,7 +4405,7 @@ function Br({
4403
4405
  w
4404
4406
  ]
4405
4407
  );
4406
- return /* @__PURE__ */ ie(Ur.Provider, { value: !0, children: /* @__PURE__ */ ie(
4408
+ return /* @__PURE__ */ ce(Ur.Provider, { value: !0, children: /* @__PURE__ */ ce(
4407
4409
  Fr,
4408
4410
  {
4409
4411
  api: l,
@@ -4476,10 +4478,10 @@ function Fr({
4476
4478
  const W = a.current;
4477
4479
  if (!W)
4478
4480
  throw new Error("No active thread to handle interrupt");
4479
- const K = y ? L ?? !0 : !1, V = B.current, ee = (S == null ? void 0 : S.checkpointId) ?? V.id ?? void 0, re = (S == null ? void 0 : S.checkpointNs) ?? V.namespace ?? void 0, de = {
4481
+ const K = y ? L ?? !0 : !1, V = B.current, ee = (S == null ? void 0 : S.checkpointId) ?? V.id ?? void 0, oe = (S == null ? void 0 : S.checkpointNs) ?? V.namespace ?? void 0, fe = {
4480
4482
  threadId: W,
4481
4483
  checkpointId: ee,
4482
- checkpointNs: re,
4484
+ checkpointNs: oe,
4483
4485
  command: { kind: "resume", value: K },
4484
4486
  payload: {
4485
4487
  ...(S == null ? void 0 : S.payload) ?? {},
@@ -4487,7 +4489,7 @@ function Fr({
4487
4489
  },
4488
4490
  edit: !1
4489
4491
  };
4490
- P(de);
4492
+ P(fe);
4491
4493
  },
4492
4494
  [a, P]
4493
4495
  );
@@ -4504,49 +4506,53 @@ function Fr({
4504
4506
  async (y, L) => {
4505
4507
  E && m();
4506
4508
  let S = a.current;
4507
- const W = (y == null ? void 0 : y.text) ?? "", K = (y == null ? void 0 : y.attachments) ?? [], V = (W == null ? void 0 : W.trim()) ?? "", ee = { ...(y == null ? void 0 : y.payload) ?? {} }, re = Array.isArray(ee.messages) ? ee.messages : void 0;
4508
- re && delete ee.messages;
4509
- const de = !!(re && re.length), H = !!(y.contentParts && y.contentParts.length > 0);
4510
- if (!S && !V && K.length === 0 && !de && !H) return;
4509
+ const W = (y == null ? void 0 : y.text) ?? "", K = (y == null ? void 0 : y.attachments) ?? [], V = (W == null ? void 0 : W.trim()) ?? "", ee = { ...(y == null ? void 0 : y.payload) ?? {} }, oe = Array.isArray(ee.messages) ? ee.messages : void 0;
4510
+ oe && delete ee.messages;
4511
+ const fe = !!(oe && oe.length), H = !!(y.contentParts && y.contentParts.length > 0);
4512
+ if (!S && !V && K.length === 0 && !fe && !H) return;
4511
4513
  if (!S) {
4512
- const ye = Math.floor(Date.now() / 1e3).toString(16).padStart(8, "0"), he = Array.from({ length: 4 }, () => Math.floor(Math.random() * 4294967295).toString(16).padStart(8, "0")).join("").slice(0, 16);
4513
- S = `${ye}${he}`, g(S), s(S);
4514
+ const re = Math.floor(Date.now() / 1e3).toString(16).padStart(8, "0"), le = Array.from({ length: 4 }, () => Math.floor(Math.random() * 4294967295).toString(16).padStart(8, "0")).join("").slice(0, 16);
4515
+ S = `${re}${le}`, g(S), s(S);
4514
4516
  }
4515
4517
  let te = null;
4516
4518
  y.contentParts && y.contentParts.length > 0 && (te = y.contentParts);
4517
4519
  const { contentParts: ke, filesInfo: Q } = te ? { contentParts: te, filesInfo: [] } : await or(K), Z = [...ke];
4518
4520
  V && Z.push({ type: "text", text: W });
4519
- const ge = y == null ? void 0 : y.originalMessageId, Ue = !!(y != null && y.edit && ge), Be = !de && Z.length > 0;
4520
- let me = null;
4521
+ const pe = y == null ? void 0 : y.originalMessageId, _e = !!(y != null && y.edit && pe), Be = !fe && Z.length > 0;
4522
+ let he = null;
4521
4523
  if (Be) {
4522
- const ye = Ue ? ge : `msg-${Date.now()}`;
4523
- me = jt({
4524
- id: ye,
4524
+ const re = _e ? pe : `msg-${Date.now()}`;
4525
+ he = jt({
4526
+ id: re,
4525
4527
  role: "user",
4526
4528
  content: Z,
4527
4529
  createdAt: (/* @__PURE__ */ new Date()).toISOString()
4528
- }, "user"), Ue ? v(me, { editingMessageId: ge }) : v(me);
4530
+ }, "user"), _e ? v(he, { editingMessageId: pe }) : v(he);
4529
4531
  }
4530
- const ve = de ? re : me ? [me] : [], oe = {
4532
+ const xe = fe ? oe : he ? [he] : [], se = {
4531
4533
  ...ee,
4532
4534
  ...Q.length ? { files_info: Q } : {}
4533
4535
  };
4534
- ve.length ? oe.messages = ve : "messages" in oe || (oe.messages = []);
4535
- const Pe = (y == null ? void 0 : y.checkpointId) ?? l.id ?? void 0;
4536
- if (y != null && y.edit && !Pe)
4536
+ xe.length ? se.messages = xe : "messages" in se || (se.messages = []);
4537
+ let Ie = (y == null ? void 0 : y.checkpointId) ?? l.id ?? void 0;
4538
+ if (_e && !Ie) {
4539
+ const re = M.find((le) => le.id === pe);
4540
+ re != null && re.checkpointId && (Ie = re.checkpointId);
4541
+ }
4542
+ if (y != null && y.edit && !Ie)
4537
4543
  throw new Error(
4538
- `[streaming] Missing checkpointId for edit submit. threadId=${S ?? "null"}`
4544
+ `[streaming] Missing checkpointId for edit submit. threadId=${S ?? "null"}. Ensure you are editing a message that has been saved.`
4539
4545
  );
4540
- const xe = {
4546
+ const Ae = {
4541
4547
  threadId: S,
4542
4548
  content: y == null ? void 0 : y.text,
4543
- payload: oe,
4549
+ payload: se,
4544
4550
  config: y == null ? void 0 : y.config,
4545
- checkpointId: Pe,
4551
+ checkpointId: Ie,
4546
4552
  command: y == null ? void 0 : y.command,
4547
4553
  edit: y == null ? void 0 : y.edit
4548
4554
  };
4549
- return P(xe, L);
4555
+ return P(Ae, L);
4550
4556
  },
4551
4557
  [l, a, E, g, v, P, m, f]
4552
4558
  ), R = Y(() => M, [M]), q = Y(
@@ -4567,9 +4573,9 @@ function Fr({
4567
4573
  }),
4568
4574
  [E, j, U, m, c, C, R]
4569
4575
  );
4570
- return /* @__PURE__ */ ie(qt.Provider, { value: q, children: e });
4576
+ return /* @__PURE__ */ ce(qt.Provider, { value: q, children: e });
4571
4577
  }
4572
- function fe(e) {
4578
+ function me(e) {
4573
4579
  const t = G(e);
4574
4580
  return X(() => {
4575
4581
  t.current = e;
@@ -4580,7 +4586,7 @@ function fe(e) {
4580
4586
  }, []);
4581
4587
  }
4582
4588
  function Or(e) {
4583
- return je(qt);
4589
+ return Ue(qt);
4584
4590
  }
4585
4591
  function bt(e) {
4586
4592
  return typeof e == "object" && e !== null;
@@ -4628,7 +4634,7 @@ function po({
4628
4634
  onMetadataEvent: g,
4629
4635
  onFinish: I
4630
4636
  }) {
4631
- const u = Dr(t), [v, x] = $(!1), M = pe(i), A = pe(d), w = pe(a), b = pe(c), h = pe(l), T = pe(f), m = pe(p), k = pe(g), E = pe(I), j = Y(
4637
+ const u = Dr(t), [v, x] = $(!1), M = ye(i), A = ye(d), w = ye(a), b = ye(c), h = ye(l), T = ye(f), m = ye(p), k = ye(g), E = ye(I), j = Y(
4632
4638
  () => ({
4633
4639
  onError: M,
4634
4640
  onAuthError: A,
@@ -4650,13 +4656,13 @@ function po({
4650
4656
  E
4651
4657
  ]
4652
4658
  );
4653
- return /* @__PURE__ */ ie(nr, { apiConfig: u, children: /* @__PURE__ */ ie(
4659
+ return /* @__PURE__ */ ce(nr, { apiConfig: u, children: /* @__PURE__ */ ce(
4654
4660
  mr,
4655
4661
  {
4656
4662
  initialThreadId: n,
4657
4663
  onError: M,
4658
4664
  onThreadChange: b,
4659
- children: /* @__PURE__ */ ie(
4665
+ children: /* @__PURE__ */ ce(
4660
4666
  jr,
4661
4667
  {
4662
4668
  initialThreadId: n,
@@ -4665,7 +4671,7 @@ function po({
4665
4671
  autoLoadInitial: s,
4666
4672
  onError: M,
4667
4673
  isStreaming: v,
4668
- children: /* @__PURE__ */ ie(
4674
+ children: /* @__PURE__ */ ce(
4669
4675
  Br,
4670
4676
  {
4671
4677
  ...j,
@@ -4678,7 +4684,7 @@ function po({
4678
4684
  }
4679
4685
  ) });
4680
4686
  }
4681
- function pe(e) {
4687
+ function ye(e) {
4682
4688
  const t = G(e);
4683
4689
  return t.current = e, _((...n) => {
4684
4690
  const r = t.current;
@@ -4695,7 +4701,7 @@ const Je = /* @__PURE__ */ new Map(), wt = (e) => {
4695
4701
  if (Je.has(e))
4696
4702
  return;
4697
4703
  const t = setTimeout(() => {
4698
- Je.delete(e), Le({
4704
+ Je.delete(e), je({
4699
4705
  type: "REMOVE_TOAST",
4700
4706
  toastId: e
4701
4707
  });
@@ -4740,17 +4746,17 @@ const Je = /* @__PURE__ */ new Map(), wt = (e) => {
4740
4746
  }
4741
4747
  }, $e = [];
4742
4748
  let De = { toasts: [] };
4743
- function Le(e) {
4749
+ function je(e) {
4744
4750
  De = Gr(De, e), $e.forEach((t) => {
4745
4751
  t(De);
4746
4752
  });
4747
4753
  }
4748
4754
  function Wr({ ...e }) {
4749
- const t = Hr(), n = (o) => Le({
4755
+ const t = Hr(), n = (o) => je({
4750
4756
  type: "UPDATE_TOAST",
4751
4757
  toast: { ...o, id: t }
4752
- }), r = () => Le({ type: "DISMISS_TOAST", toastId: t });
4753
- return Le({
4758
+ }), r = () => je({ type: "DISMISS_TOAST", toastId: t });
4759
+ return je({
4754
4760
  type: "ADD_TOAST",
4755
4761
  toast: {
4756
4762
  ...e,
@@ -4774,11 +4780,11 @@ function Jr() {
4774
4780
  }), [e]), {
4775
4781
  ...e,
4776
4782
  toast: Wr,
4777
- dismiss: (n) => Le({ type: "DISMISS_TOAST", toastId: n })
4783
+ dismiss: (n) => je({ type: "DISMISS_TOAST", toastId: n })
4778
4784
  };
4779
4785
  }
4780
4786
  function go(e) {
4781
- const { allowFileOpen: t, currentThreadId: n } = e, { fileApi: r } = Me(), { toast: o } = Jr(), [s, a] = $({}), [i, d] = $(null), [c, l] = $(!1), f = _(() => {
4787
+ const { allowFileOpen: t, currentThreadId: n } = e, { fileApi: r } = Ce(), { toast: o } = Jr(), [s, a] = $({}), [i, d] = $(null), [c, l] = $(!1), f = _(() => {
4782
4788
  l(!1), d(null);
4783
4789
  }, []), p = _((I) => {
4784
4790
  d(I), l(!0);
@@ -4908,7 +4914,7 @@ function yo(e) {
4908
4914
  const s = Qr(r) ?? r[r.length - 1];
4909
4915
  if (!s)
4910
4916
  return null;
4911
- const a = s.output ?? s.payload ?? s, i = ce(a);
4917
+ const a = s.output ?? s.payload ?? s, i = de(a);
4912
4918
  return i && typeof i == "object" && !Array.isArray(i) ? {
4913
4919
  kind: "dict",
4914
4920
  value: i,
@@ -4937,8 +4943,8 @@ function Yr(e) {
4937
4943
  isLoadingHistory: v,
4938
4944
  loadThread: x
4939
4945
  } = $t({
4940
- api: Me().chatApi,
4941
- fileApi: Me().fileApi,
4946
+ api: Ce().chatApi,
4947
+ fileApi: Ce().fileApi,
4942
4948
  seed: p,
4943
4949
  currentThreadId: c ? t : null,
4944
4950
  initialThreadId: c ? t : null,
@@ -5062,7 +5068,7 @@ function ho({
5062
5068
  } catch (Z) {
5063
5069
  throw console.error("Failed to upload file", Q.name, Z), new Error(`Failed to upload ${Q.name}`);
5064
5070
  }
5065
- }), de = (await Promise.all(ee)).map(({ record: Q }) => {
5071
+ }), fe = (await Promise.all(ee)).map(({ record: Q }) => {
5066
5072
  const Z = Q.mime_type;
5067
5073
  return Z.startsWith("image/") ? {
5068
5074
  type: "image_url",
@@ -5074,7 +5080,7 @@ function ho({
5074
5080
  name: Q.filename,
5075
5081
  size: Q.size
5076
5082
  };
5077
- }), H = [...K, ...de], te = u ? (A == null ? void 0 : A.checkpointId) ?? n ?? void 0 : n ?? void 0, ke = u ? (A == null ? void 0 : A.checkpointNs) ?? r ?? void 0 : r ?? void 0;
5083
+ }), H = [...K, ...fe], te = u ? (A == null ? void 0 : A.checkpointId) ?? n ?? void 0 : n ?? void 0, ke = u ? (A == null ? void 0 : A.checkpointNs) ?? r ?? void 0 : r ?? void 0;
5078
5084
  await e(
5079
5085
  {
5080
5086
  text: V || "",
@@ -5091,8 +5097,8 @@ function ho({
5091
5097
  f ? { onEvent: f } : void 0
5092
5098
  ), o == null || o(V), c(!0), g && (p && localStorage.removeItem(Ge(p)), O("")), h(null), v(null), M(""), w(null);
5093
5099
  } catch (ee) {
5094
- const re = (ee == null ? void 0 : ee.message) || "Failed to send message";
5095
- h(re), a == null || a(re), c(!1);
5100
+ const oe = (ee == null ? void 0 : ee.message) || "Failed to send message";
5101
+ h(oe), a == null || a(oe), c(!1);
5096
5102
  }
5097
5103
  }, B = (y, L) => {
5098
5104
  L || P(y, { files: [] });
@@ -5228,9 +5234,9 @@ export {
5228
5234
  Et as pretty,
5229
5235
  Mr as refreshHistoryFileUrls,
5230
5236
  Ke as sanitizeForDisplay,
5231
- Ie as setChatToken,
5237
+ ve as setChatToken,
5232
5238
  Fn as stringifyTool,
5233
- Me as useApi,
5239
+ Ce as useApi,
5234
5240
  ho as useChatController,
5235
5241
  Yr as useChatSession,
5236
5242
  go as useFilePreview,