@mast-ai/react-ui 0.1.2 → 0.3.0

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,185 +1,197 @@
1
- import { useState as R, useRef as C, useEffect as Q, useCallback as _, createContext as nt, useMemo as J, useContext as st, lazy as pt, Fragment as rt, Suspense as ft, useId as gt } from "react";
2
- import { jsx as e, jsxs as v } from "react/jsx-runtime";
3
- import { AgentRunner as bt } from "@mast-ai/core";
4
- import { useVirtualizer as vt } from "@tanstack/react-virtual";
5
- function tt(t, n, r) {
1
+ import { useState as $, useEffect as z, useRef as R, useCallback as D, createContext as nt, useMemo as et, useContext as st, lazy as It, Fragment as mt, Suspense as Nt, useId as wt } from "react";
2
+ import { jsx as e, jsxs as w } from "react/jsx-runtime";
3
+ import { AgentRunner as Et } from "@mast-ai/core";
4
+ import { useVirtualizer as At } from "@tanstack/react-virtual";
5
+ function ct(t, n, r) {
6
6
  return { id: crypto.randomUUID(), role: t, text: n, toolEvents: [], isStreaming: r };
7
7
  }
8
8
  function F(t, n, r) {
9
9
  return t.map((s) => s.id === n ? r(s) : s);
10
10
  }
11
- function W(t, n, r, s) {
11
+ function V(t, n, r, s) {
12
12
  return F(t, n, (a) => {
13
- let i = !1;
14
- const l = a.toolEvents.map((c) => !i && c.name === r && c.isStreaming ? (i = !0, s(c)) : c);
15
- return { ...a, toolEvents: l };
13
+ let l = !1;
14
+ const i = a.toolEvents.map((o) => !l && o.name === r && o.isStreaming ? (l = !0, s(o)) : o);
15
+ return { ...a, toolEvents: i };
16
16
  });
17
17
  }
18
- function kt(t, n, r, s, a) {
19
- return W(t, n, r, (i) => {
20
- let l = !1;
21
- const c = (i.nestedToolEvents ?? []).map((u) => !l && u.name === s && u.isStreaming ? (l = !0, a(u)) : u);
22
- return { ...i, nestedToolEvents: c };
18
+ function Ct(t, n, r, s, a) {
19
+ return V(t, n, r, (l) => {
20
+ let i = !1;
21
+ const o = (l.nestedToolEvents ?? []).map((m) => !i && m.name === s && m.isStreaming ? (i = !0, a(m)) : m);
22
+ return { ...l, nestedToolEvents: o };
23
23
  });
24
24
  }
25
- function xt(t, n) {
26
- const [r, s] = R(() => (n == null ? void 0 : n.initialEntries) ?? []), [a, i] = R(() => [...t.history]), [l, c] = R(!1), [u, m] = R(0), k = C(null), g = C(n == null ? void 0 : n.onTurnComplete);
27
- g.current = n == null ? void 0 : n.onTurnComplete, Q(() => {
28
- var b;
29
- u !== 0 && ((b = g.current) == null || b.call(g, r, a));
30
- }, [u]);
31
- const T = (b) => {
32
- for (let A = b.length - 1; A >= 0; A--) {
33
- const N = b[A];
25
+ function Rt(t, n) {
26
+ const [r, s] = $(() => (n == null ? void 0 : n.initialEntries) ?? []), [a, l] = $(
27
+ () => t ? [...t.history] : []
28
+ ), [i, o] = $(!1);
29
+ z(() => {
30
+ t && l([...t.history]);
31
+ }, [t]);
32
+ const [m, u] = $(0), d = R(null), b = R(n == null ? void 0 : n.onTurnComplete);
33
+ b.current = n == null ? void 0 : n.onTurnComplete, z(() => {
34
+ var g;
35
+ m !== 0 && ((g = b.current) == null || g.call(b, r, a));
36
+ }, [m]);
37
+ const S = (g) => {
38
+ for (let T = g.length - 1; T >= 0; T--) {
39
+ const N = g[T];
34
40
  if (N.role === "assistant" && N.isStreaming) return N.id;
35
41
  }
36
- }, M = _((b, A) => {
42
+ }, P = D((g, T) => {
37
43
  s((N) => {
38
- const E = T(N);
39
- return E === void 0 ? N : W(N, E, b, (I) => ({
40
- ...I,
41
- awaitingApproval: A
44
+ const A = S(N);
45
+ return A === void 0 ? N : V(N, A, g, (x) => ({
46
+ ...x,
47
+ awaitingApproval: T
42
48
  }));
43
49
  });
44
- }, []), P = _((b, A) => {
50
+ }, []), _ = D((g, T) => {
45
51
  s((N) => {
46
- const E = T(N);
47
- return E === void 0 ? N : W(N, E, b, (I) => ({ ...I, status: A }));
52
+ const A = S(N);
53
+ return A === void 0 ? N : V(N, A, g, (x) => ({ ...x, status: T }));
48
54
  });
49
- }, []), S = _(
50
- (b, A) => {
51
- if (l) return;
52
- const N = tt("user", A ?? b, !1), E = tt("assistant", "", !0), I = E.id;
53
- s((x) => [...x, N, E]), c(!0);
55
+ }, []), M = D(
56
+ (g, T) => {
57
+ if (i) return;
58
+ if (t === null) {
59
+ console.warn(
60
+ "useAgent().sendMessage was called while no AgentRunner is configured. Pass a non-null `runner` to <AgentProvider> to enable the agent."
61
+ );
62
+ return;
63
+ }
64
+ const N = ct("user", T ?? g, !1), A = ct("assistant", "", !0), x = A.id;
65
+ s((E) => [...E, N, A]), o(!0);
54
66
  const h = new AbortController();
55
- k.current = h, (async () => {
67
+ d.current = h, (async () => {
56
68
  try {
57
- const x = t.runStream(
58
- b,
69
+ const E = t.runStream(
70
+ g,
59
71
  h.signal,
60
- (d, f) => {
61
- if (f.type === "thinking")
72
+ (f, k) => {
73
+ if (k.type === "thinking")
62
74
  s(
63
- (o) => W(o, I, d, (y) => ({
64
- ...y,
65
- subThinking: (y.subThinking ?? "") + f.delta
75
+ (v) => V(v, x, f, (C) => ({
76
+ ...C,
77
+ subThinking: (C.subThinking ?? "") + k.delta
66
78
  }))
67
79
  );
68
- else if (f.type === "text_delta")
80
+ else if (k.type === "text_delta")
69
81
  s(
70
- (o) => W(o, I, d, (y) => ({
71
- ...y,
72
- subText: (y.subText ?? "") + f.delta
82
+ (v) => V(v, x, f, (C) => ({
83
+ ...C,
84
+ subText: (C.subText ?? "") + k.delta
73
85
  }))
74
86
  );
75
- else if (f.type === "tool_call_started") {
76
- const o = {
87
+ else if (k.type === "tool_call_started") {
88
+ const v = {
77
89
  id: crypto.randomUUID(),
78
90
  type: "tool_call_started",
79
- name: f.name,
80
- args: f.args,
91
+ name: k.name,
92
+ args: k.args,
81
93
  isStreaming: !0
82
94
  };
83
95
  s(
84
- (y) => W(y, I, d, (V) => ({
85
- ...V,
86
- nestedToolEvents: [...V.nestedToolEvents ?? [], o]
96
+ (C) => V(C, x, f, (y) => ({
97
+ ...y,
98
+ nestedToolEvents: [...y.nestedToolEvents ?? [], v]
87
99
  }))
88
100
  );
89
- } else if (f.type === "tool_call_completed") {
90
- const o = f;
101
+ } else if (k.type === "tool_call_completed") {
102
+ const v = k;
91
103
  s(
92
- (y) => kt(y, I, d, o.name, (V) => ({
93
- ...V,
104
+ (C) => Ct(C, x, f, v.name, (y) => ({
105
+ ...y,
94
106
  type: "tool_call_completed",
95
- result: o.result,
107
+ result: v.result,
96
108
  isStreaming: !1,
97
- status: o.error ? "error" : "success"
109
+ status: v.error ? "error" : "success"
98
110
  }))
99
111
  );
100
112
  }
101
113
  }
102
114
  );
103
- for await (const d of x)
104
- if (d.type === "text_delta")
115
+ for await (const f of E)
116
+ if (f.type === "text_delta")
105
117
  s(
106
- (f) => F(f, I, (o) => ({
107
- ...o,
108
- text: o.text + d.delta
118
+ (k) => F(k, x, (v) => ({
119
+ ...v,
120
+ text: v.text + f.delta
109
121
  }))
110
122
  );
111
- else if (d.type === "thinking")
123
+ else if (f.type === "thinking")
112
124
  s(
113
- (f) => F(f, I, (o) => ({
114
- ...o,
115
- thinking: (o.thinking ?? "") + d.delta
125
+ (k) => F(k, x, (v) => ({
126
+ ...v,
127
+ thinking: (v.thinking ?? "") + f.delta
116
128
  }))
117
129
  );
118
- else if (d.type === "tool_call_started") {
119
- const f = {
130
+ else if (f.type === "tool_call_started") {
131
+ const k = {
120
132
  id: crypto.randomUUID(),
121
133
  type: "tool_call_started",
122
- name: d.name,
123
- args: d.args,
134
+ name: f.name,
135
+ args: f.args,
124
136
  isStreaming: !0
125
137
  };
126
138
  s(
127
- (o) => F(o, I, (y) => ({
128
- ...y,
129
- toolEvents: [...y.toolEvents, f]
139
+ (v) => F(v, x, (C) => ({
140
+ ...C,
141
+ toolEvents: [...C.toolEvents, k]
130
142
  }))
131
143
  );
132
- } else if (d.type === "tool_call_completed")
144
+ } else if (f.type === "tool_call_completed")
133
145
  s(
134
- (f) => W(f, I, d.name, (o) => ({
135
- ...o,
146
+ (k) => V(k, x, f.name, (v) => ({
147
+ ...v,
136
148
  type: "tool_call_completed",
137
- result: d.result,
149
+ result: f.result,
138
150
  isStreaming: !1,
139
151
  // Preserve a status set by the proxy (e.g. 'cancelled'); only
140
152
  // fall back to error/success based on the runner's flag.
141
- status: o.status ?? (d.error ? "error" : "success")
153
+ status: v.status ?? (f.error ? "error" : "success")
142
154
  }))
143
155
  );
144
- else if (d.type === "done") {
145
- const f = d.history;
156
+ else if (f.type === "done") {
157
+ const k = f.history;
146
158
  s(
147
- (o) => F(o, I, (y) => ({
148
- ...y,
149
- text: d.output,
159
+ (v) => F(v, x, (C) => ({
160
+ ...C,
161
+ text: f.output,
150
162
  isStreaming: !1
151
163
  }))
152
- ), i(f), m((o) => o + 1);
164
+ ), l(k), u((v) => v + 1);
153
165
  }
154
166
  } catch {
155
167
  s(
156
- (x) => F(x, I, (d) => ({ ...d, isStreaming: !1 }))
168
+ (E) => F(E, x, (f) => ({ ...f, isStreaming: !1 }))
157
169
  );
158
170
  } finally {
159
- c(!1);
171
+ o(!1);
160
172
  }
161
173
  })();
162
174
  },
163
- [t, l]
164
- ), w = _(() => {
165
- var b;
166
- (b = k.current) == null || b.abort();
167
- }, []), B = _(() => {
168
- var b;
169
- (b = k.current) == null || b.abort(), k.current = null, s([]), i([]), c(!1);
175
+ [t, i]
176
+ ), c = D(() => {
177
+ var g;
178
+ (g = d.current) == null || g.abort();
179
+ }, []), I = D(() => {
180
+ var g;
181
+ (g = d.current) == null || g.abort(), d.current = null, s([]), l([]), o(!1);
170
182
  }, []);
171
183
  return {
172
184
  entries: r,
173
185
  history: a,
174
- isRunning: l,
175
- sendMessage: S,
176
- cancel: w,
177
- reset: B,
178
- setToolAwaitingApproval: M,
179
- setToolStatus: P
186
+ isRunning: i,
187
+ sendMessage: M,
188
+ cancel: c,
189
+ reset: I,
190
+ setToolAwaitingApproval: P,
191
+ setToolStatus: _
180
192
  };
181
193
  }
182
- const K = {
194
+ const B = {
183
195
  width: 16,
184
196
  height: 16,
185
197
  viewBox: "0 0 24 24",
@@ -191,78 +203,78 @@ const K = {
191
203
  "aria-hidden": !0,
192
204
  focusable: !1
193
205
  };
194
- function yt() {
195
- return /* @__PURE__ */ v("svg", { ...K, children: [
206
+ function Pt() {
207
+ return /* @__PURE__ */ w("svg", { ...B, children: [
196
208
  /* @__PURE__ */ e("path", { d: "M9 3a3 3 0 0 0-3 3v.5a3 3 0 0 0-2 5.2A3 3 0 0 0 6 17v.5a3 3 0 0 0 6 0V3a3 3 0 0 0-3 0Z" }),
197
209
  /* @__PURE__ */ e("path", { d: "M15 3a3 3 0 0 1 3 3v.5a3 3 0 0 1 2 5.2A3 3 0 0 1 18 17v.5a3 3 0 0 1-6 0V3a3 3 0 0 1 3 0Z" })
198
210
  ] });
199
211
  }
200
- function It() {
201
- return /* @__PURE__ */ e("svg", { ...K, children: /* @__PURE__ */ e("path", { d: "M14.7 6.3a4 4 0 0 0-5.4 5.4L3 18l3 3 6.3-6.3a4 4 0 0 0 5.4-5.4l-2.6 2.6-2.4-2.4 2.6-2.6Z" }) });
212
+ function _t() {
213
+ return /* @__PURE__ */ e("svg", { ...B, children: /* @__PURE__ */ e("path", { d: "M14.7 6.3a4 4 0 0 0-5.4 5.4L3 18l3 3 6.3-6.3a4 4 0 0 0 5.4-5.4l-2.6 2.6-2.4-2.4 2.6-2.6Z" }) });
202
214
  }
203
- function St() {
204
- return /* @__PURE__ */ v("svg", { ...K, children: [
215
+ function Mt() {
216
+ return /* @__PURE__ */ w("svg", { ...B, children: [
205
217
  /* @__PURE__ */ e("circle", { cx: "12", cy: "12", r: "9" }),
206
218
  /* @__PURE__ */ e("path", { d: "m8 12 3 3 5-6" })
207
219
  ] });
208
220
  }
209
- function Nt() {
210
- return /* @__PURE__ */ v("svg", { ...K, children: [
221
+ function Dt() {
222
+ return /* @__PURE__ */ w("svg", { ...B, children: [
211
223
  /* @__PURE__ */ e("circle", { cx: "12", cy: "12", r: "9" }),
212
224
  /* @__PURE__ */ e("path", { d: "m9 9 6 6" }),
213
225
  /* @__PURE__ */ e("path", { d: "m15 9-6 6" })
214
226
  ] });
215
227
  }
216
- function Tt() {
217
- return /* @__PURE__ */ v("svg", { ...K, children: [
228
+ function Lt() {
229
+ return /* @__PURE__ */ w("svg", { ...B, children: [
218
230
  /* @__PURE__ */ e("circle", { cx: "12", cy: "12", r: "9" }),
219
231
  /* @__PURE__ */ e("path", { d: "M5.5 5.5 18.5 18.5" })
220
232
  ] });
221
233
  }
222
- function wt() {
223
- return /* @__PURE__ */ e("svg", { ...K, className: "mast-spin", children: /* @__PURE__ */ e("path", { d: "M12 3a9 9 0 1 0 9 9" }) });
224
- }
225
- function At() {
226
- return /* @__PURE__ */ e("svg", { ...K, fill: "currentColor", stroke: "none", children: /* @__PURE__ */ e("path", { d: "M3 3 21 12 3 21l3-9-3-9Z" }) });
227
- }
228
- function Et() {
229
- return /* @__PURE__ */ e("svg", { ...K, fill: "currentColor", stroke: "none", children: /* @__PURE__ */ e("rect", { x: "5", y: "5", width: "14", height: "14", rx: "1" }) });
230
- }
231
- const L = {
232
- brain: /* @__PURE__ */ e(yt, {}),
233
- wrench: /* @__PURE__ */ e(It, {}),
234
- check: /* @__PURE__ */ e(St, {}),
235
- error: /* @__PURE__ */ e(Nt, {}),
236
- cancelled: /* @__PURE__ */ e(Tt, {}),
237
- loader: /* @__PURE__ */ e(wt, {}),
238
- send: /* @__PURE__ */ e(At, {}),
239
- stop: /* @__PURE__ */ e(Et, {})
240
- }, at = nt(L);
241
- function Ct({ icons: t, children: n }) {
242
- const r = J(() => t ? {
243
- brain: t.brain ?? L.brain,
244
- wrench: t.wrench ?? L.wrench,
245
- check: t.check ?? L.check,
246
- error: t.error ?? L.error,
247
- cancelled: t.cancelled ?? L.cancelled,
248
- loader: t.loader ?? L.loader,
249
- send: t.send ?? L.send,
250
- stop: t.stop ?? L.stop
251
- } : L, [t]);
252
- return /* @__PURE__ */ e(at.Provider, { value: r, children: n });
253
- }
254
- function q() {
255
- return st(at);
256
- }
257
- const Mt = Symbol.for("@mast-ai/react-ui.INLINE_APPROVAL"), Pt = "User cancelled the tool call.";
258
- function _t(t, n) {
234
+ function $t() {
235
+ return /* @__PURE__ */ e("svg", { ...B, className: "mast-spin", children: /* @__PURE__ */ e("path", { d: "M12 3a9 9 0 1 0 9 9" }) });
236
+ }
237
+ function jt() {
238
+ return /* @__PURE__ */ e("svg", { ...B, fill: "currentColor", stroke: "none", children: /* @__PURE__ */ e("path", { d: "M3 3 21 12 3 21l3-9-3-9Z" }) });
239
+ }
240
+ function Ht() {
241
+ return /* @__PURE__ */ e("svg", { ...B, fill: "currentColor", stroke: "none", children: /* @__PURE__ */ e("rect", { x: "5", y: "5", width: "14", height: "14", rx: "1" }) });
242
+ }
243
+ const j = {
244
+ brain: /* @__PURE__ */ e(Pt, {}),
245
+ wrench: /* @__PURE__ */ e(_t, {}),
246
+ check: /* @__PURE__ */ e(Mt, {}),
247
+ error: /* @__PURE__ */ e(Dt, {}),
248
+ cancelled: /* @__PURE__ */ e(Lt, {}),
249
+ loader: /* @__PURE__ */ e($t, {}),
250
+ send: /* @__PURE__ */ e(jt, {}),
251
+ stop: /* @__PURE__ */ e(Ht, {})
252
+ }, ht = nt(j);
253
+ function Bt({ icons: t, children: n }) {
254
+ const r = et(() => t ? {
255
+ brain: t.brain ?? j.brain,
256
+ wrench: t.wrench ?? j.wrench,
257
+ check: t.check ?? j.check,
258
+ error: t.error ?? j.error,
259
+ cancelled: t.cancelled ?? j.cancelled,
260
+ loader: t.loader ?? j.loader,
261
+ send: t.send ?? j.send,
262
+ stop: t.stop ?? j.stop
263
+ } : j, [t]);
264
+ return /* @__PURE__ */ e(ht.Provider, { value: r, children: n });
265
+ }
266
+ function J() {
267
+ return st(ht);
268
+ }
269
+ const pt = Symbol.for("@mast-ai/react-ui.INLINE_APPROVAL"), Ut = "User cancelled the tool call.";
270
+ function Kt(t, n) {
259
271
  let r = !1, s = !1;
260
272
  if (n)
261
273
  for (const a of n)
262
274
  a.startsWith("!") ? a.slice(1) === t.name && (s = !0) : a === t.name && (r = !0);
263
275
  return (t.requiresApproval === !0 || r) && !s;
264
276
  }
265
- class Rt {
277
+ class Ot {
266
278
  constructor(n, r, s) {
267
279
  this.inner = n, this.onApprovalRequired = r, this.hooks = s;
268
280
  }
@@ -270,140 +282,153 @@ class Rt {
270
282
  return this.inner.definition();
271
283
  }
272
284
  async call(n, r) {
273
- var i, l, c, u, m, k;
285
+ var l, i, o, m, u, d;
274
286
  const s = this.inner.definition();
275
- (l = (i = this.hooks).notifyAwaiting) == null || l.call(i, s.name, !0);
287
+ (i = (l = this.hooks).notifyAwaiting) == null || i.call(l, s.name, !0);
276
288
  let a;
277
289
  try {
278
- const g = await this.onApprovalRequired({
290
+ const b = await this.onApprovalRequired({
279
291
  id: "",
280
292
  type: "tool_call_started",
281
293
  name: s.name,
282
294
  args: n,
283
295
  isStreaming: !0
284
296
  });
285
- g === Mt ? this.hooks.enqueueInline ? a = await this.hooks.enqueueInline(s.name, n) : a = !0 : a = g;
297
+ b === pt ? this.hooks.enqueueInline ? a = await this.hooks.enqueueInline(s.name, n) : a = !0 : a = b;
286
298
  } finally {
287
- (u = (c = this.hooks).notifyAwaiting) == null || u.call(c, s.name, !1);
299
+ (m = (o = this.hooks).notifyAwaiting) == null || m.call(o, s.name, !1);
288
300
  }
289
- return a === !1 ? ((k = (m = this.hooks).setStatus) == null || k.call(m, s.name, "cancelled"), Pt) : typeof a == "string" ? a : this.inner.call(n, r);
301
+ return a === !1 ? ((d = (u = this.hooks).setStatus) == null || d.call(u, s.name, "cancelled"), Ut) : typeof a == "string" ? a : this.inner.call(n, r);
290
302
  }
291
303
  }
292
- function Dt(t, n, r, s = {}) {
304
+ function Vt(t, n, r, s = {}) {
293
305
  return {
294
306
  getTools: () => t.getTools(),
295
307
  getTool: (a) => {
296
- const i = t.getTool(a);
297
- if (!i) return;
298
- const l = i.definition();
299
- if (!_t(l, r())) return i;
300
- const c = n();
301
- return c ? new Rt(i, c, s) : i;
308
+ const l = t.getTool(a);
309
+ if (!l) return;
310
+ const i = l.definition();
311
+ if (!Kt(i, r())) return l;
312
+ const o = n();
313
+ return o ? new Ot(l, o, s) : l;
302
314
  }
303
315
  };
304
316
  }
305
- const it = nt(null);
306
- function ae({
317
+ const ft = nt(null), Wt = async () => pt;
318
+ function ge({
307
319
  runner: t,
308
320
  agent: n,
309
321
  children: r,
310
322
  icons: s,
311
323
  onApprovalRequired: a,
312
- approvalOverride: i,
313
- initialHistory: l,
314
- initialEntries: c,
315
- onConversationChange: u
324
+ approvalOverride: l,
325
+ initialHistory: i,
326
+ initialEntries: o,
327
+ onConversationChange: m,
328
+ theme: u,
329
+ disableRoot: d
316
330
  }) {
317
- const m = C(u);
318
- m.current = u;
319
- const k = C(l), g = C(c), T = C(a);
320
- T.current = a;
321
- const M = C(i);
322
- M.current = i;
323
- const P = C(null), S = C(null), w = C(null), B = J(() => {
324
- const $ = a !== void 0, O = i !== void 0 && i.length > 0;
325
- if (!$ && !O) return t;
326
- const z = {
327
- notifyAwaiting: (U, D) => {
328
- var j;
329
- return (j = P.current) == null ? void 0 : j.call(P, U, D);
331
+ const b = R(m);
332
+ b.current = m;
333
+ const S = R(i), P = R(o), _ = R(a);
334
+ _.current = a;
335
+ const M = R(l);
336
+ M.current = l;
337
+ const c = R(null), I = R(null), g = R(null), T = et(() => {
338
+ var W, X;
339
+ if (t === null) return null;
340
+ const L = a !== void 0, Y = l !== void 0 && l.length > 0, q = (((X = (W = t.registry) == null ? void 0 : W.getTools) == null ? void 0 : X.call(W)) ?? []).some((O) => O.requiresApproval === !0);
341
+ if (!L && !Y && !q) return t;
342
+ const Z = {
343
+ notifyAwaiting: (O, Q) => {
344
+ var H;
345
+ return (H = c.current) == null ? void 0 : H.call(c, O, Q);
330
346
  },
331
- setStatus: (U, D) => {
332
- var j;
333
- return (j = S.current) == null ? void 0 : j.call(S, U, D);
347
+ setStatus: (O, Q) => {
348
+ var H;
349
+ return (H = I.current) == null ? void 0 : H.call(I, O, Q);
334
350
  },
335
- enqueueInline: (U, D) => {
336
- var j;
337
- return ((j = w.current) == null ? void 0 : j.call(w, U, D)) ?? Promise.resolve(!0);
351
+ enqueueInline: (O, Q) => {
352
+ var H;
353
+ return ((H = g.current) == null ? void 0 : H.call(g, O, Q)) ?? Promise.resolve(!0);
338
354
  }
339
- }, H = Dt(
355
+ }, K = Vt(
340
356
  t.registry,
341
- () => T.current,
357
+ () => _.current ?? Wt,
342
358
  () => M.current,
343
- z
359
+ Z
344
360
  );
345
- return new bt(t.adapter, H);
346
- }, [t, a, i]), [b, A] = R(() => {
347
- const $ = B.conversation(n);
348
- return k.current && k.current.length > 0 && ($.history = [...k.current]), $;
349
- }), N = _(
350
- ($, O) => {
351
- var z;
352
- (z = m.current) == null || z.call(m, O, $);
361
+ return new Et(t.adapter, K);
362
+ }, [t, a, l]), [N, A] = $(() => {
363
+ if (T === null) return null;
364
+ const L = T.conversation(n);
365
+ return S.current && S.current.length > 0 && (L.history = [...S.current]), L;
366
+ });
367
+ z(() => {
368
+ if (T !== null && N === null) {
369
+ const L = T.conversation(n);
370
+ S.current && S.current.length > 0 && (L.history = [...S.current]), A(L);
371
+ }
372
+ }, [T, N, n]);
373
+ const x = D(
374
+ (L, Y) => {
375
+ var G;
376
+ (G = b.current) == null || G.call(b, Y, L);
353
377
  },
354
378
  []
355
379
  ), {
356
- entries: E,
357
- history: I,
358
- sendMessage: h,
359
- cancel: p,
360
- isRunning: x,
361
- reset: d,
362
- setToolAwaitingApproval: f,
363
- setToolStatus: o
364
- } = xt(b, {
365
- initialEntries: g.current,
366
- onTurnComplete: N
380
+ entries: h,
381
+ history: p,
382
+ sendMessage: E,
383
+ cancel: f,
384
+ isRunning: k,
385
+ reset: v,
386
+ setToolAwaitingApproval: C,
387
+ setToolStatus: y
388
+ } = Rt(N, {
389
+ initialEntries: P.current,
390
+ onTurnComplete: x
367
391
  });
368
- P.current = f, S.current = o;
369
- const [y, V] = R([]), X = C(y);
370
- X.current = y;
371
- const dt = _(($, O) => new Promise((z) => {
372
- const H = (D) => {
373
- V((j) => j.filter((ht) => ht !== U)), z(D);
374
- }, U = {
375
- toolName: $,
376
- args: O,
377
- approve: () => H(!0),
378
- reject: () => H(!1),
379
- respondWith: (D) => H(D)
392
+ c.current = C, I.current = y;
393
+ const [U, at] = $([]), lt = R(U);
394
+ lt.current = U;
395
+ const Tt = D((L, Y) => new Promise((G) => {
396
+ const q = (K) => {
397
+ at((W) => W.filter((X) => X !== Z)), G(K);
398
+ }, Z = {
399
+ toolName: L,
400
+ args: Y,
401
+ approve: () => q(!0),
402
+ reject: () => q(!1),
403
+ respondWith: (K) => q(K)
380
404
  };
381
- V((D) => [...D, U]);
405
+ at((K) => [...K, Z]);
382
406
  }), []);
383
- w.current = dt;
384
- const Y = _(() => {
385
- X.current.forEach(($) => $.reject()), d(), A(B.conversation(n));
386
- }, [d, B, n]), mt = J(
407
+ g.current = Tt;
408
+ const it = D(() => {
409
+ lt.current.forEach((L) => L.reject()), v(), A(T === null ? null : T.conversation(n));
410
+ }, [v, T, n]), ot = T !== null, yt = et(
387
411
  () => ({
388
- messages: E,
389
- history: I,
390
- sendMessage: h,
391
- cancel: p,
392
- isRunning: x,
393
- reset: Y,
394
- pendingApprovals: y
412
+ messages: h,
413
+ history: p,
414
+ sendMessage: E,
415
+ cancel: f,
416
+ isRunning: k,
417
+ reset: it,
418
+ pendingApprovals: U,
419
+ isReady: ot
395
420
  }),
396
- [E, I, h, p, x, Y, y]
397
- );
398
- return /* @__PURE__ */ e(it.Provider, { value: mt, children: /* @__PURE__ */ e(Ct, { icons: s, children: r }) });
421
+ [h, p, E, f, k, it, U, ot]
422
+ ), St = d === !1 ? /* @__PURE__ */ e("div", { "data-mast-root": !0, "data-mast-theme": u, children: r }) : r;
423
+ return /* @__PURE__ */ e(ft.Provider, { value: yt, children: /* @__PURE__ */ e(Bt, { icons: s, children: St }) });
399
424
  }
400
- function G() {
401
- const t = st(it);
425
+ function tt() {
426
+ const t = st(ft);
402
427
  if (!t)
403
428
  throw new Error("useAgent() must be called from a component rendered inside <AgentProvider>.");
404
429
  return t;
405
430
  }
406
- function $t(t) {
431
+ function Ft(t) {
407
432
  if (t === void 0) return "";
408
433
  if (typeof t == "string") return t;
409
434
  try {
@@ -412,16 +437,16 @@ function $t(t) {
412
437
  return String(t);
413
438
  }
414
439
  }
415
- function jt({ entry: t, approve: n, reject: r, className: s }) {
416
- const a = q(), i = ["mast-inline-approval", s].filter(Boolean).join(" "), l = $t(t.args);
417
- return /* @__PURE__ */ v("div", { "data-mast-inline-approval": !0, "data-tool-name": t.name, className: i, children: [
418
- /* @__PURE__ */ v("div", { className: "mast-inline-approval-header", children: [
440
+ function zt({ entry: t, approve: n, reject: r, className: s }) {
441
+ const a = J(), l = ["mast-inline-approval", s].filter(Boolean).join(" "), i = Ft(t.args);
442
+ return /* @__PURE__ */ w("div", { "data-mast-inline-approval": !0, "data-tool-name": t.name, className: l, children: [
443
+ /* @__PURE__ */ w("div", { className: "mast-inline-approval-header", children: [
419
444
  /* @__PURE__ */ e("span", { className: "mast-inline-approval-icon", "aria-hidden": "true", children: a.wrench }),
420
445
  /* @__PURE__ */ e("span", { className: "mast-inline-approval-name", children: t.name }),
421
446
  /* @__PURE__ */ e("span", { className: "mast-inline-approval-label", children: "requires approval" })
422
447
  ] }),
423
- l ? /* @__PURE__ */ e("pre", { className: "mast-inline-approval-args", children: /* @__PURE__ */ e("code", { children: l }) }) : null,
424
- /* @__PURE__ */ v("div", { className: "mast-inline-approval-actions", children: [
448
+ i ? /* @__PURE__ */ e("pre", { className: "mast-inline-approval-args", children: /* @__PURE__ */ e("code", { children: i }) }) : null,
449
+ /* @__PURE__ */ w("div", { className: "mast-inline-approval-actions", children: [
425
450
  /* @__PURE__ */ e(
426
451
  "button",
427
452
  {
@@ -443,24 +468,24 @@ function jt({ entry: t, approve: n, reject: r, className: s }) {
443
468
  ] })
444
469
  ] });
445
470
  }
446
- function lt({
471
+ function gt({
447
472
  content: t,
448
473
  isStreaming: n = !1,
449
474
  className: r,
450
475
  label: s = "Thinking Process",
451
476
  open: a
452
477
  }) {
453
- const i = q(), l = ["mast-thinking-block", r].filter(Boolean).join(" ");
454
- return /* @__PURE__ */ v(
478
+ const l = J(), i = ["mast-thinking-block", r].filter(Boolean).join(" ");
479
+ return /* @__PURE__ */ w(
455
480
  "details",
456
481
  {
457
482
  "data-mast-thinking-block": !0,
458
483
  "data-streaming": n ? "true" : void 0,
459
- className: l,
484
+ className: i,
460
485
  open: a,
461
486
  children: [
462
- /* @__PURE__ */ v("summary", { className: "mast-thinking-block-summary", children: [
463
- /* @__PURE__ */ e("span", { className: "mast-thinking-block-icon", "aria-hidden": "true", children: i.brain }),
487
+ /* @__PURE__ */ w("summary", { className: "mast-thinking-block-summary", children: [
488
+ /* @__PURE__ */ e("span", { className: "mast-thinking-block-icon", "aria-hidden": "true", children: l.brain }),
464
489
  /* @__PURE__ */ e("span", { className: "mast-thinking-block-label", children: s }),
465
490
  n ? /* @__PURE__ */ e(
466
491
  "span",
@@ -476,7 +501,8 @@ function lt({
476
501
  }
477
502
  );
478
503
  }
479
- function et(t) {
504
+ const vt = nt(void 0);
505
+ function ut(t) {
480
506
  if (t === void 0) return "";
481
507
  if (typeof t == "string") return t;
482
508
  try {
@@ -485,7 +511,7 @@ function et(t) {
485
511
  return String(t);
486
512
  }
487
513
  }
488
- function Lt(t, n) {
514
+ function Yt(t, n) {
489
515
  if (t.isStreaming) return n.loader;
490
516
  switch (t.status) {
491
517
  case "error":
@@ -496,22 +522,27 @@ function Lt(t, n) {
496
522
  return n.check;
497
523
  }
498
524
  }
499
- function Bt(t, n) {
525
+ function Gt(t, n) {
500
526
  return t === "streaming" ? n ? !0 : void 0 : t;
501
527
  }
502
- function ot({ entry: t, className: n, defaultOpen: r = "streaming" }) {
503
- const s = q(), a = ["mast-tool-call-block", n].filter(Boolean).join(" "), i = t.subThinking !== void 0 || t.subText !== void 0, l = t.nestedToolEvents ?? [], c = Lt(t, s), u = et(t.args), m = et(t.result), k = Bt(r, t.isStreaming);
504
- return /* @__PURE__ */ v(
528
+ function bt({
529
+ entry: t,
530
+ className: n,
531
+ defaultOpen: r = "streaming",
532
+ label: s
533
+ }) {
534
+ const a = J(), l = st(vt), i = ["mast-tool-call-block", n].filter(Boolean).join(" "), o = t.subThinking !== void 0 || t.subText !== void 0, m = t.nestedToolEvents ?? [], u = Yt(t, a), d = ut(t.args), b = ut(t.result), S = Gt(r, t.isStreaming), P = s ?? (l == null ? void 0 : l(t)) ?? t.name;
535
+ return /* @__PURE__ */ w(
505
536
  "details",
506
537
  {
507
538
  "data-mast-tool-call-block": !0,
508
539
  "data-streaming": t.isStreaming ? "true" : void 0,
509
540
  "data-status": t.status,
510
541
  "data-tool-name": t.name,
511
- className: a,
512
- open: k,
542
+ className: i,
543
+ open: S,
513
544
  children: [
514
- /* @__PURE__ */ v("summary", { className: "mast-tool-call-block-header", children: [
545
+ /* @__PURE__ */ w("summary", { className: "mast-tool-call-block-header", children: [
515
546
  /* @__PURE__ */ e("span", { className: "mast-tool-call-block-chevron", "aria-hidden": "true", children: "▸" }),
516
547
  /* @__PURE__ */ e(
517
548
  "span",
@@ -519,16 +550,16 @@ function ot({ entry: t, className: n, defaultOpen: r = "streaming" }) {
519
550
  className: "mast-tool-call-block-status",
520
551
  "data-testid": "mast-tool-call-status",
521
552
  "aria-hidden": "true",
522
- children: c
553
+ children: u
523
554
  }
524
555
  ),
525
- /* @__PURE__ */ e("span", { className: "mast-tool-call-block-wrench", "aria-hidden": "true", children: s.wrench }),
526
- /* @__PURE__ */ e("span", { className: "mast-tool-call-block-name", children: t.name })
556
+ /* @__PURE__ */ e("span", { className: "mast-tool-call-block-wrench", "aria-hidden": "true", children: a.wrench }),
557
+ /* @__PURE__ */ e("span", { className: "mast-tool-call-block-name", children: P })
527
558
  ] }),
528
- /* @__PURE__ */ v("div", { className: "mast-tool-call-block-body", children: [
529
- i ? /* @__PURE__ */ v("div", { className: "mast-tool-call-block-sub-output", children: [
559
+ /* @__PURE__ */ w("div", { className: "mast-tool-call-block-body", children: [
560
+ o ? /* @__PURE__ */ w("div", { className: "mast-tool-call-block-sub-output", children: [
530
561
  t.subThinking !== void 0 ? /* @__PURE__ */ e(
531
- lt,
562
+ gt,
532
563
  {
533
564
  content: t.subThinking,
534
565
  isStreaming: t.isStreaming,
@@ -538,25 +569,25 @@ function ot({ entry: t, className: n, defaultOpen: r = "streaming" }) {
538
569
  ) : null,
539
570
  t.subText !== void 0 ? /* @__PURE__ */ e("div", { className: "mast-tool-call-block-sub-text", "data-testid": "mast-tool-call-sub-text", children: t.subText }) : null
540
571
  ] }) : null,
541
- l.length > 0 ? /* @__PURE__ */ e("div", { className: "mast-tool-call-block-nested", "data-testid": "mast-tool-call-nested", children: l.map((g) => /* @__PURE__ */ e(ot, { entry: g }, g.id)) }) : null,
542
- u ? /* @__PURE__ */ v("details", { className: "mast-tool-call-block-args", children: [
572
+ m.length > 0 ? /* @__PURE__ */ e("div", { className: "mast-tool-call-block-nested", "data-testid": "mast-tool-call-nested", children: m.map((_) => /* @__PURE__ */ e(bt, { entry: _ }, _.id)) }) : null,
573
+ d ? /* @__PURE__ */ w("details", { className: "mast-tool-call-block-args", children: [
543
574
  /* @__PURE__ */ e("summary", { children: "Arguments" }),
544
- /* @__PURE__ */ e("pre", { className: "mast-tool-call-block-pre", children: /* @__PURE__ */ e("code", { children: u }) })
575
+ /* @__PURE__ */ e("pre", { className: "mast-tool-call-block-pre", children: /* @__PURE__ */ e("code", { children: d }) })
545
576
  ] }) : null,
546
- !t.isStreaming && m ? /* @__PURE__ */ v("details", { className: "mast-tool-call-block-result", children: [
577
+ !t.isStreaming && b ? /* @__PURE__ */ w("details", { className: "mast-tool-call-block-result", children: [
547
578
  /* @__PURE__ */ e("summary", { children: "Result" }),
548
- /* @__PURE__ */ e("pre", { className: "mast-tool-call-block-pre", children: /* @__PURE__ */ e("code", { children: m }) })
579
+ /* @__PURE__ */ e("pre", { className: "mast-tool-call-block-pre", children: /* @__PURE__ */ e("code", { children: b }) })
549
580
  ] }) : null
550
581
  ] })
551
582
  ]
552
583
  }
553
584
  );
554
585
  }
555
- function Ut({ entry: t, className: n }) {
586
+ function qt({ entry: t, className: n }) {
556
587
  const r = ["mast-user-message", n].filter(Boolean).join(" ");
557
588
  return /* @__PURE__ */ e("div", { "data-mast-user-message": !0, className: r, children: /* @__PURE__ */ e("div", { className: "mast-user-message-bubble", children: t.text }) });
558
589
  }
559
- const Kt = pt(async () => {
590
+ const Qt = It(async () => {
560
591
  try {
561
592
  const [{ default: t }, { default: n }, { default: r }] = await Promise.all([
562
593
  import("react-markdown"),
@@ -572,39 +603,41 @@ const Kt = pt(async () => {
572
603
  };
573
604
  }
574
605
  });
575
- function Vt({
606
+ function Jt({
576
607
  entry: t,
577
608
  className: n,
578
609
  renderMessage: r,
579
- renderToolCall: s
610
+ renderToolCall: s,
611
+ renderApproval: a,
612
+ getToolLabel: l
580
613
  }) {
581
- const a = ["mast-assistant-message", n].filter(Boolean).join(" "), { pendingApprovals: i } = G(), l = (c) => {
582
- const u = c.awaitingApproval ? i.find((m) => m.toolName === c.name) : void 0;
583
- return s ? s(c, u) : u ? /* @__PURE__ */ e(
584
- jt,
614
+ const i = ["mast-assistant-message", n].filter(Boolean).join(" "), { pendingApprovals: o } = tt(), m = (u) => {
615
+ const d = u.awaitingApproval ? o.find((b) => b.toolName === u.name) : void 0;
616
+ return d && a ? a(u, d) : s ? s(u, d) : d ? /* @__PURE__ */ e(
617
+ zt,
585
618
  {
586
- entry: c,
587
- approve: u.approve,
588
- reject: u.reject,
589
- respondWith: u.respondWith
619
+ entry: u,
620
+ approve: d.approve,
621
+ reject: d.reject,
622
+ respondWith: d.respondWith
590
623
  }
591
- ) : /* @__PURE__ */ e(ot, { entry: c });
624
+ ) : /* @__PURE__ */ e(bt, { entry: u });
592
625
  };
593
- return /* @__PURE__ */ v(
626
+ return /* @__PURE__ */ e(vt.Provider, { value: l, children: /* @__PURE__ */ w(
594
627
  "div",
595
628
  {
596
629
  "data-mast-assistant-message": !0,
597
630
  "data-streaming": t.isStreaming ? "true" : void 0,
598
- className: a,
631
+ className: i,
599
632
  children: [
600
- t.thinking ? /* @__PURE__ */ e(lt, { content: t.thinking, isStreaming: t.isStreaming }) : null,
601
- t.toolEvents.map((c, u) => /* @__PURE__ */ e(rt, { children: l(c) }, `${c.id}-${u}`)),
602
- t.text ? r ? r(t.text) : /* @__PURE__ */ e(ft, { fallback: /* @__PURE__ */ e("p", { className: "mast-assistant-message-text", children: t.text }), children: /* @__PURE__ */ e(Kt, { children: t.text }) }) : null
633
+ t.thinking ? /* @__PURE__ */ e(gt, { content: t.thinking, isStreaming: t.isStreaming }) : null,
634
+ t.toolEvents.map((u, d) => /* @__PURE__ */ e(mt, { children: m(u) }, `${u.id}-${d}`)),
635
+ t.text ? r ? r(t.text) : /* @__PURE__ */ e(Nt, { fallback: /* @__PURE__ */ e("p", { className: "mast-assistant-message-text", children: t.text }), children: /* @__PURE__ */ e(Qt, { children: t.text }) }) : null
603
636
  ]
604
637
  }
605
- );
638
+ ) });
606
639
  }
607
- function Wt({
640
+ function Zt({
608
641
  items: t,
609
642
  activeIndex: n,
610
643
  onSelect: r,
@@ -618,137 +651,137 @@ function Wt({
618
651
  id: `${s}-listbox`,
619
652
  role: "listbox",
620
653
  "aria-label": "Mention picker",
621
- children: t.map((i, l) => {
622
- const c = l === n, u = c ? "mast-mention-picker-item mast-mention-picker-active" : "mast-mention-picker-item";
654
+ children: t.map((l, i) => {
655
+ const o = i === n, m = o ? "mast-mention-picker-item mast-mention-picker-active" : "mast-mention-picker-item";
623
656
  return /* @__PURE__ */ e(
624
657
  "li",
625
658
  {
626
- id: `${s}-${l}`,
659
+ id: `${s}-${i}`,
627
660
  role: "option",
628
- "aria-selected": c,
629
- className: u,
630
- onMouseDown: (m) => {
631
- m.preventDefault(), r(i);
661
+ "aria-selected": o,
662
+ className: m,
663
+ onMouseDown: (u) => {
664
+ u.preventDefault(), r(l);
632
665
  },
633
- children: a ? a(i, c) : /* @__PURE__ */ v("div", { children: [
634
- /* @__PURE__ */ e("div", { className: "mast-mention-picker-item-label", children: i.label }),
635
- i.description && /* @__PURE__ */ e("div", { className: "mast-mention-picker-item-description", children: i.description })
666
+ children: a ? a(l, o) : /* @__PURE__ */ w("div", { children: [
667
+ /* @__PURE__ */ e("div", { className: "mast-mention-picker-item-label", children: l.label }),
668
+ l.description && /* @__PURE__ */ e("div", { className: "mast-mention-picker-item-description", children: l.description })
636
669
  ] })
637
670
  },
638
- i.id
671
+ l.id
639
672
  );
640
673
  })
641
674
  }
642
675
  );
643
676
  }
644
- const Z = "@";
645
- function ct(t) {
677
+ const rt = "@";
678
+ function kt(t) {
646
679
  return t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
647
680
  }
648
- function zt(t, n = Z) {
649
- const r = ct(n), s = new RegExp(`${r}([^\\s${r}]*)$`), a = t.match(s);
681
+ function Xt(t, n = rt) {
682
+ const r = kt(n), s = new RegExp(`${r}([^\\s${r}]*)$`), a = t.match(s);
650
683
  return a ? a[1] : null;
651
684
  }
652
- function Ft(t, n = Z) {
653
- const r = ct(n), s = new RegExp(`${r}[^\\s${r}]*$`);
685
+ function te(t, n = rt) {
686
+ const r = kt(n), s = new RegExp(`${r}[^\\s${r}]*$`);
654
687
  return t.replace(s, "");
655
688
  }
656
- function Ot(t, n) {
657
- return t.map((r) => `${r.text}${Z}${r.item.label}`).join("") + n;
658
- }
659
- function Ht(t) {
660
- const n = t.trigger ?? "@", [r, s] = R([]), [a, i] = R(""), [l, c] = R(null), [u, m] = R(0), [k, g] = R([]), T = C(r);
661
- T.current = r;
662
- const M = C(a);
663
- M.current = a;
664
- const P = C(0);
665
- Q(() => {
666
- if (l === null) {
667
- g([]);
689
+ function ee(t, n) {
690
+ return t.map((r) => `${r.text}${rt}${r.item.label}`).join("") + n;
691
+ }
692
+ function ne(t) {
693
+ const n = t.trigger ?? "@", [r, s] = $([]), [a, l] = $(""), [i, o] = $(null), [m, u] = $(0), [d, b] = $([]), S = R(r);
694
+ S.current = r;
695
+ const P = R(a);
696
+ P.current = a;
697
+ const _ = R(0);
698
+ z(() => {
699
+ if (i === null) {
700
+ b([]);
668
701
  return;
669
702
  }
670
- const h = ++P.current;
703
+ const h = ++_.current;
671
704
  if (t.onSearch) {
672
- const p = t.onSearch(l);
673
- p instanceof Promise ? p.then((x) => {
674
- h === P.current && g(x);
675
- }) : g(p);
705
+ const p = t.onSearch(i);
706
+ p instanceof Promise ? p.then((E) => {
707
+ h === _.current && b(E);
708
+ }) : b(p);
676
709
  }
677
- }, [l, t.onSearch]);
678
- const S = new Set(r.map((h) => h.item.id)), w = (() => {
679
- if (l === null) return [];
710
+ }, [i, t.onSearch]);
711
+ const M = new Set(r.map((h) => h.item.id)), c = (() => {
712
+ if (i === null) return [];
680
713
  if (t.onSearch)
681
- return k.filter((p) => !S.has(p.id));
682
- const h = l.toLowerCase();
714
+ return d.filter((p) => !M.has(p.id));
715
+ const h = i.toLowerCase();
683
716
  return (t.items ?? []).filter(
684
- (p) => !S.has(p.id) && p.label.toLowerCase().includes(h)
717
+ (p) => !M.has(p.id) && p.label.toLowerCase().includes(h)
685
718
  );
686
- })(), B = _(
719
+ })(), I = D(
687
720
  (h) => {
688
- i(h), c(zt(h, n)), m(0);
721
+ l(h), o(Xt(h, n)), u(0);
689
722
  },
690
723
  [n]
691
- ), b = _(
724
+ ), g = D(
692
725
  (h) => {
693
- const p = T.current;
694
- if (p.some((d) => d.item.id === h.id)) return;
695
- const x = Ft(M.current, n);
696
- s([...p, { text: x, item: h }]), i(""), c(null), m(0);
726
+ const p = S.current;
727
+ if (p.some((f) => f.item.id === h.id)) return;
728
+ const E = te(P.current, n);
729
+ s([...p, { text: E, item: h }]), l(""), o(null), u(0);
697
730
  },
698
731
  [n]
699
- ), A = _((h) => {
700
- const p = T.current, x = p.findIndex((o) => o.item.id === h);
701
- if (x === -1) return;
702
- const d = p[x].text, f = [...p.slice(0, x), ...p.slice(x + 1)];
703
- if (x < f.length) {
704
- f[x] = { ...f[x], text: d + f[x].text }, s(f);
732
+ ), T = D((h) => {
733
+ const p = S.current, E = p.findIndex((v) => v.item.id === h);
734
+ if (E === -1) return;
735
+ const f = p[E].text, k = [...p.slice(0, E), ...p.slice(E + 1)];
736
+ if (E < k.length) {
737
+ k[E] = { ...k[E], text: f + k[E].text }, s(k);
705
738
  return;
706
739
  }
707
- s(f), i(d + M.current);
708
- }, []), N = _(
740
+ s(k), l(f + P.current);
741
+ }, []), N = D(
709
742
  (h) => {
710
- if (l === null || w.length === 0) return !1;
743
+ if (i === null || c.length === 0) return !1;
711
744
  switch (h.key) {
712
745
  case "ArrowDown":
713
- return h.preventDefault(), m((p) => (p + 1) % w.length), !0;
746
+ return h.preventDefault(), u((p) => (p + 1) % c.length), !0;
714
747
  case "ArrowUp":
715
- return h.preventDefault(), m((p) => p === 0 ? w.length - 1 : p - 1), !0;
748
+ return h.preventDefault(), u((p) => p === 0 ? c.length - 1 : p - 1), !0;
716
749
  case "Enter":
717
- return h.preventDefault(), b(w[u] ?? w[0]), !0;
750
+ return h.preventDefault(), g(c[m] ?? c[0]), !0;
718
751
  case "Escape":
719
- return h.preventDefault(), c(null), !0;
752
+ return h.preventDefault(), o(null), !0;
720
753
  default:
721
754
  return !1;
722
755
  }
723
756
  },
724
- [l, w, u, b]
725
- ), E = _(() => {
726
- const h = Ot(r, a);
757
+ [i, c, m, g]
758
+ ), A = D(() => {
759
+ const h = ee(r, a);
727
760
  return { prompt: t.buildPrompt ? t.buildPrompt(r, a) : h, displayText: h };
728
- }, [r, a, t.buildPrompt]), I = _(() => {
729
- s([]), i(""), c(null), m(0);
761
+ }, [r, a, t.buildPrompt]), x = D(() => {
762
+ s([]), l(""), o(null), u(0);
730
763
  }, []);
731
764
  return {
732
765
  segments: r,
733
766
  trailingInput: a,
734
- mentionQuery: l,
735
- filteredItems: w,
736
- pickerIndex: u,
737
- setTrailingInput: B,
767
+ mentionQuery: i,
768
+ filteredItems: c,
769
+ pickerIndex: m,
770
+ setTrailingInput: I,
738
771
  handleKeyDown: N,
739
- selectItem: b,
740
- removeChip: A,
741
- buildSubmission: E,
742
- clear: I
772
+ selectItem: g,
773
+ removeChip: T,
774
+ buildSubmission: A,
775
+ clear: x
743
776
  };
744
777
  }
745
- const qt = 1, Gt = 8;
746
- function ut(t) {
778
+ const se = 1, re = 8;
779
+ function xt(t) {
747
780
  const n = t.split(`
748
781
  `).length;
749
- return Math.min(Gt, Math.max(qt, n));
782
+ return Math.min(re, Math.max(se, n));
750
783
  }
751
- function Jt({
784
+ function ae({
752
785
  className: t,
753
786
  placeholder: n = "Type a message and press Enter.",
754
787
  sendLabel: r,
@@ -756,7 +789,7 @@ function Jt({
756
789
  mentions: a
757
790
  }) {
758
791
  return a ? /* @__PURE__ */ e(
759
- Zt,
792
+ ie,
760
793
  {
761
794
  className: t,
762
795
  placeholder: n,
@@ -765,7 +798,7 @@ function Jt({
765
798
  mentions: a
766
799
  }
767
800
  ) : /* @__PURE__ */ e(
768
- Qt,
801
+ le,
769
802
  {
770
803
  className: t,
771
804
  placeholder: n,
@@ -774,19 +807,19 @@ function Jt({
774
807
  }
775
808
  );
776
809
  }
777
- function Qt({ className: t, placeholder: n, sendLabel: r, cancelLabel: s }) {
778
- const { sendMessage: a, cancel: i, isRunning: l } = G(), c = q(), [u, m] = R(""), k = ["mast-chat-input", t].filter(Boolean).join(" "), g = u.trim(), T = !l && g.length > 0, M = () => {
779
- T && (a(g), m(""));
780
- }, P = (S) => {
781
- S.key === "Enter" && !S.shiftKey && (S.preventDefault(), M());
810
+ function le({ className: t, placeholder: n, sendLabel: r, cancelLabel: s }) {
811
+ const { sendMessage: a, cancel: l, isRunning: i, isReady: o } = tt(), m = J(), [u, d] = $(""), b = ["mast-chat-input", t].filter(Boolean).join(" "), S = u.trim(), P = o && !i && S.length > 0, _ = !o || i, M = () => {
812
+ P && (a(S), d(""));
813
+ }, c = (I) => {
814
+ I.key === "Enter" && !I.shiftKey && (I.preventDefault(), M());
782
815
  };
783
- return /* @__PURE__ */ v(
816
+ return /* @__PURE__ */ w(
784
817
  "form",
785
818
  {
786
819
  "data-mast-chat-input": !0,
787
- className: k,
788
- onSubmit: (S) => {
789
- S.preventDefault(), M();
820
+ className: b,
821
+ onSubmit: (I) => {
822
+ I.preventDefault(), M();
790
823
  },
791
824
  children: [
792
825
  /* @__PURE__ */ e("label", { className: "mast-chat-input-label mast-visually-hidden", htmlFor: "mast-chat-input-field", children: "Message" }),
@@ -797,20 +830,20 @@ function Qt({ className: t, placeholder: n, sendLabel: r, cancelLabel: s }) {
797
830
  className: "mast-chat-input-textarea",
798
831
  value: u,
799
832
  placeholder: n,
800
- rows: ut(u),
801
- disabled: l,
802
- onChange: (S) => m(S.target.value),
803
- onKeyDown: P
833
+ rows: xt(u),
834
+ disabled: _,
835
+ onChange: (I) => d(I.target.value),
836
+ onKeyDown: c
804
837
  }
805
838
  ),
806
- l ? /* @__PURE__ */ e(
839
+ i ? /* @__PURE__ */ e(
807
840
  "button",
808
841
  {
809
842
  type: "button",
810
843
  className: "mast-chat-input-cancel",
811
844
  "aria-label": "Cancel",
812
- onClick: i,
813
- children: s ?? c.stop
845
+ onClick: l,
846
+ children: s ?? m.stop
814
847
  }
815
848
  ) : /* @__PURE__ */ e(
816
849
  "button",
@@ -818,106 +851,106 @@ function Qt({ className: t, placeholder: n, sendLabel: r, cancelLabel: s }) {
818
851
  type: "submit",
819
852
  className: "mast-chat-input-send",
820
853
  "aria-label": "Send",
821
- disabled: !T,
822
- children: r ?? c.send
854
+ disabled: !P,
855
+ children: r ?? m.send
823
856
  }
824
857
  )
825
858
  ]
826
859
  }
827
860
  );
828
861
  }
829
- function Zt({
862
+ function ie({
830
863
  className: t,
831
864
  placeholder: n,
832
865
  sendLabel: r,
833
866
  cancelLabel: s,
834
867
  mentions: a
835
868
  }) {
836
- const { sendMessage: i, cancel: l, isRunning: c } = G(), u = q(), m = gt(), {
837
- segments: k,
838
- trailingInput: g,
839
- mentionQuery: T,
840
- filteredItems: M,
841
- pickerIndex: P,
842
- setTrailingInput: S,
843
- handleKeyDown: w,
844
- selectItem: B,
845
- removeChip: b,
846
- buildSubmission: A,
847
- clear: N
848
- } = Ht(a), E = ["mast-chat-input", "mast-mention-input", t].filter(Boolean).join(" "), I = k.length > 0 || g.trim().length > 0, h = !c && I, p = T !== null && M.length > 0, x = p ? `${m}-${P}` : void 0, d = () => {
849
- if (!h) return;
850
- const { prompt: o, displayText: y } = A();
851
- y.trim() && (i(o, y), N());
852
- }, f = (o) => {
853
- w(o) || o.key === "Enter" && !o.shiftKey && (o.preventDefault(), d());
869
+ const { sendMessage: l, cancel: i, isRunning: o, isReady: m } = tt(), u = J(), d = wt(), {
870
+ segments: b,
871
+ trailingInput: S,
872
+ mentionQuery: P,
873
+ filteredItems: _,
874
+ pickerIndex: M,
875
+ setTrailingInput: c,
876
+ handleKeyDown: I,
877
+ selectItem: g,
878
+ removeChip: T,
879
+ buildSubmission: N,
880
+ clear: A
881
+ } = ne(a), x = ["mast-chat-input", "mast-mention-input", t].filter(Boolean).join(" "), h = b.length > 0 || S.trim().length > 0, p = m && !o && h, E = !m || o, f = P !== null && _.length > 0, k = f ? `${d}-${M}` : void 0, v = () => {
882
+ if (!p) return;
883
+ const { prompt: y, displayText: U } = N();
884
+ U.trim() && (l(y, U), A());
885
+ }, C = (y) => {
886
+ I(y) || y.key === "Enter" && !y.shiftKey && (y.preventDefault(), v());
854
887
  };
855
- return /* @__PURE__ */ v(
888
+ return /* @__PURE__ */ w(
856
889
  "form",
857
890
  {
858
891
  "data-mast-chat-input": !0,
859
- className: E,
860
- onSubmit: (o) => {
861
- o.preventDefault(), d();
892
+ className: x,
893
+ onSubmit: (y) => {
894
+ y.preventDefault(), v();
862
895
  },
863
896
  children: [
864
897
  /* @__PURE__ */ e("label", { className: "mast-chat-input-label mast-visually-hidden", htmlFor: "mast-chat-input-field", children: "Message" }),
865
- /* @__PURE__ */ v("div", { className: "mast-mention-input-field", children: [
866
- p && /* @__PURE__ */ e(
867
- Wt,
898
+ /* @__PURE__ */ w("div", { className: "mast-mention-input-field", children: [
899
+ f && /* @__PURE__ */ e(
900
+ Zt,
868
901
  {
869
- items: M,
870
- activeIndex: P,
871
- onSelect: B,
872
- idPrefix: m,
902
+ items: _,
903
+ activeIndex: M,
904
+ onSelect: g,
905
+ idPrefix: d,
873
906
  renderItem: a.renderItem
874
907
  }
875
908
  ),
876
- /* @__PURE__ */ v("div", { className: "mast-mention-input-content", children: [
877
- k.map((o) => /* @__PURE__ */ v(rt, { children: [
878
- o.text && /* @__PURE__ */ e("span", { className: "mast-mention-segment-text", children: o.text }),
879
- a.renderChip ? a.renderChip(o.item, () => b(o.item.id)) : /* @__PURE__ */ v("span", { className: "mast-mention-chip", children: [
909
+ /* @__PURE__ */ w("div", { className: "mast-mention-input-content", children: [
910
+ b.map((y) => /* @__PURE__ */ w(mt, { children: [
911
+ y.text && /* @__PURE__ */ e("span", { className: "mast-mention-segment-text", children: y.text }),
912
+ a.renderChip ? a.renderChip(y.item, () => T(y.item.id)) : /* @__PURE__ */ w("span", { className: "mast-mention-chip", children: [
880
913
  "@",
881
- o.item.label,
914
+ y.item.label,
882
915
  /* @__PURE__ */ e(
883
916
  "button",
884
917
  {
885
918
  type: "button",
886
919
  className: "mast-mention-chip-remove",
887
- "aria-label": `Remove reference to ${o.item.label}`,
888
- onClick: () => b(o.item.id),
920
+ "aria-label": `Remove reference to ${y.item.label}`,
921
+ onClick: () => T(y.item.id),
889
922
  children: "×"
890
923
  }
891
924
  )
892
925
  ] })
893
- ] }, o.item.id)),
926
+ ] }, y.item.id)),
894
927
  /* @__PURE__ */ e(
895
928
  "textarea",
896
929
  {
897
930
  id: "mast-chat-input-field",
898
931
  className: "mast-chat-input-textarea mast-mention-input-textarea",
899
- value: g,
900
- placeholder: k.length === 0 ? n : "",
901
- rows: ut(g),
902
- disabled: c,
903
- onChange: (o) => S(o.target.value),
904
- onKeyDown: f,
932
+ value: S,
933
+ placeholder: b.length === 0 ? n : "",
934
+ rows: xt(S),
935
+ disabled: E,
936
+ onChange: (y) => c(y.target.value),
937
+ onKeyDown: C,
905
938
  role: "combobox",
906
- "aria-expanded": p,
907
- "aria-controls": p ? `${m}-listbox` : void 0,
939
+ "aria-expanded": f,
940
+ "aria-controls": f ? `${d}-listbox` : void 0,
908
941
  "aria-autocomplete": "list",
909
- "aria-activedescendant": x
942
+ "aria-activedescendant": k
910
943
  }
911
944
  )
912
945
  ] })
913
946
  ] }),
914
- c ? /* @__PURE__ */ e(
947
+ o ? /* @__PURE__ */ e(
915
948
  "button",
916
949
  {
917
950
  type: "button",
918
951
  className: "mast-chat-input-cancel",
919
952
  "aria-label": "Cancel",
920
- onClick: l,
953
+ onClick: i,
921
954
  children: s ?? u.stop
922
955
  }
923
956
  ) : /* @__PURE__ */ e(
@@ -926,7 +959,7 @@ function Zt({
926
959
  type: "submit",
927
960
  className: "mast-chat-input-send",
928
961
  "aria-label": "Send",
929
- disabled: !h,
962
+ disabled: !p,
930
963
  children: r ?? u.send
931
964
  }
932
965
  )
@@ -934,86 +967,145 @@ function Zt({
934
967
  }
935
968
  );
936
969
  }
937
- function Xt({ entry: t, className: n, renderToolCall: r, renderMessage: s }) {
938
- return t.role === "user" ? /* @__PURE__ */ e(Ut, { entry: t, className: n }) : /* @__PURE__ */ e(
939
- Vt,
970
+ function oe({
971
+ entry: t,
972
+ className: n,
973
+ renderToolCall: r,
974
+ renderApproval: s,
975
+ renderMessage: a,
976
+ getToolLabel: l
977
+ }) {
978
+ return t.role === "user" ? /* @__PURE__ */ e(qt, { entry: t, className: n }) : /* @__PURE__ */ e(
979
+ Jt,
940
980
  {
941
981
  entry: t,
942
982
  className: n,
943
983
  renderToolCall: r,
944
- renderMessage: s
984
+ renderApproval: s,
985
+ renderMessage: a,
986
+ getToolLabel: l
945
987
  }
946
988
  );
947
989
  }
948
- const Yt = 80;
949
- function te({ className: t, renderToolCall: n, renderMessage: r }) {
950
- const { messages: s } = G(), a = C(null), i = vt({
951
- count: s.length,
952
- getScrollElement: () => a.current,
953
- estimateSize: () => Yt,
990
+ const ce = 80, ue = 4, dt = 4;
991
+ function de({
992
+ className: t,
993
+ renderToolCall: n,
994
+ renderApproval: r,
995
+ renderMessage: s,
996
+ getToolLabel: a
997
+ }) {
998
+ const { messages: l } = tt(), i = R(null), o = R(!0), m = R(0), u = R(null), d = At({
999
+ count: l.length,
1000
+ getScrollElement: () => i.current,
1001
+ estimateSize: () => ce,
954
1002
  overscan: 4
955
- }), l = s.length > 0 ? s[s.length - 1] : void 0, c = (l == null ? void 0 : l.text.length) ?? 0;
956
- Q(() => {
957
- s.length !== 0 && i.scrollToIndex(s.length - 1, { align: "end" });
958
- }, [s.length, c, i]);
959
- const u = i.getTotalSize(), m = i.getVirtualItems(), k = ["mast-message-list", t].filter(Boolean).join(" ");
960
- return /* @__PURE__ */ e("div", { ref: a, "data-mast-message-list": !0, className: k, role: "log", "aria-live": "polite", children: /* @__PURE__ */ e("div", { style: { height: `${u}px`, position: "relative", width: "100%" }, children: m.map((g) => {
961
- const T = s[g.index];
1003
+ });
1004
+ z(() => {
1005
+ const c = i.current;
1006
+ if (!c) return;
1007
+ m.current = c.scrollTop;
1008
+ const I = () => {
1009
+ const x = c.scrollTop, h = m.current;
1010
+ m.current = x;
1011
+ const p = c.scrollHeight - x - c.clientHeight;
1012
+ x < h - dt ? o.current = !1 : !o.current && p <= ue && (o.current = !0);
1013
+ }, g = (x) => {
1014
+ x.deltaY < 0 && c.scrollHeight > c.clientHeight && (o.current = !1);
1015
+ };
1016
+ let T = null;
1017
+ const N = (x) => {
1018
+ var h;
1019
+ T = ((h = x.touches[0]) == null ? void 0 : h.clientY) ?? null;
1020
+ }, A = (x) => {
1021
+ var p;
1022
+ if (T === null) return;
1023
+ (((p = x.touches[0]) == null ? void 0 : p.clientY) ?? T) - T > 0 && c.scrollHeight > c.clientHeight && (o.current = !1);
1024
+ };
1025
+ return c.addEventListener("scroll", I, { passive: !0 }), c.addEventListener("wheel", g, { passive: !0 }), c.addEventListener("touchstart", N, { passive: !0 }), c.addEventListener("touchmove", A, { passive: !0 }), () => {
1026
+ c.removeEventListener("scroll", I), c.removeEventListener("wheel", g), c.removeEventListener("touchstart", N), c.removeEventListener("touchmove", A);
1027
+ };
1028
+ }, []);
1029
+ const b = l.length > 0 ? l[l.length - 1] : void 0, S = (b == null ? void 0 : b.text.length) ?? 0, P = d.getTotalSize();
1030
+ z(() => {
1031
+ if (l.length === 0 || !o.current) return;
1032
+ const c = i.current;
1033
+ if (c && u.current !== null && c.scrollTop < u.current - dt) {
1034
+ o.current = !1;
1035
+ return;
1036
+ }
1037
+ d.scrollToIndex(l.length - 1, { align: "end" }), c && (u.current = c.scrollTop);
1038
+ }, [l.length, S, P, d]);
1039
+ const _ = d.getVirtualItems(), M = ["mast-message-list", t].filter(Boolean).join(" ");
1040
+ return /* @__PURE__ */ e("div", { ref: i, "data-mast-message-list": !0, className: M, role: "log", "aria-live": "polite", children: /* @__PURE__ */ e("div", { style: { height: `${P}px`, position: "relative", width: "100%" }, children: _.map((c) => {
1041
+ const I = l[c.index];
962
1042
  return /* @__PURE__ */ e(
963
1043
  "div",
964
1044
  {
965
- "data-index": g.index,
966
- ref: i.measureElement,
1045
+ "data-index": c.index,
1046
+ ref: d.measureElement,
967
1047
  style: {
968
1048
  position: "absolute",
969
1049
  top: 0,
970
1050
  left: 0,
971
1051
  width: "100%",
972
- transform: `translateY(${g.start}px)`
1052
+ transform: `translateY(${c.start}px)`
973
1053
  },
974
1054
  children: /* @__PURE__ */ e(
975
- Xt,
1055
+ oe,
976
1056
  {
977
- entry: T,
1057
+ entry: I,
978
1058
  renderToolCall: n,
979
- renderMessage: r
1059
+ renderApproval: r,
1060
+ renderMessage: s,
1061
+ getToolLabel: a
980
1062
  }
981
1063
  )
982
1064
  },
983
- T.id
1065
+ I.id
984
1066
  );
985
1067
  }) }) });
986
1068
  }
987
- function ie({
1069
+ function ve({
988
1070
  theme: t,
989
1071
  className: n,
990
1072
  renderToolCall: r,
991
- renderMessage: s,
992
- inputPlaceholder: a,
993
- mentions: i
1073
+ renderApproval: s,
1074
+ renderMessage: a,
1075
+ getToolLabel: l,
1076
+ inputPlaceholder: i,
1077
+ mentions: o
994
1078
  }) {
995
- const l = ["mast-conversation-panel", n].filter(Boolean).join(" ");
996
- return /* @__PURE__ */ v("div", { "data-mast-root": !0, "data-mast-theme": t, className: l, children: [
997
- /* @__PURE__ */ e(te, { renderToolCall: r, renderMessage: s }),
998
- /* @__PURE__ */ e(Jt, { placeholder: a, mentions: i })
1079
+ const m = ["mast-conversation-panel", n].filter(Boolean).join(" ");
1080
+ return /* @__PURE__ */ w("div", { "data-mast-root": !0, "data-mast-theme": t, className: m, children: [
1081
+ /* @__PURE__ */ e(
1082
+ de,
1083
+ {
1084
+ renderToolCall: r,
1085
+ renderApproval: s,
1086
+ renderMessage: a,
1087
+ getToolLabel: l
1088
+ }
1089
+ ),
1090
+ /* @__PURE__ */ e(ae, { placeholder: i, mentions: o })
999
1091
  ] });
1000
1092
  }
1001
1093
  export {
1002
- ae as AgentProvider,
1003
- Vt as AssistantMessage,
1004
- Jt as ChatInput,
1005
- ie as ConversationPanel,
1006
- Mt as INLINE_APPROVAL,
1007
- jt as InlineApproval,
1008
- Xt as MessageItem,
1009
- te as MessageList,
1010
- lt as ThinkingBlock,
1011
- ot as ToolCallBlock,
1012
- Ut as UserMessage,
1013
- Ot as buildInlineMentionPrompt,
1014
- zt as extractMentionQuery,
1015
- Ft as removeMentionTrigger,
1016
- G as useAgent,
1017
- xt as useAgentStream,
1018
- Ht as useMentions
1094
+ ge as AgentProvider,
1095
+ Jt as AssistantMessage,
1096
+ ae as ChatInput,
1097
+ ve as ConversationPanel,
1098
+ pt as INLINE_APPROVAL,
1099
+ zt as InlineApproval,
1100
+ oe as MessageItem,
1101
+ de as MessageList,
1102
+ gt as ThinkingBlock,
1103
+ bt as ToolCallBlock,
1104
+ qt as UserMessage,
1105
+ ee as buildInlineMentionPrompt,
1106
+ Xt as extractMentionQuery,
1107
+ te as removeMentionTrigger,
1108
+ tt as useAgent,
1109
+ Rt as useAgentStream,
1110
+ ne as useMentions
1019
1111
  };