@applica-software-guru/persona-sdk 0.1.65 → 0.1.66

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,7 +1,7 @@
1
1
  var B = Object.defineProperty;
2
2
  var V = (o, t, e) => t in o ? B(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
3
  var r = (o, t, e) => V(o, typeof t != "symbol" ? t + "" : t, e);
4
- import { createContext as Y, useContext as A, useState as E, useRef as G, useMemo as H, useEffect as Q, useCallback as M } from "react";
4
+ import { createContext as Y, useContext as A, useState as E, useRef as G, useMemo as H, useEffect as Q, useCallback as N } from "react";
5
5
  import { useExternalStoreRuntime as X, CompositeAttachmentAdapter as Z, SimpleImageAttachmentAdapter as ee, AssistantRuntimeProvider as te } from "@assistant-ui/react";
6
6
  var F = { exports: {} }, k = {};
7
7
  /**
@@ -13,17 +13,17 @@ var F = { exports: {} }, k = {};
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 j;
16
+ var O;
17
17
  function se() {
18
- if (j) return k;
19
- j = 1;
18
+ if (O) return k;
19
+ O = 1;
20
20
  var o = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
21
21
  function e(s, n, a) {
22
22
  var i = null;
23
23
  if (a !== void 0 && (i = "" + a), n.key !== void 0 && (i = "" + n.key), "key" in n) {
24
24
  a = {};
25
- for (var l in n)
26
- l !== "key" && (a[l] = n[l]);
25
+ for (var c in n)
26
+ c !== "key" && (a[c] = n[c]);
27
27
  } else a = n;
28
28
  return n = a.ref, {
29
29
  $$typeof: o,
@@ -36,14 +36,14 @@ function se() {
36
36
  return k.Fragment = t, k.jsx = e, k.jsxs = e, k;
37
37
  }
38
38
  F.exports = se();
39
- var N = F.exports;
39
+ var M = F.exports;
40
40
  function ne(o) {
41
41
  return o.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(o) {
46
+ function j(o) {
47
47
  const t = [];
48
48
  let e = null;
49
49
  for (const n of o)
@@ -147,10 +147,10 @@ class _ extends v {
147
147
  this.session = e;
148
148
  }
149
149
  async sendPacket(e) {
150
- var u, p, g, w;
150
+ var u, d, g, w;
151
151
  const { apiUrl: s, apiKey: n, agentId: a } = this.config, i = this.session ?? "new";
152
152
  if (e.type === "command" && ((u = e == null ? void 0 : e.payload) == null ? void 0 : u.command) == "set_initial_context") {
153
- this.context = (p = e == null ? void 0 : e.payload) == null ? void 0 : p.arguments;
153
+ this.context = (d = e == null ? void 0 : e.payload) == null ? void 0 : d.arguments;
154
154
  return;
155
155
  } else if (e.type === "command" && ((g = e == null ? void 0 : e.payload) == null ? void 0 : g.command) == "set_local_tools") {
156
156
  this.notifyPacket({
@@ -163,10 +163,10 @@ class _ extends v {
163
163
  });
164
164
  return;
165
165
  }
166
- const l = e.payload;
166
+ const c = e.payload;
167
167
  try {
168
168
  const b = await (await fetch(`${s}/sessions/${i}/messages`, {
169
- body: JSON.stringify({ agentId: a, userMessage: l, initialContext: this.context, tools: this.tools }),
169
+ body: JSON.stringify({ agentId: a, userMessage: c, initialContext: this.context, tools: this.tools }),
170
170
  method: "POST",
171
171
  headers: {
172
172
  "Content-Type": "application/json",
@@ -212,19 +212,19 @@ class W extends v {
212
212
  (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));
213
213
  }
214
214
  connect(e) {
215
- var l;
215
+ var c;
216
216
  if (this.webSocket !== null && this.status === "connected")
217
217
  return Promise.resolve(this.session);
218
218
  const s = e || this.session || "new";
219
- (l = this.config.logger) == null || l.debug("Connecting to WebSocket with sessionId:", s);
219
+ (c = this.config.logger) == null || c.debug("Connecting to WebSocket with sessionId:", s);
220
220
  const n = encodeURIComponent(this.config.apiKey), a = this.config.agentId, i = `${this.config.webSocketUrl}?sessionCode=${s}&agentId=${a}&apiKey=${n}`;
221
221
  return this.setStatus("connecting"), this.webSocket = new WebSocket(i), this.webSocket.addEventListener("open", () => {
222
222
  this.setStatus("connected");
223
223
  }), this.webSocket.addEventListener("message", (u) => {
224
- const p = JSON.parse(u.data);
225
- this.notifyPacket(p);
224
+ const d = JSON.parse(u.data);
225
+ this.notifyPacket(d);
226
226
  }), this.webSocket.addEventListener("close", (u) => {
227
- var p;
227
+ var d;
228
228
  this.setStatus("disconnected"), this.webSocket = null, u.code !== 1e3 && (this.notifyPacket({
229
229
  type: "message",
230
230
  payload: {
@@ -232,7 +232,7 @@ class W extends v {
232
232
  type: "text",
233
233
  text: "Oops! The connection to the server was lost. Please try again later."
234
234
  }
235
- }), (p = this.config.logger) == null || p.warn("WebSocket connection closed"));
235
+ }), (d = this.config.logger) == null || d.warn("WebSocket connection closed"));
236
236
  }), this.webSocket.addEventListener("error", () => {
237
237
  var u;
238
238
  this.setStatus("disconnected"), this.webSocket = null, (u = this.config.logger) == null || u.error("WebSocket connection error");
@@ -294,8 +294,8 @@ class ae {
294
294
  }), this.audioCtx || this._startAnalyzers();
295
295
  const a = new Audio();
296
296
  a.srcObject = this.remoteStream, a.play().catch((i) => {
297
- var l;
298
- (l = this.config.logger) == null || l.error("Error playing remote audio:", i);
297
+ var c;
298
+ (c = this.config.logger) == null || c.error("Error playing remote audio:", i);
299
299
  });
300
300
  }, this.pc.onicecandidate = (n) => {
301
301
  var a;
@@ -309,20 +309,20 @@ class ae {
309
309
  }, this.pc.ondatachannel = (n) => {
310
310
  const a = n.channel;
311
311
  a.onmessage = (i) => {
312
- this.messageCallbacks.forEach((l) => {
313
- l(i);
312
+ this.messageCallbacks.forEach((c) => {
313
+ c(i);
314
314
  });
315
315
  }, a.onopen = () => {
316
316
  var i;
317
317
  for (; this.queuedMessages.length > 0; ) {
318
- const l = this.queuedMessages.shift();
319
- l && (a.send(JSON.stringify(l)), (i = this.config.logger) == null || i.info("Sent queued message:", l));
318
+ const c = this.queuedMessages.shift();
319
+ c && (a.send(JSON.stringify(c)), (i = this.config.logger) == null || i.info("Sent queued message:", c));
320
320
  }
321
321
  };
322
322
  };
323
323
  const e = this.config.webrtcUrl || "wss://persona.applica.guru/api/webrtc";
324
324
  this.ws = new WebSocket(`${e}?apiKey=${encodeURIComponent(this.config.apiKey)}`), this.ws.onopen = async () => {
325
- var l, u;
325
+ var c, u;
326
326
  const n = await this.pc.createOffer();
327
327
  await this.pc.setLocalDescription(n);
328
328
  const a = {
@@ -330,7 +330,7 @@ class ae {
330
330
  agentId: this.config.agentId,
331
331
  sessionCode: t
332
332
  };
333
- (l = this.config.logger) == null || l.debug("Opening connection to WebRTC server: ", a);
333
+ (c = this.config.logger) == null || c.debug("Opening connection to WebRTC server: ", a);
334
334
  const i = {
335
335
  type: "OFFER",
336
336
  src: ((u = crypto.randomUUID) == null ? void 0 : u.call(crypto)) || "client_" + Date.now(),
@@ -352,8 +352,8 @@ class ae {
352
352
  else if (a.type === "CANDIDATE")
353
353
  try {
354
354
  await this.pc.addIceCandidate(new RTCIceCandidate(a.payload.candidate));
355
- } catch (l) {
356
- (i = this.config.logger) == null || i.error("Error adding ICE candidate:", l);
355
+ } catch (c) {
356
+ (i = this.config.logger) == null || i.error("Error adding ICE candidate:", c);
357
357
  }
358
358
  }, this.ws.onclose = (n) => {
359
359
  n.code !== 1e3 && this.errorCallbacks.forEach((a) => {
@@ -406,11 +406,11 @@ class ae {
406
406
  return;
407
407
  const n = new Uint8Array(this.localAnalyser.frequencyBinCount), a = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
408
408
  this.localAnalyser.getByteFrequencyData(n), this.remoteAnalyser.getByteFrequencyData(a);
409
- const i = n.reduce((u, p) => u + p, 0) / n.length, l = a.reduce((u, p) => u + p, 0) / a.length;
409
+ const i = n.reduce((u, d) => u + d, 0) / n.length, c = a.reduce((u, d) => u + d, 0) / a.length;
410
410
  this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((u) => {
411
411
  u({
412
412
  localAmplitude: i,
413
- remoteAmplitude: l
413
+ remoteAmplitude: c
414
414
  });
415
415
  }), this.analyzerFrame = requestAnimationFrame(s);
416
416
  };
@@ -501,6 +501,7 @@ class re {
501
501
  return this.transaction.functionCall;
502
502
  }
503
503
  async invoke(t) {
504
+ var i;
504
505
  const e = this.transaction.functionCall;
505
506
  if (!e) {
506
507
  await this.fail("No function call found");
@@ -512,10 +513,12 @@ class re {
512
513
  return;
513
514
  }
514
515
  try {
515
- const i = await a.apply(null, Object.values(n));
516
- await this.complete(i);
517
- } catch (i) {
518
- await this.fail(`Error executing tool ${s}: ${i}`);
516
+ const u = (((i = a.toString().replace(/\n/g, " ").match(/^[^(]*\(([^)]*)\)/)) == null ? void 0 : i[1].split(",").map((g) => g.trim()).filter(Boolean)) || []).map((g) => n[g]);
517
+ console.log(`Invoking tool ${s} with args:`, u);
518
+ const d = await a.apply(null, u);
519
+ await this.complete(d);
520
+ } catch (c) {
521
+ await this.fail(`Error executing tool ${s}: ${c}`);
519
522
  }
520
523
  }
521
524
  async complete(t) {
@@ -588,33 +591,33 @@ function ue({
588
591
  session: a = "new",
589
592
  ...i
590
593
  }) {
591
- const [l, u] = E(!1), [p, g] = E([]), [w, R] = E(a), [b, $] = E(/* @__PURE__ */ new Map()), O = G(!1), C = H(() => {
594
+ const [c, u] = E(!1), [d, g] = E([]), [w, R] = E(a), [b, $] = E(/* @__PURE__ */ new Map()), q = G(!1), S = H(() => {
592
595
  if (Array.isArray(e))
593
596
  return e;
594
597
  if (typeof e == "object" && e !== null) {
595
- const c = o ? "localhost:8000" : t || "persona.applica.guru/api", f = o ? "http" : "https", h = o ? "ws" : "wss";
596
- let m = Object.keys(e).map((y) => {
598
+ const l = o ? "localhost:8000" : t || "persona.applica.guru/api", m = o ? "http" : "https", h = o ? "ws" : "wss";
599
+ let f = Object.keys(e).map((y) => {
597
600
  switch (y) {
598
601
  case "rest":
599
- const d = e[y];
600
- return d === !0 ? new _({
601
- apiUrl: `${f}://${c}`,
602
+ const p = e[y];
603
+ return p === !0 ? new _({
604
+ apiUrl: `${m}://${l}`,
602
605
  apiKey: i.apiKey,
603
606
  agentId: i.agentId,
604
607
  logger: s
605
- }) : typeof d == "object" && d !== null ? new _(d) : null;
608
+ }) : typeof p == "object" && p !== null ? new _(p) : null;
606
609
  case "webrtc":
607
- const S = e[y];
608
- return S === !0 ? new z({
609
- webrtcUrl: `${h}://${c}/webrtc`,
610
+ const C = e[y];
611
+ return C === !0 ? new z({
612
+ webrtcUrl: `${h}://${l}/webrtc`,
610
613
  apiKey: i.apiKey,
611
614
  agentId: i.agentId,
612
615
  logger: s
613
- }) : typeof S == "object" && S !== null ? new z(S) : null;
616
+ }) : typeof C == "object" && C !== null ? new z(C) : null;
614
617
  case "websocket":
615
618
  const T = e[y];
616
619
  return T === !0 ? new W({
617
- webSocketUrl: `${h}://${c}/websocket`,
620
+ webSocketUrl: `${h}://${l}/websocket`,
618
621
  apiKey: i.apiKey,
619
622
  agentId: i.agentId,
620
623
  logger: s
@@ -623,32 +626,32 @@ function ue({
623
626
  throw new Error(`Unknown protocol: ${y}`);
624
627
  }
625
628
  }).filter((y) => y !== null);
626
- return i.tools && m.push(
629
+ return i.tools && f.push(
627
630
  new ce({
628
- apiUrl: `${f}://${c}`,
631
+ apiUrl: `${m}://${l}`,
629
632
  apiKey: i.apiKey,
630
633
  agentId: i.agentId,
631
634
  tools: i.tools,
632
635
  // Pass raw tools
633
636
  logger: s
634
637
  })
635
- ), m;
638
+ ), f;
636
639
  }
637
640
  throw new Error("Invalid protocols configuration");
638
641
  }, []);
639
642
  Q(() => {
640
- O.current || (O.current = !0, s == null || s.debug(
643
+ q.current || (q.current = !0, s == null || s.debug(
641
644
  "Initializing protocols: ",
642
- C.map((c) => c.getName())
643
- ), C.forEach((c) => {
644
- c.setSession(w), c.clearListeners(), c.addStatusChangeListener((f) => {
645
- s == null || s.debug(`${c.getName()} has notified new status: ${f}`), b.set(c.getName(), f), f === "connected" && (i.context && c.sendPacket({
645
+ S.map((l) => l.getName())
646
+ ), S.forEach((l) => {
647
+ l.setSession(w), l.clearListeners(), l.addStatusChangeListener((m) => {
648
+ s == null || s.debug(`${l.getName()} has notified new status: ${m}`), b.set(l.getName(), m), m === "connected" && (i.context && l.sendPacket({
646
649
  type: "command",
647
650
  payload: {
648
651
  command: "set_initial_context",
649
652
  arguments: i.context
650
653
  }
651
- }), i.tools && Array.isArray(i.tools) && c.sendPacket({
654
+ }), i.tools && Array.isArray(i.tools) && l.sendPacket({
652
655
  type: "command",
653
656
  payload: {
654
657
  command: "set_local_tools",
@@ -657,51 +660,51 @@ function ue({
657
660
  }
658
661
  }
659
662
  })), $(new Map(b));
660
- }), c.addPacketListener((f) => {
661
- if (f.type === "message") {
662
- const h = f.payload;
663
+ }), l.addPacketListener((m) => {
664
+ if (m.type === "message") {
665
+ const h = m.payload;
663
666
  g(
664
- (m) => q([...m, { ...h, protocol: c.getName() }])
667
+ (f) => j([...f, { ...h, protocol: l.getName() }])
665
668
  );
666
- } else if (f.type === "reasoning") {
667
- const h = f.payload;
668
- let m = h.thought;
669
- h.imageUrl && (m += `
669
+ } else if (m.type === "reasoning") {
670
+ const h = m.payload;
671
+ let f = h.thought;
672
+ h.imageUrl && (f += `
670
673
 
671
674
  ![image](https://persona.applica.guru/api/files/${h.imageUrl})`);
672
- const y = { type: "reasoning", text: m, role: "assistant", finishReason: "stop" };
675
+ const y = { type: "reasoning", text: f, role: "assistant", finishReason: "stop" };
673
676
  g(
674
- (d) => q([...d, { ...y, protocol: c.getName() }])
677
+ (p) => j([...p, { ...y, protocol: l.getName() }])
675
678
  );
676
- } else f.type === "transaction" && C.filter((h) => h !== c).forEach((h) => h.onTransaction(f.payload));
677
- }), c.autostart && c.status === "disconnected" && (s == null || s.debug(`Connecting to protocol: ${c.getName()}`), c.connect(w));
679
+ } else m.type === "transaction" && S.filter((h) => h !== l).forEach((h) => h.onTransaction(m.payload));
680
+ }), l.autostart && l.status === "disconnected" && (s == null || s.debug(`Connecting to protocol: ${l.getName()}`), l.connect(w));
678
681
  }));
679
- }, [w, C, s, b]);
680
- const U = async (c) => {
682
+ }, [w, S, s, b]);
683
+ const U = async (l) => {
681
684
  var y;
682
- if (((y = c.content[0]) == null ? void 0 : y.type) !== "text") throw new Error("Only text messages are supported");
683
- const f = c.content[0].text;
684
- g((d) => [...d, { role: "user", type: "text", text: f }]), u(!0);
685
- const h = C.sort((d, S) => S.getPriority() - d.getPriority()).find((d) => d.status === "connected"), m = [];
686
- if (c.attachments)
687
- for (const d of c.attachments)
688
- d.contentType.startsWith("image/") && d.file && m.push({
685
+ if (((y = l.content[0]) == null ? void 0 : y.type) !== "text") throw new Error("Only text messages are supported");
686
+ const m = l.content[0].text;
687
+ g((p) => [...p, { role: "user", type: "text", text: m }]), u(!0);
688
+ const h = S.sort((p, C) => C.getPriority() - p.getPriority()).find((p) => p.status === "connected"), f = [];
689
+ if (l.attachments)
690
+ for (const p of l.attachments)
691
+ p.contentType.startsWith("image/") && p.file && f.push({
689
692
  role: "user",
690
693
  image: {
691
- contentType: d.contentType,
692
- content: await le(d.file)
694
+ contentType: p.contentType,
695
+ content: await le(p.file)
693
696
  },
694
697
  text: "",
695
698
  type: "text"
696
699
  });
697
- c.content && m.push({
700
+ l.content && f.push({
698
701
  role: "user",
699
- text: c.content[0].text,
702
+ text: l.content[0].text,
700
703
  type: "text"
701
- }), s == null || s.debug("Sending message:", m), await (h == null ? void 0 : h.sendPacket({ type: "request", payload: m })), u(!1);
702
- }, D = M(() => (u(!1), g([]), R("new"), Promise.resolve()), []), L = M(() => Promise.resolve(), []), J = M(() => p, [p]), K = X({
703
- isRunning: l,
704
- messages: p,
704
+ }), s == null || s.debug("Sending message:", f), await (h == null ? void 0 : h.sendPacket({ type: "request", payload: f })), u(!1);
705
+ }, D = N(() => (u(!1), g([]), R("new"), Promise.resolve()), []), L = N(() => Promise.resolve(), []), J = N(() => d, [d]), K = X({
706
+ isRunning: c,
707
+ messages: d,
705
708
  convertMessage: oe,
706
709
  onNew: U,
707
710
  onCancel: D,
@@ -710,10 +713,10 @@ function ue({
710
713
  attachments: new Z([new ee()])
711
714
  }
712
715
  });
713
- return /* @__PURE__ */ N.jsx(x.Provider, { value: { protocols: C, protocolsStatus: b, getMessages: J }, children: /* @__PURE__ */ N.jsx(te, { runtime: K, children: n }) });
716
+ return /* @__PURE__ */ M.jsx(x.Provider, { value: { protocols: S, protocolsStatus: b, getMessages: J }, children: /* @__PURE__ */ M.jsx(te, { runtime: K, children: n }) });
714
717
  }
715
718
  function ge({ children: o, ...t }) {
716
- return /* @__PURE__ */ N.jsx(ue, { ...t, children: o });
719
+ return /* @__PURE__ */ M.jsx(ue, { ...t, children: o });
717
720
  }
718
721
  function we() {
719
722
  const o = A(x);
@@ -751,10 +754,10 @@ function be() {
751
754
  return t.config.apiUrl;
752
755
  throw new Error("REST protocol not found");
753
756
  }
754
- function Ce() {
757
+ function Se() {
755
758
  return he("webrtc");
756
759
  }
757
- function Se() {
760
+ function Ce() {
758
761
  const o = A(x);
759
762
  if (!o)
760
763
  throw new Error("usePersonaRuntimeMessages must be used within a PersonaRuntimeProvider");
@@ -900,9 +903,9 @@ export {
900
903
  ke as sumTool,
901
904
  we as usePersonaRuntime,
902
905
  be as usePersonaRuntimeEndpoint,
903
- Se as usePersonaRuntimeMessages,
906
+ Ce as usePersonaRuntimeMessages,
904
907
  he as usePersonaRuntimeProtocol,
905
- Ce as usePersonaRuntimeWebRTCProtocol,
908
+ Se as usePersonaRuntimeWebRTCProtocol,
906
909
  Te as validateToolParameters
907
910
  };
908
911
  //# sourceMappingURL=bundle.es.js.map