@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.cjs.js +2 -2
- package/dist/bundle.cjs.js.map +1 -1
- package/dist/bundle.es.js +274 -255
- package/dist/bundle.es.js.map +1 -1
- package/dist/bundle.iife.js +2 -2
- package/dist/bundle.iife.js.map +1 -1
- package/dist/bundle.umd.js +2 -2
- package/dist/bundle.umd.js.map +1 -1
- package/dist/messages.d.ts.map +1 -1
- package/dist/protocol/webrtc.d.ts.map +1 -1
- package/dist/runtime.d.ts +2 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -2
- package/playground/src/chat.tsx +1 -1
- package/playground/src/components/assistant-ui/thread.tsx +58 -3
- package/src/messages.ts +17 -3
- package/src/protocol/webrtc.ts +3 -1
- package/src/runtime.tsx +14 -1
- package/src/types.ts +7 -0
package/dist/bundle.es.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
var
|
|
2
|
-
var J = (
|
|
3
|
-
var
|
|
4
|
-
import { createContext as q, useContext as
|
|
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: {} },
|
|
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
|
|
18
|
+
if (M) return m;
|
|
19
19
|
M = 1;
|
|
20
|
-
var
|
|
21
|
-
function e(
|
|
22
|
-
var
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
for (var
|
|
26
|
-
|
|
27
|
-
} else
|
|
28
|
-
return
|
|
29
|
-
$$typeof:
|
|
30
|
-
type:
|
|
31
|
-
key:
|
|
32
|
-
ref:
|
|
33
|
-
props:
|
|
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
|
|
36
|
+
return m.Fragment = t, m.jsx = e, m.jsxs = e, m;
|
|
37
37
|
}
|
|
38
38
|
z.exports = V();
|
|
39
|
-
var
|
|
40
|
-
function H(
|
|
41
|
-
return
|
|
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(
|
|
46
|
+
function Q(n) {
|
|
47
47
|
const t = [];
|
|
48
48
|
let e = null;
|
|
49
|
-
for (const s of
|
|
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(
|
|
59
|
-
var
|
|
60
|
-
|
|
61
|
-
|
|
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: (
|
|
64
|
-
content: ((
|
|
65
|
-
var
|
|
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:
|
|
69
|
-
toolCallId:
|
|
70
|
-
args:
|
|
71
|
-
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:
|
|
76
|
-
role:
|
|
77
|
-
content:
|
|
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
|
|
87
|
+
class R {
|
|
81
88
|
constructor() {
|
|
82
|
-
|
|
83
|
-
|
|
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((
|
|
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((
|
|
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
|
|
120
|
+
class $ extends R {
|
|
114
121
|
constructor(e) {
|
|
115
122
|
super();
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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:
|
|
140
|
-
body: JSON.stringify({ userMessage:
|
|
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":
|
|
145
|
-
"x-persona-apikey":
|
|
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
|
|
158
|
+
class I extends R {
|
|
152
159
|
constructor(e) {
|
|
153
160
|
super();
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
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
|
|
169
|
-
(
|
|
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
|
|
179
|
+
var r;
|
|
173
180
|
if (this.webSocket !== null && this.status === "connected")
|
|
174
181
|
return Promise.resolve(this.session);
|
|
175
|
-
const
|
|
176
|
-
(
|
|
177
|
-
const
|
|
178
|
-
return this.setStatus("connecting"), this.webSocket = new WebSocket(
|
|
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(
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
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
|
|
221
|
-
if (
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
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((
|
|
321
|
-
|
|
329
|
+
this.messageCallbacks.forEach((o) => {
|
|
330
|
+
o(e);
|
|
322
331
|
});
|
|
323
332
|
});
|
|
324
333
|
}
|
|
325
334
|
sendMessage(t) {
|
|
326
|
-
var e,
|
|
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), (
|
|
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
|
|
348
|
+
const o = () => {
|
|
340
349
|
if (!this.localAnalyser || !this.remoteAnalyser || this.visualizerCallbacks.length === 0)
|
|
341
350
|
return;
|
|
342
|
-
const
|
|
343
|
-
this.localAnalyser.getByteFrequencyData(
|
|
344
|
-
const
|
|
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:
|
|
348
|
-
remoteAmplitude:
|
|
356
|
+
localAmplitude: c,
|
|
357
|
+
remoteAmplitude: r
|
|
349
358
|
});
|
|
350
|
-
}), this.analyzerFrame = requestAnimationFrame(
|
|
359
|
+
}), this.analyzerFrame = requestAnimationFrame(o);
|
|
351
360
|
};
|
|
352
|
-
this.analyzerFrame = requestAnimationFrame(
|
|
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
|
|
367
|
+
class N extends R {
|
|
359
368
|
constructor(e) {
|
|
360
369
|
super();
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
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((
|
|
367
|
-
var
|
|
368
|
-
(
|
|
369
|
-
const
|
|
370
|
-
|
|
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
|
|
384
|
-
return this.status === "connected" ? Promise.resolve(this.session) : (this.session = e || this.session || "new", this.setStatus("connecting"), (
|
|
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,
|
|
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"), (
|
|
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
|
|
405
|
+
const S = q(void 0);
|
|
397
406
|
function ee({
|
|
398
|
-
dev:
|
|
407
|
+
dev: n = !1,
|
|
399
408
|
protocols: t,
|
|
400
409
|
logger: e,
|
|
401
|
-
children:
|
|
402
|
-
session:
|
|
403
|
-
...
|
|
410
|
+
children: o,
|
|
411
|
+
session: s = "new",
|
|
412
|
+
...i
|
|
404
413
|
}) {
|
|
405
|
-
const [
|
|
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
|
|
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
|
|
415
|
-
apiUrl: `${p}://${
|
|
416
|
-
apiKey:
|
|
417
|
-
agentId:
|
|
423
|
+
return b === !0 ? new $({
|
|
424
|
+
apiUrl: `${p}://${l}`,
|
|
425
|
+
apiKey: i.apiKey,
|
|
426
|
+
agentId: i.agentId,
|
|
418
427
|
logger: e
|
|
419
|
-
}) : new
|
|
428
|
+
}) : new $(b);
|
|
420
429
|
case "webrtc":
|
|
421
430
|
const A = t[d];
|
|
422
|
-
return A === !0 ? new
|
|
423
|
-
webrtcUrl: `${g}://${
|
|
424
|
-
apiKey:
|
|
425
|
-
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
|
|
436
|
+
}) : new N(A);
|
|
428
437
|
case "websocket":
|
|
429
|
-
const
|
|
430
|
-
return
|
|
431
|
-
webSocketUrl: `${g}://${
|
|
432
|
-
apiKey:
|
|
433
|
-
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(
|
|
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
|
-
|
|
444
|
-
|
|
452
|
+
B(() => {
|
|
453
|
+
E.current || (E.current = !0, e == null || e.debug(
|
|
445
454
|
"Initializing protocols: ",
|
|
446
|
-
y.map((
|
|
447
|
-
), y.forEach((
|
|
448
|
-
|
|
449
|
-
e == null || e.debug(`${
|
|
450
|
-
}),
|
|
451
|
-
h((g) => Q([...g, { ...p, protocol:
|
|
452
|
-
}),
|
|
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,
|
|
455
|
-
const L = async (
|
|
456
|
-
var
|
|
457
|
-
if (((
|
|
458
|
-
const p =
|
|
459
|
-
h((d) => [...d, { role: "user", type: "text", text: p }]),
|
|
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)),
|
|
462
|
-
}, O =
|
|
463
|
-
isRunning:
|
|
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:
|
|
477
|
+
onReload: U
|
|
469
478
|
});
|
|
470
|
-
return /* @__PURE__ */
|
|
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
|
|
473
|
-
return /* @__PURE__ */
|
|
481
|
+
function oe({ children: n, ...t }) {
|
|
482
|
+
return /* @__PURE__ */ P.jsx(ee, { ...t, children: n });
|
|
474
483
|
}
|
|
475
|
-
function
|
|
476
|
-
const
|
|
477
|
-
if (!
|
|
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
|
|
488
|
+
return n;
|
|
480
489
|
}
|
|
481
|
-
function te(
|
|
482
|
-
const t =
|
|
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((
|
|
494
|
+
const e = t.protocols.find((s) => s.getName() === n);
|
|
486
495
|
if (!e)
|
|
487
496
|
return null;
|
|
488
|
-
const
|
|
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:
|
|
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
|
|
523
|
+
class le {
|
|
506
524
|
constructor() {
|
|
507
|
-
|
|
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
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
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
|
-
|
|
550
|
+
ae as usePersonaRuntime,
|
|
551
|
+
re as usePersonaRuntimeEndpoint,
|
|
533
552
|
te as usePersonaRuntimeProtocol,
|
|
534
|
-
|
|
553
|
+
ce as usePersonaRuntimeWebRTCProtocol
|
|
535
554
|
};
|
|
536
555
|
//# sourceMappingURL=bundle.es.js.map
|