@decartai/sdk 0.0.68 → 0.1.1
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 +49 -9
- package/dist/files/client.d.ts +33 -0
- package/dist/files/client.js +65 -0
- package/dist/files/types.d.ts +22 -0
- package/dist/files/types.js +4 -0
- package/dist/index.d.ts +19 -4
- package/dist/index.js +50 -28
- package/dist/process/client.js +1 -3
- package/dist/process/request.js +1 -3
- package/dist/queue/client.js +1 -3
- package/dist/queue/polling.js +1 -2
- package/dist/queue/request.js +1 -3
- package/dist/realtime/client.d.ts +23 -11
- package/dist/realtime/client.js +85 -156
- package/dist/realtime/config-realtime.js +49 -0
- package/dist/realtime/event-buffer.js +1 -3
- package/dist/realtime/initial-state-gate.js +21 -0
- package/dist/realtime/media-channel.js +82 -0
- package/dist/realtime/methods.js +25 -43
- package/dist/realtime/mirror-stream.js +1 -2
- package/dist/realtime/observability/diagnostics.d.ts +14 -53
- package/dist/realtime/observability/livekit-stats-provider.js +25 -0
- package/dist/realtime/observability/realtime-observability.js +70 -6
- package/dist/realtime/observability/telemetry-reporter.js +9 -28
- package/dist/realtime/observability/webrtc-stats.d.ts +5 -4
- package/dist/realtime/observability/webrtc-stats.js +3 -5
- package/dist/realtime/signaling-channel.js +302 -0
- package/dist/realtime/stream-session.js +257 -0
- package/dist/realtime/subscribe-client.d.ts +2 -3
- package/dist/realtime/subscribe-client.js +115 -11
- package/dist/realtime/types.d.ts +25 -1
- package/dist/shared/model.d.ts +11 -1
- package/dist/shared/model.js +51 -14
- package/dist/shared/request.js +1 -3
- package/dist/shared/types.js +1 -3
- package/dist/tokens/client.js +1 -3
- package/dist/utils/env.js +1 -2
- package/dist/utils/errors.d.ts +3 -0
- package/dist/utils/errors.js +4 -2
- package/dist/utils/logger.js +1 -2
- package/dist/utils/media.js +43 -0
- package/dist/utils/platform.js +13 -0
- package/dist/utils/user-agent.js +1 -3
- package/dist/version.js +1 -2
- package/package.json +2 -1
- package/dist/realtime/webrtc-connection.js +0 -500
- package/dist/realtime/webrtc-manager.js +0 -210
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { WebRTCConnection } from "./webrtc-connection.js";
|
|
2
|
-
import pRetry, { AbortError } from "p-retry";
|
|
3
|
-
|
|
4
|
-
//#region src/realtime/webrtc-manager.ts
|
|
5
|
-
const PERMANENT_ERRORS = [
|
|
6
|
-
"permission denied",
|
|
7
|
-
"not allowed",
|
|
8
|
-
"invalid session",
|
|
9
|
-
"401",
|
|
10
|
-
"invalid api key",
|
|
11
|
-
"unauthorized"
|
|
12
|
-
];
|
|
13
|
-
const CONNECTION_TIMEOUT = 6e4 * 5;
|
|
14
|
-
const RETRY_OPTIONS = {
|
|
15
|
-
retries: 5,
|
|
16
|
-
factor: 2,
|
|
17
|
-
minTimeout: 1e3,
|
|
18
|
-
maxTimeout: 1e4
|
|
19
|
-
};
|
|
20
|
-
var WebRTCManager = class {
|
|
21
|
-
connection;
|
|
22
|
-
config;
|
|
23
|
-
logger;
|
|
24
|
-
observability;
|
|
25
|
-
localStream = null;
|
|
26
|
-
subscribeMode = false;
|
|
27
|
-
managerState = "disconnected";
|
|
28
|
-
hasConnected = false;
|
|
29
|
-
isReconnecting = false;
|
|
30
|
-
intentionalDisconnect = false;
|
|
31
|
-
reconnectGeneration = 0;
|
|
32
|
-
statsProviderConnection = null;
|
|
33
|
-
constructor(config) {
|
|
34
|
-
this.config = config;
|
|
35
|
-
this.logger = config.logger ?? {
|
|
36
|
-
debug() {},
|
|
37
|
-
info() {},
|
|
38
|
-
warn() {},
|
|
39
|
-
error() {}
|
|
40
|
-
};
|
|
41
|
-
this.observability = config.observability;
|
|
42
|
-
this.connection = new WebRTCConnection({
|
|
43
|
-
onRemoteStream: config.onRemoteStream,
|
|
44
|
-
onStateChange: (state) => this.handleConnectionStateChange(state),
|
|
45
|
-
onError: config.onError,
|
|
46
|
-
customizeOffer: config.customizeOffer,
|
|
47
|
-
vp8MinBitrate: config.vp8MinBitrate,
|
|
48
|
-
vp8StartBitrate: config.vp8StartBitrate,
|
|
49
|
-
initialImage: config.initialImage,
|
|
50
|
-
initialPrompt: config.initialPrompt,
|
|
51
|
-
logger: this.logger,
|
|
52
|
-
observability: this.observability
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
emitState(state) {
|
|
56
|
-
if (this.managerState !== state) {
|
|
57
|
-
this.managerState = state;
|
|
58
|
-
if (state === "connected" || state === "generating") this.hasConnected = true;
|
|
59
|
-
this.config.onConnectionStateChange?.(state);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
syncStatsProvider() {
|
|
63
|
-
const pc = this.getPeerConnection();
|
|
64
|
-
const isLive = this.managerState === "connected" || this.managerState === "generating";
|
|
65
|
-
if (isLive && pc && pc !== this.statsProviderConnection) {
|
|
66
|
-
this.statsProviderConnection = pc;
|
|
67
|
-
this.observability.setStatsProvider(pc);
|
|
68
|
-
} else if (!isLive && this.statsProviderConnection) {
|
|
69
|
-
this.statsProviderConnection = null;
|
|
70
|
-
this.observability.setStatsProvider(null);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
handleConnectionStateChange(state) {
|
|
74
|
-
if (this.intentionalDisconnect) {
|
|
75
|
-
this.emitState("disconnected");
|
|
76
|
-
this.syncStatsProvider();
|
|
77
|
-
return;
|
|
78
|
-
}
|
|
79
|
-
if (this.isReconnecting) {
|
|
80
|
-
if (state === "connected" || state === "generating") {
|
|
81
|
-
this.isReconnecting = false;
|
|
82
|
-
this.emitState(state);
|
|
83
|
-
this.syncStatsProvider();
|
|
84
|
-
}
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (state === "disconnected" && !this.intentionalDisconnect && this.hasConnected) {
|
|
88
|
-
this.reconnect();
|
|
89
|
-
return;
|
|
90
|
-
}
|
|
91
|
-
this.emitState(state);
|
|
92
|
-
this.syncStatsProvider();
|
|
93
|
-
}
|
|
94
|
-
async reconnect() {
|
|
95
|
-
if (this.isReconnecting || this.intentionalDisconnect) return;
|
|
96
|
-
if (!this.subscribeMode && !this.localStream) return;
|
|
97
|
-
const reconnectGeneration = ++this.reconnectGeneration;
|
|
98
|
-
this.isReconnecting = true;
|
|
99
|
-
this.emitState("reconnecting");
|
|
100
|
-
this.observability.setStatsProvider(null);
|
|
101
|
-
this.statsProviderConnection = null;
|
|
102
|
-
const reconnectStart = performance.now();
|
|
103
|
-
try {
|
|
104
|
-
let attemptCount = 0;
|
|
105
|
-
await pRetry(async () => {
|
|
106
|
-
attemptCount++;
|
|
107
|
-
if (this.intentionalDisconnect || reconnectGeneration !== this.reconnectGeneration) throw new AbortError("Reconnect cancelled");
|
|
108
|
-
if (!this.subscribeMode && !this.localStream) throw new AbortError("Reconnect cancelled: no local stream");
|
|
109
|
-
this.connection.cleanup();
|
|
110
|
-
await this.connection.connect(this.config.webrtcUrl, this.localStream, CONNECTION_TIMEOUT, this.config.integration);
|
|
111
|
-
if (this.intentionalDisconnect || reconnectGeneration !== this.reconnectGeneration) {
|
|
112
|
-
this.connection.cleanup();
|
|
113
|
-
throw new AbortError("Reconnect cancelled");
|
|
114
|
-
}
|
|
115
|
-
}, {
|
|
116
|
-
...RETRY_OPTIONS,
|
|
117
|
-
onFailedAttempt: (error) => {
|
|
118
|
-
if (this.intentionalDisconnect || reconnectGeneration !== this.reconnectGeneration) return;
|
|
119
|
-
this.logger.warn("Reconnect attempt failed", {
|
|
120
|
-
error: error.message,
|
|
121
|
-
attempt: error.attemptNumber
|
|
122
|
-
});
|
|
123
|
-
this.observability.diagnostic("reconnect", {
|
|
124
|
-
attempt: error.attemptNumber,
|
|
125
|
-
maxAttempts: RETRY_OPTIONS.retries + 1,
|
|
126
|
-
durationMs: performance.now() - reconnectStart,
|
|
127
|
-
success: false,
|
|
128
|
-
error: error.message
|
|
129
|
-
});
|
|
130
|
-
this.connection.cleanup();
|
|
131
|
-
},
|
|
132
|
-
shouldRetry: (error) => {
|
|
133
|
-
if (this.intentionalDisconnect || reconnectGeneration !== this.reconnectGeneration) return false;
|
|
134
|
-
const msg = error.message.toLowerCase();
|
|
135
|
-
return !PERMANENT_ERRORS.some((err) => msg.includes(err));
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
this.observability.diagnostic("reconnect", {
|
|
139
|
-
attempt: attemptCount,
|
|
140
|
-
maxAttempts: RETRY_OPTIONS.retries + 1,
|
|
141
|
-
durationMs: performance.now() - reconnectStart,
|
|
142
|
-
success: true
|
|
143
|
-
});
|
|
144
|
-
} catch (error) {
|
|
145
|
-
this.isReconnecting = false;
|
|
146
|
-
if (this.intentionalDisconnect || reconnectGeneration !== this.reconnectGeneration) return;
|
|
147
|
-
this.emitState("disconnected");
|
|
148
|
-
this.config.onError?.(error instanceof Error ? error : new Error(String(error)));
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
async connect(localStream) {
|
|
152
|
-
this.localStream = localStream;
|
|
153
|
-
this.subscribeMode = localStream === null;
|
|
154
|
-
this.intentionalDisconnect = false;
|
|
155
|
-
this.hasConnected = false;
|
|
156
|
-
this.isReconnecting = false;
|
|
157
|
-
this.reconnectGeneration += 1;
|
|
158
|
-
this.emitState("connecting");
|
|
159
|
-
return pRetry(async () => {
|
|
160
|
-
if (this.intentionalDisconnect) throw new AbortError("Connect cancelled");
|
|
161
|
-
await this.connection.connect(this.config.webrtcUrl, localStream, CONNECTION_TIMEOUT, this.config.integration);
|
|
162
|
-
return true;
|
|
163
|
-
}, {
|
|
164
|
-
...RETRY_OPTIONS,
|
|
165
|
-
onFailedAttempt: (error) => {
|
|
166
|
-
this.logger.warn("Connection attempt failed", {
|
|
167
|
-
error: error.message,
|
|
168
|
-
attempt: error.attemptNumber
|
|
169
|
-
});
|
|
170
|
-
this.connection.cleanup();
|
|
171
|
-
},
|
|
172
|
-
shouldRetry: (error) => {
|
|
173
|
-
if (this.intentionalDisconnect) return false;
|
|
174
|
-
const msg = error.message.toLowerCase();
|
|
175
|
-
return !PERMANENT_ERRORS.some((err) => msg.includes(err));
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
sendMessage(message) {
|
|
180
|
-
return this.connection.send(message);
|
|
181
|
-
}
|
|
182
|
-
cleanup() {
|
|
183
|
-
this.intentionalDisconnect = true;
|
|
184
|
-
this.isReconnecting = false;
|
|
185
|
-
this.reconnectGeneration += 1;
|
|
186
|
-
this.connection.cleanup();
|
|
187
|
-
this.localStream = null;
|
|
188
|
-
this.statsProviderConnection = null;
|
|
189
|
-
this.observability.setStatsProvider(null);
|
|
190
|
-
this.emitState("disconnected");
|
|
191
|
-
}
|
|
192
|
-
isConnected() {
|
|
193
|
-
return this.managerState === "connected" || this.managerState === "generating";
|
|
194
|
-
}
|
|
195
|
-
getConnectionState() {
|
|
196
|
-
return this.managerState;
|
|
197
|
-
}
|
|
198
|
-
getPeerConnection() {
|
|
199
|
-
return this.connection.getPeerConnection();
|
|
200
|
-
}
|
|
201
|
-
getWebsocketMessageEmitter() {
|
|
202
|
-
return this.connection.websocketMessagesEmitter;
|
|
203
|
-
}
|
|
204
|
-
setImage(imageBase64, options) {
|
|
205
|
-
return this.connection.setImageBase64(imageBase64, options);
|
|
206
|
-
}
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
//#endregion
|
|
210
|
-
export { WebRTCManager };
|