@applica-software-guru/persona-sdk 0.0.1-preview6 → 0.1.2
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/.eslintrc.cjs +6 -5
- package/.nvmrc +1 -1
- package/.prettierignore +3 -5
- package/.prettierrc +3 -5
- package/README.md +247 -2
- package/bitbucket-pipelines.yml +2 -12
- package/dist/bundle.cjs.js +1 -14
- package/dist/bundle.cjs.js.map +1 -1
- package/dist/bundle.es.js +472 -699
- package/dist/bundle.es.js.map +1 -1
- package/dist/index.d.ts +1089 -5
- package/package.json +9 -39
- package/src/agents/agents-api.ts +54 -0
- package/src/agents/types.ts +346 -0
- package/src/auth/api-key-auth.ts +13 -0
- package/src/auth/authentication-provider.ts +4 -0
- package/src/auth/bearer-token-auth.ts +13 -0
- package/src/auth/index.ts +3 -0
- package/src/credentials/credentials-api.ts +40 -0
- package/src/credentials/types.ts +67 -0
- package/src/exceptions.ts +10 -0
- package/src/features/feature-templates-api.ts +41 -0
- package/src/features/features-api.ts +16 -0
- package/src/features/types.ts +25 -0
- package/src/http-api.ts +95 -0
- package/src/index.ts +153 -4
- package/src/knowledges/knowledge-base-documents-api.ts +38 -0
- package/src/knowledges/knowledge-bases-api.ts +47 -0
- package/src/knowledges/types.ts +70 -0
- package/src/missions/missions-api.ts +60 -0
- package/src/missions/types.ts +25 -0
- package/src/paginated.ts +6 -0
- package/src/persona-sdk.ts +81 -0
- package/src/projects/projects-api.ts +55 -0
- package/src/projects/types.ts +42 -0
- package/src/revisions/types.ts +9 -0
- package/src/service-prices/service-prices-api.ts +32 -0
- package/src/service-prices/types.ts +8 -0
- package/src/sessions/sessions-api.ts +55 -0
- package/src/sessions/types.ts +129 -0
- package/src/triggers/trigger-executions-api.ts +27 -0
- package/src/triggers/triggers-api.ts +37 -0
- package/src/triggers/types.ts +50 -0
- package/src/workflows/types.ts +150 -0
- package/src/workflows/workflow-executions-api.ts +27 -0
- package/src/workflows/workflows-api.ts +51 -0
- package/tsconfig.json +20 -28
- package/vite.config.ts +20 -62
- package/dist/bundle.iife.js +0 -15
- package/dist/bundle.iife.js.map +0 -1
- package/dist/bundle.umd.js +0 -15
- package/dist/bundle.umd.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/logging.d.ts +0 -18
- package/dist/logging.d.ts.map +0 -1
- package/dist/messages.d.ts +0 -7
- package/dist/messages.d.ts.map +0 -1
- package/dist/protocol/base.d.ts +0 -23
- package/dist/protocol/base.d.ts.map +0 -1
- package/dist/protocol/index.d.ts +0 -5
- package/dist/protocol/index.d.ts.map +0 -1
- package/dist/protocol/rest.d.ts +0 -22
- package/dist/protocol/rest.d.ts.map +0 -1
- package/dist/protocol/webrtc.d.ts +0 -56
- package/dist/protocol/webrtc.d.ts.map +0 -1
- package/dist/protocol/websocket.d.ts +0 -22
- package/dist/protocol/websocket.d.ts.map +0 -1
- package/dist/runtime.d.ts +0 -21
- package/dist/runtime.d.ts.map +0 -1
- package/dist/types.d.ts +0 -79
- package/dist/types.d.ts.map +0 -1
- package/jsconfig.node.json +0 -10
- package/playground/index.html +0 -14
- package/playground/src/app.tsx +0 -10
- package/playground/src/chat.tsx +0 -52
- package/playground/src/components/assistant-ui/assistant-modal.tsx +0 -57
- package/playground/src/components/assistant-ui/markdown-text.tsx +0 -119
- package/playground/src/components/assistant-ui/thread-list.tsx +0 -62
- package/playground/src/components/assistant-ui/thread.tsx +0 -249
- package/playground/src/components/assistant-ui/tool-fallback.tsx +0 -33
- package/playground/src/components/assistant-ui/tooltip-icon-button.tsx +0 -38
- package/playground/src/components/ui/avatar.tsx +0 -35
- package/playground/src/components/ui/button.tsx +0 -43
- package/playground/src/components/ui/tooltip.tsx +0 -32
- package/playground/src/lib/utils.ts +0 -6
- package/playground/src/main.tsx +0 -10
- package/playground/src/styles.css +0 -1
- package/playground/src/vite-env.d.ts +0 -1
- package/preview.sh +0 -13
- package/src/logging.ts +0 -34
- package/src/messages.ts +0 -79
- package/src/protocol/base.ts +0 -55
- package/src/protocol/index.ts +0 -4
- package/src/protocol/rest.ts +0 -66
- package/src/protocol/webrtc.ts +0 -339
- package/src/protocol/websocket.ts +0 -108
- package/src/runtime.tsx +0 -217
- package/src/types.ts +0 -98
- package/tsconfig.node.json +0 -15
package/src/protocol/rest.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
'use client';
|
|
3
|
-
import { PersonaProtocolBase } from './base';
|
|
4
|
-
import { Message, PersonaResponse, Session, ProtocolStatus, PersonaProtocolBaseConfig } from '../types';
|
|
5
|
-
|
|
6
|
-
type PersonaRESTProtocolConfig = PersonaProtocolBaseConfig & {
|
|
7
|
-
apiUrl: string;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
class PersonaRESTProtocol extends PersonaProtocolBase {
|
|
11
|
-
status: ProtocolStatus;
|
|
12
|
-
autostart: boolean;
|
|
13
|
-
session: Session;
|
|
14
|
-
config: PersonaRESTProtocolConfig;
|
|
15
|
-
notify: boolean = true;
|
|
16
|
-
|
|
17
|
-
constructor(config: PersonaRESTProtocolConfig) {
|
|
18
|
-
super();
|
|
19
|
-
this.config = config;
|
|
20
|
-
this.status = 'disconnected';
|
|
21
|
-
this.autostart = true;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public getName(): string {
|
|
25
|
-
return 'rest';
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
public getPriority(): number {
|
|
29
|
-
return 0;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
public async connect(session: Session): Promise<Session> {
|
|
33
|
-
this.setStatus('connected');
|
|
34
|
-
return session;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public async disconnect(): Promise<void> {
|
|
38
|
-
this.setStatus('disconnected');
|
|
39
|
-
this.session = null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public async syncSession(session: Session): Promise<void> {
|
|
43
|
-
this.session = session;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public async send(message: Message): Promise<void> {
|
|
47
|
-
const { apiUrl, apiKey, agentId } = this.config;
|
|
48
|
-
const sessionId = this.session ?? 'new';
|
|
49
|
-
const input = message;
|
|
50
|
-
|
|
51
|
-
const response = await fetch(`${apiUrl}/agents/${agentId}/sessions/${sessionId}/messages`, {
|
|
52
|
-
body: JSON.stringify({ userMessage: input }),
|
|
53
|
-
method: 'POST',
|
|
54
|
-
headers: {
|
|
55
|
-
'Content-Type': 'application/json',
|
|
56
|
-
'x-fox-apikey': apiKey,
|
|
57
|
-
'x-persona-apikey': apiKey,
|
|
58
|
-
},
|
|
59
|
-
});
|
|
60
|
-
const personaResponse = (await response.json()) as PersonaResponse;
|
|
61
|
-
this.notifyMessages(personaResponse.response.messages);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export { PersonaRESTProtocol };
|
|
66
|
-
export type { PersonaRESTProtocolConfig };
|
package/src/protocol/webrtc.ts
DELETED
|
@@ -1,339 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
'use client';
|
|
3
|
-
import { PersonaProtocolBase } from './base';
|
|
4
|
-
import { Message, PersonaMessage, PersonaProtocolBaseConfig, ProtocolStatus, Session } from '../types';
|
|
5
|
-
|
|
6
|
-
type AudioAnalysisData = {
|
|
7
|
-
localAmplitude: number;
|
|
8
|
-
remoteAmplitude: number;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
type AudioVisualizerCallback = (data: AudioAnalysisData) => void;
|
|
12
|
-
|
|
13
|
-
type PersonaWebRTCMessageCallback = (data: MessageEvent) => void;
|
|
14
|
-
|
|
15
|
-
type PersonaWebRTCConfig = PersonaProtocolBaseConfig & {
|
|
16
|
-
webrtcUrl: string;
|
|
17
|
-
iceServers?: RTCIceServer[];
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
class PersonaWebRTCClient {
|
|
21
|
-
private config: PersonaWebRTCConfig;
|
|
22
|
-
private pc: RTCPeerConnection | null = null;
|
|
23
|
-
private ws: WebSocket | null = null;
|
|
24
|
-
private localStream: MediaStream | null = null;
|
|
25
|
-
private remoteStream: MediaStream = new MediaStream();
|
|
26
|
-
private audioCtx: AudioContext | null = null;
|
|
27
|
-
|
|
28
|
-
private localAnalyser: AnalyserNode | null = null;
|
|
29
|
-
private remoteAnalyser: AnalyserNode | null = null;
|
|
30
|
-
private analyzerFrame: number | null = null;
|
|
31
|
-
private dataChannel: RTCDataChannel | null = null;
|
|
32
|
-
|
|
33
|
-
private isConnected: boolean = false;
|
|
34
|
-
private visualizerCallbacks: AudioVisualizerCallback[] = [];
|
|
35
|
-
private messageCallbacks: PersonaWebRTCMessageCallback[] = [];
|
|
36
|
-
|
|
37
|
-
constructor(config: PersonaWebRTCConfig) {
|
|
38
|
-
this.config = config;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public async connect(session: Session): Promise<Session> {
|
|
42
|
-
if (this.isConnected) return;
|
|
43
|
-
|
|
44
|
-
this.isConnected = true;
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
this.localStream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
|
48
|
-
} catch (err) {
|
|
49
|
-
this.config.logger?.error('Error accessing microphone:', err);
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
this.pc = new RTCPeerConnection({
|
|
54
|
-
iceServers: this.config.iceServers || [
|
|
55
|
-
{
|
|
56
|
-
urls: 'stun:34.38.108.251:3478',
|
|
57
|
-
},
|
|
58
|
-
{
|
|
59
|
-
urls: 'turn:34.38.108.251:3478',
|
|
60
|
-
username: 'webrtc',
|
|
61
|
-
credential: 'webrtc',
|
|
62
|
-
},
|
|
63
|
-
],
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
this.localStream.getTracks().forEach((track) => {
|
|
67
|
-
this.pc!.addTrack(track, this.localStream!);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
this.pc.ontrack = (event) => {
|
|
71
|
-
event.streams[0].getTracks().forEach((track) => {
|
|
72
|
-
this.remoteStream.addTrack(track);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
if (!this.audioCtx) {
|
|
76
|
-
this._startAnalyzers();
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const remoteAudio = new Audio();
|
|
80
|
-
remoteAudio.srcObject = this.remoteStream;
|
|
81
|
-
remoteAudio.play().catch((e) => {
|
|
82
|
-
this.config.logger?.error('Error playing remote audio:', e);
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
this.pc.onicecandidate = (event) => {
|
|
87
|
-
if (event.candidate && this.ws?.readyState === WebSocket.OPEN) {
|
|
88
|
-
this.ws.send(
|
|
89
|
-
JSON.stringify({
|
|
90
|
-
type: 'CANDIDATE',
|
|
91
|
-
src: 'client',
|
|
92
|
-
payload: { candidate: event.candidate },
|
|
93
|
-
}),
|
|
94
|
-
);
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
|
|
98
|
-
this.pc.ondatachannel = (event) => {
|
|
99
|
-
const channel = event.channel;
|
|
100
|
-
channel.onmessage = (msg) => {
|
|
101
|
-
this.messageCallbacks.forEach((callback) => {
|
|
102
|
-
callback(msg);
|
|
103
|
-
});
|
|
104
|
-
};
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
this.ws = new WebSocket(this.config.webrtcUrl || 'wss://persona.applica.guru/api/webrtc');
|
|
108
|
-
this.ws.onopen = async () => {
|
|
109
|
-
const offer = await this.pc!.createOffer();
|
|
110
|
-
await this.pc!.setLocalDescription(offer);
|
|
111
|
-
|
|
112
|
-
const metadata = {
|
|
113
|
-
agentId: this.config.agentId,
|
|
114
|
-
sessionCode: session as string,
|
|
115
|
-
};
|
|
116
|
-
this.config.logger?.debug('Opening connection to WebRTC server: ', metadata);
|
|
117
|
-
|
|
118
|
-
const offerMessage = {
|
|
119
|
-
type: 'OFFER',
|
|
120
|
-
src: crypto.randomUUID?.() || 'client_' + Date.now(),
|
|
121
|
-
payload: {
|
|
122
|
-
sdp: {
|
|
123
|
-
sdp: offer.sdp,
|
|
124
|
-
type: offer.type,
|
|
125
|
-
},
|
|
126
|
-
connectionId: (Date.now() % 1000000).toString(),
|
|
127
|
-
metadata,
|
|
128
|
-
},
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
this.ws!.send(JSON.stringify(offerMessage));
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
this.ws.onmessage = async (event) => {
|
|
135
|
-
const data = JSON.parse(event.data);
|
|
136
|
-
if (data.type === 'ANSWER') {
|
|
137
|
-
await this.pc!.setRemoteDescription(new RTCSessionDescription(data.payload.sdp));
|
|
138
|
-
} else if (data.type === 'CANDIDATE') {
|
|
139
|
-
try {
|
|
140
|
-
await this.pc!.addIceCandidate(new RTCIceCandidate(data.payload.candidate));
|
|
141
|
-
} catch (err) {
|
|
142
|
-
this.config.logger?.error('Error adding ICE candidate:', err);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
this.ws.onclose = () => {
|
|
148
|
-
this._stopAnalyzers();
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
public async disconnect(): Promise<void> {
|
|
153
|
-
if (!this.isConnected) return;
|
|
154
|
-
|
|
155
|
-
this.isConnected = false;
|
|
156
|
-
|
|
157
|
-
if (this.ws?.readyState === WebSocket.OPEN) this.ws.close();
|
|
158
|
-
if (this.pc) this.pc.close();
|
|
159
|
-
if (this.localStream) {
|
|
160
|
-
this.localStream.getTracks().forEach((track) => track.stop());
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
this.remoteStream = new MediaStream();
|
|
164
|
-
if (this.audioCtx) {
|
|
165
|
-
await this.audioCtx.close();
|
|
166
|
-
this.audioCtx = null;
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
this._stopAnalyzers();
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
public addVisualizerCallback(callback: AudioVisualizerCallback): void {
|
|
173
|
-
this.visualizerCallbacks.push(callback);
|
|
174
|
-
}
|
|
175
|
-
public addMessageCallback(callback: PersonaWebRTCMessageCallback): void {
|
|
176
|
-
this.messageCallbacks.push(callback);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
public createDataChannel(label = 'messages'): void {
|
|
180
|
-
if (!this.pc) return;
|
|
181
|
-
this.dataChannel = this.pc.createDataChannel(label);
|
|
182
|
-
this.dataChannel.onopen = () => this.config.logger?.info('Data channel opened');
|
|
183
|
-
this.dataChannel.onmessage = (msg: MessageEvent) => {
|
|
184
|
-
this.messageCallbacks.forEach((callback) => {
|
|
185
|
-
callback(msg);
|
|
186
|
-
});
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
public sendMessage(message: string): void {
|
|
191
|
-
if (!this.dataChannel) {
|
|
192
|
-
this.config.logger?.warn('Data channel is not open, cannot send message');
|
|
193
|
-
return;
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
this.dataChannel.send(message);
|
|
197
|
-
this.config.logger?.info('Sent message:', message);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
private _startAnalyzers(): void {
|
|
201
|
-
if (!this.localStream || !this.remoteStream || this.visualizerCallbacks.length === 0) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
206
|
-
this.audioCtx = new (window.AudioContext || (window as any).webkitAudioContext)();
|
|
207
|
-
|
|
208
|
-
const localSource = this.audioCtx.createMediaStreamSource(this.localStream);
|
|
209
|
-
const remoteSource = this.audioCtx.createMediaStreamSource(this.remoteStream);
|
|
210
|
-
|
|
211
|
-
this.localAnalyser = this.audioCtx.createAnalyser();
|
|
212
|
-
this.remoteAnalyser = this.audioCtx.createAnalyser();
|
|
213
|
-
this.localAnalyser.fftSize = 256;
|
|
214
|
-
this.remoteAnalyser.fftSize = 256;
|
|
215
|
-
|
|
216
|
-
localSource.connect(this.localAnalyser);
|
|
217
|
-
remoteSource.connect(this.remoteAnalyser);
|
|
218
|
-
|
|
219
|
-
const loop = () => {
|
|
220
|
-
if (!this.localAnalyser || !this.remoteAnalyser || this.visualizerCallbacks.length === 0) {
|
|
221
|
-
return;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
const localArray = new Uint8Array(this.localAnalyser.frequencyBinCount);
|
|
225
|
-
const remoteArray = new Uint8Array(this.remoteAnalyser.frequencyBinCount);
|
|
226
|
-
|
|
227
|
-
this.localAnalyser.getByteFrequencyData(localArray);
|
|
228
|
-
this.remoteAnalyser.getByteFrequencyData(remoteArray);
|
|
229
|
-
|
|
230
|
-
const localAmp = localArray.reduce((a, b) => a + b, 0) / localArray.length;
|
|
231
|
-
const remoteAmp = remoteArray.reduce((a, b) => a + b, 0) / remoteArray.length;
|
|
232
|
-
|
|
233
|
-
if (this.visualizerCallbacks.length > 0) {
|
|
234
|
-
this.visualizerCallbacks.forEach((callback) => {
|
|
235
|
-
callback({
|
|
236
|
-
localAmplitude: localAmp,
|
|
237
|
-
remoteAmplitude: remoteAmp,
|
|
238
|
-
});
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
this.analyzerFrame = requestAnimationFrame(loop);
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
this.analyzerFrame = requestAnimationFrame(loop);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
private _stopAnalyzers(): void {
|
|
249
|
-
if (this.analyzerFrame) {
|
|
250
|
-
cancelAnimationFrame(this.analyzerFrame);
|
|
251
|
-
this.analyzerFrame = null;
|
|
252
|
-
}
|
|
253
|
-
this.localAnalyser = null;
|
|
254
|
-
this.remoteAnalyser = null;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
type PersonaWebRTCProtocolConfig = PersonaWebRTCConfig & {
|
|
259
|
-
autostart?: boolean;
|
|
260
|
-
};
|
|
261
|
-
|
|
262
|
-
class PersonaWebRTCProtocol extends PersonaProtocolBase {
|
|
263
|
-
status: ProtocolStatus;
|
|
264
|
-
session: Session;
|
|
265
|
-
autostart: boolean;
|
|
266
|
-
config: PersonaWebRTCProtocolConfig;
|
|
267
|
-
webRTCClient: PersonaWebRTCClient;
|
|
268
|
-
|
|
269
|
-
constructor(config: PersonaWebRTCProtocolConfig) {
|
|
270
|
-
super();
|
|
271
|
-
this.config = config;
|
|
272
|
-
this.status = 'disconnected';
|
|
273
|
-
this.session = null;
|
|
274
|
-
this.autostart = config?.autostart ?? false;
|
|
275
|
-
this.webRTCClient = new PersonaWebRTCClient(config);
|
|
276
|
-
this.webRTCClient.addMessageCallback((msg: MessageEvent) => {
|
|
277
|
-
config.logger?.debug('Received data message:', msg.data);
|
|
278
|
-
const data = JSON.parse(msg.data) as { type: 'message' | unknown; payload: PersonaMessage | unknown };
|
|
279
|
-
if (data.type === 'message') {
|
|
280
|
-
this.notifyMessage(data.payload as PersonaMessage);
|
|
281
|
-
}
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
public getName(): string {
|
|
286
|
-
return 'webrtc';
|
|
287
|
-
}
|
|
288
|
-
public getPriority(): number {
|
|
289
|
-
return 10;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
public async syncSession(session: Session): Promise<void> {
|
|
293
|
-
super.syncSession(session);
|
|
294
|
-
if (this.status === 'connected') {
|
|
295
|
-
await this.disconnect();
|
|
296
|
-
await this.connect(session);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
public async connect(session?: Session): Promise<Session> {
|
|
301
|
-
if (this.status === 'connected') {
|
|
302
|
-
return Promise.resolve(this.session);
|
|
303
|
-
}
|
|
304
|
-
this.session = session || this.session || 'new';
|
|
305
|
-
this.setStatus('connecting');
|
|
306
|
-
|
|
307
|
-
this.config.logger?.debug('Connecting to WebRTC with sessionId:', this.session);
|
|
308
|
-
await this.webRTCClient.connect(this.session);
|
|
309
|
-
this.setStatus('connected');
|
|
310
|
-
|
|
311
|
-
await this.webRTCClient.createDataChannel();
|
|
312
|
-
|
|
313
|
-
return this.session;
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
public async disconnect(): Promise<void> {
|
|
317
|
-
if (this.status === 'disconnected') {
|
|
318
|
-
this.config.logger?.warn('Already disconnected');
|
|
319
|
-
return Promise.resolve();
|
|
320
|
-
}
|
|
321
|
-
|
|
322
|
-
await this.webRTCClient.disconnect();
|
|
323
|
-
|
|
324
|
-
this.setStatus('disconnected');
|
|
325
|
-
this.config?.logger?.debug('Disconnected from WebRTC');
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
public send(message: Message): Promise<void> {
|
|
329
|
-
if (this.status !== 'connected') {
|
|
330
|
-
return Promise.reject(new Error('Not connected'));
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
this.webRTCClient.sendMessage(message as string);
|
|
334
|
-
return Promise.resolve();
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
export { PersonaWebRTCProtocol };
|
|
339
|
-
export type { PersonaWebRTCProtocolConfig, AudioVisualizerCallback, AudioAnalysisData };
|
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
'use client';
|
|
3
|
-
import { Message, PersonaMessage, PersonaProtocolBaseConfig, ProtocolStatus, Session } from '../types';
|
|
4
|
-
import { PersonaProtocolBase } from './base';
|
|
5
|
-
|
|
6
|
-
type PersonaWebSocketProtocolConfig = PersonaProtocolBaseConfig & {
|
|
7
|
-
webSocketUrl: string;
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
class PersonaWebSocketProtocol extends PersonaProtocolBase {
|
|
11
|
-
status: ProtocolStatus;
|
|
12
|
-
autostart: boolean;
|
|
13
|
-
session: Session;
|
|
14
|
-
config: PersonaWebSocketProtocolConfig;
|
|
15
|
-
webSocket: WebSocket | null;
|
|
16
|
-
|
|
17
|
-
constructor(config: PersonaWebSocketProtocolConfig) {
|
|
18
|
-
super();
|
|
19
|
-
this.config = config;
|
|
20
|
-
this.status = 'disconnected';
|
|
21
|
-
this.autostart = true;
|
|
22
|
-
this.session = null;
|
|
23
|
-
this.webSocket = null;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public getName(): string {
|
|
27
|
-
return 'websocket';
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
public getPriority(): number {
|
|
31
|
-
return 1;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public async syncSession(session: Session): Promise<void> {
|
|
35
|
-
this.config.logger?.debug('Syncing session with WebSocket protocol:', session);
|
|
36
|
-
this.session = session;
|
|
37
|
-
if (this.webSocket && this.status === 'connected') {
|
|
38
|
-
this.disconnect();
|
|
39
|
-
this.connect(session);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
public connect(session?: Session): Promise<Session> {
|
|
44
|
-
if (this.webSocket !== null && this.status === 'connected') {
|
|
45
|
-
return Promise.resolve(this.session);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const sid = session || this.session || 'new';
|
|
49
|
-
|
|
50
|
-
this.config.logger?.debug('Connecting to WebSocket with sessionId:', sid);
|
|
51
|
-
|
|
52
|
-
const apiKey = encodeURIComponent(this.config.apiKey);
|
|
53
|
-
const agentId = this.config.agentId;
|
|
54
|
-
const webSocketUrl = `${this.config.webSocketUrl}?sessionCode=${sid}&agentId=${agentId}&apiKey=${apiKey}`;
|
|
55
|
-
this.setStatus('connecting');
|
|
56
|
-
this.webSocket = new WebSocket(webSocketUrl);
|
|
57
|
-
this.webSocket.addEventListener('open', () => {
|
|
58
|
-
this.setStatus('connected');
|
|
59
|
-
});
|
|
60
|
-
this.webSocket.addEventListener('message', (event) => {
|
|
61
|
-
const data = JSON.parse(event.data) as { type: 'message' | unknown; payload: PersonaMessage | unknown };
|
|
62
|
-
|
|
63
|
-
if (data.type !== 'message') {
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
const message = data.payload as PersonaMessage & { thought?: string };
|
|
67
|
-
|
|
68
|
-
this.notifyMessage(message?.thought ? { role: 'assistant', type: 'reasoning', text: message.thought } : message);
|
|
69
|
-
});
|
|
70
|
-
this.webSocket.addEventListener('close', () => {
|
|
71
|
-
this.setStatus('disconnected');
|
|
72
|
-
this.webSocket = null;
|
|
73
|
-
this.config.logger?.warn('WebSocket connection closed');
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
this.webSocket.addEventListener('error', (error) => {
|
|
77
|
-
this.setStatus('disconnected');
|
|
78
|
-
this.webSocket = null;
|
|
79
|
-
this.config.logger?.error('WebSocket error', error);
|
|
80
|
-
|
|
81
|
-
// TODO: Implement reconnection logic
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
return Promise.resolve(sid);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
public disconnect(): Promise<void> {
|
|
88
|
-
this.config.logger?.debug('Disconnecting WebSocket');
|
|
89
|
-
if (this.webSocket && this.status === 'connected') {
|
|
90
|
-
this.webSocket.close();
|
|
91
|
-
this.setStatus('disconnected');
|
|
92
|
-
this.webSocket = null;
|
|
93
|
-
}
|
|
94
|
-
return Promise.resolve();
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
public send(message: Message): Promise<void> {
|
|
98
|
-
if (this.webSocket && this.status === 'connected') {
|
|
99
|
-
this.webSocket.send(JSON.stringify({ type: 'request', payload: message }));
|
|
100
|
-
return Promise.resolve();
|
|
101
|
-
} else {
|
|
102
|
-
return Promise.reject(new Error('WebSocket is not connected'));
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export { PersonaWebSocketProtocol };
|
|
108
|
-
export type { PersonaWebSocketProtocolConfig };
|