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

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
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";
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
5
  import { useExternalStoreRuntime as X, CompositeAttachmentAdapter as Z, SimpleImageAttachmentAdapter as ee, AssistantRuntimeProvider as te } from "@assistant-ui/react";
6
- var F = { exports: {} }, k = {};
6
+ var z = { exports: {} }, x = {};
7
7
  /**
8
8
  * @license React
9
9
  * react-jsx-runtime.production.js
@@ -15,38 +15,38 @@ var F = { exports: {} }, k = {};
15
15
  */
16
16
  var O;
17
17
  function se() {
18
- if (O) return k;
18
+ if (O) return x;
19
19
  O = 1;
20
- var o = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
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) {
24
- a = {};
20
+ var a = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
21
+ function e(s, n, o) {
22
+ var r = null;
23
+ if (o !== void 0 && (r = "" + o), n.key !== void 0 && (r = "" + n.key), "key" in n) {
24
+ o = {};
25
25
  for (var c in n)
26
- c !== "key" && (a[c] = n[c]);
27
- } else a = n;
28
- return n = a.ref, {
29
- $$typeof: o,
26
+ c !== "key" && (o[c] = n[c]);
27
+ } else o = n;
28
+ return n = o.ref, {
29
+ $$typeof: a,
30
30
  type: s,
31
- key: i,
31
+ key: r,
32
32
  ref: n !== void 0 ? n : null,
33
- props: a
33
+ props: o
34
34
  };
35
35
  }
36
- return k.Fragment = t, k.jsx = e, k.jsxs = e, k;
36
+ return x.Fragment = t, x.jsx = e, x.jsxs = e, x;
37
37
  }
38
- F.exports = se();
39
- var M = F.exports;
40
- function ne(o) {
41
- return o.filter((t) => {
38
+ z.exports = se();
39
+ var M = z.exports;
40
+ function ne(a) {
41
+ return a.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(o) {
46
+ function j(a) {
47
47
  const t = [];
48
48
  let e = null;
49
- for (const n of o)
49
+ for (const n of a)
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
@@ -55,39 +55,39 @@ function j(o) {
55
55
  }));
56
56
  return e && t.push(e), ne(t);
57
57
  }
58
- function oe(o) {
58
+ function ae(a) {
59
59
  var e, s;
60
- const t = ((e = o.files) == null ? void 0 : e.map(
60
+ const t = ((e = a.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 o.role === "function" ? {
68
- id: o.id,
67
+ return a.role === "function" ? {
68
+ id: a.id,
69
69
  role: "assistant",
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;
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;
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: (a = o.functionResponse) == null ? void 0 : a.result
78
+ result: (o = a.functionResponse) == null ? void 0 : o.result
79
79
  };
80
80
  })) ?? []
81
81
  } : {
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]
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]
85
85
  };
86
86
  }
87
87
  class v {
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);
@@ -122,13 +122,13 @@ class v {
122
122
  class _ extends v {
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,12 +147,12 @@ 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";
150
+ var u, d, y, S;
151
+ const { apiUrl: s, apiKey: n, agentId: o } = this.config, r = this.session ?? "new";
152
152
  if (e.type === "command" && ((u = e == null ? void 0 : e.payload) == null ? void 0 : u.command) == "set_initial_context") {
153
153
  this.context = (d = e == null ? void 0 : e.payload) == null ? void 0 : d.arguments;
154
154
  return;
155
- } else if (e.type === "command" && ((g = e == null ? void 0 : e.payload) == null ? void 0 : g.command) == "set_local_tools") {
155
+ } else if (e.type === "command" && ((y = e == null ? void 0 : e.payload) == null ? void 0 : y.command) == "set_local_tools") {
156
156
  this.notifyPacket({
157
157
  type: "message",
158
158
  payload: {
@@ -165,8 +165,8 @@ class _ extends v {
165
165
  }
166
166
  const c = e.payload;
167
167
  try {
168
- const b = await (await fetch(`${s}/sessions/${i}/messages`, {
169
- body: JSON.stringify({ agentId: a, userMessage: c, initialContext: this.context, tools: this.tools }),
168
+ const C = await (await fetch(`${s}/sessions/${r}/messages`, {
169
+ body: JSON.stringify({ agentId: o, userMessage: c, initialContext: this.context, tools: this.tools }),
170
170
  method: "POST",
171
171
  headers: {
172
172
  "Content-Type": "application/json",
@@ -174,12 +174,12 @@ class _ extends v {
174
174
  }
175
175
  })).json();
176
176
  this.notifyPackets(
177
- b.response.messages.map(($) => ({
177
+ C.response.messages.map(($) => ({
178
178
  type: "message",
179
179
  payload: $
180
180
  }))
181
181
  );
182
- } catch (R) {
182
+ } catch (T) {
183
183
  this.notifyPacket({
184
184
  type: "message",
185
185
  payload: {
@@ -187,18 +187,18 @@ class _ extends v {
187
187
  type: "text",
188
188
  text: "An error occurred while processing your request. Please try again later."
189
189
  }
190
- }), (w = this.config.logger) == null || w.error("Error sending packet:", R);
190
+ }), (S = this.config.logger) == null || S.error("Error sending packet:", T);
191
191
  }
192
192
  }
193
193
  }
194
- class W extends v {
194
+ class U extends v {
195
195
  constructor(e) {
196
196
  super();
197
- r(this, "status");
198
- r(this, "autostart");
199
- r(this, "session");
200
- r(this, "config");
201
- r(this, "webSocket");
197
+ i(this, "status");
198
+ i(this, "autostart");
199
+ i(this, "session");
200
+ i(this, "config");
201
+ i(this, "webSocket");
202
202
  this.config = e, this.status = "disconnected", this.autostart = !0, this.session = null, this.webSocket = null;
203
203
  }
204
204
  getName() {
@@ -217,8 +217,8 @@ class W extends v {
217
217
  return Promise.resolve(this.session);
218
218
  const s = e || this.session || "new";
219
219
  (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", () => {
220
+ const n = encodeURIComponent(this.config.apiKey), o = this.config.agentId, r = `${this.config.webSocketUrl}?sessionCode=${s}&agentId=${o}&apiKey=${n}`;
221
+ return this.setStatus("connecting"), this.webSocket = new WebSocket(r), this.webSocket.addEventListener("open", () => {
222
222
  this.setStatus("connected");
223
223
  }), this.webSocket.addEventListener("message", (u) => {
224
224
  const d = JSON.parse(u.data);
@@ -246,23 +246,23 @@ class W extends v {
246
246
  return this.webSocket && this.status === "connected" ? (this.webSocket.send(JSON.stringify(e)), Promise.resolve()) : Promise.reject(new Error("WebSocket is not connected"));
247
247
  }
248
248
  }
249
- class ae {
249
+ class oe {
250
250
  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", []);
251
+ i(this, "config");
252
+ i(this, "pc", null);
253
+ i(this, "ws", null);
254
+ i(this, "localStream", null);
255
+ i(this, "remoteStream", new MediaStream());
256
+ i(this, "audioCtx", null);
257
+ i(this, "localAnalyser", null);
258
+ i(this, "remoteAnalyser", null);
259
+ i(this, "analyzerFrame", null);
260
+ i(this, "dataChannel", null);
261
+ i(this, "isConnected", !1);
262
+ i(this, "visualizerCallbacks", []);
263
+ i(this, "messageCallbacks", []);
264
+ i(this, "errorCallbacks", []);
265
+ i(this, "queuedMessages", []);
266
266
  this.config = t;
267
267
  }
268
268
  async connect(t) {
@@ -289,17 +289,17 @@ class ae {
289
289
  }), this.localStream.getTracks().forEach((n) => {
290
290
  this.pc.addTrack(n, this.localStream);
291
291
  }), this.pc.ontrack = (n) => {
292
- n.streams[0].getTracks().forEach((i) => {
293
- this.remoteStream.addTrack(i);
292
+ n.streams[0].getTracks().forEach((r) => {
293
+ this.remoteStream.addTrack(r);
294
294
  }), this.audioCtx || this._startAnalyzers();
295
- const a = new Audio();
296
- a.srcObject = this.remoteStream, a.play().catch((i) => {
295
+ const o = new Audio();
296
+ o.srcObject = this.remoteStream, o.play().catch((r) => {
297
297
  var c;
298
- (c = this.config.logger) == null || c.error("Error playing remote audio:", i);
298
+ (c = this.config.logger) == null || c.error("Error playing remote audio:", r);
299
299
  });
300
300
  }, this.pc.onicecandidate = (n) => {
301
- var a;
302
- n.candidate && ((a = this.ws) == null ? void 0 : a.readyState) === WebSocket.OPEN && this.ws.send(
301
+ var o;
302
+ n.candidate && ((o = this.ws) == null ? void 0 : o.readyState) === WebSocket.OPEN && this.ws.send(
303
303
  JSON.stringify({
304
304
  type: "CANDIDATE",
305
305
  src: "client",
@@ -307,16 +307,16 @@ class ae {
307
307
  })
308
308
  );
309
309
  }, this.pc.ondatachannel = (n) => {
310
- const a = n.channel;
311
- a.onmessage = (i) => {
310
+ const o = n.channel;
311
+ o.onmessage = (r) => {
312
312
  this.messageCallbacks.forEach((c) => {
313
- c(i);
313
+ c(r);
314
314
  });
315
- }, a.onopen = () => {
316
- var i;
315
+ }, o.onopen = () => {
316
+ var r;
317
317
  for (; this.queuedMessages.length > 0; ) {
318
318
  const c = this.queuedMessages.shift();
319
- c && (a.send(JSON.stringify(c)), (i = this.config.logger) == null || i.info("Sent queued message:", c));
319
+ c && (o.send(JSON.stringify(c)), (r = this.config.logger) == null || r.info("Sent queued message:", c));
320
320
  }
321
321
  };
322
322
  };
@@ -325,13 +325,13 @@ class ae {
325
325
  var c, u;
326
326
  const n = await this.pc.createOffer();
327
327
  await this.pc.setLocalDescription(n);
328
- const a = {
328
+ const o = {
329
329
  apiKey: this.config.apiKey,
330
330
  agentId: this.config.agentId,
331
331
  sessionCode: t
332
332
  };
333
- (c = this.config.logger) == null || c.debug("Opening connection to WebRTC server: ", a);
334
- const i = {
333
+ (c = this.config.logger) == null || c.debug("Opening connection to WebRTC server: ", o);
334
+ const r = {
335
335
  type: "OFFER",
336
336
  src: ((u = crypto.randomUUID) == null ? void 0 : u.call(crypto)) || "client_" + Date.now(),
337
337
  payload: {
@@ -340,24 +340,24 @@ class ae {
340
340
  type: n.type
341
341
  },
342
342
  connectionId: (Date.now() % 1e6).toString(),
343
- metadata: a
343
+ metadata: o
344
344
  }
345
345
  };
346
- this.ws.send(JSON.stringify(i));
346
+ this.ws.send(JSON.stringify(r));
347
347
  }, this.ws.onmessage = async (n) => {
348
- var i;
349
- const a = JSON.parse(n.data);
350
- if (a.type === "ANSWER")
351
- await this.pc.setRemoteDescription(new RTCSessionDescription(a.payload.sdp));
352
- else if (a.type === "CANDIDATE")
348
+ 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")
353
353
  try {
354
- await this.pc.addIceCandidate(new RTCIceCandidate(a.payload.candidate));
354
+ await this.pc.addIceCandidate(new RTCIceCandidate(o.payload.candidate));
355
355
  } catch (c) {
356
- (i = this.config.logger) == null || i.error("Error adding ICE candidate:", c);
356
+ (r = this.config.logger) == null || r.error("Error adding ICE candidate:", c);
357
357
  }
358
358
  }, this.ws.onclose = (n) => {
359
- n.code !== 1e3 && this.errorCallbacks.forEach((a) => {
360
- a("Oops! The connection to the server was lost. Please try again later.");
359
+ n.code !== 1e3 && this.errorCallbacks.forEach((o) => {
360
+ o("Oops! The connection to the server was lost. Please try again later.");
361
361
  }), this._stopAnalyzers();
362
362
  };
363
363
  }
@@ -404,12 +404,12 @@ class ae {
404
404
  const s = () => {
405
405
  if (!this.localAnalyser || !this.remoteAnalyser || this.visualizerCallbacks.length === 0)
406
406
  return;
407
- const n = new Uint8Array(this.localAnalyser.frequencyBinCount), a = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
408
- 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;
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
410
  this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((u) => {
411
411
  u({
412
- localAmplitude: i,
412
+ localAmplitude: r,
413
413
  remoteAmplitude: c
414
414
  });
415
415
  }), this.analyzerFrame = requestAnimationFrame(s);
@@ -420,15 +420,15 @@ class ae {
420
420
  this.analyzerFrame && (cancelAnimationFrame(this.analyzerFrame), this.analyzerFrame = null), this.localAnalyser = null, this.remoteAnalyser = null;
421
421
  }
422
422
  }
423
- class z extends v {
423
+ class W extends v {
424
424
  constructor(e) {
425
425
  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) => {
426
+ i(this, "status");
427
+ i(this, "session");
428
+ i(this, "autostart");
429
+ i(this, "config");
430
+ 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
432
  const n = JSON.parse(s.data);
433
433
  this.notifyPacket(n);
434
434
  }), this.webRTCClient.addErrorCallback((s) => {
@@ -466,9 +466,9 @@ class z extends v {
466
466
  return this.status !== "connected" ? Promise.reject(new Error("Not connected")) : (this.webRTCClient.sendPacket(e), Promise.resolve());
467
467
  }
468
468
  }
469
- class ie {
469
+ class re {
470
470
  constructor(t) {
471
- r(this, "config");
471
+ i(this, "config");
472
472
  this.config = t;
473
473
  }
474
474
  async complete(t, e) {
@@ -491,29 +491,29 @@ class ie {
491
491
  });
492
492
  }
493
493
  }
494
- class re {
494
+ class ie {
495
495
  constructor(t, e) {
496
- r(this, "transaction");
497
- r(this, "manager");
496
+ i(this, "transaction");
497
+ i(this, "manager");
498
498
  this.transaction = t, this.manager = e;
499
499
  }
500
500
  getFunctionCall() {
501
501
  return this.transaction.functionCall;
502
502
  }
503
503
  async invoke(t) {
504
- var i;
504
+ var r;
505
505
  const e = this.transaction.functionCall;
506
506
  if (!e) {
507
507
  await this.fail("No function call found");
508
508
  return;
509
509
  }
510
- const s = e.name, n = e.args, a = t[s];
511
- if (!a) {
510
+ const s = e.name, n = e.args, o = t[s];
511
+ if (!o) {
512
512
  await this.fail(`Tool ${s} not found`);
513
513
  return;
514
514
  }
515
515
  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);
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
517
  await this.complete(d);
518
518
  } catch (c) {
519
519
  await this.fail(`Error executing tool ${s}: ${c}`);
@@ -529,12 +529,12 @@ class re {
529
529
  class ce extends v {
530
530
  constructor(e) {
531
531
  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");
532
+ i(this, "status");
533
+ i(this, "autostart");
534
+ i(this, "session");
535
+ i(this, "config");
536
+ i(this, "notify", !0);
537
+ i(this, "_tools");
538
538
  if (this.config = e, this.status = "disconnected", this.autostart = !0, Array.isArray(e.tools)) {
539
539
  this._tools = {};
540
540
  for (const s of e.tools)
@@ -561,136 +561,143 @@ class ce extends v {
561
561
  }
562
562
  onTransaction(e) {
563
563
  console.log("transaction received:", e);
564
- const s = new ie(this.config), n = new re(e, s);
564
+ const s = new re(this.config), n = new ie(e, s);
565
565
  this.config.onTransaction ? this.config.onTransaction(n) : n.invoke(this._tools);
566
566
  }
567
567
  getTools() {
568
568
  return this._tools;
569
569
  }
570
570
  }
571
- const x = Y(void 0);
572
- function le(o) {
571
+ const R = Y(void 0);
572
+ function le(a) {
573
573
  return new Promise((t, e) => {
574
574
  const s = new FileReader();
575
- s.readAsDataURL(o), s.onload = () => {
576
- const a = s.result.split(";base64,")[1];
577
- t(a);
575
+ s.readAsDataURL(a), s.onload = () => {
576
+ const o = s.result.split(";base64,")[1];
577
+ t(o);
578
578
  }, s.onerror = (n) => {
579
579
  e(n);
580
580
  };
581
581
  });
582
582
  }
583
583
  function ue({
584
- dev: o = !1,
584
+ dev: a = !1,
585
585
  baseUrl: t,
586
586
  protocols: e,
587
587
  logger: s,
588
588
  children: n,
589
- session: a = "new",
590
- ...i
589
+ session: o = "new",
590
+ ...r
591
591
  }) {
592
- const [c, u] = E(!1), [d, g] = E([]), [w, R] = E(a), [b, $] = E(/* @__PURE__ */ new Map()), q = G(!1), S = H(() => {
592
+ const [c, u] = A(!1), [d, y] = A([]), [S, T] = A(o), [C, $] = A(/* @__PURE__ */ new Map()), q = G(!1), b = H(() => {
593
593
  if (Array.isArray(e))
594
594
  return e;
595
595
  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) {
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) {
599
599
  case "rest":
600
- const p = e[y];
601
- return p === !0 ? new _({
600
+ const w = e[f];
601
+ return w === !0 ? new _({
602
602
  apiUrl: `${m}://${l}`,
603
- apiKey: i.apiKey,
604
- agentId: i.agentId,
603
+ apiKey: r.apiKey,
604
+ agentId: r.agentId,
605
605
  logger: s
606
- }) : typeof p == "object" && p !== null ? new _(p) : null;
606
+ }) : typeof w == "object" && w !== null ? new _(w) : null;
607
607
  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,
608
+ const h = e[f];
609
+ return h === !0 ? new W({
610
+ webrtcUrl: `${p}://${l}/webrtc`,
611
+ apiKey: r.apiKey,
612
+ agentId: r.agentId,
613
613
  logger: s
614
- }) : typeof C == "object" && C !== null ? new z(C) : null;
614
+ }) : typeof h == "object" && h !== null ? new W(h) : null;
615
615
  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,
616
+ const P = e[f];
617
+ return P === !0 ? new U({
618
+ webSocketUrl: `${p}://${l}/websocket`,
619
+ apiKey: r.apiKey,
620
+ agentId: r.agentId,
621
621
  logger: s
622
- }) : typeof T == "object" && T !== null ? new W(T) : null;
622
+ }) : typeof P == "object" && P !== null ? new U(P) : null;
623
623
  default:
624
- throw new Error(`Unknown protocol: ${y}`);
624
+ throw new Error(`Unknown protocol: ${f}`);
625
625
  }
626
- }).filter((y) => y !== null);
627
- return i.tools && f.push(
626
+ }).filter((f) => f !== null);
627
+ return r.tools && g.push(
628
628
  new ce({
629
629
  apiUrl: `${m}://${l}`,
630
- apiKey: i.apiKey,
631
- agentId: i.agentId,
632
- tools: i.tools,
630
+ apiKey: r.apiKey,
631
+ agentId: r.agentId,
632
+ tools: r.tools,
633
633
  // Pass raw tools
634
634
  logger: s
635
635
  })
636
- ), f;
636
+ ), g;
637
637
  }
638
638
  throw new Error("Invalid protocols configuration");
639
639
  }, []);
640
640
  Q(() => {
641
641
  q.current || (q.current = !0, s == null || s.debug(
642
642
  "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({
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({
647
647
  type: "command",
648
648
  payload: {
649
649
  command: "set_initial_context",
650
- arguments: i.context
650
+ arguments: r.context
651
651
  }
652
- }), i.tools && Array.isArray(i.tools) && l.sendPacket({
652
+ }), r.tools && Array.isArray(r.tools) && l.sendPacket({
653
653
  type: "command",
654
654
  payload: {
655
655
  command: "set_local_tools",
656
656
  arguments: {
657
- tools: i.tools.map((h) => h.schema)
657
+ tools: r.tools.map((p) => p.schema)
658
658
  }
659
659
  }
660
- })), $(new Map(b));
660
+ })), $(new Map(C));
661
661
  }), l.addPacketListener((m) => {
662
662
  if (m.type === "message") {
663
- const h = m.payload;
664
- g(
665
- (f) => j([...f, { ...h, protocol: l.getName() }])
663
+ const p = m.payload;
664
+ y(
665
+ (g) => j([...g, { ...p, protocol: l.getName() }])
666
666
  );
667
667
  } else if (m.type === "reasoning") {
668
- const h = m.payload;
669
- let f = h.thought;
670
- h.imageUrl && (f += `
668
+ const p = m.payload;
669
+ let g = p.thought;
670
+ p.imageUrl && (g += `
671
671
 
672
- ![image](https://persona.applica.guru/api/files/${h.imageUrl})`);
673
- const y = { type: "reasoning", text: f, role: "assistant", finishReason: "stop" };
674
- g(
675
- (p) => j([...p, { ...y, protocol: l.getName() }])
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() }])
676
676
  );
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));
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
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 = [];
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,
687
+ { role: "assistant", type: "text", text: "No protocol is connected." }
688
+ ]);
689
+ return;
690
+ }
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 = [];
687
694
  if (l.attachments)
688
- for (const p of l.attachments)
689
- p.contentType.startsWith("image/") && p.file && f.push({
695
+ for (const h of l.attachments)
696
+ h.contentType.startsWith("image/") && h.file && f.push({
690
697
  role: "user",
691
698
  image: {
692
- contentType: p.contentType,
693
- content: await le(p.file)
699
+ contentType: h.contentType,
700
+ content: await le(h.file)
694
701
  },
695
702
  text: "",
696
703
  type: "text"
@@ -699,34 +706,34 @@ function ue({
699
706
  role: "user",
700
707
  text: l.content[0].text,
701
708
  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({
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({
704
711
  isRunning: c,
705
712
  messages: d,
706
- convertMessage: oe,
707
- onNew: U,
713
+ convertMessage: ae,
714
+ onNew: F,
708
715
  onCancel: D,
709
716
  onReload: L,
710
717
  adapters: {
711
718
  attachments: new Z([new ee()])
712
719
  }
713
720
  });
714
- return /* @__PURE__ */ M.jsx(x.Provider, { value: { protocols: S, protocolsStatus: b, getMessages: J }, children: /* @__PURE__ */ M.jsx(te, { runtime: K, children: n }) });
721
+ return /* @__PURE__ */ M.jsx(R.Provider, { value: { protocols: b, protocolsStatus: C, getMessages: J }, children: /* @__PURE__ */ M.jsx(te, { runtime: K, children: n }) });
715
722
  }
716
- function ge({ children: o, ...t }) {
717
- return /* @__PURE__ */ M.jsx(ue, { ...t, children: o });
723
+ function ge({ children: a, ...t }) {
724
+ return /* @__PURE__ */ M.jsx(ue, { ...t, children: a });
718
725
  }
719
726
  function we() {
720
- const o = A(x);
721
- if (!o)
727
+ const a = E(R);
728
+ if (!a)
722
729
  throw new Error("usePersonaRuntime must be used within a PersonaRuntimeProvider");
723
- return o;
730
+ return a;
724
731
  }
725
- function he(o) {
726
- const t = A(x);
732
+ function he(a) {
733
+ const t = E(R);
727
734
  if (!t)
728
735
  throw new Error("usePersonaRuntimeProtocol must be used within a PersonaRuntimeProvider");
729
- const e = t.protocols.find((n) => n.getName() === o);
736
+ const e = t.protocols.find((n) => n.getName() === a);
730
737
  if (!e)
731
738
  return null;
732
739
  const s = t.protocolsStatus.get(e.getName());
@@ -744,26 +751,33 @@ function he(o) {
744
751
  };
745
752
  }
746
753
  function be() {
747
- const o = A(x);
748
- if (!o)
754
+ const a = E(R);
755
+ if (!a)
749
756
  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");
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;
754
768
  }
755
769
  function Se() {
756
770
  return he("webrtc");
757
771
  }
758
772
  function Ce() {
759
- const o = A(x);
760
- if (!o)
773
+ const a = E(R);
774
+ if (!a)
761
775
  throw new Error("usePersonaRuntimeMessages must be used within a PersonaRuntimeProvider");
762
- return o.getMessages();
776
+ return a.getMessages();
763
777
  }
764
778
  class Pe {
765
779
  constructor() {
766
- r(this, "prefix", "[Persona]");
780
+ i(this, "prefix", "[Persona]");
767
781
  }
768
782
  log(t, ...e) {
769
783
  console.log(`${this.prefix} - ${t}`, ...e);
@@ -781,52 +795,52 @@ class Pe {
781
795
  console.debug(`${this.prefix} - ${t}`, ...e);
782
796
  }
783
797
  }
784
- function P(o, t, e) {
798
+ function k(a, t, e) {
785
799
  return {
786
- type: o,
800
+ type: a,
787
801
  description: t,
788
802
  ...e
789
803
  };
790
804
  }
791
- function de(o) {
792
- const t = Object.entries(o.parameters).filter(([e, s]) => s.required).map(([e]) => e);
805
+ function de(a) {
806
+ const t = Object.entries(a.parameters).filter(([e, s]) => s.required).map(([e]) => e);
793
807
  return {
794
808
  type: "local",
795
- name: o.name,
796
- description: o.description,
809
+ name: a.name,
810
+ description: a.description,
797
811
  config: {
798
- timeout: o.timeout || 60,
812
+ timeout: a.timeout || 60,
799
813
  parameters: {
800
814
  type: "object",
801
- title: o.title || `${o.name} parameters`,
815
+ title: a.title || `${a.name} parameters`,
802
816
  required: t,
803
- properties: o.parameters
817
+ properties: a.parameters
804
818
  },
805
819
  output: {
806
820
  type: "object",
807
- title: `${o.name} output`,
808
- properties: o.output
821
+ title: `${a.name} output`,
822
+ properties: a.output
809
823
  }
810
824
  }
811
825
  };
812
826
  }
813
- function I(o) {
827
+ function I(a) {
814
828
  return {
815
- schema: de(o),
816
- implementation: o.implementation
829
+ schema: de(a),
830
+ implementation: a.implementation
817
831
  };
818
832
  }
819
- function pe(o, t, e, s, n, a) {
820
- const i = {
821
- name: o,
833
+ function pe(a, t, e, s, n, o) {
834
+ const r = {
835
+ name: a,
822
836
  description: t,
823
- title: a == null ? void 0 : a.title,
824
- timeout: a == null ? void 0 : a.timeout,
837
+ title: o == null ? void 0 : o.title,
838
+ timeout: o == null ? void 0 : o.timeout,
825
839
  parameters: s,
826
840
  output: n,
827
841
  implementation: e
828
842
  };
829
- return I(i);
843
+ return I(r);
830
844
  }
831
845
  const ke = pe(
832
846
  "sum",
@@ -835,11 +849,11 @@ const ke = pe(
835
849
  return { result: t + e };
836
850
  },
837
851
  {
838
- a: P("number", "First number to sum", { required: !0 }),
839
- b: P("number", "Second number to sum", { required: !0 })
852
+ a: k("number", "First number to sum", { required: !0 }),
853
+ b: k("number", "Second number to sum", { required: !0 })
840
854
  },
841
855
  {
842
- result: P("number", "Sum of two numbers")
856
+ result: k("number", "Sum of two numbers")
843
857
  }
844
858
  ), xe = I({
845
859
  name: "navigate_to",
@@ -848,37 +862,37 @@ const ke = pe(
848
862
  // As per the user's example
849
863
  timeout: 60,
850
864
  parameters: {
851
- a: P("number", "First number to sum"),
852
- b: P("number", "Seconth number to sum")
865
+ a: k("number", "First number to sum"),
866
+ b: k("number", "Seconth number to sum")
853
867
  // Keeping the typo as in the original
854
868
  },
855
869
  output: {
856
- result: P("number", "Sum of two numbers")
870
+ result: k("number", "Sum of two numbers")
857
871
  },
858
872
  implementation: function(t, e) {
859
873
  return { result: t + e };
860
874
  }
861
875
  });
862
- function Re(o) {
876
+ function Re(a) {
863
877
  const t = [], e = {};
864
- return o.forEach((s) => {
865
- const { schema: n, implementation: a } = I(s);
866
- t.push(n), e[s.name] = a;
878
+ return a.forEach((s) => {
879
+ const { schema: n, implementation: o } = I(s);
880
+ t.push(n), e[s.name] = o;
867
881
  }), { schemas: t, implementations: e };
868
882
  }
869
- function Te(o, t) {
883
+ function Te(a, t) {
870
884
  const { required: e, properties: s } = t.config.parameters;
871
885
  for (const n of e)
872
- if (!(n in o))
886
+ if (!(n in a))
873
887
  throw new Error(`Missing required parameter: ${n}`);
874
- for (const [n, a] of Object.entries(o)) {
875
- const i = s[n];
876
- if (i) {
877
- if (i.type === "number" && typeof a != "number")
888
+ for (const [n, o] of Object.entries(a)) {
889
+ const r = s[n];
890
+ if (r) {
891
+ if (r.type === "number" && typeof o != "number")
878
892
  throw new Error(`Parameter ${n} should be a number`);
879
- if (i.type === "string" && typeof a != "string")
893
+ if (r.type === "string" && typeof o != "string")
880
894
  throw new Error(`Parameter ${n} should be a string`);
881
- if (i.type === "boolean" && typeof a != "boolean")
895
+ if (r.type === "boolean" && typeof o != "boolean")
882
896
  throw new Error(`Parameter ${n} should be a boolean`);
883
897
  }
884
898
  }
@@ -890,9 +904,9 @@ export {
890
904
  _ as PersonaRESTProtocol,
891
905
  ge as PersonaRuntimeProvider,
892
906
  ce as PersonaTransactionProtocol,
893
- z as PersonaWebRTCProtocol,
894
- W as PersonaWebSocketProtocol,
895
- P as createParameter,
907
+ W as PersonaWebRTCProtocol,
908
+ U as PersonaWebSocketProtocol,
909
+ k as createParameter,
896
910
  I as createTool,
897
911
  pe as createToolFromFunction,
898
912
  Re as createToolRegistry,