@applica-software-guru/persona-sdk 0.1.56 → 0.1.58

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 F = Object.defineProperty;
2
- var J = (a, t, e) => t in a ? F(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
3
- var o = (a, t, e) => J(a, typeof t != "symbol" ? t + "" : t, e);
4
- import { createContext as K, useContext as E, useState as P, useRef as q, useMemo as _, useEffect as B, useCallback as v } from "react";
5
- import { useExternalStoreRuntime as Y, CompositeAttachmentAdapter as G, SimpleImageAttachmentAdapter as V, AssistantRuntimeProvider as H } from "@assistant-ui/react";
6
- var z = { exports: {} }, C = {};
1
+ var J = Object.defineProperty;
2
+ var K = (a, t, e) => t in a ? J(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
3
+ var o = (a, t, e) => K(a, typeof t != "symbol" ? t + "" : t, e);
4
+ import { createContext as q, useContext as E, useState as x, useRef as _, useMemo as B, useEffect as Y, useCallback as v } from "react";
5
+ import { useExternalStoreRuntime as G, CompositeAttachmentAdapter as V, SimpleImageAttachmentAdapter as H, AssistantRuntimeProvider as Q } from "@assistant-ui/react";
6
+ var D = { exports: {} }, C = {};
7
7
  /**
8
8
  * @license React
9
9
  * react-jsx-runtime.production.js
@@ -14,13 +14,13 @@ var z = { exports: {} }, C = {};
14
14
  * LICENSE file in the root directory of this source tree.
15
15
  */
16
16
  var $;
17
- function Q() {
17
+ function X() {
18
18
  if ($) return C;
19
19
  $ = 1;
20
20
  var a = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
21
21
  function e(n, s, i) {
22
- var r = null;
23
- if (i !== void 0 && (r = "" + i), s.key !== void 0 && (r = "" + s.key), "key" in s) {
22
+ var c = null;
23
+ if (i !== void 0 && (c = "" + i), s.key !== void 0 && (c = "" + s.key), "key" in s) {
24
24
  i = {};
25
25
  for (var l in s)
26
26
  l !== "key" && (i[l] = s[l]);
@@ -28,22 +28,22 @@ function Q() {
28
28
  return s = i.ref, {
29
29
  $$typeof: a,
30
30
  type: n,
31
- key: r,
31
+ key: c,
32
32
  ref: s !== void 0 ? s : null,
33
33
  props: i
34
34
  };
35
35
  }
36
36
  return C.Fragment = t, C.jsx = e, C.jsxs = e, C;
37
37
  }
38
- z.exports = Q();
39
- var A = z.exports;
40
- function X(a) {
38
+ D.exports = X();
39
+ var A = D.exports;
40
+ function Z(a) {
41
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 Z(a) {
46
+ function M(a) {
47
47
  const t = [];
48
48
  let e = null;
49
49
  for (const s of a)
@@ -53,7 +53,7 @@ function Z(a) {
53
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
- return e && t.push(e), X(t);
56
+ return e && t.push(e), Z(t);
57
57
  }
58
58
  function ee(a) {
59
59
  var e, n;
@@ -84,7 +84,7 @@ function ee(a) {
84
84
  content: a.type === "reasoning" ? [{ type: "reasoning", text: a.text }, ...t] : [{ type: "text", text: a.text }, ...t]
85
85
  };
86
86
  }
87
- class x {
87
+ class P {
88
88
  constructor() {
89
89
  o(this, "statusChangeCallbacks", []);
90
90
  o(this, "messageCallbacks", []);
@@ -119,7 +119,7 @@ class x {
119
119
  onTransaction(t) {
120
120
  }
121
121
  }
122
- class M extends x {
122
+ class N extends P {
123
123
  constructor(e) {
124
124
  super();
125
125
  o(this, "status");
@@ -145,7 +145,7 @@ class M extends x {
145
145
  this.session = e;
146
146
  }
147
147
  async send(e) {
148
- const { apiUrl: n, apiKey: s, agentId: i } = this.config, r = this.session ?? "new", l = e, d = await (await fetch(`${n}/agents/${i}/sessions/${r}/messages`, {
148
+ const { apiUrl: n, apiKey: s, agentId: i } = this.config, c = this.session ?? "new", l = e, f = await (await fetch(`${n}/agents/${i}/sessions/${c}/messages`, {
149
149
  body: JSON.stringify({ userMessage: l }),
150
150
  method: "POST",
151
151
  headers: {
@@ -155,14 +155,14 @@ class M extends x {
155
155
  }
156
156
  })).json();
157
157
  this.notifyMessages(
158
- d.response.messages.map((b) => ({
158
+ f.response.messages.map((b) => ({
159
159
  type: "message",
160
160
  payload: b
161
161
  }))
162
162
  );
163
163
  }
164
164
  }
165
- class N extends x {
165
+ class I extends P {
166
166
  constructor(e) {
167
167
  super();
168
168
  o(this, "status");
@@ -188,18 +188,18 @@ class N extends x {
188
188
  return Promise.resolve(this.session);
189
189
  const n = e || this.session || "new";
190
190
  (l = this.config.logger) == null || l.debug("Connecting to WebSocket with sessionId:", n);
191
- const s = encodeURIComponent(this.config.apiKey), i = this.config.agentId, r = `${this.config.webSocketUrl}?sessionCode=${n}&agentId=${i}&apiKey=${s}`;
192
- return this.setStatus("connecting"), this.webSocket = new WebSocket(r), this.webSocket.addEventListener("open", () => {
191
+ const s = encodeURIComponent(this.config.apiKey), i = this.config.agentId, c = `${this.config.webSocketUrl}?sessionCode=${n}&agentId=${i}&apiKey=${s}`;
192
+ return this.setStatus("connecting"), this.webSocket = new WebSocket(c), this.webSocket.addEventListener("open", () => {
193
193
  this.setStatus("connected");
194
194
  }), this.webSocket.addEventListener("message", (u) => {
195
- const d = JSON.parse(u.data);
196
- this.notifyMessage(d);
195
+ const f = JSON.parse(u.data);
196
+ this.notifyMessage(f);
197
197
  }), this.webSocket.addEventListener("close", () => {
198
198
  var u;
199
199
  this.setStatus("disconnected"), this.webSocket = null, (u = this.config.logger) == null || u.warn("WebSocket connection closed");
200
200
  }), this.webSocket.addEventListener("error", (u) => {
201
- var d;
202
- this.setStatus("disconnected"), this.webSocket = null, (d = this.config.logger) == null || d.error("WebSocket error", u);
201
+ var f;
202
+ this.setStatus("disconnected"), this.webSocket = null, (f = this.config.logger) == null || f.error("WebSocket error", u);
203
203
  }), Promise.resolve(n);
204
204
  }
205
205
  disconnect() {
@@ -251,13 +251,13 @@ class te {
251
251
  }), this.localStream.getTracks().forEach((s) => {
252
252
  this.pc.addTrack(s, this.localStream);
253
253
  }), this.pc.ontrack = (s) => {
254
- s.streams[0].getTracks().forEach((r) => {
255
- this.remoteStream.addTrack(r);
254
+ s.streams[0].getTracks().forEach((c) => {
255
+ this.remoteStream.addTrack(c);
256
256
  }), this.audioCtx || this._startAnalyzers();
257
257
  const i = new Audio();
258
- i.srcObject = this.remoteStream, i.play().catch((r) => {
258
+ i.srcObject = this.remoteStream, i.play().catch((c) => {
259
259
  var l;
260
- (l = this.config.logger) == null || l.error("Error playing remote audio:", r);
260
+ (l = this.config.logger) == null || l.error("Error playing remote audio:", c);
261
261
  });
262
262
  }, this.pc.onicecandidate = (s) => {
263
263
  var i;
@@ -270,9 +270,9 @@ class te {
270
270
  );
271
271
  }, this.pc.ondatachannel = (s) => {
272
272
  const i = s.channel;
273
- i.onmessage = (r) => {
273
+ i.onmessage = (c) => {
274
274
  this.messageCallbacks.forEach((l) => {
275
- l(r);
275
+ l(c);
276
276
  });
277
277
  };
278
278
  };
@@ -287,7 +287,7 @@ class te {
287
287
  sessionCode: t
288
288
  };
289
289
  (l = this.config.logger) == null || l.debug("Opening connection to WebRTC server: ", i);
290
- const r = {
290
+ const c = {
291
291
  type: "OFFER",
292
292
  src: ((u = crypto.randomUUID) == null ? void 0 : u.call(crypto)) || "client_" + Date.now(),
293
293
  payload: {
@@ -299,9 +299,9 @@ class te {
299
299
  metadata: i
300
300
  }
301
301
  };
302
- this.ws.send(JSON.stringify(r));
302
+ this.ws.send(JSON.stringify(c));
303
303
  }, this.ws.onmessage = async (s) => {
304
- var r;
304
+ var c;
305
305
  const i = JSON.parse(s.data);
306
306
  if (i.type === "ANSWER")
307
307
  await this.pc.setRemoteDescription(new RTCSessionDescription(i.payload.sdp));
@@ -309,7 +309,7 @@ class te {
309
309
  try {
310
310
  await this.pc.addIceCandidate(new RTCIceCandidate(i.payload.candidate));
311
311
  } catch (l) {
312
- (r = this.config.logger) == null || r.error("Error adding ICE candidate:", l);
312
+ (c = this.config.logger) == null || c.error("Error adding ICE candidate:", l);
313
313
  }
314
314
  }, this.ws.onclose = () => {
315
315
  this._stopAnalyzers();
@@ -354,10 +354,10 @@ class te {
354
354
  return;
355
355
  const s = new Uint8Array(this.localAnalyser.frequencyBinCount), i = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
356
356
  this.localAnalyser.getByteFrequencyData(s), this.remoteAnalyser.getByteFrequencyData(i);
357
- const r = s.reduce((u, d) => u + d, 0) / s.length, l = i.reduce((u, d) => u + d, 0) / i.length;
357
+ const c = s.reduce((u, f) => u + f, 0) / s.length, l = i.reduce((u, f) => u + f, 0) / i.length;
358
358
  this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((u) => {
359
359
  u({
360
- localAmplitude: r,
360
+ localAmplitude: c,
361
361
  remoteAmplitude: l
362
362
  });
363
363
  }), this.analyzerFrame = requestAnimationFrame(n);
@@ -368,7 +368,7 @@ class te {
368
368
  this.analyzerFrame && (cancelAnimationFrame(this.analyzerFrame), this.analyzerFrame = null), this.localAnalyser = null, this.remoteAnalyser = null;
369
369
  }
370
370
  }
371
- class I extends x {
371
+ class z extends P {
372
372
  constructor(e) {
373
373
  super();
374
374
  o(this, "status");
@@ -450,10 +450,10 @@ class ne {
450
450
  return;
451
451
  }
452
452
  try {
453
- const r = await i(s);
454
- await this.complete(r);
455
- } catch (r) {
456
- await this.fail(`Error executing tool ${n}: ${r}`);
453
+ const c = await i(s);
454
+ await this.complete(c);
455
+ } catch (c) {
456
+ await this.fail(`Error executing tool ${n}: ${c}`);
457
457
  }
458
458
  }
459
459
  async complete(t) {
@@ -463,7 +463,7 @@ class ne {
463
463
  await this.manager.fail(this.transaction, { success: !1, output: null, error: t });
464
464
  }
465
465
  }
466
- class ie extends x {
466
+ class ie extends P {
467
467
  constructor(e) {
468
468
  super();
469
469
  o(this, "status");
@@ -502,7 +502,7 @@ class ie extends x {
502
502
  this.config.onTransaction(s);
503
503
  }
504
504
  }
505
- const R = K(void 0);
505
+ const R = q(void 0);
506
506
  function ae(a) {
507
507
  return new Promise((t, e) => {
508
508
  const n = new FileReader();
@@ -522,81 +522,91 @@ function oe({
522
522
  session: s = "new",
523
523
  ...i
524
524
  }) {
525
- const [r, l] = P(!1), [u, d] = P([]), [b, D] = P(s), [S, W] = P(/* @__PURE__ */ new Map()), T = q(!1), m = _(() => {
525
+ const [c, l] = x(!1), [u, f] = x([]), [b, U] = x(s), [S, W] = x(/* @__PURE__ */ new Map()), T = _(!1), m = B(() => {
526
526
  if (Array.isArray(t))
527
527
  return t;
528
528
  if (typeof t == "object" && t !== null) {
529
- const c = a ? "localhost:8000" : "persona.applica.guru/api", f = a ? "http" : "https", p = a ? "ws" : "wss";
530
- let g = Object.keys(t).map((y) => {
531
- switch (y) {
529
+ const r = a ? "localhost:8000" : "persona.applica.guru/api", p = a ? "http" : "https", d = a ? "ws" : "wss";
530
+ let y = Object.keys(t).map((g) => {
531
+ switch (g) {
532
532
  case "rest":
533
- const h = t[y];
534
- return h === !0 ? new M({
535
- apiUrl: `${f}://${c}`,
533
+ const h = t[g];
534
+ return h === !0 ? new N({
535
+ apiUrl: `${p}://${r}`,
536
536
  apiKey: i.apiKey,
537
537
  agentId: i.agentId,
538
538
  logger: e
539
- }) : typeof h == "object" && h !== null ? new M(h) : null;
539
+ }) : typeof h == "object" && h !== null ? new N(h) : null;
540
540
  case "webrtc":
541
- const w = t[y];
542
- return w === !0 ? new I({
543
- webrtcUrl: `${p}://${c}/webrtc`,
541
+ const w = t[g];
542
+ return w === !0 ? new z({
543
+ webrtcUrl: `${d}://${r}/webrtc`,
544
544
  apiKey: i.apiKey,
545
545
  agentId: i.agentId,
546
546
  logger: e
547
- }) : typeof w == "object" && w !== null ? new I(w) : null;
547
+ }) : typeof w == "object" && w !== null ? new z(w) : null;
548
548
  case "websocket":
549
- const k = t[y];
550
- return k === !0 ? new N({
551
- webSocketUrl: `${p}://${c}/websocket`,
549
+ const k = t[g];
550
+ return k === !0 ? new I({
551
+ webSocketUrl: `${d}://${r}/websocket`,
552
552
  apiKey: i.apiKey,
553
553
  agentId: i.agentId,
554
554
  logger: e
555
- }) : typeof k == "object" && k !== null ? new N(k) : null;
555
+ }) : typeof k == "object" && k !== null ? new I(k) : null;
556
556
  default:
557
- throw new Error(`Unknown protocol: ${y}`);
557
+ throw new Error(`Unknown protocol: ${g}`);
558
558
  }
559
- }).filter((y) => y !== null);
560
- return i.tools && g.push(
559
+ }).filter((g) => g !== null);
560
+ return i.tools && y.push(
561
561
  new ie({
562
- apiUrl: `${f}://${c}`,
562
+ apiUrl: `${p}://${r}`,
563
563
  apiKey: i.apiKey,
564
564
  agentId: i.agentId,
565
- onTransaction: async (y) => {
566
- await y.invoke(i.tools);
565
+ onTransaction: async (g) => {
566
+ await g.invoke(i.tools);
567
567
  },
568
568
  logger: e
569
569
  })
570
- ), g;
570
+ ), y;
571
571
  }
572
572
  throw new Error("Invalid protocols configuration");
573
573
  }, []);
574
- B(() => {
574
+ Y(() => {
575
575
  T.current || (T.current = !0, e == null || e.debug(
576
576
  "Initializing protocols: ",
577
- m.map((c) => c.getName())
578
- ), m.forEach((c) => {
579
- c.setSession(b), c.clearListeners(), c.addStatusChangeListener((f) => {
580
- e == null || e.debug(`${c.getName()} has notified new status: ${f}`), S.set(c.getName(), f), W(new Map(S));
581
- }), c.addMessageListener((f) => {
582
- if (f.type === "message") {
583
- const p = f.payload;
584
- d(
585
- (g) => Z([...g, { ...p, protocol: c.getName() }])
577
+ m.map((r) => r.getName())
578
+ ), m.forEach((r) => {
579
+ r.setSession(b), r.clearListeners(), r.addStatusChangeListener((p) => {
580
+ e == null || e.debug(`${r.getName()} has notified new status: ${p}`), S.set(r.getName(), p), W(new Map(S));
581
+ }), r.addMessageListener((p) => {
582
+ if (p.type === "message") {
583
+ const d = p.payload;
584
+ f(
585
+ (y) => M([...y, { ...d, protocol: r.getName() }])
586
+ );
587
+ } else if (p.type === "reasoning") {
588
+ const d = p.payload;
589
+ let y = d.thought;
590
+ d.imageUrl && (y += `
591
+
592
+ ![image](https://persona.applica.guru/api/files/${d.imageUrl})`);
593
+ const g = { type: "reasoning", text: y, role: "assistant", finishReason: "stop" };
594
+ f(
595
+ (h) => M([...h, { ...g, protocol: r.getName() }])
586
596
  );
587
- } else f.type === "transaction" && m.filter((p) => p !== c).forEach((p) => p.onTransaction(f.payload));
588
- }), c.autostart && c.status === "disconnected" && (e == null || e.debug(`Connecting to protocol: ${c.getName()}`), c.connect(b));
597
+ } else p.type === "transaction" && m.filter((d) => d !== r).forEach((d) => d.onTransaction(p.payload));
598
+ }), r.autostart && r.status === "disconnected" && (e == null || e.debug(`Connecting to protocol: ${r.getName()}`), r.connect(b));
589
599
  }));
590
600
  }, [b, m, e, S]);
591
- const j = async (c) => {
592
- var y;
593
- if (((y = c.content[0]) == null ? void 0 : y.type) !== "text") throw new Error("Only text messages are supported");
594
- const f = c.content[0].text;
595
- d((h) => [...h, { role: "user", type: "text", text: f }]), l(!0);
596
- const p = m.sort((h, w) => w.getPriority() - h.getPriority()).find((h) => h.status === "connected"), g = [];
597
- if (c.attachments)
598
- for (const h of c.attachments)
599
- h.contentType.startsWith("image/") && h.file && g.push({
601
+ const j = async (r) => {
602
+ var g;
603
+ if (((g = r.content[0]) == null ? void 0 : g.type) !== "text") throw new Error("Only text messages are supported");
604
+ const p = r.content[0].text;
605
+ f((h) => [...h, { role: "user", type: "text", text: p }]), l(!0);
606
+ const d = m.sort((h, w) => w.getPriority() - h.getPriority()).find((h) => h.status === "connected"), y = [];
607
+ if (r.attachments)
608
+ for (const h of r.attachments)
609
+ h.contentType.startsWith("image/") && h.file && y.push({
600
610
  role: "user",
601
611
  image: {
602
612
  contentType: h.contentType,
@@ -605,23 +615,23 @@ function oe({
605
615
  text: "",
606
616
  type: "text"
607
617
  });
608
- c.content && g.push({
618
+ r.content && y.push({
609
619
  role: "user",
610
- text: c.content[0].text,
620
+ text: r.content[0].text,
611
621
  type: "text"
612
- }), e == null || e.debug("Sending message:", g), await (p == null ? void 0 : p.send(g)), l(!1);
613
- }, O = v(() => (l(!1), d([]), D("new"), Promise.resolve()), []), U = v(() => Promise.resolve(), []), L = Y({
614
- isRunning: r,
622
+ }), e == null || e.debug("Sending message:", y), await (d == null ? void 0 : d.send(y)), l(!1);
623
+ }, O = v(() => (l(!1), f([]), U("new"), Promise.resolve()), []), L = v(() => Promise.resolve(), []), F = G({
624
+ isRunning: c,
615
625
  messages: u,
616
626
  convertMessage: ee,
617
627
  onNew: j,
618
628
  onCancel: O,
619
- onReload: U,
629
+ onReload: L,
620
630
  adapters: {
621
- attachments: new G([new V()])
631
+ attachments: new V([new H()])
622
632
  }
623
633
  });
624
- return /* @__PURE__ */ A.jsx(R.Provider, { value: { protocols: m, protocolsStatus: S }, children: /* @__PURE__ */ A.jsx(H, { runtime: L, children: n }) });
634
+ return /* @__PURE__ */ A.jsx(R.Provider, { value: { protocols: m, protocolsStatus: S }, children: /* @__PURE__ */ A.jsx(Q, { runtime: F, children: n }) });
625
635
  }
626
636
  function he({ children: a, ...t }) {
627
637
  return /* @__PURE__ */ A.jsx(oe, { ...t, children: a });
@@ -665,7 +675,7 @@ function fe() {
665
675
  function pe() {
666
676
  return re("webrtc");
667
677
  }
668
- class ye {
678
+ class ge {
669
679
  constructor() {
670
680
  o(this, "prefix", "[Persona]");
671
681
  }
@@ -686,13 +696,13 @@ class ye {
686
696
  }
687
697
  }
688
698
  export {
689
- ye as PersonaConsoleLogger,
690
- x as PersonaProtocolBase,
691
- M as PersonaRESTProtocol,
699
+ ge as PersonaConsoleLogger,
700
+ P as PersonaProtocolBase,
701
+ N as PersonaRESTProtocol,
692
702
  he as PersonaRuntimeProvider,
693
703
  ie as PersonaTransactionProtocol,
694
- I as PersonaWebRTCProtocol,
695
- N as PersonaWebSocketProtocol,
704
+ z as PersonaWebRTCProtocol,
705
+ I as PersonaWebSocketProtocol,
696
706
  de as usePersonaRuntime,
697
707
  fe as usePersonaRuntimeEndpoint,
698
708
  re as usePersonaRuntimeProtocol,