@applica-software-guru/persona-sdk 0.1.44 → 0.1.46

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 U = Object.defineProperty;
2
- var J = (i, t, e) => t in i ? U(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e;
3
- var o = (i, t, e) => J(i, typeof t != "symbol" ? t + "" : t, e);
4
- import { createContext as q, useContext as N, useState as C, useRef as _, useMemo as B, useEffect as K, useCallback as v } from "react";
1
+ var F = Object.defineProperty;
2
+ var J = (n, t, e) => t in n ? F(n, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : n[t] = e;
3
+ var a = (n, t, e) => J(n, typeof t != "symbol" ? t + "" : t, e);
4
+ import { createContext as q, useContext as x, useState as C, useRef as K, useMemo as _, useEffect as B, useCallback as T } from "react";
5
5
  import { useExternalStoreRuntime as Y, AssistantRuntimeProvider as G } from "@assistant-ui/react";
6
- var z = { exports: {} }, w = {};
6
+ var z = { exports: {} }, m = {};
7
7
  /**
8
8
  * @license React
9
9
  * react-jsx-runtime.production.js
@@ -15,72 +15,79 @@ var z = { exports: {} }, w = {};
15
15
  */
16
16
  var M;
17
17
  function V() {
18
- if (M) return w;
18
+ if (M) return m;
19
19
  M = 1;
20
- var i = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
21
- function e(s, n, a) {
22
- var r = null;
23
- if (a !== void 0 && (r = "" + a), n.key !== void 0 && (r = "" + n.key), "key" in n) {
24
- a = {};
25
- for (var l in n)
26
- l !== "key" && (a[l] = n[l]);
27
- } else a = n;
28
- return n = a.ref, {
29
- $$typeof: i,
30
- type: s,
31
- key: r,
32
- ref: n !== void 0 ? n : null,
33
- props: a
20
+ var n = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
21
+ function e(o, s, i) {
22
+ var c = null;
23
+ if (i !== void 0 && (c = "" + i), s.key !== void 0 && (c = "" + s.key), "key" in s) {
24
+ i = {};
25
+ for (var r in s)
26
+ r !== "key" && (i[r] = s[r]);
27
+ } else i = s;
28
+ return s = i.ref, {
29
+ $$typeof: n,
30
+ type: o,
31
+ key: c,
32
+ ref: s !== void 0 ? s : null,
33
+ props: i
34
34
  };
35
35
  }
36
- return w.Fragment = t, w.jsx = e, w.jsxs = e, w;
36
+ return m.Fragment = t, m.jsx = e, m.jsxs = e, m;
37
37
  }
38
38
  z.exports = V();
39
- var k = z.exports;
40
- function H(i) {
41
- return i.filter((t) => {
39
+ var P = z.exports;
40
+ function H(n) {
41
+ return n.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 Q(i) {
46
+ function Q(n) {
47
47
  const t = [];
48
48
  let e = null;
49
- for (const s of i)
49
+ for (const s of n)
50
50
  s.type === "reasoning" ? (e != null && (t.push(e), e = null), t.push(s)) : s.functionCalls ? (e && t.push(e), t.push(s), e = null) : s.functionResponse ? t[t.length - 1] = {
51
51
  ...t[t.length - 1],
52
52
  functionResponse: s.functionResponse
53
- } : e && s.protocol === e.protocol && (e.role === s.role || s.finishReason === "stop") ? e.text += s.text : (e && t.push(e), e = {
53
+ } : e && s.protocol === e.protocol && (e.role === s.role || s.finishReason === "stop") ? (e.text += s.text, e.files = [...e.files ?? [], ...s.files ?? []]) : (e && t.push(e), e = {
54
54
  ...s
55
55
  });
56
56
  return e && t.push(e), H(t);
57
57
  }
58
- function X(i) {
59
- var t;
60
- return i.role === "function" ? {
61
- id: i.id,
58
+ function X(n) {
59
+ var e, o;
60
+ const t = ((e = n.files) == null ? void 0 : e.map(
61
+ (s) => ({
62
+ type: "file",
63
+ data: s.url,
64
+ mimeType: s.contentType
65
+ })
66
+ )) ?? [];
67
+ return n.role === "function" ? {
68
+ id: n.id,
62
69
  role: "assistant",
63
- status: (i == null ? void 0 : i.functionResponse) === null ? { type: "running" } : { type: "complete", reason: "stop" },
64
- content: ((t = i.functionCalls) == null ? void 0 : t.map((e) => {
65
- var s;
70
+ status: (n == null ? void 0 : n.functionResponse) === null ? { type: "running" } : { type: "complete", reason: "stop" },
71
+ content: ((o = n.functionCalls) == null ? void 0 : o.map((s) => {
72
+ var i;
66
73
  return {
67
74
  type: "tool-call",
68
- toolName: e.name,
69
- toolCallId: e.id,
70
- args: e.args,
71
- result: (s = i.functionResponse) == null ? void 0 : s.result
75
+ toolName: s.name,
76
+ toolCallId: s.id,
77
+ args: s.args,
78
+ result: (i = n.functionResponse) == null ? void 0 : i.result
72
79
  };
73
80
  })) ?? []
74
81
  } : {
75
- id: i.id,
76
- role: i.role,
77
- content: i.type === "reasoning" ? [{ type: "reasoning", text: i.text }] : [{ type: "text", text: i.text }]
82
+ id: n.id,
83
+ role: n.role,
84
+ content: n.type === "reasoning" ? [{ type: "reasoning", text: n.text }, ...t] : [{ type: "text", text: n.text }, ...t]
78
85
  };
79
86
  }
80
- class x {
87
+ class R {
81
88
  constructor() {
82
- o(this, "statusChangeCallbacks", []);
83
- o(this, "messageCallbacks", []);
89
+ a(this, "statusChangeCallbacks", []);
90
+ a(this, "messageCallbacks", []);
84
91
  }
85
92
  addStatusChangeListener(t) {
86
93
  this.statusChangeCallbacks.push(t);
@@ -96,7 +103,7 @@ class x {
96
103
  }
97
104
  async notifyMessages(t) {
98
105
  t.forEach((e) => {
99
- this.messageCallbacks.forEach((s) => s(e));
106
+ this.messageCallbacks.forEach((o) => o(e));
100
107
  });
101
108
  }
102
109
  async setSession(t) {
@@ -104,20 +111,20 @@ class x {
104
111
  }
105
112
  async setStatus(t) {
106
113
  const e = this.status !== t;
107
- this.status = t, e && this.statusChangeCallbacks.forEach((s) => s(t));
114
+ this.status = t, e && this.statusChangeCallbacks.forEach((o) => o(t));
108
115
  }
109
116
  clearListeners() {
110
117
  this.statusChangeCallbacks = [], this.messageCallbacks = [];
111
118
  }
112
119
  }
113
- class T extends x {
120
+ class $ extends R {
114
121
  constructor(e) {
115
122
  super();
116
- o(this, "status");
117
- o(this, "autostart");
118
- o(this, "session");
119
- o(this, "config");
120
- o(this, "notify", !0);
123
+ a(this, "status");
124
+ a(this, "autostart");
125
+ a(this, "session");
126
+ a(this, "config");
127
+ a(this, "notify", !0);
121
128
  this.config = e, this.status = "disconnected", this.autostart = !0;
122
129
  }
123
130
  getName() {
@@ -136,26 +143,26 @@ class T extends x {
136
143
  this.session = e;
137
144
  }
138
145
  async send(e) {
139
- const { apiUrl: s, apiKey: n, agentId: a } = this.config, r = this.session ?? "new", l = e, h = await (await fetch(`${s}/agents/${a}/sessions/${r}/messages`, {
140
- body: JSON.stringify({ userMessage: l }),
146
+ const { apiUrl: o, apiKey: s, agentId: i } = this.config, c = this.session ?? "new", r = e, h = await (await fetch(`${o}/agents/${i}/sessions/${c}/messages`, {
147
+ body: JSON.stringify({ userMessage: r }),
141
148
  method: "POST",
142
149
  headers: {
143
150
  "Content-Type": "application/json",
144
- "x-fox-apikey": n,
145
- "x-persona-apikey": n
151
+ "x-fox-apikey": s,
152
+ "x-persona-apikey": s
146
153
  }
147
154
  })).json();
148
155
  this.notifyMessages(h.response.messages);
149
156
  }
150
157
  }
151
- class I extends x {
158
+ class I extends R {
152
159
  constructor(e) {
153
160
  super();
154
- o(this, "status");
155
- o(this, "autostart");
156
- o(this, "session");
157
- o(this, "config");
158
- o(this, "webSocket");
161
+ a(this, "status");
162
+ a(this, "autostart");
163
+ a(this, "session");
164
+ a(this, "config");
165
+ a(this, "webSocket");
159
166
  this.config = e, this.status = "disconnected", this.autostart = !0, this.session = null, this.webSocket = null;
160
167
  }
161
168
  getName() {
@@ -165,17 +172,17 @@ class I extends x {
165
172
  return 1;
166
173
  }
167
174
  async syncSession(e) {
168
- var s;
169
- (s = this.config.logger) == null || s.debug("Syncing session with WebSocket protocol:", e), this.session = e, this.webSocket && this.status === "connected" && (this.disconnect(), this.connect(e));
175
+ var o;
176
+ (o = this.config.logger) == null || o.debug("Syncing session with WebSocket protocol:", e), this.session = e, this.webSocket && this.status === "connected" && (this.disconnect(), this.connect(e));
170
177
  }
171
178
  connect(e) {
172
- var l;
179
+ var r;
173
180
  if (this.webSocket !== null && this.status === "connected")
174
181
  return Promise.resolve(this.session);
175
- const s = e || this.session || "new";
176
- (l = this.config.logger) == null || l.debug("Connecting to WebSocket with sessionId:", s);
177
- const n = encodeURIComponent(this.config.apiKey), a = this.config.agentId, r = `${this.config.webSocketUrl}?sessionCode=${s}&agentId=${a}&apiKey=${n}`;
178
- return this.setStatus("connecting"), this.webSocket = new WebSocket(r), this.webSocket.addEventListener("open", () => {
182
+ const o = e || this.session || "new";
183
+ (r = this.config.logger) == null || r.debug("Connecting to WebSocket with sessionId:", o);
184
+ const s = encodeURIComponent(this.config.apiKey), i = this.config.agentId, c = `${this.config.webSocketUrl}?sessionCode=${o}&agentId=${i}&apiKey=${s}`;
185
+ return this.setStatus("connecting"), this.webSocket = new WebSocket(c), this.webSocket.addEventListener("open", () => {
179
186
  this.setStatus("connected");
180
187
  }), this.webSocket.addEventListener("message", (u) => {
181
188
  const h = JSON.parse(u.data);
@@ -189,7 +196,7 @@ class I extends x {
189
196
  }), this.webSocket.addEventListener("error", (u) => {
190
197
  var h;
191
198
  this.setStatus("disconnected"), this.webSocket = null, (h = this.config.logger) == null || h.error("WebSocket error", u);
192
- }), Promise.resolve(s);
199
+ }), Promise.resolve(o);
193
200
  }
194
201
  disconnect() {
195
202
  var e;
@@ -201,106 +208,108 @@ class I extends x {
201
208
  }
202
209
  class Z {
203
210
  constructor(t) {
204
- o(this, "config");
205
- o(this, "pc", null);
206
- o(this, "ws", null);
207
- o(this, "localStream", null);
208
- o(this, "remoteStream", new MediaStream());
209
- o(this, "audioCtx", null);
210
- o(this, "localAnalyser", null);
211
- o(this, "remoteAnalyser", null);
212
- o(this, "analyzerFrame", null);
213
- o(this, "dataChannel", null);
214
- o(this, "isConnected", !1);
215
- o(this, "visualizerCallbacks", []);
216
- o(this, "messageCallbacks", []);
211
+ a(this, "config");
212
+ a(this, "pc", null);
213
+ a(this, "ws", null);
214
+ a(this, "localStream", null);
215
+ a(this, "remoteStream", new MediaStream());
216
+ a(this, "audioCtx", null);
217
+ a(this, "localAnalyser", null);
218
+ a(this, "remoteAnalyser", null);
219
+ a(this, "analyzerFrame", null);
220
+ a(this, "dataChannel", null);
221
+ a(this, "isConnected", !1);
222
+ a(this, "visualizerCallbacks", []);
223
+ a(this, "messageCallbacks", []);
217
224
  this.config = t;
218
225
  }
219
226
  async connect(t) {
220
- var e;
221
- if (!this.isConnected) {
222
- this.isConnected = !0;
223
- try {
224
- this.localStream = await navigator.mediaDevices.getUserMedia({ audio: !0 });
225
- } catch (s) {
226
- (e = this.config.logger) == null || e.error("Error accessing microphone:", s);
227
- return;
228
- }
229
- this.pc = new RTCPeerConnection({
230
- iceServers: this.config.iceServers || [
231
- {
232
- urls: "stun:34.38.108.251:3478"
233
- },
234
- {
235
- urls: "turn:34.38.108.251:3478",
236
- username: "webrtc",
237
- credential: "webrtc"
238
- }
239
- ]
240
- }), this.localStream.getTracks().forEach((s) => {
241
- this.pc.addTrack(s, this.localStream);
242
- }), this.pc.ontrack = (s) => {
243
- s.streams[0].getTracks().forEach((a) => {
244
- this.remoteStream.addTrack(a);
245
- }), this.audioCtx || this._startAnalyzers();
246
- const n = new Audio();
247
- n.srcObject = this.remoteStream, n.play().catch((a) => {
248
- var r;
249
- (r = this.config.logger) == null || r.error("Error playing remote audio:", a);
227
+ var o;
228
+ if (this.isConnected) return;
229
+ this.isConnected = !0;
230
+ try {
231
+ this.localStream = await navigator.mediaDevices.getUserMedia({ audio: !0 });
232
+ } catch (s) {
233
+ (o = this.config.logger) == null || o.error("Error accessing microphone:", s);
234
+ return;
235
+ }
236
+ this.pc = new RTCPeerConnection({
237
+ iceServers: this.config.iceServers || [
238
+ {
239
+ urls: "stun:34.38.108.251:3478"
240
+ },
241
+ {
242
+ urls: "turn:34.38.108.251:3478",
243
+ username: "webrtc",
244
+ credential: "webrtc"
245
+ }
246
+ ]
247
+ }), this.localStream.getTracks().forEach((s) => {
248
+ this.pc.addTrack(s, this.localStream);
249
+ }), this.pc.ontrack = (s) => {
250
+ s.streams[0].getTracks().forEach((c) => {
251
+ this.remoteStream.addTrack(c);
252
+ }), this.audioCtx || this._startAnalyzers();
253
+ const i = new Audio();
254
+ i.srcObject = this.remoteStream, i.play().catch((c) => {
255
+ var r;
256
+ (r = this.config.logger) == null || r.error("Error playing remote audio:", c);
257
+ });
258
+ }, this.pc.onicecandidate = (s) => {
259
+ var i;
260
+ s.candidate && ((i = this.ws) == null ? void 0 : i.readyState) === WebSocket.OPEN && this.ws.send(
261
+ JSON.stringify({
262
+ type: "CANDIDATE",
263
+ src: "client",
264
+ payload: { candidate: s.candidate }
265
+ })
266
+ );
267
+ }, this.pc.ondatachannel = (s) => {
268
+ const i = s.channel;
269
+ i.onmessage = (c) => {
270
+ this.messageCallbacks.forEach((r) => {
271
+ r(c);
250
272
  });
251
- }, this.pc.onicecandidate = (s) => {
252
- var n;
253
- s.candidate && ((n = this.ws) == null ? void 0 : n.readyState) === WebSocket.OPEN && this.ws.send(
254
- JSON.stringify({
255
- type: "CANDIDATE",
256
- src: "client",
257
- payload: { candidate: s.candidate }
258
- })
259
- );
260
- }, this.pc.ondatachannel = (s) => {
261
- const n = s.channel;
262
- n.onmessage = (a) => {
263
- this.messageCallbacks.forEach((r) => {
264
- r(a);
265
- });
266
- };
267
- }, this.ws = new WebSocket(this.config.webrtcUrl || "wss://persona.applica.guru/api/webrtc"), this.ws.onopen = async () => {
268
- var r, l;
269
- const s = await this.pc.createOffer();
270
- await this.pc.setLocalDescription(s);
271
- const n = {
272
- agentId: this.config.agentId,
273
- sessionCode: t
274
- };
275
- (r = this.config.logger) == null || r.debug("Opening connection to WebRTC server: ", n);
276
- const a = {
277
- type: "OFFER",
278
- src: ((l = crypto.randomUUID) == null ? void 0 : l.call(crypto)) || "client_" + Date.now(),
279
- payload: {
280
- sdp: {
281
- sdp: s.sdp,
282
- type: s.type
283
- },
284
- connectionId: (Date.now() % 1e6).toString(),
285
- metadata: n
286
- }
287
- };
288
- this.ws.send(JSON.stringify(a));
289
- }, this.ws.onmessage = async (s) => {
290
- var a;
291
- const n = JSON.parse(s.data);
292
- if (n.type === "ANSWER")
293
- await this.pc.setRemoteDescription(new RTCSessionDescription(n.payload.sdp));
294
- else if (n.type === "CANDIDATE")
295
- try {
296
- await this.pc.addIceCandidate(new RTCIceCandidate(n.payload.candidate));
297
- } catch (r) {
298
- (a = this.config.logger) == null || a.error("Error adding ICE candidate:", r);
299
- }
300
- }, this.ws.onclose = () => {
301
- this._stopAnalyzers();
302
273
  };
303
- }
274
+ };
275
+ const e = this.config.webrtcUrl || "wss://persona.applica.guru/api/webrtc";
276
+ this.ws = new WebSocket(`${e}?apiKey=${encodeURIComponent(this.config.apiKey)}`), this.ws.onopen = async () => {
277
+ var r, u;
278
+ const s = await this.pc.createOffer();
279
+ await this.pc.setLocalDescription(s);
280
+ const i = {
281
+ apiKey: this.config.apiKey,
282
+ agentId: this.config.agentId,
283
+ sessionCode: t
284
+ };
285
+ (r = this.config.logger) == null || r.debug("Opening connection to WebRTC server: ", i);
286
+ const c = {
287
+ type: "OFFER",
288
+ src: ((u = crypto.randomUUID) == null ? void 0 : u.call(crypto)) || "client_" + Date.now(),
289
+ payload: {
290
+ sdp: {
291
+ sdp: s.sdp,
292
+ type: s.type
293
+ },
294
+ connectionId: (Date.now() % 1e6).toString(),
295
+ metadata: i
296
+ }
297
+ };
298
+ this.ws.send(JSON.stringify(c));
299
+ }, this.ws.onmessage = async (s) => {
300
+ var c;
301
+ const i = JSON.parse(s.data);
302
+ if (i.type === "ANSWER")
303
+ await this.pc.setRemoteDescription(new RTCSessionDescription(i.payload.sdp));
304
+ else if (i.type === "CANDIDATE")
305
+ try {
306
+ await this.pc.addIceCandidate(new RTCIceCandidate(i.payload.candidate));
307
+ } catch (r) {
308
+ (c = this.config.logger) == null || c.error("Error adding ICE candidate:", r);
309
+ }
310
+ }, this.ws.onclose = () => {
311
+ this._stopAnalyzers();
312
+ };
304
313
  }
305
314
  async disconnect() {
306
315
  var t;
@@ -317,18 +326,18 @@ class Z {
317
326
  var e;
318
327
  return (e = this.config.logger) == null ? void 0 : e.info("Data channel opened");
319
328
  }, this.dataChannel.onmessage = (e) => {
320
- this.messageCallbacks.forEach((s) => {
321
- s(e);
329
+ this.messageCallbacks.forEach((o) => {
330
+ o(e);
322
331
  });
323
332
  });
324
333
  }
325
334
  sendMessage(t) {
326
- var e, s;
335
+ var e, o;
327
336
  if (!this.dataChannel) {
328
337
  (e = this.config.logger) == null || e.warn("Data channel is not open, cannot send message");
329
338
  return;
330
339
  }
331
- this.dataChannel.send(t), (s = this.config.logger) == null || s.info("Sent message:", t);
340
+ this.dataChannel.send(t), (o = this.config.logger) == null || o.info("Sent message:", t);
332
341
  }
333
342
  _startAnalyzers() {
334
343
  if (!this.localStream || !this.remoteStream || this.visualizerCallbacks.length === 0)
@@ -336,38 +345,38 @@ class Z {
336
345
  this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
337
346
  const t = this.audioCtx.createMediaStreamSource(this.localStream), e = this.audioCtx.createMediaStreamSource(this.remoteStream);
338
347
  this.localAnalyser = this.audioCtx.createAnalyser(), this.remoteAnalyser = this.audioCtx.createAnalyser(), this.localAnalyser.fftSize = 256, this.remoteAnalyser.fftSize = 256, t.connect(this.localAnalyser), e.connect(this.remoteAnalyser);
339
- const s = () => {
348
+ const o = () => {
340
349
  if (!this.localAnalyser || !this.remoteAnalyser || this.visualizerCallbacks.length === 0)
341
350
  return;
342
- const n = new Uint8Array(this.localAnalyser.frequencyBinCount), a = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
343
- this.localAnalyser.getByteFrequencyData(n), this.remoteAnalyser.getByteFrequencyData(a);
344
- const r = n.reduce((u, h) => u + h, 0) / n.length, l = a.reduce((u, h) => u + h, 0) / a.length;
351
+ const s = new Uint8Array(this.localAnalyser.frequencyBinCount), i = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
352
+ this.localAnalyser.getByteFrequencyData(s), this.remoteAnalyser.getByteFrequencyData(i);
353
+ const c = s.reduce((u, h) => u + h, 0) / s.length, r = i.reduce((u, h) => u + h, 0) / i.length;
345
354
  this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((u) => {
346
355
  u({
347
- localAmplitude: r,
348
- remoteAmplitude: l
356
+ localAmplitude: c,
357
+ remoteAmplitude: r
349
358
  });
350
- }), this.analyzerFrame = requestAnimationFrame(s);
359
+ }), this.analyzerFrame = requestAnimationFrame(o);
351
360
  };
352
- this.analyzerFrame = requestAnimationFrame(s);
361
+ this.analyzerFrame = requestAnimationFrame(o);
353
362
  }
354
363
  _stopAnalyzers() {
355
364
  this.analyzerFrame && (cancelAnimationFrame(this.analyzerFrame), this.analyzerFrame = null), this.localAnalyser = null, this.remoteAnalyser = null;
356
365
  }
357
366
  }
358
- class $ extends x {
367
+ class N extends R {
359
368
  constructor(e) {
360
369
  super();
361
- o(this, "status");
362
- o(this, "session");
363
- o(this, "autostart");
364
- o(this, "config");
365
- o(this, "webRTCClient");
366
- this.config = e, this.status = "disconnected", this.session = null, this.autostart = (e == null ? void 0 : e.autostart) ?? !1, this.webRTCClient = new Z(e), this.webRTCClient.addMessageCallback((s) => {
367
- var a;
368
- (a = e.logger) == null || a.debug("Received data message:", s.data);
369
- const n = JSON.parse(s.data);
370
- n.type === "message" && this.notifyMessage(n.payload);
370
+ a(this, "status");
371
+ a(this, "session");
372
+ a(this, "autostart");
373
+ a(this, "config");
374
+ a(this, "webRTCClient");
375
+ this.config = e, this.status = "disconnected", this.session = null, this.autostart = (e == null ? void 0 : e.autostart) ?? !1, this.webRTCClient = new Z(e), this.webRTCClient.addMessageCallback((o) => {
376
+ var i;
377
+ (i = e.logger) == null || i.debug("Received data message:", o.data);
378
+ const s = JSON.parse(o.data);
379
+ s.type === "message" && this.notifyMessage(s.payload);
371
380
  });
372
381
  }
373
382
  getName() {
@@ -380,59 +389,59 @@ class $ extends x {
380
389
  super.syncSession(e), this.status === "connected" && (await this.disconnect(), await this.connect(e));
381
390
  }
382
391
  async connect(e) {
383
- var s;
384
- return this.status === "connected" ? Promise.resolve(this.session) : (this.session = e || this.session || "new", this.setStatus("connecting"), (s = this.config.logger) == null || s.debug("Connecting to WebRTC with sessionId:", this.session), await this.webRTCClient.connect(this.session), this.setStatus("connected"), await this.webRTCClient.createDataChannel(), this.session);
392
+ var o;
393
+ return this.status === "connected" ? Promise.resolve(this.session) : (this.session = e || this.session || "new", this.setStatus("connecting"), (o = this.config.logger) == null || o.debug("Connecting to WebRTC with sessionId:", this.session), await this.webRTCClient.connect(this.session), this.setStatus("connected"), await this.webRTCClient.createDataChannel(), this.session);
385
394
  }
386
395
  async disconnect() {
387
- var e, s, n;
396
+ var e, o, s;
388
397
  if (this.status === "disconnected")
389
398
  return (e = this.config.logger) == null || e.warn("Already disconnected"), Promise.resolve();
390
- await this.webRTCClient.disconnect(), this.setStatus("disconnected"), (n = (s = this.config) == null ? void 0 : s.logger) == null || n.debug("Disconnected from WebRTC");
399
+ await this.webRTCClient.disconnect(), this.setStatus("disconnected"), (s = (o = this.config) == null ? void 0 : o.logger) == null || s.debug("Disconnected from WebRTC");
391
400
  }
392
401
  send(e) {
393
402
  return this.status !== "connected" ? Promise.reject(new Error("Not connected")) : (this.webRTCClient.sendMessage(e), Promise.resolve());
394
403
  }
395
404
  }
396
- const P = q(void 0);
405
+ const S = q(void 0);
397
406
  function ee({
398
- dev: i = !1,
407
+ dev: n = !1,
399
408
  protocols: t,
400
409
  logger: e,
401
- children: s,
402
- session: n = "new",
403
- ...a
410
+ children: o,
411
+ session: s = "new",
412
+ ...i
404
413
  }) {
405
- const [r, l] = C(!1), [u, h] = C([]), [f, D] = C(n), [m, W] = C(/* @__PURE__ */ new Map()), R = _(!1), y = B(() => {
414
+ const [c, r] = C(!1), [u, h] = C([]), [f, D] = C(s), [w, W] = C(/* @__PURE__ */ new Map()), E = K(!1), y = _(() => {
406
415
  if (Array.isArray(t))
407
416
  return t;
408
417
  if (typeof t == "object" && t !== null) {
409
- const c = i ? "localhost:8000" : "persona.applica.guru/api", p = i ? "http" : "https", g = i ? "ws" : "wss";
418
+ const l = n ? "localhost:8000" : "persona.applica.guru/api", p = n ? "http" : "https", g = n ? "ws" : "wss";
410
419
  return Object.keys(t).map((d) => {
411
420
  switch (d) {
412
421
  case "rest":
413
422
  const b = t[d];
414
- return b === !0 ? new T({
415
- apiUrl: `${p}://${c}`,
416
- apiKey: a.apiKey,
417
- agentId: a.agentId,
423
+ return b === !0 ? new $({
424
+ apiUrl: `${p}://${l}`,
425
+ apiKey: i.apiKey,
426
+ agentId: i.agentId,
418
427
  logger: e
419
- }) : new T(b);
428
+ }) : new $(b);
420
429
  case "webrtc":
421
430
  const A = t[d];
422
- return A === !0 ? new $({
423
- webrtcUrl: `${g}://${c}/webrtc`,
424
- apiKey: a.apiKey,
425
- agentId: a.agentId,
431
+ return A === !0 ? new N({
432
+ webrtcUrl: `${g}://${l}/webrtc`,
433
+ apiKey: i.apiKey,
434
+ agentId: i.agentId,
426
435
  logger: e
427
- }) : new $(A);
436
+ }) : new N(A);
428
437
  case "websocket":
429
- const E = t[d];
430
- return E === !0 ? new I({
431
- webSocketUrl: `${g}://${c}/websocket`,
432
- apiKey: a.apiKey,
433
- agentId: a.agentId,
438
+ const v = t[d];
439
+ return v === !0 ? new I({
440
+ webSocketUrl: `${g}://${l}/websocket`,
441
+ apiKey: i.apiKey,
442
+ agentId: i.agentId,
434
443
  logger: e
435
- }) : new I(E);
444
+ }) : new I(v);
436
445
  default:
437
446
  throw new Error(`Unknown protocol: ${d}`);
438
447
  }
@@ -440,52 +449,52 @@ function ee({
440
449
  }
441
450
  throw new Error("Invalid protocols configuration");
442
451
  }, []);
443
- K(() => {
444
- R.current || (R.current = !0, e == null || e.debug(
452
+ B(() => {
453
+ E.current || (E.current = !0, e == null || e.debug(
445
454
  "Initializing protocols: ",
446
- y.map((c) => c.getName())
447
- ), y.forEach((c) => {
448
- c.setSession(f), c.clearListeners(), c.addStatusChangeListener((p) => {
449
- e == null || e.debug(`${c.getName()} has notified new status: ${p}`), m.set(c.getName(), p), W(new Map(m));
450
- }), c.addMessageListener((p) => {
451
- h((g) => Q([...g, { ...p, protocol: c.getName() }]));
452
- }), c.autostart && c.status === "disconnected" && (e == null || e.debug(`Connecting to protocol: ${c.getName()}`), c.connect(f));
455
+ y.map((l) => l.getName())
456
+ ), y.forEach((l) => {
457
+ l.setSession(f), l.clearListeners(), l.addStatusChangeListener((p) => {
458
+ e == null || e.debug(`${l.getName()} has notified new status: ${p}`), w.set(l.getName(), p), W(new Map(w));
459
+ }), l.addMessageListener((p) => {
460
+ h((g) => Q([...g, { ...p, protocol: l.getName() }]));
461
+ }), l.autostart && l.status === "disconnected" && (e == null || e.debug(`Connecting to protocol: ${l.getName()}`), l.connect(f));
453
462
  }));
454
- }, [f, y, e, m]);
455
- const L = async (c) => {
456
- var S;
457
- if (((S = c.content[0]) == null ? void 0 : S.type) !== "text") throw new Error("Only text messages are supported");
458
- const p = c.content[0].text;
459
- h((d) => [...d, { role: "user", type: "text", text: p }]), l(!0);
463
+ }, [f, y, e, w]);
464
+ const L = async (l) => {
465
+ var k;
466
+ if (((k = l.content[0]) == null ? void 0 : k.type) !== "text") throw new Error("Only text messages are supported");
467
+ const p = l.content[0].text;
468
+ h((d) => [...d, { role: "user", type: "text", text: p }]), r(!0);
460
469
  const g = y.sort((d, b) => b.getPriority() - d.getPriority()).find((d) => d.status === "connected");
461
- await (g == null ? void 0 : g.send(p)), l(!1);
462
- }, O = v(() => (l(!1), h([]), D("new"), Promise.resolve()), []), j = v(() => Promise.resolve(), []), F = Y({
463
- isRunning: r,
470
+ await (g == null ? void 0 : g.send(p)), r(!1);
471
+ }, O = T(() => (r(!1), h([]), D("new"), Promise.resolve()), []), U = T(() => Promise.resolve(), []), j = Y({
472
+ isRunning: c,
464
473
  messages: u,
465
474
  convertMessage: X,
466
475
  onNew: L,
467
476
  onCancel: O,
468
- onReload: j
477
+ onReload: U
469
478
  });
470
- return /* @__PURE__ */ k.jsx(P.Provider, { value: { protocols: y, protocolsStatus: m }, children: /* @__PURE__ */ k.jsx(G, { runtime: F, children: s }) });
479
+ return /* @__PURE__ */ P.jsx(S.Provider, { value: { protocols: y, protocolsStatus: w }, children: /* @__PURE__ */ P.jsx(G, { runtime: j, children: o }) });
471
480
  }
472
- function ae({ children: i, ...t }) {
473
- return /* @__PURE__ */ k.jsx(ee, { ...t, children: i });
481
+ function oe({ children: n, ...t }) {
482
+ return /* @__PURE__ */ P.jsx(ee, { ...t, children: n });
474
483
  }
475
- function oe() {
476
- const i = N(P);
477
- if (!i)
484
+ function ae() {
485
+ const n = x(S);
486
+ if (!n)
478
487
  throw new Error("usePersonaRuntime must be used within a PersonaRuntimeProvider");
479
- return i;
488
+ return n;
480
489
  }
481
- function te(i) {
482
- const t = N(P);
490
+ function te(n) {
491
+ const t = x(S);
483
492
  if (!t)
484
493
  throw new Error("usePersonaRuntimeProtocol must be used within a PersonaRuntimeProvider");
485
- const e = t.protocols.find((n) => n.getName() === i);
494
+ const e = t.protocols.find((s) => s.getName() === n);
486
495
  if (!e)
487
496
  return null;
488
- const s = t.protocolsStatus.get(e.getName());
497
+ const o = t.protocolsStatus.get(e.getName());
489
498
  return {
490
499
  ...e,
491
500
  connect: e.connect.bind(e),
@@ -496,15 +505,24 @@ function te(i) {
496
505
  addMessageListener: e.addMessageListener.bind(e),
497
506
  getName: e.getName.bind(e),
498
507
  getPriority: e.getPriority.bind(e),
499
- status: s || e.status
508
+ status: o || e.status
500
509
  };
501
510
  }
502
511
  function re() {
512
+ const n = x(S);
513
+ if (!n)
514
+ throw new Error("usePersonaRuntimeEndpoint must be used within a PersonaRuntimeProvider");
515
+ for (const t of n.protocols)
516
+ if (t.getName() === "rest")
517
+ return t.config.apiUrl;
518
+ throw new Error("REST protocol not found");
519
+ }
520
+ function ce() {
503
521
  return te("webrtc");
504
522
  }
505
- class ce {
523
+ class le {
506
524
  constructor() {
507
- o(this, "prefix", "[Persona]");
525
+ a(this, "prefix", "[Persona]");
508
526
  }
509
527
  log(t, ...e) {
510
528
  console.log(`${this.prefix} - ${t}`, ...e);
@@ -523,14 +541,15 @@ class ce {
523
541
  }
524
542
  }
525
543
  export {
526
- ce as PersonaConsoleLogger,
527
- x as PersonaProtocolBase,
528
- T as PersonaRESTProtocol,
529
- ae as PersonaRuntimeProvider,
530
- $ as PersonaWebRTCProtocol,
544
+ le as PersonaConsoleLogger,
545
+ R as PersonaProtocolBase,
546
+ $ as PersonaRESTProtocol,
547
+ oe as PersonaRuntimeProvider,
548
+ N as PersonaWebRTCProtocol,
531
549
  I as PersonaWebSocketProtocol,
532
- oe as usePersonaRuntime,
550
+ ae as usePersonaRuntime,
551
+ re as usePersonaRuntimeEndpoint,
533
552
  te as usePersonaRuntimeProtocol,
534
- re as usePersonaRuntimeWebRTCProtocol
553
+ ce as usePersonaRuntimeWebRTCProtocol
535
554
  };
536
555
  //# sourceMappingURL=bundle.es.js.map