@applica-software-guru/persona-sdk 0.1.63 → 0.1.64
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/README.md +45 -29
- package/dist/bundle.cjs.js +3 -3
- package/dist/bundle.cjs.js.map +1 -1
- package/dist/bundle.es.js +509 -371
- package/dist/bundle.es.js.map +1 -1
- package/dist/bundle.iife.js +3 -3
- package/dist/bundle.iife.js.map +1 -1
- package/dist/bundle.umd.js +3 -3
- package/dist/bundle.umd.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/protocol/rest.d.ts +2 -0
- package/dist/protocol/rest.d.ts.map +1 -1
- package/dist/protocol/transaction.d.ts +7 -3
- package/dist/protocol/transaction.d.ts.map +1 -1
- package/dist/protocol/webrtc.d.ts +1 -0
- package/dist/protocol/webrtc.d.ts.map +1 -1
- package/dist/runtime.d.ts.map +1 -1
- package/dist/tools.d.ts +72 -0
- package/dist/tools.d.ts.map +1 -0
- package/dist/types.d.ts +6 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/playground/src/chat.tsx +12 -16
- package/playground/src/tools.ts +127 -0
- package/src/index.ts +1 -0
- package/src/protocol/rest.ts +8 -1
- package/src/protocol/transaction.ts +27 -17
- package/src/protocol/webrtc.ts +22 -3
- package/src/runtime.tsx +22 -12
- package/src/tools.ts +211 -0
- package/src/types.ts +6 -2
package/dist/bundle.es.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
import { createContext as
|
|
5
|
-
import { useExternalStoreRuntime as
|
|
6
|
-
var
|
|
1
|
+
var B = Object.defineProperty;
|
|
2
|
+
var V = (o, t, e) => t in o ? B(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
|
|
3
|
+
var r = (o, t, e) => V(o, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
import { createContext as Y, useContext as $, useState as v, useRef as G, useMemo as H, useEffect as Q, useCallback as N } from "react";
|
|
5
|
+
import { useExternalStoreRuntime as X, CompositeAttachmentAdapter as Z, SimpleImageAttachmentAdapter as ee, AssistantRuntimeProvider as te } from "@assistant-ui/react";
|
|
6
|
+
var U = { exports: {} }, k = {};
|
|
7
7
|
/**
|
|
8
8
|
* @license React
|
|
9
9
|
* react-jsx-runtime.production.js
|
|
@@ -13,122 +13,123 @@ var W = { exports: {} }, S = {};
|
|
|
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
|
|
17
|
-
function
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
var
|
|
21
|
-
function
|
|
22
|
-
var
|
|
23
|
-
if (a !== void 0 && (
|
|
16
|
+
var q;
|
|
17
|
+
function se() {
|
|
18
|
+
if (q) return k;
|
|
19
|
+
q = 1;
|
|
20
|
+
var o = Symbol.for("react.transitional.element"), t = Symbol.for("react.fragment");
|
|
21
|
+
function e(s, n, a) {
|
|
22
|
+
var i = null;
|
|
23
|
+
if (a !== void 0 && (i = "" + a), n.key !== void 0 && (i = "" + n.key), "key" in n) {
|
|
24
24
|
a = {};
|
|
25
25
|
for (var l in n)
|
|
26
26
|
l !== "key" && (a[l] = n[l]);
|
|
27
27
|
} else a = n;
|
|
28
28
|
return n = a.ref, {
|
|
29
|
-
$$typeof:
|
|
29
|
+
$$typeof: o,
|
|
30
30
|
type: s,
|
|
31
|
-
key:
|
|
31
|
+
key: i,
|
|
32
32
|
ref: n !== void 0 ? n : null,
|
|
33
33
|
props: a
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
-
return
|
|
36
|
+
return k.Fragment = t, k.jsx = e, k.jsxs = e, k;
|
|
37
37
|
}
|
|
38
|
-
|
|
39
|
-
var
|
|
40
|
-
function
|
|
41
|
-
return
|
|
42
|
-
var
|
|
43
|
-
return
|
|
38
|
+
U.exports = se();
|
|
39
|
+
var I = U.exports;
|
|
40
|
+
function ne(o) {
|
|
41
|
+
return o.filter((t) => {
|
|
42
|
+
var e;
|
|
43
|
+
return t.finishReason === "stop" ? t.text !== null && ((e = t.text) == null ? void 0 : e.trim()) !== "" : !0;
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
|
-
function
|
|
47
|
-
const
|
|
48
|
-
let
|
|
49
|
-
for (const n of
|
|
50
|
-
n.type !== "transaction" && (n.type === "reasoning" ? (
|
|
51
|
-
...
|
|
46
|
+
function O(o) {
|
|
47
|
+
const t = [];
|
|
48
|
+
let e = null;
|
|
49
|
+
for (const n of o)
|
|
50
|
+
n.type !== "transaction" && (n.type === "reasoning" ? (e != null && (t.push(e), e = null), t.push(n)) : n.functionCalls ? (e && t.push(e), t.push(n), e = null) : n.functionResponse ? t[t.length - 1] = {
|
|
51
|
+
...t[t.length - 1],
|
|
52
52
|
functionResponse: n.functionResponse
|
|
53
|
-
} :
|
|
53
|
+
} : e && n.protocol === e.protocol && (e.role === n.role || n.finishReason === "stop") ? (e.text += n.text, e.files = [...e.files ?? [], ...n.files ?? []]) : (e && t.push(e), e = {
|
|
54
54
|
...n
|
|
55
55
|
}));
|
|
56
|
-
return
|
|
56
|
+
return e && t.push(e), ne(t);
|
|
57
57
|
}
|
|
58
|
-
function
|
|
59
|
-
var
|
|
60
|
-
const
|
|
58
|
+
function oe(o) {
|
|
59
|
+
var e, s;
|
|
60
|
+
const t = ((e = o.files) == null ? void 0 : e.map(
|
|
61
61
|
(n) => ({
|
|
62
62
|
type: "file",
|
|
63
63
|
data: n.url,
|
|
64
64
|
mimeType: n.contentType
|
|
65
65
|
})
|
|
66
66
|
)) ?? [];
|
|
67
|
-
return
|
|
68
|
-
id:
|
|
67
|
+
return o.role === "function" ? {
|
|
68
|
+
id: o.id,
|
|
69
69
|
role: "assistant",
|
|
70
|
-
status: (
|
|
71
|
-
content: ((s =
|
|
70
|
+
status: (o == null ? void 0 : o.functionResponse) === null ? { type: "running" } : { type: "complete", reason: "stop" },
|
|
71
|
+
content: ((s = o.functionCalls) == null ? void 0 : s.map((n) => {
|
|
72
72
|
var a;
|
|
73
73
|
return {
|
|
74
74
|
type: "tool-call",
|
|
75
75
|
toolName: n.name,
|
|
76
76
|
toolCallId: n.id,
|
|
77
77
|
args: n.args,
|
|
78
|
-
result: (a =
|
|
78
|
+
result: (a = o.functionResponse) == null ? void 0 : a.result
|
|
79
79
|
};
|
|
80
80
|
})) ?? []
|
|
81
81
|
} : {
|
|
82
|
-
id:
|
|
83
|
-
role:
|
|
84
|
-
content:
|
|
82
|
+
id: o.id,
|
|
83
|
+
role: o.role,
|
|
84
|
+
content: o.type === "reasoning" ? [{ type: "reasoning", text: o.text }, ...t] : [{ type: "text", text: o.text }, ...t]
|
|
85
85
|
};
|
|
86
86
|
}
|
|
87
|
-
class
|
|
87
|
+
class M {
|
|
88
88
|
constructor() {
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
r(this, "statusChangeCallbacks", []);
|
|
90
|
+
r(this, "messageCallbacks", []);
|
|
91
91
|
}
|
|
92
|
-
addStatusChangeListener(
|
|
93
|
-
this.statusChangeCallbacks.push(
|
|
92
|
+
addStatusChangeListener(t) {
|
|
93
|
+
this.statusChangeCallbacks.push(t);
|
|
94
94
|
}
|
|
95
|
-
addPacketListener(
|
|
96
|
-
this.messageCallbacks.push(
|
|
95
|
+
addPacketListener(t) {
|
|
96
|
+
this.messageCallbacks.push(t);
|
|
97
97
|
}
|
|
98
|
-
async syncSession(
|
|
99
|
-
this.session =
|
|
98
|
+
async syncSession(t) {
|
|
99
|
+
this.session = t;
|
|
100
100
|
}
|
|
101
|
-
async notifyPacket(
|
|
102
|
-
this.messageCallbacks.forEach((
|
|
101
|
+
async notifyPacket(t) {
|
|
102
|
+
this.messageCallbacks.forEach((e) => e(t));
|
|
103
103
|
}
|
|
104
|
-
async notifyPackets(
|
|
105
|
-
|
|
106
|
-
this.messageCallbacks.forEach((s) => s(
|
|
104
|
+
async notifyPackets(t) {
|
|
105
|
+
t.forEach((e) => {
|
|
106
|
+
this.messageCallbacks.forEach((s) => s(e));
|
|
107
107
|
});
|
|
108
108
|
}
|
|
109
|
-
async setSession(
|
|
110
|
-
this.session =
|
|
109
|
+
async setSession(t) {
|
|
110
|
+
this.session = t;
|
|
111
111
|
}
|
|
112
|
-
async setStatus(
|
|
113
|
-
const
|
|
114
|
-
this.status =
|
|
112
|
+
async setStatus(t) {
|
|
113
|
+
const e = this.status !== t;
|
|
114
|
+
this.status = t, e && this.statusChangeCallbacks.forEach((s) => s(t));
|
|
115
115
|
}
|
|
116
116
|
clearListeners() {
|
|
117
117
|
this.statusChangeCallbacks = [], this.messageCallbacks = [];
|
|
118
118
|
}
|
|
119
|
-
onTransaction(
|
|
119
|
+
onTransaction(t) {
|
|
120
120
|
}
|
|
121
121
|
}
|
|
122
|
-
class
|
|
123
|
-
constructor(
|
|
122
|
+
class _ extends M {
|
|
123
|
+
constructor(e) {
|
|
124
124
|
super();
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
this
|
|
125
|
+
r(this, "status");
|
|
126
|
+
r(this, "autostart");
|
|
127
|
+
r(this, "session");
|
|
128
|
+
r(this, "config");
|
|
129
|
+
r(this, "notify", !0);
|
|
130
|
+
r(this, "context", {});
|
|
131
|
+
r(this, "tools", []);
|
|
132
|
+
this.config = e, this.status = "disconnected", this.autostart = !0;
|
|
132
133
|
}
|
|
133
134
|
getName() {
|
|
134
135
|
return "rest";
|
|
@@ -136,24 +137,28 @@ class z extends E {
|
|
|
136
137
|
getPriority() {
|
|
137
138
|
return 100;
|
|
138
139
|
}
|
|
139
|
-
async connect(
|
|
140
|
-
return this.setStatus("connected"),
|
|
140
|
+
async connect(e) {
|
|
141
|
+
return this.setStatus("connected"), e;
|
|
141
142
|
}
|
|
142
143
|
async disconnect() {
|
|
143
144
|
this.setStatus("disconnected"), this.session = null;
|
|
144
145
|
}
|
|
145
|
-
async syncSession(
|
|
146
|
-
this.session =
|
|
146
|
+
async syncSession(e) {
|
|
147
|
+
this.session = e;
|
|
147
148
|
}
|
|
148
|
-
async sendPacket(
|
|
149
|
-
var
|
|
150
|
-
const { apiUrl: s, apiKey: n, agentId: a } = this.config,
|
|
151
|
-
if (
|
|
152
|
-
this.context = (
|
|
149
|
+
async sendPacket(e) {
|
|
150
|
+
var w, S, T, b, A, x;
|
|
151
|
+
const { apiUrl: s, apiKey: n, agentId: a } = this.config, i = this.session ?? "new";
|
|
152
|
+
if (e.type === "command" && ((w = e == null ? void 0 : e.payload) == null ? void 0 : w.command) == "set_initial_context") {
|
|
153
|
+
this.context = (S = e == null ? void 0 : e.payload) == null ? void 0 : S.arguments;
|
|
154
|
+
return;
|
|
155
|
+
} else if (e.type === "command" && ((T = e == null ? void 0 : e.payload) == null ? void 0 : T.command) == "set_local_tools") {
|
|
156
|
+
const p = (b = e == null ? void 0 : e.payload) == null ? void 0 : b.arguments;
|
|
157
|
+
this.tools = p == null ? void 0 : p.tools, (x = (A = this.config) == null ? void 0 : A.logger) == null || x.warn("Local tools are not supported in REST protocol, ignoring command");
|
|
153
158
|
return;
|
|
154
159
|
}
|
|
155
|
-
const l =
|
|
156
|
-
body: JSON.stringify({ agentId: a, userMessage: l,
|
|
160
|
+
const l = e.payload, f = await (await fetch(`${s}/sessions/${i}/messages`, {
|
|
161
|
+
body: JSON.stringify({ agentId: a, userMessage: l, initialContext: this.context, tools: this.tools }),
|
|
157
162
|
method: "POST",
|
|
158
163
|
headers: {
|
|
159
164
|
"Content-Type": "application/json",
|
|
@@ -161,22 +166,22 @@ class z extends E {
|
|
|
161
166
|
}
|
|
162
167
|
})).json();
|
|
163
168
|
this.notifyPackets(
|
|
164
|
-
|
|
169
|
+
f.response.messages.map((p) => ({
|
|
165
170
|
type: "message",
|
|
166
|
-
payload:
|
|
171
|
+
payload: p
|
|
167
172
|
}))
|
|
168
173
|
);
|
|
169
174
|
}
|
|
170
175
|
}
|
|
171
|
-
class
|
|
172
|
-
constructor(
|
|
176
|
+
class z extends M {
|
|
177
|
+
constructor(e) {
|
|
173
178
|
super();
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
this.config =
|
|
179
|
+
r(this, "status");
|
|
180
|
+
r(this, "autostart");
|
|
181
|
+
r(this, "session");
|
|
182
|
+
r(this, "config");
|
|
183
|
+
r(this, "webSocket");
|
|
184
|
+
this.config = e, this.status = "disconnected", this.autostart = !0, this.session = null, this.webSocket = null;
|
|
180
185
|
}
|
|
181
186
|
getName() {
|
|
182
187
|
return "websocket";
|
|
@@ -184,56 +189,57 @@ class D extends E {
|
|
|
184
189
|
getPriority() {
|
|
185
190
|
return 500;
|
|
186
191
|
}
|
|
187
|
-
async syncSession(
|
|
192
|
+
async syncSession(e) {
|
|
188
193
|
var s;
|
|
189
|
-
(s = this.config.logger) == null || s.debug("Syncing session with WebSocket protocol:",
|
|
194
|
+
(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));
|
|
190
195
|
}
|
|
191
|
-
connect(
|
|
196
|
+
connect(e) {
|
|
192
197
|
var l;
|
|
193
198
|
if (this.webSocket !== null && this.status === "connected")
|
|
194
199
|
return Promise.resolve(this.session);
|
|
195
|
-
const s =
|
|
200
|
+
const s = e || this.session || "new";
|
|
196
201
|
(l = this.config.logger) == null || l.debug("Connecting to WebSocket with sessionId:", s);
|
|
197
|
-
const n = encodeURIComponent(this.config.apiKey), a = this.config.agentId,
|
|
198
|
-
return this.setStatus("connecting"), this.webSocket = new WebSocket(
|
|
202
|
+
const n = encodeURIComponent(this.config.apiKey), a = this.config.agentId, i = `${this.config.webSocketUrl}?sessionCode=${s}&agentId=${a}&apiKey=${n}`;
|
|
203
|
+
return this.setStatus("connecting"), this.webSocket = new WebSocket(i), this.webSocket.addEventListener("open", () => {
|
|
199
204
|
this.setStatus("connected");
|
|
200
205
|
}), this.webSocket.addEventListener("message", (u) => {
|
|
201
|
-
const
|
|
202
|
-
this.notifyPacket(
|
|
206
|
+
const f = JSON.parse(u.data);
|
|
207
|
+
this.notifyPacket(f);
|
|
203
208
|
}), this.webSocket.addEventListener("close", () => {
|
|
204
209
|
var u;
|
|
205
210
|
this.setStatus("disconnected"), this.webSocket = null, (u = this.config.logger) == null || u.warn("WebSocket connection closed");
|
|
206
211
|
}), this.webSocket.addEventListener("error", (u) => {
|
|
207
|
-
var
|
|
208
|
-
this.setStatus("disconnected"), this.webSocket = null, (
|
|
212
|
+
var f;
|
|
213
|
+
this.setStatus("disconnected"), this.webSocket = null, (f = this.config.logger) == null || f.error("WebSocket error", u);
|
|
209
214
|
}), Promise.resolve(s);
|
|
210
215
|
}
|
|
211
216
|
disconnect() {
|
|
212
|
-
var
|
|
213
|
-
return (
|
|
217
|
+
var e;
|
|
218
|
+
return (e = this.config.logger) == null || e.debug("Disconnecting WebSocket"), this.webSocket && this.status === "connected" && (this.webSocket.close(), this.setStatus("disconnected"), this.webSocket = null), Promise.resolve();
|
|
214
219
|
}
|
|
215
|
-
sendPacket(
|
|
216
|
-
return this.webSocket && this.status === "connected" ? (this.webSocket.send(JSON.stringify(
|
|
220
|
+
sendPacket(e) {
|
|
221
|
+
return this.webSocket && this.status === "connected" ? (this.webSocket.send(JSON.stringify(e)), Promise.resolve()) : Promise.reject(new Error("WebSocket is not connected"));
|
|
217
222
|
}
|
|
218
223
|
}
|
|
219
|
-
class
|
|
220
|
-
constructor(
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
this
|
|
224
|
+
class ae {
|
|
225
|
+
constructor(t) {
|
|
226
|
+
r(this, "config");
|
|
227
|
+
r(this, "pc", null);
|
|
228
|
+
r(this, "ws", null);
|
|
229
|
+
r(this, "localStream", null);
|
|
230
|
+
r(this, "remoteStream", new MediaStream());
|
|
231
|
+
r(this, "audioCtx", null);
|
|
232
|
+
r(this, "localAnalyser", null);
|
|
233
|
+
r(this, "remoteAnalyser", null);
|
|
234
|
+
r(this, "analyzerFrame", null);
|
|
235
|
+
r(this, "dataChannel", null);
|
|
236
|
+
r(this, "isConnected", !1);
|
|
237
|
+
r(this, "visualizerCallbacks", []);
|
|
238
|
+
r(this, "messageCallbacks", []);
|
|
239
|
+
r(this, "queuedMessages", []);
|
|
240
|
+
this.config = t;
|
|
235
241
|
}
|
|
236
|
-
async connect(
|
|
242
|
+
async connect(t) {
|
|
237
243
|
var s;
|
|
238
244
|
if (this.isConnected) return;
|
|
239
245
|
this.isConnected = !0;
|
|
@@ -257,13 +263,13 @@ class nt {
|
|
|
257
263
|
}), this.localStream.getTracks().forEach((n) => {
|
|
258
264
|
this.pc.addTrack(n, this.localStream);
|
|
259
265
|
}), this.pc.ontrack = (n) => {
|
|
260
|
-
n.streams[0].getTracks().forEach((
|
|
261
|
-
this.remoteStream.addTrack(
|
|
266
|
+
n.streams[0].getTracks().forEach((i) => {
|
|
267
|
+
this.remoteStream.addTrack(i);
|
|
262
268
|
}), this.audioCtx || this._startAnalyzers();
|
|
263
269
|
const a = new Audio();
|
|
264
|
-
a.srcObject = this.remoteStream, a.play().catch((
|
|
270
|
+
a.srcObject = this.remoteStream, a.play().catch((i) => {
|
|
265
271
|
var l;
|
|
266
|
-
(l = this.config.logger) == null || l.error("Error playing remote audio:",
|
|
272
|
+
(l = this.config.logger) == null || l.error("Error playing remote audio:", i);
|
|
267
273
|
});
|
|
268
274
|
}, this.pc.onicecandidate = (n) => {
|
|
269
275
|
var a;
|
|
@@ -276,24 +282,30 @@ class nt {
|
|
|
276
282
|
);
|
|
277
283
|
}, this.pc.ondatachannel = (n) => {
|
|
278
284
|
const a = n.channel;
|
|
279
|
-
a.onmessage = (
|
|
285
|
+
a.onmessage = (i) => {
|
|
280
286
|
this.messageCallbacks.forEach((l) => {
|
|
281
|
-
l(
|
|
287
|
+
l(i);
|
|
282
288
|
});
|
|
289
|
+
}, a.onopen = () => {
|
|
290
|
+
var i;
|
|
291
|
+
for (; this.queuedMessages.length > 0; ) {
|
|
292
|
+
const l = this.queuedMessages.shift();
|
|
293
|
+
l && (a.send(JSON.stringify(l)), (i = this.config.logger) == null || i.info("Sent queued message:", l));
|
|
294
|
+
}
|
|
283
295
|
};
|
|
284
296
|
};
|
|
285
|
-
const
|
|
286
|
-
this.ws = new WebSocket(`${
|
|
297
|
+
const e = this.config.webrtcUrl || "wss://persona.applica.guru/api/webrtc";
|
|
298
|
+
this.ws = new WebSocket(`${e}?apiKey=${encodeURIComponent(this.config.apiKey)}`), this.ws.onopen = async () => {
|
|
287
299
|
var l, u;
|
|
288
300
|
const n = await this.pc.createOffer();
|
|
289
301
|
await this.pc.setLocalDescription(n);
|
|
290
302
|
const a = {
|
|
291
303
|
apiKey: this.config.apiKey,
|
|
292
304
|
agentId: this.config.agentId,
|
|
293
|
-
sessionCode:
|
|
305
|
+
sessionCode: t
|
|
294
306
|
};
|
|
295
307
|
(l = this.config.logger) == null || l.debug("Opening connection to WebRTC server: ", a);
|
|
296
|
-
const
|
|
308
|
+
const i = {
|
|
297
309
|
type: "OFFER",
|
|
298
310
|
src: ((u = crypto.randomUUID) == null ? void 0 : u.call(crypto)) || "client_" + Date.now(),
|
|
299
311
|
payload: {
|
|
@@ -305,9 +317,9 @@ class nt {
|
|
|
305
317
|
metadata: a
|
|
306
318
|
}
|
|
307
319
|
};
|
|
308
|
-
this.ws.send(JSON.stringify(
|
|
320
|
+
this.ws.send(JSON.stringify(i));
|
|
309
321
|
}, this.ws.onmessage = async (n) => {
|
|
310
|
-
var
|
|
322
|
+
var i;
|
|
311
323
|
const a = JSON.parse(n.data);
|
|
312
324
|
if (a.type === "ANSWER")
|
|
313
325
|
await this.pc.setRemoteDescription(new RTCSessionDescription(a.payload.sdp));
|
|
@@ -315,55 +327,58 @@ class nt {
|
|
|
315
327
|
try {
|
|
316
328
|
await this.pc.addIceCandidate(new RTCIceCandidate(a.payload.candidate));
|
|
317
329
|
} catch (l) {
|
|
318
|
-
(
|
|
330
|
+
(i = this.config.logger) == null || i.error("Error adding ICE candidate:", l);
|
|
319
331
|
}
|
|
320
332
|
}, this.ws.onclose = () => {
|
|
321
333
|
this._stopAnalyzers();
|
|
322
334
|
};
|
|
323
335
|
}
|
|
324
336
|
async disconnect() {
|
|
325
|
-
var
|
|
326
|
-
this.isConnected && (this.isConnected = !1, ((
|
|
337
|
+
var t;
|
|
338
|
+
this.isConnected && (this.isConnected = !1, ((t = this.ws) == null ? void 0 : t.readyState) === WebSocket.OPEN && this.ws.close(), this.pc && this.pc.close(), this.localStream && this.localStream.getTracks().forEach((e) => e.stop()), this.remoteStream = new MediaStream(), this.audioCtx && (await this.audioCtx.close(), this.audioCtx = null), this._stopAnalyzers());
|
|
327
339
|
}
|
|
328
|
-
addVisualizerCallback(
|
|
329
|
-
this.visualizerCallbacks.push(
|
|
340
|
+
addVisualizerCallback(t) {
|
|
341
|
+
this.visualizerCallbacks.push(t);
|
|
330
342
|
}
|
|
331
|
-
addMessageCallback(
|
|
332
|
-
this.messageCallbacks.push(
|
|
343
|
+
addMessageCallback(t) {
|
|
344
|
+
this.messageCallbacks.push(t);
|
|
333
345
|
}
|
|
334
|
-
createDataChannel(
|
|
335
|
-
this.pc && (this.dataChannel = this.pc.createDataChannel(
|
|
336
|
-
var
|
|
337
|
-
|
|
338
|
-
|
|
346
|
+
createDataChannel(t = "messages") {
|
|
347
|
+
this.pc && (this.dataChannel = this.pc.createDataChannel(t), this.dataChannel.onopen = () => {
|
|
348
|
+
var e, s;
|
|
349
|
+
for ((e = this.config.logger) == null || e.info("Data channel opened"); this.queuedMessages.length > 0; ) {
|
|
350
|
+
const n = this.queuedMessages.shift();
|
|
351
|
+
n && (this.dataChannel.send(JSON.stringify(n)), (s = this.config.logger) == null || s.info("Sent queued message:", n));
|
|
352
|
+
}
|
|
353
|
+
}, this.dataChannel.onmessage = (e) => {
|
|
339
354
|
this.messageCallbacks.forEach((s) => {
|
|
340
|
-
s(
|
|
355
|
+
s(e);
|
|
341
356
|
});
|
|
342
357
|
});
|
|
343
358
|
}
|
|
344
|
-
sendPacket(
|
|
345
|
-
var
|
|
346
|
-
if (!this.dataChannel) {
|
|
347
|
-
|
|
359
|
+
sendPacket(t) {
|
|
360
|
+
var e;
|
|
361
|
+
if (!this.dataChannel || this.dataChannel.readyState !== "open") {
|
|
362
|
+
this.queuedMessages.push(t);
|
|
348
363
|
return;
|
|
349
364
|
}
|
|
350
|
-
this.dataChannel.send(JSON.stringify(
|
|
365
|
+
this.dataChannel.send(JSON.stringify(t)), (e = this.config.logger) == null || e.info("Sent message:", t);
|
|
351
366
|
}
|
|
352
367
|
_startAnalyzers() {
|
|
353
368
|
if (!this.localStream || !this.remoteStream || this.visualizerCallbacks.length === 0)
|
|
354
369
|
return;
|
|
355
370
|
this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
356
|
-
const
|
|
357
|
-
this.localAnalyser = this.audioCtx.createAnalyser(), this.remoteAnalyser = this.audioCtx.createAnalyser(), this.localAnalyser.fftSize = 256, this.remoteAnalyser.fftSize = 256,
|
|
371
|
+
const t = this.audioCtx.createMediaStreamSource(this.localStream), e = this.audioCtx.createMediaStreamSource(this.remoteStream);
|
|
372
|
+
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);
|
|
358
373
|
const s = () => {
|
|
359
374
|
if (!this.localAnalyser || !this.remoteAnalyser || this.visualizerCallbacks.length === 0)
|
|
360
375
|
return;
|
|
361
376
|
const n = new Uint8Array(this.localAnalyser.frequencyBinCount), a = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
|
|
362
377
|
this.localAnalyser.getByteFrequencyData(n), this.remoteAnalyser.getByteFrequencyData(a);
|
|
363
|
-
const
|
|
378
|
+
const i = n.reduce((u, f) => u + f, 0) / n.length, l = a.reduce((u, f) => u + f, 0) / a.length;
|
|
364
379
|
this.visualizerCallbacks.length > 0 && this.visualizerCallbacks.forEach((u) => {
|
|
365
380
|
u({
|
|
366
|
-
localAmplitude:
|
|
381
|
+
localAmplitude: i,
|
|
367
382
|
remoteAmplitude: l
|
|
368
383
|
});
|
|
369
384
|
}), this.analyzerFrame = requestAnimationFrame(s);
|
|
@@ -374,15 +389,15 @@ class nt {
|
|
|
374
389
|
this.analyzerFrame && (cancelAnimationFrame(this.analyzerFrame), this.analyzerFrame = null), this.localAnalyser = null, this.remoteAnalyser = null;
|
|
375
390
|
}
|
|
376
391
|
}
|
|
377
|
-
class
|
|
378
|
-
constructor(
|
|
392
|
+
class F extends M {
|
|
393
|
+
constructor(e) {
|
|
379
394
|
super();
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
this.config =
|
|
395
|
+
r(this, "status");
|
|
396
|
+
r(this, "session");
|
|
397
|
+
r(this, "autostart");
|
|
398
|
+
r(this, "config");
|
|
399
|
+
r(this, "webRTCClient");
|
|
400
|
+
this.config = e, this.status = "disconnected", this.session = null, this.autostart = (e == null ? void 0 : e.autostart) ?? !1, this.webRTCClient = new ae(e), this.webRTCClient.addMessageCallback((s) => {
|
|
386
401
|
const n = JSON.parse(s.data);
|
|
387
402
|
this.notifyPacket(n);
|
|
388
403
|
});
|
|
@@ -393,39 +408,39 @@ class U extends E {
|
|
|
393
408
|
getPriority() {
|
|
394
409
|
return 1e3;
|
|
395
410
|
}
|
|
396
|
-
async syncSession(
|
|
397
|
-
super.syncSession(
|
|
411
|
+
async syncSession(e) {
|
|
412
|
+
super.syncSession(e), this.status === "connected" && (await this.disconnect(), await this.connect(e));
|
|
398
413
|
}
|
|
399
|
-
async connect(
|
|
414
|
+
async connect(e) {
|
|
400
415
|
var s;
|
|
401
|
-
return this.status === "connected" ? Promise.resolve(this.session) : (this.session =
|
|
416
|
+
return this.status === "connected" ? Promise.resolve(this.session) : (this.session = e || this.session || "new", this.setStatus("connecting"), (s = this.config.logger) == null || s.debug("Connecting to WebRTC with sessionId:", this.session), await this.webRTCClient.connect(this.session), this.setStatus("connected"), await this.webRTCClient.createDataChannel(), this.session);
|
|
402
417
|
}
|
|
403
418
|
async disconnect() {
|
|
404
|
-
var
|
|
419
|
+
var e, s, n;
|
|
405
420
|
if (this.status === "disconnected")
|
|
406
|
-
return (
|
|
421
|
+
return (e = this.config.logger) == null || e.warn("Already disconnected"), Promise.resolve();
|
|
407
422
|
await this.webRTCClient.disconnect(), this.setStatus("disconnected"), (n = (s = this.config) == null ? void 0 : s.logger) == null || n.debug("Disconnected from WebRTC");
|
|
408
423
|
}
|
|
409
|
-
sendPacket(
|
|
410
|
-
return this.status !== "connected" ? Promise.reject(new Error("Not connected")) : (this.webRTCClient.sendPacket(
|
|
424
|
+
sendPacket(e) {
|
|
425
|
+
return this.status !== "connected" ? Promise.reject(new Error("Not connected")) : (this.webRTCClient.sendPacket(e), Promise.resolve());
|
|
411
426
|
}
|
|
412
427
|
}
|
|
413
|
-
class
|
|
414
|
-
constructor(
|
|
415
|
-
|
|
416
|
-
this.config =
|
|
428
|
+
class ie {
|
|
429
|
+
constructor(t) {
|
|
430
|
+
r(this, "config");
|
|
431
|
+
this.config = t;
|
|
417
432
|
}
|
|
418
|
-
async complete(
|
|
433
|
+
async complete(t, e) {
|
|
419
434
|
var s;
|
|
420
|
-
await this.persist(
|
|
435
|
+
await this.persist(t, { ...e, success: !0 }), (s = this.config.logger) == null || s.debug("Transaction completed:", t);
|
|
421
436
|
}
|
|
422
|
-
async fail(
|
|
437
|
+
async fail(t, e) {
|
|
423
438
|
var s;
|
|
424
|
-
await this.persist(
|
|
439
|
+
await this.persist(t, { ...e, success: !1 }), (s = this.config.logger) == null || s.debug("Transaction failed:", { ...t, ...e });
|
|
425
440
|
}
|
|
426
|
-
async persist(
|
|
427
|
-
await fetch(`${this.config.apiUrl}/transactions/${
|
|
428
|
-
body: JSON.stringify(
|
|
441
|
+
async persist(t, e) {
|
|
442
|
+
await fetch(`${this.config.apiUrl}/transactions/${t.id}`, {
|
|
443
|
+
body: JSON.stringify(e),
|
|
429
444
|
method: "POST",
|
|
430
445
|
headers: {
|
|
431
446
|
"Content-Type": "application/json",
|
|
@@ -435,49 +450,55 @@ class it {
|
|
|
435
450
|
});
|
|
436
451
|
}
|
|
437
452
|
}
|
|
438
|
-
class
|
|
439
|
-
constructor(
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
this.transaction =
|
|
453
|
+
class re {
|
|
454
|
+
constructor(t, e) {
|
|
455
|
+
r(this, "transaction");
|
|
456
|
+
r(this, "manager");
|
|
457
|
+
this.transaction = t, this.manager = e;
|
|
443
458
|
}
|
|
444
459
|
getFunctionCall() {
|
|
445
460
|
return this.transaction.functionCall;
|
|
446
461
|
}
|
|
447
|
-
async invoke(
|
|
448
|
-
const
|
|
449
|
-
if (!
|
|
462
|
+
async invoke(t) {
|
|
463
|
+
const e = this.transaction.functionCall;
|
|
464
|
+
if (!e) {
|
|
450
465
|
await this.fail("No function call found");
|
|
451
466
|
return;
|
|
452
467
|
}
|
|
453
|
-
const s =
|
|
468
|
+
const s = e.name, n = e.args, a = t[s];
|
|
454
469
|
if (!a) {
|
|
455
470
|
await this.fail(`Tool ${s} not found`);
|
|
456
471
|
return;
|
|
457
472
|
}
|
|
458
473
|
try {
|
|
459
|
-
const
|
|
460
|
-
await this.complete(
|
|
461
|
-
} catch (
|
|
462
|
-
await this.fail(`Error executing tool ${s}: ${
|
|
474
|
+
const i = await a.apply(null, Object.values(n));
|
|
475
|
+
await this.complete(i);
|
|
476
|
+
} catch (i) {
|
|
477
|
+
await this.fail(`Error executing tool ${s}: ${i}`);
|
|
463
478
|
}
|
|
464
479
|
}
|
|
465
|
-
async complete(
|
|
466
|
-
await this.manager.complete(this.transaction, { success: !0, output:
|
|
480
|
+
async complete(t) {
|
|
481
|
+
await this.manager.complete(this.transaction, { success: !0, output: t, error: null });
|
|
467
482
|
}
|
|
468
|
-
async fail(
|
|
469
|
-
await this.manager.fail(this.transaction, { success: !1, output: null, error:
|
|
483
|
+
async fail(t) {
|
|
484
|
+
await this.manager.fail(this.transaction, { success: !1, output: null, error: t });
|
|
470
485
|
}
|
|
471
486
|
}
|
|
472
|
-
class
|
|
473
|
-
constructor(
|
|
487
|
+
class ce extends M {
|
|
488
|
+
constructor(e) {
|
|
474
489
|
super();
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
this
|
|
490
|
+
r(this, "status");
|
|
491
|
+
r(this, "autostart");
|
|
492
|
+
r(this, "session");
|
|
493
|
+
r(this, "config");
|
|
494
|
+
r(this, "notify", !0);
|
|
495
|
+
r(this, "_tools");
|
|
496
|
+
if (this.config = e, this.status = "disconnected", this.autostart = !0, Array.isArray(e.tools)) {
|
|
497
|
+
this._tools = {};
|
|
498
|
+
for (const s of e.tools)
|
|
499
|
+
s.schema && s.implementation && (this._tools[s.schema.name] = s.implementation);
|
|
500
|
+
} else
|
|
501
|
+
this._tools = e.tools;
|
|
481
502
|
}
|
|
482
503
|
getName() {
|
|
483
504
|
return "transaction";
|
|
@@ -485,245 +506,362 @@ class ot extends E {
|
|
|
485
506
|
getPriority() {
|
|
486
507
|
return 0;
|
|
487
508
|
}
|
|
488
|
-
async connect(
|
|
489
|
-
return this.setStatus("connected"),
|
|
509
|
+
async connect(e) {
|
|
510
|
+
return this.setStatus("connected"), e;
|
|
490
511
|
}
|
|
491
512
|
async disconnect() {
|
|
492
513
|
this.setStatus("disconnected"), this.session = null;
|
|
493
514
|
}
|
|
494
|
-
async syncSession(
|
|
495
|
-
this.session =
|
|
515
|
+
async syncSession(e) {
|
|
516
|
+
this.session = e;
|
|
496
517
|
}
|
|
497
|
-
async sendPacket(
|
|
518
|
+
async sendPacket(e) {
|
|
498
519
|
}
|
|
499
|
-
onTransaction(
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
this.config.onTransaction(n);
|
|
520
|
+
onTransaction(e) {
|
|
521
|
+
console.log("transaction received:", e);
|
|
522
|
+
const s = new ie(this.config), n = new re(e, s);
|
|
523
|
+
this.config.onTransaction ? this.config.onTransaction(n) : n.invoke(this._tools);
|
|
524
|
+
}
|
|
525
|
+
getTools() {
|
|
526
|
+
return this._tools;
|
|
507
527
|
}
|
|
508
528
|
}
|
|
509
|
-
const
|
|
510
|
-
function
|
|
511
|
-
return new Promise((
|
|
529
|
+
const R = Y(void 0);
|
|
530
|
+
function le(o) {
|
|
531
|
+
return new Promise((t, e) => {
|
|
512
532
|
const s = new FileReader();
|
|
513
|
-
s.readAsDataURL(
|
|
533
|
+
s.readAsDataURL(o), s.onload = () => {
|
|
514
534
|
const a = s.result.split(";base64,")[1];
|
|
515
|
-
|
|
535
|
+
t(a);
|
|
516
536
|
}, s.onerror = (n) => {
|
|
517
|
-
|
|
537
|
+
e(n);
|
|
518
538
|
};
|
|
519
539
|
});
|
|
520
540
|
}
|
|
521
|
-
function
|
|
522
|
-
dev:
|
|
523
|
-
baseUrl:
|
|
524
|
-
protocols:
|
|
541
|
+
function ue({
|
|
542
|
+
dev: o = !1,
|
|
543
|
+
baseUrl: t,
|
|
544
|
+
protocols: e,
|
|
525
545
|
logger: s,
|
|
526
546
|
children: n,
|
|
527
547
|
session: a = "new",
|
|
528
|
-
...
|
|
548
|
+
...i
|
|
529
549
|
}) {
|
|
530
|
-
const [l, u] =
|
|
531
|
-
if (Array.isArray(
|
|
532
|
-
return
|
|
533
|
-
if (typeof
|
|
534
|
-
const c =
|
|
535
|
-
let
|
|
536
|
-
switch (
|
|
550
|
+
const [l, u] = v(!1), [f, w] = v([]), [S, T] = v(a), [b, A] = v(/* @__PURE__ */ new Map()), x = G(!1), p = H(() => {
|
|
551
|
+
if (Array.isArray(e))
|
|
552
|
+
return e;
|
|
553
|
+
if (typeof e == "object" && e !== null) {
|
|
554
|
+
const c = o ? "localhost:8000" : t || "persona.applica.guru/api", m = o ? "http" : "https", h = o ? "ws" : "wss";
|
|
555
|
+
let y = Object.keys(e).map((g) => {
|
|
556
|
+
switch (g) {
|
|
537
557
|
case "rest":
|
|
538
|
-
const
|
|
539
|
-
return
|
|
540
|
-
apiUrl: `${
|
|
541
|
-
apiKey:
|
|
542
|
-
agentId:
|
|
558
|
+
const d = e[g];
|
|
559
|
+
return d === !0 ? new _({
|
|
560
|
+
apiUrl: `${m}://${c}`,
|
|
561
|
+
apiKey: i.apiKey,
|
|
562
|
+
agentId: i.agentId,
|
|
543
563
|
logger: s
|
|
544
|
-
}) : typeof
|
|
564
|
+
}) : typeof d == "object" && d !== null ? new _(d) : null;
|
|
545
565
|
case "webrtc":
|
|
546
|
-
const C =
|
|
547
|
-
return C === !0 ? new
|
|
548
|
-
webrtcUrl: `${
|
|
549
|
-
apiKey:
|
|
550
|
-
agentId:
|
|
566
|
+
const C = e[g];
|
|
567
|
+
return C === !0 ? new F({
|
|
568
|
+
webrtcUrl: `${h}://${c}/webrtc`,
|
|
569
|
+
apiKey: i.apiKey,
|
|
570
|
+
agentId: i.agentId,
|
|
551
571
|
logger: s
|
|
552
|
-
}) : typeof C == "object" && C !== null ? new
|
|
572
|
+
}) : typeof C == "object" && C !== null ? new F(C) : null;
|
|
553
573
|
case "websocket":
|
|
554
|
-
const
|
|
555
|
-
return
|
|
556
|
-
webSocketUrl: `${
|
|
557
|
-
apiKey:
|
|
558
|
-
agentId:
|
|
574
|
+
const E = e[g];
|
|
575
|
+
return E === !0 ? new z({
|
|
576
|
+
webSocketUrl: `${h}://${c}/websocket`,
|
|
577
|
+
apiKey: i.apiKey,
|
|
578
|
+
agentId: i.agentId,
|
|
559
579
|
logger: s
|
|
560
|
-
}) : typeof
|
|
580
|
+
}) : typeof E == "object" && E !== null ? new z(E) : null;
|
|
561
581
|
default:
|
|
562
|
-
throw new Error(`Unknown protocol: ${
|
|
582
|
+
throw new Error(`Unknown protocol: ${g}`);
|
|
563
583
|
}
|
|
564
|
-
}).filter((
|
|
565
|
-
return
|
|
566
|
-
new
|
|
567
|
-
apiUrl: `${
|
|
568
|
-
apiKey:
|
|
569
|
-
agentId:
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
},
|
|
584
|
+
}).filter((g) => g !== null);
|
|
585
|
+
return i.tools && y.push(
|
|
586
|
+
new ce({
|
|
587
|
+
apiUrl: `${m}://${c}`,
|
|
588
|
+
apiKey: i.apiKey,
|
|
589
|
+
agentId: i.agentId,
|
|
590
|
+
tools: i.tools,
|
|
591
|
+
// Pass raw tools
|
|
573
592
|
logger: s
|
|
574
593
|
})
|
|
575
|
-
),
|
|
594
|
+
), y;
|
|
576
595
|
}
|
|
577
596
|
throw new Error("Invalid protocols configuration");
|
|
578
597
|
}, []);
|
|
579
|
-
|
|
580
|
-
|
|
598
|
+
Q(() => {
|
|
599
|
+
x.current || (x.current = !0, s == null || s.debug(
|
|
581
600
|
"Initializing protocols: ",
|
|
582
|
-
|
|
583
|
-
),
|
|
584
|
-
c.setSession(
|
|
585
|
-
s == null || s.debug(`${c.getName()} has notified new status: ${
|
|
601
|
+
p.map((c) => c.getName())
|
|
602
|
+
), p.forEach((c) => {
|
|
603
|
+
c.setSession(S), c.clearListeners(), c.addStatusChangeListener((m) => {
|
|
604
|
+
s == null || s.debug(`${c.getName()} has notified new status: ${m}`), b.set(c.getName(), m), m === "connected" && (i.context && c.sendPacket({
|
|
586
605
|
type: "command",
|
|
587
606
|
payload: {
|
|
588
607
|
command: "set_initial_context",
|
|
589
|
-
arguments:
|
|
608
|
+
arguments: i.context
|
|
609
|
+
}
|
|
610
|
+
}), i.tools && Array.isArray(i.tools) && c.sendPacket({
|
|
611
|
+
type: "command",
|
|
612
|
+
payload: {
|
|
613
|
+
command: "set_local_tools",
|
|
614
|
+
arguments: {
|
|
615
|
+
tools: i.tools.map((h) => h.schema)
|
|
616
|
+
}
|
|
590
617
|
}
|
|
591
|
-
}),
|
|
592
|
-
}), c.addPacketListener((
|
|
593
|
-
if (
|
|
594
|
-
const
|
|
595
|
-
|
|
596
|
-
(
|
|
618
|
+
})), A(new Map(b));
|
|
619
|
+
}), c.addPacketListener((m) => {
|
|
620
|
+
if (m.type === "message") {
|
|
621
|
+
const h = m.payload;
|
|
622
|
+
w(
|
|
623
|
+
(y) => O([...y, { ...h, protocol: c.getName() }])
|
|
597
624
|
);
|
|
598
|
-
} else if (
|
|
599
|
-
const
|
|
600
|
-
let
|
|
601
|
-
|
|
625
|
+
} else if (m.type === "reasoning") {
|
|
626
|
+
const h = m.payload;
|
|
627
|
+
let y = h.thought;
|
|
628
|
+
h.imageUrl && (y += `
|
|
602
629
|
|
|
603
|
-
`);
|
|
631
|
+
const g = { type: "reasoning", text: y, role: "assistant", finishReason: "stop" };
|
|
632
|
+
w(
|
|
633
|
+
(d) => O([...d, { ...g, protocol: c.getName() }])
|
|
607
634
|
);
|
|
608
|
-
} else
|
|
609
|
-
}), c.autostart && c.status === "disconnected" && (s == null || s.debug(`Connecting to protocol: ${c.getName()}`), c.connect(
|
|
635
|
+
} else m.type === "transaction" && p.filter((h) => h !== c).forEach((h) => h.onTransaction(m.payload));
|
|
636
|
+
}), c.autostart && c.status === "disconnected" && (s == null || s.debug(`Connecting to protocol: ${c.getName()}`), c.connect(S));
|
|
610
637
|
}));
|
|
611
|
-
}, [
|
|
612
|
-
const
|
|
613
|
-
var
|
|
614
|
-
if (((
|
|
615
|
-
const
|
|
616
|
-
|
|
617
|
-
const
|
|
638
|
+
}, [S, p, s, b]);
|
|
639
|
+
const W = async (c) => {
|
|
640
|
+
var g;
|
|
641
|
+
if (((g = c.content[0]) == null ? void 0 : g.type) !== "text") throw new Error("Only text messages are supported");
|
|
642
|
+
const m = c.content[0].text;
|
|
643
|
+
w((d) => [...d, { role: "user", type: "text", text: m }]), u(!0);
|
|
644
|
+
const h = p.sort((d, C) => C.getPriority() - d.getPriority()).find((d) => d.status === "connected"), y = [];
|
|
618
645
|
if (c.attachments)
|
|
619
|
-
for (const
|
|
620
|
-
|
|
646
|
+
for (const d of c.attachments)
|
|
647
|
+
d.contentType.startsWith("image/") && d.file && y.push({
|
|
621
648
|
role: "user",
|
|
622
649
|
image: {
|
|
623
|
-
contentType:
|
|
624
|
-
content: await
|
|
650
|
+
contentType: d.contentType,
|
|
651
|
+
content: await le(d.file)
|
|
625
652
|
},
|
|
626
653
|
text: "",
|
|
627
654
|
type: "text"
|
|
628
655
|
});
|
|
629
|
-
c.content &&
|
|
656
|
+
c.content && y.push({
|
|
630
657
|
role: "user",
|
|
631
658
|
text: c.content[0].text,
|
|
632
659
|
type: "text"
|
|
633
|
-
}), s == null || s.debug("Sending message:",
|
|
634
|
-
},
|
|
660
|
+
}), s == null || s.debug("Sending message:", y), await (h == null ? void 0 : h.sendPacket({ type: "request", payload: y })), u(!1);
|
|
661
|
+
}, D = N(() => (u(!1), w([]), T("new"), Promise.resolve()), []), L = N(() => Promise.resolve(), []), J = N(() => f, [f]), K = X({
|
|
635
662
|
isRunning: l,
|
|
636
|
-
messages:
|
|
637
|
-
convertMessage:
|
|
638
|
-
onNew:
|
|
639
|
-
onCancel:
|
|
640
|
-
onReload:
|
|
663
|
+
messages: f,
|
|
664
|
+
convertMessage: oe,
|
|
665
|
+
onNew: W,
|
|
666
|
+
onCancel: D,
|
|
667
|
+
onReload: L,
|
|
641
668
|
adapters: {
|
|
642
|
-
attachments: new
|
|
669
|
+
attachments: new Z([new ee()])
|
|
643
670
|
}
|
|
644
671
|
});
|
|
645
|
-
return /* @__PURE__ */
|
|
672
|
+
return /* @__PURE__ */ I.jsx(R.Provider, { value: { protocols: p, protocolsStatus: b, getMessages: J }, children: /* @__PURE__ */ I.jsx(te, { runtime: K, children: n }) });
|
|
646
673
|
}
|
|
647
|
-
function
|
|
648
|
-
return /* @__PURE__ */
|
|
674
|
+
function ge({ children: o, ...t }) {
|
|
675
|
+
return /* @__PURE__ */ I.jsx(ue, { ...t, children: o });
|
|
649
676
|
}
|
|
650
|
-
function
|
|
651
|
-
const
|
|
652
|
-
if (!
|
|
677
|
+
function we() {
|
|
678
|
+
const o = $(R);
|
|
679
|
+
if (!o)
|
|
653
680
|
throw new Error("usePersonaRuntime must be used within a PersonaRuntimeProvider");
|
|
654
|
-
return
|
|
681
|
+
return o;
|
|
655
682
|
}
|
|
656
|
-
function
|
|
657
|
-
const
|
|
658
|
-
if (!e)
|
|
659
|
-
throw new Error("usePersonaRuntimeProtocol must be used within a PersonaRuntimeProvider");
|
|
660
|
-
const t = e.protocols.find((n) => n.getName() === i);
|
|
683
|
+
function he(o) {
|
|
684
|
+
const t = $(R);
|
|
661
685
|
if (!t)
|
|
686
|
+
throw new Error("usePersonaRuntimeProtocol must be used within a PersonaRuntimeProvider");
|
|
687
|
+
const e = t.protocols.find((n) => n.getName() === o);
|
|
688
|
+
if (!e)
|
|
662
689
|
return null;
|
|
663
|
-
const s =
|
|
690
|
+
const s = t.protocolsStatus.get(e.getName());
|
|
664
691
|
return {
|
|
665
|
-
...
|
|
666
|
-
connect:
|
|
667
|
-
disconnect:
|
|
668
|
-
sendPacket:
|
|
669
|
-
setSession:
|
|
670
|
-
addStatusChangeListener:
|
|
671
|
-
addPacketListener:
|
|
672
|
-
getName:
|
|
673
|
-
getPriority:
|
|
674
|
-
status: s ||
|
|
692
|
+
...e,
|
|
693
|
+
connect: e.connect.bind(e),
|
|
694
|
+
disconnect: e.disconnect.bind(e),
|
|
695
|
+
sendPacket: e.sendPacket.bind(e),
|
|
696
|
+
setSession: e.setSession.bind(e),
|
|
697
|
+
addStatusChangeListener: e.addStatusChangeListener.bind(e),
|
|
698
|
+
addPacketListener: e.addPacketListener.bind(e),
|
|
699
|
+
getName: e.getName.bind(e),
|
|
700
|
+
getPriority: e.getPriority.bind(e),
|
|
701
|
+
status: s || e.status
|
|
675
702
|
};
|
|
676
703
|
}
|
|
677
|
-
function
|
|
678
|
-
const
|
|
679
|
-
if (!
|
|
704
|
+
function be() {
|
|
705
|
+
const o = $(R);
|
|
706
|
+
if (!o)
|
|
680
707
|
throw new Error("usePersonaRuntimeEndpoint must be used within a PersonaRuntimeProvider");
|
|
681
|
-
for (const
|
|
682
|
-
if (
|
|
683
|
-
return
|
|
708
|
+
for (const t of o.protocols)
|
|
709
|
+
if (t.getName() === "rest")
|
|
710
|
+
return t.config.apiUrl;
|
|
684
711
|
throw new Error("REST protocol not found");
|
|
685
712
|
}
|
|
686
|
-
function
|
|
687
|
-
return
|
|
713
|
+
function Se() {
|
|
714
|
+
return he("webrtc");
|
|
688
715
|
}
|
|
689
|
-
function
|
|
690
|
-
const
|
|
691
|
-
if (!
|
|
716
|
+
function Ce() {
|
|
717
|
+
const o = $(R);
|
|
718
|
+
if (!o)
|
|
692
719
|
throw new Error("usePersonaRuntimeMessages must be used within a PersonaRuntimeProvider");
|
|
693
|
-
return
|
|
720
|
+
return o.getMessages();
|
|
694
721
|
}
|
|
695
|
-
class
|
|
722
|
+
class Pe {
|
|
696
723
|
constructor() {
|
|
697
|
-
|
|
724
|
+
r(this, "prefix", "[Persona]");
|
|
725
|
+
}
|
|
726
|
+
log(t, ...e) {
|
|
727
|
+
console.log(`${this.prefix} - ${t}`, ...e);
|
|
698
728
|
}
|
|
699
|
-
|
|
700
|
-
console.
|
|
729
|
+
info(t, ...e) {
|
|
730
|
+
console.info(`${this.prefix} - ${t}`, ...e);
|
|
701
731
|
}
|
|
702
|
-
|
|
703
|
-
console.
|
|
732
|
+
warn(t, ...e) {
|
|
733
|
+
console.warn(`${this.prefix} - ${t}`, ...e);
|
|
704
734
|
}
|
|
705
|
-
|
|
706
|
-
console.
|
|
735
|
+
error(t, ...e) {
|
|
736
|
+
console.error(`${this.prefix} - ${t}`, ...e);
|
|
707
737
|
}
|
|
708
|
-
|
|
709
|
-
console.
|
|
738
|
+
debug(t, ...e) {
|
|
739
|
+
console.debug(`${this.prefix} - ${t}`, ...e);
|
|
710
740
|
}
|
|
711
|
-
|
|
712
|
-
|
|
741
|
+
}
|
|
742
|
+
function P(o, t, e) {
|
|
743
|
+
return {
|
|
744
|
+
type: o,
|
|
745
|
+
description: t,
|
|
746
|
+
...e
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
function de(o) {
|
|
750
|
+
const t = Object.entries(o.parameters).filter(([e, s]) => s.required).map(([e]) => e);
|
|
751
|
+
return {
|
|
752
|
+
type: "local",
|
|
753
|
+
name: o.name,
|
|
754
|
+
description: o.description,
|
|
755
|
+
config: {
|
|
756
|
+
timeout: o.timeout || 60,
|
|
757
|
+
parameters: {
|
|
758
|
+
type: "object",
|
|
759
|
+
title: o.title || `${o.name} parameters`,
|
|
760
|
+
required: t,
|
|
761
|
+
properties: o.parameters
|
|
762
|
+
},
|
|
763
|
+
output: {
|
|
764
|
+
type: "object",
|
|
765
|
+
title: `${o.name} output`,
|
|
766
|
+
properties: o.output
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
function j(o) {
|
|
772
|
+
return {
|
|
773
|
+
schema: de(o),
|
|
774
|
+
implementation: o.implementation
|
|
775
|
+
};
|
|
776
|
+
}
|
|
777
|
+
function me(o, t, e, s, n, a) {
|
|
778
|
+
const i = {
|
|
779
|
+
name: o,
|
|
780
|
+
description: t,
|
|
781
|
+
title: a == null ? void 0 : a.title,
|
|
782
|
+
timeout: a == null ? void 0 : a.timeout,
|
|
783
|
+
parameters: s,
|
|
784
|
+
output: n,
|
|
785
|
+
implementation: e
|
|
786
|
+
};
|
|
787
|
+
return j(i);
|
|
788
|
+
}
|
|
789
|
+
const xe = me(
|
|
790
|
+
"sum",
|
|
791
|
+
"Sum two numbers",
|
|
792
|
+
function(t, e) {
|
|
793
|
+
return { result: t + e };
|
|
794
|
+
},
|
|
795
|
+
{
|
|
796
|
+
a: P("number", "First number to sum", { required: !0 }),
|
|
797
|
+
b: P("number", "Second number to sum", { required: !0 })
|
|
798
|
+
},
|
|
799
|
+
{
|
|
800
|
+
result: P("number", "Sum of two numbers")
|
|
801
|
+
}
|
|
802
|
+
), ke = j({
|
|
803
|
+
name: "navigate_to",
|
|
804
|
+
description: "Allow agent to redirect user to specific sub page like /foo or #/foo or anything like that",
|
|
805
|
+
title: "Sum two numbers",
|
|
806
|
+
// As per the user's example
|
|
807
|
+
timeout: 60,
|
|
808
|
+
parameters: {
|
|
809
|
+
a: P("number", "First number to sum"),
|
|
810
|
+
b: P("number", "Seconth number to sum")
|
|
811
|
+
// Keeping the typo as in the original
|
|
812
|
+
},
|
|
813
|
+
output: {
|
|
814
|
+
result: P("number", "Sum of two numbers")
|
|
815
|
+
},
|
|
816
|
+
implementation: function(t, e) {
|
|
817
|
+
return { result: t + e };
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
function Re(o) {
|
|
821
|
+
const t = [], e = {};
|
|
822
|
+
return o.forEach((s) => {
|
|
823
|
+
const { schema: n, implementation: a } = j(s);
|
|
824
|
+
t.push(n), e[s.name] = a;
|
|
825
|
+
}), { schemas: t, implementations: e };
|
|
826
|
+
}
|
|
827
|
+
function Te(o, t) {
|
|
828
|
+
const { required: e, properties: s } = t.config.parameters;
|
|
829
|
+
for (const n of e)
|
|
830
|
+
if (!(n in o))
|
|
831
|
+
throw new Error(`Missing required parameter: ${n}`);
|
|
832
|
+
for (const [n, a] of Object.entries(o)) {
|
|
833
|
+
const i = s[n];
|
|
834
|
+
if (i) {
|
|
835
|
+
if (i.type === "number" && typeof a != "number")
|
|
836
|
+
throw new Error(`Parameter ${n} should be a number`);
|
|
837
|
+
if (i.type === "string" && typeof a != "string")
|
|
838
|
+
throw new Error(`Parameter ${n} should be a string`);
|
|
839
|
+
if (i.type === "boolean" && typeof a != "boolean")
|
|
840
|
+
throw new Error(`Parameter ${n} should be a boolean`);
|
|
841
|
+
}
|
|
713
842
|
}
|
|
843
|
+
return !0;
|
|
714
844
|
}
|
|
715
845
|
export {
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
846
|
+
Pe as PersonaConsoleLogger,
|
|
847
|
+
M as PersonaProtocolBase,
|
|
848
|
+
_ as PersonaRESTProtocol,
|
|
849
|
+
ge as PersonaRuntimeProvider,
|
|
850
|
+
ce as PersonaTransactionProtocol,
|
|
851
|
+
F as PersonaWebRTCProtocol,
|
|
852
|
+
z as PersonaWebSocketProtocol,
|
|
853
|
+
P as createParameter,
|
|
854
|
+
j as createTool,
|
|
855
|
+
me as createToolFromFunction,
|
|
856
|
+
Re as createToolRegistry,
|
|
857
|
+
de as generateToolSchema,
|
|
858
|
+
ke as navigateToToolExample,
|
|
859
|
+
xe as sumTool,
|
|
860
|
+
we as usePersonaRuntime,
|
|
861
|
+
be as usePersonaRuntimeEndpoint,
|
|
862
|
+
Ce as usePersonaRuntimeMessages,
|
|
863
|
+
he as usePersonaRuntimeProtocol,
|
|
864
|
+
Se as usePersonaRuntimeWebRTCProtocol,
|
|
865
|
+
Te as validateToolParameters
|
|
728
866
|
};
|
|
729
867
|
//# sourceMappingURL=bundle.es.js.map
|