@applica-software-guru/persona-sdk 0.1.67 → 0.1.69

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/bundle.es.js CHANGED
@@ -1,9 +1,9 @@
1
- var B = Object.defineProperty;
2
- var V = (o, t, e) => t in o ? B(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
- var r = (o, t, e) => V(o, typeof t != "symbol" ? t + "" : t, e);
4
- import { createContext as Y, useContext as A, useState as E, useRef as G, useMemo as H, useEffect as Q, useCallback as N } from "react";
5
- import { useExternalStoreRuntime as X, CompositeAttachmentAdapter as Z, SimpleImageAttachmentAdapter as ee, AssistantRuntimeProvider as te } from "@assistant-ui/react";
6
- var F = { exports: {} }, k = {};
1
+ var V = Object.defineProperty;
2
+ var Y = (o, t, e) => t in o ? V(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
+ var i = (o, t, e) => Y(o, typeof t != "symbol" ? t + "" : t, e);
4
+ import { createContext as G, useContext as v, useState as E, useRef as H, useMemo as Q, useEffect as X, useCallback as M } from "react";
5
+ import { useExternalStoreRuntime as Z, CompositeAttachmentAdapter as ee, SimpleImageAttachmentAdapter as te, AssistantRuntimeProvider as se } from "@assistant-ui/react";
6
+ var W = { exports: {} }, k = {};
7
7
  /**
8
8
  * @license React
9
9
  * react-jsx-runtime.production.js
@@ -14,13 +14,13 @@ var F = { exports: {} }, k = {};
14
14
  * LICENSE file in the root directory of this source tree.
15
15
  */
16
16
  var O;
17
- function se() {
17
+ function ne() {
18
18
  if (O) return k;
19
19
  O = 1;
20
20
  var o = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
21
21
  function e(s, n, a) {
22
- var i = null;
23
- if (a !== void 0 && (i = "" + a), n.key !== void 0 && (i = "" + n.key), "key" in n) {
22
+ var r = null;
23
+ if (a !== void 0 && (r = "" + a), n.key !== void 0 && (r = "" + n.key), "key" in n) {
24
24
  a = {};
25
25
  for (var c in n)
26
26
  c !== "key" && (a[c] = n[c]);
@@ -28,16 +28,16 @@ function se() {
28
28
  return n = a.ref, {
29
29
  $$typeof: o,
30
30
  type: s,
31
- key: i,
31
+ key: r,
32
32
  ref: n !== void 0 ? n : null,
33
33
  props: a
34
34
  };
35
35
  }
36
36
  return k.Fragment = t, k.jsx = e, k.jsxs = e, k;
37
37
  }
38
- F.exports = se();
39
- var M = F.exports;
40
- function ne(o) {
38
+ W.exports = ne();
39
+ var I = W.exports;
40
+ function oe(o) {
41
41
  return o.filter((t) => {
42
42
  var e;
43
43
  return t.finishReason === "stop" ? t.text !== null && ((e = t.text) == null ? void 0 : e.trim()) !== "" : !0;
@@ -53,9 +53,9 @@ function j(o) {
53
53
  } : e && n.protocol === e.protocol && (e.role === n.role || n.finishReason === "stop") ? (e.text += n.text, e.files = [...e.files ?? [], ...n.files ?? []]) : (e && t.push(e), e = {
54
54
  ...n
55
55
  }));
56
- return e && t.push(e), ne(t);
56
+ return e && t.push(e), oe(t);
57
57
  }
58
- function oe(o) {
58
+ function ae(o) {
59
59
  var e, s;
60
60
  const t = ((e = o.files) == null ? void 0 : e.map(
61
61
  (n) => ({
@@ -84,10 +84,10 @@ function oe(o) {
84
84
  content: o.type === "reasoning" ? [{ type: "reasoning", text: o.text }, ...t] : [{ type: "text", text: o.text }, ...t]
85
85
  };
86
86
  }
87
- class v {
87
+ class $ {
88
88
  constructor() {
89
- r(this, "statusChangeCallbacks", []);
90
- r(this, "messageCallbacks", []);
89
+ i(this, "statusChangeCallbacks", []);
90
+ i(this, "messageCallbacks", []);
91
91
  }
92
92
  addStatusChangeListener(t) {
93
93
  this.statusChangeCallbacks.push(t);
@@ -119,16 +119,16 @@ class v {
119
119
  onTransaction(t) {
120
120
  }
121
121
  }
122
- class _ extends v {
122
+ class _ extends $ {
123
123
  constructor(e) {
124
124
  super();
125
- r(this, "status");
126
- r(this, "autostart");
127
- r(this, "session");
128
- r(this, "config");
129
- r(this, "notify", !0);
130
- r(this, "context", {});
131
- r(this, "tools", []);
125
+ i(this, "status");
126
+ i(this, "autostart");
127
+ i(this, "session");
128
+ i(this, "config");
129
+ i(this, "notify", !0);
130
+ i(this, "context", {});
131
+ i(this, "tools", []);
132
132
  this.config = e, this.status = "disconnected", this.autostart = !0;
133
133
  }
134
134
  getName() {
@@ -147,13 +147,14 @@ class _ extends v {
147
147
  this.session = e;
148
148
  }
149
149
  async sendPacket(e) {
150
- var u, d, g, w;
151
- const { apiUrl: s, apiKey: n, agentId: a } = this.config, i = this.session ?? "new";
152
- if (e.type === "command" && ((u = e == null ? void 0 : e.payload) == null ? void 0 : u.command) == "set_initial_context") {
153
- this.context = (d = e == null ? void 0 : e.payload) == null ? void 0 : d.arguments;
150
+ var l, p, g, C, A;
151
+ const { apiUrl: s, apiKey: n, agentId: a } = this.config, r = this.session ?? "new";
152
+ if (e.type === "command" && ((l = e == null ? void 0 : e.payload) == null ? void 0 : l.command) == "set_initial_context") {
153
+ this.context = (p = e == null ? void 0 : e.payload) == null ? void 0 : p.arguments;
154
154
  return;
155
155
  } else if (e.type === "command" && ((g = e == null ? void 0 : e.payload) == null ? void 0 : g.command) == "set_local_tools") {
156
- this.notifyPacket({
156
+ const w = (C = e == null ? void 0 : e.payload) == null ? void 0 : C.arguments.tools;
157
+ (w == null ? void 0 : w.length) > 0 && this.notifyPacket({
157
158
  type: "message",
158
159
  payload: {
159
160
  type: "text",
@@ -165,7 +166,7 @@ class _ extends v {
165
166
  }
166
167
  const c = e.payload;
167
168
  try {
168
- const b = await (await fetch(`${s}/sessions/${i}/messages`, {
169
+ const N = await (await fetch(`${s}/sessions/${r}/messages`, {
169
170
  body: JSON.stringify({ agentId: a, userMessage: c, initialContext: this.context, tools: this.tools }),
170
171
  method: "POST",
171
172
  headers: {
@@ -174,12 +175,12 @@ class _ extends v {
174
175
  }
175
176
  })).json();
176
177
  this.notifyPackets(
177
- b.response.messages.map(($) => ({
178
+ N.response.messages.map((T) => ({
178
179
  type: "message",
179
- payload: $
180
+ payload: T
180
181
  }))
181
182
  );
182
- } catch (R) {
183
+ } catch (w) {
183
184
  this.notifyPacket({
184
185
  type: "message",
185
186
  payload: {
@@ -187,18 +188,18 @@ class _ extends v {
187
188
  type: "text",
188
189
  text: "An error occurred while processing your request. Please try again later."
189
190
  }
190
- }), (w = this.config.logger) == null || w.error("Error sending packet:", R);
191
+ }), (A = this.config.logger) == null || A.error("Error sending packet:", w);
191
192
  }
192
193
  }
193
194
  }
194
- class W extends v {
195
+ class U extends $ {
195
196
  constructor(e) {
196
197
  super();
197
- r(this, "status");
198
- r(this, "autostart");
199
- r(this, "session");
200
- r(this, "config");
201
- r(this, "webSocket");
198
+ i(this, "status");
199
+ i(this, "autostart");
200
+ i(this, "session");
201
+ i(this, "config");
202
+ i(this, "webSocket");
202
203
  this.config = e, this.status = "disconnected", this.autostart = !0, this.session = null, this.webSocket = null;
203
204
  }
204
205
  getName() {
@@ -217,25 +218,25 @@ class W extends v {
217
218
  return Promise.resolve(this.session);
218
219
  const s = e || this.session || "new";
219
220
  (c = this.config.logger) == null || c.debug("Connecting to WebSocket with sessionId:", s);
220
- const n = encodeURIComponent(this.config.apiKey), a = this.config.agentId, i = `${this.config.webSocketUrl}?sessionCode=${s}&agentId=${a}&apiKey=${n}`;
221
- return this.setStatus("connecting"), this.webSocket = new WebSocket(i), this.webSocket.addEventListener("open", () => {
221
+ const n = encodeURIComponent(this.config.apiKey), a = this.config.agentId, r = `${this.config.webSocketUrl}?sessionCode=${s}&agentId=${a}&apiKey=${n}`;
222
+ return this.setStatus("connecting"), this.webSocket = new WebSocket(r), this.webSocket.addEventListener("open", () => {
222
223
  this.setStatus("connected");
223
- }), this.webSocket.addEventListener("message", (u) => {
224
- const d = JSON.parse(u.data);
225
- this.notifyPacket(d);
226
- }), this.webSocket.addEventListener("close", (u) => {
227
- var d;
228
- this.setStatus("disconnected"), this.webSocket = null, u.code !== 1e3 && (this.notifyPacket({
224
+ }), this.webSocket.addEventListener("message", (l) => {
225
+ const p = JSON.parse(l.data);
226
+ this.notifyPacket(p);
227
+ }), this.webSocket.addEventListener("close", (l) => {
228
+ var p;
229
+ this.setStatus("disconnected"), this.webSocket = null, l.code !== 1e3 && (this.notifyPacket({
229
230
  type: "message",
230
231
  payload: {
231
232
  role: "assistant",
232
233
  type: "text",
233
234
  text: "Oops! The connection to the server was lost. Please try again later."
234
235
  }
235
- }), (d = this.config.logger) == null || d.warn("WebSocket connection closed"));
236
+ }), (p = this.config.logger) == null || p.warn("WebSocket connection closed"));
236
237
  }), this.webSocket.addEventListener("error", () => {
237
- var u;
238
- this.setStatus("disconnected"), this.webSocket = null, (u = this.config.logger) == null || u.error("WebSocket connection error");
238
+ var l;
239
+ this.setStatus("disconnected"), this.webSocket = null, (l = this.config.logger) == null || l.error("WebSocket connection error");
239
240
  }), Promise.resolve(s);
240
241
  }
241
242
  disconnect() {
@@ -246,23 +247,23 @@ class W extends v {
246
247
  return this.webSocket && this.status === "connected" ? (this.webSocket.send(JSON.stringify(e)), Promise.resolve()) : Promise.reject(new Error("WebSocket is not connected"));
247
248
  }
248
249
  }
249
- class ae {
250
+ class re {
250
251
  constructor(t) {
251
- r(this, "config");
252
- r(this, "pc", null);
253
- r(this, "ws", null);
254
- r(this, "localStream", null);
255
- r(this, "remoteStream", new MediaStream());
256
- r(this, "audioCtx", null);
257
- r(this, "localAnalyser", null);
258
- r(this, "remoteAnalyser", null);
259
- r(this, "analyzerFrame", null);
260
- r(this, "dataChannel", null);
261
- r(this, "isConnected", !1);
262
- r(this, "visualizerCallbacks", []);
263
- r(this, "messageCallbacks", []);
264
- r(this, "errorCallbacks", []);
265
- r(this, "queuedMessages", []);
252
+ i(this, "config");
253
+ i(this, "pc", null);
254
+ i(this, "ws", null);
255
+ i(this, "localStream", null);
256
+ i(this, "remoteStream", new MediaStream());
257
+ i(this, "audioCtx", null);
258
+ i(this, "localAnalyser", null);
259
+ i(this, "remoteAnalyser", null);
260
+ i(this, "analyzerFrame", null);
261
+ i(this, "dataChannel", null);
262
+ i(this, "isConnected", !1);
263
+ i(this, "visualizerCallbacks", []);
264
+ i(this, "messageCallbacks", []);
265
+ i(this, "errorCallbacks", []);
266
+ i(this, "queuedMessages", []);
266
267
  this.config = t;
267
268
  }
268
269
  async connect(t) {
@@ -289,13 +290,13 @@ class ae {
289
290
  }), this.localStream.getTracks().forEach((n) => {
290
291
  this.pc.addTrack(n, this.localStream);
291
292
  }), this.pc.ontrack = (n) => {
292
- n.streams[0].getTracks().forEach((i) => {
293
- this.remoteStream.addTrack(i);
293
+ n.streams[0].getTracks().forEach((r) => {
294
+ this.remoteStream.addTrack(r);
294
295
  }), this.audioCtx || this._startAnalyzers();
295
296
  const a = new Audio();
296
- a.srcObject = this.remoteStream, a.play().catch((i) => {
297
+ a.srcObject = this.remoteStream, a.play().catch((r) => {
297
298
  var c;
298
- (c = this.config.logger) == null || c.error("Error playing remote audio:", i);
299
+ (c = this.config.logger) == null || c.error("Error playing remote audio:", r);
299
300
  });
300
301
  }, this.pc.onicecandidate = (n) => {
301
302
  var a;
@@ -308,21 +309,21 @@ class ae {
308
309
  );
309
310
  }, this.pc.ondatachannel = (n) => {
310
311
  const a = n.channel;
311
- a.onmessage = (i) => {
312
+ a.onmessage = (r) => {
312
313
  this.messageCallbacks.forEach((c) => {
313
- c(i);
314
+ c(r);
314
315
  });
315
316
  }, a.onopen = () => {
316
- var i;
317
+ var r;
317
318
  for (; this.queuedMessages.length > 0; ) {
318
319
  const c = this.queuedMessages.shift();
319
- c && (a.send(JSON.stringify(c)), (i = this.config.logger) == null || i.info("Sent queued message:", c));
320
+ c && (a.send(JSON.stringify(c)), (r = this.config.logger) == null || r.info("Sent queued message:", c));
320
321
  }
321
322
  };
322
323
  };
323
324
  const e = this.config.webrtcUrl || "wss://persona.applica.guru/api/webrtc";
324
325
  this.ws = new WebSocket(`${e}?apiKey=${encodeURIComponent(this.config.apiKey)}`), this.ws.onopen = async () => {
325
- var c, u;
326
+ var c, l;
326
327
  const n = await this.pc.createOffer();
327
328
  await this.pc.setLocalDescription(n);
328
329
  const a = {
@@ -331,9 +332,9 @@ class ae {
331
332
  sessionCode: t
332
333
  };
333
334
  (c = this.config.logger) == null || c.debug("Opening connection to WebRTC server: ", a);
334
- const i = {
335
+ const r = {
335
336
  type: "OFFER",
336
- src: ((u = crypto.randomUUID) == null ? void 0 : u.call(crypto)) || "client_" + Date.now(),
337
+ src: ((l = crypto.randomUUID) == null ? void 0 : l.call(crypto)) || "client_" + Date.now(),
337
338
  payload: {
338
339
  sdp: {
339
340
  sdp: n.sdp,
@@ -343,9 +344,9 @@ class ae {
343
344
  metadata: a
344
345
  }
345
346
  };
346
- this.ws.send(JSON.stringify(i));
347
+ this.ws.send(JSON.stringify(r));
347
348
  }, this.ws.onmessage = async (n) => {
348
- var i;
349
+ var r;
349
350
  const a = JSON.parse(n.data);
350
351
  if (a.type === "ANSWER")
351
352
  await this.pc.setRemoteDescription(new RTCSessionDescription(a.payload.sdp));
@@ -353,7 +354,7 @@ class ae {
353
354
  try {
354
355
  await this.pc.addIceCandidate(new RTCIceCandidate(a.payload.candidate));
355
356
  } catch (c) {
356
- (i = this.config.logger) == null || i.error("Error adding ICE candidate:", c);
357
+ (r = this.config.logger) == null || r.error("Error adding ICE candidate:", c);
357
358
  }
358
359
  }, this.ws.onclose = (n) => {
359
360
  n.code !== 1e3 && this.errorCallbacks.forEach((a) => {
@@ -406,10 +407,10 @@ class ae {
406
407
  return;
407
408
  const n = new Uint8Array(this.localAnalyser.frequencyBinCount), a = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
408
409
  this.localAnalyser.getByteFrequencyData(n), this.remoteAnalyser.getByteFrequencyData(a);
409
- const i = n.reduce((u, d) => u + d, 0) / n.length, c = a.reduce((u, d) => u + d, 0) / a.length;
410
- this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((u) => {
411
- u({
412
- localAmplitude: i,
410
+ const r = n.reduce((l, p) => l + p, 0) / n.length, c = a.reduce((l, p) => l + p, 0) / a.length;
411
+ this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((l) => {
412
+ l({
413
+ localAmplitude: r,
413
414
  remoteAmplitude: c
414
415
  });
415
416
  }), this.analyzerFrame = requestAnimationFrame(s);
@@ -420,15 +421,15 @@ class ae {
420
421
  this.analyzerFrame && (cancelAnimationFrame(this.analyzerFrame), this.analyzerFrame = null), this.localAnalyser = null, this.remoteAnalyser = null;
421
422
  }
422
423
  }
423
- class z extends v {
424
+ class F extends $ {
424
425
  constructor(e) {
425
426
  super();
426
- r(this, "status");
427
- r(this, "session");
428
- r(this, "autostart");
429
- r(this, "config");
430
- r(this, "webRTCClient");
431
- this.config = e, this.status = "disconnected", this.session = null, this.autostart = (e == null ? void 0 : e.autostart) ?? !1, this.webRTCClient = new ae(e), this.webRTCClient.addMessageCallback((s) => {
427
+ i(this, "status");
428
+ i(this, "session");
429
+ i(this, "autostart");
430
+ i(this, "config");
431
+ i(this, "webRTCClient");
432
+ this.config = e, this.status = "disconnected", this.session = null, this.autostart = (e == null ? void 0 : e.autostart) ?? !1, this.webRTCClient = new re(e), this.webRTCClient.addMessageCallback((s) => {
432
433
  const n = JSON.parse(s.data);
433
434
  this.notifyPacket(n);
434
435
  }), this.webRTCClient.addErrorCallback((s) => {
@@ -468,7 +469,7 @@ class z extends v {
468
469
  }
469
470
  class ie {
470
471
  constructor(t) {
471
- r(this, "config");
472
+ i(this, "config");
472
473
  this.config = t;
473
474
  }
474
475
  async complete(t, e) {
@@ -491,17 +492,17 @@ class ie {
491
492
  });
492
493
  }
493
494
  }
494
- class re {
495
+ class ce {
495
496
  constructor(t, e) {
496
- r(this, "transaction");
497
- r(this, "manager");
497
+ i(this, "transaction");
498
+ i(this, "manager");
498
499
  this.transaction = t, this.manager = e;
499
500
  }
500
501
  getFunctionCall() {
501
502
  return this.transaction.functionCall;
502
503
  }
503
504
  async invoke(t) {
504
- var i;
505
+ var r;
505
506
  const e = this.transaction.functionCall;
506
507
  if (!e) {
507
508
  await this.fail("No function call found");
@@ -513,8 +514,8 @@ class re {
513
514
  return;
514
515
  }
515
516
  try {
516
- const u = (((i = a.toString().replace(/\n/g, " ").match(/^[^(]*\(([^)]*)\)/)) == null ? void 0 : i[1].split(",").map((g) => g.trim()).filter(Boolean)) || []).map((g) => n[g]), d = await a.apply(null, u);
517
- await this.complete(d);
517
+ const l = (((r = a.toString().replace(/\n/g, " ").match(/^[^(]*\(([^)]*)\)/)) == null ? void 0 : r[1].split(",").map((g) => g.trim()).filter(Boolean)) || []).map((g) => n[g]), p = await a.apply(null, l);
518
+ await this.complete(p);
518
519
  } catch (c) {
519
520
  await this.fail(`Error executing tool ${s}: ${c}`);
520
521
  }
@@ -526,15 +527,15 @@ class re {
526
527
  await this.manager.fail(this.transaction, { success: !1, output: null, error: t });
527
528
  }
528
529
  }
529
- class ce extends v {
530
+ class le extends $ {
530
531
  constructor(e) {
531
532
  super();
532
- r(this, "status");
533
- r(this, "autostart");
534
- r(this, "session");
535
- r(this, "config");
536
- r(this, "notify", !0);
537
- r(this, "_tools");
533
+ i(this, "status");
534
+ i(this, "autostart");
535
+ i(this, "session");
536
+ i(this, "config");
537
+ i(this, "notify", !0);
538
+ i(this, "_tools");
538
539
  if (this.config = e, this.status = "disconnected", this.autostart = !0, Array.isArray(e.tools)) {
539
540
  this._tools = {};
540
541
  for (const s of e.tools)
@@ -561,15 +562,15 @@ class ce extends v {
561
562
  }
562
563
  onTransaction(e) {
563
564
  console.log("transaction received:", e);
564
- const s = new ie(this.config), n = new re(e, s);
565
+ const s = new ie(this.config), n = new ce(e, s);
565
566
  this.config.onTransaction ? this.config.onTransaction(n) : n.invoke(this._tools);
566
567
  }
567
568
  getTools() {
568
569
  return this._tools;
569
570
  }
570
571
  }
571
- const x = Y(void 0);
572
- function le(o) {
572
+ const R = G(void 0);
573
+ function ue(o) {
573
574
  return new Promise((t, e) => {
574
575
  const s = new FileReader();
575
576
  s.readAsDataURL(o), s.onload = () => {
@@ -580,150 +581,173 @@ function le(o) {
580
581
  };
581
582
  });
582
583
  }
583
- function ue({
584
+ function z(o) {
585
+ const t = {
586
+ rest: (e) => e.config.apiUrl,
587
+ webrtc: (e) => e.config.webrtcUrl.replace("/webrtc", "").replace("ws://", "http://").replace("wss://", "https://"),
588
+ websocket: (e) => e.config.webSocketUrl.replace("/websocket", "").replace("ws://", "http://").replace("wss://", "https://")
589
+ };
590
+ for (const e of o) {
591
+ const s = t[e.getName()];
592
+ if (s)
593
+ return s(e);
594
+ }
595
+ return null;
596
+ }
597
+ function he({
584
598
  dev: o = !1,
585
599
  baseUrl: t,
586
600
  protocols: e,
587
601
  logger: s,
588
602
  children: n,
589
603
  session: a = "new",
590
- ...i
604
+ ...r
591
605
  }) {
592
- const [c, u] = E(!1), [d, g] = E([]), [w, R] = E(a), [b, $] = E(/* @__PURE__ */ new Map()), q = G(!1), S = H(() => {
606
+ const [c, l] = E(!1), [p, g] = E([]), [C, A] = E(a), [w, N] = E(/* @__PURE__ */ new Map()), T = H(!1), S = Q(() => {
593
607
  if (Array.isArray(e))
594
608
  return e;
595
609
  if (typeof e == "object" && e !== null) {
596
- const l = o ? "localhost:8000" : t || "persona.applica.guru/api", m = o ? "http" : "https", h = o ? "ws" : "wss";
597
- let f = Object.keys(e).map((y) => {
598
- switch (y) {
610
+ const y = o ? "localhost:8000" : t || "persona.applica.guru/api", h = o ? "http" : "https", f = o ? "ws" : "wss";
611
+ let d = Object.keys(e).map((m) => {
612
+ switch (m) {
599
613
  case "rest":
600
- const p = e[y];
601
- return p === !0 ? new _({
602
- apiUrl: `${m}://${l}`,
603
- apiKey: i.apiKey,
604
- agentId: i.agentId,
614
+ const b = e[m];
615
+ return b === !0 ? new _({
616
+ apiUrl: `${h}://${y}`,
617
+ apiKey: r.apiKey,
618
+ agentId: r.agentId,
605
619
  logger: s
606
- }) : typeof p == "object" && p !== null ? new _(p) : null;
620
+ }) : typeof b == "object" && b !== null ? new _(b) : null;
607
621
  case "webrtc":
608
- const C = e[y];
609
- return C === !0 ? new z({
610
- webrtcUrl: `${h}://${l}/webrtc`,
611
- apiKey: i.apiKey,
612
- agentId: i.agentId,
622
+ const u = e[m];
623
+ return u === !0 ? new F({
624
+ webrtcUrl: `${f}://${y}/webrtc`,
625
+ apiKey: r.apiKey,
626
+ agentId: r.agentId,
613
627
  logger: s
614
- }) : typeof C == "object" && C !== null ? new z(C) : null;
628
+ }) : typeof u == "object" && u !== null ? new F(u) : null;
615
629
  case "websocket":
616
- const T = e[y];
617
- return T === !0 ? new W({
618
- webSocketUrl: `${h}://${l}/websocket`,
619
- apiKey: i.apiKey,
620
- agentId: i.agentId,
630
+ const P = e[m];
631
+ return P === !0 ? new U({
632
+ webSocketUrl: `${f}://${y}/websocket`,
633
+ apiKey: r.apiKey,
634
+ agentId: r.agentId,
621
635
  logger: s
622
- }) : typeof T == "object" && T !== null ? new W(T) : null;
636
+ }) : typeof P == "object" && P !== null ? new U(P) : null;
623
637
  default:
624
- throw new Error(`Unknown protocol: ${y}`);
638
+ throw new Error(`Unknown protocol: ${m}`);
625
639
  }
626
- }).filter((y) => y !== null);
627
- return i.tools && f.push(
628
- new ce({
629
- apiUrl: `${m}://${l}`,
630
- apiKey: i.apiKey,
631
- agentId: i.agentId,
632
- tools: i.tools,
640
+ }).filter((m) => m !== null);
641
+ return r.tools && d.push(
642
+ new le({
643
+ apiUrl: `${h}://${y}`,
644
+ apiKey: r.apiKey,
645
+ agentId: r.agentId,
646
+ tools: r.tools,
633
647
  // Pass raw tools
634
648
  logger: s
635
649
  })
636
- ), f;
650
+ ), d;
637
651
  }
638
652
  throw new Error("Invalid protocols configuration");
639
653
  }, []);
640
- Q(() => {
641
- q.current || (q.current = !0, s == null || s.debug(
654
+ X(() => {
655
+ if (T.current) return;
656
+ T.current = !0, s == null || s.debug(
642
657
  "Initializing protocols: ",
643
- S.map((l) => l.getName())
644
- ), S.forEach((l) => {
645
- l.setSession(w), l.clearListeners(), l.addStatusChangeListener((m) => {
646
- s == null || s.debug(`${l.getName()} has notified new status: ${m}`), b.set(l.getName(), m), m === "connected" && (i.context && l.sendPacket({
658
+ S.map((h) => h.getName())
659
+ );
660
+ const y = z(S);
661
+ S.forEach((h) => {
662
+ h.setSession(C), h.clearListeners(), h.addStatusChangeListener((f) => {
663
+ s == null || s.debug(`${h.getName()} has notified new status: ${f}`), w.set(h.getName(), f), f === "connected" && (r.context && h.sendPacket({
647
664
  type: "command",
648
665
  payload: {
649
666
  command: "set_initial_context",
650
- arguments: i.context
667
+ arguments: r.context
651
668
  }
652
- }), i.tools && Array.isArray(i.tools) && l.sendPacket({
669
+ }), r.tools && Array.isArray(r.tools) && h.sendPacket({
653
670
  type: "command",
654
671
  payload: {
655
672
  command: "set_local_tools",
656
673
  arguments: {
657
- tools: i.tools.map((h) => h.schema)
674
+ tools: r.tools.map((d) => d.schema)
658
675
  }
659
676
  }
660
- })), $(new Map(b));
661
- }), l.addPacketListener((m) => {
662
- if (m.type === "message") {
663
- const h = m.payload;
677
+ })), N(new Map(w));
678
+ }), h.addPacketListener((f) => {
679
+ if (f.type === "message") {
680
+ const d = f.payload;
664
681
  g(
665
- (f) => j([...f, { ...h, protocol: l.getName() }])
682
+ (m) => j([...m, { ...d, protocol: h.getName() }])
666
683
  );
667
- } else if (m.type === "reasoning") {
668
- const h = m.payload;
669
- let f = h.thought;
670
- h.imageUrl && (f += `
684
+ } else if (f.type === "reasoning") {
685
+ const d = f.payload;
686
+ let m = d.thought;
687
+ d.imageUrl && (m += `
671
688
 
672
- ![image](https://persona.applica.guru/api/files/${h.imageUrl})`);
673
- const y = { type: "reasoning", text: f, role: "assistant", finishReason: "stop" };
689
+ ![image](${y}/api/files/${d.imageUrl})`);
690
+ const b = { type: "reasoning", text: m, role: "assistant", finishReason: "stop" };
674
691
  g(
675
- (p) => j([...p, { ...y, protocol: l.getName() }])
692
+ (u) => j([...u, { ...b, protocol: h.getName() }])
676
693
  );
677
- } else m.type === "transaction" && S.filter((h) => h !== l).forEach((h) => h.onTransaction(m.payload));
678
- }), l.autostart && l.status === "disconnected" && (s == null || s.debug(`Connecting to protocol: ${l.getName()}`), l.connect(w));
679
- }));
680
- }, [w, S, s, b]);
681
- const U = async (l) => {
682
- var y;
683
- if (((y = l.content[0]) == null ? void 0 : y.type) !== "text") throw new Error("Only text messages are supported");
684
- const m = l.content[0].text;
685
- g((p) => [...p, { role: "user", type: "text", text: m }]), u(!0);
686
- const h = S.sort((p, C) => C.getPriority() - p.getPriority()).find((p) => p.status === "connected"), f = [];
687
- if (l.attachments)
688
- for (const p of l.attachments)
689
- p.contentType.startsWith("image/") && p.file && f.push({
694
+ } else f.type === "transaction" && S.filter((d) => d !== h).forEach((d) => d.onTransaction(f.payload));
695
+ }), h.autostart && h.status === "disconnected" && (s == null || s.debug(`Connecting to protocol: ${h.getName()}`), h.connect(C));
696
+ });
697
+ }, [C, S, s, w]);
698
+ const D = async (y) => {
699
+ var b;
700
+ if (((b = y.content[0]) == null ? void 0 : b.type) !== "text") throw new Error("Only text messages are supported");
701
+ if (!S.some((u) => u.getName() !== "transaction" && u.status === "connected")) {
702
+ g((u) => [
703
+ ...u,
704
+ { role: "assistant", type: "text", text: "No protocol is connected." }
705
+ ]);
706
+ return;
707
+ }
708
+ const f = y.content[0].text;
709
+ g((u) => [...u, { role: "user", type: "text", text: f }]), l(!0);
710
+ const d = S.sort((u, P) => P.getPriority() - u.getPriority()).find((u) => u.status === "connected"), m = [];
711
+ if (y.attachments)
712
+ for (const u of y.attachments)
713
+ u.contentType.startsWith("image/") && u.file && m.push({
690
714
  role: "user",
691
715
  image: {
692
- contentType: p.contentType,
693
- content: await le(p.file)
716
+ contentType: u.contentType,
717
+ content: await ue(u.file)
694
718
  },
695
719
  text: "",
696
720
  type: "text"
697
721
  });
698
- l.content && f.push({
722
+ y.content && m.push({
699
723
  role: "user",
700
- text: l.content[0].text,
724
+ text: y.content[0].text,
701
725
  type: "text"
702
- }), s == null || s.debug("Sending message:", f), await (h == null ? void 0 : h.sendPacket({ type: "request", payload: f })), u(!1);
703
- }, D = N(() => (u(!1), g([]), R("new"), Promise.resolve()), []), L = N(() => Promise.resolve(), []), J = N(() => d, [d]), K = X({
726
+ }), s == null || s.debug("Sending message:", m), await (d == null ? void 0 : d.sendPacket({ type: "request", payload: m })), l(!1);
727
+ }, L = M(() => (l(!1), g([]), A("new"), Promise.resolve()), []), J = M(() => Promise.resolve(), []), K = M(() => p, [p]), B = Z({
704
728
  isRunning: c,
705
- messages: d,
706
- convertMessage: oe,
707
- onNew: U,
708
- onCancel: D,
709
- onReload: L,
729
+ messages: p,
730
+ convertMessage: ae,
731
+ onNew: D,
732
+ onCancel: L,
733
+ onReload: J,
710
734
  adapters: {
711
- attachments: new Z([new ee()])
735
+ attachments: new ee([new te()])
712
736
  }
713
737
  });
714
- return /* @__PURE__ */ M.jsx(x.Provider, { value: { protocols: S, protocolsStatus: b, getMessages: J }, children: /* @__PURE__ */ M.jsx(te, { runtime: K, children: n }) });
738
+ return /* @__PURE__ */ I.jsx(R.Provider, { value: { protocols: S, protocolsStatus: w, getMessages: K }, children: /* @__PURE__ */ I.jsx(se, { runtime: B, children: n }) });
715
739
  }
716
- function ge({ children: o, ...t }) {
717
- return /* @__PURE__ */ M.jsx(ue, { ...t, children: o });
740
+ function we({ children: o, ...t }) {
741
+ return /* @__PURE__ */ I.jsx(he, { ...t, children: o });
718
742
  }
719
- function we() {
720
- const o = A(x);
743
+ function be() {
744
+ const o = v(R);
721
745
  if (!o)
722
746
  throw new Error("usePersonaRuntime must be used within a PersonaRuntimeProvider");
723
747
  return o;
724
748
  }
725
- function he(o) {
726
- const t = A(x);
749
+ function de(o) {
750
+ const t = v(R);
727
751
  if (!t)
728
752
  throw new Error("usePersonaRuntimeProtocol must be used within a PersonaRuntimeProvider");
729
753
  const e = t.protocols.find((n) => n.getName() === o);
@@ -743,27 +767,24 @@ function he(o) {
743
767
  status: s || e.status
744
768
  };
745
769
  }
746
- function be() {
747
- const o = A(x);
770
+ function Se() {
771
+ const o = v(R);
748
772
  if (!o)
749
773
  throw new Error("usePersonaRuntimeEndpoint must be used within a PersonaRuntimeProvider");
750
- for (const t of o.protocols)
751
- if (t.getName() === "rest")
752
- return t.config.apiUrl;
753
- throw new Error("REST protocol not found");
754
- }
755
- function Se() {
756
- return he("webrtc");
774
+ return z(o.protocols);
757
775
  }
758
776
  function Ce() {
759
- const o = A(x);
777
+ return de("webrtc");
778
+ }
779
+ function Pe() {
780
+ const o = v(R);
760
781
  if (!o)
761
782
  throw new Error("usePersonaRuntimeMessages must be used within a PersonaRuntimeProvider");
762
783
  return o.getMessages();
763
784
  }
764
- class Pe {
785
+ class xe {
765
786
  constructor() {
766
- r(this, "prefix", "[Persona]");
787
+ i(this, "prefix", "[Persona]");
767
788
  }
768
789
  log(t, ...e) {
769
790
  console.log(`${this.prefix} - ${t}`, ...e);
@@ -781,14 +802,14 @@ class Pe {
781
802
  console.debug(`${this.prefix} - ${t}`, ...e);
782
803
  }
783
804
  }
784
- function P(o, t, e) {
805
+ function x(o, t, e) {
785
806
  return {
786
807
  type: o,
787
808
  description: t,
788
809
  ...e
789
810
  };
790
811
  }
791
- function de(o) {
812
+ function pe(o) {
792
813
  const t = Object.entries(o.parameters).filter(([e, s]) => s.required).map(([e]) => e);
793
814
  return {
794
815
  type: "local",
@@ -810,14 +831,14 @@ function de(o) {
810
831
  }
811
832
  };
812
833
  }
813
- function I(o) {
834
+ function q(o) {
814
835
  return {
815
- schema: de(o),
836
+ schema: pe(o),
816
837
  implementation: o.implementation
817
838
  };
818
839
  }
819
- function pe(o, t, e, s, n, a) {
820
- const i = {
840
+ function me(o, t, e, s, n, a) {
841
+ const r = {
821
842
  name: o,
822
843
  description: t,
823
844
  title: a == null ? void 0 : a.title,
@@ -826,43 +847,43 @@ function pe(o, t, e, s, n, a) {
826
847
  output: n,
827
848
  implementation: e
828
849
  };
829
- return I(i);
850
+ return q(r);
830
851
  }
831
- const ke = pe(
852
+ const ke = me(
832
853
  "sum",
833
854
  "Sum two numbers",
834
855
  function(t, e) {
835
856
  return { result: t + e };
836
857
  },
837
858
  {
838
- a: P("number", "First number to sum", { required: !0 }),
839
- b: P("number", "Second number to sum", { required: !0 })
859
+ a: x("number", "First number to sum", { required: !0 }),
860
+ b: x("number", "Second number to sum", { required: !0 })
840
861
  },
841
862
  {
842
- result: P("number", "Sum of two numbers")
863
+ result: x("number", "Sum of two numbers")
843
864
  }
844
- ), xe = I({
865
+ ), Re = q({
845
866
  name: "navigate_to",
846
867
  description: "Allow agent to redirect user to specific sub page like /foo or #/foo or anything like that",
847
868
  title: "Sum two numbers",
848
869
  // As per the user's example
849
870
  timeout: 60,
850
871
  parameters: {
851
- a: P("number", "First number to sum"),
852
- b: P("number", "Seconth number to sum")
872
+ a: x("number", "First number to sum"),
873
+ b: x("number", "Seconth number to sum")
853
874
  // Keeping the typo as in the original
854
875
  },
855
876
  output: {
856
- result: P("number", "Sum of two numbers")
877
+ result: x("number", "Sum of two numbers")
857
878
  },
858
879
  implementation: function(t, e) {
859
880
  return { result: t + e };
860
881
  }
861
882
  });
862
- function Re(o) {
883
+ function Ae(o) {
863
884
  const t = [], e = {};
864
885
  return o.forEach((s) => {
865
- const { schema: n, implementation: a } = I(s);
886
+ const { schema: n, implementation: a } = q(s);
866
887
  t.push(n), e[s.name] = a;
867
888
  }), { schemas: t, implementations: e };
868
889
  }
@@ -872,38 +893,38 @@ function Te(o, t) {
872
893
  if (!(n in o))
873
894
  throw new Error(`Missing required parameter: ${n}`);
874
895
  for (const [n, a] of Object.entries(o)) {
875
- const i = s[n];
876
- if (i) {
877
- if (i.type === "number" && typeof a != "number")
896
+ const r = s[n];
897
+ if (r) {
898
+ if (r.type === "number" && typeof a != "number")
878
899
  throw new Error(`Parameter ${n} should be a number`);
879
- if (i.type === "string" && typeof a != "string")
900
+ if (r.type === "string" && typeof a != "string")
880
901
  throw new Error(`Parameter ${n} should be a string`);
881
- if (i.type === "boolean" && typeof a != "boolean")
902
+ if (r.type === "boolean" && typeof a != "boolean")
882
903
  throw new Error(`Parameter ${n} should be a boolean`);
883
904
  }
884
905
  }
885
906
  return !0;
886
907
  }
887
908
  export {
888
- Pe as PersonaConsoleLogger,
889
- v as PersonaProtocolBase,
909
+ xe as PersonaConsoleLogger,
910
+ $ as PersonaProtocolBase,
890
911
  _ as PersonaRESTProtocol,
891
- ge as PersonaRuntimeProvider,
892
- ce as PersonaTransactionProtocol,
893
- z as PersonaWebRTCProtocol,
894
- W as PersonaWebSocketProtocol,
895
- P as createParameter,
896
- I as createTool,
897
- pe as createToolFromFunction,
898
- Re as createToolRegistry,
899
- de as generateToolSchema,
900
- xe as navigateToToolExample,
912
+ we as PersonaRuntimeProvider,
913
+ le as PersonaTransactionProtocol,
914
+ F as PersonaWebRTCProtocol,
915
+ U as PersonaWebSocketProtocol,
916
+ x as createParameter,
917
+ q as createTool,
918
+ me as createToolFromFunction,
919
+ Ae as createToolRegistry,
920
+ pe as generateToolSchema,
921
+ Re as navigateToToolExample,
901
922
  ke as sumTool,
902
- we as usePersonaRuntime,
903
- be as usePersonaRuntimeEndpoint,
904
- Ce as usePersonaRuntimeMessages,
905
- he as usePersonaRuntimeProtocol,
906
- Se as usePersonaRuntimeWebRTCProtocol,
923
+ be as usePersonaRuntime,
924
+ Se as usePersonaRuntimeEndpoint,
925
+ Pe as usePersonaRuntimeMessages,
926
+ de as usePersonaRuntimeProtocol,
927
+ Ce as usePersonaRuntimeWebRTCProtocol,
907
928
  Te as validateToolParameters
908
929
  };
909
930
  //# sourceMappingURL=bundle.es.js.map