@applica-software-guru/persona-sdk 0.1.45 → 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 +200 -183
- 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/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 -1
- package/playground/src/components/assistant-ui/thread.tsx +58 -3
- package/src/messages.ts +17 -3
- 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(
|
|
20
|
+
var n = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
|
|
21
|
+
function e(o, s, i) {
|
|
22
22
|
var c = null;
|
|
23
|
-
if (
|
|
24
|
-
|
|
25
|
-
for (var r in
|
|
26
|
-
r !== "key" && (
|
|
27
|
-
} else
|
|
28
|
-
return
|
|
29
|
-
$$typeof:
|
|
30
|
-
type:
|
|
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
31
|
key: c,
|
|
32
|
-
ref:
|
|
33
|
-
props:
|
|
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:
|
|
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`, {
|
|
140
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
|
|
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,16 +172,16 @@ class $ 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
179
|
var r;
|
|
173
180
|
if (this.webSocket !== null && this.status === "connected")
|
|
174
181
|
return Promise.resolve(this.session);
|
|
175
|
-
const
|
|
176
|
-
(r = this.config.logger) == null || r.debug("Connecting to WebSocket with sessionId:",
|
|
177
|
-
const
|
|
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}`;
|
|
178
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) => {
|
|
@@ -189,7 +196,7 @@ class $ 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,29 +208,29 @@ class $ 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
|
|
227
|
+
var o;
|
|
221
228
|
if (this.isConnected) return;
|
|
222
229
|
this.isConnected = !0;
|
|
223
230
|
try {
|
|
224
231
|
this.localStream = await navigator.mediaDevices.getUserMedia({ audio: !0 });
|
|
225
|
-
} catch (
|
|
226
|
-
(
|
|
232
|
+
} catch (s) {
|
|
233
|
+
(o = this.config.logger) == null || o.error("Error accessing microphone:", s);
|
|
227
234
|
return;
|
|
228
235
|
}
|
|
229
236
|
this.pc = new RTCPeerConnection({
|
|
@@ -237,29 +244,29 @@ class Z {
|
|
|
237
244
|
credential: "webrtc"
|
|
238
245
|
}
|
|
239
246
|
]
|
|
240
|
-
}), this.localStream.getTracks().forEach((
|
|
241
|
-
this.pc.addTrack(
|
|
242
|
-
}), this.pc.ontrack = (
|
|
243
|
-
|
|
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) => {
|
|
244
251
|
this.remoteStream.addTrack(c);
|
|
245
252
|
}), this.audioCtx || this._startAnalyzers();
|
|
246
|
-
const
|
|
247
|
-
|
|
253
|
+
const i = new Audio();
|
|
254
|
+
i.srcObject = this.remoteStream, i.play().catch((c) => {
|
|
248
255
|
var r;
|
|
249
256
|
(r = this.config.logger) == null || r.error("Error playing remote audio:", c);
|
|
250
257
|
});
|
|
251
|
-
}, this.pc.onicecandidate = (
|
|
252
|
-
var
|
|
253
|
-
|
|
258
|
+
}, this.pc.onicecandidate = (s) => {
|
|
259
|
+
var i;
|
|
260
|
+
s.candidate && ((i = this.ws) == null ? void 0 : i.readyState) === WebSocket.OPEN && this.ws.send(
|
|
254
261
|
JSON.stringify({
|
|
255
262
|
type: "CANDIDATE",
|
|
256
263
|
src: "client",
|
|
257
|
-
payload: { candidate:
|
|
264
|
+
payload: { candidate: s.candidate }
|
|
258
265
|
})
|
|
259
266
|
);
|
|
260
|
-
}, this.pc.ondatachannel = (
|
|
261
|
-
const
|
|
262
|
-
|
|
267
|
+
}, this.pc.ondatachannel = (s) => {
|
|
268
|
+
const i = s.channel;
|
|
269
|
+
i.onmessage = (c) => {
|
|
263
270
|
this.messageCallbacks.forEach((r) => {
|
|
264
271
|
r(c);
|
|
265
272
|
});
|
|
@@ -268,35 +275,35 @@ class Z {
|
|
|
268
275
|
const e = this.config.webrtcUrl || "wss://persona.applica.guru/api/webrtc";
|
|
269
276
|
this.ws = new WebSocket(`${e}?apiKey=${encodeURIComponent(this.config.apiKey)}`), this.ws.onopen = async () => {
|
|
270
277
|
var r, u;
|
|
271
|
-
const
|
|
272
|
-
await this.pc.setLocalDescription(
|
|
273
|
-
const
|
|
278
|
+
const s = await this.pc.createOffer();
|
|
279
|
+
await this.pc.setLocalDescription(s);
|
|
280
|
+
const i = {
|
|
274
281
|
apiKey: this.config.apiKey,
|
|
275
282
|
agentId: this.config.agentId,
|
|
276
283
|
sessionCode: t
|
|
277
284
|
};
|
|
278
|
-
(r = this.config.logger) == null || r.debug("Opening connection to WebRTC server: ",
|
|
285
|
+
(r = this.config.logger) == null || r.debug("Opening connection to WebRTC server: ", i);
|
|
279
286
|
const c = {
|
|
280
287
|
type: "OFFER",
|
|
281
288
|
src: ((u = crypto.randomUUID) == null ? void 0 : u.call(crypto)) || "client_" + Date.now(),
|
|
282
289
|
payload: {
|
|
283
290
|
sdp: {
|
|
284
|
-
sdp:
|
|
285
|
-
type:
|
|
291
|
+
sdp: s.sdp,
|
|
292
|
+
type: s.type
|
|
286
293
|
},
|
|
287
294
|
connectionId: (Date.now() % 1e6).toString(),
|
|
288
|
-
metadata:
|
|
295
|
+
metadata: i
|
|
289
296
|
}
|
|
290
297
|
};
|
|
291
298
|
this.ws.send(JSON.stringify(c));
|
|
292
|
-
}, this.ws.onmessage = async (
|
|
299
|
+
}, this.ws.onmessage = async (s) => {
|
|
293
300
|
var c;
|
|
294
|
-
const
|
|
295
|
-
if (
|
|
296
|
-
await this.pc.setRemoteDescription(new RTCSessionDescription(
|
|
297
|
-
else if (
|
|
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")
|
|
298
305
|
try {
|
|
299
|
-
await this.pc.addIceCandidate(new RTCIceCandidate(
|
|
306
|
+
await this.pc.addIceCandidate(new RTCIceCandidate(i.payload.candidate));
|
|
300
307
|
} catch (r) {
|
|
301
308
|
(c = this.config.logger) == null || c.error("Error adding ICE candidate:", r);
|
|
302
309
|
}
|
|
@@ -319,18 +326,18 @@ class Z {
|
|
|
319
326
|
var e;
|
|
320
327
|
return (e = this.config.logger) == null ? void 0 : e.info("Data channel opened");
|
|
321
328
|
}, this.dataChannel.onmessage = (e) => {
|
|
322
|
-
this.messageCallbacks.forEach((
|
|
323
|
-
|
|
329
|
+
this.messageCallbacks.forEach((o) => {
|
|
330
|
+
o(e);
|
|
324
331
|
});
|
|
325
332
|
});
|
|
326
333
|
}
|
|
327
334
|
sendMessage(t) {
|
|
328
|
-
var e,
|
|
335
|
+
var e, o;
|
|
329
336
|
if (!this.dataChannel) {
|
|
330
337
|
(e = this.config.logger) == null || e.warn("Data channel is not open, cannot send message");
|
|
331
338
|
return;
|
|
332
339
|
}
|
|
333
|
-
this.dataChannel.send(t), (
|
|
340
|
+
this.dataChannel.send(t), (o = this.config.logger) == null || o.info("Sent message:", t);
|
|
334
341
|
}
|
|
335
342
|
_startAnalyzers() {
|
|
336
343
|
if (!this.localStream || !this.remoteStream || this.visualizerCallbacks.length === 0)
|
|
@@ -338,38 +345,38 @@ class Z {
|
|
|
338
345
|
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
339
346
|
const t = this.audioCtx.createMediaStreamSource(this.localStream), e = this.audioCtx.createMediaStreamSource(this.remoteStream);
|
|
340
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);
|
|
341
|
-
const
|
|
348
|
+
const o = () => {
|
|
342
349
|
if (!this.localAnalyser || !this.remoteAnalyser || this.visualizerCallbacks.length === 0)
|
|
343
350
|
return;
|
|
344
|
-
const
|
|
345
|
-
this.localAnalyser.getByteFrequencyData(
|
|
346
|
-
const c =
|
|
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;
|
|
347
354
|
this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((u) => {
|
|
348
355
|
u({
|
|
349
356
|
localAmplitude: c,
|
|
350
357
|
remoteAmplitude: r
|
|
351
358
|
});
|
|
352
|
-
}), this.analyzerFrame = requestAnimationFrame(
|
|
359
|
+
}), this.analyzerFrame = requestAnimationFrame(o);
|
|
353
360
|
};
|
|
354
|
-
this.analyzerFrame = requestAnimationFrame(
|
|
361
|
+
this.analyzerFrame = requestAnimationFrame(o);
|
|
355
362
|
}
|
|
356
363
|
_stopAnalyzers() {
|
|
357
364
|
this.analyzerFrame && (cancelAnimationFrame(this.analyzerFrame), this.analyzerFrame = null), this.localAnalyser = null, this.remoteAnalyser = null;
|
|
358
365
|
}
|
|
359
366
|
}
|
|
360
|
-
class
|
|
367
|
+
class N extends R {
|
|
361
368
|
constructor(e) {
|
|
362
369
|
super();
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
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((
|
|
369
|
-
var
|
|
370
|
-
(
|
|
371
|
-
const
|
|
372
|
-
|
|
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);
|
|
373
380
|
});
|
|
374
381
|
}
|
|
375
382
|
getName() {
|
|
@@ -382,59 +389,59 @@ class I extends x {
|
|
|
382
389
|
super.syncSession(e), this.status === "connected" && (await this.disconnect(), await this.connect(e));
|
|
383
390
|
}
|
|
384
391
|
async connect(e) {
|
|
385
|
-
var
|
|
386
|
-
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);
|
|
387
394
|
}
|
|
388
395
|
async disconnect() {
|
|
389
|
-
var e,
|
|
396
|
+
var e, o, s;
|
|
390
397
|
if (this.status === "disconnected")
|
|
391
398
|
return (e = this.config.logger) == null || e.warn("Already disconnected"), Promise.resolve();
|
|
392
|
-
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");
|
|
393
400
|
}
|
|
394
401
|
send(e) {
|
|
395
402
|
return this.status !== "connected" ? Promise.reject(new Error("Not connected")) : (this.webRTCClient.sendMessage(e), Promise.resolve());
|
|
396
403
|
}
|
|
397
404
|
}
|
|
398
|
-
const
|
|
405
|
+
const S = q(void 0);
|
|
399
406
|
function ee({
|
|
400
|
-
dev:
|
|
407
|
+
dev: n = !1,
|
|
401
408
|
protocols: t,
|
|
402
409
|
logger: e,
|
|
403
|
-
children:
|
|
404
|
-
session:
|
|
405
|
-
...
|
|
410
|
+
children: o,
|
|
411
|
+
session: s = "new",
|
|
412
|
+
...i
|
|
406
413
|
}) {
|
|
407
|
-
const [c, r] =
|
|
414
|
+
const [c, r] = C(!1), [u, h] = C([]), [f, D] = C(s), [w, W] = C(/* @__PURE__ */ new Map()), E = K(!1), y = _(() => {
|
|
408
415
|
if (Array.isArray(t))
|
|
409
416
|
return t;
|
|
410
417
|
if (typeof t == "object" && t !== null) {
|
|
411
|
-
const l =
|
|
418
|
+
const l = n ? "localhost:8000" : "persona.applica.guru/api", p = n ? "http" : "https", g = n ? "ws" : "wss";
|
|
412
419
|
return Object.keys(t).map((d) => {
|
|
413
420
|
switch (d) {
|
|
414
421
|
case "rest":
|
|
415
|
-
const
|
|
416
|
-
return
|
|
422
|
+
const b = t[d];
|
|
423
|
+
return b === !0 ? new $({
|
|
417
424
|
apiUrl: `${p}://${l}`,
|
|
418
|
-
apiKey:
|
|
419
|
-
agentId:
|
|
425
|
+
apiKey: i.apiKey,
|
|
426
|
+
agentId: i.agentId,
|
|
420
427
|
logger: e
|
|
421
|
-
}) : new
|
|
428
|
+
}) : new $(b);
|
|
422
429
|
case "webrtc":
|
|
423
430
|
const A = t[d];
|
|
424
|
-
return A === !0 ? new
|
|
431
|
+
return A === !0 ? new N({
|
|
425
432
|
webrtcUrl: `${g}://${l}/webrtc`,
|
|
426
|
-
apiKey:
|
|
427
|
-
agentId:
|
|
433
|
+
apiKey: i.apiKey,
|
|
434
|
+
agentId: i.agentId,
|
|
428
435
|
logger: e
|
|
429
|
-
}) : new
|
|
436
|
+
}) : new N(A);
|
|
430
437
|
case "websocket":
|
|
431
|
-
const
|
|
432
|
-
return
|
|
438
|
+
const v = t[d];
|
|
439
|
+
return v === !0 ? new I({
|
|
433
440
|
webSocketUrl: `${g}://${l}/websocket`,
|
|
434
|
-
apiKey:
|
|
435
|
-
agentId:
|
|
441
|
+
apiKey: i.apiKey,
|
|
442
|
+
agentId: i.agentId,
|
|
436
443
|
logger: e
|
|
437
|
-
}) : new
|
|
444
|
+
}) : new I(v);
|
|
438
445
|
default:
|
|
439
446
|
throw new Error(`Unknown protocol: ${d}`);
|
|
440
447
|
}
|
|
@@ -443,51 +450,51 @@ function ee({
|
|
|
443
450
|
throw new Error("Invalid protocols configuration");
|
|
444
451
|
}, []);
|
|
445
452
|
B(() => {
|
|
446
|
-
|
|
453
|
+
E.current || (E.current = !0, e == null || e.debug(
|
|
447
454
|
"Initializing protocols: ",
|
|
448
455
|
y.map((l) => l.getName())
|
|
449
456
|
), y.forEach((l) => {
|
|
450
457
|
l.setSession(f), l.clearListeners(), l.addStatusChangeListener((p) => {
|
|
451
|
-
e == null || e.debug(`${l.getName()} has notified new status: ${p}`),
|
|
458
|
+
e == null || e.debug(`${l.getName()} has notified new status: ${p}`), w.set(l.getName(), p), W(new Map(w));
|
|
452
459
|
}), l.addMessageListener((p) => {
|
|
453
460
|
h((g) => Q([...g, { ...p, protocol: l.getName() }]));
|
|
454
461
|
}), l.autostart && l.status === "disconnected" && (e == null || e.debug(`Connecting to protocol: ${l.getName()}`), l.connect(f));
|
|
455
462
|
}));
|
|
456
|
-
}, [f, y, e,
|
|
463
|
+
}, [f, y, e, w]);
|
|
457
464
|
const L = async (l) => {
|
|
458
|
-
var
|
|
459
|
-
if (((
|
|
465
|
+
var k;
|
|
466
|
+
if (((k = l.content[0]) == null ? void 0 : k.type) !== "text") throw new Error("Only text messages are supported");
|
|
460
467
|
const p = l.content[0].text;
|
|
461
468
|
h((d) => [...d, { role: "user", type: "text", text: p }]), r(!0);
|
|
462
|
-
const g = y.sort((d,
|
|
469
|
+
const g = y.sort((d, b) => b.getPriority() - d.getPriority()).find((d) => d.status === "connected");
|
|
463
470
|
await (g == null ? void 0 : g.send(p)), r(!1);
|
|
464
|
-
}, O =
|
|
471
|
+
}, O = T(() => (r(!1), h([]), D("new"), Promise.resolve()), []), U = T(() => Promise.resolve(), []), j = Y({
|
|
465
472
|
isRunning: c,
|
|
466
473
|
messages: u,
|
|
467
474
|
convertMessage: X,
|
|
468
475
|
onNew: L,
|
|
469
476
|
onCancel: O,
|
|
470
|
-
onReload:
|
|
477
|
+
onReload: U
|
|
471
478
|
});
|
|
472
|
-
return /* @__PURE__ */
|
|
479
|
+
return /* @__PURE__ */ P.jsx(S.Provider, { value: { protocols: y, protocolsStatus: w }, children: /* @__PURE__ */ P.jsx(G, { runtime: j, children: o }) });
|
|
473
480
|
}
|
|
474
|
-
function
|
|
475
|
-
return /* @__PURE__ */
|
|
481
|
+
function oe({ children: n, ...t }) {
|
|
482
|
+
return /* @__PURE__ */ P.jsx(ee, { ...t, children: n });
|
|
476
483
|
}
|
|
477
|
-
function
|
|
478
|
-
const
|
|
479
|
-
if (!
|
|
484
|
+
function ae() {
|
|
485
|
+
const n = x(S);
|
|
486
|
+
if (!n)
|
|
480
487
|
throw new Error("usePersonaRuntime must be used within a PersonaRuntimeProvider");
|
|
481
|
-
return
|
|
488
|
+
return n;
|
|
482
489
|
}
|
|
483
|
-
function te(
|
|
484
|
-
const t =
|
|
490
|
+
function te(n) {
|
|
491
|
+
const t = x(S);
|
|
485
492
|
if (!t)
|
|
486
493
|
throw new Error("usePersonaRuntimeProtocol must be used within a PersonaRuntimeProvider");
|
|
487
|
-
const e = t.protocols.find((
|
|
494
|
+
const e = t.protocols.find((s) => s.getName() === n);
|
|
488
495
|
if (!e)
|
|
489
496
|
return null;
|
|
490
|
-
const
|
|
497
|
+
const o = t.protocolsStatus.get(e.getName());
|
|
491
498
|
return {
|
|
492
499
|
...e,
|
|
493
500
|
connect: e.connect.bind(e),
|
|
@@ -498,15 +505,24 @@ function te(i) {
|
|
|
498
505
|
addMessageListener: e.addMessageListener.bind(e),
|
|
499
506
|
getName: e.getName.bind(e),
|
|
500
507
|
getPriority: e.getPriority.bind(e),
|
|
501
|
-
status:
|
|
508
|
+
status: o || e.status
|
|
502
509
|
};
|
|
503
510
|
}
|
|
504
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() {
|
|
505
521
|
return te("webrtc");
|
|
506
522
|
}
|
|
507
|
-
class
|
|
523
|
+
class le {
|
|
508
524
|
constructor() {
|
|
509
|
-
|
|
525
|
+
a(this, "prefix", "[Persona]");
|
|
510
526
|
}
|
|
511
527
|
log(t, ...e) {
|
|
512
528
|
console.log(`${this.prefix} - ${t}`, ...e);
|
|
@@ -525,14 +541,15 @@ class ce {
|
|
|
525
541
|
}
|
|
526
542
|
}
|
|
527
543
|
export {
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
544
|
+
le as PersonaConsoleLogger,
|
|
545
|
+
R as PersonaProtocolBase,
|
|
546
|
+
$ as PersonaRESTProtocol,
|
|
547
|
+
oe as PersonaRuntimeProvider,
|
|
548
|
+
N as PersonaWebRTCProtocol,
|
|
549
|
+
I as PersonaWebSocketProtocol,
|
|
550
|
+
ae as usePersonaRuntime,
|
|
551
|
+
re as usePersonaRuntimeEndpoint,
|
|
535
552
|
te as usePersonaRuntimeProtocol,
|
|
536
|
-
|
|
553
|
+
ce as usePersonaRuntimeWebRTCProtocol
|
|
537
554
|
};
|
|
538
555
|
//# sourceMappingURL=bundle.es.js.map
|