@applica-software-guru/persona-sdk 0.1.62 → 0.1.63

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 J = Object.defineProperty;
2
- var K = (a, e, t) => e in a ? J(a, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : a[e] = t;
3
- var o = (a, e, t) => K(a, typeof e != "symbol" ? e + "" : e, t);
4
- import { createContext as q, useContext as A, useState as R, useRef as B, useMemo as Y, useEffect as G, useCallback as T } from "react";
5
- import { useExternalStoreRuntime as V, CompositeAttachmentAdapter as H, SimpleImageAttachmentAdapter as Q, AssistantRuntimeProvider as X } from "@assistant-ui/react";
6
- var U = { exports: {} }, S = {};
1
+ var K = Object.defineProperty;
2
+ var q = (i, e, t) => e in i ? K(i, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : i[e] = t;
3
+ var o = (i, e, t) => q(i, typeof e != "symbol" ? e + "" : e, t);
4
+ import { createContext as B, useContext as A, useState as R, useRef as Y, useMemo as G, useEffect as V, useCallback as v } from "react";
5
+ import { useExternalStoreRuntime as H, CompositeAttachmentAdapter as Q, SimpleImageAttachmentAdapter as X, AssistantRuntimeProvider as Z } from "@assistant-ui/react";
6
+ var W = { exports: {} }, S = {};
7
7
  /**
8
8
  * @license React
9
9
  * react-jsx-runtime.production.js
@@ -13,75 +13,75 @@ var U = { exports: {} }, S = {};
13
13
  * This source code is licensed under the MIT license found in the
14
14
  * LICENSE file in the root directory of this source tree.
15
15
  */
16
- var N;
17
- function Z() {
18
- if (N) return S;
19
- N = 1;
20
- var a = Symbol.for("react.transitional.element"), e = Symbol.for("react.fragment");
21
- function t(n, 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 l in s)
26
- l !== "key" && (i[l] = s[l]);
27
- } else i = s;
28
- return s = i.ref, {
29
- $$typeof: a,
30
- type: n,
31
- key: c,
32
- ref: s !== void 0 ? s : null,
33
- props: i
16
+ var I;
17
+ function tt() {
18
+ if (I) return S;
19
+ I = 1;
20
+ var i = Symbol.for("react.transitional.element"), e = Symbol.for("react.fragment");
21
+ function t(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
34
34
  };
35
35
  }
36
36
  return S.Fragment = e, S.jsx = t, S.jsxs = t, S;
37
37
  }
38
- U.exports = Z();
39
- var v = U.exports;
40
- function tt(a) {
41
- return a.filter((e) => {
38
+ W.exports = tt();
39
+ var $ = W.exports;
40
+ function et(i) {
41
+ return i.filter((e) => {
42
42
  var t;
43
43
  return e.finishReason === "stop" ? e.text !== null && ((t = e.text) == null ? void 0 : t.trim()) !== "" : !0;
44
44
  });
45
45
  }
46
- function I(a) {
46
+ function M(i) {
47
47
  const e = [];
48
48
  let t = null;
49
- for (const s of a)
50
- s.type !== "transaction" && (s.type === "reasoning" ? (t != null && (e.push(t), t = null), e.push(s)) : s.functionCalls ? (t && e.push(t), e.push(s), t = null) : s.functionResponse ? e[e.length - 1] = {
49
+ for (const n of i)
50
+ n.type !== "transaction" && (n.type === "reasoning" ? (t != null && (e.push(t), t = null), e.push(n)) : n.functionCalls ? (t && e.push(t), e.push(n), t = null) : n.functionResponse ? e[e.length - 1] = {
51
51
  ...e[e.length - 1],
52
- functionResponse: s.functionResponse
53
- } : t && s.protocol === t.protocol && (t.role === s.role || s.finishReason === "stop") ? (t.text += s.text, t.files = [...t.files ?? [], ...s.files ?? []]) : (t && e.push(t), t = {
54
- ...s
52
+ functionResponse: n.functionResponse
53
+ } : t && n.protocol === t.protocol && (t.role === n.role || n.finishReason === "stop") ? (t.text += n.text, t.files = [...t.files ?? [], ...n.files ?? []]) : (t && e.push(t), t = {
54
+ ...n
55
55
  }));
56
- return t && e.push(t), tt(e);
56
+ return t && e.push(t), et(e);
57
57
  }
58
- function et(a) {
59
- var t, n;
60
- const e = ((t = a.files) == null ? void 0 : t.map(
61
- (s) => ({
58
+ function st(i) {
59
+ var t, s;
60
+ const e = ((t = i.files) == null ? void 0 : t.map(
61
+ (n) => ({
62
62
  type: "file",
63
- data: s.url,
64
- mimeType: s.contentType
63
+ data: n.url,
64
+ mimeType: n.contentType
65
65
  })
66
66
  )) ?? [];
67
- return a.role === "function" ? {
68
- id: a.id,
67
+ return i.role === "function" ? {
68
+ id: i.id,
69
69
  role: "assistant",
70
- status: (a == null ? void 0 : a.functionResponse) === null ? { type: "running" } : { type: "complete", reason: "stop" },
71
- content: ((n = a.functionCalls) == null ? void 0 : n.map((s) => {
72
- var i;
70
+ status: (i == null ? void 0 : i.functionResponse) === null ? { type: "running" } : { type: "complete", reason: "stop" },
71
+ content: ((s = i.functionCalls) == null ? void 0 : s.map((n) => {
72
+ var a;
73
73
  return {
74
74
  type: "tool-call",
75
- toolName: s.name,
76
- toolCallId: s.id,
77
- args: s.args,
78
- result: (i = a.functionResponse) == null ? void 0 : i.result
75
+ toolName: n.name,
76
+ toolCallId: n.id,
77
+ args: n.args,
78
+ result: (a = i.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 }, ...e] : [{ type: "text", text: a.text }, ...e]
82
+ id: i.id,
83
+ role: i.role,
84
+ content: i.type === "reasoning" ? [{ type: "reasoning", text: i.text }, ...e] : [{ type: "text", text: i.text }, ...e]
85
85
  };
86
86
  }
87
87
  class E {
@@ -103,7 +103,7 @@ class E {
103
103
  }
104
104
  async notifyPackets(e) {
105
105
  e.forEach((t) => {
106
- this.messageCallbacks.forEach((n) => n(t));
106
+ this.messageCallbacks.forEach((s) => s(t));
107
107
  });
108
108
  }
109
109
  async setSession(e) {
@@ -111,7 +111,7 @@ class E {
111
111
  }
112
112
  async setStatus(e) {
113
113
  const t = this.status !== e;
114
- this.status = e, t && this.statusChangeCallbacks.forEach((n) => n(e));
114
+ this.status = e, t && this.statusChangeCallbacks.forEach((s) => s(e));
115
115
  }
116
116
  clearListeners() {
117
117
  this.statusChangeCallbacks = [], this.messageCallbacks = [];
@@ -119,7 +119,7 @@ class E {
119
119
  onTransaction(e) {
120
120
  }
121
121
  }
122
- class M extends E {
122
+ class z extends E {
123
123
  constructor(t) {
124
124
  super();
125
125
  o(this, "status");
@@ -146,29 +146,29 @@ class M extends E {
146
146
  this.session = t;
147
147
  }
148
148
  async sendPacket(t) {
149
- var g, k;
150
- const { apiUrl: n, apiKey: s, agentId: i } = this.config, c = this.session ?? "new";
149
+ var g, w;
150
+ const { apiUrl: s, apiKey: n, agentId: a } = this.config, r = this.session ?? "new";
151
151
  if (t.type === "command" && ((g = t == null ? void 0 : t.payload) == null ? void 0 : g.command) == "set_initial_context") {
152
- this.context = (k = t == null ? void 0 : t.payload) == null ? void 0 : k.arguments;
152
+ this.context = (w = t == null ? void 0 : t.payload) == null ? void 0 : w.arguments;
153
153
  return;
154
154
  }
155
- const l = t.payload, f = await (await fetch(`${n}/sessions/${c}/messages`, {
156
- body: JSON.stringify({ agentId: i, userMessage: l, initial_context: this.context }),
155
+ const l = t.payload, p = await (await fetch(`${s}/sessions/${r}/messages`, {
156
+ body: JSON.stringify({ agentId: a, userMessage: l, initial_context: this.context }),
157
157
  method: "POST",
158
158
  headers: {
159
159
  "Content-Type": "application/json",
160
- "x-persona-apikey": s
160
+ "x-persona-apikey": n
161
161
  }
162
162
  })).json();
163
163
  this.notifyPackets(
164
- f.response.messages.map((w) => ({
164
+ p.response.messages.map((T) => ({
165
165
  type: "message",
166
- payload: w
166
+ payload: T
167
167
  }))
168
168
  );
169
169
  }
170
170
  }
171
- class z extends E {
171
+ class D extends E {
172
172
  constructor(t) {
173
173
  super();
174
174
  o(this, "status");
@@ -185,28 +185,28 @@ class z extends E {
185
185
  return 500;
186
186
  }
187
187
  async syncSession(t) {
188
- var n;
189
- (n = this.config.logger) == null || n.debug("Syncing session with WebSocket protocol:", t), this.session = t, this.webSocket && this.status === "connected" && (this.disconnect(), this.connect(t));
188
+ var s;
189
+ (s = this.config.logger) == null || s.debug("Syncing session with WebSocket protocol:", t), this.session = t, this.webSocket && this.status === "connected" && (this.disconnect(), this.connect(t));
190
190
  }
191
191
  connect(t) {
192
192
  var l;
193
193
  if (this.webSocket !== null && this.status === "connected")
194
194
  return Promise.resolve(this.session);
195
- const n = t || this.session || "new";
196
- (l = this.config.logger) == null || l.debug("Connecting to WebSocket with sessionId:", n);
197
- const s = encodeURIComponent(this.config.apiKey), i = this.config.agentId, c = `${this.config.webSocketUrl}?sessionCode=${n}&agentId=${i}&apiKey=${s}`;
198
- return this.setStatus("connecting"), this.webSocket = new WebSocket(c), this.webSocket.addEventListener("open", () => {
195
+ const s = t || this.session || "new";
196
+ (l = this.config.logger) == null || l.debug("Connecting to WebSocket with sessionId:", s);
197
+ const n = encodeURIComponent(this.config.apiKey), a = this.config.agentId, r = `${this.config.webSocketUrl}?sessionCode=${s}&agentId=${a}&apiKey=${n}`;
198
+ return this.setStatus("connecting"), this.webSocket = new WebSocket(r), this.webSocket.addEventListener("open", () => {
199
199
  this.setStatus("connected");
200
200
  }), this.webSocket.addEventListener("message", (u) => {
201
- const f = JSON.parse(u.data);
202
- this.notifyPacket(f);
201
+ const p = JSON.parse(u.data);
202
+ this.notifyPacket(p);
203
203
  }), this.webSocket.addEventListener("close", () => {
204
204
  var u;
205
205
  this.setStatus("disconnected"), this.webSocket = null, (u = this.config.logger) == null || u.warn("WebSocket connection closed");
206
206
  }), this.webSocket.addEventListener("error", (u) => {
207
- var f;
208
- this.setStatus("disconnected"), this.webSocket = null, (f = this.config.logger) == null || f.error("WebSocket error", u);
209
- }), Promise.resolve(n);
207
+ var p;
208
+ this.setStatus("disconnected"), this.webSocket = null, (p = this.config.logger) == null || p.error("WebSocket error", u);
209
+ }), Promise.resolve(s);
210
210
  }
211
211
  disconnect() {
212
212
  var t;
@@ -216,7 +216,7 @@ class z extends E {
216
216
  return this.webSocket && this.status === "connected" ? (this.webSocket.send(JSON.stringify(t)), Promise.resolve()) : Promise.reject(new Error("WebSocket is not connected"));
217
217
  }
218
218
  }
219
- class st {
219
+ class nt {
220
220
  constructor(e) {
221
221
  o(this, "config");
222
222
  o(this, "pc", null);
@@ -234,13 +234,13 @@ class st {
234
234
  this.config = e;
235
235
  }
236
236
  async connect(e) {
237
- var n;
237
+ var s;
238
238
  if (this.isConnected) return;
239
239
  this.isConnected = !0;
240
240
  try {
241
241
  this.localStream = await navigator.mediaDevices.getUserMedia({ audio: !0 });
242
- } catch (s) {
243
- (n = this.config.logger) == null || n.error("Error accessing microphone:", s);
242
+ } catch (n) {
243
+ (s = this.config.logger) == null || s.error("Error accessing microphone:", n);
244
244
  return;
245
245
  }
246
246
  this.pc = new RTCPeerConnection({
@@ -254,68 +254,68 @@ class st {
254
254
  credential: "webrtc"
255
255
  }
256
256
  ]
257
- }), this.localStream.getTracks().forEach((s) => {
258
- this.pc.addTrack(s, this.localStream);
259
- }), this.pc.ontrack = (s) => {
260
- s.streams[0].getTracks().forEach((c) => {
261
- this.remoteStream.addTrack(c);
257
+ }), this.localStream.getTracks().forEach((n) => {
258
+ this.pc.addTrack(n, this.localStream);
259
+ }), this.pc.ontrack = (n) => {
260
+ n.streams[0].getTracks().forEach((r) => {
261
+ this.remoteStream.addTrack(r);
262
262
  }), this.audioCtx || this._startAnalyzers();
263
- const i = new Audio();
264
- i.srcObject = this.remoteStream, i.play().catch((c) => {
263
+ const a = new Audio();
264
+ a.srcObject = this.remoteStream, a.play().catch((r) => {
265
265
  var l;
266
- (l = this.config.logger) == null || l.error("Error playing remote audio:", c);
266
+ (l = this.config.logger) == null || l.error("Error playing remote audio:", r);
267
267
  });
268
- }, this.pc.onicecandidate = (s) => {
269
- var i;
270
- s.candidate && ((i = this.ws) == null ? void 0 : i.readyState) === WebSocket.OPEN && this.ws.send(
268
+ }, this.pc.onicecandidate = (n) => {
269
+ var a;
270
+ n.candidate && ((a = this.ws) == null ? void 0 : a.readyState) === WebSocket.OPEN && this.ws.send(
271
271
  JSON.stringify({
272
272
  type: "CANDIDATE",
273
273
  src: "client",
274
- payload: { candidate: s.candidate }
274
+ payload: { candidate: n.candidate }
275
275
  })
276
276
  );
277
- }, this.pc.ondatachannel = (s) => {
278
- const i = s.channel;
279
- i.onmessage = (c) => {
277
+ }, this.pc.ondatachannel = (n) => {
278
+ const a = n.channel;
279
+ a.onmessage = (r) => {
280
280
  this.messageCallbacks.forEach((l) => {
281
- l(c);
281
+ l(r);
282
282
  });
283
283
  };
284
284
  };
285
285
  const t = this.config.webrtcUrl || "wss://persona.applica.guru/api/webrtc";
286
286
  this.ws = new WebSocket(`${t}?apiKey=${encodeURIComponent(this.config.apiKey)}`), this.ws.onopen = async () => {
287
287
  var l, u;
288
- const s = await this.pc.createOffer();
289
- await this.pc.setLocalDescription(s);
290
- const i = {
288
+ const n = await this.pc.createOffer();
289
+ await this.pc.setLocalDescription(n);
290
+ const a = {
291
291
  apiKey: this.config.apiKey,
292
292
  agentId: this.config.agentId,
293
293
  sessionCode: e
294
294
  };
295
- (l = this.config.logger) == null || l.debug("Opening connection to WebRTC server: ", i);
296
- const c = {
295
+ (l = this.config.logger) == null || l.debug("Opening connection to WebRTC server: ", a);
296
+ const r = {
297
297
  type: "OFFER",
298
298
  src: ((u = crypto.randomUUID) == null ? void 0 : u.call(crypto)) || "client_" + Date.now(),
299
299
  payload: {
300
300
  sdp: {
301
- sdp: s.sdp,
302
- type: s.type
301
+ sdp: n.sdp,
302
+ type: n.type
303
303
  },
304
304
  connectionId: (Date.now() % 1e6).toString(),
305
- metadata: i
305
+ metadata: a
306
306
  }
307
307
  };
308
- this.ws.send(JSON.stringify(c));
309
- }, this.ws.onmessage = async (s) => {
310
- var c;
311
- const i = JSON.parse(s.data);
312
- if (i.type === "ANSWER")
313
- await this.pc.setRemoteDescription(new RTCSessionDescription(i.payload.sdp));
314
- else if (i.type === "CANDIDATE")
308
+ this.ws.send(JSON.stringify(r));
309
+ }, this.ws.onmessage = async (n) => {
310
+ var r;
311
+ const a = JSON.parse(n.data);
312
+ if (a.type === "ANSWER")
313
+ await this.pc.setRemoteDescription(new RTCSessionDescription(a.payload.sdp));
314
+ else if (a.type === "CANDIDATE")
315
315
  try {
316
- await this.pc.addIceCandidate(new RTCIceCandidate(i.payload.candidate));
316
+ await this.pc.addIceCandidate(new RTCIceCandidate(a.payload.candidate));
317
317
  } catch (l) {
318
- (c = this.config.logger) == null || c.error("Error adding ICE candidate:", l);
318
+ (r = this.config.logger) == null || r.error("Error adding ICE candidate:", l);
319
319
  }
320
320
  }, this.ws.onclose = () => {
321
321
  this._stopAnalyzers();
@@ -336,18 +336,18 @@ class st {
336
336
  var t;
337
337
  return (t = this.config.logger) == null ? void 0 : t.info("Data channel opened");
338
338
  }, this.dataChannel.onmessage = (t) => {
339
- this.messageCallbacks.forEach((n) => {
340
- n(t);
339
+ this.messageCallbacks.forEach((s) => {
340
+ s(t);
341
341
  });
342
342
  });
343
343
  }
344
344
  sendPacket(e) {
345
- var t, n;
345
+ var t, s;
346
346
  if (!this.dataChannel) {
347
347
  (t = this.config.logger) == null || t.warn("Data channel is not open, cannot send message");
348
348
  return;
349
349
  }
350
- this.dataChannel.send(JSON.stringify(e)), (n = this.config.logger) == null || n.info("Sent message:", e);
350
+ this.dataChannel.send(JSON.stringify(e)), (s = this.config.logger) == null || s.info("Sent message:", e);
351
351
  }
352
352
  _startAnalyzers() {
353
353
  if (!this.localStream || !this.remoteStream || this.visualizerCallbacks.length === 0)
@@ -355,26 +355,26 @@ class st {
355
355
  this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
356
356
  const e = this.audioCtx.createMediaStreamSource(this.localStream), t = this.audioCtx.createMediaStreamSource(this.remoteStream);
357
357
  this.localAnalyser = this.audioCtx.createAnalyser(), this.remoteAnalyser = this.audioCtx.createAnalyser(), this.localAnalyser.fftSize = 256, this.remoteAnalyser.fftSize = 256, e.connect(this.localAnalyser), t.connect(this.remoteAnalyser);
358
- const n = () => {
358
+ const s = () => {
359
359
  if (!this.localAnalyser || !this.remoteAnalyser || this.visualizerCallbacks.length === 0)
360
360
  return;
361
- const s = new Uint8Array(this.localAnalyser.frequencyBinCount), i = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
362
- this.localAnalyser.getByteFrequencyData(s), this.remoteAnalyser.getByteFrequencyData(i);
363
- const c = s.reduce((u, f) => u + f, 0) / s.length, l = i.reduce((u, f) => u + f, 0) / i.length;
361
+ const n = new Uint8Array(this.localAnalyser.frequencyBinCount), a = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
362
+ this.localAnalyser.getByteFrequencyData(n), this.remoteAnalyser.getByteFrequencyData(a);
363
+ const r = n.reduce((u, p) => u + p, 0) / n.length, l = a.reduce((u, p) => u + p, 0) / a.length;
364
364
  this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((u) => {
365
365
  u({
366
- localAmplitude: c,
366
+ localAmplitude: r,
367
367
  remoteAmplitude: l
368
368
  });
369
- }), this.analyzerFrame = requestAnimationFrame(n);
369
+ }), this.analyzerFrame = requestAnimationFrame(s);
370
370
  };
371
- this.analyzerFrame = requestAnimationFrame(n);
371
+ this.analyzerFrame = requestAnimationFrame(s);
372
372
  }
373
373
  _stopAnalyzers() {
374
374
  this.analyzerFrame && (cancelAnimationFrame(this.analyzerFrame), this.analyzerFrame = null), this.localAnalyser = null, this.remoteAnalyser = null;
375
375
  }
376
376
  }
377
- class D extends E {
377
+ class U extends E {
378
378
  constructor(t) {
379
379
  super();
380
380
  o(this, "status");
@@ -382,9 +382,9 @@ class D extends E {
382
382
  o(this, "autostart");
383
383
  o(this, "config");
384
384
  o(this, "webRTCClient");
385
- this.config = t, this.status = "disconnected", this.session = null, this.autostart = (t == null ? void 0 : t.autostart) ?? !1, this.webRTCClient = new st(t), this.webRTCClient.addMessageCallback((n) => {
386
- const s = JSON.parse(n.data);
387
- this.notifyPacket(s);
385
+ this.config = t, this.status = "disconnected", this.session = null, this.autostart = (t == null ? void 0 : t.autostart) ?? !1, this.webRTCClient = new nt(t), this.webRTCClient.addMessageCallback((s) => {
386
+ const n = JSON.parse(s.data);
387
+ this.notifyPacket(n);
388
388
  });
389
389
  }
390
390
  getName() {
@@ -397,31 +397,31 @@ class D extends E {
397
397
  super.syncSession(t), this.status === "connected" && (await this.disconnect(), await this.connect(t));
398
398
  }
399
399
  async connect(t) {
400
- var n;
401
- return this.status === "connected" ? Promise.resolve(this.session) : (this.session = t || this.session || "new", this.setStatus("connecting"), (n = this.config.logger) == null || n.debug("Connecting to WebRTC with sessionId:", this.session), await this.webRTCClient.connect(this.session), this.setStatus("connected"), await this.webRTCClient.createDataChannel(), this.session);
400
+ var s;
401
+ return this.status === "connected" ? Promise.resolve(this.session) : (this.session = t || 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);
402
402
  }
403
403
  async disconnect() {
404
- var t, n, s;
404
+ var t, s, n;
405
405
  if (this.status === "disconnected")
406
406
  return (t = this.config.logger) == null || t.warn("Already disconnected"), Promise.resolve();
407
- await this.webRTCClient.disconnect(), this.setStatus("disconnected"), (s = (n = this.config) == null ? void 0 : n.logger) == null || s.debug("Disconnected from WebRTC");
407
+ await this.webRTCClient.disconnect(), this.setStatus("disconnected"), (n = (s = this.config) == null ? void 0 : s.logger) == null || n.debug("Disconnected from WebRTC");
408
408
  }
409
409
  sendPacket(t) {
410
410
  return this.status !== "connected" ? Promise.reject(new Error("Not connected")) : (this.webRTCClient.sendPacket(t), Promise.resolve());
411
411
  }
412
412
  }
413
- class nt {
413
+ class it {
414
414
  constructor(e) {
415
415
  o(this, "config");
416
416
  this.config = e;
417
417
  }
418
418
  async complete(e, t) {
419
- var n;
420
- await this.persist(e, { ...t, success: !0 }), (n = this.config.logger) == null || n.debug("Transaction completed:", e);
419
+ var s;
420
+ await this.persist(e, { ...t, success: !0 }), (s = this.config.logger) == null || s.debug("Transaction completed:", e);
421
421
  }
422
422
  async fail(e, t) {
423
- var n;
424
- await this.persist(e, { ...t, success: !1 }), (n = this.config.logger) == null || n.debug("Transaction failed:", { ...e, ...t });
423
+ var s;
424
+ await this.persist(e, { ...t, success: !1 }), (s = this.config.logger) == null || s.debug("Transaction failed:", { ...e, ...t });
425
425
  }
426
426
  async persist(e, t) {
427
427
  await fetch(`${this.config.apiUrl}/transactions/${e.id}`, {
@@ -435,7 +435,7 @@ class nt {
435
435
  });
436
436
  }
437
437
  }
438
- class it {
438
+ class at {
439
439
  constructor(e, t) {
440
440
  o(this, "transaction");
441
441
  o(this, "manager");
@@ -450,16 +450,16 @@ class it {
450
450
  await this.fail("No function call found");
451
451
  return;
452
452
  }
453
- const n = t.name, s = t.args, i = e[n];
454
- if (!i) {
455
- await this.fail(`Tool ${n} not found`);
453
+ const s = t.name, n = t.args, a = e[s];
454
+ if (!a) {
455
+ await this.fail(`Tool ${s} not found`);
456
456
  return;
457
457
  }
458
458
  try {
459
- const c = await i(s);
460
- await this.complete(c);
461
- } catch (c) {
462
- await this.fail(`Error executing tool ${n}: ${c}`);
459
+ const r = await a(n);
460
+ await this.complete(r);
461
+ } catch (r) {
462
+ await this.fail(`Error executing tool ${s}: ${r}`);
463
463
  }
464
464
  }
465
465
  async complete(e) {
@@ -469,7 +469,7 @@ class it {
469
469
  await this.manager.fail(this.transaction, { success: !1, output: null, error: e });
470
470
  }
471
471
  }
472
- class at extends E {
472
+ class ot extends E {
473
473
  constructor(t) {
474
474
  super();
475
475
  o(this, "status");
@@ -497,169 +497,170 @@ class at extends E {
497
497
  async sendPacket(t) {
498
498
  }
499
499
  onTransaction(t) {
500
- var i;
500
+ var a;
501
501
  if (!this.config.onTransaction) {
502
- (i = this.config.logger) == null || i.error("Transaction protocol config is not set");
502
+ (a = this.config.logger) == null || a.error("Transaction protocol config is not set");
503
503
  return;
504
504
  }
505
- const n = new nt(this.config), s = new it(t, n);
506
- this.config.onTransaction(s);
505
+ const s = new it(this.config), n = new at(t, s);
506
+ this.config.onTransaction(n);
507
507
  }
508
508
  }
509
- const P = q(void 0);
510
- function ot(a) {
509
+ const P = B(void 0);
510
+ function rt(i) {
511
511
  return new Promise((e, t) => {
512
- const n = new FileReader();
513
- n.readAsDataURL(a), n.onload = () => {
514
- const i = n.result.split(";base64,")[1];
515
- e(i);
516
- }, n.onerror = (s) => {
517
- t(s);
512
+ const s = new FileReader();
513
+ s.readAsDataURL(i), s.onload = () => {
514
+ const a = s.result.split(";base64,")[1];
515
+ e(a);
516
+ }, s.onerror = (n) => {
517
+ t(n);
518
518
  };
519
519
  });
520
520
  }
521
- function rt({
522
- dev: a = !1,
523
- protocols: e,
524
- logger: t,
521
+ function ct({
522
+ dev: i = !1,
523
+ baseUrl: e,
524
+ protocols: t,
525
+ logger: s,
525
526
  children: n,
526
- session: s = "new",
527
- ...i
527
+ session: a = "new",
528
+ ...r
528
529
  }) {
529
- const [c, l] = R(!1), [u, f] = R([]), [g, k] = R(s), [w, W] = R(/* @__PURE__ */ new Map()), $ = B(!1), b = Y(() => {
530
- if (Array.isArray(e))
531
- return e;
532
- if (typeof e == "object" && e !== null) {
533
- const r = a ? "localhost:8000" : "persona.applica.guru/api", p = a ? "http" : "https", d = a ? "ws" : "wss";
534
- let m = Object.keys(e).map((y) => {
530
+ const [l, u] = R(!1), [p, g] = R([]), [w, T] = R(a), [k, j] = R(/* @__PURE__ */ new Map()), N = Y(!1), b = G(() => {
531
+ if (Array.isArray(t))
532
+ return t;
533
+ if (typeof t == "object" && t !== null) {
534
+ const c = i ? "localhost:8000" : e || "persona.applica.guru/api", f = i ? "http" : "https", d = i ? "ws" : "wss";
535
+ let m = Object.keys(t).map((y) => {
535
536
  switch (y) {
536
537
  case "rest":
537
- const h = e[y];
538
- return h === !0 ? new M({
539
- apiUrl: `${p}://${r}`,
540
- apiKey: i.apiKey,
541
- agentId: i.agentId,
542
- logger: t
543
- }) : typeof h == "object" && h !== null ? new M(h) : null;
538
+ const h = t[y];
539
+ return h === !0 ? new z({
540
+ apiUrl: `${f}://${c}`,
541
+ apiKey: r.apiKey,
542
+ agentId: r.agentId,
543
+ logger: s
544
+ }) : typeof h == "object" && h !== null ? new z(h) : null;
544
545
  case "webrtc":
545
- const C = e[y];
546
- return C === !0 ? new D({
547
- webrtcUrl: `${d}://${r}/webrtc`,
548
- apiKey: i.apiKey,
549
- agentId: i.agentId,
550
- logger: t
551
- }) : typeof C == "object" && C !== null ? new D(C) : null;
546
+ const C = t[y];
547
+ return C === !0 ? new U({
548
+ webrtcUrl: `${d}://${c}/webrtc`,
549
+ apiKey: r.apiKey,
550
+ agentId: r.agentId,
551
+ logger: s
552
+ }) : typeof C == "object" && C !== null ? new U(C) : null;
552
553
  case "websocket":
553
- const x = e[y];
554
- return x === !0 ? new z({
555
- webSocketUrl: `${d}://${r}/websocket`,
556
- apiKey: i.apiKey,
557
- agentId: i.agentId,
558
- logger: t
559
- }) : typeof x == "object" && x !== null ? new z(x) : null;
554
+ const x = t[y];
555
+ return x === !0 ? new D({
556
+ webSocketUrl: `${d}://${c}/websocket`,
557
+ apiKey: r.apiKey,
558
+ agentId: r.agentId,
559
+ logger: s
560
+ }) : typeof x == "object" && x !== null ? new D(x) : null;
560
561
  default:
561
562
  throw new Error(`Unknown protocol: ${y}`);
562
563
  }
563
564
  }).filter((y) => y !== null);
564
- return i.tools && m.push(
565
- new at({
566
- apiUrl: `${p}://${r}`,
567
- apiKey: i.apiKey,
568
- agentId: i.agentId,
565
+ return r.tools && m.push(
566
+ new ot({
567
+ apiUrl: `${f}://${c}`,
568
+ apiKey: r.apiKey,
569
+ agentId: r.agentId,
569
570
  onTransaction: async (y) => {
570
- await y.invoke(i.tools);
571
+ await y.invoke(r.tools);
571
572
  },
572
- logger: t
573
+ logger: s
573
574
  })
574
575
  ), m;
575
576
  }
576
577
  throw new Error("Invalid protocols configuration");
577
578
  }, []);
578
- G(() => {
579
- $.current || ($.current = !0, t == null || t.debug(
579
+ V(() => {
580
+ N.current || (N.current = !0, s == null || s.debug(
580
581
  "Initializing protocols: ",
581
- b.map((r) => r.getName())
582
- ), b.forEach((r) => {
583
- r.setSession(g), r.clearListeners(), r.addStatusChangeListener((p) => {
584
- t == null || t.debug(`${r.getName()} has notified new status: ${p}`), w.set(r.getName(), p), p === "connected" && i.context && r.sendPacket({
582
+ b.map((c) => c.getName())
583
+ ), b.forEach((c) => {
584
+ c.setSession(w), c.clearListeners(), c.addStatusChangeListener((f) => {
585
+ s == null || s.debug(`${c.getName()} has notified new status: ${f}`), k.set(c.getName(), f), f === "connected" && r.context && c.sendPacket({
585
586
  type: "command",
586
587
  payload: {
587
588
  command: "set_initial_context",
588
- arguments: i.context
589
+ arguments: r.context
589
590
  }
590
- }), W(new Map(w));
591
- }), r.addPacketListener((p) => {
592
- if (p.type === "message") {
593
- const d = p.payload;
594
- f(
595
- (m) => I([...m, { ...d, protocol: r.getName() }])
591
+ }), j(new Map(k));
592
+ }), c.addPacketListener((f) => {
593
+ if (f.type === "message") {
594
+ const d = f.payload;
595
+ g(
596
+ (m) => M([...m, { ...d, protocol: c.getName() }])
596
597
  );
597
- } else if (p.type === "reasoning") {
598
- const d = p.payload;
598
+ } else if (f.type === "reasoning") {
599
+ const d = f.payload;
599
600
  let m = d.thought;
600
601
  d.imageUrl && (m += `
601
602
 
602
603
  ![image](https://persona.applica.guru/api/files/${d.imageUrl})`);
603
604
  const y = { type: "reasoning", text: m, role: "assistant", finishReason: "stop" };
604
- f(
605
- (h) => I([...h, { ...y, protocol: r.getName() }])
605
+ g(
606
+ (h) => M([...h, { ...y, protocol: c.getName() }])
606
607
  );
607
- } else p.type === "transaction" && b.filter((d) => d !== r).forEach((d) => d.onTransaction(p.payload));
608
- }), r.autostart && r.status === "disconnected" && (t == null || t.debug(`Connecting to protocol: ${r.getName()}`), r.connect(g));
608
+ } else f.type === "transaction" && b.filter((d) => d !== c).forEach((d) => d.onTransaction(f.payload));
609
+ }), c.autostart && c.status === "disconnected" && (s == null || s.debug(`Connecting to protocol: ${c.getName()}`), c.connect(w));
609
610
  }));
610
- }, [g, b, t, w]);
611
- const j = async (r) => {
611
+ }, [w, b, s, k]);
612
+ const O = async (c) => {
612
613
  var y;
613
- if (((y = r.content[0]) == null ? void 0 : y.type) !== "text") throw new Error("Only text messages are supported");
614
- const p = r.content[0].text;
615
- f((h) => [...h, { role: "user", type: "text", text: p }]), l(!0);
614
+ if (((y = c.content[0]) == null ? void 0 : y.type) !== "text") throw new Error("Only text messages are supported");
615
+ const f = c.content[0].text;
616
+ g((h) => [...h, { role: "user", type: "text", text: f }]), u(!0);
616
617
  const d = b.sort((h, C) => C.getPriority() - h.getPriority()).find((h) => h.status === "connected"), m = [];
617
- if (r.attachments)
618
- for (const h of r.attachments)
618
+ if (c.attachments)
619
+ for (const h of c.attachments)
619
620
  h.contentType.startsWith("image/") && h.file && m.push({
620
621
  role: "user",
621
622
  image: {
622
623
  contentType: h.contentType,
623
- content: await ot(h.file)
624
+ content: await rt(h.file)
624
625
  },
625
626
  text: "",
626
627
  type: "text"
627
628
  });
628
- r.content && m.push({
629
+ c.content && m.push({
629
630
  role: "user",
630
- text: r.content[0].text,
631
+ text: c.content[0].text,
631
632
  type: "text"
632
- }), t == null || t.debug("Sending message:", m), await (d == null ? void 0 : d.sendPacket({ type: "request", payload: m })), l(!1);
633
- }, O = T(() => (l(!1), f([]), k("new"), Promise.resolve()), []), L = T(() => Promise.resolve(), []), F = T(() => u, [u]), _ = V({
634
- isRunning: c,
635
- messages: u,
636
- convertMessage: et,
637
- onNew: j,
638
- onCancel: O,
639
- onReload: L,
633
+ }), s == null || s.debug("Sending message:", m), await (d == null ? void 0 : d.sendPacket({ type: "request", payload: m })), u(!1);
634
+ }, L = v(() => (u(!1), g([]), T("new"), Promise.resolve()), []), F = v(() => Promise.resolve(), []), _ = v(() => p, [p]), J = H({
635
+ isRunning: l,
636
+ messages: p,
637
+ convertMessage: st,
638
+ onNew: O,
639
+ onCancel: L,
640
+ onReload: F,
640
641
  adapters: {
641
- attachments: new H([new Q()])
642
+ attachments: new Q([new X()])
642
643
  }
643
644
  });
644
- return /* @__PURE__ */ v.jsx(P.Provider, { value: { protocols: b, protocolsStatus: w, getMessages: F }, children: /* @__PURE__ */ v.jsx(X, { runtime: _, children: n }) });
645
+ return /* @__PURE__ */ $.jsx(P.Provider, { value: { protocols: b, protocolsStatus: k, getMessages: _ }, children: /* @__PURE__ */ $.jsx(Z, { runtime: J, children: n }) });
645
646
  }
646
- function dt({ children: a, ...e }) {
647
- return /* @__PURE__ */ v.jsx(rt, { ...e, children: a });
647
+ function ft({ children: i, ...e }) {
648
+ return /* @__PURE__ */ $.jsx(ct, { ...e, children: i });
648
649
  }
649
- function ft() {
650
- const a = A(P);
651
- if (!a)
650
+ function pt() {
651
+ const i = A(P);
652
+ if (!i)
652
653
  throw new Error("usePersonaRuntime must be used within a PersonaRuntimeProvider");
653
- return a;
654
+ return i;
654
655
  }
655
- function ct(a) {
656
+ function lt(i) {
656
657
  const e = A(P);
657
658
  if (!e)
658
659
  throw new Error("usePersonaRuntimeProtocol must be used within a PersonaRuntimeProvider");
659
- const t = e.protocols.find((s) => s.getName() === a);
660
+ const t = e.protocols.find((n) => n.getName() === i);
660
661
  if (!t)
661
662
  return null;
662
- const n = e.protocolsStatus.get(t.getName());
663
+ const s = e.protocolsStatus.get(t.getName());
663
664
  return {
664
665
  ...t,
665
666
  connect: t.connect.bind(t),
@@ -670,28 +671,28 @@ function ct(a) {
670
671
  addPacketListener: t.addPacketListener.bind(t),
671
672
  getName: t.getName.bind(t),
672
673
  getPriority: t.getPriority.bind(t),
673
- status: n || t.status
674
+ status: s || t.status
674
675
  };
675
676
  }
676
- function pt() {
677
- const a = A(P);
678
- if (!a)
677
+ function yt() {
678
+ const i = A(P);
679
+ if (!i)
679
680
  throw new Error("usePersonaRuntimeEndpoint must be used within a PersonaRuntimeProvider");
680
- for (const e of a.protocols)
681
+ for (const e of i.protocols)
681
682
  if (e.getName() === "rest")
682
683
  return e.config.apiUrl;
683
684
  throw new Error("REST protocol not found");
684
685
  }
685
- function yt() {
686
- return ct("webrtc");
687
- }
688
686
  function mt() {
689
- const a = A(P);
690
- if (!a)
687
+ return lt("webrtc");
688
+ }
689
+ function gt() {
690
+ const i = A(P);
691
+ if (!i)
691
692
  throw new Error("usePersonaRuntimeMessages must be used within a PersonaRuntimeProvider");
692
- return a.getMessages();
693
+ return i.getMessages();
693
694
  }
694
- class gt {
695
+ class wt {
695
696
  constructor() {
696
697
  o(this, "prefix", "[Persona]");
697
698
  }
@@ -712,17 +713,17 @@ class gt {
712
713
  }
713
714
  }
714
715
  export {
715
- gt as PersonaConsoleLogger,
716
+ wt as PersonaConsoleLogger,
716
717
  E as PersonaProtocolBase,
717
- M as PersonaRESTProtocol,
718
- dt as PersonaRuntimeProvider,
719
- at as PersonaTransactionProtocol,
720
- D as PersonaWebRTCProtocol,
721
- z as PersonaWebSocketProtocol,
722
- ft as usePersonaRuntime,
723
- pt as usePersonaRuntimeEndpoint,
724
- mt as usePersonaRuntimeMessages,
725
- ct as usePersonaRuntimeProtocol,
726
- yt as usePersonaRuntimeWebRTCProtocol
718
+ z as PersonaRESTProtocol,
719
+ ft as PersonaRuntimeProvider,
720
+ ot as PersonaTransactionProtocol,
721
+ U as PersonaWebRTCProtocol,
722
+ D as PersonaWebSocketProtocol,
723
+ pt as usePersonaRuntime,
724
+ yt as usePersonaRuntimeEndpoint,
725
+ gt as usePersonaRuntimeMessages,
726
+ lt as usePersonaRuntimeProtocol,
727
+ mt as usePersonaRuntimeWebRTCProtocol
727
728
  };
728
729
  //# sourceMappingURL=bundle.es.js.map