@applica-software-guru/persona-sdk 0.1.68 → 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 = (a, t, e) => t in a ? B(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
3
- var i = (a, t, e) => V(a, typeof t != "symbol" ? t + "" : t, e);
4
- import { createContext as Y, useContext as E, useState as A, 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 z = { exports: {} }, x = {};
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,77 +14,77 @@ var z = { exports: {} }, x = {};
14
14
  * LICENSE file in the root directory of this source tree.
15
15
  */
16
16
  var O;
17
- function se() {
18
- if (O) return x;
17
+ function ne() {
18
+ if (O) return k;
19
19
  O = 1;
20
- var a = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
21
- function e(s, n, o) {
20
+ var o = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
21
+ function e(s, n, a) {
22
22
  var r = null;
23
- if (o !== void 0 && (r = "" + o), n.key !== void 0 && (r = "" + n.key), "key" in n) {
24
- o = {};
23
+ if (a !== void 0 && (r = "" + a), n.key !== void 0 && (r = "" + n.key), "key" in n) {
24
+ a = {};
25
25
  for (var c in n)
26
- c !== "key" && (o[c] = n[c]);
27
- } else o = n;
28
- return n = o.ref, {
29
- $$typeof: a,
26
+ c !== "key" && (a[c] = n[c]);
27
+ } else a = n;
28
+ return n = a.ref, {
29
+ $$typeof: o,
30
30
  type: s,
31
31
  key: r,
32
32
  ref: n !== void 0 ? n : null,
33
- props: o
33
+ props: a
34
34
  };
35
35
  }
36
- return x.Fragment = t, x.jsx = e, x.jsxs = e, x;
36
+ return k.Fragment = t, k.jsx = e, k.jsxs = e, k;
37
37
  }
38
- z.exports = se();
39
- var M = z.exports;
40
- function ne(a) {
41
- return a.filter((t) => {
38
+ W.exports = ne();
39
+ var I = W.exports;
40
+ function oe(o) {
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;
44
44
  });
45
45
  }
46
- function j(a) {
46
+ function j(o) {
47
47
  const t = [];
48
48
  let e = null;
49
- for (const n of a)
49
+ for (const n of o)
50
50
  n.type !== "transaction" && (n.type === "reasoning" ? (e != null && (t.push(e), e = null), t.push(n)) : n.functionCalls ? (e && t.push(e), t.push(n), e = null) : n.functionResponse ? t[t.length - 1] = {
51
51
  ...t[t.length - 1],
52
52
  functionResponse: n.functionResponse
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 ae(a) {
58
+ function ae(o) {
59
59
  var e, s;
60
- const t = ((e = a.files) == null ? void 0 : e.map(
60
+ const t = ((e = o.files) == null ? void 0 : e.map(
61
61
  (n) => ({
62
62
  type: "file",
63
63
  data: n.url,
64
64
  mimeType: n.contentType
65
65
  })
66
66
  )) ?? [];
67
- return a.role === "function" ? {
68
- id: a.id,
67
+ return o.role === "function" ? {
68
+ id: o.id,
69
69
  role: "assistant",
70
- status: (a == null ? void 0 : a.functionResponse) === null ? { type: "running" } : { type: "complete", reason: "stop" },
71
- content: ((s = a.functionCalls) == null ? void 0 : s.map((n) => {
72
- var o;
70
+ status: (o == null ? void 0 : o.functionResponse) === null ? { type: "running" } : { type: "complete", reason: "stop" },
71
+ content: ((s = o.functionCalls) == null ? void 0 : s.map((n) => {
72
+ var a;
73
73
  return {
74
74
  type: "tool-call",
75
75
  toolName: n.name,
76
76
  toolCallId: n.id,
77
77
  args: n.args,
78
- result: (o = a.functionResponse) == null ? void 0 : o.result
78
+ result: (a = o.functionResponse) == null ? void 0 : a.result
79
79
  };
80
80
  })) ?? []
81
81
  } : {
82
- id: a.id,
83
- role: a.role,
84
- content: a.type === "reasoning" ? [{ type: "reasoning", text: a.text }, ...t] : [{ type: "text", text: a.text }, ...t]
82
+ id: o.id,
83
+ role: o.role,
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
89
  i(this, "statusChangeCallbacks", []);
90
90
  i(this, "messageCallbacks", []);
@@ -119,7 +119,7 @@ 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
125
  i(this, "status");
@@ -147,13 +147,14 @@ class _ extends v {
147
147
  this.session = e;
148
148
  }
149
149
  async sendPacket(e) {
150
- var u, d, y, S;
151
- const { apiUrl: s, apiKey: n, agentId: o } = this.config, r = 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
- } else if (e.type === "command" && ((y = e == null ? void 0 : e.payload) == null ? void 0 : y.command) == "set_local_tools") {
156
- this.notifyPacket({
155
+ } else if (e.type === "command" && ((g = e == null ? void 0 : e.payload) == null ? void 0 : g.command) == "set_local_tools") {
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,8 +166,8 @@ class _ extends v {
165
166
  }
166
167
  const c = e.payload;
167
168
  try {
168
- const C = await (await fetch(`${s}/sessions/${r}/messages`, {
169
- body: JSON.stringify({ agentId: o, userMessage: c, initialContext: this.context, tools: this.tools }),
169
+ const N = await (await fetch(`${s}/sessions/${r}/messages`, {
170
+ body: JSON.stringify({ agentId: a, userMessage: c, initialContext: this.context, tools: this.tools }),
170
171
  method: "POST",
171
172
  headers: {
172
173
  "Content-Type": "application/json",
@@ -174,12 +175,12 @@ class _ extends v {
174
175
  }
175
176
  })).json();
176
177
  this.notifyPackets(
177
- C.response.messages.map(($) => ({
178
+ N.response.messages.map((T) => ({
178
179
  type: "message",
179
- payload: $
180
+ payload: T
180
181
  }))
181
182
  );
182
- } catch (T) {
183
+ } catch (w) {
183
184
  this.notifyPacket({
184
185
  type: "message",
185
186
  payload: {
@@ -187,11 +188,11 @@ class _ extends v {
187
188
  type: "text",
188
189
  text: "An error occurred while processing your request. Please try again later."
189
190
  }
190
- }), (S = this.config.logger) == null || S.error("Error sending packet:", T);
191
+ }), (A = this.config.logger) == null || A.error("Error sending packet:", w);
191
192
  }
192
193
  }
193
194
  }
194
- class U extends v {
195
+ class U extends $ {
195
196
  constructor(e) {
196
197
  super();
197
198
  i(this, "status");
@@ -217,25 +218,25 @@ class U 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), o = this.config.agentId, r = `${this.config.webSocketUrl}?sessionCode=${s}&agentId=${o}&apiKey=${n}`;
221
+ const n = encodeURIComponent(this.config.apiKey), a = this.config.agentId, r = `${this.config.webSocketUrl}?sessionCode=${s}&agentId=${a}&apiKey=${n}`;
221
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,7 +247,7 @@ class U 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 oe {
250
+ class re {
250
251
  constructor(t) {
251
252
  i(this, "config");
252
253
  i(this, "pc", null);
@@ -292,14 +293,14 @@ class oe {
292
293
  n.streams[0].getTracks().forEach((r) => {
293
294
  this.remoteStream.addTrack(r);
294
295
  }), this.audioCtx || this._startAnalyzers();
295
- const o = new Audio();
296
- o.srcObject = this.remoteStream, o.play().catch((r) => {
296
+ const a = new Audio();
297
+ a.srcObject = this.remoteStream, a.play().catch((r) => {
297
298
  var c;
298
299
  (c = this.config.logger) == null || c.error("Error playing remote audio:", r);
299
300
  });
300
301
  }, this.pc.onicecandidate = (n) => {
301
- var o;
302
- n.candidate && ((o = this.ws) == null ? void 0 : o.readyState) === WebSocket.OPEN && this.ws.send(
302
+ var a;
303
+ n.candidate && ((a = this.ws) == null ? void 0 : a.readyState) === WebSocket.OPEN && this.ws.send(
303
304
  JSON.stringify({
304
305
  type: "CANDIDATE",
305
306
  src: "client",
@@ -307,57 +308,57 @@ class oe {
307
308
  })
308
309
  );
309
310
  }, this.pc.ondatachannel = (n) => {
310
- const o = n.channel;
311
- o.onmessage = (r) => {
311
+ const a = n.channel;
312
+ a.onmessage = (r) => {
312
313
  this.messageCallbacks.forEach((c) => {
313
314
  c(r);
314
315
  });
315
- }, o.onopen = () => {
316
+ }, a.onopen = () => {
316
317
  var r;
317
318
  for (; this.queuedMessages.length > 0; ) {
318
319
  const c = this.queuedMessages.shift();
319
- c && (o.send(JSON.stringify(c)), (r = this.config.logger) == null || r.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
- const o = {
329
+ const a = {
329
330
  apiKey: this.config.apiKey,
330
331
  agentId: this.config.agentId,
331
332
  sessionCode: t
332
333
  };
333
- (c = this.config.logger) == null || c.debug("Opening connection to WebRTC server: ", o);
334
+ (c = this.config.logger) == null || c.debug("Opening connection to WebRTC server: ", a);
334
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,
340
341
  type: n.type
341
342
  },
342
343
  connectionId: (Date.now() % 1e6).toString(),
343
- metadata: o
344
+ metadata: a
344
345
  }
345
346
  };
346
347
  this.ws.send(JSON.stringify(r));
347
348
  }, this.ws.onmessage = async (n) => {
348
349
  var r;
349
- const o = JSON.parse(n.data);
350
- if (o.type === "ANSWER")
351
- await this.pc.setRemoteDescription(new RTCSessionDescription(o.payload.sdp));
352
- else if (o.type === "CANDIDATE")
350
+ const a = JSON.parse(n.data);
351
+ if (a.type === "ANSWER")
352
+ await this.pc.setRemoteDescription(new RTCSessionDescription(a.payload.sdp));
353
+ else if (a.type === "CANDIDATE")
353
354
  try {
354
- await this.pc.addIceCandidate(new RTCIceCandidate(o.payload.candidate));
355
+ await this.pc.addIceCandidate(new RTCIceCandidate(a.payload.candidate));
355
356
  } catch (c) {
356
357
  (r = this.config.logger) == null || r.error("Error adding ICE candidate:", c);
357
358
  }
358
359
  }, this.ws.onclose = (n) => {
359
- n.code !== 1e3 && this.errorCallbacks.forEach((o) => {
360
- o("Oops! The connection to the server was lost. Please try again later.");
360
+ n.code !== 1e3 && this.errorCallbacks.forEach((a) => {
361
+ a("Oops! The connection to the server was lost. Please try again later.");
361
362
  }), this._stopAnalyzers();
362
363
  };
363
364
  }
@@ -404,11 +405,11 @@ class oe {
404
405
  const s = () => {
405
406
  if (!this.localAnalyser || !this.remoteAnalyser || this.visualizerCallbacks.length === 0)
406
407
  return;
407
- const n = new Uint8Array(this.localAnalyser.frequencyBinCount), o = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
408
- this.localAnalyser.getByteFrequencyData(n), this.remoteAnalyser.getByteFrequencyData(o);
409
- const r = n.reduce((u, d) => u + d, 0) / n.length, c = o.reduce((u, d) => u + d, 0) / o.length;
410
- this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((u) => {
411
- u({
408
+ const n = new Uint8Array(this.localAnalyser.frequencyBinCount), a = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
409
+ this.localAnalyser.getByteFrequencyData(n), this.remoteAnalyser.getByteFrequencyData(a);
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({
412
413
  localAmplitude: r,
413
414
  remoteAmplitude: c
414
415
  });
@@ -420,7 +421,7 @@ class oe {
420
421
  this.analyzerFrame && (cancelAnimationFrame(this.analyzerFrame), this.analyzerFrame = null), this.localAnalyser = null, this.remoteAnalyser = null;
421
422
  }
422
423
  }
423
- class W extends v {
424
+ class F extends $ {
424
425
  constructor(e) {
425
426
  super();
426
427
  i(this, "status");
@@ -428,7 +429,7 @@ class W extends v {
428
429
  i(this, "autostart");
429
430
  i(this, "config");
430
431
  i(this, "webRTCClient");
431
- this.config = e, this.status = "disconnected", this.session = null, this.autostart = (e == null ? void 0 : e.autostart) ?? !1, this.webRTCClient = new oe(e), this.webRTCClient.addMessageCallback((s) => {
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) => {
@@ -466,7 +467,7 @@ class W extends v {
466
467
  return this.status !== "connected" ? Promise.reject(new Error("Not connected")) : (this.webRTCClient.sendPacket(e), Promise.resolve());
467
468
  }
468
469
  }
469
- class re {
470
+ class ie {
470
471
  constructor(t) {
471
472
  i(this, "config");
472
473
  this.config = t;
@@ -491,7 +492,7 @@ class re {
491
492
  });
492
493
  }
493
494
  }
494
- class ie {
495
+ class ce {
495
496
  constructor(t, e) {
496
497
  i(this, "transaction");
497
498
  i(this, "manager");
@@ -507,14 +508,14 @@ class ie {
507
508
  await this.fail("No function call found");
508
509
  return;
509
510
  }
510
- const s = e.name, n = e.args, o = t[s];
511
- if (!o) {
511
+ const s = e.name, n = e.args, a = t[s];
512
+ if (!a) {
512
513
  await this.fail(`Tool ${s} not found`);
513
514
  return;
514
515
  }
515
516
  try {
516
- const u = (((r = o.toString().replace(/\n/g, " ").match(/^[^(]*\(([^)]*)\)/)) == null ? void 0 : r[1].split(",").map((y) => y.trim()).filter(Boolean)) || []).map((y) => n[y]), d = await o.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,7 +527,7 @@ class ie {
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
533
  i(this, "status");
@@ -561,179 +562,195 @@ class ce extends v {
561
562
  }
562
563
  onTransaction(e) {
563
564
  console.log("transaction received:", e);
564
- const s = new re(this.config), n = new ie(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 R = Y(void 0);
572
- function le(a) {
572
+ const R = G(void 0);
573
+ function ue(o) {
573
574
  return new Promise((t, e) => {
574
575
  const s = new FileReader();
575
- s.readAsDataURL(a), s.onload = () => {
576
- const o = s.result.split(";base64,")[1];
577
- t(o);
576
+ s.readAsDataURL(o), s.onload = () => {
577
+ const a = s.result.split(";base64,")[1];
578
+ t(a);
578
579
  }, s.onerror = (n) => {
579
580
  e(n);
580
581
  };
581
582
  });
582
583
  }
583
- function ue({
584
- dev: a = !1,
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({
598
+ dev: o = !1,
585
599
  baseUrl: t,
586
600
  protocols: e,
587
601
  logger: s,
588
602
  children: n,
589
- session: o = "new",
603
+ session: a = "new",
590
604
  ...r
591
605
  }) {
592
- const [c, u] = A(!1), [d, y] = A([]), [S, T] = A(o), [C, $] = A(/* @__PURE__ */ new Map()), q = G(!1), b = 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 = a ? "localhost:8000" : t || "persona.applica.guru/api", m = a ? "http" : "https", p = a ? "ws" : "wss";
597
- let g = Object.keys(e).map((f) => {
598
- switch (f) {
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 w = e[f];
601
- return w === !0 ? new _({
602
- apiUrl: `${m}://${l}`,
614
+ const b = e[m];
615
+ return b === !0 ? new _({
616
+ apiUrl: `${h}://${y}`,
603
617
  apiKey: r.apiKey,
604
618
  agentId: r.agentId,
605
619
  logger: s
606
- }) : typeof w == "object" && w !== null ? new _(w) : null;
620
+ }) : typeof b == "object" && b !== null ? new _(b) : null;
607
621
  case "webrtc":
608
- const h = e[f];
609
- return h === !0 ? new W({
610
- webrtcUrl: `${p}://${l}/webrtc`,
622
+ const u = e[m];
623
+ return u === !0 ? new F({
624
+ webrtcUrl: `${f}://${y}/webrtc`,
611
625
  apiKey: r.apiKey,
612
626
  agentId: r.agentId,
613
627
  logger: s
614
- }) : typeof h == "object" && h !== null ? new W(h) : null;
628
+ }) : typeof u == "object" && u !== null ? new F(u) : null;
615
629
  case "websocket":
616
- const P = e[f];
630
+ const P = e[m];
617
631
  return P === !0 ? new U({
618
- webSocketUrl: `${p}://${l}/websocket`,
632
+ webSocketUrl: `${f}://${y}/websocket`,
619
633
  apiKey: r.apiKey,
620
634
  agentId: r.agentId,
621
635
  logger: s
622
636
  }) : typeof P == "object" && P !== null ? new U(P) : null;
623
637
  default:
624
- throw new Error(`Unknown protocol: ${f}`);
638
+ throw new Error(`Unknown protocol: ${m}`);
625
639
  }
626
- }).filter((f) => f !== null);
627
- return r.tools && g.push(
628
- new ce({
629
- apiUrl: `${m}://${l}`,
640
+ }).filter((m) => m !== null);
641
+ return r.tools && d.push(
642
+ new le({
643
+ apiUrl: `${h}://${y}`,
630
644
  apiKey: r.apiKey,
631
645
  agentId: r.agentId,
632
646
  tools: r.tools,
633
647
  // Pass raw tools
634
648
  logger: s
635
649
  })
636
- ), g;
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
- b.map((l) => l.getName())
644
- ), b.forEach((l) => {
645
- l.setSession(S), l.clearListeners(), l.addStatusChangeListener((m) => {
646
- s == null || s.debug(`${l.getName()} has notified new status: ${m}`), C.set(l.getName(), m), m === "connected" && (r.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
667
  arguments: r.context
651
668
  }
652
- }), r.tools && Array.isArray(r.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: r.tools.map((p) => p.schema)
674
+ tools: r.tools.map((d) => d.schema)
658
675
  }
659
676
  }
660
- })), $(new Map(C));
661
- }), l.addPacketListener((m) => {
662
- if (m.type === "message") {
663
- const p = m.payload;
664
- y(
665
- (g) => j([...g, { ...p, protocol: l.getName() }])
677
+ })), N(new Map(w));
678
+ }), h.addPacketListener((f) => {
679
+ if (f.type === "message") {
680
+ const d = f.payload;
681
+ g(
682
+ (m) => j([...m, { ...d, protocol: h.getName() }])
666
683
  );
667
- } else if (m.type === "reasoning") {
668
- const p = m.payload;
669
- let g = p.thought;
670
- p.imageUrl && (g += `
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/${p.imageUrl})`);
673
- const f = { type: "reasoning", text: g, role: "assistant", finishReason: "stop" };
674
- y(
675
- (w) => j([...w, { ...f, protocol: l.getName() }])
689
+ ![image](${y}/api/files/${d.imageUrl})`);
690
+ const b = { type: "reasoning", text: m, role: "assistant", finishReason: "stop" };
691
+ g(
692
+ (u) => j([...u, { ...b, protocol: h.getName() }])
676
693
  );
677
- } else m.type === "transaction" && b.filter((p) => p !== l).forEach((p) => p.onTransaction(m.payload));
678
- }), l.autostart && l.status === "disconnected" && (s == null || s.debug(`Connecting to protocol: ${l.getName()}`), l.connect(S));
679
- }));
680
- }, [S, b, s, C]);
681
- const F = async (l) => {
682
- var w;
683
- if (((w = l.content[0]) == null ? void 0 : w.type) !== "text") throw new Error("Only text messages are supported");
684
- if (!b.some((h) => h.getName() !== "transaction" && h.status === "connected")) {
685
- y((h) => [
686
- ...h,
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,
687
704
  { role: "assistant", type: "text", text: "No protocol is connected." }
688
705
  ]);
689
706
  return;
690
707
  }
691
- const p = l.content[0].text;
692
- y((h) => [...h, { role: "user", type: "text", text: p }]), u(!0);
693
- const g = b.sort((h, P) => P.getPriority() - h.getPriority()).find((h) => h.status === "connected"), f = [];
694
- if (l.attachments)
695
- for (const h of l.attachments)
696
- h.contentType.startsWith("image/") && h.file && f.push({
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({
697
714
  role: "user",
698
715
  image: {
699
- contentType: h.contentType,
700
- content: await le(h.file)
716
+ contentType: u.contentType,
717
+ content: await ue(u.file)
701
718
  },
702
719
  text: "",
703
720
  type: "text"
704
721
  });
705
- l.content && f.push({
722
+ y.content && m.push({
706
723
  role: "user",
707
- text: l.content[0].text,
724
+ text: y.content[0].text,
708
725
  type: "text"
709
- }), s == null || s.debug("Sending message:", f), await (g == null ? void 0 : g.sendPacket({ type: "request", payload: f })), u(!1);
710
- }, D = N(() => (u(!1), y([]), T("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({
711
728
  isRunning: c,
712
- messages: d,
729
+ messages: p,
713
730
  convertMessage: ae,
714
- onNew: F,
715
- onCancel: D,
716
- onReload: L,
731
+ onNew: D,
732
+ onCancel: L,
733
+ onReload: J,
717
734
  adapters: {
718
- attachments: new Z([new ee()])
735
+ attachments: new ee([new te()])
719
736
  }
720
737
  });
721
- return /* @__PURE__ */ M.jsx(R.Provider, { value: { protocols: b, protocolsStatus: C, 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 }) });
722
739
  }
723
- function ge({ children: a, ...t }) {
724
- return /* @__PURE__ */ M.jsx(ue, { ...t, children: a });
740
+ function we({ children: o, ...t }) {
741
+ return /* @__PURE__ */ I.jsx(he, { ...t, children: o });
725
742
  }
726
- function we() {
727
- const a = E(R);
728
- if (!a)
743
+ function be() {
744
+ const o = v(R);
745
+ if (!o)
729
746
  throw new Error("usePersonaRuntime must be used within a PersonaRuntimeProvider");
730
- return a;
747
+ return o;
731
748
  }
732
- function he(a) {
733
- const t = E(R);
749
+ function de(o) {
750
+ const t = v(R);
734
751
  if (!t)
735
752
  throw new Error("usePersonaRuntimeProtocol must be used within a PersonaRuntimeProvider");
736
- const e = t.protocols.find((n) => n.getName() === a);
753
+ const e = t.protocols.find((n) => n.getName() === o);
737
754
  if (!e)
738
755
  return null;
739
756
  const s = t.protocolsStatus.get(e.getName());
@@ -750,32 +767,22 @@ function he(a) {
750
767
  status: s || e.status
751
768
  };
752
769
  }
753
- function be() {
754
- const a = E(R);
755
- if (!a)
756
- throw new Error("usePersonaRuntimeEndpoint must be used within a PersonaRuntimeProvider");
757
- const t = {
758
- rest: (e) => e.config.apiUrl,
759
- webrtc: (e) => e.config.webrtcUrl.replace("/webrtc", "").replace("ws://", "http://").replace("wss://", "https://"),
760
- websocket: (e) => e.config.webSocketUrl.replace("/websocket", "").replace("ws://", "http://").replace("wss://", "https://")
761
- };
762
- for (const e of a.protocols) {
763
- const s = t[e.getName()];
764
- if (s)
765
- return s(e);
766
- }
767
- return null;
768
- }
769
770
  function Se() {
770
- return he("webrtc");
771
+ const o = v(R);
772
+ if (!o)
773
+ throw new Error("usePersonaRuntimeEndpoint must be used within a PersonaRuntimeProvider");
774
+ return z(o.protocols);
771
775
  }
772
776
  function Ce() {
773
- const a = E(R);
774
- if (!a)
777
+ return de("webrtc");
778
+ }
779
+ function Pe() {
780
+ const o = v(R);
781
+ if (!o)
775
782
  throw new Error("usePersonaRuntimeMessages must be used within a PersonaRuntimeProvider");
776
- return a.getMessages();
783
+ return o.getMessages();
777
784
  }
778
- class Pe {
785
+ class xe {
779
786
  constructor() {
780
787
  i(this, "prefix", "[Persona]");
781
788
  }
@@ -795,129 +802,129 @@ class Pe {
795
802
  console.debug(`${this.prefix} - ${t}`, ...e);
796
803
  }
797
804
  }
798
- function k(a, t, e) {
805
+ function x(o, t, e) {
799
806
  return {
800
- type: a,
807
+ type: o,
801
808
  description: t,
802
809
  ...e
803
810
  };
804
811
  }
805
- function de(a) {
806
- const t = Object.entries(a.parameters).filter(([e, s]) => s.required).map(([e]) => e);
812
+ function pe(o) {
813
+ const t = Object.entries(o.parameters).filter(([e, s]) => s.required).map(([e]) => e);
807
814
  return {
808
815
  type: "local",
809
- name: a.name,
810
- description: a.description,
816
+ name: o.name,
817
+ description: o.description,
811
818
  config: {
812
- timeout: a.timeout || 60,
819
+ timeout: o.timeout || 60,
813
820
  parameters: {
814
821
  type: "object",
815
- title: a.title || `${a.name} parameters`,
822
+ title: o.title || `${o.name} parameters`,
816
823
  required: t,
817
- properties: a.parameters
824
+ properties: o.parameters
818
825
  },
819
826
  output: {
820
827
  type: "object",
821
- title: `${a.name} output`,
822
- properties: a.output
828
+ title: `${o.name} output`,
829
+ properties: o.output
823
830
  }
824
831
  }
825
832
  };
826
833
  }
827
- function I(a) {
834
+ function q(o) {
828
835
  return {
829
- schema: de(a),
830
- implementation: a.implementation
836
+ schema: pe(o),
837
+ implementation: o.implementation
831
838
  };
832
839
  }
833
- function pe(a, t, e, s, n, o) {
840
+ function me(o, t, e, s, n, a) {
834
841
  const r = {
835
- name: a,
842
+ name: o,
836
843
  description: t,
837
- title: o == null ? void 0 : o.title,
838
- timeout: o == null ? void 0 : o.timeout,
844
+ title: a == null ? void 0 : a.title,
845
+ timeout: a == null ? void 0 : a.timeout,
839
846
  parameters: s,
840
847
  output: n,
841
848
  implementation: e
842
849
  };
843
- return I(r);
850
+ return q(r);
844
851
  }
845
- const ke = pe(
852
+ const ke = me(
846
853
  "sum",
847
854
  "Sum two numbers",
848
855
  function(t, e) {
849
856
  return { result: t + e };
850
857
  },
851
858
  {
852
- a: k("number", "First number to sum", { required: !0 }),
853
- b: k("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 })
854
861
  },
855
862
  {
856
- result: k("number", "Sum of two numbers")
863
+ result: x("number", "Sum of two numbers")
857
864
  }
858
- ), xe = I({
865
+ ), Re = q({
859
866
  name: "navigate_to",
860
867
  description: "Allow agent to redirect user to specific sub page like /foo or #/foo or anything like that",
861
868
  title: "Sum two numbers",
862
869
  // As per the user's example
863
870
  timeout: 60,
864
871
  parameters: {
865
- a: k("number", "First number to sum"),
866
- b: k("number", "Seconth number to sum")
872
+ a: x("number", "First number to sum"),
873
+ b: x("number", "Seconth number to sum")
867
874
  // Keeping the typo as in the original
868
875
  },
869
876
  output: {
870
- result: k("number", "Sum of two numbers")
877
+ result: x("number", "Sum of two numbers")
871
878
  },
872
879
  implementation: function(t, e) {
873
880
  return { result: t + e };
874
881
  }
875
882
  });
876
- function Re(a) {
883
+ function Ae(o) {
877
884
  const t = [], e = {};
878
- return a.forEach((s) => {
879
- const { schema: n, implementation: o } = I(s);
880
- t.push(n), e[s.name] = o;
885
+ return o.forEach((s) => {
886
+ const { schema: n, implementation: a } = q(s);
887
+ t.push(n), e[s.name] = a;
881
888
  }), { schemas: t, implementations: e };
882
889
  }
883
- function Te(a, t) {
890
+ function Te(o, t) {
884
891
  const { required: e, properties: s } = t.config.parameters;
885
892
  for (const n of e)
886
- if (!(n in a))
893
+ if (!(n in o))
887
894
  throw new Error(`Missing required parameter: ${n}`);
888
- for (const [n, o] of Object.entries(a)) {
895
+ for (const [n, a] of Object.entries(o)) {
889
896
  const r = s[n];
890
897
  if (r) {
891
- if (r.type === "number" && typeof o != "number")
898
+ if (r.type === "number" && typeof a != "number")
892
899
  throw new Error(`Parameter ${n} should be a number`);
893
- if (r.type === "string" && typeof o != "string")
900
+ if (r.type === "string" && typeof a != "string")
894
901
  throw new Error(`Parameter ${n} should be a string`);
895
- if (r.type === "boolean" && typeof o != "boolean")
902
+ if (r.type === "boolean" && typeof a != "boolean")
896
903
  throw new Error(`Parameter ${n} should be a boolean`);
897
904
  }
898
905
  }
899
906
  return !0;
900
907
  }
901
908
  export {
902
- Pe as PersonaConsoleLogger,
903
- v as PersonaProtocolBase,
909
+ xe as PersonaConsoleLogger,
910
+ $ as PersonaProtocolBase,
904
911
  _ as PersonaRESTProtocol,
905
- ge as PersonaRuntimeProvider,
906
- ce as PersonaTransactionProtocol,
907
- W as PersonaWebRTCProtocol,
912
+ we as PersonaRuntimeProvider,
913
+ le as PersonaTransactionProtocol,
914
+ F as PersonaWebRTCProtocol,
908
915
  U as PersonaWebSocketProtocol,
909
- k as createParameter,
910
- I as createTool,
911
- pe as createToolFromFunction,
912
- Re as createToolRegistry,
913
- de as generateToolSchema,
914
- xe as navigateToToolExample,
916
+ x as createParameter,
917
+ q as createTool,
918
+ me as createToolFromFunction,
919
+ Ae as createToolRegistry,
920
+ pe as generateToolSchema,
921
+ Re as navigateToToolExample,
915
922
  ke as sumTool,
916
- we as usePersonaRuntime,
917
- be as usePersonaRuntimeEndpoint,
918
- Ce as usePersonaRuntimeMessages,
919
- he as usePersonaRuntimeProtocol,
920
- Se as usePersonaRuntimeWebRTCProtocol,
923
+ be as usePersonaRuntime,
924
+ Se as usePersonaRuntimeEndpoint,
925
+ Pe as usePersonaRuntimeMessages,
926
+ de as usePersonaRuntimeProtocol,
927
+ Ce as usePersonaRuntimeWebRTCProtocol,
921
928
  Te as validateToolParameters
922
929
  };
923
930
  //# sourceMappingURL=bundle.es.js.map