@harnessio/ai-chat-core 0.0.1 → 0.0.3

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,62 +1,172 @@
1
1
  var $ = Object.defineProperty;
2
- var q = (n, i, t) => i in n ? $(n, i, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[i] = t;
3
- var d = (n, i, t) => q(n, typeof i != "symbol" ? i + "" : i, t);
4
- import j, { createContext as U, useContext as J, useState as D, useEffect as k } from "react";
2
+ var U = (i, s, e) => s in i ? $(i, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[s] = e;
3
+ var c = (i, s, e) => U(i, typeof s != "symbol" ? s + "" : s, e);
4
+ import B, { createContext as H, useContext as Q, useState as x, useEffect as w, useReducer as J } from "react";
5
5
  class W {
6
6
  constructor() {
7
- d(this, "plugins", /* @__PURE__ */ new Map());
8
- d(this, "renderersByType", /* @__PURE__ */ new Map());
7
+ c(this, "plugins", /* @__PURE__ */ new Map());
8
+ c(this, "renderersByType", /* @__PURE__ */ new Map());
9
9
  }
10
- registerPlugin(i) {
11
- this.plugins.has(i.id), this.plugins.set(i.id, i), i.renderers.forEach((t) => {
12
- const e = this.renderersByType.get(t.type) || [];
13
- e.push(t), e.sort((r, s) => ((s == null ? void 0 : s.priority) ?? 0) - ((r == null ? void 0 : r.priority) ?? 0)), this.renderersByType.set(t.type, e);
10
+ registerPlugin(s) {
11
+ this.plugins.has(s.id), this.plugins.set(s.id, s), s.renderers.forEach((e) => {
12
+ const t = this.renderersByType.get(e.type) || [];
13
+ t.push(e), t.sort((n, r) => ((r == null ? void 0 : r.priority) ?? 0) - ((n == null ? void 0 : n.priority) ?? 0)), this.renderersByType.set(e.type, t);
14
14
  });
15
15
  }
16
- unregisterPlugin(i) {
17
- const t = this.plugins.get(i);
18
- return t ? (t.renderers.forEach((e) => {
19
- const s = (this.renderersByType.get(e.type) || []).filter((a) => !t.renderers.includes(a));
20
- s.length === 0 ? this.renderersByType.delete(e.type) : this.renderersByType.set(e.type, s);
21
- }), this.plugins.delete(i)) : !1;
16
+ unregisterPlugin(s) {
17
+ const e = this.plugins.get(s);
18
+ return e ? (e.renderers.forEach((t) => {
19
+ const r = (this.renderersByType.get(t.type) || []).filter((a) => !e.renderers.includes(a));
20
+ r.length === 0 ? this.renderersByType.delete(t.type) : this.renderersByType.set(t.type, r);
21
+ }), this.plugins.delete(s)) : !1;
22
22
  }
23
- getPlugin(i) {
24
- return this.plugins.get(i);
23
+ getPlugin(s) {
24
+ return this.plugins.get(s);
25
25
  }
26
26
  getAllPlugins() {
27
27
  return Array.from(this.plugins.values());
28
28
  }
29
- getRenderersByType(i) {
30
- return this.renderersByType.get(i) || [];
29
+ getRenderersByType(s) {
30
+ return this.renderersByType.get(s) || [];
31
31
  }
32
- getBestRendererForType(i) {
33
- return this.getRenderersByType(i)[0];
32
+ getBestRendererForType(s) {
33
+ return this.getRenderersByType(s)[0];
34
34
  }
35
35
  clear() {
36
36
  this.plugins.clear(), this.renderersByType.clear();
37
37
  }
38
38
  }
39
- class m {
39
+ class z {
40
40
  constructor() {
41
- d(this, "_subscriptions", /* @__PURE__ */ new Set());
41
+ c(this, "handlers", /* @__PURE__ */ new Map());
42
+ c(this, "renderers", /* @__PURE__ */ new Map());
42
43
  }
43
- subscribe(i) {
44
- return this._subscriptions.add(i), () => {
45
- this._subscriptions.delete(i);
44
+ registerHandler(s, e) {
45
+ this.handlers.set(s, e);
46
+ }
47
+ registerRenderer(s, e) {
48
+ this.renderers.set(s, e);
49
+ }
50
+ unregister(s) {
51
+ this.handlers.delete(s), this.renderers.delete(s);
52
+ }
53
+ getHandler(s) {
54
+ return this.handlers.get(s);
55
+ }
56
+ getRenderer(s) {
57
+ return this.renderers.get(s);
58
+ }
59
+ getStrategy() {
60
+ return "queue";
61
+ }
62
+ hasHandler(s) {
63
+ return this.handlers.has(s);
64
+ }
65
+ hasRenderer(s) {
66
+ return this.renderers.has(s);
67
+ }
68
+ clear() {
69
+ this.handlers.clear(), this.renderers.clear();
70
+ }
71
+ getAllCapabilityNames() {
72
+ return Array.from(/* @__PURE__ */ new Set([...this.handlers.keys(), ...this.renderers.keys()]));
73
+ }
74
+ }
75
+ class V {
76
+ constructor(s) {
77
+ c(this, "executions", /* @__PURE__ */ new Map());
78
+ c(this, "executionQueue", []);
79
+ c(this, "isProcessing", !1);
80
+ c(this, "runningByName", /* @__PURE__ */ new Map());
81
+ c(this, "subscribers", /* @__PURE__ */ new Set());
82
+ c(this, "getHandler");
83
+ this.getHandler = s;
84
+ }
85
+ async executeCapability(s, e, t, n, r = "queue") {
86
+ if (!this.getHandler(s)) {
87
+ console.warn(`No handler registered for capability: ${s}`);
88
+ return;
89
+ }
90
+ const o = {
91
+ id: e,
92
+ name: s,
93
+ args: t,
94
+ status: { type: "queued" },
95
+ timestamp: Date.now(),
96
+ messageId: n
97
+ };
98
+ switch (this.executions.set(e, o), this.notifySubscribers(e), r) {
99
+ case "queue":
100
+ default:
101
+ this.executionQueue.push(e);
102
+ break;
103
+ }
104
+ this.isProcessing || this.processQueue();
105
+ }
106
+ async processQueue() {
107
+ if (!(this.isProcessing || this.executionQueue.length === 0)) {
108
+ for (this.isProcessing = !0; this.executionQueue.length > 0; ) {
109
+ const s = this.executionQueue.shift(), e = this.executions.get(s);
110
+ if (!e) continue;
111
+ const t = this.getHandler(e.name);
112
+ if (!t) continue;
113
+ e.status = { type: "running" }, this.notifySubscribers(s);
114
+ const n = this.runningByName.get(e.name) || [];
115
+ n.push(s), this.runningByName.set(e.name, n);
116
+ try {
117
+ const r = await t.execute(e.args, {
118
+ messageId: e.messageId,
119
+ capabilityId: s
120
+ });
121
+ e.status = { type: "complete", result: r }, e.result = r;
122
+ } catch (r) {
123
+ const a = r instanceof Error ? r.message : "Unknown error";
124
+ e.status = { type: "error", error: a }, e.error = a;
125
+ } finally {
126
+ const r = this.runningByName.get(e.name) || [], a = r.indexOf(s);
127
+ a > -1 && r.splice(a, 1), r.length === 0 ? this.runningByName.delete(e.name) : this.runningByName.set(e.name, r), this.notifySubscribers(s);
128
+ }
129
+ }
130
+ this.isProcessing = !1;
131
+ }
132
+ }
133
+ getExecution(s) {
134
+ return this.executions.get(s);
135
+ }
136
+ getExecutionsByMessage(s) {
137
+ return Array.from(this.executions.values()).filter((e) => e.messageId === s);
138
+ }
139
+ subscribe(s) {
140
+ return this.subscribers.add(s), () => this.subscribers.delete(s);
141
+ }
142
+ notifySubscribers(s) {
143
+ this.subscribers.forEach((e) => e(s));
144
+ }
145
+ clear() {
146
+ this.executions.clear(), this.executionQueue = [], this.runningByName.clear();
147
+ }
148
+ }
149
+ class y {
150
+ constructor() {
151
+ c(this, "_subscriptions", /* @__PURE__ */ new Set());
152
+ }
153
+ subscribe(s) {
154
+ return this._subscriptions.add(s), () => {
155
+ this._subscriptions.delete(s);
46
156
  };
47
157
  }
48
158
  notifySubscribers() {
49
- for (const i of this._subscriptions)
50
- i();
159
+ for (const s of this._subscriptions)
160
+ s();
51
161
  }
52
162
  getSubscriberCount() {
53
163
  return this._subscriptions.size;
54
164
  }
55
165
  }
56
- class z extends m {
166
+ class G extends y {
57
167
  constructor() {
58
168
  super(...arguments);
59
- d(this, "_state", {
169
+ c(this, "_state", {
60
170
  isActive: !1,
61
171
  context: null,
62
172
  focusedContent: null,
@@ -86,14 +196,14 @@ class z extends m {
86
196
  get focusedContentIndex() {
87
197
  return this._state.focusedContentIndex;
88
198
  }
89
- focus(t, e, r, s = "detail") {
199
+ focus(e, t, n, r = "detail") {
90
200
  this._state = {
91
201
  isActive: !0,
92
- context: s,
93
- focusedContent: t,
94
- focusedMessage: e,
95
- focusedMessageId: e.id,
96
- focusedContentIndex: r
202
+ context: r,
203
+ focusedContent: e,
204
+ focusedMessage: t,
205
+ focusedMessageId: t.id,
206
+ focusedContentIndex: n
97
207
  }, this.notifySubscribers();
98
208
  }
99
209
  blur() {
@@ -106,42 +216,42 @@ class z extends m {
106
216
  focusedContentIndex: null
107
217
  }, this.notifySubscribers();
108
218
  }
109
- toggle(t, e, r, s = "detail") {
110
- this._state.isActive && this._state.focusedMessageId === e.id && this._state.focusedContentIndex === r && this._state.context === s ? this.blur() : this.focus(t, e, r, s);
219
+ toggle(e, t, n, r = "detail") {
220
+ this._state.isActive && this._state.focusedMessageId === t.id && this._state.focusedContentIndex === n && this._state.context === r ? this.blur() : this.focus(e, t, n, r);
111
221
  }
112
- switchContext(t) {
222
+ switchContext(e) {
113
223
  this._state.isActive && this._state.focusedContent && (this._state = {
114
224
  ...this._state,
115
- context: t
225
+ context: e
116
226
  }, this.notifySubscribers());
117
227
  }
118
- focusNext(t) {
119
- if (!this._state.focusedMessageId || !t.length) return;
120
- const e = t.findIndex((a) => a.id === this._state.focusedMessageId);
121
- if (e === -1) return;
122
- const r = t[e], s = this._state.focusedContentIndex ?? 0;
123
- if (s + 1 < r.content.length) {
124
- const a = r.content[s + 1];
125
- this.focus(a, r, s + 1, this._state.context || "detail");
228
+ focusNext(e) {
229
+ if (!this._state.focusedMessageId || !e.length) return;
230
+ const t = e.findIndex((a) => a.id === this._state.focusedMessageId);
231
+ if (t === -1) return;
232
+ const n = e[t], r = this._state.focusedContentIndex ?? 0;
233
+ if (r + 1 < n.content.length) {
234
+ const a = n.content[r + 1];
235
+ this.focus(a, n, r + 1, this._state.context || "detail");
126
236
  return;
127
237
  }
128
- if (e + 1 < t.length) {
129
- const a = t[e + 1];
238
+ if (t + 1 < e.length) {
239
+ const a = e[t + 1];
130
240
  a.content.length > 0 && this.focus(a.content[0], a, 0, this._state.context || "detail");
131
241
  }
132
242
  }
133
- focusPrevious(t) {
134
- if (!this._state.focusedMessageId || !t.length) return;
135
- const e = t.findIndex((a) => a.id === this._state.focusedMessageId);
136
- if (e === -1) return;
137
- const r = t[e], s = this._state.focusedContentIndex ?? 0;
138
- if (s > 0) {
139
- const a = r.content[s - 1];
140
- this.focus(a, r, s - 1, this._state.context || "detail");
243
+ focusPrevious(e) {
244
+ if (!this._state.focusedMessageId || !e.length) return;
245
+ const t = e.findIndex((a) => a.id === this._state.focusedMessageId);
246
+ if (t === -1) return;
247
+ const n = e[t], r = this._state.focusedContentIndex ?? 0;
248
+ if (r > 0) {
249
+ const a = n.content[r - 1];
250
+ this.focus(a, n, r - 1, this._state.context || "detail");
141
251
  return;
142
252
  }
143
- if (e > 0) {
144
- const a = t[e - 1];
253
+ if (t > 0) {
254
+ const a = e[t - 1];
145
255
  if (a.content.length > 0) {
146
256
  const o = a.content.length - 1;
147
257
  this.focus(a.content[o], a, o, this._state.context || "detail");
@@ -149,21 +259,18 @@ class z extends m {
149
259
  }
150
260
  }
151
261
  }
152
- let G = 0, V = 0;
153
- function E() {
154
- return `msg-${Date.now()}-${++G}`;
262
+ let Y = 0, K = 0;
263
+ function P() {
264
+ return `msg-${Date.now()}-${++Y}`;
155
265
  }
156
- function M() {
157
- return `thread-${Date.now()}-${++V}`;
266
+ function F() {
267
+ return `thread-${Date.now()}-${++K}`;
158
268
  }
159
- function ct(n = "id") {
160
- return `${n}-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
161
- }
162
- class w extends m {
163
- constructor(t) {
269
+ class R extends y {
270
+ constructor(e) {
164
271
  super();
165
- d(this, "_state");
166
- this.config = t, this._state = t.state;
272
+ c(this, "_state");
273
+ this.config = e, this._state = e.state;
167
274
  }
168
275
  get state() {
169
276
  return this._state;
@@ -171,27 +278,27 @@ class w extends m {
171
278
  getState() {
172
279
  return this._state;
173
280
  }
174
- updateState(t) {
175
- this._state = t, this.notifySubscribers();
281
+ updateState(e) {
282
+ this._state = e, this.notifySubscribers();
176
283
  }
177
284
  async switchTo() {
178
285
  await this.config.onSwitchTo(this._state.id);
179
286
  }
180
- async rename(t) {
181
- await this.config.onRename(this._state.id, t);
287
+ async rename(e) {
288
+ await this.config.onRename(this._state.id, e);
182
289
  }
183
290
  async delete() {
184
291
  await this.config.onDelete(this._state.id);
185
292
  }
186
- subscribe(t) {
187
- return super.subscribe(t);
293
+ subscribe(e) {
294
+ return super.subscribe(e);
188
295
  }
189
296
  }
190
- class H extends m {
297
+ class X extends y {
191
298
  constructor() {
192
299
  super(...arguments);
193
- d(this, "_text", "");
194
- d(this, "_isSubmitting", !1);
300
+ c(this, "_text", "");
301
+ c(this, "_isSubmitting", !1);
195
302
  }
196
303
  get text() {
197
304
  return this._text;
@@ -205,21 +312,21 @@ class H extends m {
205
312
  isSubmitting: this._isSubmitting
206
313
  };
207
314
  }
208
- setText(t) {
209
- this._text = t, this.notifySubscribers();
315
+ setText(e) {
316
+ this._text = e, this.notifySubscribers();
210
317
  }
211
318
  clear() {
212
319
  this._text = "", this.notifySubscribers();
213
320
  }
214
- setSubmitting(t) {
215
- this._isSubmitting = t, this.notifySubscribers();
321
+ setSubmitting(e) {
322
+ this._isSubmitting = e, this.notifySubscribers();
216
323
  }
217
324
  }
218
- class T extends m {
219
- constructor(t) {
325
+ class C extends y {
326
+ constructor(e) {
220
327
  super();
221
- d(this, "composer");
222
- this._core = t, this.composer = new H(), this._core.subscribe(() => {
328
+ c(this, "composer");
329
+ this._core = e, this.composer = new X(), this._core.subscribe(() => {
223
330
  this.notifySubscribers();
224
331
  });
225
332
  }
@@ -244,16 +351,16 @@ class T extends m {
244
351
  capabilities: this._core.capabilities
245
352
  };
246
353
  }
247
- append(t) {
248
- this._core.append(t);
354
+ append(e) {
355
+ this._core.append(e);
249
356
  }
250
- async send(t) {
251
- if (t.trim()) {
357
+ async send(e) {
358
+ if (e.trim()) {
252
359
  this.composer.setSubmitting(!0);
253
360
  try {
254
361
  this.composer.clear(), await this._core.startRun({
255
362
  role: "user",
256
- content: [{ type: "text", data: t }]
363
+ content: [{ type: "text", data: e }]
257
364
  });
258
365
  } catch {
259
366
  } finally {
@@ -267,23 +374,23 @@ class T extends m {
267
374
  clear() {
268
375
  this._core.clear();
269
376
  }
270
- reset(t = []) {
271
- this._core.reset(t);
377
+ reset(e = []) {
378
+ this._core.reset(e);
272
379
  }
273
- subscribe(t) {
274
- return super.subscribe(t);
380
+ subscribe(e) {
381
+ return super.subscribe(e);
275
382
  }
276
383
  }
277
- class x extends m {
278
- constructor(t) {
384
+ class E extends y {
385
+ constructor(e) {
279
386
  super();
280
- d(this, "_messages", []);
281
- d(this, "_isRunning", !1);
282
- d(this, "_isDisabled", !1);
283
- d(this, "_abortController", null);
387
+ c(this, "_messages", []);
388
+ c(this, "_isRunning", !1);
389
+ c(this, "_isDisabled", !1);
390
+ c(this, "_abortController", null);
284
391
  // Track current part being accumulated
285
- d(this, "_currentPart", null);
286
- this.config = t, t.initialMessages && (this._messages = [...t.initialMessages]);
392
+ c(this, "_currentPart", null);
393
+ this.config = e, e.initialMessages && (this._messages = [...e.initialMessages]);
287
394
  }
288
395
  get messages() {
289
396
  return this._messages;
@@ -304,133 +411,158 @@ class x extends m {
304
411
  feedback: !1
305
412
  };
306
413
  }
307
- append(t) {
308
- var r, s;
309
- const e = {
310
- id: t.id || E(),
311
- parentId: t.parentId,
312
- role: t.role,
313
- content: t.content,
314
- status: t.status || { type: "complete" },
315
- timestamp: t.timestamp || Date.now(),
316
- metadata: t.metadata
414
+ updateMessages(e) {
415
+ var t, n;
416
+ this._messages = e, (n = (t = this.config).onMessagesChange) == null || n.call(t, this._messages), this.notifySubscribers();
417
+ }
418
+ append(e) {
419
+ const t = {
420
+ id: e.id || P(),
421
+ parentId: e.parentId,
422
+ role: e.role,
423
+ content: e.content,
424
+ status: e.status || { type: "complete" },
425
+ timestamp: e.timestamp || Date.now(),
426
+ metadata: e.metadata
317
427
  };
318
- this._messages.push(e), (s = (r = this.config).onMessagesChange) == null || s.call(r, this._messages), this.notifySubscribers();
428
+ this.updateMessages([...this._messages, t]);
319
429
  }
320
- async startRun(t) {
321
- var s, a;
430
+ async startRun(e) {
322
431
  if (this._isRunning)
323
432
  throw new Error("A run is already in progress");
324
- this.append(t), this._isRunning = !0, this._abortController = new AbortController(), this._currentPart = null, this.notifySubscribers();
325
- const e = E(), r = {
326
- id: e,
433
+ this.append(e), this._isRunning = !0, this._abortController = new AbortController(), this._currentPart = null;
434
+ const t = P(), n = {
435
+ id: t,
327
436
  role: "assistant",
328
437
  content: [],
329
438
  status: { type: "running" },
330
439
  timestamp: Date.now()
331
440
  };
332
- this._messages.push(r), (a = (s = this.config).onMessagesChange) == null || a.call(s, this._messages), this.notifySubscribers();
441
+ this.updateMessages([...this._messages, n]);
333
442
  try {
334
- const o = this.config.streamAdapter.stream({
443
+ const r = this.config.streamAdapter.stream({
335
444
  messages: this._messages,
336
445
  signal: this._abortController.signal
337
446
  });
338
- for await (const u of o) {
447
+ for await (const a of r) {
339
448
  if (this._abortController.signal.aborted)
340
449
  break;
341
- this.handleStreamEvent(e, u.event);
450
+ this.handleStreamEvent(t, a.event);
342
451
  }
343
- this.updateMessageStatus(e, { type: "complete" });
344
- } catch (o) {
345
- if (o instanceof Error && o.name === "AbortError")
346
- this.updateMessageStatus(e, { type: "cancelled" });
452
+ this.updateMessageStatus(t, { type: "complete" });
453
+ } catch (r) {
454
+ if (r instanceof Error && r.name === "AbortError")
455
+ this.updateMessageStatus(t, { type: "cancelled" });
347
456
  else {
348
- const u = o instanceof Error ? o.message : "Unknown error";
349
- this.updateMessageStatus(e, {
457
+ const a = r instanceof Error ? r.message : "Unknown error";
458
+ this.updateMessageStatus(t, {
350
459
  type: "error",
351
- error: u
460
+ error: a
352
461
  });
353
462
  }
354
463
  } finally {
355
464
  this._isRunning = !1, this._abortController = null, this._currentPart = null, this.notifySubscribers();
356
465
  }
357
466
  }
358
- handleStreamEvent(t, e) {
359
- var a, o;
360
- const r = this._messages.findIndex((u) => u.id === t);
361
- if (r === -1) return;
362
- const s = this._messages[r];
363
- if (e.type === "part-start")
364
- this.handlePartStart(s, e);
365
- else if (e.type === "text-delta")
366
- this.handleTextDelta(s, e);
367
- else if (e.type === "part-finish")
467
+ handleStreamEvent(e, t) {
468
+ const n = this._messages.findIndex((o) => o.id === e);
469
+ if (n === -1) return;
470
+ const r = this._messages[n];
471
+ if (t.type === "part-start")
472
+ this.handlePartStart(r, t);
473
+ else if (t.type === "text-delta" || t.type === "assistant_thought")
474
+ this.handleTextDelta(r, t);
475
+ else if (t.type === "part-finish")
368
476
  this.handlePartFinish();
369
- else if (e.type === "metadata") {
370
- const u = e;
371
- s.metadata = {
372
- ...s.metadata,
373
- conversationId: u.conversationId,
374
- interactionId: u.interactionId
477
+ else if (t.type === "metadata") {
478
+ const o = t;
479
+ r.metadata = {
480
+ ...r.metadata,
481
+ conversationId: o.conversationId,
482
+ interactionId: o.interactionId
375
483
  };
376
- } else if (e.type === "error") {
377
- const u = e;
378
- s.content.push({
379
- type: "error",
380
- data: u.error
381
- });
484
+ } else if (t.type === "capability_execution") {
485
+ const o = t;
486
+ r.content = [
487
+ ...r.content,
488
+ {
489
+ type: "capability",
490
+ capabilityId: o.capabilityId,
491
+ capabilityName: o.capabilityName,
492
+ args: o.args
493
+ }
494
+ ], this.config.capabilityExecutionManager && this.config.capabilityExecutionManager.executeCapability(
495
+ o.capabilityName,
496
+ o.capabilityId,
497
+ o.args,
498
+ e,
499
+ o.strategy
500
+ );
501
+ } else if (t.type === "error") {
502
+ const o = t;
503
+ r.content = [
504
+ ...r.content,
505
+ {
506
+ type: "error",
507
+ data: o.error
508
+ }
509
+ ];
382
510
  } else {
383
- const u = e;
384
- s.content.push({
385
- type: u.type,
386
- data: u.data,
387
- parentId: u.parentId
388
- });
511
+ const o = t;
512
+ r.content = [
513
+ ...r.content,
514
+ {
515
+ type: o.type,
516
+ data: o.data,
517
+ parentId: o.parentId
518
+ }
519
+ ];
389
520
  }
390
- this._messages = [
391
- ...this._messages.slice(0, r),
392
- { ...s, timestamp: Date.now() },
393
- ...this._messages.slice(r + 1)
394
- ], (o = (a = this.config).onMessagesChange) == null || o.call(a, this._messages), this.notifySubscribers();
395
- }
396
- handlePartStart(t, e) {
397
- const r = t.content.length;
398
- let s;
399
- switch (e.part.type) {
521
+ const a = [
522
+ ...this._messages.slice(0, n),
523
+ { ...r, timestamp: Date.now() },
524
+ ...this._messages.slice(n + 1)
525
+ ];
526
+ this.updateMessages(a);
527
+ }
528
+ handlePartStart(e, t) {
529
+ const n = e.content.length;
530
+ let r;
531
+ switch (t.part.type) {
400
532
  case "assistant_thought":
401
- s = {
402
- type: e.part.type,
533
+ r = {
534
+ type: t.part.type,
403
535
  data: [],
404
- parentId: e.part.parentId
536
+ parentId: t.part.parentId
405
537
  };
406
538
  break;
407
539
  case "text":
408
- s = {
409
- type: e.part.type,
540
+ r = {
541
+ type: t.part.type,
410
542
  data: "",
411
- parentId: e.part.parentId
543
+ parentId: t.part.parentId
412
544
  };
413
545
  break;
414
546
  default:
415
- s = {
416
- type: e.part.type,
417
- parentId: e.part.parentId
547
+ r = {
548
+ type: t.part.type,
549
+ parentId: t.part.parentId
418
550
  };
419
551
  }
420
- t.content.push(s), this._currentPart = {
421
- messageId: t.id,
422
- contentIndex: r,
423
- type: e.part.type,
424
- parentId: e.part.parentId
552
+ e.content = [...e.content, r], this._currentPart = {
553
+ messageId: e.id,
554
+ contentIndex: n,
555
+ type: t.part.type,
556
+ parentId: t.part.parentId
425
557
  };
426
558
  }
427
- handleTextDelta(t, e) {
559
+ handleTextDelta(e, t) {
428
560
  if (!this._currentPart) {
429
561
  console.warn("Received text-delta without part-start");
430
562
  return;
431
563
  }
432
- const r = t.content[this._currentPart.contentIndex];
433
- r.type === "assistant_thought" ? (Array.isArray(r.data) || (r.data = []), e.delta.trim() && r.data.push(e.delta.trim())) : r.data = (r.data || "") + e.delta;
564
+ const n = e.content[this._currentPart.contentIndex];
565
+ n.type === "assistant_thought" ? (Array.isArray(n.data) || (n.data = []), t.delta.trim() && n.data.push(t.delta.trim())) : n.data = (n.data || "") + t.delta;
434
566
  }
435
567
  handlePartFinish() {
436
568
  if (!this._currentPart) {
@@ -439,41 +571,45 @@ class x extends m {
439
571
  }
440
572
  this._currentPart = null;
441
573
  }
442
- updateMessageStatus(t, e) {
443
- var s, a;
444
- const r = this._messages.findIndex((o) => o.id === t);
445
- r !== -1 && (this._messages[r] = {
446
- ...this._messages[r],
447
- status: e
448
- }, (a = (s = this.config).onMessagesChange) == null || a.call(s, this._messages), this.notifySubscribers());
574
+ updateMessageStatus(e, t) {
575
+ const n = this._messages.findIndex((a) => a.id === e);
576
+ if (n === -1) return;
577
+ const r = [
578
+ ...this._messages.slice(0, n),
579
+ {
580
+ ...this._messages[n],
581
+ status: t
582
+ },
583
+ ...this._messages.slice(n + 1)
584
+ ];
585
+ this.updateMessages(r);
449
586
  }
450
587
  clear() {
451
- var t, e;
452
- this._messages = [], (e = (t = this.config).onMessagesChange) == null || e.call(t, this._messages), this.notifySubscribers();
588
+ this.updateMessages([]);
453
589
  }
454
590
  cancelRun() {
455
591
  !this._isRunning || !this._abortController || this._abortController.abort();
456
592
  }
457
- reset(t = []) {
458
- var e, r;
459
- this._messages = [...t], (r = (e = this.config).onMessagesChange) == null || r.call(e, this._messages), this.notifySubscribers();
593
+ reset(e = []) {
594
+ this.updateMessages([...e]);
460
595
  }
461
596
  }
462
- class K extends m {
463
- constructor(t) {
597
+ class Z extends y {
598
+ constructor(e) {
464
599
  super();
465
- d(this, "_mainThreadId");
466
- d(this, "_threads", /* @__PURE__ */ new Map());
467
- d(this, "_threadItems", /* @__PURE__ */ new Map());
468
- d(this, "_threadStates", /* @__PURE__ */ new Map());
469
- d(this, "_isLoading", !1);
470
- d(this, "main");
471
- this.config = t, this._mainThreadId = M();
472
- const e = new x({
473
- streamAdapter: t.streamAdapter
600
+ c(this, "_mainThreadId");
601
+ c(this, "_threads", /* @__PURE__ */ new Map());
602
+ c(this, "_threadItems", /* @__PURE__ */ new Map());
603
+ c(this, "_threadStates", /* @__PURE__ */ new Map());
604
+ c(this, "_isLoading", !1);
605
+ c(this, "main");
606
+ this.config = e, this._mainThreadId = F();
607
+ const t = new E({
608
+ streamAdapter: e.streamAdapter,
609
+ capabilityExecutionManager: e.capabilityExecutionManager
474
610
  });
475
- this.main = new T(e), this._threads.set(this._mainThreadId, this.main);
476
- const r = {
611
+ this.main = new C(t), this._threads.set(this._mainThreadId, this.main);
612
+ const n = {
477
613
  id: this._mainThreadId,
478
614
  title: "New Chat",
479
615
  status: { type: "regular" },
@@ -481,14 +617,14 @@ class K extends m {
481
617
  createdAt: Date.now(),
482
618
  updatedAt: Date.now()
483
619
  };
484
- this._threadStates.set(this._mainThreadId, r);
485
- const s = new w({
486
- state: r,
620
+ this._threadStates.set(this._mainThreadId, n);
621
+ const r = new R({
622
+ state: n,
487
623
  onSwitchTo: this.switchToThread.bind(this),
488
624
  onRename: this.renameThread.bind(this),
489
625
  onDelete: this.deleteThread.bind(this)
490
626
  });
491
- this._threadItems.set(this._mainThreadId, s);
627
+ this._threadItems.set(this._mainThreadId, r);
492
628
  }
493
629
  get isLoading() {
494
630
  return this._isLoading;
@@ -497,33 +633,33 @@ class K extends m {
497
633
  * Get the currently active main thread
498
634
  */
499
635
  getMainThread() {
500
- const t = this._threads.get(this._mainThreadId);
501
- return t || this.main;
636
+ const e = this._threads.get(this._mainThreadId);
637
+ return e || this.main;
502
638
  }
503
639
  getState() {
504
- const t = [], e = {};
505
- for (const [r, s] of this._threadStates)
506
- e[r] = s, s.status.type === "regular" && t.push(r);
640
+ const e = [], t = {};
641
+ for (const [n, r] of this._threadStates)
642
+ t[n] = r, r.status.type === "regular" && e.push(n);
507
643
  return {
508
644
  mainThreadId: this._mainThreadId,
509
- threads: t,
645
+ threads: e,
510
646
  isLoading: this._isLoading,
511
- threadItems: e
647
+ threadItems: t
512
648
  };
513
649
  }
514
- async switchToThread(t) {
650
+ async switchToThread(e) {
515
651
  var a, o;
516
- const e = this._threads.get(t);
517
- if (!e)
518
- throw new Error(`Thread ${t} not found`);
519
- const r = this._threadStates.get(this._mainThreadId);
520
- r && (r.isMain = !1, this._threadStates.set(this._mainThreadId, r), (a = this._threadItems.get(this._mainThreadId)) == null || a.updateState(r)), this._mainThreadId = t;
521
- const s = this._threadStates.get(t);
522
- if (s && (s.isMain = !0, this._threadStates.set(t, s), (o = this._threadItems.get(t)) == null || o.updateState(s)), this.config.threadListAdapter && (s != null && s.conversationId)) {
652
+ const t = this._threads.get(e);
653
+ if (!t)
654
+ throw new Error(`Thread ${e} not found`);
655
+ const n = this._threadStates.get(this._mainThreadId);
656
+ n && (n.isMain = !1, this._threadStates.set(this._mainThreadId, n), (a = this._threadItems.get(this._mainThreadId)) == null || a.updateState(n)), this._mainThreadId = e;
657
+ const r = this._threadStates.get(e);
658
+ if (r && (r.isMain = !0, this._threadStates.set(e, r), (o = this._threadItems.get(e)) == null || o.updateState(r)), this.config.threadListAdapter && (r != null && r.conversationId)) {
523
659
  this._isLoading = !0, this.notifySubscribers();
524
660
  try {
525
- const u = await this.config.threadListAdapter.loadThread(t);
526
- e.reset(u);
661
+ const u = await this.config.threadListAdapter.loadThread(e);
662
+ t.reset(u);
527
663
  } catch (u) {
528
664
  console.error("Failed to load thread:", u);
529
665
  } finally {
@@ -533,35 +669,36 @@ class K extends m {
533
669
  this.notifySubscribers();
534
670
  }
535
671
  async switchToNewThread() {
536
- const t = M(), e = new x({
537
- streamAdapter: this.config.streamAdapter
538
- }), r = new T(e);
539
- this._threads.set(t, r);
540
- const s = {
541
- id: t,
672
+ const e = F(), t = new E({
673
+ streamAdapter: this.config.streamAdapter,
674
+ capabilityExecutionManager: this.config.capabilityExecutionManager
675
+ }), n = new C(t);
676
+ this._threads.set(e, n);
677
+ const r = {
678
+ id: e,
542
679
  title: "New Chat",
543
680
  status: { type: "regular" },
544
681
  isMain: !1,
545
682
  createdAt: Date.now(),
546
683
  updatedAt: Date.now()
547
684
  };
548
- this._threadStates.set(t, s);
549
- const a = new w({
550
- state: s,
685
+ this._threadStates.set(e, r);
686
+ const a = new R({
687
+ state: r,
551
688
  onSwitchTo: this.switchToThread.bind(this),
552
689
  onRename: this.renameThread.bind(this),
553
690
  onDelete: this.deleteThread.bind(this)
554
691
  });
555
- if (this._threadItems.set(t, a), r.subscribe(() => {
692
+ if (this._threadItems.set(e, a), n.subscribe(() => {
556
693
  this.notifySubscribers();
557
694
  }), this.config.threadListAdapter)
558
695
  try {
559
696
  const o = await this.config.threadListAdapter.createThread();
560
- s.conversationId = o.conversationId, this._threadStates.set(t, s), a.updateState(s);
697
+ r.conversationId = o.conversationId, this._threadStates.set(e, r), a.updateState(r);
561
698
  } catch (o) {
562
699
  console.error("Failed to create thread:", o);
563
700
  }
564
- await this.switchToThread(t);
701
+ await this.switchToThread(e);
565
702
  }
566
703
  async loadThreads() {
567
704
  if (!this.config.threadListAdapter) {
@@ -570,72 +707,75 @@ class K extends m {
570
707
  }
571
708
  this._isLoading = !0, this.notifySubscribers();
572
709
  try {
573
- const t = await this.config.threadListAdapter.loadThreads();
574
- for (const e of t) {
575
- if (this._threadStates.has(e.id))
710
+ const e = await this.config.threadListAdapter.loadThreads();
711
+ for (const t of e) {
712
+ if (this._threadStates.has(t.id))
576
713
  continue;
577
- const r = new x({
714
+ const n = new E({
578
715
  streamAdapter: this.config.streamAdapter
579
- }), s = new T(r);
580
- this._threads.set(e.id, s), this._threadStates.set(e.id, e);
581
- const a = new w({
582
- state: e,
716
+ }), r = new C(n);
717
+ this._threads.set(t.id, r), this._threadStates.set(t.id, t);
718
+ const a = new R({
719
+ state: t,
583
720
  onSwitchTo: this.switchToThread.bind(this),
584
721
  onRename: this.renameThread.bind(this),
585
722
  onDelete: this.deleteThread.bind(this)
586
723
  });
587
- this._threadItems.set(e.id, a), s.subscribe(() => {
724
+ this._threadItems.set(t.id, a), r.subscribe(() => {
588
725
  this.notifySubscribers();
589
726
  });
590
727
  }
591
- } catch (t) {
592
- console.error("Failed to load threads:", t);
728
+ } catch (e) {
729
+ console.error("Failed to load threads:", e);
593
730
  } finally {
594
731
  this._isLoading = !1, this.notifySubscribers();
595
732
  }
596
733
  }
597
- async renameThread(t, e) {
598
- var s;
599
- const r = this._threadStates.get(t);
600
- if (r) {
601
- if (r.title = e, r.updatedAt = Date.now(), this._threadStates.set(t, r), (s = this._threadItems.get(t)) == null || s.updateState(r), this.config.threadListAdapter)
734
+ async renameThread(e, t) {
735
+ var r;
736
+ const n = this._threadStates.get(e);
737
+ if (n) {
738
+ if (n.title = t, n.updatedAt = Date.now(), this._threadStates.set(e, n), (r = this._threadItems.get(e)) == null || r.updateState(n), this.config.threadListAdapter)
602
739
  try {
603
- await this.config.threadListAdapter.updateThread(t, { title: e });
740
+ await this.config.threadListAdapter.updateThread(e, { title: t });
604
741
  } catch (a) {
605
742
  console.error("Failed to rename thread:", a);
606
743
  }
607
744
  this.notifySubscribers();
608
745
  }
609
746
  }
610
- async deleteThread(t) {
611
- if (t === this._mainThreadId && this._threads.size === 1)
747
+ async deleteThread(e) {
748
+ if (e === this._mainThreadId && this._threads.size === 1)
612
749
  throw new Error("Cannot delete the last thread");
613
- if (t === this._mainThreadId) {
614
- const e = Array.from(this._threads.keys()).find((r) => r !== t);
615
- e && await this.switchToThread(e);
750
+ if (e === this._mainThreadId) {
751
+ const t = Array.from(this._threads.keys()).find((n) => n !== e);
752
+ t && await this.switchToThread(t);
616
753
  }
617
- if (this._threads.delete(t), this._threadItems.delete(t), this._threadStates.delete(t), this.config.threadListAdapter)
754
+ if (this._threads.delete(e), this._threadItems.delete(e), this._threadStates.delete(e), this.config.threadListAdapter)
618
755
  try {
619
- await this.config.threadListAdapter.deleteThread(t);
620
- } catch (e) {
621
- console.error("Failed to delete thread:", e);
756
+ await this.config.threadListAdapter.deleteThread(e);
757
+ } catch (t) {
758
+ console.error("Failed to delete thread:", t);
622
759
  }
623
760
  this.notifySubscribers();
624
761
  }
625
762
  }
626
- class ht extends m {
627
- constructor(t) {
763
+ class pe extends y {
764
+ constructor(e) {
628
765
  super();
629
- d(this, "threads");
630
- d(this, "pluginRegistry");
631
- d(this, "_contentFocusRuntime");
632
- d(this, "_currentThreadUnsubscribe");
633
- this.pluginRegistry = new W(), t.plugins && t.plugins.forEach((e) => {
634
- this.pluginRegistry.registerPlugin(e);
635
- }), this.threads = new K({
636
- streamAdapter: t.streamAdapter,
637
- threadListAdapter: t.threadListAdapter
638
- }), this._contentFocusRuntime = new z(), this._contentFocusRuntime.subscribe(() => {
766
+ c(this, "threads");
767
+ c(this, "pluginRegistry");
768
+ c(this, "capabilityRegistry");
769
+ c(this, "capabilityExecutionManager");
770
+ c(this, "_contentFocusRuntime");
771
+ c(this, "_currentThreadUnsubscribe");
772
+ this.pluginRegistry = new W(), e.plugins && e.plugins.forEach((t) => {
773
+ this.pluginRegistry.registerPlugin(t);
774
+ }), this.capabilityRegistry = new z(), this.capabilityExecutionManager = new V((t) => this.capabilityRegistry.getHandler(t)), this.threads = new Z({
775
+ streamAdapter: e.streamAdapter,
776
+ threadListAdapter: e.threadListAdapter,
777
+ capabilityExecutionManager: this.capabilityExecutionManager
778
+ }), this._contentFocusRuntime = new G(), this._contentFocusRuntime.subscribe(() => {
639
779
  this.notifySubscribers();
640
780
  }), this.threads.subscribe(() => {
641
781
  this.subscribeToCurrentThread(), this.notifySubscribers();
@@ -650,9 +790,9 @@ class ht extends m {
650
790
  /**
651
791
  * Register a plugin
652
792
  */
653
- registerPlugin(t) {
654
- var e;
655
- this.pluginRegistry.registerPlugin(t), (e = t.init) == null || e.call(t, {
793
+ registerPlugin(e) {
794
+ var t;
795
+ this.pluginRegistry.registerPlugin(e), (t = e.init) == null || t.call(e, {
656
796
  streamingEnabled: !0,
657
797
  feedbackEnabled: !1,
658
798
  detailPanelEnabled: !0
@@ -661,8 +801,8 @@ class ht extends m {
661
801
  /**
662
802
  * Unregister a plugin
663
803
  */
664
- unregisterPlugin(t) {
665
- return this.pluginRegistry.unregisterPlugin(t);
804
+ unregisterPlugin(e) {
805
+ return this.pluginRegistry.unregisterPlugin(e);
666
806
  }
667
807
  /**
668
808
  * Subscribe to the current main thread
@@ -673,21 +813,21 @@ class ht extends m {
673
813
  this.handleMessagesChange(this.thread.messages), this.notifySubscribers();
674
814
  });
675
815
  }
676
- handleMessagesChange(t) {
677
- if (t.length === 0) return;
678
- const e = t[t.length - 1];
679
- e.role === "assistant" && e.status.type === "complete" && this.autoFocusLastContent(t);
680
- }
681
- autoFocusLastContent(t) {
682
- var e;
683
- if (t.length !== 0 && !this._contentFocusRuntime.isActive)
684
- for (let r = t.length - 1; r >= 0; r--) {
685
- const s = t[r];
686
- if (s.role === "assistant")
687
- for (let a = s.content.length - 1; a >= 0; a--) {
688
- const o = s.content[a], u = this.pluginRegistry.getRenderersByType(o.type);
689
- if (u.length > 0 && ((e = u[0].capabilities) != null && e.supportsFocus)) {
690
- this._contentFocusRuntime.focus(o, s, a, "detail");
816
+ handleMessagesChange(e) {
817
+ if (e.length === 0) return;
818
+ const t = e[e.length - 1];
819
+ t.role === "assistant" && t.status.type === "complete" && this.autoFocusLastContent(e);
820
+ }
821
+ autoFocusLastContent(e) {
822
+ var t;
823
+ if (e.length !== 0 && !this._contentFocusRuntime.isActive)
824
+ for (let n = e.length - 1; n >= 0; n--) {
825
+ const r = e[n];
826
+ if (r.role === "assistant")
827
+ for (let a = r.content.length - 1; a >= 0; a--) {
828
+ const o = r.content[a], u = this.pluginRegistry.getRenderersByType(o.type);
829
+ if (u.length > 0 && ((t = u[0].capabilities) != null && t.supportsFocus)) {
830
+ this._contentFocusRuntime.focus(o, r, a, "detail");
691
831
  return;
692
832
  }
693
833
  }
@@ -700,22 +840,22 @@ object-assign
700
840
  (c) Sindre Sorhus
701
841
  @license MIT
702
842
  */
703
- var R, A;
704
- function Y() {
705
- if (A) return R;
706
- A = 1;
707
- var n = Object.getOwnPropertySymbols, i = Object.prototype.hasOwnProperty, t = Object.prototype.propertyIsEnumerable;
708
- function e(s) {
709
- if (s == null)
843
+ var M, L;
844
+ function ee() {
845
+ if (L) return M;
846
+ L = 1;
847
+ var i = Object.getOwnPropertySymbols, s = Object.prototype.hasOwnProperty, e = Object.prototype.propertyIsEnumerable;
848
+ function t(r) {
849
+ if (r == null)
710
850
  throw new TypeError("Object.assign cannot be called with null or undefined");
711
- return Object(s);
851
+ return Object(r);
712
852
  }
713
- function r() {
853
+ function n() {
714
854
  try {
715
855
  if (!Object.assign)
716
856
  return !1;
717
- var s = new String("abc");
718
- if (s[5] = "de", Object.getOwnPropertyNames(s)[0] === "5")
857
+ var r = new String("abc");
858
+ if (r[5] = "de", Object.getOwnPropertyNames(r)[0] === "5")
719
859
  return !1;
720
860
  for (var a = {}, o = 0; o < 10; o++)
721
861
  a["_" + String.fromCharCode(o)] = o;
@@ -724,27 +864,27 @@ function Y() {
724
864
  });
725
865
  if (u.join("") !== "0123456789")
726
866
  return !1;
727
- var f = {};
867
+ var p = {};
728
868
  return "abcdefghijklmnopqrst".split("").forEach(function(l) {
729
- f[l] = l;
730
- }), Object.keys(Object.assign({}, f)).join("") === "abcdefghijklmnopqrst";
869
+ p[l] = l;
870
+ }), Object.keys(Object.assign({}, p)).join("") === "abcdefghijklmnopqrst";
731
871
  } catch {
732
872
  return !1;
733
873
  }
734
874
  }
735
- return R = r() ? Object.assign : function(s, a) {
736
- for (var o, u = e(s), f, l = 1; l < arguments.length; l++) {
875
+ return M = n() ? Object.assign : function(r, a) {
876
+ for (var o, u = t(r), p, l = 1; l < arguments.length; l++) {
737
877
  o = Object(arguments[l]);
738
- for (var c in o)
739
- i.call(o, c) && (u[c] = o[c]);
740
- if (n) {
741
- f = n(o);
742
- for (var h = 0; h < f.length; h++)
743
- t.call(o, f[h]) && (u[f[h]] = o[f[h]]);
878
+ for (var h in o)
879
+ s.call(o, h) && (u[h] = o[h]);
880
+ if (i) {
881
+ p = i(o);
882
+ for (var d = 0; d < p.length; d++)
883
+ e.call(o, p[d]) && (u[p[d]] = o[p[d]]);
744
884
  }
745
885
  }
746
886
  return u;
747
- }, R;
887
+ }, M;
748
888
  }
749
889
  /** @license React v17.0.2
750
890
  * react-jsx-runtime.production.min.js
@@ -754,72 +894,72 @@ function Y() {
754
894
  * This source code is licensed under the MIT license found in the
755
895
  * LICENSE file in the root directory of this source tree.
756
896
  */
757
- var P;
758
- function Q() {
759
- if (P) return _;
760
- P = 1, Y();
761
- var n = j, i = 60103;
897
+ var j;
898
+ function te() {
899
+ if (j) return _;
900
+ j = 1, ee();
901
+ var i = B, s = 60103;
762
902
  if (_.Fragment = 60107, typeof Symbol == "function" && Symbol.for) {
763
- var t = Symbol.for;
764
- i = t("react.element"), _.Fragment = t("react.fragment");
903
+ var e = Symbol.for;
904
+ s = e("react.element"), _.Fragment = e("react.fragment");
765
905
  }
766
- var e = n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, r = Object.prototype.hasOwnProperty, s = { key: !0, ref: !0, __self: !0, __source: !0 };
767
- function a(o, u, f) {
768
- var l, c = {}, h = null, p = null;
769
- f !== void 0 && (h = "" + f), u.key !== void 0 && (h = "" + u.key), u.ref !== void 0 && (p = u.ref);
770
- for (l in u) r.call(u, l) && !s.hasOwnProperty(l) && (c[l] = u[l]);
771
- if (o && o.defaultProps) for (l in u = o.defaultProps, u) c[l] === void 0 && (c[l] = u[l]);
772
- return { $$typeof: i, type: o, key: h, ref: p, props: c, _owner: e.current };
906
+ var t = i.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, n = Object.prototype.hasOwnProperty, r = { key: !0, ref: !0, __self: !0, __source: !0 };
907
+ function a(o, u, p) {
908
+ var l, h = {}, d = null, f = null;
909
+ p !== void 0 && (d = "" + p), u.key !== void 0 && (d = "" + u.key), u.ref !== void 0 && (f = u.ref);
910
+ for (l in u) n.call(u, l) && !r.hasOwnProperty(l) && (h[l] = u[l]);
911
+ if (o && o.defaultProps) for (l in u = o.defaultProps, u) h[l] === void 0 && (h[l] = u[l]);
912
+ return { $$typeof: s, type: o, key: d, ref: f, props: h, _owner: t.current };
773
913
  }
774
914
  return _.jsx = a, _.jsxs = a, _;
775
915
  }
776
- var F;
777
- function X() {
778
- return F || (F = 1, I.exports = Q()), I.exports;
916
+ var O;
917
+ function se() {
918
+ return O || (O = 1, I.exports = te()), I.exports;
779
919
  }
780
- var Z = X();
781
- const B = U(null);
782
- function dt({ runtime: n, children: i }) {
783
- return /* @__PURE__ */ Z.jsx(B.Provider, { value: n, children: i });
920
+ var S = se();
921
+ const k = H(null);
922
+ function fe({ runtime: i, children: s }) {
923
+ return /* @__PURE__ */ S.jsx(k.Provider, { value: i, children: s });
784
924
  }
785
- function tt() {
786
- const n = J(B);
787
- if (!n)
925
+ function re() {
926
+ const i = Q(k);
927
+ if (!i)
788
928
  throw new Error("useAssistantRuntimeContext must be used within AssistantRuntimeProvider");
789
- return n;
929
+ return i;
790
930
  }
791
- function y() {
792
- return tt();
931
+ function g() {
932
+ return re();
793
933
  }
794
- function lt() {
795
- const n = y();
934
+ function ge() {
935
+ const i = g();
796
936
  return {
797
- switchToThread: (i) => n.threads.switchToThread(i),
798
- switchToNewThread: () => n.threads.switchToNewThread(),
937
+ switchToThread: (s) => i.threads.switchToThread(s),
938
+ switchToNewThread: () => i.threads.switchToNewThread(),
799
939
  loadThreads: () => {
800
- var i, t;
801
- return (t = (i = n.threads).loadThreads) == null ? void 0 : t.call(i);
940
+ var s, e;
941
+ return (e = (s = i.threads).loadThreads) == null ? void 0 : e.call(s);
802
942
  },
803
- renameThread: (i, t) => {
804
- var e, r;
805
- return (r = (e = n.threads).renameThread) == null ? void 0 : r.call(e, i, t);
943
+ renameThread: (s, e) => {
944
+ var t, n;
945
+ return (n = (t = i.threads).renameThread) == null ? void 0 : n.call(t, s, e);
806
946
  },
807
- deleteThread: (i) => {
808
- var t, e;
809
- return (e = (t = n.threads).deleteThread) == null ? void 0 : e.call(t, i);
947
+ deleteThread: (s) => {
948
+ var e, t;
949
+ return (t = (e = i.threads).deleteThread) == null ? void 0 : t.call(e, s);
810
950
  }
811
951
  };
812
952
  }
813
- function ft() {
814
- const n = y(), [i, t] = D(n.threads.getState());
815
- return k(() => n.threads.subscribe(() => {
816
- t(n.threads.getState());
817
- }), [n]), i;
953
+ function be() {
954
+ const i = g(), [s, e] = x(i.threads.getState());
955
+ return w(() => i.threads.subscribe(() => {
956
+ e(i.threads.getState());
957
+ }), [i]), s;
818
958
  }
819
- function et() {
820
- return y().threads.getMainThread();
959
+ function ne() {
960
+ return g().threads.getMainThread();
821
961
  }
822
- var C = { exports: {} }, v = {};
962
+ var v = { exports: {} }, A = {};
823
963
  /**
824
964
  * @license React
825
965
  * use-sync-external-store-shim.production.js
@@ -829,77 +969,88 @@ var C = { exports: {} }, v = {};
829
969
  * This source code is licensed under the MIT license found in the
830
970
  * LICENSE file in the root directory of this source tree.
831
971
  */
832
- var L;
833
- function st() {
834
- if (L) return v;
835
- L = 1;
836
- var n = j;
837
- function i(c, h) {
838
- return c === h && (c !== 0 || 1 / c === 1 / h) || c !== c && h !== h;
839
- }
840
- var t = typeof Object.is == "function" ? Object.is : i, e = n.useState, r = n.useEffect, s = n.useLayoutEffect, a = n.useDebugValue;
841
- function o(c, h) {
842
- var p = h(), b = e({ inst: { value: p, getSnapshot: h } }), g = b[0].inst, S = b[1];
843
- return s(
972
+ var N;
973
+ function ie() {
974
+ if (N) return A;
975
+ N = 1;
976
+ var i = B;
977
+ function s(h, d) {
978
+ return h === d && (h !== 0 || 1 / h === 1 / d) || h !== h && d !== d;
979
+ }
980
+ var e = typeof Object.is == "function" ? Object.is : s, t = i.useState, n = i.useEffect, r = i.useLayoutEffect, a = i.useDebugValue;
981
+ function o(h, d) {
982
+ var f = d(), m = t({ inst: { value: f, getSnapshot: d } }), b = m[0].inst, T = m[1];
983
+ return r(
844
984
  function() {
845
- g.value = p, g.getSnapshot = h, u(g) && S({ inst: g });
985
+ b.value = f, b.getSnapshot = d, u(b) && T({ inst: b });
846
986
  },
847
- [c, p, h]
848
- ), r(
987
+ [h, f, d]
988
+ ), n(
849
989
  function() {
850
- return u(g) && S({ inst: g }), c(function() {
851
- u(g) && S({ inst: g });
990
+ return u(b) && T({ inst: b }), h(function() {
991
+ u(b) && T({ inst: b });
852
992
  });
853
993
  },
854
- [c]
855
- ), a(p), p;
994
+ [h]
995
+ ), a(f), f;
856
996
  }
857
- function u(c) {
858
- var h = c.getSnapshot;
859
- c = c.value;
997
+ function u(h) {
998
+ var d = h.getSnapshot;
999
+ h = h.value;
860
1000
  try {
861
- var p = h();
862
- return !t(c, p);
1001
+ var f = d();
1002
+ return !e(h, f);
863
1003
  } catch {
864
1004
  return !0;
865
1005
  }
866
1006
  }
867
- function f(c, h) {
868
- return h();
1007
+ function p(h, d) {
1008
+ return d();
869
1009
  }
870
- var l = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? f : o;
871
- return v.useSyncExternalStore = n.useSyncExternalStore !== void 0 ? n.useSyncExternalStore : l, v;
1010
+ var l = typeof window > "u" || typeof window.document > "u" || typeof window.document.createElement > "u" ? p : o;
1011
+ return A.useSyncExternalStore = i.useSyncExternalStore !== void 0 ? i.useSyncExternalStore : l, A;
872
1012
  }
873
- var O;
874
- function rt() {
875
- return O || (O = 1, C.exports = st()), C.exports;
1013
+ var D;
1014
+ function ae() {
1015
+ return D || (D = 1, v.exports = ie()), v.exports;
876
1016
  }
877
- var N = rt();
878
- function pt() {
879
- const n = y();
880
- return N.useSyncExternalStore(
881
- (i) => n.subscribe(() => {
882
- i();
1017
+ var oe = ae();
1018
+ function ye() {
1019
+ const i = g();
1020
+ return oe.useSyncExternalStore(
1021
+ (s) => i.subscribe(() => {
1022
+ s();
883
1023
  }),
884
- () => n.thread.messages,
885
- () => n.thread.messages
1024
+ () => i.thread.messages,
1025
+ () => i.thread.messages
886
1026
  );
887
1027
  }
888
- function nt() {
889
- return et().composer;
890
- }
891
- function gt() {
892
- const n = nt();
893
- return N.useSyncExternalStore(
894
- (i) => n.subscribe(i),
895
- () => n.getState(),
896
- () => n.getState()
897
- );
1028
+ function me() {
1029
+ const i = ne(), [s, e] = x(""), [t, n] = x(!1);
1030
+ return {
1031
+ text: s,
1032
+ setText: e,
1033
+ isSubmitting: t,
1034
+ send: async (a) => {
1035
+ if (a == null || a.preventDefault(), !s.trim() || t) return;
1036
+ const o = s;
1037
+ e(""), n(!0);
1038
+ try {
1039
+ await i.send(o);
1040
+ } catch (u) {
1041
+ console.error("Failed to send message:", u), e(o);
1042
+ } finally {
1043
+ n(!1);
1044
+ }
1045
+ },
1046
+ clear: () => e(""),
1047
+ append: (a) => e((o) => o + a)
1048
+ };
898
1049
  }
899
- function mt(n, i) {
900
- var s, a, o, u;
901
- const e = y().pluginRegistry.getBestRendererForType(n);
902
- if (!e)
1050
+ function q(i, s) {
1051
+ var r, a, o, u;
1052
+ const t = g().pluginRegistry.getBestRendererForType(i);
1053
+ if (!t)
903
1054
  return {
904
1055
  component: null,
905
1056
  auxiliaryComponent: null,
@@ -907,109 +1058,155 @@ function mt(n, i) {
907
1058
  supportsPreview: !1,
908
1059
  supportsFullscreen: !1
909
1060
  };
910
- const r = i ? (s = e.auxiliary) == null ? void 0 : s[i] : null;
1061
+ const n = s ? (r = t.auxiliary) == null ? void 0 : r[s] : null;
911
1062
  return {
912
- component: e.component,
913
- auxiliaryComponent: r,
914
- supportsFocus: ((a = e.capabilities) == null ? void 0 : a.supportsFocus) ?? !1,
915
- supportsPreview: ((o = e.capabilities) == null ? void 0 : o.supportsPreview) ?? !1,
916
- supportsFullscreen: ((u = e.capabilities) == null ? void 0 : u.supportsFullscreen) ?? !1
1063
+ component: t.component,
1064
+ auxiliaryComponent: n,
1065
+ supportsFocus: ((a = t.capabilities) == null ? void 0 : a.supportsFocus) ?? !1,
1066
+ supportsPreview: ((o = t.capabilities) == null ? void 0 : o.supportsPreview) ?? !1,
1067
+ supportsFullscreen: ((u = t.capabilities) == null ? void 0 : u.supportsFullscreen) ?? !1
917
1068
  };
918
1069
  }
919
- function bt() {
920
- const n = y(), [i, t] = D(n.contentFocus.state);
921
- return k(() => n.contentFocus.subscribe(() => {
922
- t(n.contentFocus.state);
923
- }), [n]), {
924
- ...i,
925
- focus: (e, r, s, a) => n.contentFocus.focus(e, r, s, a),
926
- blur: () => n.contentFocus.blur(),
927
- toggle: (e, r, s, a) => n.contentFocus.toggle(e, r, s, a),
928
- switchContext: (e) => n.contentFocus.switchContext(e),
929
- focusNext: () => n.contentFocus.focusNext(n.thread.messages),
930
- focusPrevious: () => n.contentFocus.focusPrevious(n.thread.messages)
1070
+ function _e() {
1071
+ const i = g(), [s, e] = x(i.contentFocus.state);
1072
+ return w(() => i.contentFocus.subscribe(() => {
1073
+ e(i.contentFocus.state);
1074
+ }), [i]), {
1075
+ ...s,
1076
+ focus: (t, n, r, a) => i.contentFocus.focus(t, n, r, a),
1077
+ blur: () => i.contentFocus.blur(),
1078
+ toggle: (t, n, r, a) => i.contentFocus.toggle(t, n, r, a),
1079
+ switchContext: (t) => i.contentFocus.switchContext(t),
1080
+ focusNext: () => i.contentFocus.focusNext(i.thread.messages),
1081
+ focusPrevious: () => i.contentFocus.focusPrevious(i.thread.messages)
931
1082
  };
932
1083
  }
933
- function _t(n) {
934
- const i = /* @__PURE__ */ new Map();
935
- for (let e = 0; e < n.length; e++) {
936
- const a = n[e].parentId ?? `__ungrouped_${e}`, o = i.get(a) ?? [];
937
- o.push(e), i.set(a, o);
938
- }
939
- const t = [];
940
- for (const [e, r] of i) {
941
- const s = e.startsWith("__ungrouped_") ? void 0 : e, a = r.map((f) => n[f]), o = it(a), u = at(a);
942
- t.push({ groupKey: s, indices: r, items: a, groupType: o, primaryType: u });
943
- }
944
- return t;
1084
+ function ue(i) {
1085
+ const s = g();
1086
+ w(() => (i.execute && s.capabilityRegistry.registerHandler(i.name, { execute: i.execute }), i.render && s.capabilityRegistry.registerRenderer(i.name, {
1087
+ component: i.render
1088
+ }), () => {
1089
+ s.capabilityRegistry.unregister(i.name);
1090
+ }), [i.name, s]);
945
1091
  }
946
- function it(n) {
947
- if (n.length === 1) return "single-type";
948
- if (new Set(n.map((t) => t.type)).size === 1) {
949
- const t = n[0].type;
950
- return t === "tool_call" ? "tool-calls" : t === "assistant_thought" ? "reasoning" : "single-type";
951
- }
952
- return "mixed";
1092
+ function ce(i) {
1093
+ const s = g(), [e, t] = x(
1094
+ () => s.capabilityExecutionManager.getExecution(i)
1095
+ ), [, n] = J((r) => r + 1, 0);
1096
+ return w(() => {
1097
+ const r = s.capabilityExecutionManager.getExecution(i);
1098
+ return t(r), s.capabilityExecutionManager.subscribe((o) => {
1099
+ if (o === i) {
1100
+ const u = s.capabilityExecutionManager.getExecution(i);
1101
+ t(u), n();
1102
+ }
1103
+ });
1104
+ }, [i, s]), e;
1105
+ }
1106
+ function he({ message: i, content: s }) {
1107
+ const e = g(), t = s, n = ce(t.capabilityId), r = e.capabilityRegistry.getRenderer(t.capabilityName);
1108
+ if (!r || !n)
1109
+ return null;
1110
+ const a = r.component;
1111
+ return /* @__PURE__ */ S.jsx(
1112
+ a,
1113
+ {
1114
+ capabilityName: n.name,
1115
+ capabilityId: n.id,
1116
+ args: n.args,
1117
+ result: n.result,
1118
+ status: n.status,
1119
+ message: i
1120
+ }
1121
+ );
1122
+ }
1123
+ function xe({ message: i, content: s }) {
1124
+ const { component: e } = q(s.type);
1125
+ if (s.type === "capability")
1126
+ return /* @__PURE__ */ S.jsx(he, { message: i, content: s });
1127
+ if (!e)
1128
+ return null;
1129
+ const t = e;
1130
+ return /* @__PURE__ */ S.jsx(t, { content: s, message: i });
1131
+ }
1132
+ function Se({
1133
+ message: i,
1134
+ content: s,
1135
+ context: e,
1136
+ onClose: t,
1137
+ onNavigate: n,
1138
+ onSwitchContext: r
1139
+ }) {
1140
+ const { auxiliaryComponent: a } = q(s.type, e);
1141
+ if (!a)
1142
+ return null;
1143
+ const o = a;
1144
+ return /* @__PURE__ */ S.jsx(
1145
+ o,
1146
+ {
1147
+ content: s,
1148
+ message: i,
1149
+ context: e,
1150
+ onClose: t,
1151
+ onNavigate: n,
1152
+ onSwitchContext: r
1153
+ }
1154
+ );
953
1155
  }
954
- function at(n) {
955
- const i = /* @__PURE__ */ new Map();
956
- for (const r of n)
957
- i.set(r.type, (i.get(r.type) || 0) + 1);
958
- let t = 0, e = n[0].type;
959
- for (const [r, s] of i)
960
- s > t && (t = s, e = r);
961
- return e;
1156
+ function we(i) {
1157
+ const s = () => (ue(i), null);
1158
+ return s.displayName = `Capability(${i.name})`, s;
962
1159
  }
963
- class yt {
1160
+ class Te {
964
1161
  getAllowedEvents() {
965
1162
  return null;
966
1163
  }
967
- shouldProcessEvent(i) {
968
- const t = this.getAllowedEvents();
969
- return t ? t.includes(i) : !0;
1164
+ shouldProcessEvent(s) {
1165
+ const e = this.getAllowedEvents();
1166
+ return e ? e.includes(s) : !0;
970
1167
  }
971
- async *stream(i) {
972
- const { signal: t } = i, { url: e, options: r } = this.prepareRequest(i), s = await fetch(e, { ...r, signal: t });
973
- if (!s.ok)
974
- throw new Error(`HTTP error! status: ${s.status}`);
975
- if (!s.body)
1168
+ async *stream(s) {
1169
+ const { signal: e } = s, { url: t, options: n } = this.prepareRequest(s), r = await fetch(t, { ...n, signal: e });
1170
+ if (!r.ok)
1171
+ throw new Error(`HTTP error! status: ${r.status}`);
1172
+ if (!r.body)
976
1173
  throw new Error("Response body is null");
977
- yield* this.parseSSEStream(s.body, t);
1174
+ yield* this.parseSSEStream(r.body, e);
978
1175
  }
979
- async *parseSSEStream(i, t) {
980
- const e = i.getReader(), r = new TextDecoder();
981
- let s = "", a = null;
1176
+ async *parseSSEStream(s, e) {
1177
+ const t = s.getReader(), n = new TextDecoder();
1178
+ let r = "", a = null;
982
1179
  try {
983
1180
  for (; ; ) {
984
- const { done: o, value: u } = await e.read();
985
- if (o || t != null && t.aborted) break;
986
- s += r.decode(u, { stream: !0 });
987
- const f = s.split(`
1181
+ const { done: o, value: u } = await t.read();
1182
+ if (o || e != null && e.aborted) break;
1183
+ r += n.decode(u, { stream: !0 });
1184
+ const p = r.split(`
988
1185
  `);
989
- s = f.pop() || "";
990
- for (const l of f) {
991
- const c = l.trim();
992
- if (!c) {
1186
+ r = p.pop() || "";
1187
+ for (const l of p) {
1188
+ const h = l.trim();
1189
+ if (!h) {
993
1190
  a = null;
994
1191
  continue;
995
1192
  }
996
- if (c !== ": ping") {
997
- if (c.startsWith("event:")) {
998
- a = c.substring(6).trim();
1193
+ if (h !== ": ping") {
1194
+ if (h.startsWith("event:")) {
1195
+ a = h.substring(6).trim();
999
1196
  continue;
1000
1197
  }
1001
- if (c.startsWith("data:")) {
1002
- const h = c.substring(5).trim();
1003
- if (h === "eof" || c === "eof")
1198
+ if (h.startsWith("data:")) {
1199
+ const d = h.substring(5).trim();
1200
+ if (d === "eof" || h === "eof")
1004
1201
  break;
1005
1202
  try {
1006
- const p = JSON.parse(h);
1203
+ const f = JSON.parse(d);
1007
1204
  if (a && this.shouldProcessEvent(a)) {
1008
- const b = this.convertEvent({
1205
+ const m = this.convertEvent({
1009
1206
  event: a,
1010
- data: p
1207
+ data: f
1011
1208
  });
1012
- b && (yield b);
1209
+ m && (yield m);
1013
1210
  }
1014
1211
  } catch {
1015
1212
  }
@@ -1019,33 +1216,35 @@ class yt {
1019
1216
  }
1020
1217
  }
1021
1218
  } finally {
1022
- e.releaseLock();
1219
+ t.releaseLock();
1023
1220
  }
1024
1221
  }
1025
1222
  }
1026
1223
  export {
1027
- ht as AssistantRuntime,
1028
- dt as AssistantRuntimeProvider,
1029
- yt as BaseSSEStreamAdapter,
1030
- m as BaseSubscribable,
1031
- H as ComposerRuntime,
1224
+ pe as AssistantRuntime,
1225
+ fe as AssistantRuntimeProvider,
1226
+ Se as AuxiliaryRenderer,
1227
+ Te as BaseSSEStreamAdapter,
1228
+ V as CapabilityExecutionManager,
1229
+ z as CapabilityRegistry,
1230
+ he as CapabilityRendererComp,
1231
+ X as ComposerRuntime,
1232
+ xe as ContentRenderer,
1032
1233
  W as PluginRegistry,
1033
- w as ThreadListItemRuntime,
1034
- K as ThreadListRuntime,
1035
- T as ThreadRuntime,
1036
- x as ThreadRuntimeCore,
1037
- ct as generateId,
1038
- E as generateMessageId,
1039
- M as generateThreadId,
1040
- _t as groupContentByParentId,
1041
- y as useAssistantRuntime,
1042
- nt as useComposer,
1043
- gt as useComposerState,
1044
- bt as useContentFocus,
1045
- mt as useContentRenderer,
1046
- et as useCurrentThread,
1047
- pt as useMessages,
1048
- lt as useThreadList,
1049
- ft as useThreadListState
1234
+ R as ThreadListItemRuntime,
1235
+ Z as ThreadListRuntime,
1236
+ C as ThreadRuntime,
1237
+ E as ThreadRuntimeCore,
1238
+ we as makeCapability,
1239
+ g as useAssistantRuntime,
1240
+ ue as useCapability,
1241
+ ce as useCapabilityExecution,
1242
+ me as useComposer,
1243
+ _e as useContentFocus,
1244
+ q as useContentRenderer,
1245
+ ne as useCurrentThread,
1246
+ ye as useMessages,
1247
+ ge as useThreadList,
1248
+ be as useThreadListState
1050
1249
  };
1051
1250
  //# sourceMappingURL=index.js.map