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

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 P, 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;
@@ -119,7 +119,7 @@ class x {
119
119
  onTransaction(t) {
120
120
  }
121
121
  }
122
- class M extends x {
122
+ class N extends x {
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, h = 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
+ h.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 x {
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 h = JSON.parse(u.data);
196
+ this.notifyMessage(h);
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 h;
202
+ this.setStatus("disconnected"), this.webSocket = null, (h = this.config.logger) == null || h.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, h) => u + h, 0) / s.length, l = i.reduce((u, h) => u + h, 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 x {
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) {
@@ -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,48 +522,48 @@ 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] = P(!1), [u, h] = P([]), [b, W] = P(s), [S, j] = P(/* @__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", f = a ? "http" : "https", y = a ? "ws" : "wss";
530
+ let g = Object.keys(t).map((p) => {
531
+ switch (p) {
532
532
  case "rest":
533
- const h = t[y];
534
- return h === !0 ? new M({
535
- apiUrl: `${f}://${c}`,
533
+ const d = t[p];
534
+ return d === !0 ? new N({
535
+ apiUrl: `${f}://${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 d == "object" && d !== null ? new N(d) : 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[p];
542
+ return w === !0 ? new z({
543
+ webrtcUrl: `${y}://${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[p];
550
+ return k === !0 ? new I({
551
+ webSocketUrl: `${y}://${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: ${p}`);
558
558
  }
559
- }).filter((y) => y !== null);
559
+ }).filter((p) => p !== null);
560
560
  return i.tools && g.push(
561
561
  new ie({
562
- apiUrl: `${f}://${c}`,
562
+ apiUrl: `${f}://${r}`,
563
563
  apiKey: i.apiKey,
564
564
  agentId: i.agentId,
565
- onTransaction: async (y) => {
566
- await y.invoke(i.tools);
565
+ onTransaction: async (p) => {
566
+ await p.invoke(i.tools);
567
567
  },
568
568
  logger: e
569
569
  })
@@ -571,57 +571,62 @@ function oe({
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) => {
577
+ m.map((r) => r.getName())
578
+ ), m.forEach((r) => {
579
+ r.setSession(b), r.clearListeners(), r.addStatusChangeListener((f) => {
580
+ e == null || e.debug(`${r.getName()} has notified new status: ${f}`), S.set(r.getName(), f), j(new Map(S));
581
+ }), r.addMessageListener((f) => {
582
582
  if (f.type === "message") {
583
- const p = f.payload;
584
- d(
585
- (g) => Z([...g, { ...p, protocol: c.getName() }])
583
+ const y = f.payload;
584
+ h(
585
+ (g) => M([...g, { ...y, protocol: r.getName() }])
586
+ );
587
+ } else if (f.type === "reasoning") {
588
+ const g = { type: "reasoning", text: f.payload.thought, role: "assistant", finishReason: "stop" };
589
+ h(
590
+ (p) => M([...p, { ...g, protocol: r.getName() }])
586
591
  );
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));
592
+ } else f.type === "transaction" && m.filter((y) => y !== r).forEach((y) => y.onTransaction(f.payload));
593
+ }), r.autostart && r.status === "disconnected" && (e == null || e.debug(`Connecting to protocol: ${r.getName()}`), r.connect(b));
589
594
  }));
590
595
  }, [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({
596
+ const O = async (r) => {
597
+ var p;
598
+ if (((p = r.content[0]) == null ? void 0 : p.type) !== "text") throw new Error("Only text messages are supported");
599
+ const f = r.content[0].text;
600
+ h((d) => [...d, { role: "user", type: "text", text: f }]), l(!0);
601
+ const y = m.sort((d, w) => w.getPriority() - d.getPriority()).find((d) => d.status === "connected"), g = [];
602
+ if (r.attachments)
603
+ for (const d of r.attachments)
604
+ d.contentType.startsWith("image/") && d.file && g.push({
600
605
  role: "user",
601
606
  image: {
602
- contentType: h.contentType,
603
- content: await ae(h.file)
607
+ contentType: d.contentType,
608
+ content: await ae(d.file)
604
609
  },
605
610
  text: "",
606
611
  type: "text"
607
612
  });
608
- c.content && g.push({
613
+ r.content && g.push({
609
614
  role: "user",
610
- text: c.content[0].text,
615
+ text: r.content[0].text,
611
616
  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,
617
+ }), e == null || e.debug("Sending message:", g), await (y == null ? void 0 : y.send(g)), l(!1);
618
+ }, U = v(() => (l(!1), h([]), W("new"), Promise.resolve()), []), L = v(() => Promise.resolve(), []), F = G({
619
+ isRunning: c,
615
620
  messages: u,
616
621
  convertMessage: ee,
617
- onNew: j,
618
- onCancel: O,
619
- onReload: U,
622
+ onNew: O,
623
+ onCancel: U,
624
+ onReload: L,
620
625
  adapters: {
621
- attachments: new G([new V()])
626
+ attachments: new V([new H()])
622
627
  }
623
628
  });
624
- return /* @__PURE__ */ A.jsx(R.Provider, { value: { protocols: m, protocolsStatus: S }, children: /* @__PURE__ */ A.jsx(H, { runtime: L, children: n }) });
629
+ return /* @__PURE__ */ A.jsx(R.Provider, { value: { protocols: m, protocolsStatus: S }, children: /* @__PURE__ */ A.jsx(Q, { runtime: F, children: n }) });
625
630
  }
626
631
  function he({ children: a, ...t }) {
627
632
  return /* @__PURE__ */ A.jsx(oe, { ...t, children: a });
@@ -688,11 +693,11 @@ class ye {
688
693
  export {
689
694
  ye as PersonaConsoleLogger,
690
695
  x as PersonaProtocolBase,
691
- M as PersonaRESTProtocol,
696
+ N as PersonaRESTProtocol,
692
697
  he as PersonaRuntimeProvider,
693
698
  ie as PersonaTransactionProtocol,
694
- I as PersonaWebRTCProtocol,
695
- N as PersonaWebSocketProtocol,
699
+ z as PersonaWebRTCProtocol,
700
+ I as PersonaWebSocketProtocol,
696
701
  de as usePersonaRuntime,
697
702
  fe as usePersonaRuntimeEndpoint,
698
703
  re as usePersonaRuntimeProtocol,